forwardedChatError.ts ×12

Frontier kind: Code frontier

unlabeled · c_2d43000b2d06

1107 tests · 20054 LOC · 83 files · introduces 0 tests · 170 LOC · 1 file

Introduces — evidence that enters the hierarchy at this concept

Code
12 ranges170 lines · 1 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
1563 ranges20054 lines · 83 files · Browse complete extent
All tests (intent)
1107 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: 170 introduced LOC across 12 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

src/vs/platform/agentHost/node/shared/forwardedChatError.ts 170 introduced LOC · 12 ranges

Open complete file

1 > /*--------------------------------------------------------------------------------------------- forwardedChatError.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 { CopilotApiError, COPILOT_API_ERROR_STATUS_STREAMING } from './copilotApiService.js';
7 >
8 > /**
9 > * Marker prefix used to smuggle a structured, serialized chat fetch error
10 > * through the agent SDK subprocess boundary. The model proxies run in this
11 > * (the agent host) process and hold the rich {@link CopilotApiError}, but the
12 > * agent SDKs (Claude, Codex, Copilot CLI) run as child processes that only
13 > * see an HTTP/SSE error. The proxy appends `VSCODE_PROXY_ERROR:<base64>` to
14 > * the error message; the SDK forwards that text back verbatim, and the agent
15 > * decodes it on the way out.
16 > *
17 > * Mirrors the Copilot Chat extension's `PROXY_ERROR_PREFIX`
18 > * (`extensions/copilot/src/extension/chatSessions/claude/common/claudeMessageDispatch.ts`).
19 > */
20 > export const PROXY_ERROR_PREFIX = 'VSCODE_PROXY_ERROR:';
21 >
22 > /**
23 > * Upper bound on the base64 marker payload we will decode. A forwarded chat
24 > * error serializes to well under 1 KB; this cap prevents an oversized or
25 > * adversarial marker riding along in model-influenced error text from driving
26 > * an unbounded base64/JSON allocation.
27 > */
28 > const MAX_FORWARDED_MARKER_B64_LENGTH = 8 * 1024;
29 >
30 > /** Standard base64 alphabet with optional padding. */
31 > const FORWARDED_MARKER_B64_PATTERN = /^[A-Za-z0-9+/]+={0,2}$/;
32 >
33 > /**
34 > * Serialized chat fetch error payload. This is the JSON shape forwarded over
35 > * the protocol's `ErrorInfo._meta.chatError`. The core consumer
36 > * (`src/vs/workbench/contrib/chat/common/chatErrorMessages.ts`) reads the same
37 > * JSON shape to render localized, user-facing messages. The two definitions
38 > * are intentionally decoupled (the platform/node layer cannot import workbench
39 > * code), so any field change must be mirrored on both sides.
40 > */
41 > export interface IForwardedChatFetchError {
42 > /** Mirrors the extension's `ChatFetchResponseType` string value. */
43 > readonly type: string;
44 > readonly reason?: string;
45 > readonly requestId?: string;
46 > readonly serverRequestId?: string;
47 > readonly category?: string;
48 > readonly retryAfter?: number;
49 > readonly isAuto?: boolean;
50 > readonly capiError?: { readonly code?: string; readonly message?: string };
51 > }
52 >
53 > /**
54 > * The full forwarded chat error placed at `ErrorInfo._meta.chatError`.
55 > */
56 > export interface IForwardedChatError {
57 > readonly fetchError: IForwardedChatFetchError;
58 > readonly copilotPlan?: string;
59 > readonly isUsageBasedBilling?: boolean;
60 > readonly quotaResetDate?: string;
61 > }
62 >
63 > /**
64 > * Maps a {@link CopilotApiError} HTTP status (or the mid-stream streaming
65 > * sentinel) to the extension's `ChatFetchResponseType` string value. Kept in
66 > * sync with the Copilot Chat extension's error classification so the core
67 > * formatter produces identical messages.
68 > */
69 function statusToFetchType(status: number): string {
70 switch (status) {
86 }
87 }
89 > /**
90 > * Builds a {@link IForwardedChatError} from a {@link CopilotApiError}. The
91 > * error's Anthropic envelope carries the upstream message and type, which are
92 > * surfaced as `reason`/`capiError` so the core formatter can render the right
93 > * message (rate limit, quota, filtered, etc.).
94 > */
95 > export function buildForwardedChatError(err: CopilotApiError): IForwardedChatError {
96 const status = err.status === COPILOT_API_ERROR_STATUS_STREAMING ? 502 : err.status;
97 const requestId = typeof err.envelope.request_id === 'string' ? err.envelope.request_id : '';
111 };
112 }
114 > /**
115 > * Attempts to parse a CAPI-style error body (`{ "error": { "code", "message" } }`)
116 > * out of an envelope message string. Returns `undefined` when the message is
117 > * not such a JSON payload.
118 > */
119 function extractCapiError(message: string): { code?: string; message?: string } | undefined {
120 let parsed: unknown;
141 };
142 }
144 > /**
145 > * Encodes a {@link IForwardedChatError} as a `VSCODE_PROXY_ERROR:<base64>`
146 > * marker string. Base64 survives the SDK's JSON re-encoding without
147 > * double-escaping issues.
148 > */
149 > export function encodeForwardedChatError(forwarded: IForwardedChatError): string {
150 return `${PROXY_ERROR_PREFIX}${Buffer.from(JSON.stringify(forwarded)).toString('base64')}`;
151 }
153 > /**
154 > * Fields from a structured agent-SDK error (notably the Copilot CLI SDK's
155 > * `ErrorData`) used to build a forwarded chat error directly, without a
156 > * {@link PROXY_ERROR_PREFIX} marker. The Copilot CLI authenticates with CAPI
157 > * itself (no VS Code proxy to embed a marker), but its `session.error` event
158 > * already carries the structured classification we need.
159 > */
160 > export interface ISdkChatErrorFields {
161 > readonly errorType: string;
162 > readonly errorCode?: string;
163 > readonly message: string;
164 > readonly statusCode?: number;
165 > readonly providerCallId?: string;
166 > readonly serviceRequestId?: string;
167 > }
168 >
169 > /**
170 > * Maps an agent-SDK error category (and optional HTTP status) to the
171 > * extension's `ChatFetchResponseType` string value, or `undefined` when the
172 > * error is not a model/CAPI error we can render richly. Categories mirror the
173 > * Copilot CLI SDK's `ErrorData.errorType` values.
174 > */
175 function sdkErrorTypeToFetchType(errorType: string, statusCode: number | undefined): string | undefined {
176 switch (errorType) {
187 return statusCode !== undefined ? statusToFetchType(statusCode) : undefined;
188 }
190 > /**
191 > * Builds a {@link IForwardedChatError} from a structured agent-SDK error.
192 > * Returns `undefined` when the error cannot be classified as a model/CAPI
193 > * error, so callers can fall back to the raw message.
194 > */
195 > export function buildForwardedChatErrorFromFields(data: ISdkChatErrorFields): IForwardedChatError | undefined {
196 const type = sdkErrorTypeToFetchType(data.errorType, data.statusCode);
197 if (!type) {
215 };
216 }
218 > /**
219 > * Attempts to decode a {@link IForwardedChatError} from arbitrary error text
220 > * that may contain a {@link PROXY_ERROR_PREFIX} marker. Returns `undefined`
221 > * when no marker is present or the payload cannot be parsed.
222 > *
223 > * Mirrors the extension's `tryParseProxyError`.
224 > */
225 > export function tryParseForwardedChatError(errorText: string | undefined): IForwardedChatError | undefined {
226 if (!errorText) {
227 return undefined;
250 }
251 }
253 > /**
254 > * Removes the `VSCODE_PROXY_ERROR:<base64>` marker (and anything after it) from
255 > * an error message so the human-readable text isn't polluted by the forwarding
256 > * payload. The structured payload is consumed separately via `_meta`. A no-op
257 > * when no marker is present.
258 > */
259 > export function stripProxyErrorMarker(text: string): string {
260 const idx = text.indexOf(PROXY_ERROR_PREFIX);
261 if (idx === -1) {
264 return text.slice(0, idx).trim() || text.slice(0, idx);
265 }
267 > /**
268 > * Wraps a {@link IForwardedChatError} into the `_meta` record carried on the
269 > * protocol `ErrorInfo`. The core consumer reads `_meta.chatError`.
270 > */
271 > export function toChatErrorMeta(forwarded: IForwardedChatError): Record<string, unknown> {
272 return { chatError: forwarded };
273 }
275 > /**
276 > * Convenience: decode a {@link IForwardedChatError} from arbitrary error text
277 > * and wrap it into the protocol `ErrorInfo._meta` record. Returns `undefined`
278 > * when the text carries no {@link PROXY_ERROR_PREFIX} marker, so callers can
279 > * spread it onto an `ErrorInfo` without changing behavior for plain errors.
280 > */
281 > export function tryBuildChatErrorMeta(errorText: string | undefined): Record<string, unknown> | undefined {
282 const forwarded = tryParseForwardedChatError(errorText);
283 return forwarded ? toChatErrorMeta(forwarded) : undefined;
284 }
286 > /**
287 > * Convenience: build the protocol `ErrorInfo._meta` record from a structured
288 > * agent-SDK error. Returns `undefined` when the error cannot be classified as
289 > * a model/CAPI error, so callers can fall back to the raw message.
290 > */
291 > export function tryBuildChatErrorMetaFromFields(data: ISdkChatErrorFields): Record<string, unknown> | undefined {
292 const forwarded = buildForwardedChatErrorFromFields(data);
293 return forwarded ? toChatErrorMeta(forwarded) : undefined;
294 }
296 > /**
297 > * Decodes a forwarded {@link PROXY_ERROR_PREFIX} marker out of an error message
298 > * and returns the cleaned human-readable message together with the protocol
299 > * `ErrorInfo._meta` record. When no marker is present the message is returned
300 > * unchanged and `_meta` is omitted, so the result can be spread directly onto
301 > * an `ErrorInfo` without changing behavior for plain errors:
302 > *
303 > * ```ts
304 > * error: { errorType: 'CodexError', ...extractForwardedErrorInfo(message) }
305 > * ```
306 > */
307 > export function extractForwardedErrorInfo(message: string): { message: string; _meta?: Record<string, unknown> } {
308 const forwarded = tryParseForwardedChatError(message);
309 if (!forwarded) {