feat: add Automerge schemas + local-first-clients for remaining modules

- rdesign: linked Affine projects per space
- rdocs: linked Docmost documents per space
- rmeets: meeting scheduling and history
- rmaps: persistent annotations, routes, meeting points
- rforum: local-first-client wrapping existing provision schemas

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Jeff Emmett 2026-03-15 17:49:51 -07:00
parent a504a24a55
commit d3a6ad7dda
9 changed files with 476 additions and 0 deletions

View File

@ -0,0 +1,55 @@
/**
* rDesign Local-First Client syncs linked Affine projects.
*/
import { DocumentManager } from '../../shared/local-first/document';
import type { DocumentId } from '../../shared/local-first/document';
import { EncryptedDocStore } from '../../shared/local-first/storage';
import { DocSyncManager } from '../../shared/local-first/sync';
import { DocCrypto } from '../../shared/local-first/crypto';
import { designSchema, designDocId } from './schemas';
import type { DesignDoc, LinkedProject } from './schemas';
export class DesignLocalFirstClient {
#space: string; #documents: DocumentManager; #store: EncryptedDocStore; #sync: DocSyncManager; #initialized = false;
constructor(space: string, docCrypto?: DocCrypto) {
this.#space = space; this.#documents = new DocumentManager();
this.#store = new EncryptedDocStore(space, docCrypto);
this.#sync = new DocSyncManager({ documents: this.#documents, store: this.#store });
this.#documents.registerSchema(designSchema);
}
get isConnected(): boolean { return this.#sync.isConnected; }
async init(): Promise<void> {
if (this.#initialized) return;
await this.#store.open();
const cachedIds = await this.#store.listByModule('design', 'projects');
const cached = await this.#store.loadMany(cachedIds);
for (const [docId, binary] of cached) this.#documents.open<DesignDoc>(docId, designSchema, binary);
await this.#sync.preloadSyncStates(cachedIds);
const proto = location.protocol === 'https:' ? 'wss:' : 'ws:';
try { await this.#sync.connect(`${proto}//${location.host}/ws/${this.#space}`, this.#space); } catch {}
this.#initialized = true;
}
async subscribe(): Promise<DesignDoc | null> {
const docId = designDocId(this.#space) as DocumentId;
let doc = this.#documents.get<DesignDoc>(docId);
if (!doc) { const b = await this.#store.load(docId); doc = b ? this.#documents.open<DesignDoc>(docId, designSchema, b) : this.#documents.open<DesignDoc>(docId, designSchema); }
await this.#sync.subscribe([docId]); return doc ?? null;
}
getDoc(): DesignDoc | undefined { return this.#documents.get<DesignDoc>(designDocId(this.#space) as DocumentId); }
onChange(cb: (doc: DesignDoc) => void): () => void { return this.#sync.onChange(designDocId(this.#space) as DocumentId, cb as (doc: any) => void); }
linkProject(project: LinkedProject): void {
this.#sync.change<DesignDoc>(designDocId(this.#space) as DocumentId, `Link ${project.name}`, (d) => { d.linkedProjects[project.id] = project; });
}
unlinkProject(id: string): void {
this.#sync.change<DesignDoc>(designDocId(this.#space) as DocumentId, `Unlink project`, (d) => { delete d.linkedProjects[id]; });
}
async disconnect(): Promise<void> { await this.#sync.flush(); this.#sync.disconnect(); }
}

View File

@ -0,0 +1,36 @@
/**
* rDesign Automerge document schemas.
*
* Syncs linked Affine design projects per space.
* Actual design data lives in the Affine instance.
*
* DocId format: {space}:design:projects
*/
import type { DocSchema } from '../../shared/local-first/document';
export interface LinkedProject {
id: string;
url: string;
name: string;
addedBy: string | null;
addedAt: number;
}
export interface DesignDoc {
meta: { module: string; collection: string; version: number; spaceSlug: string; createdAt: number };
linkedProjects: Record<string, LinkedProject>;
}
export const designSchema: DocSchema<DesignDoc> = {
module: 'design',
collection: 'projects',
version: 1,
init: (): DesignDoc => ({
meta: { module: 'design', collection: 'projects', version: 1, spaceSlug: '', createdAt: Date.now() },
linkedProjects: {},
}),
migrate: (doc: any) => { if (!doc.linkedProjects) doc.linkedProjects = {}; doc.meta.version = 1; return doc; },
};
export function designDocId(space: string) { return `${space}:design:projects` as const; }

View File

@ -0,0 +1,55 @@
/**
* rDocs Local-First Client syncs linked Docmost documents.
*/
import { DocumentManager } from '../../shared/local-first/document';
import type { DocumentId } from '../../shared/local-first/document';
import { EncryptedDocStore } from '../../shared/local-first/storage';
import { DocSyncManager } from '../../shared/local-first/sync';
import { DocCrypto } from '../../shared/local-first/crypto';
import { docsSchema, docsDocId } from './schemas';
import type { DocsDoc, LinkedDocument } from './schemas';
export class DocsLocalFirstClient {
#space: string; #documents: DocumentManager; #store: EncryptedDocStore; #sync: DocSyncManager; #initialized = false;
constructor(space: string, docCrypto?: DocCrypto) {
this.#space = space; this.#documents = new DocumentManager();
this.#store = new EncryptedDocStore(space, docCrypto);
this.#sync = new DocSyncManager({ documents: this.#documents, store: this.#store });
this.#documents.registerSchema(docsSchema);
}
get isConnected(): boolean { return this.#sync.isConnected; }
async init(): Promise<void> {
if (this.#initialized) return;
await this.#store.open();
const cachedIds = await this.#store.listByModule('docs', 'links');
const cached = await this.#store.loadMany(cachedIds);
for (const [docId, binary] of cached) this.#documents.open<DocsDoc>(docId, docsSchema, binary);
await this.#sync.preloadSyncStates(cachedIds);
const proto = location.protocol === 'https:' ? 'wss:' : 'ws:';
try { await this.#sync.connect(`${proto}//${location.host}/ws/${this.#space}`, this.#space); } catch {}
this.#initialized = true;
}
async subscribe(): Promise<DocsDoc | null> {
const docId = docsDocId(this.#space) as DocumentId;
let doc = this.#documents.get<DocsDoc>(docId);
if (!doc) { const b = await this.#store.load(docId); doc = b ? this.#documents.open<DocsDoc>(docId, docsSchema, b) : this.#documents.open<DocsDoc>(docId, docsSchema); }
await this.#sync.subscribe([docId]); return doc ?? null;
}
getDoc(): DocsDoc | undefined { return this.#documents.get<DocsDoc>(docsDocId(this.#space) as DocumentId); }
onChange(cb: (doc: DocsDoc) => void): () => void { return this.#sync.onChange(docsDocId(this.#space) as DocumentId, cb as (doc: any) => void); }
linkDocument(doc: LinkedDocument): void {
this.#sync.change<DocsDoc>(docsDocId(this.#space) as DocumentId, `Link ${doc.title}`, (d) => { d.linkedDocuments[doc.id] = doc; });
}
unlinkDocument(id: string): void {
this.#sync.change<DocsDoc>(docsDocId(this.#space) as DocumentId, `Unlink document`, (d) => { delete d.linkedDocuments[id]; });
}
async disconnect(): Promise<void> { await this.#sync.flush(); this.#sync.disconnect(); }
}

36
modules/rdocs/schemas.ts Normal file
View File

@ -0,0 +1,36 @@
/**
* rDocs Automerge document schemas.
*
* Syncs linked Docmost documents per space.
* Actual document content lives in the Docmost instance.
*
* DocId format: {space}:docs:links
*/
import type { DocSchema } from '../../shared/local-first/document';
export interface LinkedDocument {
id: string;
url: string;
title: string;
addedBy: string | null;
addedAt: number;
}
export interface DocsDoc {
meta: { module: string; collection: string; version: number; spaceSlug: string; createdAt: number };
linkedDocuments: Record<string, LinkedDocument>;
}
export const docsSchema: DocSchema<DocsDoc> = {
module: 'docs',
collection: 'links',
version: 1,
init: (): DocsDoc => ({
meta: { module: 'docs', collection: 'links', version: 1, spaceSlug: '', createdAt: Date.now() },
linkedDocuments: {},
}),
migrate: (doc: any) => { if (!doc.linkedDocuments) doc.linkedDocuments = {}; doc.meta.version = 1; return doc; },
};
export function docsDocId(space: string) { return `${space}:docs:links` as const; }

View File

@ -0,0 +1,55 @@
/**
* rForum Local-First Client syncs forum provisioning state.
*/
import { DocumentManager } from '../../shared/local-first/document';
import type { DocumentId } from '../../shared/local-first/document';
import { EncryptedDocStore } from '../../shared/local-first/storage';
import { DocSyncManager } from '../../shared/local-first/sync';
import { DocCrypto } from '../../shared/local-first/crypto';
import { forumSchema, FORUM_DOC_ID } from './schemas';
import type { ForumDoc, ForumInstance } from './schemas';
export class ForumLocalFirstClient {
#documents: DocumentManager;
#store: EncryptedDocStore;
#sync: DocSyncManager;
#initialized = false;
constructor(space: string, docCrypto?: DocCrypto) {
this.#documents = new DocumentManager();
this.#store = new EncryptedDocStore(space, docCrypto);
this.#sync = new DocSyncManager({ documents: this.#documents, store: this.#store });
this.#documents.registerSchema(forumSchema);
}
get isConnected(): boolean { return this.#sync.isConnected; }
async init(): Promise<void> {
if (this.#initialized) return;
await this.#store.open();
const cachedIds = await this.#store.listByModule('forum', 'instances');
const cached = await this.#store.loadMany(cachedIds);
for (const [docId, binary] of cached) this.#documents.open<ForumDoc>(docId, forumSchema, binary);
await this.#sync.preloadSyncStates(cachedIds);
const proto = location.protocol === 'https:' ? 'wss:' : 'ws:';
try { await this.#sync.connect(`${proto}//${location.host}/ws/global`, 'global'); } catch {}
this.#initialized = true;
}
async subscribe(): Promise<ForumDoc | null> {
const docId = FORUM_DOC_ID as DocumentId;
let doc = this.#documents.get<ForumDoc>(docId);
if (!doc) { const b = await this.#store.load(docId); doc = b ? this.#documents.open<ForumDoc>(docId, forumSchema, b) : this.#documents.open<ForumDoc>(docId, forumSchema); }
await this.#sync.subscribe([docId]); return doc ?? null;
}
getDoc(): ForumDoc | undefined { return this.#documents.get<ForumDoc>(FORUM_DOC_ID as DocumentId); }
onChange(cb: (doc: ForumDoc) => void): () => void { return this.#sync.onChange(FORUM_DOC_ID as DocumentId, cb as (doc: any) => void); }
updateInstance(instance: ForumInstance): void {
this.#sync.change<ForumDoc>(FORUM_DOC_ID as DocumentId, `Update ${instance.name}`, (d) => { d.instances[instance.id] = instance; });
}
async disconnect(): Promise<void> { await this.#sync.flush(); this.#sync.disconnect(); }
}

View File

@ -0,0 +1,67 @@
/**
* rMaps Local-First Client persistent annotations, routes, meeting points.
*/
import { DocumentManager } from '../../shared/local-first/document';
import type { DocumentId } from '../../shared/local-first/document';
import { EncryptedDocStore } from '../../shared/local-first/storage';
import { DocSyncManager } from '../../shared/local-first/sync';
import { DocCrypto } from '../../shared/local-first/crypto';
import { mapsSchema, mapsDocId } from './schemas';
import type { MapsDoc, MapAnnotation, SavedRoute, SavedMeetingPoint } from './schemas';
export class MapsLocalFirstClient {
#space: string; #documents: DocumentManager; #store: EncryptedDocStore; #sync: DocSyncManager; #initialized = false;
constructor(space: string, docCrypto?: DocCrypto) {
this.#space = space; this.#documents = new DocumentManager();
this.#store = new EncryptedDocStore(space, docCrypto);
this.#sync = new DocSyncManager({ documents: this.#documents, store: this.#store });
this.#documents.registerSchema(mapsSchema);
}
get isConnected(): boolean { return this.#sync.isConnected; }
async init(): Promise<void> {
if (this.#initialized) return;
await this.#store.open();
const cachedIds = await this.#store.listByModule('maps', 'annotations');
const cached = await this.#store.loadMany(cachedIds);
for (const [docId, binary] of cached) this.#documents.open<MapsDoc>(docId, mapsSchema, binary);
await this.#sync.preloadSyncStates(cachedIds);
const proto = location.protocol === 'https:' ? 'wss:' : 'ws:';
try { await this.#sync.connect(`${proto}//${location.host}/ws/${this.#space}`, this.#space); } catch {}
this.#initialized = true;
}
async subscribe(): Promise<MapsDoc | null> {
const docId = mapsDocId(this.#space) as DocumentId;
let doc = this.#documents.get<MapsDoc>(docId);
if (!doc) { const b = await this.#store.load(docId); doc = b ? this.#documents.open<MapsDoc>(docId, mapsSchema, b) : this.#documents.open<MapsDoc>(docId, mapsSchema); }
await this.#sync.subscribe([docId]); return doc ?? null;
}
getDoc(): MapsDoc | undefined { return this.#documents.get<MapsDoc>(mapsDocId(this.#space) as DocumentId); }
onChange(cb: (doc: MapsDoc) => void): () => void { return this.#sync.onChange(mapsDocId(this.#space) as DocumentId, cb as (doc: any) => void); }
addAnnotation(annotation: MapAnnotation): void {
this.#sync.change<MapsDoc>(mapsDocId(this.#space) as DocumentId, `Add ${annotation.type}`, (d) => { d.annotations[annotation.id] = annotation; });
}
removeAnnotation(id: string): void {
this.#sync.change<MapsDoc>(mapsDocId(this.#space) as DocumentId, `Remove annotation`, (d) => { delete d.annotations[id]; });
}
saveRoute(route: SavedRoute): void {
this.#sync.change<MapsDoc>(mapsDocId(this.#space) as DocumentId, `Save route ${route.name}`, (d) => { d.savedRoutes[route.id] = route; });
}
deleteRoute(id: string): void {
this.#sync.change<MapsDoc>(mapsDocId(this.#space) as DocumentId, `Delete route`, (d) => { delete d.savedRoutes[id]; });
}
setMeetingPoint(point: SavedMeetingPoint): void {
this.#sync.change<MapsDoc>(mapsDocId(this.#space) as DocumentId, `Set meeting point`, (d) => { d.savedMeetingPoints[point.id] = point; });
}
removeMeetingPoint(id: string): void {
this.#sync.change<MapsDoc>(mapsDocId(this.#space) as DocumentId, `Remove meeting point`, (d) => { delete d.savedMeetingPoints[id]; });
}
async disconnect(): Promise<void> { await this.#sync.flush(); this.#sync.disconnect(); }
}

65
modules/rmaps/schemas.ts Normal file
View File

@ -0,0 +1,65 @@
/**
* rMaps Automerge document schemas.
*
* Persistent map annotations, saved routes, and meeting points.
* Ephemeral live location sharing remains via WebSocket rooms.
*
* DocId format: {space}:maps:annotations
*/
import type { DocSchema } from '../../shared/local-first/document';
export interface MapAnnotation {
id: string;
type: 'pin' | 'note' | 'area';
lat: number;
lng: number;
label: string;
authorDid: string | null;
createdAt: number;
}
export interface SavedRoute {
id: string;
name: string;
waypoints: { lat: number; lng: number; label?: string }[];
authorDid: string | null;
createdAt: number;
}
export interface SavedMeetingPoint {
id: string;
name: string;
lat: number;
lng: number;
setBy: string | null;
createdAt: number;
}
export interface MapsDoc {
meta: { module: string; collection: string; version: number; spaceSlug: string; createdAt: number };
annotations: Record<string, MapAnnotation>;
savedRoutes: Record<string, SavedRoute>;
savedMeetingPoints: Record<string, SavedMeetingPoint>;
}
export const mapsSchema: DocSchema<MapsDoc> = {
module: 'maps',
collection: 'annotations',
version: 1,
init: (): MapsDoc => ({
meta: { module: 'maps', collection: 'annotations', version: 1, spaceSlug: '', createdAt: Date.now() },
annotations: {},
savedRoutes: {},
savedMeetingPoints: {},
}),
migrate: (doc: any) => {
if (!doc.annotations) doc.annotations = {};
if (!doc.savedRoutes) doc.savedRoutes = {};
if (!doc.savedMeetingPoints) doc.savedMeetingPoints = {};
doc.meta.version = 1;
return doc;
},
};
export function mapsDocId(space: string) { return `${space}:maps:annotations` as const; }

View File

@ -0,0 +1,62 @@
/**
* rMeets Local-First Client syncs meeting scheduling and history.
*/
import { DocumentManager } from '../../shared/local-first/document';
import type { DocumentId } from '../../shared/local-first/document';
import { EncryptedDocStore } from '../../shared/local-first/storage';
import { DocSyncManager } from '../../shared/local-first/sync';
import { DocCrypto } from '../../shared/local-first/crypto';
import { meetsSchema, meetsDocId } from './schemas';
import type { MeetsDoc, Meeting } from './schemas';
export class MeetsLocalFirstClient {
#space: string; #documents: DocumentManager; #store: EncryptedDocStore; #sync: DocSyncManager; #initialized = false;
constructor(space: string, docCrypto?: DocCrypto) {
this.#space = space; this.#documents = new DocumentManager();
this.#store = new EncryptedDocStore(space, docCrypto);
this.#sync = new DocSyncManager({ documents: this.#documents, store: this.#store });
this.#documents.registerSchema(meetsSchema);
}
get isConnected(): boolean { return this.#sync.isConnected; }
async init(): Promise<void> {
if (this.#initialized) return;
await this.#store.open();
const cachedIds = await this.#store.listByModule('meets', 'meetings');
const cached = await this.#store.loadMany(cachedIds);
for (const [docId, binary] of cached) this.#documents.open<MeetsDoc>(docId, meetsSchema, binary);
await this.#sync.preloadSyncStates(cachedIds);
const proto = location.protocol === 'https:' ? 'wss:' : 'ws:';
try { await this.#sync.connect(`${proto}//${location.host}/ws/${this.#space}`, this.#space); } catch {}
this.#initialized = true;
}
async subscribe(): Promise<MeetsDoc | null> {
const docId = meetsDocId(this.#space) as DocumentId;
let doc = this.#documents.get<MeetsDoc>(docId);
if (!doc) { const b = await this.#store.load(docId); doc = b ? this.#documents.open<MeetsDoc>(docId, meetsSchema, b) : this.#documents.open<MeetsDoc>(docId, meetsSchema); }
await this.#sync.subscribe([docId]); return doc ?? null;
}
getDoc(): MeetsDoc | undefined { return this.#documents.get<MeetsDoc>(meetsDocId(this.#space) as DocumentId); }
onChange(cb: (doc: MeetsDoc) => void): () => void { return this.#sync.onChange(meetsDocId(this.#space) as DocumentId, cb as (doc: any) => void); }
scheduleMeeting(meeting: Meeting): void {
this.#sync.change<MeetsDoc>(meetsDocId(this.#space) as DocumentId, `Schedule ${meeting.title}`, (d) => { d.meetings[meeting.id] = meeting; });
}
cancelMeeting(id: string): void {
this.#sync.change<MeetsDoc>(meetsDocId(this.#space) as DocumentId, `Cancel meeting`, (d) => { delete d.meetings[id]; });
}
joinMeeting(meetingId: string, participantDid: string): void {
this.#sync.change<MeetsDoc>(meetsDocId(this.#space) as DocumentId, `Join meeting`, (d) => {
if (d.meetings[meetingId] && !d.meetings[meetingId].participants.includes(participantDid)) {
d.meetings[meetingId].participants.push(participantDid);
}
});
}
async disconnect(): Promise<void> { await this.#sync.flush(); this.#sync.disconnect(); }
}

45
modules/rmeets/schemas.ts Normal file
View File

@ -0,0 +1,45 @@
/**
* rMeets Automerge document schemas.
*
* Syncs meeting scheduling and history per space.
* Actual video calls run on the Jitsi instance.
*
* DocId format: {space}:meets:meetings
*/
import type { DocSchema } from '../../shared/local-first/document';
export interface Meeting {
id: string;
roomName: string;
title: string;
scheduledAt: number;
hostDid: string | null;
participants: string[];
createdAt: number;
}
export interface MeetsDoc {
meta: { module: string; collection: string; version: number; spaceSlug: string; createdAt: number };
meetings: Record<string, Meeting>;
meetingHistory: { id: string; roomName: string; title: string; startedAt: number; endedAt: number; participantCount: number }[];
}
export const meetsSchema: DocSchema<MeetsDoc> = {
module: 'meets',
collection: 'meetings',
version: 1,
init: (): MeetsDoc => ({
meta: { module: 'meets', collection: 'meetings', version: 1, spaceSlug: '', createdAt: Date.now() },
meetings: {},
meetingHistory: [],
}),
migrate: (doc: any) => {
if (!doc.meetings) doc.meetings = {};
if (!doc.meetingHistory) doc.meetingHistory = [];
doc.meta.version = 1;
return doc;
},
};
export function meetsDocId(space: string) { return `${space}:meets:meetings` as const; }