feat: Migrate from Anthropic SDK to Vercel AI SDK #127
+6,390
−3,875
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR migrates Chartsmith from direct Anthropic SDK usage to Vercel AI SDK for all LLM operations, enabling multi-provider support (Anthropic, OpenAI, Google, OpenRouter) while maintaining backward compatibility.
Key Changes
1. Vercel AI SDK Migration
Frontend Changes
useChathook from@ai-sdk/reactfor conversational chat, replacing custom fetch-based streamingNew API Routes (Next.js)
/api/chat/api/llm/plan/api/llm/expand/api/llm/summarize/api/llm/cleanup-values/api/llm/execute-action/api/llm/prompt-type/api/modelsGo Backend Changes
/api/chatGo Files Converted to API Routes
The following Go files had their LLM logic migrated to Next.js API routes. The Go files now act as clients that call the Next.js APIs:
pkg/llm/plan.go/api/llm/planpkg/llm/expand.go/api/llm/expandpkg/llm/summarize.go/api/llm/summarizepkg/llm/conversational.go/api/chatpkg/llm/execute-action.go/api/llm/execute-actionpkg/llm/cleanup-converted-values.go/api/llm/cleanup-valuesNote: The Go files still contain business logic (e.g., caching, file operations, workflow management) but now delegate LLM calls to the Next.js API routes via
nextjs_client.go.Infrastructure
X-Internal-API-KeyheaderArchitecture
Before (Main Branch)
After (This PR)
New Features
Multi-Provider Support
Internal API Authentication
X-Internal-API-Keyheader) for Go worker → Next.js communicationtoken) for browser requestscheckApiAuth()function for all protected API routesDependencies
Added
{ "ai": "^5.0.104", "@ai-sdk/anthropic": "^2.0.50", "@ai-sdk/openai": "^2.0.74", "@ai-sdk/google": "^2.0.44", "@ai-sdk/react": "^2.0.104", "@openrouter/ai-sdk-provider": "^1.2.8" }Environment Variables
Required (at least one provider)
ANTHROPIC_API_KEYOPENAI_API_KEYGOOGLE_GENERATIVE_AI_API_KEYOPENROUTER_API_KEYInternal Communication
INTERNAL_API_KEYdev-internal-key(dev only)NEXTJS_API_URLhttp://localhost:3000Optional
CHARTSMITH_LLM_MODELMigration Notes
For Developers
.env.localINTERNAL_API_KEY(or use defaultdev-internal-keyfor dev)npm installto get new dependenciesFor Deployment
INTERNAL_API_KEY:openssl rand -hex 32INTERNAL_API_KEYin both Next.js and worker environmentsNEXTJS_API_URLif Next.js is not at default locationSummary
This PR: