@effect-agent/storage-cloudflare 0.1.0-beta.45 → 0.1.0-beta.46
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/dist/DoMemoryStore.d.mts +24 -0
- package/dist/DoMemoryStore.mjs +50 -0
- package/dist/DoMemoryStore.mjs.map +1 -0
- package/dist/DoScheduleStore.d.mts +36 -0
- package/dist/DoScheduleStore.mjs +369 -0
- package/dist/DoScheduleStore.mjs.map +1 -0
- package/dist/DoStorageConfig.d.mts +45 -0
- package/dist/DoStorageConfig.mjs +52 -0
- package/dist/DoStorageConfig.mjs.map +1 -0
- package/dist/{do-storage-failpoint-9XB9tuif.d.mts → DoStorageError-Qwybzk0O.d.mts} +6 -15
- package/dist/DoStorageError.d.mts +2 -0
- package/dist/DoStorageError.mjs +158 -0
- package/dist/DoStorageError.mjs.map +1 -0
- package/dist/DoStorageFailpoint.d.mts +17 -0
- package/dist/{do-storage-failpoint-C-Qtchs7.mjs → DoStorageFailpoint.mjs} +5 -3
- package/dist/DoStorageFailpoint.mjs.map +1 -0
- package/dist/{testing.d.mts → DoStorageFailpointTesting.d.mts} +4 -3
- package/dist/{testing.mjs → DoStorageFailpointTesting.mjs} +3 -3
- package/dist/DoStorageFailpointTesting.mjs.map +1 -0
- package/dist/DoStorageVersion-KrYUXqeJ.d.mts +10 -0
- package/dist/DoStorageVersion.d.mts +2 -0
- package/dist/DoStorageVersion.mjs +8 -0
- package/dist/DoStorageVersion.mjs.map +1 -0
- package/dist/DoSubmissionLedger.d.mts +23 -0
- package/dist/DoSubmissionLedger.mjs +1716 -0
- package/dist/DoSubmissionLedger.mjs.map +1 -0
- package/dist/DoSubscriptionStore.d.mts +25 -0
- package/dist/DoSubscriptionStore.mjs +720 -0
- package/dist/DoSubscriptionStore.mjs.map +1 -0
- package/dist/DoThreadStore-D9ebIYPv.mjs +1083 -0
- package/dist/DoThreadStore-D9ebIYPv.mjs.map +1 -0
- package/dist/DoThreadStore.d.mts +59 -0
- package/dist/DoThreadStore.mjs +5 -0
- package/dist/MemoryProtocol.d.mts +19796 -0
- package/dist/MemoryProtocol.mjs +182 -0
- package/dist/MemoryProtocol.mjs.map +1 -0
- package/dist/PortProtocol.d.mts +1587 -0
- package/dist/PortProtocol.mjs +175 -0
- package/dist/PortProtocol.mjs.map +1 -0
- package/dist/PortRouting.d.mts +82 -0
- package/dist/PortRouting.mjs +404 -0
- package/dist/PortRouting.mjs.map +1 -0
- package/dist/index.d.mts +13 -21619
- package/dist/index.mjs +13 -4954
- package/dist/migrations-Bo4GU-pp.mjs +236 -0
- package/dist/migrations-Bo4GU-pp.mjs.map +1 -0
- package/dist/rolldown-runtime-D7D4PA-g.mjs +13 -0
- package/package.json +1 -1
- package/src/{do-memory-store.ts → DoMemoryStore.ts} +5 -2
- package/src/{do-schedule-store.ts → DoScheduleStore.ts} +6 -4
- package/src/{errors.ts → DoStorageError.ts} +1 -1
- package/src/{do-storage-failpoint.ts → DoStorageFailpoint.ts} +1 -1
- package/src/{do-storage-failpoint-testing.ts → DoStorageFailpointTesting.ts} +2 -2
- package/src/DoStorageVersion.ts +2 -0
- package/src/{do-ledger.ts → DoSubmissionLedger.ts} +22 -20
- package/src/{do-subscription-store.ts → DoSubscriptionStore.ts} +10 -8
- package/src/{do-thread-store.ts → DoThreadStore.ts} +19 -21
- package/src/{memory-protocol.ts → MemoryProtocol.ts} +15 -10
- package/src/{port-protocol.ts → PortProtocol.ts} +13 -11
- package/src/{routing.ts → PortRouting.ts} +10 -8
- package/src/index.ts +12 -36
- package/src/{do-journal.ts → internal/do-journal.ts} +2 -2
- package/dist/do-storage-failpoint-C-Qtchs7.mjs.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/testing.mjs.map +0 -1
- package/src/testing.ts +0 -2
- /package/src/{do-storage-config.ts → DoStorageConfig.ts} +0 -0
- /package/src/{migrations.ts → internal/migrations.ts} +0 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
|
|
2
|
+
import { MemoryConflict, MemoryDocument, MemoryMutationFailure, MemoryOperationConflict, MemoryStorageError, MemoryWithdrawn, MemoryWrite, MemoryWriter } from "@effect-agent/core/MemoryStore";
|
|
3
|
+
import { Clock, Context, Effect, Encoding, Schema } from "effect";
|
|
4
|
+
import { Principal } from "@effect-agent/thread/SubmissionLedger";
|
|
5
|
+
import * as MemoryNamespace from "@effect-agent/core/MemoryNamespace";
|
|
6
|
+
import { MemoryLookup, MemoryRecallError, MemoryRecallLimits } from "@effect-agent/core/MemoryReference";
|
|
7
|
+
import { MemoryAccess, revalidateMemoryLookup } from "@effect-agent/core/MemoryRevalidation";
|
|
8
|
+
import { MemoryIndexError, MemoryIndexSearch, SemanticMemoryProfile } from "@effect-agent/core/SemanticMemoryIndex";
|
|
9
|
+
import { SemanticCandidateLimits, SemanticCandidateResult, SemanticMemoryError, revalidateSemanticMemoryCandidates } from "@effect-agent/core/SemanticMemoryRevalidation";
|
|
10
|
+
//#region src/MemoryProtocol.ts
|
|
11
|
+
var MemoryProtocol_exports = /* @__PURE__ */ __exportAll({
|
|
12
|
+
MemoryOwnerAuthorizer: () => MemoryOwnerAuthorizer,
|
|
13
|
+
MemoryOwnerFailure: () => MemoryOwnerFailure,
|
|
14
|
+
MemoryOwnerIdentity: () => MemoryOwnerIdentity,
|
|
15
|
+
MemoryOwnerRequest: () => MemoryOwnerRequest,
|
|
16
|
+
MemoryOwnerResponse: () => MemoryOwnerResponse,
|
|
17
|
+
MemoryRpcError: () => MemoryRpcError,
|
|
18
|
+
MemoryRpcLimits: () => MemoryRpcLimits,
|
|
19
|
+
decodeMemoryWire: () => decodeMemoryWire,
|
|
20
|
+
defaultMemoryRpcLimits: () => defaultMemoryRpcLimits,
|
|
21
|
+
encodeMemoryWire: () => encodeMemoryWire,
|
|
22
|
+
handleMemoryOwnerRequest: () => handleMemoryOwnerRequest,
|
|
23
|
+
memoryWireBytes: () => memoryWireBytes
|
|
24
|
+
});
|
|
25
|
+
var MemoryRpcError = class extends Schema.TaggedError()("MemoryRpcError", { reason: Schema.Literals([
|
|
26
|
+
"denied",
|
|
27
|
+
"protocol",
|
|
28
|
+
"budget",
|
|
29
|
+
"timeout",
|
|
30
|
+
"unavailable"
|
|
31
|
+
]) }) {};
|
|
32
|
+
var MemoryRpcLimits = class extends Schema.Class("@effect-agent/storage-cloudflare/MemoryRpcLimits")({
|
|
33
|
+
maxRequestBytes: Schema.Int.check(Schema.isBetween({
|
|
34
|
+
minimum: 256,
|
|
35
|
+
maximum: 4194304
|
|
36
|
+
})),
|
|
37
|
+
maxResponseBytes: Schema.Int.check(Schema.isBetween({
|
|
38
|
+
minimum: 256,
|
|
39
|
+
maximum: 16777216
|
|
40
|
+
})),
|
|
41
|
+
maxSourceBytes: Schema.Int.check(Schema.isBetween({
|
|
42
|
+
minimum: 1,
|
|
43
|
+
maximum: 67108864
|
|
44
|
+
})),
|
|
45
|
+
maxSources: MemoryRecallLimits.fields.maxSources,
|
|
46
|
+
timeoutMillis: MemoryRecallLimits.fields.timeoutMillis
|
|
47
|
+
}) {};
|
|
48
|
+
const defaultMemoryRpcLimits = MemoryRpcLimits.make({
|
|
49
|
+
maxRequestBytes: 1048576,
|
|
50
|
+
maxResponseBytes: 4194304,
|
|
51
|
+
maxSourceBytes: 16777216,
|
|
52
|
+
maxSources: 16,
|
|
53
|
+
timeoutMillis: 1e4
|
|
54
|
+
});
|
|
55
|
+
const RequestFields = {
|
|
56
|
+
version: Schema.Literal(1),
|
|
57
|
+
access: MemoryAccess.Wire,
|
|
58
|
+
/** Host-authenticated identity, never copied from model input. The owner still authorizes it. */
|
|
59
|
+
principal: Principal,
|
|
60
|
+
deadlineMillis: Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0))
|
|
61
|
+
};
|
|
62
|
+
const RevalidateRequest = Schema.TaggedStruct("Revalidate", {
|
|
63
|
+
...RequestFields,
|
|
64
|
+
lookup: MemoryLookup,
|
|
65
|
+
limits: MemoryRecallLimits
|
|
66
|
+
});
|
|
67
|
+
const ChangeRequest = Schema.TaggedStruct("Change", {
|
|
68
|
+
...RequestFields,
|
|
69
|
+
write: MemoryWrite.Wire
|
|
70
|
+
});
|
|
71
|
+
const SemanticRequest = Schema.TaggedStruct("RevalidateSemantic", {
|
|
72
|
+
...RequestFields,
|
|
73
|
+
found: MemoryIndexSearch.Wire,
|
|
74
|
+
profile: SemanticMemoryProfile,
|
|
75
|
+
limits: SemanticCandidateLimits
|
|
76
|
+
});
|
|
77
|
+
const MemoryOwnerRequest = Schema.Union([
|
|
78
|
+
RevalidateRequest,
|
|
79
|
+
ChangeRequest,
|
|
80
|
+
SemanticRequest
|
|
81
|
+
]);
|
|
82
|
+
const MemoryOwnerFailure = Schema.Union([
|
|
83
|
+
MemoryRpcError,
|
|
84
|
+
MemoryStorageError,
|
|
85
|
+
MemoryRecallError,
|
|
86
|
+
MemoryConflict,
|
|
87
|
+
MemoryWithdrawn,
|
|
88
|
+
MemoryOperationConflict,
|
|
89
|
+
MemoryMutationFailure,
|
|
90
|
+
MemoryIndexError,
|
|
91
|
+
SemanticMemoryError
|
|
92
|
+
]);
|
|
93
|
+
const MemoryOwnerResponse = Schema.Union([
|
|
94
|
+
Schema.TaggedStruct("Lookup", {
|
|
95
|
+
access: MemoryAccess.Wire,
|
|
96
|
+
lookup: MemoryLookup
|
|
97
|
+
}),
|
|
98
|
+
Schema.TaggedStruct("Changed", {
|
|
99
|
+
access: MemoryAccess.Wire,
|
|
100
|
+
document: MemoryDocument.Wire
|
|
101
|
+
}),
|
|
102
|
+
Schema.TaggedStruct("Semantic", {
|
|
103
|
+
access: MemoryAccess.Wire,
|
|
104
|
+
result: SemanticCandidateResult
|
|
105
|
+
}),
|
|
106
|
+
Schema.TaggedStruct("Failed", { failure: MemoryOwnerFailure })
|
|
107
|
+
]);
|
|
108
|
+
/** Fail-closed application policy. Authorize the namespace, principal, scope, and full command. */
|
|
109
|
+
var MemoryOwnerAuthorizer = class extends Context.Service()("@effect-agent/storage-cloudflare/MemoryOwnerAuthorizer") {};
|
|
110
|
+
/** Canonical namespace derived from this object's idFromName identity, never its request. */
|
|
111
|
+
var MemoryOwnerIdentity = class extends Context.Service()("@effect-agent/storage-cloudflare/MemoryOwnerIdentity") {};
|
|
112
|
+
const memoryWireBytes = (text) => Encoding.encodeHex(text).length / 2;
|
|
113
|
+
const decodeMemoryWire = Effect.fn("decodeMemoryWire")(function* (schema, raw, maxBytes) {
|
|
114
|
+
const text = yield* Schema.decodeUnknownEffect(Schema.String)(raw).pipe(Effect.mapError(() => MemoryRpcError.make({ reason: "protocol" })));
|
|
115
|
+
if (text.length > maxBytes || memoryWireBytes(text) > maxBytes) return yield* MemoryRpcError.make({ reason: "budget" });
|
|
116
|
+
return yield* Schema.decodeEffect(Schema.fromJsonString(schema))(text).pipe(Effect.mapError(() => MemoryRpcError.make({ reason: "protocol" })));
|
|
117
|
+
});
|
|
118
|
+
const encodeMemoryWire = Effect.fn("encodeMemoryWire")(function* (schema, value, maxBytes) {
|
|
119
|
+
const encoded = yield* Schema.encodeEffect(Schema.fromJsonString(schema))(value).pipe(Effect.mapError(() => MemoryRpcError.make({ reason: "protocol" })));
|
|
120
|
+
if (encoded.length > maxBytes || memoryWireBytes(encoded) > maxBytes) return yield* MemoryRpcError.make({ reason: "budget" });
|
|
121
|
+
return encoded;
|
|
122
|
+
});
|
|
123
|
+
/** One local read per distinct candidate source, with no per-document network calls. */
|
|
124
|
+
const handleMemoryOwnerRequest = Effect.fn("MemoryOwner.handleRequest")(function* (raw, limits = defaultMemoryRpcLimits) {
|
|
125
|
+
const result = yield* Effect.gen(function* () {
|
|
126
|
+
limits = yield* Schema.decodeUnknownEffect(MemoryRpcLimits)(limits).pipe(Effect.mapError(() => MemoryRpcError.make({ reason: "protocol" })));
|
|
127
|
+
const request = yield* decodeMemoryWire(MemoryOwnerRequest, raw, limits.maxRequestBytes);
|
|
128
|
+
const { namespace } = yield* MemoryOwnerIdentity;
|
|
129
|
+
if (!MemoryNamespace.equals(namespace, request.access.namespace) || request._tag === "Change" && !MemoryNamespace.equals(namespace, request.write.key.namespace) || request._tag === "RevalidateSemantic" && request.found.candidates.some((candidate) => !MemoryNamespace.equals(namespace, candidate.key.namespace))) return yield* MemoryRpcError.make({ reason: "denied" });
|
|
130
|
+
const remaining = Math.min(limits.timeoutMillis, request.deadlineMillis - (yield* Clock.currentTimeMillis));
|
|
131
|
+
if (remaining <= 0) return yield* MemoryRpcError.make({ reason: "timeout" });
|
|
132
|
+
return yield* Effect.gen(function* () {
|
|
133
|
+
yield* (yield* MemoryOwnerAuthorizer).authorize(request);
|
|
134
|
+
if (request._tag === "Change") {
|
|
135
|
+
const writer = yield* MemoryWriter;
|
|
136
|
+
return {
|
|
137
|
+
_tag: "Changed",
|
|
138
|
+
access: request.access,
|
|
139
|
+
document: yield* writer.change(request.write)
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
if (request._tag === "RevalidateSemantic") {
|
|
143
|
+
if (new Set(request.found.candidates.map((candidate) => candidate.key.id)).size > limits.maxSources) return yield* MemoryRpcError.make({ reason: "budget" });
|
|
144
|
+
const result = yield* revalidateSemanticMemoryCandidates(request.found, request.access, request.profile, {
|
|
145
|
+
...request.limits,
|
|
146
|
+
maxSourceBytes: Math.min(limits.maxSourceBytes, request.limits.maxSourceBytes ?? 16777216),
|
|
147
|
+
maxOutputBytes: Math.min(limits.maxResponseBytes, request.limits.maxOutputBytes ?? 16777216)
|
|
148
|
+
});
|
|
149
|
+
return {
|
|
150
|
+
_tag: "Semantic",
|
|
151
|
+
access: request.access,
|
|
152
|
+
result
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
if ((request.lookup._tag === "Found" ? new Set(request.lookup.passages.map((passage) => passage.source.id)).size : 0) > Math.min(limits.maxSources, request.limits.maxSources)) return yield* MemoryRpcError.make({ reason: "budget" });
|
|
156
|
+
const lookup = yield* revalidateMemoryLookup(request.lookup, request.access, {
|
|
157
|
+
maxSourceBytes: limits.maxSourceBytes,
|
|
158
|
+
maxInputBytes: Math.min(limits.maxSourceBytes, request.limits.maxInputBytes ?? 16777216)
|
|
159
|
+
});
|
|
160
|
+
return {
|
|
161
|
+
_tag: "Lookup",
|
|
162
|
+
access: request.access,
|
|
163
|
+
lookup
|
|
164
|
+
};
|
|
165
|
+
}).pipe(Effect.scoped, Effect.timeoutOrElse({
|
|
166
|
+
duration: remaining,
|
|
167
|
+
orElse: () => Effect.fail(MemoryRpcError.make({ reason: "timeout" }))
|
|
168
|
+
}));
|
|
169
|
+
}).pipe(Effect.flatMap((response) => encodeMemoryWire(MemoryOwnerResponse, response, limits.maxResponseBytes)), Effect.result);
|
|
170
|
+
if (result._tag === "Success") return result.success;
|
|
171
|
+
return yield* encodeMemoryWire(MemoryOwnerResponse, {
|
|
172
|
+
_tag: "Failed",
|
|
173
|
+
failure: result.failure
|
|
174
|
+
}, limits.maxResponseBytes).pipe(Effect.catch(() => Schema.encodeEffect(Schema.fromJsonString(MemoryOwnerResponse))({
|
|
175
|
+
_tag: "Failed",
|
|
176
|
+
failure: MemoryRpcError.make({ reason: "budget" })
|
|
177
|
+
}).pipe(Effect.orDie)));
|
|
178
|
+
});
|
|
179
|
+
//#endregion
|
|
180
|
+
export { MemoryOwnerAuthorizer, MemoryOwnerFailure, MemoryOwnerIdentity, MemoryOwnerRequest, MemoryOwnerResponse, MemoryRpcError, MemoryRpcLimits, decodeMemoryWire, defaultMemoryRpcLimits, encodeMemoryWire, handleMemoryOwnerRequest, memoryWireBytes, MemoryProtocol_exports as t };
|
|
181
|
+
|
|
182
|
+
//# sourceMappingURL=MemoryProtocol.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MemoryProtocol.mjs","names":[],"sources":["../src/MemoryProtocol.ts"],"sourcesContent":["import * as MemoryNamespace from \"@effect-agent/core/MemoryNamespace\";\nimport {\n MemoryLookup,\n MemoryRecallError,\n MemoryRecallLimits,\n} from \"@effect-agent/core/MemoryReference\";\nimport { MemoryAccess, revalidateMemoryLookup } from \"@effect-agent/core/MemoryRevalidation\";\nimport { type MemoryReader } from \"@effect-agent/core/MemoryStore\";\nimport {\n MemoryConflict,\n MemoryDocument,\n MemoryMutationFailure,\n MemoryOperationConflict,\n MemoryStorageError,\n MemoryWithdrawn,\n MemoryWrite,\n MemoryWriter,\n} from \"@effect-agent/core/MemoryStore\";\nimport {\n MemoryIndexSearch,\n MemoryIndexError,\n SemanticMemoryProfile,\n} from \"@effect-agent/core/SemanticMemoryIndex\";\nimport {\n SemanticMemoryError,\n SemanticCandidateLimits,\n SemanticCandidateResult,\n revalidateSemanticMemoryCandidates,\n} from \"@effect-agent/core/SemanticMemoryRevalidation\";\nimport { Principal } from \"@effect-agent/thread/SubmissionLedger\";\nimport { Clock, Context, Effect, Encoding, Schema } from \"effect\";\n\nexport class MemoryRpcError extends Schema.TaggedError<MemoryRpcError>()(\"MemoryRpcError\", {\n reason: Schema.Literals([\"denied\", \"protocol\", \"budget\", \"timeout\", \"unavailable\"]),\n}) {}\n\nexport class MemoryRpcLimits extends Schema.Class<MemoryRpcLimits>(\n \"@effect-agent/storage-cloudflare/MemoryRpcLimits\",\n)({\n maxRequestBytes: Schema.Int.check(Schema.isBetween({ minimum: 256, maximum: 4_194_304 })),\n maxResponseBytes: Schema.Int.check(Schema.isBetween({ minimum: 256, maximum: 16_777_216 })),\n maxSourceBytes: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 67_108_864 })),\n maxSources: MemoryRecallLimits.fields.maxSources,\n timeoutMillis: MemoryRecallLimits.fields.timeoutMillis,\n}) {}\n\nexport const defaultMemoryRpcLimits = MemoryRpcLimits.make({\n maxRequestBytes: 1_048_576,\n maxResponseBytes: 4_194_304,\n maxSourceBytes: 16_777_216,\n maxSources: 16,\n timeoutMillis: 10_000,\n});\n\nconst RequestFields = {\n version: Schema.Literal(1),\n access: MemoryAccess.Wire,\n /** Host-authenticated identity, never copied from model input. The owner still authorizes it. */\n principal: Principal,\n deadlineMillis: Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0)),\n};\n\nconst RevalidateRequest = Schema.TaggedStruct(\"Revalidate\", {\n ...RequestFields,\n lookup: MemoryLookup,\n limits: MemoryRecallLimits,\n});\n\nconst ChangeRequest = Schema.TaggedStruct(\"Change\", { ...RequestFields, write: MemoryWrite.Wire });\n\nconst SemanticRequest: Schema.TaggedStruct<\n \"RevalidateSemantic\",\n typeof RequestFields & {\n readonly found: typeof MemoryIndexSearch.Wire;\n readonly profile: typeof SemanticMemoryProfile;\n readonly limits: typeof SemanticCandidateLimits;\n }\n> = Schema.TaggedStruct(\"RevalidateSemantic\", {\n ...RequestFields,\n found: MemoryIndexSearch.Wire,\n profile: SemanticMemoryProfile,\n limits: SemanticCandidateLimits,\n});\n\nexport const MemoryOwnerRequest: Schema.Union<\n [typeof RevalidateRequest, typeof ChangeRequest, typeof SemanticRequest]\n> = Schema.Union([RevalidateRequest, ChangeRequest, SemanticRequest]);\n\nexport type MemoryOwnerRequest = typeof MemoryOwnerRequest.Type;\n\nexport const MemoryOwnerFailure = Schema.Union([\n MemoryRpcError,\n MemoryStorageError,\n MemoryRecallError,\n MemoryConflict,\n MemoryWithdrawn,\n MemoryOperationConflict,\n MemoryMutationFailure,\n MemoryIndexError,\n SemanticMemoryError,\n]);\n\nexport type MemoryOwnerFailure = typeof MemoryOwnerFailure.Type;\n\nexport const MemoryOwnerResponse = Schema.Union([\n Schema.TaggedStruct(\"Lookup\", { access: MemoryAccess.Wire, lookup: MemoryLookup }),\n Schema.TaggedStruct(\"Changed\", { access: MemoryAccess.Wire, document: MemoryDocument.Wire }),\n Schema.TaggedStruct(\"Semantic\", { access: MemoryAccess.Wire, result: SemanticCandidateResult }),\n Schema.TaggedStruct(\"Failed\", { failure: MemoryOwnerFailure }),\n]);\n\nexport type MemoryOwnerResponse = typeof MemoryOwnerResponse.Type;\n\n/** Fail-closed application policy. Authorize the namespace, principal, scope, and full command. */\nexport class MemoryOwnerAuthorizer extends Context.Service<\n MemoryOwnerAuthorizer,\n {\n readonly authorize: (request: MemoryOwnerRequest) => Effect.Effect<void, MemoryRpcError>;\n }\n>()(\"@effect-agent/storage-cloudflare/MemoryOwnerAuthorizer\") {}\n\n/** Canonical namespace derived from this object's idFromName identity, never its request. */\nexport class MemoryOwnerIdentity extends Context.Service<\n MemoryOwnerIdentity,\n {\n readonly namespace: MemoryNamespace.Any;\n }\n>()(\"@effect-agent/storage-cloudflare/MemoryOwnerIdentity\") {}\n\nexport const memoryWireBytes = (text: string): number => Encoding.encodeHex(text).length / 2;\n\nexport const decodeMemoryWire = Effect.fn(\"decodeMemoryWire\")(function* <A, I>(\n schema: Schema.Codec<A, I, never>,\n raw: unknown,\n maxBytes: number,\n) {\n const text = yield* Schema.decodeUnknownEffect(Schema.String)(raw).pipe(\n Effect.mapError(() => MemoryRpcError.make({ reason: \"protocol\" })),\n );\n\n if (text.length > maxBytes || memoryWireBytes(text) > maxBytes)\n return yield* MemoryRpcError.make({ reason: \"budget\" });\n\n return yield* Schema.decodeEffect(Schema.fromJsonString(schema))(text).pipe(\n Effect.mapError(() => MemoryRpcError.make({ reason: \"protocol\" })),\n );\n});\n\nexport const encodeMemoryWire = Effect.fn(\"encodeMemoryWire\")(function* <A, I>(\n schema: Schema.Codec<A, I, never, never>,\n value: A,\n maxBytes: number,\n) {\n const encoded = yield* Schema.encodeEffect(Schema.fromJsonString(schema))(value).pipe(\n Effect.mapError(() => MemoryRpcError.make({ reason: \"protocol\" })),\n );\n\n if (encoded.length > maxBytes || memoryWireBytes(encoded) > maxBytes)\n return yield* MemoryRpcError.make({ reason: \"budget\" });\n\n return encoded;\n});\n\n/** One local read per distinct candidate source, with no per-document network calls. */\nexport const handleMemoryOwnerRequest = Effect.fn(\"MemoryOwner.handleRequest\")(function* (\n raw: unknown,\n limits: MemoryRpcLimits = defaultMemoryRpcLimits,\n): Effect.fn.Return<\n string,\n never,\n MemoryOwnerIdentity | MemoryOwnerAuthorizer | MemoryReader | MemoryWriter\n> {\n const result = yield* Effect.gen(function* (): Effect.fn.Return<\n MemoryOwnerResponse,\n MemoryOwnerFailure,\n MemoryOwnerIdentity | MemoryOwnerAuthorizer | MemoryReader | MemoryWriter\n > {\n limits = yield* Schema.decodeUnknownEffect(MemoryRpcLimits)(limits).pipe(\n Effect.mapError(() => MemoryRpcError.make({ reason: \"protocol\" })),\n );\n const request = yield* decodeMemoryWire(MemoryOwnerRequest, raw, limits.maxRequestBytes);\n const { namespace } = yield* MemoryOwnerIdentity;\n\n if (\n !MemoryNamespace.equals(namespace, request.access.namespace) ||\n (request._tag === \"Change\" &&\n !MemoryNamespace.equals(namespace, request.write.key.namespace)) ||\n (request._tag === \"RevalidateSemantic\" &&\n request.found.candidates.some(\n (candidate) => !MemoryNamespace.equals(namespace, candidate.key.namespace),\n ))\n )\n return yield* MemoryRpcError.make({ reason: \"denied\" });\n\n const remaining = Math.min(\n limits.timeoutMillis,\n request.deadlineMillis - (yield* Clock.currentTimeMillis),\n );\n\n if (remaining <= 0) return yield* MemoryRpcError.make({ reason: \"timeout\" });\n\n return yield* Effect.gen(function* (): Effect.fn.Return<\n MemoryOwnerResponse,\n MemoryOwnerFailure,\n MemoryOwnerAuthorizer | MemoryReader | MemoryWriter\n > {\n const authorizer = yield* MemoryOwnerAuthorizer;\n\n yield* authorizer.authorize(request);\n if (request._tag === \"Change\") {\n const writer = yield* MemoryWriter;\n\n return {\n _tag: \"Changed\",\n access: request.access,\n document: yield* writer.change(request.write),\n };\n }\n if (request._tag === \"RevalidateSemantic\") {\n if (\n new Set(request.found.candidates.map((candidate) => candidate.key.id)).size >\n limits.maxSources\n )\n return yield* MemoryRpcError.make({ reason: \"budget\" });\n\n const result = yield* revalidateSemanticMemoryCandidates(\n request.found,\n request.access,\n request.profile,\n {\n ...request.limits,\n maxSourceBytes: Math.min(\n limits.maxSourceBytes,\n request.limits.maxSourceBytes ?? 16_777_216,\n ),\n maxOutputBytes: Math.min(\n limits.maxResponseBytes,\n request.limits.maxOutputBytes ?? 16_777_216,\n ),\n },\n );\n\n return { _tag: \"Semantic\", access: request.access, result };\n }\n\n const count =\n request.lookup._tag === \"Found\"\n ? new Set(request.lookup.passages.map((passage) => passage.source.id)).size\n : 0;\n\n if (count > Math.min(limits.maxSources, request.limits.maxSources))\n return yield* MemoryRpcError.make({ reason: \"budget\" });\n\n const lookup = yield* revalidateMemoryLookup(request.lookup, request.access, {\n maxSourceBytes: limits.maxSourceBytes,\n maxInputBytes: Math.min(limits.maxSourceBytes, request.limits.maxInputBytes ?? 16_777_216),\n });\n\n return { _tag: \"Lookup\", access: request.access, lookup };\n }).pipe(\n Effect.scoped,\n Effect.timeoutOrElse({\n duration: remaining,\n orElse: () => Effect.fail(MemoryRpcError.make({ reason: \"timeout\" })),\n }),\n );\n }).pipe(\n Effect.flatMap((response) =>\n encodeMemoryWire(MemoryOwnerResponse, response, limits.maxResponseBytes),\n ),\n Effect.result,\n );\n\n if (result._tag === \"Success\") return result.success;\n\n return yield* encodeMemoryWire(\n MemoryOwnerResponse,\n {\n _tag: \"Failed\",\n failure: result.failure,\n },\n limits.maxResponseBytes,\n ).pipe(\n Effect.catch(() =>\n Schema.encodeEffect(Schema.fromJsonString(MemoryOwnerResponse))({\n _tag: \"Failed\",\n failure: MemoryRpcError.make({ reason: \"budget\" }),\n }).pipe(Effect.orDie),\n ),\n );\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAgCA,IAAa,iBAAb,cAAoC,OAAO,YAA4B,CAAC,CAAC,kBAAkB,EACzF,QAAQ,OAAO,SAAS;CAAC;CAAU;CAAY;CAAU;CAAW;AAAa,CAAC,EACpF,CAAC,CAAC,CAAC,CAAC;AAEJ,IAAa,kBAAb,cAAqC,OAAO,MAC1C,kDACF,CAAC,CAAC;CACA,iBAAiB,OAAO,IAAI,MAAM,OAAO,UAAU;EAAE,SAAS;EAAK,SAAS;CAAU,CAAC,CAAC;CACxF,kBAAkB,OAAO,IAAI,MA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