copilotkit-develop
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCopilotKit v2 Development Skill
CopilotKit v2 开发技能
Live Documentation (MCP)
实时文档 (MCP)
This plugin includes an MCP server () that provides and tools for querying live CopilotKit documentation and source code.
copilotkit-docssearch-docssearch-code- Claude Code: Auto-configured by the plugin's -- no setup needed.
.mcp.json - Codex: Requires manual configuration. See the copilotkit-debug skill for setup instructions.
该插件包含一个 MCP 服务器(),提供 和 工具,用于查询实时的 CopilotKit 文档和源代码。
copilotkit-docssearch-docssearch-code- Claude Code: 由插件的 自动配置,无需额外设置。
.mcp.json - Codex: 需要手动配置,参考 copilotkit-debug 技能 查看配置说明。
Architecture Overview
架构概览
CopilotKit v2 is built on the AG-UI protocol ( / ). The stack has three layers:
@ag-ui/client@ag-ui/core- Runtime () -- Server-side. Hosts agents, handles SSE/Intelligence transport, middleware, transcription.
@copilotkit/runtime - Core () -- Shared state management, tool registry, suggestion engine. Not imported directly by apps.
@copilotkit/core - React () -- Provider, chat components, hooks. Re-exports everything from
@copilotkit/reactso apps need only one import.@ag-ui/client
CopilotKit v2 基于 AG-UI 协议( / )构建,技术栈分为三层:
@ag-ui/client@ag-ui/core- 运行时(Runtime)()—— 服务端,托管 Agent、处理 SSE/智能传输、中间件、转写功能。
@copilotkit/runtime - 核心(Core)()—— 共享状态管理、工具注册、推荐引擎,应用无需直接导入。
@copilotkit/core - React()—— Provider、聊天组件、Hooks,重新导出
@copilotkit/react的所有内容,因此应用只需导入这一个包。@ag-ui/client
Workflow
工作流
1. Set Up the Runtime (Server)
1. 配置运行时(服务端)
Create a (or the explicit / ) and expose it via (Hono) or (Express).
CopilotRuntimeCopilotSseRuntimeCopilotIntelligenceRuntimecreateCopilotEndpointcreateCopilotEndpointExpressts
import { CopilotRuntime, createCopilotEndpoint } from "@copilotkit/runtime";
import { LangGraphAgent } from "@ag-ui/langgraph";
const runtime = new CopilotRuntime({
agents: {
myAgent: new LangGraphAgent({ /* ... */ }),
},
});
const app = createCopilotEndpoint({
runtime,
basePath: "/api/copilotkit",
});创建 (或显式使用 / ),并通过 (Hono)或 (Express)暴露接口。
CopilotRuntimeCopilotSseRuntimeCopilotIntelligenceRuntimecreateCopilotEndpointcreateCopilotEndpointExpressts
import { CopilotRuntime, createCopilotEndpoint } from "@copilotkit/runtime";
import { LangGraphAgent } from "@ag-ui/langgraph";
const runtime = new CopilotRuntime({
agents: {
myAgent: new LangGraphAgent({ /* ... */ }),
},
});
const app = createCopilotEndpoint({
runtime,
basePath: "/api/copilotkit",
});2. Wrap Your App with the Provider (Client)
2. 使用 Provider 包裹你的应用(客户端)
tsx
import { CopilotKitProvider } from "@copilotkit/react";
function App() {
return (
<CopilotKitProvider runtimeUrl="/api/copilotkit">
<YourApp />
</CopilotKitProvider>
);
}tsx
import { CopilotKitProvider } from "@copilotkit/react";
function App() {
return (
<CopilotKitProvider runtimeUrl="/api/copilotkit">
<YourApp />
</CopilotKitProvider>
);
}3. Add a Chat UI
3. 添加聊天 UI
Use , , or :
<CopilotChat><CopilotPopup><CopilotSidebar>tsx
import { CopilotChat } from "@copilotkit/react";
function ChatPage() {
return <CopilotChat agentId="myAgent" />;
}使用 、 或 :
<CopilotChat><CopilotPopup><CopilotSidebar>tsx
import { CopilotChat } from "@copilotkit/react";
function ChatPage() {
return <CopilotChat agentId="myAgent" />;
}4. Register Frontend Tools
4. 注册前端工具
Let the agent call functions in the browser:
tsx
import { useFrontendTool } from "@copilotkit/react";
import { z } from "zod";
useFrontendTool({
name: "highlightCell",
description: "Highlight a spreadsheet cell",
parameters: z.object({ row: z.number(), col: z.number() }),
handler: async ({ row, col }) => {
highlightCell(row, col);
return "done";
},
});让 Agent 可以调用浏览器内的函数:
tsx
import { useFrontendTool } from "@copilotkit/react";
import { z } from "zod";
useFrontendTool({
name: "highlightCell",
description: "Highlight a spreadsheet cell",
parameters: z.object({ row: z.number(), col: z.number() }),
handler: async ({ row, col }) => {
highlightCell(row, col);
return "done";
},
});5. Share Application Context
5. 共享应用上下文
Provide runtime data to the agent:
tsx
import { useAgentContext } from "@copilotkit/react";
useAgentContext({
description: "The user's current shopping cart",
value: cart, // any JSON-serializable value
});向 Agent 提供运行时数据:
tsx
import { useAgentContext } from "@copilotkit/react";
useAgentContext({
description: "The user's current shopping cart",
value: cart, // any JSON-serializable value
});6. Handle Agent Interrupts
6. 处理 Agent 中断
When an agent pauses for human input:
tsx
import { useInterrupt } from "@copilotkit/react";
useInterrupt({
render: ({ event, resolve }) => (
<div>
<p>{event.value.question}</p>
<button onClick={() => resolve({ approved: true })}>Approve</button>
</div>
),
});当 Agent 暂停运行等待人工输入时:
tsx
import { useInterrupt } from "@copilotkit/react";
useInterrupt({
render: ({ event, resolve }) => (
<div>
<p>{event.value.question}</p>
<button onClick={() => resolve({ approved: true })}>Approve</button>
</div>
),
});7. Render Tool Calls in Chat
7. 在聊天中渲染工具调用
Show custom UI when tools execute:
tsx
import { useRenderTool } from "@copilotkit/react";
import { z } from "zod";
useRenderTool({
name: "searchDocs",
parameters: z.object({ query: z.string() }),
render: ({ status, parameters, result }) => {
if (status === "executing") return <Spinner>Searching {parameters.query}...</Spinner>;
if (status === "complete") return <Results data={result} />;
return <div>Preparing...</div>;
},
}, []);工具执行时展示自定义 UI:
tsx
import { useRenderTool } from "@copilotkit/react";
import { z } from "zod";
useRenderTool({
name: "searchDocs",
parameters: z.object({ query: z.string() }),
render: ({ status, parameters, result }) => {
if (status === "executing") return <Spinner>Searching {parameters.query}...</Spinner>;
if (status === "complete") return <Results data={result} />;
return <div>Preparing...</div>;
},
}, []);Quick Reference: Hooks
快速参考:Hooks
| Hook | Purpose |
|---|---|
| Register a tool the agent can call in the browser |
| Register a React component as a chat-rendered tool (convenience wrapper around |
| Share JSON-serializable application state with the agent |
| Get the |
| Handle |
| Register a tool that pauses execution until the user responds via a rendered UI |
| Register a renderer for tool calls (by name or wildcard |
| Register a wildcard |
| Internal hook returning a function to resolve the correct renderer for a given tool call |
| Internal hook for rendering activity messages by type |
| Internal hook for rendering custom message decorators |
| Read the current suggestion list and control reload/clear |
| Register static or dynamic (LLM-generated) suggestion configs |
| List, rename, archive, and delete Intelligence platform threads |
| Hook | 用途 |
|---|---|
| 注册 Agent 可在浏览器中调用的工具 |
| 注册 React 组件作为聊天渲染的工具( |
| 向 Agent 共享可序列化为 JSON 的应用状态 |
| 获取指定 Agent ID 对应的 |
| 处理 Agent 触发的 |
| 注册工具,暂停执行直到用户通过渲染的 UI 给出响应 |
| 注册工具调用的渲染器(可按名称匹配或使用通配符 |
| 使用内置的可展开卡片 UI 注册通配符 |
| 内部 Hook,返回为指定工具调用匹配正确渲染器的函数 |
| 内部 Hook,用于按类型渲染活动消息 |
| 内部 Hook,用于渲染自定义消息装饰器 |
| 读取当前推荐列表,控制重载/清空操作 |
| 注册静态或动态(LLM 生成)的推荐配置 |
| 列出、重命名、归档和删除 Intelligence 平台的对话线程 |
Quick Reference: Components
快速参考:组件
| Component | Purpose |
|---|---|
| Root provider -- configures runtime URL, headers, agents, error handler |
| Full chat interface connected to an agent (inline layout) |
| Chat in a floating popup with toggle button |
| Chat in a collapsible sidebar with toggle button |
| Headless chat view with slots for message view, input, scroll, suggestions |
| Chat input textarea with send/stop/transcribe controls |
| Renders the message list |
| Renders suggestion pills |
| 组件 | 用途 |
|---|---|
| 根 Provider,用于配置运行时地址、请求头、Agent、错误处理函数 |
| 对接指定 Agent 的完整聊天界面(行内布局) |
| 悬浮弹窗形式的聊天界面,自带切换按钮 |
| 可折叠侧边栏形式的聊天界面,自带切换按钮 |
| 无样式聊天视图,提供消息列表、输入框、滚动、推荐项的插槽 |
| 聊天输入文本框,自带发送/停止/转写控制按钮 |
| 渲染消息列表 |
| 渲染推荐胶囊按钮 |
Quick Reference: Runtime
快速参考:运行时
| Export | Purpose |
|---|---|
| Auto-detecting runtime (delegates to SSE or Intelligence) |
| Explicit SSE-mode runtime |
| Intelligence-mode runtime with durable threads |
| Create a Hono app with all CopilotKit routes |
| Create an Express router with all CopilotKit routes |
| Intelligence platform client configuration |
| 导出项 | 用途 |
|---|---|
| 自动适配的运行时(自动委托给 SSE 或 Intelligence 模式) |
| 显式指定 SSE 模式的运行时 |
| 支持持久化线程的 Intelligence 模式运行时 |
| 创建包含所有 CopilotKit 路由的 Hono 应用 |
| 创建包含所有 CopilotKit 路由的 Express 路由器 |
| Intelligence 平台客户端配置 |