@copilotkit/shared 0.4.1 → 0.5.0-atai-0301-OpenAIAdapaterGetter.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.
Files changed (43) hide show
  1. package/.turbo/turbo-build.log +52 -45
  2. package/CHANGELOG.md +25 -0
  3. package/dist/chunk-DE5K76I2.mjs +1 -0
  4. package/dist/chunk-GI5AWM7D.mjs +112 -0
  5. package/dist/chunk-GI5AWM7D.mjs.map +1 -0
  6. package/dist/chunk-NAFEBKSO.mjs +1 -0
  7. package/dist/chunk-NAFEBKSO.mjs.map +1 -0
  8. package/dist/{chunk-T6YVHLB7.mjs → chunk-TYZD5BQY.mjs} +1 -1
  9. package/dist/chunk-TYZD5BQY.mjs.map +1 -0
  10. package/dist/index.d.ts +2 -1
  11. package/dist/index.js +81 -0
  12. package/dist/index.js.map +1 -1
  13. package/dist/index.mjs +9 -6
  14. package/dist/types/action.d.ts +54 -0
  15. package/dist/types/action.js +19 -0
  16. package/dist/types/action.js.map +1 -0
  17. package/dist/types/action.mjs +2 -0
  18. package/dist/types/action.mjs.map +1 -0
  19. package/dist/types/index.d.ts +1 -0
  20. package/dist/types/index.js.map +1 -1
  21. package/dist/types/index.mjs +2 -1
  22. package/dist/utils/annotated-function.d.ts +3 -1
  23. package/dist/utils/annotated-function.js +81 -0
  24. package/dist/utils/annotated-function.js.map +1 -1
  25. package/dist/utils/annotated-function.mjs +3 -1
  26. package/dist/utils/index.d.ts +2 -1
  27. package/dist/utils/index.js +81 -0
  28. package/dist/utils/index.js.map +1 -1
  29. package/dist/utils/index.mjs +7 -5
  30. package/dist/utils/parse-chat-completion.d.ts +1 -0
  31. package/dist/utils/parse-chat-completion.js.map +1 -1
  32. package/dist/utils/parse-chat-completion.mjs +1 -1
  33. package/dist/utils/utils.d.ts +6 -6
  34. package/package.json +5 -5
  35. package/src/types/action.ts +71 -0
  36. package/src/types/index.ts +1 -0
  37. package/src/utils/annotated-function.ts +90 -1
  38. package/src/utils/parse-chat-completion.ts +1 -0
  39. package/dist/chunk-FD6FGKYY.mjs +0 -1
  40. package/dist/chunk-NSY3OEEP.mjs +0 -32
  41. package/dist/chunk-NSY3OEEP.mjs.map +0 -1
  42. package/dist/chunk-T6YVHLB7.mjs.map +0 -1
  43. /package/dist/{chunk-FD6FGKYY.mjs.map → chunk-DE5K76I2.mjs.map} +0 -0
@@ -36,14 +36,14 @@ type StreamPartValueType = {
36
36
  };
37
37
  type StreamPartType = ReturnType<typeof textStreamPart.parse> | ReturnType<typeof functionCallStreamPart.parse> | ReturnType<typeof dataStreamPart.parse> | ReturnType<typeof errorStreamPart.parse> | ReturnType<typeof assistantMessage.parse> | ReturnType<typeof assistantControlData.parse>;
