{"version":3,"sources":["../src/types.ts"],"sourcesContent":["/**\n * @mdxdb/sqlite Types\n *\n * Type definitions for Cloudflare Durable Objects SQLite Storage.\n * Each Durable Object represents a namespace with its own SQLite database.\n * Uses Workers RPC for direct method calls on stubs.\n *\n * @packageDocumentation\n */\n\n// =============================================================================\n// Cloudflare Types\n// These types are provided globally by @cloudflare/workers-types\n// =============================================================================\n\n// NOTE: DurableObjectState, DurableObjectId, DurableObjectNamespace,\n// DurableObjectStub, SqlStorage, SqlStorageCursor are all available\n// globally when @cloudflare/workers-types is included in tsconfig.\n// We don't re-export them to avoid issues with cloudflare:workers module.\n\n// =============================================================================\n// MDXDatabase RPC Interface\n// =============================================================================\n\n/**\n * Thing entity (graph node)\n */\nexport interface Thing<TData = Record<string, unknown>> {\n  ns: string\n  type: string\n  id: string\n  url: string\n  data: TData\n  content?: string\n  createdAt: Date\n  updatedAt: Date\n  '@context'?: string | Record<string, unknown>\n}\n\n/**\n * Relationship (graph edge)\n */\nexport interface Relationship<TData = Record<string, unknown>> {\n  id: string\n  type: string\n  from: string\n  to: string\n  data?: TData\n  createdAt: Date\n}\n\n/**\n * Event (immutable log entry)\n */\nexport interface Event<TData = Record<string, unknown>> {\n  id: string\n  type: string\n  timestamp: Date\n  source: string\n  data: TData\n  correlationId?: string\n  causationId?: string\n}\n\n/**\n * Action status\n */\nexport type ActionStatus = 'pending' | 'active' | 'completed' | 'failed' | 'cancelled'\n\n/**\n * Action (pending/active work)\n */\nexport interface Action<TData = Record<string, unknown>> {\n  id: string\n  actor: string\n  object: string\n  action: string\n  status: ActionStatus\n  createdAt: Date\n  updatedAt: Date\n  startedAt?: Date\n  completedAt?: Date\n  result?: unknown\n  error?: string\n  metadata?: TData\n}\n\n/**\n * Artifact type\n */\nexport type ArtifactType = 'ast' | 'types' | 'esm' | 'cjs' | 'worker' | 'html' | 'markdown' | 'bundle' | 'sourcemap' | string\n\n/**\n * Artifact (cached compiled content)\n */\nexport interface Artifact<TContent = unknown> {\n  key: string\n  type: ArtifactType\n  source: string\n  sourceHash: string\n  createdAt: Date\n  expiresAt?: Date\n  content: TContent\n  size?: number\n  metadata?: Record<string, unknown>\n}\n\n/**\n * Query options for listing things\n */\nexport interface QueryOptions {\n  ns?: string\n  type?: string\n  where?: Record<string, unknown>\n  orderBy?: string\n  order?: 'asc' | 'desc'\n  limit?: number\n  offset?: number\n}\n\n/**\n * Search options\n */\nexport interface SearchOptions {\n  query: string\n  type?: string\n  limit?: number\n  offset?: number\n  minScore?: number\n}\n\n/**\n * Create options\n */\nexport interface CreateOptions<TData = Record<string, unknown>> {\n  ns: string\n  type: string\n  id?: string\n  url?: string\n  data: TData\n  content?: string\n  '@context'?: string | Record<string, unknown>\n}\n\n/**\n * Update options\n */\nexport interface UpdateOptions<TData = Record<string, unknown>> {\n  data: Partial<TData>\n  content?: string\n}\n\n/**\n * Relate options\n */\nexport interface RelateOptions<TData = Record<string, unknown>> {\n  type: string\n  from: string\n  to: string\n  data?: TData\n}\n\n/**\n * Event create options\n */\nexport interface CreateEventOptions<TData = Record<string, unknown>> {\n  type: string\n  source: string\n  data: TData\n  correlationId?: string\n  causationId?: string\n}\n\n/**\n * Action create options\n */\nexport interface CreateActionOptions<TData = Record<string, unknown>> {\n  actor: string\n  object: string\n  action: string\n  status?: ActionStatus\n  metadata?: TData\n}\n\n/**\n * Artifact store options\n */\nexport interface StoreArtifactOptions<TContent = unknown> {\n  key: string\n  type: ArtifactType\n  source: string\n  sourceHash: string\n  content: TContent\n  ttl?: number\n  metadata?: Record<string, unknown>\n}\n\n/**\n * Event query options\n */\nexport interface EventQueryOptions {\n  type?: string\n  source?: string\n  correlationId?: string\n  after?: Date\n  before?: Date\n  limit?: number\n  offset?: number\n}\n\n/**\n * Action query options\n */\nexport interface ActionQueryOptions {\n  actor?: string\n  object?: string\n  action?: string\n  status?: ActionStatus | ActionStatus[]\n  limit?: number\n  offset?: number\n}\n\n/**\n * Vector search options\n *\n * For local SQLite search, provide `embedding` directly.\n * For Cloudflare Vectorize, provide `query` and the embedding will be generated.\n */\nexport interface VectorSearchOptions {\n  /** Text query (requires external embedding service) */\n  query?: string\n  /** Pre-computed embedding vector */\n  embedding?: number[]\n  /** Maximum results to return */\n  limit?: number\n  /** Minimum similarity score (0-1) */\n  minScore?: number\n  /** Filter by thing type */\n  type?: string\n  /** Filter to specific thing URLs */\n  thingUrls?: string[]\n}\n\n/**\n * Vector search result\n */\nexport interface VectorSearchResult {\n  content: string\n  score: number\n  thingUrl: string\n  chunkIndex: number\n  metadata?: Record<string, unknown>\n}\n\n/**\n * The MDXDatabase RPC interface - methods callable on the Durable Object stub\n */\nexport interface MDXDatabaseRPC {\n  // Thing operations\n  list(options?: QueryOptions): Promise<Thing[]>\n  // Note: Using 'read' instead of 'get' to avoid conflict with DurableObjectNamespace.get()\n  read(url: string): Promise<Thing | null>\n  readById(type: string, id: string): Promise<Thing | null>\n  create(options: CreateOptions): Promise<Thing>\n  update(url: string, options: UpdateOptions): Promise<Thing>\n  upsert(options: CreateOptions): Promise<Thing>\n  // Note: Using 'remove' instead of 'delete' to avoid conflict with reserved names\n  remove(url: string): Promise<boolean>\n  search(options: SearchOptions): Promise<Thing[]>\n\n  // Relationship operations\n  relate(options: RelateOptions): Promise<Relationship>\n  unrelate(from: string, type: string, to: string): Promise<boolean>\n  related(url: string, type?: string, direction?: 'from' | 'to' | 'both'): Promise<Thing[]>\n  relationships(url: string, type?: string, direction?: 'from' | 'to' | 'both'): Promise<Relationship[]>\n\n  // Vector search (uses Cloudflare Vectorize via RPC)\n  vectorSearch(options: VectorSearchOptions): Promise<VectorSearchResult[]>\n  setEmbedding(thingUrl: string, chunkIndex: number, embedding: number[]): Promise<void>\n  upsertEmbeddings(thingUrl: string, embeddings: Array<{ chunkIndex: number; embedding: number[] }>): Promise<void>\n  deleteVectors(thingUrl: string): Promise<void>\n\n  // Event operations\n  track(options: CreateEventOptions): Promise<Event>\n  getEvent(id: string): Promise<Event | null>\n  queryEvents(options?: EventQueryOptions): Promise<Event[]>\n\n  // Action operations\n  send(options: CreateActionOptions): Promise<Action>\n  do(options: CreateActionOptions): Promise<Action>\n  getAction(id: string): Promise<Action | null>\n  queryActions(options?: ActionQueryOptions): Promise<Action[]>\n  startAction(id: string): Promise<Action>\n  completeAction(id: string, result?: unknown): Promise<Action>\n  failAction(id: string, error: string): Promise<Action>\n  cancelAction(id: string): Promise<Action>\n\n  // Artifact operations\n  storeArtifact(options: StoreArtifactOptions): Promise<Artifact>\n  getArtifact(key: string): Promise<Artifact | null>\n  getArtifactBySource(source: string, type: ArtifactType): Promise<Artifact | null>\n  deleteArtifact(key: string): Promise<boolean>\n  cleanExpiredArtifacts(): Promise<number>\n\n  // Database info\n  getDatabaseSize(): number\n  getNamespace(): string\n}\n\n// =============================================================================\n// Vectorize RPC Types (for integration with @mdxdb/vectorize)\n// =============================================================================\n\n/**\n * Vectorize RPC interface - methods callable on the Vectorize worker\n * This matches the VectorizeDatabaseRPC from @mdxdb/vectorize\n */\nexport interface VectorizeRPC {\n  /**\n   * Upsert vectors for a thing's chunks\n   */\n  upsert(vectors: VectorizeUpsertOptions[]): Promise<{ count: number }>\n\n  /**\n   * Search for similar vectors\n   */\n  search(options: VectorizeSearchOptions): Promise<VectorSearchResult[]>\n\n  /**\n   * Delete vectors for things\n   */\n  delete(options: { thingUrls: string[] }): Promise<{ count: number }>\n\n  /**\n   * Get vectors by thing URL\n   */\n  getByThingUrl(thingUrl: string): Promise<VectorSearchResult[]>\n\n  /**\n   * Get index info\n   */\n  describe(): Promise<VectorizeIndexDetails>\n\n  /**\n   * Get namespace\n   */\n  getNamespace(): string\n\n  /**\n   * Create instance with specific namespace\n   */\n  withNamespace(namespace: string): VectorizeRPC\n}\n\n/**\n * Vectorize upsert options\n */\nexport interface VectorizeUpsertOptions {\n  thingUrl: string\n  chunkIndex: number\n  embedding: number[]\n  content: string\n  type?: string\n  metadata?: Record<string, string | number | boolean>\n}\n\n/**\n * Vectorize search options (subset of VectorSearchOptions for RPC)\n */\nexport interface VectorizeSearchOptions {\n  embedding: number[]\n  topK?: number\n  type?: string\n  thingUrls?: string[]\n  minScore?: number\n  namespace?: string\n}\n\n/**\n * Vectorize index details\n */\nexport interface VectorizeIndexDetails {\n  name: string\n  dimensions: number\n  metric: 'cosine' | 'euclidean' | 'dot-product'\n  vectorCount: number\n  config: {\n    dimensions: number\n    metric: 'cosine' | 'euclidean' | 'dot-product'\n  }\n}\n\n// =============================================================================\n// Environment Bindings\n// =============================================================================\n\n/**\n * Environment with MDXDatabase Durable Object binding\n */\nexport interface Env {\n  /** MDXDatabase Durable Object namespace */\n  MDXDB: DurableObjectNamespace<MDXDatabaseRPC>\n\n  /**\n   * Optional Vectorize RPC binding for vector search\n   * This is a service binding to a Vectorize worker deployed via Workers for Platforms\n   * Methods can be called directly via Workers RPC\n   */\n  VECTORIZE?: VectorizeRPC\n}\n\n// =============================================================================\n// Database Configuration\n// =============================================================================\n\n/**\n * Configuration for MDXDatabase client\n */\nexport interface MDXClientConfig {\n  /**\n   * Namespace for the database (maps to Durable Object name)\n   * e.g., 'example.com', 'myapp.dev'\n   */\n  namespace: string\n\n  /**\n   * For Workers: The Durable Object namespace binding\n   */\n  binding?: DurableObjectNamespace<MDXDatabaseRPC>\n\n  /**\n   * Vectorize RPC binding for vector search\n   * When provided, vector operations are delegated to Cloudflare Vectorize\n   */\n  vectorize?: VectorizeRPC\n\n  /**\n   * Embedding function for vector search (client-side)\n   * Used to generate embeddings before sending to Vectorize\n   */\n  embedFn?: (text: string) => Promise<number[]>\n\n  /**\n   * For Node.js: Use miniflare\n   */\n  miniflare?: boolean\n\n  /**\n   * Miniflare persistence path (default: '.mf' in cwd)\n   */\n  persistPath?: string\n}\n\n// =============================================================================\n// Row Types (internal)\n// =============================================================================\n\n/**\n * Thing row in SQLite (graph node)\n */\nexport interface ThingRow {\n  url: string\n  ns: string\n  type: string\n  id: string\n  context: string | null\n  data: string\n  content: string\n  created_at: string\n  updated_at: string\n  deleted_at: string | null\n  version: number\n}\n\n/**\n * Relationship row (graph edge)\n */\nexport interface RelationshipRow {\n  id: string\n  type: string\n  from_url: string\n  to_url: string\n  data: string | null\n  created_at: string\n}\n\n/**\n * Search chunk row\n */\nexport interface SearchRow {\n  id: string\n  thing_url: string\n  chunk_index: number\n  content: string\n  embedding: string | null\n  metadata: string | null\n  created_at: string\n}\n\n/**\n * Event row\n */\nexport interface EventRow {\n  id: string\n  type: string\n  timestamp: string\n  source: string\n  data: string\n  correlation_id: string | null\n  causation_id: string | null\n}\n\n/**\n * Action row\n */\nexport interface ActionRow {\n  id: string\n  actor: string\n  object: string\n  action: string\n  status: ActionStatus\n  created_at: string\n  updated_at: string\n  started_at: string | null\n  completed_at: string | null\n  result: string | null\n  error: string | null\n  metadata: string | null\n}\n\n/**\n * Artifact row\n */\nexport interface ArtifactRow {\n  key: string\n  type: string\n  source: string\n  source_hash: string\n  created_at: string\n  expires_at: string | null\n  content: string\n  size: number | null\n  metadata: string | null\n}\n\n/**\n * Content chunk for indexing\n */\nexport interface Chunk {\n  content: string\n  index: number\n  start: number\n  end: number\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]}