Migrate to Vercel AI SDK for unified LLM abstraction - #6
Conversation
- Replace @anthropic-ai/sdk and openai with ai, @ai-sdk/anthropic, @ai-sdk/openai - Unify streaming logic into single streamToClient function using AI SDK's streamText - Extract provider utilities to lib/ai/providers.ts for testability - Add 21 new tests for provider detection and API key validation - Maintain backward-compatible SSE format for frontend The migration reduces the API route from ~400 lines to ~280 lines and makes adding new providers trivial (just import the provider package).
|
@vivekhaldar is attempting to deploy a commit to the dorsaroh's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
| GitGuardian id | GitGuardian status | Secret | Commit | Filename | |
|---|---|---|---|---|---|
| - | - | Generic High Entropy Secret | 80acb21 | lib/tests/providers.test.ts | View secret |
🛠 Guidelines to remediate hardcoded secrets
- Understand the implications of revoking this secret by investigating where it is used in your code.
- Replace and store your secret safely. Learn here the best practices.
- Revoke and rotate this secret.
- If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.
To avoid such incidents in the future consider
- following these best practices for managing and storing secrets including API keys and other credentials
- install secret detection on pre-commit to catch secret before it leaves your machine and ease remediation.
🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.
The truncateChatHistory and formatChatHistory functions were accidentally removed during the migration. This restores them to preserve conversational context when editing existing compositions.
Motivation
The current implementation uses two separate SDKs (
@anthropic-ai/sdkandopenai) with nearly identical streaming logic duplicated for each provider (~70 lines each). This works, but:Adding new providers requires copy-pasting — Want to add Google Gemini or Mistral? You'd need to write another 70-line streaming function.
Maintenance burden — Bug fixes or improvements to streaming logic need to be applied in two places.
Inconsistent error handling — Each SDK has slightly different error formats that need individual handling.
Solution
Replace both SDKs with the Vercel AI SDK (
aipackage), which provides:streamText()function works with any providerimport { google } from '@ai-sdk/google'Changes
Dependencies
Code
app/api/claude/route.ts— Replaced dual streaming functions with unifiedstreamToClient()using AI SDK'sstreamText()lib/ai/providers.ts(new) — Extracted provider utilities for testabilitylib/__tests__/providers.test.ts(new) — 21 tests for provider detection and validationMetrics
Backward Compatibility
The frontend is unchanged. The SSE format (
data: {"type": "content_block_delta", ...}) is preserved, so the existing streaming consumer works without modification.Testing
Future Possibilities
With this foundation, adding new providers becomes trivial: