-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy path05-streaming.ts
More file actions
96 lines (77 loc) · 2.54 KB
/
Copy path05-streaming.ts
File metadata and controls
96 lines (77 loc) · 2.54 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
/**
* 05 - Streaming
*
* Demonstrates runtime.stream() with for-await-of loop
* and event type switching.
*/
import {
Agent,
AgentRuntime,
EventTypes,
} from '@io-orkes/conductor-javascript/agents';
const MODEL = process.env.AGENTSPAN_LLM_MODEL ?? 'openai/gpt-4o';
export const agent = new Agent({
name: 'streaming_agent',
model: MODEL,
instructions: 'Answer the question thoroughly.',
});
async function main() {
const runtime = new AgentRuntime();
try {
const result = await runtime.run(agent, 'Explain how quantum computers work.');
result.printResult();
// Production pattern:
// 1. Deploy once during CI/CD (optional -- serve() below also deploys):
// await runtime.deploy(agent);
// CLI alternative:
// agentspan deploy --package sdk/typescript/examples --agents streaming_agent
//
// 2. In a separate long-lived worker process (deploys + registers workers + starts polling):
// await runtime.serve(agent);
// Streaming alternative:
// const agentStream = await runtime.stream(
// agent,
// 'Explain how quantum computers work.',
// );
// console.log(`Execution: ${agentStream.executionId}\n`);
// for await (const event of agentStream) {
// switch (event.type) {
// case EventTypes.THINKING:
// console.log(`[thinking] ${(event.content ?? '').slice(0, 80)}...`);
// break;
// case EventTypes.TOOL_CALL:
// console.log(`[tool_call] ${event.toolName}(${JSON.stringify(event.args)})`);
// break;
// case EventTypes.TOOL_RESULT:
// console.log(`[tool_result] ${event.toolName} -> ${String(event.result).slice(0, 80)}`);
// break;
// case EventTypes.HANDOFF:
// console.log(`[handoff] -> ${event.target}`);
// break;
// case EventTypes.GUARDRAIL_PASS:
// console.log(`[guardrail_pass] ${event.guardrailName}`);
// break;
// case EventTypes.GUARDRAIL_FAIL:
// console.log(`[guardrail_fail] ${event.guardrailName}: ${event.content}`);
// break;
// case EventTypes.MESSAGE:
// console.log(`[message] ${(event.content ?? '').slice(0, 120)}`);
// break;
// case EventTypes.WAITING:
// console.log('[waiting] Approval required');
// break;
// case EventTypes.ERROR:
// console.log(`[error] ${event.content}`);
// break;
// case EventTypes.DONE:
// console.log('[done] Stream complete');
// break;
// }
// }
// const result = await agentStream.getResult();
// result.printResult();
} finally {
await runtime.shutdown();
}
}
main().catch(console.error);