@party-stack/ontology-server 0.0.0-stage → 0.11.1

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 (93) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +493 -2
  3. package/lib/attachmentHttp.d.ts +15 -0
  4. package/lib/attachmentHttp.d.ts.map +1 -0
  5. package/lib/attachmentHttp.js +235 -0
  6. package/lib/attachmentHttp.js.map +1 -0
  7. package/lib/attachmentTransfers.d.ts +115 -0
  8. package/lib/attachmentTransfers.d.ts.map +1 -0
  9. package/lib/attachmentTransfers.js +208 -0
  10. package/lib/attachmentTransfers.js.map +1 -0
  11. package/lib/authorization.d.ts +21 -0
  12. package/lib/authorization.d.ts.map +1 -0
  13. package/lib/authorization.js +385 -0
  14. package/lib/authorization.js.map +1 -0
  15. package/lib/bindingFence.d.ts +27 -0
  16. package/lib/bindingFence.d.ts.map +1 -0
  17. package/lib/bindingFence.js +39 -0
  18. package/lib/bindingFence.js.map +1 -0
  19. package/lib/composition.d.ts +467 -0
  20. package/lib/composition.d.ts.map +1 -0
  21. package/lib/composition.js +102 -0
  22. package/lib/composition.js.map +1 -0
  23. package/lib/definitionProjection.d.ts +21 -0
  24. package/lib/definitionProjection.d.ts.map +1 -0
  25. package/lib/definitionProjection.js +441 -0
  26. package/lib/definitionProjection.js.map +1 -0
  27. package/lib/errorProjection.d.ts +22 -0
  28. package/lib/errorProjection.d.ts.map +1 -0
  29. package/lib/errorProjection.js +72 -0
  30. package/lib/errorProjection.js.map +1 -0
  31. package/lib/http.d.ts +23 -0
  32. package/lib/http.d.ts.map +1 -0
  33. package/lib/http.js +17 -0
  34. package/lib/http.js.map +1 -0
  35. package/lib/index.d.ts +5 -0
  36. package/lib/index.js +3 -0
  37. package/lib/internalAccess.d.ts +10 -0
  38. package/lib/internalAccess.d.ts.map +1 -0
  39. package/lib/internalAccess.js +18 -0
  40. package/lib/internalAccess.js.map +1 -0
  41. package/lib/operations.d.ts +1265 -0
  42. package/lib/operations.d.ts.map +1 -0
  43. package/lib/operations.js +621 -0
  44. package/lib/operations.js.map +1 -0
  45. package/lib/policyDefinition.d.ts +19 -0
  46. package/lib/policyDefinition.d.ts.map +1 -0
  47. package/lib/policyDefinition.js +226 -0
  48. package/lib/policyDefinition.js.map +1 -0
  49. package/lib/policyWatches.d.ts +17 -0
  50. package/lib/policyWatches.d.ts.map +1 -0
  51. package/lib/policyWatches.js +49 -0
  52. package/lib/policyWatches.js.map +1 -0
  53. package/lib/receiptPolicy.d.ts +12 -0
  54. package/lib/receiptPolicy.d.ts.map +1 -0
  55. package/lib/receiptPolicy.js +63 -0
  56. package/lib/receiptPolicy.js.map +1 -0
  57. package/lib/replicaBootstrap.d.ts +56 -0
  58. package/lib/replicaBootstrap.d.ts.map +1 -0
  59. package/lib/replicaBootstrap.js +60 -0
  60. package/lib/replicaBootstrap.js.map +1 -0
  61. package/lib/routing.d.ts +87 -0
  62. package/lib/routing.d.ts.map +1 -0
  63. package/lib/routing.js +344 -0
  64. package/lib/routing.js.map +1 -0
  65. package/lib/rpc.d.ts +4799 -0
  66. package/lib/rpc.d.ts.map +1 -0
  67. package/lib/rpc.js +575 -0
  68. package/lib/rpc.js.map +1 -0
  69. package/lib/rpcActivity.d.ts +6 -0
  70. package/lib/rpcActivity.d.ts.map +1 -0
  71. package/lib/rpcActivity.js +8 -0
  72. package/lib/rpcActivity.js.map +1 -0
  73. package/lib/rpcBackend.d.ts +8 -0
  74. package/lib/rpcBackend.d.ts.map +1 -0
  75. package/lib/rpcBackend.js +76 -0
  76. package/lib/rpcBackend.js.map +1 -0
  77. package/lib/rpcHttp.d.ts +24 -0
  78. package/lib/rpcHttp.d.ts.map +1 -0
  79. package/lib/rpcHttp.js +118 -0
  80. package/lib/rpcHttp.js.map +1 -0
  81. package/lib/rpcLocal.d.ts +13 -0
  82. package/lib/rpcLocal.d.ts.map +1 -0
  83. package/lib/rpcLocal.js +26 -0
  84. package/lib/rpcLocal.js.map +1 -0
  85. package/lib/rpcWatch.d.ts +9 -0
  86. package/lib/rpcWatch.d.ts.map +1 -0
  87. package/lib/rpcWatch.js +29 -0
  88. package/lib/rpcWatch.js.map +1 -0
  89. package/lib/rpcWorker.d.ts +38 -0
  90. package/lib/rpcWorker.d.ts.map +1 -0
  91. package/lib/rpcWorker.js +192 -0
  92. package/lib/rpcWorker.js.map +1 -0
  93. package/package.json +53 -3
