CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
Blame · Line-by-line history
ai-client.ts
Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.
| 3ef4c9d | 1 | /** |
| 2 | * Shared Anthropic client + helpers for all AI-powered features. | |
| 3 | * Centralised here so model choice, caching, and key handling live in one place. | |
| 4 | */ | |
| 5 | ||
| 6 | import Anthropic from "@anthropic-ai/sdk"; | |
| 7 | import { config } from "./config"; | |
| 8 | ||
| 9 | let _client: Anthropic | null = null; | |
| 10 | ||
| 11 | export function getAnthropic(): Anthropic { | |
| 12 | if (!_client) { | |
| 13 | if (!config.anthropicApiKey) { | |
| 14 | throw new Error("ANTHROPIC_API_KEY is not set"); | |
| 15 | } | |
| 16 | _client = new Anthropic({ apiKey: config.anthropicApiKey }); | |
| 17 | } | |
| 18 | return _client; | |
| 19 | } | |
| 20 | ||
| 21 | export function isAiAvailable(): boolean { | |
| 22 | return !!config.anthropicApiKey; | |
| 23 | } | |
| 24 | ||
| 25 | /** Default model for code understanding + review */ | |
| 26 | export const MODEL_SONNET = "claude-sonnet-4-20250514"; | |
| 27 | /** Fast model for lightweight tasks (commit messages, titles) */ | |
| 28 | export const MODEL_HAIKU = "claude-haiku-4-5-20251001"; | |
| 29 | ||
| 30 | /** | |
| 31 | * Extract text content from an Anthropic message response. | |
| 32 | */ | |
| 33 | export function extractText( | |
| 34 | message: Anthropic.Messages.Message | |
| 35 | ): string { | |
| 36 | for (const block of message.content) { | |
| 37 | if (block.type === "text") return block.text; | |
| 38 | } | |
| 39 | return ""; | |
| 40 | } | |
| 41 | ||
| 42 | /** | |
| 43 | * Safely parse JSON from a Claude response that may include surrounding prose | |
| 44 | * or a ```json code block. | |
| 45 | */ | |
| 46 | export function parseJsonResponse<T = unknown>(text: string): T | null { | |
| 47 | // Prefer ```json blocks | |
| 48 | const fenced = text.match(/```(?:json)?\s*(\{[\s\S]*?\}|\[[\s\S]*?\])\s*```/); | |
| 49 | const candidate = fenced ? fenced[1] : null; | |
| 50 | if (candidate) { | |
| 51 | try { | |
| 52 | return JSON.parse(candidate) as T; | |
| 53 | } catch { | |
| 54 | // fall through | |
| 55 | } | |
| 56 | } | |
| 57 | // Fall back to first top-level {...} or [...] | |
| 58 | const braceMatch = text.match(/\{[\s\S]*\}/); | |
| 59 | if (braceMatch) { | |
| 60 | try { | |
| 61 | return JSON.parse(braceMatch[0]) as T; | |
| 62 | } catch { | |
| 63 | // fall through | |
| 64 | } | |
| 65 | } | |
| 66 | const bracketMatch = text.match(/\[[\s\S]*\]/); | |
| 67 | if (bracketMatch) { | |
| 68 | try { | |
| 69 | return JSON.parse(bracketMatch[0]) as T; | |
| 70 | } catch { | |
| 71 | return null; | |
| 72 | } | |
| 73 | } | |
| 74 | return null; | |
| 75 | } |