@demlik/tea 0.1.2 → 0.3.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.
Files changed (212) hide show
  1. package/README.md +49 -0
  2. package/dist/agent/index.d.ts +16 -770
  3. package/dist/agent/index.js +18 -16
  4. package/dist/authed-call/index.d.ts +3 -3
  5. package/dist/authed-call/index.js +22 -36
  6. package/dist/authed-call/index.js.map +1 -1
  7. package/dist/await-terminal/index.d.ts +2 -2
  8. package/dist/await-terminal/index.js +2 -2
  9. package/dist/batch-window/index.d.ts +3 -3
  10. package/dist/batch-window/index.js +3 -3
  11. package/dist/cache/index.d.ts +2 -2
  12. package/dist/cache/index.js +5 -2
  13. package/dist/{chunk-3545BOYL.js → chunk-25LBDCBC.js} +6 -5
  14. package/dist/chunk-25LBDCBC.js.map +1 -0
  15. package/dist/{chunk-IN33Q4VJ.js → chunk-2KNLKQVN.js} +3 -3
  16. package/dist/{chunk-IN33Q4VJ.js.map → chunk-2KNLKQVN.js.map} +1 -1
  17. package/dist/{chunk-MMH7CW5K.js → chunk-BP2R7TPP.js} +216 -157
  18. package/dist/chunk-BP2R7TPP.js.map +1 -0
  19. package/dist/chunk-C6QC273X.js +71 -0
  20. package/dist/chunk-C6QC273X.js.map +1 -0
  21. package/dist/{chunk-7W4LMPUW.js → chunk-CAXMQJJ3.js} +17 -24
  22. package/dist/chunk-CAXMQJJ3.js.map +1 -0
  23. package/dist/{chunk-UV6UWGEO.js → chunk-D736QUGF.js} +6 -5
  24. package/dist/chunk-D736QUGF.js.map +1 -0
  25. package/dist/chunk-EWJP3YYP.js +21 -0
  26. package/dist/chunk-EWJP3YYP.js.map +1 -0
  27. package/dist/{chunk-TGG26XMK.js → chunk-FV3Z5MBE.js} +10 -5
  28. package/dist/chunk-FV3Z5MBE.js.map +1 -0
  29. package/dist/chunk-G5E74IJB.js +8 -0
  30. package/dist/chunk-G5E74IJB.js.map +1 -0
  31. package/dist/{chunk-27N2SPTN.js → chunk-IXBKFQH5.js} +12 -11
  32. package/dist/chunk-IXBKFQH5.js.map +1 -0
  33. package/dist/{chunk-SQTCS44W.js → chunk-JFFVBEDI.js} +3 -3
  34. package/dist/{chunk-SQTCS44W.js.map → chunk-JFFVBEDI.js.map} +1 -1
  35. package/dist/chunk-LYLDCIPH.js +365 -0
  36. package/dist/chunk-LYLDCIPH.js.map +1 -0
  37. package/dist/{chunk-HJO6VNQI.js → chunk-MU6MCIAC.js} +3 -3
  38. package/dist/{chunk-HJO6VNQI.js.map → chunk-MU6MCIAC.js.map} +1 -1
  39. package/dist/{chunk-UYWUVEP6.js → chunk-MUUROGIH.js} +2 -2
  40. package/dist/chunk-MUUROGIH.js.map +1 -0
  41. package/dist/{chunk-EHIX7NYZ.js → chunk-MVY5DAMF.js} +4 -4
  42. package/dist/{chunk-EHIX7NYZ.js.map → chunk-MVY5DAMF.js.map} +1 -1
  43. package/dist/{chunk-LP7VQBRG.js → chunk-NPERUUUE.js} +76 -95
  44. package/dist/chunk-NPERUUUE.js.map +1 -0
  45. package/dist/{chunk-QLA76GIT.js → chunk-PBDYW2TU.js} +68 -9
  46. package/dist/chunk-PBDYW2TU.js.map +1 -0
  47. package/dist/{chunk-5VNWZADP.js → chunk-Q2UM7BXU.js} +18 -7
  48. package/dist/chunk-Q2UM7BXU.js.map +1 -0
  49. package/dist/chunk-RMX4EOQK.js +26 -0
  50. package/dist/chunk-RMX4EOQK.js.map +1 -0
  51. package/dist/{chunk-2YOE5PIS.js → chunk-S2EGRPCM.js} +8 -6
  52. package/dist/chunk-S2EGRPCM.js.map +1 -0
  53. package/dist/{chunk-ON24RKSE.js → chunk-S4O26D7B.js} +7 -14
  54. package/dist/chunk-S4O26D7B.js.map +1 -0
  55. package/dist/{chunk-3XJWC7VW.js → chunk-SK5JGUY7.js} +3 -3
  56. package/dist/{chunk-3XJWC7VW.js.map → chunk-SK5JGUY7.js.map} +1 -1
  57. package/dist/{chunk-5DEQ5I3U.js → chunk-TPGASTFF.js} +8 -10
  58. package/dist/chunk-TPGASTFF.js.map +1 -0
  59. package/dist/{chunk-SC3FAFZN.js → chunk-UF4MZSLT.js} +2 -2
  60. package/dist/chunk-UF4MZSLT.js.map +1 -0
  61. package/dist/{chunk-PS23H6KQ.js → chunk-VBUCHVCV.js} +6 -5
  62. package/dist/chunk-VBUCHVCV.js.map +1 -0
  63. package/dist/{chunk-S3FQN4SL.js → chunk-WGGAMW7Y.js} +191 -178
  64. package/dist/chunk-WGGAMW7Y.js.map +1 -0
  65. package/dist/chunk-ZJ5AOYBH.js +8 -0
  66. package/dist/chunk-ZJ5AOYBH.js.map +1 -0
  67. package/dist/{chunk-LEC4U6CX.js → chunk-ZVJZ72HY.js} +3 -3
  68. package/dist/{chunk-LEC4U6CX.js.map → chunk-ZVJZ72HY.js.map} +1 -1
  69. package/dist/circuit-breaker/index.d.ts +1 -0
  70. package/dist/circuit-breaker/index.js +1 -1
  71. package/dist/{core-BoB6t7dB.d.ts → core-42kapW0n.d.ts} +105 -6
  72. package/dist/deadline/index.d.ts +77 -11
  73. package/dist/deadline/index.js +3 -3
  74. package/dist/devtools/index.js +8 -28
  75. package/dist/devtools/index.js.map +1 -1
  76. package/dist/do/index.d.ts +483 -339
  77. package/dist/do/index.js +244 -214
  78. package/dist/do/index.js.map +1 -1
  79. package/dist/{durable-effects-CO9Uh971.d.ts → durable-effects-CFRngTNA.d.ts} +3 -3
  80. package/dist/extension/index.d.ts +25 -7
  81. package/dist/extension/index.js +4 -3
  82. package/dist/extension/index.js.map +1 -1
  83. package/dist/extension/react.js +4 -3
  84. package/dist/extension/react.js.map +1 -1
  85. package/dist/extension/subs/index.d.ts +2 -2
  86. package/dist/extension/subs/index.js +7 -10
  87. package/dist/extension/subs/index.js.map +1 -1
  88. package/dist/fan-out/index.d.ts +10 -10
  89. package/dist/fan-out/index.js +3 -3
  90. package/dist/idempotent-intake/index.d.ts +2 -2
  91. package/dist/idempotent-intake/index.js +3 -3
  92. package/dist/index.d.ts +50 -472
  93. package/dist/index.js +2 -2
  94. package/dist/llm-call/index.d.ts +3 -2
  95. package/dist/llm-call/index.js +12 -10
  96. package/dist/machine-B_BF4jij.d.ts +804 -0
  97. package/dist/machine-viz/index.d.ts +1 -1
  98. package/dist/machine-viz/index.js +2 -2
  99. package/dist/mem/index.d.ts +3 -2
  100. package/dist/mem/index.js.map +1 -1
  101. package/dist/monitored-run/index.d.ts +54 -30
  102. package/dist/monitored-run/index.js +7 -7
  103. package/dist/node/index.d.ts +3 -2
  104. package/dist/node/index.js +2 -3
  105. package/dist/node/index.js.map +1 -1
  106. package/dist/paginated-walk/index.d.ts +2 -2
  107. package/dist/paginated-walk/index.js +11 -10
  108. package/dist/parity/index.d.ts +6 -5
  109. package/dist/parity/index.js +5 -4
  110. package/dist/parity/index.js.map +1 -1
  111. package/dist/pbt/arbitraries/index.d.ts +1 -1
  112. package/dist/pbt/index.d.ts +1 -1
  113. package/dist/pbt/index.js +3 -3
  114. package/dist/pbt/index.js.map +1 -1
  115. package/dist/pbt/runners/index.d.ts +1 -1
  116. package/dist/pbt/runners/index.js +3 -3
  117. package/dist/poller/index.d.ts +59 -17
  118. package/dist/poller/index.js +56 -20
  119. package/dist/poller/index.js.map +1 -1
  120. package/dist/prediction/index.d.ts +1 -1
  121. package/dist/prediction/index.js +2 -2
  122. package/dist/pure/index.d.ts +1 -1
  123. package/dist/pure/index.js +2 -2
  124. package/dist/rate-limit/index.d.ts +2 -2
  125. package/dist/rate-limit/index.js +1 -1
  126. package/dist/react/index.d.ts +3 -2
  127. package/dist/react/index.js +2 -2
  128. package/dist/react/index.js.map +1 -1
  129. package/dist/reconciler/index.d.ts +2 -2
  130. package/dist/reconciler/index.js +13 -15
  131. package/dist/reconciler/index.js.map +1 -1
  132. package/dist/recorder/index.d.ts +20 -20
  133. package/dist/recorder/index.js +1 -1
  134. package/dist/resilient-call/index.d.ts +31 -6
  135. package/dist/resilient-call/index.js +10 -9
  136. package/dist/retry-backoff/index.d.ts +191 -14
  137. package/dist/retry-backoff/index.js +1 -1
  138. package/dist/retry-to-success/index.js +1 -1
  139. package/dist/runtime-types-gsog27b6.d.ts +337 -0
  140. package/dist/saga/index.d.ts +3 -2
  141. package/dist/saga/index.js +2 -2
  142. package/dist/saga/index.js.map +1 -1
  143. package/dist/snapshot/index.d.ts +1 -1
  144. package/dist/snapshot/index.js +3 -3
  145. package/dist/subs/index.d.ts +372 -4
  146. package/dist/subs/index.js +4 -1
  147. package/dist/testing/index.d.ts +2 -2
  148. package/dist/testing/index.js +2 -2
  149. package/dist/throttled-input/index.d.ts +2 -2
  150. package/dist/throttled-input/index.js +14 -13
  151. package/dist/throttled-input/index.js.map +1 -1
  152. package/dist/token-refresh/index.d.ts +1 -1
  153. package/dist/token-refresh/index.js +3 -3
  154. package/dist/trace-replay/index.d.ts +12 -7
  155. package/dist/trace-replay/index.js +4 -3
  156. package/dist/{types-B-8bcAp8.d.ts → types-KbL3W1mj.d.ts} +1 -1
  157. package/dist/with-deadline/index.d.ts +1 -1
  158. package/dist/with-deadline/index.js +2 -2
  159. package/dist/with-resilience/index.d.ts +21 -2
  160. package/dist/with-resilience/index.js +12 -23
  161. package/dist/with-resilience/index.js.map +1 -1
  162. package/dist/with-telemetry/index.d.ts +1 -1
  163. package/dist/with-telemetry/index.js +2 -2
  164. package/dist/work-queue/adapter.d.ts +2 -2
  165. package/dist/work-queue/adapter.js +2 -2
  166. package/dist/work-queue/index.d.ts +3 -2
  167. package/dist/work-queue/index.js +1 -1
  168. package/dist/work-queue/index.js.map +1 -1
  169. package/dist/work-queue/ops.d.ts +2 -2
  170. package/dist/work-queue/ops.js +1 -1
  171. package/dist/workflow/index.d.ts +51 -19
  172. package/dist/workflow/index.js +71 -52
  173. package/dist/workflow/index.js.map +1 -1
  174. package/package.json +4 -19
  175. package/dist/chunk-27N2SPTN.js.map +0 -1
  176. package/dist/chunk-2YOE5PIS.js.map +0 -1
  177. package/dist/chunk-3545BOYL.js.map +0 -1
  178. package/dist/chunk-5DEQ5I3U.js.map +0 -1
  179. package/dist/chunk-5VNWZADP.js.map +0 -1
  180. package/dist/chunk-6RGBKODV.js +0 -268
  181. package/dist/chunk-6RGBKODV.js.map +0 -1
  182. package/dist/chunk-7W4LMPUW.js.map +0 -1
  183. package/dist/chunk-DILMKOZ7.js +0 -91
  184. package/dist/chunk-DILMKOZ7.js.map +0 -1
  185. package/dist/chunk-K7Q23EQ4.js +0 -20
  186. package/dist/chunk-K7Q23EQ4.js.map +0 -1
  187. package/dist/chunk-LP7VQBRG.js.map +0 -1
  188. package/dist/chunk-MMH7CW5K.js.map +0 -1
  189. package/dist/chunk-NSDFA6M6.js +0 -198
  190. package/dist/chunk-NSDFA6M6.js.map +0 -1
  191. package/dist/chunk-ON24RKSE.js.map +0 -1
  192. package/dist/chunk-PS23H6KQ.js.map +0 -1
  193. package/dist/chunk-QLA76GIT.js.map +0 -1
  194. package/dist/chunk-S3FQN4SL.js.map +0 -1
  195. package/dist/chunk-SC3FAFZN.js.map +0 -1
  196. package/dist/chunk-TGG26XMK.js.map +0 -1
  197. package/dist/chunk-UV6UWGEO.js.map +0 -1
  198. package/dist/chunk-UYWUVEP6.js.map +0 -1
  199. package/dist/chunk-VLGARN6H.js +0 -202
  200. package/dist/chunk-VLGARN6H.js.map +0 -1
  201. package/dist/raft/demo.d.ts +0 -105
  202. package/dist/raft/demo.js +0 -172
  203. package/dist/raft/demo.js.map +0 -1
  204. package/dist/raft/index.d.ts +0 -548
  205. package/dist/raft/index.js +0 -7
  206. package/dist/raft/index.js.map +0 -1
  207. package/dist/raft/sim.d.ts +0 -222
  208. package/dist/raft/sim.js +0 -8
  209. package/dist/raft/sim.js.map +0 -1
  210. package/dist/raft/timeline.d.ts +0 -46
  211. package/dist/raft/timeline.js +0 -62
  212. package/dist/raft/timeline.js.map +0 -1
