// The export extension's routes (/extensions/export): the local redaction // tagger the daemon may download, and the redacted trace exports of a // session. The daemon checks every gate; nothing here decides what may leave. import type { components } from './api.gen'; import { failure, request } from './api'; const BASE = '/extensions/export'; type Schemas = components['schemas']; export type Tagger = Schemas['ExportTagger']; export type TaggerState = Tagger['state']; export type ExportJob = Schemas['ExportJob']; export type ExportScope = ExportJob['scope']; export type TaggerError = Schemas['ExportTaggerError']; export type JobError = Schemas['ExportJobError']; export type OmissionKind = Schemas['ExportOmissionKind']; /** One trace.jsonl row, or a piece of a long one: `offset` is its UTF-8 byte offset in the row, `complete` marks the row's last piece. */ export type PreviewLine = Schemas['ExportPreviewLine']; export type PreviewPage = Schemas['ExportPreviewPage']; /** One redaction: Unicode scalar offsets into the redacted string at `path` (a JSON Pointer into row `line`), or into that key. */ export type Finding = Schemas['ExportFinding']; export type FindingPage = Schemas['ExportFindingPage']; export type FindingCategory = Schemas['ExportCategory']; /** The tagger as this daemon has it: absent, downloading, verifying, ready, or failed. */ export function readTagger(signal?: AbortSignal): Promise { return request('GET', `${BASE}/tagger`, { signal }); } /** Starts or resumes the model download; the daemon answers with the tagger's new state. */ export function installTagger(): Promise { return request('POST', `${BASE}/tagger/install`); } /** Stops a download, keeping verified partial files to resume from. */ export function cancelTagger(): Promise { return request('POST', `${BASE}/tagger/cancel`); } /** Deletes the downloaded model; the daemon refuses while an export uses it. */ export function removeTagger(): Promise { return request('DELETE', `${BASE}/tagger`); } /** Starts an export. Sending the same id and scope again replays it; another scope under that id is a 409. */ export function createExport( sessionId: string, id: string, scope: ExportScope, ): Promise { return request( 'POST', `${BASE}/sessions/${encodeURIComponent(sessionId)}/exports`, { body: { id, scope } }, ); } /** The session's newest exports, newest first. */ export async function listExports( sessionId: string, signal?: AbortSignal, ): Promise { const page = await request( 'GET', `${BASE}/sessions/${encodeURIComponent(sessionId)}/exports`, { signal }, ); return page.items; } /** One export and the validator approving it needs. */ export async function readExport( id: string, signal?: AbortSignal, ): Promise<{ job: ExportJob; etag: string }> { const response = await fetch( `/api${BASE}/exports/${encodeURIComponent(id)}`, { signal, headers: { accept: 'application/json' } }, ); if (!response.ok) throw await failure(response); return { job: await response.json(), etag: response.headers.get('etag') ?? '', }; } /** One bounded page of the redacted trace.jsonl, the exact rows the archive holds. */ export function readPreview( id: string, next: string | null, signal?: AbortSignal, ): Promise { return request( 'GET', `${BASE}/exports/${encodeURIComponent(id)}/preview?${page(next, 50)}`, { signal, }, ); } /** One page of what the scan redacted, the same data as redaction-report.json. */ export function readFindings( id: string, from: { next: string | null; line: number }, signal?: AbortSignal, ): Promise { const query = page(from.next, 200); if (!from.next) query.set('line_from', String(from.line)); return request( 'GET', `${BASE}/exports/${encodeURIComponent(id)}/findings?${query}`, { signal, }, ); } function page(next: string | null, limit: number): URLSearchParams { const query = new URLSearchParams({ limit: String(limit) }); if (next) query.set('next', next); return query; } /** Approves the reviewed copy at the revision read; a stale `etag` is refused. */ export function approveExport(id: string, etag: string): Promise { return request('POST', `${BASE}/exports/${encodeURIComponent(id)}/approve`, { body: { reviewed: true }, headers: { 'if-match': etag }, }); } /** Stops a scan; nothing of it can be downloaded. */ export function cancelExport(id: string): Promise { return request('POST', `${BASE}/exports/${encodeURIComponent(id)}/cancel`); } /** Deletes this export's redacted copy; the session is untouched. */ export function deleteExport(id: string): Promise { return request('DELETE', `${BASE}/exports/${encodeURIComponent(id)}`); } /** * Starts the browser's own download of an approved export's zip. It is a * same-origin link, so the web cookie signs it and the daemon's answer * streams to disk under the name its Content-Disposition gives, with no * copy held in the page. A refusal shows in the browser's downloads. */ export function downloadArchive(id: string) { const link = document.createElement('a'); link.href = `/api${BASE}/exports/${encodeURIComponent(id)}/archive`; link.download = ''; link.rel = 'noopener'; document.body.append(link); link.click(); link.remove(); }