@orkestrel/agent 0.0.8 → 0.0.9

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.
@@ -277,7 +277,7 @@ var WorkspaceError = class extends Error {
277
277
  super(message);
278
278
  this.name = "WorkspaceError";
279
279
  this.code = code;
280
- this.context = context;
280
+ if (context !== void 0) this.context = context;
281
281
  }
282
282
  };
283
283
  /**
@@ -397,8 +397,8 @@ var Conversation = class {
397
397
  constructor(options, seed) {
398
398
  this.#id = seed?.id ?? options?.id ?? crypto.randomUUID();
399
399
  this.#emitter = new Emitter({
400
- on: options?.on,
401
- error: options?.error
400
+ ...options?.on === void 0 ? {} : { on: options.on },
401
+ ...options?.error === void 0 ? {} : { error: options.error }
402
402
  });
403
403
  this.#summarize = options?.summarize;
404
404
  this.#keep = options?.keep ?? 0;
@@ -608,10 +608,11 @@ var ConversationManager = class {
608
608
  }
609
609
  add(input) {
610
610
  const sections = input?.sections ?? this.#sections;
611
+ const summarize = input?.summarize ?? this.#summarize;
611
612
  const conversation = new Conversation({
612
613
  ...input?.id === void 0 ? {} : { id: input.id },
613
614
  ...input?.on === void 0 ? {} : { on: input.on },
614
- summarize: input?.summarize ?? this.#summarize,
615
+ ...summarize === void 0 ? {} : { summarize },
615
616
  keep: input?.keep ?? this.#keep,
616
617
  ...sections === void 0 ? {} : { sections }
617
618
  }, input?.snapshot);
@@ -817,6 +818,38 @@ async function settleAgentJob(agent, allowPartial) {
817
818
  return result;
818
819
  }
819
820
  /**
821
+ * Handle one queued agent job by rehydrating it through a registry with the queue
822
+ * attempt's signal, then applying the shared partial-result policy.
823
+ *
824
+ * @param registry - The registry that rehydrates the serializable job
825
+ * @param allowPartial - Whether a partial result resolves instead of throwing
826
+ * @param input - The serializable agent job
827
+ * @param execution - The queue attempt whose signal bounds the agent
828
+ * @returns The settled agent result
829
+ */
830
+ function handleAgentQueueJob(registry, allowPartial, input, execution) {
831
+ return settleAgentJob(registry.build(input, execution.signal), allowPartial);
832
+ }
833
+ /**
834
+ * Handle one runner agent job by fanning out its declared children, rehydrating the
835
+ * parent through a registry with the controller signal, and applying the shared
836
+ * partial-result policy.
837
+ *
838
+ * @remarks
839
+ * Children are fired and tracked through the runner controller without awaiting them
840
+ * inline, preserving bounded-runner progress.
841
+ *
842
+ * @param registry - The registry that rehydrates serializable jobs
843
+ * @param allowPartial - Whether a partial result resolves instead of throwing
844
+ * @param controller - The runner controller for this parent job
845
+ * @returns The settled parent agent result
846
+ */
847
+ function handleAgentRunnerJob(registry, allowPartial, controller) {
848
+ const children = controller.input.children;
849
+ if (children !== void 0) for (const child of children) controller.spawn(child);
850
+ return settleAgentJob(registry.build(controller.input, controller.signal), allowPartial);
851
+ }
852
+ /**
820
853
  * Whether a {@link FileContent} is the TEXT arm — the narrowing guard for the text-vs-binary
821
854
  * union (AGENTS §14: narrow an untyped arm via a guard, never an `as`).
822
855
  *
@@ -1414,6 +1447,15 @@ function escapeRegExp(value) {
1414
1447
  return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1415
1448
  }
1416
1449
  /**
1450
+ * Sanitize one reported token count into a safe non-negative integer.
1451
+ *
1452
+ * @param value - The token count to sanitize
1453
+ * @returns The floored count, or `0` when the value is non-finite or non-positive
1454
+ */
1455
+ function sanitizeToken(value) {
1456
+ return Number.isFinite(value) && value > 0 ? Math.floor(value) : 0;
1457
+ }
1458
+ /**
1417
1459
  * Sanitize a {@link TokenUsage} into safe, non-negative integers — the guard an agent's
1418
1460
  * abort-usage path applies to a provider's partial usage before it is charged against a
1419
1461
  * budget or folded into the run total.
@@ -1432,11 +1474,10 @@ function escapeRegExp(value) {
1432
1474
  * ```
1433
1475
  */
