call-ai 0.7.0-dev-preview-8 → 0.7.0-dev-preview-9

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 +19 -6
  2. package/package.json +1 -1
package/dist/api.js CHANGED
@@ -462,6 +462,10 @@ async function extractClaudeResponse(response) {
462
462
  /**
463
463
  * Generator factory function for streaming API calls
464
464
  * This is called after the fetch is made and response is validated
465
+ *
466
+ * Note: Even though we checked response.ok before creating this generator,
467
+ * we need to be prepared for errors that may occur during streaming. Some APIs
468
+ * return a 200 OK initially but then deliver error information in the stream.
465
469
  */
466
470
  async function* createStreamingGenerator(response, options, schemaStrategy, model) {
467
471
  try {
@@ -503,12 +507,21 @@ async function* createStreamingGenerator(response, options, schemaStrategy, mode
503
507
  chunkCount++;
504
508
  // Parse the JSON chunk
505
509
  const json = JSON.parse(jsonLine);
506
- // Check for error in the parsed JSON response
507
- if (json.error) {
508
- // Use the standard error format as the rest of the library
509
- // We need to throw the error properly
510
- handleApiError(new Error(`API returned error: ${JSON.stringify(json.error)}`), "Streaming API call error", options.debug);
511
- // This code is unreachable as handleApiError throws
510
+ // Enhanced error detection - check for BOTH error and json.error
511
+ // Some APIs return 200 OK but then deliver errors in the stream
512
+ if (json.error || (typeof json === 'object' && 'error' in json)) {
513
+ console.error(`[callAI:${PACKAGE_VERSION}] Detected error in streaming response:`, json);
514
+ // Create a detailed error object similar to our HTTP error handling
515
+ const errorMessage = json.error?.message ||
516
+ json.error?.toString() ||
517
+ JSON.stringify(json.error || json);
518
+ const detailedError = new Error(`API streaming error: ${errorMessage}`);
519
+ // Add error metadata
520
+ detailedError.status = json.error?.status || 400;
521
+ detailedError.statusText = json.error?.type || 'Bad Request';
522
+ detailedError.details = JSON.stringify(json.error || json);
523
+ console.error(`[callAI:${PACKAGE_VERSION}] Throwing stream error:`, detailedError);
524
+ throw detailedError;
512
525
  }
513
526
  // Handle tool use response - Claude with schema cases
514
527
  const isClaudeWithSchema = /claude/i.test(model) && schemaStrategy.strategy === "tool_mode";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "call-ai",
3
- "version": "0.7.0-dev-preview-8",
3
+ "version": "0.7.0-dev-preview-9",
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",