symposium 0.15.2 → 0.15.4
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 +20 -51
- package/package.json +1 -1
package/Agent.js
CHANGED
|
@@ -114,15 +114,17 @@ export default class Agent {
|
|
|
114
114
|
return this.execute(thread);
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
-
async beforeExecute(thread) {
|
|
117
|
+
async beforeExecute(thread, emitter) {
|
|
118
118
|
if (this.options.memory_handler)
|
|
119
119
|
thread = await this.options.memory_handler.handle(thread);
|
|
120
120
|
return thread;
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
async execute(thread, counter = 0, existing_emitter = null) {
|
|
124
|
+
const emitter = existing_emitter || new BufferedEventEmitter();
|
|
125
|
+
|
|
124
126
|
if (counter === 0)
|
|
125
|
-
thread = await this.beforeExecute(thread);
|
|
127
|
+
thread = await this.beforeExecute(thread, emitter);
|
|
126
128
|
|
|
127
129
|
const model = Symposium.getModelByName(thread.state.model);
|
|
128
130
|
|
|
@@ -161,10 +163,7 @@ export default class Agent {
|
|
|
161
163
|
console.error(e.message);
|
|
162
164
|
switch (this.type) {
|
|
163
165
|
case 'chat':
|
|
164
|
-
const emitter = existing_emitter || new BufferedEventEmitter();
|
|
165
166
|
emitter.emit('error', e.message);
|
|
166
|
-
if (!existing_emitter)
|
|
167
|
-
emitter.emit('end');
|
|
168
167
|
return emitter;
|
|
169
168
|
|
|
170
169
|
case 'utility':
|
|
@@ -177,18 +176,11 @@ export default class Agent {
|
|
|
177
176
|
|
|
178
177
|
try {
|
|
179
178
|
thread = await this.afterExecute(thread, completion);
|
|
180
|
-
const
|
|
179
|
+
const response = await this.handleCompletion(thread, completion, emitter);
|
|
181
180
|
|
|
182
181
|
switch (this.type) {
|
|
183
182
|
case 'utility':
|
|
184
|
-
return
|
|
185
|
-
emitter.on('data', data => {
|
|
186
|
-
if (data.type === 'response')
|
|
187
|
-
resolve(data.content);
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
emitter.on('error', error => reject(error));
|
|
191
|
-
});
|
|
183
|
+
return response;
|
|
192
184
|
|
|
193
185
|
case 'chat':
|
|
194
186
|
if (!existing_emitter) {
|
|
@@ -207,7 +199,7 @@ export default class Agent {
|
|
|
207
199
|
console.error(e);
|
|
208
200
|
|
|
209
201
|
if (counter < this.max_retries)
|
|
210
|
-
await this.execute(thread, counter + 1,
|
|
202
|
+
await this.execute(thread, counter + 1, emitter);
|
|
211
203
|
}
|
|
212
204
|
}
|
|
213
205
|
|
|
@@ -306,11 +298,9 @@ export default class Agent {
|
|
|
306
298
|
return message;
|
|
307
299
|
}
|
|
308
300
|
|
|
309
|
-
async handleCompletion(thread, completion,
|
|
301
|
+
async handleCompletion(thread, completion, emitter) {
|
|
310
302
|
const model = Symposium.getModelByName(thread.state.model);
|
|
311
303
|
|
|
312
|
-
const emitter = existing_emitter || new BufferedEventEmitter();
|
|
313
|
-
|
|
314
304
|
const functions = [];
|
|
315
305
|
for (let message of completion) {
|
|
316
306
|
thread.addDirectMessage(message);
|
|
@@ -320,20 +310,13 @@ export default class Agent {
|
|
|
320
310
|
switch (m.type) {
|
|
321
311
|
case 'text':
|
|
322
312
|
if (this.type === 'utility') {
|
|
323
|
-
let response = null;
|
|
324
313
|
if (this.utility.type === 'text')
|
|
325
|
-
|
|
314
|
+
return this.afterHandle(thread, completion, 'return', m.content);
|
|
326
315
|
if (this.utility.type === 'json' && model.supports_structured_output)
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
if (response) {
|
|
330
|
-
emitter.emit('data', {type: 'response', content: response});
|
|
331
|
-
emitter.emit('end');
|
|
332
|
-
return emitter;
|
|
333
|
-
}
|
|
316
|
+
return this.afterHandle(thread, completion, 'return', JSON.parse(m.content));
|
|
334
317
|
}
|
|
335
318
|
|
|
336
|
-
emitter.emit('
|
|
319
|
+
emitter.emit('output', m.content);
|
|
337
320
|
break;
|
|
338
321
|
|
|
339
322
|
case 'function':
|
|
@@ -344,13 +327,10 @@ export default class Agent {
|
|
|
344
327
|
}
|
|
345
328
|
}
|
|
346
329
|
|
|
347
|
-
let response;
|
|
348
330
|
if (functions.length) {
|
|
349
331
|
for (let f of functions) {
|
|
350
|
-
if (this.utility && ['function', 'json'].includes(this.utility.type))
|
|
351
|
-
|
|
352
|
-
break;
|
|
353
|
-
}
|
|
332
|
+
if (this.utility && ['function', 'json'].includes(this.utility.type))
|
|
333
|
+
return this.afterHandle(thread, completion, 'return', f.arguments);
|
|
354
334
|
|
|
355
335
|
const function_response = await this.callFunction(thread, f, emitter);
|
|
356
336
|
|
|
@@ -364,23 +344,11 @@ export default class Agent {
|
|
|
364
344
|
await this.log('function_response', function_response);
|
|
365
345
|
}
|
|
366
346
|
|
|
367
|
-
|
|
368
|
-
response = this.afterHandle(thread, completion, 'continue');
|
|
347
|
+
return this.afterHandle(thread, completion, 'continue');
|
|
369
348
|
} else {
|
|
370
349
|
await thread.storeState();
|
|
371
|
-
|
|
350
|
+
return this.afterHandle(thread, completion, 'void');
|
|
372
351
|
}
|
|
373
|
-
|
|
374
|
-
response
|
|
375
|
-
.then(content => {
|
|
376
|
-
emitter.emit('data', {type: 'response', content});
|
|
377
|
-
emitter.emit('end');
|
|
378
|
-
})
|
|
379
|
-
.catch(error => {
|
|
380
|
-
emitter.emit('error', error);
|
|
381
|
-
});
|
|
382
|
-
|
|
383
|
-
return emitter;
|
|
384
352
|
}
|
|
385
353
|
|
|
386
354
|
async afterHandle(thread, completion, type, value = null) {
|
|
@@ -420,16 +388,16 @@ export default class Agent {
|
|
|
420
388
|
|
|
421
389
|
const func = functions.get(function_call.name);
|
|
422
390
|
const partialOutput = func.partialOutput ? ((typeof func.partialOutput) === 'text' ? func.partialOutput : func.partialOutput.call(this, function_call.arguments)) : 'Uso lo strumento ' + function_call.name + '...';
|
|
423
|
-
emitter.emit('
|
|
391
|
+
emitter.emit('partial', partialOutput);
|
|
424
392
|
|
|
425
393
|
await this.log('function_call', function_call);
|
|
426
394
|
|
|
427
395
|
try {
|
|
428
396
|
const response = await func.tool.callFunction(thread, function_call.name, function_call.arguments);
|
|
429
|
-
emitter.emit('
|
|
397
|
+
emitter.emit('partial', 'Risposta ricevuta da ' + func.tool.name);
|
|
430
398
|
return response;
|
|
431
399
|
} catch (error) {
|
|
432
|
-
emitter.emit('
|
|
400
|
+
emitter.emit('partial', 'Ricevuto errore da ' + func.tool.name);
|
|
433
401
|
return {error};
|
|
434
402
|
}
|
|
435
403
|
}
|
|
@@ -451,6 +419,7 @@ export default class Agent {
|
|
|
451
419
|
return [this.name];
|
|
452
420
|
}
|
|
453
421
|
|
|
422
|
+
// Currently specific for OpenAI Realtime API
|
|
454
423
|
async createRealtimeSession(thread_id = null, options = {}) {
|
|
455
424
|
options = {
|
|
456
425
|
include_thread: true,
|
|
@@ -458,7 +427,7 @@ export default class Agent {
|
|
|
458
427
|
...options,
|
|
459
428
|
};
|
|
460
429
|
|
|
461
|
-
//
|
|
430
|
+
// If a thread is passed, it is used, otherwise a temporary thread is created
|
|
462
431
|
const thread = await this.getThread(thread_id || uuid());
|
|
463
432
|
|
|
464
433
|
const system_message = [], conversation = [];
|