forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodelProtocolAdapterShared.ts
More file actions
39 lines (33 loc) · 1.36 KB
/
Copy pathmodelProtocolAdapterShared.ts
File metadata and controls
39 lines (33 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
export type JsonObject = Record<string, unknown>;
export type CanonicalToolCall = {
id: string;
type: "function";
function: { name: string; arguments: string };
};
export type CanonicalMessage = {
role: "system" | "user" | "assistant" | "tool";
content?: string | unknown[] | null;
tool_call_id?: string;
tool_calls?: CanonicalToolCall[];
};
export function functionToolCalls(message: unknown): CanonicalToolCall[] {
if (!isObject(message) || !Array.isArray(message.tool_calls)) return [];
return message.tool_calls.filter(
(toolCall): toolCall is CanonicalToolCall =>
isObject(toolCall) &&
typeof toolCall.id === "string" &&
toolCall.type === "function" &&
isObject(toolCall.function) &&
typeof toolCall.function.name === "string" &&
typeof toolCall.function.arguments === "string",
);
}
export function formatSseEvent(type: string, data: unknown): string {
return `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`;
}
export function isObject(value: unknown): value is JsonObject {
return value !== null && typeof value === "object" && !Array.isArray(value);
}