-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathsdk-demo.ts
More file actions
48 lines (44 loc) · 1.3 KB
/
Copy pathsdk-demo.ts
File metadata and controls
48 lines (44 loc) · 1.3 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
import { query, tool } from "../dist/exports.js";
// A custom tool the agent can call
const greet = tool("greet", "Greet someone by name", {
properties: { name: { type: "string", description: "Name to greet" } },
required: ["name"],
}, async (args) => `Hello, ${args.name}! Welcome to Gitagent.`);
async function main() {
console.log("Starting SDK demo...\n");
for await (const msg of query({
prompt: "Use the greet tool to greet Zeus, then say something short and fun.",
dir: process.cwd(),
model: "openai:gpt-4o-mini",
tools: [greet],
hooks: {
preToolUse: async (ctx) => {
console.log(`[hook] tool "${ctx.toolName}" called with:`, ctx.args);
return { action: "allow" };
},
},
})) {
switch (msg.type) {
case "delta":
process.stdout.write(msg.content);
break;
case "assistant":
if (msg.errorMessage) {
console.error(`\n[error] ${msg.errorMessage}`);
} else {
console.log(`\n\n[done] model=${msg.model} tokens=${msg.usage?.totalTokens}`);
}
break;
case "tool_use":
console.log(`\n[tool_use] ${msg.toolName}(${JSON.stringify(msg.args)})`);
break;
case "tool_result":
console.log(`[tool_result] ${msg.content}`);
break;
case "system":
console.log(`[${msg.subtype}] ${msg.content}`);
break;
}
}
}
main().catch(console.error);