Repository navigation
feat(vue): mirror React's useAgent thread scoping, remove thread cloning - #6234
Merged
Merged
Conversation
mme
requested review from
jpr5,
marthakelly,
ranst91 and
tylerslaton
as code owners
July 29, 2026 14:31
@copilotkit/a2ui-renderer
@copilotkit/agentcore-runner
@copilotkit/angular
@copilotkit/channels
@copilotkit/channels-core
@copilotkit/channels-discord
@copilotkit/channels-intelligence
@copilotkit/channels-slack
@copilotkit/channels-teams
@copilotkit/channels-telegram
@copilotkit/channels-ui
@copilotkit/channels-whatsapp
@copilotkit/core
@copilotkit/react-core
@copilotkit/react-native
@copilotkit/react-textarea
@copilotkit/react-ui
@copilotkit/runtime
@copilotkit/runtime-client-gql
@copilotkit/sdk-js
@copilotkit/shared
@copilotkit/sqlite-runner
@copilotkit/voice
@copilotkit/vue
@copilotkit/web-components
@copilotkit/web-inspector
commit: |
Contributor
|
Size Change: 0 B Total Size: 1.2 MB ℹ️ View Unchanged
|
Ports PR #6141's React contract to Vue and deletes the per-thread cloning Vue used instead. Cloning is gone: cloneForThread, getOrCreateThreadClone, getThreadClone and the module-level globalThreadCloneMap. Those clones were reachable only through a WeakMap, components had to look them up to find the agent actually in use, and nothing tied a clone's lifetime to the scope that created it. In its place, the same two-shape contract React now has: useAgent() // shared registry agent useAgent({ agentId }) // shared registry agent useAgent({ agentId, runtimeAgentId, threadId }) // private proxied agent UseAgentProps becomes a base plus a two-branch union, so every partial set — { agentId, threadId }, { agentId, runtimeAgentId }, { runtimeAgentId, threadId } — is a compile error, with the same three runtime guards and the same messages as React for callers TypeScript doesn't reach. Vue's branches take MaybeRefOrGetter where React takes plain values; the shapes are otherwise identical. Thread resolution mirrors React exactly: an explicit `threadId` prop wins, otherwise the chat configuration's thread gated on `hasExplicitThreadId`, so a ThreadsProvider-minted placeholder UUID never overwrites the agent's own. Two Vue-specific details, both load-bearing: - The pin watcher's first source is `() => agent.value`, not `agent`. Vue sets `forceTrigger` when any array watch source is a shallow ref, which would re-run the pin on every `triggerRef(agent)` — i.e. every streamed message — and re-pin the inherited thread over one CopilotChat had deliberately set for the chat it renders. Two suites cover this ("uses the explicit agentId and threadId over inherited configuration"). - CopilotChat assigns `agent.threadId` inside its /connect watcher rather than a separate one, because CopilotKitCore.connectAgent reads that field synchronously to decide whether a restore is fresh; a later assignment would let /connect address the previous thread. React does the same, in the same place. CopilotChatMessageView resolves the registry agent directly instead of consulting the clone map, and reads `copilotkit.agents` so it recomputes when the registry changes. Tests: use-agent-thread-isolation.test.ts covered clone semantics that no longer exist; use-agent-thread-pinning.test.ts replaces it with the new invariants — one instance per agentId, config-thread pinning gated on explicitness, and all three all-or-nothing guards. Four component suites used getThreadClone purely as a lookup and now read from the registry. MockMCPProxyAgent recorded addMessage only inside its clone() override, so those assertions passed only because cloning existed; the recording moves onto the class. clone() itself is left intact everywhere, since CopilotKitCore's SuggestionEngine still clones agents (packages/core/src/core/suggestion-engine.ts). Call-site enumeration: cloneForThread and getOrCreateThreadClone were module-private, zero references. getThreadClone and globalThreadCloneMap were used by CopilotChatMessageView (rewritten) and 4 test files (retargeted); zero remain, including in prose. useAgent's other callers, use-capabilities and use-interrupt, pass `{ agentId }` only and match the unscoped branch unchanged. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
mme
force-pushed
the
feat/vue-useagent-thread-scoping
branch
from
July 29, 2026 14:40
73ab1e1 to
2d7f188
Compare
tylerslaton
approved these changes
Jul 29, 2026
This was referenced Sep 10, 2026
BenTaylorDev
added a commit
that referenced
this pull request
Sep 10, 2026
…e reference (#7025) ## Summary The React Native `useAgent` reference page still documents the pre-`1.64.2` hook. Parameters list only `agentId` and `updates`, and the signature returns `{ agent: AbstractAgent }` with no `isReady`. React Native re-exports react-core's `useAgent` verbatim (`packages/react-native/src/headless.ts:53`, re-exporting from `@copilotkit/react-core/v2/headless`), so it is the same hook with the same props. The page is simply stale. This adds: - `threadId`, `runtimeAgentId` and `throttleMs` parameters - the `isReady` return value, and `isReady` in the signature - the `@copilotkit/react-native/headless` import note - the two valid option shapes, and a thread-scoped example - `Optional Throttling`, `Readiness` and `Thread Scoping` behavior bullets Content mirrors #6388, which makes the equivalent changes to the React page, so the two pages stay consistent. No overlap in files. Docs only. No source or public API change. ## Context Reported by a community user in #6125 for the React page: the thread-scoping API shipped in `v1.64.2` (#6141) and the Vue mirror in `v1.68.0` (#6234), but the docs site was never updated. #6388 covers the React page; this covers React Native. ## Testing Verified against a live `next dev` render of shell-docs on `origin/main` (`35c1e801ae`), with the edited page in place. **1. MDX compiles with the real compiler** (`@mdx-js/mdx` from `showcase/shell-docs/node_modules`, the untouched pages as controls): ``` OK .../worktrees/docs-rn-useagent/.../react-native/hooks/useAgent.mdx <- edited OK .../showcase/shell-docs/.../react-native/hooks/useAgent.mdx <- control OK .../showcase/shell-docs/.../hooks/useAgent.mdx <- control exit=0 ``` **2. Component tags balance** (no unclosed `<PropertyReference>`, which renders as silently swallowed content): ``` open: 23 close: 23 self-closing: 0 BALANCED fences: 20 (even=ok) ``` **3. Page serves 200 and renders the new content** (`next dev`, port 3003): ``` 200 /reference/react-native/hooks/useAgent threadId: 17 runtimeAgentId: 10 throttleMs: 5 isReady: 17 Thread-scoped Agent: 2 Thread Scoping: 2 react-native/headless: 2 preview-: 2 error markers: (none) ``` No `Unhandled Runtime Error`, `Build Error`, `Failed to compile`, or error overlay in the served HTML. **4. Prop descriptions actually render**, checked against the untouched React page as a negative control. The new text is present on the edited page and correctly absent on the React page, which is what #6388 adds: ``` === react-native (edited) === ID of the agent to retrieve PRESENT Controls which agent changes trigger PRESENT Thread ID for a private proxied agent PRESENT Unique identifier for the agent instance PRESENT === react (untouched control) === Thread ID for a private proxied agent absent ``` Not run: `showcase/shell-docs` `npm run dev` via its `predev` script fails in this environment on a pre-existing missing `js-yaml` in `showcase/harness`, unrelated to this change. I ran `next dev` directly instead, which is why the generators (registry, search index) did not run. Those generate demo and search content, not reference pages. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Documentation** - Expanded `useAgent` reference documentation to cover shared and thread-scoped agent resolution. - Documented private proxied agents, headless imports, required option combinations, and runtime/type validation. - Added guidance for readiness states, provisional agents, connection failures, and thread-scoped usage. - Documented the `isReady` return value and `throttleMs` configuration for controlling update frequency. - Clarified agent ID resolution, including provider-level configuration and default fallback behavior. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Vue's
useAgentimplemented per-thread agent cloning — a mechanism React never had. Passing athreadIdsilently handed you a copy of the agent:The clones lived in a module-level
WeakMap(globalThreadCloneMap), so:CopilotChatMessageViewcalledgetThreadClone(registryAgent, config.threadId) ?? registryAgentjust to find the agent actually being rendered.getThreadClone/globalThreadCloneMapwere exported from the module purely so components could do that lookup.Meanwhile React grew an explicit contract for the same use case in #6141: a private proxied agent, registered under a local
agentIdand routed to aruntimeAgentId.Change
Deletes cloning entirely and ports React #6141's contract to Vue.
cloneForThread,getOrCreateThreadClone,getThreadCloneandglobalThreadCloneMapare gone — zero references remain, including in prose.UseAgentPropsbecomes a base plus a two-branch union, with the same all-or-nothing rule React now enforces:Every partial set —
{ agentId, threadId },{ agentId, runtimeAgentId },{ runtimeAgentId, threadId }— is a compile error, backed by the same three runtime guards with the same messages for callers TypeScript doesn't reach.Parity with #6141
agentId/threadId/runtimeAgentId, all requiredstringMaybeRefOrGetter<string>agentId?: string,threadId?: undefined,runtimeAgentId?: undefinedhasExplicitThreadIdTwo Vue-specific details
Both are load-bearing and were found by tests failing, not by inspection:
The pin watcher's first source is
() => agent.value, notagent. Vue setsforceTriggerwhen any array watch source is a shallow ref, so passing the ref directly re-ran the pin on everytriggerRef(agent)— i.e. every streamed message — re-pinning the inherited thread over oneCopilotChathad deliberately set for the chat it renders. Two existing suites cover this (uses the explicit agentId and threadId over inherited configuration). React has no equivalent hazard because effect deps compare by identity.CopilotChatassignsagent.threadIdinside its/connectwatcher, not a separate one.CopilotKitCore.connectAgentreads that field synchronously (run-handler.ts) to decide whether a restore is fresh, so a later assignment lets/connectaddress the previous thread — skipping the messages/state reset and re-stamping its restore key with the stale id. Same placement as React'sCopilotChat.CopilotChatMessageViewnow resolves the registry agent directly instead of consulting the clone map, and readscopilotkit.agentsso it recomputes when the registry changes.What callers see
One agent per
agentId— the model React has always had. Thread isolation is now explicit instead of implicit: ask for it and you get a real, separately-registered agent rather than a copy that appears out of nowhere.Nothing in this repo needed updating:
CopilotChat,use-capabilities,use-interruptand all six example apps already used{ agentId }.<CopilotChat agentId threadId>is unchanged for consumers.Tests
use-agent-thread-isolation.test.ts(433 lines) covered clone semantics that no longer exist; it's replaced byuse-agent-thread-pinning.test.ts, which pins the new invariants — one instance peragentIdnever a copy, config-thread pinning gated on explicitness, and all three all-or-nothing guards.Four component suites used
getThreadClonepurely as a lookup to find the agent under test and now read from the registry.MockMCPProxyAgentrecordedaddMessageonly inside itsclone()override, so those assertions were passing only because cloning existed. The recording moves onto the class.clone()itself is left intact everywhere —CopilotKitCore'sSuggestionEnginestill clones agents (packages/core/src/core/suggestion-engine.ts), so removing those overrides would have planted a latent trap.Deliberately not included
Found while reviewing this area, real, but out of scope — each wants its own change:
useAgent's header watcher replacesagent.headersinstead of callingcopilotkit.applyHeadersToAgent(), dropping per-agent construction-time headers. Regresses 🐛 Bug:HttpAgent.headersare silently overwritten when the agent is registered viaagents__unsafe_dev_only#5635 in Vue; React does this correctly.credentialsnever reach a provisional agent.onAgentsChangedsubscription anywhere inpackages/vue, so() => copilotkit.value.agentsas a watch source never re-evaluates on registry change./connectis skipped for a plainHttpAgent— thehasCustomConnectprototype comparison matches every real agent. Vue-only, no React equivalent.CopilotThreadsDrawer.ssr.test.tsis a latent flake (5s timeout on a dynamic import; passes in isolation).