@copilotkit/shared 0.4.0-tools.0 → 0.4.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.
Files changed (75) hide show
  1. package/.turbo/turbo-build.log +100 -86
  2. package/CHANGELOG.md +14 -0
  3. package/dist/chunk-2M3YVRFS.mjs +1 -0
  4. package/dist/chunk-2M3YVRFS.mjs.map +1 -0
  5. package/dist/chunk-3ZU7SB62.mjs +7 -0
  6. package/dist/chunk-3ZU7SB62.mjs.map +1 -0
  7. package/dist/{chunk-FHLEKC6N.mjs → chunk-CPRMXQXK.mjs} +14 -3
  8. package/dist/chunk-CPRMXQXK.mjs.map +1 -0
  9. package/dist/{chunk-HT5RV27L.mjs → chunk-T6YVHLB7.mjs} +1 -1
  10. package/dist/chunk-T6YVHLB7.mjs.map +1 -0
  11. package/dist/{chunk-NCTOECKL.mjs → chunk-XJLZZQTA.mjs} +11 -1
  12. package/dist/chunk-XJLZZQTA.mjs.map +1 -0
  13. package/dist/constants/copilot-protocol.d.ts +3 -0
  14. package/dist/constants/copilot-protocol.js +31 -0
  15. package/dist/constants/copilot-protocol.js.map +1 -0
  16. package/dist/constants/copilot-protocol.mjs +7 -0
  17. package/dist/constants/copilot-protocol.mjs.map +1 -0
  18. package/dist/constants/index.d.ts +1 -0
  19. package/dist/constants/index.js +33 -0
  20. package/dist/constants/index.js.map +1 -0
  21. package/dist/constants/index.mjs +8 -0
  22. package/dist/constants/index.mjs.map +1 -0
  23. package/dist/index.d.ts +3 -2
  24. package/dist/index.js +29 -2
  25. package/dist/index.js.map +1 -1
  26. package/dist/index.mjs +32 -333
  27. package/dist/index.mjs.map +1 -1
  28. package/dist/types/annotated-function.d.ts +1 -1
  29. package/dist/types/annotated-function.js.map +1 -1
  30. package/dist/types/annotated-function.mjs +1 -0
  31. package/dist/types/index.mjs +3 -0
  32. package/dist/types/openai-assistant.d.ts +5 -1
  33. package/dist/types/openai-assistant.js.map +1 -1
  34. package/dist/types/openai-assistant.mjs +1 -0
  35. package/dist/utils/annotated-function.mjs +3 -27
  36. package/dist/utils/annotated-function.mjs.map +1 -1
  37. package/dist/utils/decode-chat-completion-as-text.mjs +3 -27
  38. package/dist/utils/decode-chat-completion-as-text.mjs.map +1 -1
  39. package/dist/utils/decode-chat-completion.d.ts +8 -2
  40. package/dist/utils/decode-chat-completion.js +13 -2
  41. package/dist/utils/decode-chat-completion.js.map +1 -1
  42. package/dist/utils/decode-chat-completion.mjs +3 -92
  43. package/dist/utils/decode-chat-completion.mjs.map +1 -1
  44. package/dist/utils/index.d.ts +2 -2
  45. package/dist/utils/index.js +24 -2
  46. package/dist/utils/index.js.map +1 -1
  47. package/dist/utils/index.mjs +24 -333
  48. package/dist/utils/index.mjs.map +1 -1
  49. package/dist/utils/parse-chat-completion.d.ts +2 -0
  50. package/dist/utils/parse-chat-completion.js.map +1 -1
  51. package/dist/utils/parse-chat-completion.mjs +3 -55
  52. package/dist/utils/parse-chat-completion.mjs.map +1 -1
  53. package/dist/utils/utils.d.ts +2 -1
  54. package/dist/utils/utils.js +11 -0
  55. package/dist/utils/utils.js.map +1 -1
  56. package/dist/utils/utils.mjs +11 -128
  57. package/dist/utils/utils.mjs.map +1 -1
  58. package/package.json +3 -3
  59. package/src/constants/copilot-protocol.ts +1 -0
  60. package/src/constants/index.ts +1 -0
  61. package/src/index.ts +1 -0
  62. package/src/types/annotated-function.ts +1 -1
  63. package/src/types/openai-assistant.ts +6 -1
  64. package/src/utils/decode-chat-completion.ts +26 -2
  65. package/src/utils/parse-chat-completion.ts +15 -0
  66. package/src/utils/utils.ts +10 -0
  67. package/dist/chunk-FHLEKC6N.mjs.map +0 -1
  68. package/dist/chunk-GFCJUF6J.mjs +0 -96
  69. package/dist/chunk-GFCJUF6J.mjs.map +0 -1
  70. package/dist/chunk-HT5RV27L.mjs.map +0 -1
  71. package/dist/chunk-L46AW3ET.mjs +0 -29
  72. package/dist/chunk-L46AW3ET.mjs.map +0 -1
  73. package/dist/chunk-NCTOECKL.mjs.map +0 -1
  74. package/dist/chunk-W6NBBCWB.mjs +0 -60
  75. package/dist/chunk-W6NBBCWB.mjs.map +0 -1
