@copilotkit/shared 0.3.0-alpha.1 → 0.4.0-tools.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 (45) hide show
  1. package/.turbo/turbo-build.log +55 -55
  2. package/CHANGELOG.md +14 -0
  3. package/dist/chunk-FHLEKC6N.mjs +97 -0
  4. package/dist/chunk-FHLEKC6N.mjs.map +1 -0
  5. package/dist/chunk-HT5RV27L.mjs +60 -0
  6. package/dist/chunk-HT5RV27L.mjs.map +1 -0
  7. package/dist/chunk-NSY3OEEP.mjs +32 -0
  8. package/dist/chunk-NSY3OEEP.mjs.map +1 -0
  9. package/dist/index.d.ts +1 -1
  10. package/dist/index.js +16 -12
  11. package/dist/index.js.map +1 -1
  12. package/dist/index.mjs +333 -25
  13. package/dist/index.mjs.map +1 -1
  14. package/dist/types/annotated-function.mjs +0 -1
  15. package/dist/types/index.d.ts +1 -1
  16. package/dist/types/index.mjs +0 -3
  17. package/dist/types/openai-assistant.d.ts +6 -2
  18. package/dist/types/openai-assistant.js.map +1 -1
  19. package/dist/types/openai-assistant.mjs +0 -1
  20. package/dist/utils/annotated-function.d.ts +2 -2
  21. package/dist/utils/annotated-function.js +9 -6
  22. package/dist/utils/annotated-function.js.map +1 -1
  23. package/dist/utils/annotated-function.mjs +27 -3
  24. package/dist/utils/annotated-function.mjs.map +1 -1
  25. package/dist/utils/decode-chat-completion-as-text.mjs +27 -3
  26. package/dist/utils/decode-chat-completion-as-text.mjs.map +1 -1
  27. package/dist/utils/decode-chat-completion.js +7 -6
  28. package/dist/utils/decode-chat-completion.js.map +1 -1
  29. package/dist/utils/decode-chat-completion.mjs +92 -3
  30. package/dist/utils/decode-chat-completion.mjs.map +1 -1
  31. package/dist/utils/index.js +16 -12
  32. package/dist/utils/index.js.map +1 -1
  33. package/dist/utils/index.mjs +333 -22
  34. package/dist/utils/index.mjs.map +1 -1
  35. package/dist/utils/parse-chat-completion.d.ts +8 -0
  36. package/dist/utils/parse-chat-completion.js.map +1 -1
  37. package/dist/utils/parse-chat-completion.mjs +55 -3
  38. package/dist/utils/parse-chat-completion.mjs.map +1 -1
  39. package/dist/utils/utils.mjs +128 -9
  40. package/dist/utils/utils.mjs.map +1 -1
  41. package/package.json +3 -3
  42. package/src/types/openai-assistant.ts +6 -1
  43. package/src/utils/annotated-function.ts +12 -9
  44. package/src/utils/decode-chat-completion.ts +14 -7
  45. package/src/utils/parse-chat-completion.ts +8 -0
@@ -1 +1 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
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,MA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,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":[]}
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.3.0-alpha.1",
7
+ "version": "0.4.0-tools.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.4.0-alpha.1",
25
- "tsconfig": "0.8.0-alpha.1"
24
+ "eslint-config-custom": "0.5.0-tools.0",
25
+ "tsconfig": "0.9.0-tools.0"
26
26
  },
27
27
  "dependencies": {},
