# Types

> TypeScript types and error classes exported from revise.

All declarations below are exported from `"revise"`:

```ts
import type { ReviseEditorHandle, ReviseToolbarState } from "revise";
```

## Documents and export

```ts
type ReviseDocumentSource = File | Blob | ArrayBuffer;
type ReviseDocumentFormat = "docx" | "odt" | "rtf" | "markdown" | "txt" | "html";
type ReviseExportFormat = "docx" | "pdf" | "html" | "markdown" | "txt";

interface ReviseDocument {
  type: "doc";
  children: unknown[];
  [key: string]: unknown;
}

interface ReviseDocumentStatistics {
  wordCount: number;
  characterCount: number;
}

type ReviseSourceFidelity = "exact" | "grafted" | "converted";

interface ReviseSourceFidelityReport {
  fidelity: ReviseSourceFidelity;
  regeneratedBlockIds: string[];
  changedDomains: string[];
  convertedReason?:
    | "no-capture"
    | "no-carrier"
    | "carrier-hash-mismatch"
    | "unpaired-capture";
}

interface EditorUserIdentity {
  id?: string;
  name?: string;
  email?: string;
  image?: string;
  color?: string;
}

interface ReviseDocumentInput {
  id: string;
  source?: ReviseDocumentSource;
  docx?: ReviseDocumentSource; // legacy alias for source
  collaboration?: ReviseCollaborationConfig;
  role?: ReviseDocumentRole;
  format?: ReviseDocumentFormat;
  title?: string;
  documentMode?: ReviseDocumentMode;
  readOnly?: boolean;
  zoom?: ReviseZoom;
  defaultCommentsOpen?: boolean;
  agent?: ReviseAgentConfig;
}

interface ReviseOpenDocumentOptions {
  activate?: boolean; // default true
}

interface ReviseOpenDocument {
  id: string;
  title: string;
  status: "opening" | "ready" | "error";
  active: boolean;
  error?: string;
}

interface ReviseDocumentCollectionState {
  activeDocumentId: string | null;
  documents: ReviseOpenDocument[];
}

interface ReviseExportOptions {
  format?: ReviseExportFormat;
  mode?: "review" | "final";
  onWarnings?: (warnings: string[]) => void; // PDF only
  strict?: boolean; // PDF only: throws ReviseExportError when degraded
}

declare class ReviseExportError extends Error {
  readonly code: "export_degraded";
  readonly warnings: readonly string[];
}

type ReviseDocumentScoped<T> = T & { forDocument(documentId: string): T };
```

## Collaboration

`ReviseYDoc` and `ReviseAwareness` are typed `any` to avoid a type-level dependency on Yjs.

```ts
type ReviseYDoc = any;
type ReviseAwareness = any;

interface ReviseCollaborationProvider {
  document?: ReviseYDoc;
  doc?: ReviseYDoc;
  getYDoc?: () => ReviseYDoc;
  awareness?: ReviseAwareness | null;
}

type ReviseCollaborationConnect = (
  ydoc: ReviseYDoc,
  awareness: ReviseAwareness,
) => ReviseCollaborationProvider | { destroy?: () => void } | void;

interface ReviseCollaborationConfig {
  connect?: ReviseCollaborationConnect;
  provider?: ReviseCollaborationProvider;
  ydoc?: ReviseYDoc;
  awareness?: ReviseAwareness | null;
  remoteOrigin?: unknown; // required when passing ydoc without provider
  synced?: boolean;
  seed?: "if-empty" | "never"; // default "if-empty"
}

interface RevisePeer {
  clientId: number;
  isLocal: boolean;
  id?: string;
  name?: string;
  email?: string;
  image?: string;
  color: string;
}

interface ReviseCollaborationState {
  enabled: boolean;
  synced: boolean;
  peers: RevisePeer[];
}
```

## Roles

```ts
type ReviseDocumentRole = "editor" | "suggester" | "viewer";

interface ReviseRolePolicy {
  readonly modes: readonly ReviseDocumentMode[];
  readonly canEdit: boolean;
  readonly canEditDirectly: boolean;
  readonly canResolveSuggestions: boolean;
  readonly canComment: boolean;
}

declare function rolePolicy(role: ReviseDocumentRole): ReviseRolePolicy;

declare class ReviseRoleError extends Error {
  readonly role: ReviseDocumentRole;
  readonly action: string;
}
```

## Tracked changes and settings

