effect-durable-agent 0.1.0-alpha.3 → 0.1.0-alpha.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (221) hide show
  1. package/README.md +43 -41
  2. package/dist/assert-never-CoyUpoRB.js +18 -0
  3. package/dist/assert-never-CoyUpoRB.js.map +1 -0
  4. package/dist/{commands-Ce1ec6kH.d.ts → commands-3OD-p8dD.d.ts} +16 -9
  5. package/dist/compaction-63iEDHHl.js +523 -0
  6. package/dist/compaction-63iEDHHl.js.map +1 -0
  7. package/dist/context-projection-D_uRtK3r.d.ts +139 -0
  8. package/dist/{core-Hv3cFydL.d.ts → core-BcimKMIv.d.ts} +6 -4
  9. package/dist/delivery-2ZILvoRD.js +507 -0
  10. package/dist/delivery-2ZILvoRD.js.map +1 -0
  11. package/dist/{websocket-wire-D3tLrhe2.d.ts → delivery-RjM1_NAE.d.ts} +1019 -200
  12. package/dist/domain/command-queues.d.ts +81 -2
  13. package/dist/domain/command-queues.js +1 -1
  14. package/dist/domain/command-queues.js.map +1 -1
  15. package/dist/domain/context-projection.d.ts +2 -0
  16. package/dist/domain/context-projection.js +32 -0
  17. package/dist/domain/context-projection.js.map +1 -0
  18. package/dist/domain/message-transcript.d.ts +36 -2
  19. package/dist/domain/message-transcript.js +10 -2
  20. package/dist/domain/message-transcript.js.map +1 -1
  21. package/dist/domain/model-usage.d.ts +2 -0
  22. package/dist/domain/model-usage.js +59 -0
  23. package/dist/domain/model-usage.js.map +1 -0
  24. package/dist/domain/reduced-state-schema.d.ts +3 -996
  25. package/dist/domain/reduced-state-schema.js +121 -12
  26. package/dist/domain/reduced-state-schema.js.map +1 -1
  27. package/dist/domain/reduced-state.d.ts +3 -2
  28. package/dist/domain/reduced-state.js +937 -2
  29. package/dist/domain/reduced-state.js.map +1 -0
  30. package/dist/domain/run-scheduling.d.ts +2 -0
  31. package/dist/domain/run-scheduling.js +127 -0
  32. package/dist/domain/run-scheduling.js.map +1 -0
  33. package/dist/{durable-DlScwmCy.d.ts → durable-Cqv5ZRFA.d.ts} +8505 -2497
  34. package/dist/{durable-C3Rz7kg3.js → durable-D13w1Qr3.js} +253 -91
  35. package/dist/durable-D13w1Qr3.js.map +1 -0
  36. package/dist/envelope-BCzKUq_z.js +91 -0
  37. package/dist/envelope-BCzKUq_z.js.map +1 -0
  38. package/dist/envelope-Che6KatU.d.ts +254 -0
  39. package/dist/{keep-alive-p8dIcSVy.js → event-factory-_vF1bIJ7.js} +8 -10
  40. package/dist/event-factory-_vF1bIJ7.js.map +1 -0
  41. package/dist/{events-Bb9-Cu-7.d.ts → events-Dg0o5wWs.d.ts} +140 -324
  42. package/dist/{id-generator-C2Q15uZM.d.ts → id-generator-BKGoKjt-.d.ts} +3 -3
  43. package/dist/in-memory-CeODnZhE.js +150 -0
  44. package/dist/in-memory-CeODnZhE.js.map +1 -0
  45. package/dist/index.d.ts +8 -7
  46. package/dist/index.js +6 -5
  47. package/dist/interpreter-DKGJ1sSx.js +1452 -0
  48. package/dist/interpreter-DKGJ1sSx.js.map +1 -0
  49. package/dist/keep-alive-_zFCUvt3.d.ts +28 -0
  50. package/dist/model-resolver-D0vcDZsU.d.ts +24 -0
  51. package/dist/model-usage-0GeHwaaU.d.ts +54 -0
  52. package/dist/records--54mZEBx.js +675 -0
  53. package/dist/records--54mZEBx.js.map +1 -0
  54. package/dist/reduced-state-DU2ECOUp.d.ts +47821 -0
  55. package/dist/reduced-state-schema-B2t3cobf.d.ts +2187 -0
  56. package/dist/{reducer-registry-C0svUa63.d.ts → reducer-registry-JLU1yptN.d.ts} +24 -8
  57. package/dist/run-scheduling-wakeup-D_bGdicf.d.ts +24 -0
  58. package/dist/services/compaction.d.ts +2 -2
  59. package/dist/services/compaction.js +2 -426
  60. package/dist/services/id-generator.d.ts +1 -1
  61. package/dist/services/id-generator.js +4 -2
  62. package/dist/services/id-generator.js.map +1 -1
  63. package/dist/services/keep-alive.d.ts +2 -0
  64. package/dist/services/keep-alive.js +13 -0
  65. package/dist/services/keep-alive.js.map +1 -0
  66. package/dist/services/model-call.d.ts +15 -0
  67. package/dist/services/model-call.js +15 -0
  68. package/dist/services/model-call.js.map +1 -0
  69. package/dist/services/model-resolver.d.ts +2 -0
  70. package/dist/services/model-resolver.js +17 -0
  71. package/dist/services/model-resolver.js.map +1 -0
  72. package/dist/services/prompt-projector.d.ts +2 -2
  73. package/dist/services/prompt-projector.js +49 -5
  74. package/dist/services/prompt-projector.js.map +1 -1
  75. package/dist/services/reducer-registry.d.ts +2 -2
  76. package/dist/services/reducer-registry.js +21 -4
  77. package/dist/services/reducer-registry.js.map +1 -1
  78. package/dist/services/run-scheduler.d.ts +2 -0
  79. package/dist/services/run-scheduler.js +25 -0
  80. package/dist/services/run-scheduler.js.map +1 -0
  81. package/dist/services/run-scheduling-wakeup.d.ts +2 -0
  82. package/dist/services/run-scheduling-wakeup.js +13 -0
  83. package/dist/services/run-scheduling-wakeup.js.map +1 -0
  84. package/dist/services/runtime-layer.d.ts +48 -0
  85. package/dist/services/runtime-layer.js +51 -0
  86. package/dist/services/runtime-layer.js.map +1 -0
  87. package/dist/services/runtime.d.ts +1 -1
  88. package/dist/services/runtime.js +34 -20
  89. package/dist/services/runtime.js.map +1 -1
  90. package/dist/services/session-event-observer.d.ts +2 -0
  91. package/dist/services/session-event-observer.js +13 -0
  92. package/dist/services/session-event-observer.js.map +1 -0
  93. package/dist/services/session-query.d.ts +2 -2
  94. package/dist/services/session-query.js +141 -2
  95. package/dist/services/session-query.js.map +1 -0
  96. package/dist/services/session-state.d.ts +2 -0
  97. package/dist/services/session-state.js +2 -0
  98. package/dist/services/session-store.d.ts +2 -2
  99. package/dist/services/session-store.js +272 -2
  100. package/dist/services/session-store.js.map +1 -0
  101. package/dist/services/sink-checkpoint-store.d.ts +2 -0
  102. package/dist/services/sink-checkpoint-store.js +50 -0
  103. package/dist/services/sink-checkpoint-store.js.map +1 -0
  104. package/dist/services/sink-registry.d.ts +2 -2
  105. package/dist/services/sink-registry.js +1 -1
  106. package/dist/services/span-names.d.ts +1 -1
  107. package/dist/services/span-names.js +7 -1
  108. package/dist/services/span-names.js.map +1 -1
  109. package/dist/services/tool-registry.d.ts +2 -2
  110. package/dist/services/tool-registry.js +12 -3
  111. package/dist/services/tool-registry.js.map +1 -1
  112. package/dist/services/tracing.d.ts +3 -3
  113. package/dist/services/tracing.js +25 -4
  114. package/dist/services/tracing.js.map +1 -1
  115. package/dist/session-event-observer-HvrRckbC.d.ts +24 -0
  116. package/dist/{session-query-Cbf-e0rG.js → session-state-BQYsuBqi.js} +693 -251
  117. package/dist/session-state-BQYsuBqi.js.map +1 -0
  118. package/dist/session-store-CQxg9U82.d.ts +3302 -0
  119. package/dist/sink-checkpoint-store-DWGifxp0.d.ts +39 -0
  120. package/dist/sink-registry-DskRcyOS.js +345 -0
  121. package/dist/sink-registry-DskRcyOS.js.map +1 -0
  122. package/dist/subscription-DmO3BcdT.js +505 -0
  123. package/dist/subscription-DmO3BcdT.js.map +1 -0
  124. package/dist/subscription-rC35dAWn.d.ts +958 -0
  125. package/dist/testkit/layers.d.ts +34 -19
  126. package/dist/testkit/layers.js +30 -16
  127. package/dist/testkit/layers.js.map +1 -1
  128. package/dist/testkit/transcript.d.ts +226 -0
  129. package/dist/testkit/transcript.js +1475 -0
  130. package/dist/testkit/transcript.js.map +1 -0
  131. package/dist/{tool-registry-CcnkAAQK.d.ts → tool-registry-BzlMhwzj.d.ts} +44 -8
  132. package/dist/{tracing-BXHAkxkx.d.ts → tracing-BwjbF285.d.ts} +1 -2
  133. package/dist/transcript.d.ts +101 -0
  134. package/dist/transcript.js +107 -0
  135. package/dist/transcript.js.map +1 -0
  136. package/dist/types/commands.d.ts +2 -2
  137. package/dist/types/commands.js +8 -3
  138. package/dist/types/commands.js.map +1 -1
  139. package/dist/types/core.d.ts +2 -2
  140. package/dist/types/core.js +4 -2
  141. package/dist/types/core.js.map +1 -1
  142. package/dist/types/events/durable.d.ts +2 -2
  143. package/dist/types/events/durable.js +2 -2
  144. package/dist/types/events.d.ts +5 -4
  145. package/dist/types/events.js +3 -2
  146. package/dist/types/events.js.map +1 -1
  147. package/dist/types/tracing.d.ts +1 -1
  148. package/dist/types/tracing.js.map +1 -1
  149. package/dist/websocket/events.d.ts +2 -0
  150. package/dist/websocket/events.js +2 -0
  151. package/dist/websocket.d.ts +2201 -0
  152. package/dist/websocket.js +684 -0
  153. package/dist/websocket.js.map +1 -0
  154. package/dist/window-read-CBZEJ84M.d.ts +113 -0
  155. package/dist/window-read-DPFAA1hM.js +264 -0
  156. package/dist/window-read-DPFAA1hM.js.map +1 -0
  157. package/package.json +87 -46
  158. package/testing/offline-trace/README.md +2 -1
  159. package/CHANGELOG.md +0 -25
  160. package/CONTRIBUTING.md +0 -26
  161. package/SECURITY.md +0 -7
  162. package/dist/command-queues-CQ9cwRP7.d.ts +0 -468
  163. package/dist/compaction-1LSZR944.d.ts +0 -144
  164. package/dist/durable-C3Rz7kg3.js.map +0 -1
  165. package/dist/durable-object-runtime-CiMKxGs6.d.ts +0 -257
  166. package/dist/durable-object-runtime-wKiqaAt2.js +0 -1494
  167. package/dist/durable-object-runtime-wKiqaAt2.js.map +0 -1
  168. package/dist/durable-object-storage-Czy7IPrh.d.ts +0 -41
  169. package/dist/host/durable-object-runtime.d.ts +0 -2
  170. package/dist/host/durable-object-runtime.js +0 -2
  171. package/dist/host/durable-object-storage.d.ts +0 -2
  172. package/dist/host/durable-object-storage.js +0 -0
  173. package/dist/host/durable-object.d.ts +0 -83
  174. package/dist/host/durable-object.js +0 -191
  175. package/dist/host/durable-object.js.map +0 -1
  176. package/dist/host/websocket-wire.d.ts +0 -2
  177. package/dist/host/websocket-wire.js +0 -105
  178. package/dist/host/websocket-wire.js.map +0 -1
  179. package/dist/index-BVVgDSdq.d.ts +0 -1
  180. package/dist/keep-alive-DYDsH6qN.d.ts +0 -111
  181. package/dist/keep-alive-p8dIcSVy.js.map +0 -1
  182. package/dist/message-transcript-BCHdHAe2.d.ts +0 -32
  183. package/dist/prompt-projector-DimXJPOJ.d.ts +0 -39
  184. package/dist/reduced-state-C4nVVzz9.js +0 -910
  185. package/dist/reduced-state-C4nVVzz9.js.map +0 -1
  186. package/dist/runtime-DcdPOEuE.d.ts +0 -60
  187. package/dist/services/compaction.js.map +0 -1
  188. package/dist/session-query-Cbf-e0rG.js.map +0 -1
  189. package/dist/session-query-hxfujxUH.d.ts +0 -769
  190. package/dist/session-store-D2Ore1QC.d.ts +0 -295
  191. package/dist/session-store-pYxQW9qh.js +0 -214
  192. package/dist/session-store-pYxQW9qh.js.map +0 -1
  193. package/dist/sink-registry-BziFpxoE.d.ts +0 -158
  194. package/dist/sink-registry-CFC6xjsN.js +0 -312
  195. package/dist/sink-registry-CFC6xjsN.js.map +0 -1
  196. package/docs/assets/architecture.svg +0 -216
  197. package/docs/assets/event-sequence.svg +0 -133
  198. package/docs/assets/gcp-trace.png +0 -0
  199. package/docs/assets/hero.svg +0 -33
  200. package/docs/message-steering.md +0 -831
  201. package/docs/releasing.md +0 -96
  202. package/docs/spec.md +0 -777
  203. package/docs/subagents.md +0 -725
  204. package/docs/testing.md +0 -64
  205. package/docs/ui-projection.md +0 -742
  206. package/docs/websocket-protocol.md +0 -639
  207. package/examples/001-no-tools/README.md +0 -65
  208. package/examples/001-no-tools/worker.ts +0 -79
  209. package/examples/002-slack-bridge/README.md +0 -134
  210. package/examples/002-slack-bridge/events.ts +0 -115
  211. package/examples/002-slack-bridge/reducer.ts +0 -216
  212. package/examples/002-slack-bridge/scenario.test.ts +0 -223
  213. package/examples/002-slack-bridge/sinks.ts +0 -109
  214. package/examples/002-slack-bridge/worker.ts +0 -140
  215. package/examples/003-sandbox-lifecycle/README.md +0 -78
  216. package/examples/003-sandbox-lifecycle/events.ts +0 -203
  217. package/examples/003-sandbox-lifecycle/reducer.ts +0 -305
  218. package/examples/003-sandbox-lifecycle/scenario.test.ts +0 -202
  219. package/examples/_shared/event-id.ts +0 -12
  220. package/examples/_shared/http.ts +0 -27
  221. package/examples/_shared/openai.ts +0 -33
