-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
181 lines (157 loc) · 5.63 KB
/
Copy pathmain.py
File metadata and controls
181 lines (157 loc) · 5.63 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import argparse
import asyncio
from pathlib import Path
from typing import Any
from qoder_agent_sdk import (
AssistantMessage,
HookContext,
HookInput,
HookJSONOutput,
HookMatcher,
QoderAgentOptions,
QoderSDKClient,
ResultMessage,
TextBlock,
access_token_from_env,
)
SAFE_STATUS_COMMANDS = {
"git status --short",
"git status --porcelain",
"git --no-pager status --short",
"git --no-pager status --porcelain",
}
DEFAULT_PROMPT = (
"Run exactly `git --no-pager status --short`, read README.md, then explain "
"in two sentences what this repository contains and whether it has "
"uncommitted changes."
)
event_counts: dict[str, int] = {}
def record(event: str, detail: str) -> None:
event_counts[event] = event_counts.get(event, 0) + 1
print(f"[hook:{event}] {detail}")
async def on_session_start(
input_data: HookInput, _tool_use_id: str | None, _context: HookContext
) -> HookJSONOutput:
if input_data["hook_event_name"] != "SessionStart":
return {}
record("SessionStart", f"source={input_data['source']}")
return {
"hookSpecificOutput": {
"hookEventName": "SessionStart",
"additionalContext": (
"This application is running a read-only repository inspection session."
),
}
}
async def on_prompt_submit(
input_data: HookInput, _tool_use_id: str | None, _context: HookContext
) -> HookJSONOutput:
if input_data["hook_event_name"] != "UserPromptSubmit":
return {}
record("UserPromptSubmit", f"received {len(input_data['prompt'])} characters")
return {
"hookSpecificOutput": {
"hookEventName": "UserPromptSubmit",
"additionalContext": (
"Report only observed repository facts and do not propose file changes."
),
}
}
async def before_bash(
input_data: HookInput, _tool_use_id: str | None, _context: HookContext
) -> HookJSONOutput:
if (
input_data["hook_event_name"] != "PreToolUse"
or input_data["tool_name"] != "Bash"
):
return {}
raw_command: Any = input_data["tool_input"].get("command")
command = raw_command.strip() if isinstance(raw_command, str) else ""
if command not in SAFE_STATUS_COMMANDS:
record("PreToolUse", f"denied Bash: {command or '<missing command>'}")
return {
"hookSpecificOutput": {
"hookEventName": "PreToolUse",
"permissionDecision": "deny",
"permissionDecisionReason": (
"This sample permits only a read-only git status command."
),
}
}
record("PreToolUse", f"allowed Bash: {command}")
return {
"hookSpecificOutput": {
"hookEventName": "PreToolUse",
"permissionDecision": "allow",
"permissionDecisionReason": ("The command matches the read-only policy."),
}
}
async def after_tool(
input_data: HookInput, _tool_use_id: str | None, _context: HookContext
) -> HookJSONOutput:
if input_data["hook_event_name"] != "PostToolUse":
return {}
record("PostToolUse", f"completed {input_data['tool_name']}")
if input_data["tool_name"] != "Bash":
return {}
return {
"hookSpecificOutput": {
"hookEventName": "PostToolUse",
"additionalContext": (
"The host application audited the git status command successfully."
),
}
}
async def on_stop(
input_data: HookInput, _tool_use_id: str | None, _context: HookContext
) -> HookJSONOutput:
if input_data["hook_event_name"] != "Stop":
return {}
record("Stop", "assistant finished generating")
return {}
async def run(workspace: Path, prompt: str) -> None:
event_counts.clear()
options = QoderAgentOptions(
auth=access_token_from_env(),
cwd=workspace,
model="auto",
tools=["Read", "Bash"],
allowed_tools=["Read", "Bash"],
max_turns=4,
hooks={
"SessionStart": [HookMatcher(hooks=[on_session_start])],
"UserPromptSubmit": [HookMatcher(hooks=[on_prompt_submit])],
"PreToolUse": [HookMatcher(matcher="Bash", hooks=[before_bash])],
"PostToolUse": [HookMatcher(hooks=[after_tool])],
"Stop": [HookMatcher(hooks=[on_stop])],
},
)
completed = False
async with QoderSDKClient(options=options) as client:
await client.query(prompt)
async for message in client.receive_response():
if isinstance(message, AssistantMessage):
for block in message.content:
if isinstance(block, TextBlock):
print(block.text, flush=True)
elif isinstance(message, ResultMessage):
if message.subtype != "success":
raise RuntimeError("\n".join(message.errors or [message.subtype]))
completed = True
if not completed:
raise RuntimeError("The query ended without a success result.")
print("\n\nHook summary:")
for event, count in event_counts.items():
print(f"- {event}: {count}")
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("workspace", nargs="?", type=Path, default=Path.cwd())
parser.add_argument("prompt", nargs="*")
args = parser.parse_args()
prompt = " ".join(args.prompt) or DEFAULT_PROMPT
asyncio.run(run(args.workspace.resolve(), prompt))
if __name__ == "__main__":
try:
main()
except (RuntimeError, OSError, KeyboardInterrupt) as error:
raise SystemExit(str(error)) from error