@niscorp/cortex 0.1.6 → 0.2.0

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/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { resolveTransport, RESPOND_TOOL_NAME, deepDecodeJsonish, decodeJsonish } from '@niscorp/signal';
2
1
  import { z } from 'zod';
2
+ import { RESPOND_TOOL_NAME, resolveTransport, deepDecodeJsonish, decodeJsonish } from '@niscorp/signal';
3
3
  import { createStream } from '@niscorp/solid';
4
4
 
5
5
  // src/errors/cortex.errors.ts
@@ -33,7 +33,10 @@ var createEventChannel = (runId, agentPath) => {
33
33
  const buffer = [];
34
34
  let closed = false;
35
35
  let seq = 0;
36
- let wake;
36
+ const waiting = [];
37
+ const wakeAll = () => {
38
+ for (const resolve of waiting.splice(0)) resolve();
39
+ };
37
40
  const push = (event) => {
38
41
  for (const listener of [...listeners]) {
39
42
  try {
@@ -42,7 +45,7 @@ var createEventChannel = (runId, agentPath) => {
42
45
  }
43
46
  }
44
47
  buffer.push(event);
45
- wake?.();
48
+ wakeAll();
46
49
  };
47
50
  const emit = (body) => {
48
51
  if (closed) return;
@@ -62,7 +65,7 @@ var createEventChannel = (runId, agentPath) => {
62
65
  };
63
66
  const close = () => {
64
67
  closed = true;
65
- wake?.();
68
+ wakeAll();
66
69
  };
67
70
  const events = {
68
71
  [Symbol.asyncIterator]: () => {
@@ -72,9 +75,8 @@ var createEventChannel = (runId, agentPath) => {
72
75
  while (cursor >= buffer.length) {
73
76
  if (closed) return { done: true, value: void 0 };
74
77
  await new Promise((resolve) => {
75
- wake = resolve;
78
+ waiting.push(resolve);
76
79
  });
77
- wake = void 0;
78
80
  }
79
81
  const value = buffer[cursor];
80
82
  cursor += 1;
@@ -87,78 +89,6 @@ var createEventChannel = (runId, agentPath) => {
87
89
  return { emit, forward, onEvent, events, close };
88
90
  };
89
91
 
90
- // src/context/assemble.ts
91
- var isMessageArray = (value) => Array.isArray(value) && value.length > 0 && value.every(
92
- (item) => typeof item === "object" && item !== null && "role" in item && typeof item.role === "string"
93
- );
94
- var inputMessages = (input) => {
95
- if (typeof input === "string") return [{ role: "user", content: input }];
96
- if (isMessageArray(input)) return input;
97
- return [{ role: "user", content: JSON.stringify(input) }];
98
- };
99
- var entryMessages = (entry) => {
100
- if (typeof entry === "string") {
101
- return entry.length > 0 ? [{ role: "system", content: entry }] : [];
102
- }
103
- if (isMessageArray(entry)) return entry;
104
- return entry.filter((chunk) => typeof chunk === "string" && chunk.length > 0).map((chunk) => ({ role: "system", content: chunk }));
105
- };
106
- var assembleContext = async (items, args) => {
107
- const messages = [];
108
- for (const item of items) {
109
- const entry = typeof item === "function" ? await item(args) : item;
110
- messages.push(...entryMessages(entry));
111
- }
112
- return messages;
113
- };
114
- var toolGuidesMessage = (tools) => {
115
- const guides = tools.map((tool) => {
116
- const guide = tool.config.guide;
117
- const produced = typeof guide === "function" ? guide() : guide;
118
- const text = Array.isArray(produced) ? produced.join("\n") : produced;
119
- return text !== void 0 && text.length > 0 ? `\u2500\u2500 ${tool.config.name} \u2500\u2500
120
- ${text}` : void 0;
121
- }).filter((section) => section !== void 0);
122
- if (guides.length === 0) return [];
123
- return [
124
- {
125
- role: "system",
126
- content: `TOOL GUIDES \u2014 how to use the tools on this run:
127
-
128
- ${guides.join("\n\n")}`
129
- }
130
- ];
131
- };
132
- var estimateTokens = (messages) => {
133
- let total = 0;
134
- for (const message of messages) {
135
- total += 4;
136
- if (typeof message.content === "string") {
137
- total += Math.ceil(message.content.length / 4);
138
- continue;
139
- }
140
- for (const part of message.content) {
141
- total += part.type === "text" ? Math.ceil(part.text.length / 4) : 256;
142
- }
143
- }
144
- return total;
145
- };
146
-
147
- // src/context/schema-doc.ts
148
- var toJsonSchemaString = (schema) => {
149
- const convert = schema["~standard"].jsonSchema.output;
150
- try {
151
- return JSON.stringify(convert({ target: "draft-07" }));
152
- } catch {
153
- return JSON.stringify(convert({ target: "draft-07", libraryOptions: { unrepresentable: "any" } }));
154
- }
155
- };
156
- var schemaDoc = (schema, options) => {
157
- const title = options?.title ?? "OUTPUT SCHEMA";
158
- return `${title} \u2014 your output MUST validate against this JSON Schema:
159
- ${toJsonSchemaString(schema)}`;
160
- };
161
-
162
92
  // src/utils/schema-issues.ts
163
93
  var rootScore = (branch) => {
164
94
  let score = 0;
@@ -263,6 +193,10 @@ var validateEnvelope = (raw, spec) => {
263
193
  const repaired = spec.schema.safeParse(deepDecodeJsonish(data));
264
194
  if (repaired.success) parsed = repaired;
265
195
  }
196
+ if (!parsed.success && data !== record["data"] && spec.schema.type === "string") {
197
+ const asWritten = spec.schema.safeParse(record["data"]);
198
+ if (asWritten.success) parsed = asWritten;
199
+ }
266
200
  if (!parsed.success) {
267
201
  for (const issue of flattenSchemaIssues(parsed.error.issues)) {
268
202
  const path = issue.path.length > 0 ? `data.${issue.path.join(".")}` : "data";
@@ -326,24 +260,26 @@ var checkStop = (conditions, progress) => {
326
260
 
327
261
  // src/utils/timeout.ts
328
262
  var DEFAULT_TOOL_TIMEOUT_MS = 3e4;
329
- var withTimeout = (promise, timeoutMs, label) => {
330
- const resolved = Promise.resolve(promise);
331
- if (timeoutMs <= 0) return resolved;
332
- return new Promise((resolve, reject) => {
333
- const timer = setTimeout(() => {
334
- reject(new Error(`${label} timed out after ${timeoutMs}ms`));
335
- }, timeoutMs);
336
- resolved.then(
337
- (value) => {
338
- clearTimeout(timer);
339
- resolve(value);
340
- },
341
- (err2) => {
342
- clearTimeout(timer);
343
- reject(err2);
344
- }
345
- );
346
- });
263
+ var withTimeout = async (execute, limit) => {
264
+ const call = new AbortController();
265
+ const abort = () => call.abort();
266
+ if (limit.run.aborted) abort();
267
+ else limit.run.addEventListener("abort", abort, { once: true });
268
+ let timer;
269
+ try {
270
+ const pending = execute(call.signal);
271
+ if (limit.timeoutMs <= 0) return await pending;
272
+ const timedOut = new Promise((_resolve, reject) => {
273
+ timer = setTimeout(() => {
274
+ reject(new Error(`${limit.label} timed out after ${limit.timeoutMs}ms`));
275
+ abort();
276
+ }, limit.timeoutMs);
277
+ });
278
+ return await Promise.race([pending, timedOut]);
279
+ } finally {
280
+ clearTimeout(timer);
281
+ limit.run.removeEventListener("abort", abort);
282
+ }
347
283
  };
348
284
 
349
285
  // src/utils/id.ts
@@ -366,6 +302,24 @@ var stringify = (value) => {
366
302
  return String(value);
367
303
  }
368
304
  };
305
+ var argsFromText = (text) => {
306
+ try {
307
+ return JSON.parse(text);
308
+ } catch {
309
+ return text;
310
+ }
311
+ };
312
+ var unansweredCalls = (messages) => {
313
+ const answered = /* @__PURE__ */ new Set();
314
+ let at = messages.length - 1;
315
+ for (let message = messages[at]; message?.role === "tool"; message = messages[at]) {
316
+ answered.add(message.toolCallId);
317
+ at -= 1;
318
+ }
319
+ const step = messages[at];
320
+ if (step?.role !== "assistant" || step.toolCalls === void 0) return [];
321
+ return step.toolCalls.filter((call) => !answered.has(call.id)).map((call) => ({ id: call.id, toolId: call.name, args: argsFromText(call.args) }));
322
+ };
369
323
  var buildToolDescriptor = (tool) => ({
370
324
  name: tool.config.name,
371
325
  description: tool.config.description,
@@ -414,12 +368,13 @@ var runLoop = async (cfg) => {
414
368
  usage: s.usage,
415
369
  signal: cfg.abort
416
370
  });
417
- const toolContext = () => ({
371
+ const toolContext = (signal) => ({
418
372
  runId: cfg.runId,
419
373
  agentId: cfg.agentId,
420
374
  agentPath: cfg.agentPath,
421
- signal: cfg.abort,
422
- forward: cfg.forwardEvent
375
+ signal,
376
+ forward: cfg.forwardEvent,
377
+ forwardApprovals: cfg.approvals.forwardTo
423
378
  });
424
379
  const appendToolMessage = (call, content) => {
425
380
  s.messages.push({ role: "tool", toolCallId: call.id, name: call.toolId, content });
@@ -468,18 +423,11 @@ var runLoop = async (cfg) => {
468
423
  type: "approval-required",
469
424
  approval: { id: approvalId, toolId, callId: call.id, args, reason: decision.ask.reason }
470
425
  });
471
- const outcome = await new Promise((resolve) => {
472
- const onAbort = () => resolve({ approved: false, reason: "aborted" });
473
- if (cfg.abort.aborted) {
474
- onAbort();
475
- return;
476
- }
477
- cfg.abort.addEventListener("abort", onAbort, { once: true });
478
- void askPromise.then((askDecision) => {
479
- cfg.abort.removeEventListener("abort", onAbort);
480
- resolve(askDecision);
481
- });
482
- });
426
+ const onAbort = () => cfg.approvals.deny(approvalId, "aborted");
427
+ if (cfg.abort.aborted) onAbort();
428
+ else cfg.abort.addEventListener("abort", onAbort, { once: true });
429
+ const outcome = await askPromise;
430
+ cfg.abort.removeEventListener("abort", onAbort);
483
431
  if (cfg.abort.aborted) return fail(makeError("aborted", "run aborted", errorContext));
484
432
  s.pending = void 0;
485
433
  if (!outcome.approved) {
@@ -499,7 +447,7 @@ var runLoop = async (cfg) => {
499
447
  );
500
448
  return void 0;
501
449
  }
502
- let parsedArgs = tool.config.input.safeParse(args);
450
+ let parsedArgs = tool.config.input.safeParse(args === "" ? {} : args);
503
451
  if (!parsedArgs.success) {
504
452
  const repaired = tool.config.input.safeParse(deepDecodeJsonish(args));
505
453
  if (repaired.success) parsedArgs = repaired;
@@ -522,44 +470,37 @@ var runLoop = async (cfg) => {
522
470
  return void 0;
523
471
  }
524
472
  const started = Date.now();
473
+ const errorObservation = (cause) => ({
474
+ kind: "error",
475
+ callId: call.id,
476
+ toolId,
477
+ args: parsedArgs.data,
478
+ error: cause instanceof Error ? cause.message : String(cause),
479
+ durationMs: Date.now() - started
480
+ });
481
+ let observation;
525
482
  try {
526
483
  const timeoutMs = tool.config.timeoutMs ?? DEFAULT_TOOL_TIMEOUT_MS;
527
- const value = await withTimeout(
528
- Promise.resolve(tool.config.execute(parsedArgs.data, toolContext())),
484
+ const value = await withTimeout((signal) => tool.config.execute(parsedArgs.data, toolContext(signal)), {
529
485
  timeoutMs,
530
- `tool ${toolId}`
531
- );
532
- let observation = {
533
- kind: "result",
534
- callId: call.id,
535
- toolId,
536
- args: parsedArgs.data,
537
- result: value,
538
- durationMs: Date.now() - started
539
- };
486
+ label: `tool ${toolId}`,
487
+ run: cfg.abort
488
+ });
489
+ observation = { kind: "result", callId: call.id, toolId, args: parsedArgs.data, result: value, durationMs: Date.now() - started };
490
+ } catch (cause) {
491
+ observation = errorObservation(cause);
492
+ }
493
+ try {
540
494
  for (const hook of cfg.resultHooks) {
541
495
  const outcome = await hook(observation, runCtx());
542
496
  if (outcome && outcome.result !== void 0) {
543
- observation = { ...observation, result: outcome.result };
497
+ observation = observation.kind === "error" ? { ...observation, error: stringify(outcome.result) } : { ...observation, result: outcome.result };
544
498
  }
545
499
  }
546
- const finalResult = observation.kind === "result" ? observation.result : void 0;
547
- record(call, observation, stringify(finalResult));
548
500
  } catch (cause) {
549
- const message = cause instanceof Error ? cause.message : String(cause);
550
- record(
551
- call,
552
- {
553
- kind: "error",
554
- callId: call.id,
555
- toolId,
556
- args: parsedArgs.data,
557
- error: message,
558
- durationMs: Date.now() - started
559
- },
560
- `error: ${message}`
561
- );
501
+ observation = errorObservation(cause);
562
502
  }
503
+ record(call, observation, observation.kind === "error" ? `error: ${observation.error}` : stringify(observation.result));
563
504
  return void 0;
564
505
  };
565
506
  const processBatch = async (calls) => {
@@ -581,10 +522,10 @@ var runLoop = async (cfg) => {
581
522
  return verdict;
582
523
  };
583
524
  const streamEmittedOutput = cfg.resolved.transport !== "respond";
584
- const oneStep = async (llm, request) => {
525
+ const callModel = async (llm, request) => {
585
526
  if (cfg.transport === "step") {
586
527
  try {
587
- return ok(await llm.step(request));
528
+ return ok(await llm.step(request, { signal: cfg.abort }));
588
529
  } catch (cause) {
589
530
  const message = cause instanceof Error ? cause.message : String(cause);
590
531
  return err(makeError("model_call_failed", message, { ...errorContext, cause }));
@@ -622,9 +563,14 @@ var runLoop = async (cfg) => {
622
563
  return err(makeError("model_call_failed", message, { ...errorContext, cause }));
623
564
  }
624
565
  if (final) return ok(final);
625
- if (cfg.abort.aborted) return err(makeError("aborted", "run aborted during model call", errorContext));
626
566
  return err(makeError("model_call_failed", "stream ended without a done event", errorContext));
627
567
  };
568
+ const oneStep = async (llm, request) => {
569
+ const called = await callModel(llm, request);
570
+ if (cfg.abort.aborted) return err(makeError("aborted", "run aborted during model call", errorContext));
571
+ return called;
572
+ };
573
+ const correction = (issues) => cfg.resolved.corrections.invalidOutput.replace("{issues}", () => issues);
628
574
  let lastAttempt;
629
575
  const retryOutput = (kind, issues, attempt) => {
630
576
  if (attempt !== void 0) lastAttempt = attempt;
@@ -643,8 +589,9 @@ var runLoop = async (cfg) => {
643
589
  }
644
590
  return void 0;
645
591
  };
646
- if (cfg.resumePending) {
647
- const structural = await processBatch([cfg.resumePending.call, ...cfg.resumePending.remaining]);
592
+ const resumedCalls = cfg.resumePending ? [cfg.resumePending.call, ...cfg.resumePending.remaining] : cfg.resumed === true ? unansweredCalls(s.messages) : [];
593
+ if (resumedCalls.length > 0) {
594
+ const structural = await processBatch(resumedCalls);
648
595
  if (structural) return structural;
649
596
  }
650
597
  let providerRetries = 0;
@@ -684,7 +631,8 @@ var runLoop = async (cfg) => {
684
631
  ...cfg.resolved.responseFormat !== void 0 && { responseFormat: cfg.resolved.responseFormat },
685
632
  output: {
686
633
  accept: cfg.accept,
687
- ...cfg.resolved.outputToolName !== void 0 && { outputTool: cfg.resolved.outputToolName }
634
+ ...cfg.resolved.outputToolName !== void 0 && { outputTool: cfg.resolved.outputToolName },
635
+ ...cfg.refuseCutOff === true && { refuseCutOff: true }
688
636
  }
689
637
  };
690
638
  const stepOutcome = await oneStep(llm, request);
@@ -744,7 +692,7 @@ var runLoop = async (cfg) => {
744
692
  }
745
693
  s.messages.push({
746
694
  role: "system",
747
- content: cfg.resolved.corrections.invalidOutput.replace("{issues}", validated.issues)
695
+ content: correction(validated.issues)
748
696
  });
749
697
  continue;
750
698
  }
@@ -758,7 +706,7 @@ var runLoop = async (cfg) => {
758
706
  }
759
707
  s.messages.push({
760
708
  role: "system",
761
- content: cfg.resolved.corrections.invalidOutput.replace("{issues}", outcome.evidence)
709
+ content: correction(outcome.evidence)
762
710
  });
763
711
  }
764
712
  };
@@ -811,24 +759,35 @@ var createPartialTracker = (config) => {
811
759
  // src/gates/approval.ts
812
760
  var createApprovalBridge = () => {
813
761
  const pending = /* @__PURE__ */ new Map();
762
+ const children = /* @__PURE__ */ new Set();
814
763
  const settle = (approvalId, decision) => {
815
- const resolve = pending.get(approvalId);
816
- if (!resolve) return;
764
+ const ask = pending.get(approvalId);
765
+ if (!ask) return false;
817
766
  pending.delete(approvalId);
818
- resolve(decision);
767
+ clearTimeout(ask.timer);
768
+ ask.resolve(decision);
769
+ return true;
819
770
  };
820
771
  return {
821
772
  ask: (approvalId, timeoutMs) => new Promise((resolve) => {
822
- pending.set(approvalId, resolve);
823
- if (timeoutMs !== void 0 && timeoutMs > 0) {
824
- setTimeout(() => settle(approvalId, { approved: false, reason: "approval timeout" }), timeoutMs);
825
- }
826
- }),
827
- approve: (approvalId, options) => settle(approvalId, {
828
- approved: true,
829
- ...options?.args !== void 0 && { args: options.args }
773
+ const timer = timeoutMs !== void 0 && timeoutMs > 0 ? setTimeout(() => settle(approvalId, { approved: false, reason: "approval timeout" }), timeoutMs) : void 0;
774
+ pending.set(approvalId, { resolve, ...timer !== void 0 && { timer } });
830
775
  }),
831
- deny: (approvalId, reason) => settle(approvalId, { approved: false, reason: reason ?? "denied" })
776
+ approve: (approvalId, options) => {
777
+ const decision = { approved: true, ...options?.args !== void 0 && { args: options.args } };
778
+ if (!settle(approvalId, decision)) for (const child of children) child.approve(approvalId, options);
779
+ },
780
+ deny: (approvalId, reason) => {
781
+ if (!settle(approvalId, { approved: false, reason: reason ?? "denied" })) {
782
+ for (const child of children) child.deny(approvalId, reason);
783
+ }
784
+ },
785
+ forwardTo: (child) => {
786
+ children.add(child);
787
+ return () => {
788
+ children.delete(child);
789
+ };
790
+ }
832
791
  };
833
792
  };
834
793
 
@@ -852,6 +811,117 @@ var policyGate = (policy) => {
852
811
  };
853
812
  };
854
813
 
814
+ // src/context/assemble.ts
815
+ var isMessageArray = (value) => Array.isArray(value) && value.length > 0 && value.every(
816
+ (item) => typeof item === "object" && item !== null && "role" in item && typeof item.role === "string"
817
+ );
818
+ var inputMessages = (input) => {
819
+ if (typeof input === "string") return [{ role: "user", content: input }];
820
+ if (isMessageArray(input)) return input;
821
+ return [{ role: "user", content: JSON.stringify(input) }];
822
+ };
823
+ var entryMessages = (entry) => {
824
+ if (typeof entry === "string") {
825
+ return entry.length > 0 ? [{ role: "system", content: entry }] : [];
826
+ }
827
+ if (isMessageArray(entry)) return entry;
828
+ return entry.filter((chunk) => typeof chunk === "string" && chunk.length > 0).map((chunk) => ({ role: "system", content: chunk }));
829
+ };
830
+ var assembleContext = async (items, args) => {
831
+ const messages = [];
832
+ for (const item of items) {
833
+ const entry = typeof item === "function" ? await item(args) : item;
834
+ messages.push(...entryMessages(entry));
835
+ }
836
+ return messages;
837
+ };
838
+ var toolGuidesMessage = (tools) => {
839
+ const guides = tools.map((tool) => {
840
+ const guide = tool.config.guide;
841
+ const produced = typeof guide === "function" ? guide() : guide;
842
+ const text = Array.isArray(produced) ? produced.join("\n") : produced;
843
+ return text !== void 0 && text.length > 0 ? `\u2500\u2500 ${tool.config.name} \u2500\u2500
844
+ ${text}` : void 0;
845
+ }).filter((section) => section !== void 0);
846
+ if (guides.length === 0) return [];
847
+ return [
848
+ {
849
+ role: "system",
850
+ content: `TOOL GUIDES \u2014 how to use the tools on this run:
851
+
852
+ ${guides.join("\n\n")}`
853
+ }
854
+ ];
855
+ };
856
+ var estimateTokens = (messages) => {
857
+ let total = 0;
858
+ for (const message of messages) {
859
+ total += 4;
860
+ if (typeof message.content === "string") {
861
+ total += Math.ceil(message.content.length / 4);
862
+ continue;
863
+ }
864
+ for (const part of message.content) {
865
+ total += part.type === "text" ? Math.ceil(part.text.length / 4) : 256;
866
+ }
867
+ }
868
+ return total;
869
+ };
870
+
871
+ // src/context/schema-doc.ts
872
+ var toJsonSchemaString = (schema) => {
873
+ const convert = schema["~standard"].jsonSchema.output;
874
+ try {
875
+ return JSON.stringify(convert({ target: "draft-07" }));
876
+ } catch {
877
+ return JSON.stringify(convert({ target: "draft-07", libraryOptions: { unrepresentable: "any" } }));
878
+ }
879
+ };
880
+ var schemaDoc = (schema, options) => {
881
+ const title = options?.title ?? "OUTPUT SCHEMA";
882
+ return `${title} \u2014 your output MUST validate against this JSON Schema:
883
+ ${toJsonSchemaString(schema)}`;
884
+ };
885
+
886
+ // src/agent/prefix.ts
887
+ var responseModeOf = (config) => config.output?.response ?? (config.output?.schema ? "optional" : "required");
888
+ var resolveAgentTransport = (config, tools, capabilities) => {
889
+ const outputSchema = config.output?.schema;
890
+ const responseMode = responseModeOf(config);
891
+ return resolveTransport(
892
+ {
893
+ wire: envelopeWireSchema({ ...outputSchema && { schema: outputSchema }, responseMode }),
894
+ looseWire: envelopeLooseWireSchema({ hasData: outputSchema !== void 0, responseMode }),
895
+ responseMode,
896
+ hasData: outputSchema !== void 0,
897
+ hasTools: tools.length > 0,
898
+ ...config.output?.strategy && { choice: config.output.strategy },
899
+ ...config.output?.forceTool && { forceTool: true }
900
+ },
901
+ capabilities
902
+ );
903
+ };
904
+ var assemblePrefix = async (prefix) => {
905
+ const { config, resolved, tools, input, deps } = prefix;
906
+ const outputSchema = config.output?.schema;
907
+ const contextMessages = await assembleContext([config.instructions, ...config.context ?? [], ...prefix.producers], {
908
+ deps,
909
+ input,
910
+ agent: { id: config.id, ...config.description !== void 0 && { description: config.description } }
911
+ });
912
+ const doc = config.output?.doc ?? "auto";
913
+ const docMessage = resolved.injectSchemaDoc && doc !== "off" && outputSchema ? [{ role: "system", content: typeof doc === "string" && doc !== "auto" ? doc : schemaDoc(outputSchema) }] : [];
914
+ const [identity, ...rest] = contextMessages;
915
+ return [
916
+ ...identity ? [identity] : [],
917
+ ...toolGuidesMessage(tools),
918
+ ...docMessage,
919
+ { role: "system", content: resolved.finishProtocol },
920
+ ...rest,
921
+ ...inputMessages(input)
922
+ ];
923
+ };
924
+
855
925
  // src/agent/run.ts
856
926
  var wireRun = (wiring) => {
857
927
  const { definition, input, options } = wiring;
@@ -867,19 +937,8 @@ var wireRun = (wiring) => {
867
937
  }
868
938
  }
869
939
  const outputSchema = config.output?.schema;
870
- const responseMode = config.output?.response ?? (outputSchema ? "optional" : "required");
871
- const resolved = resolveTransport(
872
- {
873
- wire: envelopeWireSchema({ ...outputSchema && { schema: outputSchema }, responseMode }),
874
- looseWire: envelopeLooseWireSchema({ hasData: outputSchema !== void 0, responseMode }),
875
- responseMode,
876
- hasData: outputSchema !== void 0,
877
- hasTools: tools.length > 0,
878
- ...config.output?.strategy && { choice: config.output.strategy },
879
- ...config.output?.forceTool && { forceTool: true }
880
- },
881
- llm.describe().capabilities
882
- );
940
+ responseModeOf(config);
941
+ const resolved = resolveAgentTransport(config, tools, llm.describe().capabilities);
883
942
  if (resolved.respondDescriptor && taken.has(RESPOND_TOOL_NAME)) {
884
943
  throwConfig(`agent "${config.id}": a tool named "respond" collides with the transport's exit tool`);
885
944
  }
@@ -923,29 +982,7 @@ var wireRun = (wiring) => {
923
982
  let outcome;
924
983
  try {
925
984
  if (!wiring.seeded) {
926
- const args = {
927
- deps,
928
- input,
929
- agent: { id: config.id, ...config.description !== void 0 && { description: config.description } }
930
- };
931
- const items = [
932
- config.instructions,
933
- ...config.context ?? [],
934
- ...options?.producers ?? []
935
- ];
936
- const contextMessages = await assembleContext(items, args);
937
- const doc = config.output?.doc ?? "auto";
938
- const docMessage = resolved.injectSchemaDoc && doc !== "off" && outputSchema ? [{ role: "system", content: typeof doc === "string" && doc !== "auto" ? doc : schemaDoc(outputSchema) }] : [];
939
- const finishMessage = { role: "system", content: resolved.finishProtocol };
940
- const [identity, ...rest] = contextMessages;
941
- state.messages = [
942
- ...identity ? [identity] : [],
943
- ...toolGuidesMessage(tools),
944
- ...docMessage,
945
- finishMessage,
946
- ...rest,
947
- ...inputMessages(input)
948
- ];
985
+ state.messages = await assemblePrefix({ config, resolved, tools, producers: options?.producers ?? [], deps, input });
949
986
  }
950
987
  outcome = await runLoop({
951
988
  runId: wiring.runId,
@@ -972,7 +1009,9 @@ var wireRun = (wiring) => {
972
1009
  abort: controller.signal,
973
1010
  partials,
974
1011
  state,
975
- ...wiring.seeded?.pending && { resumePending: wiring.seeded.pending }
1012
+ ...wiring.seeded?.pending && { resumePending: wiring.seeded.pending },
1013
+ resumed: wiring.seeded !== void 0,
1014
+ ...config.output?.refuseCutOff === true && { refuseCutOff: true }
976
1015
  });
977
1016
  } catch (cause) {
978
1017
  const message = cause instanceof Error ? cause.message : String(cause);
@@ -1024,10 +1063,19 @@ var wireRun = (wiring) => {
1024
1063
  };
1025
1064
  };
1026
1065
  var createRun = (definition, input, options) => wireRun({ definition, input, options, runId: newRunId() });
1066
+ var ResumedCallSchema = z.looseObject({ id: z.string(), toolId: z.string() });
1067
+ var ResumableSnapshotSchema = z.looseObject({
1068
+ messages: z.array(z.looseObject({})),
1069
+ pending: z.looseObject({ call: ResumedCallSchema, remaining: z.array(ResumedCallSchema) }).nullish()
1070
+ });
1027
1071
  var resumeRun = (definition, snapshot, ...args) => {
1028
1072
  if (snapshot.agentId !== definition.agentId) {
1029
1073
  throwConfig(`snapshot belongs to agent "${snapshot.agentId}", not "${definition.agentId}"`);
1030
1074
  }
1075
+ const resumable = ResumableSnapshotSchema.safeParse(snapshot);
1076
+ if (!resumable.success) {
1077
+ throwConfig(`snapshot cannot be resumed: ${resumable.error.issues.map((issue) => `${issue.path.join(".")}: ${issue.message}`).join("; ")}`);
1078
+ }
1031
1079
  return wireRun({
1032
1080
  definition,
1033
1081
  input: snapshot.input,
@@ -1043,6 +1091,8 @@ var resumeRun = (definition, snapshot, ...args) => {
1043
1091
  }
1044
1092
  });
1045
1093
  };
1094
+
1095
+ // src/agent/preview.ts
1046
1096
  var FALLBACK_CAPABILITIES = {
1047
1097
  nativeTools: true,
1048
1098
  nativeJsonSchema: false,
@@ -1056,46 +1106,16 @@ var FALLBACK_CAPABILITIES = {
1056
1106
  var previewAgent = async (definition, input, options) => {
1057
1107
  const config = definition.config;
1058
1108
  const llm = options?.llm ?? config.llm;
1059
- const capabilities = llm ? llm.describe().capabilities : FALLBACK_CAPABILITIES;
1060
1109
  const allTools = [...config.tools ?? [], ...options?.tools ?? []];
1061
- const outputSchema = config.output?.schema;
1062
- const responseMode = config.output?.response ?? (outputSchema ? "optional" : "required");
1063
- const resolved = resolveTransport(
1064
- {
1065
- wire: envelopeWireSchema({ ...outputSchema && { schema: outputSchema }, responseMode }),
1066
- looseWire: envelopeLooseWireSchema({ hasData: outputSchema !== void 0, responseMode }),
1067
- responseMode,
1068
- hasData: outputSchema !== void 0,
1069
- hasTools: allTools.length > 0,
1070
- ...config.output?.strategy && { choice: config.output.strategy },
1071
- ...config.output?.forceTool && { forceTool: true }
1072
- },
1073
- capabilities
1074
- );
1075
- const deps = trustErased(options?.deps);
1076
- const args = {
1077
- deps,
1078
- input,
1079
- agent: { id: config.id, ...config.description !== void 0 && { description: config.description } }
1080
- };
1081
- const items = [
1082
- config.instructions,
1083
- ...config.context ?? [],
1084
- ...options?.producers ?? []
1085
- ];
1086
- const contextMessages = await assembleContext(items, args);
1087
- const doc = config.output?.doc ?? "auto";
1088
- const docMessage = resolved.injectSchemaDoc && doc !== "off" && outputSchema ? [{ role: "system", content: typeof doc === "string" && doc !== "auto" ? doc : schemaDoc(outputSchema) }] : [];
1089
- const finishMessage = { role: "system", content: resolved.finishProtocol };
1090
- const [identity, ...rest] = contextMessages;
1091
- const messages = [
1092
- ...identity ? [identity] : [],
1093
- ...toolGuidesMessage(allTools),
1094
- ...docMessage,
1095
- finishMessage,
1096
- ...rest,
1097
- ...inputMessages(input)
1098
- ];
1110
+ const resolved = resolveAgentTransport(config, allTools, llm ? llm.describe().capabilities : FALLBACK_CAPABILITIES);
1111
+ const messages = await assemblePrefix({
1112
+ config,
1113
+ resolved,
1114
+ tools: allTools,
1115
+ producers: options?.producers ?? [],
1116
+ deps: trustErased(options?.deps),
1117
+ input
1118
+ });
1099
1119
  const tools = [
1100
1120
  ...allTools.map(buildToolDescriptor),
1101
1121
  ...resolved.respondDescriptor ? [resolved.respondDescriptor] : []
@@ -1130,7 +1150,7 @@ var defineAgent = (config) => {
1130
1150
  var ToolRiskLevelSchema = z.enum(["low", "medium", "high"]);
1131
1151
  var ToolConfigSchema = z.object({
1132
1152
  id: z.string().describe("Unique tool identifier."),
1133
- name: z.string().describe("Human-readable tool name."),
1153
+ name: z.string().describe("The name the model calls: letters, digits, `_` and `-`, at most 64 characters (what OpenAI-compatible providers accept)."),
1134
1154
  description: z.string().describe("What this tool does, for the model to read."),
1135
1155
  category: z.string().optional().describe("Tool category for grouping."),
1136
1156
  riskLevel: ToolRiskLevelSchema.optional().describe("Risk level: low, medium, or high."),
@@ -1171,7 +1191,8 @@ var buildAgentTool = (core) => defineTool({
1171
1191
  agentPath: ctx.agentPath,
1172
1192
  onEvent: ctx.forward
1173
1193
  });
1174
- const result = await handle.result;
1194
+ const stopForwarding = ctx.forwardApprovals?.(handle);
1195
+ const result = await handle.result.finally(() => stopForwarding?.());
1175
1196
  if (!result.ok) return `error: ${result.error.message}`;
1176
1197
  if (core.select) return core.select(result.output);
1177
1198
  return result.output.data !== void 0 ? result.output.data : result.output.response ?? "";
@@ -1199,7 +1220,6 @@ var asTool = (agent, ...args) => {
1199
1220
  };
1200
1221
 
1201
1222
  // src/manifold/manifold.ts
1202
- var DEFAULT_AGENT_TOOL_TIMEOUT_MS2 = 3e5;
1203
1223
  var createManifold = (config = {}) => {
1204
1224
  const agents = /* @__PURE__ */ new Map();
1205
1225
  const tools = /* @__PURE__ */ new Map();
@@ -1245,7 +1265,7 @@ var createManifold = (config = {}) => {
1245
1265
  return buildAgentTool({
1246
1266
  id: options?.id ?? agent.agentId,
1247
1267
  description: options?.description ?? agent.config.description ?? `Delegate the task to agent "${agent.agentId}".`,
1248
- timeoutMs: options?.timeoutMs ?? DEFAULT_AGENT_TOOL_TIMEOUT_MS2,
1268
+ timeoutMs: options?.timeoutMs ?? DEFAULT_AGENT_TOOL_TIMEOUT_MS,
1249
1269
  ...options?.select && { select: options.select },
1250
1270
  start: (input, context) => {
1251
1271
  const handle = run(agent.agentId, input, {