|
1 | 1 | import { |
2 | 2 | CopilotRuntime, |
| 3 | + CopilotKitIntelligence, |
3 | 4 | createCopilotEndpoint, |
4 | 5 | InMemoryAgentRunner, |
5 | 6 | } from "@copilotkit/runtime/v2"; |
6 | 7 | import { HttpAgent } from "@ag-ui/client"; |
| 8 | +import type { |
| 9 | + AgentSubscriber, |
| 10 | + RunAgentInput, |
| 11 | + RunAgentParameters, |
| 12 | + RunAgentResult, |
| 13 | +} from "@ag-ui/client"; |
7 | 14 | import { A2AMiddlewareAgent } from "@ag-ui/a2a-middleware"; |
| 15 | +import type { A2AAgentConfig } from "@ag-ui/a2a-middleware"; |
8 | 16 | import { handle } from "hono/vercel"; |
9 | 17 |
|
10 | | -export async function POST(request: Request) { |
11 | | - const researchAgentUrl = |
12 | | - process.env.RESEARCH_AGENT_URL || "http://localhost:9001"; |
13 | | - const analysisAgentUrl = |
14 | | - process.env.ANALYSIS_AGENT_URL || "http://localhost:9002"; |
15 | | - const orchestratorUrl = |
16 | | - process.env.ORCHESTRATOR_URL || "http://localhost:9000"; |
17 | | - |
18 | | - const orchestrationAgent = new HttpAgent({ |
19 | | - url: orchestratorUrl, |
20 | | - }); |
21 | | - |
22 | | - const a2aMiddlewareAgent = new A2AMiddlewareAgent({ |
23 | | - description: |
24 | | - "Research assistant with 2 specialized agents: Research (LangGraph) and Analysis (ADK)", |
25 | | - agentUrls: [researchAgentUrl, analysisAgentUrl], |
26 | | - orchestrationAgent, |
27 | | - instructions: ` |
28 | | - You are a research assistant that orchestrates between 2 specialized agents. |
29 | | -
|
30 | | - AVAILABLE AGENTS: |
31 | | -
|
32 | | - - Research Agent (LangGraph): Gathers and summarizes information about a topic |
33 | | - - Analysis Agent (ADK): Analyzes research findings and provides insights |
34 | | -
|
35 | | - WORKFLOW STRATEGY (SEQUENTIAL - ONE AT A TIME): |
36 | | -
|
37 | | - When the user asks to research a topic: |
38 | | -
|
39 | | - 1. Research Agent - First, gather information about the topic |
40 | | - - Pass: The user's research query or topic |
41 | | - - The agent will return structured JSON with research findings |
42 | | -
|
43 | | - 2. Analysis Agent - Then, analyze the research results |
44 | | - - Pass: The research results from step 1 |
45 | | - - The agent will return structured JSON with analysis and insights |
46 | | -
|
47 | | - 3. Present the complete research and analysis to the user |
48 | | -
|
49 | | - CRITICAL RULES: |
50 | | - - Call agents ONE AT A TIME, wait for results before making next call |
51 | | - - Pass information from earlier agents to later agents |
52 | | - - Synthesize all gathered information in final response |
53 | | - `, |
54 | | - }); |
55 | | - |
56 | | - const runtime = new CopilotRuntime({ |
57 | | - agents: { |
58 | | - a2a_chat: a2aMiddlewareAgent, |
59 | | - }, |
60 | | - runner: new InMemoryAgentRunner(), |
61 | | - }); |
62 | | - |
63 | | - const app = createCopilotEndpoint({ |
64 | | - runtime, |
65 | | - basePath: "/api/copilotkit", |
66 | | - }); |
67 | | - |
68 | | - return handle(app)(request); |
69 | | -} |
| 18 | +const researchAgentUrl = |
| 19 | + process.env.RESEARCH_AGENT_URL || "http://localhost:9001"; |
| 20 | +const analysisAgentUrl = |
| 21 | + process.env.ANALYSIS_AGENT_URL || "http://localhost:9002"; |
| 22 | +const orchestratorUrl = process.env.ORCHESTRATOR_URL || "http://localhost:9000"; |
| 23 | + |
| 24 | +type RuntimeRunAgentInput = RunAgentParameters & |
| 25 | + Partial<Pick<RunAgentInput, "messages" | "state" | "threadId">>; |
| 26 | + |
| 27 | +type RuntimeA2AMiddlewareAgentConfig = Omit< |
| 28 | + A2AAgentConfig, |
| 29 | + "orchestrationAgent" |
| 30 | +> & { |
| 31 | + orchestrationAgentUrl: string; |
| 32 | +}; |
| 33 | + |
| 34 | +class RuntimeA2AMiddlewareAgent extends A2AMiddlewareAgent { |
| 35 | + private readonly config: RuntimeA2AMiddlewareAgentConfig; |
| 36 | + |
| 37 | + constructor(config: RuntimeA2AMiddlewareAgentConfig) { |
| 38 | + super({ |
| 39 | + ...config, |
| 40 | + orchestrationAgent: new HttpAgent({ |
| 41 | + url: config.orchestrationAgentUrl, |
| 42 | + }), |
| 43 | + }); |
| 44 | + this.config = config; |
| 45 | + } |
| 46 | + |
| 47 | + async runAgent( |
| 48 | + parameters: RuntimeRunAgentInput = {}, |
| 49 | + subscriber?: AgentSubscriber, |
| 50 | + ): Promise<RunAgentResult> { |
| 51 | + const isolatedAgent = new A2AMiddlewareAgent({ |
| 52 | + ...this.config, |
| 53 | + agentId: this.agentId, |
| 54 | + debug: this.debug, |
| 55 | + description: this.description, |
| 56 | + initialMessages: this.messages, |
| 57 | + initialState: this.state, |
| 58 | + threadId: parameters.threadId ?? this.threadId, |
| 59 | + orchestrationAgent: new HttpAgent({ |
| 60 | + url: this.config.orchestrationAgentUrl, |
| 61 | + }), |
| 62 | + }); |
70 | 63 |
|
71 | | -export async function GET(request: Request) { |
72 | | - const runtime = new CopilotRuntime({ |
73 | | - agents: {}, |
74 | | - runner: new InMemoryAgentRunner(), |
75 | | - }); |
76 | | - const app = createCopilotEndpoint({ |
77 | | - runtime, |
78 | | - basePath: "/api/copilotkit", |
79 | | - }); |
80 | | - return handle(app)(request); |
| 64 | + if (parameters.state) { |
| 65 | + isolatedAgent.setState(parameters.state); |
| 66 | + } |
| 67 | + |
| 68 | + if (parameters.messages) { |
| 69 | + isolatedAgent.setMessages(parameters.messages); |
| 70 | + } |
| 71 | + |
| 72 | + return isolatedAgent.runAgent( |
| 73 | + { |
| 74 | + context: parameters.context, |
| 75 | + forwardedProps: parameters.forwardedProps, |
| 76 | + runId: parameters.runId, |
| 77 | + tools: parameters.tools, |
| 78 | + }, |
| 79 | + subscriber, |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + clone(): RuntimeA2AMiddlewareAgent { |
| 84 | + return new RuntimeA2AMiddlewareAgent({ |
| 85 | + ...this.config, |
| 86 | + agentId: this.agentId, |
| 87 | + debug: this.debug, |
| 88 | + description: this.description, |
| 89 | + initialMessages: this.messages, |
| 90 | + initialState: this.state, |
| 91 | + threadId: this.threadId, |
| 92 | + }); |
| 93 | + } |
81 | 94 | } |
| 95 | + |
| 96 | +const a2aMiddlewareAgent = new RuntimeA2AMiddlewareAgent({ |
| 97 | + orchestrationAgentUrl: orchestratorUrl, |
| 98 | + agentId: "a2a_chat", |
| 99 | + description: |
| 100 | + "Research assistant with 2 specialized agents: Research (LangGraph) and Analysis (ADK)", |
| 101 | + agentUrls: [researchAgentUrl, analysisAgentUrl], |
| 102 | + instructions: ` |
| 103 | + You are a research assistant that orchestrates between 2 specialized agents. |
| 104 | +
|
| 105 | + AVAILABLE AGENTS: |
| 106 | +
|
| 107 | + - Research Agent (LangGraph): Gathers and summarizes information about a topic |
| 108 | + - Analysis Agent (ADK): Analyzes research findings and provides insights |
| 109 | +
|
| 110 | + WORKFLOW STRATEGY (SEQUENTIAL - ONE AT A TIME): |
| 111 | +
|
| 112 | + When the user asks to research a topic: |
| 113 | +
|
| 114 | + 1. Research Agent - First, gather information about the topic |
| 115 | + - Pass: The user's research query or topic |
| 116 | + - The agent will return structured JSON with research findings |
| 117 | +
|
| 118 | + 2. Analysis Agent - Then, analyze the research results |
| 119 | + - Pass: The research results from step 1 |
| 120 | + - The agent will return structured JSON with analysis and insights |
| 121 | +
|
| 122 | + 3. Present the complete research and analysis to the user |
| 123 | +
|
| 124 | + CRITICAL RULES: |
| 125 | + - Call agents ONE AT A TIME, wait for results before making next call |
| 126 | + - Pass information from earlier agents to later agents |
| 127 | + - Synthesize all gathered information in final response |
| 128 | + `, |
| 129 | +}); |
| 130 | + |
| 131 | +const runtime = new CopilotRuntime({ |
| 132 | + agents: { |
| 133 | + a2a_chat: a2aMiddlewareAgent, |
| 134 | + }, |
| 135 | + // --- copilotkit:intelligence (remove this block to opt out) --- |
| 136 | + ...(process.env.COPILOTKIT_LICENSE_TOKEN |
| 137 | + ? { |
| 138 | + intelligence: new CopilotKitIntelligence({ |
| 139 | + apiKey: process.env.INTELLIGENCE_API_KEY ?? "", |
| 140 | + apiUrl: process.env.INTELLIGENCE_API_URL ?? "http://localhost:4201", |
| 141 | + wsUrl: |
| 142 | + process.env.INTELLIGENCE_GATEWAY_WS_URL ?? "ws://localhost:4401", |
| 143 | + }), |
| 144 | + // Demo stub - replace with your own auth-derived user identity (e.g. OIDC) |
| 145 | + // before any multi-user deployment, or all users share one thread history. |
| 146 | + identifyUser: () => ({ id: "demo-user", name: "Demo User" }), |
| 147 | + licenseToken: process.env.COPILOTKIT_LICENSE_TOKEN, |
| 148 | + } |
| 149 | + : { runner: new InMemoryAgentRunner() }), |
| 150 | + // --- /copilotkit:intelligence --- |
| 151 | +}); |
| 152 | + |
| 153 | +const app = createCopilotEndpoint({ |
| 154 | + runtime, |
| 155 | + basePath: "/api/copilotkit", |
| 156 | +}); |
| 157 | + |
| 158 | +export const GET = handle(app); |
| 159 | +export const POST = handle(app); |
| 160 | +export const PATCH = handle(app); |
| 161 | +export const DELETE = handle(app); |
0 commit comments