-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy path06-streaming.ts
More file actions
114 lines (101 loc) · 3.85 KB
/
Copy path06-streaming.ts
File metadata and controls
114 lines (101 loc) · 3.85 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
/**
* Google ADK Agent with Streaming -- real-time event streaming.
*
* Demonstrates:
* - Streaming events from a Google ADK agent
* - Agentspan path: runtime.stream() with event types
*
* Requirements:
* - npm install @google/adk zod
* - AGENTSPAN_SERVER_URL for agentspan path
*/
import { LlmAgent, FunctionTool } from '@google/adk';
import { z } from 'zod';
import { AgentRuntime } from '@io-orkes/conductor-javascript/agents';
const model = process.env.AGENTSPAN_LLM_MODEL ?? 'gemini-2.5-flash';
// ── Tool ─────────────────────────────────────────────────────────────
const searchDocumentation = new FunctionTool({
name: 'search_documentation',
description: 'Search the product documentation.',
parameters: z.object({
query: z.string().describe('Search query string'),
}),
execute: async (args: { query: string }) => {
const docs: Record<string, { title: string; content: string }> = {
installation: {
title: 'Installation Guide',
content: 'Run `npm install mypackage`. Requires Node.js 18+.',
},
authentication: {
title: 'Authentication',
content: 'Use API keys via the X-API-Key header. Keys are managed in the dashboard.',
},
'rate limits': {
title: 'Rate Limiting',
content: 'Free tier: 100 req/min. Pro: 1000 req/min. Enterprise: unlimited.',
},
};
for (const [key, value] of Object.entries(docs)) {
if (args.query.toLowerCase().includes(key)) {
return { found: true, ...value };
}
}
return { found: false, message: 'No matching documentation found.' };
},
});
// ── Agent ────────────────────────────────────────────────────────────
export const agent = new LlmAgent({
name: 'docs_assistant',
model,
instruction:
'You are a documentation assistant. Use the search tool to find ' +
'relevant docs and provide clear, well-formatted answers.',
tools: [searchDocumentation],
});
// ── Run on agentspan (streaming) ───────────────────────────────────
async function main() {
const runtime = new AgentRuntime();
try {
const result = await runtime.run(agent, 'How do I authenticate with the API?');
result.printResult();
// Production pattern:
// 1. Deploy once during CI/CD:
// await runtime.deploy(agent);
// CLI alternative:
// agentspan deploy --package sdk/typescript/examples/adk --agents docs_assistant
//
// 2. In a separate long-lived worker process:
// await runtime.serve(agent);
// Streaming alternative:
// const streamHandle = await runtime.stream(
// agent,
// 'How do I authenticate with the API?',
// );
// console.log(`Execution started: ${streamHandle.executionId}\n`);
// console.log('Events:');
// for await (const event of streamHandle) {
// switch (event.type) {
// case 'thinking':
// console.log(` [thinking] ${event.content}`);
// break;
// case 'tool_call':
// console.log(` [tool_call] ${event.toolName}(${JSON.stringify(event.args)})`);
// break;
// case 'tool_result':
// console.log(` [tool_result] ${event.toolName} -> ${JSON.stringify(event.result).slice(0, 100)}`);
// break;
// case 'done':
// console.log(` [done] ${JSON.stringify(event.output).slice(0, 200)}`);
// break;
// case 'error':
// console.log(` [error] ${event.content}`);
// break;
// }
// }
// const final = await streamHandle.getResult();
// console.log(`\nStatus: ${final.status}`);
} finally {
await runtime.shutdown();
}
}
main().catch(console.error);