forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-schema.ts
More file actions
120 lines (110 loc) · 3.04 KB
/
Copy pathjson-schema.ts
File metadata and controls
120 lines (110 loc) · 3.04 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
import { Parameter } from "../types";
export type JSONSchemaString = {
type: "string";
description?: string;
enum?: string[];
};
export type JSONSchemaNumber = {
type: "number";
description?: string;
};
export type JSONSchemaBoolean = {
type: "boolean";
description?: string;
};
export type JSONSchemaObject = {
type: "object";
properties?: Record<string, JSONSchema>;
required?: string[];
description?: string;
};
export type JSONSchemaArray = {
type: "array";
items: JSONSchema;
description?: string;
};
export type JSONSchema =
| JSONSchemaString
| JSONSchemaNumber
| JSONSchemaBoolean
| JSONSchemaObject
| JSONSchemaArray;
export function actionParametersToJsonSchema(actionParameters: Parameter[]): JSONSchema {
// Create the parameters object based on the argumentAnnotations
let parameters: { [key: string]: any } = {};
for (let parameter of actionParameters || []) {
parameters[parameter.name] = convertAttribute(parameter);
}
let requiredParameterNames: string[] = [];
for (let arg of actionParameters || []) {
if (arg.required !== false) {
requiredParameterNames.push(arg.name);
}
}
// Create the ChatCompletionFunctions object
return {
type: "object",
properties: parameters,
required: requiredParameterNames,
};
}
function convertAttribute(attribute: Parameter): JSONSchema {
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 as any },
description: attribute.description,
};
}
// Fallback for undefined type or any other unexpected type
return {
type: "string",
description: attribute.description,
};
}
}