wulf-pulse/lib/services/llm/client.ts
lorentz 8f8b5ab7be feat: AI ticket analyzer (phases 1-6)
Multi-stage LLM pipeline that produces structured analyses of Autotask
tickets from local Postgres. Migration 069 + Zod schemas, Stage 0
preprocessor, IT Glue redaction + search, Anthropic SDK wrapper, Stages
1/3/4 (Haiku/Sonnet/Opus), pipeline + cost circuit breaker, job worker
(opt-in autostart), 6 API routes, 3 frontend pages, share-row
persistence (email send deferred to phase 7). 128 vitest tests, tsc
clean. Build journal in docs/wulf-pulse-ticket-analyzer-build-notes.md.

Sync: adds syncTicketNotes() + ticket_notes to ordered/date-filtered
entities so the analyzer's local mirror stays current via scheduler.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-29 10:59:40 -04:00

32 lines
918 B
TypeScript

/**
* Anthropic SDK singleton.
*
* Lazy: the client is constructed on first access so importing this module is
* cheap and doesn't require ANTHROPIC_API_KEY to be set at boot. Throws on
* first use if the env var is missing.
*/
import Anthropic from '@anthropic-ai/sdk';
let instance: Anthropic | null = null;
export function getAnthropicClient(): Anthropic {
if (instance) return instance;
const apiKey = process.env.ANTHROPIC_API_KEY;
if (!apiKey) {
throw new Error(
'ANTHROPIC_API_KEY is not set. The AI Ticket Analyzer cannot run without it.'
);
}
instance = new Anthropic({ apiKey });
return instance;
}
export function isAnthropicConfigured(): boolean {
return !!process.env.ANTHROPIC_API_KEY;
}
/** Test-only: replace the singleton (e.g. with a vitest-mocked client). */
export function _setAnthropicClientForTests(client: Anthropic | null): void {
instance = client;
}