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