* feat(media): establish shared asset ownership primitives Introduce global browser asset-pool ownership, asset-reference collection, stage reclamation planning, and lease-based URL access. Carry allocated media identity through storage and generation boundaries with regression coverage. * fix(media): enforce safe resolution across every consumer Route image, video, thumbnail, presentation, and video-export consumers through one resolution state machine. Prevent opaque allocated or generated references from reaching render and export sinks, with fallback and ownership tests. * fix(media): protect document-owned assets across mutations Allocate pool bytes before compatibility writes and document commits, then roll back uncommitted generations safely. Preserve document ownership across edits, retries, imports, speech generation, scene changes, and stage deletion. * fix(media): scope retries and tighten ownership guard Scope retries to the target scene and slide and refuse ambiguous shared-reference mutations. Expand retry rendering coverage and keep direct pool URL resolution behind the shared lease owner. * fix(storage): avoid nested lock during stage cleanup Execute prepared reclamation plans against an explicitly deleted document so the compatibility cascade cannot re-enter the per-document lock. Cover deletion of stage-owned media rows even when the document has no references. * refactor(media): confine reclamation to stage deletion Remove inline pool and compatibility-row cleanup from element, speech, scene, and audio replacement flows. Keep whole-stage reclamation behind explicit stage ownership, preserve stage-less legacy audio rows, and document deferred document-truth sweeping. * fix(media): close retry and resolution gaps Restore shared source tasks after successful forks, scope retries across both whiteboard locations, and wait for parallel TTS workers before rollback. Resolve background media through import, rendering, and PPTX export paths while keeping retry controls visible over last-good bytes. * test(media): execute the consumer safety matrix Replace source-substring checks with resolver seam execution across all six UI consumers, stage hydration, and both exporters. Pin each rollback layer independently and harden the ownership guard against aliased pool imports. * chore(packages): publish the additive DSL field Bump the DSL patch version for the optional speech-action field. Keep the transitional reclamation policy app-owned and document the legitimate transaction rollback removals there. * fix(media): make retry rollback task-safe Delay allocation task re-keying until final document reconciliation succeeds, restore shared source tasks on failed forks, and require exact stage-whiteboard targets before falling through from a missed scene. * fix(media): clear private assets and refresh leases Delete the asset-pool database during the confirmed local-data wipe. Notify the app lease layer after same-id replacement so mounted consumers re-resolve current bytes without reaching into storage internals. * test(media): pin closure safety guards Exercise the CSS allocation boundary, unfiltered legacy-row ownership, delete-and-undo byte survival, and real distinct consumer seams. Restore the video-only manifest overwrite condition and share the direct video resolution hook across both element variants. * test(media): narrow rollback element assertion Narrow the reconciled slide element to an image before checking its source so the rollback regression remains type-safe under the full root compiler configuration. * fix(media): close lease refresh races Gate the first batch publication by unique resolved refs, then publish every replacement snapshot without mutating the prior React state object. Serialize invalidation behind pending releases, evict rejected refreshes, and register replacement observation at the pool boundary. * fix(media): reopen pool after clear failures Always evict the singleton once its store has been closed, including blocked and failed database deletion paths. Report blocked deletion as deferred and prove a later write uses a fresh live store. * fix(media): isolate shared retry progress Track forked regeneration under the selected element until it receives a fresh asset identity, leaving the shared source task and bytes untouched. Surface targeted failures through renderer task lookup and skip the redundant fork reconciliation lock. * fix(media): close final asset retry gaps Keep blocked asset clears fail-loud until a successful retry, with actionable settings guidance. Clear failed shared-fork state across durable and live key spaces, and pin renderer lookups, lease publication identity, and committed-ref rollback protection. * fix: preserve actionable retry failures Localize the blocked cache-clear recovery hint across every supported locale and pin the deferred-error mapping. Retain durable fork failure rows while retries run, deleting them only after successful generation so unstructured failures survive reload. * fix(media): hydrate legacy stored video thumbnails Home-page recent-video thumbnails regressed for legacy Dexie mediaFiles rows keyed by gen_vid placeholders: the reworked hydration resolved the row's bytes through the sealed resolver but never surfaced the stored blob (and its poster) as object URLs for the preview card, so the CI recent-video-thumbnail e2e specs found no visible element. Hydration now materializes legacy stored video rows into blob URLs for both the element src and poster while keeping the resolver invariants: opaque refs still never reach a DOM src, and concrete addresses are never blanked. Unit pins cover the seam so the vitest suite catches this class without a browser. * fix(media): resolve sole restored legacy video Classroom playback restored tasks by exact document media references. Legacy gen_vid references can outlive the key used by the one persisted video row, leaving the player on a placeholder even though bytes were restored. Select the sole completed stage video only for legacy sequential refs after exact and reconciled matches. Keep exact failures authoritative, refuse ambiguous candidates, and cover success, ambiguity, and failure precedence in unit tests. * fix(media): scope legacy video recovery Decide restored legacy video recovery once from the complete document and record it through the shared task lookup consumed by playback, editing, and resolved slides. Keep ambiguous documents as placeholders, apply the same decision to thumbnail hydration, and preserve exact failure precedence. * fix(media): exclude claimed video recovery tasks Model restored legacy recovery as a two-pass match across document video elements and task rows. Remove tasks claimed by exact, targeted, or placeholder lookup before applying the sole-candidate fallback, and cover the ownership/cardinality matrix. * fix(media): unify video element resolution Centralize source, task, poster, and legacy recovery decisions for every video consumer. Ensure direct URLs win over opaque refs and play_video waits on element-targeted retry tasks. * refactor(media): route legacy recovery through resolver Let document-aware consumers request legacy video recovery through the unified element binding API, keeping thumbnail hydration on a single decision path. * fix(media): preserve import refs and prefer pool bytes Recognize unambiguous extensionless relative media addresses during classroom import. Resolve allocated export and thumbnail assets from the shared pool before falling back to lagging compatibility rows. * fix(export): preserve concrete video sources Route PPTX video elements through the shared media binding resolver so an unresolved opaque reference cannot replace a playable source. Complete browser and persisted-store cleanup when asset-pool deletion is deferred, while retaining distinct hard-failure behavior. * fix(media): fork retries without exclusive ownership Enumerate logical asset owners across every persisted document before allowing global pool replacement. Thread explicit targets through fresh-id rewrites and cover cross-document aliases plus unreadable ownership. * fix(media): guard global asset reclamation Share a fail-closed persisted-document liveness check between stage deletion and retry replacement. Preserve cross-document pool aliases while deleting stage-owned compatibility rows and cover enumeration failures. * fix(media): cover complete slide asset references Route slide media traversal through a shared mutable slot contract so backgrounds participate in export, thumbnail hydration, collection, and rewrite lifecycles. Snapshot complete surviving-document refs once per reclamation and preserve manifest-only owners while retaining fail-closed behavior. * fix(media): preserve exclusive retry asset ids Allow targeted retries to replace exclusively owned pool assets in place. Keep shared and unprovable ownership paths on fresh allocations, and pin production-shaped retries plus compatibility-row cleanup. * fix(media): revalidate asset bindings at completion Recheck repository-wide ownership before replacing generated media and fork scoped retries when exclusivity changed. Route video export selection through the unified resolver and keep concrete posters independent of task state. * fix(media): count unflushed owners and broadcast replacements The completion-time exclusivity proof read only the persisted document, but slide duplication updates the Zustand aggregate synchronously and schedules persistence behind a debounce. A retry finishing inside that window saw a single persisted owner and replaced the bytes behind a reference the duplicate also held. The proof now also counts owners in the live stage snapshot when that snapshot represents the stage being retried, so an unflushed duplicate forks instead. Same-id replacement notifications were realm-local, so a second tab showing the same classroom kept its lease pinned to the superseded blob URL. The notification now travels over a BroadcastChannel; each receiving realm runs its own observers against its own pool, so a spoofed message can at most force a re-resolve. A missing or failing channel never fails the replacement. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(media): bind replacement listeners and spare shared audio rows A realm that only renders never sends a replacement, so binding the channel from the sender path left passive tabs deaf to peers. Binding now happens where the observer is registered, when the asset-pool module loads, and the receiving realm resolves its own pool lazily so a cleared or unavailable pool degrades to the next resolve instead of throwing. Observer notifications ran under Promise.all, so a rejection surfaced after BrowserAssetStore.replace had already committed and turned a durable success into a reported failure. They are settled individually now; the callback is wrapped because a synchronous throw would otherwise escape before allSettled sees the array. audioFiles rows are keyed globally by audioId, so deleting a stage removed the sole row for an id a surviving document still referenced — playback and both export paths read that table directly and cannot fall back to the preserved pool blob. Rows are now filtered against surviving references, while a failed enumeration still withholds only the irreversible pool removal. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(media): serve replaced bytes from the pool everywhere Unknown survivor liveness deleted every planned audioFiles row. Those rows are keyed globally by audioId, and playback plus both export paths read the table directly, so losing one is as irreversible for them as removing the pool entry. Unknown liveness now preserves the rows too, leaving bounded garbage for a later pass that can prove exclusivity. The earlier pool-first change covered PPTX, video collection and thumbnail hydration but missed classroom ZIP export, which still serialized the stale compatibility row after a lagged same-id replacement, shipping media the classroom no longer renders. Auditing every direct reader of the media and audio tables surfaced the same gap in playback: speech regeneration also replaces bytes under a stable id and does not roll the pool back when the compatibility write fails, so the player kept serving superseded narration. It now resolves the pool first and falls back to stored rows for legacy and imported audio. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(audio): replace exclusively owned speech clips in place regenerateSpeechAudio accepted the action's audioId but always passed undefined as replaceAssetId, so regenerating an exclusively owned pool-backed clip allocated a new asset every time, rewrote the action and orphaned the previous pool entry and compatibility row until stage reclamation — contradicting the stable-id path generateAndStoreTTS already implements for media. Ownership is now established before synthesis, and the rule itself moved to the shared reference module so media retries, poster replacement and speech regeneration consume one implementation instead of restating it. An exclusively owned clip keeps its id and has its bytes replaced; a shared clip, a legacy id with no pool entry, or unprovable ownership still gets a fresh allocation so other holders keep their audio. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * chore(media): drop the import left behind by the ownership move Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(media): resolve audio pool-first and fence peer realms Stable-id TTS regeneration commits replaced narration to the pool before the audioFiles mirror write, so a failed mirror leaves the row stale. AudioPlayer already resolved pool-first, but audioObjectUrl, collectAudioFiles and the video timeline dependencies still read Dexie directly and would serve the superseded clip. All allocated-audio readers now share one resolver, with Dexie kept as the fallback for legacy and imported rows. The exclusivity proof modelled unflushed owners in the active realm only, so another tab duplicating the same asset during its save debounce could still be observed as a single owner and have its bytes replaced globally. A peer's pending state cannot be read across realms, so presence is probed instead: any realm holding the stage forces the fork path. A deferred asset-pool deletion no longer reloads the page. The database is still on disk and the guidance asks the user to close the other tab and retry, which the reload discarded. The decision moved into a shared helper so the rule is pinned rather than living inline in the component. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(media): fail closed when presence cannot be probed The presence helper documented that an unanswerable probe must count as a peer, but every unavailable path — no BroadcastChannel, a constructor that threw, a send that failed, and the window before the pool's asynchronous binding completes — returned false, so the ownership proof cleared a single local owner and replaced globally shared bytes in place. Probing now returns present, absent or unknown, and only a probe that was actually sent and went unanswered is absent; the ownership decision treats unknown exactly like present. The pool declares its binding intent synchronously so a probe issued during the load-time window waits for the bind instead of concluding that presence is unavailable, and releases that gate if the import fails. Coverage reaches the write boundary: with presence unknown, a production-shaped targeted retry forks to a fresh id instead of calling replace, and the original bytes stay intact for a peer's unflushed owner. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * chore(dsl): bump to 0.6.3 after the release dedupe took 0.6.2 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(i18n): add the deferred-clear guidance to fr-FR The locale landed on main after this branch added the key, so the alignment check flagged it as the one missing translation. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: 杨慎 <117187635+cosarah@users.noreply.github.com>
971 lines
35 KiB
TypeScript
971 lines
35 KiB
TypeScript
import Dexie, { type EntityTable, type Table } from 'dexie';
|
|
import { DSL_VERSION } from '@openmaic/dsl';
|
|
import type {
|
|
Scene,
|
|
SceneType,
|
|
SceneContent,
|
|
Whiteboard,
|
|
VideoManifest,
|
|
GeneratedAgentConfig,
|
|
} from '@/lib/types/stage';
|
|
import type { Action } from '@/lib/types/action';
|
|
import type {
|
|
SessionType,
|
|
SessionStatus,
|
|
SessionConfig,
|
|
ToolCallRecord,
|
|
ToolCallRequest,
|
|
ChatSession,
|
|
} from '@/lib/types/chat';
|
|
import type { SceneOutline } from '@/lib/types/generation';
|
|
import type { VoiceDesign } from '@/lib/audio/voice-design';
|
|
import type { UIMessage } from 'ai';
|
|
import type { AgentEditSessionRecord } from '@/lib/agent/client/agent-edit-session-types';
|
|
import { createLogger } from '@/lib/logger';
|
|
import { beginStageRuntimeDeletionSafely, getRuntimeStore } from '@/lib/runtime/store';
|
|
import type { RuntimeStore } from '@openmaic/storage';
|
|
import {
|
|
withRuntimeStorageExclusiveLock,
|
|
withRuntimeStorageExclusiveLockUntilSettled,
|
|
withRuntimeStorageSharedLock,
|
|
} from './chat-storage-lock';
|
|
import type { ChatStorageOptions } from './chat-storage';
|
|
import type { AppDocument } from '@/lib/document-store';
|
|
import { BrowserKVStore } from '@openmaic/storage';
|
|
import { clearAssetPool } from '@/lib/media/asset-pool';
|
|
|
|
const log = createLogger('Database');
|
|
|
|
/**
|
|
* Legacy Snapshot type for undo/redo functionality
|
|
* Used by useSnapshotStore
|
|
*/
|
|
export interface Snapshot {
|
|
id?: number;
|
|
index: number;
|
|
slides: Scene[];
|
|
}
|
|
|
|
/**
|
|
* MAIC Local Database
|
|
*
|
|
* Uses IndexedDB to store all user data locally
|
|
* - Does not delete expired data; all data is stored permanently
|
|
* - Uses a fixed database name
|
|
* - Supports multi-course management
|
|
*/
|
|
|
|
// ==================== Database Table Type Definitions ====================
|
|
|
|
/**
|
|
* Stage table - Course basic info
|
|
*/
|
|
export interface StageRecord {
|
|
id: string; // Primary key
|
|
name: string;
|
|
description?: string;
|
|
createdAt: number; // timestamp
|
|
updatedAt: number; // timestamp
|
|
languageDirective?: string;
|
|
style?: string;
|
|
currentSceneId?: string;
|
|
agentIds?: string[]; // Agent IDs selected at creation time
|
|
videoManifest?: VideoManifest; // Generated video request manifest; non-indexed
|
|
interactiveMode?: boolean; // Interactive Mode flag; non-indexed
|
|
taskEngineMode?: boolean; // Vocational Task Engine flag; non-indexed
|
|
generatedAgentConfigs?: GeneratedAgentConfig[]; // Editor-authored agent roster snapshot
|
|
}
|
|
|
|
/**
|
|
* Scene table - Scene/page data
|
|
*/
|
|
export interface SceneRecord {
|
|
id: string; // Primary key
|
|
stageId: string; // Foreign key -> stages.id
|
|
type: SceneType;
|
|
title: string;
|
|
order: number; // Display order
|
|
content: SceneContent; // Stored as JSON
|
|
actions?: Action[]; // Stored as JSON
|
|
whiteboard?: Whiteboard[]; // Stored as JSON
|
|
createdAt: number;
|
|
updatedAt: number;
|
|
}
|
|
|
|
/**
|
|
* AudioFile table - Audio files (TTS)
|
|
*/
|
|
export interface AudioFileRecord {
|
|
id: string; // Primary key (audioId)
|
|
/** Stage ownership index. Absent on legacy rows; document walking remains their fallback. */
|
|
stageId?: string;
|
|
blob: Blob; // Audio binary data
|
|
duration?: number; // Duration (seconds)
|
|
format: string; // mp3, wav, etc.
|
|
text?: string; // Corresponding text content
|
|
voice?: string; // Voice used
|
|
createdAt: number;
|
|
ossKey?: string; // Full CDN URL for this audio blob
|
|
}
|
|
|
|
/**
|
|
* ImageFile table - Image files
|
|
*/
|
|
export interface ImageFileRecord {
|
|
id: string; // Primary key
|
|
blob: Blob | ArrayBuffer; // Image binary data
|
|
filename: string; // Original filename
|
|
mimeType: string; // image/png, image/jpeg, etc.
|
|
size: number; // File size (bytes)
|
|
createdAt: number;
|
|
}
|
|
|
|
/**
|
|
* ChatSession table - Chat session data
|
|
*/
|
|
export interface ChatSessionRecord {
|
|
id: string; // PK (session id)
|
|
stageId: string; // FK -> stages.id
|
|
type: SessionType;
|
|
title: string;
|
|
status: SessionStatus;
|
|
messages: UIMessage[]; // JSON-safe serialized messages
|
|
config: SessionConfig;
|
|
toolCalls: ToolCallRecord[];
|
|
pendingToolCalls: ToolCallRequest[];
|
|
createdAt: number;
|
|
updatedAt: number;
|
|
sceneId?: string;
|
|
lastActionIndex?: number;
|
|
}
|
|
|
|
/**
|
|
* PlaybackState table - Playback state snapshot (at most one per stage)
|
|
*/
|
|
export interface PlaybackStateRecord {
|
|
stageId: string; // PK
|
|
sceneIndex: number;
|
|
actionIndex: number;
|
|
consumedDiscussions: string[];
|
|
updatedAt: number;
|
|
}
|
|
|
|
/**
|
|
* StageOutlines table - Persisted outlines for resume-on-refresh
|
|
*/
|
|
export interface StageOutlinesRecord {
|
|
stageId: string; // Primary key (FK -> stages.id)
|
|
outlines: SceneOutline[];
|
|
// True once generation finished for this stage. Gates resume-on-mount so an
|
|
// edited (e.g. slide-deleted) finished deck is not treated as "interrupted"
|
|
// and regenerated. Optional for backward compat with pre-existing records.
|
|
generationComplete?: boolean;
|
|
createdAt: number;
|
|
updatedAt: number;
|
|
}
|
|
|
|
/**
|
|
* MediaFile table - AI-generated media files (images/videos)
|
|
*/
|
|
export interface MediaFileRecord {
|
|
// Compound key: `${stageId}:${mediaRef}`. Successful and failed rows use
|
|
// the same reference space (allocated id after allocation, legacy ref before it).
|
|
id: string;
|
|
stageId: string; // FK → stages.id
|
|
/** Original gen_* reference retained after allocation for reload reconciliation. */
|
|
placeholderRef?: string;
|
|
type: 'image' | 'video';
|
|
blob: Blob; // Media binary
|
|
mimeType: string; // image/png, video/mp4
|
|
size: number;
|
|
poster?: Blob; // Video thumbnail blob
|
|
prompt: string; // Original prompt (for retry)
|
|
params: string; // JSON-serialized generation params
|
|
error?: string; // If set, this is a failed task (blob is empty placeholder)
|
|
errorCode?: string; // Structured error code (e.g. 'CONTENT_SENSITIVE')
|
|
ossKey?: string; // Full CDN URL for this media blob
|
|
posterOssKey?: string; // Full CDN URL for the poster blob
|
|
createdAt: number;
|
|
}
|
|
|
|
/**
|
|
* GeneratedAgent table - AI-generated agent profiles.
|
|
*
|
|
* LEGACY. The roster now persists on the stage document
|
|
* (`stage.generatedAgentConfigs`); this table is kept only as a lazy-migration
|
|
* source for classrooms whose roster (or voice fields) predate the
|
|
* document-embedded model. Production access is migration reads plus deletion
|
|
* hygiene: `deleteStageData` clears a deleted stage's rows (as does the
|
|
* deprecated `deleteStageWithRelatedData` cascade). Nothing writes new rows;
|
|
* do not add writers.
|
|
*/
|
|
export interface GeneratedAgentRecord {
|
|
id: string; // PK: agent ID (e.g. "gen-abc123")
|
|
stageId: string; // FK -> stages.id
|
|
name: string;
|
|
role: string; // 'teacher' | 'assistant' | 'student'
|
|
persona: string;
|
|
avatar: string;
|
|
color: string;
|
|
priority: number;
|
|
voiceDesign?: VoiceDesign; // 3-layer vocal descriptor for auto voice
|
|
createdAt: number;
|
|
}
|
|
|
|
/**
|
|
* VoiceProfile table - Browser-local TTS voice profiles
|
|
*/
|
|
export interface VoiceProfileRecord {
|
|
id: string;
|
|
providerId: string;
|
|
kind: 'prompt' | 'clone';
|
|
name: string;
|
|
voicePrompt?: string;
|
|
promptText?: string;
|
|
referenceAudio?: Blob;
|
|
referenceAudioName?: string;
|
|
referenceAudioMimeType?: string;
|
|
createdAt: number;
|
|
updatedAt: number;
|
|
}
|
|
|
|
/**
|
|
* Cached reference clip for a registered auto voice (any TTS provider). The
|
|
* clip is the source of truth; the deterministic `voiceId` is its key, enabling
|
|
* register-on-invalid re-registration after backend GC/restart.
|
|
*/
|
|
export interface AutoVoiceCacheRecord {
|
|
voiceId: string;
|
|
referenceAudio: Blob;
|
|
mimeType: string;
|
|
updatedAt: number;
|
|
}
|
|
|
|
/** Build the compound primary key for mediaFiles: `${stageId}:${elementId}` */
|
|
export function mediaFileKey(stageId: string, elementId: string): string {
|
|
return `${stageId}:${elementId}`;
|
|
}
|
|
|
|
// ==================== Database Definition ====================
|
|
|
|
const DATABASE_NAME = 'MAIC-Database';
|
|
const _DATABASE_VERSION = 16;
|
|
|
|
/**
|
|
* MAIC Database Instance
|
|
*/
|
|
class MAICDatabase extends Dexie {
|
|
// Table definitions
|
|
stages!: EntityTable<StageRecord, 'id'>;
|
|
scenes!: EntityTable<SceneRecord, 'id'>;
|
|
audioFiles!: EntityTable<AudioFileRecord, 'id'>;
|
|
imageFiles!: EntityTable<ImageFileRecord, 'id'>;
|
|
snapshots!: EntityTable<Snapshot, 'id'>; // Undo/redo snapshots (legacy)
|
|
chatSessions!: EntityTable<ChatSessionRecord, 'id'>;
|
|
chatRestoreStaging!: Table<ChatSessionRecord, [string, string]>;
|
|
playbackState!: EntityTable<PlaybackStateRecord, 'stageId'>;
|
|
stageOutlines!: EntityTable<StageOutlinesRecord, 'stageId'>;
|
|
mediaFiles!: EntityTable<MediaFileRecord, 'id'>;
|
|
generatedAgents!: EntityTable<GeneratedAgentRecord, 'id'>;
|
|
voiceProfiles!: EntityTable<VoiceProfileRecord, 'id'>;
|
|
autoVoiceCache!: EntityTable<AutoVoiceCacheRecord, 'voiceId'>;
|
|
agentEditSessions!: EntityTable<AgentEditSessionRecord, 'id'>;
|
|
|
|
constructor() {
|
|
super(DATABASE_NAME);
|
|
|
|
// Version 1: Initial schema
|
|
this.version(1).stores({
|
|
stages: 'id, updatedAt',
|
|
scenes: 'id, stageId, order, [stageId+order]',
|
|
audioFiles: 'id, createdAt',
|
|
imageFiles: 'id, createdAt',
|
|
snapshots: '++id',
|
|
// Previously had: messages, participants, discussions, sceneSnapshots
|
|
});
|
|
|
|
// Version 2: Remove unused tables
|
|
this.version(2).stores({
|
|
stages: 'id, updatedAt',
|
|
scenes: 'id, stageId, order, [stageId+order]',
|
|
audioFiles: 'id, createdAt',
|
|
imageFiles: 'id, createdAt',
|
|
snapshots: '++id',
|
|
// Delete removed tables
|
|
messages: null,
|
|
participants: null,
|
|
discussions: null,
|
|
sceneSnapshots: null,
|
|
});
|
|
|
|
// Version 3: Add chatSessions and playbackState tables
|
|
this.version(3).stores({
|
|
stages: 'id, updatedAt',
|
|
scenes: 'id, stageId, order, [stageId+order]',
|
|
audioFiles: 'id, createdAt',
|
|
imageFiles: 'id, createdAt',
|
|
snapshots: '++id',
|
|
chatSessions: 'id, stageId, [stageId+createdAt]',
|
|
playbackState: 'stageId',
|
|
});
|
|
|
|
// Version 4: Add stageOutlines table for resume-on-refresh
|
|
this.version(4).stores({
|
|
stages: 'id, updatedAt',
|
|
scenes: 'id, stageId, order, [stageId+order]',
|
|
audioFiles: 'id, createdAt',
|
|
imageFiles: 'id, createdAt',
|
|
snapshots: '++id',
|
|
chatSessions: 'id, stageId, [stageId+createdAt]',
|
|
playbackState: 'stageId',
|
|
stageOutlines: 'stageId',
|
|
});
|
|
|
|
// Version 5: Add mediaFiles table for async media generation
|
|
this.version(5).stores({
|
|
stages: 'id, updatedAt',
|
|
scenes: 'id, stageId, order, [stageId+order]',
|
|
audioFiles: 'id, createdAt',
|
|
imageFiles: 'id, createdAt',
|
|
snapshots: '++id',
|
|
chatSessions: 'id, stageId, [stageId+createdAt]',
|
|
playbackState: 'stageId',
|
|
stageOutlines: 'stageId',
|
|
mediaFiles: 'id, stageId, [stageId+type]',
|
|
});
|
|
|
|
// Version 6: Fix mediaFiles primary key — use compound key stageId:elementId
|
|
// to prevent cross-course collisions (gen_img_1 is NOT globally unique)
|
|
this.version(6)
|
|
.stores({
|
|
stages: 'id, updatedAt',
|
|
scenes: 'id, stageId, order, [stageId+order]',
|
|
audioFiles: 'id, createdAt',
|
|
imageFiles: 'id, createdAt',
|
|
snapshots: '++id',
|
|
chatSessions: 'id, stageId, [stageId+createdAt]',
|
|
playbackState: 'stageId',
|
|
stageOutlines: 'stageId',
|
|
mediaFiles: 'id, stageId, [stageId+type]',
|
|
})
|
|
.upgrade(async (tx) => {
|
|
const table = tx.table('mediaFiles');
|
|
const allRecords = await table.toArray();
|
|
for (const rec of allRecords) {
|
|
const newKey = `${rec.stageId}:${rec.id}`;
|
|
// Skip if already migrated (idempotent)
|
|
if (rec.id.includes(':')) continue;
|
|
await table.delete(rec.id);
|
|
await table.put({ ...rec, id: newKey });
|
|
}
|
|
});
|
|
|
|
// Version 7: Add ossKey fields to mediaFiles and audioFiles for OSS storage plugin
|
|
// Non-indexed optional fields — Dexie handles these transparently.
|
|
this.version(7).stores({
|
|
stages: 'id, updatedAt',
|
|
scenes: 'id, stageId, order, [stageId+order]',
|
|
audioFiles: 'id, createdAt',
|
|
imageFiles: 'id, createdAt',
|
|
snapshots: '++id',
|
|
chatSessions: 'id, stageId, [stageId+createdAt]',
|
|
playbackState: 'stageId',
|
|
stageOutlines: 'stageId',
|
|
mediaFiles: 'id, stageId, [stageId+type]',
|
|
});
|
|
|
|
// Version 8: Add generatedAgents table for AI-generated agent profiles
|
|
this.version(8).stores({
|
|
stages: 'id, updatedAt',
|
|
scenes: 'id, stageId, order, [stageId+order]',
|
|
audioFiles: 'id, createdAt',
|
|
imageFiles: 'id, createdAt',
|
|
snapshots: '++id',
|
|
chatSessions: 'id, stageId, [stageId+createdAt]',
|
|
playbackState: 'stageId',
|
|
stageOutlines: 'stageId',
|
|
mediaFiles: 'id, stageId, [stageId+type]',
|
|
generatedAgents: 'id, stageId',
|
|
});
|
|
|
|
// Version 9: Migrate legacy `language` field to `languageDirective`
|
|
// Old stages stored a BCP-47 locale code (e.g. "zh-CN"); new code expects a
|
|
// natural-language directive. Convert known locales and drop the old field.
|
|
const LOCALE_TO_DIRECTIVE: Record<string, string> = {
|
|
'zh-CN': 'Deliver the entire course in Chinese (Simplified, zh-CN).',
|
|
'en-US': 'Deliver the entire course in English (en-US).',
|
|
'ja-JP': 'Deliver the entire course in Japanese (ja-JP).',
|
|
'ru-RU': 'Deliver the entire course in Russian (ru-RU).',
|
|
};
|
|
this.version(9)
|
|
.stores({
|
|
stages: 'id, updatedAt',
|
|
scenes: 'id, stageId, order, [stageId+order]',
|
|
audioFiles: 'id, createdAt',
|
|
imageFiles: 'id, createdAt',
|
|
snapshots: '++id',
|
|
chatSessions: 'id, stageId, [stageId+createdAt]',
|
|
playbackState: 'stageId',
|
|
stageOutlines: 'stageId',
|
|
mediaFiles: 'id, stageId, [stageId+type]',
|
|
generatedAgents: 'id, stageId',
|
|
})
|
|
.upgrade(async (tx) => {
|
|
const table = tx.table('stages');
|
|
await table.toCollection().modify((stage: Record<string, unknown>) => {
|
|
const lang = stage.language as string | undefined;
|
|
if (lang && !stage.languageDirective) {
|
|
stage.languageDirective =
|
|
LOCALE_TO_DIRECTIVE[lang] || `Deliver the entire course in ${lang}.`;
|
|
}
|
|
delete stage.language;
|
|
});
|
|
});
|
|
|
|
// Version 10: Add browser-local voice profiles for serverless TTS voice storage.
|
|
this.version(10).stores({
|
|
stages: 'id, updatedAt',
|
|
scenes: 'id, stageId, order, [stageId+order]',
|
|
audioFiles: 'id, createdAt',
|
|
imageFiles: 'id, createdAt',
|
|
snapshots: '++id',
|
|
chatSessions: 'id, stageId, [stageId+createdAt]',
|
|
playbackState: 'stageId',
|
|
stageOutlines: 'stageId',
|
|
mediaFiles: 'id, stageId, [stageId+type]',
|
|
generatedAgents: 'id, stageId',
|
|
voiceProfiles: 'id, providerId, kind, updatedAt',
|
|
});
|
|
|
|
// Version 11: Add auto-voice reference-clip cache (provider-neutral register-by-id).
|
|
this.version(11).stores({
|
|
stages: 'id, updatedAt',
|
|
scenes: 'id, stageId, order, [stageId+order]',
|
|
audioFiles: 'id, createdAt',
|
|
imageFiles: 'id, createdAt',
|
|
snapshots: '++id',
|
|
chatSessions: 'id, stageId, [stageId+createdAt]',
|
|
playbackState: 'stageId',
|
|
stageOutlines: 'stageId',
|
|
mediaFiles: 'id, stageId, [stageId+type]',
|
|
generatedAgents: 'id, stageId',
|
|
voiceProfiles: 'id, providerId, kind, updatedAt',
|
|
autoVoiceCache: 'voiceId, updatedAt',
|
|
});
|
|
|
|
// Version 12: Add agentEditSessions — multi-session AI-editing conversation
|
|
// history per stage (replaces the single-thread localStorage store).
|
|
this.version(12).stores({
|
|
stages: 'id, updatedAt',
|
|
scenes: 'id, stageId, order, [stageId+order]',
|
|
audioFiles: 'id, createdAt',
|
|
imageFiles: 'id, createdAt',
|
|
snapshots: '++id',
|
|
chatSessions: 'id, stageId, [stageId+createdAt]',
|
|
playbackState: 'stageId',
|
|
stageOutlines: 'stageId',
|
|
mediaFiles: 'id, stageId, [stageId+type]',
|
|
generatedAgents: 'id, stageId',
|
|
voiceProfiles: 'id, providerId, kind, updatedAt',
|
|
autoVoiceCache: 'voiceId, updatedAt',
|
|
agentEditSessions: 'id, stageId, [stageId+updatedAt]',
|
|
});
|
|
|
|
// Version 13 briefly added chatStorageLocks on the draft chat cutover
|
|
// branch. Advance past it and remove the abandoned lease table so database
|
|
// versions stay monotonic for anyone who opened that intermediate build.
|
|
this.version(14).stores({
|
|
stages: 'id, updatedAt',
|
|
scenes: 'id, stageId, order, [stageId+order]',
|
|
audioFiles: 'id, createdAt',
|
|
imageFiles: 'id, createdAt',
|
|
snapshots: '++id',
|
|
chatSessions: 'id, stageId, [stageId+createdAt]',
|
|
playbackState: 'stageId',
|
|
stageOutlines: 'stageId',
|
|
mediaFiles: 'id, stageId, [stageId+type]',
|
|
generatedAgents: 'id, stageId',
|
|
voiceProfiles: 'id, providerId, kind, updatedAt',
|
|
autoVoiceCache: 'voiceId, updatedAt',
|
|
agentEditSessions: 'id, stageId, [stageId+updatedAt]',
|
|
chatStorageLocks: null,
|
|
});
|
|
|
|
// Version 15: backup restore staging must preserve chat IDs that are reused
|
|
// in different stage partitions; the legacy chat table is keyed by id only.
|
|
this.version(15).stores({
|
|
chatRestoreStaging: '[stageId+id], stageId, [stageId+createdAt]',
|
|
});
|
|
|
|
// Version 16: make newly-written audio independently reclaimable by stage.
|
|
// Legacy rows remain valid and are found through speech-action references.
|
|
this.version(16).stores({
|
|
audioFiles: 'id, stageId, createdAt',
|
|
});
|
|
}
|
|
}
|
|
|
|
// Create database instance
|
|
export const db = new MAICDatabase();
|
|
|
|
// ==================== Helper Functions ====================
|
|
|
|
/**
|
|
* Initialize database
|
|
* Call at application startup
|
|
*/
|
|
export async function initDatabase(): Promise<void> {
|
|
try {
|
|
await db.open();
|
|
// Request persistent storage to prevent browser from evicting IndexedDB
|
|
// under storage pressure (large media blobs can trigger LRU cleanup)
|
|
void navigator.storage?.persist?.();
|
|
log.info('Database initialized successfully');
|
|
} catch (error) {
|
|
log.error('Failed to initialize database:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Clear database (optional)
|
|
* Use with caution: deletes all data
|
|
*/
|
|
export async function clearDatabase(runtimeStore?: RuntimeStore): Promise<void> {
|
|
// Clear the whole runtime database first, including rows orphaned by an
|
|
// earlier best-effort stage deletion. This user-requested destructive action
|
|
// must fail loud: reporting success while runtime data remains is misleading.
|
|
await withRuntimeStorageExclusiveLock(async () => {
|
|
const { bumpGeneration } = await import('@/lib/document-store/storage-generation');
|
|
await bumpGeneration();
|
|
await (runtimeStore ?? getRuntimeStore()).deleteAllRuntime();
|
|
await deleteAllDocuments();
|
|
await clearDocumentStoreKeys();
|
|
await db.delete();
|
|
await clearAssetPool();
|
|
});
|
|
log.info('Database cleared');
|
|
}
|
|
|
|
/** Delete every aggregate without bypassing the document store's open connection. */
|
|
export async function deleteAllDocuments(): Promise<void> {
|
|
const { getDocumentStore } = await import('@/lib/document-store');
|
|
const store = getDocumentStore();
|
|
const documents = await store.listDocuments();
|
|
await Promise.all(documents.map((document) => store.deleteDocument(document.id)));
|
|
}
|
|
|
|
/** Remove device-local metadata owned by the document cutover. */
|
|
export async function clearDocumentStoreKeys(): Promise<void> {
|
|
if (typeof localStorage === 'undefined') return;
|
|
const kv = new BrowserKVStore();
|
|
for (const prefix of ['document-migration:', 'editor-current-scene:']) {
|
|
const keys = await kv.keys(prefix, 'device');
|
|
await Promise.all(keys.map((key) => kv.remove(key, 'device')));
|
|
}
|
|
}
|
|
|
|
function toChatSessionRecord(stageId: string, session: ChatSession): ChatSessionRecord {
|
|
return {
|
|
id: session.id,
|
|
stageId,
|
|
type: session.type,
|
|
title: session.title,
|
|
status: session.status,
|
|
messages: session.messages,
|
|
config: session.config,
|
|
toolCalls: session.toolCalls,
|
|
pendingToolCalls: session.pendingToolCalls,
|
|
createdAt: session.createdAt,
|
|
updatedAt: session.updatedAt,
|
|
sceneId: session.sceneId,
|
|
lastActionIndex: session.lastActionIndex,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Export database contents (for backup)
|
|
*/
|
|
export async function exportDatabase(chatOptions: ChatStorageOptions = {}): Promise<{
|
|
documents: AppDocument[];
|
|
chatSessions: ChatSessionRecord[];
|
|
playbackState: PlaybackStateRecord[];
|
|
}> {
|
|
const {
|
|
accessDocument,
|
|
canonicalizeLegacyOutline,
|
|
canonicalizeLegacyScene,
|
|
canonicalizeLegacyStage,
|
|
getDocumentStore,
|
|
getLegacyDocumentStore,
|
|
} = await import('@/lib/document-store');
|
|
const documentStore = getDocumentStore();
|
|
// Backups must not strand courses that have not yet been opened since cutover.
|
|
// Route them through the normal lazy-migration seam before enumerating aggregates.
|
|
const legacyStages = await getLegacyDocumentStore().listStages();
|
|
await Promise.all(legacyStages.map((stage) => accessDocument(stage.id)));
|
|
const summaries = await documentStore.listDocuments();
|
|
const storedDocuments = (
|
|
await Promise.all(summaries.map((summary) => documentStore.loadDocument(summary.id)))
|
|
).filter((document): document is AppDocument => document !== null);
|
|
const storedIds = new Set(summaries.map((summary) => summary.id));
|
|
const legacyOnlyDocuments = (
|
|
await Promise.all(
|
|
legacyStages
|
|
.filter((stage) => !storedIds.has(stage.id))
|
|
.map(async (stage): Promise<AppDocument | null> => {
|
|
const snapshot = await getLegacyDocumentStore().read(stage.id);
|
|
if (!snapshot) return null;
|
|
const { stage: canonicalStage } = canonicalizeLegacyStage(snapshot.stage);
|
|
const document: AppDocument = {
|
|
stage: canonicalStage,
|
|
scenes: snapshot.scenes.map(canonicalizeLegacyScene).sort((a, b) => a.order - b.order),
|
|
dslVersion: DSL_VERSION,
|
|
};
|
|
if (snapshot.outline) document.outline = canonicalizeLegacyOutline(snapshot.outline);
|
|
return document;
|
|
}),
|
|
)
|
|
).filter((document): document is AppDocument => document !== null);
|
|
const documents = [...storedDocuments, ...legacyOnlyDocuments];
|
|
const legacyChatMap = new Map<string, ChatSessionRecord>();
|
|
for (const session of [
|
|
...(await db.chatSessions.toArray()),
|
|
...(await db.chatRestoreStaging.toArray()),
|
|
]) {
|
|
legacyChatMap.set(JSON.stringify([session.stageId, session.id]), session);
|
|
}
|
|
const legacyChats = [...legacyChatMap.values()];
|
|
const { loadChatSessions } = await import('./chat-storage');
|
|
// Chat sessions live on the learner RuntimeStore — an independent seam from
|
|
// document migration — so legacy-only documents can still own runtime chat
|
|
// history. Enumerate chats for EVERY exported document, not just stored ones.
|
|
// The legacy chat rows are collected separately above by direct table reads,
|
|
// so the RuntimeStore load gets an empty legacy source and never needs the
|
|
// cross-realm migration lock. It may still finalize a pending restore marker;
|
|
// `observe: false` only keeps this export from changing partition memos.
|
|
const runtimeChats = (
|
|
await Promise.all(
|
|
documents.map(async ({ stage }) =>
|
|
(
|
|
await loadChatSessions(stage.id, {
|
|
legacyStore: { load: async () => [], clear: async () => {} },
|
|
...chatOptions,
|
|
fallbackToLegacyOnError: false,
|
|
observe: false,
|
|
})
|
|
).map((session) => toChatSessionRecord(stage.id, session)),
|
|
),
|
|
)
|
|
).flat();
|
|
const runtimeChatKeys = new Set(
|
|
runtimeChats.map((session) => JSON.stringify([session.stageId, session.id])),
|
|
);
|
|
|
|
return {
|
|
documents,
|
|
chatSessions: [
|
|
...runtimeChats,
|
|
...legacyChats.filter(
|
|
(session) => !runtimeChatKeys.has(JSON.stringify([session.stageId, session.id])),
|
|
),
|
|
],
|
|
playbackState: await db.playbackState.toArray(),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Import database contents (for restoring backups)
|
|
*/
|
|
export async function importDatabase(
|
|
data: {
|
|
documents?: AppDocument[];
|
|
stages?: StageRecord[];
|
|
scenes?: SceneRecord[];
|
|
chatSessions?: ChatSessionRecord[];
|
|
playbackState?: PlaybackStateRecord[];
|
|
},
|
|
chatOptions: ChatStorageOptions = {},
|
|
): Promise<void> {
|
|
const { canonicalizeLegacyScene, canonicalizeLegacyStage, mutateDocument } =
|
|
await import('@/lib/document-store');
|
|
const legacyScenesByStage = new Map<string, SceneRecord[]>();
|
|
for (const scene of data.scenes ?? []) {
|
|
const scenes = legacyScenesByStage.get(scene.stageId) ?? [];
|
|
scenes.push(scene);
|
|
legacyScenesByStage.set(scene.stageId, scenes);
|
|
}
|
|
const documents =
|
|
data.documents ??
|
|
(data.stages ?? []).map((legacyStage): AppDocument => {
|
|
const { stage } = canonicalizeLegacyStage(legacyStage);
|
|
return {
|
|
stage,
|
|
scenes: (legacyScenesByStage.get(legacyStage.id) ?? [])
|
|
.map(canonicalizeLegacyScene)
|
|
.sort((a, b) => a.order - b.order),
|
|
};
|
|
});
|
|
const importedDocuments: Array<{
|
|
id: string;
|
|
preImage: AppDocument | null;
|
|
wasDeleted: boolean;
|
|
}> = [];
|
|
const importedCurrentScenes: Array<{ key: string; preImage: unknown | null }> = [];
|
|
const kv = new BrowserKVStore();
|
|
const { isStageDeleted, markStageDeleted, unmarkStageDeleted } = await import('./deleted-stages');
|
|
|
|
try {
|
|
for (const document of documents) {
|
|
// Record the pre-import deletion state alongside the document pre-image:
|
|
// a failed import rolls the document back, so it must roll this back too.
|
|
const wasDeleted = isStageDeleted(document.stage.id);
|
|
await mutateDocument(document.stage.id, async (_existing, store) => {
|
|
const preImage = (await store.loadDocument(document.stage.id)) as AppDocument | null;
|
|
await store.saveDocument(document);
|
|
importedDocuments.push({ id: document.stage.id, preImage, wasDeleted });
|
|
});
|
|
// Explicit document (re)creation: a backup may restore a stage deleted
|
|
// earlier this session under the same id. Lift the deleted flag so later
|
|
// edits of the restored document persist instead of being dropped. (The
|
|
// deletion epoch stays bumped, so pre-delete in-flight writes remain
|
|
// fenced off the restored document.)
|
|
unmarkStageDeleted(document.stage.id);
|
|
}
|
|
for (const legacyStage of data.stages ?? []) {
|
|
if (legacyStage.currentSceneId !== undefined) {
|
|
const key = `editor-current-scene:${legacyStage.id}`;
|
|
const preImage = await kv.get<unknown>(key, 'device');
|
|
await kv.set(
|
|
key,
|
|
{ sceneId: legacyStage.currentSceneId, updatedAt: new Date().toISOString() },
|
|
'device',
|
|
);
|
|
importedCurrentScenes.push({ key, preImage });
|
|
}
|
|
}
|
|
|
|
await withRuntimeStorageSharedLock(async () => {
|
|
const restoredChatStageIds =
|
|
data.chatSessions === undefined
|
|
? []
|
|
: [
|
|
...new Set([
|
|
...documents.map((document) => document.stage.id),
|
|
...data.chatSessions.map((session) => session.stageId),
|
|
]),
|
|
];
|
|
const restoreRows = () =>
|
|
db.transaction(
|
|
'rw',
|
|
[db.stages, db.scenes, db.chatSessions, db.chatRestoreStaging, db.playbackState],
|
|
async () => {
|
|
if (data.chatSessions) {
|
|
for (const stageId of restoredChatStageIds) {
|
|
await db.chatSessions.where('stageId').equals(stageId).delete();
|
|
await db.chatRestoreStaging.where('stageId').equals(stageId).delete();
|
|
}
|
|
await db.chatRestoreStaging.bulkPut(data.chatSessions);
|
|
}
|
|
if (data.playbackState) await db.playbackState.bulkPut(data.playbackState);
|
|
},
|
|
);
|
|
if (data.chatSessions !== undefined) {
|
|
// RuntimeStore cannot join Dexie's transaction. Keep a rollback image
|
|
// until durable restore markers have been created for every affected
|
|
// runtime partition; marker creation failure must not commit half an
|
|
// imported backup.
|
|
const rollbackImage = await db.transaction(
|
|
'r',
|
|
[db.stages, db.scenes, db.chatSessions, db.chatRestoreStaging, db.playbackState],
|
|
async () => ({
|
|
chatSessions: (
|
|
await Promise.all(
|
|
restoredChatStageIds.map((stageId) =>
|
|
db.chatSessions.where('stageId').equals(stageId).toArray(),
|
|
),
|
|
)
|
|
).flat(),
|
|
chatRestoreStaging: (
|
|
await Promise.all(
|
|
restoredChatStageIds.map((stageId) =>
|
|
db.chatRestoreStaging.where('stageId').equals(stageId).toArray(),
|
|
),
|
|
)
|
|
).flat(),
|
|
playbackState: (
|
|
await db.playbackState.bulkGet(
|
|
(data.playbackState ?? []).map((playback) => playback.stageId),
|
|
)
|
|
).filter((playback): playback is PlaybackStateRecord => playback !== undefined),
|
|
}),
|
|
);
|
|
const rollbackRows = () =>
|
|
db.transaction(
|
|
'rw',
|
|
[db.stages, db.scenes, db.chatSessions, db.chatRestoreStaging, db.playbackState],
|
|
async () => {
|
|
for (const stageId of restoredChatStageIds) {
|
|
await db.chatSessions.where('stageId').equals(stageId).delete();
|
|
await db.chatRestoreStaging.where('stageId').equals(stageId).delete();
|
|
}
|
|
await db.playbackState.bulkDelete(
|
|
(data.playbackState ?? []).map((playback) => playback.stageId),
|
|
);
|
|
await db.chatSessions.bulkPut(rollbackImage.chatSessions);
|
|
await db.chatRestoreStaging.bulkPut(rollbackImage.chatRestoreStaging);
|
|
await db.playbackState.bulkPut(rollbackImage.playbackState);
|
|
},
|
|
);
|
|
const { restoreChatSessionsFromBackup } = await import('./chat-storage');
|
|
await restoreChatSessionsFromBackup(restoredChatStageIds, restoreRows, {
|
|
...chatOptions,
|
|
globalLockHeld: true,
|
|
rollbackLegacyRows: rollbackRows,
|
|
});
|
|
} else {
|
|
await restoreRows();
|
|
}
|
|
log.info('Database imported successfully');
|
|
});
|
|
} catch (error) {
|
|
for (const { key, preImage } of importedCurrentScenes.reverse()) {
|
|
try {
|
|
if (preImage === null) await kv.remove(key, 'device');
|
|
else await kv.set(key, preImage, 'device');
|
|
} catch (rollbackError) {
|
|
log.error(`Failed to roll back imported current-scene key ${key}:`, rollbackError);
|
|
}
|
|
}
|
|
for (const { id, preImage, wasDeleted } of importedDocuments.reverse()) {
|
|
try {
|
|
await mutateDocument(id, async (_document, store) => {
|
|
if (preImage) await store.saveDocument(preImage);
|
|
else await store.deleteDocument(id);
|
|
});
|
|
// The rollback reinstated the pre-import world; reinstate the deletion
|
|
// state the import lifted, or the rolled-back (absent) document would
|
|
// stay writable and an outstanding flush could recreate it. Re-marking
|
|
// bumps the epoch again — consistent either way, since every pre-import
|
|
// capture is already stale. Deliberately skipped when the rollback
|
|
// write itself failed above: the imported document then still exists,
|
|
// and re-marking would silently drop edits to a document that is
|
|
// present (the exact bug the lift exists to prevent).
|
|
if (wasDeleted) markStageDeleted(id);
|
|
} catch (rollbackError) {
|
|
log.error(`Failed to roll back imported document ${id}:`, rollbackError);
|
|
}
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// ==================== Convenience Query Functions ====================
|
|
|
|
/**
|
|
* Get all scenes for a course
|
|
*/
|
|
export async function getScenesByStageId(stageId: string): Promise<Scene[]> {
|
|
const { accessDocument } = await import('@/lib/document-store');
|
|
return (await accessDocument(stageId)).document?.scenes ?? [];
|
|
}
|
|
|
|
/**
|
|
* Delete a course and all its related data
|
|
*
|
|
* @deprecated No production caller remains. `deleteStageData` is the primary
|
|
* stage-deletion path; this compatibility helper retains its broader author-side cascade.
|
|
*/
|
|
export async function deleteStageWithRelatedData(stageId: string): Promise<void> {
|
|
const { mutateDocument } = await import('@/lib/document-store');
|
|
// storageSharedLockHeld: the cascade holds the EXCLUSIVE epoch, which
|
|
// subsumes shared — the generation-guarded store must not re-acquire shared
|
|
// inside it (self-deadlock against our own exclusive hold).
|
|
await mutateDocument(
|
|
stageId,
|
|
async (document, store) =>
|
|
withRuntimeStorageExclusiveLockUntilSettled(async (releaseCaller) => {
|
|
const {
|
|
buildStageAssetReclamationPlan,
|
|
executeStageAssetReclamation,
|
|
loadStageAssetInventory,
|
|
} = await import('@/lib/media/reclaim-stage-assets');
|
|
const deletionDocument = document ?? {
|
|
stage: { id: stageId, name: '', createdAt: 0, updatedAt: 0 },
|
|
scenes: [],
|
|
};
|
|
const inventory = await loadStageAssetInventory(deletionDocument);
|
|
const assetPlan = buildStageAssetReclamationPlan(
|
|
stageId,
|
|
inventory.refs,
|
|
inventory.mediaRows,
|
|
inventory.audioRows,
|
|
);
|
|
await store.deleteDocument(stageId);
|
|
await executeStageAssetReclamation(assetPlan, null);
|
|
await db.transaction(
|
|
'rw',
|
|
[
|
|
db.stages,
|
|
db.scenes,
|
|
db.chatSessions,
|
|
db.chatRestoreStaging,
|
|
db.playbackState,
|
|
db.stageOutlines,
|
|
db.generatedAgents,
|
|
db.agentEditSessions,
|
|
],
|
|
async () => {
|
|
await db.stages.delete(stageId);
|
|
await db.scenes.where('stageId').equals(stageId).delete();
|
|
await db.chatSessions.where('stageId').equals(stageId).delete();
|
|
await db.chatRestoreStaging.where('stageId').equals(stageId).delete();
|
|
await db.playbackState.delete(stageId);
|
|
await db.stageOutlines.delete(stageId);
|
|
await db.generatedAgents.where('stageId').equals(stageId).delete();
|
|
await db.agentEditSessions.where('stageId').equals(stageId).delete();
|
|
},
|
|
);
|
|
// Learner-runtime data lives in a separate IndexedDB database, so it is
|
|
// cascaded after the Dexie transaction: it cannot join it, and a runtime
|
|
// failure must not abort it (the helper warns instead of throwing).
|
|
const runtimeDeletion = beginStageRuntimeDeletionSafely(stageId);
|
|
await runtimeDeletion.completion;
|
|
releaseCaller(undefined);
|
|
await runtimeDeletion.settlement;
|
|
}),
|
|
{ storageSharedLockHeld: true },
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get all generated agents for a course
|
|
*/
|
|
export async function getGeneratedAgentsByStageId(
|
|
stageId: string,
|
|
): Promise<GeneratedAgentRecord[]> {
|
|
return db.generatedAgents.where('stageId').equals(stageId).toArray();
|
|
}
|
|
|
|
/**
|
|
* Get database statistics
|
|
*/
|
|
export async function getDatabaseStats() {
|
|
const { getDocumentStore } = await import('@/lib/document-store');
|
|
const documents = await getDocumentStore().listDocuments();
|
|
return {
|
|
documents: documents.length,
|
|
documentScenes: documents.reduce((total, document) => total + document.sceneCount, 0),
|
|
legacyStages: await db.stages.count(),
|
|
legacyScenes: await db.scenes.count(),
|
|
audioFiles: await db.audioFiles.count(),
|
|
imageFiles: await db.imageFiles.count(),
|
|
snapshots: await db.snapshots.count(),
|
|
chatSessions: await db.chatSessions.count(),
|
|
playbackState: await db.playbackState.count(),
|
|
stageOutlines: await db.stageOutlines.count(),
|
|
mediaFiles: await db.mediaFiles.count(),
|
|
generatedAgents: await db.generatedAgents.count(),
|
|
};
|
|
}
|