@umituz/web-ai 1.0.1 → 1.0.2

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.d.cts CHANGED
@@ -1,11 +1,12 @@
1
1
  export { C as ChatMessage, F as FinishReason, G as GenerationResult, P as ProviderHealth, S as StreamCallbacks, a as StructuredGeneration, b as StructuredTextRequest, T as TextRequest, c as TextTier, d as TokenUsage } from './index-fXjxTR5W.cjs';
2
- export { A as AIError, a as AIErrorMetadata, b as AIErrorType, T as TEXT_TIERS, c as TierModelMap, U as USER_FRIENDLY_ERROR_MESSAGES, i as isTextTier, m as mergeTierOverrides, r as resolveModel } from './index-DKHNcMbl.cjs';
3
- export { AIErrorBody, ApiKeyValidationRule, CompositeExecutor, DEFAULT_API_KEY_RULE, DEFAULT_ERROR_CLASSIFICATION_RULE, DEFAULT_HTTP_STATUS_RULE, DEFAULT_RETRY_BACKOFF_MULTIPLIER, DEFAULT_RETRY_INITIAL_DELAY_MS, DEFAULT_RETRY_JITTER_FACTOR, DEFAULT_RETRY_MAX_ATTEMPTS, DEFAULT_RETRY_MAX_DELAY_MS, DebounceManager, DebounceOptions, DedupeExecutor, DedupeExecutorOptions, ErrorClassificationRule, HttpStatusClassificationRule, IRequestExecutor, JSON_CODE_BLOCK_PATTERN, JSON_OBJECT_PATTERN, JsonResponseParser, LoggedError, NormalizedSseChunk, QueueExecutor, RetryExecutor, SSE_BRIDGE_EVENTS, SSE_DONE_MARKER, SSE_PREFIX, SseDeltaExtractor, SseResponseOptions, StreamingParserCallbacks, StreamingResponseParser, StructuredPromptBuilder, ThrottleManager, ThrottleOptions, asObjectArray, asRecord, asStringArray, asTrimmedOptionalString, buildCacheKey, buildHttpStatusFallback, clampScore, classifyHttpStatus, createSseResponse, debounce, debounceManager, describeAIErrorType, extractErrorMessageFromJsonBody, extractErrorMessageFromResponse, extractJsonArray, extractJsonCandidate, extractJsonFromCodeFence, extractJsonObject, extractSseDataPayload, formatErrorForLogging, isAuthErrorType, isRetryableErrorType, isSseDataLine, isSseDoneMarker, isValidApiKey, normalizeSseChunk, oneOfCatalog, repairCommonJsonDefects, resolveUserFriendlyErrorMessage, resolveUserFriendlyMessage, splitSseLines, throttle, throttleManager, validateApiKey } from './core/index.cjs';
2
+ export { A as AIError, a as AIErrorMetadata, b as AIErrorType, b as GroqErrorType, T as TEXT_TIERS, c as TierModelMap, U as USER_FRIENDLY_ERROR_MESSAGES, i as isTextTier, m as mergeTierOverrides, r as resolveModel } from './index-DKHNcMbl.cjs';
3
+ export { AIErrorBody, ApiKeyValidationRule, CompositeExecutor, DEFAULT_API_KEY_RULE, DEFAULT_ERROR_CLASSIFICATION_RULE, DEFAULT_HTTP_STATUS_RULE, DEFAULT_RETRY_BACKOFF_MULTIPLIER, DEFAULT_RETRY_INITIAL_DELAY_MS, DEFAULT_RETRY_JITTER_FACTOR, DEFAULT_RETRY_MAX_ATTEMPTS, DEFAULT_RETRY_MAX_DELAY_MS, DebounceManager, DebounceOptions, DedupeExecutor, DedupeExecutorOptions, ErrorClassificationRule, GroqError, GroqErrorMetadata, HttpStatusClassificationRule, IRequestExecutor, JSON_CODE_BLOCK_PATTERN, JSON_OBJECT_PATTERN, JsonResponseParser, LoggedError, NormalizedSseChunk, QueueExecutor, RetryExecutor, SSE_BRIDGE_EVENTS, SSE_DONE_MARKER, SSE_PREFIX, SseDeltaExtractor, SseResponseOptions, StreamingParserCallbacks, StreamingResponseParser, StructuredPromptBuilder, ThrottleManager, ThrottleOptions, asObjectArray, asRecord, asStringArray, asTrimmedOptionalString, buildCacheKey, buildHttpStatusFallback, clampScore, classifyHttpStatus, createSseResponse, debounce, debounceManager, describeAIErrorType, extractErrorMessageFromJsonBody, extractErrorMessageFromResponse, extractJsonArray, extractJsonCandidate, extractJsonFromCodeFence, extractJsonObject, extractSseDataPayload, formatErrorForLogging, isAuthErrorType, isRetryableErrorType, isSseDataLine, isSseDoneMarker, isValidApiKey, normalizeSseChunk, oneOfCatalog, repairCommonJsonDefects, resolveUserFriendlyErrorMessage, resolveUserFriendlyMessage, splitSseLines, throttle, throttleManager, validateApiKey } from './core/index.cjs';
4
4
  export { C as CacheManager, a as CacheOptions, b as CacheStats, c as cacheManager } from './cache-manager-Df9LLYGA.cjs';
5
5
  export { Q as QueueOptions, R as RequestDeduplicator, a as RequestQueue, b as RetryManager, c as RetryOptions, r as requestDeduplicator, d as requestQueue, e as retryManager, w as withRetry } from './request-deduplicator.util-NDo0_n1y.cjs';
6
6
  export { A as AnyProvider, C as CostEstimateRequest, I as IMediaProvider, a as IProviderBase, b as ITextProvider, P as ProviderCapability, c as ProviderFactory, d as ProviderFactoryConfig, i as isMediaProvider, e as isTextProvider } from './provider.factory-BxsfK4WW.cjs';
