agentPeerChats.ts ×25

Frontier kind: Code frontier

unlabeled · c_9874931354a3

664 tests · 10355 LOC · 37 files · introduces 0 tests · 131 LOC · 1 file

Introduces — evidence that enters the hierarchy at this concept

Code
25 ranges131 lines · 1 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
657 ranges10355 lines · 37 files · Browse complete extent
All tests (intent)
664 testsBrowse complete intent

Neighbourhood graph

The orange circle is the focus. Violet and green circles are every ancestor and descendant, broader and narrower, at any distance; blue squares and pink diamonds are the introduced files and exact introduced tests of every visible concept, not only the focus's. Arrows point from broader to narrower concepts and bridge only concepts omitted from this view. Undirected links show source or test introduction. Concept and file size follows LOC; exact test nodes use test-count units.

Introduced files, introduced tests, and structurally relevant concept specialization

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 native relationship evidence on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the native relationship evidence on this page while the interactive map is unavailable.

Native relationship evidence

Every exact file and test below is linked only from the concept that introduces it.

Introduced tests

Every collected test enters the hierarchy at exactly one concept.

No tests are introduced at this concept. Its intent tests are introduced by other concepts.

Introduced code

Every collected source range enters the hierarchy at exactly one concept.

1 file ranked by introduced lines: 131 introduced LOC across 25 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

src/vs/platform/agentHost/node/agentPeerChats.ts 131 introduced LOC · 25 ranges

Open complete file