1434
1476
  function sanitizeUsage(usage) {
1435
- const clean = (value) => Number.isFinite(value) && value > 0 ? Math.floor(value) : 0;
1436
1477
  return {
1437
- prompt: clean(usage.prompt),
1438
- completion: clean(usage.completion),
1439
- total: clean(usage.total)
1478
+ prompt: sanitizeToken(usage.prompt),
1479
+ completion: sanitizeToken(usage.completion),
1480
+ total: sanitizeToken(usage.total)
1440
1481
  };
1441
1482
  }
1442
1483
  //#endregion
@@ -1520,8 +1561,8 @@ var InstructionManager = class {
1520
1561
  #format;
1521
1562
  constructor(options) {
1522
1563
  this.#emitter = new Emitter({
1523
- on: options?.on,
1524
- error: options?.error
1564
+ ...options?.on === void 0 ? {} : { on: options.on },
1565
+ ...options?.error === void 0 ? {} : { error: options.error }
1525
1566
  });
1526
1567
  this.#format = options?.format;
1527
1568
  }
@@ -1723,8 +1764,8 @@ var Workspace = class {
1723
1764
  constructor(options, seed) {
1724
1765
  this.#id = options?.id ?? crypto.randomUUID();
1725
1766
  this.#emitter = new Emitter({
1726
- on: options?.on,
1727
- error: options?.error
1767
+ ...options?.on === void 0 ? {} : { on: options.on },
1768
+ ...options?.error === void 0 ? {} : { error: options.error }
1728
1769
  });
1729
1770
  if (seed) for (const [path, file] of seed) this.#files.set(path, file);
1730
1771
  }
