@systemfsoftware/agent-platform-cloudflare 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/Alarm-CbhflIzz.mjs +1931 -0
- package/dist/Alarm-CbhflIzz.mjs.map +1 -0
- package/dist/Alarm-Nz1gIjfv.d.mts +252 -0
- package/dist/Alarm.d.mts +3 -0
- package/dist/Alarm.mjs +3 -0
- package/dist/BrowserCredentials-BWC4RqRn.d.mts +116 -0
- package/dist/BrowserCredentials-CktBoPgE.mjs +140 -0
- package/dist/BrowserCredentials-CktBoPgE.mjs.map +1 -0
- package/dist/BrowserCredentials.d.mts +2 -0
- package/dist/BrowserCredentials.mjs +1 -0
- package/dist/BrowserRestCapture.d.mts +14 -0
- package/dist/BrowserRestCapture.mjs +195 -0
- package/dist/BrowserRestCapture.mjs.map +1 -0
- package/dist/BrowserSession-5bdpqPIT.d.mts +152 -0
- package/dist/BrowserSession-Ce8cWN1A.mjs +673 -0
- package/dist/BrowserSession-Ce8cWN1A.mjs.map +1 -0
- package/dist/BrowserSession.d.mts +3 -0
- package/dist/BrowserSession.mjs +4 -0
- package/dist/BrowserUse-Bsi830Yv.mjs +263 -0
- package/dist/BrowserUse-Bsi830Yv.mjs.map +1 -0
- package/dist/BrowserUse-DF8afbNt.d.mts +59 -0
- package/dist/BrowserUse.d.mts +2 -0
- package/dist/BrowserUse.mjs +1 -0
- package/dist/CloudflareAiGateway-By8C8c5z.mjs +79 -0
- package/dist/CloudflareAiGateway-By8C8c5z.mjs.map +1 -0
- package/dist/CloudflareAiGateway-ukN34014.d.mts +63 -0
- package/dist/CloudflareAiGateway.d.mts +2 -0
- package/dist/CloudflareAiGateway.mjs +1 -0
- package/dist/CloudflareBindings-Dylkir84.mjs +123 -0
- package/dist/CloudflareBindings-Dylkir84.mjs.map +1 -0
- package/dist/CloudflareBindings-E1Sty9Vq.d.mts +134 -0
- package/dist/CloudflareBindings.d.mts +2 -0
- package/dist/CloudflareBindings.mjs +1 -0
- package/dist/CloudflareBrowser-CmiQpslZ.d.mts +41 -0
- package/dist/CloudflareBrowser-DmMCFeIQ.mjs +397 -0
- package/dist/CloudflareBrowser-DmMCFeIQ.mjs.map +1 -0
- package/dist/CloudflareBrowser.d.mts +3 -0
- package/dist/CloudflareBrowser.mjs +1 -0
- package/dist/CloudflareCodeMode-BMva6dCs.mjs +627 -0
- package/dist/CloudflareCodeMode-BMva6dCs.mjs.map +1 -0
- package/dist/CloudflareCodeMode-CeGXZtHv.d.mts +43 -0
- package/dist/CloudflareCodeMode.d.mts +2 -0
- package/dist/CloudflareCodeMode.mjs +1 -0
- package/dist/CloudflareConfig-BFWgEyhH.d.mts +102 -0
- package/dist/CloudflareConfig-rBlbPSIZ.mjs +125 -0
- package/dist/CloudflareConfig-rBlbPSIZ.mjs.map +1 -0
- package/dist/CloudflareConfig.d.mts +2 -0
- package/dist/CloudflareConfig.mjs +1 -0
- package/dist/CloudflareMemory-Bzp32ZpR.d.mts +120 -0
- package/dist/CloudflareMemory-DejIVKBT.mjs +207 -0
- package/dist/CloudflareMemory-DejIVKBT.mjs.map +1 -0
- package/dist/CloudflareMemory.d.mts +2 -0
- package/dist/CloudflareMemory.mjs +1 -0
- package/dist/CloudflareScheduling-BleqfC44.mjs +395 -0
- package/dist/CloudflareScheduling-BleqfC44.mjs.map +1 -0
- package/dist/CloudflareScheduling-vZ7iSR57.d.mts +54 -0
- package/dist/CloudflareScheduling.d.mts +2 -0
- package/dist/CloudflareScheduling.mjs +1 -0
- package/dist/CloudflareSubscriptions-IHSo19MU.d.mts +91 -0
- package/dist/CloudflareSubscriptions-S3fZF-Jo.mjs +529 -0
- package/dist/CloudflareSubscriptions-S3fZF-Jo.mjs.map +1 -0
- package/dist/CloudflareSubscriptions.d.mts +2 -0
- package/dist/CloudflareSubscriptions.mjs +1 -0
- package/dist/CloudflareThreadClient-BUv9Te1s.d.mts +2141 -0
- package/dist/CloudflareThreadClient-K7QJHYpK.mjs +496 -0
- package/dist/CloudflareThreadClient-K7QJHYpK.mjs.map +1 -0
- package/dist/CloudflareThreadClient.d.mts +3 -0
- package/dist/CloudflareThreadClient.mjs +3 -0
- package/dist/InteractiveBrowser-CMbA7YPP.d.mts +228 -0
- package/dist/InteractiveBrowser-e8ZvxC1C.mjs +2013 -0
- package/dist/InteractiveBrowser-e8ZvxC1C.mjs.map +1 -0
- package/dist/InteractiveBrowser.d.mts +2 -0
- package/dist/InteractiveBrowser.mjs +1 -0
- package/dist/LiveText-CfpcZaGb.d.mts +91 -0
- package/dist/LiveText-DaF0tN_p.mjs +148 -0
- package/dist/LiveText-DaF0tN_p.mjs.map +1 -0
- package/dist/LiveText.d.mts +2 -0
- package/dist/LiveText.mjs +1 -0
- package/dist/R2ToolResultStorage.d.mts +27 -0
- package/dist/R2ToolResultStorage.mjs +84 -0
- package/dist/R2ToolResultStorage.mjs.map +1 -0
- package/dist/ThreadObject-B2-PqnpT.d.mts +390 -0
- package/dist/ThreadObject-D1pCeNrq.mjs +1225 -0
- package/dist/ThreadObject-D1pCeNrq.mjs.map +1 -0
- package/dist/ThreadObject.d.mts +2 -0
- package/dist/ThreadObject.mjs +1 -0
- package/dist/WakeScheduler-67UeXBYK.d.mts +22 -0
- package/dist/WakeScheduler-DV4J4TRe.mjs +69 -0
- package/dist/WakeScheduler-DV4J4TRe.mjs.map +1 -0
- package/dist/WakeScheduler.d.mts +2 -0
- package/dist/WakeScheduler.mjs +1 -0
- package/dist/boundary-YrOnE2zO.mjs +41 -0
- package/dist/boundary-YrOnE2zO.mjs.map +1 -0
- package/dist/browser-failure-CWBBnf1j.mjs +70 -0
- package/dist/browser-failure-CWBBnf1j.mjs.map +1 -0
- package/dist/index.d.mts +17 -0
- package/dist/index.mjs +17 -0
- package/dist/prepared-admission-DngoYN74.mjs +57 -0
- package/dist/prepared-admission-DngoYN74.mjs.map +1 -0
- package/dist/rolldown-runtime-D3KUYPsf.mjs +12 -0
- package/package.json +134 -3
- package/src/Alarm.ts +3402 -0
- package/src/BrowserCredentials.ts +181 -0
- package/src/BrowserRestCapture.ts +383 -0
- package/src/BrowserSession.ts +993 -0
- package/src/BrowserUse.ts +438 -0
- package/src/CloudflareAiGateway.ts +176 -0
- package/src/CloudflareBindings.ts +225 -0
- package/src/CloudflareBrowser.ts +9 -0
- package/src/CloudflareCodeMode.ts +896 -0
- package/src/CloudflareConfig.ts +142 -0
- package/src/CloudflareMemory.ts +418 -0
- package/src/CloudflareScheduling.ts +736 -0
- package/src/CloudflareSubscriptions.ts +1032 -0
- package/src/CloudflareThreadClient.ts +980 -0
- package/src/InteractiveBrowser.ts +3300 -0
- package/src/LiveText.ts +145 -0
- package/src/R2ToolResultStorage.ts +131 -0
- package/src/ThreadObject.ts +1348 -0
- package/src/WakeScheduler.ts +102 -0
- package/src/index.ts +16 -0
- package/src/internal/boundary.ts +53 -0
- package/src/internal/browser-binding.ts +483 -0
- package/src/internal/browser-credentials.ts +481 -0
- package/src/internal/browser-failure.ts +145 -0
- package/src/internal/browser-file-selection.ts +146 -0
- package/src/internal/browser-quick-action.ts +666 -0
- package/src/internal/browser-readonly-live-view.ts +121 -0
- package/src/internal/browser-session-lifecycle.ts +200 -0
- package/src/internal/crypto.ts +44 -0
- package/src/internal/due-queue.ts +452 -0
- package/src/internal/host-protocol-error.ts +23 -0
- package/src/internal/layers.ts +1181 -0
- package/src/internal/live-text-observation.ts +8 -0
- package/src/internal/live-text-protocol.ts +59 -0
- package/src/internal/live-text.ts +339 -0
- package/src/internal/message-delivery.ts +149 -0
- package/src/internal/page-function.ts +12 -0
- package/src/internal/prepared-admission.ts +83 -0
- package/src/internal/progress-wait.ts +121 -0
- package/src/internal/transport.ts +53 -0
- package/README.md +0 -3
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-D3KUYPsf.mjs";
|
|
2
|
+
import { defaultDoMemoryStorageLimits, doMemoryStoreLayerWithFailpoints } from "@systemfsoftware/agent-storage-cloudflare/do-memory-store";
|
|
3
|
+
import { MemoryOwnerIdentity, MemoryOwnerRequest, MemoryOwnerResponse, MemoryRpcError, MemoryRpcLimits, decodeMemoryWire, defaultMemoryRpcLimits, encodeMemoryWire, handleMemoryOwnerRequest } from "@systemfsoftware/agent-storage-cloudflare/memory-protocol";
|
|
4
|
+
import * as Memory from "@systemfsoftware/agent/memory";
|
|
5
|
+
import * as MemoryNamespace from "@systemfsoftware/agent/memory-namespace";
|
|
6
|
+
import { MemoryNamespaceAddress } from "@systemfsoftware/agent/memory-namespace";
|
|
7
|
+
import { MemoryRecallLimits } from "@systemfsoftware/agent/memory-reference";
|
|
8
|
+
import { MemoryAccess } from "@systemfsoftware/agent/memory-revalidation";
|
|
9
|
+
import { MemoryDocument, MemoryKey, MemoryMutationFailpoint, MemoryStorageError, MemoryWriter } from "@systemfsoftware/agent/memory-store";
|
|
10
|
+
import "@systemfsoftware/agent/semantic-memory-index";
|
|
11
|
+
import "@systemfsoftware/agent/semantic-memory-revalidation";
|
|
12
|
+
import { Principal } from "@systemfsoftware/agent/submission-ledger";
|
|
13
|
+
import { DurableObject, DurableObjectState, RpcTargets } from "effect-cf";
|
|
14
|
+
import * as Clock from "effect/Clock";
|
|
15
|
+
import * as Context from "effect/Context";
|
|
16
|
+
import * as Effect from "effect/Effect";
|
|
17
|
+
import * as Layer from "effect/Layer";
|
|
18
|
+
import * as Schema from "effect/Schema";
|
|
19
|
+
// src/CloudflareMemory.ts
|
|
20
|
+
var CloudflareMemory_exports = /* @__PURE__ */ __exportAll({
|
|
21
|
+
CloudflareMemoryClient: () => CloudflareMemoryClient,
|
|
22
|
+
MemoryObject: () => MemoryObject,
|
|
23
|
+
MemoryObjectNamespace: () => MemoryObjectNamespace,
|
|
24
|
+
cloudflareMemoryWriterLayer: () => cloudflareMemoryWriterLayer,
|
|
25
|
+
memoryObjectName: () => memoryObjectName
|
|
26
|
+
});
|
|
27
|
+
var MemoryObjectNamespace = class extends Context.Service()("@effect-agent/platform-cloudflare/MemoryObjectNamespace") {};
|
|
28
|
+
/** Namespace version and identity are already canonicalized by MemoryNamespace. */
|
|
29
|
+
const memoryObjectName = (namespace) => namespace.address;
|
|
30
|
+
/**
|
|
31
|
+
* Effect-native, host-bound memory client. Recall revalidates the entire admitted lookup
|
|
32
|
+
* in one RPC and renders it locally. No retries or per-source splitting occur here.
|
|
33
|
+
* Interrupted callers stop waiting; the owner has its own deadline. A timed-out write
|
|
34
|
+
* may have committed: reconcile by sending the identical operation ID and command.
|
|
35
|
+
*/
|
|
36
|
+
const makeMemoryClient = Effect.fnUntraced(function* (access, principal, rpcLimits = defaultMemoryRpcLimits) {
|
|
37
|
+
const validated = yield* Schema.decodeEffect(MemoryRpcLimits)(rpcLimits).pipe(Effect.mapError(() => MemoryRpcError.make({ reason: "protocol" })));
|
|
38
|
+
const bound = yield* Schema.decodeUnknownEffect(MemoryAccess.Wire)(access).pipe(Effect.mapError(() => MemoryRpcError.make({ reason: "protocol" })));
|
|
39
|
+
principal = yield* Schema.decodeEffect(Principal)(principal).pipe(Effect.mapError(() => MemoryRpcError.make({ reason: "protocol" })));
|
|
40
|
+
const { namespace } = yield* MemoryObjectNamespace;
|
|
41
|
+
const call = Effect.fnUntraced(function* (request) {
|
|
42
|
+
const decoded = yield* Schema.decodeEffect(MemoryOwnerRequest)(request).pipe(Effect.mapError(() => MemoryRpcError.make({ reason: "protocol" })));
|
|
43
|
+
const encoded = yield* encodeMemoryWire(MemoryOwnerRequest, decoded, validated.maxRequestBytes);
|
|
44
|
+
const unavailable = (cause) => {
|
|
45
|
+
const error = MemoryRpcError.make({ reason: "unavailable" });
|
|
46
|
+
error.cause = cause;
|
|
47
|
+
return error;
|
|
48
|
+
};
|
|
49
|
+
const address = memoryObjectName(bound.namespace);
|
|
50
|
+
const target = yield* RpcTargets.get(namespace, address, () => namespace.get(namespace.idFromName(address))).pipe(Effect.mapError(unavailable));
|
|
51
|
+
const raw = yield* Effect.tryPromise({
|
|
52
|
+
try: () => target.memory(encoded),
|
|
53
|
+
catch: unavailable
|
|
54
|
+
}).pipe(Effect.tapCause(() => RpcTargets.invalidate(target)));
|
|
55
|
+
const response = yield* decodeMemoryWire(MemoryOwnerResponse, raw, validated.maxResponseBytes);
|
|
56
|
+
if (response._tag === "Failed") return yield* response.failure;
|
|
57
|
+
if (!MemoryNamespace.equals(response.access.namespace, bound.namespace) || response.access.scope !== bound.scope) return yield* MemoryRpcError.make({ reason: "protocol" });
|
|
58
|
+
return response;
|
|
59
|
+
});
|
|
60
|
+
const withinDeadline = (effect, timeoutMillis) => effect.pipe(Effect.timeoutOrElse({
|
|
61
|
+
duration: timeoutMillis,
|
|
62
|
+
orElse: () => Effect.fail(MemoryRpcError.make({ reason: "timeout" }))
|
|
63
|
+
}));
|
|
64
|
+
const revalidate = Effect.fnUntraced(function* (lookup, limits) {
|
|
65
|
+
limits = yield* Schema.decodeEffect(MemoryRecallLimits)(limits).pipe(Effect.mapError(() => MemoryRpcError.make({ reason: "protocol" })));
|
|
66
|
+
const timeoutMillis = Math.min(validated.timeoutMillis, limits.timeoutMillis);
|
|
67
|
+
return yield* Effect.gen(function* () {
|
|
68
|
+
const response = yield* call({
|
|
69
|
+
_tag: "Revalidate",
|
|
70
|
+
version: 1,
|
|
71
|
+
access: bound,
|
|
72
|
+
principal,
|
|
73
|
+
lookup,
|
|
74
|
+
limits,
|
|
75
|
+
deadlineMillis: (yield* Clock.currentTimeMillis) + timeoutMillis
|
|
76
|
+
});
|
|
77
|
+
if (response._tag !== "Lookup") return yield* MemoryRpcError.make({ reason: "protocol" });
|
|
78
|
+
return response.lookup;
|
|
79
|
+
}).pipe((effect) => withinDeadline(effect, timeoutMillis));
|
|
80
|
+
});
|
|
81
|
+
const change = Effect.fnUntraced(function* (write) {
|
|
82
|
+
if (!MemoryNamespace.equals(write.key.namespace, bound.namespace)) return yield* MemoryRpcError.make({ reason: "denied" });
|
|
83
|
+
return yield* Effect.gen(function* () {
|
|
84
|
+
const response = yield* call({
|
|
85
|
+
_tag: "Change",
|
|
86
|
+
version: 1,
|
|
87
|
+
access: bound,
|
|
88
|
+
principal,
|
|
89
|
+
write,
|
|
90
|
+
deadlineMillis: (yield* Clock.currentTimeMillis) + validated.timeoutMillis
|
|
91
|
+
});
|
|
92
|
+
if (response._tag !== "Changed" || response.document.key.id !== write.key.id) return yield* MemoryRpcError.make({ reason: "protocol" });
|
|
93
|
+
return yield* MemoryDocument.restore(access.namespace, response.document);
|
|
94
|
+
}).pipe((effect) => withinDeadline(effect, validated.timeoutMillis));
|
|
95
|
+
});
|
|
96
|
+
/**
|
|
97
|
+
* Read one exact current document in one owner RPC. Null means absent; withdrawals return
|
|
98
|
+
* tombstones. Denial, unavailable storage and deadlines fail typed, never become absence.
|
|
99
|
+
* Reads begun after an acknowledged write observe it or a later revision. The owner checks
|
|
100
|
+
* exact-key authority and active document scopes; source-dependent provenance policy remains
|
|
101
|
+
* application-owned. No extraction, job draining, embedding, discovery or rendering occurs.
|
|
102
|
+
*/
|
|
103
|
+
const get = Effect.fnUntraced(function* (key) {
|
|
104
|
+
const decodedKey = yield* Schema.decodeUnknownEffect(MemoryKey.Wire)(key).pipe(Effect.mapError(() => MemoryRpcError.make({ reason: "protocol" })));
|
|
105
|
+
if (!MemoryNamespace.equals(decodedKey.namespace, bound.namespace)) return yield* MemoryRpcError.make({ reason: "denied" });
|
|
106
|
+
return yield* Effect.gen(function* () {
|
|
107
|
+
const response = yield* call({
|
|
108
|
+
_tag: "Get",
|
|
109
|
+
version: 1,
|
|
110
|
+
access: bound,
|
|
111
|
+
principal,
|
|
112
|
+
key: decodedKey,
|
|
113
|
+
deadlineMillis: (yield* Clock.currentTimeMillis) + validated.timeoutMillis
|
|
114
|
+
});
|
|
115
|
+
if (response._tag !== "Document" || !MemoryNamespace.equals(response.key.namespace, bound.namespace) || response.key.id !== decodedKey.id) return yield* MemoryRpcError.make({ reason: "protocol" });
|
|
116
|
+
if (response.document === null) return null;
|
|
117
|
+
if (response.document.key.id !== decodedKey.id || response.document.source.id !== decodedKey.id || response.document._tag === "ActiveMemoryDocument" && !response.document.scopes.includes(bound.scope)) return yield* MemoryRpcError.make({ reason: "protocol" });
|
|
118
|
+
yield* encodeMemoryWire(MemoryDocument.Wire, response.document, validated.maxSourceBytes);
|
|
119
|
+
return yield* MemoryDocument.restore(access.namespace, response.document);
|
|
120
|
+
}).pipe((effect) => withinDeadline(effect, validated.timeoutMillis));
|
|
121
|
+
});
|
|
122
|
+
const revalidateSemantic = Effect.fnUntraced(function* (found, profile, limits) {
|
|
123
|
+
const response = yield* call({
|
|
124
|
+
_tag: "RevalidateSemantic",
|
|
125
|
+
version: 1,
|
|
126
|
+
access: bound,
|
|
127
|
+
principal,
|
|
128
|
+
found,
|
|
129
|
+
profile,
|
|
130
|
+
limits,
|
|
131
|
+
deadlineMillis: (yield* Clock.currentTimeMillis) + validated.timeoutMillis
|
|
132
|
+
});
|
|
133
|
+
if (response._tag !== "Semantic") return yield* MemoryRpcError.make({ reason: "protocol" });
|
|
134
|
+
return response.result;
|
|
135
|
+
}, (effect) => withinDeadline(effect, validated.timeoutMillis));
|
|
136
|
+
/**
|
|
137
|
+
* Revalidate in one owner RPC, then render whole passages within the caller's budget.
|
|
138
|
+
* The bound source is essential: unavailable/stale results and matches that cannot fit
|
|
139
|
+
* fail instead of silently producing empty context. No-match remains successful.
|
|
140
|
+
* The single outcome has sourceId "memory". No embedding or candidate search is performed.
|
|
141
|
+
* Use revalidate with Memory.recall for multiple readers sharing one output budget.
|
|
142
|
+
*/
|
|
143
|
+
const recall = (lookup, limits, estimateTokens) => Memory.recall([{
|
|
144
|
+
id: "memory",
|
|
145
|
+
essential: true,
|
|
146
|
+
read: revalidate(lookup, limits)
|
|
147
|
+
}], limits, estimateTokens);
|
|
148
|
+
return {
|
|
149
|
+
get,
|
|
150
|
+
recall,
|
|
151
|
+
revalidate,
|
|
152
|
+
revalidateSemantic,
|
|
153
|
+
change
|
|
154
|
+
};
|
|
155
|
+
});
|
|
156
|
+
const CloudflareMemoryClient = {
|
|
157
|
+
/** Bind access and principal using the MemoryObjectNamespace supplied by the application. */
|
|
158
|
+
make: makeMemoryClient,
|
|
159
|
+
/** Use a resolved Worker or Durable Object binding without manual service provisioning. */
|
|
160
|
+
fromBinding: (binding, options) => makeMemoryClient(options.access, options.principal, options.rpcLimits).pipe(Effect.provideService(MemoryObjectNamespace, { namespace: binding }))
|
|
161
|
+
};
|
|
162
|
+
/**
|
|
163
|
+
* Optional activity-processor destination. Keeps domain write errors intact; transport,
|
|
164
|
+
* authorization and deadline failures become the existing MemoryStorageError contract.
|
|
165
|
+
* Receipts remain authoritative, including after caller interruption or lost replies.
|
|
166
|
+
*/
|
|
167
|
+
const cloudflareMemoryWriterLayer = (access, principal, limits = defaultMemoryRpcLimits) => Layer.effect(MemoryWriter, Effect.gen(function* () {
|
|
168
|
+
const client = yield* CloudflareMemoryClient.make(access, principal, limits);
|
|
169
|
+
return MemoryWriter.fromAdapter({ change: (write) => client.change(write).pipe(Effect.catchTag("MemoryRpcError", (error) => Effect.fail(MemoryStorageError.make({
|
|
170
|
+
operation: `memory RPC ${error.reason}`,
|
|
171
|
+
reason: error.reason === "unavailable" || error.reason === "timeout" ? "unavailable" : "invalid-input"
|
|
172
|
+
}))), Effect.catchTag([
|
|
173
|
+
"MemoryRecallError",
|
|
174
|
+
"MemoryIndexError",
|
|
175
|
+
"SemanticMemoryError"
|
|
176
|
+
], () => Effect.fail(MemoryStorageError.make({
|
|
177
|
+
operation: "memory RPC response",
|
|
178
|
+
reason: "corrupt"
|
|
179
|
+
})))) });
|
|
180
|
+
}));
|
|
181
|
+
/**
|
|
182
|
+
* Dedicated SQLite owner, independent of Thread lifetimes. The host binds authorization
|
|
183
|
+
* after restoring its namespace definition from MemoryOwnerIdentity. Do not retain
|
|
184
|
+
* cleanup-scoped resources in the host Layer; it lives for the DO incarnation.
|
|
185
|
+
*/
|
|
186
|
+
const makeMemoryObject = (host, options = {}) => {
|
|
187
|
+
const identity = Layer.effect(MemoryOwnerIdentity, Effect.gen(function* () {
|
|
188
|
+
const state = yield* DurableObjectState.DurableObjectState;
|
|
189
|
+
const address = yield* Schema.decodeUnknownEffect(MemoryNamespaceAddress)(state.raw.id.name).pipe(Effect.mapError(() => MemoryRpcError.make({ reason: "denied" })));
|
|
190
|
+
return { namespace: MemoryNamespace.Any.make({ address }) };
|
|
191
|
+
}));
|
|
192
|
+
const store = Layer.unwrap(Effect.map(DurableObjectState.DurableObjectState, (state) => doMemoryStoreLayerWithFailpoints(state.raw.storage, options.storageLimits ?? defaultDoMemoryStorageLimits))).pipe(Layer.provide(options.failpoints ?? MemoryMutationFailpoint.layer));
|
|
193
|
+
const application = Layer.merge(store, host).pipe(Layer.provideMerge(identity));
|
|
194
|
+
const runtime = Layer.effectContext(Effect.gen(function* () {
|
|
195
|
+
const state = yield* DurableObjectState.DurableObjectState;
|
|
196
|
+
const scope = yield* Effect.scope;
|
|
197
|
+
yield* Schema.decodeEffect(MemoryRpcLimits)(options.rpcLimits ?? defaultMemoryRpcLimits).pipe(Effect.mapError(() => MemoryRpcError.make({ reason: "protocol" })));
|
|
198
|
+
return yield* state.blockConcurrencyWhile(Layer.buildWithScope(application, scope));
|
|
199
|
+
}));
|
|
200
|
+
return DurableObject.make(runtime, { rpc: { memory: (encoded) => handleMemoryOwnerRequest(encoded, options.rpcLimits) } });
|
|
201
|
+
};
|
|
202
|
+
const MemoryObject = {
|
|
203
|
+
/** Build the SQLite Durable Object class with the application's owner authorization Layer. */
|
|
204
|
+
make: makeMemoryObject };
|
|
205
|
+
export { cloudflareMemoryWriterLayer as a, MemoryObjectNamespace as i, CloudflareMemory_exports as n, memoryObjectName as o, MemoryObject as r, CloudflareMemoryClient as t };
|
|
206
|
+
|
|
207
|
+
//# sourceMappingURL=CloudflareMemory-DejIVKBT.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CloudflareMemory-DejIVKBT.mjs","names":["EffectCfDurableObject"],"sources":["../src/CloudflareMemory.ts"],"sourcesContent":["import {\n defaultDoMemoryStorageLimits,\n type DoMemoryStorageLimits,\n doMemoryStoreLayerWithFailpoints,\n} from '@systemfsoftware/agent-storage-cloudflare/do-memory-store'\nimport {\n decodeMemoryWire,\n defaultMemoryRpcLimits,\n encodeMemoryWire,\n handleMemoryOwnerRequest,\n type MemoryOwnerAuthorizer,\n type MemoryOwnerFailure,\n MemoryOwnerIdentity,\n MemoryOwnerRequest,\n MemoryOwnerResponse,\n MemoryRpcError,\n MemoryRpcLimits,\n} from '@systemfsoftware/agent-storage-cloudflare/memory-protocol'\nimport * as Memory from '@systemfsoftware/agent/memory'\nimport * as MemoryNamespace from '@systemfsoftware/agent/memory-namespace'\nimport { MemoryNamespaceAddress } from '@systemfsoftware/agent/memory-namespace'\nimport { type MemoryLookup, MemoryRecallLimits } from '@systemfsoftware/agent/memory-reference'\nimport { MemoryAccess } from '@systemfsoftware/agent/memory-revalidation'\nimport {\n MemoryDocument,\n MemoryKey,\n MemoryMutationFailpoint,\n type MemoryReader,\n MemoryStorageError,\n type MemoryWrite,\n MemoryWriter,\n} from '@systemfsoftware/agent/memory-store'\nimport { type MemoryIndexSearch, type SemanticMemoryProfile } from '@systemfsoftware/agent/semantic-memory-index'\nimport { type SemanticCandidateLimits } from '@systemfsoftware/agent/semantic-memory-revalidation'\nimport { Principal } from '@systemfsoftware/agent/submission-ledger'\nimport {\n DurableObject as EffectCfDurableObject,\n DurableObjectState,\n RpcTargets,\n type WorkerEnvironment,\n} from 'effect-cf'\nimport * as Clock from 'effect/Clock'\nimport * as Context from 'effect/Context'\nimport * as Effect from 'effect/Effect'\nimport * as Layer from 'effect/Layer'\nimport * as Schema from 'effect/Schema'\n\nexport interface MemoryObjectRpc extends Rpc.DurableObjectBranded {\n memory(encoded: string): Promise<string>\n}\n\nexport class MemoryObjectNamespace extends Context.Service<\n MemoryObjectNamespace,\n {\n readonly namespace: DurableObjectNamespace<MemoryObjectRpc>\n }\n>()('@effect-agent/platform-cloudflare/MemoryObjectNamespace') {}\n\n/** Namespace version and identity are already canonicalized by MemoryNamespace. */\nexport const memoryObjectName = (namespace: MemoryNamespace.Any): string => namespace.address\n\n/**\n * Effect-native, host-bound memory client. Recall revalidates the entire admitted lookup\n * in one RPC and renders it locally. No retries or per-source splitting occur here.\n * Interrupted callers stop waiting; the owner has its own deadline. A timed-out write\n * may have committed: reconcile by sending the identical operation ID and command.\n */\nconst makeMemoryClient = Effect.fnUntraced(function*<Namespace extends MemoryNamespace.Any>(\n access: MemoryAccess<Namespace>,\n principal: Principal,\n rpcLimits: MemoryRpcLimits = defaultMemoryRpcLimits,\n) {\n const validated = yield* Schema.decodeEffect(MemoryRpcLimits)(rpcLimits).pipe(\n Effect.mapError(() => MemoryRpcError.make({ reason: 'protocol' })),\n )\n\n const bound = yield* Schema.decodeUnknownEffect(MemoryAccess.Wire)(access).pipe(\n Effect.mapError(() => MemoryRpcError.make({ reason: 'protocol' })),\n )\n\n principal = yield* Schema.decodeEffect(Principal)(principal).pipe(\n Effect.mapError(() => MemoryRpcError.make({ reason: 'protocol' })),\n )\n const { namespace } = yield* MemoryObjectNamespace\n\n const call = Effect.fnUntraced(function*(request: MemoryOwnerRequest) {\n const decoded = yield* Schema.decodeEffect(MemoryOwnerRequest)(request).pipe(\n Effect.mapError(() => MemoryRpcError.make({ reason: 'protocol' })),\n )\n\n const encoded = yield* encodeMemoryWire(MemoryOwnerRequest, decoded, validated.maxRequestBytes)\n\n const unavailable = (cause: unknown) => {\n const error = MemoryRpcError.make({ reason: 'unavailable' })\n\n error.cause = cause\n\n return error\n }\n\n const address = memoryObjectName(bound.namespace)\n\n const target = yield* RpcTargets.get(namespace, address, () => namespace.get(namespace.idFromName(address))).pipe(\n Effect.mapError(unavailable),\n )\n\n const raw = yield* Effect.tryPromise({\n try: () => target.memory(encoded),\n catch: unavailable,\n }).pipe(Effect.tapCause(() => RpcTargets.invalidate(target)))\n\n const response = yield* decodeMemoryWire(MemoryOwnerResponse, raw, validated.maxResponseBytes)\n\n if (response._tag === 'Failed') return yield* response.failure\n if (\n !MemoryNamespace.equals(response.access.namespace, bound.namespace) ||\n response.access.scope !== bound.scope\n ) {\n return yield* MemoryRpcError.make({ reason: 'protocol' })\n }\n\n return response\n })\n\n const withinDeadline = <A, E, R>(effect: Effect.Effect<A, E, R>, timeoutMillis: number) =>\n effect.pipe(\n Effect.timeoutOrElse({\n duration: timeoutMillis,\n orElse: () => Effect.fail(MemoryRpcError.make({ reason: 'timeout' })),\n }),\n )\n\n const revalidate = Effect.fnUntraced(function*(\n lookup: MemoryLookup,\n limits: MemoryRecallLimits,\n ) {\n limits = yield* Schema.decodeEffect(MemoryRecallLimits)(limits).pipe(\n Effect.mapError(() => MemoryRpcError.make({ reason: 'protocol' })),\n )\n const timeoutMillis = Math.min(validated.timeoutMillis, limits.timeoutMillis)\n\n return yield* Effect.gen(function*() {\n const response = yield* call({\n _tag: 'Revalidate',\n version: 1,\n access: bound,\n principal,\n lookup,\n limits,\n deadlineMillis: (yield* Clock.currentTimeMillis) + timeoutMillis,\n })\n\n if (response._tag !== 'Lookup') return yield* MemoryRpcError.make({ reason: 'protocol' })\n\n return response.lookup\n }).pipe((effect) => withinDeadline(effect, timeoutMillis))\n })\n\n const change = Effect.fnUntraced(function*(write: MemoryWrite<Namespace>) {\n if (!MemoryNamespace.equals(write.key.namespace, bound.namespace)) {\n return yield* MemoryRpcError.make({ reason: 'denied' })\n }\n\n return yield* Effect.gen(function*() {\n const response = yield* call({\n _tag: 'Change',\n version: 1,\n access: bound,\n principal,\n write,\n deadlineMillis: (yield* Clock.currentTimeMillis) + validated.timeoutMillis,\n })\n\n if (response._tag !== 'Changed' || response.document.key.id !== write.key.id) {\n return yield* MemoryRpcError.make({ reason: 'protocol' })\n }\n\n return yield* MemoryDocument.restore(access.namespace, response.document)\n }).pipe((effect) => withinDeadline(effect, validated.timeoutMillis))\n })\n\n /**\n * Read one exact current document in one owner RPC. Null means absent; withdrawals return\n * tombstones. Denial, unavailable storage and deadlines fail typed, never become absence.\n * Reads begun after an acknowledged write observe it or a later revision. The owner checks\n * exact-key authority and active document scopes; source-dependent provenance policy remains\n * application-owned. No extraction, job draining, embedding, discovery or rendering occurs.\n */\n const get = Effect.fnUntraced(function*(key: MemoryKey<Namespace>) {\n const decodedKey = yield* Schema.decodeUnknownEffect(MemoryKey.Wire)(key).pipe(\n Effect.mapError(() => MemoryRpcError.make({ reason: 'protocol' })),\n )\n\n if (!MemoryNamespace.equals(decodedKey.namespace, bound.namespace)) {\n return yield* MemoryRpcError.make({ reason: 'denied' })\n }\n\n return yield* Effect.gen(function*() {\n const response = yield* call({\n _tag: 'Get',\n version: 1,\n access: bound,\n principal,\n key: decodedKey,\n deadlineMillis: (yield* Clock.currentTimeMillis) + validated.timeoutMillis,\n })\n\n if (\n response._tag !== 'Document' ||\n !MemoryNamespace.equals(response.key.namespace, bound.namespace) ||\n response.key.id !== decodedKey.id\n ) {\n return yield* MemoryRpcError.make({ reason: 'protocol' })\n }\n if (response.document === null) return null\n if (\n response.document.key.id !== decodedKey.id ||\n response.document.source.id !== decodedKey.id ||\n (response.document._tag === 'ActiveMemoryDocument' &&\n !response.document.scopes.includes(bound.scope))\n ) {\n return yield* MemoryRpcError.make({ reason: 'protocol' })\n }\n\n yield* encodeMemoryWire(MemoryDocument.Wire, response.document, validated.maxSourceBytes)\n\n return yield* MemoryDocument.restore(access.namespace, response.document)\n }).pipe((effect) => withinDeadline(effect, validated.timeoutMillis))\n })\n\n const revalidateSemantic = Effect.fnUntraced(\n function*(\n found: MemoryIndexSearch<Namespace>,\n profile: SemanticMemoryProfile,\n limits: SemanticCandidateLimits,\n ) {\n const response = yield* call({\n _tag: 'RevalidateSemantic',\n version: 1,\n access: bound,\n principal,\n found,\n profile,\n limits,\n deadlineMillis: (yield* Clock.currentTimeMillis) + validated.timeoutMillis,\n })\n\n if (response._tag !== 'Semantic') return yield* MemoryRpcError.make({ reason: 'protocol' })\n\n return response.result\n },\n (effect) => withinDeadline(effect, validated.timeoutMillis),\n )\n\n /**\n * Revalidate in one owner RPC, then render whole passages within the caller's budget.\n * The bound source is essential: unavailable/stale results and matches that cannot fit\n * fail instead of silently producing empty context. No-match remains successful.\n * The single outcome has sourceId \"memory\". No embedding or candidate search is performed.\n * Use revalidate with Memory.recall for multiple readers sharing one output budget.\n */\n const recall = (\n lookup: MemoryLookup,\n limits: MemoryRecallLimits,\n estimateTokens?: (text: string) => number,\n ) =>\n Memory.recall(\n [{ id: 'memory', essential: true, read: revalidate(lookup, limits) }],\n limits,\n estimateTokens,\n )\n\n return { get, recall, revalidate, revalidateSemantic, change }\n})\n\nexport const CloudflareMemoryClient = {\n /** Bind access and principal using the MemoryObjectNamespace supplied by the application. */\n make: makeMemoryClient,\n /** Use a resolved Worker or Durable Object binding without manual service provisioning. */\n fromBinding: <Namespace extends MemoryNamespace.Any>(\n binding: DurableObjectNamespace<MemoryObjectRpc>,\n options: {\n readonly access: MemoryAccess<Namespace>\n readonly principal: Principal\n readonly rpcLimits?: MemoryRpcLimits\n },\n ) =>\n makeMemoryClient(options.access, options.principal, options.rpcLimits).pipe(\n Effect.provideService(MemoryObjectNamespace, { namespace: binding }),\n ),\n}\n\n/**\n * Optional activity-processor destination. Keeps domain write errors intact; transport,\n * authorization and deadline failures become the existing MemoryStorageError contract.\n * Receipts remain authoritative, including after caller interruption or lost replies.\n */\nexport const cloudflareMemoryWriterLayer = (\n access: MemoryAccess,\n principal: Principal,\n limits: MemoryRpcLimits = defaultMemoryRpcLimits,\n) =>\n Layer.effect(\n MemoryWriter,\n Effect.gen(function*() {\n const client = yield* CloudflareMemoryClient.make(access, principal, limits)\n\n return MemoryWriter.fromAdapter({\n change: (write) =>\n client.change(write).pipe(\n Effect.catchTag('MemoryRpcError', (error) =>\n Effect.fail(\n MemoryStorageError.make({\n operation: `memory RPC ${error.reason}`,\n reason: error.reason === 'unavailable' || error.reason === 'timeout'\n ? 'unavailable'\n : 'invalid-input',\n }),\n )),\n Effect.catchTag(['MemoryRecallError', 'MemoryIndexError', 'SemanticMemoryError'], () =>\n Effect.fail(\n MemoryStorageError.make({ operation: 'memory RPC response', reason: 'corrupt' }),\n )),\n ),\n })\n }),\n )\n\ntype OwnerServices = MemoryReader | MemoryWriter | MemoryOwnerAuthorizer | MemoryOwnerIdentity\n\nexport interface MemoryObjectInstance extends\n InstanceType<\n EffectCfDurableObject.DurableObjectClass<Record<never, never>, OwnerServices>\n >\n{\n memory(encoded: string): Promise<string>\n}\n\nexport interface MemoryObjectClass {\n new(ctx: globalThis.DurableObjectState, env: Cloudflare.Env): MemoryObjectInstance\n}\n\n/**\n * Dedicated SQLite owner, independent of Thread lifetimes. The host binds authorization\n * after restoring its namespace definition from MemoryOwnerIdentity. Do not retain\n * cleanup-scoped resources in the host Layer; it lives for the DO incarnation.\n */\nconst makeMemoryObject = <E>(\n host: Layer.Layer<\n MemoryOwnerAuthorizer,\n E,\n MemoryOwnerIdentity | DurableObjectState.DurableObjectState | WorkerEnvironment\n >,\n options: {\n readonly storageLimits?: DoMemoryStorageLimits\n readonly rpcLimits?: MemoryRpcLimits\n readonly failpoints?: Layer.Layer<\n MemoryMutationFailpoint,\n never,\n DurableObjectState.DurableObjectState\n >\n } = {},\n): MemoryObjectClass => {\n const identity = Layer.effect(\n MemoryOwnerIdentity,\n Effect.gen(function*() {\n const state = yield* DurableObjectState.DurableObjectState\n\n const address = yield* Schema.decodeUnknownEffect(MemoryNamespaceAddress)(\n state.raw.id.name,\n ).pipe(Effect.mapError(() => MemoryRpcError.make({ reason: 'denied' })))\n\n return { namespace: MemoryNamespace.Any.make({ address }) }\n }),\n )\n\n const store = Layer.unwrap(\n Effect.map(DurableObjectState.DurableObjectState, (state) =>\n doMemoryStoreLayerWithFailpoints(\n state.raw.storage,\n options.storageLimits ?? defaultDoMemoryStorageLimits,\n )),\n ).pipe(Layer.provide(options.failpoints ?? MemoryMutationFailpoint.layer))\n\n const application = Layer.merge(store, host).pipe(Layer.provideMerge(identity))\n\n const runtime: Layer.Layer<\n OwnerServices,\n E | MemoryOwnerFailure,\n DurableObjectState.DurableObjectState | WorkerEnvironment\n > = Layer.effectContext(\n Effect.gen(function*() {\n const state = yield* DurableObjectState.DurableObjectState\n const scope = yield* Effect.scope\n\n yield* Schema.decodeEffect(MemoryRpcLimits)(options.rpcLimits ?? defaultMemoryRpcLimits).pipe(\n Effect.mapError(() => MemoryRpcError.make({ reason: 'protocol' })),\n )\n\n return yield* state.blockConcurrencyWhile(Layer.buildWithScope(application, scope))\n }),\n )\n\n const rpc = { memory: (encoded: string) => handleMemoryOwnerRequest(encoded, options.rpcLimits) }\n\n return EffectCfDurableObject.make<\n OwnerServices,\n E | MemoryOwnerFailure,\n never,\n never,\n typeof rpc\n >(runtime, { rpc })\n}\n\nexport const MemoryObject = {\n /** Build the SQLite Durable Object class with the application's owner authorization Layer. */\n make: makeMemoryObject,\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAmDA,IAAa,wBAAb,cAA2C,QAAQ,QAKjD,CAAC,CAAC,yDAAyD,CAAC,CAAC,CAAC;;AAGhE,MAAa,oBAAoB,cAA2C,UAAU;;;;;;;AAQtF,MAAM,mBAAmB,OAAO,WAAW,WACzC,QACA,WACA,YAA6B,wBAC7B;CACA,MAAM,YAAY,OAAO,OAAO,aAAa,eAAe,CAAC,CAAC,SAAS,CAAC,CAAC,KACvE,OAAO,eAAe,eAAe,KAAK,EAAE,QAAQ,WAAW,CAAC,CAAC,CACnE;CAEA,MAAM,QAAQ,OAAO,OAAO,oBAAoB,aAAa,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,KACzE,OAAO,eAAe,eAAe,KAAK,EAAE,QAAQ,WAAW,CAAC,CAAC,CACnE;CAEA,YAAY,OAAO,OAAO,aAAa,SAAS,CAAC,CAAC,SAAS,CAAC,CAAC,KAC3D,OAAO,eAAe,eAAe,KAAK,EAAE,QAAQ,WAAW,CAAC,CAAC,CACnE;CACA,MAAM,EAAE,cAAc,OA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AC,CAAC;EAEvE,OAAO,EAAE,WAAW,gBAAgB,IAAI,KAAK,EAAE,QAAQ,CAAC,EAAE;CAC5D,CAAC,CACH;CAEA,MAAM,QAAQ,MAAM,OAClB,OAAO,IAAI,mBAAmB,qBAAqB,UACjD,iCACE,MAAM,IAAI,SACV,QAAQ,iBAAiB,4BAC3B,CAAC,CACL,CAAC,CAAC,KAAK,MAAM,QAAQ,QAAQ,cAAc,wBAAwB,KAAK,CAAC;CAEzE,MAAM,cAAc,MAAM,MAAM,OAAO,IAAI,CAAC,CAAC,KAAK,MAAM,aAAa,QAAQ,CAAC;CAE9E,MAAM,UAIF,MAAM,cACR,OAAO,IAAI,aAAY;EACrB,MAAM,QAAQ,OAAO,mBAAmB;EACxC,MAAM,QAAQ,OAAO,OAAO;EAE5B,OAAO,OAAO,aAAa,eAAe,CAAC,CAAC,QAAQ,aAAa,sBAAsB,CAAC,CAAC,KACvF,OAAO,eAAe,eAAe,KAAK,EAAE,QAAQ,WAAW,CAAC,CAAC,CACnE;EAEA,OAAO,OAAO,MAAM,sBAAsB,MAAM,eAAe,aAAa,KAAK,CAAC;CACpF,CAAC,CACH;CAIA,OAAOA,cAAsB,KAM3B,SAAS,EAAE,KAAA,EARC,SAAS,YAAoB,yBAAyB,SAAS,QAAQ,SAAS,EAQ/E,EAAE,CAAC;AACpB;AAEA,MAAa,eAAe;;AAE1B,MAAM,iBACR"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as MemoryObjectInstance, c as cloudflareMemoryWriterLayer, i as MemoryObjectClass, l as memoryObjectName, o as MemoryObjectNamespace, r as MemoryObject, s as MemoryObjectRpc, t as CloudflareMemoryClient } from "./CloudflareMemory-Bzp32ZpR.mjs";
|
|
2
|
+
export { CloudflareMemoryClient, MemoryObject, MemoryObjectClass, MemoryObjectInstance, MemoryObjectNamespace, MemoryObjectRpc, cloudflareMemoryWriterLayer, memoryObjectName };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { t as CloudflareMemoryClient, r as MemoryObject, i as MemoryObjectNamespace, a as cloudflareMemoryWriterLayer, o as memoryObjectName } from "./CloudflareMemory-DejIVKBT.mjs";
|
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-D3KUYPsf.mjs";
|
|
2
|
+
import * as SqliteClient from "@effect/sql-sqlite-do/SqliteClient";
|
|
3
|
+
import { DoScheduleAlarmControl, DoScheduleTransaction, scheduleStoreLayer } from "@systemfsoftware/agent-storage-cloudflare/do-schedule-store";
|
|
4
|
+
import "@systemfsoftware/agent/durable-agent-runtime";
|
|
5
|
+
import { AgentId } from "@systemfsoftware/agent/identifiers";
|
|
6
|
+
import { DefinitionDigests, PersistedJson } from "@systemfsoftware/agent/records";
|
|
7
|
+
import { ScheduleAuthorizationError, ScheduleCapacityError, ScheduleConflict, ScheduleDestination, ScheduleFailpointError, ScheduleId, ScheduleNotFound, ScheduleOwner, ScheduleScope, ScheduleSnapshot, ScheduleSnapshotPage, ScheduleStorageError, ScheduleTimingRequest, ScheduleValidationError, defaultSchedulingLimits } from "@systemfsoftware/agent/schedule";
|
|
8
|
+
import { scheduleOwnerKey } from "@systemfsoftware/agent/schedule-transition";
|
|
9
|
+
import { ScheduleDriver, ScheduleWakeNoop, Scheduling } from "@systemfsoftware/agent/scheduling";
|
|
10
|
+
import { AdmissionFence } from "@systemfsoftware/agent/submission-ledger";
|
|
11
|
+
import { DurableObject, DurableObjectAlarm, DurableObjectState, RpcTargets } from "effect-cf";
|
|
12
|
+
import * as Clock from "effect/Clock";
|
|
13
|
+
import * as Context from "effect/Context";
|
|
14
|
+
import * as SecureRandom from "effect/crypto/SecureRandom";
|
|
15
|
+
import * as DateTime from "effect/DateTime";
|
|
16
|
+
import * as Effect from "effect/Effect";
|
|
17
|
+
import * as Layer from "effect/Layer";
|
|
18
|
+
import * as Schema from "effect/Schema";
|
|
19
|
+
import { U as cloudflareCryptoLayer, o as CloudflareThreadClient } from "./CloudflareThreadClient-K7QJHYpK.mjs";
|
|
20
|
+
import { t as cloudflarePreparedInputAdmissionLayer } from "./prepared-admission-DngoYN74.mjs";
|
|
21
|
+
// src/CloudflareScheduling.ts
|
|
22
|
+
var CloudflareScheduling_exports = /* @__PURE__ */ __exportAll({
|
|
23
|
+
CloudflareSchedulingClient: () => CloudflareSchedulingClient,
|
|
24
|
+
ScheduleAlarmProtocolError: () => ScheduleAlarmProtocolError,
|
|
25
|
+
ScheduleOwnerIdentity: () => ScheduleOwnerIdentity,
|
|
26
|
+
ScheduleOwnerNamespace: () => ScheduleOwnerNamespace,
|
|
27
|
+
ScheduleOwnerProtocolError: () => ScheduleOwnerProtocolError,
|
|
28
|
+
makeScheduleOwnerObjectClass: () => makeScheduleOwnerObjectClass,
|
|
29
|
+
scheduleAlarmHandler: () => scheduleAlarmHandler
|
|
30
|
+
});
|
|
31
|
+
const SCHEDULE_ALARM_TAG = "effect-agent/ScheduleOwnerWake";
|
|
32
|
+
const SCHEDULE_ALARM_ID = "driver";
|
|
33
|
+
const ScheduleAlarmPayload = Schema.Struct({
|
|
34
|
+
schemaVersion: Schema.Literal(1),
|
|
35
|
+
generation: Schema.Int.check(Schema.isGreaterThan(0))
|
|
36
|
+
});
|
|
37
|
+
var ScheduleAlarmProtocolError = class extends Schema.TaggedError()("ScheduleAlarmProtocolError", { message: Schema.String }) {};
|
|
38
|
+
const boundedProtocolMessage = (message) => message.length <= 4096 ? message : `${message.slice(0, 4093)}...`;
|
|
39
|
+
var ScheduleOwnerProtocolError = class extends Schema.TaggedError()("ScheduleOwnerProtocolError", { message: Schema.String.check(Schema.isMaxLength(4096)) }) {};
|
|
40
|
+
const ScheduleMutationRequestFields = {
|
|
41
|
+
schemaVersion: Schema.Literal(1),
|
|
42
|
+
agentId: AgentId,
|
|
43
|
+
input: PersistedJson,
|
|
44
|
+
scope: ScheduleScope,
|
|
45
|
+
scheduleId: ScheduleId,
|
|
46
|
+
timing: ScheduleTimingRequest,
|
|
47
|
+
destination: ScheduleDestination,
|
|
48
|
+
deliveryPrincipal: ScheduleScope.fields.principal,
|
|
49
|
+
definitions: DefinitionDigests,
|
|
50
|
+
admissionGroup: Schema.optionalKey(Schema.NonEmptyString.check(Schema.isMaxLength(256))),
|
|
51
|
+
admissionFence: Schema.optionalKey(AdmissionFence)
|
|
52
|
+
};
|
|
53
|
+
const ScheduleCreateRequest = Schema.TaggedStruct("Create", ScheduleMutationRequestFields);
|
|
54
|
+
const ScheduleUpdateRequest = Schema.TaggedStruct("Update", {
|
|
55
|
+
...ScheduleMutationRequestFields,
|
|
56
|
+
expectedRevision: Schema.Int.check(Schema.isGreaterThan(0))
|
|
57
|
+
});
|
|
58
|
+
const ScheduleGetRequest = Schema.TaggedStruct("Get", {
|
|
59
|
+
schemaVersion: Schema.Literal(1),
|
|
60
|
+
scope: ScheduleScope,
|
|
61
|
+
scheduleId: ScheduleId
|
|
62
|
+
});
|
|
63
|
+
const ScheduleListRequest = Schema.TaggedStruct("List", {
|
|
64
|
+
schemaVersion: Schema.Literal(1),
|
|
65
|
+
scope: ScheduleScope,
|
|
66
|
+
after: Schema.optionalKey(ScheduleId),
|
|
67
|
+
limit: Schema.optionalKey(Schema.Int.check(Schema.isGreaterThan(0), Schema.isLessThanOrEqualTo(100)))
|
|
68
|
+
});
|
|
69
|
+
const ScheduleControlRequest = Schema.TaggedStruct("Control", {
|
|
70
|
+
schemaVersion: Schema.Literal(1),
|
|
71
|
+
operation: Schema.Literals([
|
|
72
|
+
"pause",
|
|
73
|
+
"resume",
|
|
74
|
+
"cancel"
|
|
75
|
+
]),
|
|
76
|
+
scope: ScheduleScope,
|
|
77
|
+
scheduleId: ScheduleId,
|
|
78
|
+
expectedRevision: Schema.Int.check(Schema.isGreaterThan(0))
|
|
79
|
+
});
|
|
80
|
+
const ScheduleRecoverRequest = Schema.TaggedStruct("Recover", {
|
|
81
|
+
schemaVersion: Schema.Literal(1),
|
|
82
|
+
scope: ScheduleScope,
|
|
83
|
+
scheduleId: ScheduleId,
|
|
84
|
+
expectedRevision: Schema.Int.check(Schema.isGreaterThan(0)),
|
|
85
|
+
expectedGeneration: Schema.Natural
|
|
86
|
+
});
|
|
87
|
+
const ScheduleOwnerRequest = Schema.Union([
|
|
88
|
+
ScheduleRecoverRequest,
|
|
89
|
+
ScheduleCreateRequest,
|
|
90
|
+
ScheduleUpdateRequest,
|
|
91
|
+
ScheduleGetRequest,
|
|
92
|
+
ScheduleListRequest,
|
|
93
|
+
ScheduleControlRequest
|
|
94
|
+
]);
|
|
95
|
+
const ScheduleOwnerFailure = Schema.Union([
|
|
96
|
+
ScheduleValidationError,
|
|
97
|
+
ScheduleAuthorizationError,
|
|
98
|
+
ScheduleConflict,
|
|
99
|
+
ScheduleNotFound,
|
|
100
|
+
ScheduleCapacityError,
|
|
101
|
+
ScheduleStorageError,
|
|
102
|
+
ScheduleFailpointError,
|
|
103
|
+
ScheduleOwnerProtocolError
|
|
104
|
+
]);
|
|
105
|
+
const ScheduleOwnerResponse = Schema.Union([
|
|
106
|
+
Schema.TaggedStruct("Snapshot", { value: ScheduleSnapshot }),
|
|
107
|
+
Schema.TaggedStruct("Page", { value: ScheduleSnapshotPage }),
|
|
108
|
+
Schema.TaggedStruct("Failed", { failure: ScheduleOwnerFailure })
|
|
109
|
+
]);
|
|
110
|
+
const decodeScheduleOwnerRequest = Schema.decodeUnknownEffect(ScheduleOwnerRequest);
|
|
111
|
+
const encodeScheduleOwnerRequest = Schema.encodeEffect(ScheduleOwnerRequest);
|
|
112
|
+
const decodeScheduleOwnerResponse = Schema.decodeUnknownEffect(ScheduleOwnerResponse);
|
|
113
|
+
const encodeScheduleOwnerResponse = Schema.encodeEffect(ScheduleOwnerResponse);
|
|
114
|
+
const scheduleProtocolFailure = (message) => ({
|
|
115
|
+
_tag: "Failed",
|
|
116
|
+
failure: ScheduleOwnerProtocolError.make({ message: boundedProtocolMessage(message) })
|
|
117
|
+
});
|
|
118
|
+
var ScheduleOwnerNamespace = class extends Context.Service()("@effect-agent/platform-cloudflare/ScheduleOwnerNamespace") {};
|
|
119
|
+
const passthroughAgent = (agentId) => ({ definition: {
|
|
120
|
+
id: agentId,
|
|
121
|
+
input: PersistedJson
|
|
122
|
+
} });
|
|
123
|
+
const requestOwner = (request) => request.scope.owner;
|
|
124
|
+
/** Provides the same authorized management service as NodeScheduling.layer. */
|
|
125
|
+
var CloudflareSchedulingClient = class {
|
|
126
|
+
static layer = Layer.effect(Scheduling, Effect.gen(function* () {
|
|
127
|
+
const { namespace } = yield* ScheduleOwnerNamespace;
|
|
128
|
+
const call = Effect.fnUntraced(function* (owner, request) {
|
|
129
|
+
const encoded = yield* encodeScheduleOwnerRequest(request).pipe(Effect.mapError(() => ScheduleStorageError.make({
|
|
130
|
+
operation: "Schedule Owner protocol",
|
|
131
|
+
reason: "corrupt"
|
|
132
|
+
})));
|
|
133
|
+
const unavailable = (cause) => {
|
|
134
|
+
const error = ScheduleStorageError.make({
|
|
135
|
+
operation: "call Schedule Owner",
|
|
136
|
+
reason: "unavailable"
|
|
137
|
+
});
|
|
138
|
+
error.cause = cause;
|
|
139
|
+
return error;
|
|
140
|
+
};
|
|
141
|
+
const address = scheduleOwnerKey(owner);
|
|
142
|
+
const target = yield* RpcTargets.get(namespace, address, () => namespace.get(namespace.idFromName(address))).pipe(Effect.mapError(unavailable));
|
|
143
|
+
const raw = yield* Effect.tryPromise({
|
|
144
|
+
try: () => target.schedule(encoded),
|
|
145
|
+
catch: unavailable
|
|
146
|
+
}).pipe(Effect.tapCause(() => RpcTargets.invalidate(target)));
|
|
147
|
+
const response = yield* decodeScheduleOwnerResponse(raw).pipe(Effect.mapError(() => ScheduleStorageError.make({
|
|
148
|
+
operation: "Schedule Owner protocol",
|
|
149
|
+
reason: "corrupt"
|
|
150
|
+
})));
|
|
151
|
+
if (response._tag !== "Failed") return response;
|
|
152
|
+
return yield* response.failure._tag === "ScheduleOwnerProtocolError" ? ScheduleStorageError.make({
|
|
153
|
+
operation: "Schedule Owner protocol",
|
|
154
|
+
reason: "corrupt"
|
|
155
|
+
}) : response.failure;
|
|
156
|
+
});
|
|
157
|
+
const encodeInput = Effect.fnUntraced(function* (agent, input) {
|
|
158
|
+
const encoded = yield* Schema.encodeEffect(agent.definition.input)(input).pipe(Effect.mapError(() => ScheduleValidationError.make({ message: "Unable to encode Agent input" })));
|
|
159
|
+
return yield* Schema.decodeUnknownEffect(PersistedJson)(encoded).pipe(Effect.mapError(() => ScheduleValidationError.make({ message: "Agent input does not satisfy the canonical persistence bounds" })));
|
|
160
|
+
});
|
|
161
|
+
const create = Effect.fnUntraced(function* (agent, input, options) {
|
|
162
|
+
const payload = yield* encodeInput(agent, input);
|
|
163
|
+
const response = yield* call(options.scope.owner, {
|
|
164
|
+
_tag: "Create",
|
|
165
|
+
schemaVersion: 1,
|
|
166
|
+
agentId: agent.definition.id,
|
|
167
|
+
input: payload,
|
|
168
|
+
...options
|
|
169
|
+
});
|
|
170
|
+
return response._tag === "Snapshot" ? response.value : yield* ScheduleStorageError.make({
|
|
171
|
+
operation: "Schedule Owner protocol",
|
|
172
|
+
reason: "corrupt"
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
const update = Effect.fnUntraced(function* (agent, input, options) {
|
|
176
|
+
const payload = yield* encodeInput(agent, input);
|
|
177
|
+
const response = yield* call(options.scope.owner, {
|
|
178
|
+
_tag: "Update",
|
|
179
|
+
schemaVersion: 1,
|
|
180
|
+
agentId: agent.definition.id,
|
|
181
|
+
input: payload,
|
|
182
|
+
...options
|
|
183
|
+
});
|
|
184
|
+
return response._tag === "Snapshot" ? response.value : yield* ScheduleStorageError.make({
|
|
185
|
+
operation: "Schedule Owner protocol",
|
|
186
|
+
reason: "corrupt"
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
const get = Effect.fnUntraced(function* (scope, scheduleId) {
|
|
190
|
+
const response = yield* call(scope.owner, {
|
|
191
|
+
_tag: "Get",
|
|
192
|
+
schemaVersion: 1,
|
|
193
|
+
scope,
|
|
194
|
+
scheduleId
|
|
195
|
+
});
|
|
196
|
+
return response._tag === "Snapshot" ? response.value : yield* ScheduleStorageError.make({
|
|
197
|
+
operation: "Schedule Owner protocol",
|
|
198
|
+
reason: "corrupt"
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
const list = Effect.fnUntraced(function* (scope, options = {}) {
|
|
202
|
+
const response = yield* call(scope.owner, {
|
|
203
|
+
_tag: "List",
|
|
204
|
+
schemaVersion: 1,
|
|
205
|
+
scope,
|
|
206
|
+
...options.after === void 0 ? {} : { after: options.after },
|
|
207
|
+
...options.limit === void 0 ? {} : { limit: options.limit }
|
|
208
|
+
});
|
|
209
|
+
return response._tag === "Page" ? response.value : yield* ScheduleStorageError.make({
|
|
210
|
+
operation: "Schedule Owner protocol",
|
|
211
|
+
reason: "corrupt"
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
const control = Effect.fnUntraced(function* (operation, scope, scheduleId, expectedRevision) {
|
|
215
|
+
const response = yield* call(scope.owner, {
|
|
216
|
+
_tag: "Control",
|
|
217
|
+
schemaVersion: 1,
|
|
218
|
+
operation,
|
|
219
|
+
scope,
|
|
220
|
+
scheduleId,
|
|
221
|
+
expectedRevision
|
|
222
|
+
});
|
|
223
|
+
return response._tag === "Snapshot" ? response.value : yield* ScheduleStorageError.make({
|
|
224
|
+
operation: "Schedule Owner protocol",
|
|
225
|
+
reason: "corrupt"
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
return Scheduling.of({
|
|
229
|
+
create,
|
|
230
|
+
update,
|
|
231
|
+
get,
|
|
232
|
+
list,
|
|
233
|
+
pause: (scope, id, revision) => control("pause", scope, id, revision),
|
|
234
|
+
resume: (scope, id, revision) => control("resume", scope, id, revision),
|
|
235
|
+
recover: Effect.fnUntraced(function* (scope, scheduleId, expectedRevision, expectedGeneration) {
|
|
236
|
+
const response = yield* call(scope.owner, {
|
|
237
|
+
_tag: "Recover",
|
|
238
|
+
schemaVersion: 1,
|
|
239
|
+
scope,
|
|
240
|
+
scheduleId,
|
|
241
|
+
expectedRevision,
|
|
242
|
+
expectedGeneration
|
|
243
|
+
});
|
|
244
|
+
return response._tag === "Snapshot" ? response.value : yield* ScheduleStorageError.make({
|
|
245
|
+
operation: "Schedule Owner protocol",
|
|
246
|
+
reason: "corrupt"
|
|
247
|
+
});
|
|
248
|
+
}),
|
|
249
|
+
cancel: (scope, id, revision) => control("cancel", scope, id, revision)
|
|
250
|
+
});
|
|
251
|
+
}));
|
|
252
|
+
};
|
|
253
|
+
var ScheduleOwnerIdentity = class extends Context.Service()("@effect-agent/platform-cloudflare/ScheduleOwnerIdentity") {};
|
|
254
|
+
const decodeOwnerName = Effect.fnUntraced(function* (name) {
|
|
255
|
+
if (name === null || name === void 0) return yield* ScheduleOwnerProtocolError.make({ message: "Schedule Owner objects require an idFromName identity" });
|
|
256
|
+
const tuple = yield* Schema.decodeEffect(Schema.fromJsonString(Schema.Tuple([Schema.String, Schema.String])))(name).pipe(Effect.mapError(() => ScheduleOwnerProtocolError.make({ message: "Schedule Owner object name is malformed" })));
|
|
257
|
+
return yield* Schema.decodeEffect(ScheduleOwner)({
|
|
258
|
+
tenantId: tuple[0],
|
|
259
|
+
ownerId: tuple[1]
|
|
260
|
+
}).pipe(Effect.mapError(() => ScheduleOwnerProtocolError.make({ message: "Schedule Owner object identity is invalid" })));
|
|
261
|
+
});
|
|
262
|
+
const alarmStorageError = (operation) => (error) => ScheduleStorageError.make({
|
|
263
|
+
operation,
|
|
264
|
+
reason: error._tag === "StorageOperationError" ? "unavailable" : "corrupt"
|
|
265
|
+
});
|
|
266
|
+
const transactionLayer = Layer.effect(DoScheduleTransaction, Effect.gen(function* () {
|
|
267
|
+
const alarms = yield* DurableObjectAlarm.DurableObjectAlarm;
|
|
268
|
+
const random = yield* SecureRandom.SecureRandom;
|
|
269
|
+
return DoScheduleTransaction.of({ run: Effect.fnUntraced(function* (body) {
|
|
270
|
+
const nowMillis = yield* Clock.currentTimeMillis;
|
|
271
|
+
return yield* alarms.transaction((transaction) => body((replacement) => replacement.deadlineAtMillis === null ? transaction.cancelAlarm({
|
|
272
|
+
id: SCHEDULE_ALARM_ID,
|
|
273
|
+
tag: SCHEDULE_ALARM_TAG
|
|
274
|
+
}).pipe(Effect.mapError(alarmStorageError("cancel Schedule Owner alarm"))) : Effect.fromOption(DateTime.make(Math.max(replacement.deadlineAtMillis, nowMillis + 1))).pipe(Effect.mapError(() => ScheduleStorageError.make({
|
|
275
|
+
operation: "validate Schedule Owner alarm deadline",
|
|
276
|
+
reason: "corrupt"
|
|
277
|
+
})), Effect.flatMap((runAt) => transaction.scheduleAlarm({
|
|
278
|
+
id: SCHEDULE_ALARM_ID,
|
|
279
|
+
tag: SCHEDULE_ALARM_TAG,
|
|
280
|
+
runAt,
|
|
281
|
+
payload: {
|
|
282
|
+
schemaVersion: 1,
|
|
283
|
+
generation: replacement.generation
|
|
284
|
+
}
|
|
285
|
+
}).pipe(Effect.provideService(SecureRandom.SecureRandom, random), Effect.mapError(alarmStorageError("schedule Schedule Owner alarm"))))))).pipe(Effect.catchTag("StorageOperationError", () => ScheduleStorageError.make({
|
|
286
|
+
operation: "commit Schedule Owner transaction",
|
|
287
|
+
reason: "unavailable"
|
|
288
|
+
})));
|
|
289
|
+
}) });
|
|
290
|
+
}));
|
|
291
|
+
const ensureOwner = (expected, request) => {
|
|
292
|
+
const observed = requestOwner(request);
|
|
293
|
+
return observed.tenantId === expected.tenantId && observed.ownerId === expected.ownerId ? Effect.void : Effect.fail(ScheduleOwnerProtocolError.make({ message: "The request owner does not match the addressed Schedule Owner object" }));
|
|
294
|
+
};
|
|
295
|
+
const handleScheduleRequest = Effect.fnUntraced(function* (encoded) {
|
|
296
|
+
const decoded = yield* decodeScheduleOwnerRequest(encoded).pipe(Effect.result);
|
|
297
|
+
if (decoded._tag === "Failure") return yield* encodeScheduleOwnerResponse(scheduleProtocolFailure("The Schedule request could not be decoded")).pipe(Effect.orDie);
|
|
298
|
+
const request = decoded.success;
|
|
299
|
+
const { owner } = yield* ScheduleOwnerIdentity;
|
|
300
|
+
const scheduling = yield* Scheduling;
|
|
301
|
+
const response = yield* Effect.gen(function* () {
|
|
302
|
+
yield* ensureOwner(owner, request);
|
|
303
|
+
switch (request._tag) {
|
|
304
|
+
case "Create": return {
|
|
305
|
+
_tag: "Snapshot",
|
|
306
|
+
value: yield* scheduling.create(passthroughAgent(request.agentId), request.input, {
|
|
307
|
+
scope: request.scope,
|
|
308
|
+
scheduleId: request.scheduleId,
|
|
309
|
+
timing: request.timing,
|
|
310
|
+
destination: request.destination,
|
|
311
|
+
deliveryPrincipal: request.deliveryPrincipal,
|
|
312
|
+
definitions: request.definitions,
|
|
313
|
+
...request.admissionGroup === void 0 ? {} : { admissionGroup: request.admissionGroup },
|
|
314
|
+
...request.admissionFence === void 0 ? {} : { admissionFence: request.admissionFence }
|
|
315
|
+
})
|
|
316
|
+
};
|
|
317
|
+
case "Update": return {
|
|
318
|
+
_tag: "Snapshot",
|
|
319
|
+
value: yield* scheduling.update(passthroughAgent(request.agentId), request.input, {
|
|
320
|
+
scope: request.scope,
|
|
321
|
+
scheduleId: request.scheduleId,
|
|
322
|
+
timing: request.timing,
|
|
323
|
+
destination: request.destination,
|
|
324
|
+
deliveryPrincipal: request.deliveryPrincipal,
|
|
325
|
+
definitions: request.definitions,
|
|
326
|
+
...request.admissionGroup === void 0 ? {} : { admissionGroup: request.admissionGroup },
|
|
327
|
+
...request.admissionFence === void 0 ? {} : { admissionFence: request.admissionFence },
|
|
328
|
+
expectedRevision: request.expectedRevision
|
|
329
|
+
})
|
|
330
|
+
};
|
|
331
|
+
case "Get": return {
|
|
332
|
+
_tag: "Snapshot",
|
|
333
|
+
value: yield* scheduling.get(request.scope, request.scheduleId)
|
|
334
|
+
};
|
|
335
|
+
case "List": return {
|
|
336
|
+
_tag: "Page",
|
|
337
|
+
value: yield* scheduling.list(request.scope, {
|
|
338
|
+
...request.after === void 0 ? {} : { after: request.after },
|
|
339
|
+
...request.limit === void 0 ? {} : { limit: request.limit }
|
|
340
|
+
})
|
|
341
|
+
};
|
|
342
|
+
case "Recover": return {
|
|
343
|
+
_tag: "Snapshot",
|
|
344
|
+
value: yield* scheduling.recover(request.scope, request.scheduleId, request.expectedRevision, request.expectedGeneration)
|
|
345
|
+
};
|
|
346
|
+
case "Control": return {
|
|
347
|
+
_tag: "Snapshot",
|
|
348
|
+
value: request.operation === "pause" ? yield* scheduling.pause(request.scope, request.scheduleId, request.expectedRevision) : request.operation === "resume" ? yield* scheduling.resume(request.scope, request.scheduleId, request.expectedRevision) : yield* scheduling.cancel(request.scope, request.scheduleId, request.expectedRevision)
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
}).pipe(Effect.map((value) => value), Effect.catch((failure) => Schema.is(ScheduleOwnerFailure)(failure) ? Effect.succeed({
|
|
352
|
+
_tag: "Failed",
|
|
353
|
+
failure
|
|
354
|
+
}) : Effect.succeed(scheduleProtocolFailure("The Schedule operation failed outside its public contract"))));
|
|
355
|
+
return yield* encodeScheduleOwnerResponse(response).pipe(Effect.orDie);
|
|
356
|
+
});
|
|
357
|
+
/** @internal The complete native alarm operation, including its event deadline. */
|
|
358
|
+
const scheduleAlarmHandler = (limits) => DurableObjectAlarm.processDue((event) => Effect.gen(function* () {
|
|
359
|
+
if (event.tag !== SCHEDULE_ALARM_TAG || event.id !== SCHEDULE_ALARM_ID) return yield* ScheduleAlarmProtocolError.make({ message: `Unsupported Schedule Owner alarm ${event.tag}/${event.id}` });
|
|
360
|
+
yield* Schema.decodeUnknownEffect(ScheduleAlarmPayload)(event.payload).pipe(Effect.mapError(() => ScheduleAlarmProtocolError.make({ message: "Unsupported Schedule Owner alarm payload version" })));
|
|
361
|
+
const scheduling = yield* ScheduleDriver;
|
|
362
|
+
const alarmControl = yield* DoScheduleAlarmControl;
|
|
363
|
+
const { owner } = yield* ScheduleOwnerIdentity;
|
|
364
|
+
const nowMillis = yield* Clock.currentTimeMillis;
|
|
365
|
+
yield* alarmControl.prearm(nowMillis + limits.recoveryPollMillis);
|
|
366
|
+
if ((yield* scheduling.runDue(owner)).failed > 0) yield* alarmControl.prearm((yield* Clock.currentTimeMillis) + limits.recoveryPollMillis);
|
|
367
|
+
else yield* alarmControl.reconcile;
|
|
368
|
+
})).pipe(Effect.timeout("14 minutes"), Effect.asVoid);
|
|
369
|
+
/**
|
|
370
|
+
* The host Layer supplies authorization and routing and is cached for the object incarnation.
|
|
371
|
+
* Cloudflare eviction does not guarantee its finalizers run. Do not acquire resources requiring
|
|
372
|
+
* cleanup in this Layer; acquire them inside scoped `manage` / `prepare` operations instead.
|
|
373
|
+
* Native services belong to effect-cf; the database and alarm runtime remain instance-owned.
|
|
374
|
+
*/
|
|
375
|
+
const makeScheduleOwnerObjectClass = (host, limits = defaultSchedulingLimits) => {
|
|
376
|
+
const ownerLayer = Layer.effect(ScheduleOwnerIdentity, Effect.gen(function* () {
|
|
377
|
+
const state = yield* DurableObjectState.DurableObjectState;
|
|
378
|
+
return ScheduleOwnerIdentity.of({ owner: yield* decodeOwnerName(state.raw.id.name) });
|
|
379
|
+
}));
|
|
380
|
+
const sqlLayer = Layer.unwrap(Effect.map(DurableObjectState.DurableObjectState, (state) => SqliteClient.layer({ storage: state.raw.storage })));
|
|
381
|
+
const application = Layer.merge(Scheduling.layer(limits), ScheduleDriver.layer(limits)).pipe(Layer.provideMerge(scheduleStoreLayer.pipe(Layer.provide(transactionLayer), Layer.provide(sqlLayer))), Layer.provide(cloudflarePreparedInputAdmissionLayer.pipe(Layer.provide(CloudflareThreadClient.layer))), Layer.provide(ScheduleWakeNoop), Layer.provideMerge(DurableObjectAlarm.DurableObjectAlarm.layer), Layer.provide(host), Layer.provideMerge(ownerLayer), Layer.provide(cloudflareCryptoLayer));
|
|
382
|
+
const runtime = Layer.effectContext(Effect.gen(function* () {
|
|
383
|
+
const state = yield* DurableObjectState.DurableObjectState;
|
|
384
|
+
const scope = yield* Effect.scope;
|
|
385
|
+
return yield* state.blockConcurrencyWhile(Layer.buildWithScope(application, scope));
|
|
386
|
+
}));
|
|
387
|
+
return DurableObject.make(runtime, {
|
|
388
|
+
initialize: Effect.void,
|
|
389
|
+
rpc: { schedule: (encoded) => handleScheduleRequest(encoded) },
|
|
390
|
+
alarms: scheduleAlarmHandler(limits)
|
|
391
|
+
});
|
|
392
|
+
};
|
|
393
|
+
export { ScheduleOwnerNamespace as a, scheduleAlarmHandler as c, ScheduleOwnerIdentity as i, CloudflareScheduling_exports as n, ScheduleOwnerProtocolError as o, ScheduleAlarmProtocolError as r, makeScheduleOwnerObjectClass as s, CloudflareSchedulingClient as t };
|
|
394
|
+
|
|
395
|
+
//# sourceMappingURL=CloudflareScheduling-BleqfC44.mjs.map
|