@@ -0,0 +1 @@
1
+ {"version":3,"file":"operations.d.ts","names":["_$__attachmentTransfers_js0.AttachmentUpload","_$__attachmentTransfers_js0.AttachmentValue"],"sources":["../src/operations.ts"],"mappings":";;;;;;;;;;;;;;cA8EM,UAAQ,OAAA;;;;;;;;;qBAgHD,uBAAqB,SAAA,SAAA,IAAA,mBAAA,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAA2C,OAAA,aAAA;YACjE;WAEO;WACA,+CAA+C;WAC/C,SAAS;;WAEX;;WACA;WAAmC;WAA0B;;WAE3D;WACA,QAAQ;;WAGR;WACA,oDAAoD;;WAGpD;WACA,mDAAmD;WACnD;;WAGA;WACA;WACA,YAAY;WACZ;;WAGA;WACA;WACA,SAAS,OAAO,OAAO,YAAY;WACnC;WACA;;W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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAqNvC,OAAA,OAAA,iCAAA,MAAA"}
@@ -0,0 +1,621 @@
1
+ import { Attachment, AttachmentTransferRpcs, attachmentTransferHandlers, splitAttachmentChunk } from "./attachmentTransfers.js";
2
+ import { projectValidationReport } from "./errorProjection.js";
3
+ import { ActionReceiptSchema, BackendChangePageSchema, BackendError, BackendSnapshotSchema, DecryptedValueSchema, EncryptedValueSchema, ValidationReportSchema, classifyBackendFailure, encryptedPropertyCodec } from "@party-stack/ontology-backend";
4
+ import { ActionRequestSchema, ObjectSubsetCodec, protocolValueCodec } from "@party-stack/ontology-backend/protocol";
5
+ import { attachmentByteSource, byteChunks, collectByteBlob, scopedByteSource } from "@party-stack/ontology-storage/blobs";
6
+ import { Effect, Exit, Fiber, Layer, Option, Queue, Schema, Scope, Sink, Stream } from "effect";
7
+ import { Rpc, RpcGroup } from "effect/rpc";
8
+ import { PinOptionsSchema, PinStatusSchema, ReplicaStatusSchema, makeReplicaMonitor } from "@party-stack/ontology-replica";
9
+ import { QueryDocumentSchema } from "@party-stack/ontology-spec/effect";
10
+ import { getTargetValueType } from "@party-stack/ontology-spec/utils";
11
+ import { withObjectSources } from "@party-stack/ontology-storage";
12
+ //#region src/operations.ts
13
+ const Validation = ValidationReportSchema;
14
+ const Trace = Schema.Struct({
15
+ id: Schema.String,
16
+ traceId: Schema.String,
17
+ parentId: Schema.optional(Schema.String),
18
+ name: Schema.String,
19
+ startedAt: Schema.Number,
20
+ durationMs: Schema.Number,
21
+ outcome: Schema.Literals([
22
+ "running",
23
+ "success",
24
+ "failure",
25
+ "interrupted"
26
+ ]),
27
+ error: Schema.optional(Schema.Struct({
28
+ kind: Schema.Literals(["Error", "Defect"]),
29
+ message: Schema.String
30
+ })),
31
+ events: Schema.optional(Schema.Array(Schema.Struct({
32
+ name: Schema.String,
33
+ at: Schema.Number,
34
+ attributes: Schema.Record(Schema.String, Schema.Union([
35
+ Schema.String,
36
+ Schema.Number,
37
+ Schema.Boolean
38
+ ]))
39
+ }))),
40
+ eventCount: Schema.optional(Schema.Number),
41
+ attributes: Schema.Record(Schema.String, Schema.Union([
42
+ Schema.String,
43
+ Schema.Number,
44
+ Schema.Boolean
45
+ ]))
46
+ });
47
+ const Parameters = Schema.Record(Schema.String, Schema.Unknown);
48
+ const Metadata = Schema.Struct({
49
+ size: Schema.optional(Schema.Number),
50
+ type: Schema.optional(Schema.String),
51
+ name: Schema.optional(Schema.String),
52
+ dimensions: Schema.optional(Schema.Struct({
53
+ width: Schema.Number,
54
+ height: Schema.Number
55
+ }))
56
+ });
57
+ /** Explicit helper operations alongside replica transfer. These do not bypass row replication. */
58
+ const HelperRpcs = RpcGroup.make(Rpc.make("Query", {
59
+ payload: protocolValueCodec(Schema.Struct({
60
+ document: QueryDocumentSchema,
61
+ inputs: Schema.optional(Parameters)
62
+ })),
63
+ success: protocolValueCodec(Parameters),
64
+ error: BackendError
65
+ }), Rpc.make("LoadSubset", {
66
+ payload: ObjectSubsetCodec,
67
+ success: protocolValueCodec(BackendSnapshotSchema),
68
+ error: BackendError
69
+ }), Rpc.make("WatchChanges", {
70
+ success: Schema.Literal("invalidate"),
71
+ error: BackendError,
72
+ stream: true
73
+ }), Rpc.make("WatchSubset", {
74
+ payload: ObjectSubsetCodec,
75
+ success: Schema.Literals(["reset", "invalidate"]),
76
+ error: BackendError,
77
+ stream: true
78
+ }), Rpc.make("PullChanges", {
79
+ payload: Schema.Struct({
80
+ cursor: Schema.optional(Schema.String),
81
+ limit: Schema.optional(Schema.Int.check(Schema.isBetween({
82
+ minimum: 1,
83
+ maximum: 1e4
84
+ })))
85
+ }),
86
+ success: protocolValueCodec(BackendChangePageSchema),
87
+ error: BackendError
88
+ }), Rpc.make("ApplyAction", {
89
+ payload: protocolValueCodec(ActionRequestSchema),
90
+ success: ActionReceiptSchema,
91
+ error: BackendError
92
+ }), Rpc.make("Decrypt", {
93
+ payload: Schema.Struct({
94
+ value: EncryptedValueSchema,
95
+ cache: Schema.optional(Schema.Boolean)
96
+ }),
97
+ success: protocolValueCodec(DecryptedValueSchema),
98
+ error: BackendError
99
+ }), Rpc.make("QueryFunction", {
100
+ payload: protocolValueCodec(Schema.Struct({
101
+ name: Schema.NonEmptyString,
102
+ parameters: Parameters
103
+ })),
104
+ success: protocolValueCodec(Schema.Unknown),
105
+ error: BackendError
106
+ }), Rpc.make("ResolveActionParameters", {
107
+ payload: protocolValueCodec(Schema.Struct({
108
+ name: Schema.NonEmptyString,
109
+ parameters: Parameters
110
+ })),
111
+ success: protocolValueCodec(Parameters),
112
+ error: BackendError
113
+ }), Rpc.make("ValidateAction", {
114
+ payload: protocolValueCodec(Schema.Struct({
115
+ name: Schema.NonEmptyString,
116
+ parameters: Parameters,
117
+ knownParameters: Schema.optional(Schema.Array(Schema.String))
118
+ })),
119
+ success: protocolValueCodec(Validation),
120
+ error: BackendError
121
+ }), Rpc.make("AttachmentMetadata", {
122
+ payload: Schema.Struct({
123
+ attachment: Attachment,
124
+ select: Schema.optional(Schema.Array(Schema.Literals([
125
+ "size",
126
+ "type",
127
+ "name",
128
+ "dimensions"
129
+ ])))
130
+ }),
131
+ success: Metadata,
132
+ error: BackendError
133
+ }), Rpc.make("ControlOutbox", {
134
+ payload: protocolValueCodec(Schema.Struct({
135
+ operation: Schema.Literals([
136
+ "remove",
137
+ "retry",
138
+ "edit"
139
+ ]),
140
+ id: Schema.NonEmptyString,
141
+ invocation: Schema.optional(ActionRequestSchema),
142
+ allowUnknownOutcome: Schema.optional(Schema.Boolean)
143
+ })),
144
+ error: BackendError
145
+ }), Rpc.make("ReadPin", {
146
+ success: protocolValueCodec(Schema.UndefinedOr(PinStatusSchema)),
147
+ error: BackendError
148
+ }), Rpc.make("WatchPin", {
149
+ success: protocolValueCodec(Schema.UndefinedOr(PinStatusSchema)),
150
+ error: BackendError,
151
+ stream: true
152
+ }), Rpc.make("SetPin", {
153
+ payload: {
154
+ query: protocolValueCodec(QueryDocumentSchema),
155
+ options: Schema.optional(protocolValueCodec(PinOptionsSchema))
156
+ },
157
+ error: BackendError
158
+ }), Rpc.make("ClearPin", { error: BackendError }), Rpc.make("ReplicaStatus", {
159
+ success: protocolValueCodec(ReplicaStatusSchema),
160
+ error: BackendError,
161
+ stream: true
162
+ }), Rpc.make("Traces", {
163
+ success: Schema.Array(Trace),
164
+ error: BackendError,
165
+ stream: true
166
+ }), Rpc.make("DeleteLocalData", { error: BackendError }), Rpc.make("ReleaseOwner", { error: BackendError }));
167
+ const OntologyOperationRpcs = HelperRpcs.merge(AttachmentTransferRpcs);
168
+ const failure = (cause) => classifyBackendFailure("ontology.operations", cause);
169
+ const decryptFailure = (cause) => new BackendError({
170
+ operation: "decrypt",
171
+ kind: cause instanceof BackendError ? cause.kind : "Unavailable",
172
+ retryable: cause instanceof BackendError ? cause.retryable : true,
173
+ message: "Decryption failed."
174
+ });
175
+ const missing = (name) => new BackendError({
176
+ operation: name,
177
+ kind: "NotImplemented",
178
+ message: `Ontology operation ${name} is unavailable.`,
179
+ retryable: false
180
+ });
181
+ /** Shared storage/application operation; transports supply framing and authorization. */
182
+ const ontologyAttachmentHandlers = (options, authorize = options.authorize) => ({
183
+ create: (request, blob) => Effect.gen(function* () {
184
+ yield* options.ready ?? Effect.void;
185
+ if (options.backend) {
186
+ if (!options.backend.attachments?.stage) return yield* missing("CreateAttachment");
187
+ const ir = options.ir ?? options.replica?.ir;
188
+ const type = request.target && ir ? yield* Effect.try({
189
+ try: () => getTargetValueType(ir, request.target),
190
+ catch: () => new BackendError({
191
+ operation: "attachment.stage",
192
+ kind: "InvalidRequest",
193
+ message: "Invalid attachment target.",
194
+ retryable: false
195
+ })
196
+ }) : void 0;
197
+ if (type && type.kind !== "attachment") return yield* new BackendError({
198
+ operation: "attachment.stage",
199
+ kind: "InvalidRequest",
200
+ message: "Target is not an attachment.",
201
+ retryable: false
202
+ });
203
+ return yield* options.backend.attachments.stage({
204
+ id: request.id ?? crypto.randomUUID(),
205
+ type: request.type
206
+ }, blob, { target: type?.kind === "attachment" ? type.value : void 0 });
207
+ }
208
+ if (!options.replica) return yield* missing("CreateAttachment");
209
+ const created = yield* options.replica.attachments.create(blob, request);
210
+ if (created.isMaterialized) yield* created.isMaterialized;
211
+ return created.attachment;
212
+ }),
213
+ read: (attachment, headers) => Effect.gen(function* () {
214
+ yield* authorize({
215
+ operation: "ReadAttachment",
216
+ attachment
217
+ }, headers);
218
+ yield* options.ready ?? Effect.void;
219
+ if (options.backend) {
220
+ if (!options.backend.attachments?.read) return yield* missing("ReadAttachment");
221
+ return yield* options.backend.attachments.read(attachment);
222
+ }
223
+ if (!options.replica) return yield* missing("ReadAttachment");
224
+ return yield* options.replica.attachments.source(attachment);
225
+ })
226
+ });
227
+ const ontologyOperationHandlers = (options, authorize = options.authorize) => {
228
+ return HelperRpcs.toLayer({
229
+ Query: (request, context) => Effect.gen(function* () {
230
+ yield* authorize({
231
+ operation: "Query",
232
+ ...request
233
+ }, context.headers);
234
+ yield* options.ready ?? Effect.void;
235
+ if (!options.backend) return yield* missing("Query");
236
+ return yield* options.backend.query(request.document, { inputs: request.inputs });
237
+ }),
238
+ LoadSubset: (subset, context) => Effect.gen(function* () {
239
+ yield* authorize({
240
+ operation: "LoadSubset",
241
+ subset
242
+ }, context.headers);
243
+ yield* options.ready ?? Effect.void;
244
+ if (!options.backend) return yield* missing("LoadSubset");
245
+ const snapshot = yield* options.backend.loadSubset(subset);
246
+ const ir = options.ir ?? options.replica?.ir;
247
+ const objectType = ir?.objectTypes.find((type) => type.name === subset.from.source.objectType);
248
+ return !ir || !objectType ? snapshot : {
249
+ ...snapshot,
250
+ rows: snapshot.rows.map((row) => ({
251
+ ...row,
252
+ value: withObjectSources({
253
+ ir,
254
+ objectType,
255
+ object: row.value,
256
+ key: row.key
257
+ })
258
+ }))
259
+ };
260
+ }),
261
+ WatchChanges: (_, context) => Stream.unwrap(Effect.gen(function* () {
262
+ yield* authorize({ operation: "WatchChanges" }, context.headers);
263
+ yield* options.ready ?? Effect.void;
264
+ if (!options.backend?.changes?.watch) return yield* missing("WatchChanges");
265
+ return Stream.concat(Stream.make("invalidate"), (yield* options.backend.changes.watch).pipe(Stream.map(() => "invalidate")));
266
+ })),
267
+ WatchSubset: (subset, context) => Stream.unwrap(Effect.gen(function* () {
268
+ yield* authorize({
269
+ operation: "WatchSubset",
270
+ subset
271
+ }, context.headers);
272
+ yield* options.ready ?? Effect.void;
273
+ if (!options.backend) return yield* missing("WatchSubset");
274
+ const watched = options.backend.watchSubset?.(subset);
275
+ const interval = options.backend.refresh?.(subset)?.pollIntervalMs ?? false;
276
+ if (!watched && interval !== false && (!Number.isFinite(interval) || interval < 100)) return yield* new BackendError({
277
+ operation: "WatchSubset",
278
+ kind: "InvalidConfiguration",
279
+ retryable: false,
280
+ message: "Invalid subset poll interval."
281
+ });
282
+ if (!watched && interval === false) return yield* missing("WatchSubset");
283
+ const changes = watched ? yield* watched : Stream.tick(interval);
284
+ return Stream.concat(Stream.make("reset"), changes.pipe(Stream.map((event) => event === "reset" ? "reset" : "invalidate")));
285
+ })),
286
+ PullChanges: (request, context) => Effect.gen(function* () {
287
+ yield* authorize({
288
+ operation: "PullChanges",
289
+ ...request
290
+ }, context.headers);
291
+ yield* options.ready ?? Effect.void;
292
+ if (!options.backend?.changes) return yield* missing("PullChanges");
293
+ return yield* options.backend.changes.pull(request);
294
+ }),
295
+ ApplyAction: (invocation, context) => Effect.gen(function* () {
296
+ yield* authorize({
297
+ operation: "ApplyAction",
298
+ invocation
299
+ }, context.headers);
300
+ yield* options.ready ?? Effect.void;
301
+ if (!options.backend) return yield* missing("ApplyAction");
302
+ return yield* options.backend.applyAction(invocation);
303
+ }),
304
+ Decrypt: (request, context) => Effect.gen(function* () {
305
+ yield* authorize({
306
+ operation: "Decrypt",
307
+ ...request
308
+ }, context.headers);
309
+ yield* options.ready ?? Effect.void;
310
+ if (options.backend) {
311
+ if (!options.backend.encrypted) return yield* missing("Decrypt");
312
+ const result = yield* options.backend.encrypted.decrypt(request.value);
313
+ if (result.ciphertext !== void 0 && result.ciphertext !== request.value.ciphertext) return yield* new BackendError({
314
+ operation: "decrypt",
315
+ kind: "Conflict",
316
+ message: "The encrypted value changed.",
317
+ retryable: false
318
+ });
319
+ return result;
320
+ }
321
+ if (!options.replica) return yield* missing("Decrypt");
322
+ const result = yield* options.replica.decrypt(request.value, { cache: request.cache });
323
+ return {
324
+ ...result,
325
+ value: encryptedPropertyCodec(options.replica.ir, request.value).encode(result.value)
326
+ };
327
+ }).pipe(Effect.mapError(decryptFailure)),
328
+ ResolveActionParameters: (request, context) => Effect.gen(function* () {
329
+ yield* authorize({
330
+ operation: "ResolveActionParameters",
331
+ ...request
332
+ }, context.headers);
333
+ yield* options.ready ?? Effect.void;
334
+ if (options.backend) {
335
+ if (!options.backend.resolveActionParameters) return yield* missing("ResolveActionParameters");
336
+ return yield* options.backend.resolveActionParameters(request.name, request.parameters);
337
+ }
338
+ if (!options.replica) return yield* missing("ResolveActionParameters");
339
+ return yield* options.replica.actions.resolveParameters(request.name, request.parameters);
340
+ }),
341
+ QueryFunction: (request, context) => Effect.gen(function* () {
342
+ yield* authorize({
343
+ operation: "QueryFunction",
344
+ ...request
345
+ }, context.headers);
346
+ if (options.backend) {
347
+ yield* options.ready ?? Effect.void;
348
+ if (!options.backend.queryFunction) return yield* missing(request.name);
349
+ return yield* options.backend.queryFunction(request.name, request.parameters);
350
+ }
351
+ return yield* missing(request.name);
352
+ }),
353
+ ValidateAction: (request, context) => Effect.gen(function* () {
354
+ yield* authorize({
355
+ operation: "ValidateAction",
356
+ ...request
357
+ }, context.headers);
358
+ if (options.backend?.validateAction) return yield* options.backend.validateAction(request.name, request.parameters, { knownParameters: request.knownParameters }).pipe(Effect.map((report) => projectValidationReport(report, options, request.name)));
359
+ const report = options.replica ? yield* options.replica.actions.validate(request.name, request.parameters, { knownParameters: request.knownParameters }) : { certain: false };
360
+ return projectValidationReport(report, options, request.name);
361
+ }),
362
+ AttachmentMetadata: (request, context) => Effect.gen(function* () {
363
+ yield* authorize({
364
+ operation: "AttachmentMetadata",
365
+ ...request
366
+ }, context.headers);
367
+ if (options.backend) {
368
+ yield* options.ready ?? Effect.void;
369
+ if (!options.backend.attachments?.metadata) return yield* missing("AttachmentMetadata");
370
+ return yield* options.backend.attachments.metadata(request.attachment, { select: request.select });
371
+ }
372
+ if (!options.replica) return yield* missing("AttachmentMetadata");
373
+ return yield* options.replica.attachments.metadata(request.attachment, { select: request.select }).pipe(Effect.map(({ name, dimensions, ...rest }) => ({
374
+ ...rest,
375
+ name: name ?? void 0,
376
+ dimensions: dimensions ?? void 0
377
+ })));
378
+ }),
379
+ ControlOutbox: (request, context) => Effect.gen(function* () {
380
+ yield* authorize({
381
+ operation: "ControlOutbox",
382
+ id: request.id,
383
+ command: request.operation,
384
+ invocation: request.invocation
385
+ }, context.headers);
386
+ const outbox = options.replica?.outbox;
387
+ if (!outbox) return yield* missing("ControlOutbox");
388
+ if (request.operation === "edit") {
389
+ if (!request.invocation || request.invocation.id !== request.id) return yield* missing("edit invocation");
390
+ yield* outbox.edit(request.id, request.invocation);
391
+ } else if (request.operation === "remove") yield* outbox.remove(request.id, { allowUnknownOutcome: request.allowUnknownOutcome });
392
+ else yield* outbox.retry(request.id, { allowUnknownOutcome: request.allowUnknownOutcome });
393
+ }),
394
+ ReplicaStatus: (_, context) => Stream.unwrap(Effect.gen(function* () {
395
+ yield* Effect.annotateCurrentSpan("activity", "subscription");
396
+ yield* authorize({ operation: "ReplicaStatus" }, context.headers);
397
+ yield* options.ready ?? Effect.void;
398
+ if (!options.replica) return yield* missing("ReplicaStatus");
399
+ return options.replica.status.changes;
400
+ })),
401
+ Traces: (_, context) => Stream.unwrap(Effect.gen(function* () {
402
+ yield* Effect.annotateCurrentSpan("activity", "subscription");
403
+ yield* authorize({ operation: "Traces" }, context.headers);
404
+ yield* options.ready ?? Effect.void;
405
+ const traces = options.traces;
406
+ if (!traces) return Stream.succeed([]);
407
+ return Stream.callback((queue) => Effect.acquireRelease(Effect.sync(() => {
408
+ const send = (record) => Queue.offerUnsafe(queue, record ? [record] : traces.getSnapshot());
409
+ const unsubscribe = traces.subscribe(send);
410
+ send();
411
+ return unsubscribe;
412
+ }), (unsubscribe) => Effect.sync(unsubscribe)), {
413
+ bufferSize: 500,
414
+ strategy: "sliding"
415
+ });
416
+ })),
417
+ ReadPin: (_, context) => Effect.gen(function* () {
418
+ yield* authorize({ operation: "ReadPin" }, context.headers);
419
+ yield* options.ready ?? Effect.void;
420
+ const pins = options.pins ?? options.replica?.demand;
421
+ if (!pins) return yield* missing("ReadPin");
422
+ return yield* pins.pinStatus;
423
+ }),
424
+ WatchPin: (_, context) => Stream.unwrap(Effect.gen(function* () {
425
+ yield* authorize({ operation: "WatchPin" }, context.headers);
426
+ yield* options.ready ?? Effect.void;
427
+ return (options.pins ?? options.replica?.demand)?.pinStatusChanges ?? Stream.fail(missing("WatchPin"));
428
+ })),
429
+ SetPin: ({ query, options: settings }, context) => Effect.gen(function* () {
430
+ yield* authorize({
431
+ operation: "SetPin",
432
+ query,
433
+ options: settings
434
+ }, context.headers);
435
+ yield* options.ready ?? Effect.void;
436
+ const pins = options.pins ?? options.replica?.demand;
437
+ if (!pins) return yield* missing("SetPin");
438
+ return yield* pins.setPin(query, settings);
439
+ }),
440
+ ClearPin: (_, context) => Effect.gen(function* () {
441
+ yield* authorize({ operation: "ClearPin" }, context.headers);
442
+ yield* options.ready ?? Effect.void;
443
+ const pins = options.pins ?? options.replica?.demand;
444
+ if (!pins) return yield* missing("ClearPin");
445
+ return yield* pins.clearPin;
446
+ }),
447
+ DeleteLocalData: (_, context) => authorize({ operation: "DeleteLocalData" }, context.headers).pipe(Effect.andThen(options.deleteLocalData ?? Effect.fail(missing("DeleteLocalData")))),
448
+ ReleaseOwner: (_, context) => authorize({ operation: "ReleaseOwner" }, context.headers).pipe(Effect.andThen(options.releaseOwner ?? Effect.fail(missing("ReleaseOwner"))))
449
+ }).pipe(Layer.merge(attachmentTransferHandlers({
450
+ ...options.attachmentTransfers,
451
+ authorizeUpload: (request, headers) => authorize({
452
+ operation: "CreateAttachment",
453
+ id: request.id,
454
+ target: request.target,
455
+ type: request.type,
456
+ size: request.size
457
+ }, headers),
458
+ ...ontologyAttachmentHandlers(options, authorize)
459
+ })));
460
+ };
461
+ const makeOntologyOperationsClient = Effect.fnUntraced(function* (client) {
462
+ const replicaMonitor = yield* makeReplicaMonitor(client.ReplicaStatus().pipe(Stream.mapError(failure)));
463
+ const scope = yield* Effect.scope;
464
+ const context = yield* Effect.context();
465
+ const listeners = /* @__PURE__ */ new Set();
466
+ let spans = [];
467
+ let watcher;
468
+ const notify = () => {
469
+ for (const listener of listeners) try {
470
+ listener();
471
+ } catch {}
472
+ };
473
+ const watch = Stream.runForEach(client.Traces(), (snapshot) => Effect.sync(() => {
474
+ for (const record of snapshot) {
475
+ const index = spans.findIndex((previous) => previous.id === record.id && previous.traceId === record.traceId);
476
+ spans = index < 0 ? [...spans.slice(-499), record] : spans.map((previous, at) => at === index ? record : previous);
477
+ }
478
+ notify();
479
+ })).pipe(Effect.catch(() => Effect.void));
480
+ const traces = {
481
+ getSnapshot: () => spans,
482
+ subscribe: (listener) => {
483
+ listeners.add(listener);
484
+ if (!watcher) watcher = Effect.runSyncWith(context)(Effect.forkIn(watch, scope));
485
+ return () => {
486
+ listeners.delete(listener);
487
+ if (!listeners.size && watcher) {
488
+ Effect.runFork(Fiber.interrupt(watcher));
489
+ watcher = void 0;
490
+ }
491
+ };
492
+ },
493
+ clear: () => {
494
+ spans = [];
495
+ notify();
496
+ }
497
+ };
498
+ yield* Effect.addFinalizer(() => watcher ? Fiber.interrupt(watcher).pipe(Effect.asVoid) : Effect.void);
499
+ const attachments = {
500
+ create: (input, opts) => Effect.gen(function* () {
501
+ const blob = attachmentByteSource(input);
502
+ return { attachment: yield* Effect.gen(function* () {
503
+ let result;
504
+ yield* Stream.runForEach(client.CreateAttachment({
505
+ size: blob.size,
506
+ type: blob.type,
507
+ name: blob.name,
508
+ ...opts
509
+ }), (event) => Effect.gen(function* () {
510
+ if (event.kind === "complete") {
511
+ result = event.attachment;
512
+ return;
513
+ }
514
+ let sequence = 0;
515
+ yield* Stream.runForEach(byteChunks(blob).pipe(Stream.mapError(failure)), (chunk) => Effect.forEach(splitAttachmentChunk(chunk), (bytes) => client.WriteAttachmentChunk({
516
+ uploadId: event.uploadId,
517
+ sequence: sequence++,
518
+ bytes,
519
+ end: false
520
+ }), { discard: true }));
521
+ yield* client.WriteAttachmentChunk({
522
+ uploadId: event.uploadId,
523
+ sequence,
524
+ bytes: /* @__PURE__ */ new Uint8Array(0),
525
+ end: true
526
+ });
527
+ }));
528
+ if (!result) return yield* missing("completed attachment transfer");
529
+ return result;
530
+ }) };
531
+ }).pipe(Effect.mapError(failure)),
532
+ source: (attachment) => scopedByteSource(Effect.gen(function* () {
533
+ const readScope = yield* Scope.fork(yield* Effect.scope);
534
+ return yield* Effect.gen(function* () {
535
+ const [header, tail] = yield* Scope.provide(Stream.peel(client.ReadAttachment(attachment), Sink.head()), readScope);
536
+ if (Option.isNone(header) || header.value.kind !== "metadata") return yield* failure(/* @__PURE__ */ new Error("Missing attachment metadata."));
537
+ const { kind: _kind, ...metadata } = header.value;
538
+ let consumed = false;
539
+ return {
540
+ ...metadata,
541
+ stream: Stream.suspend(() => {
542
+ if (consumed) return Stream.unwrap(attachments.source(attachment).pipe(Effect.map((source) => source.stream)));
543
+ consumed = true;
544
+ let received = 0;
545
+ return Stream.concat(tail.pipe(Stream.mapEffect((event) => Effect.gen(function* () {
546
+ if (event.kind !== "bytes") return yield* failure(/* @__PURE__ */ new Error("Duplicate attachment metadata."));
547
+ received += event.bytes.length;
548
+ if (metadata.size !== void 0 && received > metadata.size) return yield* failure(/* @__PURE__ */ new Error("Attachment exceeds its declared size."));
549
+ return event.bytes;
550
+ }))), Stream.fromEffect(Effect.gen(function* () {
551
+ if (metadata.size !== void 0 && received !== metadata.size) return yield* failure(/* @__PURE__ */ new Error("Incomplete attachment download."));
552
+ })).pipe(Stream.drain)).pipe(Stream.ensuring(Scope.close(readScope, Exit.void)));
553
+ })
554
+ };
555
+ }).pipe(Effect.onError(() => Scope.close(readScope, Exit.void)));
556
+ }), scope).pipe(Effect.mapError(failure)),
557
+ blob: (attachment) => Effect.scoped(attachments.source(attachment).pipe(Effect.flatMap(collectByteBlob), Effect.mapError(failure))),
558
+ metadata: (attachment, opts) => client.AttachmentMetadata({
559
+ attachment,
560
+ select: opts?.select
561
+ }).pipe(Effect.mapError(failure))
562
+ };
563
+ return {
564
+ query: (document, inputs) => client.Query({
565
+ document,
566
+ inputs
567
+ }).pipe(Effect.mapError(failure)),
568
+ decrypt: (value, settings) => client.Decrypt({
569
+ value,
570
+ cache: settings?.cache
571
+ }).pipe(Effect.map((result) => result.value), Effect.mapError(decryptFailure)),
572
+ deleteLocalData: client.DeleteLocalData().pipe(Effect.mapError(failure)),
573
+ releaseOwner: client.ReleaseOwner(void 0, { discard: true }).pipe(Effect.mapError(failure)),
574
+ pins: {
575
+ pinStatus: client.ReadPin().pipe(Effect.mapError(failure)),
576
+ pinStatusChanges: client.WatchPin().pipe(Stream.mapError(failure)),
577
+ setPin: (query, settings) => client.SetPin({
578
+ query,
579
+ options: settings
580
+ }).pipe(Effect.mapError(failure)),
581
+ clearPin: client.ClearPin().pipe(Effect.mapError(failure))
582
+ },
583
+ queryFunction: (name, parameters) => client.QueryFunction({
584
+ name,
585
+ parameters
586
+ }).pipe(Effect.mapError(failure)),
587
+ resolveActionParameters: (name, parameters) => client.ResolveActionParameters({
588
+ name,
589
+ parameters
590
+ }).pipe(Effect.mapError(failure)),
591
+ validate: (name, parameters, knownParameters) => client.ValidateAction({
592
+ name,
593
+ parameters,
594
+ knownParameters
595
+ }).pipe(Effect.mapError(failure)),
596
+ attachments,
597
+ outbox: {
598
+ remove: (id, settings) => client.ControlOutbox({
599
+ operation: "remove",
600
+ id,
601
+ allowUnknownOutcome: settings?.allowUnknownOutcome
602
+ }).pipe(Effect.mapError(failure)),
603
+ retry: (id, settings) => client.ControlOutbox({
604
+ operation: "retry",
605
+ id,
606
+ allowUnknownOutcome: settings?.allowUnknownOutcome
607
+ }).pipe(Effect.mapError(failure)),
608
+ edit: (id, invocation) => client.ControlOutbox({
609
+ operation: "edit",
610
+ id,
611
+ invocation
612
+ }).pipe(Effect.mapError(failure))
613
+ },
614
+ traces,
615
+ replicaMonitor
616
+ };
617
+ });
618
+ //#endregion
619
+ export { OntologyOperationRpcs, makeOntologyOperationsClient, ontologyAttachmentHandlers, ontologyOperationHandlers };
620
+
621
+ //# sourceMappingURL=operations.js.map