dorfteich/packages/shared/src/conversion.ts
Claude Fable 5 8ae010218e
Some checks failed
CD / Build and push images (push) Successful in 4m5s
CD / Deploy to Test (push) Successful in 9s
CI / Lint, typecheck, test (push) Failing after 4m21s
CI / Auth e2e pack (push) Has been skipped
CI / Import/export fidelity gate (push) Has been skipped
CI / Build container images (push) Has been skipped
CD / Smoke tests against Test (push) Successful in 1m22s
CD / Promote to Int (push) Successful in 11s
Obsidian vault import: endpoint, job orchestration, rollback (#117)
POST /ponds/:pondId/import/vault (pond-admin-gated; a vault import
creates a subtree, uploads files, and creates labels — administration,
not everyday editing) takes the ZIP plus a JSON options field
{parentPageId?, labelIds?, frontmatterMode}. The archive is parsed at
enqueue for fast 400s; the job (new kind import_vault, riding the
existing isImportKind worker routing) re-parses and runs the #116
transform, then: containers top-down → notes (asset placeholders →
uploaded pond files; non-images become page attachments) → tags to
labels (nested tags build a label hierarchy via LabelsService, so
locking and cache invalidation apply) plus the dialog labels.

All-or-nothing: any failure hard-deletes the created pages (children
first) and removes the stored files (quota restored), then surfaces as
import_vault_invalid_zip / import_vault_too_large / quota_exceeded /
conversion_failed — and makes the worker's retry policy safe.

Supporting changes:
- conversion_jobs gains a nullable options jsonb column; enqueue takes
  kind-specific options and a maxInputBytes override (the 25 MiB
  default protects the pandoc sidecar, which a vault never touches —
  vaults use the 64 MiB upload limit).
- insertPage accepts a pre-reserved slug (the batch reserves all slugs
  up front against pond ∪ batch).
- NEW: pages born with content seed their outgoing page_links rows
  (deriveContent now returns wikilinkSlugs) — imported pages would
  otherwise stay invisible to backlinks and the graph until their
  first collab save. Collab still rewrites the rows on every save, and
  the existing phantom resolution heals batch creation order.

import-vault.e2e.db.test.ts (4 tests, real worker drained): gating +
input rejection, the full fixture import (tree under a mount page,
collision suffixes, link rows incl. phantom, nested tag labels, extra
label everywhere, frontmatter stripped, image embedded + PDF attached),
complete quota rollback, and a clean re-import with fresh suffixes.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-14 17:13:11 +02:00

67 lines
2.9 KiB
TypeScript

import { z } from 'zod';
/**
* Import/export conversion job types shared between api and web (ADR 0009,
* issue #62). A conversion runs asynchronously against the pandoc sidecar;
* the client enqueues it and polls `GET /jobs/:id` for this view.
*/
export type ConversionJobStatus = 'pending' | 'running' | 'succeeded' | 'failed';
export interface ConversionJobView {
id: string;
status: ConversionJobStatus;
kind: string;
sourceFormat: string;
targetFormat: string;
/** Set only when `status` is `failed` — a code from the errors namespace
* (`converter_unavailable` | `converter_timeout` | `conversion_failed` |
* `quota_exceeded`). */
errorCode: string | null;
/** Set once an import job (#63) succeeds: the id of the page it created, so
* the client can navigate to it. `null` for a pending/failed import and for
* plain byte→byte conversions (export). */
resultPageId: string | null;
/** For a data-export job (#68), when its download link stops working — the
* result is purged after this. `null` for every other job kind, which never
* expires. */
expiresAt: string | null;
createdAt: string;
updatedAt: string;
}
/** Extensions the import endpoint accepts (ADR 0009, issues #63/#64). `.docx`
* and `.odt` convert via the sidecar (a job to poll); `.md`/`.markdown` import
* in-process and come back already `succeeded`. */
export const IMPORT_EXTENSIONS = ['docx', 'odt', 'md', 'markdown'] as const;
export type ImportExtension = (typeof IMPORT_EXTENSIONS)[number];
/** What happens to a note's YAML frontmatter on vault import (issue #117):
* dropped, or preserved as a yaml code block at the top of the page. */
export const VAULT_FRONTMATTER_MODES = ['strip', 'preserve'] as const;
export type VaultFrontmatterMode = (typeof VAULT_FRONTMATTER_MODES)[number];
/**
* Options of an Obsidian vault import (issue #117), sent as a JSON string in
* the multipart `options` field and stored on the job row. `parentPageId`
* mounts the vault under an existing page (null/absent = the pond root);
* `labelIds` are assigned to every imported page on top of the tag labels.
*/
export const importVaultOptionsSchema = z.object({
parentPageId: z.string().min(1).nullish(),
labelIds: z.array(z.string().min(1)).max(20).default([]),
frontmatterMode: z.enum(VAULT_FRONTMATTER_MODES).default('strip'),
});
export type ImportVaultOptions = z.infer<typeof importVaultOptionsSchema>;
/** Formats a single page exports to via a conversion job whose result is
* downloaded from `GET /jobs/:id/result` (issues #65/#67). `docx`/`odt` run
* `markdown → pandoc`; `pdf` renders HTML through Gotenberg. Markdown export is
* a separate direct download (#30). */
export const EXPORT_FORMATS = ['docx', 'odt', 'pdf'] as const;
export type ExportFormat = (typeof EXPORT_FORMATS)[number];
export const pageExportInputSchema = z.object({
format: z.enum(EXPORT_FORMATS),
});
export type PageExportInput = z.infer<typeof pageExportInputSchema>;