|
5 | 5 | * AgentCore Service - Streaming Response Handler |
6 | 6 | * |
7 | 7 | * Handles streaming responses from AgentCore agents using Server-Sent Events (SSE). |
8 | | - * Dynamically loads the appropriate parser based on the agent pattern in aws-exports.json. |
9 | 8 | * |
10 | | - * CUSTOMIZATION GUIDE |
11 | | - * See strandsParser.js and langgraphParser.js for sample implementation of parsers for Strands and Langgraph. |
12 | | - * Modify strandsParser.js or langgraphParser.js when You need to handle additional event types (e.g., tool calls, images). |
13 | | - * Create a new parser file to support other agent frameworks (not Strands or LangGraph), and replace the parser |
14 | | - * with your agent's specific event parsing logic. |
| 9 | + * CUSTOMIZATION FOR OTHER AGENT TYPES: |
| 10 | + * This service uses separate parser files for Strands and LangGraph agents. |
| 11 | + * Both parsers try each streaming event, each processes its format and ignores others. |
| 12 | + * To support other agent frameworks, create a new parser file and add a call in the streaming loop. |
| 13 | + * See strandsParser.js and langgraphParser.js for examples. |
15 | 14 | */ |
16 | 15 |
|
17 | 16 | import * as strandsParser from './strandsParser.js'; |
18 | 17 | import * as langgraphParser from './langgraphParser.js'; |
19 | 18 |
|
20 | | -// Parser selection based on agent pattern |
21 | | -let activeParser = null; |
22 | | - |
23 | | -/** |
24 | | - * Load and select the appropriate parser based on agent pattern |
25 | | - */ |
26 | | -const loadParser = async () => { |
27 | | - if (activeParser) { |
28 | | - return activeParser; |
29 | | - } |
30 | | - |
31 | | - try { |
32 | | - const response = await fetch("/aws-exports.json"); |
33 | | - const config = await response.json(); |
34 | | - const pattern = config.agentPattern || "strands-single-agent"; |
35 | | - |
36 | | - // Select parser based on exact pattern match |
37 | | - if (pattern === 'strands-single-agent') { |
38 | | - activeParser = strandsParser; |
39 | | - console.log(`Using Strands parser for pattern: ${pattern}`); |
40 | | - } else if (pattern === 'langgraph-single-agent') { |
41 | | - activeParser = langgraphParser; |
42 | | - console.log(`Using LangGraph parser for pattern: ${pattern}`); |
43 | | - } else { |
44 | | - throw new Error( |
45 | | - `Unsupported agent pattern: "${pattern}". ` + |
46 | | - `Supported patterns: "strands-single-agent", "langgraph-single-agent". ` + |
47 | | - `To add support for this pattern, create a parser file and update loadParser() in agentCoreService.js.` |
48 | | - ); |
49 | | - } |
50 | | - |
51 | | - return activeParser; |
52 | | - } catch (error) { |
53 | | - console.error("Failed to load agent pattern:", error); |
54 | | - // Default to Strands parser as fallback |
55 | | - console.warn("Defaulting to Strands parser"); |
56 | | - activeParser = strandsParser; |
57 | | - return activeParser; |
58 | | - } |
59 | | -}; |
60 | | - |
61 | | -// Generate a UUID-like string that meets AgentCore requirements (min 33 chars) |
| 19 | +// Generate a UUID |
62 | 20 | const generateId = () => { |
63 | | - const timestamp = Date.now().toString(36) |
64 | | - |
65 | | - // Use cryptographically secure random number generation |
66 | | - const getSecureRandom = () => { |
67 | | - const array = new Uint32Array(1) |
68 | | - crypto.getRandomValues(array) |
69 | | - return array[0].toString(36) |
70 | | - } |
71 | | - |
72 | | - const random1 = getSecureRandom() |
73 | | - const random2 = getSecureRandom() |
74 | | - const random3 = getSecureRandom() |
75 | | - return `${timestamp}-${random1}-${random2}-${random3}` |
| 21 | + return crypto.randomUUID(); |
76 | 22 | } |
77 | 23 |
|
78 | 24 | // Configuration - will be populated from aws-exports.json |
@@ -104,9 +50,6 @@ export const invokeAgentCore = async (query, sessionId, onStreamUpdate, accessTo |
104 | 50 | throw new Error("Agent Runtime ARN not configured") |
105 | 51 | } |
106 | 52 |
|
107 | | - // Load the appropriate parser |
108 | | - const parser = await loadParser(); |
109 | | - |
110 | 53 | // Bedrock Agent Core endpoint |
111 | 54 | const endpoint = `https://bedrock-agentcore.${AGENT_CONFIG.AWS_REGION}.amazonaws.com` |
112 | 55 |
|
@@ -168,8 +111,11 @@ export const invokeAgentCore = async (query, sessionId, onStreamUpdate, accessTo |
168 | 111 |
|
169 | 112 | for (const line of lines) { |
170 | 113 | if (line.trim()) { |
171 | | - // Use the dynamically loaded parser |
172 | | - completion = parser.parseStreamingChunk(line, completion, onStreamUpdate); |
| 114 | + // Both parsers try each event, each processes its format and ignores others |
| 115 | + // No overlap between formats, so both can safely run |
| 116 | + // Suggestion: Keep only the parser for your agent type to reduce unnecessary processing |
| 117 | + completion = strandsParser.parseStreamingChunk(line, completion, onStreamUpdate); |
| 118 | + completion = langgraphParser.parseStreamingChunk(line, completion, onStreamUpdate); |
173 | 119 | } |
174 | 120 | } |
175 | 121 | } |
|
0 commit comments