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') {