@yuva-devlab/ai-client 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -164,42 +164,36 @@ var MissingCredentialError = class extends errors.DomainError {
164
164
  };
165
165
 
166
166
  // src/config/env-resolver.ts
167
- function safeGetEnv(key) {
168
- if (typeof process !== "undefined" && process?.env) {
169
- return process.env[key];
170
- }
171
- return void 0;
172
- }
173
- function resolveEnvCredentials(provider) {
167
+ function resolveEnvCredentials(provider, env = {}) {
174
168
  const norm = provider.toLowerCase();
175
169
  switch (norm) {
176
170
  case "ollama":
177
171
  return {
178
- baseUrl: safeGetEnv("OLLAMA_BASE_URL") ?? AI_ENDPOINTS.OLLAMA_DEFAULT_BASE_URL,
179
- defaultModel: safeGetEnv("OLLAMA_MODEL") ?? AI_DEFAULT_MODELS.OLLAMA
172
+ baseUrl: env.OLLAMA_BASE_URL ?? AI_ENDPOINTS.OLLAMA_DEFAULT_BASE_URL,
173
+ defaultModel: env.OLLAMA_MODEL ?? AI_DEFAULT_MODELS.OLLAMA
180
174
  };
181
175
  case "groq":
182
176
  return {
183
- apiKey: safeGetEnv("GROQ_API_KEY"),
184
- baseUrl: safeGetEnv("GROQ_BASE_URL") ?? AI_ENDPOINTS.GROQ_DEFAULT_BASE_URL,
185
- defaultModel: safeGetEnv("GROQ_MODEL") ?? AI_DEFAULT_MODELS.GROQ
177
+ apiKey: env.GROQ_API_KEY,
178
+ baseUrl: env.GROQ_BASE_URL ?? AI_ENDPOINTS.GROQ_DEFAULT_BASE_URL,
179
+ defaultModel: env.GROQ_MODEL ?? AI_DEFAULT_MODELS.GROQ
186
180
  };
187
181
  case "google":
188
182
  return {
189
- apiKey: safeGetEnv("GEMINI_API_KEY") ?? safeGetEnv("GOOGLE_AI_API_KEY"),
190
- defaultModel: safeGetEnv("GEMINI_MODEL") ?? AI_DEFAULT_MODELS.GOOGLE
183
+ apiKey: env.GEMINI_API_KEY ?? env.GOOGLE_AI_API_KEY,
184
+ defaultModel: env.GEMINI_MODEL ?? AI_DEFAULT_MODELS.GOOGLE
191
185
  };
192
186
  case "openai":
193
187
  return {
194
- apiKey: safeGetEnv("OPENAI_API_KEY"),
195
- baseUrl: safeGetEnv("OPENAI_BASE_URL") ?? AI_ENDPOINTS.OPENAI_DEFAULT_BASE_URL,
196
- defaultModel: safeGetEnv("OPENAI_MODEL") ?? AI_DEFAULT_MODELS.OPENAI
188
+ apiKey: env.OPENAI_API_KEY,
189
+ baseUrl: env.OPENAI_BASE_URL ?? AI_ENDPOINTS.OPENAI_DEFAULT_BASE_URL,
190
+ defaultModel: env.OPENAI_MODEL ?? AI_DEFAULT_MODELS.OPENAI
197
191
  };
198
192
  case "anthropic":
199
193
  return {
200
- apiKey: safeGetEnv("ANTHROPIC_API_KEY"),
201
- baseUrl: safeGetEnv("ANTHROPIC_BASE_URL") ?? AI_ENDPOINTS.ANTHROPIC_DEFAULT_BASE_URL,
202
- defaultModel: safeGetEnv("ANTHROPIC_MODEL") ?? AI_DEFAULT_MODELS.ANTHROPIC
194
+ apiKey: env.ANTHROPIC_API_KEY,
195
+ baseUrl: env.ANTHROPIC_BASE_URL ?? AI_ENDPOINTS.ANTHROPIC_DEFAULT_BASE_URL,
196
+ defaultModel: env.ANTHROPIC_MODEL ?? AI_DEFAULT_MODELS.ANTHROPIC
203
197
  };
204
198
  default:
205
199
  return {};
@@ -237,9 +231,8 @@ var OllamaAdapter = class {
237
231
  */
238
232
  async resolveCredentials() {
239
233
  const dynamic = this.config.credentials ? await this.config.credentials() : {};
240
- const env = resolveEnvCredentials(this.name);
241
- const baseUrl = (this.config.baseUrl ?? dynamic.baseUrl ?? env.baseUrl ?? AI_ENDPOINTS.OLLAMA_DEFAULT_BASE_URL).replace(/\/+$/, "");
242
- const defaultModel = this.config.defaultModel ?? dynamic.defaultModel ?? env.defaultModel ?? AI_DEFAULT_MODELS.OLLAMA;
234
+ const baseUrl = (this.config.baseUrl ?? dynamic.baseUrl ?? AI_ENDPOINTS.OLLAMA_DEFAULT_BASE_URL).replace(/\/+$/, "");
235
+ const defaultModel = this.config.defaultModel ?? dynamic.defaultModel ?? AI_DEFAULT_MODELS.OLLAMA;
243
236
  return { baseUrl, defaultModel };
244
237
  }
245
238
  /**
@@ -413,12 +406,11 @@ var GroqAdapter = class {
413
406
  */
414
407
  async resolveCredentials() {
415
408
  const dynamic = this.config.credentials ? await this.config.credentials() : {};
416
- const env = resolveEnvCredentials(this.name);
417
- const apiKey = this.config.apiKey ?? dynamic.apiKey ?? env.apiKey;
418
- const baseUrl = (this.config.baseUrl ?? dynamic.baseUrl ?? env.baseUrl ?? AI_ENDPOINTS.GROQ_DEFAULT_BASE_URL).replace(/\/+$/, "");
419
- const defaultModel = this.config.defaultModel ?? dynamic.defaultModel ?? env.defaultModel ?? AI_DEFAULT_MODELS.GROQ;
409
+ const apiKey = this.config.apiKey ?? dynamic.apiKey;
410
+ const baseUrl = (this.config.baseUrl ?? dynamic.baseUrl ?? AI_ENDPOINTS.GROQ_DEFAULT_BASE_URL).replace(/\/+$/, "");
411
+ const defaultModel = this.config.defaultModel ?? dynamic.defaultModel ?? AI_DEFAULT_MODELS.GROQ;
420
412
  if (!apiKey) {
421
- throw new MissingCredentialError("Groq", "GROQ_API_KEY");
413
+ throw new MissingCredentialError("Groq", "apiKey");
422
414
  }
423
415
  return { apiKey, baseUrl, defaultModel };
424
416
  }
@@ -601,12 +593,12 @@ var GoogleAdapter = class {
601
593
  */
602
594
  async resolveCredentials() {
603
595
  const dynamic = this.config.credentials ? await this.config.credentials() : {};
604
- const env = resolveEnvCredentials(this.name);
605
- const apiKey = this.config.apiKey ?? dynamic.apiKey ?? env.apiKey;
606
- const baseUrl = (this.config.baseUrl ?? dynamic.baseUrl ?? env.baseUrl ?? AI_ENDPOINTS.GOOGLE_GEMINI_DEFAULT_BASE_URL).replace(/\/+$/, "");
607
- const defaultModel = this.config.defaultModel ?? dynamic.defaultModel ?? env.defaultModel ?? "gemini-2.5-flash";
596
+ const apiKey = this.config.apiKey ?? dynamic.apiKey;
597
+ const rawBaseUrl = (this.config.baseUrl ?? dynamic.baseUrl ?? AI_ENDPOINTS.GOOGLE_GEMINI_DEFAULT_BASE_URL).replace(/\/+$/, "");
598
+ const baseUrl = rawBaseUrl.endsWith("/v1beta") ? rawBaseUrl : `${rawBaseUrl}/v1beta`;
599
+ const defaultModel = this.config.defaultModel ?? dynamic.defaultModel ?? "gemini-3.8-flash";
608
600
  if (!apiKey) {
609
- throw new MissingCredentialError("Google Gemini", "GEMINI_API_KEY");
601
+ throw new MissingCredentialError("Google Gemini", "apiKey");
610
602
  }
611
603
  return { apiKey, baseUrl, defaultModel };
612
604
  }
@@ -686,11 +678,24 @@ var GoogleAdapter = class {
686
678
  const { apiKey, baseUrl, defaultModel } = await this.resolveCredentials();
687
679
  const model = request.model || defaultModel;
688
680
  const url = `${baseUrl}/models/${model}:streamGenerateContent?alt=sse&key=${apiKey}`;
689
- const response = await fetch(url, {
681
+ let response = await fetch(url, {
690
682
  method: "POST",
691
683
  headers: { "Content-Type": "application/json" },
692
- body: JSON.stringify(buildGeminiRequestBody(request))
693
- });
684
+ body: JSON.stringify(buildGeminiRequestBody(request)),
685
+ signal: AbortSignal.timeout(6e3)
686
+ }).catch(() => null);
687
+ if (!response || !response.ok && (response.status === 503 || response.status === 404)) {
688
+ logger3.warn("[stream] Primary model unavailable, using fallback", {
689
+ model,
690
+ status: response?.status
691
+ });
692
+ const fallbackUrl = `${baseUrl}/models/gemini-3.1-flash-lite:streamGenerateContent?alt=sse&key=${apiKey}`;
693
+ response = await fetch(fallbackUrl, {
694
+ method: "POST",
695
+ headers: { "Content-Type": "application/json" },
696
+ body: JSON.stringify(buildGeminiRequestBody(request))
697
+ });
698
+ }
694
699
  if (!response.ok || !response.body) {
695
700
  throw new errors.DomainError(
696
701
  `Gemini streaming error HTTP ${response.status}`,
@@ -752,12 +757,11 @@ var OpenAIAdapter = class {
752
757
  */
753
758
  async resolveCredentials() {
754
759
  const dynamic = this.config.credentials ? await this.config.credentials() : {};
755
- const env = resolveEnvCredentials(this.name);
756
- const apiKey = this.config.apiKey ?? dynamic.apiKey ?? env.apiKey;
757
- const baseUrl = (this.config.baseUrl ?? dynamic.baseUrl ?? env.baseUrl ?? AI_ENDPOINTS.OPENAI_DEFAULT_BASE_URL).replace(/\/+$/, "");
758
- const defaultModel = this.config.defaultModel ?? dynamic.defaultModel ?? env.defaultModel ?? AI_DEFAULT_MODELS.OPENAI;
760
+ const apiKey = this.config.apiKey ?? dynamic.apiKey;
761
+ const baseUrl = (this.config.baseUrl ?? dynamic.baseUrl ?? AI_ENDPOINTS.OPENAI_DEFAULT_BASE_URL).replace(/\/+$/, "");
762
+ const defaultModel = this.config.defaultModel ?? dynamic.defaultModel ?? AI_DEFAULT_MODELS.OPENAI;
759
763
  if (!apiKey) {
760
- throw new MissingCredentialError("OpenAI", "OPENAI_API_KEY");
764
+ throw new MissingCredentialError("OpenAI", "apiKey");
761
765
  }
762
766
  return { apiKey, baseUrl, defaultModel };
763
767
  }
@@ -932,12 +936,11 @@ var AnthropicAdapter = class {
932
936
  */
933
937
  async resolveCredentials() {
934
938
  const dynamic = this.config.credentials ? await this.config.credentials() : {};
935
- const env = resolveEnvCredentials(this.name);
936
- const apiKey = this.config.apiKey ?? dynamic.apiKey ?? env.apiKey;
937
- const baseUrl = (this.config.baseUrl ?? dynamic.baseUrl ?? env.baseUrl ?? AI_ENDPOINTS.ANTHROPIC_DEFAULT_BASE_URL).replace(/\/+$/, "");
938
- const defaultModel = this.config.defaultModel ?? dynamic.defaultModel ?? env.defaultModel ?? AI_DEFAULT_MODELS.ANTHROPIC;
939
+ const apiKey = this.config.apiKey ?? dynamic.apiKey;
940
+ const baseUrl = (this.config.baseUrl ?? dynamic.baseUrl ?? AI_ENDPOINTS.ANTHROPIC_DEFAULT_BASE_URL).replace(/\/+$/, "");
941
+ const defaultModel = this.config.defaultModel ?? dynamic.defaultModel ?? AI_DEFAULT_MODELS.ANTHROPIC;
939
942
  if (!apiKey) {
940
- throw new MissingCredentialError("Anthropic", "ANTHROPIC_API_KEY");
943
+ throw new MissingCredentialError("Anthropic", "apiKey");
941
944
  }
942
945
  return { apiKey, baseUrl, defaultModel };
943
946
  }
@@ -1202,32 +1205,38 @@ function createProvider(type, config = {}) {
1202
1205
  );
1203
1206
  }
1204
1207
  }
1205
- function createCascadeCandidates() {
1208
+ function createCascadeCandidates(config = {}) {
1206
1209
  const candidates = [];
1207
- candidates.push(new OllamaAdapter());
1208
- if (typeof process !== "undefined" && process?.env?.GROQ_API_KEY) {
1209
- candidates.push(new GroqAdapter());
1210
+ if (config.ollama) {
1211
+ candidates.push(new OllamaAdapter(config.ollama));
1210
1212
  }
1211
- if (typeof process !== "undefined" && (process?.env?.GEMINI_API_KEY || process?.env?.GOOGLE_AI_API_KEY)) {
1212
- candidates.push(new GoogleAdapter());
1213
+ if (config.groq) {
1214
+ candidates.push(new GroqAdapter(config.groq));
1213
1215
  }
1214
- if (typeof process !== "undefined" && process?.env?.OPENAI_API_KEY) {
1215
- candidates.push(new OpenAIAdapter());
1216
+ if (config.google) {
1217
+ candidates.push(new GoogleAdapter(config.google));
1216
1218
  }
1217
- if (typeof process !== "undefined" && process?.env?.ANTHROPIC_API_KEY) {
1218
- candidates.push(new AnthropicAdapter());
1219
+ if (config.openai) {
1220
+ candidates.push(new OpenAIAdapter(config.openai));
1221
+ }
1222
+ if (config.anthropic) {
1223
+ candidates.push(new AnthropicAdapter(config.anthropic));
1224
+ }
1225
+ if (candidates.length === 0) {
1226
+ candidates.push(new OllamaAdapter(config.ollama ?? {}));
1219
1227
  }
1220
1228
  return candidates;
1221
1229
  }
1222
- function createDefaultProvider() {
1223
- const envProvider = typeof process !== "undefined" ? process?.env?.AI_PROVIDER : void 0;
1224
- if (envProvider) {
1230
+ function createDefaultProvider(options = {}) {
1231
+ if (options.provider) {
1225
1232
  logger8.info(
1226
- `[createDefaultProvider] Using explicitly configured provider: ${envProvider}`
1233
+ `[createDefaultProvider] Using explicitly configured provider: ${options.provider}`
1227
1234
  );
1228
- return createProvider(envProvider);
1235
+ return createProvider(options.provider, options.config);
1229
1236
  }
1230
- return new FallbackCascade({ providers: createCascadeCandidates() });
1237
+ return new FallbackCascade({
1238
+ providers: createCascadeCandidates(options.cascade)
1239
+ });
1231
1240
  }
1232
1241
 
1233
1242
  exports.AIMessageSchema = AIMessageSchema;