byokOpenAiTranslation.ts ×9

Frontier kind: Code frontier

unlabeled · c_7088f9e138cf

441 tests · 3473 LOC · 19 files · introduces 0 tests · 95 LOC · 1 file

Introduces — evidence that enters the hierarchy at this concept

Code
9 ranges95 lines · 1 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
489 ranges3473 lines · 19 files · Browse complete extent
All tests (intent)
441 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: 95 introduced LOC across 9 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

src/vs/platform/agentHost/node/copilot/byokOpenAiTranslation.ts 95 introduced LOC · 9 ranges

Open complete file

1 > /*--------------------------------------------------------------------------------------------- byokOpenAiTranslation.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 {
7 > IByokLmChatMessage,
8 > IByokLmChatRequest,
9 > IByokLmChatResult,
10 > IByokLmTool,
11 > IByokLmToolCall,
12 > } from '../../common/agentHostByokLm.js';
13 >
14 > /**
15 > * Minimal subset of the OpenAI Chat Completions wire format the Copilot SDK
16 > * runtime emits for a `type: 'openai'`, `wireApi: 'completions'` provider
17 > * (verified against the runtime's `chat_completion_transport.rs`, which POSTs
18 > * to `{baseUrl}/chat/completions`). Only the fields this proxy understands are
19 > * modeled; unknown fields are ignored.
20 > */
21 >
22 > interface IOpenAiTextContentPart {
23 > readonly type: 'text';
24 > readonly text: string;
25 > }
26 >
27 > type IOpenAiContentPart = IOpenAiTextContentPart | { readonly type: string;[k: string]: unknown };
28 >
29 > interface IOpenAiToolCall {
30 > readonly id?: string;
31 > readonly type?: string;
32 > readonly function?: {
33 > readonly name?: string;
34 > readonly arguments?: string;
35 > };
36 > }
37 >
38 > interface IOpenAiRequestMessage {
39 > readonly role?: string;
40 > readonly content?: string | IOpenAiContentPart[] | null;
41 > readonly tool_calls?: IOpenAiToolCall[];
42 > readonly tool_call_id?: string;
43 > }
44 >
45 > interface IOpenAiToolDefinition {
46 > readonly type?: string;
47 > readonly function?: {
48 > readonly name?: string;
49 > readonly description?: string;
50 > readonly parameters?: object;
51 > };
52 > }
53 >
54 > export interface IOpenAiChatRequest {
55 > readonly model?: string;
56 > readonly messages?: IOpenAiRequestMessage[];
57 > readonly tools?: IOpenAiToolDefinition[];
58 > readonly stream?: boolean;
59 > readonly temperature?: number;
60 > readonly top_p?: number;
61 > readonly max_tokens?: number;
62 > readonly [k: string]: unknown;
63 > }
64 >
65 > /** Thrown when the inbound body cannot be mapped to a bridge request. */
66 > export class OpenAiTranslationError extends Error { }
67 >
68 function flattenContent(content: string | IOpenAiContentPart[] | null | undefined): string {
69 if (typeof content === 'string') {
81 return '';
82 }
84 function toBridgeRole(role: string | undefined): IByokLmChatMessage['role'] {
85 switch (role) {
97 }
98 }
100 function toBridgeToolCalls(toolCalls: IOpenAiToolCall[] | undefined): IByokLmToolCall[] | undefined {
101 if (!toolCalls || toolCalls.length === 0) {
120 return mapped;
121 }
123 function toBridgeTools(tools: IOpenAiToolDefinition[] | undefined): IByokLmTool[] | undefined {
124 if (!tools || tools.length === 0) {
139 return mapped.length ? mapped : undefined;
140 }
142 > /**
143 > * Convert a parsed OpenAI Chat Completions request into the serializable
144 > * bridge request. `vendor` is the synthesized provider name the runtime used
145 > * (it is not present in the OpenAI body); `model` becomes the provider-local
146 > * wire model id resolved on the renderer.
147 > */
148 > export function openAiRequestToBridge(vendor: string, body: IOpenAiChatRequest): IByokLmChatRequest {
149 const model = typeof body.model === 'string' ? body.model : '';
150 if (!model) {
178 };
179 }
181 > let chunkCounter = 0;
182 >
183 function nextCompletionId(): string {
184 chunkCounter = (chunkCounter + 1) % Number.MAX_SAFE_INTEGER;
185 return `chatcmpl-byok-${Date.now().toString(36)}-${chunkCounter.toString(36)}`;
186 }
188 > /** Serialize a single SSE `data:` frame. */
189 function sseFrame(payload: unknown): string {
190 return `data: ${JSON.stringify(payload)}\n\n`;
191 }
193 > /**
194 > * Encode a buffered {@link IByokLmChatResult} as a sequence of OpenAI
195 > * `chat.completion.chunk` SSE frames terminated by `data: [DONE]`.
196 > *
197 > * The whole completion is emitted in one content delta (Stage 1 is
198 > * non-streaming end-to-end); the runtime's SSE parser accepts this shape.
199 > */
200 > export function bridgeResultToSseFrames(result: IByokLmChatResult, model: string): string[] {
201 const id = nextCompletionId();
202 const created = Math.floor(Date.now() / 1000);
235 return frames;
236 }
238 > /** Build an OpenAI-style error envelope body. */
239 > export function openAiErrorBody(message: string, type = 'api_error'): string {
240 return JSON.stringify({ error: { message, type } });
241 }