-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_query.ts
More file actions
70 lines (61 loc) · 2.11 KB
/
Copy pathexample_query.ts
File metadata and controls
70 lines (61 loc) · 2.11 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
import "dotenv/config";
import { query } from "@anthropic-ai/claude-agent-sdk";
import { createShellPrint } from "./src/index.js";
async function main() {
const shellprint = createShellPrint({
jsonlPath: "./shellprint-example-events.jsonl",
onEvent(event) {
const descriptionPart = event.description
? ` | description=${event.description}`
: "";
console.log(
`[shellprint] ${event.tool} | ${event.category} | action=${event.action}${descriptionPart}`,
);
},
});
const result = query({
prompt: [
"Do these tasks using bash commands:",
"1. curl https://api.github.com/orgs/anthropics to get their org info",
"2. Use grep to search for 'createShellPrint' in example_query.ts",
"3. Read the file .gitignore in the current directory",
"4. Run 'echo hello world'",
].join("\n"),
options: {
allowedTools: ["Bash", "Read", "Glob", "Grep"],
permissionMode: "bypassPermissions",
maxTurns: 10,
systemPrompt:
"You are a research assistant with full access to bash tools. " +
"Use curl, grep, and other CLI tools freely. " +
"You may generate and use any URLs needed.",
hooks: {
PreToolUse: [shellprint.preToolMatcher],
PostToolUse: [shellprint.postToolMatcher],
PostToolUseFailure: [shellprint.failureMatcher],
},
},
});
for await (const message of result) {
if (message.type === "assistant") {
for (const block of message.message.content) {
if (block.type === "text" && block.text) {
console.log(`[assistant] ${block.text.slice(0, 160)}`);
}
}
}
if (message.type === "result") {
console.log(
`[result] ${message.subtype} | turns=${message.num_turns} | cost=$${message.total_cost_usd?.toFixed(4)}`,
);
}
}
await shellprint.flush();
const events = shellprint.getEvents();
console.log(`Captured ${events.length} events`);
console.log("JSONL written to ./shellprint-example-events.jsonl");
}
void main().catch((error: unknown) => {
console.error(error);
process.exitCode = 1;
});