Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
163 lines (147 loc) · 4.83 KB
/
Copy pathindex.ts
File metadata and controls
163 lines (147 loc) · 4.83 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
import { resolve } from "node:path";
import { pathToFileURL } from "node:url";
import {
accessTokenFromEnv,
query,
type HookCallback,
type HookEvent,
type SDKMessage,
} from "@qoder-ai/qoder-agent-sdk";
const SAFE_STATUS_COMMANDS = new Set([
"git status --short",
"git status --porcelain",
"git --no-pager status --short",
"git --no-pager status --porcelain",
]);
const 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.";
const eventCounts = new Map<HookEvent, number>();
function record(event: HookEvent, detail: string): void {
eventCounts.set(event, (eventCounts.get(event) ?? 0) + 1);
console.log(`[hook:${event}] ${detail}`);
}
const onSessionStart: HookCallback = async (input) => {
if (input.hook_event_name !== "SessionStart") return {};
record("SessionStart", `source=${input.source}`);
return {
hookSpecificOutput: {
hookEventName: "SessionStart",
additionalContext:
"This application is running a read-only repository inspection session.",
},
};
};
const onPromptSubmit: HookCallback = async (input) => {
if (input.hook_event_name !== "UserPromptSubmit") return {};
record("UserPromptSubmit", `received ${input.prompt.length} characters`);
return {
hookSpecificOutput: {
hookEventName: "UserPromptSubmit",
additionalContext:
"Report only observed repository facts and do not propose file changes.",
},
};
};
const beforeBash: HookCallback = async (input) => {
if (input.hook_event_name !== "PreToolUse" || input.tool_name !== "Bash") {
return {};
}
const toolInput = input.tool_input as { command?: unknown };
const command =
typeof toolInput.command === "string" ? toolInput.command.trim() : "";
if (!SAFE_STATUS_COMMANDS.has(command)) {
record("PreToolUse", `denied Bash: ${command || "<missing command>"}`);
return {
hookSpecificOutput: {
hookEventName: "PreToolUse",
permissionDecision: "deny",
permissionDecisionReason:
"This sample permits only a read-only git status command.",
},
};
}
record("PreToolUse", `allowed Bash: ${command}`);
return {
hookSpecificOutput: {
hookEventName: "PreToolUse",
permissionDecision: "allow",
permissionDecisionReason: "The command matches the read-only policy.",
},
};
};
const afterTool: HookCallback = async (input) => {
if (input.hook_event_name !== "PostToolUse") return {};
record("PostToolUse", `completed ${input.tool_name}`);
if (input.tool_name !== "Bash") return {};
return {
hookSpecificOutput: {
hookEventName: "PostToolUse",
additionalContext:
"The host application audited the git status command successfully.",
},
};
};
const onStop: HookCallback = async (input) => {
if (input.hook_event_name !== "Stop") return {};
record("Stop", "assistant finished generating");
return {};
};
function assistantText(message: SDKMessage): string[] {
if (message.type !== "assistant") return [];
const content = message.message.content;
if (!Array.isArray(content)) return [];
return content.flatMap((block) =>
block.type === "text" ? [block.text] : [],
);
}
export async function run(workspace: string, prompt: string): Promise<void> {
eventCounts.clear();
const stream = query({
prompt,
options: {
auth: accessTokenFromEnv(),
cwd: workspace,
model: "auto",
tools: ["Read", "Bash"],
allowedTools: ["Read", "Bash"],
maxTurns: 4,
hooks: {
SessionStart: [{ hooks: [onSessionStart] }],
UserPromptSubmit: [{ hooks: [onPromptSubmit] }],
PreToolUse: [{ matcher: "Bash", hooks: [beforeBash] }],
PostToolUse: [{ hooks: [afterTool] }],
Stop: [{ hooks: [onStop] }],
},
},
});
let completed = false;
try {
for await (const message of stream) {
for (const text of assistantText(message)) console.log(text);
if (message.type === "result") {
if (message.subtype !== "success") {
throw new Error(message.errors?.join("\n") || message.subtype);
}
completed = true;
}
}
} finally {
await stream.close();
}
if (!completed) throw new Error("The query ended without a success result.");
console.log("\n\nHook summary:");
for (const [event, count] of eventCounts) {
console.log(`- ${event}: ${count}`);
}
}
async function main(): Promise<void> {
const workspace = resolve(process.argv[2] ?? process.cwd());
const prompt = process.argv.slice(3).join(" ") || DEFAULT_PROMPT;
await run(workspace, prompt);
}
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
main().catch((error: unknown) => {
console.error(error instanceof Error ? error.message : error);
process.exitCode = 1;
});
}