CopilotKit React Best Practices
简介
CopilotKit React Best Practices
原始描述
CopilotKit React best practices for agentic applications. Use when writing, reviewing, or refactoring React code that uses CopilotKit hooks (useAgent, useFro...
通过你的 Agent 添加
点「快速安装」复制一段指令,粘给你正在用的 Agent(Claude Code、Codex、OpenClaw 等),它会自己完成安装。
或者手动安装
包里是 SKILL.md 加作者附带的文件,解压后目录名就是技能名。
技能申请的权限由作者决定,装之前请确认它能读什么、能执行什么。
技能正文
name: copilotkit-react-patterns description: CopilotKit React best practices for agentic applications. Use when writing, reviewing, or refactoring React code that uses CopilotKit hooks (useAgent, useFrontendTool, useRenderTool, useHumanInTheLoop), providers (CopilotKitProvider), or chat UI components (CopilotChat, CopilotSidebar, CopilotPopup). Triggers on tasks involving agent integration, tool rendering, shared state, or generative UI in React. license: MIT metadata: author: copilotkit version: "1.0.0"
CopilotKit React Patterns
Best practices for building agentic React applications with CopilotKit. Contains 25 rules across 6 categories, prioritized by impact to guide code generation and refactoring.
When to Apply
Reference these guidelines when:
- Setting up CopilotKitProvider in a React application
- Using agent hooks (useAgent, useFrontendTool, useHumanInTheLoop)
- Rendering tool calls or custom messages from agents
- Managing shared state between UI and agents
- Building chat interfaces with CopilotChat, CopilotSidebar, or CopilotPopup
- Configuring suggestions for proactive agent assistance
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Provider Setup | CRITICAL | provider- |
| 2 | Agent Hooks | HIGH | agent- |
| 3 | Tool Rendering | HIGH | tool- |
| 4 | Context & State | MEDIUM | state- |
| 5 | Chat UI | MEDIUM | ui- |
| 6 | Suggestions | LOW | suggestions- |
Quick Reference
1. Provider Setup (CRITICAL)
provider-runtime-url- Always configure runtimeUrl to connect to your agent backendprovider-single-endpoint- Use useSingleEndpoint for v2 AG-UI protocolprovider-nested-providers- Scope agent configurations with nested CopilotKitProvidersprovider-tool-registration- Register tools via hooks inside provider, not as props when possible
2. Agent Hooks (HIGH)
agent-use-agent-updates- Specify update subscriptions to avoid unnecessary re-rendersagent-agent-id- Always pass agentId when running multiple agentsagent-context-description- Write descriptive context values for useAgentContextagent-frontend-tool-deps- Declare dependency arrays for useFrontendToolagent-stable-tool-handlers- Use useCallback for tool handler functions
3. Tool Rendering (HIGH)
tool-typed-args- Define Zod schemas for useRenderTool argstool-status-handling- Handle all three tool call statuses (inProgress, executing, complete)tool-wildcard-fallback- Register a wildcard renderer as fallback for unknown toolstool-render-vs-frontend- Use useRenderTool for display-only, useFrontendTool for side effectstool-component-hook- Prefer useComponent for simple component rendering from tool args
4. Context & State (MEDIUM)
state-minimal-context- Provide only relevant context to agents, not entire app statestate-structured-values- Use structured objects in useAgentContext, not serialized stringsstate-context-granularity- Split context into multiple useAgentContext calls by domainstate-avoid-stale-closures- Use functional state updates in tool handlers
5. Chat UI (MEDIUM)
ui-chat-layout- Choose CopilotSidebar for persistent chat, CopilotPopup for on-demandui-custom-labels- Always customize labels for your domain instead of defaultsui-welcome-screen- Provide a welcome screen with suggested promptsui-input-mode- Use appropriate inputMode for your use case
6. Suggestions (LOW)
suggestions-configure- Use useConfigureSuggestions to control suggestion generationsuggestions-context-driven- Provide rich context so suggestions are relevantsuggestions-loading-state- Handle suggestion loading states in UI
How to Use
Read individual rule files for detailed explanations and code examples:
rules/provider-runtime-url.md
rules/agent-use-agent-updates.md
rules/tool-typed-args.md
Each rule file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Additional context and references
Full Compiled Document
For the complete guide with all rules expanded: AGENTS.md
正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。