@@ -1,203 +0,0 @@
1
- import * as Schema from "effect/Schema";
2
-
3
- import { EventId, SessionId, ToolCallId } from "../../src/types/core";
4
- import {
5
- DurableEventEnvelope,
6
- EventNamespace,
7
- EventType,
8
- NonNegativeInt,
9
- UnixEpochMillis,
10
- makeRootEDAEventTrace,
11
- schemaV1,
12
- } from "../../src/types/events";
13
-
14
- export const sandboxLifecycleNamespace = EventNamespace.make("example.sandbox");
15
-
16
- export const sandboxStartingEventType = EventType.make("SandboxStarting");
17
- export const sandboxStartedEventType = EventType.make("SandboxStarted");
18
- export const sandboxCommandStartedEventType = EventType.make("CommandStarted");
19
- export const sandboxCommandCompletedEventType = EventType.make("CommandCompleted");
20
- export const previewReadyEventType = EventType.make("PreviewReady");
21
- export const approvalRequestedEventType = EventType.make("ApprovalRequested");
22
- export const approvalGrantedEventType = EventType.make("ApprovalGranted");
23
- export const sandboxStoppedEventType = EventType.make("SandboxStopped");
24
-
25
- export const SandboxId = Schema.NonEmptyString.pipe(Schema.brand("SandboxId"));
26
- export type SandboxId = typeof SandboxId.Type;
27
-
28
- export const SandboxCommandId = Schema.NonEmptyString.pipe(Schema.brand("SandboxCommandId"));
29
- export type SandboxCommandId = typeof SandboxCommandId.Type;
30
-
31
- export const PreviewUrl = Schema.NonEmptyString.pipe(Schema.brand("PreviewUrl"));
32
- export type PreviewUrl = typeof PreviewUrl.Type;
33
-
34
- export const ApprovalAction = Schema.NonEmptyString.pipe(Schema.brand("ApprovalAction"));
35
- export type ApprovalAction = typeof ApprovalAction.Type;
36
-
37
- export const ApproverId = Schema.NonEmptyString.pipe(Schema.brand("ApproverId"));
38
- export type ApproverId = typeof ApproverId.Type;
39
-
40
- export const SandboxStartingPayload = Schema.Struct({ toolCallId: ToolCallId });
41
- export type SandboxStartingPayload = typeof SandboxStartingPayload.Type;
42
-
43
- export const SandboxStartedPayload = Schema.Struct({
44
- toolCallId: ToolCallId,
45
- sandboxId: SandboxId,
46
- });
47
- export type SandboxStartedPayload = typeof SandboxStartedPayload.Type;
48
-
49
- export const SandboxCommandStartedPayload = Schema.Struct({
50
- sandboxId: SandboxId,
51
- commandId: SandboxCommandId,
52
- command: Schema.NonEmptyString,
53
- });
54
- export type SandboxCommandStartedPayload = typeof SandboxCommandStartedPayload.Type;
55
-
56
- export const SandboxCommandCompletedPayload = Schema.Struct({
57
- sandboxId: SandboxId,
58
- commandId: SandboxCommandId,
59
- wallClockTimeMs: NonNegativeInt,
60
- });
61
- export type SandboxCommandCompletedPayload = typeof SandboxCommandCompletedPayload.Type;
62
-
63
- export const PreviewReadyPayload = Schema.Struct({
64
- sandboxId: SandboxId,
65
- url: PreviewUrl,
66
- });
67
- export type PreviewReadyPayload = typeof PreviewReadyPayload.Type;
68
-
69
- export const ApprovalRequestedPayload = Schema.Struct({
70
- sandboxId: SandboxId,
71
- action: ApprovalAction,
72
- });
73
- export type ApprovalRequestedPayload = typeof ApprovalRequestedPayload.Type;
74
-
75
- export const ApprovalGrantedPayload = Schema.Struct({
76
- sandboxId: SandboxId,
77
- action: ApprovalAction,
78
- approverId: ApproverId,
79
- });
80
- export type ApprovalGrantedPayload = typeof ApprovalGrantedPayload.Type;
81
-
82
- export const SandboxStoppedPayload = Schema.Struct({
83
- sandboxId: SandboxId,
84
- reason: Schema.NonEmptyString,
85
- });
86
- export type SandboxStoppedPayload = typeof SandboxStoppedPayload.Type;
87
-
88
- interface AppEventContext {
89
- readonly createdAtMs: UnixEpochMillis;
90
- readonly eventId: EventId;
91
- readonly sessionId: SessionId;
92
- }
93
-
94
- export const SandboxEvents = {
95
- starting: (input: AppEventContext & SandboxStartingPayload): DurableEventEnvelope => {
96
- const { createdAtMs, eventId, sessionId, ...payload } = input;
97
- return sandboxDurableEvent({
98
- createdAtMs,
99
- eventId,
100
- payload: SandboxStartingPayload.make(payload),
101
- sessionId,
102
- type: sandboxStartingEventType,
103
- });
104
- },
105
-
106
- started: (input: AppEventContext & SandboxStartedPayload): DurableEventEnvelope => {
107
- const { createdAtMs, eventId, sessionId, ...payload } = input;
108
- return sandboxDurableEvent({
109
- createdAtMs,
110
- eventId,
111
- payload: SandboxStartedPayload.make(payload),
112
- sessionId,
113
- type: sandboxStartedEventType,
114
- });
115
- },
116
-
117
- commandStarted: (input: AppEventContext & SandboxCommandStartedPayload): DurableEventEnvelope => {
118
- const { createdAtMs, eventId, sessionId, ...payload } = input;
119
- return sandboxDurableEvent({
120
- createdAtMs,
121
- eventId,
122
- payload: SandboxCommandStartedPayload.make(payload),
123
- sessionId,
124
- type: sandboxCommandStartedEventType,
125
- });
126
- },
127
-
128
- commandCompleted: (
129
- input: AppEventContext & SandboxCommandCompletedPayload,
130
- ): DurableEventEnvelope => {
131
- const { createdAtMs, eventId, sessionId, ...payload } = input;
132
- return sandboxDurableEvent({
133
- createdAtMs,
134
- eventId,
135
- payload: SandboxCommandCompletedPayload.make(payload),
136
- sessionId,
137
- type: sandboxCommandCompletedEventType,
138
- });
139
- },
140
-
141
- previewReady: (input: AppEventContext & PreviewReadyPayload): DurableEventEnvelope => {
142
- const { createdAtMs, eventId, sessionId, ...payload } = input;
143
- return sandboxDurableEvent({
144
- createdAtMs,
145
- eventId,
146
- payload: PreviewReadyPayload.make(payload),
147
- sessionId,
148
- type: previewReadyEventType,
149
- });
150
- },
151
-
152
- approvalRequested: (input: AppEventContext & ApprovalRequestedPayload): DurableEventEnvelope => {
153
- const { createdAtMs, eventId, sessionId, ...payload } = input;
154
- return sandboxDurableEvent({
155
- createdAtMs,
156
- eventId,
157
- payload: ApprovalRequestedPayload.make(payload),
158
- sessionId,
159
- type: approvalRequestedEventType,
160
- });
161
- },
162
-
163
- approvalGranted: (input: AppEventContext & ApprovalGrantedPayload): DurableEventEnvelope => {
164
- const { createdAtMs, eventId, sessionId, ...payload } = input;
165
- return sandboxDurableEvent({
166
- createdAtMs,
167
- eventId,
168
- payload: ApprovalGrantedPayload.make(payload),
169
- sessionId,
170
- type: approvalGrantedEventType,
171
- });
172
- },
173
-
174
- stopped: (input: AppEventContext & SandboxStoppedPayload): DurableEventEnvelope => {
175
- const { createdAtMs, eventId, sessionId, ...payload } = input;
176
- return sandboxDurableEvent({
177
- createdAtMs,
178
- eventId,
179
- payload: SandboxStoppedPayload.make(payload),
180
- sessionId,
181
- type: sandboxStoppedEventType,
182
- });
183
- },
184
- };
185
-
186
- const sandboxDurableEvent = (input: {
187
- readonly createdAtMs: UnixEpochMillis;
188
- readonly eventId: EventId;
189
- readonly payload: unknown;
190
- readonly sessionId: SessionId;
191
- readonly type: EventType;
192
- }): DurableEventEnvelope =>
193
- DurableEventEnvelope.make({
194
- namespace: sandboxLifecycleNamespace,
195
- type: input.type,
196
- schemaVersion: schemaV1,
197
- durability: "durable",
198
- eventId: input.eventId,
199
- sessionId: input.sessionId,
200
- createdAtMs: input.createdAtMs,
201
- trace: makeRootEDAEventTrace(),
202
- payload: input.payload,
203
- });
@@ -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
- });