symposium 1.2.11 → 1.2.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/Agent.js +60 -43
  2. package/package.json +1 -1
package/Agent.js CHANGED
@@ -422,26 +422,7 @@ export default class Agent {
422
422
  if (this.utility && ['function', 'json'].includes(this.utility.type))
423
423
  return {type: 'response', value: this.afterHandle(thread, completion, functions[0].arguments)};
424
424
 
425
- const responses = await Promise.all(functions.map(async f => this.callFunction(thread, f, emitter)));
426
-
427
- if (responses.some(r => r.type === 'tool_auth')) {
428
- emitter.emit('tool_auth', responses.filter(r => r.type === 'tool_auth').map(r => ({tool: r.tool, arguments: r.arguments})));
429
- return {type: 'void'};
430
- }
431
-
432
- for (let r of responses) {
433
- thread.addMessage('tool', [
434
- {
435
- type: 'function_response',
436
- content: {name: f.name, response: r, id: f.id || undefined},
437
- },
438
- ], f.name);
439
-
440
- await this.log('function_response', r);
441
- }
442
-
443
- await this.afterHandle(thread, completion);
444
- return {type: 'continue'};
425
+ return this.callFunctions(thread, emitter, completion, functions);
445
426
  } else {
446
427
  await thread.storeState();
447
428
  await this.afterHandle(thread, completion);
@@ -449,12 +430,50 @@ export default class Agent {
449
430
  }
450
431
  }
451
432
 
452
- async afterHandle(thread, completion, value = null) {
453
- return value;
433
+ async confirmFunctions({thread, functions, completion, emitter}) {
434
+ const response = await this.callFunctions(thread, emitter, completion, functions, true);
435
+ if (response.continue)
436
+ return this.execute(thread, emitter);
454
437
  }
455
438
 
456
- getEmitter() {
457
- return new BufferedEventEmitter();
439
+ async callFunctions(thread, emitter, completion, functions_to_call, force_authorize = false) {
440
+ const functions = await this.getFunctions(false);
441
+
442
+ let is_authorized = true;
443
+ for (let f of functions_to_call) {
444
+ if (!functions.has(f.name))
445
+ throw new Error('Unrecognized function ' + f.name);
446
+
447
+ if (!force_authorize && !(await functions.get(f.name).tool.authorize(thread, f.name, f.arguments))) {
448
+ is_authorized = false;
449
+ break;
450
+ }
451
+ }
452
+
453
+ if (!is_authorized) {
454
+ emitter.emit('tools_auth', {thread, functions: functions_to_call, completion, emitter});
455
+ return {type: 'void'};
456
+ }
457
+
458
+ const responses = await Promise.all(functions_to_call.map(async f => this.callFunction(thread, functions, f, emitter)));
459
+
460
+ for (let response of responses) {
461
+ thread.addMessage('tool', [
462
+ {
463
+ type: 'function_response',
464
+ content: {
465
+ name: response.function.name,
466
+ id: response.function.id || undefined,
467
+ response,
468
+ },
469
+ },
470
+ ], response.function.name);
471
+
472
+ await this.log('function_response', response);
473
+ }
474
+
475
+ await this.afterHandle(thread, completion);
476
+ return {type: 'continue'};
458
477
  }
459
478
 
460
479
  async getFunctions(parsed = true) {
@@ -480,44 +499,42 @@ export default class Agent {
480
499
  return this.functions;
481
500
  }
482
501
 
483
- async callFunction(thread, function_call, emitter = null) {
484
- const functions = await this.getFunctions(false);
485
- if (!functions.has(function_call.name))
486
- throw new Error('Unrecognized function ' + function_call.name);
487
-
488
- const func = functions.get(function_call.name);
502
+ async callFunction(thread, functions, function_call, emitter = null) {
503
+ const function_definition = functions.get(function_call.name);
489
504
 
490
505
  await this.log('function_call', function_call);
491
506
  emitter.emit('tool', function_call);
492
507
 
493
- if (!func.tool.authorize(thread, function_call.name, function_call.arguments)) {
494
- return {
495
- type: 'tool_auth',
496
- tool: function_call.name,
497
- arguments: function_call.arguments,
498
- };
499
- }
500
-
501
508
  try {
502
- const response = await func.tool.callFunction(thread, function_call.name, function_call.arguments);
509
+ const response = await function_definition.tool.callFunction(thread, function_call.name, function_call.arguments);
503
510
  if (emitter)
504
- emitter.emit('tool_response', {name: func.tool.name, success: true, response});
511
+ emitter.emit('tool_response', {name: function_definition.tool.name, success: true, response});
505
512
 
506
513
  return {
507
514
  type: 'response',
508
515
  response,
516
+ function: function_call,
509
517
  };
510
518
  } catch (error) {
511
519
  if (emitter)
512
- emitter.emit('tool_response', {name: func.tool.name, success: false, error: error.message || error});
520
+ emitter.emit('tool_response', {name: function_definition.tool.name, success: false, error: error.message || error});
513
521
 
514
522
  return {
515
- type: 'error',
516
- error,
523
+ type: 'response',
524
+ response: {error},
525
+ function: function_call,
517
526
  };
518
527
  }
519
528
  }
520
529
 
530
+ async afterHandle(thread, completion, value = null) {
531
+ return value;
532
+ }
533
+
534
+ getEmitter() {
535
+ return new BufferedEventEmitter();
536
+ }
537
+
521
538
  async setModel(thread, label) {
522
539
  const model_to_switch = Symposium.getModelByLabel(label);
523
540
  if (model_to_switch && model_to_switch.type === 'llm')
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "symposium",
4
- "version": "1.2.11",
4
+ "version": "1.2.13",
5
5
  "description": "Agents",
6
6
  "main": "index.js",
7
7
  "author": "Domenico Giambra",