UNPKG

@draftor/tools

Version:

A simple TypeScript/Javascript functions to openai tool call format

11 lines (10 loc) 5.14 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 private queue: Array<{ toolCall: IToolCall | string; args?: object }> = [];\r\n private isProcessing = false;\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 }\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?.trim() || 'Description unavailable.',\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\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 /**\r\n * Processes the queue of function calls\r\n * @returns Array of results from executed functions\r\n */\r\n \r\n private async processQueue(): Promise<any[]> {\r\n this.isProcessing = true;\r\n const results: any[] = [];\r\n\r\n while (this.queue.length > 0) {\r\n const { toolCall, args } = this.queue.shift()!; \r\n try {\r\n const result = this.exec(toolCall, args);\r\n results.push(result); \r\n } catch (error) {\r\n console.error('Error executing function:', error); \r\n results.push(null);\r\n }\r\n }\r\n \r\n this.isProcessing = false;\r\n return results;\r\n }\r\n /**\r\n * Adds function calls to queue and processes them\r\n * @param toolCalls Array of tool calls or function names\r\n * @param args Optional arguments for each call\r\n */\r\n async executeAll(toolCalls: Array<IToolCall | string>, args?: object[]): Promise<any[]> {\r\n // Add calls to queue\r\n toolCalls.forEach((call, index) => {\r\n this.queue.push({\r\n toolCall: call,\r\n args: args?.[index]\r\n });\r\n });\r\n\r\n // Process queue if not already processing\r\n if (!this.isProcessing) {\r\n return this.processQueue();\r\n\r\n }\r\n\r\n return [];\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 \r\n}```\n</context>\n\n\n QUERY: test" } ] } context: file:///c%3A/Users/PRAVEEN%20NAIK/Codespace/tool/.cache/draftor/2025-01-17/1737129799-draftor-deepseek-deepseek-chat.md =================================