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

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 +46 -3
  2. package/package.json +1 -1
package/dist/api.js CHANGED
@@ -35,10 +35,26 @@ function callAI(prompt, options = {}) {
35
35
  // Make the fetch request and handle errors before creating the generator
36
36
  console.log(`[callAI:${PACKAGE_VERSION}] Making fetch request to: ${endpoint}`);
37
37
  console.log(`[callAI:${PACKAGE_VERSION}] With model: ${model}`);
38
+ console.log(`[callAI:${PACKAGE_VERSION}] Request headers:`, JSON.stringify(requestOptions.headers));
38
39
  let response;
39
40
  try {
40
41
  response = await fetch(endpoint, requestOptions);
41
42
  console.log(`[callAI:${PACKAGE_VERSION}] Fetch completed with status:`, response.status, response.statusText);
43
+ // Log all headers
44
+ console.log(`[callAI:${PACKAGE_VERSION}] Response headers:`);
45
+ response.headers.forEach((value, name) => {
46
+ console.log(`[callAI:${PACKAGE_VERSION}] ${name}: ${value}`);
47
+ });
48
+ // Clone response for diagnostic purposes only
49
+ const diagnosticResponse = response.clone();
50
+ try {
51
+ // Try to get the response as text for debugging
52
+ const responseText = await diagnosticResponse.text();
53
+ console.log(`[callAI:${PACKAGE_VERSION}] First 500 chars of response body:`, responseText.substring(0, 500) + (responseText.length > 500 ? '...' : ''));
54
+ }
55
+ catch (e) {
56
+ console.log(`[callAI:${PACKAGE_VERSION}] Could not read response body for diagnostics:`, e);
57
+ }
42
58
  }
43
59
  catch (fetchError) {
44
60
  console.error(`[callAI:${PACKAGE_VERSION}] Network error during fetch:`, fetchError);
@@ -47,6 +63,7 @@ function callAI(prompt, options = {}) {
47
63
  // Explicitly check for HTTP error status and log extensively
48
64
  console.log(`[callAI:${PACKAGE_VERSION}] Response.ok =`, response.ok);
49
65
  console.log(`[callAI:${PACKAGE_VERSION}] Response.status =`, response.status);
66
+ console.log(`[callAI:${PACKAGE_VERSION}] Response.type =`, response.type);
50
67
  // Enhanced error handling with more debugging - MUST check !response.ok
51
68
  if (!response.ok) {
52
69
  console.log(`[callAI:${PACKAGE_VERSION}] Detected error response with status:`, response.status);
@@ -468,6 +485,10 @@ async function extractClaudeResponse(response) {
468
485
  * return a 200 OK initially but then deliver error information in the stream.
469
486
  */
470
487
  async function* createStreamingGenerator(response, options, schemaStrategy, model) {
488
+ console.log(`[callAI:${PACKAGE_VERSION}] Starting streaming generator with model: ${model}`);
489
+ console.log(`[callAI:${PACKAGE_VERSION}] Response status:`, response.status);
490
+ console.log(`[callAI:${PACKAGE_VERSION}] Response type:`, response.type);
491
+ console.log(`[callAI:${PACKAGE_VERSION}] Response Content-Type:`, response.headers.get('content-type'));
471
492
  try {
472
493
  // Handle streaming response
473
494
  if (!response.body) {
@@ -481,16 +502,26 @@ async function* createStreamingGenerator(response, options, schemaStrategy, mode
481
502
  while (true) {
482
503
  const { done, value } = await reader.read();
483
504
  if (done) {
505
+ console.log(`[callAI:${PACKAGE_VERSION}] Stream done=true after ${chunkCount} chunks`);
484
506
  if (options.debug) {
485
507
  console.log(`[callAI-streaming:complete v${PACKAGE_VERSION}] Stream finished after ${chunkCount} chunks`);
486
508
  }
487
509
  break;
488
510
  }
511
+ // Increment chunk counter before processing
512
+ chunkCount++;
489
513
  const chunk = decoder.decode(value);
514
+ console.log(`[callAI:${PACKAGE_VERSION}] Raw chunk #${chunkCount} (${chunk.length} bytes):`, chunk.length > 200 ? chunk.substring(0, 200) + '...' : chunk);
490
515
  const lines = chunk.split("\n").filter((line) => line.trim() !== "");
516
+ console.log(`[callAI:${PACKAGE_VERSION}] Chunk #${chunkCount} contains ${lines.length} non-empty lines`);
491
517
  for (const line of lines) {
518
+ console.log(`[callAI:${PACKAGE_VERSION}] Processing line:`, line.length > 100 ? line.substring(0, 100) + '...' : line);
492
519
  if (line.startsWith("data: ")) {
493
- // Simple debug logging of raw SSE events with no processing
520
+ let data = line.slice(6);
521
+ if (data === "[DONE]") {
522
+ console.log(`[callAI:${PACKAGE_VERSION}] Received [DONE] marker`);
523
+ break;
524
+ }
494
525
  if (options.debug) {
495
526
  console.log(`[callAI:raw] ${line}`);
496
527
  }
@@ -502,11 +533,23 @@ async function* createStreamingGenerator(response, options, schemaStrategy, mode
502
533
  try {
503
534
  const jsonLine = line.replace("data: ", "");
504
535
  if (!jsonLine.trim()) {
536
+ console.log(`[callAI:${PACKAGE_VERSION}] Empty JSON line after data: prefix`);
505
537
  continue;
506
538
  }
507
- chunkCount++;
539
+ console.log(`[callAI:${PACKAGE_VERSION}] JSON line (first 100 chars):`, jsonLine.length > 100 ? jsonLine.substring(0, 100) + '...' : jsonLine);
508
540
  // Parse the JSON chunk
509
- const json = JSON.parse(jsonLine);
541
+ let json;
542
+ try {
543
+ json = JSON.parse(jsonLine);
544
+ console.log(`[callAI:${PACKAGE_VERSION}] Parsed JSON:`, JSON.stringify(json).length > 100 ?
545
+ JSON.stringify(json).substring(0, 100) + '...' :
546
+ JSON.stringify(json));
547
+ }
548
+ catch (parseError) {
549
+ console.error(`[callAI:${PACKAGE_VERSION}] JSON parse error:`, parseError);
550
+ console.error(`[callAI:${PACKAGE_VERSION}] Failed to parse:`, jsonLine);
551
+ continue;
552
+ }
510
553
  // Enhanced error detection - check for BOTH error and json.error
511
554
  // Some APIs return 200 OK but then deliver errors in the stream
512
555
  if (json.error || (typeof json === 'object' && 'error' in json)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "call-ai",
3
- "version": "0.7.0-dev-preview-9",
3
+ "version": "0.7.0-dev-preview-10",
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",