Spaces:
Sleeping

File size: 2,881 Bytes
7f81110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { eq } from "drizzle-orm";
import { IKnowledgeRepository } from "../../domain/repositories/IKnowledgeRepository";
import { embeddings } from "../../../../libs/shared/infrastructure/postgres/schema/embeddings";
import { KnowledgeDocument, KnowledgeMetadata } from "../../domain/entities/KnowledgeDocument";

export class PgKnowledgeRepository implements IKnowledgeRepository {
    constructor(private readonly db: any) { }

    async upsert(input: {
        tenantId: string;
        documentId: string;
        title: string;
        text: string;
        language: string;
        sourceType: string;
        metadata: KnowledgeMetadata;
        productAvailability: boolean;
        externalId: string;
        customerId: string | null;
    }): Promise<{ documentId: string }> {
        return { documentId: input.documentId };
    }

    async archive(input: {
        tenantId: string;
        documentId: string;
    }): Promise<void> {
        await this.db.delete(embeddings).where(eq(embeddings.documentId, input.documentId));
    }

    async updateEnrichment(input: {
        tenantId: string;
        documentId: string;
        visualDescriptors: string[];
        imageAnalysisSignature: string;
        imageAnalysisUpdatedAt: Date;
    }): Promise<void> {
        // Implementation for enrichment update in Postgres
    }

    async findById(input: {
        tenantId: string;
        documentId: string;
    }): Promise<KnowledgeDocument | null> {
        const [result] = await this.db
            .select()
            .from(embeddings)
            .where(eq(embeddings.documentId, input.documentId))
            .limit(1);

        if (!result) return null;

        return {
            id: result.documentId,
            tenantId: input.tenantId,
            documentId: result.documentId,
            title: "",
            text: result.text,
            language: "en",
            sourceType: "shopify",
            metadata: result.metadata ? JSON.parse(result.metadata) : {},
            createdAt: result.createdAt,
            updatedAt: result.createdAt,
        } as any;
    }

    async saveEmbedding(input: {
        tenantId: string;
        documentId: string;
        vector: number[];
        dimension: number;
        model: string;
    }): Promise<void> {
        const storeId = input.tenantId.split(":")[1];

        await this.db.insert(embeddings).values({
            storeId,
            documentId: input.documentId,
            chunkId: "root",
            text: "",
            embedding: input.vector,
            model: input.model,
            metadata: JSON.stringify({}),
        }).onConflictDoUpdate({
            target: [embeddings.id], // or documentId/chunkId if unique index exists
            set: {
                embedding: input.vector,
                model: input.model,
            }
        });
    }
}