-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest-streaming.ts
More file actions
348 lines (279 loc) · 13.5 KB
/
test-streaming.ts
File metadata and controls
348 lines (279 loc) · 13.5 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* test-streaming.ts — Streaming Adapter Test Suite
*
* Tests:
* - StreamingBaseAdapter fallback (single-chunk wrapper around executeAgent)
* - CustomStreamingAdapter with async-generator handlers
* - CustomStreamingAdapter fallback for plain promise handlers
* - LangChainStreamingAdapter with a streamable runnable
* - LangChainStreamingAdapter fallback for non-streamable runnables
* - collectStream() helper
* - supportsStreaming() detection
*
* Run: npx ts-node test-streaming.ts
*/
import { StreamingBaseAdapter, collectStream } from './adapters/streaming-base-adapter';
import { CustomStreamingAdapter } from './adapters/custom-streaming-adapter';
import { LangChainStreamingAdapter } from './adapters/langchain-streaming-adapter';
import type { AgentPayload, AgentContext, AgentResult } from './types/agent-adapter';
import type { StreamingChunk } from './types/streaming-adapter';
// ─── Colours ─────────────────────────────────────────────────────────────────
const c = {
green: '\x1b[32m',
red: '\x1b[31m',
cyan: '\x1b[36m',
bold: '\x1b[1m',
reset: '\x1b[0m',
};
let passed = 0;
let failed = 0;
function assert(condition: boolean, message: string): void {
if (condition) {
console.log(` ${c.green}[v]${c.reset} ${message}`);
passed++;
} else {
console.log(` ${c.red}[x]${c.reset} ${message}`);
failed++;
}
}
function section(title: string): void {
console.log(`\n${c.cyan}${c.bold}> ${title}${c.reset}`);
}
// ─── Concrete StreamingBaseAdapter for tests ─────────────────────────────────
class ConcreteStreamingAdapter extends StreamingBaseAdapter {
readonly name = 'test-streaming-base' as const;
readonly version = '1.0.0';
async executeAgent(
agentId: string,
payload: AgentPayload,
_context: AgentContext,
): Promise<AgentResult> {
if (agentId === 'greet') {
const name = (payload.params?.name as string) ?? 'world';
return this.successResult(`Hello, ${name}!`, 1);
}
return this.errorResult('NOT_FOUND', `Agent "${agentId}" not found`, false);
}
}
// ─── Shared context / payload helpers ────────────────────────────────────────
const ctx: AgentContext = { agentId: 'tester', taskId: 'test-stream' };
function payload(instruction: string, params?: Record<string, unknown>): AgentPayload {
return {
action: 'stream',
params: params ?? {},
handoff: {
handoffId: 'h1',
sourceAgent: 'tester',
targetAgent: 'target',
taskType: 'delegate',
instruction,
},
};
}
// ─── 1. StreamingBaseAdapter — fallback wrapper ───────────────────────────────
async function testStreamingBaseAdapter(): Promise<void> {
section('StreamingBaseAdapter — single-chunk fallback');
const adapter = new ConcreteStreamingAdapter();
await adapter.initialize({});
// supportsStreaming is false by default
assert(adapter.supportsStreaming('greet') === false, 'supportsStreaming returns false by default');
// collect success result
const { output, chunks } = await collectStream(
adapter.executeAgentStream('greet', payload('ignored', { name: 'Alice' }), ctx),
);
assert(output === 'Hello, Alice!', 'Fallback stream yields correct text');
assert(chunks.length === 2, 'Fallback emits 2 chunks (content + done sentinel)');
assert(chunks[chunks.length - 1].done === true, 'Last chunk has done=true');
assert(chunks[0].done === false, 'Content chunk has done=false');
// collect error result
const errStream = await collectStream(
adapter.executeAgentStream('missing', payload('noop'), ctx),
);
assert(errStream.chunks.length === 1, 'Error emits single done-chunk');
assert(errStream.chunks[0].done === true, 'Error chunk has done=true');
assert(
errStream.chunks[0].metadata?.['error'] === true,
'Error chunk carries error metadata',
);
}
// ─── 2. collectStream() helper ────────────────────────────────────────────────
async function testCollectStream(): Promise<void> {
section('collectStream() helper');
async function* fakeStream(): AsyncIterable<StreamingChunk> {
yield { text: 'Hel', done: false };
yield { text: 'lo ', done: false };
yield { text: 'world', done: false };
yield { text: '', done: true };
}
const { output, chunks } = await collectStream(fakeStream());
assert(output === 'Hello world', 'collectStream concatenates text chunks correctly');
assert(chunks.length === 4, 'collectStream preserves all chunks');
}
// ─── 3. CustomStreamingAdapter — async generator handler ─────────────────────
async function testCustomStreamingAdapterGenerator(): Promise<void> {
section('CustomStreamingAdapter — async generator handler');
const adapter = new CustomStreamingAdapter();
await adapter.initialize({});
// Async generator handler
adapter.registerHandler(
'counter',
async function* (p: AgentPayload) {
const n = (p.params?.count as number) ?? 3;
for (let i = 1; i <= n; i++) {
yield `${i}`;
await new Promise(res => setTimeout(res, 1));
}
} as any,
);
adapter.markStreaming('counter');
assert(adapter.supportsStreaming('counter') === true, 'Generator handler marked as streaming');
const { output, chunks } = await collectStream(
adapter.executeAgentStream('counter', payload('count', { count: 3 }), ctx),
);
assert(output === '123', 'Generator handler streams tokens correctly');
assert(chunks[chunks.length - 1].done === true, 'Generator stream ends with done=true');
}
// ─── 4. CustomStreamingAdapter — plain promise handler (fallback) ─────────────
async function testCustomStreamingAdapterPromise(): Promise<void> {
section('CustomStreamingAdapter — plain promise handler (single-chunk)');
const adapter = new CustomStreamingAdapter();
await adapter.initialize({});
adapter.registerHandler('adder', async (p: AgentPayload) => {
const a = (p.params?.a as number) ?? 0;
const b = (p.params?.b as number) ?? 0;
return { sum: a + b };
});
assert(adapter.supportsStreaming('adder') === false, 'Plain handler not marked as streaming');
const { output, chunks } = await collectStream(
adapter.executeAgentStream('adder', payload('add', { a: 3, b: 4 }), ctx),
);
assert(JSON.parse(output).sum === 7, 'Plain handler result serialised correctly');
assert(chunks.length === 2, 'Plain handler emits content + done chunks');
}
// ─── 5. CustomStreamingAdapter — unknown agent ───────────────────────────────
async function testCustomStreamingAdapterUnknown(): Promise<void> {
section('CustomStreamingAdapter — unknown agent error');
const adapter = new CustomStreamingAdapter();
await adapter.initialize({});
const { chunks } = await collectStream(
adapter.executeAgentStream('ghost', payload('noop'), ctx),
);
assert(chunks.length === 1, 'Unknown agent yields single error chunk');
assert(chunks[0].done === true, 'Error chunk has done=true');
assert(
chunks[0].metadata?.['error'] === true,
'Error chunk carries error flag',
);
}
// ─── 6. LangChainStreamingAdapter — streamable runnable ──────────────────────
async function testLangChainStreamingAdapterStream(): Promise<void> {
section('LangChainStreamingAdapter — streamable runnable');
const adapter = new LangChainStreamingAdapter();
await adapter.initialize({});
// Runnable with .stream() method
const streamableRunnable = {
invoke: async (_input: unknown) => ({ output: 'Full non-streaming response' }),
stream: async function* (_input: unknown): AsyncIterable<unknown> {
const words = ['The ', 'answer ', 'is ', '42.'];
for (const w of words) {
yield w;
await new Promise(res => setTimeout(res, 1));
}
},
};
adapter.registerAgent('oracle', streamableRunnable, { description: 'Test streaming oracle' });
assert(adapter.supportsStreaming('oracle') === true, 'Streamable runnable detected');
const { output, chunks } = await collectStream(
adapter.executeAgentStream('oracle', payload('What is the answer?'), ctx),
);
assert(output === 'The answer is 42.', 'LangChain stream yields correct concatenated text');
assert(chunks[chunks.length - 1].done === true, 'LangChain stream ends cleanly');
assert(chunks.length >= 5, 'Multiple chunks emitted (one per word + done)');
}
// ─── 7. LangChainStreamingAdapter — non-streamable runnable (fallback) ────────
async function testLangChainStreamingAdapterFallback(): Promise<void> {
section('LangChainStreamingAdapter — non-streamable runnable (fallback)');
const adapter = new LangChainStreamingAdapter();
await adapter.initialize({});
// Runnable without .stream()
const basicRunnable = {
invoke: async (_input: unknown) => ({ output: 'Fallback response data' }),
};
adapter.registerAgent('basic', basicRunnable, { description: 'No streaming' });
assert(adapter.supportsStreaming('basic') === false, 'Non-streamable runnable not marked');
const { output, chunks } = await collectStream(
adapter.executeAgentStream('basic', payload('Do the thing'), ctx),
);
assert(output === 'Fallback response data', 'Fallback yields correct result text');
assert(chunks[chunks.length - 1].done === true, 'Fallback stream ends cleanly');
}
// ─── 8. LangChainStreamingAdapter — yields AIMessage-shaped chunks ────────────
async function testLangChainStreamingAIMessage(): Promise<void> {
section('LangChainStreamingAdapter — AIMessage chunk shape');
const adapter = new LangChainStreamingAdapter();
await adapter.initialize({});
const aiMessageRunnable = {
invoke: async (_input: unknown) => ({ output: 'unused' }),
stream: async function* (_input: unknown): AsyncIterable<unknown> {
// LangChain ChatOpenAI.stream() yields AIMessage-like objects
yield { content: 'Chat ' };
yield { content: 'response.' };
},
};
adapter.registerAgent('chat', aiMessageRunnable);
const { output } = await collectStream(
adapter.executeAgentStream('chat', payload('hello'), ctx),
);
assert(output === 'Chat response.', 'AIMessage .content field extracted correctly');
}
// ─── 9. LangChainStreamingAdapter — unknown agent ────────────────────────────
async function testLangChainStreamingUnknown(): Promise<void> {
section('LangChainStreamingAdapter — unknown agent');
const adapter = new LangChainStreamingAdapter();
await adapter.initialize({});
const { chunks } = await collectStream(
adapter.executeAgentStream('ghost', payload('noop'), ctx),
);
assert(chunks.length >= 1, 'Unknown agent yields at least one error chunk');
assert(chunks[chunks.length - 1].done === true, 'Error chunk has done=true');
}
// ─── 10. StreamingChunk types are correct ────────────────────────────────────
async function testStreamingChunkTypes(): Promise<void> {
section('StreamingChunk — type shapes');
const contentChunk: StreamingChunk = { text: 'hello', done: false };
const doneChunk: StreamingChunk = { text: '', done: true, metadata: { adapter: 'test' } };
assert(contentChunk.text === 'hello' && contentChunk.done === false, 'Content chunk shape correct');
assert(doneChunk.done === true && doneChunk.metadata?.['adapter'] === 'test', 'Done chunk shape correct');
}
// ─── Run all ──────────────────────────────────────────────────────────────────
async function runAllTests(): Promise<void> {
console.log(`\n${c.bold}+=====================================================+${c.reset}`);
console.log(`${c.bold}| Streaming Adapter Test Suite |${c.reset}`);
console.log(`${c.bold}+=====================================================+${c.reset}`);
try {
await testStreamingBaseAdapter();
await testCollectStream();
await testCustomStreamingAdapterGenerator();
await testCustomStreamingAdapterPromise();
await testCustomStreamingAdapterUnknown();
await testLangChainStreamingAdapterStream();
await testLangChainStreamingAdapterFallback();
await testLangChainStreamingAIMessage();
await testLangChainStreamingUnknown();
await testStreamingChunkTypes();
} catch (err) {
console.log(`\n${c.red}FATAL: ${err}${c.reset}`);
if (err instanceof Error) console.log(err.stack);
failed++;
}
const total = passed + failed;
console.log(`\n${c.bold}=======================================================${c.reset}`);
if (failed === 0) {
console.log(`${c.green}${c.bold} ALL ${total} STREAMING TESTS PASSED [v]${c.reset}`);
} else {
console.log(`${c.red}${c.bold} ${failed} of ${total} TESTS FAILED${c.reset}`);
}
console.log(`${c.bold}=======================================================${c.reset}\n`);
process.exit(failed > 0 ? 1 : 0);
}
runAllTests();