MCPcopy Create free account
hub / github.com/Superflows-AI/superflows / getFilledNoChoiceRequiredFields

Function getFilledNoChoiceRequiredFields

lib/actionUtils.ts:31–72  ·  view source on GitHub ↗
(
  schema: OpenAPIV3_1.SchemaObject | undefined,
  parentRequired: boolean = true,
)

Source from the content-addressed store, hash-verified

29}
30
31export function getFilledNoChoiceRequiredFields(
32 schema: OpenAPIV3_1.SchemaObject | undefined,
33 parentRequired: boolean = true,
34): any {
35 /** Gets an object with filled fields where no choice is required by the AI.
36 *
37 * Used after the AI call, before API call, to get the parameters
38 * where no choice is required, but the parameters need to be filled
39 * for the API call to succeed. **/
40 // TODO: Doesn't deal with arrays yet, only deals with nested objects
41 if (!schema || schema.type !== "object" || !parentRequired) {
42 return null;
43 }
44 // Below is for case where it's an object schema but that's all you know
45 if (Object.keys(schema).length === 1) {
46 return {};
47 }
48
49 let filledObject: any = {};
50 const requiredKeys = schema.required || [];
51
52 for (let key in schema.properties) {
53 const isKeyRequired = requiredKeys.includes(key);
54 const childSchema = schema.properties[key] as OpenAPIV3_1.SchemaObject;
55
56 // If key is required and has only one enum value, add it into the filled object.
57 if (isKeyRequired && childSchema.enum && childSchema.enum.length === 1) {
58 filledObject[key] = childSchema.enum[0];
59 } else if (childSchema.type === "object") {
60 // Else if the property is an object, then recursively fill its parameters.
61 const filledChildObject = getFilledNoChoiceRequiredFields(
62 childSchema,
63 isKeyRequired,
64 );
65 if (filledChildObject) {
66 filledObject[key] = filledChildObject;
67 }
68 }
69 }
70
71 return Object.keys(filledObject).length > 0 ? filledObject : null;
72}
73
74export function fillNoChoiceRequiredParams<
75 Params extends Record<string, unknown>,

Callers 2

Calls

no outgoing calls

Tested by

no test coverage detected