Skip to content

Add Anthropic as alternative AI text provider#159

Merged
mayashavin merged 8 commits into
mainfrom
feat/multi-provider-support
May 14, 2026
Merged

Add Anthropic as alternative AI text provider#159
mayashavin merged 8 commits into
mainfrom
feat/multi-provider-support

Conversation

@mayashavin

Copy link
Copy Markdown
Owner

Summary

  • Add anthropic as a supported ai.provider option alongside openai
  • Introduce provider factory functions (createTextAIProvider, createImageProvider) in src/cli/shared/providers.ts, replacing hardcoded OpenAI imports in all 6 CLI commands
  • Add @anthropic-ai/sdk dependency and src/providers/ai/anthropicTextProvider.ts implementation
  • Add AIProvider type ('openai' | 'anthropic') to core types
  • Update config schema to accept z.enum(['openai', 'anthropic'])
  • Update config init wizard with Anthropic choice
  • Update docs (providers.md, environment-variables.md) and .env.example

Usage

// ktavi.config.ts
export default {
  ai: {
    provider: 'anthropic',
    textModel: 'claude-sonnet-4-20250514',
  },
};

Image generation still requires OPENAI_API_KEY since Anthropic doesn't offer image generation.

Closes #107

Test plan

  • 5 unit tests for Anthropic text provider (API key validation, JSON parsing, error handling)
  • 8 new tests for provider factories (OpenAI routing, Anthropic routing, missing key, image provider)
  • All 200 tests pass
  • Typecheck clean

🤖 Generated with Claude Code

Introduce provider factory pattern for AI text and image providers,
replacing hardcoded OpenAI imports in CLI commands. Users can now set
`ai.provider: 'anthropic'` in config to use Claude models.

Closes #107

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings May 12, 2026 11:27
@codecov-commenter

codecov-commenter commented May 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 78.68852% with 39 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.44%. Comparing base (a193e33) to head (4d9ce5a).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/cli/commands/cover.ts 0.00% 11 Missing ⚠️
src/cli/commands/prepare.ts 0.00% 11 Missing ⚠️
src/cli/commands/review.ts 0.00% 6 Missing ⚠️
src/cli/commands/analyze.ts 0.00% 3 Missing ⚠️
src/cli/commands/fix.ts 0.00% 2 Missing ⚠️
src/cli/commands/seo.ts 0.00% 2 Missing ⚠️
src/providers/ai/anthropicTextProvider.ts 97.59% 2 Missing ⚠️
src/providers/ai/openaiTextProvider.ts 0.00% 1 Missing ⚠️
src/providers/image/openaiImageProvider.ts 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #159      +/-   ##
==========================================
+ Coverage   64.65%   67.44%   +2.78%     
==========================================
  Files          57       58       +1     
  Lines        2340     2543     +203     
  Branches      369      429      +60     
==========================================
+ Hits         1513     1715     +202     
- Misses        827      828       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds Anthropic (Claude) as an alternative text AI provider, wiring provider selection through config/schema and centralizing CLI provider creation so commands can route to either OpenAI or Anthropic.

Changes:

  • Introduce AIProvider ('openai' | 'anthropic') and update config schema + config init wizard to support Anthropic.
  • Add createAnthropicTextProvider and CLI provider factories (createTextAIProvider, createImageProvider, getApiKeyForProvider) and update CLI commands to use them.
  • Add Anthropic SDK dependency and update docs + .env.example for ANTHROPIC_API_KEY.

Reviewed changes

Copilot reviewed 17 out of 18 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/providers/ai/anthropicTextProvider.test.ts Adds unit tests for Anthropic text provider behavior and error cases.
tests/cli/shared/providers.test.ts Adds tests for provider factory routing and env-key lookup.
src/providers/ai/anthropicTextProvider.ts Implements Anthropic-based TextAIProvider.
src/core/types.ts Adds AIProvider union type.
src/core/config.ts Updates config typing and Zod schema to allow anthropic.
src/cli/shared/providers.ts Centralizes provider/env-key mapping and provider factory creation.
src/cli/commands/seo.ts Uses provider factory instead of hardcoded OpenAI provider.
src/cli/commands/review.ts Uses provider factory and updates missing-key messaging.
src/cli/commands/prepare.ts Uses provider factories for text + image providers.
src/cli/commands/fix.ts Uses provider factory instead of hardcoded OpenAI provider.
src/cli/commands/cover.ts Uses provider factories for text + image providers.
src/cli/commands/configInit.ts Adds Anthropic option and prints correct env next steps.
src/cli/commands/analyze.ts Uses provider factory instead of conditional OpenAI usage.
package.json Adds @anthropic-ai/sdk dependency.
package-lock.json Locks Anthropic SDK and transitive dependencies.
docs/providers.md Documents Anthropic setup and clarifies image generation remains OpenAI-only.
docs/environment-variables.md Documents ANTHROPIC_API_KEY and updated command/key behavior.
.env.example Adds ANTHROPIC_API_KEY placeholder.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/cli/commands/cover.ts
Comment thread src/cli/commands/prepare.ts
Comment on lines +33 to +35
const content = textBlock.text;
const jsonMatch = content.match(/\{[\s\S]*\}/);
if (!jsonMatch) {
);
}