1 > /*--------------------------------------------------------------------------------------------- agentPeerChats.ts
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 { Disposable, DisposableMap, IDisposable } from '../../../base/common/lifecycle.js';
7 > import { renderResponseMarkdown, truncateMiddle } from '../common/agentHostConversationContext.js';
8 > import { type ActiveTurn, type ModelSelection, type Turn } from '../common/state/protocol/state.js';
9 >
10 > const SIDE_CHAT_CONTEXT_START = '<side-chat-context>';
11 > const SIDE_CHAT_CONTEXT_END = '</side-chat-context>';
12 > const SIDE_CHAT_CONTEXT_LENGTH_PREFIX = 'length=';
13 > const SIDE_CHAT_GUIDANCE = 'This is a side conversation. Prefer explanation over action; do not make changes or carry out work unless the user explicitly asks.';
14 > export const MAX_SIDE_CHAT_CONTEXT_CHARS = 20_000;
15 >
16 > export interface IPersistedSideChat {
17 > readonly source: string;
18 > readonly turnId: string;
19 > readonly selection?: { readonly text: string; readonly responsePartId?: string };
20 > readonly providerAnchorTurnId?: string;
21 > readonly inheritedTurnCount: number;
22 > readonly partialResponse?: string;
23 > readonly context?: string;
24 > }
25 >
26 > export function buildSideChatSourceContext(turns: readonly Turn[], activeTurn?: ActiveTurn): string | undefined {
27 const blocks: string[] = [];
28 for (const turn of turns) {
44 return conversation.length > MAX_SIDE_CHAT_CONTEXT_CHARS ? truncateMiddle(conversation, MAX_SIDE_CHAT_CONTEXT_CHARS) : conversation;
45 }
47 > export function getSideChatPartialResponse(activeTurn: ActiveTurn | undefined): string | undefined {
48 if (!activeTurn) {
49 return undefined;
52 return responseMarkdown ? truncateMiddle(responseMarkdown, MAX_SIDE_CHAT_CONTEXT_CHARS) : undefined;
53 }
55 > export function buildBoundedSideChatSourceContext(turns: readonly Turn[], turnId: string, activeTurn?: ActiveTurn): string | undefined {
56 if (activeTurn?.id === turnId) {
57 return buildSideChatSourceContext(turns, activeTurn);
60 return turnIndex === -1 ? undefined : buildSideChatSourceContext(turns.slice(0, turnIndex + 1));
61 }
63 > export function injectSideChatContext(prompt: string, partialResponse?: string, sourceContext?: string, selectionText?: string): string {
64 const context = [SIDE_CHAT_GUIDANCE];
65 if (selectionText) {
91 return [SIDE_CHAT_CONTEXT_START, `${SIDE_CHAT_CONTEXT_LENGTH_PREFIX}${contextBody.length}`, contextBody, SIDE_CHAT_CONTEXT_END, '', prompt].join('\n');
92 }
94 > export function prepareSideChatPrompt(prompt: string, turns: readonly Turn[], sideChat: IPersistedSideChat | undefined): string {
95 if (!sideChat || turns.length > sideChat.inheritedTurnCount) {
96 return prompt;
107 return injectSideChatContext(prompt, partialResponse, sourceContext, sideChat.selection?.text);
108 }
110 function buildSideChatContextBlock(message: string, response: string | undefined): string | undefined {
111 const userText = message.trim();
118 : `User request:\n${userText}`;
119 }
121 > export function stripSideChatContext(turns: readonly Turn[], sideChat: IPersistedSideChat | undefined): readonly Turn[] {
122 if (!sideChat || turns.length === 0) {
123 return turns;
150 return [{ ...first, message: { ...first.message, text: userPrompt } }, ...turns.slice(1)];
151 }
153 > /**
154 > * In-memory backing for an additional (non-default) peer chat. Records the SDK
155 > * chat id that backs the chat so it can be re-resumed after a process restart,
156 > * along with any model override chosen at creation time. This is also the shape
157 > * serialized into the opaque, agent-owned `providerData` blob the orchestrator
158 > * persists in its chat catalog and hands back on restore.
159 > */
160 > export interface IPersistedChat {
161 > readonly sdkSessionId: string;
162 > readonly model?: ModelSelection;
163 > readonly sideChat?: IPersistedSideChat;
164 > }
165 >
166 > export interface IResolvedAgentChat<TSession extends IDisposable> {
167 > readonly chatSession: TSession;
168 > readonly isDefault: boolean;
169 > }
170 >
171 > /**
172 > * Serializes a peer-chat backing into the opaque `providerData` token the
173 > * orchestrator persists verbatim. The encoding is the agent's private business
174 > * — today it is the JSON of {@link IPersistedChat}.
175 > */
176 > export function encodeProviderData(backing: IPersistedChat): string {
177 return JSON.stringify(backing);
178 }
180 > /**
181 > * Decodes an opaque `providerData` token produced by {@link encodeProviderData}
182 > * back into a peer-chat backing, tolerating corrupt/foreign blobs by returning
183 > * `undefined` (the same drop-on-corrupt policy as the legacy chat catalog read).
184 > */
185 > export function decodeProviderData(providerData: string): IPersistedChat | undefined {
186 try {
187 const value = JSON.parse(providerData) as { sdkSessionId?: unknown; model?: unknown; sideChat?: unknown };
231 }
232 }
234 > /**
235 > * Per-session container shared by the multi-chat agents. Keeps ALL chats of a
236 > * session — the default (main) chat and any additional peer chats — together in
237 > * ONE per-agent map keyed by each chat's channel URI string (no parallel maps,
238 > * no default-vs-peer storage split). The default chat is just the entry marked
239 > * as default, so send/abort/model/agent/history operations resolve any chat by a
240 > * single uniform {@link getChat} lookup with no default-chat resolution branch.
241 > *
242 > * Each entry can act as a leaf (wrapping one {@link ownSession} plus its
243 > * event-forwarding disposables) or as the container (holding the chat map).
244 > * Disposing the container disposes every chat leaf it holds.
245 > */
246 > export class AgentSessionEntry<TSession extends IDisposable> extends Disposable {
247 > /** All chats of the session (default + peers) as leaf entries, keyed by chat-URI string. */
248 > private readonly _chats = this._register(new DisposableMap<string, AgentSessionEntry<TSession>>());
249 > /** The key of the session's default (main) chat within {@link _chats}. */
250 > private _defaultChatKey: string | undefined;
251 > /** This leaf's own chat session (set when the entry wraps a single chat). */
252 > private _ownSession: TSession | undefined;
253 >
254 > constructor(session?: TSession) {
255 super();
256 if (session) {
259 }
260 }
262 > /** This leaf's own chat session, or `undefined` for a bare container. */
263 > get ownSession(): TSession | undefined {
264 return this._ownSession;
265 }
267 > addDisposable(disposable: IDisposable): void {
268 this._register(disposable);
269 }
271 > // ---- Uniform chat map (default + peers) --------------------------------
272 >
273 > /** Register the session's default (main) chat leaf under its chat-URI key. */
274 > setDefaultChat(chatKey: string, entry: AgentSessionEntry<TSession>): void {
275 this._chats.set(chatKey, entry);
276 this._defaultChatKey = chatKey;
277 }
279 > /** Dispose the default chat leaf (e.g. a config-driven restart) while keeping peer chats. */
280 > clearDefaultChat(): void {
281 if (this._defaultChatKey !== undefined) {
282 this._chats.deleteAndDispose(this._defaultChatKey);
284 }
285 }
287 > /** The session's materialized default (main) chat, or `undefined` while provisional. */
288 > get defaultChat(): TSession | undefined {
289 return this._defaultChatKey !== undefined ? this._chats.get(this._defaultChatKey)?.ownSession : undefined;
290 }
292 > /** Uniform lookup: the chat's session (default OR peer) by its chat-URI key. */
293 > getChat(chatKey: string): TSession | undefined {
294 return this._chats.get(chatKey)?.ownSession;
295 }
297 > /** Uniform lookup with default-vs-peer identity from the entry that resolved the chat. */
298 > resolveChat(chatKey: string): IResolvedAgentChat<TSession> | undefined {
299 const chatSession = this._chats.get(chatKey)?.ownSession;
300 if (!chatSession) {
303 return { chatSession, isDefault: chatKey === this._defaultChatKey };
304 }
306 > /** Every live chat session — the default chat plus all peers. */
307 > allChatSessions(): TSession[] {
308 const sessions: TSession[] = [];
309 for (const entry of this._chats.values()) {
314 return sessions;
315 }
317 > // ---- Peer chats (every chat except the default) ------------------------
318 >
319 > getPeerChat(chatKey: string): TSession | undefined {
320 return chatKey === this._defaultChatKey ? undefined : this._chats.get(chatKey)?.ownSession;
321 }
323 > hasPeerChat(chatKey: string): boolean {
324 return chatKey !== this._defaultChatKey && this._chats.has(chatKey);
325 }
327 > registerPeerChat(chatKey: string, entry: AgentSessionEntry<TSession>): void {
328 this._chats.set(chatKey, entry);
329 }
331 > disposePeerChat(chatKey: string): void {
332 if (chatKey !== this._defaultChatKey) {
333 this._chats.deleteAndDispose(chatKey);
334 }
335 }
337 > peerChatKeys(): string[] {
338 return [...this._chats.keys()].filter(key => key !== this._defaultChatKey);
339 }
341 > peerChatSessions(): TSession[] {
342 const sessions: TSession[] = [];
343 for (const key of this._chats.keys()) {