diff options
| author | aura <nw@moneybot.cc> | 2026-02-17 22:39:42 +0100 |
|---|---|---|
| committer | aura <nw@moneybot.cc> | 2026-02-17 22:39:42 +0100 |
| commit | 636b0323075225c584b62719ed51e75521bb7ffb (patch) | |
| tree | 61b02271b6d0695a4beffc23fb6eb062a7da22c3 /backend/instance/tools.ts | |
push source
Diffstat (limited to 'backend/instance/tools.ts')
| -rw-r--r-- | backend/instance/tools.ts | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/backend/instance/tools.ts b/backend/instance/tools.ts new file mode 100644 index 0000000..0baae71 --- /dev/null +++ b/backend/instance/tools.ts @@ -0,0 +1,48 @@ +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; +} + |
