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.

1 > /*--------------------------------------------------------------------------------------------- agentHostConversationContext.ts ×3
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 { ResponsePartKind, type ResponsePart, type Turn } from './state/sessionState.js';
7 >
8 > /**
9 > * Options for {@link buildConversationContext}.
10 > */
11 > export interface IConversationContextOptions {
12 > /**
13 > * Soft upper bound, in characters, for the conversation portion of the
14 > * produced context string. When the conversation exceeds this budget its
15 > * middle is removed (marked with `...`) via {@link truncateMiddle}. The
16 > * optional {@link framing} is always preserved in full and does not count
17 > * against this budget.
18 > */
19 > readonly maxChars: number;
20 >
21 > /**
22 > * Optional framing text prepended to the conversation (e.g. a note that the
23 > * conversation was branched from an earlier chat). Always preserved in full
24 > * — only the conversation is truncated to {@link maxChars}.
25 > */
26 > readonly framing?: string;
27 > }
28 >
29 > /**
30 > * Concatenates the normal textual (markdown) response parts of a turn into a
31 > * single string. Tool calls, reasoning, content references, and other
32 > * non-markdown parts are intentionally ignored so that only the assistant's
33 > * user-facing prose is included — this keeps utility-model prompts focused and
34 > * free of large tool payloads or subagent traces.
35 > */
36 > export function renderResponseMarkdown(parts: readonly ResponsePart[]): string {
37 > const segments: string[] = []; agentHostConversationContext.ts ×2
38 > for (const part of parts) {
39 > if (part.kind === ResponsePartKind.Markdown) { agentHostConversationContext.ts ×1
40 > const text = part.content.trim();
41 > if (text) {
42 > segments.push(text);
43 > }
44 > }
45 > }
46 > return segments.join('\n\n'); agentHostConversationContext.ts ×2
47 > }
49 > /**
50 > * Builds a plain-text conversation context string from the given turns by
51 > * concatenating each turn's user request and the assistant's textual
52 > * (markdown) response. Only normal text response parts are considered — tool
53 > * calls, reasoning, subagent traces, and other parts are ignored (see
54 > * {@link renderResponseMarkdown}). The conversation is middle-truncated to
55 > * {@link IConversationContextOptions.maxChars} to bound model cost; any
56 > * {@link IConversationContextOptions.framing} is prepended afterwards and is
57 > * always preserved in full.
58 > *
59 > * @returns the context string, or `undefined` when no turn carries any text
60 > * worth including.
61 > */
62 > export function buildConversationContext(turns: readonly Turn[], options: IConversationContextOptions): string | undefined {
63 > const blocks: string[] = []; agentHostConversationContext.ts ×3
64 > for (const turn of turns) {
65 > const userText = turn.message.text.trim(); agentHostConversationContext.ts ×4
66 > const responseText = renderResponseMarkdown(turn.responseParts);
67 > if (!userText && !responseText) {
68 continue;
69 }
70 > blocks.push(responseText agentHostConversationContext.ts ×4
71 > ? `User request:\n${userText}\n\nAgent response:\n${responseText}` agentHostConversationContext.ts ×1
72 > : `User request:\n${userText}`); agentHostConversationContext.ts ×4
73 > }
74 > if (blocks.length === 0) { agentHostConversationContext.ts ×3
76 > }
77 > const conversation = blocks.join('\n\n---\n\n'); agentHostConversationContext.ts ×4
78 > const truncatedConversation = conversation.length > options.maxChars ? truncateMiddle(conversation, options.maxChars) : conversation; agentHostConversationContext.ts ×3
79 > return `${options.framing ?? ''}${truncatedConversation}`;
80 > }
82 > /**
83 > * Truncates `text` to at most `maxChars` characters by removing the middle and
84 > * inserting a `...` marker, preserving the start and end.
85 > */
86 > export function truncateMiddle(text: string, maxChars: number): string {
87 > if (text.length <= maxChars) { agentHostConversationContext.ts ×1
88 > return text; agentPeerChats.ts ×2
89 > }
90 > const marker = '\n...\n'; agentHostConversationContext.ts ×2
91 > if (maxChars <= marker.length) {
92 return text.slice(0, maxChars);
93 }
94 > const keep = maxChars - marker.length; agentHostConversationContext.ts ×2
95 > const head = Math.ceil(keep / 2);
96 > const tail = keep - head;
97 > return `${text.slice(0, head)}${marker}${text.slice(text.length - tail)}`;
98 > }