effect-durable-agent 0.1.0-alpha.3 → 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 +9 -0
- package/README.md +28 -27
- package/dist/{command-queues-CQ9cwRP7.d.ts → command-queues-CV7O-51r.d.ts} +7 -6
- package/dist/{compaction-1LSZR944.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 +1 -1
- package/dist/host/websocket-wire.js +2 -2
- 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-DimXJPOJ.d.ts → prompt-projector-C1IbkHr0.d.ts} +5 -5
- 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-DcdPOEuE.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 +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-hxfujxUH.d.ts → session-state-CjOSvWbe.d.ts} +134 -42
- package/dist/{session-query-Cbf-e0rG.js → session-state-SStf0KUo.js} +18 -77
- 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-BziFpxoE.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-D3tLrhe2.d.ts → websocket-wire-QD2SquQE.d.ts} +74 -2
- package/docs/message-steering.md +1 -1
- package/docs/releasing.md +42 -24
- package/docs/spec.md +11 -11
- package/docs/websocket-protocol.md +4 -4
- package/package.json +55 -35
- package/dist/durable-C3Rz7kg3.js.map +0 -1
- package/dist/durable-object-runtime-CiMKxGs6.d.ts +0 -257
- package/dist/durable-object-runtime-wKiqaAt2.js +0 -1494
- package/dist/durable-object-runtime-wKiqaAt2.js.map +0 -1
- 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-Cbf-e0rG.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/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
|
@@ -1,305 +0,0 @@
|
|
|
1
|
-
import * as Schema from "effect/Schema";
|
|
2
|
-
|
|
3
|
-
import type { CommittedDurableEvent } from "../../src/services/session-store";
|
|
4
|
-
import { EDAReducer } from "../../src/services/reducer-registry";
|
|
5
|
-
import { ToolCallId } from "../../src/types/core";
|
|
6
|
-
import {
|
|
7
|
-
ToolCallCompletedPayload,
|
|
8
|
-
ToolCallCreatedPayload,
|
|
9
|
-
toolCallCompletedEventType,
|
|
10
|
-
toolCallCreatedEventType,
|
|
11
|
-
} from "../../src/types/events";
|
|
12
|
-
import {
|
|
13
|
-
ApprovalGrantedPayload,
|
|
14
|
-
ApprovalRequestedPayload,
|
|
15
|
-
PreviewReadyPayload,
|
|
16
|
-
SandboxCommandCompletedPayload,
|
|
17
|
-
SandboxCommandId,
|
|
18
|
-
SandboxCommandStartedPayload,
|
|
19
|
-
SandboxId,
|
|
20
|
-
SandboxStartedPayload,
|
|
21
|
-
SandboxStartingPayload,
|
|
22
|
-
SandboxStoppedPayload,
|
|
23
|
-
approvalGrantedEventType,
|
|
24
|
-
approvalRequestedEventType,
|
|
25
|
-
previewReadyEventType,
|
|
26
|
-
sandboxCommandCompletedEventType,
|
|
27
|
-
sandboxCommandStartedEventType,
|
|
28
|
-
sandboxLifecycleNamespace,
|
|
29
|
-
sandboxStartedEventType,
|
|
30
|
-
sandboxStartingEventType,
|
|
31
|
-
sandboxStoppedEventType,
|
|
32
|
-
} from "./events";
|
|
33
|
-
|
|
34
|
-
export interface SandboxToolCard {
|
|
35
|
-
readonly toolCallId: ToolCallId;
|
|
36
|
-
readonly toolName?: string;
|
|
37
|
-
readonly status: "created" | "completed";
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export interface SandboxCommandState {
|
|
41
|
-
readonly commandId: SandboxCommandId;
|
|
42
|
-
readonly command: string;
|
|
43
|
-
readonly wallClockTimeMs?: number;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export interface SandboxState {
|
|
47
|
-
readonly sandboxId: SandboxId;
|
|
48
|
-
readonly toolCallId: ToolCallId;
|
|
49
|
-
readonly status: "starting" | "running" | "stopped";
|
|
50
|
-
readonly runningCommand?: SandboxCommandState;
|
|
51
|
-
readonly lastCommand?: SandboxCommandState;
|
|
52
|
-
readonly previewUrl?: string;
|
|
53
|
-
readonly approval?:
|
|
54
|
-
| { readonly _tag: "Requested"; readonly action: string }
|
|
55
|
-
| { readonly _tag: "Granted"; readonly action: string; readonly approverId: string };
|
|
56
|
-
readonly stopReason?: string;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export interface SandboxLifecycleState {
|
|
60
|
-
readonly activeSandboxId?: SandboxId;
|
|
61
|
-
readonly sandboxIdByToolCallId: ReadonlyMap<ToolCallId, SandboxId>;
|
|
62
|
-
readonly startingToolCalls: ReadonlySet<ToolCallId>;
|
|
63
|
-
readonly toolCards: ReadonlyMap<ToolCallId, SandboxToolCard>;
|
|
64
|
-
readonly sandboxes: ReadonlyMap<SandboxId, SandboxState>;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export const initialSandboxLifecycleState: SandboxLifecycleState = {
|
|
68
|
-
activeSandboxId: undefined,
|
|
69
|
-
sandboxIdByToolCallId: new Map(),
|
|
70
|
-
startingToolCalls: new Set(),
|
|
71
|
-
toolCards: new Map(),
|
|
72
|
-
sandboxes: new Map(),
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
const SandboxCommandStateSchema = Schema.Struct({
|
|
76
|
-
commandId: SandboxCommandId,
|
|
77
|
-
command: Schema.String,
|
|
78
|
-
wallClockTimeMs: Schema.optionalKey(Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))),
|
|
79
|
-
});
|
|
80
|
-
const SandboxToolCardSchema = Schema.Struct({
|
|
81
|
-
toolCallId: ToolCallId,
|
|
82
|
-
toolName: Schema.optionalKey(Schema.String),
|
|
83
|
-
status: Schema.Literals(["created", "completed"]),
|
|
84
|
-
});
|
|
85
|
-
const SandboxStateSchema = Schema.Struct({
|
|
86
|
-
sandboxId: SandboxId,
|
|
87
|
-
toolCallId: ToolCallId,
|
|
88
|
-
status: Schema.Literals(["starting", "running", "stopped"]),
|
|
89
|
-
runningCommand: Schema.optionalKey(SandboxCommandStateSchema),
|
|
90
|
-
lastCommand: Schema.optionalKey(SandboxCommandStateSchema),
|
|
91
|
-
previewUrl: Schema.optionalKey(Schema.String),
|
|
92
|
-
approval: Schema.optionalKey(
|
|
93
|
-
Schema.Union([
|
|
94
|
-
Schema.Struct({ _tag: Schema.Literal("Requested"), action: Schema.String }),
|
|
95
|
-
Schema.Struct({
|
|
96
|
-
_tag: Schema.Literal("Granted"),
|
|
97
|
-
action: Schema.String,
|
|
98
|
-
approverId: Schema.String,
|
|
99
|
-
}),
|
|
100
|
-
]),
|
|
101
|
-
),
|
|
102
|
-
stopReason: Schema.optionalKey(Schema.String),
|
|
103
|
-
});
|
|
104
|
-
const SandboxLifecycleStateSchema = Schema.Struct({
|
|
105
|
-
activeSandboxId: Schema.optionalKey(SandboxId),
|
|
106
|
-
sandboxIdByToolCallId: Schema.ReadonlyMap(ToolCallId, SandboxId),
|
|
107
|
-
startingToolCalls: Schema.ReadonlySet(ToolCallId),
|
|
108
|
-
toolCards: Schema.ReadonlyMap(ToolCallId, SandboxToolCardSchema),
|
|
109
|
-
sandboxes: Schema.ReadonlyMap(SandboxId, SandboxStateSchema),
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
/** Pure UI/application projection over framework tool events and app sandbox events. */
|
|
113
|
-
export const SandboxLifecycleReducer = EDAReducer.make<SandboxLifecycleState>({
|
|
114
|
-
name: "example.sandbox",
|
|
115
|
-
initial: initialSandboxLifecycleState,
|
|
116
|
-
stateSchema: SandboxLifecycleStateSchema,
|
|
117
|
-
reduce: (state, entry) => reduceSandboxLifecycleState(state, entry),
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
export const reduceSandboxLifecycleState = (
|
|
121
|
-
state: SandboxLifecycleState,
|
|
122
|
-
entry: CommittedDurableEvent,
|
|
123
|
-
): SandboxLifecycleState => {
|
|
124
|
-
const event = entry.event;
|
|
125
|
-
|
|
126
|
-
if (event.type === toolCallCreatedEventType && Schema.is(ToolCallCreatedPayload)(event.payload)) {
|
|
127
|
-
return rememberToolCallCreated(state, event.payload);
|
|
128
|
-
}
|
|
129
|
-
if (
|
|
130
|
-
event.type === toolCallCompletedEventType &&
|
|
131
|
-
Schema.is(ToolCallCompletedPayload)(event.payload)
|
|
132
|
-
) {
|
|
133
|
-
return rememberToolCallCompleted(state, event.payload);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
if (event.namespace !== sandboxLifecycleNamespace) {
|
|
137
|
-
return state;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
if (event.type === sandboxStartingEventType && Schema.is(SandboxStartingPayload)(event.payload)) {
|
|
141
|
-
return rememberSandboxStarting(state, event.payload);
|
|
142
|
-
}
|
|
143
|
-
if (event.type === sandboxStartedEventType && Schema.is(SandboxStartedPayload)(event.payload)) {
|
|
144
|
-
return rememberSandboxStarted(state, event.payload);
|
|
145
|
-
}
|
|
146
|
-
if (
|
|
147
|
-
event.type === sandboxCommandStartedEventType &&
|
|
148
|
-
Schema.is(SandboxCommandStartedPayload)(event.payload)
|
|
149
|
-
) {
|
|
150
|
-
return rememberCommandStarted(state, event.payload);
|
|
151
|
-
}
|
|
152
|
-
if (
|
|
153
|
-
event.type === sandboxCommandCompletedEventType &&
|
|
154
|
-
Schema.is(SandboxCommandCompletedPayload)(event.payload)
|
|
155
|
-
) {
|
|
156
|
-
return rememberCommandCompleted(state, event.payload);
|
|
157
|
-
}
|
|
158
|
-
if (event.type === previewReadyEventType && Schema.is(PreviewReadyPayload)(event.payload)) {
|
|
159
|
-
const payload = event.payload;
|
|
160
|
-
return updateSandbox(state, payload.sandboxId, (sandbox) => ({
|
|
161
|
-
...sandbox,
|
|
162
|
-
previewUrl: payload.url,
|
|
163
|
-
}));
|
|
164
|
-
}
|
|
165
|
-
if (
|
|
166
|
-
event.type === approvalRequestedEventType &&
|
|
167
|
-
Schema.is(ApprovalRequestedPayload)(event.payload)
|
|
168
|
-
) {
|
|
169
|
-
const payload = event.payload;
|
|
170
|
-
return updateSandbox(state, payload.sandboxId, (sandbox) => ({
|
|
171
|
-
...sandbox,
|
|
172
|
-
approval: { _tag: "Requested", action: payload.action },
|
|
173
|
-
}));
|
|
174
|
-
}
|
|
175
|
-
if (event.type === approvalGrantedEventType && Schema.is(ApprovalGrantedPayload)(event.payload)) {
|
|
176
|
-
const payload = event.payload;
|
|
177
|
-
return updateSandbox(state, payload.sandboxId, (sandbox) => ({
|
|
178
|
-
...sandbox,
|
|
179
|
-
approval: {
|
|
180
|
-
_tag: "Granted",
|
|
181
|
-
action: payload.action,
|
|
182
|
-
approverId: payload.approverId,
|
|
183
|
-
},
|
|
184
|
-
}));
|
|
185
|
-
}
|
|
186
|
-
if (event.type === sandboxStoppedEventType && Schema.is(SandboxStoppedPayload)(event.payload)) {
|
|
187
|
-
const payload = event.payload;
|
|
188
|
-
return updateSandbox(state, payload.sandboxId, (sandbox) => ({
|
|
189
|
-
...sandbox,
|
|
190
|
-
status: "stopped",
|
|
191
|
-
stopReason: payload.reason,
|
|
192
|
-
}));
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
return state;
|
|
196
|
-
};
|
|
197
|
-
|
|
198
|
-
const rememberToolCallCreated = (
|
|
199
|
-
state: SandboxLifecycleState,
|
|
200
|
-
payload: ToolCallCreatedPayload,
|
|
201
|
-
): SandboxLifecycleState => ({
|
|
202
|
-
...state,
|
|
203
|
-
toolCards: new Map(state.toolCards).set(payload.toolCallId, {
|
|
204
|
-
toolCallId: payload.toolCallId,
|
|
205
|
-
toolName: payload.promptPart.name,
|
|
206
|
-
status: "created",
|
|
207
|
-
}),
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
const rememberToolCallCompleted = (
|
|
211
|
-
state: SandboxLifecycleState,
|
|
212
|
-
payload: ToolCallCompletedPayload,
|
|
213
|
-
): SandboxLifecycleState => {
|
|
214
|
-
const existing = state.toolCards.get(payload.toolCallId);
|
|
215
|
-
return {
|
|
216
|
-
...state,
|
|
217
|
-
toolCards: new Map(state.toolCards).set(payload.toolCallId, {
|
|
218
|
-
toolCallId: payload.toolCallId,
|
|
219
|
-
toolName: existing?.toolName,
|
|
220
|
-
status: "completed",
|
|
221
|
-
}),
|
|
222
|
-
};
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
const rememberSandboxStarting = (
|
|
226
|
-
state: SandboxLifecycleState,
|
|
227
|
-
payload: SandboxStartingPayload,
|
|
228
|
-
): SandboxLifecycleState => ({
|
|
229
|
-
...state,
|
|
230
|
-
startingToolCalls: new Set(state.startingToolCalls).add(payload.toolCallId),
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
const rememberSandboxStarted = (
|
|
234
|
-
state: SandboxLifecycleState,
|
|
235
|
-
payload: SandboxStartedPayload,
|
|
236
|
-
): SandboxLifecycleState => ({
|
|
237
|
-
...state,
|
|
238
|
-
activeSandboxId: payload.sandboxId,
|
|
239
|
-
sandboxIdByToolCallId: new Map(state.sandboxIdByToolCallId).set(
|
|
240
|
-
payload.toolCallId,
|
|
241
|
-
payload.sandboxId,
|
|
242
|
-
),
|
|
243
|
-
startingToolCalls: without(state.startingToolCalls, payload.toolCallId),
|
|
244
|
-
sandboxes: new Map(state.sandboxes).set(payload.sandboxId, {
|
|
245
|
-
sandboxId: payload.sandboxId,
|
|
246
|
-
toolCallId: payload.toolCallId,
|
|
247
|
-
status: "running",
|
|
248
|
-
}),
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
const rememberCommandStarted = (
|
|
252
|
-
state: SandboxLifecycleState,
|
|
253
|
-
payload: SandboxCommandStartedPayload,
|
|
254
|
-
): SandboxLifecycleState =>
|
|
255
|
-
updateSandbox(state, payload.sandboxId, (sandbox) => ({
|
|
256
|
-
...sandbox,
|
|
257
|
-
runningCommand: {
|
|
258
|
-
commandId: payload.commandId,
|
|
259
|
-
command: payload.command,
|
|
260
|
-
},
|
|
261
|
-
}));
|
|
262
|
-
|
|
263
|
-
const rememberCommandCompleted = (
|
|
264
|
-
state: SandboxLifecycleState,
|
|
265
|
-
payload: SandboxCommandCompletedPayload,
|
|
266
|
-
): SandboxLifecycleState =>
|
|
267
|
-
updateSandbox(state, payload.sandboxId, (sandbox) => {
|
|
268
|
-
const completed =
|
|
269
|
-
sandbox.runningCommand?.commandId === payload.commandId
|
|
270
|
-
? {
|
|
271
|
-
...sandbox.runningCommand,
|
|
272
|
-
wallClockTimeMs: payload.wallClockTimeMs,
|
|
273
|
-
}
|
|
274
|
-
: {
|
|
275
|
-
commandId: payload.commandId,
|
|
276
|
-
command: "(command text was not in this replay window)",
|
|
277
|
-
wallClockTimeMs: payload.wallClockTimeMs,
|
|
278
|
-
};
|
|
279
|
-
return {
|
|
280
|
-
...sandbox,
|
|
281
|
-
runningCommand: undefined,
|
|
282
|
-
lastCommand: completed,
|
|
283
|
-
};
|
|
284
|
-
});
|
|
285
|
-
|
|
286
|
-
const updateSandbox = (
|
|
287
|
-
state: SandboxLifecycleState,
|
|
288
|
-
sandboxId: SandboxId,
|
|
289
|
-
update: (sandbox: SandboxState) => SandboxState,
|
|
290
|
-
): SandboxLifecycleState => {
|
|
291
|
-
const sandbox = state.sandboxes.get(sandboxId);
|
|
292
|
-
if (sandbox === undefined) {
|
|
293
|
-
return state;
|
|
294
|
-
}
|
|
295
|
-
return {
|
|
296
|
-
...state,
|
|
297
|
-
sandboxes: new Map(state.sandboxes).set(sandboxId, update(sandbox)),
|
|
298
|
-
};
|
|
299
|
-
};
|
|
300
|
-
|
|
301
|
-
const without = <Value>(set: ReadonlySet<Value>, value: Value): ReadonlySet<Value> => {
|
|
302
|
-
const next = new Set(set);
|
|
303
|
-
next.delete(value);
|
|
304
|
-
return next;
|
|
305
|
-
};
|
|
@@ -1,202 +0,0 @@
|
|
|
1
|
-
import * as Prompt from "effect/unstable/ai/Prompt";
|
|
2
|
-
import { describe, expect, it } from "vite-plus/test";
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
InferenceId,
|
|
6
|
-
EventId,
|
|
7
|
-
RunId,
|
|
8
|
-
SequenceNumber,
|
|
9
|
-
SessionId,
|
|
10
|
-
ToolCallId,
|
|
11
|
-
TurnId,
|
|
12
|
-
durablePosition,
|
|
13
|
-
} from "../../src/types/core";
|
|
14
|
-
import {
|
|
15
|
-
DurableEventEnvelope,
|
|
16
|
-
EventType,
|
|
17
|
-
ToolCallCompletedPayload,
|
|
18
|
-
ToolCallCreatedPayload,
|
|
19
|
-
UnixEpochMillis,
|
|
20
|
-
effectDurableAgentNamespace,
|
|
21
|
-
schemaV1,
|
|
22
|
-
toolCallCompletedEventType,
|
|
23
|
-
toolCallCreatedEventType,
|
|
24
|
-
} from "../../src/types/events";
|
|
25
|
-
import { sequentialUuidV7 } from "../../src/services/id-generator";
|
|
26
|
-
import { CommittedDurableEvent } from "../../src/services/session-store";
|
|
27
|
-
import {
|
|
28
|
-
ApprovalAction,
|
|
29
|
-
ApproverId,
|
|
30
|
-
PreviewUrl,
|
|
31
|
-
SandboxCommandId,
|
|
32
|
-
SandboxEvents,
|
|
33
|
-
SandboxId,
|
|
34
|
-
} from "./events";
|
|
35
|
-
import { initialSandboxLifecycleState, reduceSandboxLifecycleState } from "./reducer";
|
|
36
|
-
|
|
37
|
-
const SESSION_ID = SessionId.make("018f6bd5-2f2a-7b1e-8f1a-1f2e3d4c5b6a");
|
|
38
|
-
const RUN_ID = RunId.make(sequentialUuidV7(31));
|
|
39
|
-
const TURN_ID = TurnId.make(sequentialUuidV7(32));
|
|
40
|
-
const INFERENCE_ID = InferenceId.make(sequentialUuidV7(33));
|
|
41
|
-
const TOOL_CALL_ID = ToolCallId.make(sequentialUuidV7(34));
|
|
42
|
-
const SANDBOX_ID = SandboxId.make("sbx_123");
|
|
43
|
-
const COMMAND_ID = SandboxCommandId.make("cmd_test_1");
|
|
44
|
-
const ACTION = ApprovalAction.make("deploy");
|
|
45
|
-
|
|
46
|
-
describe("sandbox lifecycle example", () => {
|
|
47
|
-
it("derives one UI model from framework tool events and app lifecycle events", () => {
|
|
48
|
-
const state = sandboxScenarioEvents().reduce(
|
|
49
|
-
reduceSandboxLifecycleState,
|
|
50
|
-
initialSandboxLifecycleState,
|
|
51
|
-
);
|
|
52
|
-
const sandbox = state.sandboxes.get(SANDBOX_ID);
|
|
53
|
-
const tool = state.toolCards.get(TOOL_CALL_ID);
|
|
54
|
-
|
|
55
|
-
expect(tool).toMatchObject({ status: "completed", toolName: "runSandboxTask" });
|
|
56
|
-
expect(state.activeSandboxId).toBe(SANDBOX_ID);
|
|
57
|
-
expect(sandbox?.status).toBe("stopped");
|
|
58
|
-
expect(sandbox?.lastCommand).toMatchObject({
|
|
59
|
-
command: "pnpm test",
|
|
60
|
-
wallClockTimeMs: 1412,
|
|
61
|
-
});
|
|
62
|
-
expect(sandbox?.previewUrl).toBe("https://preview.example.dev");
|
|
63
|
-
expect(sandbox?.approval).toEqual({
|
|
64
|
-
_tag: "Granted",
|
|
65
|
-
action: "deploy",
|
|
66
|
-
approverId: "user_456",
|
|
67
|
-
});
|
|
68
|
-
expect(sandbox?.stopReason).toBe("idle");
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it("supports SSR handoff by replaying only events after the server cursor", () => {
|
|
72
|
-
const events = sandboxScenarioEvents();
|
|
73
|
-
const serverCursor = 6;
|
|
74
|
-
const serverState = events
|
|
75
|
-
.slice(0, serverCursor)
|
|
76
|
-
.reduce(reduceSandboxLifecycleState, initialSandboxLifecycleState);
|
|
77
|
-
const clientState = events.slice(serverCursor).reduce(reduceSandboxLifecycleState, serverState);
|
|
78
|
-
const fullReplayState = events.reduce(
|
|
79
|
-
reduceSandboxLifecycleState,
|
|
80
|
-
initialSandboxLifecycleState,
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
expect(clientState).toEqual(fullReplayState);
|
|
84
|
-
expect(clientState.sandboxes.get(SANDBOX_ID)?.previewUrl).toBe("https://preview.example.dev");
|
|
85
|
-
expect(clientState.sandboxes.get(SANDBOX_ID)?.lastCommand?.wallClockTimeMs).toBe(1412);
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
const sandboxScenarioEvents = (): ReadonlyArray<CommittedDurableEvent> => [
|
|
90
|
-
frameworkEvent(
|
|
91
|
-
1,
|
|
92
|
-
toolCallCreatedEventType,
|
|
93
|
-
ToolCallCreatedPayload.make({
|
|
94
|
-
runId: RUN_ID,
|
|
95
|
-
turnId: TURN_ID,
|
|
96
|
-
inferenceId: INFERENCE_ID,
|
|
97
|
-
toolCallId: TOOL_CALL_ID,
|
|
98
|
-
promptPart: Prompt.toolCallPart({
|
|
99
|
-
id: "tool-call-1",
|
|
100
|
-
name: "runSandboxTask",
|
|
101
|
-
params: { task: "run tests and prepare preview" },
|
|
102
|
-
providerExecuted: false,
|
|
103
|
-
}),
|
|
104
|
-
}),
|
|
105
|
-
),
|
|
106
|
-
committed(2, SandboxEvents.starting({ toolCallId: TOOL_CALL_ID, ...context(2) })),
|
|
107
|
-
committed(
|
|
108
|
-
3,
|
|
109
|
-
SandboxEvents.started({ toolCallId: TOOL_CALL_ID, sandboxId: SANDBOX_ID, ...context(3) }),
|
|
110
|
-
),
|
|
111
|
-
committed(
|
|
112
|
-
4,
|
|
113
|
-
SandboxEvents.commandStarted({
|
|
114
|
-
sandboxId: SANDBOX_ID,
|
|
115
|
-
commandId: COMMAND_ID,
|
|
116
|
-
command: "pnpm test",
|
|
117
|
-
...context(4),
|
|
118
|
-
}),
|
|
119
|
-
),
|
|
120
|
-
committed(
|
|
121
|
-
5,
|
|
122
|
-
SandboxEvents.commandCompleted({
|
|
123
|
-
sandboxId: SANDBOX_ID,
|
|
124
|
-
commandId: COMMAND_ID,
|
|
125
|
-
wallClockTimeMs: 1412,
|
|
126
|
-
...context(5),
|
|
127
|
-
}),
|
|
128
|
-
),
|
|
129
|
-
committed(
|
|
130
|
-
6,
|
|
131
|
-
SandboxEvents.previewReady({
|
|
132
|
-
sandboxId: SANDBOX_ID,
|
|
133
|
-
url: PreviewUrl.make("https://preview.example.dev"),
|
|
134
|
-
...context(6),
|
|
135
|
-
}),
|
|
136
|
-
),
|
|
137
|
-
committed(
|
|
138
|
-
7,
|
|
139
|
-
SandboxEvents.approvalRequested({
|
|
140
|
-
sandboxId: SANDBOX_ID,
|
|
141
|
-
action: ACTION,
|
|
142
|
-
...context(7),
|
|
143
|
-
}),
|
|
144
|
-
),
|
|
145
|
-
committed(
|
|
146
|
-
8,
|
|
147
|
-
SandboxEvents.approvalGranted({
|
|
148
|
-
sandboxId: SANDBOX_ID,
|
|
149
|
-
action: ACTION,
|
|
150
|
-
approverId: ApproverId.make("user_456"),
|
|
151
|
-
...context(8),
|
|
152
|
-
}),
|
|
153
|
-
),
|
|
154
|
-
frameworkEvent(
|
|
155
|
-
9,
|
|
156
|
-
toolCallCompletedEventType,
|
|
157
|
-
ToolCallCompletedPayload.make({
|
|
158
|
-
toolCallId: TOOL_CALL_ID,
|
|
159
|
-
promptPart: Prompt.toolResultPart({
|
|
160
|
-
id: "tool-call-1",
|
|
161
|
-
name: "runSandboxTask",
|
|
162
|
-
isFailure: false,
|
|
163
|
-
result: { ok: true },
|
|
164
|
-
}),
|
|
165
|
-
}),
|
|
166
|
-
),
|
|
167
|
-
committed(
|
|
168
|
-
10,
|
|
169
|
-
SandboxEvents.stopped({
|
|
170
|
-
sandboxId: SANDBOX_ID,
|
|
171
|
-
reason: "idle",
|
|
172
|
-
...context(10),
|
|
173
|
-
}),
|
|
174
|
-
),
|
|
175
|
-
];
|
|
176
|
-
|
|
177
|
-
const frameworkEvent = (seq: number, type: EventType, payload: unknown): CommittedDurableEvent =>
|
|
178
|
-
committed(
|
|
179
|
-
seq,
|
|
180
|
-
DurableEventEnvelope.make({
|
|
181
|
-
namespace: effectDurableAgentNamespace,
|
|
182
|
-
type,
|
|
183
|
-
schemaVersion: schemaV1,
|
|
184
|
-
durability: "durable",
|
|
185
|
-
eventId: EventId.make(sequentialUuidV7(300 + seq)),
|
|
186
|
-
sessionId: SESSION_ID,
|
|
187
|
-
createdAtMs: UnixEpochMillis.make(1_715_000_000_000 + seq),
|
|
188
|
-
payload,
|
|
189
|
-
}),
|
|
190
|
-
);
|
|
191
|
-
|
|
192
|
-
const committed = (seq: number, event: DurableEventEnvelope): CommittedDurableEvent =>
|
|
193
|
-
CommittedDurableEvent.make({
|
|
194
|
-
position: durablePosition(SequenceNumber.make(seq)),
|
|
195
|
-
event,
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
const context = (seq: number) => ({
|
|
199
|
-
createdAtMs: UnixEpochMillis.make(1_715_000_000_000 + seq),
|
|
200
|
-
eventId: EventId.make(sequentialUuidV7(400 + seq)),
|
|
201
|
-
sessionId: SESSION_ID,
|
|
202
|
-
});
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import * as Effect from "effect/Effect";
|
|
2
|
-
|
|
3
|
-
import { IdGenerator } from "../../src/services/id-generator";
|
|
4
|
-
import { EventId } from "../../src/types/core";
|
|
5
|
-
|
|
6
|
-
export const mintExampleEventId = (): Promise<EventId> =>
|
|
7
|
-
Effect.runPromise(
|
|
8
|
-
Effect.gen(function* () {
|
|
9
|
-
const ids = yield* IdGenerator;
|
|
10
|
-
return yield* ids.makeEventId();
|
|
11
|
-
}).pipe(Effect.provide(IdGenerator.Live)),
|
|
12
|
-
);
|
package/examples/_shared/http.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
export const json = (body: unknown, status = 200): Response =>
|
|
2
|
-
Response.json(body, {
|
|
3
|
-
headers: { "Cache-Control": "no-store" },
|
|
4
|
-
status,
|
|
5
|
-
});
|
|
6
|
-
|
|
7
|
-
export const parseJsonObject = async (request: Request): Promise<Record<string, unknown>> => {
|
|
8
|
-
const body = await request.json().catch(() => undefined);
|
|
9
|
-
return typeof body === "object" && body !== null && !Array.isArray(body)
|
|
10
|
-
? (body as Record<string, unknown>)
|
|
11
|
-
: {};
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export const requiredString = (body: Record<string, unknown>, key: string): string | undefined => {
|
|
15
|
-
const value = body[key];
|
|
16
|
-
return typeof value === "string" && value.trim().length > 0 ? value.trim() : undefined;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export const optionalString = (value: string | undefined): string | undefined => {
|
|
20
|
-
const trimmed = value?.trim();
|
|
21
|
-
return trimmed === undefined || trimmed.length === 0 ? undefined : trimmed;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export const pathParam = (pathname: string, pattern: RegExp): string | null => {
|
|
25
|
-
const match = pattern.exec(pathname);
|
|
26
|
-
return match === null ? null : decodeURIComponent(match[1]!);
|
|
27
|
-
};
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
edaRuntimeConfig,
|
|
3
|
-
type EDASessionDurableObjectOptions,
|
|
4
|
-
} from "../../src/host/durable-object";
|
|
5
|
-
import { makeEDADurableObjectOpenAiModelLayer } from "../../src/host/durable-object-runtime";
|
|
6
|
-
import { optionalString } from "./http";
|
|
7
|
-
|
|
8
|
-
export const EXAMPLE_DEFAULT_MODEL_ID = "gpt-4.1-mini";
|
|
9
|
-
|
|
10
|
-
export interface ExampleOpenAiEnv {
|
|
11
|
-
readonly EDA_OPENAI_API_URL?: string;
|
|
12
|
-
readonly EDA_OPENAI_MODEL?: string;
|
|
13
|
-
readonly EDA_SYSTEM_PROMPT?: string;
|
|
14
|
-
readonly OPENAI_API_KEY?: string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export const makeExampleOpenAiOptions = (
|
|
18
|
-
env: ExampleOpenAiEnv,
|
|
19
|
-
): Pick<EDASessionDurableObjectOptions, "config" | "modelLayer"> => {
|
|
20
|
-
const modelId = optionalString(env.EDA_OPENAI_MODEL) ?? EXAMPLE_DEFAULT_MODEL_ID;
|
|
21
|
-
return {
|
|
22
|
-
config: edaRuntimeConfig({
|
|
23
|
-
modelId,
|
|
24
|
-
provider: "openai",
|
|
25
|
-
systemPrompt: optionalString(env.EDA_SYSTEM_PROMPT),
|
|
26
|
-
}),
|
|
27
|
-
modelLayer: makeEDADurableObjectOpenAiModelLayer({
|
|
28
|
-
apiKey: optionalString(env.OPENAI_API_KEY),
|
|
29
|
-
apiUrl: optionalString(env.EDA_OPENAI_API_URL),
|
|
30
|
-
modelId,
|
|
31
|
-
}),
|
|
32
|
-
};
|
|
33
|
-
};
|