mcpField
C
copilotkit-develop

copilotkit-develop

github.com/copilotkit开发39602 次安装21 周前更新
代码仓库

关于 copilotkit-develop

copilotkit-develop 是用于开发 CopilotKit v2 应用的技能,涵盖运行时配置、React 界面和 Agent 交互,并提供查询最新文档与源代码的工具。

主要功能

  1. 01配置 CopilotKit v2 运行时
  2. 02React 聊天组件与 Hooks
  3. 03注册浏览器端 Agent 工具
  4. 04共享应用上下文并处理打断
  5. 05自定义工具调用渲染

适用场景

  1. 01配置 CopilotKit Agent 运行时
  2. 02为 React 应用添加聊天界面
  3. 03向 Agent 提供浏览器端函数
  4. 04在 Agent 执行期间处理人工审批

MCP 相关

包含 `copilotkit-docs` MCP 服务,提供 `search-docs` 和 `search-code` 工具,用于查询 CopilotKit 文档与源代码。

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/copilotkit/skills --skill copilotkit-develop

安装后在 Claude Code 中输入/copilotkit-develop确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

eval.yaml
sources.md
references/
api-surface.md
chat-customization.md
runtime-api.md

详细介绍

CopilotKit v2 开发技能

实时文档(MCP)

此插件包含一个 MCP 服务(copilotkit-docs),提供 search-docs 和 search-code 工具,用于查询 CopilotKit 的实时文档和源代码。

  • Claude Code: 插件会通过 .mcp.json 自动完成配置,无需进行设置。
  • Codex: 需要手动配置。请参阅 copilotkit-debug 技能中的设置说明。

架构概览

CopilotKit v2 基于 AG-UI 协议(@ag-ui/client / @ag-ui/core)构建。技术栈分为三层:

  1. 运行时(@copilotkit/runtime)——服务端。托管 Agent,处理 SSE/Intelligence 传输、中间件和转录。
  2. 核心(@copilotkit/core)——共享状态管理、工具注册表和建议引擎。应用不会直接导入它。
  3. React(@copilotkit/react)——Provider、聊天组件和 hooks。重新导出 @ag-ui/client 中的所有内容,因此应用只需一次导入。

工作流

1. 设置运行时(服务端)

创建 CopilotRuntime(或显式创建 CopilotSseRuntime / CopilotIntelligenceRuntime),并通过 createCopilotEndpoint(Hono)或 createCopilotEndpointExpress(Express)将其公开。

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. 使用 Provider 包装应用(客户端)
import { CopilotKitProvider } from "@copilotkit/react";

function App() {
  return (
    <CopilotKitProvider runtimeUrl="/api/copilotkit">
      <YourApp />
    </CopilotKitProvider>
  );
}
3. 添加聊天界面

使用 <CopilotChat>、<CopilotPopup> 或 <CopilotSidebar>:

import { CopilotChat } from "@copilotkit/react";

function ChatPage() {
  return <CopilotChat agentId="myAgent" />;
}
4. 注册前端工具

让 Agent 在浏览器中调用函数:

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. 共享应用上下文

向 Agent 提供运行时数据:

import { useAgentContext } from "@copilotkit/react";

useAgentContext({
  description: "The user's current shopping cart",
  value: cart, // any JSON-serializable value
});
6. 处理 Agent 中断

当 Agent 暂停以等待人工输入时:

import { useInterrupt } from "@copilotkit/react";

useInterrupt({
  render: ({ event, resolve }) => (
    <div>
      <p>{event.value.question}</p>
      <button onClick={() => resolve({ approved: true })}>Approve</button>
    </div>
  ),
});
7. 在聊天中渲染工具调用

在工具执行时显示自定义 UI:

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>;
  },
}, []);

快速参考:Hooks

Hook用途
useFrontendTool注册 Agent 可在浏览器中调用的工具
useComponent将 React 组件注册为在聊天中渲染的工具(useFrontendTool 的便捷封装)
useAgentContext与 Agent 共享可序列化为 JSON 的应用状态
useAgent获取指定 Agent ID 的 AbstractAgent 实例;订阅消息、状态和运行状态的变化
useInterrupt处理 Agent 发出的 on_interrupt 事件,支持渲染以及可选的处理器/筛选器
useHumanInTheLoop注册一个工具,该工具会暂停执行,直到用户通过渲染的 UI 作出响应
useRenderTool注册工具调用的渲染器(按名称或使用通配符 "*")
useDefaultRenderTool使用内置的可展开卡片 UI 注册通配符 "*" 渲染器
useRenderToolCall内部 Hook,返回一个函数,用于解析给定工具调用对应的渲染器
useRenderActivityMessage用于按类型渲染活动消息的内部 Hook
useRenderCustomMessages用于渲染自定义消息装饰器的内部 Hook
useSuggestions读取当前建议列表,并控制重新加载/清除
useConfigureSuggestions注册静态或动态(由大模型生成)的建议配置
useThreads列出、重命名、归档和删除 Intelligence 平台会话

快速参考:组件

组件用途
CopilotKitProvider根 Provider——配置运行时 URL、请求头、Agent 和错误处理器
CopilotChat连接到 Agent 的完整聊天界面(内嵌布局)
CopilotPopup带切换按钮的浮动弹窗聊天界面
CopilotSidebar带切换按钮的可折叠侧边栏聊天界面
CopilotChatView无头聊天视图,提供消息视图、输入框、滚动和建议的插槽
CopilotChatInput聊天输入文本区域,带发送/停止/转录控件
CopilotChatMessageView渲染消息列表
CopilotChatSuggestionView渲染建议标签

快速参考:运行时

导出项用途
CopilotRuntime自动检测运行时(委托给 SSE 或 Intelligence)
CopilotSseRuntime显式 SSE 模式运行时
CopilotIntelligenceRuntime支持持久化线程的 Intelligence 模式运行时
createCopilotEndpoint创建包含所有 CopilotKit 路由的 Hono 应用
createCopilotEndpointExpress创建包含所有 CopilotKit 路由的 Express 路由器
CopilotKitIntelligenceIntelligence 平台客户端配置

评论

支持 Markdown