Address the schema architecture crisis
This commit is contained in:
parent
2eec03661c
commit
b555e41c0b
1 changed files with 114 additions and 40 deletions
|
|
@ -81,46 +81,65 @@ export function hasOutputSchema(toolName: string): boolean {
|
|||
/**
|
||||
* Generates a default value based on a JSON schema type
|
||||
* @param schema The JSON schema definition
|
||||
* @returns A default value matching the schema type, or null for non-required fields
|
||||
* @param propertyName Optional property name for checking if it's required in parent schema
|
||||
* @param parentSchema Optional parent schema to check required array
|
||||
* @returns A default value matching the schema type
|
||||
*/
|
||||
export function generateDefaultValue(schema: JsonSchemaType): JsonValue {
|
||||
if ("default" in schema) {
|
||||
export function generateDefaultValue(
|
||||
schema: JsonSchemaType,
|
||||
propertyName?: string,
|
||||
parentSchema?: JsonSchemaType
|
||||
): JsonValue {
|
||||
if ("default" in schema && schema.default !== undefined) {
|
||||
return schema.default;
|
||||
}
|
||||
|
||||
if (!schema.required) {
|
||||
if (schema.type === "array") return [];
|
||||
if (schema.type === "object") return {};
|
||||
return undefined;
|
||||
}
|
||||
// Check if this property is required in the parent schema
|
||||
const isRequired = parentSchema?.required?.includes(propertyName || "") ?? false;
|
||||
|
||||
switch (schema.type) {
|
||||
case "string":
|
||||
return "";
|
||||
return isRequired ? "" : undefined;
|
||||
case "number":
|
||||
case "integer":
|
||||
return 0;
|
||||
return isRequired ? 0 : undefined;
|
||||
case "boolean":
|
||||
return false;
|
||||
return isRequired ? false : undefined;
|
||||
case "array":
|
||||
return [];
|
||||
case "object": {
|
||||
if (!schema.properties) return {};
|
||||
|
||||
const obj: JsonObject = {};
|
||||
Object.entries(schema.properties)
|
||||
.filter(([, prop]) => prop.required)
|
||||
.forEach(([key, prop]) => {
|
||||
const value = generateDefaultValue(prop);
|
||||
obj[key] = value;
|
||||
});
|
||||
// Only include properties that are required according to the schema's required array
|
||||
Object.entries(schema.properties).forEach(([key, prop]) => {
|
||||
const isPropertyRequired = schema.required?.includes(key) ?? false;
|
||||
if (isPropertyRequired) {
|
||||
const value = generateDefaultValue(prop, key, schema);
|
||||
if (value !== undefined) {
|
||||
obj[key] = value;
|
||||
}
|
||||
}
|
||||
});
|
||||
return obj;
|
||||
}
|
||||
default:
|
||||
case "null":
|
||||
return null;
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to check if a property is required in a schema
|
||||
* @param propertyName The name of the property to check
|
||||
* @param schema The parent schema containing the required array
|
||||
* @returns true if the property is required, false otherwise
|
||||
*/
|
||||
export function isPropertyRequired(propertyName: string, schema: JsonSchemaType): boolean {
|
||||
return schema.required?.includes(propertyName) ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a field key into a human-readable label
|
||||
* @param key The field key to format
|
||||
|
|
|
|||
Loading…
Reference in a new issue