@demlik/tea 0.2.0 → 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.
- package/README.md +49 -0
- package/dist/agent/index.d.ts +16 -770
- package/dist/agent/index.js +18 -16
- package/dist/authed-call/index.d.ts +3 -3
- package/dist/authed-call/index.js +22 -36
- package/dist/authed-call/index.js.map +1 -1
- package/dist/await-terminal/index.d.ts +2 -2
- package/dist/await-terminal/index.js +2 -2
- package/dist/batch-window/index.d.ts +3 -3
- package/dist/batch-window/index.js +3 -3
- package/dist/cache/index.d.ts +2 -2
- package/dist/cache/index.js +5 -2
- package/dist/{chunk-3545BOYL.js → chunk-25LBDCBC.js} +6 -5
- package/dist/chunk-25LBDCBC.js.map +1 -0
- package/dist/{chunk-IN33Q4VJ.js → chunk-2KNLKQVN.js} +3 -3
- package/dist/{chunk-IN33Q4VJ.js.map → chunk-2KNLKQVN.js.map} +1 -1
- package/dist/{chunk-6VDCTR7N.js → chunk-BP2R7TPP.js} +203 -141
- package/dist/chunk-BP2R7TPP.js.map +1 -0
- package/dist/chunk-C6QC273X.js +71 -0
- package/dist/chunk-C6QC273X.js.map +1 -0
- package/dist/{chunk-ZDRN3YCE.js → chunk-CAXMQJJ3.js} +17 -24
- package/dist/chunk-CAXMQJJ3.js.map +1 -0
- package/dist/{chunk-UV6UWGEO.js → chunk-D736QUGF.js} +6 -5
- package/dist/chunk-D736QUGF.js.map +1 -0
- package/dist/chunk-EWJP3YYP.js +21 -0
- package/dist/chunk-EWJP3YYP.js.map +1 -0
- package/dist/{chunk-TGG26XMK.js → chunk-FV3Z5MBE.js} +10 -5
- package/dist/chunk-FV3Z5MBE.js.map +1 -0
- package/dist/chunk-G5E74IJB.js +8 -0
- package/dist/chunk-G5E74IJB.js.map +1 -0
- package/dist/{chunk-6GIGQT6C.js → chunk-IXBKFQH5.js} +12 -11
- package/dist/chunk-IXBKFQH5.js.map +1 -0
- package/dist/{chunk-G2U7DJHL.js → chunk-JFFVBEDI.js} +3 -3
- package/dist/{chunk-G2U7DJHL.js.map → chunk-JFFVBEDI.js.map} +1 -1
- package/dist/chunk-LYLDCIPH.js +365 -0
- package/dist/chunk-LYLDCIPH.js.map +1 -0
- package/dist/{chunk-HJO6VNQI.js → chunk-MU6MCIAC.js} +3 -3
- package/dist/{chunk-HJO6VNQI.js.map → chunk-MU6MCIAC.js.map} +1 -1
- package/dist/{chunk-UYWUVEP6.js → chunk-MUUROGIH.js} +2 -2
- package/dist/chunk-MUUROGIH.js.map +1 -0
- package/dist/{chunk-5KVHID6A.js → chunk-MVY5DAMF.js} +4 -4
- package/dist/{chunk-5KVHID6A.js.map → chunk-MVY5DAMF.js.map} +1 -1
- package/dist/{chunk-EGWTT5XJ.js → chunk-NPERUUUE.js} +76 -95
- package/dist/chunk-NPERUUUE.js.map +1 -0
- package/dist/{chunk-DYRKDCJI.js → chunk-PBDYW2TU.js} +41 -7
- package/dist/chunk-PBDYW2TU.js.map +1 -0
- package/dist/{chunk-5VNWZADP.js → chunk-Q2UM7BXU.js} +18 -7
- package/dist/chunk-Q2UM7BXU.js.map +1 -0
- package/dist/chunk-RMX4EOQK.js +26 -0
- package/dist/chunk-RMX4EOQK.js.map +1 -0
- package/dist/{chunk-2YOE5PIS.js → chunk-S2EGRPCM.js} +8 -6
- package/dist/chunk-S2EGRPCM.js.map +1 -0
- package/dist/{chunk-IK4S5DJN.js → chunk-S4O26D7B.js} +7 -14
- package/dist/chunk-S4O26D7B.js.map +1 -0
- package/dist/{chunk-6OUDPSSG.js → chunk-SK5JGUY7.js} +3 -3
- package/dist/{chunk-6OUDPSSG.js.map → chunk-SK5JGUY7.js.map} +1 -1
- package/dist/{chunk-FOGL3TC7.js → chunk-TPGASTFF.js} +8 -10
- package/dist/chunk-TPGASTFF.js.map +1 -0
- package/dist/{chunk-SC3FAFZN.js → chunk-UF4MZSLT.js} +2 -2
- package/dist/chunk-UF4MZSLT.js.map +1 -0
- package/dist/{chunk-M6UZT7HS.js → chunk-VBUCHVCV.js} +6 -5
- package/dist/chunk-VBUCHVCV.js.map +1 -0
- package/dist/{chunk-VFGKTGGC.js → chunk-WGGAMW7Y.js} +191 -178
- package/dist/chunk-WGGAMW7Y.js.map +1 -0
- package/dist/chunk-ZJ5AOYBH.js +8 -0
- package/dist/chunk-ZJ5AOYBH.js.map +1 -0
- package/dist/{chunk-LEC4U6CX.js → chunk-ZVJZ72HY.js} +3 -3
- package/dist/{chunk-LEC4U6CX.js.map → chunk-ZVJZ72HY.js.map} +1 -1
- package/dist/circuit-breaker/index.d.ts +1 -0
- package/dist/circuit-breaker/index.js +1 -1
- package/dist/{core-BmlJOvxM.d.ts → core-42kapW0n.d.ts} +96 -6
- package/dist/deadline/index.d.ts +77 -11
- package/dist/deadline/index.js +3 -3
- package/dist/devtools/index.js +8 -28
- package/dist/devtools/index.js.map +1 -1
- package/dist/do/index.d.ts +52 -24
- package/dist/do/index.js +94 -92
- package/dist/do/index.js.map +1 -1
- package/dist/{durable-effects-9LxFpmh3.d.ts → durable-effects-CFRngTNA.d.ts} +3 -3
- package/dist/extension/index.d.ts +25 -7
- package/dist/extension/index.js +4 -3
- package/dist/extension/index.js.map +1 -1
- package/dist/extension/react.js +4 -3
- package/dist/extension/react.js.map +1 -1
- package/dist/extension/subs/index.d.ts +2 -2
- package/dist/extension/subs/index.js +7 -10
- package/dist/extension/subs/index.js.map +1 -1
- package/dist/fan-out/index.d.ts +10 -10
- package/dist/fan-out/index.js +3 -3
- package/dist/idempotent-intake/index.d.ts +2 -2
- package/dist/idempotent-intake/index.js +3 -3
- package/dist/index.d.ts +50 -489
- package/dist/index.js +2 -2
- package/dist/llm-call/index.d.ts +3 -2
- package/dist/llm-call/index.js +12 -10
- package/dist/machine-B_BF4jij.d.ts +804 -0
- package/dist/machine-viz/index.d.ts +1 -1
- package/dist/machine-viz/index.js +2 -2
- package/dist/mem/index.d.ts +3 -2
- package/dist/mem/index.js.map +1 -1
- package/dist/monitored-run/index.d.ts +54 -30
- package/dist/monitored-run/index.js +7 -7
- package/dist/node/index.d.ts +3 -2
- package/dist/node/index.js +2 -3
- package/dist/node/index.js.map +1 -1
- package/dist/paginated-walk/index.d.ts +2 -2
- package/dist/paginated-walk/index.js +11 -10
- package/dist/parity/index.d.ts +6 -5
- package/dist/parity/index.js +5 -4
- package/dist/parity/index.js.map +1 -1
- package/dist/pbt/arbitraries/index.d.ts +1 -1
- package/dist/pbt/index.d.ts +1 -1
- package/dist/pbt/index.js +3 -3
- package/dist/pbt/index.js.map +1 -1
- package/dist/pbt/runners/index.d.ts +1 -1
- package/dist/pbt/runners/index.js +3 -3
- package/dist/poller/index.d.ts +59 -17
- package/dist/poller/index.js +56 -20
- package/dist/poller/index.js.map +1 -1
- package/dist/prediction/index.d.ts +1 -1
- package/dist/prediction/index.js +2 -2
- package/dist/pure/index.d.ts +1 -1
- package/dist/pure/index.js +2 -2
- package/dist/rate-limit/index.d.ts +2 -2
- package/dist/rate-limit/index.js +1 -1
- package/dist/react/index.d.ts +3 -2
- package/dist/react/index.js +2 -2
- package/dist/react/index.js.map +1 -1
- package/dist/reconciler/index.d.ts +2 -2
- package/dist/reconciler/index.js +13 -15
- package/dist/reconciler/index.js.map +1 -1
- package/dist/recorder/index.d.ts +20 -20
- package/dist/recorder/index.js +1 -1
- package/dist/resilient-call/index.d.ts +31 -6
- package/dist/resilient-call/index.js +10 -9
- package/dist/retry-backoff/index.d.ts +191 -14
- package/dist/retry-backoff/index.js +1 -1
- package/dist/retry-to-success/index.js +1 -1
- package/dist/runtime-types-gsog27b6.d.ts +337 -0
- package/dist/saga/index.d.ts +3 -2
- package/dist/saga/index.js +2 -2
- package/dist/saga/index.js.map +1 -1
- package/dist/snapshot/index.d.ts +1 -1
- package/dist/snapshot/index.js +3 -3
- package/dist/subs/index.d.ts +372 -4
- package/dist/subs/index.js +4 -1
- package/dist/testing/index.d.ts +2 -2
- package/dist/testing/index.js +2 -2
- package/dist/throttled-input/index.d.ts +2 -2
- package/dist/throttled-input/index.js +14 -13
- package/dist/throttled-input/index.js.map +1 -1
- package/dist/token-refresh/index.d.ts +1 -1
- package/dist/token-refresh/index.js +3 -3
- package/dist/trace-replay/index.d.ts +12 -7
- package/dist/trace-replay/index.js +4 -3
- package/dist/{types-BoUWgwSm.d.ts → types-KbL3W1mj.d.ts} +1 -1
- package/dist/with-deadline/index.d.ts +1 -1
- package/dist/with-deadline/index.js +2 -2
- package/dist/with-resilience/index.d.ts +21 -2
- package/dist/with-resilience/index.js +12 -23
- package/dist/with-resilience/index.js.map +1 -1
- package/dist/with-telemetry/index.d.ts +1 -1
- package/dist/with-telemetry/index.js +2 -2
- package/dist/work-queue/adapter.d.ts +2 -2
- package/dist/work-queue/adapter.js +2 -2
- package/dist/work-queue/index.d.ts +3 -2
- package/dist/work-queue/index.js +1 -1
- package/dist/work-queue/index.js.map +1 -1
- package/dist/work-queue/ops.d.ts +2 -2
- package/dist/work-queue/ops.js +1 -1
- package/dist/workflow/index.d.ts +51 -19
- package/dist/workflow/index.js +71 -52
- package/dist/workflow/index.js.map +1 -1
- package/package.json +4 -1
- package/dist/chunk-2YOE5PIS.js.map +0 -1
- package/dist/chunk-3545BOYL.js.map +0 -1
- package/dist/chunk-5VNWZADP.js.map +0 -1
- package/dist/chunk-6GIGQT6C.js.map +0 -1
- package/dist/chunk-6VDCTR7N.js.map +0 -1
- package/dist/chunk-DYRKDCJI.js.map +0 -1
- package/dist/chunk-EGWTT5XJ.js.map +0 -1
- package/dist/chunk-EMJTPHI7.js +0 -91
- package/dist/chunk-EMJTPHI7.js.map +0 -1
- package/dist/chunk-FOGL3TC7.js.map +0 -1
- package/dist/chunk-IK4S5DJN.js.map +0 -1
- package/dist/chunk-M6UZT7HS.js.map +0 -1
- package/dist/chunk-NSDFA6M6.js +0 -198
- package/dist/chunk-NSDFA6M6.js.map +0 -1
- package/dist/chunk-SC3FAFZN.js.map +0 -1
- package/dist/chunk-SKAMZKTE.js +0 -20
- package/dist/chunk-SKAMZKTE.js.map +0 -1
- package/dist/chunk-TGG26XMK.js.map +0 -1
- package/dist/chunk-UV6UWGEO.js.map +0 -1
- package/dist/chunk-UYWUVEP6.js.map +0 -1
- package/dist/chunk-VFGKTGGC.js.map +0 -1
- package/dist/chunk-ZDRN3YCE.js.map +0 -1
|
@@ -1,45 +1,146 @@
|
|
|
1
|
-
import { createMonitoredRun } from './chunk-
|
|
2
|
-
import { createLlmCall } from './chunk-
|
|
3
|
-
import { createResilientCall } from './chunk-
|
|
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-
|
|
6
|
-
import { subscribeDeadline } from './chunk-
|
|
7
|
-
import { defineMachine } from './chunk-
|
|
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/
|
|
10
|
-
function
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
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"
|
|
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
|
-
|
|
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
|
-
|
|
149
|
-
|
|
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
|
|
191
|
-
|
|
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
|
-
|
|
202
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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] =
|
|
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
|
-
|
|
308
|
-
|
|
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
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
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] =
|
|
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
|
|
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
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
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] =
|
|
359
|
-
return [{ ...s, compaction }, cmds2
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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-
|
|
506
|
-
//# sourceMappingURL=chunk-
|
|
518
|
+
//# sourceMappingURL=chunk-WGGAMW7Y.js.map
|
|
519
|
+
//# sourceMappingURL=chunk-WGGAMW7Y.js.map
|