[PR #12] [MERGED] fix: auto-detect AI provider from environment variables and lazy initialize AI assistant #11

Closed
opened 2026-03-04 14:09:22 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ersinkoc/git-commit-time-machine/pull/12
Author: @KilimcininKorOglu
Created: 12/18/2025
Status: Merged
Merged: 12/18/2025
Merged by: @ersinkoc

Base: mainHead: main


📝 Commits (2)

  • ed64489 fix: auto-detect AI provider from environment variables and lazy initialize AI assistant
  • e1bd20e test: update tests to support auto-detected local provider as default

📊 Changes

5 files changed (+149 additions, -20 deletions)

View changed files

📝 src/aiCommitAssistant.js (+46 -3)
📝 src/index.js (+15 -1)
📝 test/aiCommitAssistant.simple.test.js (+40 -7)
📝 test/aiCommitAssistant.test.js (+26 -2)
📝 test/gitHistoryRewriter.test.js (+22 -7)

📄 Description

Summary

 Fixes a critical bug where the CLI would fail with API key validation errors even for non-AI commands like `redate` or `edit-message`.

 ## Problem
 When a user set `GOOGLE_API_KEY` environment variable and ran any gctm command, they would get:

[ERROR] API key validation failed:
• OpenAI API key format invalid. Expected: sk-... (48+ chars)

 This happened because:
 1. `AICommitAssistant` was always instantiated in `GitCommitTimeMachine` constructor
 2. The provider defaulted to `openai` regardless of which API key was set
 3. Validation ran immediately, checking Google's `AIza...` key against OpenAI's `sk-...` format

 ## Solution

 ### 1. Auto-detect provider from environment variables
 Added `detectProviderFromEnv()` method that checks environment variables in order:
 - `OPENAI_API_KEY` → openai
 - `ANTHROPIC_API_KEY` → anthropic
 - `GOOGLE_API_KEY` → google
 - `OLLAMA_URL` → local
 - No key → local (default)

 ### 2. Lazy initialization of AI assistant
 Changed `AICommitAssistant` from eager to lazy initialization using a getter. Now it's only created when AI features are actually used, preventing validation errors for non-AI commands.

 ## Changes
 - `src/aiCommitAssistant.js`: Added `detectProviderFromEnv()` and `getApiKeyForProvider()` methods
 - `src/index.js`: Changed to lazy initialization with getter
 - `test/*.test.js`: Updated tests to clear environment variables and expect `local` as default provider

 ## Testing
 - All 512 tests passing
 - Manually verified `gctm redate --interactive` works with `GOOGLE_API_KEY` set
 - Manually verified `gctm ai-config --show` correctly detects provider as `google`

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ersinkoc/git-commit-time-machine/pull/12 **Author:** [@KilimcininKorOglu](https://github.com/KilimcininKorOglu) **Created:** 12/18/2025 **Status:** ✅ Merged **Merged:** 12/18/2025 **Merged by:** [@ersinkoc](https://github.com/ersinkoc) **Base:** `main` ← **Head:** `main` --- ### 📝 Commits (2) - [`ed64489`](https://github.com/ersinkoc/git-commit-time-machine/commit/ed644896a778a83a149c415c613a90b8184c4b72) fix: auto-detect AI provider from environment variables and lazy initialize AI assistant - [`e1bd20e`](https://github.com/ersinkoc/git-commit-time-machine/commit/e1bd20ef2c95bfae22d5e46d7bfed51bae47d10d) test: update tests to support auto-detected local provider as default ### 📊 Changes **5 files changed** (+149 additions, -20 deletions) <details> <summary>View changed files</summary> 📝 `src/aiCommitAssistant.js` (+46 -3) 📝 `src/index.js` (+15 -1) 📝 `test/aiCommitAssistant.simple.test.js` (+40 -7) 📝 `test/aiCommitAssistant.test.js` (+26 -2) 📝 `test/gitHistoryRewriter.test.js` (+22 -7) </details> ### 📄 Description ## Summary Fixes a critical bug where the CLI would fail with API key validation errors even for non-AI commands like `redate` or `edit-message`. ## Problem When a user set `GOOGLE_API_KEY` environment variable and ran any gctm command, they would get: [ERROR] API key validation failed: • OpenAI API key format invalid. Expected: sk-... (48+ chars) This happened because: 1. `AICommitAssistant` was always instantiated in `GitCommitTimeMachine` constructor 2. The provider defaulted to `openai` regardless of which API key was set 3. Validation ran immediately, checking Google's `AIza...` key against OpenAI's `sk-...` format ## Solution ### 1. Auto-detect provider from environment variables Added `detectProviderFromEnv()` method that checks environment variables in order: - `OPENAI_API_KEY` → openai - `ANTHROPIC_API_KEY` → anthropic - `GOOGLE_API_KEY` → google - `OLLAMA_URL` → local - No key → local (default) ### 2. Lazy initialization of AI assistant Changed `AICommitAssistant` from eager to lazy initialization using a getter. Now it's only created when AI features are actually used, preventing validation errors for non-AI commands. ## Changes - `src/aiCommitAssistant.js`: Added `detectProviderFromEnv()` and `getApiKeyForProvider()` methods - `src/index.js`: Changed to lazy initialization with getter - `test/*.test.js`: Updated tests to clear environment variables and expect `local` as default provider ## Testing - All 512 tests passing - Manually verified `gctm redate --interactive` works with `GOOGLE_API_KEY` set - Manually verified `gctm ai-config --show` correctly detects provider as `google` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem closed this issue 2026-03-04 14:09:22 +03:00
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/git-commit-time-machine#11
No description provided.