-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclaude-slack
More file actions
executable file
·121 lines (95 loc) · 3.87 KB
/
Copy pathclaude-slack
File metadata and controls
executable file
·121 lines (95 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
"""
claude-slack — Slack↔Terminal bridge wrapper for Claude Code.
Drop-in replacement for `claude`. Creates a named pipe session, exports
CLAUDE_BRIDGE_SESSION and CLAUDE_BRIDGE_CHANNEL_ID, and launches `claude`
normally (stdin = your terminal). When Claude needs input it reads from the
named pipe explicitly via a Bash tool command; Slack button clicks write the
answer to the pipe so the read unblocks.
"""
from __future__ import annotations
import atexit
import json
import os
import signal
import subprocess
import sys
import uuid
# Add Shellack repo to path so orchestrator_config and tools are importable
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, SCRIPT_DIR)
from dotenv import load_dotenv
load_dotenv(os.path.join(SCRIPT_DIR, ".env"))
from tools.slack_bridge import detect_channel_id, post_session_start
# ---------------------------------------------------------------------------
# Session setup
# ---------------------------------------------------------------------------
proc_handle = None # initialised before signal handlers so SIGTERM is safe
session_id = str(uuid.uuid4())
session_dir = "/tmp/claude_bridge"
pipe_path = os.path.join(session_dir, session_id)
session_file = os.path.join(session_dir, f"{session_id}.json")
os.makedirs(session_dir, exist_ok=True)
os.mkfifo(pipe_path)
# macOS: open read-end first (O_NONBLOCK succeeds immediately without a writer on BSD).
# Then open write-end (O_NONBLOCK now succeeds because a reader exists).
# Linux is the opposite — this order is correct for macOS.
read_fd = os.open(pipe_path, os.O_RDONLY | os.O_NONBLOCK)
write_fd = os.open(pipe_path, os.O_WRONLY | os.O_NONBLOCK)
# Close the read-end in this process — Claude Code will open it via Bash tool
# when it needs to block-read an answer from Slack. write_fd stays open as the
# keep-alive so SlackClaw can write to the pipe without getting EPIPE.
os.close(read_fd)
channel_id, project_name = detect_channel_id()
with open(session_file, "w") as f:
json.dump(
{"pipe": pipe_path, "channel_id": channel_id, "project_name": project_name},
f,
)
# ---------------------------------------------------------------------------
# Cleanup
# ---------------------------------------------------------------------------
def cleanup() -> None:
try:
os.close(write_fd)
except Exception:
pass
for path in [pipe_path, session_file]:
try:
os.unlink(path)
except Exception:
pass
atexit.register(cleanup)
def _sigterm_handler(*_) -> None:
# Forward SIGTERM to claude subprocess so it can save state, then clean up
if proc_handle is not None:
try:
proc_handle.send_signal(signal.SIGTERM)
except Exception:
pass
cleanup()
sys.exit(0)
signal.signal(signal.SIGTERM, _sigterm_handler)
# ---------------------------------------------------------------------------
# Announce session to Slack
# ---------------------------------------------------------------------------
try:
post_session_start(channel_id, project_name)
except Exception as e:
print(f"[claude-slack] Could not post session-start to Slack: {e}", file=sys.stderr)
# ---------------------------------------------------------------------------
# Launch claude
# ---------------------------------------------------------------------------
env = os.environ.copy()
env["CLAUDE_BRIDGE_SESSION"] = session_id
env["CLAUDE_BRIDGE_CHANNEL_ID"] = channel_id
# Claude inherits stdin from this process (the real terminal TTY) so its TUI
# works normally. write_fd is inherited by the child on macOS (O_CLOEXEC absent
# by default) — that's intentional: the child keeps the pipe open so SlackClaw
# can write answers without getting EPIPE.
proc_handle = subprocess.Popen(
["claude"] + sys.argv[1:],
env=env,
)
proc_handle.wait()
sys.exit(proc_handle.returncode)