@umituz/web-ai 1.3.0 → 1.4.0

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
@@ -322,6 +322,31 @@ function aiErrorStatus(code) {
322
322
  return AI_ERROR_STATUS[code];
323
323
  }
324
324
 
325
+ // src/core/errors/ai-failure.ts
326
+ var TYPE_TO_CODE = {
327
+ ["INVALID_API_KEY" /* INVALID_API_KEY */]: "invalid_key",
328
+ ["MISSING_CONFIG" /* MISSING_CONFIG */]: "missing_key",
329
+ ["NETWORK_ERROR" /* NETWORK_ERROR */]: "network",
330
+ ["ABORT_ERROR" /* ABORT_ERROR */]: "timeout",
331
+ ["RATE_LIMIT_ERROR" /* RATE_LIMIT_ERROR */]: "rate_limited",
332
+ ["SERVER_ERROR" /* SERVER_ERROR */]: "upstream_error",
333
+ ["CLIENT_ERROR" /* CLIENT_ERROR */]: "invalid_request",
334
+ ["UNKNOWN_ERROR" /* UNKNOWN_ERROR */]: "upstream_error"
335
+ };
336
+ function aiFailureOf(error) {
337
+ if (error instanceof AiError) {
338
+ return { code: error.code, status: AI_ERROR_STATUS[error.code], message: error.userMessage };
339
+ }
340
+ if (error instanceof AIError) {
341
+ const code = TYPE_TO_CODE[error.type];
342
+ return { code, status: AI_ERROR_STATUS[code], message: CODE_MESSAGES[code] };
343
+ }
344
+ if (error instanceof Error && error.name === "AbortError") {
345
+ return { code: "timeout", status: AI_ERROR_STATUS.timeout, message: CODE_MESSAGES.timeout };
346
+ }
347
+ return { code: "upstream_error", status: AI_ERROR_STATUS.upstream_error, message: CODE_MESSAGES.upstream_error };
348
+ }
349
+
325
350
  // src/core/executor/retry-executor.strategy.ts
326
351
  var RetryExecutor = class {
327
352
  constructor(retryManager2, options) {
@@ -942,6 +967,24 @@ function deriveConversationTitle(content, options = {}) {
942
967
  return cleaned.length > maxChars ? `${cleaned.slice(0, maxChars).trimEnd()}\u2026` : cleaned;
943
968
  }
944
969
 
970
+ // src/core/utils/allowance-window.ts
971
+ var DEFAULT_ALLOWANCE_WINDOW_MS = 36e5;
972
+ function createAllowanceWindow(options) {
973
+ const windowMs = options.windowMs ?? DEFAULT_ALLOWANCE_WINDOW_MS;
974
+ const buckets = /* @__PURE__ */ new Map();
975
+ return {
976
+ allow(key, now = Date.now()) {
977
+ const bucket = buckets.get(key);
978
+ if (bucket === void 0 || now - bucket.windowStart >= windowMs) {
979
+ buckets.set(key, { windowStart: now, count: 1 });
980
+ return true;
981
+ }
982
+ bucket.count += 1;
983
+ return bucket.count <= options.limit;
984
+ }
985
+ };
986
+ }
987
+
945
988
  // src/core/constants/protocol.constants.ts
946
989
  var SSE_PREFIX = "data: ";
947
990
  var SSE_DONE_MARKER = "[DONE]";
@@ -6060,6 +6103,14 @@ function resetProvider() {
6060
6103
  container?.groqHttpClient.reset();
6061
6104
  container = null;
6062
6105
  }
6106
+ function bootProvider(apiKey, options = {}) {
6107
+ if (!isValidApiKey(apiKey)) return null;
6108
+ return getProvider() ?? configureProvider({
6109
+ apiKey: apiKey.trim(),
6110
+ timeoutMs: options.timeoutMs ?? 3e4,
6111
+ textModel: options.textModel ?? GROQ_MODELS.GPT_OSS_120B
6112
+ });
6113
+ }
6063
6114
 
6064
6115
  // src/providers/workers-ai/workers-ai.speech.ts
6065
6116
  var WORKERS_AI_SPEECH_TIMEOUT_MS = 45e3;
@@ -8399,6 +8450,7 @@ exports.ChatService = ChatService;
8399
8450
  exports.CompositeExecutor = CompositeExecutor;
8400
8451
  exports.ContentCalendarService = ContentCalendarService;
8401
8452
  exports.DEFAULT_AI_IDENTITY = DEFAULT_AI_IDENTITY;
8453
+ exports.DEFAULT_ALLOWANCE_WINDOW_MS = DEFAULT_ALLOWANCE_WINDOW_MS;
8402
8454
  exports.DEFAULT_API_KEY_RULE = DEFAULT_API_KEY_RULE;
8403
8455
  exports.DEFAULT_BASE_URL = DEFAULT_BASE_URL;
8404
8456
  exports.DEFAULT_EMBED_BATCH_SIZE = DEFAULT_EMBED_BATCH_SIZE;
@@ -8506,6 +8558,7 @@ exports.WorkersAIProvider = WorkersAIProvider;
8506
8558
  exports.accumulateReport = accumulateReport;
8507
8559
  exports.aiErrorMessage = aiErrorMessage;
8508
8560
  exports.aiErrorStatus = aiErrorStatus;
8561
+ exports.aiFailureOf = aiFailureOf;
8509
8562
  exports.apiKeyAvailable = apiKeyAvailable;
8510
8563
  exports.asObjectArray = asObjectArray;
8511
8564
  exports.asRecord = asRecord;
@@ -8514,6 +8567,7 @@ exports.asTrimmedOptionalString = asTrimmedOptionalString;
8514
8567
  exports.audioTranscriptionAdapterFor = audioTranscriptionAdapterFor;
8515
8568
  exports.batchEmbed = batchEmbed;
8516
8569
  exports.batchRequest = batchRequest;
8570
+ exports.bootProvider = bootProvider;
8517
8571
  exports.buildCacheKey = buildCacheKey;
8518
8572
  exports.buildContentRequest = buildContentRequest;
8519
8573
  exports.buildContext = buildContext;
@@ -8550,6 +8604,7 @@ exports.cosineSimilarity = cosineSimilarity;
8550
8604
  exports.countChanges = countChanges;
8551
8605
  exports.createAI = createAI;
8552
8606
  exports.createAdapterResolver = createAdapterResolver;
8607
+ exports.createAllowanceWindow = createAllowanceWindow;
8553
8608
  exports.createAnthropicAdapter = createAnthropicAdapter;
8554
8609
  exports.createAssistantMessage = createAssistantMessage;
8555
8610
  exports.createFalProvider = createFalProvider;