@agentshouse/mdruntime 0.1.0-alpha.4 → 0.1.0-alpha.40
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/AGENTS.md +191 -0
- package/README.md +44 -0
- package/dist/adapter.d.ts +174 -18
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +2 -1
- package/dist/adapter.js.map +1 -1
- package/dist/bootstrap.d.ts +7 -7
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +14 -9
- package/dist/bootstrap.js.map +1 -1
- package/dist/change-set.d.ts +33 -0
- package/dist/change-set.d.ts.map +1 -0
- package/dist/change-set.js +89 -0
- package/dist/change-set.js.map +1 -0
- package/dist/change.d.ts +10 -0
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js.map +1 -1
- package/dist/client.d.ts +74 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +232 -0
- package/dist/client.js.map +1 -0
- package/dist/collection.d.ts +25 -12
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +113 -31
- package/dist/collection.js.map +1 -1
- package/dist/connection.d.ts +84 -27
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +1685 -336
- package/dist/connection.js.map +1 -1
- package/dist/conversation.d.ts +79 -0
- package/dist/conversation.d.ts.map +1 -0
- package/dist/conversation.js +593 -0
- package/dist/conversation.js.map +1 -0
- package/dist/durability.d.ts +9 -0
- package/dist/durability.d.ts.map +1 -0
- package/dist/durability.js +11 -0
- package/dist/durability.js.map +1 -0
- package/dist/file.d.ts +20 -0
- package/dist/file.d.ts.map +1 -0
- package/dist/file.js +149 -0
- package/dist/file.js.map +1 -0
- package/dist/host.d.ts +14 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +211 -0
- package/dist/host.js.map +1 -0
- package/dist/identity.d.ts +3 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +23 -0
- package/dist/identity.js.map +1 -0
- package/dist/index.d.ts +15 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/memory.d.ts +17 -2
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +201 -15
- package/dist/memory.js.map +1 -1
- package/dist/model.d.ts +55 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/model.js +36 -0
- package/dist/model.js.map +1 -0
- package/dist/observation.d.ts +25 -0
- package/dist/observation.d.ts.map +1 -0
- package/dist/observation.js +92 -0
- package/dist/observation.js.map +1 -0
- package/dist/outcome.d.ts +22 -18
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +1 -11
- package/dist/outcome.js.map +1 -1
- package/dist/overlay.d.ts +2 -0
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +107 -1
- package/dist/overlay.js.map +1 -1
- package/dist/paths.d.ts +0 -1
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +0 -4
- package/dist/paths.js.map +1 -1
- package/dist/query.d.ts +209 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/query.js +467 -0
- package/dist/query.js.map +1 -0
- package/dist/read-side.d.ts +14 -0
- package/dist/read-side.d.ts.map +1 -0
- package/dist/read-side.js +451 -0
- package/dist/read-side.js.map +1 -0
- package/dist/refresh.d.ts +3 -3
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +10 -7
- package/dist/refresh.js.map +1 -1
- package/dist/replica.d.ts +23 -4
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +224 -13
- package/dist/replica.js.map +1 -1
- package/dist/room-draft.d.ts +124 -0
- package/dist/room-draft.d.ts.map +1 -0
- package/dist/room-draft.js +406 -0
- package/dist/room-draft.js.map +1 -0
- package/dist/rows.d.ts +39 -0
- package/dist/rows.d.ts.map +1 -0
- package/dist/rows.js +100 -0
- package/dist/rows.js.map +1 -0
- package/dist/save.d.ts +11 -2
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +116 -128
- package/dist/save.js.map +1 -1
- package/dist/search.d.ts +61 -0
- package/dist/search.d.ts.map +1 -0
- package/dist/search.js +155 -0
- package/dist/search.js.map +1 -0
- package/dist/status.d.ts +9 -2
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +5 -4
- package/dist/status.js.map +1 -1
- package/dist/store.d.ts +89 -18
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +445 -57
- package/dist/store.js.map +1 -1
- package/dist/transfer.d.ts +5 -0
- package/dist/transfer.d.ts.map +1 -1
- package/dist/transfer.js.map +1 -1
- package/dist/wire.d.ts +64 -0
- package/dist/wire.d.ts.map +1 -0
- package/dist/wire.js +144 -0
- package/dist/wire.js.map +1 -0
- package/package.json +15 -13
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/workspace/packages/mdruntime/src/conversation.ts"],"sourcesContent":["import type {\n AcceptedMessage,\n ConversationAction,\n ConversationCards,\n ConversationEvent,\n ConversationHistoryPage,\n ConversationMessage,\n ConversationStream,\n WorkingNote,\n} from './adapter.js';\nimport { conversationChangeSet, type ChangeSet, type MessageChange } from './change-set.js';\nimport { declaredFile, type ChatDraftFile } from './file.js';\nimport type { CallResult } from './outcome.js';\nimport type { DocumentResult, QueryDocument } from './query.js';\nimport type { SearchedMessage } from './search.js';\nimport type { CycleProgress } from './status.js';\nimport type { ChatDraftRecord, HeldConversation, RuntimeStore } from './store.js';\n\nexport type ConversationEntry = {\n conversation: string;\n card: Required<QueryDocument>;\n agent: Required<QueryDocument> | null;\n};\n\nexport type GrowingText = { turn: string; text: string };\n\nexport type ChatDraft = {\n text: string;\n files: readonly ChatDraftFile[];\n};\n\nexport type ConversationOptions = { limit?: number };\n\nexport type ConversationView = {\n conversation: string;\n messages: readonly ConversationMessage[];\n growingText: GrowingText | null;\n workingNote: WorkingNote | null;\n plan: unknown;\n questions: readonly unknown[];\n process: { running: boolean } | null;\n commands: readonly unknown[];\n options: readonly unknown[];\n chatDraft: ChatDraft | null;\n};\n\nexport type ConversationSurface = {\n list(): Promise<readonly ConversationEntry[]>;\n read(conversation: string, options?: ConversationOptions): Promise<ConversationView>;\n create(input: unknown): Promise<CallResult>;\n send(conversation: string): Promise<CallResult>;\n answer(conversation: string, answer: unknown): Promise<CallResult>;\n interrupt(conversation: string): Promise<CallResult>;\n kill(conversation: string): Promise<CallResult>;\n cancel(conversation: string, turn: string): Promise<CallResult>;\n delete(conversation: string): Promise<CallResult>;\n};\n\nexport type ChatDraftSurface = {\n write(conversation: string, draft: ChatDraft): Promise<void>;\n discard(conversation: string): Promise<void>;\n};\n\nexport type ConversationDependencies = {\n cards(): ConversationCards | undefined;\n documents(locator: string): Promise<DocumentResult>;\n call(name: ConversationAction, input: unknown): Promise<CallResult>;\n durably<T>(work: (local: RuntimeStore) => Promise<T>): Promise<T>;\n stream(): ConversationStream | undefined;\n publish(set: ChangeSet): void;\n closed(): boolean;\n};\n\nexport type ConversationStore = {\n surface: ConversationSurface;\n chatDraft: ChatDraftSurface;\n hydrate(held: readonly HeldConversation[], drafts: readonly ChatDraftRecord[]): void;\n reconcile(): Promise<void>;\n catchUp(progress: CycleProgress): Promise<void>;\n current(): boolean;\n reconnected(): void;\n watch(\n conversation: string,\n options: ConversationOptions,\n deliver: (view: ConversationView) => void,\n ): () => void;\n changed(set: ChangeSet): void;\n receive(event: ConversationEvent): void;\n messages(): readonly SearchedMessage[];\n message(conversation: string, identity: string): SearchedMessage | null;\n close(): void;\n};\n\ntype Watcher = {\n notify: (view: ConversationView) => void;\n ready: boolean;\n};\n\ntype Sending = { draft: ChatDraftRecord };\n\ntype Live = {\n growingText: GrowingText | null;\n plan: { turn: string; plan: unknown } | null;\n workingNote: WorkingNote | null;\n questions: readonly unknown[];\n process: { running: boolean } | null;\n commands: readonly unknown[];\n options: readonly unknown[];\n};\n\nconst QUIET: Live = {\n growingText: null,\n plan: null,\n workingNote: null,\n questions: [],\n process: null,\n commands: [],\n options: [],\n};\n\ntype SharedView = {\n conversation: string;\n limit: number | undefined;\n watchers: Set<Watcher>;\n};\n\nfunction ordered(left: AcceptedMessage, right: AcceptedMessage): number {\n if (left.position !== right.position) return left.position < right.position ? -1 : 1;\n if (left.identity !== right.identity) return left.identity < right.identity ? -1 : 1;\n return 0;\n}\n\nfunction merged(\n held: readonly AcceptedMessage[],\n arriving: readonly AcceptedMessage[],\n): readonly AcceptedMessage[] {\n const byIdentity = new Map(held.map((message) => [message.identity, message]));\n for (const message of arriving) byIdentity.set(message.identity, message);\n return [...byIdentity.values()].sort(ordered);\n}\n\nfunction acceptedMessage(value: unknown): AcceptedMessage | null {\n if (typeof value !== 'object' || value === null) return null;\n const record = value as Partial<Record<keyof AcceptedMessage, unknown>>;\n if (\n typeof record.identity !== 'string' ||\n typeof record.turn !== 'string' ||\n typeof record.position !== 'string' ||\n typeof record.text !== 'string'\n ) {\n return null;\n }\n return {\n identity: record.identity,\n turn: record.turn,\n position: record.position,\n text: record.text,\n message: structuredClone(record.message),\n };\n}\n\nfunction historyPage(result: CallResult): ConversationHistoryPage | null {\n if (result.kind !== 'action') return null;\n const answer = result.result;\n if (typeof answer !== 'object' || answer === null) return null;\n const record = answer as { messages?: unknown; next?: unknown };\n if (!Array.isArray(record.messages)) return null;\n if (record.next !== undefined && typeof record.next !== 'string') return null;\n const messages: AcceptedMessage[] = [];\n for (const entry of record.messages) {\n const message = acceptedMessage(entry);\n if (message === null) return null;\n messages.push(message);\n }\n return { messages, ...(record.next === undefined ? {} : { next: record.next }) };\n}\n\nfunction turnOf(result: CallResult): string | null {\n if (result.kind !== 'action') return null;\n const answer = result.result;\n if (typeof answer !== 'object' || answer === null) return null;\n const record = answer as { turn?: unknown };\n return typeof record.turn === 'string' ? record.turn : null;\n}\n\nfunction limitOf(options: ConversationOptions): number | undefined {\n const { limit } = options;\n if (limit === undefined) return undefined;\n if (!Number.isInteger(limit) || limit < 0) {\n throw new Error(`conversation limit must be a non-negative integer, got ${String(limit)}`);\n }\n return limit;\n}\n\nexport function createConversationStore(deps: ConversationDependencies): ConversationStore {\n const messages = new Map<string, readonly AcceptedMessage[]>();\n const reads = new Map<string, string | null>();\n const drafts = new Map<string, ChatDraftRecord>();\n const sending = new Map<string, Sending>();\n const behind = new Set<string>();\n const gaps = new Map<string, number>();\n const lives = new Map<string, Live>();\n const views = new Map<string, SharedView>();\n let replicated: ReadonlySet<string> = new Set();\n let handedReplicated: string | undefined;\n\n function liveOf(conversation: string): Live {\n return lives.get(conversation) ?? QUIET;\n }\n\n function viewOf(conversation: string, limit: number | undefined): ConversationView {\n const held = messages.get(conversation) ?? [];\n const shown = limit === undefined ? held : held.slice(Math.max(0, held.length - limit));\n const draft = drafts.get(conversation);\n const facts = liveOf(conversation);\n return {\n conversation,\n messages: shown.map(({ identity, turn, text, message }) => ({\n identity,\n turn,\n text,\n message: structuredClone(message),\n })),\n growingText: facts.growingText === null ? null : { ...facts.growingText },\n workingNote: facts.workingNote === null ? null : { ...facts.workingNote },\n plan: structuredClone(facts.plan?.plan ?? null),\n questions: structuredClone(facts.questions),\n process: facts.process === null ? null : { ...facts.process },\n commands: structuredClone(facts.commands),\n options: structuredClone(facts.options),\n chatDraft:\n draft === undefined || draft.turn !== null || sending.get(conversation)?.draft === draft\n ? null\n : { text: draft.text, files: [...draft.files] },\n };\n }\n\n function emit(conversation: string) {\n if (deps.closed()) return;\n for (const view of views.values()) {\n if (view.conversation !== conversation) continue;\n const ready = [...view.watchers].filter((watcher) => watcher.ready);\n if (ready.length === 0) continue;\n const value = viewOf(conversation, view.limit);\n for (const watcher of ready) watcher.notify(value);\n }\n }\n\n function live(conversation: string, change: Partial<Live>) {\n lives.set(conversation, { ...liveOf(conversation), ...change });\n emit(conversation);\n }\n\n function sealTurns(conversation: string, turns: ReadonlySet<string>): boolean {\n const held = lives.get(conversation);\n if (held === undefined) return false;\n const growingText =\n held.growingText !== null && turns.has(held.growingText.turn) ? null : held.growingText;\n const plan = held.plan !== null && turns.has(held.plan.turn) ? null : held.plan;\n if (growingText === held.growingText && plan === held.plan) return false;\n lives.set(conversation, { ...held, growingText, plan });\n return true;\n }\n\n function handReplicated() {\n const stream = deps.stream();\n if (stream === undefined) return;\n const held = [...replicated].sort();\n const named = JSON.stringify(held);\n if (named === handedReplicated) return;\n handedReplicated = named;\n stream.replicate(held);\n }\n\n async function documented(): Promise<readonly Required<QueryDocument>[]> {\n const declaration = deps.cards();\n if (declaration === undefined) return [];\n const { documents } = await deps.documents(declaration.locator);\n return documents.filter(\n (document): document is Required<QueryDocument> => typeof document.type === 'string',\n );\n }\n\n function carded(\n documents: readonly Required<QueryDocument>[],\n ): readonly { conversation: string; card: Required<QueryDocument> }[] {\n const declaration = deps.cards();\n if (declaration === undefined) return [];\n const cards: { conversation: string; card: Required<QueryDocument> }[] = [];\n for (const card of documents) {\n if (card.type !== declaration.type) continue;\n const identity = card.fields[declaration.identity];\n if (typeof identity === 'string') cards.push({ conversation: identity, card });\n }\n return cards;\n }\n\n async function remove(leaving: readonly string[]) {\n await deps.durably(async (local) => {\n await local.dropConversations(leaving);\n const removed: MessageChange[] = [];\n const changed: string[] = [];\n for (const conversation of leaving) {\n for (const message of messages.get(conversation) ?? []) {\n removed.push({ conversation, identity: message.identity });\n }\n if (messages.has(conversation) || drafts.has(conversation) || lives.has(conversation)) {\n changed.push(conversation);\n }\n messages.delete(conversation);\n reads.delete(conversation);\n drafts.delete(conversation);\n behind.delete(conversation);\n lives.delete(conversation);\n }\n if (changed.length > 0) deps.publish(conversationChangeSet(removed, changed));\n });\n }\n\n function gap(conversation: string) {\n gaps.set(conversation, (gaps.get(conversation) ?? 0) + 1);\n behind.add(conversation);\n }\n\n function keep(conversation: string, message: AcceptedMessage) {\n return deps.durably(async (local) => {\n if (!replicated.has(conversation)) return;\n const read = reads.get(conversation);\n const advances =\n read !== undefined &&\n !behind.has(conversation) &&\n (read === null || message.position > read);\n const sealed = drafts.get(conversation)?.turn === message.turn;\n await local.keepMessages({\n conversation,\n messages: [message],\n ...(advances ? { read: message.position } : {}),\n sealed,\n });\n messages.set(conversation, merged(messages.get(conversation) ?? [], [message]));\n if (advances) reads.set(conversation, message.position);\n if (sealed) drafts.delete(conversation);\n sealTurns(conversation, new Set([message.turn]));\n deps.publish(\n conversationChangeSet([{ conversation, identity: message.identity }], [conversation]),\n );\n });\n }\n\n async function readHistory(conversation: string): Promise<CallResult | null> {\n const generation = gaps.get(conversation) ?? 0;\n const from = reads.get(conversation);\n const arriving: AcceptedMessage[] = [];\n let after = from ?? undefined;\n for (;;) {\n const result = await deps.call('conversation.history', {\n conversation,\n ...(after === undefined ? {} : { after }),\n });\n const page = historyPage(result);\n if (page === null) return result;\n arriving.push(...page.messages);\n if (page.next === undefined) break;\n after = page.next;\n }\n await deps.durably(async (local) => {\n if (!replicated.has(conversation)) return;\n const newest = arriving.reduce<string | null>(\n (top, message) => (top === null || message.position > top ? message.position : top),\n from ?? null,\n );\n const turn = drafts.get(conversation)?.turn;\n const sealed = arriving.some((message) => message.turn === turn);\n await local.keepMessages({ conversation, messages: arriving, read: newest, sealed });\n messages.set(conversation, merged(messages.get(conversation) ?? [], arriving));\n reads.set(conversation, newest);\n if (sealed) drafts.delete(conversation);\n sealTurns(conversation, new Set(arriving.map((message) => message.turn)));\n if ((gaps.get(conversation) ?? 0) === generation) behind.delete(conversation);\n if (arriving.length > 0 || sealed) {\n deps.publish(\n conversationChangeSet(\n arriving.map((message) => ({ conversation, identity: message.identity })),\n [conversation],\n ),\n );\n }\n });\n return null;\n }\n\n function withDraft(\n conversation: string,\n next: (draft: ChatDraftRecord | undefined) => ChatDraftRecord | null | undefined,\n ): Promise<void> {\n return deps.durably(async (local) => {\n const following = next(drafts.get(conversation));\n if (following === undefined) return;\n if (following === null) {\n await local.dropChatDraft(conversation);\n drafts.delete(conversation);\n } else {\n await local.keepChatDraft(following);\n drafts.set(conversation, following);\n }\n deps.publish(conversationChangeSet([], [conversation]));\n });\n }\n\n function invoke(\n name: ConversationAction,\n conversation: string,\n extra: Readonly<Record<string, unknown>> = {},\n ): Promise<CallResult> {\n return deps.call(name, { conversation, ...extra });\n }\n\n const surface: ConversationSurface = {\n async list() {\n const declaration = deps.cards();\n if (declaration === undefined) return [];\n const documents = await documented();\n const entries: ConversationEntry[] = carded(documents).map(({ conversation, card }) => ({\n conversation,\n card,\n agent:\n documents.find((document) =>\n document.backlinks.some(\n (backlink) => backlink.path === card.path && backlink.field === declaration.agent,\n ),\n ) ?? null,\n }));\n entries.sort((left, right) => (left.conversation < right.conversation ? -1 : 1));\n return entries;\n },\n async read(conversation: string, options: ConversationOptions = {}) {\n const limit = limitOf(options);\n return deps.durably(() => Promise.resolve(viewOf(conversation, limit)));\n },\n create(input: unknown) {\n return deps.call('conversation.create', input);\n },\n async send(conversation: string) {\n const draft = drafts.get(conversation);\n const pending: Sending | undefined = draft === undefined ? undefined : { draft };\n if (pending !== undefined) {\n sending.set(conversation, pending);\n deps.publish(conversationChangeSet([], [conversation]));\n }\n try {\n const files = await Promise.all(\n (draft?.files ?? []).map(async (file) => ({\n ...(await declaredFile(file)),\n blob: file.blob,\n })),\n );\n const result = await invoke('conversation.send', conversation, {\n text: draft?.text ?? '',\n files,\n });\n const turn = turnOf(result);\n if (turn === null || draft === undefined) return result;\n await withDraft(conversation, (current) => {\n if (current !== draft) return undefined;\n const sealed = (messages.get(conversation) ?? []).some(\n (message) => message.turn === turn,\n );\n return sealed ? null : { ...draft, turn };\n });\n return result;\n } finally {\n if (pending !== undefined && sending.get(conversation) === pending) {\n sending.delete(conversation);\n if (drafts.get(conversation) === draft) {\n deps.publish(conversationChangeSet([], [conversation]));\n }\n }\n }\n },\n answer(conversation: string, answer: unknown) {\n return invoke('conversation.answer', conversation, { answer });\n },\n interrupt(conversation: string) {\n return invoke('conversation.interrupt', conversation);\n },\n kill(conversation: string) {\n return invoke('conversation.kill', conversation);\n },\n async cancel(conversation: string, turn: string) {\n const result = await invoke('conversation.cancel', conversation, { turn });\n if (result.kind !== 'action') return result;\n await withDraft(conversation, (current) =>\n current === undefined || current.turn !== turn ? undefined : { ...current, turn: null },\n );\n return result;\n },\n delete(conversation: string) {\n return invoke('conversation.delete', conversation);\n },\n };\n\n const chatDraft: ChatDraftSurface = {\n write(conversation: string, draft: ChatDraft) {\n return withDraft(conversation, () => ({\n conversation,\n text: draft.text,\n files: [...draft.files],\n turn: null,\n }));\n },\n discard(conversation: string) {\n return withDraft(conversation, (current) => (current === undefined ? undefined : null));\n },\n };\n\n return {\n surface,\n chatDraft,\n hydrate(held, stored) {\n messages.clear();\n reads.clear();\n for (const entry of held) {\n messages.set(entry.conversation, [...entry.messages].sort(ordered));\n if (entry.read !== undefined) reads.set(entry.conversation, entry.read);\n }\n drafts.clear();\n for (const record of stored) drafts.set(record.conversation, record);\n },\n async reconcile() {\n const named = new Set(carded(await documented()).map((entry) => entry.conversation));\n const leaving = [\n ...new Set([...replicated, ...messages.keys(), ...reads.keys(), ...drafts.keys()]),\n ].filter((conversation) => !named.has(conversation));\n for (const conversation of named) {\n if (!replicated.has(conversation)) behind.add(conversation);\n }\n replicated = named;\n handReplicated();\n if (leaving.length > 0) await remove(leaving);\n },\n async catchUp(progress) {\n for (const conversation of [...behind].sort()) {\n if (deps.closed()) return;\n if (!replicated.has(conversation)) continue;\n const failed = await readHistory(conversation);\n if (failed?.kind === 'transport-uncertainty') progress.reachable = false;\n }\n if (behind.size > 0) progress.complete = false;\n },\n current() {\n return behind.size === 0;\n },\n reconnected() {\n for (const conversation of replicated) gap(conversation);\n },\n watch(conversation, options, deliver) {\n const limit = limitOf(options);\n const key = JSON.stringify([conversation, limit ?? null]);\n const view = views.get(key) ?? { conversation, limit, watchers: new Set<Watcher>() };\n views.set(key, view);\n const watcher: Watcher = { notify: deliver, ready: false };\n view.watchers.add(watcher);\n void deps\n .durably(() => Promise.resolve())\n .then(() => {\n if (deps.closed() || !view.watchers.has(watcher)) return;\n watcher.ready = true;\n watcher.notify(viewOf(conversation, limit));\n })\n .catch(() => {});\n return () => {\n if (!view.watchers.delete(watcher)) return;\n if (view.watchers.size === 0) views.delete(key);\n };\n },\n changed(set) {\n for (const conversation of set.conversations) emit(conversation);\n },\n receive(event) {\n if (deps.closed()) return;\n const { conversation } = event;\n if (event.kind === 'gap') {\n if (replicated.has(conversation)) gap(conversation);\n if (lives.delete(conversation)) emit(conversation);\n return;\n }\n if (event.kind === 'growing-text') {\n live(conversation, { growingText: { turn: event.turn, text: event.text } });\n return;\n }\n if (event.kind === 'plan') {\n live(conversation, { plan: { turn: event.turn, plan: structuredClone(event.plan) } });\n return;\n }\n if (event.kind === 'working-note') {\n live(conversation, {\n workingNote:\n event.note === null ? null : { noteId: event.note.noteId, text: event.note.text },\n });\n return;\n }\n if (event.kind === 'questions') {\n live(conversation, { questions: structuredClone(event.questions) });\n return;\n }\n if (event.kind === 'process') {\n live(conversation, { process: { running: event.running } });\n return;\n }\n if (event.kind === 'commands') {\n live(conversation, { commands: structuredClone(event.commands) });\n return;\n }\n if (event.kind === 'options') {\n live(conversation, { options: structuredClone(event.options) });\n return;\n }\n const arriving = acceptedMessage(event.message);\n if (arriving === null) return;\n if (replicated.has(conversation)) {\n void keep(conversation, arriving).catch(() => {});\n return;\n }\n if (sealTurns(conversation, new Set([arriving.turn]))) emit(conversation);\n },\n messages() {\n return [...messages].flatMap(([conversation, held]) =>\n held.map(({ identity, text }) => ({ conversation, identity, text })),\n );\n },\n message(conversation, identity) {\n const held = messages.get(conversation)?.find((message) => message.identity === identity);\n return held === undefined ? null : { conversation, identity, text: held.text };\n },\n close() {\n views.clear();\n lives.clear();\n },\n };\n}\n"],"names":["conversationChangeSet","declaredFile","QUIET","growingText","plan","workingNote","questions","process","commands","options","ordered","left","right","position","identity","merged","held","arriving","byIdentity","Map","map","message","set","values","sort","acceptedMessage","value","record","turn","text","structuredClone","historyPage","result","kind","answer","Array","isArray","messages","next","undefined","entry","push","turnOf","limitOf","limit","Number","isInteger","Error","String","createConversationStore","deps","reads","drafts","sending","behind","Set","gaps","lives","views","replicated","handedReplicated","liveOf","conversation","get","viewOf","shown","slice","Math","max","length","draft","facts","chatDraft","files","emit","closed","view","ready","watchers","filter","watcher","notify","live","change","sealTurns","turns","has","handReplicated","stream","named","JSON","stringify","replicate","documented","declaration","cards","documents","locator","document","type","carded","card","fields","remove","leaving","durably","local","dropConversations","removed","changed","delete","publish","gap","add","keep","read","advances","sealed","keepMessages","readHistory","generation","from","after","call","page","newest","reduce","top","some","withDraft","following","dropChatDraft","keepChatDraft","invoke","name","extra","surface","list","entries","agent","find","backlinks","backlink","path","field","Promise","resolve","create","input","send","pending","all","file","blob","current","interrupt","kill","cancel","write","discard","hydrate","stored","clear","reconcile","keys","catchUp","progress","failed","reachable","size","complete","reconnected","watch","deliver","key","then","catch","conversations","receive","event","note","noteId","running","flatMap","close"],"mappings":"AAUA,SAASA,qBAAqB,QAA4C,kBAAkB;AAC5F,SAASC,YAAY,QAA4B,YAAY;AAmG7D,MAAMC,QAAc;IAClBC,aAAa;IACbC,MAAM;IACNC,aAAa;IACbC,WAAW,EAAE;IACbC,SAAS;IACTC,UAAU,EAAE;IACZC,SAAS,EAAE;AACb;AAQA,SAASC,QAAQC,IAAqB,EAAEC,KAAsB;IAC5D,IAAID,KAAKE,QAAQ,KAAKD,MAAMC,QAAQ,EAAE,OAAOF,KAAKE,QAAQ,GAAGD,MAAMC,QAAQ,GAAG,CAAC,IAAI;IACnF,IAAIF,KAAKG,QAAQ,KAAKF,MAAME,QAAQ,EAAE,OAAOH,KAAKG,QAAQ,GAAGF,MAAME,QAAQ,GAAG,CAAC,IAAI;IACnF,OAAO;AACT;AAEA,SAASC,OACPC,IAAgC,EAChCC,QAAoC;IAEpC,MAAMC,aAAa,IAAIC,IAAIH,KAAKI,GAAG,CAAC,CAACC,UAAY;YAACA,QAAQP,QAAQ;YAAEO;SAAQ;IAC5E,KAAK,MAAMA,WAAWJ,SAAUC,WAAWI,GAAG,CAACD,QAAQP,QAAQ,EAAEO;IACjE,OAAO;WAAIH,WAAWK,MAAM;KAAG,CAACC,IAAI,CAACd;AACvC;AAEA,SAASe,gBAAg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AChDA,KAAKI,GAAG,CAAC,CAAC,EAAEN,QAAQ,EAAEe,IAAI,EAAE,GAAM,CAAA;wBAAEiC;wBAAchD;wBAAUe;oBAAK,CAAA;QAErE;QACAR,SAAQyC,YAAY,EAAEhD,QAAQ;YAC5B,MAAME,OAAOqB,SAAS0B,GAAG,CAACD,eAAe8E,KAAK,CAACvH,UAAYA,QAAQP,QAAQ,KAAKA;YAChF,OAAOE,SAASuB,YAAY,OAAO;gBAAEuB;gBAAchD;gBAAUe,MAAMb,KAAKa,IAAI;YAAC;QAC/E;QACA2J;YACE9H,MAAMwG,KAAK;YACXzG,MAAMyG,KAAK;QACb;IACF;AACF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type RuntimeShell = {
|
|
2
|
+
persistentStorage: boolean;
|
|
3
|
+
generation: string | null;
|
|
4
|
+
keepGeneration(value: string): void | Promise<void>;
|
|
5
|
+
dropGeneration(): void | Promise<void>;
|
|
6
|
+
};
|
|
7
|
+
export declare function detectStorageLoss(stored: string | null, reported: string | null): string | null;
|
|
8
|
+
export declare function nextGeneration(stored: string | null, reported: string | null): string;
|
|
9
|
+
//# sourceMappingURL=durability.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"durability.d.ts","sourceRoot":"","sources":["../src/durability.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,YAAY,GAAG;IACzB,iBAAiB,EAAE,OAAO,CAAC;IAC3B,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACpD,cAAc,IAAI,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACxC,CAAC;AAEF,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,GAAG,IAAI,CAG/F;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,CAGrF"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const STORAGE_LOSS_CODE = 'storage-loss';
|
|
2
|
+
export function detectStorageLoss(stored, reported) {
|
|
3
|
+
if (reported === null || reported === stored) return null;
|
|
4
|
+
return STORAGE_LOSS_CODE;
|
|
5
|
+
}
|
|
6
|
+
export function nextGeneration(stored, reported) {
|
|
7
|
+
if (stored !== null && (reported === null || reported === stored)) return stored;
|
|
8
|
+
return crypto.randomUUID();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
//# sourceMappingURL=durability.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/workspace/packages/mdruntime/src/durability.ts"],"sourcesContent":["const STORAGE_LOSS_CODE = 'storage-loss';\n\nexport type RuntimeShell = {\n persistentStorage: boolean;\n generation: string | null;\n keepGeneration(value: string): void | Promise<void>;\n dropGeneration(): void | Promise<void>;\n};\n\nexport function detectStorageLoss(stored: string | null, reported: string | null): string | null {\n if (reported === null || reported === stored) return null;\n return STORAGE_LOSS_CODE;\n}\n\nexport function nextGeneration(stored: string | null, reported: string | null): string {\n if (stored !== null && (reported === null || reported === stored)) return stored;\n return crypto.randomUUID();\n}\n"],"names":["STORAGE_LOSS_CODE","detectStorageLoss","stored","reported","nextGeneration","crypto","randomUUID"],"mappings":"AAAA,MAAMA,oBAAoB;AAS1B,OAAO,SAASC,kBAAkBC,MAAqB,EAAEC,QAAuB;IAC9E,IAAIA,aAAa,QAAQA,aAAaD,QAAQ,OAAO;IACrD,OAAOF;AACT;AAEA,OAAO,SAASI,eAAeF,MAAqB,EAAEC,QAAuB;IAC3E,IAAID,WAAW,QAASC,CAAAA,aAAa,QAAQA,aAAaD,MAAK,GAAI,OAAOA;IAC1E,OAAOG,OAAOC,UAAU;AAC1B"}
|
package/dist/file.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export type ChatDraftFile = {
|
|
2
|
+
name: string;
|
|
3
|
+
blob: Blob;
|
|
4
|
+
};
|
|
5
|
+
export type FileDeclaration = {
|
|
6
|
+
name: string;
|
|
7
|
+
mediaType: string;
|
|
8
|
+
byteLength: number;
|
|
9
|
+
contentHash: string;
|
|
10
|
+
width?: number;
|
|
11
|
+
height?: number;
|
|
12
|
+
};
|
|
13
|
+
export type Extent = {
|
|
14
|
+
width: number;
|
|
15
|
+
height: number;
|
|
16
|
+
};
|
|
17
|
+
export declare function extentOf(bytes: Uint8Array): Extent | null;
|
|
18
|
+
export declare function hexDigest(digest: ArrayBuffer): string;
|
|
19
|
+
export declare function declaredFile(file: ChatDraftFile): Promise<FileDeclaration>;
|
|
20
|
+
//# sourceMappingURL=file.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file.d.ts","sourceRoot":"","sources":["../src/file.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,GAAG;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,IAAI,CAAC;CACZ,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AA2GvD,wBAAgB,QAAQ,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,GAAG,IAAI,CAQzD;AAED,wBAAgB,SAAS,CAAC,MAAM,EAAE,WAAW,GAAG,MAAM,CAErD;AAED,wBAAsB,YAAY,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO,CAAC,eAAe,CAAC,CAUhF"}
|
package/dist/file.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
const PNG_SIGNATURE = [
|
|
2
|
+
0x89,
|
|
3
|
+
0x50,
|
|
4
|
+
0x4e,
|
|
5
|
+
0x47,
|
|
6
|
+
0x0d,
|
|
7
|
+
0x0a,
|
|
8
|
+
0x1a,
|
|
9
|
+
0x0a
|
|
10
|
+
];
|
|
11
|
+
const WEBP_SYNC = [
|
|
12
|
+
0x9d,
|
|
13
|
+
0x01,
|
|
14
|
+
0x2a
|
|
15
|
+
];
|
|
16
|
+
const SHAPELESS_JPEG_MARKERS = new Set([
|
|
17
|
+
0xc4,
|
|
18
|
+
0xc8,
|
|
19
|
+
0xcc
|
|
20
|
+
]);
|
|
21
|
+
const BMFF_CONTAINERS = new Set([
|
|
22
|
+
'meta',
|
|
23
|
+
'iprp',
|
|
24
|
+
'ipco'
|
|
25
|
+
]);
|
|
26
|
+
function starts(bytes, signature, at = 0) {
|
|
27
|
+
return signature.every((value, index)=>bytes[at + index] === value);
|
|
28
|
+
}
|
|
29
|
+
function text(bytes, at, length) {
|
|
30
|
+
return String.fromCharCode(...bytes.subarray(at, at + length));
|
|
31
|
+
}
|
|
32
|
+
function bigEndian(bytes, at, length) {
|
|
33
|
+
let value = 0;
|
|
34
|
+
for(let index = 0; index < length; index += 1)value = value * 256 + (bytes[at + index] ?? 0);
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
function littleEndian(bytes, at, length) {
|
|
38
|
+
let value = 0;
|
|
39
|
+
for(let index = length - 1; index >= 0; index -= 1)value = value * 256 + (bytes[at + index] ?? 0);
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
function pngExtent(bytes) {
|
|
43
|
+
if (bytes.length < 24 || !starts(bytes, PNG_SIGNATURE)) return null;
|
|
44
|
+
if (text(bytes, 12, 4) !== 'IHDR') return null;
|
|
45
|
+
return {
|
|
46
|
+
width: bigEndian(bytes, 16, 4),
|
|
47
|
+
height: bigEndian(bytes, 20, 4)
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function gifExtent(bytes) {
|
|
51
|
+
if (bytes.length < 10 || text(bytes, 0, 3) !== 'GIF') return null;
|
|
52
|
+
return {
|
|
53
|
+
width: littleEndian(bytes, 6, 2),
|
|
54
|
+
height: littleEndian(bytes, 8, 2)
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function jpegExtent(bytes) {
|
|
58
|
+
if (bytes.length < 4 || bytes[0] !== 0xff || bytes[1] !== 0xd8) return null;
|
|
59
|
+
let at = 2;
|
|
60
|
+
while(at + 9 < bytes.length){
|
|
61
|
+
if (bytes[at] !== 0xff) return null;
|
|
62
|
+
const marker = bytes[at + 1] ?? 0;
|
|
63
|
+
if (marker === 0xd8 || marker === 0x01 || marker >= 0xd0 && marker <= 0xd7) {
|
|
64
|
+
at += 2;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const length = bigEndian(bytes, at + 2, 2);
|
|
68
|
+
if (marker >= 0xc0 && marker <= 0xcf && !SHAPELESS_JPEG_MARKERS.has(marker)) {
|
|
69
|
+
return {
|
|
70
|
+
width: bigEndian(bytes, at + 7, 2),
|
|
71
|
+
height: bigEndian(bytes, at + 5, 2)
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
if (length < 2) return null;
|
|
75
|
+
at += 2 + length;
|
|
76
|
+
}
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
function webpExtent(bytes) {
|
|
80
|
+
if (bytes.length < 30 || text(bytes, 0, 4) !== 'RIFF' || text(bytes, 8, 4) !== 'WEBP') return null;
|
|
81
|
+
const chunk = text(bytes, 12, 4);
|
|
82
|
+
if (chunk === 'VP8X') {
|
|
83
|
+
return {
|
|
84
|
+
width: littleEndian(bytes, 24, 3) + 1,
|
|
85
|
+
height: littleEndian(bytes, 27, 3) + 1
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
if (chunk === 'VP8 ' && starts(bytes, WEBP_SYNC, 23)) {
|
|
89
|
+
return {
|
|
90
|
+
width: littleEndian(bytes, 26, 2) & 0x3fff,
|
|
91
|
+
height: littleEndian(bytes, 28, 2) & 0x3fff
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
if (chunk === 'VP8L' && bytes[20] === 0x2f) {
|
|
95
|
+
const packed = littleEndian(bytes, 21, 4);
|
|
96
|
+
return {
|
|
97
|
+
width: (packed & 0x3fff) + 1,
|
|
98
|
+
height: (packed >>> 14 & 0x3fff) + 1
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
function spatialExtent(bytes, from, to) {
|
|
104
|
+
let at = from;
|
|
105
|
+
while(at + 8 <= to){
|
|
106
|
+
const size = bigEndian(bytes, at, 4);
|
|
107
|
+
const name = text(bytes, at + 4, 4);
|
|
108
|
+
const end = size === 0 ? to : at + size;
|
|
109
|
+
if (size === 1 || end <= at || end > to) return null;
|
|
110
|
+
if (name === 'ispe') {
|
|
111
|
+
if (end < at + 20) return null;
|
|
112
|
+
return {
|
|
113
|
+
width: bigEndian(bytes, at + 12, 4),
|
|
114
|
+
height: bigEndian(bytes, at + 16, 4)
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
if (BMFF_CONTAINERS.has(name)) {
|
|
118
|
+
const inner = spatialExtent(bytes, at + (name === 'meta' ? 12 : 8), end);
|
|
119
|
+
if (inner !== null) return inner;
|
|
120
|
+
}
|
|
121
|
+
at = end;
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
function bmffExtent(bytes) {
|
|
126
|
+
if (bytes.length < 16 || text(bytes, 4, 4) !== 'ftyp') return null;
|
|
127
|
+
return spatialExtent(bytes, 0, bytes.length);
|
|
128
|
+
}
|
|
129
|
+
export function extentOf(bytes) {
|
|
130
|
+
return pngExtent(bytes) ?? jpegExtent(bytes) ?? gifExtent(bytes) ?? webpExtent(bytes) ?? bmffExtent(bytes);
|
|
131
|
+
}
|
|
132
|
+
export function hexDigest(digest) {
|
|
133
|
+
return [
|
|
134
|
+
...new Uint8Array(digest)
|
|
135
|
+
].map((value)=>value.toString(16).padStart(2, '0')).join('');
|
|
136
|
+
}
|
|
137
|
+
export async function declaredFile(file) {
|
|
138
|
+
const bytes = new Uint8Array(await file.blob.arrayBuffer());
|
|
139
|
+
const extent = file.blob.type.startsWith('image/') ? extentOf(bytes) : null;
|
|
140
|
+
return {
|
|
141
|
+
name: file.name,
|
|
142
|
+
mediaType: file.blob.type,
|
|
143
|
+
byteLength: bytes.byteLength,
|
|
144
|
+
contentHash: hexDigest(await crypto.subtle.digest('SHA-256', bytes)),
|
|
145
|
+
...extent === null ? {} : extent
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
//# sourceMappingURL=file.js.map
|
package/dist/file.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/workspace/packages/mdruntime/src/file.ts"],"sourcesContent":["export type ChatDraftFile = {\n name: string;\n blob: Blob;\n};\n\nexport type FileDeclaration = {\n name: string;\n mediaType: string;\n byteLength: number;\n contentHash: string;\n width?: number;\n height?: number;\n};\n\nexport type Extent = { width: number; height: number };\n\nconst PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];\nconst WEBP_SYNC = [0x9d, 0x01, 0x2a];\nconst SHAPELESS_JPEG_MARKERS = new Set([0xc4, 0xc8, 0xcc]);\nconst BMFF_CONTAINERS = new Set(['meta', 'iprp', 'ipco']);\n\nfunction starts(bytes: Uint8Array, signature: readonly number[], at = 0): boolean {\n return signature.every((value, index) => bytes[at + index] === value);\n}\n\nfunction text(bytes: Uint8Array, at: number, length: number): string {\n return String.fromCharCode(...bytes.subarray(at, at + length));\n}\n\nfunction bigEndian(bytes: Uint8Array, at: number, length: number): number {\n let value = 0;\n for (let index = 0; index < length; index += 1) value = value * 256 + (bytes[at + index] ?? 0);\n return value;\n}\n\nfunction littleEndian(bytes: Uint8Array, at: number, length: number): number {\n let value = 0;\n for (let index = length - 1; index >= 0; index -= 1)\n value = value * 256 + (bytes[at + index] ?? 0);\n return value;\n}\n\nfunction pngExtent(bytes: Uint8Array): Extent | null {\n if (bytes.length < 24 || !starts(bytes, PNG_SIGNATURE)) return null;\n if (text(bytes, 12, 4) !== 'IHDR') return null;\n return { width: bigEndian(bytes, 16, 4), height: bigEndian(bytes, 20, 4) };\n}\n\nfunction gifExtent(bytes: Uint8Array): Extent | null {\n if (bytes.length < 10 || text(bytes, 0, 3) !== 'GIF') return null;\n return { width: littleEndian(bytes, 6, 2), height: littleEndian(bytes, 8, 2) };\n}\n\nfunction jpegExtent(bytes: Uint8Array): Extent | null {\n if (bytes.length < 4 || bytes[0] !== 0xff || bytes[1] !== 0xd8) return null;\n let at = 2;\n while (at + 9 < bytes.length) {\n if (bytes[at] !== 0xff) return null;\n const marker = bytes[at + 1] ?? 0;\n if (marker === 0xd8 || marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) {\n at += 2;\n continue;\n }\n const length = bigEndian(bytes, at + 2, 2);\n if (marker >= 0xc0 && marker <= 0xcf && !SHAPELESS_JPEG_MARKERS.has(marker)) {\n return { width: bigEndian(bytes, at + 7, 2), height: bigEndian(bytes, at + 5, 2) };\n }\n if (length < 2) return null;\n at += 2 + length;\n }\n return null;\n}\n\nfunction webpExtent(bytes: Uint8Array): Extent | null {\n if (bytes.length < 30 || text(bytes, 0, 4) !== 'RIFF' || text(bytes, 8, 4) !== 'WEBP')\n return null;\n const chunk = text(bytes, 12, 4);\n if (chunk === 'VP8X') {\n return {\n width: littleEndian(bytes, 24, 3) + 1,\n height: littleEndian(bytes, 27, 3) + 1,\n };\n }\n if (chunk === 'VP8 ' && starts(bytes, WEBP_SYNC, 23)) {\n return {\n width: littleEndian(bytes, 26, 2) & 0x3fff,\n height: littleEndian(bytes, 28, 2) & 0x3fff,\n };\n }\n if (chunk === 'VP8L' && bytes[20] === 0x2f) {\n const packed = littleEndian(bytes, 21, 4);\n return { width: (packed & 0x3fff) + 1, height: ((packed >>> 14) & 0x3fff) + 1 };\n }\n return null;\n}\n\nfunction spatialExtent(bytes: Uint8Array, from: number, to: number): Extent | null {\n let at = from;\n while (at + 8 <= to) {\n const size = bigEndian(bytes, at, 4);\n const name = text(bytes, at + 4, 4);\n const end = size === 0 ? to : at + size;\n if (size === 1 || end <= at || end > to) return null;\n if (name === 'ispe') {\n if (end < at + 20) return null;\n return { width: bigEndian(bytes, at + 12, 4), height: bigEndian(bytes, at + 16, 4) };\n }\n if (BMFF_CONTAINERS.has(name)) {\n const inner = spatialExtent(bytes, at + (name === 'meta' ? 12 : 8), end);\n if (inner !== null) return inner;\n }\n at = end;\n }\n return null;\n}\n\nfunction bmffExtent(bytes: Uint8Array): Extent | null {\n if (bytes.length < 16 || text(bytes, 4, 4) !== 'ftyp') return null;\n return spatialExtent(bytes, 0, bytes.length);\n}\n\nexport function extentOf(bytes: Uint8Array): Extent | null {\n return (\n pngExtent(bytes) ??\n jpegExtent(bytes) ??\n gifExtent(bytes) ??\n webpExtent(bytes) ??\n bmffExtent(bytes)\n );\n}\n\nexport function hexDigest(digest: ArrayBuffer): string {\n return [...new Uint8Array(digest)].map((value) => value.toString(16).padStart(2, '0')).join('');\n}\n\nexport async function declaredFile(file: ChatDraftFile): Promise<FileDeclaration> {\n const bytes = new Uint8Array(await file.blob.arrayBuffer());\n const extent = file.blob.type.startsWith('image/') ? extentOf(bytes) : null;\n return {\n name: file.name,\n mediaType: file.blob.type,\n byteLength: bytes.byteLength,\n contentHash: hexDigest(await crypto.subtle.digest('SHA-256', bytes)),\n ...(extent === null ? {} : extent),\n };\n}\n"],"names":["PNG_SIGNATURE","WEBP_SYNC","SHAPELESS_JPEG_MARKERS","Set","BMFF_CONTAINERS","starts","bytes","signature","at","every","value","index","text","length","String","fromCharCode","subarray","bigEndian","littleEndian","pngExtent","width","height","gifExtent","jpegExtent","marker","has","webpExtent","chunk","packed","spatialExtent","from","to","size","name","end","inner","bmffExtent","extentOf","hexDigest","digest","Uint8Array","map","toString","padStart","join","declaredFile","file","blob","arrayBuffer","extent","type","startsWith","mediaType","byteLength","contentHash","crypto","subtle"],"mappings":"AAgBA,MAAMA,gBAAgB;IAAC;IAAM;IAAM;IAAM;IAAM;IAAM;IAAM;IAAM;CAAK;AACtE,MAAMC,YAAY;IAAC;IAAM;IAAM;CAAK;AACpC,MAAMC,yBAAyB,IAAIC,IAAI;IAAC;IAAM;IAAM;CAAK;AACzD,MAAMC,kBAAkB,IAAID,IAAI;IAAC;IAAQ;IAAQ;CAAO;AAExD,SAASE,OAAOC,KAAiB,EAAEC,SAA4B,EAAEC,KAAK,CAAC;IACrE,OAAOD,UAAUE,KAAK,CAAC,CAACC,OAAOC,QAAUL,KAAK,CAACE,KAAKG,MAAM,KAAKD;AACjE;AAEA,SAASE,KAAKN,KAAiB,EAAEE,EAAU,EAAEK,MAAc;IACzD,OAAOC,OAAOC,YAAY,IAAIT,MAAMU,QAAQ,CAACR,IAAIA,KAAKK;AACxD;AAEA,SAASI,UAAUX,KAAiB,EAAEE,EAAU,EAAEK,MAAc;IAC9D,IAAIH,QAAQ;IACZ,IAAK,IAAIC,QAAQ,GAAGA,QAAQE,QAAQF,SAAS,EAAGD,QAAQA,QAAQ,MAAOJ,CAAAA,KAAK,CAACE,KAAKG,MAAM,IAAI,CAAA;IAC5F,OAAOD;AACT;AAEA,SAASQ,aAAaZ,KAAiB,EAAEE,EAAU,EAAEK,MAAc;IACjE,IAAIH,QAAQ;IACZ,IAAK,IAAIC,QAAQE,SAAS,GAAGF,SAAS,GAAGA,SAAS,EAChDD,QAAQA,QAAQ,MAAOJ,CAAAA,KAAK,CAACE,KAAKG,MAAM,IAAI,CAAA;IAC9C,OAAOD;AACT;AAEA,SAASS,UAAUb,KAAiB;IAClC,IAAIA,MAAMO,MAAM,GAAG,MAAM,CAACR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ACP;IACA,OAAO;AACT;AAEA,SAASE,WAAW9B,KAAiB;IACnC,IAAIA,MAAMO,MAAM,GAAG,MAAMD,KAAKN,OAAO,GAAG,OAAO,QAAQ,OAAO;IAC9D,OAAOuB,cAAcvB,OAAO,GAAGA,MAAMO,MAAM;AAC7C;AAEA,OAAO,SAASwB,SAAS/B,KAAiB;IACxC,OACEa,UAAUb,UACViB,WAAWjB,UACXgB,UAAUhB,UACVoB,WAAWpB,UACX8B,WAAW9B;AAEf;AAEA,OAAO,SAASgC,UAAUC,MAAmB;IAC3C,OAAO;WAAI,IAAIC,WAAWD;KAAQ,CAACE,GAAG,CAAC,CAAC/B,QAAUA,MAAMgC,QAAQ,CAAC,IAAIC,QAAQ,CAAC,GAAG,MAAMC,IAAI,CAAC;AAC9F;AAEA,OAAO,eAAeC,aAAaC,IAAmB;IACpD,MAAMxC,QAAQ,IAAIkC,WAAW,MAAMM,KAAKC,IAAI,CAACC,WAAW;IACxD,MAAMC,SAASH,KAAKC,IAAI,CAACG,IAAI,CAACC,UAAU,CAAC,YAAYd,SAAS/B,SAAS;IACvE,OAAO;QACL2B,MAAMa,KAAKb,IAAI;QACfmB,WAAWN,KAAKC,IAAI,CAACG,IAAI;QACzBG,YAAY/C,MAAM+C,UAAU;QAC5BC,aAAahB,UAAU,MAAMiB,OAAOC,MAAM,CAACjB,MAAM,CAAC,WAAWjC;QAC7D,GAAI2C,WAAW,OAAO,CAAC,IAAIA,MAAM;IACnC;AACF"}
|
package/dist/host.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type MdRuntimeOptions } from './connection.js';
|
|
2
|
+
import { type RuntimePort } from './wire.js';
|
|
3
|
+
export type MdRuntimeHostOptions = MdRuntimeOptions & {
|
|
4
|
+
deploymentFacts?: unknown;
|
|
5
|
+
};
|
|
6
|
+
export type MdRuntimeHost = {
|
|
7
|
+
replaceDeploymentFacts(value: unknown): void;
|
|
8
|
+
attach(port: RuntimePort, app: string, release?: string): () => void;
|
|
9
|
+
endAuthorization(): Promise<void>;
|
|
10
|
+
stop(): Promise<void>;
|
|
11
|
+
};
|
|
12
|
+
export declare function serveMdRuntime(options: MdRuntimeHostOptions): MdRuntimeHost;
|
|
13
|
+
export type { RuntimePort } from './wire.js';
|
|
14
|
+
//# sourceMappingURL=host.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"host.d.ts","sourceRoot":"","sources":["../src/host.ts"],"names":[],"mappings":"AACA,OAAO,EAIL,KAAK,gBAAgB,EAEtB,MAAM,iBAAiB,CAAC;AAKzB,OAAO,EAML,KAAK,WAAW,EAIjB,MAAM,WAAW,CAAC;AAEnB,MAAM,MAAM,oBAAoB,GAAG,gBAAgB,GAAG;IAAE,eAAe,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAEpF,MAAM,MAAM,aAAa,GAAG;IAC1B,sBAAsB,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAAC;IAC7C,MAAM,CAAC,IAAI,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,IAAI,CAAC;IACrE,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAClC,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACvB,CAAC;AAqFF,wBAAgB,cAAc,CAAC,OAAO,EAAE,oBAAoB,GAAG,aAAa,CAwH3E;AAED,YAAY,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/host.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { composeRuntime } from './connection.js';
|
|
2
|
+
import { removeRuntimeStore } from './store.js';
|
|
3
|
+
import { AUTHORIZATION_ENDED, carried, envelope, PROTOCOL_MISMATCH, request } from './wire.js';
|
|
4
|
+
function answered(md, app, writtenBy, member, input, deploymentFacts) {
|
|
5
|
+
switch(member){
|
|
6
|
+
case 'rooms':
|
|
7
|
+
return md.rooms();
|
|
8
|
+
case 'slots':
|
|
9
|
+
return md.slots();
|
|
10
|
+
case 'query':
|
|
11
|
+
return md.query(input[0]);
|
|
12
|
+
case 'search':
|
|
13
|
+
return md.search(input[0]);
|
|
14
|
+
case 'types':
|
|
15
|
+
return md.types(input[0]);
|
|
16
|
+
case 'syncStatus':
|
|
17
|
+
return md.syncStatus();
|
|
18
|
+
case 'serviceMode':
|
|
19
|
+
return md.serviceMode();
|
|
20
|
+
case 'save':
|
|
21
|
+
return md.save(input[0], input[1], writtenBy);
|
|
22
|
+
case 'outcome':
|
|
23
|
+
return md.outcome(input[0]);
|
|
24
|
+
case 'history':
|
|
25
|
+
return md.history(input[0], input[1], input[2]);
|
|
26
|
+
case 'call':
|
|
27
|
+
return md.call(input[0], input[1]);
|
|
28
|
+
case 'log':
|
|
29
|
+
return md.log();
|
|
30
|
+
case 'storageLoss':
|
|
31
|
+
return md.storageLoss();
|
|
32
|
+
case 'deploymentFacts':
|
|
33
|
+
return structuredClone(deploymentFacts);
|
|
34
|
+
case 'persistence':
|
|
35
|
+
return md.persistence();
|
|
36
|
+
case 'reauthorize':
|
|
37
|
+
return md.reauthorize();
|
|
38
|
+
case 'roomDraft.list':
|
|
39
|
+
return md.roomDraft.list();
|
|
40
|
+
case 'roomDraft.read':
|
|
41
|
+
return md.roomDraft.read(input[0], input[1]);
|
|
42
|
+
case 'roomDraft.write':
|
|
43
|
+
return md.roomDraft.write(input[0], input[1], input[2], input[3]);
|
|
44
|
+
case 'roomDraft.discard':
|
|
45
|
+
return md.roomDraft.discard(input[0], input[1], input[2]);
|
|
46
|
+
case 'presentation.read':
|
|
47
|
+
return md.presentation.read(app);
|
|
48
|
+
case 'presentation.write':
|
|
49
|
+
return md.presentation.write(app, input[0]);
|
|
50
|
+
case 'conversation.list':
|
|
51
|
+
return md.conversation.list();
|
|
52
|
+
case 'conversation.read':
|
|
53
|
+
return md.conversation.read(input[0], input[1]);
|
|
54
|
+
case 'conversation.create':
|
|
55
|
+
return md.conversation.create(input[0]);
|
|
56
|
+
case 'conversation.send':
|
|
57
|
+
return md.conversation.send(input[0]);
|
|
58
|
+
case 'conversation.answer':
|
|
59
|
+
return md.conversation.answer(input[0], input[1]);
|
|
60
|
+
case 'conversation.interrupt':
|
|
61
|
+
return md.conversation.interrupt(input[0]);
|
|
62
|
+
case 'conversation.kill':
|
|
63
|
+
return md.conversation.kill(input[0]);
|
|
64
|
+
case 'conversation.cancel':
|
|
65
|
+
return md.conversation.cancel(input[0], input[1]);
|
|
66
|
+
case 'conversation.delete':
|
|
67
|
+
return md.conversation.delete(input[0]);
|
|
68
|
+
case 'chatDraft.write':
|
|
69
|
+
return md.chatDraft.write(input[0], input[1]);
|
|
70
|
+
case 'chatDraft.discard':
|
|
71
|
+
return md.chatDraft.discard(input[0]);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export function serveMdRuntime(options) {
|
|
75
|
+
let deploymentFacts = structuredClone(options.deploymentFacts);
|
|
76
|
+
const closing = new AbortController();
|
|
77
|
+
const close = ()=>{
|
|
78
|
+
closing.abort();
|
|
79
|
+
};
|
|
80
|
+
options.signal?.addEventListener('abort', close, {
|
|
81
|
+
once: true
|
|
82
|
+
});
|
|
83
|
+
if (options.signal?.aborted === true) close();
|
|
84
|
+
const runtime = composeRuntime({
|
|
85
|
+
...options,
|
|
86
|
+
signal: closing.signal
|
|
87
|
+
});
|
|
88
|
+
const { md, watch } = runtime;
|
|
89
|
+
const attachments = new Map();
|
|
90
|
+
let authorized = true;
|
|
91
|
+
const factsObservers = new Set();
|
|
92
|
+
const watchFacts = (deliver)=>{
|
|
93
|
+
factsObservers.add(deliver);
|
|
94
|
+
deliver(structuredClone(deploymentFacts));
|
|
95
|
+
return ()=>{
|
|
96
|
+
factsObservers.delete(deliver);
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
function attach(port, app, release) {
|
|
100
|
+
if (!authorized) throw new Error(AUTHORIZATION_ENDED);
|
|
101
|
+
const writtenBy = release === undefined ? undefined : {
|
|
102
|
+
app,
|
|
103
|
+
release
|
|
104
|
+
};
|
|
105
|
+
const subscriptions = new Map();
|
|
106
|
+
let serving = true;
|
|
107
|
+
const send = (message)=>{
|
|
108
|
+
port.postMessage(envelope(message));
|
|
109
|
+
};
|
|
110
|
+
const detach = ()=>{
|
|
111
|
+
if (!serving) return;
|
|
112
|
+
serving = false;
|
|
113
|
+
for (const end of subscriptions.values())end();
|
|
114
|
+
subscriptions.clear();
|
|
115
|
+
attachments.delete(detach);
|
|
116
|
+
};
|
|
117
|
+
const serve = async (message)=>{
|
|
118
|
+
if (message.kind === 'hello') return;
|
|
119
|
+
if (message.kind === 'stop') {
|
|
120
|
+
subscriptions.get(message.id)?.();
|
|
121
|
+
subscriptions.delete(message.id);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
if (message.kind !== 'observe' || message.topic !== 'room-drafts') await md.loaded();
|
|
126
|
+
if (message.kind === 'observe') {
|
|
127
|
+
const { id, topic } = message;
|
|
128
|
+
let resolved = false;
|
|
129
|
+
const deliver = (value)=>{
|
|
130
|
+
if (!serving) return;
|
|
131
|
+
send(resolved ? {
|
|
132
|
+
kind: 'event',
|
|
133
|
+
id,
|
|
134
|
+
value
|
|
135
|
+
} : {
|
|
136
|
+
kind: 'result',
|
|
137
|
+
id,
|
|
138
|
+
value
|
|
139
|
+
});
|
|
140
|
+
resolved = true;
|
|
141
|
+
};
|
|
142
|
+
subscriptions.set(id, topic === 'deployment-facts' ? watchFacts(deliver) : watch(topic, topic === 'presentation' ? [
|
|
143
|
+
app
|
|
144
|
+
] : message.input, deliver));
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const value = await answered(md, app, writtenBy, message.member, message.input, deploymentFacts);
|
|
148
|
+
if (serving) send({
|
|
149
|
+
kind: 'result',
|
|
150
|
+
id: message.id,
|
|
151
|
+
value
|
|
152
|
+
});
|
|
153
|
+
} catch (error) {
|
|
154
|
+
if (serving) {
|
|
155
|
+
send({
|
|
156
|
+
kind: 'failure',
|
|
157
|
+
id: message.id,
|
|
158
|
+
message: error instanceof Error ? error.message : String(error)
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
port.addEventListener('message', (event)=>{
|
|
164
|
+
if (!serving) return;
|
|
165
|
+
const wire = carried(event.data);
|
|
166
|
+
if (wire === null) return;
|
|
167
|
+
if (!wire.matching) {
|
|
168
|
+
send({
|
|
169
|
+
kind: 'refusal',
|
|
170
|
+
refusal: PROTOCOL_MISMATCH
|
|
171
|
+
});
|
|
172
|
+
detach();
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
const message = request(wire.message);
|
|
176
|
+
if (message === null) return;
|
|
177
|
+
void serve(message);
|
|
178
|
+
});
|
|
179
|
+
port.addEventListener('close', detach);
|
|
180
|
+
port.start();
|
|
181
|
+
attachments.set(detach, send);
|
|
182
|
+
return detach;
|
|
183
|
+
}
|
|
184
|
+
return {
|
|
185
|
+
attach,
|
|
186
|
+
replaceDeploymentFacts (value) {
|
|
187
|
+
if (!authorized) throw new Error(AUTHORIZATION_ENDED);
|
|
188
|
+
deploymentFacts = structuredClone(value);
|
|
189
|
+
for (const deliver of factsObservers)deliver(structuredClone(deploymentFacts));
|
|
190
|
+
},
|
|
191
|
+
async endAuthorization () {
|
|
192
|
+
authorized = false;
|
|
193
|
+
for (const [detach, send] of [
|
|
194
|
+
...attachments
|
|
195
|
+
]){
|
|
196
|
+
send({
|
|
197
|
+
kind: 'ended'
|
|
198
|
+
});
|
|
199
|
+
detach();
|
|
200
|
+
}
|
|
201
|
+
close();
|
|
202
|
+
await removeRuntimeStore();
|
|
203
|
+
await options.shell.dropGeneration();
|
|
204
|
+
},
|
|
205
|
+
stop () {
|
|
206
|
+
return runtime.stop();
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
//# sourceMappingURL=host.js.map
|
package/dist/host.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/workspace/packages/mdruntime/src/host.ts"],"sourcesContent":["import type { WrittenBy } from './change.js';\nimport {\n composeRuntime,\n type HistoryQuery,\n type MdRuntime,\n type MdRuntimeOptions,\n type SavedRoomDraft,\n} from './connection.js';\nimport type { ChatDraft, ConversationOptions } from './conversation.js';\nimport type { DocumentQuery } from './query.js';\nimport type { SearchRequest } from './search.js';\nimport { removeRuntimeStore } from './store.js';\nimport {\n AUTHORIZATION_ENDED,\n carried,\n envelope,\n PROTOCOL_MISMATCH,\n request,\n type RuntimePort,\n type WireAnswer,\n type WireMember,\n type WireRequest,\n} from './wire.js';\n\nexport type MdRuntimeHostOptions = MdRuntimeOptions & { deploymentFacts?: unknown };\n\nexport type MdRuntimeHost = {\n replaceDeploymentFacts(value: unknown): void;\n attach(port: RuntimePort, app: string, release?: string): () => void;\n endAuthorization(): Promise<void>;\n stop(): Promise<void>;\n};\n\nfunction answered(\n md: MdRuntime,\n app: string,\n writtenBy: WrittenBy | undefined,\n member: WireMember,\n input: readonly unknown[],\n deploymentFacts: unknown,\n): unknown {\n switch (member) {\n case 'rooms':\n return md.rooms();\n case 'slots':\n return md.slots();\n case 'query':\n return md.query(input[0] as DocumentQuery);\n case 'search':\n return md.search(input[0] as SearchRequest);\n case 'types':\n return md.types(input[0] as string);\n case 'syncStatus':\n return md.syncStatus();\n case 'serviceMode':\n return md.serviceMode();\n case 'save':\n return md.save(input[0] as string, input[1] as readonly SavedRoomDraft[], writtenBy);\n case 'outcome':\n return md.outcome(input[0] as string);\n case 'history':\n return md.history(input[0] as string, input[1] as string, input[2] as HistoryQuery);\n case 'call':\n return md.call(input[0] as string, input[1]);\n case 'log':\n return md.log();\n case 'storageLoss':\n return md.storageLoss();\n case 'deploymentFacts':\n return structuredClone(deploymentFacts);\n case 'persistence':\n return md.persistence();\n case 'reauthorize':\n return md.reauthorize();\n case 'roomDraft.list':\n return md.roomDraft.list();\n case 'roomDraft.read':\n return md.roomDraft.read(input[0] as string, input[1] as string);\n case 'roomDraft.write':\n return md.roomDraft.write(\n input[0] as string,\n input[1] as string,\n input[2] as string | null,\n input[3],\n );\n case 'roomDraft.discard':\n return md.roomDraft.discard(input[0] as string, input[1] as string, input[2] as string);\n case 'presentation.read':\n return md.presentation.read(app);\n case 'presentation.write':\n return md.presentation.write(app, input[0]);\n case 'conversation.list':\n return md.conversation.list();\n case 'conversation.read':\n return md.conversation.read(input[0] as string, input[1] as ConversationOptions | undefined);\n case 'conversation.create':\n return md.conversation.create(input[0]);\n case 'conversation.send':\n return md.conversation.send(input[0] as string);\n case 'conversation.answer':\n return md.conversation.answer(input[0] as string, input[1]);\n case 'conversation.interrupt':\n return md.conversation.interrupt(input[0] as string);\n case 'conversation.kill':\n return md.conversation.kill(input[0] as string);\n case 'conversation.cancel':\n return md.conversation.cancel(input[0] as string, input[1] as string);\n case 'conversation.delete':\n return md.conversation.delete(input[0] as string);\n case 'chatDraft.write':\n return md.chatDraft.write(input[0] as string, input[1] as ChatDraft);\n case 'chatDraft.discard':\n return md.chatDraft.discard(input[0] as string);\n }\n}\n\nexport function serveMdRuntime(options: MdRuntimeHostOptions): MdRuntimeHost {\n let deploymentFacts = structuredClone(options.deploymentFacts);\n const closing = new AbortController();\n const close = () => {\n closing.abort();\n };\n options.signal?.addEventListener('abort', close, { once: true });\n if (options.signal?.aborted === true) close();\n const runtime = composeRuntime({ ...options, signal: closing.signal });\n const { md, watch } = runtime;\n const attachments = new Map<() => void, (message: WireAnswer) => void>();\n let authorized = true;\n const factsObservers = new Set<(value: unknown) => void>();\n const watchFacts = (deliver: (value: unknown) => void) => {\n factsObservers.add(deliver);\n deliver(structuredClone(deploymentFacts));\n return () => {\n factsObservers.delete(deliver);\n };\n };\n\n function attach(port: RuntimePort, app: string, release?: string): () => void {\n if (!authorized) throw new Error(AUTHORIZATION_ENDED);\n const writtenBy = release === undefined ? undefined : { app, release };\n const subscriptions = new Map<number, () => void>();\n let serving = true;\n const send = (message: WireAnswer) => {\n port.postMessage(envelope(message));\n };\n const detach = () => {\n if (!serving) return;\n serving = false;\n for (const end of subscriptions.values()) end();\n subscriptions.clear();\n attachments.delete(detach);\n };\n const serve = async (message: WireRequest) => {\n if (message.kind === 'hello') return;\n if (message.kind === 'stop') {\n subscriptions.get(message.id)?.();\n subscriptions.delete(message.id);\n return;\n }\n try {\n if (message.kind !== 'observe' || message.topic !== 'room-drafts') await md.loaded();\n if (message.kind === 'observe') {\n const { id, topic } = message;\n let resolved = false;\n const deliver = (value: unknown) => {\n if (!serving) return;\n send(resolved ? { kind: 'event', id, value } : { kind: 'result', id, value });\n resolved = true;\n };\n subscriptions.set(\n id,\n topic === 'deployment-facts'\n ? watchFacts(deliver)\n : watch(topic, topic === 'presentation' ? [app] : message.input, deliver),\n );\n return;\n }\n const value = await answered(\n md,\n app,\n writtenBy,\n message.member,\n message.input,\n deploymentFacts,\n );\n if (serving) send({ kind: 'result', id: message.id, value });\n } catch (error) {\n if (serving) {\n send({\n kind: 'failure',\n id: message.id,\n message: error instanceof Error ? error.message : String(error),\n });\n }\n }\n };\n port.addEventListener('message', (event) => {\n if (!serving) return;\n const wire = carried(event.data);\n if (wire === null) return;\n if (!wire.matching) {\n send({ kind: 'refusal', refusal: PROTOCOL_MISMATCH });\n detach();\n return;\n }\n const message = request(wire.message);\n if (message === null) return;\n void serve(message);\n });\n port.addEventListener('close', detach);\n port.start();\n attachments.set(detach, send);\n return detach;\n }\n\n return {\n attach,\n replaceDeploymentFacts(value) {\n if (!authorized) throw new Error(AUTHORIZATION_ENDED);\n deploymentFacts = structuredClone(value);\n for (const deliver of factsObservers) deliver(structuredClone(deploymentFacts));\n },\n async endAuthorization() {\n authorized = false;\n for (const [detach, send] of [...attachments]) {\n send({ kind: 'ended' });\n detach();\n }\n close();\n await removeRuntimeStore();\n await options.shell.dropGeneration();\n },\n stop() {\n return runtime.stop();\n },\n };\n}\n\nexport type { RuntimePort } from './wire.js';\n"],"names":["composeRuntime","removeRuntimeStore","AUTHORIZATION_ENDED","carried","envelope","PROTOCOL_MISMATCH","request","answered","md","app","writtenBy","member","input","deploymentFacts","rooms","slots","query","search","types","syncStatus","serviceMode","save","outcome","history","call","log","storageLoss","structuredClone","persistence","reauthorize","roomDraft","list","read","write","discard","presentation","conversation","create","send","answer","interrupt","kill","cancel","delete","chatDraft","serveMdRuntime","options","closing","AbortController","close","abort","signal","addEventListener","once","aborted","runtime","watch","attachments","Map","authorized","factsObservers","Set","watchFacts","deliver","add","attach","port","release","Error","undefined","subscriptions","serving","message","postMessage","detach","end","values","clear","serve","kind","get","id","topic","loaded","resolved","value","set","error","String","event","wire","data","matching","refusal","start","replaceDeploymentFacts","endAuthorization","shell","dropGeneration","stop"],"mappings":"AACA,SACEA,cAAc,QAKT,kBAAkB;AAIzB,SAASC,kBAAkB,QAAQ,aAAa;AAChD,SACEC,mBAAmB,EACnBC,OAAO,EACPC,QAAQ,EACRC,iBAAiB,EACjBC,OAAO,QAKF,YAAY;AAWnB,SAASC,SACPC,EAAa,EACbC,GAAW,EACXC,SAAgC,EAChCC,MAAkB,EAClBC,KAAyB,EACzBC,eAAwB;IAExB,OAAQF;QACN,KAAK;YACH,OAAOH,GAAGM,KAAK;QACjB,KAAK;YACH,OAAON,GAAGO,KAAK;QACjB,KAAK;YACH,OAAOP,GAAGQ,KAAK,CAACJ,KAAK,CAAC,EAAE;QAC1B,KAAK;YACH,OAAOJ,GAAGS,MAAM,CAACL,KAAK,CAAC,EAAE;QAC3B,KAAK;YACH,OAAOJ,GAAGU,KAAK,CAACN,KAAK,CAAC,EAAE;QAC1B,KAAK;YACH,OAAOJ,GAAGW,UAAU;QACtB,KAAK;YACH,OAAOX,GAAGY,WAAW;QACvB,KAAK;YACH,OAAOZ,GAAGa,IAAI,CAACT,KAAK,CAAC,EAAE,EAAYA,KAAK,CAAC,EAAE,EAA+BF;QAC5E,KAAK;YACH,OAAOF,GAAGc,OAAO,CAACV,KAAK,CAAC,EAAE;QAC5B,K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cAAc;QACpC;QACAC;YACE,OAAO5C,QAAQ4C,IAAI;QACrB;IACF;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identity.d.ts","sourceRoot":"","sources":["../src/identity.ts"],"names":[],"mappings":"AAAA,wBAAgB,mBAAmB,CAAC,EAAE,GAAE,MAAmB,GAAG,MAAM,CAWnE;AAED,wBAAgB,iBAAiB,IAAI,MAAM,CAI1C"}
|
package/dist/identity.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export function changeBatchIdentity(at = Date.now()) {
|
|
2
|
+
const bytes = new Uint8Array(16);
|
|
3
|
+
crypto.getRandomValues(bytes);
|
|
4
|
+
const time = BigInt(at);
|
|
5
|
+
for(let index = 0; index < 6; index += 1){
|
|
6
|
+
bytes[index] = Number(time >> BigInt(8 * (5 - index)) & 0xffn);
|
|
7
|
+
}
|
|
8
|
+
bytes[6] = bytes[6] & 0x0f | 0x70;
|
|
9
|
+
bytes[8] = bytes[8] & 0x3f | 0x80;
|
|
10
|
+
const hex = [
|
|
11
|
+
...bytes
|
|
12
|
+
].map((byte)=>byte.toString(16).padStart(2, '0')).join('');
|
|
13
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
14
|
+
}
|
|
15
|
+
export function roomDraftRevision() {
|
|
16
|
+
const bytes = new Uint8Array(16);
|
|
17
|
+
crypto.getRandomValues(bytes);
|
|
18
|
+
return [
|
|
19
|
+
...bytes
|
|
20
|
+
].map((byte)=>byte.toString(16).padStart(2, '0')).join('');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
//# sourceMappingURL=identity.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/workspace/packages/mdruntime/src/identity.ts"],"sourcesContent":["export function changeBatchIdentity(at: number = Date.now()): string {\n const bytes = new Uint8Array(16);\n crypto.getRandomValues(bytes);\n const time = BigInt(at);\n for (let index = 0; index < 6; index += 1) {\n bytes[index] = Number((time >> BigInt(8 * (5 - index))) & 0xffn);\n }\n bytes[6] = (bytes[6] & 0x0f) | 0x70;\n bytes[8] = (bytes[8] & 0x3f) | 0x80;\n const hex = [...bytes].map((byte) => byte.toString(16).padStart(2, '0')).join('');\n return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;\n}\n\nexport function roomDraftRevision(): string {\n const bytes = new Uint8Array(16);\n crypto.getRandomValues(bytes);\n return [...bytes].map((byte) => byte.toString(16).padStart(2, '0')).join('');\n}\n"],"names":["changeBatchIdentity","at","Date","now","bytes","Uint8Array","crypto","getRandomValues","time","BigInt","index","Number","hex","map","byte","toString","padStart","join","slice","roomDraftRevision"],"mappings":"AAAA,OAAO,SAASA,oBAAoBC,KAAaC,KAAKC,GAAG,EAAE;IACzD,MAAMC,QAAQ,IAAIC,WAAW;IAC7BC,OAAOC,eAAe,CAACH;IACvB,MAAMI,OAAOC,OAAOR;IACpB,IAAK,IAAIS,QAAQ,GAAGA,QAAQ,GAAGA,SAAS,EAAG;QACzCN,KAAK,CAACM,MAAM,GAAGC,OAAO,AAACH,QAAQC,OAAO,IAAK,CAAA,IAAIC,KAAI,KAAO,KAAK;IACjE;IACAN,KAAK,CAAC,EAAE,GAAG,AAACA,KAAK,CAAC,EAAE,GAAG,OAAQ;IAC/BA,KAAK,CAAC,EAAE,GAAG,AAACA,KAAK,CAAC,EAAE,GAAG,OAAQ;IAC/B,MAAMQ,MAAM;WAAIR;KAAM,CAACS,GAAG,CAAC,CAACC,OAASA,KAAKC,QAAQ,CAAC,IAAIC,QAAQ,CAAC,GAAG,MAAMC,IAAI,CAAC;IAC9E,OAAO,GAAGL,IAAIM,KAAK,CAAC,GAAG,GAAG,CAAC,EAAEN,IAAIM,KAAK,CAAC,GAAG,IAAI,CAAC,EAAEN,IAAIM,KAAK,CAAC,IAAI,IAAI,CAAC,EAAEN,IAAIM,KAAK,CAAC,IAAI,IAAI,CAAC,EAAEN,IAAIM,KAAK,CAAC,KAAK;AAC5G;AAEA,OAAO,SAASC;IACd,MAAMf,QAAQ,IAAIC,WAAW;IAC7BC,OAAOC,eAAe,CAACH;IACvB,OAAO;WAAIA;KAAM,CAACS,GAAG,CAAC,CAACC,OAASA,KAAKC,QAAQ,CAAC,IAAIC,QAAQ,CAAC,GAAG,MAAMC,IAAI,CAAC;AAC3E"}
|