-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.ts
More file actions
88 lines (78 loc) · 2.52 KB
/
Copy pathindex.ts
File metadata and controls
88 lines (78 loc) · 2.52 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
import { resolve } from "node:path";
import { pathToFileURL } from "node:url";
import {
accessTokenFromEnv,
query,
type CanUseTool,
type SDKMessage,
} from "@qoder-ai/qoder-agent-sdk";
const ALLOWED_COMMANDS = new Set([
"git status --short",
"git status --porcelain",
"git --no-pager status --short",
"git --no-pager status --porcelain",
]);
export const authorizeTool: CanUseTool = async (toolName, input) => {
if (toolName !== "Bash") {
return { behavior: "deny", message: `Tool ${toolName} is not permitted.` };
}
const command = typeof input.command === "string" ? input.command.trim() : "";
if (!ALLOWED_COMMANDS.has(command)) {
console.log(`[permission] denied Bash: ${command || "<missing command>"}`);
return {
behavior: "deny",
message: "Only a read-only git status command is permitted.",
};
}
console.log(`[permission] allowed Bash: ${command}`);
return { behavior: "allow", updatedInput: input };
};
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): Promise<void> {
const stream = query({
prompt:
"Run exactly `git --no-pager status --short`, then explain the repository status in one sentence. Do not modify any files.",
options: {
auth: accessTokenFromEnv(),
cwd: workspace,
model: "auto",
tools: ["Read", "Glob", "Grep", "Bash"],
allowedTools: ["Read", "Glob", "Grep"],
permissionMode: "default",
canUseTool: authorizeTool,
maxTurns: 3,
},
});
let completed = false;
try {
for await (const message of stream) {
for (const text of assistantText(message)) process.stdout.write(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.");
process.stdout.write("\n");
}
async function main(): Promise<void> {
await run(resolve(process.argv[2] ?? process.cwd()));
}
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
main().catch((error: unknown) => {
console.error(error instanceof Error ? error.message : error);
process.exitCode = 1;
});
}