src/vs/platform/agentHost/node/activeClientState.ts

195 LOC · 180 covered · 15 uncovered · 51 ranges · 1441 concepts · 25 introducers · 693 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- activeClientState.ts ×17
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { equals } from '../../../base/common/objects.js';
7 > import type { ToolDefinition } from '../common/state/protocol/state.js';
8 >
9 > /**
10 > * Structural view of the active client's contributions that, when changed,
11 > * requires the underlying SDK session to be restarted / rebound. The
12 > * `clientId` is deliberately excluded — a window reload that produces a new
13 > * `clientId` with an identical tool list does NOT require a restart.
14 > */
15 > export interface IActiveClientStructuralSnapshot {
16 > readonly tools: readonly ToolDefinition[];
17 > }
18 >
19 > /**
20 > * Deep-equal two client-tool snapshots on `name + description + inputSchema`.
21 > * `undefined` and `[]` compare equal. Order-insensitive.
22 > *
23 > * Single shared implementation for the agent-host providers — previously
24 > * duplicated as `snapshotsEqual` (Claude client-tools model) and an inline
25 > * loop in the Copilot `ActiveClient` staleness check.
26 > */
27 > export function structuralToolsEqual(
28 > a: readonly ToolDefinition[] | undefined, activeClientState.ts ×1
29 > b: readonly ToolDefinition[] | undefined,
30 > ): boolean {
31 > const aa = a ?? [];
32 > const bb = b ?? [];
33 > if (aa.length !== bb.length) {
34 > return false; activeClientState.ts ×1
35 > }
36 > const byName = new Map<string, ToolDefinition>(); activeClientState.ts ×2
37 > for (const t of aa) {
38 > byName.set(t.name, t); activeClientState.ts ×3
39 > }
40 > for (const t of bb) { activeClientState.ts ×2
41 > const prev = byName.get(t.name); activeClientState.ts ×3
42 > if (!prev) {
43 > return false; activeClientState.ts ×1
44 > }
45 > if (prev.description !== t.description) { activeClientState.ts ×2
46 > return false; activeClientState.ts ×2
47 > }
48 > if (!equals(prev.inputSchema, t.inputSchema)) { activeClientState.ts ×2
49 > return false; activeClientState.ts ×2
50 > }
52 > return true; activeClientState.ts ×1
53 > }
55 > /**
56 > * A per-session registry of the tools contributed by each active client,
57 > * keyed by `clientId` and kept in insertion order. Backs the multi-active-client
58 > * tool model shared by the agent-host providers (Copilot, Claude, Codex):
59 > * each provider stores one of these per session and exposes the
60 > * {@link merged} view to its SDK while routing tool calls back to the
61 > * {@link ownerOf | owning client}.
62 > *
63 > * Deduplication of {@link merged} is by tool `name`, first-inserted-client
64 > * wins, so the merged order and the owner of any given tool name are
65 > * deterministic regardless of how many clients contribute it.
66 > */
67 > export class ActiveClientToolSet {
68 > private readonly _byClient = new Map<string, readonly ToolDefinition[]>(); activeClientState.ts ×1
70 > /** Number of clients currently contributing tools. */
71 > get size(): number {
72 return this._byClient.size;
73 }
75 > /** Whether `clientId` currently contributes tools. */
76 > has(clientId: string): boolean {
77 return this._byClient.has(clientId);
78 }
80 > /** The client ids currently contributing tools, in insertion order. */
81 > clientIds(): IterableIterator<string> {
82 > return this._byClient.keys(); copilotAgent.ts ×3
83 > }
85 > /** This client's contributed tools, or an empty array when absent. */
86 > get(clientId: string): readonly ToolDefinition[] {
87 > return this._byClient.get(clientId) ?? []; activeClientState.ts ×1
88 > }
90 > /**
91 > * Replace `clientId`'s contributed tools (full replacement). A new
92 > * `clientId` is appended after existing ones; re-setting an existing
93 > * `clientId` keeps its insertion position so merged ordering and tool
94 > * ownership stay stable across updates.
95 > */
96 > set(clientId: string, tools: readonly ToolDefinition[]): void {
97 > this._byClient.set(clientId, tools); activeClientState.ts ×1
98 > }
100 > /** Remove `clientId`'s contribution. Returns whether anything was removed. */
101 > delete(clientId: string): boolean {
102 > return this._byClient.delete(clientId); activeClientState.ts ×1
103 > }
105 > /**
106 > * The union of every client's tools, deduplicated by `name` with the
107 > * first-inserted contributor winning. Order follows client insertion
108 > * order, then per-client tool order.
109 > */
110 > merged(): readonly ToolDefinition[] {
111 > const seen = new Set<string>(); activeClientState.ts ×2
112 > const result: ToolDefinition[] = [];
113 > for (const tools of this._byClient.values()) {
114 > for (const tool of tools) { activeClientState.ts ×2
115 > if (seen.has(tool.name)) { activeClientState.ts ×2
116 > continue; activeClientState.ts ×1
117 > }
118 > seen.add(tool.name); activeClientState.ts ×2
119 > result.push(tool);
120 > }
122 > return result; activeClientState.ts ×2
123 > }
125 > /**
126 > * The `clientId` that owns the tool named `toolName`, or `undefined` when
127 > * no active client provides it. When `preferredClientId` currently provides
128 > * the tool it wins; otherwise the first-inserted contributor wins.
129 > */
130 > ownerOf(toolName: string, preferredClientId?: string): string | undefined {
131 > if (preferredClientId && this.get(preferredClientId).some(tool => tool.name === toolName)) { activeClientState.ts ×2
132 > return preferredClientId; activeClientState.ts ×1
133 > }
134 > for (const [clientId, tools] of this._byClient) { activeClientState.ts ×1
135 > if (tools.some(tool => tool.name === toolName)) { activeClientState.ts ×2
136 > return clientId; activeClientState.ts ×1
137 > }
139 > return undefined; activeClientState.ts ×1
142 > /**
143 > * Structural comparison of the current {@link merged} tools against a
144 > * previously-applied snapshot (`name + description + inputSchema`,
145 > * order-insensitive). Returns `true` when no SDK restart is required.
146 > */
147 > structuralEquals(applied: readonly ToolDefinition[] | undefined): boolean {
148 > return structuralToolsEqual(this.merged(), applied); copilotAgent.ts ×3
149 > }
151 >
152 > /**
153 > * Live, mutable holder for the active client's identity (`clientId`) and the
154 > * structural tool snapshot it contributes. Shared between the Copilot and
155 > * Claude providers so a single long-lived instance per session URI survives
156 > * SDK-session dispose / resume cycles.
157 > *
158 > * The `clientId` is read at tool-call **stamp time** (not cached per turn) so
159 > * that a window reload — which connects with a new `clientId` and re-pushes an
160 > * identical tool list — stamps subsequent client tool calls with the new,
161 > * live `clientId` instead of a frozen one baked in at session creation.
162 > */
163 > export class ActiveClientState {
164 private _clientId: string | undefined = undefined;
165 private _tools: readonly ToolDefinition[] = [];
167 > /** Live owning client id, or `undefined` when no client is currently connected. */
168 > get clientId(): string | undefined {
169 return this._clientId;
170 }
172 > /** Structural state (tool definitions). Changing these requires an SDK restart/rebind. */
173 > get tools(): readonly ToolDefinition[] {
174 return this._tools;
175 }
177 > /**
178 > * Replace the owning `clientId` (`undefined` when no client is connected)
179 > * and the contributed tool list. A `clientId`-only change does NOT mark
180 > * structural dirt (see {@link structuralEquals}).
181 > */
182 > update(clientId: string | undefined, tools: readonly ToolDefinition[]): void {
183 this._clientId = clientId;
184 this._tools = tools;
185 }
187 > /**
188 > * Structural comparison of the live tools against a previously-applied
189 > * snapshot (`name + description + inputSchema`, order-insensitive).
190 > * Returns `true` when no SDK restart is required.
191 > */
192 > structuralEquals(applied: IActiveClientStructuralSnapshot): boolean {
193 return structuralToolsEqual(this._tools, applied.tools);
194 }