@copilotkit/shared 0.9.0-multi-release-alpha.0 → 0.9.0-multi-feature-usecopilotreadable.3
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/.turbo/turbo-build.log +73 -77
- package/CHANGELOG.md +20 -2
- package/dist/constants/copilot-protocol.mjs +3 -2
- package/dist/constants/copilot-protocol.mjs.map +1 -1
- package/dist/constants/index.mjs +4 -2
- package/dist/constants/index.mjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +19 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +34 -489
- package/dist/index.mjs.map +1 -1
- package/dist/types/action.mjs +1 -0
- package/dist/types/annotated-function.mjs +1 -0
- package/dist/types/index.mjs +4 -0
- package/dist/types/openai-assistant.mjs +1 -0
- package/dist/utils/annotated-function.mjs +5 -153
- package/dist/utils/annotated-function.mjs.map +1 -1
- package/dist/utils/decode-chat-completion-as-text.mjs +3 -27
- package/dist/utils/decode-chat-completion-as-text.mjs.map +1 -1
- package/dist/utils/decode-chat-completion.js +19 -11
- package/dist/utils/decode-chat-completion.js.map +1 -1
- package/dist/utils/decode-chat-completion.mjs +3 -103
- package/dist/utils/decode-chat-completion.mjs.map +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +19 -11
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +26 -486
- package/dist/utils/index.mjs.map +1 -1
- package/dist/utils/parse-chat-completion.d.ts +12 -10
- package/dist/utils/parse-chat-completion.js.map +1 -1
- package/dist/utils/parse-chat-completion.mjs +3 -55
- package/dist/utils/parse-chat-completion.mjs.map +1 -1
- package/dist/utils/utils.mjs +11 -144
- package/dist/utils/utils.mjs.map +1 -1
- package/package.json +3 -3
- package/src/utils/decode-chat-completion.ts +29 -16
- package/src/utils/parse-chat-completion.ts +19 -15
|
@@ -1,58 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
let buffer = new Uint8Array();
|
|
5
|
-
async function cleanup(controller) {
|
|
6
|
-
if (controller) {
|
|
7
|
-
try {
|
|
8
|
-
controller.close();
|
|
9
|
-
} catch (_) {
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
if (reader) {
|
|
13
|
-
try {
|
|
14
|
-
await reader.cancel();
|
|
15
|
-
} catch (_) {
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
return new ReadableStream({
|
|
20
|
-
async pull(controller) {
|
|
21
|
-
while (true) {
|
|
22
|
-
try {
|
|
23
|
-
const { done, value } = await reader.read();
|
|
24
|
-
if (done) {
|
|
25
|
-
await cleanup(controller);
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
const newBuffer = new Uint8Array(buffer.length + value.length);
|
|
29
|
-
newBuffer.set(buffer);
|
|
30
|
-
newBuffer.set(value, buffer.length);
|
|
31
|
-
buffer = newBuffer;
|
|
32
|
-
const valueString = new TextDecoder("utf-8").decode(buffer);
|
|
33
|
-
const lines = valueString.split("\n").filter((line) => line.trim() !== "");
|
|
34
|
-
buffer = !valueString.endsWith("\n") ? new TextEncoder().encode(lines.pop() || "") : new Uint8Array();
|
|
35
|
-
for (const line of lines) {
|
|
36
|
-
const cleanedLine = line.replace(/^data: /, "");
|
|
37
|
-
if (cleanedLine === "[DONE]") {
|
|
38
|
-
await cleanup(controller);
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
const json = JSON.parse(cleanedLine);
|
|
42
|
-
controller.enqueue(json);
|
|
43
|
-
}
|
|
44
|
-
} catch (error) {
|
|
45
|
-
controller.error(error);
|
|
46
|
-
await cleanup(controller);
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
},
|
|
51
|
-
cancel() {
|
|
52
|
-
reader.cancel();
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
parseChatCompletion
|
|
3
|
+
} from "../chunk-HW4V75UQ.mjs";
|
|
56
4
|
export {
|
|
57
5
|
parseChatCompletion
|
|
58
6
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/utils/utils.mjs
CHANGED
|
@@ -1,147 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
try {
|
|
13
|
-
return JSON.parse(result);
|
|
14
|
-
} catch (e) {
|
|
15
|
-
return result;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
var textStreamPart = {
|
|
19
|
-
code: "0",
|
|
20
|
-
name: "text",
|
|
21
|
-
parse: (value) => {
|
|
22
|
-
if (typeof value !== "string") {
|
|
23
|
-
throw new Error('"text" parts expect a string value.');
|
|
24
|
-
}
|
|
25
|
-
return { type: "text", value };
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
var functionCallStreamPart = {
|
|
29
|
-
code: "1",
|
|
30
|
-
name: "function_call",
|
|
31
|
-
parse: (value) => {
|
|
32
|
-
if (value == null || typeof value !== "object" || !("function_call" in value) || typeof value.function_call !== "object" || value.function_call == null || !("name" in value.function_call) || !("arguments" in value.function_call) || typeof value.function_call.name !== "string" || typeof value.function_call.arguments !== "string") {
|
|
33
|
-
throw new Error('"function_call" parts expect an object with a "function_call" property.');
|
|
34
|
-
}
|
|
35
|
-
return {
|
|
36
|
-
type: "function_call",
|
|
37
|
-
value
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
var dataStreamPart = {
|
|
42
|
-
code: "2",
|
|
43
|
-
name: "data",
|
|
44
|
-
parse: (value) => {
|
|
45
|
-
if (!Array.isArray(value)) {
|
|
46
|
-
throw new Error('"data" parts expect an array value.');
|
|
47
|
-
}
|
|
48
|
-
return { type: "data", value };
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
var errorStreamPart = {
|
|
52
|
-
code: "3",
|
|
53
|
-
name: "error",
|
|
54
|
-
parse: (value) => {
|
|
55
|
-
if (typeof value !== "string") {
|
|
56
|
-
throw new Error('"error" parts expect a string value.');
|
|
57
|
-
}
|
|
58
|
-
return { type: "error", value };
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
var assistantMessage = {
|
|
62
|
-
code: "4",
|
|
63
|
-
name: "assistant_message",
|
|
64
|
-
parse: (value) => {
|
|
65
|
-
if (value == null || typeof value !== "object" || !("id" in value) || !("role" in value) || !("content" in value) || typeof value.id !== "string" || typeof value.role !== "string" || value.role !== "assistant" || !Array.isArray(value.content) || !value.content.every(
|
|
66
|
-
(item) => item != null && typeof item === "object" && "type" in item && item.type === "text" && "text" in item && item.text != null && typeof item.text === "object" && "value" in item.text && typeof item.text.value === "string"
|
|
67
|
-
)) {
|
|
68
|
-
throw new Error(
|
|
69
|
-
'"assistant_message" parts expect an object with an "id", "role", and "content" property.'
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
return {
|
|
73
|
-
type: "assistant_message",
|
|
74
|
-
value
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
var assistantControlData = {
|
|
79
|
-
code: "5",
|
|
80
|
-
name: "assistant_control_data",
|
|
81
|
-
parse: (value) => {
|
|
82
|
-
if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
|
|
83
|
-
throw new Error(
|
|
84
|
-
'"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
return {
|
|
88
|
-
type: "assistant_control_data",
|
|
89
|
-
value: {
|
|
90
|
-
threadId: value.threadId,
|
|
91
|
-
messageId: value.messageId
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
var streamParts = [
|
|
97
|
-
textStreamPart,
|
|
98
|
-
functionCallStreamPart,
|
|
99
|
-
dataStreamPart,
|
|
100
|
-
errorStreamPart,
|
|
101
|
-
assistantMessage,
|
|
102
|
-
assistantControlData
|
|
103
|
-
];
|
|
104
|
-
var streamPartsByCode = {
|
|
105
|
-
[textStreamPart.code]: textStreamPart,
|
|
106
|
-
[functionCallStreamPart.code]: functionCallStreamPart,
|
|
107
|
-
[dataStreamPart.code]: dataStreamPart,
|
|
108
|
-
[errorStreamPart.code]: errorStreamPart,
|
|
109
|
-
[assistantMessage.code]: assistantMessage,
|
|
110
|
-
[assistantControlData.code]: assistantControlData
|
|
111
|
-
};
|
|
112
|
-
var StreamStringPrefixes = {
|
|
113
|
-
[textStreamPart.name]: textStreamPart.code,
|
|
114
|
-
[functionCallStreamPart.name]: functionCallStreamPart.code,
|
|
115
|
-
[dataStreamPart.name]: dataStreamPart.code,
|
|
116
|
-
[errorStreamPart.name]: errorStreamPart.code,
|
|
117
|
-
[assistantMessage.name]: assistantMessage.code,
|
|
118
|
-
[assistantControlData.name]: assistantControlData.code
|
|
119
|
-
};
|
|
120
|
-
var validCodes = streamParts.map((part) => part.code);
|
|
121
|
-
var parseStreamPart = (line) => {
|
|
122
|
-
const firstSeparatorIndex = line.indexOf(":");
|
|
123
|
-
if (firstSeparatorIndex === -1) {
|
|
124
|
-
throw new Error("Failed to parse stream string. No separator found.");
|
|
125
|
-
}
|
|
126
|
-
const prefix = line.slice(0, firstSeparatorIndex);
|
|
127
|
-
if (!validCodes.includes(prefix)) {
|
|
128
|
-
throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
|
|
129
|
-
}
|
|
130
|
-
const code = prefix;
|
|
131
|
-
const textValue = line.slice(firstSeparatorIndex + 1);
|
|
132
|
-
const jsonValue = JSON.parse(textValue);
|
|
133
|
-
return streamPartsByCode[code].parse(jsonValue);
|
|
134
|
-
};
|
|
135
|
-
function formatStreamPart(type, value) {
|
|
136
|
-
const streamPart = streamParts.find((part) => part.name === type);
|
|
137
|
-
if (!streamPart) {
|
|
138
|
-
throw new Error(`Invalid stream part type: ${type}`);
|
|
139
|
-
}
|
|
140
|
-
return `${streamPart.code}:${JSON.stringify(value)}
|
|
141
|
-
`;
|
|
142
|
-
}
|
|
143
|
-
var isStreamStringEqualToType = (type, value) => value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith("\n");
|
|
144
|
-
var COMPLEX_HEADER = "X-Experimental-Stream-Data";
|
|
1
|
+
import {
|
|
2
|
+
COMPLEX_HEADER,
|
|
3
|
+
StreamStringPrefixes,
|
|
4
|
+
decodeResult,
|
|
5
|
+
encodeResult,
|
|
6
|
+
formatStreamPart,
|
|
7
|
+
isStreamStringEqualToType,
|
|
8
|
+
parseStreamPart,
|
|
9
|
+
streamPartsByCode,
|
|
10
|
+
validCodes
|
|
11
|
+
} from "../chunk-2C54HQBI.mjs";
|
|
145
12
|
export {
|
|
146
13
|
COMPLEX_HEADER,
|
|
147
14
|
StreamStringPrefixes,
|
package/dist/utils/utils.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/utils.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"],"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;AACA,WAAO,EAAE,MAAM,QAAQ,MAAM;AAAA,EAC/B;AACF;AASA,IAAM,yBAA4F;AAAA,EAChG,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO,CAAC,UAAqB;AAC3B,QACE,SAAS,QACT,OAAO,UAAU,YACjB,EAAE,mBAAmB,UACrB,OAAO,MAAM,kBAAkB,YAC/B,MAAM,iBAAiB,QACvB,EAAE,UAAU,MAAM,kBAClB,EAAE,eAAe,MAAM,kBACvB,OAAO,MAAM,cAAc,SAAS,YACpC,OAAO,MAAM,cAAc,cAAc,UACzC;AACA,YAAM,IAAI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S,MAAwC,GAAG;AAClE,UAAM,IAAI,MAAM,+CAA+C,SAAS;AAAA,EAC1E;AAEA,QAAM,OAAO;AAEb,QAAM,YAAY,KAAK,MAAM,sBAAsB,CAAC;AACpD,QAAM,YAAuB,KAAK,MAAM,SAAS;AAEjD,SAAO,kBAAkB,IAAI,EAAE,MAAM,SAAS;AAChD;AAQO,SAAS,iBACd,MACA,OACc;AACd,QAAM,aAAa,YAAY,KAAK,CAAC,SAAS,KAAK,SAAS,IAAI;AAEhE,MAAI,CAAC,YAAY;AACf,UAAM,IAAI,MAAM,6BAA6B,MAAM;AAAA,EACrD;AAEA,SAAO,GAAG,WAAW,QAAQ,KAAK,UAAU,KAAK;AAAA;AACnD;AAEO,IAAM,4BAA4B,CACvC,MACA,UAEA,MAAM,WAAW,GAAG,qBAAqB,IAAI,IAAI,KAAK,MAAM,SAAS,IAAI;AAQpE,IAAM,iBAAiB;","names":[]}
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.9.0-multi-
|
|
7
|
+
"version": "0.9.0-multi-feature-usecopilotreadable.3",
|
|
8
8
|
"sideEffects": false,
|
|
9
9
|
"main": "./dist/index.js",
|
|
10
10
|
"module": "./dist/index.mjs",
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
"ts-jest": "^29.1.1",
|
|
26
26
|
"tsup": "^6.7.0",
|
|
27
27
|
"typescript": "^5.2.3",
|
|
28
|
-
"eslint-config-custom": "0.10.0-multi-
|
|
29
|
-
"tsconfig": "0.14.0-multi-
|
|
28
|
+
"eslint-config-custom": "0.10.0-multi-feature-usecopilotreadable.3",
|
|
29
|
+
"tsconfig": "0.14.0-multi-feature-usecopilotreadable.3"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {},
|
|
32
32
|
"scripts": {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ChatCompletionChunk } from "./parse-chat-completion";
|
|
1
|
+
import { ChatCompletionChunk, ToolCallFunctionCall } from "./parse-chat-completion";
|
|
2
2
|
|
|
3
3
|
export interface ChatCompletionContentEvent {
|
|
4
4
|
type: "content";
|
|
@@ -35,7 +35,9 @@ export function decodeChatCompletion(
|
|
|
35
35
|
): ReadableStream<ChatCompletionEvent> {
|
|
36
36
|
const reader = stream.getReader();
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
type Mode = { type: "function"; function: ToolCallFunctionCall } | { type: "message" };
|
|
39
|
+
|
|
40
|
+
let mode: Mode | null = null;
|
|
39
41
|
let functionCallName: string = "";
|
|
40
42
|
let functionCallArguments: string = "";
|
|
41
43
|
let functionCallScope: "client" | "server" = "client";
|
|
@@ -82,19 +84,19 @@ export function decodeChatCompletion(
|
|
|
82
84
|
const { done, value } = await reader.read();
|
|
83
85
|
|
|
84
86
|
if (done) {
|
|
85
|
-
if (mode === "function") {
|
|
87
|
+
if (mode?.type === "function") {
|
|
86
88
|
flushFunctionCall();
|
|
87
89
|
}
|
|
88
90
|
await cleanup(controller);
|
|
89
91
|
return;
|
|
90
92
|
}
|
|
91
93
|
|
|
92
|
-
// In case we are
|
|
94
|
+
// In case we are currently handling a function call but the next message is either
|
|
93
95
|
// - not a function call
|
|
94
|
-
// - another function call (
|
|
95
|
-
// => flush
|
|
96
|
+
// - or is another function call (indicated by the presence of 'name' field in the next function call object)
|
|
97
|
+
// => flush the current function call.
|
|
96
98
|
if (
|
|
97
|
-
mode === "function" &&
|
|
99
|
+
mode?.type === "function" &&
|
|
98
100
|
(!value.choices[0].delta.tool_calls?.[0]?.function ||
|
|
99
101
|
value.choices[0].delta.tool_calls?.[0]?.function.name)
|
|
100
102
|
) {
|
|
@@ -103,10 +105,15 @@ export function decodeChatCompletion(
|
|
|
103
105
|
}
|
|
104
106
|
}
|
|
105
107
|
|
|
106
|
-
|
|
108
|
+
const maybeFunctionCall = value.choices[0].delta.tool_calls?.[0]?.function;
|
|
109
|
+
if (maybeFunctionCall) {
|
|
110
|
+
mode = { type: "function", function: maybeFunctionCall };
|
|
111
|
+
} else {
|
|
112
|
+
mode = { type: "message" };
|
|
113
|
+
}
|
|
107
114
|
|
|
108
115
|
// if we get a message, emit the content and continue;
|
|
109
|
-
if (mode === "message") {
|
|
116
|
+
if (mode.type === "message") {
|
|
110
117
|
// if we got a result message, send a result event
|
|
111
118
|
if (value.choices[0].delta.role === "function") {
|
|
112
119
|
controller.enqueue({
|
|
@@ -125,16 +132,22 @@ export function decodeChatCompletion(
|
|
|
125
132
|
continue;
|
|
126
133
|
}
|
|
127
134
|
// if we get a function call, buffer the name and arguments, then emit a partial event.
|
|
128
|
-
else if (mode === "function") {
|
|
129
|
-
|
|
130
|
-
|
|
135
|
+
else if (mode.type === "function") {
|
|
136
|
+
const maybeFunctionCallName = mode.function.name;
|
|
137
|
+
if (maybeFunctionCallName) {
|
|
138
|
+
functionCallName = maybeFunctionCallName;
|
|
131
139
|
}
|
|
132
|
-
|
|
133
|
-
|
|
140
|
+
|
|
141
|
+
const maybeFunctionCallArguments = mode.function.arguments;
|
|
142
|
+
if (maybeFunctionCallArguments) {
|
|
143
|
+
functionCallArguments += maybeFunctionCallArguments;
|
|
134
144
|
}
|
|
135
|
-
|
|
136
|
-
|
|
145
|
+
|
|
146
|
+
const maybeFunctionCallScope = mode.function.scope;
|
|
147
|
+
if (maybeFunctionCallScope) {
|
|
148
|
+
functionCallScope = maybeFunctionCallScope;
|
|
137
149
|
}
|
|
150
|
+
|
|
138
151
|
controller.enqueue({
|
|
139
152
|
type: "partial",
|
|
140
153
|
name: functionCallName,
|
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
import { Role } from "../types/openai-assistant";
|
|
2
2
|
|
|
3
|
+
export interface ToolCallFunctionCall {
|
|
4
|
+
arguments?: string;
|
|
5
|
+
|
|
6
|
+
name?: string;
|
|
7
|
+
// TODO:
|
|
8
|
+
// Temporarily add scope to the OpenAI protocol until we
|
|
9
|
+
// have our own protocol.
|
|
10
|
+
// When scope is "server", the client will not attempt to
|
|
11
|
+
// execute the function.
|
|
12
|
+
scope?: "client" | "server";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface ToolCallPayload {
|
|
16
|
+
index: number;
|
|
17
|
+
id?: string;
|
|
18
|
+
function: ToolCallFunctionCall;
|
|
19
|
+
}
|
|
20
|
+
|
|
3
21
|
export interface ChatCompletionChunk {
|
|
4
22
|
choices: {
|
|
5
23
|
delta: {
|
|
@@ -18,21 +36,7 @@ export interface ChatCompletionChunk {
|
|
|
18
36
|
name?: string;
|
|
19
37
|
arguments?: string;
|
|
20
38
|
};
|
|
21
|
-
tool_calls?:
|
|
22
|
-
index: number;
|
|
23
|
-
id?: string;
|
|
24
|
-
function: {
|
|
25
|
-
arguments?: string;
|
|
26
|
-
|
|
27
|
-
name?: string;
|
|
28
|
-
// TODO:
|
|
29
|
-
// Temporarily add scope to the OpenAI protocol until we
|
|
30
|
-
// have our own protocol.
|
|
31
|
-
// When scope is "server", the client will not attempt to
|
|
32
|
-
// execute the function.
|
|
33
|
-
scope?: "client" | "server";
|
|
34
|
-
};
|
|
35
|
-
}[];
|
|
39
|
+
tool_calls?: ToolCallPayload[];
|
|
36
40
|
};
|
|
37
41
|
}[];
|
|
38
42
|
}
|