return JSON.parse(jsonMatch[0]) as TOutput;
Comment thread src/cli/commands/review.ts
…centralized key names

- cover/prepare: error immediately when --generate requested but
  OPENAI_API_KEY missing, instead of silently skipping
- anthropicTextProvider: prefer fenced code blocks for JSON extraction,
  use balanced-brace parser instead of greedy regex, wrap JSON.parse
  in try/catch to throw KtaviError with cause
- review/cover: use getApiKeyEnvName() instead of inline provider check
- Add 5 new tests (code block extraction, balanced parse, malformed JSON,
  getApiKeyEnvName)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 17 out of 18 changed files in this pull request and generated 2 comments.

Comment on lines +5 to +13
const start = text.indexOf('{');
if (start === -1) return null;

let depth = 0;
for (let i = start; i < text.length; i++) {
if (text[i] === '{') depth++;
else if (text[i] === '}') depth--;
if (depth === 0) return text.slice(start, i + 1);
}
Comment thread src/providers/ai/anthropicTextProvider.ts
mayashavin and others added 2 commits May 13, 2026 13:43
Introduce KTAVI_TEXT_API_KEY for text generation and KTAVI_IMAGE_API_KEY
for image generation, with provider-specific keys (OPENAI_API_KEY,
ANTHROPIC_API_KEY) as fallbacks. Switching providers is now purely a
config change — .env stays the same.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…onses

extractJsonObject now tries each `{` candidate until JSON.parse succeeds,
handling cases where the model uses braces in explanatory text before the
actual JSON payload. Also improves error message clarity.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 19 out of 20 changed files in this pull request and generated 7 comments.

if (!apiKey) {
throw new KtaviError(
'OPENAI_API_KEY is not set. Please add it to your .env file.',
'API key is not set. Please set KTAVI_IMAGE_API_KEY in your .env file.',
Comment thread src/providers/ai/openaiTextProvider.ts Outdated
if (!apiKey) {
throw new KtaviError(
'OPENAI_API_KEY is not set. Please add it to your .env file.',
'API key is not set. Please set KTAVI_TEXT_API_KEY in your .env file.',
Comment thread src/providers/ai/anthropicTextProvider.ts
Comment thread src/cli/commands/review.ts
Comment thread src/cli/commands/cover.ts Outdated
Comment thread src/cli/commands/prepare.ts Outdated
imageProvider = createImageProvider(config, process.env);
if (!imageProvider) {
logger.error(
'KTAVI_IMAGE_API_KEY is required for image generation. Add it to your .env file.',
export function createAnthropicTextProvider(apiKey: string, model: string): TextAIProvider {
if (!apiKey) {
throw new KtaviError(
'API key is not set. Please set KTAVI_TEXT_API_KEY in your .env file.',
… vars

- Make extractJsonObject track string context so braces inside JSON
  string values (e.g. {"text": "}"}) don't break depth counting
- Update all provider and CLI error messages to mention both the KTAVI
  key and the provider-specific fallback key
- Fix integration test asserting old error text

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 20 out of 21 changed files in this pull request and generated 5 comments.

Comment thread src/core/config.ts
Comment thread src/cli/commands/configInit.ts
Comment thread src/cli/commands/configInit.ts Outdated
Comment on lines +197 to +199
steps.push(
'Set KTAVI_TEXT_API_KEY in your .env (or the provider-specific key like OPENAI_API_KEY)',
);
Comment thread docs/providers.md Outdated
Comment on lines +20 to +25
**Image model** is used by: `cover --generate` and `prepare --generate-cover`. Image generation currently uses OpenAI regardless of the text provider setting.

Set your API key in `.env`:

```
OPENAI_API_KEY=sk-...
KTAVI_TEXT_API_KEY=sk-...
Comment on lines 7 to +11

describe('openaiImageProvider', () => {
it('throws KtaviError when API key is empty', () => {
expect(() => createOpenAIImageProvider('')).toThrow(KtaviError);
expect(() => createOpenAIImageProvider('')).toThrow('OPENAI_API_KEY is not set');
expect(() => createOpenAIImageProvider('')).toThrow('KTAVI_IMAGE_API_KEY');
…or messages

- Default textModel to claude-sonnet-4 when provider is anthropic
  (deepMerge applies provider-specific default when textModel is not
  explicitly set in the user config)
- Config init wizard now suggests the correct model for the selected
  provider and tailors next-steps to mention the right fallback key
- Document KTAVI_IMAGE_API_KEY in providers.md
- Integration test reads KTAVI_IMAGE_API_KEY with OPENAI_API_KEY fallback

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 22 out of 23 changed files in this pull request and generated 4 comments.

Comment thread src/cli/shared/providers.ts Outdated
Comment thread tests/providers/image/openaiImageProvider.integration.test.ts Outdated
Comment thread src/cli/commands/cover.ts Outdated
Comment thread src/cli/commands/prepare.ts Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 22 out of 23 changed files in this pull request and generated 2 comments.

Comment thread src/cli/shared/providers.ts
Comment thread src/cli/commands/configInit.ts Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 22 out of 23 changed files in this pull request and generated 1 comment.

Comment thread src/cli/shared/providers.ts
@mayashavin mayashavin merged commit 3854765 into main May 14, 2026
7 checks passed
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.

Support additional AI providers beyond OpenAI

3 participants