7
- export { AnthropicHttpTransport, AnthropicProvider, AnthropicRequestBuilder, BaseProvider, BaseProviderConfig, FalProvider, GEMINI_TIERS, GROQ_MODELS, GROQ_TIERS, GeminiProvider, GroqChatRequestBuilder, GroqHttpClient, GroqHttpTransport, GroqProvider, ProviderError, PrunaProgressStage, PrunaProvider, PrunaRequest, consumeAnthropicStream, consumeGroqStream, createFalProvider, createGeminiProvider, createPrunaProvider, defaultEffortForModel, extractContentFromSsePayload, generateWithPruna, mapAnthropicStopReason, toGroqMessages } from './providers/index.cjs';
8
- export { A as AIProviderConfig, a as ANTHROPIC_API_VERSION, b as ANTHROPIC_DEFAULT_MAX_TOKENS, c as ANTHROPIC_MODELS, d as ANTHROPIC_TIERS, e as AnthropicEffort, f as AnthropicMessagesRequest, g as AnthropicMessagesResponse, h as AnthropicProviderConfig, i as AnthropicStopReason, j as AnthropicStreamEvent, G as GroqChatChunk, k as GroqChatRequest, l as GroqChatResponse, m as GroqMessage, n as GroqProviderConfig, o as createAI } from './create-ai-DOI9jD3v.cjs';
7
+ export { API_ENDPOINTS, AnthropicHttpTransport, AnthropicProvider, AnthropicRequestBuilder, AppContainer, BaseProvider, BaseProviderConfig, DEFAULT_BASE_URL, DEFAULT_GENERATION_CONFIG, DEFAULT_MODELS, FalProvider, GEMINI_TIERS, GROQ_MODELS, GROQ_TIERS, GeminiProvider, GroqChatRequestBuilder, GroqConfigStore, GroqHttpClient, GroqHttpClientDeps, GroqHttpTransport, GroqProvider, ProviderError, PrunaProgressStage, PrunaProvider, PrunaRequest, StructuredJsonRequest, TIMEOUTS, configureProvider, consumeAnthropicStream, consumeGroqStream, createAssistantMessage, createFalProvider, createGeminiProvider, createMessage, createPrunaProvider, createSystemMessage, createUserMessage, defaultEffortForModel, extractContentFromSsePayload, extractTextFromMessages, formatMessagesForDisplay, generateWithPruna, getProvider, mapAnthropicStopReason, promptToMessages, requestStructuredJson, resetProvider, toGroqMessages } from './providers/index.cjs';
8
+ export { A as AIProviderConfig, a as ANTHROPIC_API_VERSION, b as ANTHROPIC_DEFAULT_MAX_TOKENS, c as ANTHROPIC_MODELS, d as ANTHROPIC_TIERS, e as AnthropicEffort, f as AnthropicMessagesRequest, g as AnthropicMessagesResponse, h as AnthropicProviderConfig, i as AnthropicStopReason, j as AnthropicStreamEvent, G as GroqChatChunk, k as GroqChatRequest, l as GroqChatResponse, m as GroqChoice, n as GroqChunkChoice, o as GroqConfig, p as GroqErrorResponse, q as GroqGenerationConfig, r as GroqMessage, s as GroqMessageRole, t as GroqProviderConfig, u as GroqReasoningEffort, v as GroqUsage, S as StreamRequestOptions, w as createAI } from './create-ai-CXYKPK6G.cjs';
9
+ export { I as IGroqChatService, a as IGroqHttpClient, S as StreamingCallbacks, b as StructuredGenerationOptions, T as TextGenerationOptions, c as TextGenerationService } from './text-generation.service-BQO3Tvz2.cjs';
9
10
  export { C as ChatConfig, a as ChatConversation, b as ChatMessageSender, c as ChatMessageType, d as ChatService, e as ChatTokenUsage, f as ConversationMessage, D as DEFAULT_PROMPT_STRATEGY, I as IChatService, g as IChatStorage, h as IMessageFormatter, S as SendMessageInput, i as SendMessageResult, j as SystemPromptStrategy, U as UseChatOptions, k as UseChatReturn, l as extractTokenUsage } from './chat.service-CL1qtYxe.cjs';
10
11
  export { ChatIdGenerator, chatIdGenerator, messageFormatter } from './chat/index.cjs';
11
12
  import './ProviderConfig-DIFa9nmG.cjs';
