forked from trpc-group/trpc-agent-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
58 lines (52 loc) · 1.62 KB
/
Copy pathmain.js
File metadata and controls
58 lines (52 loc) · 1.62 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
import { HttpAgent } from '@ag-ui/client';
const agent = new HttpAgent({
url: 'http://127.0.0.1:18080/weather_agent',
debug: false
});
let chunkCount = 0;
const ABORT_AFTER_CHUNKS = 5;
const subscription = agent.subscribe({
onTextMessageStartEvent: ({ event }) => {
process.stdout.write('\n🤖 Assistant: ');
},
onTextMessageContentEvent: ({ event }) => {
process.stdout.write(event.delta ?? '');
chunkCount++;
if (chunkCount === ABORT_AFTER_CHUNKS) {
process.stdout.write('\n\n⏸️ Aborting run after receiving ' + ABORT_AFTER_CHUNKS + ' text chunks...\n');
agent.abortRun();
}
},
onTextMessageEndEvent: ({ event }) => {
process.stdout.write('\n');
},
onToolCallStartEvent: ({ event }) => {
process.stdout.write(`\n🔧 Call Tool ${event.toolCallName}: `);
},
onToolCallArgsEvent: ({ event }) => {
process.stdout.write(event.delta ?? '');
},
onToolCallResultEvent: ({ event }) => {
process.stdout.write(`\n✅ Tool result: ${event.content}`);
},
onRunStartedEvent: ({ event }) => {
process.stdout.write(`\n⚙️ Run started: ${event.runId}`);
},
onRunFinishedEvent: ({ result }) => {
if (result !== undefined) {
process.stdout.write(`⚙️ Run finished, result: ${result}\n`);
} else {
process.stdout.write('⚙️ Run finished\n');
}
},
onRunFailedEvent: ({ error }) => {
process.stdout.write(`❌ Run failed: ${error}\n`);
}
});
await agent.addMessage({
role: 'user',
content: 'Please introduce yourself in detail and tell me what you can do.',
id: 'user_123'
});
await agent.runAgent();
subscription.unsubscribe?.();