Datasets:
Download generator/utils.ts from labqoat/AsyncTool: direct link, hf CLI and curl.
- Browser
- Download file 3.23 kB
-
https://huggingface.co/datasets/labqoat/AsyncTool/resolve/main/generator/utils.ts
- Command line
-
hf download hf://datasets/labqoat/AsyncTool/generator/utils.ts
-
curl -L -o utils.ts https://huggingface.co/datasets/labqoat/AsyncTool/resolve/main/generator/utils.ts
3.23 kB
| import { convertJsonSchemaToZod } from "zod-from-json-schema"; | |
| import z from "zod"; | |
| import { | |
| isEmptyObjectSchema, | |
| tool, | |
| uniqueOneOf, | |
| type IToolDefinition, | |
| } from "@qforge/torque"; | |
| import { allTools } from "./tools-list"; | |
| function extractPropertyPaths( | |
| schema: z.ZodTypeAny, | |
| prefix: string = "" | |
| ): string[] { | |
| if (schema instanceof z.ZodObject) { | |
| const shape = schema.shape; | |
| const paths: string[] = []; | |
| for (const [key, value] of Object.entries(shape)) { | |
| const currentPath = prefix ? `${prefix}.${key}` : key; | |
| if (value instanceof z.ZodObject) { | |
| // Recursively extract nested properties | |
| paths.push(...extractPropertyPaths(value, currentPath)); | |
| } else { | |
| // Leaf property | |
| paths.push(currentPath); | |
| } | |
| } | |
| return paths; | |
| } | |
| return prefix ? [prefix] : []; | |
| } | |
| export function getResultProperties(outputSchema: z.ZodTypeAny): string { | |
| if (outputSchema instanceof z.ZodObject && outputSchema.shape.result) { | |
| const resultSchema = outputSchema.shape.result; | |
| const properties = extractPropertyPaths(resultSchema); | |
| return properties.join(", "); | |
| } | |
| return "N/A"; | |
| } | |
| export function toolsWithNonEmptyInputParameters( | |
| tools: IToolDefinition[] | |
| ): IToolDefinition[] { | |
| return tools.filter((tool) => !isEmptyObjectSchema(tool.parameters)); | |
| } | |
| let cachedTools: IToolDefinition[] = []; | |
| export function toolsToToolDefinitionArray(): IToolDefinition[] { | |
| if (cachedTools.length) return cachedTools; | |
| const result = allTools.reduce((acc, t) => { | |
| if (typeof t !== "object" || t === null) { | |
| console.warn("Skipping tool: ", JSON.stringify(t)); | |
| return acc; | |
| } | |
| if ( | |
| typeof t.name !== "string" || | |
| typeof t.description !== "string" || | |
| typeof t.parameters !== "object" | |
| ) { | |
| console.warn("Skipping tool: ", JSON.stringify(t)); | |
| return acc; | |
| } | |
| return [ | |
| ...acc, | |
| tool({ | |
| name: t.name, | |
| description: t.description, | |
| // @ts-ignore | |
| parameters: convertJsonSchemaToZod(t.parameters), | |
| output: z.object({ | |
| result: convertJsonSchemaToZod(t.output), | |
| }), | |
| }), | |
| ]; | |
| }, [] as IToolDefinition[]); | |
| cachedTools = result; | |
| return result; | |
| } | |
| export const uniqueOneOfTools = uniqueOneOf(toolsToToolDefinitionArray()); | |
| export const uniqueOneOfToolsWithInputParameters = uniqueOneOf( | |
| toolsWithNonEmptyInputParameters(toolsToToolDefinitionArray()) | |
| ); | |
| export const uniqueRandomSampleOfToolsWithInputParameters = ( | |
| sampleSize: number | |
| ) => { | |
| const uniqueTools: IToolDefinition[] = []; | |
| while (uniqueTools.length < sampleSize) { | |
| const tool = uniqueOneOfToolsWithInputParameters(); | |
| uniqueTools.push(tool); | |
| } | |
| return uniqueTools; | |
| }; | |
| export const uniqueRandomSampleOfTools = (sampleSize: number) => { | |
| const uniqueTools: IToolDefinition[] = []; | |
| while (uniqueTools.length < sampleSize) { | |
| const tool = uniqueOneOfTools(); | |
| uniqueTools.push(tool); | |
| } | |
| return uniqueTools; | |
| }; | |
| export function getScenarioFileName(filePath: string) { | |
| return filePath.split("/").pop()?.split(".")[0]; | |
| } | |
| export function getScenarioResultPath(filePath: string) { | |
| return `data/result_${getScenarioFileName(filePath)}.jsonl`; | |
| } | |