package/dist/index.d.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  export { C as ChatMessage, F as FinishReason, G as GenerationResult, P as ProviderHealth, S as StreamCallbacks, a as StructuredGeneration, b as StructuredTextRequest, T as TextRequest, c as TextTier, d as TokenUsage } from './index-fXjxTR5W.js';
2
- export { A as AIError, a as AIErrorMetadata, b as AIErrorType, T as TEXT_TIERS, c as TierModelMap, U as USER_FRIENDLY_ERROR_MESSAGES, i as isTextTier, m as mergeTierOverrides, r as resolveModel } from './index-DIHz8_OJ.js';
3
- export { AIErrorBody, ApiKeyValidationRule, CompositeExecutor, DEFAULT_API_KEY_RULE, DEFAULT_ERROR_CLASSIFICATION_RULE, DEFAULT_HTTP_STATUS_RULE, DEFAULT_RETRY_BACKOFF_MULTIPLIER, DEFAULT_RETRY_INITIAL_DELAY_MS, DEFAULT_RETRY_JITTER_FACTOR, DEFAULT_RETRY_MAX_ATTEMPTS, DEFAULT_RETRY_MAX_DELAY_MS, DebounceManager, DebounceOptions, DedupeExecutor, DedupeExecutorOptions, ErrorClassificationRule, HttpStatusClassificationRule, IRequestExecutor, JSON_CODE_BLOCK_PATTERN, JSON_OBJECT_PATTERN, JsonResponseParser, LoggedError, NormalizedSseChunk, QueueExecutor, RetryExecutor, SSE_BRIDGE_EVENTS, SSE_DONE_MARKER, SSE_PREFIX, SseDeltaExtractor, SseResponseOptions, StreamingParserCallbacks, StreamingResponseParser, StructuredPromptBuilder, ThrottleManager, ThrottleOptions, asObjectArray, asRecord, asStringArray, asTrimmedOptionalString, buildCacheKey, buildHttpStatusFallback, clampScore, classifyHttpStatus, createSseResponse, debounce, debounceManager, describeAIErrorType, extractErrorMessageFromJsonBody, extractErrorMessageFromResponse, extractJsonArray, extractJsonCandidate, extractJsonFromCodeFence, extractJsonObject, extractSseDataPayload, formatErrorForLogging, isAuthErrorType, isRetryableErrorType, isSseDataLine, isSseDoneMarker, isValidApiKey, normalizeSseChunk, oneOfCatalog, repairCommonJsonDefects, resolveUserFriendlyErrorMessage, resolveUserFriendlyMessage, splitSseLines, throttle, throttleManager, validateApiKey } from './core/index.js';
2
+ export { A as AIError, a as AIErrorMetadata, b as AIErrorType, b as GroqErrorType, T as TEXT_TIERS, c as TierModelMap, U as USER_FRIENDLY_ERROR_MESSAGES, i as isTextTier, m as mergeTierOverrides, r as resolveModel } from './index-DIHz8_OJ.js';
3
+ export { AIErrorBody, ApiKeyValidationRule, CompositeExecutor, DEFAULT_API_KEY_RULE, DEFAULT_ERROR_CLASSIFICATION_RULE, DEFAULT_HTTP_STATUS_RULE, DEFAULT_RETRY_BACKOFF_MULTIPLIER, DEFAULT_RETRY_INITIAL_DELAY_MS, DEFAULT_RETRY_JITTER_FACTOR, DEFAULT_RETRY_MAX_ATTEMPTS, DEFAULT_RETRY_MAX_DELAY_MS, DebounceManager, DebounceOptions, DedupeExecutor, DedupeExecutorOptions, ErrorClassificationRule, GroqError, GroqErrorMetadata, HttpStatusClassificationRule, IRequestExecutor, JSON_CODE_BLOCK_PATTERN, JSON_OBJECT_PATTERN, JsonResponseParser, LoggedError, NormalizedSseChunk, QueueExecutor, RetryExecutor, SSE_BRIDGE_EVENTS, SSE_DONE_MARKER, SSE_PREFIX, SseDeltaExtractor, SseResponseOptions, StreamingParserCallbacks, StreamingResponseParser, StructuredPromptBuilder, ThrottleManager, ThrottleOptions, asObjectArray, asRecord, asStringArray, asTrimmedOptionalString, buildCacheKey, buildHttpStatusFallback, clampScore, classifyHttpStatus, createSseResponse, debounce, debounceManager, describeAIErrorType, extractErrorMessageFromJsonBody, extractErrorMessageFromResponse, extractJsonArray, extractJsonCandidate, extractJsonFromCodeFence, extractJsonObject, extractSseDataPayload, formatErrorForLogging, isAuthErrorType, isRetryableErrorType, isSseDataLine, isSseDoneMarker, isValidApiKey, normalizeSseChunk, oneOfCatalog, repairCommonJsonDefects, resolveUserFriendlyErrorMessage, resolveUserFriendlyMessage, splitSseLines, throttle, throttleManager, validateApiKey } from './core/index.js';
4
4
  export { C as CacheManager, a as CacheOptions, b as CacheStats, c as cacheManager } from './cache-manager-Df9LLYGA.js';
5
5
  export { Q as QueueOptions, R as RequestDeduplicator, a as RequestQueue, b as RetryManager, c as RetryOptions, r as requestDeduplicator, d as requestQueue, e as retryManager, w as withRetry } from './request-deduplicator.util-NDo0_n1y.js';
6
6
  export { A as AnyProvider, C as CostEstimateRequest, I as IMediaProvider, a as IProviderBase, b as ITextProvider, P as ProviderCapability, c as ProviderFactory, d as ProviderFactoryConfig, i as isMediaProvider, e as isTextProvider } from './provider.factory-DfDfk0dK.js';
7
- export { AnthropicHttpTransport, AnthropicProvider, AnthropicRequestBuilder, BaseProvider, BaseProviderConfig, FalProvider, GEMINI_TIERS, GROQ_MODELS, GROQ_TIERS, GeminiProvider, GroqChatRequestBuilder, GroqHttpClient, GroqHttpTransport, GroqProvider, ProviderError, PrunaProgressStage, PrunaProvider, PrunaRequest, consumeAnthropicStream, consumeGroqStream, createFalProvider, createGeminiProvider, createPrunaProvider, defaultEffortForModel, extractContentFromSsePayload, generateWithPruna, mapAnthropicStopReason, toGroqMessages } from './providers/index.js';
8
- export { A as AIProviderConfig, a as ANTHROPIC_API_VERSION, b as ANTHROPIC_DEFAULT_MAX_TOKENS, c as ANTHROPIC_MODELS, d as ANTHROPIC_TIERS, e as AnthropicEffort, f as AnthropicMessagesRequest, g as AnthropicMessagesResponse, h as AnthropicProviderConfig, i as AnthropicStopReason, j as AnthropicStreamEvent, G as GroqChatChunk, k as GroqChatRequest, l as GroqChatResponse, m as GroqMessage, n as GroqProviderConfig, o as createAI } from './create-ai-DXEoke8-.js';
7
+ export { API_ENDPOINTS, AnthropicHttpTransport, AnthropicProvider, AnthropicRequestBuilder, AppContainer, BaseProvider, BaseProviderConfig, DEFAULT_BASE_URL, DEFAULT_GENERATION_CONFIG, DEFAULT_MODELS, FalProvider, GEMINI_TIERS, GROQ_MODELS, GROQ_TIERS, GeminiProvider, GroqChatRequestBuilder, GroqConfigStore, GroqHttpClient, GroqHttpClientDeps, GroqHttpTransport, GroqProvider, ProviderError, PrunaProgressStage, PrunaProvider, PrunaRequest, StructuredJsonRequest, TIMEOUTS, configureProvider, consumeAnthropicStream, consumeGroqStream, createAssistantMessage, createFalProvider, createGeminiProvider, createMessage, createPrunaProvider, createSystemMessage, createUserMessage, defaultEffortForModel, extractContentFromSsePayload, extractTextFromMessages, formatMessagesForDisplay, generateWithPruna, getProvider, mapAnthropicStopReason, promptToMessages, requestStructuredJson, resetProvider, toGroqMessages } from './providers/index.js';
8
+ export { A as AIProviderConfig, a as ANTHROPIC_API_VERSION, b as ANTHROPIC_DEFAULT_MAX_TOKENS, c as ANTHROPIC_MODELS, d as ANTHROPIC_TIERS, e as AnthropicEffort, f as AnthropicMessagesRequest, g as AnthropicMessagesResponse, h as AnthropicProviderConfig, i as AnthropicStopReason, j as AnthropicStreamEvent, G as GroqChatChunk, k as GroqChatRequest, l as GroqChatResponse, m as GroqChoice, n as GroqChunkChoice, o as GroqConfig, p as GroqErrorResponse, q as GroqGenerationConfig, r as GroqMessage, s as GroqMessageRole, t as GroqProviderConfig, u as GroqReasoningEffort, v as GroqUsage, S as StreamRequestOptions, w as createAI } from './create-ai-9PW752z8.js';
9
+ export { I as IGroqChatService, a as IGroqHttpClient, S as StreamingCallbacks, b as StructuredGenerationOptions, T as TextGenerationOptions, c as TextGenerationService } from './text-generation.service-CcYQM6Ch.js';
9
10
  export { C as ChatConfig, a as ChatConversation, b as ChatMessageSender, c as ChatMessageType, d as ChatService, e as ChatTokenUsage, f as ConversationMessage, D as DEFAULT_PROMPT_STRATEGY, I as IChatService, g as IChatStorage, h as IMessageFormatter, S as SendMessageInput, i as SendMessageResult, j as SystemPromptStrategy, U as UseChatOptions, k as UseChatReturn, l as extractTokenUsage } from './chat.service-q05GnhEm.js';
