feat(ai): native Grok + Claude providers, Claude web proxy, opt-in browser-Ollama (ADR-0016/0017) - #298
feat(ai): native Grok + Claude providers, Claude web proxy, opt-in browser-Ollama (ADR-0016/0017)#298qnbs wants to merge 7 commits into
Conversation
Four findings deferred from PR #297 to keep that PR's merge unblocked, addressed here first per explicit instruction before starting the Grok/Claude/Ollama plan execution: 1. GROK-PROVIDER-INTEGRATION-PLAN.md: the addendum was numbered "## 7" but its own subsections said "8.1"-"8.4", and a "Β§Β§1-7 above" self-reference included the addendum itself. Renumbered to 7.1-7.4, fixed both self-references, and fixed a second pre-existing ambiguous "Β§5" cross-reference (meant "item 5 within Phase 1's own list", not top-level section 5) while in the area. 2. tests/unit/listenerMiddleware.test.ts: added the required QNBS-v3 comment next to the new featureFlagsActions import. 3. Added the required QNBS-v3 comment before the new "local-first shadow sync" describe block. 4. Rewrote the local-first sync test to assert the actual selector-to-binding boundary directly instead of inferring success from an absence of logger calls in an unmocked-dependency, environment-dependent setup. Added real mocks for services/localFirst/{projectDoc,docBinding,docPersistence} and services/storage/storageEncryptionService so the test can assert the exact object selectProjectData(state) returns is what reaches ProjectDocBinding's constructor and syncFromProject/verify. Hit two real bugs while building this: (a) an arrow-function mock implementation can never be used as a constructor -- switched to a real class, which Biome's useArrowFunction rule doesn't flag either (a function-expression fix would have been immediately reverted by the same linter that was satisfied by removing the arrow function in the first place); (b) vi.fn().mockImplementation() can't type a class constructor against its generic (...args) => any signature -- mocked the class directly instead of wrapping it in vi.fn().
Re-checked the plan's own findings against current main before starting
implementation (Phase 0's explicit purpose), and found the original
draft's Claude analysis was incomplete in a way that changes the whole
approach:
- AiProviderCard.tsx's primary provider dropdown already has an
'anthropic' entry with a dedicated warning block -- missed originally
because the investigation searched for the literal string "claude",
never "anthropic" (the actual identifier), when checking that file.
Its existing i18n copy already half-promises desktop support
("...or use the Tauri desktop app for Anthropic calls").
- streamAnthropic() throws unconditionally on EVERY platform, including
desktop -- but CORS is a browser-only restriction. It never even
checks isTauriRuntime() before throwing. Tauri's native HTTP plugin
(@tauri-apps/plugin-http, services/localServerHttp.ts) already solves
exactly this class of problem for Ollama/LM Studio/vLLM (ADR-0012);
the same escape hatch works for any HTTPS endpoint, including
api.anthropic.com, since native networking isn't subject to browser
CORS at all.
Restructured Phase 2 into two independent tracks: Track A (desktop) is
a narrow bug fix reusing ADR-0012's established pattern, no new
infrastructure, ships first. Track B (web/PWA) is the actual new
architecture -- the serverless proxy -- and is now correctly scoped to
only the three web deploy targets (desktop no longer needs it at all).
Updated the Executive Summary, both Definition of Done checklists, and
all internal cross-references accordingly.
Formalizes GROK-PROVIDER-INTEGRATION-PLAN.md's Phase 0 decision record: Grok is a pure UI-wiring fix (backend already works); Claude splits into Track A (desktop, native-HTTP via the ADR-0012 pattern, ships first, no new infrastructure) and Track B (web/PWA, this app's first serverless backend dependency, ships second). Also documents why the Ollama-in-PWA follow-up (Issue #266) is a separate decision, not a Track-B variant -- a hosted proxy can't reach a user's own localhost.
Grok (xAI, Cloud 4) had a real, working backend (aiProviderService.ts's streamGrok(), key storage, a live /v1/models connection test) but was never selectable as a primary provider -- reachable only as a hybrid- fallback-chain option. This closes that UI/wiring gap; no new backend work needed. - AiProviderCard.tsx: added Grok to the primary provider dropdown with its own API-key input + model selector (grok-3 / grok-3-mini), reusing the existing storageService key-storage plumbing and the already-working testAIConnection() grok case for Test Connection. Also fixed the whole provider array's i18n while touching it -- gemini/openai/ollama/anthropic labels were hardcoded literals too, not just Grok's gap. - providerFactory.ts: providerToKind() gets a distinct 'grok' kind (not folded into 'openaiCompatible') -- Grok needs a fixed baseURL (https://api.x.ai/v1) and a real stored key, unlike the Ollama default that also maps to 'openaiCompatible'; folding it in would have silently sent Grok requests through Ollama's localhost default. - worldScriptCompletionFetch.ts: resolveModelConfig() branches on the new 'grok' kind, bringing Grok into the newer Writer-streaming path (useWorldScriptAI), not just legacy thunks. - i18n: added settings.ai.grokKey + settings.ai.provider.{gemini, openai,ollama,anthropic,grok} across all 19 locales -- hand- translated for the 5 production locales (de/en/es/fr/it), EN fallback seeded for the other 14 pending the established bulk- translate workflow. Bundles rebuilt; README's key-count badge/table updated 2849 -> 2855. - Tests: providerFactory (grok maps to 'grok', not 'unsupported'), worldScriptCompletionFetch (grok success path + missing-key 401), AiProviderCard (renders key input + model selector, saves via storageService.saveApiKey('grok', ...)). No CSP change needed: https://api.x.ai is already in src-tauri/tauri.conf.json, and the web CSP's https: scheme-source (ADR-0004) already covers it. No new feature flag -- confirmed via `pnpm exec tsx scripts/audit-feature-parity.ts` (22 flags, 0 drifts). The many locales/*/{characters,common,dashboard,desktop,export, outline,worlds,writer}.json changes in this commit are incidental -- running the i18n tooling's --fix pass re-sorted keys in files it touched along the way. No content was added, removed, or changed in any of those files, only key order.
Claude/Anthropic's streamAnthropic() threw unconditionally on every platform, including desktop -- but CORS is a browser-only restriction. Tauri's native HTTP plugin (localServerFetch, ADR-0012) already exists in this codebase to bypass exactly this class of problem for Ollama; the same escape hatch works for any HTTPS endpoint, since native networking isn't subject to browser CORS at all. - aiProviderService.ts: streamAnthropic() branches on isTauriRuntime() before throwing -- desktop calls https://api.anthropic.com/v1/messages directly via localServerFetch (Messages API format: x-api-key + anthropic-version headers). generateTextSingleProvider()'s anthropic case now delegates to the fixed streamAnthropic instead of its own separate unconditional throw. testAIConnection()'s anthropic case gets the same branch, using a minimal (max_tokens: 1) real request as the connectivity check since Anthropic has no public /v1/models endpoint to probe. Image generation is deliberately left throwing -- Anthropic's API doesn't offer that endpoint at all, so this isn't a CORS bug to fix. - src-tauri/tauri.conf.json: added https://api.anthropic.com to the CSP connect-src (mirrors the existing Grok entry). - AiProviderCard.tsx: the existing 'anthropic' warning-only block is now desktop-conditional -- desktop renders a real API-key input + model selector (Opus/Sonnet/Haiku 4.x) exactly like every other cloud provider; web/PWA keeps the warning, with copy updated to say desktop now genuinely works rather than "might" (Track B's proxy note stays "coming soon", since that part isn't built yet). - i18n: added settings.ai.anthropicKey; revised anthropicCorsNote/ anthropicHint wording across the 5 production locales to reflect desktop actually working now; EN fallback seeded for the other 14. 2855 -> 2856 keys; README badge/table updated. - Tests: aiProviderService (testAIConnection + streamText desktop success/failure/no-key paths, web-path unchanged), AiProviderCard (desktop shows key input, web shows warning, key save wired to storageService.saveApiKey('anthropic', ...)). Track B (the web/PWA serverless proxy) is unaffected by this commit -- still not built, still the only path for browser-tab Claude.
Track A (previous commit) fixed Claude on desktop. This closes the
other half: browser tabs have no native-HTTP escape hatch from CORS,
so Anthropic requests from Vercel/Cloudflare Pages deployments now
relay through this app's own stateless serverless proxy -- its first
backend dependency ever (previously a purely static SPA + a desktop
bundle with no server of its own).
- api/_shared/claudeProxyCore.ts: platform-agnostic relay core (Web
Request/Response only, no @vercel/node or @cloudflare/workers-types
dependency). Forwards { apiKey, model, messages, maxTokens? } to
Anthropic's Messages API and returns the response unmodified. Mandatory
abuse controls per ADR-0016 (the endpoint is public and unauthenticated
by construction -- CWE-400 surface): Zod schema validation, a 256 KiB
body-size cap checked against both the Content-Length header and the
actual body (defeats a spoofed header), a same-origin check (Origin
must match the deployment's own host), a best-effort per-client-IP
rate limit (20 req/60s, in-memory -- explicitly not distributed, see
code comment), and a 20s outbound timeout to Anthropic. Never logs the
key, prompt, or response on any path -- asserted directly in tests via
console spies across every branch.
- api/claude-proxy.ts: Vercel Edge Function entry point (thin wrapper).
- functions/api/claude-proxy.ts: Cloudflare Pages Function equivalent,
at the matching /api/claude-proxy route (Cloudflare has no automatic
"/api" prefix the way some platforms do -- the path has to earn it).
- services/deployTarget.ts: isServerlessProxyCapable() -- reuses
import.meta.env.BASE_URL (already this codebase's build-time
GitHub-Pages-vs-edge marker, see config/resolveViteBase.ts) to detect
whether the current web build can host the proxy at all. GitHub Pages
is static-only and never can.
- services/aiProviderService.ts: streamAnthropic() now has three
branches -- desktop (Track A, unchanged), proxy-capable web (fetches
/api/claude-proxy), and GitHub Pages (throws immediately, before ever
checking for a key, since no key would help). testAIConnection's
anthropic case gets the same three-way split. Retired the
'backendProxyRequired' TestConnectionErrorKind (nothing can produce it
anymore) in favor of 'proxyUnavailableStaticHost'.
- AiProviderCard.tsx: extracted the whole anthropic UI block into
components/settings/AnthropicProviderFields.tsx -- both to keep
AiProviderCard under the Biome cognitive-complexity gate (52 > 50
before the split) and under CLAUDE.md's 700-line file-size target.
Desktop and proxy-capable web now render the same real key + model
UI every other cloud provider gets; only GitHub Pages keeps the
warning block, with copy rewritten from a generic CORS note to the
actual "no proxy host" reason.
- i18n: settings.ai.anthropicProxyNote (new); anthropicCorsNote/
anthropicHint/corsRestriction copy rewritten (desktop and Vercel/CF
no longer say "coming soon" -- they work); testError.
backendProxyRequired removed, testError.proxyUnavailableStaticHost
added. Hand-translated for the 5 production locales; EN fallback
seeded for the other 14. 2856 -> 2857 keys; README badge/table/count
updated in all 4 places.
- docs/SECURITY-THREAT-MODEL.md: new "Claude serverless proxy
trust-model change" section, an Information Disclosure row, a Denial
of Service row (the abuse-control list), a Mitigation Mapping row,
and a checklist item -- this is the first provider whose BYOK key
transits infrastructure WorldScript runs, and that's stated plainly
rather than folded into the general BYOK framing.
- README.md: privacy bullet and the encryption section both carry the
same caveat; the Cloud 3 provider-stack row lists the actual model
ids (Opus 4.7/Sonnet 4.6/Haiku 4.5, not the stale "Claude 3.5
Sonnet") and where Claude does/doesn't work.
- docs/adr/0016: documents why providerFactory.ts's Vercel-AI-SDK layer
still returns 'unsupported' for anthropic (would need the
@ai-sdk/anthropic package -- Anthropic's Messages API isn't
OpenAI-Chat-Completions-shaped like Grok/Ollama are, so that's a new
runtime dependency out of scope here) and why no dedicated E2E spec
was added (no other cloud provider -- including Grok -- has one in
this repo; the RTL/unit level is where this class of Settings-form
coverage already lives).
Tests: 14 new (claudeProxyCore: schema/size/origin/rate-limit/timeout/
relay-fidelity/no-logging), 3 new (Vercel + Cloudflare entry-point
delegation), 3 new (deployTarget branch coverage), aiProviderService's
Anthropic describe block rewritten for the three-way split,
AiProviderCard's anthropic describe block rewritten with a new
proxy-capable-web case. 126 tests green across the full Track B
surface; lint, typecheck, i18n:check, docs:check, and parity:check
(22 flags, 0 drifts -- no new flag needed) all clean.
β¦DR-0017, Issue #266) Issue #266's own comment thread already worked out the correct answer: CORS is the server's own configuration, not an immutable browser wall. If a user starts their own Ollama server with OLLAMA_ORIGINS covering the PWA's exact origin, a direct browser fetch to localhost succeeds -- real, standards-compliant CORS, the same non-magic model NovelCrafter's own browser-Ollama support uses. This wires that up as an explicit, default-off opt-in, without touching the "PWA stays desktop-only by default" guarantee ADR-0012 established. - features/featureFlags/featureFlagsSlice.ts: new enableBrowserOllama flag, default off (23rd flag, 7th user opt-in). - features/featureCatalog.ts: catalog entry (tier: ai, risk: medium, matches enableVoiceSupport's classification for a similarly-scoped opt-in) with real gateLocations. - hooks/useSettingsView.ts: dispatch case for the new flag (caught by scripts/audit-feature-parity.ts's "toggle fires, Redux doesn't update" check -- exactly the class of drift that gate exists for). - services/aiProviderService.ts: testAIConnection's ollama case now accepts opts.browserOllamaEnabled instead of hard-requiring isTauriRuntime(); a generic 'unreachable' result remaps to a new 'corsSuspected' kind when running the opt-in browser path -- CORS rejection and "server genuinely down" are identical TypeErrors at the JS level, so this is an honestly-hedged heuristic, not a diagnosis. - services/localServerHttp.ts: NO functional change -- verified during implementation that resolveFetch() already returns globalThis.fetch unconditionally on the web; the "PWA never probes localhost" policy lived entirely in the UI/testAIConnection call sites, not the transport layer. Added a comment explaining why, so a future reader doesn't go looking for a change that was never needed here. - components/settings/AiProviderCard.tsx (+ AiSections.tsx passing the flag down): canAttemptOllama = isDesktop || browserOllamaEnabled replaces every bare isDesktop check gating the ollama auto-probe effect, Load Models, and Test Connection. When on, an info block renders the exact `OLLAMA_ORIGINS=<origin> ollama serve` command for window.location.origin -- always correct for the current deployment, never a guessed/generic value, since WorldScript ships from multiple possible origins (unlike NovelCrafter's single fixed SaaS URL). - docs/adr/0017-pwa-browser-ollama-opt-in.md: new ADR: why this differs from the Claude proxy pattern (a hosted function can reach the public internet, never a user's own localhost -- hard networking fact, not policy), why WorldScript never defaulted this on unlike NovelCrafter, explicit non-goals (no LAN-IP, no PNA, not the default). ADR-0012 cross-referenced both directions. - docs/FEATURE-PARITY.md, CLAUDE.md (x2), tests/CLAUDE.md, AGENTS.md (x2), .github/copilot-instructions.md: flag-count references updated 22->23 total / 6->7 opt-in-off across every doc that enumerates them. - i18n: settings.featureFlags.enableBrowserOllama, ai.ollamaBrowserOptInTitle/ Body, ai.testError.corsSuspected -- hand-translated for the 5 production locales, EN fallback seeded for the other 14. 2857 -> 2861 keys; README updated in all 4 places. - Help articles (help.aiStudio.providers.content, help.faq.api.content, 5 production locales): fixed pre-existing stale content discovered while updating for this work -- wrong model names (Claude 3.5 Sonnet/Grok-2, both outdated), a genuine CSP-vs-CORS factual error in the Ollama description (CORS blocks localhost, not CSP -- the exact misconception ADR-0012 already corrected elsewhere), and a missing OpenRouter entry (shipped as Cloud 5, never documented in either article). Not scope creep: same articles, same edit, left broken would misinform users reading about the very feature this PR ships. - README.md: Ollama provider-stack row now documents both the desktop native path and the new opt-in browser path with the ADR-0017 link. Tests: 5 new (testAIConnection's browserOllamaEnabled on/off, corsSuspected remapping, non-remap of timeout/success), 5 new (AiProviderCard's flag-off/on UI states, auto-probe, button enablement, desktop-unaffected), 1 new (useSettingsView's dispatch case), plus the parametrized featureFlagsSlice case and updated 21->22 toggle count in FeatureFlagsSection. 336 tests green across the full affected surface; lint, typecheck, i18n:check, docs:check, and parity:check (23 flags, 0 drifts) all clean.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Important Review skippedToo many files! This PR contains 204 files, which is 54 over the limit of 150. To get a review, narrow the scope: Upgrade to Pro+ to raise the limit. This review couldn't start because sufficient usage credits or metered capacity aren't available. Add credits or update usage-based reviews in the billing tab, then retry. βοΈ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: π Files selected for processing (204)
You can disable this status message by setting the Comment |
π CodeAnt Quality Gate ResultsCommit: β Overall Status: PASSEDQuality Gate Details
|
|
Splitting into a stacked sequence β CodeRabbit's own 150-file limit blocked review on this combined PR (204 files). Replaced by:
Branch |
Summary
Closes the gap between README's provider claims and actual Settings UI reachability for Grok and Claude, and follows up Issue #266 with an opt-in browser-Ollama path. Full rationale in ADR-0016 and ADR-0017; execution plan in
GROK-PROVIDER-INTEGRATION-PLAN.md; user-facing changes inCHANGELOG.md's[Unreleased]section.providerFactory.ts. The backend (streamGrok()) already worked β pure UI/wiring gap.streamAnthropic()now branches onisTauriRuntime()before throwing; desktop calls Anthropic directly via the native-HTTP pattern ADR-0012 established for Ollama.api/claude-proxy.tsVercel Edge Function +functions/api/claude-proxy.tsCloudflare Pages Function) β this app's first backend dependency. Schema-validated, body-size-capped, same-origin-checked, rate-limited, timeout-bounded; never logs the key/prompt/response. GitHub Pages (static-only, can't host the proxy) shows an honest unavailable state.enableBrowserOllamaflag (default off) lets the browser attempt a direct Ollama connection when the user has separately configured their own server'sOLLAMA_ORIGINSβ the real, non-proxy model NovelCrafter uses. Not the default; desktop stays the recommended zero-config path.docs/SECURITY-THREAT-MODEL.md, README privacy framing + provider table, help articles (help.aiStudio.providers,help.faq.api) β including fixing pre-existing stale content (wrong Claude/Grok model names, a genuine CSP-vs-CORS factual error, a missing OpenRouter entry) discovered while updating the same articles for this work.Test plan
pnpm run lintβ cleanpnpm run typecheckβ clean (exact CI command,--checkers 4)pnpm run i18n:checkβ 19 locales Γ 2861 keys, bundles rebuiltpnpm run docs:checkβ cleanpnpm exec tsx scripts/audit-feature-parity.tsβ 23 flags, 0 driftsπ€ Generated with Claude Code