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
62 lines (57 loc) · 1.64 KB
/
Copy pathmain.js
File metadata and controls
62 lines (57 loc) · 1.64 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
import { HttpAgent } from '@ag-ui/client';
const agent = new HttpAgent({
url: 'http://127.0.0.1:18080/weather_agent',
debug: false
});
const subscription = agent.subscribe({
// Text message start
onTextMessageStartEvent: ({ event }) => {
process.stdout.write('\n🤖 Assistant: ');
},
// Text message content delta
onTextMessageContentEvent: ({ event }) => {
process.stdout.write(event.delta ?? '');
},
// Text message end
onTextMessageEndEvent: ({ event }) => {
process.stdout.write('\n');
},
// Tool call start
onToolCallStartEvent: ({ event }) => {
process.stdout.write(`\n🔧 Call Tool ${event.toolCallName}: `);
},
// Tool call arguments delta
onToolCallArgsEvent: ({ event }) => {
process.stdout.write(event.delta ?? '');
},
// Tool call result
onToolCallResultEvent: ({ event }) => {
process.stdout.write(`\n✅ Tool result: ${event.content}`);
},
// Run started
onRunStartedEvent: ({ event }) => {
process.stdout.write(`\n⚙️ Run started: ${event.runId}`);
},
// Run finished
onRunFinishedEvent: ({ result }) => {
if (result !== undefined) {
process.stdout.write(`⚙️ Run finished, result: ${result}\n`);
} else {
process.stdout.write('⚙️ Run finished\n');
}
},
// Run failed
onRunFailedEvent: ({ error }) => {
process.stdout.write(`❌ Run failed: ${error}\n`);
}
});
// Add user message
await agent.addMessage({
role: 'user',
content: 'What is the weather like in Beijing?',
id: 'user_123'
});
// Execute conversation (automatically sends message)
await agent.runAgent();
// Unsubscribe when done
subscription.unsubscribe?.();