Skip to main content
The worker uses a service-oriented architecture with specialized services for different concerns.

Service Structure

R2 Storage Service

File: services/r2.ts Handles document storage in R2 buckets.

uploadToR2

Uploads content to R2 with metadata.
Parameters:
  • bucket: R2 bucket instance
  • key: Object key (path)
  • content: Content to upload
  • metadata: Custom metadata key-value pairs
Example:

Key Generation

R2 keys are generated using a utility function:
Format: users/{userId}/documents/{docId}-{filename}

Summarization Service

File: services/summarization.ts AI-powered text summarization using Workers AI.

summarizeGoals

Summarizes user goals using Workers AI.
Parameters:
  • ai: Workers AI binding
  • rawGoals: Raw user goals text
Model: @cf/meta/llama-3-8b-instruct Output: Summarized goals (max 200 characters)

Signup Handler

File: handlers/signup.ts Handles admin-only user creation.

handleSignup

Creates new users with goal summarization and optional file uploads.
Process:
  1. Validate admin authentication
  2. Extract form data (name, email, password, goals, files)
  3. Generate user ID (UUID)
  4. Hash password (SHA-256)
  5. Summarize goals using AI
  6. Upload files to R2 (if provided)
  7. Create user record in D1
  8. Return success response

Upload Handler

File: handlers/upload.ts Handles multi-file document uploads.

handleMultiFileUpload

Processes multiple file uploads with metadata.
Process:
  1. Parse multipart form data
  2. Validate files (.md, .pdf, .txt)
  3. Generate document IDs
  4. Upload to R2
  5. Trigger ingestion pipeline
  6. Return upload status

Utility Functions

UUID Generation

File: utils/uuid.ts Generates UUID v4 identifiers.

R2 Key Generation

File: utils/r2-keys.ts Generates standardized R2 keys.

Password Hashing

File: utils/password.ts SHA-256 password hashing.
Format: SHA256(username + password)

Database Schema

users Table

documents Table

graph_nodes Table

graph_edges Table

Indexes

Performance indexes on frequently queried columns:
  • idx_users_username - Username lookups
  • idx_documents_userId - User document queries
  • idx_documents_status - Status filtering
  • idx_graph_edges_source - Graph traversal
  • idx_graph_edges_target - Graph traversal

Caching Strategy

KV Cache

Graph data is cached in KV for 5 minutes:
Cache Invalidation:
  • Cache cleared on new document ingestion
  • Cache key: graph:latest

Error Handling

Services use consistent error handling: