call-ai 0.7.0-dev-preview-2 → 0.7.0-dev-preview-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.
Files changed (2) hide show
  1. package/dist/api.js +32 -2
  2. package/package.json +1 -1
package/dist/api.js CHANGED
@@ -2,6 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.callAI = callAI;
4
4
  const strategies_1 = require("./strategies");
5
+ // Import package version for debugging
6
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
7
+ const PACKAGE_VERSION = require('../package.json').version;
5
8
  // Default fallback model when the primary model fails or is unavailable
6
9
  const FALLBACK_MODEL = "openrouter/auto";
7
10
  /**
@@ -280,6 +283,8 @@ async function extractClaudeResponse(response) {
280
283
  async function* callAIStreaming(prompt, options = {}, isRetry = false) {
281
284
  // Track errors to ensure consistent propagation across environments
282
285
  let streamingError = null;
286
+ // Browser-specific detection to help with environment-specific handling
287
+ const isBrowser = typeof window !== 'undefined';
283
288
  try {
284
289
  const { endpoint, requestOptions, model, schemaStrategy } = prepareRequestParams(prompt, { ...options, stream: true });
285
290
  const response = await fetch(endpoint, requestOptions);
@@ -291,8 +296,18 @@ async function* callAIStreaming(prompt, options = {}, isRetry = false) {
291
296
  }
292
297
  const errorText = await response.text();
293
298
  console.error(`API Error: ${response.status} ${response.statusText}`, errorText);
299
+ // Create the error object
294
300
  streamingError = new Error(`API returned error ${response.status}: ${response.statusText}`);
295
- throw streamingError;
301
+ // For browsers, we need to handle errors differently
302
+ if (isBrowser) {
303
+ // In browsers, return an error string that can be detected and handled
304
+ // The string format helps our client detect this is an error condition
305
+ return `{"error":true,"message":"API returned error ${response.status}: ${response.statusText}"}`;
306
+ }
307
+ else {
308
+ // In Node.js, throw directly as it propagates correctly
309
+ throw streamingError;
310
+ }
296
311
  }
297
312
  // Handle streaming response
298
313
  if (!response.body) {
@@ -307,7 +322,7 @@ async function* callAIStreaming(prompt, options = {}, isRetry = false) {
307
322
  const { done, value } = await reader.read();
308
323
  if (done) {
309
324
  if (options.debug) {
310
- console.log(`[callAI-streaming:complete] Stream finished after ${chunkCount} chunks`);
325
+ console.log(`[callAI-streaming:complete v${PACKAGE_VERSION}] Stream finished after ${chunkCount} chunks`);
311
326
  }
312
327
  break;
313
328
  }
@@ -454,6 +469,21 @@ async function* callAIStreaming(prompt, options = {}, isRetry = false) {
454
469
  if (streamingError) {
455
470
  throw streamingError;
456
471
  }
472
+ // Check if the completeText looks like our error marker format
473
+ if (completeText && completeText.includes('"error":true') && completeText.includes('"message":')) {
474
+ try {
475
+ // Try to parse the error info
476
+ const errorInfo = JSON.parse(completeText);
477
+ if (errorInfo.error === true && errorInfo.message) {
478
+ // Create and throw proper error
479
+ const detectedError = new Error(errorInfo.message);
480
+ handleApiError(detectedError, "Streaming API call", options.debug);
481
+ }
482
+ }
483
+ catch (parseError) {
484
+ // If we can't parse, continue with normal processing
485
+ }
486
+ }
457
487
  // If we have assembled tool calls but haven't yielded them yet
458
488
  if (toolCallsAssembled && (!completeText || completeText.length === 0)) {
459
489
  return toolCallsAssembled;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "call-ai",
3
- "version": "0.7.0-dev-preview-2",
3
+ "version": "0.7.0-dev-preview-3",
4
4
  "description": "Lightweight library for making AI API calls with streaming support",
5
5
  "main": "dist/index.js",
6
6
  "browser": "dist/index.js",