-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(web): unify AI providers behind OpenAI-compatible config (Ollama, Whisper, etc.) #1877
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kovashikawa
wants to merge
7
commits into
CapSoftware:main
Choose a base branch
from
kovashikawa:feat/openai-compatible-ai-providers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4023507
feat(env): add AI_*/STT_* env vars for OpenAI-compatible providers
kovashikawa 92c9f51
refactor(web): unify chat AI behind OpenAI-compatible client
kovashikawa ba17a06
feat(web): OpenAI-compatible STT + widen self-host AI gates
kovashikawa 4347a39
chore(docker): expose AI/STT env vars through compose files
kovashikawa 0b0f372
fix(env): require AI_API_KEY/AI_MODEL with AI_BASE_URL; validate URL …
kovashikawa e46a8cb
fix(web): harden ai-provider client, tighten STT/JSON handling, dedup…
kovashikawa 24e45ec
fix(web): restore Groq -> OpenAI failover when both keys configured
kovashikawa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import "server-only"; | ||
|
|
||
| import { serverEnv } from "@cap/env"; | ||
| import OpenAI from "openai"; | ||
|
|
||
| const GROQ_DEFAULT_BASE_URL = "https://api.groq.com/openai/v1"; | ||
| const GROQ_DEFAULT_MODEL = "openai/gpt-oss-120b"; | ||
| const OPENAI_DEFAULT_MODEL = "gpt-4o-mini"; | ||
| const OPENAI_WHISPER_DEFAULT_MODEL = "whisper-1"; | ||
|
|
||
| const AI_TIMEOUT_MS = 120_000; | ||
| const STT_TIMEOUT_MS = 300_000; | ||
| const MAX_RETRIES = 2; | ||
|
|
||
| export function isAiConfigured(): boolean { | ||
| const env = serverEnv(); | ||
| return Boolean(env.AI_BASE_URL || env.GROQ_API_KEY || env.OPENAI_API_KEY); | ||
| } | ||
|
|
||
| export function isSttConfigured(): boolean { | ||
| const env = serverEnv(); | ||
| return Boolean(env.STT_BASE_URL || env.DEEPGRAM_API_KEY); | ||
| } | ||
|
|
||
| export function getAiClient(): OpenAI | null { | ||
| const env = serverEnv(); | ||
|
|
||
| if (env.AI_BASE_URL) { | ||
| if (!env.AI_API_KEY) { | ||
| throw new Error( | ||
| "AI_API_KEY is required when AI_BASE_URL is set. Use any non-empty string for local providers that ignore auth.", | ||
| ); | ||
| } | ||
| if (!env.AI_MODEL) { | ||
| throw new Error("AI_MODEL is required when AI_BASE_URL is set."); | ||
| } | ||
| return new OpenAI({ | ||
| baseURL: env.AI_BASE_URL, | ||
| apiKey: env.AI_API_KEY, | ||
| timeout: AI_TIMEOUT_MS, | ||
| maxRetries: MAX_RETRIES, | ||
| }); | ||
| } | ||
|
|
||
| if (env.GROQ_API_KEY) { | ||
| return new OpenAI({ | ||
| baseURL: GROQ_DEFAULT_BASE_URL, | ||
| apiKey: env.GROQ_API_KEY, | ||
| timeout: AI_TIMEOUT_MS, | ||
| maxRetries: MAX_RETRIES, | ||
| }); | ||
| } | ||
|
|
||
| if (env.OPENAI_API_KEY) { | ||
| return new OpenAI({ | ||
| apiKey: env.OPENAI_API_KEY, | ||
| timeout: AI_TIMEOUT_MS, | ||
| maxRetries: MAX_RETRIES, | ||
| }); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| export function getAiFallbackClient(): { | ||
| client: OpenAI; | ||
| model: string; | ||
| } | null { | ||
| const env = serverEnv(); | ||
| if (env.AI_BASE_URL) return null; | ||
| if (!env.GROQ_API_KEY || !env.OPENAI_API_KEY) return null; | ||
| return { | ||
| client: new OpenAI({ | ||
| apiKey: env.OPENAI_API_KEY, | ||
| timeout: AI_TIMEOUT_MS, | ||
| maxRetries: MAX_RETRIES, | ||
| }), | ||
| model: OPENAI_DEFAULT_MODEL, | ||
| }; | ||
| } | ||
|
|
||
| export function getAiModel(): string { | ||
| const env = serverEnv(); | ||
| if (env.AI_BASE_URL) { | ||
| if (!env.AI_MODEL) { | ||
| throw new Error("AI_MODEL is required when AI_BASE_URL is set."); | ||
| } | ||
| return env.AI_MODEL; | ||
| } | ||
| if (env.GROQ_API_KEY) return GROQ_DEFAULT_MODEL; | ||
| return OPENAI_DEFAULT_MODEL; | ||
| } | ||
|
|
||
| export function getSttClient(): OpenAI | null { | ||
| const env = serverEnv(); | ||
| if (!env.STT_BASE_URL) return null; | ||
|
|
||
| if (!env.STT_API_KEY) { | ||
| throw new Error( | ||
| "STT_API_KEY is required when STT_BASE_URL is set. Use any non-empty string for local providers that ignore auth.", | ||
| ); | ||
| } | ||
| if (!env.STT_MODEL) { | ||
| throw new Error("STT_MODEL is required when STT_BASE_URL is set."); | ||
| } | ||
|
|
||
| return new OpenAI({ | ||
| baseURL: env.STT_BASE_URL, | ||
| apiKey: env.STT_API_KEY, | ||
| timeout: STT_TIMEOUT_MS, | ||
| maxRetries: MAX_RETRIES, | ||
| }); | ||
| } | ||
|
|
||
| export function getSttModel(): string { | ||
| const env = serverEnv(); | ||
| if (env.STT_BASE_URL) { | ||
| if (!env.STT_MODEL) { | ||
| throw new Error("STT_MODEL is required when STT_BASE_URL is set."); | ||
| } | ||
| return env.STT_MODEL; | ||
| } | ||
| return OPENAI_WHISPER_DEFAULT_MODEL; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.