-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock_daemon.py
More file actions
146 lines (122 loc) · 4.33 KB
/
Copy pathmock_daemon.py
File metadata and controls
146 lines (122 loc) · 4.33 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python3
"""
Mock 'claude' binary for testing the Daemon.
Handles BOTH modes:
- One-shot (default, used by non-streaming Daemon): reads prompt from
-p arg OR stdin, emits a single scripted response as stream-json, exits.
- Streaming (--input-format stream-json): stays alive, reads one JSON
message per stdin line, emits a scripted response per message, loops
until stdin closes.
Response logic (identical in both modes):
- If message contains "GOAL:", decompose into 3 subtasks (status=waiting)
- If message contains "OUTCOMES:", mark done (status=done)
- Otherwise emit a default waiting response
Accepts and ignores all other Claude Code flags. Drop-in for `claude`
for testing purposes only.
"""
from __future__ import annotations
import argparse
import json
import sys
import time
import uuid
def emit(obj: dict) -> None:
sys.stdout.write(json.dumps(obj) + "\n")
sys.stdout.flush()
def emit_init(session_id: str) -> None:
emit({
"type": "system",
"subtype": "init",
"session_id": session_id,
"tools": [],
"cwd": ".",
})
def emit_assistant_text(text: str) -> None:
emit({
"type": "assistant",
"message": {
"role": "assistant",
"content": [{"type": "text", "text": text}],
},
})
def emit_result(session_id: str, duration_ms: int) -> None:
emit({
"type": "result",
"subtype": "success",
"session_id": session_id,
"duration_ms": duration_ms,
"is_error": False,
})
def decide_response(prompt: str) -> dict:
if "GOAL:" in prompt:
return {
"thinking": "Breaking the goal into three concrete subtasks",
"chat": "I'll handle this as three parallel workstreams.",
"actions": [
{"type": "spawn", "task": "subtask alpha (mock)"},
{"type": "spawn", "task": "subtask beta (mock)"},
{"type": "spawn", "task": "subtask gamma (mock)"},
],
"status": "waiting",
}
if "OUTCOMES:" in prompt:
return {
"thinking": "All three subtasks completed; goal is satisfied",
"chat": "All three constructs finished. Goal achieved.",
"actions": [],
"status": "done",
}
return {
"thinking": "Awaiting further input",
"chat": None,
"actions": [],
"status": "waiting",
}
def answer_prompt(prompt: str, session_id: str) -> None:
"""Emit one full turn's worth of events for a single prompt."""
response = decide_response(prompt)
assistant_text = f"```json\n{json.dumps(response, indent=2)}\n```"
emit_assistant_text(assistant_text)
time.sleep(0.05)
emit_result(session_id, 200)
def main() -> int:
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("-p", "--print", dest="prompt", default="", nargs="?")
parser.add_argument("--resume", dest="resume", default=None)
parser.add_argument("--input-format", dest="input_format", default="text")
args, _unknown = parser.parse_known_args()
session_id = args.resume or f"mock-dm-{uuid.uuid4().hex[:12]}"
emit_init(session_id)
time.sleep(0.1)
if args.input_format == "stream-json":
# Streaming mode: one JSON message per line, loop until EOF.
for raw_line in sys.stdin:
line = raw_line.strip()
if not line:
continue
try:
msg = json.loads(line)
except json.JSONDecodeError:
continue
content = msg.get("message", {}).get("content", "")
if isinstance(content, list):
# content can be a list of blocks; concat text ones
content = "".join(
b.get("text", "") for b in content
if isinstance(b, dict) and b.get("type") == "text"
)
answer_prompt(str(content), session_id)
return 0
# One-shot mode: prompt from stdin if piped, else -p arg
prompt = args.prompt or ""
if not sys.stdin.isatty():
try:
stdin_data = sys.stdin.read()
if stdin_data.strip():
prompt = stdin_data
except Exception:
pass
answer_prompt(prompt, session_id)
return 0
if __name__ == "__main__":
sys.exit(main())