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/Intelligence传输、中间件、转录。
@copilotkit/runtime - Core()——共享状态管理、工具注册、建议引擎。无需被应用直接导入。
@copilotkit/core - React()——提供者、聊天组件、钩子。重新导出
@copilotkit/react的所有内容,因此应用只需一次导入。@ag-ui/client
Workflow
工作流程
1. Set Up the Runtime (Server)
1. 设置Runtime(服务器)
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
快速参考:钩子
| 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 |
| 钩子名称 | 用途 |
|---|---|
| 注册Agent可在浏览器中调用的工具 |
| 将React组件注册为聊天渲染工具( |
| 与Agent共享可JSON序列化的应用状态 |
| 根据Agent ID获取 |
| 通过渲染+可选处理器/过滤器处理来自Agent的 |
| 注册一个工具,在用户通过渲染UI响应前暂停执行 |
| 为工具调用注册渲染器(按名称或通配符 |
| 使用内置可展开卡片UI注册通配符 |
| 内部钩子,返回一个函数以解析给定工具调用的正确渲染器 |
| 用于按类型渲染活动消息的内部钩子 |
| 用于渲染自定义消息装饰器的内部钩子 |
| 读取当前建议列表并控制重新加载/清除 |
| 注册静态或动态(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 |
| 组件名称 | 用途 |
|---|---|
| 根提供者——配置runtime URL、请求头、Agent、错误处理器 |
| 连接到Agent的完整聊天界面(内联布局) |
| 带切换按钮的浮动弹窗聊天 |
| 带切换按钮的可折叠侧边栏聊天 |
| 无头聊天视图,包含消息视图、输入框、滚动、建议的插槽 |
| 带发送/停止/转录控件的聊天输入文本框 |
| 渲染消息列表 |
| 渲染建议胶囊 |
Quick Reference: Runtime
快速参考: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 |
| 导出项名称 | 用途 |
|---|---|
| 自动检测型runtime(委托给SSE或Intelligence) |
| 显式SSE模式runtime |
| 带持久化线程的Intelligence模式runtime |
| 创建包含所有CopilotKit路由的Hono应用 |
| 创建包含所有CopilotKit路由的Express路由器 |
| Intelligence平台客户端配置 |