diff --git a/app/api/app-idea-chat/route.ts b/app/api/app-idea-chat/route.ts index 5e2c64d..d9b28da 100644 --- a/app/api/app-idea-chat/route.ts +++ b/app/api/app-idea-chat/route.ts @@ -10,7 +10,16 @@ import { getAnthropicModel } from '@/lib/anthropic-model' import { getCurrentUser } from '@/lib/auth' import { deductCredits, CREDITS } from '@/lib/credits' -const anthropic = new Anthropic() +let __anthropicClient: Anthropic | null = null +function getAnthropic(): Anthropic { + if (__anthropicClient) return __anthropicClient + const key = process.env.ANTHROPIC_API_KEY + if (!key) { + throw new Error('ANTHROPIC_API_KEY is not configured') + } + __anthropicClient = new Anthropic({ apiKey: key }) + return __anthropicClient +} export interface AppIdeaSuggestion { name: string @@ -141,7 +150,7 @@ Always respond with valid JSON only (no markdown fences): { role: 'user', content: message }, ] - const response = await anthropic.messages.create({ + const response = await getAnthropic().messages.create({ model: getAnthropicModel(), max_tokens: 2048, system: systemPrompt, diff --git a/app/api/build-app/route.ts b/app/api/build-app/route.ts index 6757ef0..0be2987 100644 --- a/app/api/build-app/route.ts +++ b/app/api/build-app/route.ts @@ -4,7 +4,16 @@ import { getCurrentUser } from '@/lib/auth' import { getAnthropicModel } from '@/lib/anthropic-model' import type { AppBlueprint } from '@/lib/queries' -const anthropic = new Anthropic() +let __anthropicClient: Anthropic | null = null +function getAnthropic(): Anthropic { + if (__anthropicClient) return __anthropicClient + const key = process.env.ANTHROPIC_API_KEY + if (!key) { + throw new Error('ANTHROPIC_API_KEY is not configured') + } + __anthropicClient = new Anthropic({ apiKey: key }) + return __anthropicClient +} type Platform = 'github' | 'gitlab' @@ -59,7 +68,7 @@ Rules: Return format: {"path/to/file.ts": "...full content...", "README.md": "..."} ` - const response = await anthropic.messages.create({ + const response = await getAnthropic().messages.create({ model: getAnthropicModel(), max_tokens: 8192, messages: [{ role: 'user', content: prompt }], diff --git a/app/api/pattern-analyzer/route.ts b/app/api/pattern-analyzer/route.ts index 0b948ae..5100fce 100644 --- a/app/api/pattern-analyzer/route.ts +++ b/app/api/pattern-analyzer/route.ts @@ -10,7 +10,16 @@ import { getAnthropicModel } from '@/lib/anthropic-model' import { getCurrentUser } from '@/lib/auth' import { deductCredits, CREDITS } from '@/lib/credits' -const anthropic = new Anthropic() +let __anthropicClient: Anthropic | null = null +function getAnthropic(): Anthropic { + if (__anthropicClient) return __anthropicClient + const key = process.env.ANTHROPIC_API_KEY + if (!key) { + throw new Error('ANTHROPIC_API_KEY is not configured') + } + __anthropicClient = new Anthropic({ apiKey: key }) + return __anthropicClient +} export interface ProjectSuggestion { name: string @@ -154,7 +163,7 @@ Respond ONLY with a valid JSON object (no markdown fences) matching this exact s ] }` - const response = await anthropic.messages.create({ + const response = await getAnthropic().messages.create({ model: getAnthropicModel(), max_tokens: 4096, messages: [{ role: 'user', content: prompt }], diff --git a/lib/app-discovery.ts b/lib/app-discovery.ts index c735398..925fc54 100644 --- a/lib/app-discovery.ts +++ b/lib/app-discovery.ts @@ -1,6 +1,15 @@ import { Anthropic } from '@anthropic-ai/sdk' -const anthropic = new Anthropic() +let __anthropicClient: Anthropic | null = null +function getAnthropic(): Anthropic { + if (__anthropicClient) return __anthropicClient + const key = process.env.ANTHROPIC_API_KEY + if (!key) { + throw new Error('ANTHROPIC_API_KEY is not configured') + } + __anthropicClient = new Anthropic({ apiKey: key }) + return __anthropicClient +} export interface DiscoveredApp { name: string @@ -44,7 +53,7 @@ For each app, provide: Format as JSON array with objects containing these fields.` - const response = await anthropic.messages.create({ + const response = await getAnthropic().messages.create({ model: 'claude-3-5-sonnet-20241022', max_tokens: 8000, messages: [ diff --git a/lib/code-completion.ts b/lib/code-completion.ts index b7432e0..6a95e62 100644 --- a/lib/code-completion.ts +++ b/lib/code-completion.ts @@ -1,6 +1,15 @@ import { Anthropic } from '@anthropic-ai/sdk' -const anthropic = new Anthropic() +let __anthropicClient: Anthropic | null = null +function getAnthropic(): Anthropic { + if (__anthropicClient) return __anthropicClient + const key = process.env.ANTHROPIC_API_KEY + if (!key) { + throw new Error('ANTHROPIC_API_KEY is not configured') + } + __anthropicClient = new Anthropic({ apiKey: key }) + return __anthropicClient +} /** * Represents a code snippet with semantic metadata @@ -61,7 +70,7 @@ Extract and return JSON with: Return ONLY valid JSON, no markdown formatting.` try { - const response = await anthropic.messages.create({ + const response = await getAnthropic().messages.create({ model: 'claude-3-5-sonnet-20241022', max_tokens: 2000, messages: [{ role: 'user', content: prompt }], @@ -261,7 +270,7 @@ Complete the code: \`\`\`${language}` try { - const response = await anthropic.messages.create({ + const response = await getAnthropic().messages.create({ model: 'claude-3-5-sonnet-20241022', max_tokens: 4000, messages: [{ role: 'user', content: prompt }], diff --git a/lib/cross-platform-scanner.ts b/lib/cross-platform-scanner.ts index 5c0ab6d..e5a53c0 100644 --- a/lib/cross-platform-scanner.ts +++ b/lib/cross-platform-scanner.ts @@ -1,7 +1,16 @@ import { cookies } from 'next/headers' import { Anthropic } from '@anthropic-ai/sdk' -const anthropic = new Anthropic() +let __anthropicClient: Anthropic | null = null +function getAnthropic(): Anthropic { + if (__anthropicClient) return __anthropicClient + const key = process.env.ANTHROPIC_API_KEY + if (!key) { + throw new Error('ANTHROPIC_API_KEY is not configured') + } + __anthropicClient = new Anthropic({ apiKey: key }) + return __anthropicClient +} interface ScannedFile { path: string @@ -55,7 +64,7 @@ For each file, provide: - Reusability score - Can this be combined with other files to build apps?` - const response = await anthropic.messages.create({ + const response = await getAnthropic().messages.create({ model: 'claude-3-5-sonnet-20241022', max_tokens: 4000, messages: [