call-ai 0.7.0-dev-preview-3 → 0.7.0-dev-preview-4

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/api.d.ts CHANGED
@@ -9,4 +9,4 @@ import { CallAIOptions, Message } from "./types";
9
9
  * @returns A Promise that resolves to the complete response string when streaming is disabled,
10
10
  * or an AsyncGenerator that yields partial responses when streaming is enabled
11
11
  */
12
- export declare function callAI(prompt: string | Message[], options?: CallAIOptions): Promise<string> | AsyncGenerator<string, string, unknown>;
12
+ export declare function callAI(prompt: string | Message[], options?: CallAIOptions): Promise<string> | AsyncGenerator<string, string | undefined, unknown>;
package/dist/api.js CHANGED
@@ -296,18 +296,21 @@ async function* callAIStreaming(prompt, options = {}, isRetry = false) {
296
296
  }
297
297
  const errorText = await response.text();
298
298
  console.error(`API Error: ${response.status} ${response.statusText}`, errorText);
299
- // Create the error object
300
- streamingError = new Error(`API returned error ${response.status}: ${response.statusText}`);
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
- }
299
+ // Create and yield a structured error object instead of throwing
300
+ const errorMessage = `API returned error ${response.status}: ${response.statusText}`;
301
+ const errorObj = {
302
+ error: true,
303
+ type: "APIError",
304
+ status: response.status,
305
+ message: errorMessage,
306
+ timestamp: new Date().toISOString()
307
+ };
308
+ // Store for later reference
309
+ streamingError = new Error(errorMessage);
310
+ // Yield the error object as a JSON string - this works in all environments
311
+ yield JSON.stringify(errorObj);
312
+ // Return after yielding the error to end the generator
313
+ return;
311
314
  }
312
315
  // Handle streaming response
313
316
  if (!response.body) {
@@ -465,25 +468,10 @@ async function* callAIStreaming(prompt, options = {}, isRetry = false) {
465
468
  if (streamingError) {
466
469
  handleApiError(streamingError, "Streaming API call", options.debug);
467
470
  }
468
- // Final check for errors before returning
469
- if (streamingError) {
470
- throw streamingError;
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
- }
471
+ // We don't need this check anymore as we're yielding errors
472
+ // and returning immediately after
473
+ // We no longer need this check since we're yielding errors directly
474
+ // and returning from the generator after yielding an error
487
475
  // If we have assembled tool calls but haven't yielded them yet
488
476
  if (toolCallsAssembled && (!completeText || completeText.length === 0)) {
489
477
  return toolCallsAssembled;
package/dist/types.d.ts CHANGED
@@ -64,6 +64,16 @@ export interface SchemaStrategy {
64
64
  processResponse: ModelStrategy["processResponse"];
65
65
  shouldForceStream: boolean;
66
66
  }
67
+ /**
68
+ * Structured error response format for streaming errors
69
+ */
70
+ export interface StreamingErrorResponse {
71
+ error: true;
72
+ type: string;
73
+ status: number;
74
+ message: string;
75
+ timestamp: string;
76
+ }
67
77
  export interface CallAIOptions {
68
78
  /**
69
79
  * API key for authentication
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "call-ai",
3
- "version": "0.7.0-dev-preview-3",
3
+ "version": "0.7.0-dev-preview-4",
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",