28
28
  "scripts": {
@@ -37,7 +37,7 @@ export interface Message {
37
37
  function_call?: string | FunctionCall;
38
38
  }
39
39
 
40
- export interface Function {
40
+ export interface FunctionDefinition {
41
41
  /**
42
42
  * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain
43
43
  * underscores and dashes, with a maximum length of 64.
@@ -60,6 +60,11 @@ export interface Function {
60
60
  description?: string;
61
61
  }
62
62
 
63
+ export interface ToolDefinition {
64
+ type: "function";
65
+ function: FunctionDefinition;
66
+ }
67
+
63
68
  export type FunctionCallHandler = (
64
69
  chatMessages: Message[],
65
70
  functionCall: FunctionCall,
@@ -1,8 +1,8 @@
1
- import { AnnotatedFunction, Function } from "../types";
1
+ import { AnnotatedFunction, FunctionDefinition, ToolDefinition } from "../types";
2
2
 
3
3
  export function annotatedFunctionToChatCompletionFunction(
4
4
  annotatedFunction: AnnotatedFunction<any[]>,
5
- ): Function {
5
+ ): ToolDefinition {
6
6
  // Create the parameters object based on the argumentAnnotations
7
7
  let parameters: { [key: string]: any } = {};
8
8
  for (let arg of annotatedFunction.argumentAnnotations) {
@@ -19,13 +19,16 @@ export function annotatedFunctionToChatCompletionFunction(
19
19
  }
20
20
 
21
21
  // Create the ChatCompletionFunctions object
22
- let chatCompletionFunction: Function = {
23
- name: annotatedFunction.name,
24
- description: annotatedFunction.description,
25
- parameters: {
26
- type: "object",
27
- properties: parameters,
28
- required: requiredParameterNames,
22
+ let chatCompletionFunction: ToolDefinition = {
23
+ type: "function",
24
+ function: {
25
+ name: annotatedFunction.name,
26
+ description: annotatedFunction.description,
27
+ parameters: {
28
+ type: "object",
29
+ properties: parameters,
30
+ required: requiredParameterNames,
31
+ },
29
32
  },
30
33
  };
31
34
 
@@ -79,14 +79,21 @@ export function decodeChatCompletion(
79
79
  return;
80
80
  }
81
81
 
82
- // In case we are in a function call but the next message is not a function call, flush it.
83
- if (mode === "function" && !value.choices[0].delta.function_call) {
82
+ // In case we are in a function call but the next message is
83
+ // - not a function call
84
+ // - another function call (when name is present)
85
+ // => flush it.
86
+ if (
87
+ mode === "function" &&
88
+ (!value.choices[0].delta.tool_calls?.[0]?.function ||
89
+ value.choices[0].delta.tool_calls?.[0]?.function.name)
90
+ ) {
84
91
  if (!flushFunctionCall()) {
85
92
  return;
86
93
  }
87
94
  }
88
95
 
89
- mode = value.choices[0].delta.function_call ? "function" : "message";
96
+ mode = value.choices[0].delta.tool_calls?.[0]?.function ? "function" : "message";
90
97
 
91
98
  // if we get a message, emit the content and continue;
92
99
  if (mode === "message") {
@@ -100,11 +107,11 @@ export function decodeChatCompletion(
100
107
  }
101
108
  // if we get a function call, buffer the name and arguments, then emit a partial event.
102
109
  else if (mode === "function") {
103
- if (value.choices[0].delta.function_call!.name) {
104
- functionCallName = value.choices[0].delta.function_call!.name!;
110
+ if (value.choices[0].delta.tool_calls![0].function.name) {
111
+ functionCallName = value.choices[0].delta.tool_calls![0].function.name!;
105
112
  }
106
- if (value.choices[0].delta.function_call!.arguments) {
107
- functionCallArguments += value.choices[0].delta.function_call!.arguments!;
113
+ if (value.choices[0].delta.tool_calls![0].function.arguments) {
114
+ functionCallArguments += value.choices[0].delta.tool_calls![0].function.arguments!;
108
115
  }
109
116
  controller.enqueue({
110
117
  type: "partial",
@@ -9,6 +9,14 @@ export interface ChatCompletionChunk {
9
9
  name?: string;
10
10
  arguments?: string;
11
11
  };
12
+ tool_calls?: {
13
+ index: number;
14
+ id?: string;
15
+ function: {
16
+ arguments?: string;
17
+ name?: string;
18
+ };
19
+ }[];
12
20
  };
13
21
  }[];
14
22
  }