import { generateChunkId, generateDocumentId } from '../utils/id.js';
import { DEFAULT_CHUNKING_CONFIG, TextChunker } from './chunking.js';
export class DefaultIngestionPipeline {
    vectorStore;
    embeddingProvider;
    chunker;
    chunkingConfig;
    constructor(vectorStore, embeddingProvider, chunkingConfig) {
        this.vectorStore = vectorStore;
        this.embeddingProvider = embeddingProvider;
        this.chunker = new TextChunker();
        this.chunkingConfig = { ...DEFAULT_CHUNKING_CONFIG, ...chunkingConfig };
    }
    async ingest(content, metadata, scope, knowledgeBaseId) {
        const startTime = Date.now();
        const documentId = generateDocumentId();
        const chunkContents = this.chunker.chunk(content, this.chunkingConfig);
        if (chunkContents.length === 0) {
            return { documentId, chunkCount: 0, totalTokens: 0, durationMs: Date.now() - startTime };
        }
        const texts = chunkContents.map((c) => c.content);
        const embeddings = await this.embeddingProvider.embed(texts);
        const now = Date.now();
        const chunks = chunkContents.map((cc, i) => ({
            id: generateChunkId(),
            documentId,
            knowledgeBaseId,
            tenantId: scope.tenantId,
            content: cc.content,
            index: cc.index,
            tokenCount: estimateTokens(cc.content),
            embedding: embeddings[i],
            metadata: { ...metadata, chunkIndex: cc.index },
            createdAt: now,
        }));
        await this.vectorStore.upsert(chunks);
        const totalTokens = chunks.reduce((sum, c) => sum + c.tokenCount, 0);
        return {
            documentId,
            chunkCount: chunks.length,
            totalTokens,
            durationMs: Date.now() - startTime,
        };
    }
    async remove(documentId) {
        await this.vectorStore.deleteByDocument(documentId);
    }
}
function estimateTokens(text) {
    return Math.ceil(text.length / 4);
}
//# sourceMappingURL=ingestion.js.map