38
38
  declare const streamPartsByCode: {
39
- readonly 0: StreamPart<"0", "text", string>;
40
- readonly 1: StreamPart<"1", "function_call", {
39
+ readonly "0": StreamPart<"0", "text", string>;
40
+ readonly "1": StreamPart<"1", "function_call", {
41
41
  function_call: FunctionCall;
42
42
  }>;
43
- readonly 2: StreamPart<"2", "data", JSONValue[]>;
44
- readonly 3: StreamPart<"3", "error", string>;
45
- readonly 4: StreamPart<"4", "assistant_message", AssistantMessage>;
46
- readonly 5: StreamPart<"5", "assistant_control_data", {
43
+ readonly "2": StreamPart<"2", "data", JSONValue[]>;
44
+ readonly "3": StreamPart<"3", "error", string>;
45
+ readonly "4": StreamPart<"4", "assistant_message", AssistantMessage>;
46
+ readonly "5": StreamPart<"5", "assistant_control_data", {
47
47
  threadId: string;
48
48
  messageId: string;
49
49
  }>;
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.4.1",
7
+ "version": "0.5.0-atai-0301-OpenAIAdapaterGetter.3",
8
8
  "sideEffects": false,
9
9
  "main": "./dist/index.js",
10
10
  "module": "./dist/index.mjs",
@@ -16,13 +16,13 @@
16
16
  "peerDependencies": {},
17
17
  "devDependencies": {
18
18
  "@types/jest": "^29.5.4",
19
- "eslint": "^7.32.0",
19
+ "eslint": "^8.56.0",
20
20
  "jest": "^29.6.4",
21
21
  "ts-jest": "^29.1.1",
22
22
  "tsup": "^6.7.0",
23
- "typescript": "^5.1.3",
24
- "eslint-config-custom": "0.5.1",
25
- "tsconfig": "0.9.1"
23
+ "typescript": "^5.2.3",
24
+ "eslint-config-custom": "0.6.0-atai-0301-OpenAIAdapaterGetter.3",
25
+ "tsconfig": "0.10.0-atai-0301-OpenAIAdapaterGetter.3"
26
26
  },
27
27
  "dependencies": {},
28
28
  "scripts": {
@@ -0,0 +1,71 @@
1
+ type TypeMap = {
2
+ string: string;
3
+ number: number;
4
+ boolean: boolean;
5
+ object: object;
6
+ "string[]": string[];
7
+ "number[]": number[];
8
+ "boolean[]": boolean[];
9
+ "object[]": object[];
10
+ };
11
+
12
+ export type BaseParameter = {
13
+ name: string;
14
+ type?: Exclude<keyof TypeMap, "string" | "object" | "object[]">; // Exclude object types for BaseParameter
15
+ description?: string;
16
+ required?: boolean;
17
+ };
18
+
19
+ export type StringParameter = {
20
+ name: string;
21
+ type: "string";
22
+ description?: string;
23
+ required?: boolean;
24
+ enum?: string[];
25
+ };
26
+
27
+ export type ObjectParameter = {
28
+ name: string;
29
+ type: "object" | "object[]";
30
+ description?: string;
31
+ required?: boolean;
32
+ attributes?: Parameter[];
33
+ };
34
+
35
+ export type Parameter = BaseParameter | StringParameter | ObjectParameter;
36
+
37
+ type MappedParameterTypes<T extends Parameter[]> = {
38
+ // Check if the parameter has an 'enum' defined
39
+ [P in T[number] as P["name"]]: P extends { enum: Array<infer E> }
40
+ ? E extends string // Ensure the enum values are strings
41
+ ? P["required"] extends false // Check if the parameter is optional
42
+ ? E | undefined // If so, include 'undefined' in the type
43
+ : E // Otherwise, use the enum type directly
44
+ : never // This case should not occur since 'enum' implies string values
45
+ : // Handle parameters defined as 'object' with specified attributes
46
+ P extends { type: "object"; attributes: infer Attributes }
47
+ ? Attributes extends Parameter[]
48
+ ? MappedParameterTypes<Attributes> // Recursively map the attributes of the object
49
+ : never // If 'attributes' is not an array of Parameters, this is invalid
50
+ : // Handle parameters defined as 'object[]' without specified attributes
51
+ P extends { type: "object[]"; attributes?: never }
52
+ ? any[] // Default to 'any[]' for arrays of objects without specific attributes
53
+ : // Handle parameters defined as 'object[]' with specified attributes
54
+ P extends { type: "object[]"; attributes: infer Attributes }
55
+ ? Attributes extends Parameter[]
56
+ ? MappedParameterTypes<Attributes>[] // Recursively map each object in the array
57
+ : any[] // Default to 'any[]' if attributes are not properly defined
58
+ : // Handle all other parameter types
59
+ P["required"] extends false
60
+ ? // Include 'undefined' for optional parameters
61
+ TypeMap[P["type"] extends keyof TypeMap ? P["type"] : "string"] | undefined
62
+ : // Use the direct mapping from 'TypeMap' for the parameter's type
63
+ TypeMap[P["type"] extends keyof TypeMap ? P["type"] : "string"];
64
+ };
65
+
66
+ export type Action<T extends Parameter[] | [] = []> = {
67
+ name: string;
68
+ description?: string;
69
+ parameters?: T;
70
+ handler: T extends [] ? () => void : (args: MappedParameterTypes<T>) => any | Promise<any>;
71
+ };
@@ -1,2 +1,3 @@
1
1
  export * from "./openai-assistant";
2
2
  export * from "./annotated-function";
3
+ export * from "./action";
@@ -1,4 +1,4 @@
1
- import { AnnotatedFunction, FunctionDefinition, ToolDefinition } from "../types";
1
+ import { Action, AnnotatedFunction, ToolDefinition, Parameter } from "../types";
2
2
 
3
3
  export function annotatedFunctionToChatCompletionFunction(
4
4
  annotatedFunction: AnnotatedFunction<any[]>,
@@ -34,3 +34,92 @@ export function annotatedFunctionToChatCompletionFunction(
34
34
 
35
35
  return chatCompletionFunction;
36
36
  }
37
+
38
+ function convertAttribute(attribute: Parameter): any {
39
+ switch (attribute.type) {
40
+ case "string":
41
+ return {
42
+ type: "string",
43
+ description: attribute.description,
44
+ ...(attribute.enum && { enum: attribute.enum }),
45
+ };
46
+ case "number":
47
+ case "boolean":
48
+ return {
49
+ type: attribute.type,
50
+ description: attribute.description,
51
+ };
52
+ case "object":
53
+ case "object[]":
54
+ const properties = attribute.attributes?.reduce((acc, attr) => {
55
+ acc[attr.name] = convertAttribute(attr);
56
+ return acc;
57
+ }, {} as Record<string, any>);
58
+ const required = attribute.attributes
59
+ ?.filter((attr) => attr.required !== false)
60
+ .map((attr) => attr.name);
61
+ if (attribute.type === "object[]") {
62
+ return {
63
+ type: "array",
64
+ items: {
65
+ type: "object",
66
+ ...(properties && { properties }),
67
+ ...(required && required.length > 0 && { required }),
68
+ },
69
+ description: attribute.description,
70
+ };
71
+ }
72
+ return {
73
+ type: "object",
74
+ description: attribute.description,
75
+ ...(properties && { properties }),
76
+ ...(required && required.length > 0 && { required }),
77
+ };
78
+ default:
79
+ // Handle arrays of primitive types and undefined attribute.type
80
+ if (attribute.type?.endsWith("[]")) {
81
+ const itemType = attribute.type.slice(0, -2);
82
+ return {
83
+ type: "array",
84
+ items: { type: itemType },
85
+ description: attribute.description,
86
+ };
87
+ }
88
+ // Fallback for undefined type or any other unexpected type
89
+ return {
90
+ type: "string",
91
+ description: attribute.description,
92
+ };
93
+ }
94
+ }
95
+
96
+ export function actionToChatCompletionFunction(action: Action<any>): ToolDefinition {
97
+ // Create the parameters object based on the argumentAnnotations
98
+ let parameters: { [key: string]: any } = {};
99
+ for (let parameter of action.parameters || []) {
100
+ parameters[parameter.name] = convertAttribute(parameter);
101
+ }
102
+
103
+ let requiredParameterNames: string[] = [];
104
+ for (let arg of action.parameters || []) {
105
+ if (arg.required !== false) {
106
+ requiredParameterNames.push(arg.name);
107
+ }
108
+ }
109
+
110
+ // Create the ChatCompletionFunctions object
111
+ let chatCompletionFunction: ToolDefinition = {
112
+ type: "function",
113
+ function: {
114
+ name: action.name,
115
+ ...(action.description && { description: action.description }),
116
+ parameters: {
117
+ type: "object",
118
+ properties: parameters,
119
+ required: requiredParameterNames,
120
+ },
121
+ },
122
+ };
123
+
124
+ return chatCompletionFunction;
125
+ }
@@ -3,6 +3,7 @@ import { Role } from "../types/openai-assistant";
3
3
  export interface ChatCompletionChunk {
4
4
  choices: {
5
5
  delta: {
6
+ id?: string;
6
7
  role: Role;
7
8
  content?: string | null;
8
9
 
@@ -1 +0,0 @@
1
- //# sourceMappingURL=chunk-FD6FGKYY.mjs.map
@@ -1,32 +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
- type: "function",
16
- function: {
17
- name: annotatedFunction.name,
18
- description: annotatedFunction.description,
19
- parameters: {
20
- type: "object",
21
- properties: parameters,
22
- required: requiredParameterNames
23
- }
24
- }
25
- };
26
- return chatCompletionFunction;
27
- }
28
-
29
- export {
30
- annotatedFunctionToChatCompletionFunction
31
- };
32
- //# sourceMappingURL=chunk-NSY3OEEP.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/utils/annotated-function.ts"],"sourcesContent":["import { AnnotatedFunction, FunctionDefinition, ToolDefinition } from \"../types\";\n\nexport function annotatedFunctionToChatCompletionFunction(\n annotatedFunction: AnnotatedFunction<any[]>,\n): ToolDefinition {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let arg of annotatedFunction.argumentAnnotations) {\n // isolate the args we should forward inline\n let { name, required, ...forwardedArgs } = arg;\n parameters[arg.name] = forwardedArgs;\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of annotatedFunction.argumentAnnotations) {\n if (arg.required) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n let chatCompletionFunction: ToolDefinition = {\n type: \"function\",\n function: {\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n },\n };\n\n return chatCompletionFunction;\n}\n"],"mappings":";AAEO,SAAS,0CACd,mBACgB;AAEhB,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,yBAAyC;AAAA,IAC3C,MAAM;AAAA,IACN,UAAU;AAAA,MACR,MAAM,kBAAkB;AAAA,MACxB,aAAa,kBAAkB;AAAA,MAC/B,YAAY;AAAA,QACV,MAAM;AAAA,QACN,YAAY;AAAA,QACZ,UAAU;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;","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\n // TODO:\n // Temporarily add name to the OpenAI protocol until we\n // have our own protocol.\n // When name is set, we return the result of a server-side\n // function call.\n name?: string;\n\n function_call?: {\n name?: string;\n arguments?: string;\n };\n tool_calls?: {\n index: number;\n id?: string;\n function: {\n arguments?: string;\n\n name?: string;\n // TODO:\n // Temporarily add scope to the OpenAI protocol until we\n // have our own protocol.\n // When scope is \"server\", the client will not attempt to\n // execute the function.\n scope?: \"client\" | \"server\";\n };\n }[];\n };\n }[];\n}\n\n// TODO:\n// it's possible that unicode characters could be split across chunks\n// make sure to properly handle that\nexport function parseChatCompletion(\n stream: ReadableStream<Uint8Array>,\n): ReadableStream<ChatCompletionChunk> {\n const reader = stream.getReader();\n let buffer = new Uint8Array();\n\n async function cleanup(controller?: ReadableStreamDefaultController<any>) {\n if (controller) {\n try {\n controller.close();\n } catch (_) {}\n }\n if (reader) {\n try {\n await reader.cancel();\n } catch (_) {}\n }\n }\n\n return new ReadableStream<ChatCompletionChunk>({\n async pull(controller) {\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n if (done) {\n await cleanup(controller);\n return;\n }\n\n const newBuffer = new Uint8Array(buffer.length + value.length);\n newBuffer.set(buffer);\n newBuffer.set(value, buffer.length);\n buffer = newBuffer;\n\n const valueString = new TextDecoder(\"utf-8\").decode(buffer);\n const lines = valueString.split(\"\\n\").filter((line) => line.trim() !== \"\");\n\n // If the last line isn't complete, keep it in the buffer for next time\n buffer = !valueString.endsWith(\"\\n\")\n ? new TextEncoder().encode(lines.pop() || \"\")\n : new Uint8Array();\n\n for (const line of lines) {\n const cleanedLine = line.replace(/^data: /, \"\");\n\n if (cleanedLine === \"[DONE]\") {\n await cleanup(controller);\n return;\n }\n\n const json = JSON.parse(cleanedLine);\n controller.enqueue(json);\n }\n } catch (error) {\n controller.error(error);\n await cleanup(controller);\n return;\n }\n }\n },\n cancel() {\n reader.cancel();\n },\n });\n}\n"],"mappings":";AAyCO,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":[]}