forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotated-function.ts
More file actions
179 lines (168 loc) · 5.19 KB
/
Copy pathannotated-function.ts
File metadata and controls
179 lines (168 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { Action, AnnotatedFunction, ToolDefinition, Parameter } from "../types";
export function annotatedFunctionToChatCompletionFunction(
annotatedFunction: AnnotatedFunction<any[]>,
): ToolDefinition {
// Create the parameters object based on the argumentAnnotations
let parameters: { [key: string]: any } = {};
for (let arg of annotatedFunction.argumentAnnotations) {
// isolate the args we should forward inline
let { name, required, ...forwardedArgs } = arg;
parameters[arg.name] = forwardedArgs;
}
let requiredParameterNames: string[] = [];
for (let arg of annotatedFunction.argumentAnnotations) {
if (arg.required) {
requiredParameterNames.push(arg.name);
}
}
// Create the ChatCompletionFunctions object
let chatCompletionFunction: ToolDefinition = {
type: "function",
function: {
name: annotatedFunction.name,
description: annotatedFunction.description,
parameters: {
type: "object",
properties: parameters,
required: requiredParameterNames,
},
},
};
return chatCompletionFunction;
}
function convertAttribute(attribute: Parameter): any {
switch (attribute.type) {
case "string":
return {
type: "string",
description: attribute.description,
...(attribute.enum && { enum: attribute.enum }),
};
case "number":
case "boolean":
return {
type: attribute.type,
description: attribute.description,
};
case "object":
case "object[]":
const properties = attribute.attributes?.reduce(
(acc, attr) => {
acc[attr.name] = convertAttribute(attr);
return acc;
},
{} as Record<string, any>,
);
const required = attribute.attributes
?.filter((attr) => attr.required !== false)
.map((attr) => attr.name);
if (attribute.type === "object[]") {
return {
type: "array",
items: {
type: "object",
...(properties && { properties }),
...(required && required.length > 0 && { required }),
},
description: attribute.description,
};
}
return {
type: "object",
description: attribute.description,
...(properties && { properties }),
...(required && required.length > 0 && { required }),
};
default:
// Handle arrays of primitive types and undefined attribute.type
if (attribute.type?.endsWith("[]")) {
const itemType = attribute.type.slice(0, -2);
return {
type: "array",
items: { type: itemType },
description: attribute.description,
};
}
// Fallback for undefined type or any other unexpected type
return {
type: "string",
description: attribute.description,
};
}
}
export function actionToChatCompletionFunction(action: Action<any>): ToolDefinition {
// Create the parameters object based on the argumentAnnotations
let parameters: { [key: string]: any } = {};
for (let parameter of action.parameters || []) {
parameters[parameter.name] = convertAttribute(parameter);
}
let requiredParameterNames: string[] = [];
for (let arg of action.parameters || []) {
if (arg.required !== false) {
requiredParameterNames.push(arg.name);
}
}
// Create the ChatCompletionFunctions object
let chatCompletionFunction: ToolDefinition = {
type: "function",
function: {
name: action.name,
...(action.description && { description: action.description }),
parameters: {
type: "object",
properties: parameters,
required: requiredParameterNames,
},
},
};
return chatCompletionFunction;
}
export function annotatedFunctionToAction(
annotatedFunction: AnnotatedFunction<any[]>,
): Action<any> {
const parameters: Parameter[] = annotatedFunction.argumentAnnotations.map((annotation) => {
switch (annotation.type) {
case "string":
case "number":
case "boolean":
case "object":
return {
name: annotation.name,
description: annotation.description,
type: annotation.type,
required: annotation.required,
};
case "array":
let type;
if (annotation.items.type === "string") {
type = "string[]";
} else if (annotation.items.type === "number") {
type = "number[]";
} else if (annotation.items.type === "boolean") {
type = "boolean[]";
} else if (annotation.items.type === "object") {
type = "object[]";
} else {
type = "string[]";
}
return {
name: annotation.name,
description: annotation.description,
type: type as any,
required: annotation.required,
};
}
});
return {
name: annotatedFunction.name,
description: annotatedFunction.description,
parameters: parameters,
handler: (args) => {
const paramsInCorrectOrder: any[] = [];
for (let arg of annotatedFunction.argumentAnnotations) {
paramsInCorrectOrder.push(args[arg.name]);
}
return annotatedFunction.implementation(...paramsInCorrectOrder);
},
};
}