AI-native design editor. Open-source Figma alternative.
fix(ai): opt into Anthropic direct browser access (#438)
rcoenenff31f90de973
diff --git a/src/app/ai/providers/compatible.ts b/src/app/ai/providers/compatible.tsindex 19ba9fdf4..ac8f54beb 100644--- a/src/app/ai/providers/compatible.ts+++ b/src/app/ai/providers/compatible.ts@@ -1,8 +1,14 @@import { createAnthropic } from '@ai-sdk/anthropic'import { createOpenAI } from '@ai-sdk/openai'+import { IS_TAURI } from '@open-pencil/core/constants'+import type { ModelConfig, ModelProviderAdapter } from '@/app/ai/providers/types'+// Anthropic omits CORS headers unless the caller opts in, so browser requests fail with an+// opaque "failed to fetch". The desktop build goes through tauriFetch and is not subject to CORS.+const BROWSER_ACCESS_HEADERS = { 'anthropic-dangerous-direct-browser-access': 'true' }+export type CompatibleEndpoint = string | ((config: ModelConfig) => string)type OpenAICompatibleOptions = {@@ -48,7 +54,8 @@ export function createAnthropicCompatibleAdapter(const provider = createAnthropic({apiKey: config.apiKey,baseURL: resolveEndpoint(options.baseURL, config),- fetch: runtime.fetch+ fetch: runtime.fetch,+ headers: IS_TAURI ? undefined : BROWSER_ACCESS_HEADERS})return provider(config.customModelID.trim() || config.modelID)}diff --git a/tests/engine/app/ai/model.test.ts b/tests/engine/app/ai/model.test.tsindex fcb30f1f5..937525949 100644--- a/tests/engine/app/ai/model.test.ts+++ b/tests/engine/app/ai/model.test.ts@@ -4,7 +4,9 @@ import { AI_PROVIDERS } from '@open-pencil/core/constants'import { resolveLanguageModelID } from '@/app/ai/chat/model'import { normalizeOpenRouterModel } from '@/app/ai/chat/provider-models'+import { createAnthropicCompatibleAdapter } from '@/app/ai/providers/compatible'import { modelProviderAdapter } from '@/app/ai/providers/registry'+import type { ModelConfig } from '@/app/ai/providers/types'describe('resolveLanguageModelID', () => {test('uses the selected OpenRouter model when no custom model is configured', () => {@@ -39,6 +41,41 @@ describe('model provider registry', () => {})})+describe('anthropic compatible adapter', () => {+ const config: ModelConfig = {+ providerID: 'anthropic',+ apiKey: 'test-key',+ modelID: 'claude-sonnet-4.6',+ customModelID: '',+ customBaseURL: '',+ customAPIType: 'completions'+ }++ async function capturedHeaders(): Promise<Record<string, string>> {+ let captured: Record<string, string> = {}+ const fetchSpy: typeof fetch = async (_input, init) => {+ captured = Object.fromEntries(new Headers(init?.headers).entries())+ throw new Error('stop')+ }++ const model = createAnthropicCompatibleAdapter().create(config, { fetch: fetchSpy })+ await model+ .doGenerate({ prompt: [{ role: 'user', content: [{ type: 'text', text: 'hi' }] }] })+ .catch(() => undefined)++ return captured+ }++ test('opts into direct browser access so Anthropic returns CORS headers', async () => {+ // Anthropic omits Access-Control-Allow-Origin unless this header is sent, which makes the+ // web build fail with an opaque network error. See #436.+ expect(await capturedHeaders()).toHaveProperty(+ 'anthropic-dangerous-direct-browser-access',+ 'true'+ )+ })+})+describe('normalizeOpenRouterModel', () => {test('keeps tool-capable OpenRouter models', () => {expect(