@agent-assistant/core 0.2.19 → 0.2.21
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/dist/core.js +30 -13
- package/package.json +1 -1
package/dist/core.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
const DEFAULT_HANDLER_TIMEOUT_MS =
|
|
1
|
+
const DEFAULT_HANDLER_TIMEOUT_MS = 5 * 60_000;
|
|
2
2
|
const DEFAULT_MAX_CONCURRENT_HANDLERS = 10;
|
|
3
|
-
const
|
|
3
|
+
const DRAIN_MARGIN_MS = 5_000;
|
|
4
4
|
export class AssistantDefinitionError extends Error {
|
|
5
5
|
constructor(message) {
|
|
6
6
|
super(message);
|
|
@@ -206,14 +206,23 @@ export function createAssistant(definition, adapters) {
|
|
|
206
206
|
return;
|
|
207
207
|
}
|
|
208
208
|
drainWaiter ??= createDeferred();
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
209
|
+
const drainTimeoutMs = constraints.handlerTimeoutMs + DRAIN_MARGIN_MS;
|
|
210
|
+
let timeoutHandle;
|
|
211
|
+
try {
|
|
212
|
+
await Promise.race([
|
|
213
|
+
drainWaiter.promise,
|
|
214
|
+
new Promise((_, reject) => {
|
|
215
|
+
timeoutHandle = setTimeout(() => {
|
|
216
|
+
reject(new Error(`Timed out waiting ${drainTimeoutMs}ms for in-flight handlers to drain`));
|
|
217
|
+
}, drainTimeoutMs);
|
|
218
|
+
}),
|
|
219
|
+
]);
|
|
220
|
+
}
|
|
221
|
+
finally {
|
|
222
|
+
if (timeoutHandle) {
|
|
223
|
+
clearTimeout(timeoutHandle);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
217
226
|
}
|
|
218
227
|
function runNext() {
|
|
219
228
|
while (lifecycleState === 'started' &&
|
|
@@ -227,6 +236,14 @@ export function createAssistant(definition, adapters) {
|
|
|
227
236
|
void executeDispatch(nextDispatch);
|
|
228
237
|
}
|
|
229
238
|
}
|
|
239
|
+
function reportError(error, message) {
|
|
240
|
+
const userHandler = frozenDefinition.hooks?.onError;
|
|
241
|
+
if (typeof userHandler === 'function') {
|
|
242
|
+
userHandler(error, message);
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
console.error(`[${frozenDefinition.id ?? 'agent'}] unhandled capability handler error (capability=${message.capability}, id=${message.id}):`, error);
|
|
246
|
+
}
|
|
230
247
|
async function executeDispatch(dispatchJob) {
|
|
231
248
|
const { message, resolve, reject } = dispatchJob;
|
|
232
249
|
try {
|
|
@@ -237,7 +254,7 @@ export function createAssistant(definition, adapters) {
|
|
|
237
254
|
}
|
|
238
255
|
const handler = capabilityMap.get(message.capability);
|
|
239
256
|
if (!handler) {
|
|
240
|
-
|
|
257
|
+
reportError(new Error(`No capability registered for '${message.capability}'`), message);
|
|
241
258
|
resolve();
|
|
242
259
|
return;
|
|
243
260
|
}
|
|
@@ -253,13 +270,13 @@ export function createAssistant(definition, adapters) {
|
|
|
253
270
|
await withTimeout(handler, message, context, constraints.handlerTimeoutMs);
|
|
254
271
|
}
|
|
255
272
|
catch (error) {
|
|
256
|
-
|
|
273
|
+
reportError(error instanceof Error ? error : new Error(String(error)), message);
|
|
257
274
|
}
|
|
258
275
|
resolve();
|
|
259
276
|
}
|
|
260
277
|
catch (error) {
|
|
261
278
|
const wrappedError = error instanceof Error ? error : new Error(String(error));
|
|
262
|
-
|
|
279
|
+
reportError(wrappedError, message);
|
|
263
280
|
reject(wrappedError);
|
|
264
281
|
}
|
|
265
282
|
finally {
|