@@ -1,134 +1,17 @@
1
- // src/utils/utils.ts
2
- var textStreamPart = {
3
- code: "0",
4
- name: "text",
5
- parse: (value) => {
6
- if (typeof value !== "string") {
7
- throw new Error('"text" parts expect a string value.');
8
- }
9
- return { type: "text", value };
10
- }
11
- };
12
- var functionCallStreamPart = {
13
- code: "1",
14
- name: "function_call",
15
- parse: (value) => {
16
- 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") {
17
- throw new Error('"function_call" parts expect an object with a "function_call" property.');
18
- }
19
- return {
20
- type: "function_call",
21
- value
22
- };
23
- }
24
- };
25
- var dataStreamPart = {
26
- code: "2",
27
- name: "data",
28
- parse: (value) => {
29
- if (!Array.isArray(value)) {
30
- throw new Error('"data" parts expect an array value.');
31
- }
32
- return { type: "data", value };
33
- }
34
- };
35
- var errorStreamPart = {
36
- code: "3",
37
- name: "error",
38
- parse: (value) => {
39
- if (typeof value !== "string") {
40
- throw new Error('"error" parts expect a string value.');
41
- }
42
- return { type: "error", value };
43
- }
44
- };
45
- var assistantMessage = {
46
- code: "4",
47
- name: "assistant_message",
48
- parse: (value) => {
49
- 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(
50
- (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"
51
- )) {
52
- throw new Error(
53
- '"assistant_message" parts expect an object with an "id", "role", and "content" property.'
54
- );
55
- }
56
- return {
57
- type: "assistant_message",
58
- value
59
- };
60
- }
61
- };
62
- var assistantControlData = {
63
- code: "5",
64
- name: "assistant_control_data",
65
- parse: (value) => {
66
- if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
67
- throw new Error(
68
- '"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
69
- );
70
- }
71
- return {
72
- type: "assistant_control_data",
73
- value: {
74
- threadId: value.threadId,
75
- messageId: value.messageId
76
- }
77
- };
78
- }
79
- };
80
- var streamParts = [
81
- textStreamPart,
82
- functionCallStreamPart,
83
- dataStreamPart,
84
- errorStreamPart,
85
- assistantMessage,
86
- assistantControlData
87
- ];
88
- var streamPartsByCode = {
89
- [textStreamPart.code]: textStreamPart,
90
- [functionCallStreamPart.code]: functionCallStreamPart,
91
- [dataStreamPart.code]: dataStreamPart,
92
- [errorStreamPart.code]: errorStreamPart,
93
- [assistantMessage.code]: assistantMessage,
94
- [assistantControlData.code]: assistantControlData
95
- };
96
- var StreamStringPrefixes = {
97
- [textStreamPart.name]: textStreamPart.code,
98
- [functionCallStreamPart.name]: functionCallStreamPart.code,
99
- [dataStreamPart.name]: dataStreamPart.code,
100
- [errorStreamPart.name]: errorStreamPart.code,
101
- [assistantMessage.name]: assistantMessage.code,
102
- [assistantControlData.name]: assistantControlData.code
103
- };
104
- var validCodes = streamParts.map((part) => part.code);
105
- var parseStreamPart = (line) => {
106
- const firstSeparatorIndex = line.indexOf(":");
107
- if (firstSeparatorIndex === -1) {
108
- throw new Error("Failed to parse stream string. No separator found.");
109
- }
110
- const prefix = line.slice(0, firstSeparatorIndex);
111
- if (!validCodes.includes(prefix)) {
112
- throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
113
- }
114
- const code = prefix;
115
- const textValue = line.slice(firstSeparatorIndex + 1);
116
- const jsonValue = JSON.parse(textValue);
117
- return streamPartsByCode[code].parse(jsonValue);
118
- };
119
- function formatStreamPart(type, value) {
120
- const streamPart = streamParts.find((part) => part.name === type);
121
- if (!streamPart) {
122
- throw new Error(`Invalid stream part type: ${type}`);
123
- }
124
- return `${streamPart.code}:${JSON.stringify(value)}
125
- `;
126
- }
127
- var isStreamStringEqualToType = (type, value) => value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith("\n");
128
- var COMPLEX_HEADER = "X-Experimental-Stream-Data";
1
+ import {
2
+ COMPLEX_HEADER,
3
+ StreamStringPrefixes,
4
+ encodeResult,
5
+ formatStreamPart,
6
+ isStreamStringEqualToType,
7
+ parseStreamPart,
8
+ streamPartsByCode,
9
+ validCodes
10
+ } from "../chunk-XJLZZQTA.mjs";
129
11
  export {
130
12
  COMPLEX_HEADER,
131
13
  StreamStringPrefixes,
14
+ encodeResult,
132
15
  formatStreamPart,
133
16
  isStreamStringEqualToType,
134
17
  parseStreamPart,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/utils/utils.ts"],"sourcesContent":["import { AssistantMessage, FunctionCall, JSONValue } from \"../types/openai-assistant\";\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":";AAQA,IAAM,iBAAkD;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Ae;AAAA,EACtC,CAAC,uBAAuB,IAAI,GAAG,uBAAuB;AAAA,EACtD,CAAC,eAAe,IAAI,GAAG,eAAe;AAAA,EACtC,CAAC,gBAAgB,IAAI,GAAG,gBAAgB;AAAA,EACxC,CAAC,iBAAiB,IAAI,GAAG,iBAAiB;AAAA,EAC1C,CAAC,qBAAqB,IAAI,GAAG,qBAAqB;AACpD;AAEO,IAAM,aAAa,YAAY,IAAI,CAAC,SAAS,KAAK,IAAI;AAStD,IAAM,kBAAkB,CAAC,SAAiC;AAC/D,QAAM,sBAAsB,KAAK,QAAQ,GAAG;AAE5C,MAAI,wBAAwB,IAAI;AAC9B,UAAM,IAAI,MAAM,oDAAoD;AAAA,EACtE;AAEA,QAAM,SAAS,KAAK,MAAM,GAAG,mBAAmB;AAEhD,MAAI,CAAC,WAAW,SAAS,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.4.0-tools.0",
7
+ "version": "0.4.0",
8
8
  "sideEffects": false,
9
9
  "main": "./dist/index.js",
10
10
  "module": "./dist/index.mjs",
@@ -21,8 +21,8 @@
21
21
  "ts-jest": "^29.1.1",
22
22
  "tsup": "^6.7.0",
23
23
  "typescript": "^5.1.3",
24
- "eslint-config-custom": "0.5.0-tools.0",
25
- "tsconfig": "0.9.0-tools.0"
24
+ "eslint-config-custom": "0.5.0",
25
+ "tsconfig": "0.9.0"
26
26
  },
