learning_ai_notes/backend/src/lib/llm.ts
saravanakumardb1 9e3a7206b9 feat(backend): add note-prompts module with Smart Actions LLM integration
- Add @bytelyst/llm dependency (file: ref) + llm.ts singleton wrapper
- Add LLM env vars to config (LLM_PROVIDER, LLM_DEFAULT_MODEL, LLM_VISION_MODEL, LLM_EMBEDDING_MODEL)
- Create note-prompts module: types, repository, runner, routes, seed (20 built-in templates)
- Built-in templates: 8 transform, 3 extract, 3 generate, 2 analyze, 2 vision, 2 export
- Prompt runner supports text, image, and text+image inputs via @bytelyst/llm vision
- Upgrade copilot-transform.ts to use @bytelyst/llm directly (with local heuristic fallback)
- Add reading-time endpoint (GET /api/notes/:id/reading-time)
- Extend agent-action types with smart_action and auto_enrich
- Add note_prompts Cosmos container to cosmos-init
- Register notePromptRoutes in server.ts
- 15 new tests (CRUD, run, slug resolution, seed validation, reading-time)
2026-04-06 08:01:42 -07:00

37 lines
910 B
TypeScript

/**
* LLM singleton for NoteLett backend.
*
* Wraps @bytelyst/llm with lazy initialization.
* Provider is auto-detected from env vars (LLM_PROVIDER, OPENAI_API_KEY, etc.).
*/
import { getLLM, createLLMProvider, setLLM } from '@bytelyst/llm';
import type { LLMProvider } from '@bytelyst/llm';
let initialized = false;
/**
* Initialize the LLM provider singleton.
* Safe to call multiple times — only initializes once.
*/
export function initLLM(): LLMProvider {
if (!initialized) {
const providerType = (process.env.LLM_PROVIDER || 'mock') as 'azure' | 'openai' | 'mock';
const provider = createLLMProvider(providerType);
setLLM(provider);
initialized = true;
}
return getLLM();
}
/**
* Get the initialized LLM provider.
* Calls initLLM() if not yet initialized.
*/
export function llm(): LLMProvider {
if (!initialized) {
return initLLM();
}
return getLLM();
}