summaryrefslogtreecommitdiff
path: root/backend/instance/tools.ts
blob: 0baae718fbf8cff1dd6675f06e69d4c4bc6852c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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;
}