Files
CatoandClaude Opus 5.5 ce98b0cc24 fix(ai): context extraction only on the user's own key; vendors by id (main green again) (#205)
* fix(ai): pick chain vendors by id, and make main green again

main has been red since #203 bumped @bitbaum/ai-kit to 1.10, which put
Gemini second in freeChain('BOTSMANN'). vendorChain() indexed [1] for
OpenRouter, so a BYOK OpenRouter user's key would have been sent down the
Gemini link. Look vendors up by id instead (code and both tests).

Also clears the rest of the red verify from the same bump: prettier 3.x
reformat of six files and the new next lint rule on relative
window.location.href assignments (sign-out keeps its full reload).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Cato <41178744+catomean@users.noreply.github.com>

* fix(ai): context extraction runs only on the user's own key

Every professional-chat turn fired a second, unasked model call
(extractAndSaveContext, fire-and-forget after the answer) on the shared
free-tier Groq key. Standing rule (2026-09-25): free-tier keys are spent
only when a person deliberately asks. Extraction now requires the user's
own key and uses their provider; without one it returns before any call.

Adds tests/__tests__/no-free-background-ai.test.ts: walks the import graph
from the timer route (/api/rebuild, appcron-botsmann-rebuild), /api/warmup
and /api/health and fails if any reaches a model client. Proven by
mutation: importing lib/nlp into /api/rebuild fails it, and dropping the
own-key guard fails the three new extractor cases.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Cato <41178744+catomean@users.noreply.github.com>

---------

Signed-off-by: Cato <41178744+catomean@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-25 00:44:17 -07:00

112 lines
2.9 KiB
TypeScript

/**
* Document types for the RAG system
*/
import { type DocumentStatusType } from '@/lib/constants';
/**
* Document categories that map to professionals
*/
export type DocumentCategory =
'legal' | 'health' | 'research' | 'language' | 'creative' | 'business' | 'general';
export const DOCUMENT_CATEGORIES: {
value: DocumentCategory;
label: string;
emoji: string;
description: string;
}[] = [
{ value: 'general', label: 'General', emoji: '📄', description: 'Uncategorized documents' },
{ value: 'legal', label: 'Legal', emoji: '⚖️', description: 'Contracts, agreements, legal docs' },
{ value: 'health', label: 'Health', emoji: '⚕️', description: 'Medical records, health info' },
{ value: 'research', label: 'Research', emoji: '🔬', description: 'Papers, studies, data' },
{ value: 'business', label: 'Business', emoji: '🔱', description: 'Plans, financials, strategy' },
{ value: 'creative', label: 'Creative', emoji: '🎨', description: 'Art, design, portfolios' },
{
value: 'language',
label: 'Language',
emoji: '🇨🇭',
description: 'Learning materials, translations',
},
];
/**
* Map professional slugs to document categories they can access
*/
export const PROFESSIONAL_DOCUMENT_ACCESS: Record<string, DocumentCategory[]> = {
legal: ['legal', 'general'],
health: ['health', 'general'],
research: ['research', 'general'],
business: ['business', 'general'],
creative: ['creative', 'general'],
language: ['language', 'general'],
};
export interface Document {
id: string;
user_id: string;
name: string;
type: string;
size_bytes: number;
storage_path: string;
status: DocumentStatusType;
domains: DocumentCategory[];
/** @deprecated Use `domains` instead. Kept for backward compatibility with existing code. */
category?: DocumentCategory;
error_message?: string;
chunk_count?: number;
created_at: string;
updated_at: string;
}
export interface DocumentChunk {
id: string;
document_id: string;
user_id: string;
content: string;
embedding: number[];
metadata: {
chunk_index: number;
start_char?: number;
end_char?: number;
};
created_at: string;
}
export interface DocumentUploadResponse {
success: boolean;
document?: Document;
error?: string;
}
export interface DocumentListResponse {
success: boolean;
documents?: Document[];
error?: string;
}
export interface DocumentSearchResult {
chunk_id: string;
document_id: string;
document_name: string;
content: string;
similarity: number;
}
export interface SearchResponse {
success: boolean;
results?: DocumentSearchResult[];
error?: string;
}
export type SupportedFileType = 'text/plain' | 'text/markdown' | 'application/pdf';
export const SUPPORTED_FILE_TYPES: SupportedFileType[] = [
'text/plain',
'text/markdown',
'application/pdf',
];
// Re-exported from SSOT for convenience
export { VALIDATION } from '@/lib/constants';