myavana-bot-test-core 1.0.6 → 1.0.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "myavana-bot-test-core",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "Shared bot functionality",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -365,51 +365,79 @@ const postResponseProductCheckPrompt = async (chatHistory, message) => {
365
365
  Identify and list the names of the products recommended in the bot's last response AND provide a brief reason for each recommendation based on the conversation.
366
366
  If no products were explicitly recommended, return an empty list.
367
367
 
368
- Response Format:
368
+ IMPORTANT: Your response must be ONLY valid JSON. Do not include any explanatory text, markdown formatting, or code blocks.
369
+
370
+ Expected Response Format (JSON only):
369
371
  [
370
372
  { "productName": "Product Name 1", "reason": "Reason for recommendation 1" },
371
- { "productName": "Product Name 2", "reason": "Reason for recommendation 2" },
372
- ...
373
- ] or [] if no products recommended.
373
+ { "productName": "Product Name 2", "reason": "Reason for recommendation 2" }
374
+ ]
375
+
376
+ If no products were recommended, respond with:
377
+ []
374
378
  `;
375
379
  };
376
380
 
377
381
  const postResponseProductCheck = async (chatHistory, message) => {
378
382
  const extractionPrompt = await postResponseProductCheckPrompt(chatHistory, message);
379
383
  //console.log('Extraction prompt for product check (with reason): ', extractionPrompt); // Updated log message
384
+
385
+ // Try Gemini first
380
386
  try {
381
387
  const chat = ai.chat({
382
388
  model: gemini15Flash, // or gemini20FlashExp
383
389
  config: {
384
- temperature: 0.9,
390
+ temperature: 0.3, // Reduced temperature for more consistent JSON formatting
385
391
  },
386
392
  });
387
393
  const { output } = await chat.send(extractionPrompt);
388
- //console.log("Raw post-processing output for product check (with reason):", JSON.stringify(output)); // Updated log
394
+ console.log("Raw post-processing output for product check (with reason):", JSON.stringify(output)); // Enable logging to debug
389
395
 
390
396
  if (!output) {
391
397
  console.warn("Post-processing output for product check (with reason) is empty. Returning default empty array."); // Updated log
392
398
  return [];
393
399
  }
394
400
 
401
+ // Clean the output to handle markdown code blocks and extra whitespace
402
+ let cleanOutput = output.toString().trim();
403
+
404
+ // Remove markdown code blocks if present
405
+ if (cleanOutput.startsWith('```json') && cleanOutput.endsWith('```')) {
406
+ cleanOutput = cleanOutput.slice(7, -3).trim();
407
+ } else if (cleanOutput.startsWith('```') && cleanOutput.endsWith('```')) {
408
+ cleanOutput = cleanOutput.slice(3, -3).trim();
409
+ }
410
+
411
+ // Handle case where AI returns just "[]" without quotes
412
+ if (cleanOutput === '[]' || cleanOutput === '') {
413
+ return [];
414
+ }
415
+
395
416
  try {
396
417
  // Attempt to parse the output as JSON (array of product objects)
397
- const recommendedProductsData = JSON.parse(output); // Renamed variable
418
+ const recommendedProductsData = JSON.parse(cleanOutput); // Renamed variable
398
419
  if (Array.isArray(recommendedProductsData)) {
399
420
  return recommendedProductsData; // Return the array of product objects
400
421
  } else {
401
422
  console.warn("Post-processing output is not an array. Returning empty array.");
423
+ console.warn("Parsed data:", recommendedProductsData);
402
424
  return [];
403
425
  }
404
426
  } catch (jsonError) {
405
427
  console.error("Error parsing post-processing JSON output:", jsonError);
428
+ console.error("Raw output was:", JSON.stringify(output));
429
+ console.error("Cleaned output was:", cleanOutput);
406
430
  console.warn("Returning default empty array due to JSON parsing error.");
407
431
  return [];
408
432
  }
409
433
 
410
434
  } catch (error) {
411
- console.error("Error in postResponseProductCheck:", error);
435
+ console.error("Gemini model failed in postResponseProductCheck:", error);
436
+ console.warn("Returning default empty array due to API call error - skipping xAI fallback for post-processing to avoid additional costs.");
412
437
  return [];
438
+
439
+ // Note: We're not adding xAI fallback here to avoid extra API costs for post-processing
440
+ // The main conversation flow is more critical than product extraction
413
441
  }
414
442
  };
415
443