symposium 1.2.10 → 1.2.12
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.
- package/Agent.js +70 -32
- package/Tool.js +4 -0
- package/package.json +1 -1
package/Agent.js
CHANGED
|
@@ -419,24 +419,10 @@ export default class Agent {
|
|
|
419
419
|
}
|
|
420
420
|
|
|
421
421
|
if (functions.length) {
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
return {type: 'response', value: this.afterHandle(thread, completion, f.arguments)};
|
|
422
|
+
if (this.utility && ['function', 'json'].includes(this.utility.type))
|
|
423
|
+
return {type: 'response', value: this.afterHandle(thread, completion, functions[0].arguments)};
|
|
425
424
|
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
thread.addMessage('tool', [
|
|
429
|
-
{
|
|
430
|
-
type: 'function_response',
|
|
431
|
-
content: {name: f.name, response: function_response, id: f.id || undefined},
|
|
432
|
-
},
|
|
433
|
-
], f.name);
|
|
434
|
-
|
|
435
|
-
await this.log('function_response', function_response);
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
await this.afterHandle(thread, completion);
|
|
439
|
-
return {type: 'continue'};
|
|
425
|
+
return this.callFunctions(thread, emitter, completion, functions);
|
|
440
426
|
} else {
|
|
441
427
|
await thread.storeState();
|
|
442
428
|
await this.afterHandle(thread, completion);
|
|
@@ -444,12 +430,50 @@ export default class Agent {
|
|
|
444
430
|
}
|
|
445
431
|
}
|
|
446
432
|
|
|
447
|
-
async
|
|
448
|
-
|
|
433
|
+
async confirmFunctions(thread, completion, functions, emitter) {
|
|
434
|
+
const response = await this.callFunctions(thread, emitter, completion, functions, true);
|
|
435
|
+
if (response.continue)
|
|
436
|
+
return this.execute(thread, emitter);
|
|
449
437
|
}
|
|
450
438
|
|
|
451
|
-
|
|
452
|
-
|
|
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('tool_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'};
|
|
453
477
|
}
|
|
454
478
|
|
|
455
479
|
async getFunctions(parsed = true) {
|
|
@@ -475,28 +499,42 @@ export default class Agent {
|
|
|
475
499
|
return this.functions;
|
|
476
500
|
}
|
|
477
501
|
|
|
478
|
-
async callFunction(thread, function_call, emitter = null) {
|
|
479
|
-
const
|
|
480
|
-
if (!functions.has(function_call.name))
|
|
481
|
-
throw new Error('Unrecognized function ' + function_call.name);
|
|
502
|
+
async callFunction(thread, functions, function_call, emitter = null) {
|
|
503
|
+
const function_definition = functions.get(function_call.name);
|
|
482
504
|
|
|
483
|
-
const func = functions.get(function_call.name);
|
|
484
505
|
await this.log('function_call', function_call);
|
|
485
|
-
|
|
486
506
|
emitter.emit('tool', function_call);
|
|
487
507
|
|
|
488
508
|
try {
|
|
489
|
-
const response = await
|
|
509
|
+
const response = await function_definition.tool.callFunction(thread, function_call.name, function_call.arguments);
|
|
490
510
|
if (emitter)
|
|
491
|
-
emitter.emit('tool_response', {name:
|
|
492
|
-
|
|
511
|
+
emitter.emit('tool_response', {name: function_definition.tool.name, success: true, response});
|
|
512
|
+
|
|
513
|
+
return {
|
|
514
|
+
type: 'response',
|
|
515
|
+
response,
|
|
516
|
+
function: function_call,
|
|
517
|
+
};
|
|
493
518
|
} catch (error) {
|
|
494
519
|
if (emitter)
|
|
495
|
-
emitter.emit('tool_response', {name:
|
|
496
|
-
|
|
520
|
+
emitter.emit('tool_response', {name: function_definition.tool.name, success: false, error: error.message || error});
|
|
521
|
+
|
|
522
|
+
return {
|
|
523
|
+
type: 'response',
|
|
524
|
+
response: {error},
|
|
525
|
+
function: function_call,
|
|
526
|
+
};
|
|
497
527
|
}
|
|
498
528
|
}
|
|
499
529
|
|
|
530
|
+
async afterHandle(thread, completion, value = null) {
|
|
531
|
+
return value;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
getEmitter() {
|
|
535
|
+
return new BufferedEventEmitter();
|
|
536
|
+
}
|
|
537
|
+
|
|
500
538
|
async setModel(thread, label) {
|
|
501
539
|
const model_to_switch = Symposium.getModelByLabel(label);
|
|
502
540
|
if (model_to_switch && model_to_switch.type === 'llm')
|
package/Tool.js
CHANGED