File size: 1,750 Bytes
ff0e173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { NextResponse } from 'next/server';
import { readStore, addQA, toPublicQA, type QARecord } from '@/lib/kb-store';

export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';

function qaEmbeddingText(question: string, answer: string): string {
  return `Q: ${question}\nA: ${answer}`;
}

// GET — public list of Q&A pairs (no embeddings).
export async function GET() {
  try {
    const store = await readStore();
    return NextResponse.json({ qa: store.qa.map(toPublicQA) });
  } catch (err) {
    const message = err instanceof Error ? err.message : 'Failed to load Q&A pairs.';
    return NextResponse.json({ error: message, qa: [] }, { status: 500 });
  }
}

// POST — create a Q&A pair, embed it for retrieval.
export async function POST(request: Request) {
  let body: {
    question?: string;
    answer?: string;
    category?: string;
    prioritize?: boolean;
  };
  try {
    body = await request.json();
  } catch {
    return NextResponse.json({ error: 'Invalid JSON body.' }, { status: 400 });
  }

  const question = body.question?.trim();
  const answer = body.answer?.trim();
  if (!question || !answer) {
    return NextResponse.json(
      { error: 'Both question and answer are required.' },
      { status: 400 }
    );
  }

  const { embedDocuments } = await import('@/lib/cohere');
  const [embedding] = await embedDocuments([qaEmbeddingText(question, answer)]);

  const record: QARecord = {
    id: `qa-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
    question,
    answer,
    category: body.category?.trim() || 'General',
    prioritize: Boolean(body.prioritize),
    embedding: embedding ?? [],
  };
  await addQA(record);

  return NextResponse.json({ qa: toPublicQA(record) });
}