10
11
  export { ChatIdGenerator, chatIdGenerator, messageFormatter } from './chat/index.js';
11
12
  import './ProviderConfig-DIFa9nmG.js';
package/dist/index.js CHANGED
@@ -130,6 +130,14 @@ var AIError = class _AIError extends Error {
130
130
  }
131
131
  };
132
132
 
133
+ // src/core/errors/groq-error.ts
134
+ var GroqError = class extends AIError {
135
+ constructor(type, message, cause, metadata = {}) {
136
+ super(type, message, cause, metadata);
137
+ this.name = "GroqError";
138
+ }
139
+ };
140
+
133
141
  // src/core/errors/error-classifier.ts
134
142
  var DEFAULT_RETRYABLE_TYPES = /* @__PURE__ */ new Set([
135
143
  "NETWORK_ERROR" /* NETWORK_ERROR */,
@@ -408,6 +416,8 @@ function calculateExponentialBackoff(params) {
408
416
  // src/core/constants/identifier.constants.ts
409
417
  var MESSAGE_ID_PREFIX = "msg-";
410
418
  var CHAT_MESSAGE_ID_SUFFIX_LENGTH = 9;
419
+ var CACHE_KEY_TYPE_COMPLETION = "completion";
420
+ var CACHE_KEY_TYPE_CHAT = "chat";
411
421
  var USER_ROLE = "user";
412
422
  var ASSISTANT_ROLE = "assistant";
413
423
  var QUEUE_PRIORITY_USER_REQUEST = 10;
@@ -1879,12 +1889,22 @@ var DEFAULT_GENERATION_CONFIG = {
1879
1889
  maxTokens: 1024,
1880
1890
  topP: 1
1881
1891
  };
1892
+ var DEFAULT_MODELS = {
1893
+ TEXT: GROQ_MODELS.LLAMA_3_1_8B_INSTANT,
1894
+ FAST: GROQ_MODELS.LLAMA_3_1_8B_INSTANT,
1895
+ EXPERIMENTAL: GROQ_MODELS.GPT_OSS_20B
1896
+ };
1882
1897
  var GROQ_BASE_URL = "https://api.groq.com/openai/v1";
1898
+ var DEFAULT_BASE_URL = GROQ_BASE_URL;
1883
1899
  var CHAT_COMPLETIONS_PATH = "/chat/completions";
1900
+ var API_ENDPOINTS = {
1901
+ CHAT: CHAT_COMPLETIONS_PATH
1902
+ };
1884
1903
  var GROQ_TIMEOUTS = {
1885
1904
  DEFAULT: 3e4,
1886
1905
  STREAMING: 6e4
1887
1906
  };
1907
+ var TIMEOUTS = GROQ_TIMEOUTS;
1888
1908
  var HTTP_HEADERS = {
1889
1909
  CONTENT_TYPE: "Content-Type",
1890
1910
  APPLICATION_JSON: "application/json",
@@ -1943,13 +1963,65 @@ var GroqChatRequestBuilder = class _GroqChatRequestBuilder {
1943
1963
  }
1944
1964
  };
1945
1965
 
1966
+ // src/providers/groq/groq-config-store.ts
1967
+ var GroqConfigStore = class {
1968
+ config = null;
1969
+ initialize(config) {
1970
+ const validation = validateApiKey(config.apiKey, { minLength: 1 });
1971
+ if (!validation.valid) {
1972
+ throw new GroqError("INVALID_API_KEY" /* INVALID_API_KEY */, validation.reason ?? "Invalid API key");
1973
+ }
1974
+ const normalized = {
1975
+ apiKey: config.apiKey.trim(),
1976
+ baseUrl: config.baseUrl ?? DEFAULT_BASE_URL,
1977
+ timeoutMs: config.timeoutMs ?? TIMEOUTS.DEFAULT,
1978
+ ...config.models !== void 0 ? { models: config.models } : {},
1979
+ ...config.textModel !== void 0 ? { textModel: config.textModel } : {}
1980
+ };
1981
+ this.config = normalized;
1982
+ return normalized;
1983
+ }
1984
+ get() {
1985
+ if (!this.config) {
1986
+ throw new GroqError(
1987
+ "MISSING_CONFIG" /* MISSING_CONFIG */,
1988
+ "Client not initialized. Call configureProvider first."
1989
+ );
1990
+ }
1991
+ return this.config;
1992
+ }
1993
+ getOrNull() {
1994
+ return this.config;
1995
+ }
1996
+ isInitialized() {
1997
+ return this.config !== null;
1998
+ }
1999
+ setApiKey(apiKey) {
2000
+ const current = this.get();
2001
+ const validation = validateApiKey(apiKey, { minLength: 1 });
2002
+ if (!validation.valid) {
2003
+ throw new GroqError("INVALID_API_KEY" /* INVALID_API_KEY */, validation.reason ?? "Invalid API key");
2004
+ }
2005
+ this.config = { ...current, apiKey: apiKey.trim() };
2006
+ }
2007
+ reset() {
2008
+ this.config = null;
2009
+ }
2010
+ };
2011
+
1946
2012
  // src/providers/groq/groq-http-transport.ts
1947
2013
  var GroqHttpTransport = class {
1948
- constructor(config) {
1949
- this.config = config;
2014
+ constructor(configSource) {
2015
+ this.configSource = configSource;
1950
2016
  }
1951
- config;
2017
+ configSource;
1952
2018
  pendingAbortControllers = /* @__PURE__ */ new Set();
2019
+ getConfig() {
2020
+ if ("get" in this.configSource && typeof this.configSource.get === "function") {
2021
+ return this.configSource.get();
2022
+ }
2023
+ return this.configSource;
2024
+ }
1953
2025
  cancelPendingRequests() {
1954
2026
  for (const controller of this.pendingAbortControllers) {
1955
2027
  controller.abort();
@@ -1964,7 +2036,7 @@ var GroqHttpTransport = class {
1964
2036
  }
1965
2037
  async streamChatCompletion(request, options = {}) {
1966
2038
  const url = `${this.baseUrl()}${CHAT_COMPLETIONS_PATH}`;
1967
- const timeout = this.config.timeoutMs ?? GROQ_TIMEOUTS.STREAMING;
2039
+ const timeout = this.getConfig().timeoutMs ?? GROQ_TIMEOUTS.STREAMING;
1968
2040
  const controller = new AbortController();
1969
2041
  const timeoutId = setTimeout(() => controller.abort(), timeout);
1970
2042
  const onExternalAbort = () => controller.abort();
@@ -1991,11 +2063,11 @@ var GroqHttpTransport = class {
1991
2063
  }
1992
2064
  }
1993
2065
  baseUrl() {
1994
- return this.config.baseUrl ?? GROQ_BASE_URL;
2066
+ return this.getConfig().baseUrl ?? GROQ_BASE_URL;
1995
2067
  }
1996
2068
  async request(endpoint, body) {
1997
2069
  const url = `${this.baseUrl()}${endpoint}`;
1998
- const timeout = this.config.timeoutMs ?? GROQ_TIMEOUTS.DEFAULT;
2070
+ const timeout = this.getConfig().timeoutMs ?? GROQ_TIMEOUTS.DEFAULT;
1999
2071
  const controller = new AbortController();
2000
2072
  const timeoutId = setTimeout(() => controller.abort(), timeout);
2001
2073
  this.pendingAbortControllers.add(controller);
@@ -2018,7 +2090,7 @@ var GroqHttpTransport = class {
2018
2090
  buildHeaders() {
2019
2091
  return {
2020
2092
  [HTTP_HEADERS.CONTENT_TYPE]: HTTP_HEADERS.APPLICATION_JSON,
2021
- [HTTP_HEADERS.AUTHORIZATION]: `${HTTP_HEADERS.BEARER_PREFIX}${this.config.apiKey}`
2093
+ [HTTP_HEADERS.AUTHORIZATION]: `${HTTP_HEADERS.BEARER_PREFIX}${this.getConfig().apiKey}`
2022
2094
  };
2023
2095
  }
2024
2096
  async throwFromErrorResponse(response) {
@@ -2062,21 +2134,40 @@ var GroqHttpTransport = class {
2062
2134
 
2063
2135
  // src/providers/groq/groq-http-client.ts
2064
2136
  var GroqHttpClient = class {
2137
+ configStore;
2065
2138
  transport;
2066
2139
  executor;
2067
- constructor(config, deps = {}) {
2068
- this.transport = deps.transport ?? new GroqHttpTransport(config);
2140
+ constructor(configOrDeps, maybeDeps = {}) {
2141
+ const isConfig = typeof configOrDeps === "object" && configOrDeps !== null && "apiKey" in configOrDeps && typeof configOrDeps.apiKey === "string";
2142
+ const deps = isConfig ? maybeDeps : configOrDeps ?? {};
2143
+ this.configStore = deps.configStore ?? new GroqConfigStore();
2144
+ if (isConfig) {
2145
+ this.configStore.initialize(configOrDeps);
2146
+ }
2147
+ this.transport = deps.transport ?? new GroqHttpTransport(this.configStore);
2069
2148
  const retryManager2 = deps.retryManager ?? new RetryManager();
2070
2149
  const queue = deps.queue ?? new RequestQueue();
2071
2150
  const deduplicator = deps.deduplicator ?? new RequestDeduplicator();
2072
2151
  this.executor = new CompositeExecutor([
2073
2152
  new DedupeExecutor(deduplicator, {
2074
- generateKey: (request) => deduplicatorKey(request, config.apiKey)
2153
+ generateKey: (request) => deduplicatorKey(request, this.configStore.isInitialized() ? this.configStore.get().apiKey : "")
2075
2154
  }),
2076
2155
  new RetryExecutor(retryManager2),
2077
2156
  new QueueExecutor(queue, QUEUE_PRIORITY_USER_REQUEST)
2078
2157
  ]);
2079
2158
  }
2159
+ initialize(config) {
2160
+ this.configStore.initialize(config);
2161
+ }
2162
+ isInitialized() {
2163
+ return this.configStore.isInitialized();
2164
+ }
2165
+ getApiKey() {
2166
+ return this.configStore.get().apiKey;
2167
+ }
2168
+ setApiKey(apiKey) {
2169
+ this.configStore.setApiKey(apiKey);
2170
+ }
2080
2171
  cancelPendingRequests() {
2081
2172
  this.transport.cancelPendingRequests();
2082
2173
  }
@@ -2089,6 +2180,10 @@ var GroqHttpClient = class {
2089
2180
  streamChatCompletion(request, options) {
2090
2181
  return this.transport.streamChatCompletion(request, options);
2091
2182
  }
2183
+ reset() {
2184
+ this.transport.cancelPendingRequests();
2185
+ this.configStore.reset();
2186
+ }
2092
2187
  };
2093
2188
  function deduplicatorKey(request, apiKey) {
2094
2189
  const { model, messages, stream: _stream, ...rest } = request;
@@ -2965,6 +3060,278 @@ function createAI(config = {}) {
2965
3060
  return factory;
2966
3061
  }
2967
3062
 
3063
+ // src/providers/groq/groq-messages.ts
3064
+ var createUserMessage = (content) => ({
3065
+ role: "user",
3066
+ content
3067
+ });
3068
+ var createAssistantMessage = (content) => ({
3069
+ role: "assistant",
3070
+ content
3071
+ });
3072
+ var createSystemMessage = (content) => ({
3073
+ role: "system",
3074
+ content
3075
+ });
3076
+ var createMessage = (role, content) => ({
3077
+ role,
3078
+ content
3079
+ });
3080
+ var promptToMessages = (prompt) => [
3081
+ createUserMessage(prompt)
3082
+ ];
3083
+ var extractTextFromMessages = (messages) => messages.map((m) => m.content).join("\n\n");
3084
+ var formatMessagesForDisplay = (messages) => messages.map((m) => `[${m.role.toUpperCase()}]: ${m.content}`).join("\n\n---\n\n");
3085
+
3086
+ // src/providers/groq/groq-structured-json.ts
3087
+ async function requestStructuredJson(client, request) {
3088
+ const chatRequest = {
3089
+ model: request.model,
3090
+ messages: [createUserMessage(request.instructions)],
3091
+ temperature: request.temperature,
3092
+ max_tokens: request.maxTokens,
3093
+ response_format: { type: "json_object" },
3094
+ ...request.reasoningEffort !== void 0 ? { reasoning_effort: request.reasoningEffort } : {}
3095
+ };
3096
+ const response = await client.postChatCompletion(chatRequest);
3097
+ const content = response.choices[0]?.message?.content ?? "";
3098
+ const candidate = extractJsonCandidate(content);
3099
+ try {
3100
+ return JSON.parse(candidate);
3101
+ } catch (err) {
3102
+ throw new GroqError(
3103
+ "UNKNOWN_ERROR" /* UNKNOWN_ERROR */,
3104
+ "model response contained no parsable json object",
3105
+ err instanceof Error ? err : void 0
3106
+ );
3107
+ }
3108
+ }
3109
+
3110
+ // src/providers/groq/text-generation.service.ts
3111
+ var STRUCTURED_OUTPUT_TEMPERATURE = 0.1;
3112
+ var TextGenerationService = class {
3113
+ constructor(httpClient, cache = new CacheManager(), defaultModel = DEFAULT_MODELS.TEXT) {
3114
+ this.httpClient = httpClient;
3115
+ this.cache = cache;
3116
+ this.defaultModel = defaultModel;
3117
+ }
3118
+ httpClient;
3119
+ cache;
3120
+ defaultModel;
3121
+ id = "groq-text-generation";
3122
+ name = "Groq Text Generation";
3123
+ capabilities = ["text"];
3124
+ jsonParser = new JsonResponseParser();
3125
+ generateCompletion(prompt, options = {}) {
3126
+ const model = options.model ?? this.defaultModel;
3127
+ return this.executeWithCache(
3128
+ {
3129
+ type: CACHE_KEY_TYPE_COMPLETION,
3130
+ model,
3131
+ prompt,
3132
+ config: options.generationConfig
3133
+ },
3134
+ async () => {
3135
+ const request = GroqChatRequestBuilder.for(model).withMessages([createUserMessage(prompt)]).withConfig(options.generationConfig).build();
3136
+ const response = await this.httpClient.postChatCompletion(request);
3137
+ return this.extractContent(response.choices[0]?.message?.content);
3138
+ }
3139
+ );
3140
+ }
3141
+ generateChatCompletion(messages, options = {}) {
3142
+ const model = options.model ?? this.defaultModel;
3143
+ return this.executeWithCache(
3144
+ {
3145
+ type: CACHE_KEY_TYPE_CHAT,
3146
+ model,
3147
+ messages: [...messages],
3148
+ config: options.generationConfig
3149
+ },
3150
+ async () => {
3151
+ const request = GroqChatRequestBuilder.for(model).withMessages(messages).withConfig(options.generationConfig).build();
3152
+ const response = await this.httpClient.postChatCompletion(request);
3153
+ return this.extractContent(response.choices[0]?.message?.content);
3154
+ }
3155
+ );
3156
+ }
3157
+ generateStructured(promptOrRequest, options) {
3158
+ if (typeof promptOrRequest === "string") {
3159
+ const model2 = options?.model ?? this.defaultModel;
3160
+ const jsonPrompt = StructuredPromptBuilder.for(promptOrRequest).withSchema(options?.schema).build();
3161
+ const request = GroqChatRequestBuilder.for(model2).withMessages([createUserMessage(jsonPrompt)]).withConfig({
3162
+ ...options?.generationConfig,
3163
+ temperature: STRUCTURED_OUTPUT_TEMPERATURE
3164
+ }).build();
3165
+ return this.httpClient.postChatCompletion(request).then((response) => {
3166
+ const content = this.extractContent(response.choices[0]?.message?.content);
3167
+ return this.jsonParser.parse(content);
3168
+ });
3169
+ }
3170
+ const req = promptOrRequest;
3171
+ const model = req.model ?? (req.tier ? this.resolveModel(req.tier) : this.defaultModel);
3172
+ const schemaObj = req.schema;
3173
+ const messages = [];
3174
+ if (req.systemPrompt) {
3175
+ messages.push({ role: "system", content: req.systemPrompt });
3176
+ }
3177
+ for (const m of req.messages) {
3178
+ messages.push({ role: m.role, content: m.content });
3179
+ }
3180
+ const lastMsg = messages[messages.length - 1];
3181
+ if (lastMsg) {
3182
+ const structuredPrompt = StructuredPromptBuilder.for(lastMsg.content).withSchema(schemaObj).build();
3183
+ messages[messages.length - 1] = { ...lastMsg, content: structuredPrompt };
3184
+ }
3185
+ const groqReq = GroqChatRequestBuilder.for(model).withMessages(messages).withConfig({
3186
+ temperature: req.temperature ?? STRUCTURED_OUTPUT_TEMPERATURE,
3187
+ maxTokens: req.maxTokens
3188
+ }).build();
3189
+ return this.httpClient.postChatCompletion(groqReq).then((response) => {
3190
+ const content = this.extractContent(response.choices[0]?.message?.content);
3191
+ const parsed = this.jsonParser.parse(content);
3192
+ const genResult = {
3193
+ text: content,
3194
+ model: response.model,
3195
+ providerId: this.id,
3196
+ finishReason: response.choices[0]?.finish_reason ?? "stop",
3197
+ usage: response.usage ? {
3198
+ inputTokens: response.usage.prompt_tokens,
3199
+ outputTokens: response.usage.completion_tokens,
3200
+ totalTokens: response.usage.total_tokens
3201
+ } : void 0
3202
+ };
3203
+ return { value: parsed, result: genResult };
3204
+ });
3205
+ }
3206
+ streamCompletion(prompt, callbacks, options = {}) {
3207
+ return this.streamChatCompletion([createUserMessage(prompt)], callbacks, options);
3208
+ }
3209
+ async streamChatCompletion(messages, callbacks, options = {}) {
3210
+ const model = options.model ?? this.defaultModel;
3211
+ const request = GroqChatRequestBuilder.for(model).withMessages(messages).withConfig(options.generationConfig).withStream(true).build();
3212
+ const parser = new StreamingResponseParser(
3213
+ {
3214
+ onChunk: (chunk) => callbacks.onChunk?.(chunk)
3215
+ },
3216
+ extractContentFromSsePayload
3217
+ );
3218
+ try {
3219
+ const stream = await this.httpClient.streamChatCompletion(request, {
3220
+ signal: options.signal
3221
+ });
3222
+ const fullText = await parser.parse(stream);
3223
+ callbacks.onComplete?.(fullText);
3224
+ return fullText;
3225
+ } catch (error) {
3226
+ const err = error instanceof Error ? error : new Error(String(error));
3227
+ callbacks.onError?.(err);
3228
+ throw err;
3229
+ }
3230
+ }
3231
+ clearCache() {
3232
+ this.cache.clear();
3233
+ }
3234
+ // --- ITextProvider implementation ---
3235
+ resolveModel(tier) {
3236
+ return GROQ_TIERS[tier] ?? this.defaultModel;
3237
+ }
3238
+ async generate(request) {
3239
+ const messages = [];
3240
+ if (request.systemPrompt) {
3241
+ messages.push({ role: "system", content: request.systemPrompt });
3242
+ }
3243
+ for (const m of request.messages) {
3244
+ messages.push({ role: m.role, content: m.content });
3245
+ }
3246
+ const model = request.model ?? (request.tier ? this.resolveModel(request.tier) : this.defaultModel);
3247
+ const groqReq = GroqChatRequestBuilder.for(model).withMessages(messages).withConfig({
3248
+ temperature: request.temperature,
3249
+ maxTokens: request.maxTokens
3250
+ }).build();
3251
+ const resp = await this.httpClient.postChatCompletion(groqReq);
3252
+ const content = resp.choices[0]?.message?.content ?? "";
3253
+ return {
3254
+ text: content,
3255
+ finishReason: resp.choices[0]?.finish_reason ?? "stop",
3256
+ model: resp.model,
3257
+ providerId: this.id,
3258
+ usage: resp.usage ? {
3259
+ inputTokens: resp.usage.prompt_tokens,
3260
+ outputTokens: resp.usage.completion_tokens,
3261
+ totalTokens: resp.usage.total_tokens
3262
+ } : void 0
3263
+ };
3264
+ }
3265
+ async stream(request, callbacks) {
3266
+ const messages = [];
3267
+ if (request.systemPrompt) {
3268
+ messages.push({ role: "system", content: request.systemPrompt });
3269
+ }
3270
+ for (const m of request.messages) {
3271
+ messages.push({ role: m.role, content: m.content });
3272
+ }
3273
+ const model = request.model ?? (request.tier ? this.resolveModel(request.tier) : this.defaultModel);
3274
+ const text = await this.streamChatCompletion(
3275
+ messages,
3276
+ {
3277
+ onChunk: callbacks.onChunk,
3278
+ onError: callbacks.onError
3279
+ },
3280
+ {
3281
+ model,
3282
+ signal: request.signal,
3283
+ generationConfig: {
3284
+ temperature: request.temperature,
3285
+ maxTokens: request.maxTokens
3286
+ }
3287
+ }
3288
+ );
3289
+ const result = {
3290
+ text,
3291
+ finishReason: "stop",
3292
+ model,
3293
+ providerId: this.id
3294
+ };
3295
+ callbacks.onComplete?.(result);
3296
+ return result;
3297
+ }
3298
+ async countTokens(text) {
3299
+ return Math.max(1, Math.ceil(text.length / 4));
3300
+ }
3301
+ async estimateCost(_request) {
3302
+ return 0;
3303
+ }
3304
+ async healthCheck() {
3305
+ try {
3306
+ await this.generateCompletion("ping", {
3307
+ generationConfig: { maxTokens: 1 }
3308
+ });
3309
+ return "healthy";
3310
+ } catch {
3311
+ return "unavailable";
3312
+ }
3313
+ }
3314
+ async executeWithCache(params, factory) {
3315
+ const key = this.cache.generateKey(params);
3316
+ const cached = this.cache.get(key);
3317
+ if (cached !== null) {
3318
+ return cached;
3319
+ }
3320
+ const result = await factory();
3321
+ this.cache.set(key, result);
3322
+ return result;
3323
+ }
3324
+ extractContent(content) {
3325
+ if (!content) {
3326
+ throw new GroqError(
3327
+ "UNKNOWN_ERROR" /* UNKNOWN_ERROR */,
3328
+ "No content generated from Groq API"
3329
+ );
3330
+ }
3331
+ return content;
3332
+ }
3333
+ };
3334
+
2968
3335
  // src/core/calculations/random-base36-suffix.ts
2969
3336
  var BASE_36_RADIX = 36;
2970
3337
  var DECIMAL_OFFSET = 2;
@@ -3192,6 +3559,30 @@ function readNumber(source, keys) {
3192
3559
  return null;
3193
3560
  }
3194
3561
 
3195
- export { AIError, AIErrorType, ANTHROPIC_API_VERSION, ANTHROPIC_DEFAULT_MAX_TOKENS, ANTHROPIC_MODELS, ANTHROPIC_TIERS, AnthropicHttpTransport, AnthropicProvider, AnthropicRequestBuilder, BaseProvider, CacheManager, ChatIdGenerator, ChatService, CompositeExecutor, DEFAULT_API_KEY_RULE, DEFAULT_ERROR_CLASSIFICATION_RULE, DEFAULT_HTTP_STATUS_RULE, DEFAULT_PROMPT_STRATEGY, DEFAULT_RETRY_BACKOFF_MULTIPLIER, DEFAULT_RETRY_INITIAL_DELAY_MS, DEFAULT_RETRY_JITTER_FACTOR, DEFAULT_RETRY_MAX_ATTEMPTS, DEFAULT_RETRY_MAX_DELAY_MS, DebounceManager, DedupeExecutor, FalProvider, GEMINI_TIERS, GROQ_MODELS, GROQ_TIERS, GeminiProvider, GroqChatRequestBuilder, GroqHttpClient, GroqHttpTransport, GroqProvider, JSON_CODE_BLOCK_PATTERN, JSON_OBJECT_PATTERN, JsonResponseParser, ProviderError, ProviderFactory, PrunaProvider, QueueExecutor, RequestDeduplicator, RequestQueue, RetryExecutor, RetryManager, SSE_BRIDGE_EVENTS, SSE_DONE_MARKER, SSE_PREFIX, StreamingResponseParser, StructuredPromptBuilder, TEXT_TIERS, ThrottleManager, USER_FRIENDLY_ERROR_MESSAGES, asObjectArray, asRecord, asStringArray, asTrimmedOptionalString, buildCacheKey, buildHttpStatusFallback, cacheManager, chatIdGenerator, clampScore, classifyHttpStatus, consumeAnthropicStream, consumeGroqStream, createAI, createFalProvider, createGeminiProvider, createPrunaProvider, createSseResponse, debounce, debounceManager, defaultEffortForModel, describeAIErrorType, extractContentFromSsePayload, extractErrorMessageFromJsonBody, extractErrorMessageFromResponse, extractJsonArray, extractJsonCandidate, extractJsonFromCodeFence, extractJsonObject, extractSseDataPayload, extractTokenUsage, formatErrorForLogging, generateWithPruna, isAuthErrorType, isMediaProvider, isRetryableErrorType, isSseDataLine, isSseDoneMarker, isTextProvider, isTextTier, isValidApiKey, mapAnthropicStopReason, mergeTierOverrides, messageFormatter, normalizeSseChunk, oneOfCatalog, repairCommonJsonDefects, requestDeduplicator, requestQueue, resolveModel, resolveUserFriendlyErrorMessage, resolveUserFriendlyMessage, retryManager, splitSseLines, throttle, throttleManager, toGroqMessages, validateApiKey, withRetry };
3562
+ // src/providers/groq/groq-container.ts
3563
+ var container = null;
3564
+ function configureProvider(config) {
3565
+ const configStore = new GroqConfigStore();
3566
+ const transport = new GroqHttpTransport(configStore);
3567
+ const httpClient = new GroqHttpClient({ configStore, transport });
3568
+ httpClient.initialize(config);
3569
+ const textGeneration = new TextGenerationService(
3570
+ httpClient,
3571
+ new CacheManager(),
3572
+ config.textModel
3573
+ );
3574
+ const chat = new ChatService(textGeneration);
3575
+ container = { groqHttpClient: httpClient, textGeneration, chat };
3576
+ return container;
3577
+ }
3578
+ function getProvider() {
3579
+ return container;
3580
+ }
3581
+ function resetProvider() {
3582
+ container?.groqHttpClient.reset();
3583
+ container = null;
3584
+ }
3585
+
3586
+ export { AIError, AIErrorType, ANTHROPIC_API_VERSION, ANTHROPIC_DEFAULT_MAX_TOKENS, ANTHROPIC_MODELS, ANTHROPIC_TIERS, API_ENDPOINTS, AnthropicHttpTransport, AnthropicProvider, AnthropicRequestBuilder, BaseProvider, CacheManager, ChatIdGenerator, ChatService, CompositeExecutor, DEFAULT_API_KEY_RULE, DEFAULT_BASE_URL, DEFAULT_ERROR_CLASSIFICATION_RULE, DEFAULT_GENERATION_CONFIG, DEFAULT_HTTP_STATUS_RULE, DEFAULT_MODELS, DEFAULT_PROMPT_STRATEGY, DEFAULT_RETRY_BACKOFF_MULTIPLIER, DEFAULT_RETRY_INITIAL_DELAY_MS, DEFAULT_RETRY_JITTER_FACTOR, DEFAULT_RETRY_MAX_ATTEMPTS, DEFAULT_RETRY_MAX_DELAY_MS, DebounceManager, DedupeExecutor, FalProvider, GEMINI_TIERS, GROQ_MODELS, GROQ_TIERS, GeminiProvider, GroqChatRequestBuilder, GroqConfigStore, GroqError, AIErrorType as GroqErrorType, GroqHttpClient, GroqHttpTransport, GroqProvider, JSON_CODE_BLOCK_PATTERN, JSON_OBJECT_PATTERN, JsonResponseParser, ProviderError, ProviderFactory, PrunaProvider, QueueExecutor, RequestDeduplicator, RequestQueue, RetryExecutor, RetryManager, SSE_BRIDGE_EVENTS, SSE_DONE_MARKER, SSE_PREFIX, StreamingResponseParser, StructuredPromptBuilder, TEXT_TIERS, TIMEOUTS, TextGenerationService, ThrottleManager, USER_FRIENDLY_ERROR_MESSAGES, asObjectArray, asRecord, asStringArray, asTrimmedOptionalString, buildCacheKey, buildHttpStatusFallback, cacheManager, chatIdGenerator, clampScore, classifyHttpStatus, configureProvider, consumeAnthropicStream, consumeGroqStream, createAI, createAssistantMessage, createFalProvider, createGeminiProvider, createMessage, createPrunaProvider, createSseResponse, createSystemMessage, createUserMessage, debounce, debounceManager, defaultEffortForModel, describeAIErrorType, extractContentFromSsePayload, extractErrorMessageFromJsonBody, extractErrorMessageFromResponse, extractJsonArray, extractJsonCandidate, extractJsonFromCodeFence, extractJsonObject, extractSseDataPayload, extractTextFromMessages, extractTokenUsage, formatErrorForLogging, formatMessagesForDisplay, generateWithPruna, getProvider, isAuthErrorType, isMediaProvider, isRetryableErrorType, isSseDataLine, isSseDoneMarker, isTextProvider, isTextTier, isValidApiKey, mapAnthropicStopReason, mergeTierOverrides, messageFormatter, normalizeSseChunk, oneOfCatalog, promptToMessages, repairCommonJsonDefects, requestDeduplicator, requestQueue, requestStructuredJson, resetProvider, resolveModel, resolveUserFriendlyErrorMessage, resolveUserFriendlyMessage, retryManager, splitSseLines, throttle, throttleManager, toGroqMessages, validateApiKey, withRetry };
3196
3587
  //# sourceMappingURL=index.js.map
3197
3588
  //# sourceMappingURL=index.js.map