@orkestrel/agent 0.0.4 → 0.0.6

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.
@@ -2272,10 +2272,13 @@ var Agent = class {
2272
2272
  return this.stream(options).result;
2273
2273
  }
2274
2274
  stream(options) {
2275
- const timeout = this.#timeoutMs === void 0 ? void 0 : (0, _orkestrel_timeout.createTimeout)({ ms: this.#timeoutMs });
2275
+ const timeoutMs = options?.timeout ?? this.#timeoutMs;
2276
+ const timeout = timeoutMs === void 0 ? void 0 : (0, _orkestrel_timeout.createTimeout)({ ms: timeoutMs });
2276
2277
  timeout?.start();
2277
- this.#budget?.start();
2278
- const abort = (0, _orkestrel_abort.createAbort)({ signal: this.#parents(timeout) });
2278
+ const budget = options?.budget ?? this.#budget;
2279
+ budget?.start();
2280
+ const limit = options?.limit ?? this.#limit;
2281
+ const abort = (0, _orkestrel_abort.createAbort)({ signal: this.#parents(timeout, budget, options?.signal) });
2279
2282
  this.#runs.add(abort);
2280
2283
  this.#status = "running";
2281
2284
  this.#emitter.emit("start", this.id);
@@ -2283,11 +2286,12 @@ var Agent = class {
2283
2286
  content: "",
2284
2287
  thinking: void 0,
2285
2288
  usage: void 0,
2286
- partial: false
2289
+ partial: false,
2290
+ exhausted: false
2287
2291
  };
2288
2292
  const channel = new Channel();
2289
2293
  const settled = (0, _orkestrel_workflow.createDeferred)();
2290
- this.#pump(abort, outcome, timeout, channel, settled, options?.think);
2294
+ this.#pump(abort, outcome, timeout, channel, settled, options?.think, options?.schema, limit, budget);
2291
2295
  settled.promise.catch(() => {});
2292
2296
  return {
2293
2297
  events: this.#events(channel, abort),
@@ -2300,10 +2304,10 @@ var Agent = class {
2300
2304
  abort(reason) {
2301
2305
  for (const abort of [...this.#runs]) abort.abort(reason);
2302
2306
  }
2303
- async #pump(abort, outcome, timeout, channel, settled, think) {
2307
+ async #pump(abort, outcome, timeout, channel, settled, think, schema, limit, budget) {
2304
2308
  let failure;
2305
2309
  try {
2306
- for await (const chunk of this.#run(abort, outcome, think)) channel.push(chunk);
2310
+ for await (const chunk of this.#run(abort, outcome, think, schema, limit, budget)) channel.push(chunk);
2307
2311
  } catch (error) {
2308
2312
  failure = { error };
2309
2313
  } finally {
@@ -2314,7 +2318,8 @@ var Agent = class {
2314
2318
  channel.close();
2315
2319
  const result = this.#result(outcome);
2316
2320
  settled.resolve(result);
2317
- if (outcome.partial) this.#emitter.emit("abort", abort.signal.reason);
2321
+ if (outcome.exhausted) this.#emitter.emit("exhaust", limit);
2322
+ else if (outcome.partial) this.#emitter.emit("abort", abort.signal.reason);
2318
2323
  this.#emitter.emit("finish", result);
2319
2324
  } else {
2320
2325
  this.#status = "error";
@@ -2331,51 +2336,83 @@ var Agent = class {
2331
2336
  abort.abort();
2332
2337
  }
2333
2338
  }
2334
- async *#run(abort, outcome, think) {
2339
+ async *#run(abort, outcome, think, schema, limit, budget) {
2335
2340
  const messages = [...this.#context.build(this.#provider.format)];
2336
2341
  const tools = this.#context.tools;
2337
2342
  let content = "";
2338
2343
  let thinking;
2339
2344
  let usage;
2345
+ let pending = false;
2346
+ let broke = false;
2340
2347
  const compaction = { futile: false };
2341
2348
  const compacting = this.#window !== void 0 && this.#context.conversations.active?.summarizable === true;
2342
2349
  if (compacting) {
2343
2350
  this.#window?.clear();
2344
2351
  if (!abort.signal.aborted) await this.#trim(messages, compaction, false);
2345
2352
  }
2346
- for (let turn = 0; turn < this.#limit; turn += 1) {
2353
+ for (let turn = 0; turn < limit; turn += 1) {
2347
2354
  this.#emitter.emit("turn", turn);
2348
2355
  if (turn > 0) try {
2349
2356
  await this.#scheduler?.yield({ signal: abort.signal });
2350
2357
  } catch (error) {
2351
2358
  if (abort.signal.aborted) {
2352
2359
  outcome.partial = true;
2360
+ broke = true;
2353
2361
  break;
2354
2362
  }
2355
2363
  throw error;
2356
2364
  }
2357
2365
  if (abort.signal.aborted) {
2358
2366
  outcome.partial = true;
2367
+ broke = true;
2359
2368
  break;
2360
2369
  }
2361
2370
  const advertised = filterAllowList(this.#context.scope?.tools, tools.definitions(), (definition) => definition.name);
2362
2371
  const definitions = advertised.length > 0 ? advertised : void 0;
2372
+ let charged = 0;
2373
+ let turnContent = "";
2363
2374
  let result;
2364
2375
  try {
2365
- result = yield* this.#provide(messages, abort.signal, definitions, think, (delta) => {
2376
+ result = yield* this.#provide(messages, abort.signal, definitions, think, schema, (delta) => {
2366
2377
  content += delta;
2378
+ turnContent += delta;
2379
+ const est = estimateTokens(turnContent);
2380
+ if (est > charged) {
2381
+ budget?.consume({
2382
+ prompt: 0,
2383
+ completion: est - charged,
2384
+ total: est - charged
2385
+ });
2386
+ charged = est;
2387
+ }
2367
2388
  });
2368
2389
  } catch (error) {
2369
2390
  if (abort.signal.aborted) {
2370
- if (isProviderAbortError(error) && error.partial.thinking !== void 0) thinking = this.#thought(thinking, error.partial.thinking);
2391
+ if (isProviderAbortError(error)) {
2392
+ if (error.partial.thinking !== void 0) thinking = this.#thought(thinking, error.partial.thinking);
2393
+ if (error.partial.usage !== void 0) {
2394
+ const abortUsage = error.partial.usage;
2395
+ budget?.consume({
2396
+ prompt: abortUsage.prompt,
2397
+ completion: Math.max(0, abortUsage.completion - charged),
2398
+ total: Math.max(0, abortUsage.total - charged)
2399
+ });
2400
+ usage = this.#sum(usage, abortUsage);
2401
+ }
2402
+ }
2371
2403
  outcome.partial = true;
2404
+ broke = true;
2372
2405
  break;
2373
2406
  }
2374
2407
  throw error;
2375
2408
  }
2376
2409
  if (result.thinking !== void 0 && result.thinking.length > 0) thinking = this.#thought(thinking, result.thinking);
2377
2410
  if (result.usage !== void 0) {
2378
- this.#budget?.consume(result.usage);
2411
+ budget?.consume({
2412
+ prompt: result.usage.prompt,
2413
+ completion: Math.max(0, result.usage.completion - charged),
2414
+ total: Math.max(0, result.usage.total - charged)
2415
+ });
2379
2416
  usage = this.#sum(usage, result.usage);
2380
2417
  this.#emitter.emit("usage", result.usage);
2381
2418
  yield {
@@ -2408,6 +2445,7 @@ var Agent = class {
2408
2445
  messages.push(toolMessage);
2409
2446
  }
2410
2447
  if (compacting) await this.#trim(messages, compaction, true);
2448
+ pending = true;
2411
2449
  continue;
2412
2450
  }
2413
2451
  messages.push(this.#context.messages.add({
@@ -2415,8 +2453,14 @@ var Agent = class {
2415
2453
  content: result.content
2416
2454
  }));
2417
2455
  content = result.content;
2456
+ pending = false;
2457
+ broke = true;
2418
2458
  break;
2419
2459
  }
2460
+ if (!broke && pending) {
2461
+ outcome.partial = true;
2462
+ outcome.exhausted = !abort.signal.aborted;
2463
+ }
2420
2464
  outcome.content = content;
2421
2465
  outcome.thinking = thinking;
2422
2466
  outcome.usage = usage;
@@ -2476,8 +2520,11 @@ var Agent = class {
2476
2520
  #reason(error) {
2477
2521
  return error instanceof Error ? error.message : String(error);
2478
2522
  }
2479
- async *#provide(messages, signal, definitions, think, onDelta) {
2480
- const generator = this.#provider.stream(messages, signal, definitions, think === void 0 ? void 0 : { think });
2523
+ async *#provide(messages, signal, definitions, think, schema, onDelta) {
2524
+ const options = {};
2525
+ if (think !== void 0) options.think = think;
2526
+ if (schema !== void 0) options.schema = schema;
2527
+ const generator = this.#provider.stream(messages, signal, definitions, Object.keys(options).length > 0 ? options : void 0);
2481
2528
  let next = await generator.next();
2482
2529
  while (!next.done) {
2483
2530
  const delta = next.value;
@@ -2495,11 +2542,12 @@ var Agent = class {
2495
2542
  }
2496
2543
  return next.value;
2497
2544
  }
2498
- #parents(timeout) {
2545
+ #parents(timeout, budget, signal) {
2499
2546
  const signals = [];
2500
2547
  if (this.#signal !== void 0) signals.push(this.#signal);
2548
+ if (signal !== void 0) signals.push(signal);
2501
2549
  if (timeout !== void 0) signals.push(timeout.signal);
2502
- if (this.#budget !== void 0) signals.push(this.#budget.signal);
2550
+ if (budget !== void 0) signals.push(budget.signal);
2503
2551
  if (signals.length === 0) return void 0;
2504
2552
  if (signals.length === 1) return signals[0];
2505
2553
  return AbortSignal.any(signals);