Files
g-butandClaude Opus 4.6 b32b30c4cb feat(context): add user context accumulation system
Core differentiator: professionals learn about users over time.

- Add user_context table with vector embeddings (migration 011)
- Add get_relevant_context() SQL function for semantic search
- Create lib/context/ module:
  - store.ts: save and retrieve user context entries
  - extractor.ts: LLM-based fact extraction from conversations
  - domain-detector.ts: keyword-based domain classification
- Integrate into professional-chat route:
  - Retrieve relevant user context and inject into system prompt
  - Async extraction of facts after each conversation turn

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-02 17:23:15 +01:00

94 lines
2.2 KiB
TypeScript

/**
* User Context Store
*
* CRUD operations for the user_context table.
* Stores accumulated knowledge extracted from conversations and documents.
*/
import { getServiceClient } from '@/lib/supabase';
import { generateEmbedding } from '@/lib/embeddings';
export interface UserContextEntry {
id: string;
content: string;
domains: string[];
source_type: 'conversation' | 'document' | 'manual';
source_id?: string;
confidence: number;
similarity?: number;
}
/**
* Save extracted context facts for a user.
* Generates embeddings and stores them for later semantic search.
*/
export async function saveUserContext(
userId: string,
facts: Array<{
content: string;
domains: string[];
sourceType: 'conversation' | 'document' | 'manual';
sourceId?: string;
confidence?: number;
}>,
): Promise<number> {
if (facts.length === 0) return 0;
const supabase = getServiceClient();
let saved = 0;
for (const fact of facts) {
try {
const embedding = await generateEmbedding(fact.content);
const { error } = await supabase.from('user_context').insert({
user_id: userId,
content: fact.content,
domains: fact.domains,
source_type: fact.sourceType,
source_id: fact.sourceId,
confidence: fact.confidence ?? 0.8,
embedding,
});
if (!error) saved++;
} catch {
// Skip facts that fail to save — non-critical
}
}
return saved;
}
/**
* Find relevant context for a user based on semantic similarity.
* Used to inject personalized context into professional chat prompts.
*/
export async function getRelevantContext(
userId: string,
domains: string[],
query: string,
maxResults: number = 10,
matchThreshold: number = 0.3,
): Promise<UserContextEntry[]> {
const supabase = getServiceClient();
try {
const embedding = await generateEmbedding(query);
const { data, error } = await supabase.rpc('get_relevant_context', {
p_user_id: userId,
p_domains: domains,
p_embedding: embedding,
p_match_threshold: matchThreshold,
p_max_results: maxResults,
});
if (error || !data) return [];
return data as UserContextEntry[];
} catch {
return [];
}
}