Added new json output support
This commit is contained in:
49
src/tools.ts
49
src/tools.ts
@@ -1,6 +1,6 @@
|
||||
import * as cheerio from 'cheerio';
|
||||
import {$Sync} from '@ztimson/node-utils';
|
||||
import {ASet, consoleInterceptor, Http, fn as Fn, decodeHtml} from '@ztimson/utils';
|
||||
import {ASet, consoleInterceptor, Http, fn as Fn, decodeHtml, objectMap} from '@ztimson/utils';
|
||||
import * as os from 'node:os';
|
||||
import {Ai} from './ai.ts';
|
||||
import {LLMRequest} from './llm.ts';
|
||||
@@ -44,6 +44,53 @@ export type AiTool = {
|
||||
fn: (args: any, stream: LLMRequest['stream'], ai: Ai) => any | Promise<any>,
|
||||
};
|
||||
|
||||
export function convertSchema(schema: any): any {
|
||||
if(!schema) return null;
|
||||
|
||||
const convertProp = (prop: any): any => {
|
||||
const converted: any = {
|
||||
type: prop.type || 'string',
|
||||
};
|
||||
|
||||
if(prop.description) converted.description = prop.description;
|
||||
if(prop.default !== undefined) converted.default = prop.default;
|
||||
if(prop.enum) converted.enum = prop.enum;
|
||||
if(prop.pattern) converted.pattern = prop.pattern;
|
||||
|
||||
// Handle array items
|
||||
if(prop.type === 'array' && prop.items) {
|
||||
converted.items = convertProp(prop.items);
|
||||
}
|
||||
|
||||
// Handle object properties
|
||||
if(prop.type === 'object' && prop.items) {
|
||||
converted.properties = objectMap(prop.items, (key, value) => convertProp(value));
|
||||
const required = Object.entries(prop.items).filter(([_, v]: any) => v.required).map(([k]) => k);
|
||||
if(required.length) converted.required = required;
|
||||
converted.additionalProperties = false;
|
||||
}
|
||||
|
||||
// Handle min/max based on type
|
||||
if(prop.min !== undefined) {
|
||||
if(prop.type === 'string' || prop.type === 'array') converted.minLength = prop.min;
|
||||
else converted.minimum = prop.min;
|
||||
}
|
||||
if(prop.max !== undefined) {
|
||||
if(prop.type === 'string' || prop.type === 'array') converted.maxLength = prop.max;
|
||||
else converted.maximum = prop.max;
|
||||
}
|
||||
|
||||
return converted;
|
||||
};
|
||||
|
||||
return {
|
||||
type: 'object',
|
||||
properties: objectMap(schema, (key, value) => convertProp(value)),
|
||||
required: Object.entries(schema).filter(([_, v]: any) => v.required).map(([k]) => k),
|
||||
additionalProperties: false
|
||||
};
|
||||
}
|
||||
|
||||
export const CliTool: AiTool = {
|
||||
name: 'cli',
|
||||
description: 'Use the command line interface, returns any output',
|
||||
|
||||
Reference in New Issue
Block a user