src/vs/platform/agentHost/common/agentHostConversationContext.ts
98 LOC · 94 covered · 4 uncovered · 19 ranges · 1592 concepts · 10 introducers · 717 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.
/*---------------------------------------------------------------------------------------------
agentHostConversationContext.ts ×3
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ResponsePartKind, type ResponsePart, type Turn } from './state/sessionState.js';
/**
* Options for {@link buildConversationContext}.
*/
export interface IConversationContextOptions {
/**
* Soft upper bound, in characters, for the conversation portion of the
* produced context string. When the conversation exceeds this budget its
* middle is removed (marked with `...`) via {@link truncateMiddle}. The
* optional {@link framing} is always preserved in full and does not count
* against this budget.
*/
readonly maxChars: number;
/**
* Optional framing text prepended to the conversation (e.g. a note that the
* conversation was branched from an earlier chat). Always preserved in full
* — only the conversation is truncated to {@link maxChars}.
*/
readonly framing?: string;
}
/**
* Concatenates the normal textual (markdown) response parts of a turn into a
* single string. Tool calls, reasoning, content references, and other
* non-markdown parts are intentionally ignored so that only the assistant's
* user-facing prose is included — this keeps utility-model prompts focused and
* free of large tool payloads or subagent traces.
*/
export function renderResponseMarkdown(parts: readonly ResponsePart[]): string {
for (const part of parts) {
const text = part.content.trim();
if (text) {
segments.push(text);
}
}
}
}
/**
* Builds a plain-text conversation context string from the given turns by
* concatenating each turn's user request and the assistant's textual
* (markdown) response. Only normal text response parts are considered — tool
* calls, reasoning, subagent traces, and other parts are ignored (see
* {@link renderResponseMarkdown}). The conversation is middle-truncated to
* {@link IConversationContextOptions.maxChars} to bound model cost; any
* {@link IConversationContextOptions.framing} is prepended afterwards and is
* always preserved in full.
*
* @returns the context string, or `undefined` when no turn carries any text
* worth including.
*/
export function buildConversationContext(turns: readonly Turn[], options: IConversationContextOptions): string | undefined {
for (const turn of turns) {
const responseText = renderResponseMarkdown(turn.responseParts);
if (!userText && !responseText) {
continue;
}
? `User request:\n${userText}\n\nAgent response:\n${responseText}`
agentHostConversationContext.ts ×1
}
}
const truncatedConversation = conversation.length > options.maxChars ? truncateMiddle(conversation, options.maxChars) : conversation;
agentHostConversationContext.ts ×3
return `${options.framing ?? ''}${truncatedConversation}`;
}
/**
* Truncates `text` to at most `maxChars` characters by removing the middle and
* inserting a `...` marker, preserving the start and end.
*/
export function truncateMiddle(text: string, maxChars: number): string {
}
if (maxChars <= marker.length) {
return text.slice(0, maxChars);
}
const head = Math.ceil(keep / 2);
const tail = keep - head;
return `${text.slice(0, head)}${marker}${text.slice(text.length - tail)}`;
}