-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmulti-turn-session.ts
More file actions
65 lines (54 loc) · 1.8 KB
/
multi-turn-session.ts
File metadata and controls
65 lines (54 loc) · 1.8 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
/**
* Multi-turn session example.
*
* Demonstrates `createSession()` for a persistent session, multiple
* `stream()` calls for streaming turns, `send()` for a non-streaming
* turn, and `close()` for cleanup.
*
* Usage:
* npx tsx examples/multi-turn-session.ts
*/
import { createSession } from '../src/index.js';
async function main(): Promise<void> {
const session = await createSession({ cwd: process.cwd() });
console.log(`Session created: ${session.sessionId}\n`);
console.log('=== Turn 1 (streaming) ===');
console.log('Prompt: "List the TypeScript files in this project"\n');
for await (const msg of session.stream(
'List the TypeScript files in this project'
)) {
if (msg.type === 'assistant_text_delta') {
process.stdout.write(msg.text);
}
if (msg.type === 'turn_complete') {
console.log('\n');
}
}
console.log('=== Turn 2 (streaming) ===');
console.log('Prompt: "How many lines of code total?"\n');
for await (const msg of session.stream('How many lines of code total?')) {
if (msg.type === 'assistant_text_delta') {
process.stdout.write(msg.text);
}
if (msg.type === 'turn_complete') {
console.log('\n');
}
}
console.log('=== Turn 3 (non-streaming) ===');
console.log('Prompt: "Summarize the project in one sentence"\n');
const result = await session.send('Summarize the project in one sentence');
console.log('Response:', result.text);
console.log(`Messages received: ${result.messages.length}`);
if (result.tokenUsage) {
console.log(
`Tokens — input: ${result.tokenUsage.inputTokens}, ` +
`output: ${result.tokenUsage.outputTokens}`
);
}
await session.close();
console.log('\nSession closed.');
}
main().catch((err: unknown) => {
console.error('Error:', err);
process.exit(1);
});