UNPKG

@draftor/tools

Version:

A simple TypeScript/Javascript functions to openai tool call format

83 lines (64 loc) 5.37 kB
prompt: { "messages": [ { "role": "user", "content": "<context>\n```typescript|file:///c%3A/Users/PRAVEEN%20NAIK/Codespace/tool/src/types.ts\nexport interface IParam {\r\n type: 'object';\r\n properties: Record<string, { type: string; description: string }>;\r\n}\r\n\r\nexport interface ITools {\r\n toOpenAI: (type?: string) => string | ITool[];\r\n}\r\n\r\n\r\n\r\n// https://openrouter.ai/docs/requests#:~:text=object%0A%7D%3B-,type%20Tool%20%3D%20%7B,-type%3A%20%22function\r\nexport interface ITool {\r\n type: 'function';\r\n function: IFunctionDescription;\r\n}\r\n\r\nexport interface IFunctionDescription {\r\n description?: string;\r\n name: string;\r\n parameters: object; // JSON Schema object\r\n}\r\n```\n\n```typescript|file:///c%3A/Users/PRAVEEN%20NAIK/Codespace/tool/src/index.ts\nimport * as doctrine from 'doctrine';\r\nimport { IParam, ITools, ITool, IFunctionDescription } from './types';\r\n\r\nexport class Tools implements ITools {\r\n public funcs: Function[] | undefined;\r\n constructor(funcs: Function[]) {\r\n this.funcs = funcs;\r\n }\r\n\r\n private extractFirstJsDocComment(func: Function) {\r\n const jsDocRegex = /\\/\\*\\*([\\s\\S]*?)\\*\\//;\r\n const match = jsDocRegex.exec(func.toString());\r\n return match ? match[0] : null;\r\n }\r\n\r\n private transformDoctrineToOpenAITool(\r\n doctrineOutput: any,\r\n func: Function\r\n ): ITool {\r\n const { description, tags } = doctrineOutput;\r\n\r\n const params: IParam = {\r\n type: 'object',\r\n properties: {},\r\n };\r\n\r\n let returnType = 'void';\r\n let returnDescription = '';\r\n\r\n tags.forEach((tag: any) => {\r\n if (tag.title === 'param') {\r\n const param = tag?.name as string;\r\n const type = tag?.type?.name || 'any';\r\n const desc = tag?.description || '';\r\n console.log(typeof desc);\r\n\r\n params.properties[param] = {\r\n type,\r\n description: desc,\r\n };\r\n\r\n // params.required.push(tag?.name);\r\n }\r\n\r\n if (tag.title === 'returns') {\r\n returnType = tag.type?.name || 'any';\r\n returnDescription = tag.description || '';\r\n }\r\n });\r\n\r\n const funcDesc: IFunctionDescription = {\r\n name: func.name,\r\n description: description || 'No description provided.',\r\n parameters: params,\r\n };\r\n\r\n const tool: ITool = {\r\n type: 'function',\r\n function: funcDesc,\r\n };\r\n\r\n return tool;\r\n }\r\n\r\n toOpenAI(type?: string): ITool[] | string {\r\n if (this.funcs && this.funcs?.length !== 0) {\r\n // reducer\r\n const processedFunctions = this.funcs.reduce((acc, func) => {\r\n const comment = this.extractFirstJsDocComment(func);\r\n const doc = doctrine.parse(comment || '', { unwrap: true });\r\n const transformed = this.transformDoctrineToOpenAITool(\r\n doc,\r\n func\r\n ) as unknown as ITool;\r\n acc.push(transformed);\r\n return acc;\r\n }, [] as ITool[]);\r\n\r\n return type == 'string'? JSON.stringify(processedFunctions): processedFunctions;\r\n }\r\n return '';\r\n }\r\n}\r\n```\n</context>\n\n\n QUERY: -> Add a fucntion to ITools called exec which executes the function\n-> Allow overloading of fucntion or options in params, iter ttake the tool call respose completely ie IToolCall or take func: string, params: object\n-> complete the fucntion in index.ts" } ] } context: file:///c%3A/Users/PRAVEEN%20NAIK/Codespace/tool/.cache/draftor/2025-01-16/1737042250-draftor-openai-gpt-4o-2024-11-20.md ================================= **Draft changes**: ```bash root/ ├── src/ ├── types.ts | +3 -0 └── index.ts | +27 -0 ``` **Gist** : Key changes 1. `types.ts` - Added `exec` function signature to `ITools`. 2. `index.ts` - Implemented `exec` for function execution with overload supporting both `IToolCall` and string-based `func`. --- ```typescript|file:///c%3A/Users/PRAVEEN%20NAIK/Codespace/tool/src/types.ts // ... existing code ... export interface IToolCall { tool: string; params: object; } export interface ITools { // ... existing code ... exec(input: IToolCall): any; exec(func: string, params: object): any; } // ... existing code ... ``` --- ```typescript|file:///c%3A/Users/PRAVEEN%20NAIK/Codespace/tool/src/index.ts // ... existing code ... export class Tools implements ITools { // ... existing code ... exec(inputOrFunc: IToolCall | string, paramsObject?: object): any { if (typeof inputOrFunc === 'string') { const funcName = inputOrFunc; if (!this.funcs) { throw new Error('No functions available to execute.'); } const func = this.funcs.find((fn) => fn.name === funcName); if (!func) { throw new Error(`Function '${funcName}' not found.`); } return func(paramsObject); } const { tool, params } = inputOrFunc; if (!this.funcs) { throw new Error('No functions available to execute.'); } const func = this.funcs.find((fn) => fn.name === tool); if (!func) { throw new Error(`Function '${tool}' not found.`); } return func(params); } // ... existing code ... } ```