Skip to content

Commit fff8390

Browse files
committed
feat: document JSON Schema type properties on Functionary chat function types
1 parent a39b84b commit fff8390

File tree

2 files changed

+136
-4
lines changed

2 files changed

+136
-4
lines changed

src/utils/getTypeScriptTypeStringForGbnfJsonSchema.ts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
2-
GbnfJsonSchema, isGbnfJsonArraySchema, isGbnfJsonBasicSchemaIncludesType, isGbnfJsonConstSchema,
3-
isGbnfJsonEnumSchema, isGbnfJsonObjectSchema, isGbnfJsonOneOfSchema
2+
GbnfJsonSchema, isGbnfJsonArraySchema, isGbnfJsonBasicSchemaIncludesType, isGbnfJsonBasicStringSchema, isGbnfJsonConstSchema,
3+
isGbnfJsonEnumSchema, isGbnfJsonFormatStringSchema, isGbnfJsonObjectSchema, isGbnfJsonOneOfSchema
44
} from "./gbnfJson/types.js";
55

66
const maxTypeRepetition = 10;
@@ -31,11 +31,50 @@ export function getTypeScriptTypeStringForGbnfJsonSchema(schema: GbnfJsonSchema)
3131

3232
const mapping = keyText + ": " + valueType;
3333

34-
if (propSchema.description != null && propSchema.description !== "") {
34+
const description: string[] = (propSchema.description != null && propSchema.description !== "")
35+
? [propSchema.description]
36+
: [];
37+
const propInfo: string[] = [];
38+
39+
if (isGbnfJsonBasicStringSchema(propSchema)) {
40+
if (propSchema.minLength != null && propSchema.minLength > 0)
41+
propInfo.push("minimum length: " + String(Math.floor(propSchema.minLength)));
42+
43+
if (propSchema.maxLength != null)
44+
propInfo.push("maximum length: " + String(Math.floor(Math.max(propSchema.maxLength, propSchema.minLength ?? 0, 0))));
45+
} else if (isGbnfJsonFormatStringSchema(propSchema)) {
46+
if (propSchema.format === "date-time")
47+
propInfo.push("format: ISO 8601 date-time");
48+
else
49+
propInfo.push("format: " + String(propSchema.format));
50+
} else if (isGbnfJsonArraySchema(propSchema)) {
51+
if (propSchema.minItems != null && propSchema.minItems > maxTypeRepetition)
52+
propInfo.push("minimum items: " + String(Math.floor(propSchema.minItems)));
53+
54+
if (propSchema.maxItems != null)
55+
propInfo.push("maximum items: " + String(Math.floor(Math.max(propSchema.maxItems, propSchema.minItems ?? 0, 0))));
56+
} else if (isGbnfJsonObjectSchema(propSchema)) {
57+
if (propSchema.minProperties != null && propSchema.minProperties > 0)
58+
propInfo.push("minimum number of properties: " + String(Math.floor(propSchema.minProperties)));
59+
60+
if (propSchema.maxProperties != null)
61+
propInfo.push(
62+
"maximum number of properties: " +
63+
String(Math.floor(Math.max(propSchema.maxProperties, propSchema.minProperties ?? 0, 0)))
64+
);
65+
}
66+
67+
if (propInfo.length > 0)
68+
description.push(propInfo.join(", "));
69+
70+
if (description.length > 0) {
3571
addNewline = true;
3672
return [
3773
"\n",
38-
"// ", propSchema.description.split("\n").join("\n// "),
74+
"// ", description
75+
.join("\n")
76+
.split("\n")
77+
.join("\n// "),
3978
"\n",
4079
mapping
4180
].join("");

test/standalone/chatWrappers/FunctionaryChatWrapper.test.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,61 @@ describe("FunctionaryChatWrapper", () => {
3838
async handler(params) {
3939
return Math.floor(Math.random() * (params.max - params.min + 1) + params.min);
4040
}
41+
}),
42+
func1: defineChatSessionFunction({
43+
description: "Some function",
44+
params: {
45+
type: "object",
46+
properties: {
47+
message: {
48+
type: "string",
49+
description: "Some message",
50+
minLength: 3,
51+
maxLength: 10
52+
},
53+
words: {
54+
type: "array",
55+
description: "Some words",
56+
items: {
57+
type: "string"
58+
},
59+
minItems: 2,
60+
maxItems: 5
61+
},
62+
headers: {
63+
type: "object",
64+
description: "Some headers",
65+
additionalProperties: {
66+
type: "string"
67+
},
68+
minProperties: 4,
69+
maxProperties: 12
70+
},
71+
mappings: {
72+
type: "object",
73+
description: "Some mappings",
74+
properties: {
75+
a: {
76+
type: "boolean"
77+
},
78+
b: {
79+
type: "number"
80+
},
81+
c: {
82+
type: ["string", "null"]
83+
}
84+
},
85+
additionalProperties: {
86+
type: "string"
87+
},
88+
minProperties: 4,
89+
maxProperties: 12
90+
}
91+
}
92+
},
93+
handler(params) {
94+
95+
}
4196
})
4297
};
4398
const conversationHistory2: ChatHistoryItem[] = [{
@@ -169,6 +224,25 @@ describe("FunctionaryChatWrapper", () => {
169224
// Get a random number
170225
type getRandomNumber = (_: {min: number, max: number}) => any;
171226
227+
// Some function
228+
type func1 = (_: {
229+
// Some message
230+
// minimum length: 3, maximum length: 10
231+
message: string,
232+
233+
// Some words
234+
// maximum items: 5
235+
words: [string, string, ...string[]],
236+
237+
// Some headers
238+
// minimum number of properties: 4, maximum number of properties: 12
239+
headers: {[key: string]: string},
240+
241+
// Some mappings
242+
// minimum number of properties: 4, maximum number of properties: 12
243+
mappings: {a: boolean, b: number, c: string | null} & {[key: string]: string}
244+
}) => any;
245+
172246
} // namespace functions",
173247
{
174248
"type": "specialToken",
@@ -524,6 +598,25 @@ describe("FunctionaryChatWrapper", () => {
524598
// Get a random number
525599
type getRandomNumber = (_: {min: number, max: number}) => any;
526600
601+
// Some function
602+
type func1 = (_: {
603+
// Some message
604+
// minimum length: 3, maximum length: 10
605+
message: string,
606+
607+
// Some words
608+
// maximum items: 5
609+
words: [string, string, ...string[]],
610+
611+
// Some headers
612+
// minimum number of properties: 4, maximum number of properties: 12
613+
headers: {[key: string]: string},
614+
615+
// Some mappings
616+
// minimum number of properties: 4, maximum number of properties: 12
617+
mappings: {a: boolean, b: number, c: string | null} & {[key: string]: string}
618+
}) => any;
619+
527620
} // namespace functions",
528621
{
529622
"type": "specialTokensText",

0 commit comments

Comments
 (0)