-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstream-example.ts
More file actions
230 lines (194 loc) · 6.37 KB
/
Copy pathstream-example.ts
File metadata and controls
230 lines (194 loc) · 6.37 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* Stream Invoke Example
*
* This example demonstrates how to use the streamInvoke method
* for real-time streaming responses from the AgentFlow API.
*/
import {
AgentFlowClient,
Message,
StreamChunk,
StreamEventType
} from '../src/index.js';
/**
* Basic streaming example
*/
async function basicStreamExample() {
// Create client
const client = new AgentFlowClient({
baseUrl: 'http://127.0.0.1:8000',
debug: true
});
// Create a message
const messages = [Message.text_message('What is the weather like?', 'user')];
console.log('Starting stream...\n');
// Stream the response
const stream = client.stream(messages, {
response_granularity: 'low',
recursion_limit: 25
});
let messageCount = 0;
let updateCount = 0;
// Iterate over stream chunks
for await (const chunk of stream) {
switch (chunk.event) {
case StreamEventType.MESSAGE:
if (chunk.message) {
messageCount++;
console.log(`\n[Message ${messageCount}] ${chunk.message.role}:`);
// Print text content
const textBlocks = chunk.message.content?.filter(
(block: any) => block.type === 'text'
);
textBlocks?.forEach((block: any) => {
console.log(block.text);
});
}
break;
case StreamEventType.UPDATES:
updateCount++;
console.log(`\n[Update ${updateCount}] State changed`);
if (chunk.state) {
console.log(' Context messages:', chunk.state.context?.length || 0);
}
break;
case StreamEventType.ERROR:
console.error('\n[Error]', chunk.data);
break;
default:
console.log(`\n[${chunk.event}]`, chunk.data);
}
}
console.log('\n\nStream completed!');
}
/**
* Collect all chunks then process them
*/
async function collectAndProcessExample() {
const client = new AgentFlowClient({
baseUrl: 'http://127.0.0.1:8000',
debug: false
});
const messages = [Message.text_message('Hello!', 'user')];
const chunks: StreamChunk[] = [];
// Collect all chunks
const stream = client.stream(messages);
for await (const chunk of stream) {
chunks.push(chunk);
}
// Now process all at once
const messageChunks = chunks.filter(c => c.event === 'message');
const updateChunks = chunks.filter(c => c.event === 'updates');
console.log(`Received ${messageChunks.length} messages and ${updateChunks.length} updates`);
console.log(`Total chunks: ${chunks.length}`);
}
/**
* Real-time UI update simulation
*/
async function realtimeUIExample() {
const client = new AgentFlowClient({
baseUrl: 'http://127.0.0.1:8000',
debug: false
});
const userMessage = Message.text_message('Tell me a joke', 'user');
console.log('User:', userMessage.content[0]);
let assistantResponse = '';
// Stream response and update "UI" incrementally
const stream = client.stream([userMessage], {
response_granularity: 'low'
});
for await (const chunk of stream) {
if (chunk.event === 'message' && chunk.message?.role === 'assistant') {
// Accumulate assistant response text
const textBlocks = chunk.message.content?.filter(
(block: any) => block.type === 'text'
);
textBlocks?.forEach((block: any) => {
assistantResponse += block.text;
// In a real UI, you'd update React state here
process.stdout.write(block.text);
});
}
}
console.log('\n\nFull response:', assistantResponse);
}
/**
* With error handling
*/
async function withErrorHandlingExample() {
const client = new AgentFlowClient({
baseUrl: 'http://127.0.0.1:8000',
timeout: 30000, // 30 seconds
debug: false
});
const messages = [Message.text_message('Hello', 'user')];
try {
const stream = client.stream(messages);
for await (const chunk of stream) {
if (chunk.event === 'error') {
console.error('Received error in stream:', chunk.data);
break;
}
if (chunk.event === 'message') {
console.log('Message received');
}
}
} catch (error) {
if (error instanceof Error) {
if (error.message.includes('timeout')) {
console.error('Stream timed out');
} else if (error.message.includes('HTTP error')) {
console.error('Server error');
} else {
console.error('Unknown error:', error.message);
}
}
}
}
/**
* Multiple messages example
*/
async function multiMessageExample() {
const client = new AgentFlowClient({
baseUrl: 'http://127.0.0.1:8000'
});
// Create a conversation
const messages = [
Message.text_message('What is machine learning?', 'user'),
Message.text_message(
'Machine learning is a subset of AI that enables systems to learn from data.',
'assistant'
),
Message.text_message('Give me an example', 'user')
];
console.log('Continuing conversation...\n');
const stream = client.stream(messages, {
response_granularity: 'low'
});
for await (const chunk of stream) {
if (chunk.event === 'message') {
console.log(`\n[${chunk.message?.role}]: ${chunk.message?.content}`);
}
}
}
// Run example based on command line argument
const example = process.argv[2] || 'basic';
switch (example) {
case 'basic':
basicStreamExample().catch(console.error);
break;
case 'collect':
collectAndProcessExample().catch(console.error);
break;
case 'realtime':
realtimeUIExample().catch(console.error);
break;
case 'error':
withErrorHandlingExample().catch(console.error);
break;
case 'multi':
multiMessageExample().catch(console.error);
break;
default:
console.log('Usage: npx ts-node stream-example.ts [basic|collect|realtime|error|multi]');
}