Skip to content

Conversation

@davidturnbull
Copy link
Contributor

@davidturnbull davidturnbull commented Oct 3, 2025

Summary

This PR refactors provider management across the Lingo.dev monorepo by consolidating scattered provider-specific code into two new shared packages: @lingo.dev/providers and @lingo.dev/config.

What Changed

For Users:

  • No breaking changes - all existing i18n.json configurations continue to work as before
  • More consistent error messages when provider API keys are missing
  • Better support for adding new AI providers in the future

For Developers:

  • Created @lingo.dev/providers package that centralizes:
    • Provider factory (createProviderClient())
    • Provider metadata (API key env vars, config keys, documentation links)
    • API key resolution logic
    • Custom error types (ProviderKeyMissingError, ProviderAuthFailedError, UnsupportedProviderError)
    • Supported providers: OpenAI, Anthropic, Google, Groq, Mistral, Ollama, OpenRouter
  • Created @lingo.dev/config package for RC file management:
    • getRcConfig() - reads from ~/.lingodotdevrc
    • saveRcConfig() - writes to ~/.lingodotdevrc
  • Removed duplicate provider code from CLI and compiler packages
  • Reduced bundle sizes by removing redundant AI SDK dependencies from individual packages

Packages Modified

  • packages/cli/ - Uses new provider factory and config package
  • packages/compiler/ - Uses new provider factory and config package
  • packages/spec/ - Enhanced provider type definitions
  • packages/config/ - NEW - RC file management
  • packages/providers/ - NEW - Centralized provider logic

Files Changed

34 files changed: +1,016 insertions, -741 deletions

Key changes:

  • New packages: config and providers
  • Removed files: compiler/src/utils/llm-api-key.ts, compiler/src/utils/rc.ts
  • New test files: routing tests for CLI and compiler packages
  • Updated: provider routing logic in CLI and compiler

Testing Instructions

Prerequisites

pnpm install
pnpm build

Test 1: Run Unit Tests

Providers package:

cd packages/providers
pnpm test

CLI provider routing:

cd packages/cli
pnpm test providers-routing

Compiler provider routing:

cd packages/compiler
pnpm test providers-routing

Test 2: Test CLI with Different Providers

Configure a provider in your i18n.json:

{
  "provider": {
    "id": "openai",
    "model": "gpt-4o",
    "prompt": "Translate professionally"
  }
}

Set the corresponding API key:

export OPENAI_API_KEY="your-key"
# or use the config command:
node packages/cli/bin/cli.mjs config set llm.openaiApiKey "your-key"

Run localization:

node packages/cli/bin/cli.mjs run

Test with other providers by changing id to: anthropic, google, groq, mistral, ollama, or openrouter

Test 3: Test Compiler with Different Providers

The compiler is used via bundler integration (Vite, Next.js, etc.). Test with a demo project:

Using Vite demo:

cd demo/vite-project

Configure the compiler in vite.config.ts with a specific provider:

lingoCompiler.vite({
  sourceRoot: "src",
  targetLocales: ["es", "fr"],
  models: {
    es: "openai:gpt-4o",
    fr: "anthropic:claude-3-5-sonnet-20241022"
  }
})(viteConfig)

Set API keys and run the dev server:

export OPENAI_API_KEY="your-key"
export ANTHROPIC_API_KEY="your-key"
pnpm dev

The compiler will validate provider API keys on startup and show clear error messages if any are missing.

Test other demo projects:

# Next.js demo
cd demo/next-app
pnpm dev

# React Router demo
cd demo/react-router-app
pnpm dev

# AdonisJS demo
cd demo/adonisjs
pnpm dev

Test 4: Verify Error Handling

CLI - Test missing API key:

unset OPENAI_API_KEY
node packages/cli/bin/cli.mjs run
# Should see clear message about missing OPENAI_API_KEY with instructions

Compiler - Test missing API key:

cd demo/vite-project
unset OPENAI_API_KEY
pnpm dev
# Compiler should exit with helpful error message showing how to set the key

Test 5: Config Commands

Test RC file operations:

node packages/cli/bin/cli.mjs config set llm.openaiApiKey "test-key"
node packages/cli/bin/cli.mjs config get llm.openaiApiKey
# Should output: test-key

node packages/cli/bin/cli.mjs config set llm.anthropicApiKey "another-key"
node packages/cli/bin/cli.mjs config get llm.anthropicApiKey
# Should output: another-key

Test 6: Full Regression

Run all tests:

pnpm test

Build all packages:

pnpm build

Migration Notes

No migration required - this is a non-breaking internal refactoring.

@davidturnbull davidturnbull force-pushed the davidt-providers-package branch from 29eebb6 to 09e1d7e Compare October 16, 2025 06:59
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants