Skip to content

Harness engineering beginner tutorial, from 0 to 1

3.9 KB
Raw Blame
import * as fs from 'fs';
import * as path from 'path';
import { v4 as uuidv4 } from 'uuid';
import { Document, DocumentMetadata } from '../shared/types';
import { PersistenceService } from './persistence-service';

const DOCUMENTS_META = 'documents-meta.json';

export class DocumentService {
  private persistence: PersistenceService;

  constructor(persistence: PersistenceService) {
    this.persistence = persistence;
  }

  /** List all imported documents. */
  listDocuments(): Document[] {
    const docs = this.persistence.readJson<Document[]>(DOCUMENTS_META);
    return docs ?? [];
  }

  /** Import a file from the given path. Extracts metadata on import. */
  importDocument(filePath: string): Document {
    if (!fs.existsSync(filePath)) {
      throw new Error(`File not found: ${filePath}`);
    }

    const filename = path.basename(filePath);
    const content = fs.readFileSync(filePath, 'utf-8');
    const stats = fs.statSync(filePath);

    // Extract metadata from content
    const metadata = this.extractMetadata(content, filename);

    const doc: Document = {
      id: uuidv4(),
      title: filename.replace(/\.[^.]+$/, ''),
      filename,
      importedAt: new Date().toISOString(),
      size: stats.size,
      status: 'imported',
      metadata,
    };

    // Copy file to data directory
    this.persistence.copyFileToDocuments(filePath, filename);

    // Store content for indexing and viewing
    this.persistence.writeText(`content/${doc.id}.txt`, content);

    // Update metadata
    const docs = this.listDocuments();
    docs.push(doc);
    this.persistence.writeJson(DOCUMENTS_META, docs);

    return doc;
  }

  /** Get a single document by ID. */
  getDocument(id: string): Document | null {
    const docs = this.listDocuments();
    return docs.find(d => d.id === id) ?? null;
  }

  /** Get the text content of a document. */
  getDocumentContent(id: string): string | null {
    return this.persistence.readText(`content/${id}.txt`);
  }

  /** Update a document's metadata. */
  updateDocument(id: string, updates: Partial<Document>): Document | null {
    const docs = this.listDocuments();
    const index = docs.findIndex(d => d.id === id);
    if (index === -1) return null;

    docs[index] = { ...docs[index], ...updates };
    this.persistence.writeJson(DOCUMENTS_META, docs);
    return docs[index];
  }

  /** Delete a document by ID. Removes content, chunks, and metadata. */
  deleteDocument(id: string): boolean {
    const docs = this.listDocuments();
    const doc = docs.find(d => d.id === id);
    if (!doc) return false;

    // Remove file from documents directory
    this.persistence.deleteFromDocuments(doc.filename);

    // Remove stored content
    const contentPath = path.join(this.persistence.getDataDir(), 'content', `${id}.txt`);
    if (fs.existsSync(contentPath)) {
      fs.unlinkSync(contentPath);
    }

    // Remove chunks if they exist
    const chunksPath = path.join(this.persistence.getDataDir(), 'chunks', `${id}.json`);
    if (fs.existsSync(chunksPath)) {
      fs.unlinkSync(chunksPath);
    }

    // Update metadata
    const updated = docs.filter(d => d.id !== id);
    this.persistence.writeJson(DOCUMENTS_META, updated);
    return true;
  }

  /** Check whether the persistence layer has stored data. */
  hasPersistedData(): boolean {
    return this.persistence.exists(DOCUMENTS_META);
  }

  /** Extract metadata from document content. */
  extractMetadata(content: string, filename: string): DocumentMetadata {
    const lines = content.split('\n');
    const words = content.split(/\s+/).filter(w => w.length > 0);
    const paragraphs = content.split(/\n\s*\n/).filter(p => p.trim().length > 0);
    const ext = path.extname(filename).toLowerCase();

    return {
      wordCount: words.length,
      lineCount: lines.length,
      fileType: ext === '.md' ? 'markdown' : 'plaintext',
      paragraphCount: paragraphs.length,
      charCount: content.length,
    };
  }
}