-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsimple-query.ts
More file actions
53 lines (44 loc) · 1.23 KB
/
simple-query.ts
File metadata and controls
53 lines (44 loc) · 1.23 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
/**
* Simple one-shot query example.
*
* Demonstrates using `query()` to send a single prompt, streaming
* `AssistantTextDelta` messages to stdout, and handling `TurnComplete`.
*
* Usage:
* npx tsx examples/simple-query.ts
*/
import { query } from '../src/index.js';
async function main(): Promise<void> {
const prompt = process.argv[2] ?? 'What files are in the current directory?';
console.log(`Sending prompt: "${prompt}"\n`);
const stream = query({
prompt,
cwd: process.cwd(),
});
for await (const msg of stream) {
switch (msg.type) {
case 'assistant_text_delta':
process.stdout.write(msg.text);
break;
case 'tool_use':
console.log(`\n[Tool] ${msg.toolName}`);
break;
case 'tool_result':
console.log(`[Tool Result] ${msg.isError ? 'Error' : 'OK'}`);
break;
case 'turn_complete':
console.log('\n\n--- Turn complete ---');
if (msg.tokenUsage) {
console.log(
`Tokens — input: ${msg.tokenUsage.inputTokens}, ` +
`output: ${msg.tokenUsage.outputTokens}`
);
}
break;
}
}
}
main().catch((err: unknown) => {
console.error('Error:', err);
process.exit(1);
});