@draftor/tools
Version:
A simple TypeScript/Javascript functions to openai tool call format
58 lines (48 loc) • 5.51 kB
Markdown
prompt: {
"messages": [
{
"role": "user",
"content": "-> 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"
},
{
"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 } 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 exec(inputOrFunc: IToolCall | string, paramsObject?: object): any {\r\n\r\n return func(params);\r\n }\r\n\r\n}```\n</context>\n\n\n QUERY: complete the exec fuction. \n\nthis is sample response\n```\n {\n \"index\": 0,\n \"id\": \"call_F0tCbPIDQyzxGxYaFvuSGPQN\",\n \"type\": \"function\",\n \"function\": {\n \"name\": \"snipUpdate\",\n \"arguments\": \"{\\\"start\\\":66,\\\"end\\\":66,\\\"code\\\":\\\" exec(inputOrFunc: IToolCall | string, paramsObject?: object): any {\\\\n if (typeof inputOrFunc === 'string') {\\\\n const funcName = inputOrFunc;\\\\n if (!this.funcs) {\\\\n throw new Error('No functions available to execute.');\\\\n }\\\\n\\\\n const func = this.funcs.find((fn) => fn.name === funcName);\\\\n if (!func) {\\\\n throw new Error(`Function '${funcName}' not found.`);\\\\n }\\\\n return func(paramsObject);\\\\n } \\\\n \\\\n const { tool, params } = inputOrFunc;\\\\n if (!this.funcs) {\\\\n throw new Error('No functions available to execute.');\\\\n }\\\\n\\\\n const func = this.funcs.find((fn) => fn.name === tool);\\\\n if (!func) {\\\\n throw new Error(`Function '${tool}' not found.`);\\\\n }\\\\n return func(params);\\\\n }\\\"}\"\n }\n }\n```"
}
]
}
context: file:///c%3A/Users/PRAVEEN%20NAIK/Codespace/tool/.cache/draftor/2025-01-16/1737042998-draftor-openai-gpt-4o-2024-11-20.md
=================================
**Draft changes**:
```bash
root/
├── src/
│ ├── index.ts | +29 -0
```
**Gist** : Key changes
1. `index.ts` - Completed the `exec` function to handle IToolCall or string execution.
```typescript|file:///c%3A/Users/PRAVEEN%20NAIK/Codespace/tool/src/index.ts
// ... 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 ...
```