effect-durable-agent 0.1.0-alpha.21 → 0.1.0-alpha.22
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 +2 -2
- package/dist/assert-never-CoyUpoRB.js +18 -0
- package/dist/assert-never-CoyUpoRB.js.map +1 -0
- package/dist/domain/command-queues.d.ts +1 -1
- package/dist/domain/message-transcript.d.ts +1 -1
- package/dist/domain/reduced-state.d.ts +1 -1
- package/dist/domain/reduced-state.js +927 -2
- package/dist/domain/reduced-state.js.map +1 -0
- package/dist/domain/run-scheduling.d.ts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/{reduced-state-BGd_yCBT.d.ts → reduced-state-PJdig48e.d.ts} +4171 -4171
- package/dist/{reducer-registry-BIhncOHH.d.ts → reducer-registry-BAbhZs6d.d.ts} +2 -2
- package/dist/{run-scheduling-wakeup-BH2pHE0U.d.ts → run-scheduling-wakeup-QLK87OqG.d.ts} +2 -2
- package/dist/services/compaction.d.ts +1 -1
- package/dist/services/model-call.d.ts +1 -1
- package/dist/services/prompt-projector.d.ts +1 -1
- package/dist/services/reducer-registry.d.ts +1 -1
- package/dist/services/reducer-registry.js +1 -1
- package/dist/services/run-scheduler.d.ts +1 -1
- package/dist/services/run-scheduling-wakeup.d.ts +1 -1
- package/dist/services/runtime-layer.d.ts +6 -6
- package/dist/services/runtime-layer.js +3 -3
- package/dist/services/runtime.d.ts +1 -1
- package/dist/services/runtime.js +1 -1
- package/dist/services/session-query.d.ts +1 -1
- package/dist/services/session-query.js +2 -2
- package/dist/services/session-state.d.ts +1 -1
- package/dist/services/session-state.js +1 -1
- package/dist/services/session-store.d.ts +2 -2
- package/dist/services/session-store.js +1 -251
- package/dist/services/sink-checkpoint-store.d.ts +1 -1
- package/dist/services/sink-registry.d.ts +1 -1
- package/dist/services/sink-registry.js +1 -1
- package/dist/services/tool-registry.d.ts +1 -1
- package/dist/{session-state-C_1XxhvE.js → session-state-StlO5B2N.js} +5 -4
- package/dist/{session-state-C_1XxhvE.js.map → session-state-StlO5B2N.js.map} +1 -1
- package/dist/session-store-ByDjS03N.d.ts +3184 -0
- package/dist/session-store-DcJjs4Fd.js +413 -0
- package/dist/session-store-DcJjs4Fd.js.map +1 -0
- package/dist/{sink-checkpoint-store-C-SY0cG2.d.ts → sink-checkpoint-store-BHb8YRTw.d.ts} +2 -2
- package/dist/{sink-registry-CjK5MC90.js → sink-registry-DEneTH8u.js} +3 -3
- package/dist/{sink-registry-CjK5MC90.js.map → sink-registry-DEneTH8u.js.map} +1 -1
- package/dist/testkit/layers.d.ts +6 -6
- package/dist/testkit/layers.js +3 -3
- package/dist/{tool-registry-wPsHagF_.d.ts → tool-registry-CxZ0iuYb.d.ts} +2 -2
- package/dist/transcript.d.ts +137 -0
- package/dist/transcript.js +178 -0
- package/dist/transcript.js.map +1 -0
- package/dist/websocket.d.ts +1 -1
- package/dist/window-read-B8aF_hDq.js +2173 -0
- package/dist/window-read-B8aF_hDq.js.map +1 -0
- package/package.json +5 -1
- package/dist/reduced-state-Vuud6LCM.js +0 -942
- package/dist/reduced-state-Vuud6LCM.js.map +0 -1
- package/dist/services/session-store.js.map +0 -1
- package/dist/session-store-BpitF4Wg.d.ts +0 -191
|
@@ -1,3 +1,928 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ContextVersion, SequenceNumber } from "../types/core.js";
|
|
2
|
+
import { RunSchedulingRequest, RunSchedulingRequestRecord } from "./run-scheduling.js";
|
|
3
|
+
import "../envelope-BCzKUq_z.js";
|
|
4
|
+
import { TokenConsumptionState, addModelUsage } from "./model-usage.js";
|
|
5
|
+
import { $n as pendingMessagesPausedEventType, An as baseStateCreatedEventType, Bn as compactionRequestedEventType, Cr as turnStartedEventType, Dn as assistantMessageCommittedEventType, Dr as userMessagePromotedEventType, Er as userMessageCommittedEventType, Fn as commandCompletedEventType, Hn as contextProjectedEventType, In as commandFailedEventType, Jn as inferenceCompletedEventType, Ln as commandStartedEventType, Mn as baseStateRequestedEventType, Nn as commandAdmittedEventType, On as assistantMessageImportedEventType, Or as userMessageSubmittedEventType, Pn as commandCancelledEventType, Qn as messageQueuePausedEventType, Rn as compactionCompletedEventType, Sr as turnFailedEventType, Tr as userMessageCancelledEventType, Un as contextRebasedEventType, Vn as compactionStartedEventType, Xn as inferenceStartedEventType, Yn as inferenceFailedEventType, Zn as interpretLegacyAttemptForReduction, _r as toolCallCreatedEventType, ar as runSchedulingGrantedEventType, br as toolCallStartedEventType, cr as runStartedEventType, dr as steeringMessageQueuedEventType, er as recoveryCompletedEventType, fr as stopTurnAppliedEventType, gr as toolCallCompletedEventType, hr as systemMessageCommittedEventType, ir as runSchedulingDeliveredEventType, jn as baseStateFailedEventType, kn as assistantPartialCommittedEventType, lr as sessionConfiguredEventType, mr as summaryCreatedEventType, nr as runFailedEventType, or as runSchedulingInvalidatedEventType, pr as stopTurnRequestedEventType, rr as runInterruptedEventType, sr as runSchedulingRequestedEventType, tr as runCompletedEventType, ur as steeringMessageCancelledEventType, vr as toolCallFailedEventType, wr as turnStoppedEventType, xr as turnCompletedEventType, yr as toolCallRejectedEventType, zn as compactionFailedEventType } from "../durable-CD__3ztN.js";
|
|
6
|
+
import { t as assertNever } from "../assert-never-CoyUpoRB.js";
|
|
7
|
+
import { deriveCommandQueues, emptyCommandQueues } from "./command-queues.js";
|
|
8
|
+
import * as Schema from "effect/Schema";
|
|
9
|
+
//#region src/domain/reduced-state.ts
|
|
10
|
+
const emptyTokenConsumptionState = { byModel: [] };
|
|
11
|
+
/** Empty durable replay state before any session event has committed. */
|
|
12
|
+
const initialReducedState = {
|
|
13
|
+
lastSeq: SequenceNumber.make(0),
|
|
14
|
+
commands: /* @__PURE__ */ new Map(),
|
|
15
|
+
runs: /* @__PURE__ */ new Map(),
|
|
16
|
+
recoveryContinuations: /* @__PURE__ */ new Map(),
|
|
17
|
+
turns: /* @__PURE__ */ new Map(),
|
|
18
|
+
inferences: /* @__PURE__ */ new Map(),
|
|
19
|
+
toolCalls: /* @__PURE__ */ new Map(),
|
|
20
|
+
messages: /* @__PURE__ */ new Map(),
|
|
21
|
+
stopRequests: /* @__PURE__ */ new Map(),
|
|
22
|
+
compactions: /* @__PURE__ */ new Map(),
|
|
23
|
+
context: { version: ContextVersion.make(0) },
|
|
24
|
+
tokenConsumption: emptyTokenConsumptionState,
|
|
25
|
+
commandQueues: emptyCommandQueues
|
|
26
|
+
};
|
|
27
|
+
/** Reserved reducer checkpoint name for the framework-owned `ReducedState`. */
|
|
28
|
+
const frameworkReducedStateReducerName = "_eda.framework.reduced-state";
|
|
29
|
+
/** Schema version for the framework-owned `ReducedState` checkpoint payload. */
|
|
30
|
+
const frameworkReducedStateReducerSchemaVersion = 7;
|
|
31
|
+
/** Encode the framework reducer state into a pointer-based JSON-safe checkpoint payload. */
|
|
32
|
+
const encodeReducedStateCheckpoint = (state) => ({
|
|
33
|
+
lastSeq: state.lastSeq,
|
|
34
|
+
...state.runSchedulingRequest === void 0 ? {} : { runSchedulingRequest: state.runSchedulingRequest },
|
|
35
|
+
...state.modelSelection === void 0 ? {} : { modelSelection: state.modelSelection },
|
|
36
|
+
commands: Array.from(state.commands.entries()).map(([commandId, record]) => [commandId, encodeCheckpointCommandRecord(record)]),
|
|
37
|
+
runs: Array.from(state.runs.entries()),
|
|
38
|
+
recoveryContinuations: Array.from(state.recoveryContinuations.entries()),
|
|
39
|
+
turns: Array.from(state.turns.entries()),
|
|
40
|
+
inferences: Array.from(state.inferences.entries()),
|
|
41
|
+
toolCalls: Array.from(state.toolCalls.entries()).map(([toolCallId, record]) => [toolCallId, encodeCheckpointToolCallRecord(record)]),
|
|
42
|
+
messages: Array.from(state.messages.entries()).map(([messageId, record]) => [messageId, encodeCheckpointMessageRecord(record)]),
|
|
43
|
+
stopRequests: Array.from(state.stopRequests.entries()),
|
|
44
|
+
compactions: Array.from(state.compactions.entries()),
|
|
45
|
+
context: state.context,
|
|
46
|
+
tokenConsumption: state.tokenConsumption
|
|
47
|
+
});
|
|
48
|
+
/** Event-log positions required to hydrate a pointer-based framework checkpoint. */
|
|
49
|
+
const reducedStateCheckpointEventSeqs = (payload) => {
|
|
50
|
+
const checkpoint = payload;
|
|
51
|
+
const seqs = [];
|
|
52
|
+
for (const [, record] of checkpoint.commands ?? []) if (record.admittedSeq !== void 0) seqs.push(record.admittedSeq);
|
|
53
|
+
for (const [, record] of checkpoint.messages ?? []) seqs.push(record.seq);
|
|
54
|
+
for (const [, record] of checkpoint.toolCalls ?? []) {
|
|
55
|
+
if (record.decision !== void 0) seqs.push(record.decision.seq);
|
|
56
|
+
if (record.terminal !== void 0 && (record.terminal._tag === "Completed" || record.terminal._tag === "Failed")) seqs.push(record.terminal.seq);
|
|
57
|
+
}
|
|
58
|
+
return uniqueSequenceNumbers(seqs);
|
|
59
|
+
};
|
|
60
|
+
/** Decode the framework reducer checkpoint and rebuild derived scheduler queues. */
|
|
61
|
+
const decodeReducedStateCheckpoint = (payload, referencedEvents) => {
|
|
62
|
+
const checkpoint = payload;
|
|
63
|
+
const eventsBySeq = committedEventMapBySeq(referencedEvents);
|
|
64
|
+
const commands = decodeCheckpointCommandRecords(checkpoint.commands ?? [], eventsBySeq);
|
|
65
|
+
const runs = new Map(checkpoint.runs ?? []);
|
|
66
|
+
const messages = decodeCheckpointMessageRecords(checkpoint.messages ?? [], eventsBySeq);
|
|
67
|
+
return {
|
|
68
|
+
lastSeq: SequenceNumber.make(Number(checkpoint.lastSeq ?? 0)),
|
|
69
|
+
...checkpoint.runSchedulingRequest === void 0 ? {} : { runSchedulingRequest: Schema.decodeUnknownSync(RunSchedulingRequestRecord)(checkpoint.runSchedulingRequest) },
|
|
70
|
+
...checkpoint.modelSelection === void 0 ? {} : { modelSelection: checkpoint.modelSelection },
|
|
71
|
+
commands,
|
|
72
|
+
runs,
|
|
73
|
+
recoveryContinuations: new Map(checkpoint.recoveryContinuations ?? []),
|
|
74
|
+
turns: new Map(checkpoint.turns ?? []),
|
|
75
|
+
inferences: new Map(checkpoint.inferences ?? []),
|
|
76
|
+
toolCalls: decodeCheckpointToolCallRecords(checkpoint.toolCalls ?? [], eventsBySeq),
|
|
77
|
+
messages,
|
|
78
|
+
stopRequests: new Map(checkpoint.stopRequests ?? []),
|
|
79
|
+
compactions: new Map(checkpoint.compactions ?? []),
|
|
80
|
+
context: checkpoint.context ?? { version: ContextVersion.make(0) },
|
|
81
|
+
tokenConsumption: decodeTokenConsumptionState(checkpoint.tokenConsumption),
|
|
82
|
+
commandQueues: deriveCommandQueues({
|
|
83
|
+
commands,
|
|
84
|
+
runs,
|
|
85
|
+
messages
|
|
86
|
+
})
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
const encodeCheckpointCommandRecord = (record) => {
|
|
90
|
+
const { command: _command, ...checkpoint } = record;
|
|
91
|
+
return checkpoint;
|
|
92
|
+
};
|
|
93
|
+
const encodeCheckpointMessageRecord = (record) => {
|
|
94
|
+
switch (record._tag) {
|
|
95
|
+
case "System": {
|
|
96
|
+
const { content: _content, ...checkpoint } = record;
|
|
97
|
+
return checkpoint;
|
|
98
|
+
}
|
|
99
|
+
case "User": {
|
|
100
|
+
const { content: _content, ...checkpoint } = record;
|
|
101
|
+
return checkpoint;
|
|
102
|
+
}
|
|
103
|
+
case "Steering": {
|
|
104
|
+
const { content: _content, ...checkpoint } = record;
|
|
105
|
+
return checkpoint;
|
|
106
|
+
}
|
|
107
|
+
case "Assistant": {
|
|
108
|
+
const { content: _content, promptParts: _promptParts, ...checkpoint } = record;
|
|
109
|
+
return checkpoint;
|
|
110
|
+
}
|
|
111
|
+
case "AssistantPartial": {
|
|
112
|
+
const { content: _content, promptParts: _promptParts, ...checkpoint } = record;
|
|
113
|
+
return checkpoint;
|
|
114
|
+
}
|
|
115
|
+
default: return assertNever(record, "checkpoint message record");
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
const encodeCheckpointToolCallRecord = (record) => ({
|
|
119
|
+
toolCallId: record.toolCallId,
|
|
120
|
+
...record.decision === void 0 ? {} : { decision: encodeCheckpointToolDecision(record.decision) },
|
|
121
|
+
...record.durationMs === void 0 ? {} : { durationMs: record.durationMs },
|
|
122
|
+
...record.startedAtMs === void 0 ? {} : { startedAtMs: record.startedAtMs },
|
|
123
|
+
...record.startedSeq === void 0 ? {} : { startedSeq: record.startedSeq },
|
|
124
|
+
...record.terminalAtMs === void 0 ? {} : { terminalAtMs: record.terminalAtMs },
|
|
125
|
+
...record.terminal === void 0 ? {} : { terminal: encodeCheckpointToolTerminal(record.terminal) }
|
|
126
|
+
});
|
|
127
|
+
const encodeCheckpointToolDecision = (decision) => {
|
|
128
|
+
switch (decision._tag) {
|
|
129
|
+
case "Created": {
|
|
130
|
+
const { params: _params, promptPart: _promptPart, ...checkpoint } = decision;
|
|
131
|
+
return checkpoint;
|
|
132
|
+
}
|
|
133
|
+
case "Rejected": {
|
|
134
|
+
const { promptPart: _promptPart, ...checkpoint } = decision;
|
|
135
|
+
return checkpoint;
|
|
136
|
+
}
|
|
137
|
+
default: return assertNever(decision, "checkpoint tool decision");
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
const encodeCheckpointToolTerminal = (terminal) => {
|
|
141
|
+
switch (terminal._tag) {
|
|
142
|
+
case "Completed": {
|
|
143
|
+
const { result: _result, promptPart: _promptPart, ...checkpoint } = terminal;
|
|
144
|
+
return checkpoint;
|
|
145
|
+
}
|
|
146
|
+
case "Failed": {
|
|
147
|
+
const { error: _error, promptPart: _promptPart, ...checkpoint } = terminal;
|
|
148
|
+
return checkpoint;
|
|
149
|
+
}
|
|
150
|
+
default: return assertNever(terminal, "checkpoint tool terminal");
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
const decodeCheckpointCommandRecords = (records, eventsBySeq) => new Map(records.map(([commandId, record]) => {
|
|
154
|
+
if (record.admittedSeq === void 0) return [commandId, record];
|
|
155
|
+
const admitted = reduceSingleCheckpointEvent(eventsBySeq, record.admittedSeq, commandAdmittedEventType).commands.get(commandId);
|
|
156
|
+
if (admitted?.command === void 0) throw new Error(`Checkpoint command ${commandId} missing admission payload`);
|
|
157
|
+
return [commandId, {
|
|
158
|
+
...record,
|
|
159
|
+
command: admitted.command
|
|
160
|
+
}];
|
|
161
|
+
}));
|
|
162
|
+
const decodeCheckpointMessageRecords = (records, eventsBySeq) => new Map(records.map(([messageId, record]) => {
|
|
163
|
+
const message = reduceSingleCheckpointEvent(eventsBySeq, record.seq, checkpointMessageEventType(record)).messages.get(messageId);
|
|
164
|
+
if (message === void 0) throw new Error(`Checkpoint message ${messageId} missing message payload`);
|
|
165
|
+
return [messageId, {
|
|
166
|
+
...message,
|
|
167
|
+
...record
|
|
168
|
+
}];
|
|
169
|
+
}));
|
|
170
|
+
const decodeCheckpointToolCallRecords = (records, eventsBySeq) => new Map(records.map(([toolCallId, record]) => [toolCallId, {
|
|
171
|
+
toolCallId,
|
|
172
|
+
...record.decision === void 0 ? {} : { decision: decodeCheckpointToolDecision(toolCallId, record.decision, eventsBySeq) },
|
|
173
|
+
...record.durationMs === void 0 ? {} : { durationMs: record.durationMs },
|
|
174
|
+
...record.startedAtMs === void 0 ? {} : { startedAtMs: record.startedAtMs },
|
|
175
|
+
...record.startedSeq === void 0 ? {} : { startedSeq: record.startedSeq },
|
|
176
|
+
...record.terminalAtMs === void 0 ? {} : { terminalAtMs: record.terminalAtMs },
|
|
177
|
+
...record.terminal === void 0 ? {} : { terminal: decodeCheckpointToolTerminal(toolCallId, record.terminal, eventsBySeq) }
|
|
178
|
+
}]));
|
|
179
|
+
const decodeCheckpointToolDecision = (toolCallId, decision, eventsBySeq) => {
|
|
180
|
+
const hydrated = reduceSingleCheckpointEvent(eventsBySeq, decision.seq, decision._tag === "Created" ? toolCallCreatedEventType : toolCallRejectedEventType).toolCalls.get(toolCallId)?.decision;
|
|
181
|
+
if (hydrated === void 0 || hydrated._tag !== decision._tag) throw new Error(`Checkpoint tool decision ${toolCallId} missing ${decision._tag} payload`);
|
|
182
|
+
return hydrated;
|
|
183
|
+
};
|
|
184
|
+
const decodeCheckpointToolTerminal = (toolCallId, terminal, eventsBySeq) => {
|
|
185
|
+
const hydrated = reduceSingleCheckpointEvent(eventsBySeq, terminal.seq, terminal._tag === "Completed" ? toolCallCompletedEventType : toolCallFailedEventType).toolCalls.get(toolCallId)?.terminal;
|
|
186
|
+
if (hydrated === void 0 || hydrated._tag !== terminal._tag) throw new Error(`Checkpoint tool terminal ${toolCallId} missing ${terminal._tag} payload`);
|
|
187
|
+
return hydrated;
|
|
188
|
+
};
|
|
189
|
+
const checkpointMessageEventType = (record) => {
|
|
190
|
+
switch (record._tag) {
|
|
191
|
+
case "System": return systemMessageCommittedEventType;
|
|
192
|
+
case "User": return record.requestedDisposition === void 0 ? userMessageCommittedEventType : userMessageSubmittedEventType;
|
|
193
|
+
case "Steering": return steeringMessageQueuedEventType;
|
|
194
|
+
case "Assistant": return record.imported === true ? assistantMessageImportedEventType : assistantMessageCommittedEventType;
|
|
195
|
+
case "AssistantPartial": return assistantPartialCommittedEventType;
|
|
196
|
+
default: return assertNever(record, "checkpoint message type");
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
const reduceSingleCheckpointEvent = (eventsBySeq, seq, expectedType) => {
|
|
200
|
+
const event = eventsBySeq.get(Number(seq));
|
|
201
|
+
if (event === void 0) throw new Error(`Checkpoint reference seq ${seq} is missing`);
|
|
202
|
+
if (event.event.type !== expectedType) throw new Error(`Checkpoint reference seq ${seq} expected ${expectedType}; got ${event.event.type}`);
|
|
203
|
+
return foldReducedState(initialReducedState, [event]);
|
|
204
|
+
};
|
|
205
|
+
const committedEventMapBySeq = (events) => new Map(events.map((event) => [Number(event.position.seq), event]));
|
|
206
|
+
const uniqueSequenceNumbers = (seqs) => Array.from(new Set(seqs.map((seq) => Number(seq)))).sort((left, right) => left - right).map((seq) => SequenceNumber.make(seq));
|
|
207
|
+
const decodeTokenConsumptionState = (value) => value ?? emptyTokenConsumptionState;
|
|
208
|
+
/** Fold newly committed durable events into the canonical replay state. */
|
|
209
|
+
const foldReducedState = (state, committed) => {
|
|
210
|
+
let lastSeq = state.lastSeq;
|
|
211
|
+
let runSchedulingRequest = state.runSchedulingRequest;
|
|
212
|
+
let modelSelection = state.modelSelection;
|
|
213
|
+
const commands = new Map(state.commands);
|
|
214
|
+
const runs = new Map(state.runs);
|
|
215
|
+
const recoveryContinuations = new Map(state.recoveryContinuations);
|
|
216
|
+
const turns = new Map(state.turns);
|
|
217
|
+
const inferences = new Map(state.inferences);
|
|
218
|
+
const toolCalls = new Map(state.toolCalls);
|
|
219
|
+
const messages = new Map(state.messages);
|
|
220
|
+
const stopRequests = new Map(state.stopRequests);
|
|
221
|
+
const compactions = new Map(state.compactions);
|
|
222
|
+
let context = state.context;
|
|
223
|
+
let tokenConsumption = state.tokenConsumption;
|
|
224
|
+
for (const entry of committed) {
|
|
225
|
+
lastSeq = entry.position.seq;
|
|
226
|
+
if (entry.event.namespace !== "effect-durable-agent") continue;
|
|
227
|
+
const event = interpretLegacyAttemptForReduction(entry.event);
|
|
228
|
+
const seq = entry.position.seq;
|
|
229
|
+
const eventCreatedAtMs = Number(event.createdAtMs);
|
|
230
|
+
switch (event.type) {
|
|
231
|
+
case runSchedulingRequestedEventType:
|
|
232
|
+
runSchedulingRequest = Schema.decodeUnknownSync(RunSchedulingRequestRecord)({
|
|
233
|
+
...Schema.decodeUnknownSync(RunSchedulingRequest)(event.payload),
|
|
234
|
+
requestedSeq: seq
|
|
235
|
+
});
|
|
236
|
+
break;
|
|
237
|
+
case runSchedulingDeliveredEventType:
|
|
238
|
+
if (runSchedulingRequest !== void 0 && runSchedulingRequest.requestId === event.payload.requestId) runSchedulingRequest = {
|
|
239
|
+
...runSchedulingRequest,
|
|
240
|
+
deliveredSeq: seq
|
|
241
|
+
};
|
|
242
|
+
break;
|
|
243
|
+
case runSchedulingInvalidatedEventType:
|
|
244
|
+
case runSchedulingGrantedEventType:
|
|
245
|
+
if (runSchedulingRequest?.requestId === event.payload.requestId) runSchedulingRequest = void 0;
|
|
246
|
+
break;
|
|
247
|
+
case commandAdmittedEventType: {
|
|
248
|
+
const { command } = event.payload;
|
|
249
|
+
const commandId = requireAdmittedCommandId(command);
|
|
250
|
+
upsert(commands, commandId, { commandId }, (record) => ({
|
|
251
|
+
...record,
|
|
252
|
+
admittedSeq: seq,
|
|
253
|
+
admissionTrace: event.trace,
|
|
254
|
+
command
|
|
255
|
+
}));
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
case commandStartedEventType: {
|
|
259
|
+
const { commandId } = event.payload;
|
|
260
|
+
upsert(commands, commandId, { commandId }, (record) => ({
|
|
261
|
+
...record,
|
|
262
|
+
startedSeq: seq
|
|
263
|
+
}));
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
case commandCompletedEventType: {
|
|
267
|
+
const { commandId } = event.payload;
|
|
268
|
+
upsert(commands, commandId, { commandId }, (record) => ({
|
|
269
|
+
...record,
|
|
270
|
+
terminal: {
|
|
271
|
+
_tag: "Completed",
|
|
272
|
+
seq
|
|
273
|
+
}
|
|
274
|
+
}));
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
case commandFailedEventType: {
|
|
278
|
+
const { commandId, error } = event.payload;
|
|
279
|
+
upsert(commands, commandId, { commandId }, (record) => ({
|
|
280
|
+
...record,
|
|
281
|
+
terminal: {
|
|
282
|
+
_tag: "Failed",
|
|
283
|
+
seq,
|
|
284
|
+
error
|
|
285
|
+
}
|
|
286
|
+
}));
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
case commandCancelledEventType: {
|
|
290
|
+
const { commandId, reason } = event.payload;
|
|
291
|
+
upsert(commands, commandId, { commandId }, (record) => ({
|
|
292
|
+
...record,
|
|
293
|
+
terminal: {
|
|
294
|
+
_tag: "Cancelled",
|
|
295
|
+
seq,
|
|
296
|
+
reason
|
|
297
|
+
}
|
|
298
|
+
}));
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
301
|
+
case runStartedEventType: {
|
|
302
|
+
const { runId, commandIds, trace } = event.payload;
|
|
303
|
+
modelSelection ??= event.payload.modelSelection;
|
|
304
|
+
const terminal = runs.get(runId)?.terminal;
|
|
305
|
+
runs.set(runId, {
|
|
306
|
+
runId,
|
|
307
|
+
commandIds,
|
|
308
|
+
startedAtMs: eventCreatedAtMs,
|
|
309
|
+
startedSeq: seq,
|
|
310
|
+
trace,
|
|
311
|
+
modelSelection: event.payload.modelSelection,
|
|
312
|
+
...terminal === void 0 ? {} : { terminal }
|
|
313
|
+
});
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
case runCompletedEventType: {
|
|
317
|
+
const { runId } = event.payload;
|
|
318
|
+
updateExisting(runs, runId, (record) => ({
|
|
319
|
+
...record,
|
|
320
|
+
...terminalTiming(record, eventCreatedAtMs),
|
|
321
|
+
terminal: {
|
|
322
|
+
_tag: "Completed",
|
|
323
|
+
seq
|
|
324
|
+
}
|
|
325
|
+
}));
|
|
326
|
+
break;
|
|
327
|
+
}
|
|
328
|
+
case runFailedEventType: {
|
|
329
|
+
const { runId, error } = event.payload;
|
|
330
|
+
updateExisting(runs, runId, (record) => ({
|
|
331
|
+
...record,
|
|
332
|
+
...terminalTiming(record, eventCreatedAtMs),
|
|
333
|
+
terminal: {
|
|
334
|
+
_tag: "Failed",
|
|
335
|
+
seq,
|
|
336
|
+
error
|
|
337
|
+
}
|
|
338
|
+
}));
|
|
339
|
+
break;
|
|
340
|
+
}
|
|
341
|
+
case runInterruptedEventType: {
|
|
342
|
+
const { runId, reason } = event.payload;
|
|
343
|
+
updateExisting(runs, runId, (record) => ({
|
|
344
|
+
...record,
|
|
345
|
+
...terminalTiming(record, eventCreatedAtMs),
|
|
346
|
+
terminal: {
|
|
347
|
+
_tag: "Interrupted",
|
|
348
|
+
seq,
|
|
349
|
+
reason
|
|
350
|
+
}
|
|
351
|
+
}));
|
|
352
|
+
break;
|
|
353
|
+
}
|
|
354
|
+
case turnStartedEventType: {
|
|
355
|
+
const { runId, turnId, inputMessageIds } = event.payload;
|
|
356
|
+
const terminal = turns.get(turnId)?.terminal;
|
|
357
|
+
turns.set(turnId, {
|
|
358
|
+
runId,
|
|
359
|
+
turnId,
|
|
360
|
+
startedAtMs: eventCreatedAtMs,
|
|
361
|
+
startedSeq: seq,
|
|
362
|
+
...terminal === void 0 ? {} : { terminal }
|
|
363
|
+
});
|
|
364
|
+
for (const messageId of inputMessageIds ?? []) {
|
|
365
|
+
const message = messages.get(messageId);
|
|
366
|
+
if (message?._tag === "Steering" || message?._tag === "User") messages.set(messageId, {
|
|
367
|
+
...message,
|
|
368
|
+
consumedSeq: seq,
|
|
369
|
+
consumedTurnId: turnId
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
break;
|
|
373
|
+
}
|
|
374
|
+
case turnCompletedEventType: {
|
|
375
|
+
const { turnId, usage } = event.payload;
|
|
376
|
+
updateExisting(turns, turnId, (record) => ({
|
|
377
|
+
...record,
|
|
378
|
+
...terminalTiming(record, eventCreatedAtMs),
|
|
379
|
+
terminal: {
|
|
380
|
+
_tag: "Completed",
|
|
381
|
+
seq,
|
|
382
|
+
...usage === void 0 ? {} : { usage }
|
|
383
|
+
}
|
|
384
|
+
}));
|
|
385
|
+
break;
|
|
386
|
+
}
|
|
387
|
+
case turnFailedEventType: {
|
|
388
|
+
const { turnId, error } = event.payload;
|
|
389
|
+
updateExisting(turns, turnId, (record) => ({
|
|
390
|
+
...record,
|
|
391
|
+
...terminalTiming(record, eventCreatedAtMs),
|
|
392
|
+
terminal: {
|
|
393
|
+
_tag: "Failed",
|
|
394
|
+
seq,
|
|
395
|
+
error
|
|
396
|
+
}
|
|
397
|
+
}));
|
|
398
|
+
break;
|
|
399
|
+
}
|
|
400
|
+
case turnStoppedEventType: {
|
|
401
|
+
const { turnId, reason } = event.payload;
|
|
402
|
+
updateExisting(turns, turnId, (record) => ({
|
|
403
|
+
...record,
|
|
404
|
+
...terminalTiming(record, eventCreatedAtMs),
|
|
405
|
+
terminal: {
|
|
406
|
+
_tag: "Stopped",
|
|
407
|
+
seq,
|
|
408
|
+
reason
|
|
409
|
+
}
|
|
410
|
+
}));
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
case inferenceStartedEventType: {
|
|
414
|
+
const { runId, turnId, inferenceId } = event.payload;
|
|
415
|
+
const terminal = inferences.get(inferenceId)?.terminal;
|
|
416
|
+
inferences.set(inferenceId, {
|
|
417
|
+
runId,
|
|
418
|
+
turnId,
|
|
419
|
+
inferenceId,
|
|
420
|
+
startedAtMs: eventCreatedAtMs,
|
|
421
|
+
startedSeq: seq,
|
|
422
|
+
...terminal === void 0 ? {} : { terminal }
|
|
423
|
+
});
|
|
424
|
+
break;
|
|
425
|
+
}
|
|
426
|
+
case inferenceCompletedEventType: {
|
|
427
|
+
const { inferenceId, usage } = event.payload;
|
|
428
|
+
const previouslyCompleted = inferences.get(inferenceId)?.terminal?._tag === "Completed";
|
|
429
|
+
updateExisting(inferences, inferenceId, (record) => ({
|
|
430
|
+
...record,
|
|
431
|
+
...terminalTiming(record, eventCreatedAtMs),
|
|
432
|
+
terminal: {
|
|
433
|
+
_tag: "Completed",
|
|
434
|
+
seq,
|
|
435
|
+
...usage === void 0 ? {} : { usage }
|
|
436
|
+
}
|
|
437
|
+
}));
|
|
438
|
+
if (!previouslyCompleted && inferences.get(inferenceId)?.terminal?._tag === "Completed") {
|
|
439
|
+
const model = runs.get(event.payload.runId)?.modelSelection;
|
|
440
|
+
if (model !== void 0) tokenConsumption = addModelUsage(tokenConsumption, model, usage);
|
|
441
|
+
}
|
|
442
|
+
break;
|
|
443
|
+
}
|
|
444
|
+
case inferenceFailedEventType: {
|
|
445
|
+
const { inferenceId, error } = event.payload;
|
|
446
|
+
updateExisting(inferences, inferenceId, (record) => ({
|
|
447
|
+
...record,
|
|
448
|
+
...terminalTiming(record, eventCreatedAtMs),
|
|
449
|
+
terminal: {
|
|
450
|
+
_tag: "Failed",
|
|
451
|
+
seq,
|
|
452
|
+
error
|
|
453
|
+
}
|
|
454
|
+
}));
|
|
455
|
+
break;
|
|
456
|
+
}
|
|
457
|
+
case toolCallCreatedEventType: {
|
|
458
|
+
const { runId, turnId, inferenceId, toolCallId, promptPart } = event.payload;
|
|
459
|
+
upsert(toolCalls, toolCallId, { toolCallId }, (record) => ({
|
|
460
|
+
...record,
|
|
461
|
+
decision: {
|
|
462
|
+
_tag: "Created",
|
|
463
|
+
seq,
|
|
464
|
+
runId,
|
|
465
|
+
turnId,
|
|
466
|
+
inferenceId,
|
|
467
|
+
providerPartId: promptPart.id,
|
|
468
|
+
toolName: promptPart.name,
|
|
469
|
+
params: promptPart.params,
|
|
470
|
+
providerExecuted: promptPart.providerExecuted,
|
|
471
|
+
promptPart
|
|
472
|
+
}
|
|
473
|
+
}));
|
|
474
|
+
break;
|
|
475
|
+
}
|
|
476
|
+
case toolCallRejectedEventType: {
|
|
477
|
+
const { runId, turnId, inferenceId, toolCallId, promptPart } = event.payload;
|
|
478
|
+
upsert(toolCalls, toolCallId, { toolCallId }, (record) => ({
|
|
479
|
+
...record,
|
|
480
|
+
decision: {
|
|
481
|
+
_tag: "Rejected",
|
|
482
|
+
seq,
|
|
483
|
+
runId,
|
|
484
|
+
turnId,
|
|
485
|
+
inferenceId,
|
|
486
|
+
providerPartId: promptPart.id,
|
|
487
|
+
toolName: promptPart.name,
|
|
488
|
+
reason: promptPart.result.reason,
|
|
489
|
+
modelFeedback: promptPart.result.modelFeedback,
|
|
490
|
+
promptPart
|
|
491
|
+
}
|
|
492
|
+
}));
|
|
493
|
+
break;
|
|
494
|
+
}
|
|
495
|
+
case toolCallStartedEventType: {
|
|
496
|
+
const { toolCallId } = event.payload;
|
|
497
|
+
upsert(toolCalls, toolCallId, { toolCallId }, (record) => ({
|
|
498
|
+
...record,
|
|
499
|
+
startedAtMs: eventCreatedAtMs,
|
|
500
|
+
startedSeq: seq
|
|
501
|
+
}));
|
|
502
|
+
break;
|
|
503
|
+
}
|
|
504
|
+
case toolCallCompletedEventType: {
|
|
505
|
+
const { toolCallId, promptPart } = event.payload;
|
|
506
|
+
upsert(toolCalls, toolCallId, { toolCallId }, (record) => ({
|
|
507
|
+
...record,
|
|
508
|
+
...terminalTiming(record, eventCreatedAtMs),
|
|
509
|
+
terminal: {
|
|
510
|
+
_tag: "Completed",
|
|
511
|
+
seq,
|
|
512
|
+
result: promptPart.result,
|
|
513
|
+
promptPart
|
|
514
|
+
}
|
|
515
|
+
}));
|
|
516
|
+
break;
|
|
517
|
+
}
|
|
518
|
+
case toolCallFailedEventType: {
|
|
519
|
+
const { toolCallId, promptPart, error } = event.payload;
|
|
520
|
+
upsert(toolCalls, toolCallId, { toolCallId }, (record) => ({
|
|
521
|
+
...record,
|
|
522
|
+
...terminalTiming(record, eventCreatedAtMs),
|
|
523
|
+
terminal: {
|
|
524
|
+
_tag: "Failed",
|
|
525
|
+
seq,
|
|
526
|
+
error: error ?? promptPart.result,
|
|
527
|
+
promptPart
|
|
528
|
+
}
|
|
529
|
+
}));
|
|
530
|
+
break;
|
|
531
|
+
}
|
|
532
|
+
case systemMessageCommittedEventType: {
|
|
533
|
+
const { messageId, content } = event.payload;
|
|
534
|
+
messages.set(messageId, {
|
|
535
|
+
_tag: "System",
|
|
536
|
+
messageId,
|
|
537
|
+
content,
|
|
538
|
+
...Number.isFinite(eventCreatedAtMs) ? { createdAtMs: eventCreatedAtMs } : {},
|
|
539
|
+
seq
|
|
540
|
+
});
|
|
541
|
+
break;
|
|
542
|
+
}
|
|
543
|
+
case userMessageCommittedEventType: {
|
|
544
|
+
const { commandId, messageId, content } = event.payload;
|
|
545
|
+
messages.set(messageId, {
|
|
546
|
+
_tag: "User",
|
|
547
|
+
messageId,
|
|
548
|
+
commandId,
|
|
549
|
+
content,
|
|
550
|
+
...Number.isFinite(eventCreatedAtMs) ? { createdAtMs: eventCreatedAtMs } : {},
|
|
551
|
+
seq
|
|
552
|
+
});
|
|
553
|
+
break;
|
|
554
|
+
}
|
|
555
|
+
case userMessageSubmittedEventType: {
|
|
556
|
+
const { commandId, messageId, disposition: requestedDisposition, content } = event.payload;
|
|
557
|
+
messages.set(messageId, {
|
|
558
|
+
_tag: "User",
|
|
559
|
+
messageId,
|
|
560
|
+
commandId,
|
|
561
|
+
content,
|
|
562
|
+
requestedDisposition,
|
|
563
|
+
disposition: requestedDisposition,
|
|
564
|
+
...Number.isFinite(eventCreatedAtMs) ? { createdAtMs: eventCreatedAtMs } : {},
|
|
565
|
+
seq
|
|
566
|
+
});
|
|
567
|
+
break;
|
|
568
|
+
}
|
|
569
|
+
case userMessagePromotedEventType: {
|
|
570
|
+
const { messageId } = event.payload;
|
|
571
|
+
const message = messages.get(messageId);
|
|
572
|
+
if (message?._tag === "User" && message.disposition === "queue" || message?._tag === "Steering" && message.pausedByCommandId !== void 0) {
|
|
573
|
+
const { pausedByCommandId: _pausedByCommandId, pausedSeq: _pausedSeq, ...unpausedMessage } = message;
|
|
574
|
+
messages.set(messageId, {
|
|
575
|
+
...unpausedMessage,
|
|
576
|
+
...message._tag === "User" ? { disposition: "steer" } : {},
|
|
577
|
+
promotedSeq: seq
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
break;
|
|
581
|
+
}
|
|
582
|
+
case userMessageCancelledEventType: {
|
|
583
|
+
const { commandId, messageId, reason } = event.payload;
|
|
584
|
+
const message = messages.get(messageId);
|
|
585
|
+
if (message?._tag === "User" || message?._tag === "Steering") messages.set(messageId, {
|
|
586
|
+
...message,
|
|
587
|
+
cancelledSeq: seq,
|
|
588
|
+
cancelledByCommandId: commandId,
|
|
589
|
+
cancellationReason: reason
|
|
590
|
+
});
|
|
591
|
+
break;
|
|
592
|
+
}
|
|
593
|
+
case messageQueuePausedEventType:
|
|
594
|
+
case pendingMessagesPausedEventType: {
|
|
595
|
+
const messageIds = event.payload.messageIds;
|
|
596
|
+
const interruptionCommandId = event.type === messageQueuePausedEventType ? event.payload.stopCommandId : event.payload.interruptionCommandId;
|
|
597
|
+
for (const messageId of messageIds) {
|
|
598
|
+
const message = messages.get(messageId);
|
|
599
|
+
if ((message?._tag === "User" || message?._tag === "Steering") && message.consumedSeq === void 0 && message.cancelledSeq === void 0) messages.set(messageId, {
|
|
600
|
+
...message,
|
|
601
|
+
...message._tag === "User" ? { disposition: "queue" } : {},
|
|
602
|
+
pausedSeq: seq,
|
|
603
|
+
pausedByCommandId: interruptionCommandId
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
break;
|
|
607
|
+
}
|
|
608
|
+
case steeringMessageQueuedEventType: {
|
|
609
|
+
const { commandId, messageId, runId, content } = event.payload;
|
|
610
|
+
messages.set(messageId, {
|
|
611
|
+
_tag: "Steering",
|
|
612
|
+
messageId,
|
|
613
|
+
commandId,
|
|
614
|
+
runId,
|
|
615
|
+
content,
|
|
616
|
+
...Number.isFinite(eventCreatedAtMs) ? { createdAtMs: eventCreatedAtMs } : {},
|
|
617
|
+
seq
|
|
618
|
+
});
|
|
619
|
+
break;
|
|
620
|
+
}
|
|
621
|
+
case steeringMessageCancelledEventType: {
|
|
622
|
+
const { messageId, reason } = event.payload;
|
|
623
|
+
const message = messages.get(messageId);
|
|
624
|
+
if (message?._tag === "Steering") messages.set(messageId, {
|
|
625
|
+
...message,
|
|
626
|
+
cancelledSeq: seq,
|
|
627
|
+
cancellationReason: reason
|
|
628
|
+
});
|
|
629
|
+
break;
|
|
630
|
+
}
|
|
631
|
+
case assistantMessageCommittedEventType: {
|
|
632
|
+
const { messageId, runId, turnId, inferenceId, promptParts } = event.payload;
|
|
633
|
+
messages.set(messageId, {
|
|
634
|
+
_tag: "Assistant",
|
|
635
|
+
messageId,
|
|
636
|
+
runId,
|
|
637
|
+
turnId,
|
|
638
|
+
inferenceId,
|
|
639
|
+
content: assistantContentFromPromptParts(promptParts),
|
|
640
|
+
...Number.isFinite(eventCreatedAtMs) ? { createdAtMs: eventCreatedAtMs } : {},
|
|
641
|
+
promptParts,
|
|
642
|
+
seq
|
|
643
|
+
});
|
|
644
|
+
break;
|
|
645
|
+
}
|
|
646
|
+
case assistantMessageImportedEventType: {
|
|
647
|
+
const { messageId, runId, turnId, inferenceId, promptParts } = event.payload;
|
|
648
|
+
messages.set(messageId, {
|
|
649
|
+
_tag: "Assistant",
|
|
650
|
+
messageId,
|
|
651
|
+
runId,
|
|
652
|
+
turnId,
|
|
653
|
+
inferenceId,
|
|
654
|
+
imported: true,
|
|
655
|
+
content: assistantContentFromPromptParts(promptParts),
|
|
656
|
+
...Number.isFinite(eventCreatedAtMs) ? { createdAtMs: eventCreatedAtMs } : {},
|
|
657
|
+
promptParts,
|
|
658
|
+
seq
|
|
659
|
+
});
|
|
660
|
+
break;
|
|
661
|
+
}
|
|
662
|
+
case assistantPartialCommittedEventType: {
|
|
663
|
+
const { messageId, runId, turnId, inferenceId, promptParts, reason } = event.payload;
|
|
664
|
+
messages.set(messageId, {
|
|
665
|
+
_tag: "AssistantPartial",
|
|
666
|
+
messageId,
|
|
667
|
+
runId,
|
|
668
|
+
turnId,
|
|
669
|
+
inferenceId,
|
|
670
|
+
content: assistantContentFromPromptParts(promptParts),
|
|
671
|
+
...Number.isFinite(eventCreatedAtMs) ? { createdAtMs: eventCreatedAtMs } : {},
|
|
672
|
+
promptParts,
|
|
673
|
+
reason,
|
|
674
|
+
seq
|
|
675
|
+
});
|
|
676
|
+
break;
|
|
677
|
+
}
|
|
678
|
+
case stopTurnRequestedEventType: {
|
|
679
|
+
const { commandId, runId, turnId } = event.payload;
|
|
680
|
+
stopRequests.set(commandId, {
|
|
681
|
+
commandId,
|
|
682
|
+
requestedSeq: seq,
|
|
683
|
+
...runId === void 0 ? {} : { requestedRunId: runId },
|
|
684
|
+
...turnId === void 0 ? {} : { requestedTurnId: turnId },
|
|
685
|
+
...appliedFields(stopRequests.get(commandId))
|
|
686
|
+
});
|
|
687
|
+
break;
|
|
688
|
+
}
|
|
689
|
+
case stopTurnAppliedEventType: {
|
|
690
|
+
const { commandId, runId, turnId, inferenceId } = event.payload;
|
|
691
|
+
const existing = stopRequests.get(commandId);
|
|
692
|
+
stopRequests.set(commandId, {
|
|
693
|
+
commandId,
|
|
694
|
+
requestedSeq: existing?.requestedSeq ?? seq,
|
|
695
|
+
...existing?.requestedRunId === void 0 ? {} : { requestedRunId: existing.requestedRunId },
|
|
696
|
+
...existing?.requestedTurnId === void 0 ? {} : { requestedTurnId: existing.requestedTurnId },
|
|
697
|
+
appliedSeq: seq,
|
|
698
|
+
appliedRunId: runId,
|
|
699
|
+
appliedTurnId: turnId,
|
|
700
|
+
...inferenceId === void 0 ? {} : { appliedInferenceId: inferenceId }
|
|
701
|
+
});
|
|
702
|
+
break;
|
|
703
|
+
}
|
|
704
|
+
case contextProjectedEventType: {
|
|
705
|
+
const { contextVersion } = event.payload;
|
|
706
|
+
context = {
|
|
707
|
+
...context,
|
|
708
|
+
version: contextVersion
|
|
709
|
+
};
|
|
710
|
+
break;
|
|
711
|
+
}
|
|
712
|
+
case compactionRequestedEventType: {
|
|
713
|
+
const { compactionId, sourceFromSeq, sourceToSeq } = event.payload;
|
|
714
|
+
upsert(compactions, compactionId, { compactionId }, (record) => ({
|
|
715
|
+
...record,
|
|
716
|
+
requestedSeq: seq,
|
|
717
|
+
sourceFromSeq,
|
|
718
|
+
sourceToSeq
|
|
719
|
+
}));
|
|
720
|
+
break;
|
|
721
|
+
}
|
|
722
|
+
case compactionStartedEventType: {
|
|
723
|
+
const { compactionId } = event.payload;
|
|
724
|
+
upsert(compactions, compactionId, { compactionId }, (record) => ({
|
|
725
|
+
...record,
|
|
726
|
+
startedSeq: seq
|
|
727
|
+
}));
|
|
728
|
+
break;
|
|
729
|
+
}
|
|
730
|
+
case summaryCreatedEventType: {
|
|
731
|
+
const { compactionId, summaryId, sourceFromSeq, sourceToSeq } = event.payload;
|
|
732
|
+
upsert(compactions, compactionId, { compactionId }, (record) => ({
|
|
733
|
+
...record,
|
|
734
|
+
summaryCreatedSeq: seq,
|
|
735
|
+
summaryId,
|
|
736
|
+
sourceFromSeq,
|
|
737
|
+
sourceToSeq
|
|
738
|
+
}));
|
|
739
|
+
break;
|
|
740
|
+
}
|
|
741
|
+
case contextRebasedEventType: {
|
|
742
|
+
const { compactionId, summaryId, contextVersion, retainedFromContextSeq } = event.payload;
|
|
743
|
+
upsert(compactions, compactionId, { compactionId }, (record) => ({
|
|
744
|
+
...record,
|
|
745
|
+
summaryId,
|
|
746
|
+
rebasedSeq: seq,
|
|
747
|
+
contextVersion
|
|
748
|
+
}));
|
|
749
|
+
pruneForContextRebase({
|
|
750
|
+
commands,
|
|
751
|
+
runs,
|
|
752
|
+
recoveryContinuations,
|
|
753
|
+
turns,
|
|
754
|
+
inferences,
|
|
755
|
+
toolCalls,
|
|
756
|
+
messages,
|
|
757
|
+
stopRequests,
|
|
758
|
+
compactions
|
|
759
|
+
}, retainedFromContextSeq, summaryId);
|
|
760
|
+
context = {
|
|
761
|
+
version: contextVersion,
|
|
762
|
+
currentSummaryId: summaryId
|
|
763
|
+
};
|
|
764
|
+
break;
|
|
765
|
+
}
|
|
766
|
+
case compactionCompletedEventType: {
|
|
767
|
+
const { compactionId } = event.payload;
|
|
768
|
+
if (compactions.get(compactionId)?.terminal?._tag !== "Completed" && event.payload.modelSelection !== void 0) tokenConsumption = addModelUsage(tokenConsumption, event.payload.modelSelection, event.payload.usage);
|
|
769
|
+
upsert(compactions, compactionId, { compactionId }, (record) => ({
|
|
770
|
+
...record,
|
|
771
|
+
terminal: {
|
|
772
|
+
_tag: "Completed",
|
|
773
|
+
seq
|
|
774
|
+
}
|
|
775
|
+
}));
|
|
776
|
+
break;
|
|
777
|
+
}
|
|
778
|
+
case compactionFailedEventType: {
|
|
779
|
+
const { compactionId, error } = event.payload;
|
|
780
|
+
if (event.payload.modelSelection !== void 0) tokenConsumption = addModelUsage(tokenConsumption, event.payload.modelSelection, event.payload.usage);
|
|
781
|
+
upsert(compactions, compactionId, { compactionId }, (record) => ({
|
|
782
|
+
...record,
|
|
783
|
+
terminal: {
|
|
784
|
+
_tag: "Failed",
|
|
785
|
+
seq,
|
|
786
|
+
error
|
|
787
|
+
}
|
|
788
|
+
}));
|
|
789
|
+
const failed = compactions.get(compactionId);
|
|
790
|
+
if (failed?.summaryId === void 0 || failed.summaryId !== context.currentSummaryId) compactions.delete(compactionId);
|
|
791
|
+
break;
|
|
792
|
+
}
|
|
793
|
+
case recoveryCompletedEventType: {
|
|
794
|
+
const { continuation } = event.payload;
|
|
795
|
+
if (continuation !== void 0) recoveryContinuations.set(continuation.replacementRunId, {
|
|
796
|
+
...continuation,
|
|
797
|
+
seq
|
|
798
|
+
});
|
|
799
|
+
break;
|
|
800
|
+
}
|
|
801
|
+
case sessionConfiguredEventType:
|
|
802
|
+
modelSelection ??= event.payload.modelSelection;
|
|
803
|
+
break;
|
|
804
|
+
case baseStateRequestedEventType:
|
|
805
|
+
case baseStateCreatedEventType:
|
|
806
|
+
case baseStateFailedEventType: break;
|
|
807
|
+
default: assertNever(event, "reduced-state durable event");
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
return {
|
|
811
|
+
lastSeq,
|
|
812
|
+
...runSchedulingRequest === void 0 ? {} : { runSchedulingRequest },
|
|
813
|
+
...modelSelection === void 0 ? {} : { modelSelection },
|
|
814
|
+
commands,
|
|
815
|
+
runs,
|
|
816
|
+
recoveryContinuations,
|
|
817
|
+
turns,
|
|
818
|
+
inferences,
|
|
819
|
+
toolCalls,
|
|
820
|
+
messages,
|
|
821
|
+
stopRequests,
|
|
822
|
+
compactions,
|
|
823
|
+
context,
|
|
824
|
+
tokenConsumption,
|
|
825
|
+
commandQueues: deriveCommandQueues({
|
|
826
|
+
commands,
|
|
827
|
+
runs,
|
|
828
|
+
messages
|
|
829
|
+
})
|
|
830
|
+
};
|
|
831
|
+
};
|
|
832
|
+
/** Fold committed events from genesis into a fresh `ReducedState`. */
|
|
833
|
+
const reduceCommittedEvents = (committed) => foldReducedState(initialReducedState, committed);
|
|
834
|
+
/** Drop runtime state made model-invisible by a context rebase; storage remains append-only. */
|
|
835
|
+
const pruneForContextRebase = (maps, retainedFromContextSeq, currentSummaryId) => {
|
|
836
|
+
for (const [messageId, message] of maps.messages) if (message._tag !== "System" && messagePruneSeq(message) < retainedFromContextSeq) maps.messages.delete(messageId);
|
|
837
|
+
for (const [toolCallId, tool] of maps.toolCalls) {
|
|
838
|
+
const terminalSeq = tool.terminal?.seq;
|
|
839
|
+
if (terminalSeq !== void 0 && terminalSeq < retainedFromContextSeq) maps.toolCalls.delete(toolCallId);
|
|
840
|
+
}
|
|
841
|
+
for (const [inferenceId, inference] of maps.inferences) if (inference.terminal !== void 0 && inference.terminal.seq < retainedFromContextSeq) maps.inferences.delete(inferenceId);
|
|
842
|
+
for (const [turnId, turn] of maps.turns) if (turn.terminal !== void 0 && turn.terminal.seq < retainedFromContextSeq) maps.turns.delete(turnId);
|
|
843
|
+
for (const [runId, run] of maps.runs) if (run.terminal !== void 0 && run.terminal.seq < retainedFromContextSeq) maps.runs.delete(runId);
|
|
844
|
+
for (const replacementRunId of maps.recoveryContinuations.keys()) if (!maps.runs.has(replacementRunId)) maps.recoveryContinuations.delete(replacementRunId);
|
|
845
|
+
for (const [commandId, command] of maps.commands) if (command.terminal !== void 0 && command.terminal.seq < retainedFromContextSeq) maps.commands.delete(commandId);
|
|
846
|
+
for (const [commandId, request] of maps.stopRequests) if (request.appliedSeq !== void 0 && request.appliedSeq < retainedFromContextSeq) maps.stopRequests.delete(commandId);
|
|
847
|
+
for (const [compactionId, compaction] of maps.compactions) if (compaction.terminal !== void 0 && compaction.summaryId !== currentSummaryId) maps.compactions.delete(compactionId);
|
|
848
|
+
};
|
|
849
|
+
const messagePruneSeq = (message) => {
|
|
850
|
+
if (message._tag === "Steering") return message.consumedSeq ?? message.cancelledSeq ?? Number.POSITIVE_INFINITY;
|
|
851
|
+
return message.seq;
|
|
852
|
+
};
|
|
853
|
+
/** Pending admitted commands in deterministic scheduler order. */
|
|
854
|
+
const pendingCommands = (state) => state.commandQueues.pendingCommands;
|
|
855
|
+
/** First pending StopTurn control command, when one is waiting. */
|
|
856
|
+
const pendingStopCommand = (state) => state.commandQueues.activeControlCommands.find((command) => command.command._tag === "StopTurn");
|
|
857
|
+
/** Currently active command evidence, if any unfinished command is blocking FIFO work. */
|
|
858
|
+
const activeCommand = (state) => state.commandQueues.active;
|
|
859
|
+
/** Active run/turn/inference identity owned by a specific active command. */
|
|
860
|
+
const activeTurnIdentityForCommand = (state, commandId) => {
|
|
861
|
+
const run = findLast(Array.from(state.runs.values()), (candidate) => candidate.commandIds.includes(commandId));
|
|
862
|
+
if (run === void 0) return;
|
|
863
|
+
const turn = findLast(Array.from(state.turns.values()), (candidate) => candidate.runId === run.runId);
|
|
864
|
+
if (turn === void 0) return;
|
|
865
|
+
const inference = findLast(Array.from(state.inferences.values()), (candidate) => candidate.runId === run.runId && candidate.turnId === turn.turnId);
|
|
866
|
+
return {
|
|
867
|
+
runId: run.runId,
|
|
868
|
+
turnId: turn.turnId,
|
|
869
|
+
...inference === void 0 ? {} : { inferenceId: inference.inferenceId }
|
|
870
|
+
};
|
|
871
|
+
};
|
|
872
|
+
/** Classify unfinished durable lifecycles that recovery may need to repair. */
|
|
873
|
+
const classifyRecoverableWork = (state) => {
|
|
874
|
+
const activeCommands = Array.from(state.commands.values()).filter((command) => command.startedSeq !== void 0 && command.terminal === void 0 && command.commandId !== state.runSchedulingRequest?.work.commandId);
|
|
875
|
+
const activeRuns = Array.from(state.runs.values()).filter((run) => run.terminal === void 0);
|
|
876
|
+
const activeTurns = Array.from(state.turns.values()).filter((turn) => turn.terminal === void 0);
|
|
877
|
+
const activeInferences = Array.from(state.inferences.values()).filter((inference) => inference.terminal === void 0);
|
|
878
|
+
const openToolCalls = Array.from(state.toolCalls.values()).filter((tool) => tool.decision?._tag === "Created" && !tool.decision.providerExecuted && tool.terminal === void 0);
|
|
879
|
+
return {
|
|
880
|
+
activeCommands,
|
|
881
|
+
activeRuns,
|
|
882
|
+
activeTurns,
|
|
883
|
+
activeInferences,
|
|
884
|
+
openToolCalls,
|
|
885
|
+
runningToolCalls: openToolCalls.filter((tool) => tool.startedSeq !== void 0),
|
|
886
|
+
pendingStopRequests: Array.from(state.stopRequests.values()).filter((request) => request.appliedSeq === void 0 && state.commands.get(request.commandId)?.terminal === void 0),
|
|
887
|
+
openCompactions: Array.from(state.compactions.values()).filter((compaction) => compaction.terminal === void 0)
|
|
888
|
+
};
|
|
889
|
+
};
|
|
890
|
+
const assistantContentFromPromptParts = (promptParts) => {
|
|
891
|
+
const text = promptParts.filter((part) => part.type === "text").map((part) => part.text).join("");
|
|
892
|
+
const reasoning = promptParts.filter((part) => part.type === "reasoning").map((part) => part.text).join("");
|
|
893
|
+
return {
|
|
894
|
+
text,
|
|
895
|
+
...reasoning.length === 0 ? {} : { reasoning }
|
|
896
|
+
};
|
|
897
|
+
};
|
|
898
|
+
const findLast = (items, predicate) => {
|
|
899
|
+
for (let index = items.length - 1; index >= 0; index -= 1) {
|
|
900
|
+
const item = items[index];
|
|
901
|
+
if (predicate(item)) return item;
|
|
902
|
+
}
|
|
903
|
+
};
|
|
904
|
+
const requireAdmittedCommandId = (command) => {
|
|
905
|
+
if (command.commandId !== void 0) return command.commandId;
|
|
906
|
+
throw new Error("CommandAdmitted command is missing a framework commandId");
|
|
907
|
+
};
|
|
908
|
+
const terminalTiming = (record, terminalAtMs) => ({
|
|
909
|
+
terminalAtMs,
|
|
910
|
+
...record.startedAtMs === void 0 ? {} : { durationMs: Math.max(0, terminalAtMs - record.startedAtMs) }
|
|
911
|
+
});
|
|
912
|
+
const upsert = (map, key, fallback, update) => {
|
|
913
|
+
map.set(key, update(map.get(key) ?? fallback));
|
|
914
|
+
};
|
|
915
|
+
const updateExisting = (map, key, update) => {
|
|
916
|
+
const record = map.get(key);
|
|
917
|
+
if (record !== void 0) map.set(key, update(record));
|
|
918
|
+
};
|
|
919
|
+
const appliedFields = (record) => record?.appliedSeq === void 0 ? {} : {
|
|
920
|
+
appliedSeq: record.appliedSeq,
|
|
921
|
+
appliedRunId: record.appliedRunId,
|
|
922
|
+
appliedTurnId: record.appliedTurnId,
|
|
923
|
+
...record.appliedInferenceId === void 0 ? {} : { appliedInferenceId: record.appliedInferenceId }
|
|
924
|
+
};
|
|
925
|
+
//#endregion
|
|
3
926
|
export { TokenConsumptionState, activeCommand, activeTurnIdentityForCommand, classifyRecoverableWork, decodeReducedStateCheckpoint, encodeReducedStateCheckpoint, foldReducedState, frameworkReducedStateReducerName, frameworkReducedStateReducerSchemaVersion, initialReducedState, pendingCommands, pendingStopCommand, reduceCommittedEvents, reducedStateCheckpointEventSeqs };
|
|
927
|
+
|
|
928
|
+
//# sourceMappingURL=reduced-state.js.map
|