27
27
  "dependencies": {},
28
28
  "scripts": {
@@ -0,0 +1 @@
1
+ export const EXCLUDE_FROM_FORWARD_PROPS_KEYS = "exclude_from_forward_props_keys";
@@ -0,0 +1 @@
1
+ export * from "./copilot-protocol";
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from "./types";
2
2
  export * from "./utils";
3
+ export * from "./constants";
@@ -23,5 +23,5 @@ export interface AnnotatedFunction<Inputs extends any[]> {
23
23
  name: string;
24
24
  description: string;
25
25
  argumentAnnotations: AnnotatedFunctionArgument[];
26
- implementation: (...args: Inputs) => Promise<void>;
26
+ implementation: (...args: Inputs) => Promise<any>;
27
27
  }
@@ -13,6 +13,11 @@ export interface FunctionCall {
13
13
  * The name of the function to call.
14
14
  */
15
15
  name?: string;
16
+
17
+ /**
18
+ * Temporarily add scope to the function call.
19
+ */
20
+ scope?: "client" | "server";
16
21
  }
17
22
 
18
23
  /**
@@ -68,7 +73,7 @@ export interface ToolDefinition {
68
73
  export type FunctionCallHandler = (
69
74
  chatMessages: Message[],
70
75
  functionCall: FunctionCall,
71
- ) => Promise<void>;
76
+ ) => Promise<any>;
72
77
 
73
78
  export type AssistantMessage = {
74
79
  id: string;
@@ -15,12 +15,20 @@ export interface ChatCompletionFunctionEvent {
15
15
  type: "function";
16
16
  name: string;
17
17
  arguments: any;
18
+ scope: "client" | "server";
19
+ }
20
+
21
+ export interface ChatCompletionResultEvent {
22
+ type: "result";
23
+ content: string;
24
+ name: string;
18
25
  }
19
26
 
20
27
  export type ChatCompletionEvent =
21
28
  | ChatCompletionContentEvent
22
29
  | ChatCompletionPartialEvent
23
- | ChatCompletionFunctionEvent;
30
+ | ChatCompletionFunctionEvent
31
+ | ChatCompletionResultEvent;
24
32
 
25
33
  export function decodeChatCompletion(
26
34
  stream: ReadableStream<ChatCompletionChunk>,
@@ -30,6 +38,7 @@ export function decodeChatCompletion(
30
38
  let mode: "function" | "message" | null = null;
31
39
  let functionCallName: string = "";
32
40
  let functionCallArguments: string = "";
41
+ let functionCallScope: "client" | "server" = "client";
33
42
 
34
43
  async function cleanup(controller?: ReadableStreamDefaultController<any>) {
35
44
  if (controller) {
@@ -59,6 +68,7 @@ export function decodeChatCompletion(
59
68
  type: "function",
60
69
  name: functionCallName,
61
70
  arguments: args,
71
+ scope: functionCallScope,
62
72
  });
63
73
 
64
74
  mode = null;
@@ -79,6 +89,8 @@ export function decodeChatCompletion(
79
89
  return;
80
90
  }
81
91
 
92
+ // TODO what can we get here now, since we can also get results back
93
+
82
94
  // In case we are in a function call but the next message is
83
95
  // - not a function call
84
96
  // - another function call (when name is present)
@@ -97,7 +109,16 @@ export function decodeChatCompletion(
97
109
 
98
110
  // if we get a message, emit the content and continue;
99
111
  if (mode === "message") {
100
- if (value.choices[0].delta.content) {
112
+ // if we got a result message, send a result event
113
+ if (value.choices[0].delta.role === "function") {
114
+ controller.enqueue({
115
+ type: "result",
116
+ content: value.choices[0].delta.content!,
117
+ name: value.choices[0].delta.name!,
118
+ });
119
+ }
120
+ // otherwise, send a content event
121
+ else if (value.choices[0].delta.content) {
101
122
  controller.enqueue({
102
123
  type: "content",
103
124
  content: value.choices[0].delta.content,
@@ -113,6 +134,9 @@ export function decodeChatCompletion(
113
134
  if (value.choices[0].delta.tool_calls![0].function.arguments) {
114
135
  functionCallArguments += value.choices[0].delta.tool_calls![0].function.arguments!;
115
136
  }
137
+ if (value.choices[0].delta.tool_calls![0].function.scope) {
138
+ functionCallScope = value.choices[0].delta.tool_calls![0].function.scope!;
139
+ }
116
140
  controller.enqueue({
117
141
  type: "partial",
118
142
  name: functionCallName,
@@ -5,6 +5,14 @@ export interface ChatCompletionChunk {
5
5
  delta: {
6
6
  role: Role;
7
7
  content?: string | null;
8
+
9
+ // TODO:
10
+ // Temporarily add name to the OpenAI protocol until we
11
+ // have our own protocol.
12
+ // When name is set, we return the result of a server-side
13
+ // function call.
14
+ name?: string;
15
+
8
16
  function_call?: {
9
17
  name?: string;
10
18
  arguments?: string;
@@ -14,7 +22,14 @@ export interface ChatCompletionChunk {
14
22
  id?: string;
15
23
  function: {
16
24
  arguments?: string;
25
+
17
26
  name?: string;
27
+ // TODO:
28
+ // Temporarily add scope to the OpenAI protocol until we
29
+ // have our own protocol.
30
+ // When scope is "server", the client will not attempt to
31
+ // execute the function.
32
+ scope?: "client" | "server";
18
33
  };
19
34
  }[];
20
35
  };
@@ -1,5 +1,15 @@
1
1
  import { AssistantMessage, FunctionCall, JSONValue } from "../types/openai-assistant";
2
2
 
3
+ export function encodeResult(result: string): string {
4
+ if (result === undefined) {
5
+ return "";
6
+ } else if (typeof result === "string") {
7
+ return result;
8
+ } else {
9
+ return JSON.stringify(result);
10
+ }
11
+ }
12
+
3
13
  export interface StreamPart<CODE extends string, NAME extends string, TYPE> {
4
14
  code: CODE;
5
15
  name: NAME;
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/utils/decode-chat-completion.ts"],"sourcesContent":["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}\n\nexport type ChatCompletionEvent =\n | ChatCompletionContentEvent\n | ChatCompletionPartialEvent\n | ChatCompletionFunctionEvent;\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\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 });\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 (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 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}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eAAvB,mBAAoC,OAApC,mBAAwC,YAAW,aAAa;AAGvE,cAAI,SAAS,WAAW;AACtB,gBAAI,MAAM,QAAQ,CAAC,EAAE,MAAM,SAAS;AAClC,yBAAW,QAAQ;AAAA,gBACjB,MAAM;AAAA,gBACN,SAAS,MAAM,QAAQ,CAAC,EAAE,MAAM;AAAA,cAClC,CAAC;AAAA,YACH;AACA;AAAA,UACF,WAES,SAAS,YAAY;AAC5B,gBAAI,MAAM,QAAQ,CAAC,EAAE,MAAM,WAAY,CAAC,EAAE,SAAS,MAAM;AACvD,iCAAmB,MAAM,QAAQ,CAAC,EAAE,MAAM,WAAY,CAAC,EAAE,SAAS;AAAA,YACpE;AACA,gBAAI,MAAM,QAAQ,CAAC,EAAE,MAAM,WAAY,CAAC,EAAE,SAAS,WAAW;AAC5D,uCAAyB,MAAM,QAAQ,CAAC,EAAE,MAAM,WAAY,CAAC,EAAE,SAAS;AAAA,YAC1E;AACA,uBAAW,QAAQ;AAAA,cACjB,MAAM;AAAA,cACN,MAAM;AAAA,cACN,WAAW;AAAA,YACb,CAAC;AACD;AAAA,UACF;AAAA,QACF,SAAS,OAAP;AACA,qBAAW,MAAM,KAAK;AACtB,gBAAM,QAAQ,UAAU;AACxB;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IACA,SAAS;AACP,aAAO,OAAO;AAAA,IAChB;AAAA,EACF,CAAC;AACH;","names":[]}
@@ -1,96 +0,0 @@
1
- // src/utils/decode-chat-completion.ts
2
- function decodeChatCompletion(stream) {
3
- const reader = stream.getReader();
4
- let mode = null;
5
- let functionCallName = "";
6
- let functionCallArguments = "";
7
- async function cleanup(controller) {
8
- if (controller) {
9
- try {
10
- controller.close();
11
- } catch (_) {
12
- }
13
- }
14
- if (reader) {
15
- try {
16
- await reader.cancel();
17
- } catch (_) {
18
- }
19
- }
20
- }
21
- return new ReadableStream({
22
- async pull(controller) {
23
- const flushFunctionCall = () => {
24
- let args = null;
25
- try {
26
- args = JSON.parse(functionCallArguments);
27
- } catch (error) {
28
- cleanup(controller);
29
- controller.error(error);
30
- return false;
31
- }
32
- controller.enqueue({
33
- type: "function",
34
- name: functionCallName,
35
- arguments: args
36
- });
37
- mode = null;
38
- functionCallName = "";
39
- functionCallArguments = "";
40
- return true;
41
- };
42
- while (true) {
43
- try {
44
- const { done, value } = await reader.read();
45
- if (done) {
46
- if (mode === "function") {
47
- flushFunctionCall();
48
- }
49
- await cleanup(controller);
50
- return;
51
- }
52
- if (mode === "function" && !value.choices[0].delta.function_call) {
53
- if (!flushFunctionCall()) {
54
- return;
55
- }
56
- }
57
- mode = value.choices[0].delta.function_call ? "function" : "message";
58
- if (mode === "message") {
59
- if (value.choices[0].delta.content) {
60
- controller.enqueue({
61
- type: "content",
62
- content: value.choices[0].delta.content
63
- });
64
- }
65
- continue;
66
- } else if (mode === "function") {
67
- if (value.choices[0].delta.function_call.name) {
68
- functionCallName = value.choices[0].delta.function_call.name;
69
- }
70
- if (value.choices[0].delta.function_call.arguments) {
71
- functionCallArguments += value.choices[0].delta.function_call.arguments;
72
- }
73
- controller.enqueue({
74
- type: "partial",
75
- name: functionCallName,
76
- arguments: functionCallArguments
77
- });
78
- continue;
79
- }
80
- } catch (error) {
81
- controller.error(error);
82
- await cleanup(controller);
83
- return;
84
- }
85
- }
86
- },
87
- cancel() {
88
- reader.cancel();
89
- }
90
- });
91
- }
92
-
93
- export {
94
- decodeChatCompletion
95
- };
96
- //# sourceMappingURL=chunk-GFCJUF6J.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/utils/decode-chat-completion.ts"],"sourcesContent":["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}\n\nexport type ChatCompletionEvent =\n | ChatCompletionContentEvent\n | ChatCompletionPartialEvent\n | ChatCompletionFunctionEvent;\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\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 });\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 not a function call, flush it.\n if (mode === \"function\" && !value.choices[0].delta.function_call) {\n if (!flushFunctionCall()) {\n return;\n }\n }\n\n mode = value.choices[0].delta.function_call ? \"function\" : \"message\";\n\n // if we get a message, emit the content and continue;\n if (mode === \"message\") {\n 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.function_call!.name) {\n functionCallName = value.choices[0].delta.function_call!.name!;\n }\n if (value.choices[0].delta.function_call!.arguments) {\n functionCallArguments += value.choices[0].delta.function_call!.arguments!;\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.c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AAW,QAAQ;AAAA,gBACjB,MAAM;AAAA,gBACN,SAAS,MAAM,QAAQ,CAAC,EAAE,MAAM;AAAA,cAClC,CAAC;AAAA,YACH;AACA;AAAA,UACF,WAES,SAAS,YAAY;AAC5B,gBAAI,MAAM,QAAQ,CAAC,EAAE,MAAM,cAAe,MAAM;AAC9C,iCAAmB,MAAM,QAAQ,CAAC,EAAE,MAAM,cAAe;AAAA,YAC3D;AACA,gBAAI,MAAM,QAAQ,CAAC,EAAE,MAAM,cAAe,WAAW;AACnD,uCAAyB,MAAM,QAAQ,CAAC,EAAE,MAAM,cAAe;AAAA,YACjE;AACA,uBAAW,QAAQ;AAAA,cACjB,MAAM;AAAA,cACN,MAAM;AAAA,cACN,WAAW;AAAA,YACb,CAAC;AACD;AAAA,UACF;AAAA,QACF,SAAS,OAAP;AACA,qBAAW,MAAM,KAAK;AACtB,gBAAM,QAAQ,UAAU;AACxB;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IACA,SAAS;AACP,aAAO,OAAO;AAAA,IAChB;AAAA,EACF,CAAC;AACH;","names":[]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/utils/parse-chat-completion.ts"],"sourcesContent":["import { Role } from \"../types/openai-assistant\";\n\nexport interface ChatCompletionChunk {\n choices: {\n delta: {\n role: Role;\n content?: string | null;\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 name?: string;\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"],"mappings":";AA0BO,SAAS,oBACd,QACqC;AACrC,QAAM,SAAS,OAAO,UAAU;AAChC,MAAI,SAAS,IAAI,WAAW;AAE5B,iBAAe,QAAQ,YAAmD;AACxE,QAAI,YAAY;AACd,UAAI;AACF,mBAAW,MAAM;AAAA,MACnB,SAAS,GAAP;AAAA,MAAW;AAAA,IACf;AACA,QAAI,QAAQ;AACV,UAAI;AACF,cAAM,OAAO,OAAO;AAAA,MACtB,SAAS,GAAP;AAAA,MAAW;AAAA,IACf;AAAA,EACF;AAEA,SAAO,IAAI,eAAoC;AAAA,IAC7C,MAAM,KAAK,YAAY;AACrB,aAAO,MAAM;AACX,YAAI;AACF,gBAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAE1C,cAAI,MAAM;AACR,kBAAM,QAAQ,UAAU;AACxB;AAAA,UACF;AAEA,gBAAM,YAAY,IAAI,WAAW,OAAO,SAAS,MAAM,MAAM;AAC7D,oBAAU,IAAI,MAAM;AACpB,oBAAU,IAAI,OAAO,OAAO,MAAM;AAClC,mBAAS;AAET,gBAAM,cAAc,IAAI,YAAY,OAAO,EAAE,OAAO,MAAM;AAC1D,gBAAM,QAAQ,YAAY,MAAM,IAAI,EAAE,OAAO,CAAC,SAAS,KAAK,KAAK,MAAM,EAAE;AAGzE,mBAAS,CAAC,YAAY,SAAS,IAAI,IAC/B,IAAI,YAAY,EAAE,OAAO,MAAM,IAAI,KAAK,EAAE,IAC1C,IAAI,WAAW;AAEnB,qBAAW,QAAQ,OAAO;AACxB,kBAAM,cAAc,KAAK,QAAQ,WAAW,EAAE;AAE9C,gBAAI,gBAAgB,UAAU;AAC5B,oBAAM,QAAQ,UAAU;AACxB;AAAA,YACF;AAEA,kBAAM,OAAO,KAAK,MAAM,WAAW;AACnC,uBAAW,QAAQ,IAAI;AAAA,UACzB;AAAA,QACF,SAAS,OAAP;AACA,qBAAW,MAAM,KAAK;AACtB,gBAAM,QAAQ,UAAU;AACxB;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IACA,SAAS;AACP,aAAO,OAAO;AAAA,IAChB;AAAA,EACF,CAAC;AACH;","names":[]}
@@ -1,29 +0,0 @@
1
- // src/utils/annotated-function.ts
2
- function annotatedFunctionToChatCompletionFunction(annotatedFunction) {
3
- let parameters = {};
4
- for (let arg of annotatedFunction.argumentAnnotations) {
5
- let { name, required, ...forwardedArgs } = arg;
6
- parameters[arg.name] = forwardedArgs;
7
- }
8
- let requiredParameterNames = [];
9
- for (let arg of annotatedFunction.argumentAnnotations) {
10
- if (arg.required) {
11
- requiredParameterNames.push(arg.name);
12
- }
13
- }
14
- let chatCompletionFunction = {
15
- name: annotatedFunction.name,
16
- description: annotatedFunction.description,
17
- parameters: {
18
- type: "object",
19
- properties: parameters,
20
- required: requiredParameterNames
21
- }
22
- };
23
- return chatCompletionFunction;
24
- }
25
-
26
- export {
27
- annotatedFunctionToChatCompletionFunction
28
- };
29
- //# sourceMappingURL=chunk-L46AW3ET.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/utils/annotated-function.ts"],"sourcesContent":["import { AnnotatedFunction, Function } from \"../types\";\n\nexport function annotatedFunctionToChatCompletionFunction(\n annotatedFunction: AnnotatedFunction<any[]>,\n): Function {\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: Function = {\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n };\n\n return chatCompletionFunction;\n}\n"],"mappings":";AAEO,SAAS,0CACd,mBACU;AAEV,MAAI,aAAqC,CAAC;AAC1C,WAAS,OAAO,kBAAkB,qBAAqB;AAErD,QAAI,EAAE,MAAM,UAAU,GAAG,cAAc,IAAI;AAC3C,eAAW,IAAI,IAAI,IAAI;AAAA,EACzB;AAEA,MAAI,yBAAmC,CAAC;AACxC,WAAS,OAAO,kBAAkB,qBAAqB;AACrD,QAAI,IAAI,UAAU;AAChB,6BAAuB,KAAK,IAAI,IAAI;AAAA,IACtC;AAAA,EACF;AAGA,MAAI,yBAAmC;AAAA,IACrC,MAAM,kBAAkB;AAAA,IACxB,aAAa,kBAAkB;AAAA,IAC/B,YAAY;AAAA,MACV,MAAM;AAAA,MACN,YAAY;AAAA,MACZ,UAAU;AAAA,IACZ;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/utils/utils.ts"],"sourcesContent":["import { AssistantMessage, FunctionCall, JSONValue } from \"../types/openai-assistant\";\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":";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,MAAM,yEAAyE;AAAA,IAC3F;AAEA,WAAO;AAAA,MACL,MAAM;AAAA,MACN;AAAA,IACF;AAAA,EACF;AACF;AAEA,IAAM,iBAA4D;AAAA,EAChE,MAAM;AAAA,EAC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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,60 +0,0 @@
1
- // src/utils/parse-chat-completion.ts
2
- function parseChatCompletion(stream) {
3
- const reader = stream.getReader();
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
- }
56
-
57
- export {
58
- parseChatCompletion
59
- };
60
- //# sourceMappingURL=chunk-W6NBBCWB.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/utils/parse-chat-completion.ts"],"sourcesContent":["import { Role } from \"../types/openai-assistant\";\n\nexport interface ChatCompletionChunk {\n choices: {\n delta: {\n role: Role;\n content?: string | null;\n function_call?: {\n name?: string;\n arguments?: string;\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"],"mappings":";AAkBO,SAAS,oBACd,QACqC;AACrC,QAAM,SAAS,OAA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