call-ai 0.7.0-dev-preview-2 → 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
@@ -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,21 @@ 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);
294
- streamingError = new Error(`API returned error ${response.status}: ${response.statusText}`);
295
- throw streamingError;
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;
296
314
  }
297
315
  // Handle streaming response
298
316
  if (!response.body) {
@@ -307,7 +325,7 @@ async function* callAIStreaming(prompt, options = {}, isRetry = false) {
307
325
  const { done, value } = await reader.read();
308
326
  if (done) {
309
327
  if (options.debug) {
310
- console.log(`[callAI-streaming:complete] Stream finished after ${chunkCount} chunks`);
328
+ console.log(`[callAI-streaming:complete v${PACKAGE_VERSION}] Stream finished after ${chunkCount} chunks`);
311
329
  }
312
330
  break;
313
331
  }
@@ -450,10 +468,10 @@ async function* callAIStreaming(prompt, options = {}, isRetry = false) {
450
468
  if (streamingError) {
451
469
  handleApiError(streamingError, "Streaming API call", options.debug);
452
470
  }
453
- // Final check for errors before returning
454
- if (streamingError) {
455
- throw streamingError;
456
- }
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
457
475
  // If we have assembled tool calls but haven't yielded them yet
458
476
  if (toolCallsAssembled && (!completeText || completeText.length === 0)) {
459
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-2",
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",