diff --git a/packages/cultmesh-ts/src/index.ts b/packages/cultmesh-ts/src/index.ts index 8c243a77..c9ad766c 100644 --- a/packages/cultmesh-ts/src/index.ts +++ b/packages/cultmesh-ts/src/index.ts @@ -75,6 +75,20 @@ export interface CultMeshRouteRecord { readonly description: string; } +/** + * Consumer view of provider-session publications. The provider-session client + * owns ordering, replay, and reconnect; document handles only project that + * live publication state into typed latest/watch semantics. + */ +export interface CultMeshLivePublicationSource { + latest(schemaId: string, recordKey: string): Promise; + watch( + schemaId: string, + recordKey: string, + callback: (value: unknown) => void, + ): CultMeshUnsubscribe; +} + export type CultMeshDocumentPublicationSource = | { readonly kind: "single-file"; @@ -88,6 +102,11 @@ export type CultMeshDocumentPublicationSource = readonly kind: "peer-snapshot"; readonly peer: CultNetPeer | (() => CultNetPeer | Promise); readonly endpoint?: string; + } + | { + readonly kind: "live-publication"; + readonly source: CultMeshLivePublicationSource; + readonly endpoint?: string; }; export interface CultMeshPublicationDocumentBinding { @@ -2712,6 +2731,33 @@ export function cultMeshDocumentFromPublication( requireNonEmpty(recordKey, "recordKey"); switch (source.kind) { + case "live-publication": { + const schema = typeof schemaOrDefinition === "string" + ? { schemaId: schemaOrDefinition } + : cultMeshSchemaFromDefinition(schemaOrDefinition); + const schemaId = schema.schemaId ?? schema.type; + if (!schemaId) { + throw new Error("CultMesh live publication document requires a schema id or document type."); + } + const parse = (value: unknown) => parseCultMeshDocumentValue(schemaOrDefinition, value); + return cultMeshDocument( + options.documentId ?? recordKey, + schema, + async () => parse(await source.source.latest(schemaId, recordKey)), + { + routeHint: options.routeHint ?? cultMeshRouteHint( + "network", + source.endpoint ?? "CultMesh provider-session publication", + ), + sources: [cultMeshProjectionSource(options.sourceId ?? recordKey, { schemaId })], + watchDocument: (_context, callback) => source.source.watch( + schemaId, + recordKey, + value => callback(parse(value)), + ), + }, + ); + } case "single-file": return typeof schemaOrDefinition === "string" ? cultMeshDocumentFromSingleFile(source.path, schemaOrDefinition, { diff --git a/packages/cultmesh-ts/test/cultmesh.test.ts b/packages/cultmesh-ts/test/cultmesh.test.ts index f4e8f9cc..3ac8e129 100644 --- a/packages/cultmesh-ts/test/cultmesh.test.ts +++ b/packages/cultmesh-ts/test/cultmesh.test.ts @@ -619,6 +619,76 @@ test("CultMesh TS document handles read schema publications from single-file sto ]); }); +test("CultMesh TS projects live provider publications as typed document handles", async () => { + let current: unknown = { + noteId: "note:live-provider", + body: "initial publication", + }; + let watcher: ((value: unknown) => void) | undefined; + const calls: string[] = []; + + const document = CultMesh.documentFromPublication( + { + kind: "live-publication", + endpoint: "provider-session:aetheria", + source: { + latest: async (schemaId, recordKey) => { + calls.push(`latest:${schemaId}:${recordKey}`); + return current; + }, + watch: (schemaId, recordKey, callback) => { + calls.push(`watch:${schemaId}:${recordKey}`); + watcher = callback; + return () => { + calls.push("unsubscribed"); + watcher = undefined; + }; + }, + }, + }, + noteDocument, + "note:live-provider", + { documentId: "aetheria.note.live" }, + ); + + assert.deepEqual(await document.latest(), current); + assert.equal(document.routeHint.kind, "network"); + assert.equal(document.routeHint.description, "provider-session:aetheria"); + + const observed: string[] = []; + const unsubscribe = document.watch(value => observed.push(value.body)); + current = { + noteId: "note:live-provider", + body: "ordered live upsert", + }; + watcher?.(current); + unsubscribe(); + + assert.deepEqual(observed, ["ordered live upsert"]); + assert.deepEqual(calls, [ + "latest:cultmesh.note.v0:note:live-provider", + "watch:cultmesh.note.v0:note:live-provider", + "unsubscribed", + ]); + assert.equal(watcher, undefined); +}); + +test("CultMesh TS validates live provider publications through typed document schemas", async () => { + const document = CultMesh.documentFromPublication( + { + kind: "live-publication", + source: { + latest: async () => ({ noteId: 42, body: "invalid" }), + watch: () => () => undefined, + }, + }, + noteDocument, + "note:invalid-live-provider", + ); + + await assert.rejects(document.latest(), /noteId/); +}); + test("CultMesh TS binds publication document catalogs from source resolvers", async () => { const root = await mkdtemp(join(tmpdir(), "cultmesh-ts-publication-catalog-")); const firstPath = join(root, "first.ccmp");