This solution has been AI generated
You can isolate tools and message scope per sub‑agent in CopilotKit v2. Register each frontend action/tool with an agentId and execute sub‑agents through the runtime so only their scoped tools and histories are used. For chaining, model UI interrupts as tool calls and feed toolOutputs back to continue the run.
1) Scoped message history and tool isolation
- Scope frontend tools to a sub‑agent (v2):
import { useFrontendTool } from "@copilotkit/react-core/v2";
useFrontendTool({
name: "performSpecializedTaskA",
description: "Only Sub‑Agent A",
agentId: "sub-agent-a-id",
parameters: [{ name: "data", type: "string" }],
handler: async ({ data }) => {},
});
- Run the sub‑agent via CopilotKit so its scoped tools are bundled automatically:
await copilotkit.runAgent({ agent: subAgentA });
- Keep message scope per sub‑agent:
- Maintain separate histories/threads and stream each sub‑agent’s messages directly (supported). You can also subscribe/forward events to an orchestrator if you need a combined view.
- In v2, you manage thread IDs yourself; pass a distinct threadId per sub‑agent when rendering/connecting so histories don’t leak.
Frontend actions remain supported: when a scoped tool is invoked, the client handles the ActionExecutionMessage and runs the handler; tools without agentId are global, tools with agentId are visible only to that agent.
2) Recommended multi‑agent architecture
- Sub‑agents: Wrap each in an agent (e.g., ADK/AG‑UI/LangGraph) and let the orchestrator choose which to run. Avoid switching agentId dynamically inside one hook; keep separate useAgent hooks per sub‑agent.
- History: Both shared and isolated are supported. Given your leakage issue, prefer isolated histories per sub‑agent and optionally mirror important messages into the orchestrator’s store.