vibe.workbench.draft_manager

Draft State Manager.

Handles all filesystem operations for workbench template drafts within the unified data directory. Provides secure access to draft files and prevents path traversal attacks.

DraftStateManager

Manages template draft storage and retrieval on the filesystem.

Each draft is stored in a subdirectory named with a UUIDv4 within the unified data directory. Each draft contains: - source.docx: The original uploaded document - draft_config.json: The template configuration - markup_overlay.json: Document markup annotations

__init__

__init__(drafts_root_dir: Path | None = None) -> None

Initialize the draft manager.

Parameters:
  • drafts_root_dir (Path | None, default: None ) –

    Root directory for drafts. If None, uses unified data directory.

create_draft

create_draft(uploaded_file: FileStorage) -> str

Create a new draft from an uploaded file.

Parameters:
  • uploaded_file (FileStorage) –

    Flask FileStorage object with the uploaded .docx

Returns:
  • str( str ) –

    The generated draft_id (UUIDv4)

create_draft_from_template

create_draft_from_template(template_provider: TemplateDataProvider, template_id: str) -> str

Create a new draft from an existing template.

Parameters:
  • template_provider (TemplateDataProvider) –

    VIBE TemplateDataProvider instance

  • template_id (str) –

    ID of the existing template to copy

Returns:
  • str( str ) –

    The generated draft_id (UUIDv4)

get_draft_path

get_draft_path(draft_id: str) -> Path

Get the filesystem path for a draft, with security validation.

Parameters:
  • draft_id (str) –

    The draft identifier

Returns:
  • Path( Path ) –

    The draft directory path

Raises:
  • ValueError

    If draft_id contains invalid characters or path traversal

draft_exists

draft_exists(draft_id: str) -> bool

Check if a draft exists.

Parameters:
  • draft_id (str) –

    The draft identifier

Returns:
  • bool( bool ) –

    True if the draft directory exists

read_json

read_json(draft_id: str, filename: str) -> dict[str, Any]

Read a JSON file from a draft directory.

Parameters:
  • draft_id (str) –

    The draft identifier

  • filename (str) –

    Name of the JSON file to read

Returns:
  • dict( dict[str, Any] ) –

    The parsed JSON data

Raises:
  • FileNotFoundError

    If the draft or file doesn't exist

  • ValueError

    If the JSON is invalid

write_json

write_json(draft_id: str, filename: str, data: dict[str, Any]) -> None

Write a JSON file to a draft directory.

Parameters:
  • draft_id (str) –

    The draft identifier

  • filename (str) –

    Name of the JSON file to write

  • data (dict[str, Any]) –

    The data to write as JSON

Raises:
  • FileNotFoundError

    If the draft doesn't exist

get_source_docx_path

get_source_docx_path(draft_id: str) -> Path

Get the path to the source.docx file for a draft.

Parameters:
  • draft_id (str) –

    The draft identifier

Returns:
  • Path( Path ) –

    Path to the source.docx file

Raises:
  • FileNotFoundError

    If the draft or source file doesn't exist

delete_draft

delete_draft(draft_id: str) -> None

Delete a draft and all its files.

Parameters:
  • draft_id (str) –

    The draft identifier

list_drafts

list_drafts() -> list[str]

List all existing draft IDs.

Returns:
  • list( list[str] ) –

    List of draft ID strings