```ts
interface ReviseTrackedChange {
  id: string;
  kind: "insert" | "delete" | "format" | "move";
  author: string;
  authorType: "human" | "ai";
  agentModel?: string;
  createdAt?: number; // epoch ms
  description?: string;
  blockIds: string[];
  moveSourceBlockIds?: string[];
  moveDestinationBlockIds?: string[];
  insertedText?: string;
  deletedText?: string;
  commentThreadId?: string;
}

interface ReviseTrackedChangesSettings {
  showRemovalsInReview?: boolean; // default false
  markupStyle?: "revise" | "word"; // default "revise"
  strictMode?: boolean; // default true
}

interface ReviseEditorSettings {
  trackedChanges?: ReviseTrackedChangesSettings;
}
```

## Modes, toolbar, and view state

```ts
type ReviseDocumentMode = "editing" | "suggesting" | "viewing";
type ReviseToolbarMode = "native" | "compact" | "floating" | "none";
type ReviseZoom = number | "fit-width";

interface ReviseZoomState {
  zoom: ReviseZoom;
  scale: number;
}

interface ReviseToolbarState {
  ready: boolean;
  readOnly: boolean;
  documentMode: ReviseDocumentMode;
  activeTab: "edit" | "layout" | "insert" | "tools" | "review";
  hasSelection: boolean;
  canUndo: boolean;
  canRedo: boolean;
  formatting: Record<string, unknown> & {
    bold?: boolean;
    italic?: boolean;
    underline?: boolean;
    strikethrough?: boolean;
    color?: string;
    backgroundColor?: string;
    fontFamily?: string;
  };
  heading?: 1 | 2 | 3 | 4 | 5 | 6;
  alignment?: "left" | "center" | "right" | "justify";
  lineSpacing?: number;
  fontSizePt?: number;
  blockType: { type: string; variant?: string } | null;
  review: {
    open: boolean;
    targetCount: number;
    currentTargetSuggestionCount: number;
    allOpenSuggestionCount: number;
    showRemovals: boolean;
    viewMode: "all-markup" | "final" | "original";
  };
}

interface ReviseViewState {
  ready: boolean;
  title: string;
  documentMode: ReviseDocumentMode;
  readOnly: boolean;
  commentsOpen: boolean;
  commentCount: number;
  reviewOpen: boolean;
  reviewTargetCount: number;
}
```

## Review and comments

```ts
interface ReviseReviewState {
  open: boolean;
  commentsOpen: boolean;
  targetCount: number;
  currentTargetSuggestionCount: number;
  allOpenSuggestionCount: number;
  openSuggestionIds: string[];
  currentSuggestionIds: string[];
  visibleAgentSuggestionIds: string[];
  activeCommentId: string | null;
  showRemovals: boolean;
  viewMode: "all-markup" | "final" | "original";
  commentThreads: ReviseCommentThread[];
}

interface ReviseCommentAnchor {
  blockId: string;
  start: number;
  end: number;
}

interface ReviseCommentRecord {
  id: string;
  author: string;
  initials?: string;
  authorId?: string;
  authorImageUrl?: string;
  createdAt: string;
  bodyMd: string;
  mentions?: ReviseCommentMention[];
  parentId?: string;
  relatedSuggestionIds?: string[];
  resolved?: boolean;
}

interface ReviseCommentMention {
  id: string;
  type: "user" | "agent";
  label: string; // includes "@"
  start: number; // UTF-16 offset in bodyMd
  end: number;
  email?: string;
}

interface ReviseCommentThread {
  root: ReviseCommentRecord;
  replies: ReviseCommentRecord[];
  anchor: ReviseCommentAnchor | null;
  relatedSuggestionIds: string[];
  detached: boolean;
}

type ReviseCommentAgentStatus =
  | "running"
  | "complete"
  | "error"
  | "interrupted"
  | "canceled";

interface ReviseCommentAgentState {
  threadId: string;
  status: ReviseCommentAgentStatus;
  statusText?: string;
  error?: string;
  relatedSuggestionIds?: string[];
  updatedAt: number;
}

interface ReviseCommentAgentRunResult {
  threadId: string;
  status: ReviseCommentAgentStatus;
  reply?: string;
  relatedSuggestionIds: string[];
  error?: string;
}
```

## Selection