@@ -2017,10 +2058,12 @@ var WorkspaceManager = class {
2017
2058
  return [...this.#workspaces.values()];
2018
2059
  }
2019
2060
  add(input) {
2061
+ const on = input?.on ?? this.#on;
2062
+ const error = input?.error ?? this.#error;
2020
2063
  const workspace = new Workspace({
2021
2064
  ...input?.id === void 0 ? {} : { id: input.id },
2022
- on: input?.on ?? this.#on,
2023
- error: input?.error ?? this.#error
2065
+ ...on === void 0 ? {} : { on },
2066
+ ...error === void 0 ? {} : { error }
2024
2067
  }, input?.seed);
2025
2068
  this.#workspaces.set(workspace.id, workspace);
2026
2069
  if (this.#active === void 0) this.#active = workspace.id;
@@ -2372,12 +2415,12 @@ var Agent = class {
2372
2415
  constructor(provider, options) {
2373
2416
  this.#provider = provider;
2374
2417
  this.#context = new AgentContext({
2375
- system: options?.system,
2376
- tools: options?.tools,
2377
- instructions: options?.instructions,
2378
- workspaces: options?.workspaces,
2379
- scope: options?.scope,
2380
- conversations: options?.conversations
2418
+ ...options?.system === void 0 ? {} : { system: options.system },
2419
+ ...options?.tools === void 0 ? {} : { tools: options.tools },
2420
+ ...options?.instructions === void 0 ? {} : { instructions: options.instructions },
2421
+ ...options?.workspaces === void 0 ? {} : { workspaces: options.workspaces },
2422
+ ...options?.scope === void 0 ? {} : { scope: options.scope },
2423
+ ...options?.conversations === void 0 ? {} : { conversations: options.conversations }
2381
2424
  });
2382
2425
  this.#limit = options?.limit ?? 10;
2383
2426
  this.#timeoutMs = options?.timeout;
@@ -2388,8 +2431,8 @@ var Agent = class {
2388
2431
  this.#window = options?.window;
2389
2432
  this.#strict = options?.strict ?? false;
2390
2433
  this.#emitter = new Emitter({
2391
- on: options?.on,
2392
- error: options?.error
2434
+ ...options?.on === void 0 ? {} : { on: options.on },
2435
+ ...options?.error === void 0 ? {} : { error: options.error }
2393
2436
  });
2394
2437
  }
2395
2438
  get emitter() {
@@ -2412,42 +2455,42 @@ var Agent = class {
2412
2455
  const budget = options?.budget ?? this.#budget;
2413
2456
  budget?.start();
2414
2457
  const limit = options?.limit ?? this.#limit;
2415
- const abort = createAbort({ signal: this.#parents(timeout, budget, options?.signal) });
2458
+ const signal = this.#parents(timeout, budget, options?.signal);
2459
+ const abort = createAbort(signal === void 0 ? {} : { signal });
2416
2460
  this.#runs.add(abort);
2417
2461
  this.#status = "running";
2418
2462
  this.#emitter.emit("start", this.id);
2419
- const outcome = {
2463
+ const state = { outcome: {
2420
2464
  content: "",
2421
2465
  thinking: void 0,
2422
2466
  usage: void 0,
2423
2467
  partial: false,
2424
2468
  exhausted: false
2425
- };
2469
+ } };
2426
2470
  const channel = new Channel();
2427
2471
  const settled = createDeferred();
2428
- this.#pump(abort, outcome, timeout, channel, settled, options?.think, options?.schema, limit, budget);
2472
+ this.#pump(abort, state, timeout, channel, settled, options?.think, options?.schema, limit, budget);
2429
2473
  settled.promise.catch(() => {});
2430
2474
  return {
2431
2475
  events: this.#events(channel, abort),
2432
2476
  result: settled.promise,
2433
- abort: (reason) => {
2434
- abort.abort(reason);
2435
- }
2477
+ abort: abort.abort.bind(abort)
2436
2478
  };
2437
2479
  }
2438
2480
  abort(reason) {
2439
2481
  for (const abort of [...this.#runs]) abort.abort(reason);
2440
2482
  }
2441
- async #pump(abort, outcome, timeout, channel, settled, think, schema, limit, budget) {
2483
+ async #pump(abort, state, timeout, channel, settled, think, schema, limit, budget) {
2442
2484
  let failure;
2443
2485
  try {
2444
- for await (const chunk of this.#run(abort, outcome, think, schema, limit, budget)) channel.push(chunk);
2486
+ for await (const chunk of this.#run(abort, state, think, schema, limit, budget)) channel.push(chunk);
2445
2487
  } catch (error) {
2446
2488
  failure = { error };
2447
2489
  } finally {
2448
2490
  timeout?.clear();
2449
2491
  this.#runs.delete(abort);
2450
2492
  if (failure === void 0) {
2493
+ const outcome = state.outcome;
2451
2494
  this.#status = "done";
2452
2495
  channel.close();
2453
2496
  const result = this.#result(outcome);
@@ -2470,7 +2513,7 @@ var Agent = class {
2470
2513
  abort.abort();
2471
2514
  }
2472
2515
  }
2473
- async *#run(abort, outcome, think, schema, limit, budget) {
2516
+ async *#run(abort, state, think, schema, limit, budget) {
2474
2517
  const messages = [...this.#context.build(this.#provider.format)];
2475
2518
  const tools = this.#context.tools;
2476
2519
  let content = "";
@@ -2478,7 +2521,7 @@ var Agent = class {
2478
2521
  let usage;
2479
2522
  let pending = false;
2480
2523
  let broke = false;
2481
- const compaction = { futile: false };
2524
+ const compaction = { state: { futile: false } };
2482
2525
  const compacting = this.#window !== void 0 && this.#context.conversations.active?.summarizable === true;
2483
2526
  if (compacting) {
2484
2527
  this.#window?.clear();
@@ -2490,14 +2533,20 @@ var Agent = class {
2490
2533
  await this.#scheduler?.yield({ signal: abort.signal });
2491
2534
  } catch (error) {
2492
2535
  if (abort.signal.aborted) {
2493
- outcome.partial = true;
2536
+ state.outcome = {
2537
+ ...state.outcome,
2538
+ partial: true
2539
+ };
2494
2540
  broke = true;
2495
2541
  break;
2496
2542
  }
2497
2543
  throw error;
2498
2544
  }
2499
2545
  if (abort.signal.aborted) {
2500
- outcome.partial = true;
2546
+ state.outcome = {
2547
+ ...state.outcome,
2548
+ partial: true
2549
+ };
2501
2550
  broke = true;
2502
2551
  break;
2503
2552
  }
@@ -2534,7 +2583,10 @@ var Agent = class {
2534
2583
  usage = this.#sum(usage, abortUsage);
2535
2584
  }
2536
2585
  }
2537
- outcome.partial = true;
2586
+ state.outcome = {
2587
+ ...state.outcome,
2588
+ partial: true
2589
+ };
2538
2590
  broke = true;
2539
2591
  break;
2540
2592
  }
@@ -2592,17 +2644,21 @@ var Agent = class {
2592
2644
  broke = true;
2593
2645
  break;
2594
2646
  }
2595
- if (!broke && pending) {
2596
- outcome.partial = true;
2597
- outcome.exhausted = !abort.signal.aborted;
2598
- }
2599
- outcome.content = content;
2600
- outcome.thinking = thinking;
2601
- outcome.usage = usage;
2647
+ if (!broke && pending) state.outcome = {
2648
+ ...state.outcome,
2649
+ partial: true,
2650
+ exhausted: !abort.signal.aborted
2651
+ };
2652
+ state.outcome = {
2653
+ ...state.outcome,
2654
+ content,
2655
+ thinking,
2656
+ usage
2657
+ };
2602
2658
  }
2603
2659
  async #trim(messages, compaction, latchFutile) {
2604
2660
  const conversation = this.#context.conversations.active;
2605
- if (this.#window === void 0 || conversation?.summarizable !== true || compaction.futile) return;
2661
+ if (this.#window === void 0 || conversation?.summarizable !== true || compaction.state.futile) return;
2606
2662
  this.#window.clear();
2607
2663
  this.#window.consume(messages);
2608
2664
  if (!this.#window.exhausted) return;
@@ -2615,7 +2671,7 @@ var Agent = class {
2615
2671
  return;
2616
2672
  }
2617
2673
  if (section === void 0) {
2618
- if (latchFutile) compaction.futile = true;
2674
+ if (latchFutile) compaction.state = { futile: true };
2619
2675
  return;
2620
2676
  }
2621
2677
  messages.splice(0, messages.length, ...this.#context.build(this.#provider.format));
@@ -2777,16 +2833,17 @@ var AgentRegistry = class {
2777
2833
  return agent;
2778
2834
  }
2779
2835
  #options(input, signal) {
2836
+ const budget = this.#budget(input.budget);
2780
2837
  return {
2781
- system: input.system,
2782
2838
  tools: this.#manager(input.tools),
2783
- limit: input.limit,
2784
- timeout: input.timeout,
2785
- budget: this.#budget(input.budget),
2786
- authority: input.authority === void 0 ? void 0 : this.authority(input.authority),
2787
- scheduler: input.scheduler === void 0 ? void 0 : this.scheduler(input.scheduler),
2788
- conversations: this.#store === void 0 ? void 0 : new ConversationManager({ store: this.#store }),
2789
- signal
2839
+ ...input.system === void 0 ? {} : { system: input.system },
2840
+ ...input.limit === void 0 ? {} : { limit: input.limit },
2841
+ ...input.timeout === void 0 ? {} : { timeout: input.timeout },
2842
+ ...budget === void 0 ? {} : { budget },
2843
+ ...input.authority === void 0 ? {} : { authority: this.authority(input.authority) },
2844
+ ...input.scheduler === void 0 ? {} : { scheduler: this.scheduler(input.scheduler) },
2845
+ ...this.#store === void 0 ? {} : { conversations: new ConversationManager({ store: this.#store }) },
2846
+ ...signal === void 0 ? {} : { signal }
2790
2847
  };
2791
2848
  }
2792
2849
  #manager(names) {
@@ -2852,7 +2909,7 @@ var Authority = class {
2852
2909
  for (const rule of this.#rules) if (rule.match(context)) return {
2853
2910
  zone: rule.zone,
2854
2911
  allowed: rule.allowed ?? true,
2855
- reason: rule.reason
2912
+ ...rule.reason === void 0 ? {} : { reason: rule.reason }
2856
2913
  };
2857
2914
  return this.#fallback;
2858
2915
  }
@@ -3031,16 +3088,19 @@ var Scope = class Scope {
3031
3088
  files;
3032
3089
  constructor(input) {
3033
3090
  this.name = input.name;
3034
- this.instructions = input.instructions === void 0 ? void 0 : [...input.instructions];
3035
- this.tools = input.tools === void 0 ? void 0 : [...input.tools];
3036
- this.files = input.files === void 0 ? void 0 : [...input.files];
3091
+ if (input.instructions !== void 0) this.instructions = [...input.instructions];
3092
+ if (input.tools !== void 0) this.tools = [...input.tools];
3093
+ if (input.files !== void 0) this.files = [...input.files];
3037
3094
  }
3038
3095
  narrow(config) {
3096
+ const instructions = Scope.#intersect(this.instructions, config.instructions);
3097
+ const tools = Scope.#intersect(this.tools, config.tools);
3098
+ const files = Scope.#intersect(this.files, config.files);
3039
3099
  return new Scope({
3040
3100
  name: this.name,
3041
- instructions: Scope.#intersect(this.instructions, config.instructions),
3042
- tools: Scope.#intersect(this.tools, config.tools),
3043
- files: Scope.#intersect(this.files, config.files)
3101
+ ...instructions === void 0 ? {} : { instructions },
3102
+ ...tools === void 0 ? {} : { tools },
3103
+ ...files === void 0 ? {} : { files }
3044
3104
  });
3045
3105
  }
3046
3106
  static #intersect(parent, child) {
@@ -3084,8 +3144,8 @@ var ScopeManager = class {
3084
3144
  #emitter;
3085
3145
  constructor(on, error) {
3086
3146
  this.#emitter = new Emitter({
3087
- on,
3088
- error
3147
+ ...on === void 0 ? {} : { on },
3148
+ ...error === void 0 ? {} : { error }
3089
3149
  });
3090
3150
  }
3091
3151
  get emitter() {
@@ -3275,9 +3335,9 @@ var Tool = class {
3275
3335
  #execute;
3276
3336
  constructor(options) {
3277
3337
  this.name = options.name;
3278
- this.description = options.description;
3279
- this.summary = options.summary;
3280
- this.parameters = options.parameters;
3338
+ if (options.description !== void 0) this.description = options.description;
3339
+ if (options.summary !== void 0) this.summary = options.summary;
3340
+ if (options.parameters !== void 0) this.parameters = options.parameters;
3281
3341
  this.#execute = options.execute;
3282
3342
  }
3283
3343
  execute(args) {
@@ -3935,11 +3995,11 @@ function createAgentRegistry(options) {
3935
3995
  function createAgentQueue(options) {
3936
3996
  const { registry, allowPartial = false, concurrency, retries, timeout, store } = options;
3937
3997
  return createQueue({
3938
- concurrency,
3939
- retries,
3940
- timeout,
3941
- store,
3942
- handler: (input, execution) => settleAgentJob(registry.build(input, execution.signal), allowPartial)
3998
+ ...concurrency === void 0 ? {} : { concurrency },
3999
+ ...retries === void 0 ? {} : { retries },
4000
+ ...timeout === void 0 ? {} : { timeout },
4001
+ ...store === void 0 ? {} : { store },
4002
+ handler: handleAgentQueueJob.bind(void 0, registry, allowPartial)
3943
4003
  });
3944
4004
  }
3945
4005
  /**
@@ -3981,14 +4041,10 @@ function createAgentQueue(options) {
3981
4041
  function createAgentRunner(options) {
3982
4042
  const { registry, allowPartial = false, concurrency, retries, timeout } = options;
3983
4043
  return createRunner({
3984
- concurrency,
3985
- retries,
3986
- timeout,
3987
- handler: (controller) => {
3988
- const children = controller.input.children;
3989
- if (children !== void 0) for (const child of children) controller.spawn(child);
3990
- return settleAgentJob(registry.build(controller.input, controller.signal), allowPartial);
3991
- }
4044
+ ...concurrency === void 0 ? {} : { concurrency },
4045
+ ...retries === void 0 ? {} : { retries },
4046
+ ...timeout === void 0 ? {} : { timeout },
4047
+ handler: handleAgentRunnerJob.bind(void 0, registry, allowPartial)
3992
4048
  });
3993
4049
  }
3994
4050
  /**
@@ -4198,6 +4254,6 @@ function createWorkspaceManager(options) {
4198
4254
  return new WorkspaceManager(options);
4199
4255
  }
4200
4256
  //#endregion
4201
- export { Agent, AgentContext, AgentError, AgentJobError, AgentRegistry, Authority, CONVERSATION_RECAP_PREFIX, Channel, Conversation, ConversationError, ConversationManager, DEFAULT_AGENT_LIMIT, DEFAULT_AUTHORITY_ZONE, DEFAULT_CONVERSATION_KEEP, DatabaseConversationStore, DatabaseWorkspaceStore, EXTENSION_TO_LANGUAGE, IMAGE_TOKEN_ESTIMATE, Instruction, InstructionManager, MESSAGE_TOKEN_OVERHEAD, MemoryConversationStore, MemoryWorkspaceStore, ProviderAbortError, Scope, ScopeManager, THINK_CLOSE, THINK_OPEN, ThinkSplitter, Tool, ToolManager, WORKSPACE_SECTION_HEADER, Workspace, WorkspaceError, WorkspaceManager, buildToolResult, clampPosition, clampRange, computeSize, countLines, createAgent, createAgentContext, createAgentQueue, createAgentRegistry, createAgentRunner, createAuthority, createBinaryContent, createConversation, createConversationManager, createDatabaseConversationStore, createDatabaseWorkspaceStore, createFile, createInstruction, createInstructionManager, createMemoryConversationStore, createMemoryWorkspaceStore, createScope, createScopeManager, createTextContent, createThinkSplitter, createTool, createToolManager, createWorkspace, createWorkspaceManager, decodedSize, escapeRegExp, estimateMessages, estimateTokens, fencedFile, filterAllowList, inferLanguage, isAgentError, isAgentJobError, isBinary, isConversationError, isConversationSnapshot, isFile, isImage, isMessage, isProviderAbortError, isSection, isText, isToolCall, isValidRange, isWorkspaceError, isWorkspaceSnapshot, offsetAt, rangeOf, sanitizeUsage, settleAgentJob, sliceRange, spliceRange };
4257
+ export { Agent, AgentContext, AgentError, AgentJobError, AgentRegistry, Authority, CONVERSATION_RECAP_PREFIX, Channel, Conversation, ConversationError, ConversationManager, DEFAULT_AGENT_LIMIT, DEFAULT_AUTHORITY_ZONE, DEFAULT_CONVERSATION_KEEP, DatabaseConversationStore, DatabaseWorkspaceStore, EXTENSION_TO_LANGUAGE, IMAGE_TOKEN_ESTIMATE, Instruction, InstructionManager, MESSAGE_TOKEN_OVERHEAD, MemoryConversationStore, MemoryWorkspaceStore, ProviderAbortError, Scope, ScopeManager, THINK_CLOSE, THINK_OPEN, ThinkSplitter, Tool, ToolManager, WORKSPACE_SECTION_HEADER, Workspace, WorkspaceError, WorkspaceManager, buildToolResult, clampPosition, clampRange, computeSize, countLines, createAgent, createAgentContext, createAgentQueue, createAgentRegistry, createAgentRunner, createAuthority, createBinaryContent, createConversation, createConversationManager, createDatabaseConversationStore, createDatabaseWorkspaceStore, createFile, createInstruction, createInstructionManager, createMemoryConversationStore, createMemoryWorkspaceStore, createScope, createScopeManager, createTextContent, createThinkSplitter, createTool, createToolManager, createWorkspace, createWorkspaceManager, decodedSize, escapeRegExp, estimateMessages, estimateTokens, fencedFile, filterAllowList, handleAgentQueueJob, handleAgentRunnerJob, inferLanguage, isAgentError, isAgentJobError, isBinary, isConversationError, isConversationSnapshot, isFile, isImage, isMessage, isProviderAbortError, isSection, isText, isToolCall, isValidRange, isWorkspaceError, isWorkspaceSnapshot, offsetAt, rangeOf, sanitizeToken, sanitizeUsage, settleAgentJob, sliceRange, spliceRange };
4202
4258
 
4203
4259
  //# sourceMappingURL=index.js.map