/** Browser download operations and FIFO modal state shared by menu actions and `/export`. */ import { createSnapshotStore, type SnapshotStore } from '@deepseek-ai/dsh-client-store' import type { SessionId } from '@deepseek-ai/dsh-session/types' import { SESSION_LOG_EXPORT_ROUTE } from '../routes.ts' /** Download phases presented by the shared modal. */ export type SessionLogDownloadStatus = 'downloading' | 'success' | 'error' /** One Session's current download-dialog state. */ export interface SessionLogDownloadEntry { /** Initiation order of this operation; repeated in-flight requests keep it. */ readonly order: number readonly open: boolean readonly status: SessionLogDownloadStatus readonly error: string | null } /** Authoritative download states; open entries display in initiation order across Sessions. */ export interface SessionLogDownloadState { bySession: Record } /** HTTP carrier for the export route. */ type Fetch = (input: string | URL, init?: RequestInit) => Promise type Save = (url: string, filename: string) => void const INITIAL: SessionLogDownloadState = { bySession: {} } /** * Collapse an untrusted Session id into the filename convention owned by the host endpoint. * @param sessionId - Session whose archive is downloaded. * @returns one safe browser download filename. */ export function sessionLogZipFilename(sessionId: SessionId): string { return `dsh-session-${String(sessionId).replace(/[^A-Za-z0-9_-]/g, '_')}.zip` } /** * Hand a Host download route to the browser download manager, which resolves it * against the document's own base. * @param url - document-relative Host download route. * @param filename - browser download filename. */ export function downloadUrl(url: string, filename: string): void { const anchor = document.createElement('a') anchor.href = url anchor.download = filename anchor.click() } function messageOf(error: unknown): string { return error instanceof Error ? error.message : String(error) } /** Owns one in-flight browser download per Session and publishes modal state. */ export class SessionLogDownloadController { /** Stable snapshot source for the root modal and menu availability. */ readonly store: SnapshotStore = createSnapshotStore(INITIAL) private readonly active = new Map }>() private disposed = false private nextOrder = 0 /** * @param fetcher - HTTP carrier used to read the host-streamed ZIP. * @param save - browser save operation. */ constructor( private readonly fetcher: Fetch = (input, init) => fetch(input, init), private readonly save: Save = downloadUrl, ) {} /** * Download one Session tree; concurrent gestures for the same Session share one operation. * Different Sessions run concurrently; each new operation joins the modal FIFO. * @param sessionId - root Session whose ZIP includes descendants and attachments. * @returns after the browser save starts, an error state is published, or a late post-disposal request is ignored. */ download(sessionId: SessionId): Promise { const existing = this.active.get(sessionId) if (existing !== undefined) return existing.done if (this.disposed) return Promise.resolve() const abort = new AbortController() const done = this.run(sessionId, abort.signal, this.nextOrder++).finally(() => { this.active.delete(sessionId) }) this.active.set(sessionId, { abort, done }) return done } /** * Close one Session's dialog without cancelling an in-flight browser download. * @param sessionId - Session whose modal closes. */ dismiss(sessionId: SessionId): void { const current = this.store.getSnapshot().bySession[String(sessionId)] if (current === undefined || !current.open) return this.publish(sessionId, { ...current, open: false }) } /** * Abort active fetches and reach quiescence. * @returns after every active operation settles. */ async dispose(): Promise { this.disposed = true const active = [...this.active.values()] for (const operation of active) operation.abort.abort() await Promise.allSettled(active.map(operation => operation.done)) } private async run(sessionId: SessionId, signal: AbortSignal, order: number): Promise { this.publish(sessionId, { order, open: true, status: 'downloading', error: null }) try { const query = new URLSearchParams({ sessionId, includeDescendants: 'true' }) const route = `${SESSION_LOG_EXPORT_ROUTE}?${query.toString()}` const response = await this.fetcher(route, { method: 'HEAD', signal }) if (!response.ok) { const detail = await response.text().catch(() => '') throw new Error(`Export failed: HTTP ${response.status}${detail === '' ? '' : ` ${detail}`}`) } if (signal.aborted) return this.save(route, sessionLogZipFilename(sessionId)) const open = this.store.getSnapshot().bySession[String(sessionId)]?.open ?? true this.publish(sessionId, { order, open, status: 'success', error: null }) } catch (error: unknown) { if (signal.aborted) return const open = this.store.getSnapshot().bySession[String(sessionId)]?.open ?? true this.publish(sessionId, { order, open, status: 'error', error: messageOf(error) }) } } private publish(sessionId: SessionId, entry: SessionLogDownloadEntry): void { this.store.update((state) => { state.bySession = { ...state.bySession, [String(sessionId)]: entry } }) } }