symposium 0.15.2 → 0.15.3
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 +16 -44
- 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,7 +163,6 @@ 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
167
|
if (!existing_emitter)
|
|
167
168
|
emitter.emit('end');
|
|
@@ -177,18 +178,13 @@ export default class Agent {
|
|
|
177
178
|
|
|
178
179
|
try {
|
|
179
180
|
thread = await this.afterExecute(thread, completion);
|
|
180
|
-
const
|
|
181
|
+
const response = await this.handleCompletion(thread, completion, emitter);
|
|
181
182
|
|
|
182
183
|
switch (this.type) {
|
|
183
184
|
case 'utility':
|
|
184
|
-
|
|
185
|
-
emitter.
|
|
186
|
-
|
|
187
|
-
resolve(data.content);
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
emitter.on('error', error => reject(error));
|
|
191
|
-
});
|
|
185
|
+
if (!existing_emitter)
|
|
186
|
+
emitter.emit('end');
|
|
187
|
+
return response;
|
|
192
188
|
|
|
193
189
|
case 'chat':
|
|
194
190
|
if (!existing_emitter) {
|
|
@@ -207,7 +203,7 @@ export default class Agent {
|
|
|
207
203
|
console.error(e);
|
|
208
204
|
|
|
209
205
|
if (counter < this.max_retries)
|
|
210
|
-
await this.execute(thread, counter + 1,
|
|
206
|
+
await this.execute(thread, counter + 1, emitter);
|
|
211
207
|
}
|
|
212
208
|
}
|
|
213
209
|
|
|
@@ -306,11 +302,9 @@ export default class Agent {
|
|
|
306
302
|
return message;
|
|
307
303
|
}
|
|
308
304
|
|
|
309
|
-
async handleCompletion(thread, completion,
|
|
305
|
+
async handleCompletion(thread, completion, emitter) {
|
|
310
306
|
const model = Symposium.getModelByName(thread.state.model);
|
|
311
307
|
|
|
312
|
-
const emitter = existing_emitter || new BufferedEventEmitter();
|
|
313
|
-
|
|
314
308
|
const functions = [];
|
|
315
309
|
for (let message of completion) {
|
|
316
310
|
thread.addDirectMessage(message);
|
|
@@ -320,17 +314,10 @@ export default class Agent {
|
|
|
320
314
|
switch (m.type) {
|
|
321
315
|
case 'text':
|
|
322
316
|
if (this.type === 'utility') {
|
|
323
|
-
let response = null;
|
|
324
317
|
if (this.utility.type === 'text')
|
|
325
|
-
|
|
318
|
+
return this.afterHandle(thread, completion, 'return', m.content);
|
|
326
319
|
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
|
-
}
|
|
320
|
+
return this.afterHandle(thread, completion, 'return', JSON.parse(m.content));
|
|
334
321
|
}
|
|
335
322
|
|
|
336
323
|
emitter.emit('data', {type: 'output', content: m.content});
|
|
@@ -344,13 +331,10 @@ export default class Agent {
|
|
|
344
331
|
}
|
|
345
332
|
}
|
|
346
333
|
|
|
347
|
-
let response;
|
|
348
334
|
if (functions.length) {
|
|
349
335
|
for (let f of functions) {
|
|
350
|
-
if (this.utility && ['function', 'json'].includes(this.utility.type))
|
|
351
|
-
|
|
352
|
-
break;
|
|
353
|
-
}
|
|
336
|
+
if (this.utility && ['function', 'json'].includes(this.utility.type))
|
|
337
|
+
return this.afterHandle(thread, completion, 'return', f.arguments);
|
|
354
338
|
|
|
355
339
|
const function_response = await this.callFunction(thread, f, emitter);
|
|
356
340
|
|
|
@@ -364,23 +348,11 @@ export default class Agent {
|
|
|
364
348
|
await this.log('function_response', function_response);
|
|
365
349
|
}
|
|
366
350
|
|
|
367
|
-
|
|
368
|
-
response = this.afterHandle(thread, completion, 'continue');
|
|
351
|
+
return this.afterHandle(thread, completion, 'continue');
|
|
369
352
|
} else {
|
|
370
353
|
await thread.storeState();
|
|
371
|
-
|
|
354
|
+
return this.afterHandle(thread, completion, 'void');
|
|
372
355
|
}
|
|
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
356
|
}
|
|
385
357
|
|
|
386
358
|
async afterHandle(thread, completion, type, value = null) {
|