Compare commits

..

2 Commits
0.8.5 ... 0.8.7

Author SHA1 Message Date
5d34652d46 Fixed CLI tool
All checks were successful
Publish Library / Build NPM Project (push) Successful in 39s
Publish Library / Tag Version (push) Successful in 10s
2026-03-01 18:11:25 -05:00
6454548364 Fixed CLI tool
All checks were successful
Publish Library / Build NPM Project (push) Successful in 41s
Publish Library / Tag Version (push) Successful in 9s
2026-03-01 17:18:30 -05:00
3 changed files with 20 additions and 11 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@ztimson/ai-utils", "name": "@ztimson/ai-utils",
"version": "0.8.5", "version": "0.8.7",
"description": "AI Utility library", "description": "AI Utility library",
"author": "Zak Timson", "author": "Zak Timson",
"license": "MIT", "license": "MIT",

View File

@@ -148,6 +148,7 @@ export class OpenAi extends LLMProvider {
try { try {
const args = JSONAttemptParse(toolCall.function.arguments, {}); const args = JSONAttemptParse(toolCall.function.arguments, {});
const result = await tool.fn(args, options.stream, this.ai); const result = await tool.fn(args, options.stream, this.ai);
console.log(result);
return {role: 'tool', tool_call_id: toolCall.id, content: JSONSanitize(result)}; return {role: 'tool', tool_call_id: toolCall.id, content: JSONSanitize(result)};
} catch (err: any) { } catch (err: any) {
return {role: 'tool', tool_call_id: toolCall.id, content: JSONSanitize({error: err?.message || err?.toString() || 'Unknown'})}; return {role: 'tool', tool_call_id: toolCall.id, content: JSONSanitize({error: err?.message || err?.toString() || 'Unknown'})};

View File

@@ -1,9 +1,15 @@
import * as cheerio from 'cheerio'; import * as cheerio from 'cheerio';
import {$, $Sync} from '@ztimson/node-utils'; import {$Sync} from '@ztimson/node-utils';
import {ASet, consoleInterceptor, Http, fn as Fn} from '@ztimson/utils'; import {ASet, consoleInterceptor, Http, fn as Fn} from '@ztimson/utils';
import * as os from 'node:os';
import {Ai} from './ai.ts'; import {Ai} from './ai.ts';
import {LLMRequest} from './llm.ts'; import {LLMRequest} from './llm.ts';
const getShell = () => {
if(os.platform() == 'win32') return 'cmd';
return $Sync`echo $SHELL`?.split('/').pop() || 'bash';
}
export type AiToolArg = {[key: string]: { export type AiToolArg = {[key: string]: {
/** Argument type */ /** Argument type */
type: 'array' | 'boolean' | 'number' | 'object' | 'string', type: 'array' | 'boolean' | 'number' | 'object' | 'string',
@@ -40,7 +46,7 @@ export const CliTool: AiTool = {
name: 'cli', name: 'cli',
description: 'Use the command line interface, returns any output', description: 'Use the command line interface, returns any output',
args: {command: {type: 'string', description: 'Command to run', required: true}}, args: {command: {type: 'string', description: 'Command to run', required: true}},
fn: (args: {command: string}) => $`${args.command}` fn: (args: {command: string}) => $Sync`${args.command}`
} }
export const DateTimeTool: AiTool = { export const DateTimeTool: AiTool = {
@@ -54,19 +60,20 @@ export const ExecTool: AiTool = {
name: 'exec', name: 'exec',
description: 'Run code/scripts', description: 'Run code/scripts',
args: { args: {
language: {type: 'string', description: 'Execution language', enum: ['cli', 'node', 'python'], required: true}, language: {type: 'string', description: `Execution language (CLI: ${getShell()})`, enum: ['cli', 'node', 'python'], required: true},
code: {type: 'string', description: 'Code to execute', required: true} code: {type: 'string', description: 'Code to execute', required: true}
}, },
fn: async (args, stream, ai) => { fn: async (args, stream, ai) => {
try { try {
switch(args.type) { switch(args.language) {
case 'bash': case 'cli':
return await CliTool.fn({command: args.code}, stream, ai); return await CliTool.fn({command: args.code}, stream, ai);
case 'node': case 'node':
return await JSTool.fn({code: args.code}, stream, ai); return await JSTool.fn({code: args.code}, stream, ai);
case 'python': { case 'python':
return await PythonTool.fn({code: args.code}, stream, ai); return await PythonTool.fn({code: args.code}, stream, ai);
} default:
throw new Error(`Unsupported language: ${args.language}`);
} }
} catch(err: any) { } catch(err: any) {
return {error: err?.message || err.toString()}; return {error: err?.message || err.toString()};
@@ -98,9 +105,10 @@ export const JSTool: AiTool = {
code: {type: 'string', description: 'CommonJS javascript', required: true} code: {type: 'string', description: 'CommonJS javascript', required: true}
}, },
fn: async (args: {code: string}) => { fn: async (args: {code: string}) => {
const console = consoleInterceptor(null); console.log('executing js')
const resp = await Fn<any>({console}, args.code, true).catch((err: any) => console.output.error.push(err)); const c = consoleInterceptor(null);
return {...console.output, return: resp, stdout: undefined, stderr: undefined}; const resp = await Fn<any>({console: c}, args.code, true).catch((err: any) => c.output.error.push(err));
return {...c.output, return: resp, stdout: undefined, stderr: undefined};
} }
} }