@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.
@@ -198,6 +198,14 @@ var ProviderQuotaExhaustedError = class extends AIError {
198
198
  }
199
199
  };
200
200
 
201
+ // src/core/errors/groq-error.ts
202
+ var GroqError = class extends AIError {
203
+ constructor(type, message, cause, metadata = {}) {
204
+ super(type, message, cause, metadata);
205
+ this.name = "GroqError";
206
+ }
207
+ };
208
+
201
209
  // src/core/errors/error-classifier.ts
202
210
  var DEFAULT_RETRYABLE_TYPES = /* @__PURE__ */ new Set([
203
211
  "NETWORK_ERROR" /* NETWORK_ERROR */,
@@ -1394,6 +1402,27 @@ var RequestDeduplicator = class {
1394
1402
  }
1395
1403
  };
1396
1404
 
1405
+ // src/core/utils/api-key-validator.ts
1406
+ var DEFAULT_API_KEY_RULE = {
1407
+ minLength: 10
1408
+ };
1409
+ function validateApiKey(apiKey, rule = DEFAULT_API_KEY_RULE) {
1410
+ if (!apiKey) {
1411
+ return { valid: false, reason: "API key is required" };
1412
+ }
1413
+ const trimmed = apiKey.trim();
1414
+ if (trimmed.length === 0) {
1415
+ return { valid: false, reason: "API key cannot be empty" };
1416
+ }
1417
+ if (trimmed.length < rule.minLength) {
1418
+ return {
1419
+ valid: false,
1420
+ reason: `API key must be at least ${rule.minLength} characters`
1421
+ };
1422
+ }
1423
+ return { valid: true };
1424
+ }
1425
+
1397
1426
  // src/providers/groq/groq.constants.ts
1398
1427
  var GROQ_MODELS = {
1399
1428
  LLAMA_3_1_8B_INSTANT: "llama-3.1-8b-instant",
@@ -1410,11 +1439,13 @@ var DEFAULT_GENERATION_CONFIG = {
1410
1439
  topP: 1
1411
1440
  };
1412
1441
  var GROQ_BASE_URL = "https://api.groq.com/openai/v1";
1442
+ var DEFAULT_BASE_URL = GROQ_BASE_URL;
1413
1443
  var CHAT_COMPLETIONS_PATH = "/chat/completions";
1414
1444
  var GROQ_TIMEOUTS = {
1415
1445
  DEFAULT: 3e4,
1416
1446
  STREAMING: 6e4
1417
1447
  };
1448
+ var TIMEOUTS = GROQ_TIMEOUTS;
1418
1449
  var HTTP_HEADERS = {
1419
1450
  CONTENT_TYPE: "Content-Type",
1420
1451
  APPLICATION_JSON: "application/json",
@@ -1473,13 +1504,65 @@ var GroqChatRequestBuilder = class _GroqChatRequestBuilder {
1473
1504
  }
1474
1505
  };
1475
1506
 
1507
+ // src/providers/groq/groq-config-store.ts
1508
+ var GroqConfigStore = class {
1509
+ config = null;
1510
+ initialize(config) {
1511
+ const validation = validateApiKey(config.apiKey, { minLength: 1 });
1512
+ if (!validation.valid) {
1513
+ throw new GroqError("INVALID_API_KEY" /* INVALID_API_KEY */, validation.reason ?? "Invalid API key");
1514
+ }
1515
+ const normalized = {
1516
+ apiKey: config.apiKey.trim(),
1517
+ baseUrl: config.baseUrl ?? DEFAULT_BASE_URL,
1518
+ timeoutMs: config.timeoutMs ?? TIMEOUTS.DEFAULT,
1519
+ ...config.models !== void 0 ? { models: config.models } : {},
1520
+ ...config.textModel !== void 0 ? { textModel: config.textModel } : {}
1521
+ };
1522
+ this.config = normalized;
1523
+ return normalized;
1524
+ }
1525
+ get() {
1526
+ if (!this.config) {
1527
+ throw new GroqError(
1528
+ "MISSING_CONFIG" /* MISSING_CONFIG */,
1529
+ "Client not initialized. Call configureProvider first."
1530
+ );
1531
+ }
1532
+ return this.config;
1533
+ }
1534
+ getOrNull() {
1535
+ return this.config;
1536
+ }
1537
+ isInitialized() {
1538
+ return this.config !== null;
1539
+ }
1540
+ setApiKey(apiKey) {
1541
+ const current = this.get();
1542
+ const validation = validateApiKey(apiKey, { minLength: 1 });
1543
+ if (!validation.valid) {
1544
+ throw new GroqError("INVALID_API_KEY" /* INVALID_API_KEY */, validation.reason ?? "Invalid API key");
1545
+ }
1546
+ this.config = { ...current, apiKey: apiKey.trim() };
1547
+ }
1548
+ reset() {
1549
+ this.config = null;
1550
+ }
1551
+ };
1552
+
1476
1553
  // src/providers/groq/groq-http-transport.ts
1477
1554
  var GroqHttpTransport = class {
1478
- constructor(config) {
1479
- this.config = config;
1555
+ constructor(configSource) {
1556
+ this.configSource = configSource;
1480
1557
  }
1481
- config;
1558
+ configSource;
1482
1559
  pendingAbortControllers = /* @__PURE__ */ new Set();
1560
+ getConfig() {
1561
+ if ("get" in this.configSource && typeof this.configSource.get === "function") {
1562
+ return this.configSource.get();
1563
+ }
1564
+ return this.configSource;
1565
+ }
1483
1566
  cancelPendingRequests() {
1484
1567
  for (const controller of this.pendingAbortControllers) {
1485
1568
  controller.abort();
@@ -1494,7 +1577,7 @@ var GroqHttpTransport = class {
1494
1577
  }
1495
1578
  async streamChatCompletion(request, options = {}) {
1496
1579
  const url = `${this.baseUrl()}${CHAT_COMPLETIONS_PATH}`;
1497
- const timeout = this.config.timeoutMs ?? GROQ_TIMEOUTS.STREAMING;
1580
+ const timeout = this.getConfig().timeoutMs ?? GROQ_TIMEOUTS.STREAMING;
1498
1581
  const controller = new AbortController();
1499
1582
  const timeoutId = setTimeout(() => controller.abort(), timeout);
1500
1583
  const onExternalAbort = () => controller.abort();
@@ -1521,11 +1604,11 @@ var GroqHttpTransport = class {
1521
1604
  }
1522
1605
  }
1523
1606
  baseUrl() {
1524
- return this.config.baseUrl ?? GROQ_BASE_URL;
1607
+ return this.getConfig().baseUrl ?? GROQ_BASE_URL;
1525
1608
  }
1526
1609
  async request(endpoint, body) {
1527
1610
  const url = `${this.baseUrl()}${endpoint}`;
1528
- const timeout = this.config.timeoutMs ?? GROQ_TIMEOUTS.DEFAULT;
1611
+ const timeout = this.getConfig().timeoutMs ?? GROQ_TIMEOUTS.DEFAULT;
1529
1612
  const controller = new AbortController();
1530
1613
  const timeoutId = setTimeout(() => controller.abort(), timeout);
1531
1614
  this.pendingAbortControllers.add(controller);
@@ -1548,7 +1631,7 @@ var GroqHttpTransport = class {
1548
1631
  buildHeaders() {
1549
1632
  return {
1550
1633
  [HTTP_HEADERS.CONTENT_TYPE]: HTTP_HEADERS.APPLICATION_JSON,
1551
- [HTTP_HEADERS.AUTHORIZATION]: `${HTTP_HEADERS.BEARER_PREFIX}${this.config.apiKey}`
1634
+ [HTTP_HEADERS.AUTHORIZATION]: `${HTTP_HEADERS.BEARER_PREFIX}${this.getConfig().apiKey}`
1552
1635
  };
1553
1636
  }
1554
1637
  async throwFromErrorResponse(response) {
@@ -1650,21 +1733,40 @@ var CompositeExecutor = class {
1650
1733
 
1651
1734
  // src/providers/groq/groq-http-client.ts
1652
1735
  var GroqHttpClient = class {
1736
+ configStore;
1653
1737
  transport;
1654
1738
  executor;
1655
- constructor(config, deps = {}) {
1656
- this.transport = deps.transport ?? new GroqHttpTransport(config);
1739
+ constructor(configOrDeps, maybeDeps = {}) {
1740
+ const isConfig = typeof configOrDeps === "object" && configOrDeps !== null && "apiKey" in configOrDeps && typeof configOrDeps.apiKey === "string";
1741
+ const deps = isConfig ? maybeDeps : configOrDeps ?? {};
1742
+ this.configStore = deps.configStore ?? new GroqConfigStore();
1743
+ if (isConfig) {
1744
+ this.configStore.initialize(configOrDeps);
1745
+ }
1746
+ this.transport = deps.transport ?? new GroqHttpTransport(this.configStore);
1657
1747
  const retryManager2 = deps.retryManager ?? new RetryManager();
1658
1748
  const queue = deps.queue ?? new RequestQueue();
1659
1749
  const deduplicator = deps.deduplicator ?? new RequestDeduplicator();
1660
1750
  this.executor = new CompositeExecutor([
1661
1751
  new DedupeExecutor(deduplicator, {
1662
- generateKey: (request) => deduplicatorKey(request, config.apiKey)
1752
+ generateKey: (request) => deduplicatorKey(request, this.configStore.isInitialized() ? this.configStore.get().apiKey : "")
1663
1753
  }),
1664
1754
  new RetryExecutor(retryManager2),
1665
1755
  new QueueExecutor(queue, QUEUE_PRIORITY_USER_REQUEST)
1666
1756
  ]);
1667
1757
  }
1758
+ initialize(config) {
1759
+ this.configStore.initialize(config);
1760
+ }
1761
+ isInitialized() {
1762
+ return this.configStore.isInitialized();
1763
+ }
1764
+ getApiKey() {
1765
+ return this.configStore.get().apiKey;
1766
+ }
1767
+ setApiKey(apiKey) {
1768
+ this.configStore.setApiKey(apiKey);
1769
+ }
1668
1770
  cancelPendingRequests() {
1669
1771
  this.transport.cancelPendingRequests();
1670
1772
  }
@@ -1677,6 +1779,10 @@ var GroqHttpClient = class {
1677
1779
  streamChatCompletion(request, options) {
1678
1780
  return this.transport.streamChatCompletion(request, options);
1679
1781
  }
1782
+ reset() {
1783
+ this.transport.cancelPendingRequests();
1784
+ this.configStore.reset();
1785
+ }
1680
1786
  };
1681
1787
  function deduplicatorKey(request, apiKey) {
1682
1788
  const { model, messages, stream: _stream, ...rest } = request;