大模型应用
把外部 Agent 框架接入 CopilotKit 应用时…
当需要将 LangGraph、CrewAI、PydanticAI、Mastra、ADK、LlamaIndex、Agno、Strands、Microsoft Agent Framework 等外部 Agent 框架通过 AG-UI 协议接入 CopilotKit 前端时使用。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 38/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 59/100 ×0.15
适用场景
把外部 Agent 框架接入 Copil特定
解决思路
- 先确认 Agent 的语言与框架:TypeScript/JS 走 Mastra、LangGraph JS 或 BuiltInAgent+MCP Apps 中间件/HttpAgent;Python 或 .NET 按框架分别查阅对应集成文档(LangGraph、CrewAI、PydanticAI、Google ADK、LlamaIndex、Agno、Strands、MS Agent Framework),多 Agent 场景走 A2A。
- 按四层架构搭建:1) Agent server 以 HTTP 服务运行(Python 通常用 FastAPI/uvicorn,JS/TS 用 Express 或 Next.js route);2) 使用框架专属 AG-UI adapter 在 Agent 原生接口与 AG-UI 线协议之间转换;3) 在 Next.js API route 中创建 CopilotRuntime,通过对应 AG-UI client 类连接 Agent;4) 前端用 useAgent、useFrontendTool、useRenderToolCall、useHumanInTheLoop 与 Agent 交互。
- 按框架选择 AG-UI client 类:LangGraph 用 @copilotkit/runtime/langgraph 的 LangGraphAgent 或 LangGraphHttpAgent;CrewAI 用 @ag-ui/crewai 的 CrewAIAgent;Mastra 用 @ag-ui/mastra 的 MastraAgent;LlamaIndex 用 @ag-ui/llamaindex 的 LlamaIndexAgent;A2A 用 @ag-ui/a2a-middleware 的 A2AMiddlewareAgent;其余通用场景用 @ag-ui/client 的 HttpAgent。
- 按框架选择 AG-UI server adapter:LangGraph 用 ag-ui-langgraph 的 add_langgraph_fastapi_endpoint(自托管端口 8123)或 LangGraph Platform 托管;CrewAI 用 ag-ui-crewai 的 add_crewai_flow_fastapi_endpoint 或 add_crewai_crew_fastapi_endpoint(端口 8000);PydanticAI 用 pydantic-ai-slim[ag-ui] 的 agent.to_ag_ui()(端口 8000);Google ADK 用 ag-ui-adk 的 add_adk_fastapi_endpoint(端口 8000);LlamaIndex 用 llama-index-protocols-ag-ui 的 get_ag_ui_workflow_router(端口 9000);Agno 用内置 AgentOS 的 AGUI 接口(端口 8000);Strands 用 ag_ui_strands 的 create_strands_app(端口 8000);Microsoft Agent Framework Python 用 agent-framework-ag-ui 的 add_agent_framework_fastapi_endpoint(端口 8000),.NET 用 Microsoft.Agents.AI.Hosting.AGUI.AspNetCore 的 MapAGUI(端口 8000);Mastra 内置在 @ag-ui/mastra 中,走 Next.js dev server;MCP Apps 用 BuiltInAgent 加 MCPAppsMiddleware(端口 3108)。
- 在 layout.tsx 中用 CopilotKitProvider 包裹应用,引入 @copilotkit/react/styles.css,并设置 runtimeUrl="/api/copilotkit" 与 agent="my_agent_name";agent 属性必须与 CopilotRuntime({ agents: { my_agent_name: ... } }) 中的 key 一致。
- 在 src/app/api/copilotkit/route.ts 创建 Next.js API route:从 @copilotkit/runtime 引入 CopilotRuntime、ExperimentalEmptyAdapter、copilotRuntimeNextJSAppRouterEndpoint,实例化 CopilotRuntime 并传入对应框架的 Agent 类实例(如 new SomeAgentClass({ url: "http://localhost:8000/" })),导出 POST 调用 copilotRuntimeNextJSAppRouterEndpoint({ runtime, serviceAdapter: new ExperimentalEmptyAdapter(), endpoint: "/api/copilotkit" }) 并返回 handleRequest(req)。
- 用 useAgent 管理共享状态:传入 name 与 initialState,从返回值取 state 和 setState;Agent 侧通过发出 STATE_SNAPSHOT 事件更新前端,各框架具体写法见对应集成指南。
- 用 useFrontendTool 注册前端工具:提供 name、parameters(含 name/description/required)与 handler,在 handler 中执行前端逻辑(如 setThemeColor)。
- 用 useRenderToolCall 实现生成式 UI:提供 name、description、parameters 与 render 回调,在 render 中根据 args 渲染组件(如 WeatherCard)。
- 用 useHumanInTheLoop 实现人在回路:提供 name、description 与 render 回调,render 中接收 respond 和 status 并渲染交互组件(如 MoonCard)。
- 需要查询实时 CopilotKit 文档或源码时使用插件内置的 copilotkit-docs MCP server 的 search-docs 与 search-code 工具;Claude Code 由插件 .mcp.json 自动配置,Codex 需手动配置(参见 copilotkit-debug skill 的 MCP setup)。
- 前端依赖统一使用 @copilotkit/react(hooks 与 CopilotSidebar、CopilotPopup 等 UI 组件)和 @copilotkit/runtime(CopilotRuntime、ExperimentalEmptyAdapter)。
适用边界 · 注意事项
- 不通过 AG-UI 协议接入、或不需要 CopilotKit 前端交互的场景不适用。
- 未确定 Agent 框架或语言时无法直接套用,需先完成选型。
- Codex 环境下使用 copilotkit-docs MCP 需先手动配置,未配置时无法使用该文档查询能力。