File size: 2,394 Bytes
34367da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 { prisma } from '../../database/prisma.js';
import { RawDocumentInput, StructuredFactInput } from '@widget-tdc/mcp-types';

export class SragRepository {
  async ingestDocument(input: RawDocumentInput): Promise<number> {
    const doc = await prisma.rawDocument.create({
      data: {
        orgId: input.orgId,
        sourceType: input.sourceType,
        sourcePath: input.sourcePath,
        content: input.content,
      },
    });

    return doc.id;
  }

  async ingestFact(input: StructuredFactInput): Promise<number> {
    const fact = await prisma.structuredFact.create({
      data: {
        orgId: input.orgId,
        docId: input.docId || null,
        factType: input.factType,
        jsonPayload: input.jsonPayload,
        occurredAt: input.occurredAt ? new Date(input.occurredAt) : null,
      },
    });

    return fact.id;
  }

  async queryFacts(orgId: string, factType?: string, limit: number = 50): Promise<any[]> {
    const where: any = { orgId };

    if (factType) {
      where.factType = factType;
    }

    const facts = await prisma.structuredFact.findMany({
      where,
      orderBy: { createdAt: 'desc' },
      take: limit,
    });

    return facts.map(f => ({
      id: f.id,
      org_id: f.orgId,
      doc_id: f.docId,
      fact_type: f.factType,
      json_payload: f.jsonPayload,
      occurred_at: f.occurredAt,
      created_at: f.createdAt,
    }));
  }

  async searchDocuments(orgId: string, keyword: string, limit: number = 10): Promise<any[]> {
    const docs = await prisma.rawDocument.findMany({
      where: {
        orgId,
        content: { contains: keyword, mode: 'insensitive' },
      },
      orderBy: { createdAt: 'desc' },
      take: limit,
    });

    return docs.map(d => ({
      id: d.id,
      org_id: d.orgId,
      source_type: d.sourceType,
      source_path: d.sourcePath,
      content: d.content,
      created_at: d.createdAt,
    }));
  }

  async getDocumentById(id: number): Promise<any | null> {
    const doc = await prisma.rawDocument.findUnique({
      where: { id },
    });

    if (!doc) return null;

    return {
      id: doc.id,
      org_id: doc.orgId,
      source_type: doc.sourceType,
      source_path: doc.sourcePath,
      content: doc.content,
      created_at: doc.createdAt,
    };
  }
}