```ts
interface ReviseSelectionPosition {
  kind: "text";
  blockId: string;
  offset: number;
}

interface ReviseTextTargetSegment {
  blockId: string;
  range: { start: number; end: number };
}

interface ReviseTextTarget {
  kind: "text";
  segments: ReviseTextTargetSegment[];
}

interface ReviseSelectionTarget {
  kind: "selection";
  start: ReviseSelectionPosition;
  end: ReviseSelectionPosition;
}

interface ReviseSelectionSnapshot {
  documentId: string | null;
  anchor: ReviseSelectionPosition | null;
  focus: ReviseSelectionPosition | null;
  start: ReviseSelectionPosition | null;
  end: ReviseSelectionPosition | null;
  target: ReviseTextTarget | null;
  selectionTarget: ReviseSelectionTarget | null;
  activeMarks: string[];
  activeCommentIds: string[];
  activeChangeIds: string[];
  quotedText: string;
  text: string;      // alias for quotedText
  collapsed: boolean;
  empty: boolean;    // alias for collapsed
}

type ReviseSelectionCapture = ReviseSelectionSnapshot & {
  documentId: string;
  target: ReviseTextTarget;
  selectionTarget: ReviseSelectionTarget;
};

type ReviseSelectionRestoreResult =
  | { success: true }
  | { success: false; reason: "not-ready" | "read-only" | "missing-target" | "stale" };
```

## Tools and decisions

```ts
interface ReviseEditorToolDefinition {
  name: string;
  description: string;
  inputSchema: Record<string, unknown>;
}

interface ReviseEditorToolExecutionOptions {
  documentId?: string;
  directMode?: boolean;
}

type ReviseToolResult<Name> =
  | { ok: true; value: ReviseToolResponse<Name> }
  | { ok: false; error: ReviseToolFailure<Name> };

interface ReviseToolResponse<Name> {
  callId: string;
  tool: Name;
  message: string | null;
  data: ReviseToolData<Name>;
  context: { format: "revise-html"; html: string } | null;
  suggestionIds: string[] | null;
}

interface ReviseToolFailure<Name> {
  callId: string;
  tool: Name;
  code: string;
  message: string;
  details?: unknown;
}

interface ReviseSuggestionRefusal {
  id: string;
  code: "decision_unsupported_structure" | "decision_incomplete";
}

interface ReviseSuggestionDecision {
  resolved: string[];
  missing: string[];
  unresolved: string[];
  refused?: ReviseSuggestionRefusal[];
}
```

## Agent and fonts

```ts
interface ReviseAgentMessage {
  role: string;
  content: string;
  timestamp?: number;
  toolName?: string;
  incomplete?: boolean;
  [key: string]: unknown;
}

interface ReviseAgentTurnRequest {
  prompt: string;
  currentDocument: ReviseDocument;
  currentTurn: number;
  config: Record<string, unknown>;
  [key: string]: unknown;
}

interface ReviseAgentStreamEvent {
  type:
    | "text"
    | "tool_call"
    | "tool_result"
    | "tool_start"
    | "usage"
    | "error"
    | "done"
    | "request_mode"
    | "web_search"
    | "reasoning"
    | "history_repair";
  data: any;
}

type ReviseAgentTurnStream = (
  request: ReviseAgentTurnRequest,
) => AsyncGenerator<ReviseAgentStreamEvent, void, unknown>;

interface ReviseAgentRunOptions {
  executionPolicy?: {
    documentAccess: "read" | "comment" | "edit";
    interaction: "interactive" | "best_effort";
    allowedTools?: readonly string[] | null;
  };
  developerPrePrompt?: string;
  allowedToolNames?: string[];
  providerOverride?: string;
  modelOverride?: string;
  forceFirstTurnNoTools?: boolean;
  attachments?: unknown[];
  directMode?: boolean;
}

interface ReviseAgentConfig {
  name?: string; // author name for tool/agent edits; default "Revise Agent"
  baseUrl?: string;
  token?: string;
  conversationId?: string;
  provider?: string;
  model?: string;
  turnStream?: ReviseAgentTurnStream;
  disableMetrics?: boolean;
  stagingWorker?: () => Worker;
}

interface ReviseAgentEvent {
  state: "idle" | "streaming" | "complete" | "error";
  activeTool: string | null;
  status: string | null;
  error?: string;
  messages: ReviseAgentMessage[];
}

interface ReviseAgentRunResult extends ReviseAgentEvent {
  actionCount: number;
}

interface ReviseFontDefinition {
  family: string;
  label?: string;
  cssFamily?: string;
  fontWeight?: number | string;
}
```
