import * as u from './utils.js'; import * as wget from './wget.js'; import * as chat from './chat.js'; import * as notes from './notes.js'; import * as remind from './remind.js'; import * as t from './tools-utils.js'; export type Call = t.Call; export const getCall = t.getCall; export const isToolStr = t.isToolStr; export const getPromptStr = t.getPromptStr; let toolUseCount = 0; export async function resetUseCount() { toolUseCount = 0; } export async function run( msglog: chat.Msg[], options: chat.Options, toolCall: Call, notelog: notes.Note[], onChunk: Function ) { onChunk( JSON.stringify( toolCall ) + '\n' ); const capabilities = options.model.capabilities; let res: chat.Msg | null = null; switch( toolCall.name.toLowerCase() ) { case 'web': { if( !!capabilities.web ) res = await wget.run( toolCall ); break; } case 'notes': { if( !!capabilities.notes ) res = notes.run( toolCall, notelog, options.uuid ); break; } case 'remind': { if( !!capabilities.remind ) res = await remind.run( toolCall, msglog ); break; } } toolUseCount++; if( !res ) { res = { timestamp: u.getTimestamp(), role: "tool", content: "the specified tool call was invalid. please ensure all the supplied parameters are valid." }; } res.content += "\ndo not attempt another tool call without asking for permission."; onChunk( res.content, true ); msglog.push( res ); let chatRes = await chat.run( msglog, options, false, notelog, onChunk ); msglog.push( chatRes ); if( toolUseCount <= 2 ) { if( chatRes.toolCall ) { return await run( msglog, options, chatRes.toolCall, notelog, onChunk ); } } return chatRes; }