UNPKG

@draftor/tools

Version:

A simple TypeScript/Javascript functions to openai tool call format

54 lines (43 loc) 5.05 kB
prompt: { "messages": [ { "role": "user", "content": "<context>\n```typescript|file:///c%3A/Users/PRAVEEN%20NAIK/Codespace/tool/src/index.ts\nimport * as doctrine from 'doctrine';\r\nimport { IParam, ITools, ITool, IFunctionDescription, IToolCall, IFunctionCall } 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\r\n exec(toolCall: IToolCall | string, args?: object): any {\r\n if (typeof toolCall !== 'string') {\r\n const { function: func } = toolCall;\r\n const { name, arguments: args } = func;\r\n\r\n try {\r\n const callArgs = JSON.parse(args);\r\n const callFunc = this.funcs?.filter((e) => e.name == name)[0] as unknown as Function;\r\n const x = Object.keys(callArgs).map(e => callArgs[e]);\r\n return callFunc(...x);\r\n } catch (e) {\r\n console.log(e)\r\n }\r\n } else {\r\n try {\r\n const callFunc = this.funcs?.filter((e) => e.name == toolCall)[0] as unknown as Function;\r\n const y = args ?? {};\r\n const x = Object.keys(y).map((key: string) => (y as Record<string, any>)[key]);\r\n return callFunc(...x);\r\n } catch (e) {\r\n console.log(e)\r\n }\r\n }\r\n }\r\n}```\n</context>\n\n\n QUERY: Improve this exec function" } ] } context: file:///c%3A/Users/PRAVEEN%20NAIK/Codespace/tool/.cache/draftor/2025-01-16/1737048611-draftor-openai-gpt-4o-2024-11-20.md ================================= **Draft changes**: ```bash root/ ├── tool/ └── src/ ├── index.ts | +20 -7 ``` **Gist** : Key changes 1. Enhanced error handling in `exec` function. 2. Eliminated redundant code by combining string and object handling cases. 3. Improved readability by extracting repetitive operations into helper functions. ```typescript|file:///c%3A/Users/PRAVEEN%20NAIK/Codespace/tool/src/index.ts // ... existing code ... private getFunctionByName(name: string): Function | undefined { return this.funcs?.find((e) => e.name === name); } private parseArgs(args: object = {}): any[] { return Object.keys(args).map((key: string) => (args as Record<string, any>)[key]); } exec(toolCall: IToolCall | string, args?: object): any { try { const functionName = typeof toolCall === 'string' ? toolCall : toolCall.function.name; const parsedArgs = typeof toolCall === 'string' ? this.parseArgs(args) : this.parseArgs(JSON.parse(toolCall.function.arguments)); const callFunc = this.getFunctionByName(functionName); if (!callFunc) throw new Error(`Function ${functionName} not found`); return callFunc(...parsedArgs); } catch (error) { console.error("Error in exec function:", error.message); return null; } } // ... existing code ... ```