@copilotkit/shared 0.9.0-function-calling-fixes.2 → 0.9.0-multi-release-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -14
- package/dist/index.d.ts +2 -2
- package/dist/index.js +11 -19
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +11 -19
- package/dist/index.mjs.map +1 -1
- package/dist/types/action.d.ts +33 -32
- package/dist/types/action.js.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/utils/decode-chat-completion.js +11 -19
- package/dist/utils/decode-chat-completion.js.map +1 -1
- package/dist/utils/decode-chat-completion.mjs +11 -19
- package/dist/utils/decode-chat-completion.mjs.map +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +11 -19
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +11 -19
- package/dist/utils/index.mjs.map +1 -1
- package/dist/utils/parse-chat-completion.d.ts +10 -12
- package/dist/utils/parse-chat-completion.js.map +1 -1
- package/dist/utils/parse-chat-completion.mjs.map +1 -1
- package/package.json +3 -3
- package/src/types/action.ts +51 -34
- package/src/utils/decode-chat-completion.ts +16 -29
- package/src/utils/parse-chat-completion.ts +15 -19
package/CHANGELOG.md
CHANGED
|
@@ -1,22 +1,10 @@
|
|
|
1
1
|
# @copilotkit/shared
|
|
2
2
|
|
|
3
|
-
## 0.9.0-
|
|
3
|
+
## 0.9.0-multi-release-alpha.0
|
|
4
4
|
|
|
5
5
|
### Minor Changes
|
|
6
6
|
|
|
7
|
-
-
|
|
8
|
-
|
|
9
|
-
## 0.9.0-function-calling-fixes.1
|
|
10
|
-
|
|
11
|
-
### Minor Changes
|
|
12
|
-
|
|
13
|
-
- gpt-4-turbo-april-2024 function calling fixes
|
|
14
|
-
|
|
15
|
-
## 0.9.0-alpha.0
|
|
16
|
-
|
|
17
|
-
### Minor Changes
|
|
18
|
-
|
|
19
|
-
- gpt-4-turbo-april-2024
|
|
7
|
+
- types refactor
|
|
20
8
|
|
|
21
9
|
## 0.8.0
|
|
22
10
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export { AssistantMessage, FunctionCall, FunctionCallHandler, FunctionDefinition, JSONValue, Message, Role, ToolDefinition } from './types/openai-assistant.js';
|
|
2
2
|
export { AnnotatedFunction, AnnotatedFunctionArgument, AnnotatedFunctionArrayArgument, AnnotatedFunctionSimpleArgument } from './types/annotated-function.js';
|
|
3
|
-
export { Action,
|
|
3
|
+
export { Action, MappedParameterTypes, Parameter } from './types/action.js';
|
|
4
4
|
export { COMPLEX_HEADER, StreamPart, StreamPartType, StreamString, StreamStringPrefixes, decodeResult, encodeResult, formatStreamPart, isStreamStringEqualToType, parseStreamPart, streamPartsByCode, validCodes } from './utils/utils.js';
|
|
5
|
-
export { ChatCompletionChunk,
|
|
5
|
+
export { ChatCompletionChunk, parseChatCompletion } from './utils/parse-chat-completion.js';
|
|
6
6
|
export { ChatCompletionContentEvent, ChatCompletionEvent, ChatCompletionFunctionEvent, ChatCompletionPartialEvent, ChatCompletionResultEvent, decodeChatCompletion } from './utils/decode-chat-completion.js';
|
|
7
7
|
export { decodeChatCompletionAsText } from './utils/decode-chat-completion-as-text.js';
|
|
8
8
|
export { actionToChatCompletionFunction, annotatedFunctionToAction, annotatedFunctionToChatCompletionFunction } from './utils/annotated-function.js';
|
package/dist/index.js
CHANGED
|
@@ -288,24 +288,19 @@ function decodeChatCompletion(stream) {
|
|
|
288
288
|
try {
|
|
289
289
|
const { done, value } = await reader.read();
|
|
290
290
|
if (done) {
|
|
291
|
-
if (
|
|
291
|
+
if (mode === "function") {
|
|
292
292
|
flushFunctionCall();
|
|
293
293
|
}
|
|
294
294
|
await cleanup(controller);
|
|
295
295
|
return;
|
|
296
296
|
}
|
|
297
|
-
if (
|
|
297
|
+
if (mode === "function" && (!((_b = (_a = value.choices[0].delta.tool_calls) == null ? void 0 : _a[0]) == null ? void 0 : _b.function) || ((_d = (_c = value.choices[0].delta.tool_calls) == null ? void 0 : _c[0]) == null ? void 0 : _d.function.name))) {
|
|
298
298
|
if (!flushFunctionCall()) {
|
|
299
299
|
return;
|
|
300
300
|
}
|
|
301
301
|
}
|
|
302
|
-
|
|
303
|
-
if (
|
|
304
|
-
mode = { type: "function", function: maybeFunctionCall };
|
|
305
|
-
} else {
|
|
306
|
-
mode = { type: "message" };
|
|
307
|
-
}
|
|
308
|
-
if (mode.type === "message") {
|
|
302
|
+
mode = ((_f = (_e = value.choices[0].delta.tool_calls) == null ? void 0 : _e[0]) == null ? void 0 : _f.function) ? "function" : "message";
|
|
303
|
+
if (mode === "message") {
|
|
309
304
|
if (value.choices[0].delta.role === "function") {
|
|
310
305
|
controller.enqueue({
|
|
311
306
|
type: "result",
|
|
@@ -319,18 +314,15 @@ function decodeChatCompletion(stream) {
|
|
|
319
314
|
});
|
|
320
315
|
}
|
|
321
316
|
continue;
|
|
322
|
-
} else if (mode
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
functionCallName = maybeFunctionCallName;
|
|
317
|
+
} else if (mode === "function") {
|
|
318
|
+
if (value.choices[0].delta.tool_calls[0].function.name) {
|
|
319
|
+
functionCallName = value.choices[0].delta.tool_calls[0].function.name;
|
|
326
320
|
}
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
functionCallArguments += maybeFunctionCallArguments;
|
|
321
|
+
if (value.choices[0].delta.tool_calls[0].function.arguments) {
|
|
322
|
+
functionCallArguments += value.choices[0].delta.tool_calls[0].function.arguments;
|
|
330
323
|
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
functionCallScope = maybeFunctionCallScope;
|
|
324
|
+
if (value.choices[0].delta.tool_calls[0].function.scope) {
|
|
325
|
+
functionCallScope = value.choices[0].delta.tool_calls[0].function.scope;
|
|
334
326
|
}
|
|
335
327
|
controller.enqueue({
|
|
336
328
|
type: "partial",
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/utils/utils.ts","../src/utils/parse-chat-completion.ts","../src/utils/decode-chat-completion.ts","../src/utils/decode-chat-completion-as-text.ts","../src/utils/annotated-function.ts","../src/constants/copilot-protocol.ts"],"sourcesContent":["export * from \"./types\";\nexport * from \"./utils\";\nexport * from \"./constants\";\n","import { AssistantMessage, FunctionCall, JSONValue } from \"../types/openai-assistant\";\n\nexport function encodeResult(result: string): string {\n if (result === undefined) {\n return \"\";\n } else if (typeof result === \"string\") {\n return result;\n } else {\n return JSON.stringify(result);\n }\n}\n\nexport function decodeResult(result: string): any {\n try {\n return JSON.parse(result);\n } catch (e) {\n return result;\n }\n}\n\nexport interface StreamPart<CODE extends string, NAME extends string, TYPE> {\n code: CODE;\n name: NAME;\n parse: (value: JSONValue) => { type: NAME; value: TYPE };\n}\n\nconst textStreamPart: StreamPart<\"0\", \"text\", string> = {\n code: \"0\",\n name: \"text\",\n parse: (value: JSONValue) => {\n if (typeof value !== \"string\") {\n throw new Error('\"text\" parts expect a string value.');\n }\n return { type: \"text\", value };\n },\n};\n\n/**\n * This is a utility function that helps in parsing the stream parts.\n * It takes a JSONValue as input and returns an object with type and value.\n * The type is a string that represents the type of the stream part.\n * The value is the actual value of the stream part.\n * If the input value is not a string, it throws an error.\n */\nconst functionCallStreamPart: StreamPart<\"1\", \"function_call\", { function_call: FunctionCall }> = {\n code: \"1\",\n name: \"function_call\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"function_call\" in value) ||\n typeof value.function_call !== \"object\" ||\n value.function_call == null ||\n !(\"name\" in value.function_call) ||\n !(\"arguments\" in value.function_call) ||\n typeof value.function_call.name !== \"string\" ||\n typeof value.function_call.arguments !== \"string\"\n ) {\n throw new Error('\"function_call\" parts expect an object with a \"function_call\" property.');\n }\n\n return {\n type: \"function_call\",\n value: value as unknown as { function_call: FunctionCall },\n };\n },\n};\n\nconst dataStreamPart: StreamPart<\"2\", \"data\", Array<JSONValue>> = {\n code: \"2\",\n name: \"data\",\n parse: (value: JSONValue) => {\n if (!Array.isArray(value)) {\n throw new Error('\"data\" parts expect an array value.');\n }\n\n return { type: \"data\", value };\n },\n};\n\nconst errorStreamPart: StreamPart<\"3\", \"error\", string> = {\n code: \"3\",\n name: \"error\",\n parse: (value: JSONValue) => {\n if (typeof value !== \"string\") {\n throw new Error('\"error\" parts expect a string value.');\n }\n return { type: \"error\", value };\n },\n};\n\nconst assistantMessage: StreamPart<\"4\", \"assistant_message\", AssistantMessage> = {\n code: \"4\",\n name: \"assistant_message\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"id\" in value) ||\n !(\"role\" in value) ||\n !(\"content\" in value) ||\n typeof value.id !== \"string\" ||\n typeof value.role !== \"string\" ||\n value.role !== \"assistant\" ||\n !Array.isArray(value.content) ||\n !value.content.every(\n (item) =>\n item != null &&\n typeof item === \"object\" &&\n \"type\" in item &&\n item.type === \"text\" &&\n \"text\" in item &&\n item.text != null &&\n typeof item.text === \"object\" &&\n \"value\" in item.text &&\n typeof item.text.value === \"string\",\n )\n ) {\n throw new Error(\n '\"assistant_message\" parts expect an object with an \"id\", \"role\", and \"content\" property.',\n );\n }\n\n return {\n type: \"assistant_message\",\n value: value as AssistantMessage,\n };\n },\n};\n\nconst assistantControlData: StreamPart<\n \"5\",\n \"assistant_control_data\",\n {\n threadId: string;\n messageId: string;\n }\n> = {\n code: \"5\",\n name: \"assistant_control_data\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"threadId\" in value) ||\n !(\"messageId\" in value) ||\n typeof value.threadId !== \"string\" ||\n typeof value.messageId !== \"string\"\n ) {\n throw new Error(\n '\"assistant_control_data\" parts expect an object with a \"threadId\" and \"messageId\" property.',\n );\n }\n\n return {\n type: \"assistant_control_data\",\n value: {\n threadId: value.threadId,\n messageId: value.messageId,\n },\n };\n },\n};\n\nconst streamParts = [\n textStreamPart,\n functionCallStreamPart,\n dataStreamPart,\n errorStreamPart,\n assistantMessage,\n assistantControlData,\n] as const;\n\n// union type of all stream parts\ntype StreamParts =\n | typeof textStreamPart\n | typeof functionCallStreamPart\n | typeof dataStreamPart\n | typeof errorStreamPart\n | typeof assistantMessage\n | typeof assistantControlData;\n\n/**\n * Maps the type of a stream part to its value type.\n */\ntype StreamPartValueType = {\n [P in StreamParts as P[\"name\"]]: ReturnType<P[\"parse\"]>[\"value\"];\n};\n\nexport type StreamPartType =\n | ReturnType<typeof textStreamPart.parse>\n | ReturnType<typeof functionCallStreamPart.parse>\n | ReturnType<typeof dataStreamPart.parse>\n | ReturnType<typeof errorStreamPart.parse>\n | ReturnType<typeof assistantMessage.parse>\n | ReturnType<typeof assistantControlData.parse>;\n\nexport const streamPartsByCode = {\n [textStreamPart.code]: textStreamPart,\n [functionCallStreamPart.code]: functionCallStreamPart,\n [dataStreamPart.code]: dataStreamPart,\n [errorStreamPart.code]: errorStreamPart,\n [assistantMessage.code]: assistantMessage,\n [assistantControlData.code]: assistantControlData,\n} as const;\n\n/**\n * The map of prefixes for data in the stream\n *\n * - 0: Text from the LLM response\n * - 1: (OpenAI) function_call responses\n * - 2: custom JSON added by the user using `Data`\n *\n * Example:\n * ```\n * 0:Vercel\n * 0:'s\n * 0: AI\n * 0: AI\n * 0: SDK\n * 0: is great\n * 0:!\n * 2: { \"someJson\": \"value\" }\n * 1: {\"function_call\": {\"name\": \"get_current_weather\", \"arguments\": \"{\\\\n\\\\\"location\\\\\": \\\\\"Charlottesville, Virginia\\\\\",\\\\n\\\\\"format\\\\\": \\\\\"celsius\\\\\"\\\\n}\"}}\n *```\n */\nexport const StreamStringPrefixes = {\n [textStreamPart.name]: textStreamPart.code,\n [functionCallStreamPart.name]: functionCallStreamPart.code,\n [dataStreamPart.name]: dataStreamPart.code,\n [errorStreamPart.name]: errorStreamPart.code,\n [assistantMessage.name]: assistantMessage.code,\n [assistantControlData.name]: assistantControlData.code,\n} as const;\n\nexport const validCodes = streamParts.map((part) => part.code);\n\n/**\n * Parses a stream part from a string.\n *\n * @param line The string to parse.\n * @returns The parsed stream part.\n * @throws An error if the string cannot be parsed.\n */\nexport const parseStreamPart = (line: string): StreamPartType => {\n const firstSeparatorIndex = line.indexOf(\":\");\n\n if (firstSeparatorIndex === -1) {\n throw new Error(\"Failed to parse stream string. No separator found.\");\n }\n\n const prefix = line.slice(0, firstSeparatorIndex);\n\n if (!validCodes.includes(prefix as keyof typeof streamPartsByCode)) {\n throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);\n }\n\n const code = prefix as keyof typeof streamPartsByCode;\n\n const textValue = line.slice(firstSeparatorIndex + 1);\n const jsonValue: JSONValue = JSON.parse(textValue);\n\n return streamPartsByCode[code].parse(jsonValue);\n};\n\n/**\n * Prepends a string with a prefix from the `StreamChunkPrefixes`, JSON-ifies it,\n * and appends a new line.\n *\n * It ensures type-safety for the part type and value.\n */\nexport function formatStreamPart<T extends keyof StreamPartValueType>(\n type: T,\n value: StreamPartValueType[T],\n): StreamString {\n const streamPart = streamParts.find((part) => part.name === type);\n\n if (!streamPart) {\n throw new Error(`Invalid stream part type: ${type}`);\n }\n\n return `${streamPart.code}:${JSON.stringify(value)}\\n`;\n}\n\nexport const isStreamStringEqualToType = (\n type: keyof typeof StreamStringPrefixes,\n value: string,\n): value is StreamString =>\n value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith(\"\\n\");\n\nexport type StreamString =\n `${typeof StreamStringPrefixes[keyof typeof StreamStringPrefixes]}:${string}\\n`;\n\n/**\n * A header sent to the client so it knows how to handle parsing the stream (as a deprecated text response or using the new prefixed protocol)\n */\nexport const COMPLEX_HEADER = \"X-Experimental-Stream-Data\";\n","import { Role } from \"../types/openai-assistant\";\n\nexport interface ToolCallFunctionCall {\n arguments?: string;\n\n name?: string;\n // TODO:\n // Temporarily add scope to the OpenAI protocol until we\n // have our own protocol.\n // When scope is \"server\", the client will not attempt to\n // execute the function.\n scope?: \"client\" | \"server\";\n}\n\nexport interface ToolCallPayload {\n index: number;\n id?: string;\n function: ToolCallFunctionCall;\n}\n\nexport interface ChatCompletionChunk {\n choices: {\n delta: {\n id?: string;\n role: Role;\n content?: string | null;\n\n // TODO:\n // Temporarily add name to the OpenAI protocol until we\n // have our own protocol.\n // When name is set, we return the result of a server-side\n // function call.\n name?: string;\n\n function_call?: {\n name?: string;\n arguments?: string;\n };\n tool_calls?: ToolCallPayload[];\n };\n }[];\n}\n\n// TODO:\n// it's possible that unicode characters could be split across chunks\n// make sure to properly handle that\nexport function parseChatCompletion(\n stream: ReadableStream<Uint8Array>,\n): ReadableStream<ChatCompletionChunk> {\n const reader = stream.getReader();\n let buffer = new Uint8Array();\n\n async function cleanup(controller?: ReadableStreamDefaultController<any>) {\n if (controller) {\n try {\n controller.close();\n } catch (_) {}\n }\n if (reader) {\n try {\n await reader.cancel();\n } catch (_) {}\n }\n }\n\n return new ReadableStream<ChatCompletionChunk>({\n async pull(controller) {\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n if (done) {\n await cleanup(controller);\n return;\n }\n\n const newBuffer = new Uint8Array(buffer.length + value.length);\n newBuffer.set(buffer);\n newBuffer.set(value, buffer.length);\n buffer = newBuffer;\n\n const valueString = new TextDecoder(\"utf-8\").decode(buffer);\n const lines = valueString.split(\"\\n\").filter((line) => line.trim() !== \"\");\n\n // If the last line isn't complete, keep it in the buffer for next time\n buffer = !valueString.endsWith(\"\\n\")\n ? new TextEncoder().encode(lines.pop() || \"\")\n : new Uint8Array();\n\n for (const line of lines) {\n const cleanedLine = line.replace(/^data: /, \"\");\n\n if (cleanedLine === \"[DONE]\") {\n await cleanup(controller);\n return;\n }\n\n const json = JSON.parse(cleanedLine);\n controller.enqueue(json);\n }\n } catch (error) {\n controller.error(error);\n await cleanup(controller);\n return;\n }\n }\n },\n cancel() {\n reader.cancel();\n },\n });\n}\n","import { ChatCompletionChunk, ToolCallFunctionCall } from \"./parse-chat-completion\";\n\nexport interface ChatCompletionContentEvent {\n type: \"content\";\n content: string;\n}\n\nexport interface ChatCompletionPartialEvent {\n type: \"partial\";\n name: string;\n arguments: string;\n}\n\nexport interface ChatCompletionFunctionEvent {\n type: \"function\";\n name: string;\n arguments: any;\n scope: \"client\" | \"server\";\n}\n\nexport interface ChatCompletionResultEvent {\n type: \"result\";\n content: string;\n name: string;\n}\n\nexport type ChatCompletionEvent =\n | ChatCompletionContentEvent\n | ChatCompletionPartialEvent\n | ChatCompletionFunctionEvent\n | ChatCompletionResultEvent;\n\nexport function decodeChatCompletion(\n stream: ReadableStream<ChatCompletionChunk>,\n): ReadableStream<ChatCompletionEvent> {\n const reader = stream.getReader();\n\n type Mode = { type: \"function\"; function: ToolCallFunctionCall } | { type: \"message\" };\n\n let mode: Mode | null = null;\n let functionCallName: string = \"\";\n let functionCallArguments: string = \"\";\n let functionCallScope: \"client\" | \"server\" = \"client\";\n\n async function cleanup(controller?: ReadableStreamDefaultController<any>) {\n if (controller) {\n try {\n controller.close();\n } catch (_) {}\n }\n if (reader) {\n try {\n await reader.cancel();\n } catch (_) {}\n }\n }\n\n return new ReadableStream<ChatCompletionEvent>({\n async pull(controller) {\n const flushFunctionCall = (): boolean => {\n let args: any = null;\n try {\n args = JSON.parse(functionCallArguments);\n } catch (error) {\n cleanup(controller);\n controller.error(error);\n return false;\n }\n controller.enqueue({\n type: \"function\",\n name: functionCallName,\n arguments: args,\n scope: functionCallScope,\n });\n\n mode = null;\n functionCallName = \"\";\n functionCallArguments = \"\";\n return true;\n };\n\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n if (done) {\n if (mode?.type === \"function\") {\n flushFunctionCall();\n }\n await cleanup(controller);\n return;\n }\n\n // In case we are currently handling a function call but the next message is either\n // - not a function call\n // - or is another function call (indicated by the presence of 'name' field in the next function call object)\n // => flush the current function call.\n if (\n mode?.type === \"function\" &&\n (!value.choices[0].delta.tool_calls?.[0]?.function ||\n value.choices[0].delta.tool_calls?.[0]?.function.name)\n ) {\n if (!flushFunctionCall()) {\n return;\n }\n }\n\n const maybeFunctionCall = value.choices[0].delta.tool_calls?.[0]?.function;\n if (maybeFunctionCall) {\n mode = { type: \"function\", function: maybeFunctionCall };\n } else {\n mode = { type: \"message\" };\n }\n\n // if we get a message, emit the content and continue;\n if (mode.type === \"message\") {\n // if we got a result message, send a result event\n if (value.choices[0].delta.role === \"function\") {\n controller.enqueue({\n type: \"result\",\n content: value.choices[0].delta.content!,\n name: value.choices[0].delta.name!,\n });\n }\n // otherwise, send a content event\n else if (value.choices[0].delta.content) {\n controller.enqueue({\n type: \"content\",\n content: value.choices[0].delta.content,\n });\n }\n continue;\n }\n // if we get a function call, buffer the name and arguments, then emit a partial event.\n else if (mode.type === \"function\") {\n const maybeFunctionCallName = mode.function.name;\n if (maybeFunctionCallName) {\n functionCallName = maybeFunctionCallName;\n }\n\n const maybeFunctionCallArguments = mode.function.arguments;\n if (maybeFunctionCallArguments) {\n functionCallArguments += maybeFunctionCallArguments;\n }\n\n const maybeFunctionCallScope = mode.function.scope;\n if (maybeFunctionCallScope) {\n functionCallScope = maybeFunctionCallScope;\n }\n\n controller.enqueue({\n type: \"partial\",\n name: functionCallName,\n arguments: functionCallArguments,\n });\n continue;\n }\n } catch (error) {\n controller.error(error);\n await cleanup(controller);\n return;\n }\n }\n },\n cancel() {\n reader.cancel();\n },\n });\n}\n","import { ChatCompletionEvent } from \"./decode-chat-completion\";\n\nexport function decodeChatCompletionAsText(\n stream: ReadableStream<ChatCompletionEvent>,\n): ReadableStream<string> {\n const reader = stream.getReader();\n\n return new ReadableStream<string>({\n async pull(controller) {\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n if (done) {\n controller.close();\n return;\n }\n\n if (value.type === \"content\") {\n controller.enqueue(value.content);\n continue;\n }\n } catch (error) {\n controller.error(error);\n return;\n }\n }\n },\n cancel() {\n reader.cancel();\n },\n });\n}\n","import { Action, AnnotatedFunction, ToolDefinition, Parameter } from \"../types\";\n\nexport function annotatedFunctionToChatCompletionFunction(\n annotatedFunction: AnnotatedFunction<any[]>,\n): ToolDefinition {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let arg of annotatedFunction.argumentAnnotations) {\n // isolate the args we should forward inline\n let { name, required, ...forwardedArgs } = arg;\n parameters[arg.name] = forwardedArgs;\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of annotatedFunction.argumentAnnotations) {\n if (arg.required) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n let chatCompletionFunction: ToolDefinition = {\n type: \"function\",\n function: {\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n },\n };\n\n return chatCompletionFunction;\n}\n\nfunction convertAttribute(attribute: Parameter): any {\n switch (attribute.type) {\n case \"string\":\n return {\n type: \"string\",\n description: attribute.description,\n ...(attribute.enum && { enum: attribute.enum }),\n };\n case \"number\":\n case \"boolean\":\n return {\n type: attribute.type,\n description: attribute.description,\n };\n case \"object\":\n case \"object[]\":\n const properties = attribute.attributes?.reduce((acc, attr) => {\n acc[attr.name] = convertAttribute(attr);\n return acc;\n }, {} as Record<string, any>);\n const required = attribute.attributes\n ?.filter((attr) => attr.required !== false)\n .map((attr) => attr.name);\n if (attribute.type === \"object[]\") {\n return {\n type: \"array\",\n items: {\n type: \"object\",\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n },\n description: attribute.description,\n };\n }\n return {\n type: \"object\",\n description: attribute.description,\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n };\n default:\n // Handle arrays of primitive types and undefined attribute.type\n if (attribute.type?.endsWith(\"[]\")) {\n const itemType = attribute.type.slice(0, -2);\n return {\n type: \"array\",\n items: { type: itemType },\n description: attribute.description,\n };\n }\n // Fallback for undefined type or any other unexpected type\n return {\n type: \"string\",\n description: attribute.description,\n };\n }\n}\n\nexport function actionToChatCompletionFunction(action: Action<any>): ToolDefinition {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let parameter of action.parameters || []) {\n parameters[parameter.name] = convertAttribute(parameter);\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of action.parameters || []) {\n if (arg.required !== false) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n let chatCompletionFunction: ToolDefinition = {\n type: \"function\",\n function: {\n name: action.name,\n ...(action.description && { description: action.description }),\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n },\n };\n\n return chatCompletionFunction;\n}\n\nexport function annotatedFunctionToAction(\n annotatedFunction: AnnotatedFunction<any[]>,\n): Action<any> {\n const parameters: Parameter[] = annotatedFunction.argumentAnnotations.map((annotation) => {\n switch (annotation.type) {\n case \"string\":\n case \"number\":\n case \"boolean\":\n case \"object\":\n return {\n name: annotation.name,\n description: annotation.description,\n type: annotation.type,\n required: annotation.required,\n };\n case \"array\":\n let type;\n if (annotation.items.type === \"string\") {\n type = \"string[]\";\n } else if (annotation.items.type === \"number\") {\n type = \"number[]\";\n } else if (annotation.items.type === \"boolean\") {\n type = \"boolean[]\";\n } else if (annotation.items.type === \"object\") {\n type = \"object[]\";\n } else {\n type = \"string[]\";\n }\n return {\n name: annotation.name,\n description: annotation.description,\n type: type as any,\n required: annotation.required,\n };\n }\n });\n\n return {\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n parameters: parameters,\n handler: (args) => {\n const paramsInCorrectOrder: any[] = [];\n for (let arg of annotatedFunction.argumentAnnotations) {\n paramsInCorrectOrder.push(args[arg.name]);\n }\n return annotatedFunction.implementation(...paramsInCorrectOrder);\n },\n };\n}\n","export const EXCLUDE_FROM_FORWARD_PROPS_KEYS = \"exclude_from_forward_props_keys\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;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AAW;AAAA,UACxB;AAAA,UACA,UAAU,WAAW;AAAA,QACvB;AAAA,IACJ;AAAA,EACF,CAAC;AAED,SAAO;AAAA,IACL,MAAM,kBAAkB;AAAA,IACxB,aAAa,kBAAkB;AAAA,IAC/B;AAAA,IACA,SAAS,CAAC,SAAS;AACjB,YAAM,uBAA8B,CAAC;AACrC,eAAS,OAAO,kBAAkB,qBAAqB;AACrD,6BAAqB,KAAK,KAAK,IAAI,IAAI,CAAC;AAAA,MAC1C;AACA,aAAO,kBAAkB,eAAe,GAAG,oBAAoB;AAAA,IACjE;AAAA,EACF;AACF;;;AC/KO,IAAM,kCAAkC;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/utils/utils.ts","../src/utils/parse-chat-completion.ts","../src/utils/decode-chat-completion.ts","../src/utils/decode-chat-completion-as-text.ts","../src/utils/annotated-function.ts","../src/constants/copilot-protocol.ts"],"sourcesContent":["export * from \"./types\";\nexport * from \"./utils\";\nexport * from \"./constants\";\n","import { AssistantMessage, FunctionCall, JSONValue } from \"../types/openai-assistant\";\n\nexport function encodeResult(result: string): string {\n if (result === undefined) {\n return \"\";\n } else if (typeof result === \"string\") {\n return result;\n } else {\n return JSON.stringify(result);\n }\n}\n\nexport function decodeResult(result: string): any {\n try {\n return JSON.parse(result);\n } catch (e) {\n return result;\n }\n}\n\nexport interface StreamPart<CODE extends string, NAME extends string, TYPE> {\n code: CODE;\n name: NAME;\n parse: (value: JSONValue) => { type: NAME; value: TYPE };\n}\n\nconst textStreamPart: StreamPart<\"0\", \"text\", string> = {\n code: \"0\",\n name: \"text\",\n parse: (value: JSONValue) => {\n if (typeof value !== \"string\") {\n throw new Error('\"text\" parts expect a string value.');\n }\n return { type: \"text\", value };\n },\n};\n\n/**\n * This is a utility function that helps in parsing the stream parts.\n * It takes a JSONValue as input and returns an object with type and value.\n * The type is a string that represents the type of the stream part.\n * The value is the actual value of the stream part.\n * If the input value is not a string, it throws an error.\n */\nconst functionCallStreamPart: StreamPart<\"1\", \"function_call\", { function_call: FunctionCall }> = {\n code: \"1\",\n name: \"function_call\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"function_call\" in value) ||\n typeof value.function_call !== \"object\" ||\n value.function_call == null ||\n !(\"name\" in value.function_call) ||\n !(\"arguments\" in value.function_call) ||\n typeof value.function_call.name !== \"string\" ||\n typeof value.function_call.arguments !== \"string\"\n ) {\n throw new Error('\"function_call\" parts expect an object with a \"function_call\" property.');\n }\n\n return {\n type: \"function_call\",\n value: value as unknown as { function_call: FunctionCall },\n };\n },\n};\n\nconst dataStreamPart: StreamPart<\"2\", \"data\", Array<JSONValue>> = {\n code: \"2\",\n name: \"data\",\n parse: (value: JSONValue) => {\n if (!Array.isArray(value)) {\n throw new Error('\"data\" parts expect an array value.');\n }\n\n return { type: \"data\", value };\n },\n};\n\nconst errorStreamPart: StreamPart<\"3\", \"error\", string> = {\n code: \"3\",\n name: \"error\",\n parse: (value: JSONValue) => {\n if (typeof value !== \"string\") {\n throw new Error('\"error\" parts expect a string value.');\n }\n return { type: \"error\", value };\n },\n};\n\nconst assistantMessage: StreamPart<\"4\", \"assistant_message\", AssistantMessage> = {\n code: \"4\",\n name: \"assistant_message\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"id\" in value) ||\n !(\"role\" in value) ||\n !(\"content\" in value) ||\n typeof value.id !== \"string\" ||\n typeof value.role !== \"string\" ||\n value.role !== \"assistant\" ||\n !Array.isArray(value.content) ||\n !value.content.every(\n (item) =>\n item != null &&\n typeof item === \"object\" &&\n \"type\" in item &&\n item.type === \"text\" &&\n \"text\" in item &&\n item.text != null &&\n typeof item.text === \"object\" &&\n \"value\" in item.text &&\n typeof item.text.value === \"string\",\n )\n ) {\n throw new Error(\n '\"assistant_message\" parts expect an object with an \"id\", \"role\", and \"content\" property.',\n );\n }\n\n return {\n type: \"assistant_message\",\n value: value as AssistantMessage,\n };\n },\n};\n\nconst assistantControlData: StreamPart<\n \"5\",\n \"assistant_control_data\",\n {\n threadId: string;\n messageId: string;\n }\n> = {\n code: \"5\",\n name: \"assistant_control_data\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"threadId\" in value) ||\n !(\"messageId\" in value) ||\n typeof value.threadId !== \"string\" ||\n typeof value.messageId !== \"string\"\n ) {\n throw new Error(\n '\"assistant_control_data\" parts expect an object with a \"threadId\" and \"messageId\" property.',\n );\n }\n\n return {\n type: \"assistant_control_data\",\n value: {\n threadId: value.threadId,\n messageId: value.messageId,\n },\n };\n },\n};\n\nconst streamParts = [\n textStreamPart,\n functionCallStreamPart,\n dataStreamPart,\n errorStreamPart,\n assistantMessage,\n assistantControlData,\n] as const;\n\n// union type of all stream parts\ntype StreamParts =\n | typeof textStreamPart\n | typeof functionCallStreamPart\n | typeof dataStreamPart\n | typeof errorStreamPart\n | typeof assistantMessage\n | typeof assistantControlData;\n\n/**\n * Maps the type of a stream part to its value type.\n */\ntype StreamPartValueType = {\n [P in StreamParts as P[\"name\"]]: ReturnType<P[\"parse\"]>[\"value\"];\n};\n\nexport type StreamPartType =\n | ReturnType<typeof textStreamPart.parse>\n | ReturnType<typeof functionCallStreamPart.parse>\n | ReturnType<typeof dataStreamPart.parse>\n | ReturnType<typeof errorStreamPart.parse>\n | ReturnType<typeof assistantMessage.parse>\n | ReturnType<typeof assistantControlData.parse>;\n\nexport const streamPartsByCode = {\n [textStreamPart.code]: textStreamPart,\n [functionCallStreamPart.code]: functionCallStreamPart,\n [dataStreamPart.code]: dataStreamPart,\n [errorStreamPart.code]: errorStreamPart,\n [assistantMessage.code]: assistantMessage,\n [assistantControlData.code]: assistantControlData,\n} as const;\n\n/**\n * The map of prefixes for data in the stream\n *\n * - 0: Text from the LLM response\n * - 1: (OpenAI) function_call responses\n * - 2: custom JSON added by the user using `Data`\n *\n * Example:\n * ```\n * 0:Vercel\n * 0:'s\n * 0: AI\n * 0: AI\n * 0: SDK\n * 0: is great\n * 0:!\n * 2: { \"someJson\": \"value\" }\n * 1: {\"function_call\": {\"name\": \"get_current_weather\", \"arguments\": \"{\\\\n\\\\\"location\\\\\": \\\\\"Charlottesville, Virginia\\\\\",\\\\n\\\\\"format\\\\\": \\\\\"celsius\\\\\"\\\\n}\"}}\n *```\n */\nexport const StreamStringPrefixes = {\n [textStreamPart.name]: textStreamPart.code,\n [functionCallStreamPart.name]: functionCallStreamPart.code,\n [dataStreamPart.name]: dataStreamPart.code,\n [errorStreamPart.name]: errorStreamPart.code,\n [assistantMessage.name]: assistantMessage.code,\n [assistantControlData.name]: assistantControlData.code,\n} as const;\n\nexport const validCodes = streamParts.map((part) => part.code);\n\n/**\n * Parses a stream part from a string.\n *\n * @param line The string to parse.\n * @returns The parsed stream part.\n * @throws An error if the string cannot be parsed.\n */\nexport const parseStreamPart = (line: string): StreamPartType => {\n const firstSeparatorIndex = line.indexOf(\":\");\n\n if (firstSeparatorIndex === -1) {\n throw new Error(\"Failed to parse stream string. No separator found.\");\n }\n\n const prefix = line.slice(0, firstSeparatorIndex);\n\n if (!validCodes.includes(prefix as keyof typeof streamPartsByCode)) {\n throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);\n }\n\n const code = prefix as keyof typeof streamPartsByCode;\n\n const textValue = line.slice(firstSeparatorIndex + 1);\n const jsonValue: JSONValue = JSON.parse(textValue);\n\n return streamPartsByCode[code].parse(jsonValue);\n};\n\n/**\n * Prepends a string with a prefix from the `StreamChunkPrefixes`, JSON-ifies it,\n * and appends a new line.\n *\n * It ensures type-safety for the part type and value.\n */\nexport function formatStreamPart<T extends keyof StreamPartValueType>(\n type: T,\n value: StreamPartValueType[T],\n): StreamString {\n const streamPart = streamParts.find((part) => part.name === type);\n\n if (!streamPart) {\n throw new Error(`Invalid stream part type: ${type}`);\n }\n\n return `${streamPart.code}:${JSON.stringify(value)}\\n`;\n}\n\nexport const isStreamStringEqualToType = (\n type: keyof typeof StreamStringPrefixes,\n value: string,\n): value is StreamString =>\n value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith(\"\\n\");\n\nexport type StreamString =\n `${typeof StreamStringPrefixes[keyof typeof StreamStringPrefixes]}:${string}\\n`;\n\n/**\n * A header sent to the client so it knows how to handle parsing the stream (as a deprecated text response or using the new prefixed protocol)\n */\nexport const COMPLEX_HEADER = \"X-Experimental-Stream-Data\";\n","import { Role } from \"../types/openai-assistant\";\n\nexport interface ChatCompletionChunk {\n choices: {\n delta: {\n id?: string;\n role: Role;\n content?: string | null;\n\n // TODO:\n // Temporarily add name to the OpenAI protocol until we\n // have our own protocol.\n // When name is set, we return the result of a server-side\n // function call.\n name?: string;\n\n function_call?: {\n name?: string;\n arguments?: string;\n };\n tool_calls?: {\n index: number;\n id?: string;\n function: {\n arguments?: string;\n\n name?: string;\n // TODO:\n // Temporarily add scope to the OpenAI protocol until we\n // have our own protocol.\n // When scope is \"server\", the client will not attempt to\n // execute the function.\n scope?: \"client\" | \"server\";\n };\n }[];\n };\n }[];\n}\n\n// TODO:\n// it's possible that unicode characters could be split across chunks\n// make sure to properly handle that\nexport function parseChatCompletion(\n stream: ReadableStream<Uint8Array>,\n): ReadableStream<ChatCompletionChunk> {\n const reader = stream.getReader();\n let buffer = new Uint8Array();\n\n async function cleanup(controller?: ReadableStreamDefaultController<any>) {\n if (controller) {\n try {\n controller.close();\n } catch (_) {}\n }\n if (reader) {\n try {\n await reader.cancel();\n } catch (_) {}\n }\n }\n\n return new ReadableStream<ChatCompletionChunk>({\n async pull(controller) {\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n if (done) {\n await cleanup(controller);\n return;\n }\n\n const newBuffer = new Uint8Array(buffer.length + value.length);\n newBuffer.set(buffer);\n newBuffer.set(value, buffer.length);\n buffer = newBuffer;\n\n const valueString = new TextDecoder(\"utf-8\").decode(buffer);\n const lines = valueString.split(\"\\n\").filter((line) => line.trim() !== \"\");\n\n // If the last line isn't complete, keep it in the buffer for next time\n buffer = !valueString.endsWith(\"\\n\")\n ? new TextEncoder().encode(lines.pop() || \"\")\n : new Uint8Array();\n\n for (const line of lines) {\n const cleanedLine = line.replace(/^data: /, \"\");\n\n if (cleanedLine === \"[DONE]\") {\n await cleanup(controller);\n return;\n }\n\n const json = JSON.parse(cleanedLine);\n controller.enqueue(json);\n }\n } catch (error) {\n controller.error(error);\n await cleanup(controller);\n return;\n }\n }\n },\n cancel() {\n reader.cancel();\n },\n });\n}\n","import { ChatCompletionChunk } from \"./parse-chat-completion\";\n\nexport interface ChatCompletionContentEvent {\n type: \"content\";\n content: string;\n}\n\nexport interface ChatCompletionPartialEvent {\n type: \"partial\";\n name: string;\n arguments: string;\n}\n\nexport interface ChatCompletionFunctionEvent {\n type: \"function\";\n name: string;\n arguments: any;\n scope: \"client\" | \"server\";\n}\n\nexport interface ChatCompletionResultEvent {\n type: \"result\";\n content: string;\n name: string;\n}\n\nexport type ChatCompletionEvent =\n | ChatCompletionContentEvent\n | ChatCompletionPartialEvent\n | ChatCompletionFunctionEvent\n | ChatCompletionResultEvent;\n\nexport function decodeChatCompletion(\n stream: ReadableStream<ChatCompletionChunk>,\n): ReadableStream<ChatCompletionEvent> {\n const reader = stream.getReader();\n\n let mode: \"function\" | \"message\" | null = null;\n let functionCallName: string = \"\";\n let functionCallArguments: string = \"\";\n let functionCallScope: \"client\" | \"server\" = \"client\";\n\n async function cleanup(controller?: ReadableStreamDefaultController<any>) {\n if (controller) {\n try {\n controller.close();\n } catch (_) {}\n }\n if (reader) {\n try {\n await reader.cancel();\n } catch (_) {}\n }\n }\n\n return new ReadableStream<ChatCompletionEvent>({\n async pull(controller) {\n const flushFunctionCall = (): boolean => {\n let args: any = null;\n try {\n args = JSON.parse(functionCallArguments);\n } catch (error) {\n cleanup(controller);\n controller.error(error);\n return false;\n }\n controller.enqueue({\n type: \"function\",\n name: functionCallName,\n arguments: args,\n scope: functionCallScope,\n });\n\n mode = null;\n functionCallName = \"\";\n functionCallArguments = \"\";\n return true;\n };\n\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n if (done) {\n if (mode === \"function\") {\n flushFunctionCall();\n }\n await cleanup(controller);\n return;\n }\n\n // In case we are in a function call but the next message is\n // - not a function call\n // - another function call (when name is present)\n // => flush it.\n if (\n mode === \"function\" &&\n (!value.choices[0].delta.tool_calls?.[0]?.function ||\n value.choices[0].delta.tool_calls?.[0]?.function.name)\n ) {\n if (!flushFunctionCall()) {\n return;\n }\n }\n\n mode = value.choices[0].delta.tool_calls?.[0]?.function ? \"function\" : \"message\";\n\n // if we get a message, emit the content and continue;\n if (mode === \"message\") {\n // if we got a result message, send a result event\n if (value.choices[0].delta.role === \"function\") {\n controller.enqueue({\n type: \"result\",\n content: value.choices[0].delta.content!,\n name: value.choices[0].delta.name!,\n });\n }\n // otherwise, send a content event\n else if (value.choices[0].delta.content) {\n controller.enqueue({\n type: \"content\",\n content: value.choices[0].delta.content,\n });\n }\n continue;\n }\n // if we get a function call, buffer the name and arguments, then emit a partial event.\n else if (mode === \"function\") {\n if (value.choices[0].delta.tool_calls![0].function.name) {\n functionCallName = value.choices[0].delta.tool_calls![0].function.name!;\n }\n if (value.choices[0].delta.tool_calls![0].function.arguments) {\n functionCallArguments += value.choices[0].delta.tool_calls![0].function.arguments!;\n }\n if (value.choices[0].delta.tool_calls![0].function.scope) {\n functionCallScope = value.choices[0].delta.tool_calls![0].function.scope!;\n }\n controller.enqueue({\n type: \"partial\",\n name: functionCallName,\n arguments: functionCallArguments,\n });\n continue;\n }\n } catch (error) {\n controller.error(error);\n await cleanup(controller);\n return;\n }\n }\n },\n cancel() {\n reader.cancel();\n },\n });\n}\n","import { ChatCompletionEvent } from \"./decode-chat-completion\";\n\nexport function decodeChatCompletionAsText(\n stream: ReadableStream<ChatCompletionEvent>,\n): ReadableStream<string> {\n const reader = stream.getReader();\n\n return new ReadableStream<string>({\n async pull(controller) {\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n if (done) {\n controller.close();\n return;\n }\n\n if (value.type === \"content\") {\n controller.enqueue(value.content);\n continue;\n }\n } catch (error) {\n controller.error(error);\n return;\n }\n }\n },\n cancel() {\n reader.cancel();\n },\n });\n}\n","import { Action, AnnotatedFunction, ToolDefinition, Parameter } from \"../types\";\n\nexport function annotatedFunctionToChatCompletionFunction(\n annotatedFunction: AnnotatedFunction<any[]>,\n): ToolDefinition {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let arg of annotatedFunction.argumentAnnotations) {\n // isolate the args we should forward inline\n let { name, required, ...forwardedArgs } = arg;\n parameters[arg.name] = forwardedArgs;\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of annotatedFunction.argumentAnnotations) {\n if (arg.required) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n let chatCompletionFunction: ToolDefinition = {\n type: \"function\",\n function: {\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n },\n };\n\n return chatCompletionFunction;\n}\n\nfunction convertAttribute(attribute: Parameter): any {\n switch (attribute.type) {\n case \"string\":\n return {\n type: \"string\",\n description: attribute.description,\n ...(attribute.enum && { enum: attribute.enum }),\n };\n case \"number\":\n case \"boolean\":\n return {\n type: attribute.type,\n description: attribute.description,\n };\n case \"object\":\n case \"object[]\":\n const properties = attribute.attributes?.reduce((acc, attr) => {\n acc[attr.name] = convertAttribute(attr);\n return acc;\n }, {} as Record<string, any>);\n const required = attribute.attributes\n ?.filter((attr) => attr.required !== false)\n .map((attr) => attr.name);\n if (attribute.type === \"object[]\") {\n return {\n type: \"array\",\n items: {\n type: \"object\",\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n },\n description: attribute.description,\n };\n }\n return {\n type: \"object\",\n description: attribute.description,\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n };\n default:\n // Handle arrays of primitive types and undefined attribute.type\n if (attribute.type?.endsWith(\"[]\")) {\n const itemType = attribute.type.slice(0, -2);\n return {\n type: \"array\",\n items: { type: itemType },\n description: attribute.description,\n };\n }\n // Fallback for undefined type or any other unexpected type\n return {\n type: \"string\",\n description: attribute.description,\n };\n }\n}\n\nexport function actionToChatCompletionFunction(action: Action<any>): ToolDefinition {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let parameter of action.parameters || []) {\n parameters[parameter.name] = convertAttribute(parameter);\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of action.parameters || []) {\n if (arg.required !== false) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n let chatCompletionFunction: ToolDefinition = {\n type: \"function\",\n function: {\n name: action.name,\n ...(action.description && { description: action.description }),\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n },\n };\n\n return chatCompletionFunction;\n}\n\nexport function annotatedFunctionToAction(\n annotatedFunction: AnnotatedFunction<any[]>,\n): Action<any> {\n const parameters: Parameter[] = annotatedFunction.argumentAnnotations.map((annotation) => {\n switch (annotation.type) {\n case \"string\":\n case \"number\":\n case \"boolean\":\n case \"object\":\n return {\n name: annotation.name,\n description: annotation.description,\n type: annotation.type,\n required: annotation.required,\n };\n case \"array\":\n let type;\n if (annotation.items.type === \"string\") {\n type = \"string[]\";\n } else if (annotation.items.type === \"number\") {\n type = \"number[]\";\n } else if (annotation.items.type === \"boolean\") {\n type = \"boolean[]\";\n } else if (annotation.items.type === \"object\") {\n type = \"object[]\";\n } else {\n type = \"string[]\";\n }\n return {\n name: annotation.name,\n description: annotation.description,\n type: type as any,\n required: annotation.required,\n };\n }\n });\n\n return {\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n parameters: parameters,\n handler: (args) => {\n const paramsInCorrectOrder: any[] = [];\n for (let arg of annotatedFunction.argumentAnnotations) {\n paramsInCorrectOrder.push(args[arg.name]);\n }\n return annotatedFunction.implementation(...paramsInCorrectOrder);\n },\n };\n}\n","export const EXCLUDE_FROM_FORWARD_PROPS_KEYS = \"excl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AAW,MAAM,SAAS,UAAU;AAC7C,iBAAO;AAAA,QACT,WAAW,WAAW,MAAM,SAAS,WAAW;AAC9C,iBAAO;AAAA,QACT,WAAW,WAAW,MAAM,SAAS,UAAU;AAC7C,iBAAO;AAAA,QACT,OAAO;AACL,iBAAO;AAAA,QACT;AACA,eAAO;AAAA,UACL,MAAM,WAAW;AAAA,UACjB,aAAa,WAAW;AAAA,UACxB;AAAA,UACA,UAAU,WAAW;AAAA,QACvB;AAAA,IACJ;AAAA,EACF,CAAC;AAED,SAAO;AAAA,IACL,MAAM,kBAAkB;AAAA,IACxB,aAAa,kBAAkB;AAAA,IAC/B;AAAA,IACA,SAAS,CAAC,SAAS;AACjB,YAAM,uBAA8B,CAAC;AACrC,eAAS,OAAO,kBAAkB,qBAAqB;AACrD,6BAAqB,KAAK,KAAK,IAAI,IAAI,CAAC;AAAA,MAC1C;AACA,aAAO,kBAAkB,eAAe,GAAG,oBAAoB;AAAA,IACjE;AAAA,EACF;AACF;;;AC/KO,IAAM,kCAAkC;","names":[]}
|
package/dist/index.mjs
CHANGED
|
@@ -247,24 +247,19 @@ function decodeChatCompletion(stream) {
|
|
|
247
247
|
try {
|
|
248
248
|
const { done, value } = await reader.read();
|
|
249
249
|
if (done) {
|
|
250
|
-
if (
|
|
250
|
+
if (mode === "function") {
|
|
251
251
|
flushFunctionCall();
|
|
252
252
|
}
|
|
253
253
|
await cleanup(controller);
|
|
254
254
|
return;
|
|
255
255
|
}
|
|
256
|
-
if (
|
|
256
|
+
if (mode === "function" && (!((_b = (_a = value.choices[0].delta.tool_calls) == null ? void 0 : _a[0]) == null ? void 0 : _b.function) || ((_d = (_c = value.choices[0].delta.tool_calls) == null ? void 0 : _c[0]) == null ? void 0 : _d.function.name))) {
|
|
257
257
|
if (!flushFunctionCall()) {
|
|
258
258
|
return;
|
|
259
259
|
}
|
|
260
260
|
}
|
|
261
|
-
|
|
262
|
-
if (
|
|
263
|
-
mode = { type: "function", function: maybeFunctionCall };
|
|
264
|
-
} else {
|
|
265
|
-
mode = { type: "message" };
|
|
266
|
-
}
|
|
267
|
-
if (mode.type === "message") {
|
|
261
|
+
mode = ((_f = (_e = value.choices[0].delta.tool_calls) == null ? void 0 : _e[0]) == null ? void 0 : _f.function) ? "function" : "message";
|
|
262
|
+
if (mode === "message") {
|
|
268
263
|
if (value.choices[0].delta.role === "function") {
|
|
269
264
|
controller.enqueue({
|
|
270
265
|
type: "result",
|
|
@@ -278,18 +273,15 @@ function decodeChatCompletion(stream) {
|
|
|
278
273
|
});
|
|
279
274
|
}
|
|
280
275
|
continue;
|
|
281
|
-
} else if (mode
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
functionCallName = maybeFunctionCallName;
|
|
276
|
+
} else if (mode === "function") {
|
|
277
|
+
if (value.choices[0].delta.tool_calls[0].function.name) {
|
|
278
|
+
functionCallName = value.choices[0].delta.tool_calls[0].function.name;
|
|
285
279
|
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
functionCallArguments += maybeFunctionCallArguments;
|
|
280
|
+
if (value.choices[0].delta.tool_calls[0].function.arguments) {
|
|
281
|
+
functionCallArguments += value.choices[0].delta.tool_calls[0].function.arguments;
|
|
289
282
|
}
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
functionCallScope = maybeFunctionCallScope;
|
|
283
|
+
if (value.choices[0].delta.tool_calls[0].function.scope) {
|
|
284
|
+
functionCallScope = value.choices[0].delta.tool_calls[0].function.scope;
|
|
293
285
|
}
|
|
294
286
|
controller.enqueue({
|
|
295
287
|
type: "partial",
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/utils/utils.ts","../src/utils/parse-chat-completion.ts","../src/utils/decode-chat-completion.ts","../src/utils/decode-chat-completion-as-text.ts","../src/utils/annotated-function.ts","../src/constants/copilot-protocol.ts"],"sourcesContent":["import { AssistantMessage, FunctionCall, JSONValue } from \"../types/openai-assistant\";\n\nexport function encodeResult(result: string): string {\n if (result === undefined) {\n return \"\";\n } else if (typeof result === \"string\") {\n return result;\n } else {\n return JSON.stringify(result);\n }\n}\n\nexport function decodeResult(result: string): any {\n try {\n return JSON.parse(result);\n } catch (e) {\n return result;\n }\n}\n\nexport interface StreamPart<CODE extends string, NAME extends string, TYPE> {\n code: CODE;\n name: NAME;\n parse: (value: JSONValue) => { type: NAME; value: TYPE };\n}\n\nconst textStreamPart: StreamPart<\"0\", \"text\", string> = {\n code: \"0\",\n name: \"text\",\n parse: (value: JSONValue) => {\n if (typeof value !== \"string\") {\n throw new Error('\"text\" parts expect a string value.');\n }\n return { type: \"text\", value };\n },\n};\n\n/**\n * This is a utility function that helps in parsing the stream parts.\n * It takes a JSONValue as input and returns an object with type and value.\n * The type is a string that represents the type of the stream part.\n * The value is the actual value of the stream part.\n * If the input value is not a string, it throws an error.\n */\nconst functionCallStreamPart: StreamPart<\"1\", \"function_call\", { function_call: FunctionCall }> = {\n code: \"1\",\n name: \"function_call\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"function_call\" in value) ||\n typeof value.function_call !== \"object\" ||\n value.function_call == null ||\n !(\"name\" in value.function_call) ||\n !(\"arguments\" in value.function_call) ||\n typeof value.function_call.name !== \"string\" ||\n typeof value.function_call.arguments !== \"string\"\n ) {\n throw new Error('\"function_call\" parts expect an object with a \"function_call\" property.');\n }\n\n return {\n type: \"function_call\",\n value: value as unknown as { function_call: FunctionCall },\n };\n },\n};\n\nconst dataStreamPart: StreamPart<\"2\", \"data\", Array<JSONValue>> = {\n code: \"2\",\n name: \"data\",\n parse: (value: JSONValue) => {\n if (!Array.isArray(value)) {\n throw new Error('\"data\" parts expect an array value.');\n }\n\n return { type: \"data\", value };\n },\n};\n\nconst errorStreamPart: StreamPart<\"3\", \"error\", string> = {\n code: \"3\",\n name: \"error\",\n parse: (value: JSONValue) => {\n if (typeof value !== \"string\") {\n throw new Error('\"error\" parts expect a string value.');\n }\n return { type: \"error\", value };\n },\n};\n\nconst assistantMessage: StreamPart<\"4\", \"assistant_message\", AssistantMessage> = {\n code: \"4\",\n name: \"assistant_message\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"id\" in value) ||\n !(\"role\" in value) ||\n !(\"content\" in value) ||\n typeof value.id !== \"string\" ||\n typeof value.role !== \"string\" ||\n value.role !== \"assistant\" ||\n !Array.isArray(value.content) ||\n !value.content.every(\n (item) =>\n item != null &&\n typeof item === \"object\" &&\n \"type\" in item &&\n item.type === \"text\" &&\n \"text\" in item &&\n item.text != null &&\n typeof item.text === \"object\" &&\n \"value\" in item.text &&\n typeof item.text.value === \"string\",\n )\n ) {\n throw new Error(\n '\"assistant_message\" parts expect an object with an \"id\", \"role\", and \"content\" property.',\n );\n }\n\n return {\n type: \"assistant_message\",\n value: value as AssistantMessage,\n };\n },\n};\n\nconst assistantControlData: StreamPart<\n \"5\",\n \"assistant_control_data\",\n {\n threadId: string;\n messageId: string;\n }\n> = {\n code: \"5\",\n name: \"assistant_control_data\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"threadId\" in value) ||\n !(\"messageId\" in value) ||\n typeof value.threadId !== \"string\" ||\n typeof value.messageId !== \"string\"\n ) {\n throw new Error(\n '\"assistant_control_data\" parts expect an object with a \"threadId\" and \"messageId\" property.',\n );\n }\n\n return {\n type: \"assistant_control_data\",\n value: {\n threadId: value.threadId,\n messageId: value.messageId,\n },\n };\n },\n};\n\nconst streamParts = [\n textStreamPart,\n functionCallStreamPart,\n dataStreamPart,\n errorStreamPart,\n assistantMessage,\n assistantControlData,\n] as const;\n\n// union type of all stream parts\ntype StreamParts =\n | typeof textStreamPart\n | typeof functionCallStreamPart\n | typeof dataStreamPart\n | typeof errorStreamPart\n | typeof assistantMessage\n | typeof assistantControlData;\n\n/**\n * Maps the type of a stream part to its value type.\n */\ntype StreamPartValueType = {\n [P in StreamParts as P[\"name\"]]: ReturnType<P[\"parse\"]>[\"value\"];\n};\n\nexport type StreamPartType =\n | ReturnType<typeof textStreamPart.parse>\n | ReturnType<typeof functionCallStreamPart.parse>\n | ReturnType<typeof dataStreamPart.parse>\n | ReturnType<typeof errorStreamPart.parse>\n | ReturnType<typeof assistantMessage.parse>\n | ReturnType<typeof assistantControlData.parse>;\n\nexport const streamPartsByCode = {\n [textStreamPart.code]: textStreamPart,\n [functionCallStreamPart.code]: functionCallStreamPart,\n [dataStreamPart.code]: dataStreamPart,\n [errorStreamPart.code]: errorStreamPart,\n [assistantMessage.code]: assistantMessage,\n [assistantControlData.code]: assistantControlData,\n} as const;\n\n/**\n * The map of prefixes for data in the stream\n *\n * - 0: Text from the LLM response\n * - 1: (OpenAI) function_call responses\n * - 2: custom JSON added by the user using `Data`\n *\n * Example:\n * ```\n * 0:Vercel\n * 0:'s\n * 0: AI\n * 0: AI\n * 0: SDK\n * 0: is great\n * 0:!\n * 2: { \"someJson\": \"value\" }\n * 1: {\"function_call\": {\"name\": \"get_current_weather\", \"arguments\": \"{\\\\n\\\\\"location\\\\\": \\\\\"Charlottesville, Virginia\\\\\",\\\\n\\\\\"format\\\\\": \\\\\"celsius\\\\\"\\\\n}\"}}\n *```\n */\nexport const StreamStringPrefixes = {\n [textStreamPart.name]: textStreamPart.code,\n [functionCallStreamPart.name]: functionCallStreamPart.code,\n [dataStreamPart.name]: dataStreamPart.code,\n [errorStreamPart.name]: errorStreamPart.code,\n [assistantMessage.name]: assistantMessage.code,\n [assistantControlData.name]: assistantControlData.code,\n} as const;\n\nexport const validCodes = streamParts.map((part) => part.code);\n\n/**\n * Parses a stream part from a string.\n *\n * @param line The string to parse.\n * @returns The parsed stream part.\n * @throws An error if the string cannot be parsed.\n */\nexport const parseStreamPart = (line: string): StreamPartType => {\n const firstSeparatorIndex = line.indexOf(\":\");\n\n if (firstSeparatorIndex === -1) {\n throw new Error(\"Failed to parse stream string. No separator found.\");\n }\n\n const prefix = line.slice(0, firstSeparatorIndex);\n\n if (!validCodes.includes(prefix as keyof typeof streamPartsByCode)) {\n throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);\n }\n\n const code = prefix as keyof typeof streamPartsByCode;\n\n const textValue = line.slice(firstSeparatorIndex + 1);\n const jsonValue: JSONValue = JSON.parse(textValue);\n\n return streamPartsByCode[code].parse(jsonValue);\n};\n\n/**\n * Prepends a string with a prefix from the `StreamChunkPrefixes`, JSON-ifies it,\n * and appends a new line.\n *\n * It ensures type-safety for the part type and value.\n */\nexport function formatStreamPart<T extends keyof StreamPartValueType>(\n type: T,\n value: StreamPartValueType[T],\n): StreamString {\n const streamPart = streamParts.find((part) => part.name === type);\n\n if (!streamPart) {\n throw new Error(`Invalid stream part type: ${type}`);\n }\n\n return `${streamPart.code}:${JSON.stringify(value)}\\n`;\n}\n\nexport const isStreamStringEqualToType = (\n type: keyof typeof StreamStringPrefixes,\n value: string,\n): value is StreamString =>\n value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith(\"\\n\");\n\nexport type StreamString =\n `${typeof StreamStringPrefixes[keyof typeof StreamStringPrefixes]}:${string}\\n`;\n\n/**\n * A header sent to the client so it knows how to handle parsing the stream (as a deprecated text response or using the new prefixed protocol)\n */\nexport const COMPLEX_HEADER = \"X-Experimental-Stream-Data\";\n","import { Role } from \"../types/openai-assistant\";\n\nexport interface ToolCallFunctionCall {\n arguments?: string;\n\n name?: string;\n // TODO:\n // Temporarily add scope to the OpenAI protocol until we\n // have our own protocol.\n // When scope is \"server\", the client will not attempt to\n // execute the function.\n scope?: \"client\" | \"server\";\n}\n\nexport interface ToolCallPayload {\n index: number;\n id?: string;\n function: ToolCallFunctionCall;\n}\n\nexport interface ChatCompletionChunk {\n choices: {\n delta: {\n id?: string;\n role: Role;\n content?: string | null;\n\n // TODO:\n // Temporarily add name to the OpenAI protocol until we\n // have our own protocol.\n // When name is set, we return the result of a server-side\n // function call.\n name?: string;\n\n function_call?: {\n name?: string;\n arguments?: string;\n };\n tool_calls?: ToolCallPayload[];\n };\n }[];\n}\n\n// TODO:\n// it's possible that unicode characters could be split across chunks\n// make sure to properly handle that\nexport function parseChatCompletion(\n stream: ReadableStream<Uint8Array>,\n): ReadableStream<ChatCompletionChunk> {\n const reader = stream.getReader();\n let buffer = new Uint8Array();\n\n async function cleanup(controller?: ReadableStreamDefaultController<any>) {\n if (controller) {\n try {\n controller.close();\n } catch (_) {}\n }\n if (reader) {\n try {\n await reader.cancel();\n } catch (_) {}\n }\n }\n\n return new ReadableStream<ChatCompletionChunk>({\n async pull(controller) {\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n if (done) {\n await cleanup(controller);\n return;\n }\n\n const newBuffer = new Uint8Array(buffer.length + value.length);\n newBuffer.set(buffer);\n newBuffer.set(value, buffer.length);\n buffer = newBuffer;\n\n const valueString = new TextDecoder(\"utf-8\").decode(buffer);\n const lines = valueString.split(\"\\n\").filter((line) => line.trim() !== \"\");\n\n // If the last line isn't complete, keep it in the buffer for next time\n buffer = !valueString.endsWith(\"\\n\")\n ? new TextEncoder().encode(lines.pop() || \"\")\n : new Uint8Array();\n\n for (const line of lines) {\n const cleanedLine = line.replace(/^data: /, \"\");\n\n if (cleanedLine === \"[DONE]\") {\n await cleanup(controller);\n return;\n }\n\n const json = JSON.parse(cleanedLine);\n controller.enqueue(json);\n }\n } catch (error) {\n controller.error(error);\n await cleanup(controller);\n return;\n }\n }\n },\n cancel() {\n reader.cancel();\n },\n });\n}\n","import { ChatCompletionChunk, ToolCallFunctionCall } from \"./parse-chat-completion\";\n\nexport interface ChatCompletionContentEvent {\n type: \"content\";\n content: string;\n}\n\nexport interface ChatCompletionPartialEvent {\n type: \"partial\";\n name: string;\n arguments: string;\n}\n\nexport interface ChatCompletionFunctionEvent {\n type: \"function\";\n name: string;\n arguments: any;\n scope: \"client\" | \"server\";\n}\n\nexport interface ChatCompletionResultEvent {\n type: \"result\";\n content: string;\n name: string;\n}\n\nexport type ChatCompletionEvent =\n | ChatCompletionContentEvent\n | ChatCompletionPartialEvent\n | ChatCompletionFunctionEvent\n | ChatCompletionResultEvent;\n\nexport function decodeChatCompletion(\n stream: ReadableStream<ChatCompletionChunk>,\n): ReadableStream<ChatCompletionEvent> {\n const reader = stream.getReader();\n\n type Mode = { type: \"function\"; function: ToolCallFunctionCall } | { type: \"message\" };\n\n let mode: Mode | null = null;\n let functionCallName: string = \"\";\n let functionCallArguments: string = \"\";\n let functionCallScope: \"client\" | \"server\" = \"client\";\n\n async function cleanup(controller?: ReadableStreamDefaultController<any>) {\n if (controller) {\n try {\n controller.close();\n } catch (_) {}\n }\n if (reader) {\n try {\n await reader.cancel();\n } catch (_) {}\n }\n }\n\n return new ReadableStream<ChatCompletionEvent>({\n async pull(controller) {\n const flushFunctionCall = (): boolean => {\n let args: any = null;\n try {\n args = JSON.parse(functionCallArguments);\n } catch (error) {\n cleanup(controller);\n controller.error(error);\n return false;\n }\n controller.enqueue({\n type: \"function\",\n name: functionCallName,\n arguments: args,\n scope: functionCallScope,\n });\n\n mode = null;\n functionCallName = \"\";\n functionCallArguments = \"\";\n return true;\n };\n\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n if (done) {\n if (mode?.type === \"function\") {\n flushFunctionCall();\n }\n await cleanup(controller);\n return;\n }\n\n // In case we are currently handling a function call but the next message is either\n // - not a function call\n // - or is another function call (indicated by the presence of 'name' field in the next function call object)\n // => flush the current function call.\n if (\n mode?.type === \"function\" &&\n (!value.choices[0].delta.tool_calls?.[0]?.function ||\n value.choices[0].delta.tool_calls?.[0]?.function.name)\n ) {\n if (!flushFunctionCall()) {\n return;\n }\n }\n\n const maybeFunctionCall = value.choices[0].delta.tool_calls?.[0]?.function;\n if (maybeFunctionCall) {\n mode = { type: \"function\", function: maybeFunctionCall };\n } else {\n mode = { type: \"message\" };\n }\n\n // if we get a message, emit the content and continue;\n if (mode.type === \"message\") {\n // if we got a result message, send a result event\n if (value.choices[0].delta.role === \"function\") {\n controller.enqueue({\n type: \"result\",\n content: value.choices[0].delta.content!,\n name: value.choices[0].delta.name!,\n });\n }\n // otherwise, send a content event\n else if (value.choices[0].delta.content) {\n controller.enqueue({\n type: \"content\",\n content: value.choices[0].delta.content,\n });\n }\n continue;\n }\n // if we get a function call, buffer the name and arguments, then emit a partial event.\n else if (mode.type === \"function\") {\n const maybeFunctionCallName = mode.function.name;\n if (maybeFunctionCallName) {\n functionCallName = maybeFunctionCallName;\n }\n\n const maybeFunctionCallArguments = mode.function.arguments;\n if (maybeFunctionCallArguments) {\n functionCallArguments += maybeFunctionCallArguments;\n }\n\n const maybeFunctionCallScope = mode.function.scope;\n if (maybeFunctionCallScope) {\n functionCallScope = maybeFunctionCallScope;\n }\n\n controller.enqueue({\n type: \"partial\",\n name: functionCallName,\n arguments: functionCallArguments,\n });\n continue;\n }\n } catch (error) {\n controller.error(error);\n await cleanup(controller);\n return;\n }\n }\n },\n cancel() {\n reader.cancel();\n },\n });\n}\n","import { ChatCompletionEvent } from \"./decode-chat-completion\";\n\nexport function decodeChatCompletionAsText(\n stream: ReadableStream<ChatCompletionEvent>,\n): ReadableStream<string> {\n const reader = stream.getReader();\n\n return new ReadableStream<string>({\n async pull(controller) {\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n if (done) {\n controller.close();\n return;\n }\n\n if (value.type === \"content\") {\n controller.enqueue(value.content);\n continue;\n }\n } catch (error) {\n controller.error(error);\n return;\n }\n }\n },\n cancel() {\n reader.cancel();\n },\n });\n}\n","import { Action, AnnotatedFunction, ToolDefinition, Parameter } from \"../types\";\n\nexport function annotatedFunctionToChatCompletionFunction(\n annotatedFunction: AnnotatedFunction<any[]>,\n): ToolDefinition {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let arg of annotatedFunction.argumentAnnotations) {\n // isolate the args we should forward inline\n let { name, required, ...forwardedArgs } = arg;\n parameters[arg.name] = forwardedArgs;\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of annotatedFunction.argumentAnnotations) {\n if (arg.required) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n let chatCompletionFunction: ToolDefinition = {\n type: \"function\",\n function: {\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n },\n };\n\n return chatCompletionFunction;\n}\n\nfunction convertAttribute(attribute: Parameter): any {\n switch (attribute.type) {\n case \"string\":\n return {\n type: \"string\",\n description: attribute.description,\n ...(attribute.enum && { enum: attribute.enum }),\n };\n case \"number\":\n case \"boolean\":\n return {\n type: attribute.type,\n description: attribute.description,\n };\n case \"object\":\n case \"object[]\":\n const properties = attribute.attributes?.reduce((acc, attr) => {\n acc[attr.name] = convertAttribute(attr);\n return acc;\n }, {} as Record<string, any>);\n const required = attribute.attributes\n ?.filter((attr) => attr.required !== false)\n .map((attr) => attr.name);\n if (attribute.type === \"object[]\") {\n return {\n type: \"array\",\n items: {\n type: \"object\",\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n },\n description: attribute.description,\n };\n }\n return {\n type: \"object\",\n description: attribute.description,\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n };\n default:\n // Handle arrays of primitive types and undefined attribute.type\n if (attribute.type?.endsWith(\"[]\")) {\n const itemType = attribute.type.slice(0, -2);\n return {\n type: \"array\",\n items: { type: itemType },\n description: attribute.description,\n };\n }\n // Fallback for undefined type or any other unexpected type\n return {\n type: \"string\",\n description: attribute.description,\n };\n }\n}\n\nexport function actionToChatCompletionFunction(action: Action<any>): ToolDefinition {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let parameter of action.parameters || []) {\n parameters[parameter.name] = convertAttribute(parameter);\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of action.parameters || []) {\n if (arg.required !== false) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n let chatCompletionFunction: ToolDefinition = {\n type: \"function\",\n function: {\n name: action.name,\n ...(action.description && { description: action.description }),\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n },\n };\n\n return chatCompletionFunction;\n}\n\nexport function annotatedFunctionToAction(\n annotatedFunction: AnnotatedFunction<any[]>,\n): Action<any> {\n const parameters: Parameter[] = annotatedFunction.argumentAnnotations.map((annotation) => {\n switch (annotation.type) {\n case \"string\":\n case \"number\":\n case \"boolean\":\n case \"object\":\n return {\n name: annotation.name,\n description: annotation.description,\n type: annotation.type,\n required: annotation.required,\n };\n case \"array\":\n let type;\n if (annotation.items.type === \"string\") {\n type = \"string[]\";\n } else if (annotation.items.type === \"number\") {\n type = \"number[]\";\n } else if (annotation.items.type === \"boolean\") {\n type = \"boolean[]\";\n } else if (annotation.items.type === \"object\") {\n type = \"object[]\";\n } else {\n type = \"string[]\";\n }\n return {\n name: annotation.name,\n description: annotation.description,\n type: type as any,\n required: annotation.required,\n };\n }\n });\n\n return {\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n parameters: parameters,\n handler: (args) => {\n const paramsInCorrectOrder: any[] = [];\n for (let arg of annotatedFunction.argumentAnnotations) {\n paramsInCorrectOrder.push(args[arg.name]);\n }\n return annotatedFunction.implementation(...paramsInCorrectOrder);\n },\n };\n}\n","export const EXCLUDE_FROM_FORWARD_PROPS_KEYS = \"exclude_from_forward_props_keys\";\n"],"mappings":";AAEO,SAAS,aAAa,QAAwB;AACnD,MAAI,WAAW,QAAW;AACxB,WAAO;AAAA,EACT,WAAW,OAAO,WAAW,UAAU;AACrC,WAAO;AAAA,EACT,OAAO;AACL,WAAO,KAAK,UAAU,MAAM;AAAA,EAC9B;AACF;AAEO,SAAS,aAAa,QAAqB;AAChD,MAAI;AACF,WAAO,KAAK,MAAM,MAAM;AAAA,EAC1B,SAAS,GAAP;AACA,WAAO;AAAA,EACT;AACF;AAQA,IAAM,iBAAkD;AAAA,EACtD,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO,CAAC,UAAqB;AAC3B,QAAI,OAAO,UAAU,UAAU;AAC7B,YAAM,IAA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sources":["../src/utils/utils.ts","../src/utils/parse-chat-completion.ts","../src/utils/decode-chat-completion.ts","../src/utils/decode-chat-completion-as-text.ts","../src/utils/annotated-function.ts","../src/constants/copilot-protocol.ts"],"sourcesContent":["import { AssistantMessage, FunctionCall, JSONValue } from \"../types/openai-assistant\";\n\nexport function encodeResult(result: string): string {\n if (result === undefined) {\n return \"\";\n } else if (typeof result === \"string\") {\n return result;\n } else {\n return JSON.stringify(result);\n }\n}\n\nexport function decodeResult(result: string): any {\n try {\n return JSON.parse(result);\n } catch (e) {\n return result;\n }\n}\n\nexport interface StreamPart<CODE extends string, NAME extends string, TYPE> {\n code: CODE;\n name: NAME;\n parse: (value: JSONValue) => { type: NAME; value: TYPE };\n}\n\nconst textStreamPart: StreamPart<\"0\", \"text\", string> = {\n code: \"0\",\n name: \"text\",\n parse: (value: JSONValue) => {\n if (typeof value !== \"string\") {\n throw new Error('\"text\" parts expect a string value.');\n }\n return { type: \"text\", value };\n },\n};\n\n/**\n * This is a utility function that helps in parsing the stream parts.\n * It takes a JSONValue as input and returns an object with type and value.\n * The type is a string that represents the type of the stream part.\n * The value is the actual value of the stream part.\n * If the input value is not a string, it throws an error.\n */\nconst functionCallStreamPart: StreamPart<\"1\", \"function_call\", { function_call: FunctionCall }> = {\n code: \"1\",\n name: \"function_call\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"function_call\" in value) ||\n typeof value.function_call !== \"object\" ||\n value.function_call == null ||\n !(\"name\" in value.function_call) ||\n !(\"arguments\" in value.function_call) ||\n typeof value.function_call.name !== \"string\" ||\n typeof value.function_call.arguments !== \"string\"\n ) {\n throw new Error('\"function_call\" parts expect an object with a \"function_call\" property.');\n }\n\n return {\n type: \"function_call\",\n value: value as unknown as { function_call: FunctionCall },\n };\n },\n};\n\nconst dataStreamPart: StreamPart<\"2\", \"data\", Array<JSONValue>> = {\n code: \"2\",\n name: \"data\",\n parse: (value: JSONValue) => {\n if (!Array.isArray(value)) {\n throw new Error('\"data\" parts expect an array value.');\n }\n\n return { type: \"data\", value };\n },\n};\n\nconst errorStreamPart: StreamPart<\"3\", \"error\", string> = {\n code: \"3\",\n name: \"error\",\n parse: (value: JSONValue) => {\n if (typeof value !== \"string\") {\n throw new Error('\"error\" parts expect a string value.');\n }\n return { type: \"error\", value };\n },\n};\n\nconst assistantMessage: StreamPart<\"4\", \"assistant_message\", AssistantMessage> = {\n code: \"4\",\n name: \"assistant_message\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"id\" in value) ||\n !(\"role\" in value) ||\n !(\"content\" in value) ||\n typeof value.id !== \"string\" ||\n typeof value.role !== \"string\" ||\n value.role !== \"assistant\" ||\n !Array.isArray(value.content) ||\n !value.content.every(\n (item) =>\n item != null &&\n typeof item === \"object\" &&\n \"type\" in item &&\n item.type === \"text\" &&\n \"text\" in item &&\n item.text != null &&\n typeof item.text === \"object\" &&\n \"value\" in item.text &&\n typeof item.text.value === \"string\",\n )\n ) {\n throw new Error(\n '\"assistant_message\" parts expect an object with an \"id\", \"role\", and \"content\" property.',\n );\n }\n\n return {\n type: \"assistant_message\",\n value: value as AssistantMessage,\n };\n },\n};\n\nconst assistantControlData: StreamPart<\n \"5\",\n \"assistant_control_data\",\n {\n threadId: string;\n messageId: string;\n }\n> = {\n code: \"5\",\n name: \"assistant_control_data\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"threadId\" in value) ||\n !(\"messageId\" in value) ||\n typeof value.threadId !== \"string\" ||\n typeof value.messageId !== \"string\"\n ) {\n throw new Error(\n '\"assistant_control_data\" parts expect an object with a \"threadId\" and \"messageId\" property.',\n );\n }\n\n return {\n type: \"assistant_control_data\",\n value: {\n threadId: value.threadId,\n messageId: value.messageId,\n },\n };\n },\n};\n\nconst streamParts = [\n textStreamPart,\n functionCallStreamPart,\n dataStreamPart,\n errorStreamPart,\n assistantMessage,\n assistantControlData,\n] as const;\n\n// union type of all stream parts\ntype StreamParts =\n | typeof textStreamPart\n | typeof functionCallStreamPart\n | typeof dataStreamPart\n | typeof errorStreamPart\n | typeof assistantMessage\n | typeof assistantControlData;\n\n/**\n * Maps the type of a stream part to its value type.\n */\ntype StreamPartValueType = {\n [P in StreamParts as P[\"name\"]]: ReturnType<P[\"parse\"]>[\"value\"];\n};\n\nexport type StreamPartType =\n | ReturnType<typeof textStreamPart.parse>\n | ReturnType<typeof functionCallStreamPart.parse>\n | ReturnType<typeof dataStreamPart.parse>\n | ReturnType<typeof errorStreamPart.parse>\n | ReturnType<typeof assistantMessage.parse>\n | ReturnType<typeof assistantControlData.parse>;\n\nexport const streamPartsByCode = {\n [textStreamPart.code]: textStreamPart,\n [functionCallStreamPart.code]: functionCallStreamPart,\n [dataStreamPart.code]: dataStreamPart,\n [errorStreamPart.code]: errorStreamPart,\n [assistantMessage.code]: assistantMessage,\n [assistantControlData.code]: assistantControlData,\n} as const;\n\n/**\n * The map of prefixes for data in the stream\n *\n * - 0: Text from the LLM response\n * - 1: (OpenAI) function_call responses\n * - 2: custom JSON added by the user using `Data`\n *\n * Example:\n * ```\n * 0:Vercel\n * 0:'s\n * 0: AI\n * 0: AI\n * 0: SDK\n * 0: is great\n * 0:!\n * 2: { \"someJson\": \"value\" }\n * 1: {\"function_call\": {\"name\": \"get_current_weather\", \"arguments\": \"{\\\\n\\\\\"location\\\\\": \\\\\"Charlottesville, Virginia\\\\\",\\\\n\\\\\"format\\\\\": \\\\\"celsius\\\\\"\\\\n}\"}}\n *```\n */\nexport const StreamStringPrefixes = {\n [textStreamPart.name]: textStreamPart.code,\n [functionCallStreamPart.name]: functionCallStreamPart.code,\n [dataStreamPart.name]: dataStreamPart.code,\n [errorStreamPart.name]: errorStreamPart.code,\n [assistantMessage.name]: assistantMessage.code,\n [assistantControlData.name]: assistantControlData.code,\n} as const;\n\nexport const validCodes = streamParts.map((part) => part.code);\n\n/**\n * Parses a stream part from a string.\n *\n * @param line The string to parse.\n * @returns The parsed stream part.\n * @throws An error if the string cannot be parsed.\n */\nexport const parseStreamPart = (line: string): StreamPartType => {\n const firstSeparatorIndex = line.indexOf(\":\");\n\n if (firstSeparatorIndex === -1) {\n throw new Error(\"Failed to parse stream string. No separator found.\");\n }\n\n const prefix = line.slice(0, firstSeparatorIndex);\n\n if (!validCodes.includes(prefix as keyof typeof streamPartsByCode)) {\n throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);\n }\n\n const code = prefix as keyof typeof streamPartsByCode;\n\n const textValue = line.slice(firstSeparatorIndex + 1);\n const jsonValue: JSONValue = JSON.parse(textValue);\n\n return streamPartsByCode[code].parse(jsonValue);\n};\n\n/**\n * Prepends a string with a prefix from the `StreamChunkPrefixes`, JSON-ifies it,\n * and appends a new line.\n *\n * It ensures type-safety for the part type and value.\n */\nexport function formatStreamPart<T extends keyof StreamPartValueType>(\n type: T,\n value: StreamPartValueType[T],\n): StreamString {\n const streamPart = streamParts.find((part) => part.name === type);\n\n if (!streamPart) {\n throw new Error(`Invalid stream part type: ${type}`);\n }\n\n return `${streamPart.code}:${JSON.stringify(value)}\\n`;\n}\n\nexport const isStreamStringEqualToType = (\n type: keyof typeof StreamStringPrefixes,\n value: string,\n): value is StreamString =>\n value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith(\"\\n\");\n\nexport type StreamString =\n `${typeof StreamStringPrefixes[keyof typeof StreamStringPrefixes]}:${string}\\n`;\n\n/**\n * A header sent to the client so it knows how to handle parsing the stream (as a deprecated text response or using the new prefixed protocol)\n */\nexport const COMPLEX_HEADER = \"X-Experimental-Stream-Data\";\n","import { Role } from \"../types/openai-assistant\";\n\nexport interface ChatCompletionChunk {\n choices: {\n delta: {\n id?: string;\n role: Role;\n content?: string | null;\n\n // TODO:\n // Temporarily add name to the OpenAI protocol until we\n // have our own protocol.\n // When name is set, we return the result of a server-side\n // function call.\n name?: string;\n\n function_call?: {\n name?: string;\n arguments?: string;\n };\n tool_calls?: {\n index: number;\n id?: string;\n function: {\n arguments?: string;\n\n name?: string;\n // TODO:\n // Temporarily add scope to the OpenAI protocol until we\n // have our own protocol.\n // When scope is \"server\", the client will not attempt to\n // execute the function.\n scope?: \"client\" | \"server\";\n };\n }[];\n };\n }[];\n}\n\n// TODO:\n// it's possible that unicode characters could be split across chunks\n// make sure to properly handle that\nexport function parseChatCompletion(\n stream: ReadableStream<Uint8Array>,\n): ReadableStream<ChatCompletionChunk> {\n const reader = stream.getReader();\n let buffer = new Uint8Array();\n\n async function cleanup(controller?: ReadableStreamDefaultController<any>) {\n if (controller) {\n try {\n controller.close();\n } catch (_) {}\n }\n if (reader) {\n try {\n await reader.cancel();\n } catch (_) {}\n }\n }\n\n return new ReadableStream<ChatCompletionChunk>({\n async pull(controller) {\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n if (done) {\n await cleanup(controller);\n return;\n }\n\n const newBuffer = new Uint8Array(buffer.length + value.length);\n newBuffer.set(buffer);\n newBuffer.set(value, buffer.length);\n buffer = newBuffer;\n\n const valueString = new TextDecoder(\"utf-8\").decode(buffer);\n const lines = valueString.split(\"\\n\").filter((line) => line.trim() !== \"\");\n\n // If the last line isn't complete, keep it in the buffer for next time\n buffer = !valueString.endsWith(\"\\n\")\n ? new TextEncoder().encode(lines.pop() || \"\")\n : new Uint8Array();\n\n for (const line of lines) {\n const cleanedLine = line.replace(/^data: /, \"\");\n\n if (cleanedLine === \"[DONE]\") {\n await cleanup(controller);\n return;\n }\n\n const json = JSON.parse(cleanedLine);\n controller.enqueue(json);\n }\n } catch (error) {\n controller.error(error);\n await cleanup(controller);\n return;\n }\n }\n },\n cancel() {\n reader.cancel();\n },\n });\n}\n","import { ChatCompletionChunk } from \"./parse-chat-completion\";\n\nexport interface ChatCompletionContentEvent {\n type: \"content\";\n content: string;\n}\n\nexport interface ChatCompletionPartialEvent {\n type: \"partial\";\n name: string;\n arguments: string;\n}\n\nexport interface ChatCompletionFunctionEvent {\n type: \"function\";\n name: string;\n arguments: any;\n scope: \"client\" | \"server\";\n}\n\nexport interface ChatCompletionResultEvent {\n type: \"result\";\n content: string;\n name: string;\n}\n\nexport type ChatCompletionEvent =\n | ChatCompletionContentEvent\n | ChatCompletionPartialEvent\n | ChatCompletionFunctionEvent\n | ChatCompletionResultEvent;\n\nexport function decodeChatCompletion(\n stream: ReadableStream<ChatCompletionChunk>,\n): ReadableStream<ChatCompletionEvent> {\n const reader = stream.getReader();\n\n let mode: \"function\" | \"message\" | null = null;\n let functionCallName: string = \"\";\n let functionCallArguments: string = \"\";\n let functionCallScope: \"client\" | \"server\" = \"client\";\n\n async function cleanup(controller?: ReadableStreamDefaultController<any>) {\n if (controller) {\n try {\n controller.close();\n } catch (_) {}\n }\n if (reader) {\n try {\n await reader.cancel();\n } catch (_) {}\n }\n }\n\n return new ReadableStream<ChatCompletionEvent>({\n async pull(controller) {\n const flushFunctionCall = (): boolean => {\n let args: any = null;\n try {\n args = JSON.parse(functionCallArguments);\n } catch (error) {\n cleanup(controller);\n controller.error(error);\n return false;\n }\n controller.enqueue({\n type: \"function\",\n name: functionCallName,\n arguments: args,\n scope: functionCallScope,\n });\n\n mode = null;\n functionCallName = \"\";\n functionCallArguments = \"\";\n return true;\n };\n\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n if (done) {\n if (mode === \"function\") {\n flushFunctionCall();\n }\n await cleanup(controller);\n return;\n }\n\n // In case we are in a function call but the next message is\n // - not a function call\n // - another function call (when name is present)\n // => flush it.\n if (\n mode === \"function\" &&\n (!value.choices[0].delta.tool_calls?.[0]?.function ||\n value.choices[0].delta.tool_calls?.[0]?.function.name)\n ) {\n if (!flushFunctionCall()) {\n return;\n }\n }\n\n mode = value.choices[0].delta.tool_calls?.[0]?.function ? \"function\" : \"message\";\n\n // if we get a message, emit the content and continue;\n if (mode === \"message\") {\n // if we got a result message, send a result event\n if (value.choices[0].delta.role === \"function\") {\n controller.enqueue({\n type: \"result\",\n content: value.choices[0].delta.content!,\n name: value.choices[0].delta.name!,\n });\n }\n // otherwise, send a content event\n else if (value.choices[0].delta.content) {\n controller.enqueue({\n type: \"content\",\n content: value.choices[0].delta.content,\n });\n }\n continue;\n }\n // if we get a function call, buffer the name and arguments, then emit a partial event.\n else if (mode === \"function\") {\n if (value.choices[0].delta.tool_calls![0].function.name) {\n functionCallName = value.choices[0].delta.tool_calls![0].function.name!;\n }\n if (value.choices[0].delta.tool_calls![0].function.arguments) {\n functionCallArguments += value.choices[0].delta.tool_calls![0].function.arguments!;\n }\n if (value.choices[0].delta.tool_calls![0].function.scope) {\n functionCallScope = value.choices[0].delta.tool_calls![0].function.scope!;\n }\n controller.enqueue({\n type: \"partial\",\n name: functionCallName,\n arguments: functionCallArguments,\n });\n continue;\n }\n } catch (error) {\n controller.error(error);\n await cleanup(controller);\n return;\n }\n }\n },\n cancel() {\n reader.cancel();\n },\n });\n}\n","import { ChatCompletionEvent } from \"./decode-chat-completion\";\n\nexport function decodeChatCompletionAsText(\n stream: ReadableStream<ChatCompletionEvent>,\n): ReadableStream<string> {\n const reader = stream.getReader();\n\n return new ReadableStream<string>({\n async pull(controller) {\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n if (done) {\n controller.close();\n return;\n }\n\n if (value.type === \"content\") {\n controller.enqueue(value.content);\n continue;\n }\n } catch (error) {\n controller.error(error);\n return;\n }\n }\n },\n cancel() {\n reader.cancel();\n },\n });\n}\n","import { Action, AnnotatedFunction, ToolDefinition, Parameter } from \"../types\";\n\nexport function annotatedFunctionToChatCompletionFunction(\n annotatedFunction: AnnotatedFunction<any[]>,\n): ToolDefinition {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let arg of annotatedFunction.argumentAnnotations) {\n // isolate the args we should forward inline\n let { name, required, ...forwardedArgs } = arg;\n parameters[arg.name] = forwardedArgs;\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of annotatedFunction.argumentAnnotations) {\n if (arg.required) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n let chatCompletionFunction: ToolDefinition = {\n type: \"function\",\n function: {\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n },\n };\n\n return chatCompletionFunction;\n}\n\nfunction convertAttribute(attribute: Parameter): any {\n switch (attribute.type) {\n case \"string\":\n return {\n type: \"string\",\n description: attribute.description,\n ...(attribute.enum && { enum: attribute.enum }),\n };\n case \"number\":\n case \"boolean\":\n return {\n type: attribute.type,\n description: attribute.description,\n };\n case \"object\":\n case \"object[]\":\n const properties = attribute.attributes?.reduce((acc, attr) => {\n acc[attr.name] = convertAttribute(attr);\n return acc;\n }, {} as Record<string, any>);\n const required = attribute.attributes\n ?.filter((attr) => attr.required !== false)\n .map((attr) => attr.name);\n if (attribute.type === \"object[]\") {\n return {\n type: \"array\",\n items: {\n type: \"object\",\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n },\n description: attribute.description,\n };\n }\n return {\n type: \"object\",\n description: attribute.description,\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n };\n default:\n // Handle arrays of primitive types and undefined attribute.type\n if (attribute.type?.endsWith(\"[]\")) {\n const itemType = attribute.type.slice(0, -2);\n return {\n type: \"array\",\n items: { type: itemType },\n description: attribute.description,\n };\n }\n // Fallback for undefined type or any other unexpected type\n return {\n type: \"string\",\n description: attribute.description,\n };\n }\n}\n\nexport function actionToChatCompletionFunction(action: Action<any>): ToolDefinition {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let parameter of action.parameters || []) {\n parameters[parameter.name] = convertAttribute(parameter);\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of action.parameters || []) {\n if (arg.required !== false) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n let chatCompletionFunction: ToolDefinition = {\n type: \"function\",\n function: {\n name: action.name,\n ...(action.description && { description: action.description }),\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n },\n };\n\n return chatCompletionFunction;\n}\n\nexport function annotatedFunctionToAction(\n annotatedFunction: AnnotatedFunction<any[]>,\n): Action<any> {\n const parameters: Parameter[] = annotatedFunction.argumentAnnotations.map((annotation) => {\n switch (annotation.type) {\n case \"string\":\n case \"number\":\n case \"boolean\":\n case \"object\":\n return {\n name: annotation.name,\n description: annotation.description,\n type: annotation.type,\n required: annotation.required,\n };\n case \"array\":\n let type;\n if (annotation.items.type === \"string\") {\n type = \"string[]\";\n } else if (annotation.items.type === \"number\") {\n type = \"number[]\";\n } else if (annotation.items.type === \"boolean\") {\n type = \"boolean[]\";\n } else if (annotation.items.type === \"object\") {\n type = \"object[]\";\n } else {\n type = \"string[]\";\n }\n return {\n name: annotation.name,\n description: annotation.description,\n type: type as any,\n required: annotation.required,\n };\n }\n });\n\n return {\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n parameters: parameters,\n handler: (args) => {\n const paramsInCorrectOrder: any[] = [];\n for (let arg of annotatedFunction.argumentAnnotations) {\n paramsInCorrectOrder.push(args[arg.name]);\n }\n return annotatedFunction.implementation(...paramsInCorrectOrder);\n },\n };\n}\n","export const EXCLUDE_FROM_FORWARD_PROPS_KEYS = \"exclude_from_forward_props_keys\";\n"],"mappings":";AAEO,SAAS,aAAa,QAAwB;AACnD,MAAI,WAAW,QAAW;AACxB,WAAO;AAAA,EACT,WAAW,OAAO,WAAW,UAAU;AACrC,WAAO;AAAA,EACT,OAAO;AACL,WAAO,KAAK,UAAU,MAAM;AAAA,EAC9B;AACF;AAEO,SAAS,aAAa,QAAqB;AAChD,MAAI;AACF,WAAO,KAAK,MAAM,MAAM;AAAA,EAC1B,SAAS,GAAP;AACA,WAAO;AAAA,EACT;AACF;AAQA,IAAM,iBAAkD;AAAA,EACtD,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO,CAAC,UAAqB;AAC3B,QAAI,OAAO,UAAU,UAAU;AAC7B,YAAM,IAAI,MAAM,qCAAqC;AAAA,IACvD;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M,kCAAkC;","names":[]}
|
package/dist/types/action.d.ts
CHANGED
|
@@ -8,44 +8,45 @@ type TypeMap = {
|
|
|
8
8
|
"boolean[]": boolean[];
|
|
9
9
|
"object[]": object[];
|
|
10
10
|
};
|
|
11
|
-
type
|
|
11
|
+
type AbstractParameter = {
|
|
12
12
|
name: string;
|
|
13
|
-
type?:
|
|
13
|
+
type?: keyof TypeMap;
|
|
14
14
|
description?: string;
|
|
15
15
|
required?: boolean;
|
|
16
16
|
};
|
|
17
|
-
|
|
18
|
-
name: string;
|
|
17
|
+
interface StringParameter extends AbstractParameter {
|
|
19
18
|
type: "string";
|
|
20
|
-
description?: string;
|
|
21
|
-
required?: boolean;
|
|
22
19
|
enum?: string[];
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
type: "object" | "object[]";
|
|
27
|
-
description?: string;
|
|
28
|
-
required?: boolean;
|
|
20
|
+
}
|
|
21
|
+
interface ObjectParameter extends AbstractParameter {
|
|
22
|
+
type: "object";
|
|
29
23
|
attributes?: Parameter[];
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
type
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
type
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
24
|
+
}
|
|
25
|
+
interface ObjectArrayParameter extends AbstractParameter {
|
|
26
|
+
type: "object[]";
|
|
27
|
+
attributes?: Parameter[];
|
|
28
|
+
}
|
|
29
|
+
type SpecialParameters = StringParameter | ObjectParameter | ObjectArrayParameter;
|
|
30
|
+
interface BaseParameter extends AbstractParameter {
|
|
31
|
+
type?: Exclude<AbstractParameter["type"], SpecialParameters["type"]>;
|
|
32
|
+
}
|
|
33
|
+
type Parameter = BaseParameter | SpecialParameters;
|
|
34
|
+
type OptionalParameterType<P extends AbstractParameter> = P["required"] extends false ? undefined : never;
|
|
35
|
+
type StringParameterType<P> = P extends StringParameter ? P extends {
|
|
36
|
+
enum?: Array<infer E>;
|
|
37
|
+
} ? E : string : never;
|
|
38
|
+
type ObjectParameterType<P> = P extends ObjectParameter ? P extends {
|
|
39
|
+
attributes?: infer Attributes extends Parameter[];
|
|
40
|
+
} ? MappedParameterTypes<Attributes> : object : never;
|
|
41
|
+
type ObjectArrayParameterType<P> = P extends ObjectArrayParameter ? P extends {
|
|
42
|
+
attributes?: infer Attributes extends Parameter[];
|
|
43
|
+
} ? MappedParameterTypes<Attributes>[] : any[] : never;
|
|
44
|
+
type MappedTypeOrString<T> = T extends keyof TypeMap ? TypeMap[T] : string;
|
|
45
|
+
type BaseParameterType<P extends AbstractParameter> = P extends {
|
|
46
|
+
type: infer T;
|
|
47
|
+
} ? T extends BaseParameter["type"] ? MappedTypeOrString<T> : never : string;
|
|
48
|
+
type MappedParameterTypes<T extends Parameter[] | [] = []> = T extends [] ? Record<string, any> : {
|
|
49
|
+
[P in T[number] as P["name"]]: OptionalParameterType<P> | StringParameterType<P> | ObjectParameterType<P> | ObjectArrayParameterType<P> | BaseParameterType<P>;
|
|
49
50
|
};
|
|
50
51
|
type Action<T extends Parameter[] | [] = []> = {
|
|
51
52
|
name: string;
|
|
@@ -54,4 +55,4 @@ type Action<T extends Parameter[] | [] = []> = {
|
|
|
54
55
|
handler: T extends [] ? () => any | Promise<any> : (args: MappedParameterTypes<T>) => any | Promise<any>;
|
|
55
56
|
};
|
|
56
57
|
|
|
57
|
-
export { Action,
|
|
58
|
+
export { Action, MappedParameterTypes, Parameter };
|