@@ -1,45 +1,146 @@
1
- import { createMonitoredRun } from './chunk-7W4LMPUW.js';
2
- import { createLlmCall } from './chunk-PS23H6KQ.js';
3
- import { createResilientCall } from './chunk-27N2SPTN.js';
1
+ import { createMonitoredRun } from './chunk-CAXMQJJ3.js';
2
+ import { createLlmCall } from './chunk-VBUCHVCV.js';
3
+ import { createResilientCall } from './chunk-IXBKFQH5.js';
4
4
  import { MsgType } from './chunk-JELSZN25.js';
5
- import { createFanOut, initFanOut } from './chunk-UV6UWGEO.js';
6
- import { subscribeDeadline } from './chunk-K7Q23EQ4.js';
7
- import { defineMachine } from './chunk-MMH7CW5K.js';
5
+ import { createFanOut, initFanOut } from './chunk-D736QUGF.js';
6
+ import { subscribeDeadline } from './chunk-RMX4EOQK.js';
7
+ import { defineMachine } from './chunk-BP2R7TPP.js';
8
8
 
9
- // src/agent/index.ts
10
- function isAgentTurn(value) {
11
- if (value === null || typeof value !== "object") return false;
12
- const t = value;
13
- return typeof t.content === "string" && Array.isArray(t.toolCalls);
14
- }
15
- var agentTurnSchema = {
16
- parse: (value) => {
17
- if (!isAgentTurn(value)) {
18
- throw new Error("structured-output parse failed: not an AgentTurn");
9
+ // src/agent/schema.ts
10
+ function schemaFromGuard(guard, name) {
11
+ return {
12
+ parse: (value) => {
13
+ if (!guard(value)) {
14
+ throw new Error(`structured-output parse failed: not ${name}`);
15
+ }
16
+ return value;
19
17
  }
20
- return value;
21
- }
22
- };
18
+ };
19
+ }
20
+
21
+ // src/agent/compaction.ts
23
22
  var COMPACTION_PURPOSE = "$compact";
24
23
  function isCompactionSummary(value) {
25
24
  if (value === null || typeof value !== "object") return false;
26
25
  return typeof value.summary === "string";
27
26
  }
28
- var compactionSummarySchema = {
29
- parse: (value) => {
30
- if (!isCompactionSummary(value)) {
31
- throw new Error(
32
- "structured-output parse failed: not a CompactionSummary"
33
- );
27
+ var compactionSummarySchema = schemaFromGuard(isCompactionSummary, "a CompactionSummary");
28
+
29
+ // src/agent/internal.ts
30
+ function freshConversation() {
31
+ return {
32
+ turns: [],
33
+ toolRecords: [],
34
+ turnCount: 0,
35
+ awaiting: { kind: "llm" }
36
+ };
37
+ }
38
+ function dedupeByCallId(calls) {
39
+ const seen = /* @__PURE__ */ new Set();
40
+ const out = [];
41
+ for (const call of calls) {
42
+ if (seen.has(call.callId)) continue;
43
+ seen.add(call.callId);
44
+ out.push(call);
45
+ }
46
+ return out;
47
+ }
48
+ function foldSummary(conv, folding, summaryTurn) {
49
+ const shift = folding - 1;
50
+ const survivingTurns = conv.turns.slice(folding);
51
+ const survivingRecords = conv.toolRecords.filter((rec) => rec.turn >= folding).map((rec) => ({ ...rec, turn: rec.turn - shift }));
52
+ return {
53
+ ...conv,
54
+ turns: [summaryTurn, ...survivingTurns],
55
+ toolRecords: survivingRecords,
56
+ awaiting: { kind: "llm" }
57
+ };
58
+ }
59
+ function currentStage(s) {
60
+ return s.run.stepStates.find((i) => i.status === "running")?.input;
61
+ }
62
+ function isSettled(s) {
63
+ return s.run.phase === "done" || s.run.phase === "failed" || s.failure !== null;
64
+ }
65
+ function requireAwaiting(s, kind) {
66
+ if (isSettled(s)) return null;
67
+ const conv = s.conversation;
68
+ if (conv === null || conv.awaiting.kind !== kind) return null;
69
+ return conv;
70
+ }
71
+ function toCompactRunCmd(cmd) {
72
+ return {
73
+ type: MsgType.CompactRun,
74
+ key: COMPACTION_PURPOSE,
75
+ input: cmd.input
76
+ };
77
+ }
78
+
79
+ // src/agent/machine.ts
80
+ function agentBootMsg(at) {
81
+ return { type: MsgType.AgentBoot, at };
82
+ }
83
+ function agentEvents() {
84
+ return (msg, state) => {
85
+ const events = [];
86
+ switch (msg.type) {
87
+ case MsgType.ResilientOk:
88
+ events.push({ type: "TurnSettled", turn: msg.result.output });
89
+ break;
90
+ case MsgType.AgentToolOk:
91
+ events.push({
92
+ type: "ToolSettled",
93
+ callId: msg.callId,
94
+ result: msg.result
95
+ });
96
+ break;
97
+ // start / boot / timer / the *_err arms / the compaction settles carry no
98
+ // public event — return []. (No `default`: the switch is exhaustive over
99
+ // the Msg discriminant, so a new Msg variant forces a decision here at
100
+ // compile time.) Compaction is an internal optimization, not a semantic
101
+ // lifecycle moment a UI folds — its settles are deliberately silent here.
102
+ case MsgType.AgentStart:
103
+ case MsgType.AgentToolErr:
104
+ case MsgType.ResilientErr:
105
+ case MsgType.CompactOk:
106
+ case MsgType.CompactErr:
107
+ case "deadline_exceeded":
108
+ case MsgType.AgentBoot:
109
+ break;
34
110
  }
35
- return value;
36
- }
37
- };
111
+ if (state.run.phase === "done") {
112
+ events.push({ type: "RunDone", output: state.output });
113
+ }
114
+ return events;
115
+ };
116
+ }
117
+ function mergeInterpret(a, b) {
118
+ return { ...a ?? {}, ...b };
119
+ }
120
+ function liftAgent(state, [slice, cmds]) {
121
+ return [{ ...state, agent: slice }, cmds];
122
+ }
123
+
124
+ // src/agent/types.ts
125
+ function isToolCall(value) {
126
+ if (value === null || typeof value !== "object") return false;
127
+ const t = value;
128
+ return typeof t.callId === "string" && typeof t.name === "string" && t.args !== null && typeof t.args === "object";
129
+ }
130
+ function isAgentTurn(value) {
131
+ if (value === null || typeof value !== "object") return false;
132
+ const t = value;
133
+ return typeof t.content === "string" && Array.isArray(t.toolCalls) && t.toolCalls.every(isToolCall);
134
+ }
135
+ var agentTurnSchema = schemaFromGuard(
136
+ isAgentTurn,
137
+ "an AgentTurn"
138
+ );
38
139
  function status(s) {
39
140
  if (s.failure !== null) {
40
141
  return { kind: "failed", failure: s.failure };
41
142
  }
42
- if (s.run.phase === "failed" && s.run.failure !== null) {
143
+ if (s.run.phase === "failed") {
43
144
  return { kind: "failed", failure: s.run.failure };
44
145
  }
45
146
  if (s.run.phase === "done") {
@@ -50,6 +151,8 @@ function status(s) {
50
151
  }
51
152
  return { kind: "running" };
52
153
  }
154
+
155
+ // src/agent/index.ts
53
156
  function createAgent(config) {
54
157
  const rng = config.rng ?? Math.random;
55
158
  const run = createMonitoredRun({
@@ -83,38 +186,6 @@ function createAgent(config) {
83
186
  output: null
84
187
  };
85
188
  }
86
- function freshConversation() {
87
- return {
88
- turns: [],
89
- toolRecords: [],
90
- turnCount: 0,
91
- awaiting: { kind: "llm" }
92
- };
93
- }
94
- function dedupeByCallId(calls) {
95
- const seen = /* @__PURE__ */ new Set();
96
- const out = [];
97
- for (const call of calls) {
98
- if (seen.has(call.callId)) continue;
99
- seen.add(call.callId);
100
- out.push(call);
101
- }
102
- return out;
103
- }
104
- function foldSummary(conv, folding, summaryTurn) {
105
- const shift = folding - 1;
106
- const survivingTurns = conv.turns.slice(folding);
107
- const survivingRecords = conv.toolRecords.filter((rec) => rec.turn >= folding).map((rec) => ({ ...rec, turn: rec.turn - shift }));
108
- return {
109
- ...conv,
110
- turns: [summaryTurn, ...survivingTurns],
111
- toolRecords: survivingRecords,
112
- awaiting: { kind: "llm" }
113
- };
114
- }
115
- function currentStage(s) {
116
- return s.run.stepStates.find((i) => i.status === "running")?.input;
117
- }
118
189
  function brainCall(s) {
119
190
  const stage = currentStage(s);
120
191
  const conversation = s.conversation ?? freshConversation();
@@ -124,6 +195,29 @@ function createAgent(config) {
124
195
  payload: config.payloadOf ? config.payloadOf(stage, conversation) : null
125
196
  };
126
197
  }
198
+ function fireBrainCall(s, at) {
199
+ const call = brainCall(s);
200
+ const [resilience, cmds] = llm.attempt(s.resilience, call, at);
201
+ return [{ ...s, resilience }, cmds];
202
+ }
203
+ function compactionCall(conv, folding) {
204
+ const payloadOf = config.compaction?.payloadOf;
205
+ return {
206
+ purpose: COMPACTION_PURPOSE,
207
+ model: config.modelId ?? null,
208
+ payload: payloadOf ? payloadOf(conv, folding) : null
209
+ };
210
+ }
211
+ function reKeyCompactCmds(result) {
212
+ const [slice, cmds] = result;
213
+ return [slice, cmds.map(toCompactRunCmd)];
214
+ }
215
+ const compact = {
216
+ attempt: (slice, key, input, at) => reKeyCompactCmds(compactRc.attempt(slice, key, input, at)),
217
+ succeed: (slice, key, msg) => reKeyCompactCmds(compactRc.succeed(slice, key, msg)),
218
+ fail: (slice, key, msg) => reKeyCompactCmds(compactRc.fail(slice, key, msg)),
219
+ onTimer: (slice, msg) => reKeyCompactCmds(compactRc.onTimer(slice, msg))
220
+ };
127
221
  function start(s, runId, at) {
128
222
  const [runSlice] = run.start(s.run, runId, at);
129
223
  const conversation = freshConversation();
@@ -140,14 +234,11 @@ function createAgent(config) {
140
234
  // inherits the previous run's summarize retry bookkeeping (#85).
141
235
  compaction: compactRc.init()
142
236
  };
143
- const call = brainCall(withRun);
144
- const [resilience, cmds] = llm.attempt(withRun.resilience, call, at);
145
- return [{ ...withRun, resilience }, cmds];
237
+ return fireBrainCall(withRun, at);
146
238
  }
147
239
  function turn(s, result, at) {
148
- if (isSettled(s)) return [s, []];
149
- const conv = s.conversation;
150
- if (conv === null || conv.awaiting.kind !== "llm") return [s, []];
240
+ const conv = requireAwaiting(s, "llm");
241
+ if (conv === null) return [s, []];
151
242
  const withTurn = {
152
243
  ...conv,
153
244
  turns: [...conv.turns, result]
@@ -187,9 +278,8 @@ function createAgent(config) {
187
278
  conversation,
188
279
  tools: initFanOut()
189
280
  };
190
- const call = brainCall(moved);
191
- const [resilience, llmCmds] = llm.attempt(moved.resilience, call, at);
192
- return [{ ...moved, resilience }, [...runCmds, ...llmCmds]];
281
+ const [fired, llmCmds] = fireBrainCall(moved, at);
282
+ return [fired, [...runCmds, ...llmCmds]];
193
283
  }
194
284
  function toolOk(s, callId, result, at) {
195
285
  return settleTool(s, callId, { kind: "ok", result }, at);
@@ -198,9 +288,8 @@ function createAgent(config) {
198
288
  return settleTool(s, callId, { kind: "error", reason }, at);
199
289
  }
200
290
  function settleTool(s, callId, outcome, at) {
201
- if (isSettled(s)) return [s, []];
202
- const conv = s.conversation;
203
- if (conv === null || conv.awaiting.kind !== "tools") return [s, []];
291
+ const conv = requireAwaiting(s, "tools");
292
+ if (conv === null) return [s, []];
204
293
  const call = s.tools.running.find((c) => c.callId === callId);
205
294
  if (call === void 0) return [s, []];
206
295
  const [tools, launchCmds] = outcome.kind === "ok" ? fan.itemOk(s.tools, callId, outcome) : fan.itemErr(s.tools, callId, outcome);
@@ -240,10 +329,8 @@ function createAgent(config) {
240
329
  const compacting = maybeCompact(drained, at);
241
330
  if (compacting !== null) return compacting;
242
331
  const [runSlice] = run.progress(drained.run, void 0, at);
243
- const fired = { ...drained, run: runSlice };
244
- const nextCall = brainCall(fired);
245
- const [resilience, llmCmds] = llm.attempt(fired.resilience, nextCall, at);
246
- return [{ ...fired, resilience }, [...launchCmds, ...llmCmds]];
332
+ const [fired, llmCmds] = fireBrainCall({ ...drained, run: runSlice }, at);
333
+ return [fired, [...launchCmds, ...llmCmds]];
247
334
  }
248
335
  function maybeCompact(s, at) {
249
336
  const policy = config.compaction;
@@ -257,30 +344,18 @@ function createAgent(config) {
257
344
  awaiting: { kind: "compacting", folding }
258
345
  };
259
346
  const [runSlice] = run.progress(s.run, void 0, at);
260
- const input = {
261
- purpose: COMPACTION_PURPOSE,
262
- model: config.modelId ?? null,
263
- payload: policy.payloadOf ? policy.payloadOf(conv, folding) : null
264
- };
265
- const [compaction, runCmds] = compactRc.attempt(
347
+ const input = compactionCall(conv, folding);
348
+ const [compaction, cmds] = compact.attempt(
266
349
  s.compaction,
267
350
  COMPACTION_PURPOSE,
268
351
  input,
269
352
  at
270
353
  );
271
- const cmds = runCmds.map(toCompactRunCmd);
272
354
  return [
273
355
  { ...s, run: runSlice, conversation: compactConv, compaction },
274
356
  cmds
275
357
  ];
276
358
  }
277
- function toCompactRunCmd(cmd) {
278
- return {
279
- type: MsgType.CompactRun,
280
- key: COMPACTION_PURPOSE,
281
- input: cmd.input
282
- };
283
- }
284
359
  function failTurnLimit(s, at) {
285
360
  return [{ ...s, failure: { reason: "turn_limit", at } }, []];
286
361
  }
@@ -297,34 +372,29 @@ function createAgent(config) {
297
372
  return [{ ...s, resilience, failure }, cmds];
298
373
  }
299
374
  function compactOk(s, key, msg, at) {
300
- const [compaction, retryCmds] = compactRc.succeed(s.compaction, key, {
375
+ const [compaction, retryCmds] = compact.succeed(s.compaction, key, {
301
376
  type: MsgType.ResilientOk,
302
377
  key: msg.key,
303
378
  result: msg.result,
304
379
  at: msg.at
305
380
  });
306
381
  const settledRetry = { ...s, compaction };
307
- if (isSettled(settledRetry)) return [settledRetry, []];
308
- const conv = settledRetry.conversation;
309
- if (conv === null || conv.awaiting.kind !== "compacting") {
310
- return [settledRetry, retryCmds.map(toCompactRunCmd)];
311
- }
382
+ const conv = requireAwaiting(settledRetry, "compacting");
383
+ if (conv === null) return [settledRetry, retryCmds];
312
384
  const folding = conv.awaiting.folding;
313
385
  const summaryTurn = {
314
386
  content: msg.result.output.summary,
315
387
  toolCalls: []
316
388
  };
317
389
  const foldedConv = foldSummary(conv, folding, summaryTurn);
318
- const fired = { ...settledRetry, conversation: foldedConv };
319
- const nextCall = brainCall(fired);
320
- const [resilience, llmCmds] = llm.attempt(fired.resilience, nextCall, at);
321
- return [
322
- { ...fired, resilience },
323
- [...retryCmds.map(toCompactRunCmd), ...llmCmds]
324
- ];
390
+ const [fired, llmCmds] = fireBrainCall(
391
+ { ...settledRetry, conversation: foldedConv },
392
+ at
393
+ );
394
+ return [fired, [...retryCmds, ...llmCmds]];
325
395
  }
326
396
  function compactErr(s, key, msg, at) {
327
- const [compaction, cmds] = compactRc.fail(s.compaction, key, {
397
+ const [compaction, cmds] = compact.fail(s.compaction, key, {
328
398
  type: MsgType.ResilientErr,
329
399
  key: msg.key,
330
400
  error: msg.error,
@@ -333,21 +403,16 @@ function createAgent(config) {
333
403
  const settledRetry = { ...s, compaction };
334
404
  const call = compaction.calls[key];
335
405
  if (call?.phase === "waiting_retry") {
336
- return [settledRetry, cmds.map(toCompactRunCmd)];
337
- }
338
- if (isSettled(settledRetry)) return [settledRetry, []];
339
- const conv = settledRetry.conversation;
340
- if (conv === null || conv.awaiting.kind !== "compacting") {
341
- return [settledRetry, cmds.map(toCompactRunCmd)];
406
+ return [settledRetry, cmds];
342
407
  }
408
+ const conv = requireAwaiting(settledRetry, "compacting");
409
+ if (conv === null) return [settledRetry, cmds];
343
410
  const resumedConv = { ...conv, awaiting: { kind: "llm" } };
344
- const fired = { ...settledRetry, conversation: resumedConv };
345
- const nextCall = brainCall(fired);
346
- const [resilience, llmCmds] = llm.attempt(fired.resilience, nextCall, at);
347
- return [
348
- { ...fired, resilience },
349
- [...cmds.map(toCompactRunCmd), ...llmCmds]
350
- ];
411
+ const [fired, llmCmds] = fireBrainCall(
412
+ { ...settledRetry, conversation: resumedConv },
413
+ at
414
+ );
415
+ return [fired, [...cmds, ...llmCmds]];
351
416
  }
352
417
  function onTimer(s, msg) {
353
418
  if (msg.id.startsWith("monitored:")) {
@@ -355,8 +420,8 @@ function createAgent(config) {
355
420
  return [{ ...s, run: runSlice }, []];
356
421
  }
357
422
  if (msg.id.endsWith(`:${COMPACTION_PURPOSE}`)) {
358
- const [compaction, cmds2] = compactRc.onTimer(s.compaction, msg);
359
- return [{ ...s, compaction }, cmds2.map(toCompactRunCmd)];
423
+ const [compaction, cmds2] = compact.onTimer(s.compaction, msg);
424
+ return [{ ...s, compaction }, cmds2];
360
425
  }
361
426
  const [resilience, cmds] = llm.onTimer(s.resilience, msg);
362
427
  return [{ ...s, resilience }, cmds];
@@ -368,30 +433,21 @@ function createAgent(config) {
368
433
  const conv = rebooted.conversation;
369
434
  if (conv === null) return [rebooted, []];
370
435
  if (conv.awaiting.kind === "llm") {
371
- const call = brainCall(rebooted);
372
- const [resilience, cmds2] = llm.attempt(rebooted.resilience, call, at);
373
- return [{ ...rebooted, resilience }, cmds2];
436
+ return fireBrainCall(rebooted, at);
374
437
  }
375
438
  if (conv.awaiting.kind === "compacting") {
376
- const input = {
377
- purpose: COMPACTION_PURPOSE,
378
- model: config.modelId ?? null,
379
- payload: config.compaction?.payloadOf ? config.compaction.payloadOf(conv, conv.awaiting.folding) : null
380
- };
381
- const [compaction, cmds2] = compactRc.attempt(
439
+ const input = compactionCall(conv, conv.awaiting.folding);
440
+ const [compaction, cmds2] = compact.attempt(
382
441
  rebooted.compaction,
383
442
  COMPACTION_PURPOSE,
384
443
  input,
385
444
  at
386
445
  );
387
- return [{ ...rebooted, compaction }, cmds2.map(toCompactRunCmd)];
446
+ return [{ ...rebooted, compaction }, cmds2];
388
447
  }
389
448
  const cmds = rebooted.tools.running.map((call) => config.toolOf(call));
390
449
  return [rebooted, cmds];
391
450
  }
392
- function isSettled(s) {
393
- return s.run.phase === "done" || s.run.phase === "failed" || s.failure !== null;
394
- }
395
451
  function subs(s) {
396
452
  return [
397
453
  ...llm.subs(s.resilience),
@@ -457,50 +513,7 @@ function createAgent(config) {
457
513
  unsafeDetachedHandlers
458
514
  };
459
515
  }
460
- function agentBootMsg(at) {
461
- return { type: MsgType.AgentBoot, at };
462
- }
463
- function agentEvents() {
464
- return (msg, state) => {
465
- const events = [];
466
- switch (msg.type) {
467
- case MsgType.ResilientOk:
468
- events.push({ type: "TurnSettled", turn: msg.result.output });
469
- break;
470
- case MsgType.AgentToolOk:
471
- events.push({
472
- type: "ToolSettled",
473
- callId: msg.callId,
474
- result: msg.result
475
- });
476
- break;
477
- // start / boot / timer / the *_err arms / the compaction settles carry no
478
- // public event — return []. (No `default`: the switch is exhaustive over
479
- // the Msg discriminant, so a new Msg variant forces a decision here at
480
- // compile time.) Compaction is an internal optimization, not a semantic
481
- // lifecycle moment a UI folds — its settles are deliberately silent here.
482
- case MsgType.AgentStart:
483
- case MsgType.AgentToolErr:
484
- case MsgType.ResilientErr:
485
- case MsgType.CompactOk:
486
- case MsgType.CompactErr:
487
- case "deadline_exceeded":
488
- case MsgType.AgentBoot:
489
- break;
490
- }
491
- if (state.run.phase === "done") {
492
- events.push({ type: "RunDone", output: state.output });
493
- }
494
- return events;
495
- };
496
- }
497
- function mergeInterpret(a, b) {
498
- return { ...a ?? {}, ...b };
499
- }
500
- function liftAgent(state, [slice, cmds]) {
501
- return [{ ...state, agent: slice }, cmds];
502
- }
503
516
 
504
517
  export { COMPACTION_PURPOSE, agentBootMsg, agentEvents, agentTurnSchema, compactionSummarySchema, createAgent, isAgentTurn, isCompactionSummary, liftAgent, mergeInterpret, status };
505
- //# sourceMappingURL=chunk-S3FQN4SL.js.map
506
- //# sourceMappingURL=chunk-S3FQN4SL.js.map
518
+ //# sourceMappingURL=chunk-WGGAMW7Y.js.map
519
+ //# sourceMappingURL=chunk-WGGAMW7Y.js.map