effect-durable-agent 0.1.0-alpha.1 → 0.1.0-alpha.4
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/CHANGELOG.md +25 -1
- package/README.md +131 -128
- package/SECURITY.md +3 -4
- package/dist/{command-queues-CQ9cwRP7.d.ts → command-queues-CV7O-51r.d.ts} +7 -6
- package/dist/{compaction-YayFKo_1.d.ts → compaction-zbvLREI8.d.ts} +8 -6
- package/dist/context-projection-n6Rdup0N.d.ts +140 -0
- package/dist/{core-Hv3cFydL.d.ts → core-CYT6DBaS.d.ts} +2 -2
- package/dist/domain/command-queues.d.ts +1 -1
- package/dist/domain/context-projection.d.ts +2 -0
- package/dist/domain/context-projection.js +32 -0
- package/dist/domain/context-projection.js.map +1 -0
- package/dist/domain/message-transcript.d.ts +1 -1
- package/dist/domain/reduced-state-schema.d.ts +2 -0
- package/dist/domain/reduced-state-schema.js +2 -1
- package/dist/domain/reduced-state-schema.js.map +1 -1
- package/dist/domain/reduced-state.d.ts +1 -1
- package/dist/domain/reduced-state.js +1 -1
- package/dist/{durable-DlScwmCy.d.ts → durable-B7M6TD_0.d.ts} +341 -2
- package/dist/{durable-C3Rz7kg3.js → durable-DsV9PxZC.js} +13 -2
- package/dist/durable-DsV9PxZC.js.map +1 -0
- package/dist/{keep-alive-DYDsH6qN.d.ts → event-factory-CLJcyymV.d.ts} +5 -29
- package/dist/{keep-alive-p8dIcSVy.js → event-factory-tcpLgdMN.js} +3 -10
- package/dist/event-factory-tcpLgdMN.js.map +1 -0
- package/dist/host/websocket-protocol.d.ts +229 -0
- package/dist/host/websocket-protocol.js +99 -0
- package/dist/host/websocket-protocol.js.map +1 -0
- package/dist/host/websocket-wire.d.ts +2 -2
- package/dist/host/websocket-wire.js +37 -20
- package/dist/host/websocket-wire.js.map +1 -1
- package/dist/{id-generator-C2Q15uZM.d.ts → id-generator-Ckl8_XWW.d.ts} +2 -2
- package/dist/index.d.ts +4 -4
- package/dist/index.js +2 -2
- package/dist/keep-alive-Chl5SJxy.d.ts +29 -0
- package/dist/{message-transcript-BCHdHAe2.d.ts → message-transcript-DiOPBY3l.d.ts} +3 -3
- package/dist/{prompt-projector-9_02lk5-.d.ts → prompt-projector-C1IbkHr0.d.ts} +15 -16
- package/dist/{reduced-state-C4nVVzz9.js → reduced-state-TMi7ISla.js} +20 -4
- package/dist/reduced-state-TMi7ISla.js.map +1 -0
- package/dist/{reducer-registry-C0svUa63.d.ts → reducer-registry-CjSJBPKo.d.ts} +2 -2
- package/dist/{runtime-C56FQxiL.d.ts → runtime-DOgmAKsO.d.ts} +7 -6
- package/dist/services/compaction.d.ts +1 -1
- package/dist/services/compaction.js +4 -3
- package/dist/services/compaction.js.map +1 -1
- package/dist/services/id-generator.d.ts +1 -1
- package/dist/services/keep-alive.d.ts +2 -0
- package/dist/services/keep-alive.js +13 -0
- package/dist/services/keep-alive.js.map +1 -0
- package/dist/services/prompt-projector.d.ts +2 -2
- package/dist/services/prompt-projector.js +20 -10
- package/dist/services/prompt-projector.js.map +1 -1
- package/dist/services/reducer-registry.d.ts +1 -1
- package/dist/services/runtime-layer.d.ts +58 -0
- package/dist/services/runtime-layer.js +42 -0
- package/dist/services/runtime-layer.js.map +1 -0
- package/dist/services/runtime.d.ts +1 -1
- package/dist/services/runtime.js +4 -3
- package/dist/services/runtime.js.map +1 -1
- package/dist/services/session-query.d.ts +1 -1
- package/dist/services/session-query.js +76 -1
- package/dist/services/session-query.js.map +1 -0
- package/dist/services/session-state.d.ts +2 -0
- package/dist/services/session-state.js +2 -0
- package/dist/services/session-store.d.ts +2 -2
- package/dist/services/session-store.js +181 -2
- package/dist/services/session-store.js.map +1 -0
- package/dist/services/sink-checkpoint-store.d.ts +2 -0
- package/dist/services/sink-checkpoint-store.js +49 -0
- package/dist/services/sink-checkpoint-store.js.map +1 -0
- 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/services/websocket-subscriber.d.ts +54 -0
- package/dist/services/websocket-subscriber.js +190 -0
- package/dist/services/websocket-subscriber.js.map +1 -0
- package/dist/session-query-C-C4m1eq.d.ts +38 -0
- package/dist/{session-query-DlRo5Ufc.d.ts → session-state-CjOSvWbe.d.ts} +132 -40
- package/dist/{session-query-DOc1KanA.js → session-state-SStf0KUo.js} +43 -146
- package/dist/session-state-SStf0KUo.js.map +1 -0
- package/dist/{session-store-D2Ore1QC.d.ts → session-store-DZZUffUy.d.ts} +5 -94
- package/dist/sink-checkpoint-store-BZmQFTRE.d.ts +40 -0
- package/dist/{sink-registry-CIU0LD3m.d.ts → sink-registry-Cf79ICdW.d.ts} +11 -40
- package/dist/{sink-registry-CFC6xjsN.js → sink-registry-D13eUO_c.js} +9 -47
- package/dist/sink-registry-D13eUO_c.js.map +1 -0
- package/dist/testkit/layers.d.ts +14 -11
- package/dist/testkit/layers.js +8 -5
- package/dist/testkit/layers.js.map +1 -1
- package/dist/{tool-registry-CcnkAAQK.d.ts → tool-registry-B7Wona6N.d.ts} +3 -3
- package/dist/types/core.d.ts +1 -1
- package/dist/types/core.js +1 -1
- package/dist/types/core.js.map +1 -1
- package/dist/types/events/durable.d.ts +2 -2
- package/dist/types/events/durable.js +2 -2
- package/dist/types/events.d.ts +2 -2
- package/dist/types/events.js +2 -2
- package/dist/websocket-wire-QD2SquQE.d.ts +2244 -0
- package/docs/assets/architecture.svg +170 -60
- package/docs/assets/event-sequence.svg +68 -78
- package/docs/assets/hero.svg +33 -0
- package/docs/message-steering.md +1 -1
- package/docs/releasing.md +96 -39
- package/docs/spec.md +15 -12
- package/docs/websocket-protocol.md +35 -9
- package/package.json +55 -35
- package/dist/durable-C3Rz7kg3.js.map +0 -1
- package/dist/durable-object-runtime-B7jwBY2R.js +0 -1494
- package/dist/durable-object-runtime-B7jwBY2R.js.map +0 -1
- package/dist/durable-object-runtime-pWv0ohlz.d.ts +0 -257
- package/dist/durable-object-storage-Czy7IPrh.d.ts +0 -41
- package/dist/host/durable-object-runtime.d.ts +0 -2
- package/dist/host/durable-object-runtime.js +0 -2
- package/dist/host/durable-object-storage.d.ts +0 -2
- package/dist/host/durable-object-storage.js +0 -0
- package/dist/host/durable-object.d.ts +0 -83
- package/dist/host/durable-object.js +0 -191
- package/dist/host/durable-object.js.map +0 -1
- package/dist/keep-alive-p8dIcSVy.js.map +0 -1
- package/dist/reduced-state-C4nVVzz9.js.map +0 -1
- package/dist/session-query-DOc1KanA.js.map +0 -1
- package/dist/session-store-pYxQW9qh.js +0 -214
- package/dist/session-store-pYxQW9qh.js.map +0 -1
- package/dist/sink-registry-CFC6xjsN.js.map +0 -1
- package/dist/websocket-wire-DvOu_6BJ.d.ts +0 -16773
- package/examples/001-no-tools/README.md +0 -65
- package/examples/001-no-tools/worker.ts +0 -79
- package/examples/002-slack-bridge/README.md +0 -134
- package/examples/002-slack-bridge/events.ts +0 -115
- package/examples/002-slack-bridge/reducer.ts +0 -216
- package/examples/002-slack-bridge/scenario.test.ts +0 -223
- package/examples/002-slack-bridge/sinks.ts +0 -109
- package/examples/002-slack-bridge/worker.ts +0 -140
- package/examples/003-sandbox-lifecycle/README.md +0 -78
- package/examples/003-sandbox-lifecycle/events.ts +0 -203
- package/examples/003-sandbox-lifecycle/reducer.ts +0 -305
- package/examples/003-sandbox-lifecycle/scenario.test.ts +0 -202
- package/examples/_shared/event-id.ts +0 -12
- package/examples/_shared/http.ts +0 -27
- package/examples/_shared/openai.ts +0 -33
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { SequenceNumber } from "../types/core.js";
|
|
2
|
+
import { yr as UnixEpochMillis } from "../durable-DsV9PxZC.js";
|
|
3
|
+
import { EDARuntime } from "./runtime.js";
|
|
4
|
+
import { EDAWebSocketEventsFrame, EDAWebSocketHeartbeatFrame, EDAWebSocketHelloFrame, EDA_WEB_SOCKET_PROTOCOL_VERSION, FrameId, SubscriberId } from "../host/websocket-protocol.js";
|
|
5
|
+
import * as Effect from "effect/Effect";
|
|
6
|
+
import * as Schema from "effect/Schema";
|
|
7
|
+
import * as Stream from "effect/Stream";
|
|
8
|
+
import * as Clock from "effect/Clock";
|
|
9
|
+
import * as Queue from "effect/Queue";
|
|
10
|
+
import * as Ref from "effect/Ref";
|
|
11
|
+
import * as Deferred from "effect/Deferred";
|
|
12
|
+
//#region src/services/websocket-subscriber.ts
|
|
13
|
+
/** Slow subscriber reasons that intentionally close only the lagging socket. */
|
|
14
|
+
const SubscriberLagReason = Schema.Literals(["buffer-overflow", "ack-timeout"]);
|
|
15
|
+
/** The subscriber could not keep up with the bounded live-delivery policy. */
|
|
16
|
+
var SubscriberLagged = class extends Schema.TaggedErrorClass()("SubscriberLagged", {
|
|
17
|
+
subscriberId: SubscriberId,
|
|
18
|
+
lastAckedSeq: SequenceNumber,
|
|
19
|
+
reason: SubscriberLagReason
|
|
20
|
+
}) {};
|
|
21
|
+
/** The client sent a malformed or impossible protocol frame. */
|
|
22
|
+
var SubscriberProtocolError = class extends Schema.TaggedErrorClass()("SubscriberProtocolError", {
|
|
23
|
+
subscriberId: SubscriberId,
|
|
24
|
+
message: Schema.String
|
|
25
|
+
}) {};
|
|
26
|
+
/** The host WebSocket boundary failed while sending a frame. */
|
|
27
|
+
var SubscriberSendFailed = class extends Schema.TaggedErrorClass()("SubscriberSendFailed", {
|
|
28
|
+
subscriberId: SubscriberId,
|
|
29
|
+
cause: Schema.Defect()
|
|
30
|
+
}) {};
|
|
31
|
+
/** Run one scoped, ACK-gated live-event WebSocket subscriber. */
|
|
32
|
+
const runWebSocketSubscriber = (input) => Effect.gen(function* () {
|
|
33
|
+
const eda = yield* EDARuntime;
|
|
34
|
+
const outbound = yield* Queue.dropping(input.policy.subscriberBufferCapacityEvents);
|
|
35
|
+
const windowSignal = yield* Queue.sliding(1);
|
|
36
|
+
const timeoutFailure = yield* Deferred.make();
|
|
37
|
+
const lastAckedSeq = yield* Ref.make(input.resumeSeq);
|
|
38
|
+
const lastAckedFrameId = yield* Ref.make(0);
|
|
39
|
+
const lastSentFrameId = yield* Ref.make(0);
|
|
40
|
+
const lastSentDurableThroughSeq = yield* Ref.make(input.resumeSeq);
|
|
41
|
+
const nextFrameId = yield* Ref.make(FrameId.make(1));
|
|
42
|
+
const inFlight = yield* Ref.make([]);
|
|
43
|
+
yield* input.transport.send(EDAWebSocketHelloFrame.make({
|
|
44
|
+
_tag: "hello",
|
|
45
|
+
protocolVersion: EDA_WEB_SOCKET_PROTOCOL_VERSION,
|
|
46
|
+
subscriberId: input.subscriberId,
|
|
47
|
+
resumeSeq: input.resumeSeq,
|
|
48
|
+
flowControl: input.policy
|
|
49
|
+
}));
|
|
50
|
+
const pump = (yield* eda.eventsAfter(input.resumeSeq)).pipe(Stream.interruptWhen(Deferred.await(input.transport.closed)), Stream.runForEach((event) => Queue.offer(outbound, event).pipe(Effect.flatMap((accepted) => accepted ? Effect.void : Effect.gen(function* () {
|
|
51
|
+
const seq = yield* Ref.get(lastAckedSeq);
|
|
52
|
+
return yield* new SubscriberLagged({
|
|
53
|
+
subscriberId: input.subscriberId,
|
|
54
|
+
lastAckedSeq: seq,
|
|
55
|
+
reason: "buffer-overflow"
|
|
56
|
+
});
|
|
57
|
+
})))));
|
|
58
|
+
const sender = sendLoop({
|
|
59
|
+
inFlight,
|
|
60
|
+
input,
|
|
61
|
+
lastAckedSeq,
|
|
62
|
+
lastSentDurableThroughSeq,
|
|
63
|
+
lastSentFrameId,
|
|
64
|
+
nextFrameId,
|
|
65
|
+
outbound,
|
|
66
|
+
timeoutFailure,
|
|
67
|
+
windowSignal
|
|
68
|
+
});
|
|
69
|
+
const ackLoop = receiveAcks({
|
|
70
|
+
inFlight,
|
|
71
|
+
input,
|
|
72
|
+
lastAckedFrameId,
|
|
73
|
+
lastAckedSeq,
|
|
74
|
+
lastSentFrameId,
|
|
75
|
+
windowSignal
|
|
76
|
+
});
|
|
77
|
+
yield* Effect.raceFirst(Deferred.await(input.transport.closed), Effect.raceFirst(Deferred.await(timeoutFailure), Effect.raceFirst(pump, Effect.raceFirst(sender, ackLoop))));
|
|
78
|
+
});
|
|
79
|
+
const sendLoop = (input) => Effect.forever(Effect.gen(function* () {
|
|
80
|
+
yield* waitForWindow(input.inFlight, input.windowSignal, input.input.policy.maxInFlightFrames);
|
|
81
|
+
const next = yield* takeNextOutboundOrHeartbeat(input.outbound, input.input.policy.heartbeatIntervalMs);
|
|
82
|
+
if (next._tag === "heartbeat") {
|
|
83
|
+
yield* sendHeartbeat(input.input, input.lastSentDurableThroughSeq);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const event = next.event;
|
|
87
|
+
const frameId = yield* Ref.get(input.nextFrameId);
|
|
88
|
+
yield* Ref.set(input.nextFrameId, FrameId.make(frameId + 1));
|
|
89
|
+
const previousSentDurableSeq = yield* Ref.get(input.lastSentDurableThroughSeq);
|
|
90
|
+
const durableThroughSeq = durableThroughForFrame([event], previousSentDurableSeq);
|
|
91
|
+
const frame = EDAWebSocketEventsFrame.make({
|
|
92
|
+
_tag: "events",
|
|
93
|
+
frameId,
|
|
94
|
+
events: [event],
|
|
95
|
+
durableThroughSeq
|
|
96
|
+
});
|
|
97
|
+
const encodedBytes = encodedFrameBytes(frame);
|
|
98
|
+
if (encodedBytes > input.input.policy.maxFrameBytes) return yield* new SubscriberProtocolError({
|
|
99
|
+
subscriberId: input.input.subscriberId,
|
|
100
|
+
message: `Encoded WebSocket frame exceeded maxFrameBytes (${encodedBytes} > ${input.input.policy.maxFrameBytes})`
|
|
101
|
+
});
|
|
102
|
+
const sent = {
|
|
103
|
+
frameId,
|
|
104
|
+
durableThroughSeq,
|
|
105
|
+
sentAtMs: yield* Clock.currentTimeMillis
|
|
106
|
+
};
|
|
107
|
+
yield* Ref.update(input.inFlight, (frames) => [...frames, sent]);
|
|
108
|
+
yield* Ref.set(input.lastSentFrameId, frameId);
|
|
109
|
+
yield* Ref.set(input.lastSentDurableThroughSeq, durableThroughSeq);
|
|
110
|
+
yield* input.input.transport.send(frame);
|
|
111
|
+
yield* startAckTimeout(input.input, input.inFlight, input.lastAckedSeq, input.timeoutFailure, sent);
|
|
112
|
+
}));
|
|
113
|
+
const takeNextOutboundOrHeartbeat = (outbound, heartbeatIntervalMs) => Effect.raceFirst(Queue.take(outbound).pipe(Effect.map((event) => ({
|
|
114
|
+
_tag: "event",
|
|
115
|
+
event
|
|
116
|
+
}))), Effect.sleep(`${heartbeatIntervalMs} millis`).pipe(Effect.as({ _tag: "heartbeat" })));
|
|
117
|
+
const sendHeartbeat = (input, lastSentDurableThroughSeq) => Effect.gen(function* () {
|
|
118
|
+
const durableThroughSeq = yield* Ref.get(lastSentDurableThroughSeq);
|
|
119
|
+
const serverTimeMs = yield* Clock.currentTimeMillis;
|
|
120
|
+
yield* input.transport.send(EDAWebSocketHeartbeatFrame.make({
|
|
121
|
+
_tag: "heartbeat",
|
|
122
|
+
serverTimeMs: UnixEpochMillis.make(serverTimeMs),
|
|
123
|
+
durableThroughSeq
|
|
124
|
+
}));
|
|
125
|
+
});
|
|
126
|
+
const receiveAcks = (input) => Effect.forever(Effect.gen(function* () {
|
|
127
|
+
const frame = yield* Queue.take(input.input.transport.incoming);
|
|
128
|
+
if (frame._tag !== "ack") return yield* new SubscriberProtocolError({
|
|
129
|
+
subscriberId: input.input.subscriberId,
|
|
130
|
+
message: `Unsupported client frame ${frame._tag ?? "unknown"}`
|
|
131
|
+
});
|
|
132
|
+
yield* applyAck(input, frame);
|
|
133
|
+
}));
|
|
134
|
+
const applyAck = (input, ack) => Effect.gen(function* () {
|
|
135
|
+
const lastAckedFrame = yield* Ref.get(input.lastAckedFrameId);
|
|
136
|
+
const lastSentFrame = yield* Ref.get(input.lastSentFrameId);
|
|
137
|
+
const currentSeq = yield* Ref.get(input.lastAckedSeq);
|
|
138
|
+
if (ack.frameId <= lastAckedFrame) {
|
|
139
|
+
if (ack.durableThroughSeq > currentSeq) return yield* new SubscriberProtocolError({
|
|
140
|
+
subscriberId: input.input.subscriberId,
|
|
141
|
+
message: "Duplicate ACK attempted to advance durable seq"
|
|
142
|
+
});
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
if (ack.frameId > lastSentFrame) return yield* new SubscriberProtocolError({
|
|
146
|
+
subscriberId: input.input.subscriberId,
|
|
147
|
+
message: "ACK referenced an unsent frame"
|
|
148
|
+
});
|
|
149
|
+
if (ack.durableThroughSeq < currentSeq) return yield* new SubscriberProtocolError({
|
|
150
|
+
subscriberId: input.input.subscriberId,
|
|
151
|
+
message: "ACK durable seq moved backwards"
|
|
152
|
+
});
|
|
153
|
+
const acknowledged = (yield* Ref.get(input.inFlight)).filter((frame) => frame.frameId <= ack.frameId);
|
|
154
|
+
if (acknowledged.length === 0) return yield* new SubscriberProtocolError({
|
|
155
|
+
subscriberId: input.input.subscriberId,
|
|
156
|
+
message: "ACK referenced no in-flight frames"
|
|
157
|
+
});
|
|
158
|
+
const allowedDurableSeq = SequenceNumber.make(acknowledged.reduce((max, frame) => Math.max(max, frame.durableThroughSeq), currentSeq));
|
|
159
|
+
if (ack.durableThroughSeq > allowedDurableSeq) return yield* new SubscriberProtocolError({
|
|
160
|
+
subscriberId: input.input.subscriberId,
|
|
161
|
+
message: "ACK durable seq exceeded sent frame boundary"
|
|
162
|
+
});
|
|
163
|
+
yield* Ref.update(input.inFlight, (frames) => frames.filter((frame) => frame.frameId > ack.frameId));
|
|
164
|
+
yield* Ref.set(input.lastAckedFrameId, ack.frameId);
|
|
165
|
+
yield* Ref.set(input.lastAckedSeq, ack.durableThroughSeq);
|
|
166
|
+
yield* input.input.transport.persistAck(ack.durableThroughSeq);
|
|
167
|
+
yield* Queue.offer(input.windowSignal, void 0);
|
|
168
|
+
});
|
|
169
|
+
const waitForWindow = (inFlight, windowSignal, maxInFlightFrames) => Effect.gen(function* () {
|
|
170
|
+
while (true) {
|
|
171
|
+
if ((yield* Ref.get(inFlight)).length < maxInFlightFrames) return;
|
|
172
|
+
yield* Queue.take(windowSignal);
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
const startAckTimeout = (input, inFlight, lastAckedSeq, timeoutFailure, sent) => Effect.gen(function* () {
|
|
176
|
+
yield* Effect.sleep(`${input.policy.ackTimeoutMs} millis`);
|
|
177
|
+
if (!(yield* Ref.get(inFlight)).some((frame) => frame.frameId === sent.frameId)) return;
|
|
178
|
+
const seq = yield* Ref.get(lastAckedSeq);
|
|
179
|
+
yield* Deferred.fail(timeoutFailure, new SubscriberLagged({
|
|
180
|
+
subscriberId: input.subscriberId,
|
|
181
|
+
lastAckedSeq: seq,
|
|
182
|
+
reason: "ack-timeout"
|
|
183
|
+
}));
|
|
184
|
+
}).pipe(Effect.forkScoped, Effect.asVoid);
|
|
185
|
+
const durableThroughForFrame = (events, previousSentDurableSeq) => SequenceNumber.make(events.reduce((max, event) => event.event.durability === "durable" ? Math.max(max, event.position.seq) : max, previousSentDurableSeq));
|
|
186
|
+
const encodedFrameBytes = (frame) => new TextEncoder().encode(JSON.stringify(frame)).byteLength;
|
|
187
|
+
//#endregion
|
|
188
|
+
export { SubscriberLagReason, SubscriberLagged, SubscriberProtocolError, SubscriberSendFailed, runWebSocketSubscriber };
|
|
189
|
+
|
|
190
|
+
//# sourceMappingURL=websocket-subscriber.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"websocket-subscriber.js","names":[],"sources":["../../src/services/websocket-subscriber.ts"],"sourcesContent":["import * as Clock from \"effect/Clock\";\nimport * as Deferred from \"effect/Deferred\";\nimport * as Effect from \"effect/Effect\";\nimport * as Queue from \"effect/Queue\";\nimport * as Ref from \"effect/Ref\";\nimport * as Schema from \"effect/Schema\";\nimport type * as Scope from \"effect/Scope\";\nimport * as Stream from \"effect/Stream\";\n\nimport { EDARuntime } from \"./runtime\";\nimport { EDASessionStoreError } from \"./session-store\";\nimport { SequenceNumber } from \"../types/core\";\nimport { UnixEpochMillis, type PositionedEvent } from \"../types/events\";\nimport {\n EDA_WEB_SOCKET_PROTOCOL_VERSION,\n EDAWebSocketAckFrame,\n EDAWebSocketEventsFrame,\n EDAWebSocketFlowControl,\n EDAWebSocketHeartbeatFrame,\n EDAWebSocketHelloFrame,\n FrameId,\n SubscriberId,\n type EDAWebSocketClientFrame,\n type EDAWebSocketServerFrame,\n} from \"../host/websocket-protocol\";\n\n/** Slow subscriber reasons that intentionally close only the lagging socket. */\nexport const SubscriberLagReason = Schema.Literals([\"buffer-overflow\", \"ack-timeout\"]);\nexport type SubscriberLagReason = typeof SubscriberLagReason.Type;\n\n/** The subscriber could not keep up with the bounded live-delivery policy. */\nexport class SubscriberLagged extends Schema.TaggedErrorClass<SubscriberLagged>()(\n \"SubscriberLagged\",\n {\n subscriberId: SubscriberId,\n lastAckedSeq: SequenceNumber,\n reason: SubscriberLagReason,\n },\n) {}\n\n/** The client sent a malformed or impossible protocol frame. */\nexport class SubscriberProtocolError extends Schema.TaggedErrorClass<SubscriberProtocolError>()(\n \"SubscriberProtocolError\",\n {\n subscriberId: SubscriberId,\n message: Schema.String,\n },\n) {}\n\n/** The host WebSocket boundary failed while sending a frame. */\nexport class SubscriberSendFailed extends Schema.TaggedErrorClass<SubscriberSendFailed>()(\n \"SubscriberSendFailed\",\n {\n subscriberId: SubscriberId,\n cause: Schema.Defect(),\n },\n) {}\n\n/** Expected error surface for one WebSocket subscriber workflow. */\nexport type EDAWebSocketSubscriberError =\n | SubscriberLagged\n | SubscriberProtocolError\n | SubscriberSendFailed\n | EDASessionStoreError;\n\n/** Transport boundary implemented by a host WebSocket adapter or tests. */\nexport interface EDAWebSocketSubscriberTransport {\n readonly send: (frame: EDAWebSocketServerFrame) => Effect.Effect<void, SubscriberSendFailed>;\n readonly incoming: Queue.Queue<EDAWebSocketClientFrame>;\n readonly closed: Deferred.Deferred<void>;\n readonly persistAck: (seq: SequenceNumber) => Effect.Effect<void>;\n}\n\n/** Configuration for one scoped ACK-window subscriber workflow. */\nexport interface EDAWebSocketSubscriberInput {\n readonly subscriberId: SubscriberId;\n readonly resumeSeq: SequenceNumber;\n readonly policy: EDAWebSocketFlowControl;\n readonly transport: EDAWebSocketSubscriberTransport;\n}\n\ninterface SentFrame {\n readonly frameId: FrameId;\n readonly durableThroughSeq: SequenceNumber;\n readonly sentAtMs: number;\n}\n\n/** Run one scoped, ACK-gated live-event WebSocket subscriber. */\nexport const runWebSocketSubscriber = (\n input: EDAWebSocketSubscriberInput,\n): Effect.Effect<void, EDAWebSocketSubscriberError, EDARuntime | Scope.Scope> =>\n Effect.gen(function* () {\n const eda = yield* EDARuntime;\n const outbound = yield* Queue.dropping<PositionedEvent>(\n input.policy.subscriberBufferCapacityEvents,\n );\n const windowSignal = yield* Queue.sliding<void>(1);\n const timeoutFailure = yield* Deferred.make<never, SubscriberLagged>();\n const lastAckedSeq = yield* Ref.make(input.resumeSeq);\n const lastAckedFrameId = yield* Ref.make(0);\n const lastSentFrameId = yield* Ref.make(0);\n const lastSentDurableThroughSeq = yield* Ref.make(input.resumeSeq);\n const nextFrameId = yield* Ref.make(FrameId.make(1));\n const inFlight = yield* Ref.make<ReadonlyArray<SentFrame>>([]);\n\n yield* input.transport.send(\n EDAWebSocketHelloFrame.make({\n _tag: \"hello\",\n protocolVersion: EDA_WEB_SOCKET_PROTOCOL_VERSION,\n subscriberId: input.subscriberId,\n resumeSeq: input.resumeSeq,\n flowControl: input.policy,\n }),\n );\n\n const events = yield* eda.eventsAfter(input.resumeSeq);\n // TODO(backpressure): this pump drains the replay/live stream into an intermediate queue ahead\n // of the ACK window. That bounded queue protects memory, but it still breaks end-to-end\n // backpressure from slow WebSocket clients to host-level paged durable replay.\n const pump = events.pipe(\n Stream.interruptWhen(Deferred.await(input.transport.closed)),\n Stream.runForEach((event) =>\n Queue.offer(outbound, event).pipe(\n Effect.flatMap((accepted) =>\n accepted\n ? Effect.void\n : Effect.gen(function* () {\n const seq = yield* Ref.get(lastAckedSeq);\n return yield* new SubscriberLagged({\n subscriberId: input.subscriberId,\n lastAckedSeq: seq,\n reason: \"buffer-overflow\",\n });\n }),\n ),\n ),\n ),\n );\n\n const sender = sendLoop({\n inFlight,\n input,\n lastAckedSeq,\n lastSentDurableThroughSeq,\n lastSentFrameId,\n nextFrameId,\n outbound,\n timeoutFailure,\n windowSignal,\n });\n const ackLoop = receiveAcks({\n inFlight,\n input,\n lastAckedFrameId,\n lastAckedSeq,\n lastSentFrameId,\n windowSignal,\n });\n\n yield* Effect.raceFirst(\n Deferred.await(input.transport.closed),\n Effect.raceFirst(\n Deferred.await(timeoutFailure),\n Effect.raceFirst(pump, Effect.raceFirst(sender, ackLoop)),\n ),\n );\n });\n\nconst sendLoop = (input: {\n readonly inFlight: Ref.Ref<ReadonlyArray<SentFrame>>;\n readonly input: EDAWebSocketSubscriberInput;\n readonly lastAckedSeq: Ref.Ref<SequenceNumber>;\n readonly lastSentDurableThroughSeq: Ref.Ref<SequenceNumber>;\n readonly lastSentFrameId: Ref.Ref<number>;\n readonly nextFrameId: Ref.Ref<FrameId>;\n readonly outbound: Queue.Queue<PositionedEvent>;\n readonly timeoutFailure: Deferred.Deferred<never, SubscriberLagged>;\n readonly windowSignal: Queue.Queue<void>;\n}): Effect.Effect<\n void,\n SubscriberLagged | SubscriberProtocolError | SubscriberSendFailed,\n Scope.Scope\n> =>\n Effect.forever(\n Effect.gen(function* () {\n yield* waitForWindow(\n input.inFlight,\n input.windowSignal,\n input.input.policy.maxInFlightFrames,\n );\n const next = yield* takeNextOutboundOrHeartbeat(\n input.outbound,\n input.input.policy.heartbeatIntervalMs,\n );\n if (next._tag === \"heartbeat\") {\n yield* sendHeartbeat(input.input, input.lastSentDurableThroughSeq);\n return;\n }\n\n const event = next.event;\n const frameId = yield* Ref.get(input.nextFrameId);\n yield* Ref.set(input.nextFrameId, FrameId.make(frameId + 1));\n\n const previousSentDurableSeq = yield* Ref.get(input.lastSentDurableThroughSeq);\n const durableThroughSeq = durableThroughForFrame([event], previousSentDurableSeq);\n const frame = EDAWebSocketEventsFrame.make({\n _tag: \"events\",\n frameId,\n events: [event],\n durableThroughSeq,\n });\n const encodedBytes = encodedFrameBytes(frame);\n if (encodedBytes > input.input.policy.maxFrameBytes) {\n return yield* new SubscriberProtocolError({\n subscriberId: input.input.subscriberId,\n message: `Encoded WebSocket frame exceeded maxFrameBytes (${encodedBytes} > ${input.input.policy.maxFrameBytes})`,\n });\n }\n\n const sentAtMs = yield* Clock.currentTimeMillis;\n const sent = { frameId, durableThroughSeq, sentAtMs } satisfies SentFrame;\n // Mark the frame in-flight before the synchronous host send so an immediate\n // client ACK cannot race ahead of our bookkeeping.\n yield* Ref.update(input.inFlight, (frames) => [...frames, sent]);\n yield* Ref.set(input.lastSentFrameId, frameId);\n yield* Ref.set(input.lastSentDurableThroughSeq, durableThroughSeq);\n yield* input.input.transport.send(frame);\n yield* startAckTimeout(\n input.input,\n input.inFlight,\n input.lastAckedSeq,\n input.timeoutFailure,\n sent,\n );\n }),\n );\n\nconst takeNextOutboundOrHeartbeat = (\n outbound: Queue.Queue<PositionedEvent>,\n heartbeatIntervalMs: number,\n): Effect.Effect<\n { readonly _tag: \"event\"; readonly event: PositionedEvent } | { readonly _tag: \"heartbeat\" }\n> =>\n Effect.raceFirst(\n Queue.take(outbound).pipe(Effect.map((event) => ({ _tag: \"event\", event }) as const)),\n Effect.sleep(`${heartbeatIntervalMs} millis`).pipe(Effect.as({ _tag: \"heartbeat\" } as const)),\n );\n\nconst sendHeartbeat = (\n input: EDAWebSocketSubscriberInput,\n lastSentDurableThroughSeq: Ref.Ref<SequenceNumber>,\n): Effect.Effect<void, SubscriberSendFailed> =>\n Effect.gen(function* () {\n const durableThroughSeq = yield* Ref.get(lastSentDurableThroughSeq);\n const serverTimeMs = yield* Clock.currentTimeMillis;\n yield* input.transport.send(\n EDAWebSocketHeartbeatFrame.make({\n _tag: \"heartbeat\",\n serverTimeMs: UnixEpochMillis.make(serverTimeMs),\n durableThroughSeq,\n }),\n );\n });\n\nconst receiveAcks = (input: {\n readonly inFlight: Ref.Ref<ReadonlyArray<SentFrame>>;\n readonly input: EDAWebSocketSubscriberInput;\n readonly lastAckedFrameId: Ref.Ref<number>;\n readonly lastAckedSeq: Ref.Ref<SequenceNumber>;\n readonly lastSentFrameId: Ref.Ref<number>;\n readonly windowSignal: Queue.Queue<void>;\n}): Effect.Effect<void, SubscriberProtocolError> =>\n Effect.forever(\n Effect.gen(function* () {\n const frame = yield* Queue.take(input.input.transport.incoming);\n if (frame._tag !== \"ack\") {\n return yield* new SubscriberProtocolError({\n subscriberId: input.input.subscriberId,\n message: `Unsupported client frame ${\n (\n frame as {\n readonly _tag?: string;\n }\n )._tag ?? \"unknown\"\n }`,\n });\n }\n yield* applyAck(input, frame as EDAWebSocketAckFrame);\n }),\n );\n\nconst applyAck = (\n input: Parameters<typeof receiveAcks>[0],\n ack: EDAWebSocketAckFrame,\n): Effect.Effect<void, SubscriberProtocolError> =>\n Effect.gen(function* () {\n const lastAckedFrame = yield* Ref.get(input.lastAckedFrameId);\n const lastSentFrame = yield* Ref.get(input.lastSentFrameId);\n const currentSeq = yield* Ref.get(input.lastAckedSeq);\n\n if (ack.frameId <= lastAckedFrame) {\n if (ack.durableThroughSeq > currentSeq) {\n return yield* new SubscriberProtocolError({\n subscriberId: input.input.subscriberId,\n message: \"Duplicate ACK attempted to advance durable seq\",\n });\n }\n return;\n }\n\n if (ack.frameId > lastSentFrame) {\n return yield* new SubscriberProtocolError({\n subscriberId: input.input.subscriberId,\n message: \"ACK referenced an unsent frame\",\n });\n }\n\n if (ack.durableThroughSeq < currentSeq) {\n return yield* new SubscriberProtocolError({\n subscriberId: input.input.subscriberId,\n message: \"ACK durable seq moved backwards\",\n });\n }\n\n const frames = yield* Ref.get(input.inFlight);\n const acknowledged = frames.filter((frame) => frame.frameId <= ack.frameId);\n if (acknowledged.length === 0) {\n return yield* new SubscriberProtocolError({\n subscriberId: input.input.subscriberId,\n message: \"ACK referenced no in-flight frames\",\n });\n }\n const allowedDurableSeq = SequenceNumber.make(\n acknowledged.reduce<number>(\n (max, frame) => Math.max(max, frame.durableThroughSeq),\n currentSeq,\n ),\n );\n if (ack.durableThroughSeq > allowedDurableSeq) {\n return yield* new SubscriberProtocolError({\n subscriberId: input.input.subscriberId,\n message: \"ACK durable seq exceeded sent frame boundary\",\n });\n }\n\n yield* Ref.update(input.inFlight, (frames) =>\n frames.filter((frame) => frame.frameId > ack.frameId),\n );\n yield* Ref.set(input.lastAckedFrameId, ack.frameId);\n yield* Ref.set(input.lastAckedSeq, ack.durableThroughSeq);\n yield* input.input.transport.persistAck(ack.durableThroughSeq);\n yield* Queue.offer(input.windowSignal, undefined);\n });\n\nconst waitForWindow = (\n inFlight: Ref.Ref<ReadonlyArray<SentFrame>>,\n windowSignal: Queue.Queue<void>,\n maxInFlightFrames: number,\n): Effect.Effect<void> =>\n Effect.gen(function* () {\n while (true) {\n const frames = yield* Ref.get(inFlight);\n if (frames.length < maxInFlightFrames) {\n return;\n }\n yield* Queue.take(windowSignal);\n }\n });\n\nconst startAckTimeout = (\n input: EDAWebSocketSubscriberInput,\n inFlight: Ref.Ref<ReadonlyArray<SentFrame>>,\n lastAckedSeq: Ref.Ref<SequenceNumber>,\n timeoutFailure: Deferred.Deferred<never, SubscriberLagged>,\n sent: SentFrame,\n): Effect.Effect<void, never, Scope.Scope> =>\n Effect.gen(function* () {\n yield* Effect.sleep(`${input.policy.ackTimeoutMs} millis`);\n const stillInFlight = (yield* Ref.get(inFlight)).some(\n (frame) => frame.frameId === sent.frameId,\n );\n if (!stillInFlight) {\n return;\n }\n const seq = yield* Ref.get(lastAckedSeq);\n yield* Deferred.fail(\n timeoutFailure,\n new SubscriberLagged({\n subscriberId: input.subscriberId,\n lastAckedSeq: seq,\n reason: \"ack-timeout\",\n }),\n );\n }).pipe(Effect.forkScoped, Effect.asVoid);\n\nconst durableThroughForFrame = (\n events: ReadonlyArray<PositionedEvent>,\n previousSentDurableSeq: SequenceNumber,\n): SequenceNumber =>\n SequenceNumber.make(\n events.reduce<number>(\n (max, event) =>\n event.event.durability === \"durable\" ? Math.max(max, event.position.seq) : max,\n previousSentDurableSeq,\n ),\n );\n\nconst encodedFrameBytes = (frame: EDAWebSocketServerFrame): number =>\n new TextEncoder().encode(JSON.stringify(frame)).byteLength;\n"],"mappings":";;;;;;;;;;;;;AA2BA,MAAa,sBAAsB,OAAO,SAAS,CAAC,mBAAmB,aAAa,CAAC;;AAIrF,IAAa,mBAAb,cAAsC,OAAO,iBAAmC,EAC9E,oBACA;CACE,cAAc;CACd,cAAc;CACd,QAAQ;AACV,CACF,EAAE,CAAC;;AAGH,IAAa,0BAAb,cAA6C,OAAO,iBAA0C,EAC5F,2BACA;CACE,cAAc;CACd,SAAS,OAAO;AAClB,CACF,EAAE,CAAC;;AAGH,IAAa,uBAAb,cAA0C,OAAO,iBAAuC,EACtF,wBACA;CACE,cAAc;CACd,OAAO,OAAO,OAAO;AACvB,CACF,EAAE,CAAC;;AAgCH,MAAa,0BACX,UAEA,OAAO,IAAI,aAAa;CACtB,MAAM,MAAM,OAAO;CACnB,MAAM,WAAW,OAAO,MAAM,SAC5B,MAAM,OAAO,8BACf;CACA,MAAM,eAAe,OAAO,MAAM,QAAc,CAAC;CACjD,MAAM,iBAAiB,OAAO,SAAS,KAA8B;CACrE,MAAM,eAAe,OAAO,IAAI,KAAK,MAAM,SAAS;CACpD,MAAM,mBAAmB,OAAO,IAAI,KAAK,CAAC;CAC1C,MAAM,kBAAkB,OAAO,IAAI,KAAK,CAAC;CACzC,MAAM,4BAA4B,OAAO,IAAI,KAAK,MAAM,SAAS;CACjE,MAAM,cAAc,OAAO,IAAI,KAAK,QAAQ,KAAK,CAAC,CAAC;CACnD,MAAM,WAAW,OAAO,IAAI,KAA+B,CAAC,CAAC;CAE7D,OAAO,MAAM,UAAU,KACrB,uBAAuB,KAAK;EAC1B,MAAM;EACN,iBAAiB;EACjB,cAAc,MAAM;EACpB,WAAW,MAAM;EACjB,aAAa,MAAM;CACrB,CAAC,CACH;CAMA,MAAM,QAAO,OAJS,IAAI,YAAY,MAAM,SAAS,GAIjC,KAClB,OAA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AI,MAAM,cAAc,IAAI,iBAAiB;CACxD,OAAO,MAAM,MAAM,UAAU,WAAW,IAAI,iBAAiB;CAC7D,OAAO,MAAM,MAAM,MAAM,cAAc,KAAA,CAAS;AAClD,CAAC;AAEH,MAAM,iBACJ,UACA,cACA,sBAEA,OAAO,IAAI,aAAa;CACtB,OAAO,MAAM;EAEX,KAAI,OADkB,IAAI,IAAI,QAAQ,GAC3B,SAAS,mBAClB;EAEF,OAAO,MAAM,KAAK,YAAY;CAChC;AACF,CAAC;AAEH,MAAM,mBACJ,OACA,UACA,cACA,gBACA,SAEA,OAAO,IAAI,aAAa;CACtB,OAAO,OAAO,MAAM,GAAG,MAAM,OAAO,aAAa,QAAQ;CAIzD,IAAI,EAHmB,OAAO,IAAI,IAAI,QAAQ,GAAG,MAC9C,UAAU,MAAM,YAAY,KAAK,OAEnB,GACf;CAEF,MAAM,MAAM,OAAO,IAAI,IAAI,YAAY;CACvC,OAAO,SAAS,KACd,gBACA,IAAI,iBAAiB;EACnB,cAAc,MAAM;EACpB,cAAc;EACd,QAAQ;CACV,CAAC,CACH;AACF,CAAC,EAAE,KAAK,OAAO,YAAY,OAAO,MAAM;AAE1C,MAAM,0BACJ,QACA,2BAEA,eAAe,KACb,OAAO,QACJ,KAAK,UACJ,MAAM,MAAM,eAAe,YAAY,KAAK,IAAI,KAAK,MAAM,SAAS,GAAG,IAAI,KAC7E,sBACF,CACF;AAEF,MAAM,qBAAqB,UACzB,IAAI,YAAY,EAAE,OAAO,KAAK,UAAU,KAAK,CAAC,EAAE"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { m as SequenceNumber } from "./core-CYT6DBaS.js";
|
|
2
|
+
import { L as PositionedEvent } from "./events-Bb9-Cu-7.js";
|
|
3
|
+
import { a as EDASessionStore, o as EDASessionStoreError } from "./session-store-DZZUffUy.js";
|
|
4
|
+
import { S as ReducedState } from "./command-queues-CV7O-51r.js";
|
|
5
|
+
import { t as DurableTranscriptMessage } from "./message-transcript-DiOPBY3l.js";
|
|
6
|
+
import { u as LiveEventBus } from "./sink-registry-Cf79ICdW.js";
|
|
7
|
+
import { i as EDAReducerStateSnapshot } from "./reducer-registry-CjSJBPKo.js";
|
|
8
|
+
import { h as SessionState } from "./session-state-CjOSvWbe.js";
|
|
9
|
+
import * as Context from "effect/Context";
|
|
10
|
+
import * as Effect from "effect/Effect";
|
|
11
|
+
import * as Layer from "effect/Layer";
|
|
12
|
+
import * as Stream from "effect/Stream";
|
|
13
|
+
import * as Scope from "effect/Scope";
|
|
14
|
+
|
|
15
|
+
//#region src/services/session-query.d.ts
|
|
16
|
+
/** Authoritative live snapshot for one EDA session plus its derived durable transcript. */
|
|
17
|
+
interface EDASessionSnapshot {
|
|
18
|
+
readonly state: ReducedState;
|
|
19
|
+
readonly reducerStates: EDAReducerStateSnapshot;
|
|
20
|
+
readonly messages: ReadonlyArray<DurableTranscriptMessage>;
|
|
21
|
+
}
|
|
22
|
+
/** Read-only query facade over authoritative live state and reconnect-safe event streams. */
|
|
23
|
+
interface EDASessionQueryShape {
|
|
24
|
+
/** Read the authoritative live state and derive its durable transcript. */
|
|
25
|
+
readonly snapshot: () => Effect.Effect<EDASessionSnapshot, EDASessionStoreError>;
|
|
26
|
+
/** Read durable user/assistant messages in committed sequence order from the live snapshot. */
|
|
27
|
+
readonly messages: () => Effect.Effect<ReadonlyArray<DurableTranscriptMessage>, EDASessionStoreError>;
|
|
28
|
+
/** Backfill durable events after `afterSeq`, then follow live durable/ephemeral events. */
|
|
29
|
+
readonly eventsAfter: (afterSeq: SequenceNumber) => Effect.Effect<Stream.Stream<PositionedEvent, EDASessionStoreError>, EDASessionStoreError, Scope.Scope>;
|
|
30
|
+
}
|
|
31
|
+
declare const EDASessionQuery_base: Context.ServiceClass<EDASessionQuery, "@effect-durable-agent/EDASessionQuery", EDASessionQueryShape>;
|
|
32
|
+
/** Read-only session query facade backed by authoritative SessionState plus durable backfill. */
|
|
33
|
+
declare class EDASessionQuery extends EDASessionQuery_base {
|
|
34
|
+
static readonly Live: Layer.Layer<EDASessionQuery, never, EDASessionStore | SessionState | LiveEventBus>;
|
|
35
|
+
}
|
|
36
|
+
//#endregion
|
|
37
|
+
export { EDASessionQueryShape as n, EDASessionSnapshot as r, EDASessionQuery as t };
|
|
38
|
+
//# sourceMappingURL=session-query-C-C4m1eq.d.ts.map
|
|
@@ -1,25 +1,22 @@
|
|
|
1
|
-
import { m as SequenceNumber } from "./core-Hv3cFydL.js";
|
|
2
1
|
import { r as EDACommand } from "./commands-Ce1ec6kH.js";
|
|
3
2
|
import { D as DurableEventEnvelope, F as ModelSelectionPayload, I as NonNegativeInt, L as PositionedEvent, O as EphemeralEventEnvelope } from "./events-Bb9-Cu-7.js";
|
|
4
|
-
import {
|
|
5
|
-
import { a as EDASessionStore, o as EDASessionStoreError, t as CommittedDurableEvent } from "./session-store-
|
|
6
|
-
import { S as ReducedState } from "./command-queues-
|
|
7
|
-
import { t as
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import { r as EDAToolRegistry } from "./tool-registry-
|
|
3
|
+
import { kt as ToolCallCreatedEvent } from "./durable-B7M6TD_0.js";
|
|
4
|
+
import { a as EDASessionStore, o as EDASessionStoreError, t as CommittedDurableEvent } from "./session-store-DZZUffUy.js";
|
|
5
|
+
import { S as ReducedState } from "./command-queues-CV7O-51r.js";
|
|
6
|
+
import { t as IdGenerator } from "./id-generator-Ckl8_XWW.js";
|
|
7
|
+
import { r as SessionContext, t as EventFactory } from "./event-factory-CLJcyymV.js";
|
|
8
|
+
import { t as EDAKeepAlive } from "./keep-alive-Chl5SJxy.js";
|
|
9
|
+
import { d as CompactionRunner } from "./compaction-zbvLREI8.js";
|
|
10
|
+
import { s as EDASinkRegistry, u as LiveEventBus } from "./sink-registry-Cf79ICdW.js";
|
|
11
|
+
import { i as EDAReducerStateSnapshot, n as EDAReducerRegistry } from "./reducer-registry-CjSJBPKo.js";
|
|
12
|
+
import { r as EDAPromptProjector } from "./prompt-projector-C1IbkHr0.js";
|
|
13
|
+
import { r as EDAToolRegistry } from "./tool-registry-B7Wona6N.js";
|
|
15
14
|
import * as Context from "effect/Context";
|
|
16
15
|
import * as Effect from "effect/Effect";
|
|
17
16
|
import * as Layer from "effect/Layer";
|
|
18
17
|
import * as Schema from "effect/Schema";
|
|
19
|
-
import * as Stream from "effect/Stream";
|
|
20
18
|
import * as Prompt from "effect/unstable/ai/Prompt";
|
|
21
19
|
import * as Cause from "effect/Cause";
|
|
22
|
-
import * as Scope from "effect/Scope";
|
|
23
20
|
import * as LanguageModel from "effect/unstable/ai/LanguageModel";
|
|
24
21
|
import * as Response from "effect/unstable/ai/Response";
|
|
25
22
|
|
|
@@ -294,7 +291,7 @@ interface InferenceRunnerShape {
|
|
|
294
291
|
declare const InferenceRunner_base: Context.ServiceClass<InferenceRunner, "@effect-durable-agent/InferenceRunner", InferenceRunnerShape>;
|
|
295
292
|
/** Owns provider inference streams and routes them into durable/live inference emissions. */
|
|
296
293
|
declare class InferenceRunner extends InferenceRunner_base {
|
|
297
|
-
static readonly Live: Layer.Layer<InferenceRunner, never, IdGenerator |
|
|
294
|
+
static readonly Live: Layer.Layer<InferenceRunner, never, IdGenerator | EventFactory | EDAToolRegistry | LanguageModel.LanguageModel>;
|
|
298
295
|
}
|
|
299
296
|
//#endregion
|
|
300
297
|
//#region src/services/turn-runner.d.ts
|
|
@@ -567,6 +564,120 @@ declare const SessionRunInput: Schema.Struct<{
|
|
|
567
564
|
readonly systemPrompt: Schema.optionalKey<Schema.NonEmptyString>;
|
|
568
565
|
}>;
|
|
569
566
|
type SessionRunInput = typeof SessionRunInput.Type;
|
|
567
|
+
/** Command outcome when the command's run completed and the command was completed. */
|
|
568
|
+
declare const SessionCommandCompleted: Schema.TaggedStruct<"SessionCommandCompleted", {
|
|
569
|
+
readonly committed: Schema.Struct<{
|
|
570
|
+
readonly position: Schema.Struct<{
|
|
571
|
+
readonly seq: Schema.brand<Schema.Int, "SequenceNumber">;
|
|
572
|
+
readonly subSeq: Schema.brand<Schema.Literal<0>, "SubSequenceNumber">;
|
|
573
|
+
}>;
|
|
574
|
+
readonly event: Schema.Struct<{
|
|
575
|
+
readonly durability: Schema.Literal<"durable">;
|
|
576
|
+
readonly namespace: Schema.NonEmptyString;
|
|
577
|
+
readonly type: Schema.NonEmptyString;
|
|
578
|
+
readonly schemaVersion: Schema.Int;
|
|
579
|
+
readonly eventId: Schema.brand<Schema.String, "EventId">;
|
|
580
|
+
readonly sessionId: Schema.brand<Schema.String, "SessionId">;
|
|
581
|
+
readonly correlationId: Schema.optionalKey<Schema.brand<Schema.String, "CorrelationId">>;
|
|
582
|
+
readonly createdAtMs: Schema.brand<Schema.Int, "UnixEpochMillis">;
|
|
583
|
+
readonly trace: Schema.withConstructorDefault<Schema.Struct<{
|
|
584
|
+
readonly span: Schema.Struct<{
|
|
585
|
+
readonly traceId: Schema.brand<Schema.String, "EDATraceId">;
|
|
586
|
+
readonly spanId: Schema.brand<Schema.String, "EDASpanId">;
|
|
587
|
+
readonly sampled: Schema.Boolean;
|
|
588
|
+
readonly tracestate: Schema.NullOr<Schema.String>;
|
|
589
|
+
}>;
|
|
590
|
+
readonly links: Schema.$Array<Schema.Struct<{
|
|
591
|
+
readonly context: Schema.Struct<{
|
|
592
|
+
readonly traceId: Schema.brand<Schema.String, "EDATraceId">;
|
|
593
|
+
readonly spanId: Schema.brand<Schema.String, "EDASpanId">;
|
|
594
|
+
readonly sampled: Schema.Boolean;
|
|
595
|
+
readonly tracestate: Schema.NullOr<Schema.String>;
|
|
596
|
+
}>;
|
|
597
|
+
readonly attributes: Schema.$Record<Schema.String, Schema.Union<readonly [Schema.String, Schema.Number, Schema.Boolean]>>;
|
|
598
|
+
}>>;
|
|
599
|
+
}>>;
|
|
600
|
+
readonly payload: Schema.Unknown;
|
|
601
|
+
}>;
|
|
602
|
+
}>;
|
|
603
|
+
}>;
|
|
604
|
+
type SessionCommandCompleted = typeof SessionCommandCompleted.Type;
|
|
605
|
+
/** Command outcome when the command's run failed and the command was failed. */
|
|
606
|
+
declare const SessionCommandFailed: Schema.TaggedStruct<"SessionCommandFailed", {
|
|
607
|
+
readonly committed: Schema.Struct<{
|
|
608
|
+
readonly position: Schema.Struct<{
|
|
609
|
+
readonly seq: Schema.brand<Schema.Int, "SequenceNumber">;
|
|
610
|
+
readonly subSeq: Schema.brand<Schema.Literal<0>, "SubSequenceNumber">;
|
|
611
|
+
}>;
|
|
612
|
+
readonly event: Schema.Struct<{
|
|
613
|
+
readonly durability: Schema.Literal<"durable">;
|
|
614
|
+
readonly namespace: Schema.NonEmptyString;
|
|
615
|
+
readonly type: Schema.NonEmptyString;
|
|
616
|
+
readonly schemaVersion: Schema.Int;
|
|
617
|
+
readonly eventId: Schema.brand<Schema.String, "EventId">;
|
|
618
|
+
readonly sessionId: Schema.brand<Schema.String, "SessionId">;
|
|
619
|
+
readonly correlationId: Schema.optionalKey<Schema.brand<Schema.String, "CorrelationId">>;
|
|
620
|
+
readonly createdAtMs: Schema.brand<Schema.Int, "UnixEpochMillis">;
|
|
621
|
+
readonly trace: Schema.withConstructorDefault<Schema.Struct<{
|
|
622
|
+
readonly span: Schema.Struct<{
|
|
623
|
+
readonly traceId: Schema.brand<Schema.String, "EDATraceId">;
|
|
624
|
+
readonly spanId: Schema.brand<Schema.String, "EDASpanId">;
|
|
625
|
+
readonly sampled: Schema.Boolean;
|
|
626
|
+
readonly tracestate: Schema.NullOr<Schema.String>;
|
|
627
|
+
}>;
|
|
628
|
+
readonly links: Schema.$Array<Schema.Struct<{
|
|
629
|
+
readonly context: Schema.Struct<{
|
|
630
|
+
readonly traceId: Schema.brand<Schema.String, "EDATraceId">;
|
|
631
|
+
readonly spanId: Schema.brand<Schema.String, "EDASpanId">;
|
|
632
|
+
readonly sampled: Schema.Boolean;
|
|
633
|
+
readonly tracestate: Schema.NullOr<Schema.String>;
|
|
634
|
+
}>;
|
|
635
|
+
readonly attributes: Schema.$Record<Schema.String, Schema.Union<readonly [Schema.String, Schema.Number, Schema.Boolean]>>;
|
|
636
|
+
}>>;
|
|
637
|
+
}>>;
|
|
638
|
+
readonly payload: Schema.Unknown;
|
|
639
|
+
}>;
|
|
640
|
+
}>;
|
|
641
|
+
}>;
|
|
642
|
+
type SessionCommandFailed = typeof SessionCommandFailed.Type;
|
|
643
|
+
/** Command outcome when command work was cancelled by stop, interrupt, or recovery. */
|
|
644
|
+
declare const SessionCommandCancelled: Schema.TaggedStruct<"SessionCommandCancelled", {
|
|
645
|
+
readonly committed: Schema.Struct<{
|
|
646
|
+
readonly position: Schema.Struct<{
|
|
647
|
+
readonly seq: Schema.brand<Schema.Int, "SequenceNumber">;
|
|
648
|
+
readonly subSeq: Schema.brand<Schema.Literal<0>, "SubSequenceNumber">;
|
|
649
|
+
}>;
|
|
650
|
+
readonly event: Schema.Struct<{
|
|
651
|
+
readonly durability: Schema.Literal<"durable">;
|
|
652
|
+
readonly namespace: Schema.NonEmptyString;
|
|
653
|
+
readonly type: Schema.NonEmptyString;
|
|
654
|
+
readonly schemaVersion: Schema.Int;
|
|
655
|
+
readonly eventId: Schema.brand<Schema.String, "EventId">;
|
|
656
|
+
readonly sessionId: Schema.brand<Schema.String, "SessionId">;
|
|
657
|
+
readonly correlationId: Schema.optionalKey<Schema.brand<Schema.String, "CorrelationId">>;
|
|
658
|
+
readonly createdAtMs: Schema.brand<Schema.Int, "UnixEpochMillis">;
|
|
659
|
+
readonly trace: Schema.withConstructorDefault<Schema.Struct<{
|
|
660
|
+
readonly span: Schema.Struct<{
|
|
661
|
+
readonly traceId: Schema.brand<Schema.String, "EDATraceId">;
|
|
662
|
+
readonly spanId: Schema.brand<Schema.String, "EDASpanId">;
|
|
663
|
+
readonly sampled: Schema.Boolean;
|
|
664
|
+
readonly tracestate: Schema.NullOr<Schema.String>;
|
|
665
|
+
}>;
|
|
666
|
+
readonly links: Schema.$Array<Schema.Struct<{
|
|
667
|
+
readonly context: Schema.Struct<{
|
|
668
|
+
readonly traceId: Schema.brand<Schema.String, "EDATraceId">;
|
|
669
|
+
readonly spanId: Schema.brand<Schema.String, "EDASpanId">;
|
|
670
|
+
readonly sampled: Schema.Boolean;
|
|
671
|
+
readonly tracestate: Schema.NullOr<Schema.String>;
|
|
672
|
+
}>;
|
|
673
|
+
readonly attributes: Schema.$Record<Schema.String, Schema.Union<readonly [Schema.String, Schema.Number, Schema.Boolean]>>;
|
|
674
|
+
}>>;
|
|
675
|
+
}>>;
|
|
676
|
+
readonly payload: Schema.Unknown;
|
|
677
|
+
}>;
|
|
678
|
+
}>;
|
|
679
|
+
}>;
|
|
680
|
+
type SessionCommandCancelled = typeof SessionCommandCancelled.Type;
|
|
570
681
|
/** Terminal command-processing variants returned from one control action. */
|
|
571
682
|
declare const SessionCommandOutcome: Schema.Union<readonly [Schema.TaggedStruct<"SessionCommandCompleted", {
|
|
572
683
|
readonly committed: Schema.Struct<{
|
|
@@ -680,6 +791,9 @@ interface SessionCommandResult {
|
|
|
680
791
|
readonly started: CommittedDurableEvent;
|
|
681
792
|
readonly outcome: SessionCommandOutcome;
|
|
682
793
|
}
|
|
794
|
+
/** Why a control drain stopped without starting more command work. */
|
|
795
|
+
declare const SessionNoRunnableReason: Schema.Literals<readonly ["active-command", "no-pending-command"]>;
|
|
796
|
+
type SessionNoRunnableReason = typeof SessionNoRunnableReason.Type;
|
|
683
797
|
/** Session control result when no admitted command is runnable. */
|
|
684
798
|
declare const SessionNoRunnableCommand: Schema.TaggedStruct<"SessionNoRunnableCommand", {
|
|
685
799
|
readonly reason: Schema.Literals<readonly ["active-command", "no-pending-command"]>;
|
|
@@ -740,30 +854,8 @@ interface SessionStateShape extends SessionEventSink {
|
|
|
740
854
|
declare const SessionState_base: Context.ServiceClass<SessionState, "@effect-durable-agent/SessionState", SessionStateShape>;
|
|
741
855
|
/** Internal authoritative live state and root control loop for one session. */
|
|
742
856
|
declare class SessionState extends SessionState_base {
|
|
743
|
-
static readonly Live: Layer.Layer<SessionState, EDASessionStoreError,
|
|
744
|
-
}
|
|
745
|
-
//#endregion
|
|
746
|
-
//#region src/services/session-query.d.ts
|
|
747
|
-
/** Authoritative live snapshot for one EDA session plus its derived durable transcript. */
|
|
748
|
-
interface EDASessionSnapshot {
|
|
749
|
-
readonly state: ReducedState;
|
|
750
|
-
readonly reducerStates: EDAReducerStateSnapshot;
|
|
751
|
-
readonly messages: ReadonlyArray<DurableTranscriptMessage>;
|
|
752
|
-
}
|
|
753
|
-
/** Read-only query facade over authoritative live state and reconnect-safe event streams. */
|
|
754
|
-
interface EDASessionQueryShape {
|
|
755
|
-
/** Read the authoritative live state and derive its durable transcript. */
|
|
756
|
-
readonly snapshot: () => Effect.Effect<EDASessionSnapshot, EDASessionStoreError>;
|
|
757
|
-
/** Read durable user/assistant messages in committed sequence order from the live snapshot. */
|
|
758
|
-
readonly messages: () => Effect.Effect<ReadonlyArray<DurableTranscriptMessage>, EDASessionStoreError>;
|
|
759
|
-
/** Backfill durable events after `afterSeq`, then follow live durable/ephemeral events. */
|
|
760
|
-
readonly eventsAfter: (afterSeq: SequenceNumber) => Effect.Effect<Stream.Stream<PositionedEvent, EDASessionStoreError>, EDASessionStoreError, Scope.Scope>;
|
|
761
|
-
}
|
|
762
|
-
declare const EDASessionQuery_base: Context.ServiceClass<EDASessionQuery, "@effect-durable-agent/EDASessionQuery", EDASessionQueryShape>;
|
|
763
|
-
/** Read-only session query facade backed by authoritative SessionState plus durable backfill. */
|
|
764
|
-
declare class EDASessionQuery extends EDASessionQuery_base {
|
|
765
|
-
static readonly Live: Layer.Layer<EDASessionQuery, never, SessionState | LiveEventBus | EDASessionStore>;
|
|
857
|
+
static readonly Live: Layer.Layer<SessionState, EDASessionStoreError, EDAPromptProjector | EDASessionStore | LiveEventBus | EDAKeepAlive | IdGenerator | EventFactory | TurnRunner | CompactionRunner | SessionContext | EDAReducerRegistry | EDASinkRegistry>;
|
|
766
858
|
}
|
|
767
859
|
//#endregion
|
|
768
|
-
export {
|
|
769
|
-
//# sourceMappingURL=session-
|
|
860
|
+
export { ToolExecutor as S, SessionStateShape as _, SessionCommandCompleted as a, InferenceRunner as b, SessionCommandResult as c, SessionForkedCommand as d, SessionNoRunnableCommand as f, SessionStateError as g, SessionState as h, SessionCommandCancelled as i, SessionDrainProcessed as l, SessionRunInput as m, SessionCommandAdmissionConflict as n, SessionCommandFailed as o, SessionNoRunnableReason as p, SessionCommandAdmissionError as r, SessionCommandOutcome as s, EDASubmittable as t, SessionDrainResult as u, SessionStateSnapshotData as v, InferenceRunnerStreamPart as x, TurnRunner as y };
|
|
861
|
+
//# sourceMappingURL=session-state-CjOSvWbe.d.ts.map
|