"script":"import json\nimport time\nimport urllib.parse\n\n# Extract the nested data properly\nmessage_body = \"\"\nuser_phone = \"\"\nmessage_id = \"\"\n\n# The data now resides under variables.intent_param.messages\nif isinstance(input, dict):\n # First, try top-level \"text\" field\n if \"text\" in input and isinstance(input[\"text\"], str):\n message_body = input[\"text\"]\n # Fallback to messages[0].text.body\n elif \"messages\" in input and isinstance(input[\"messages\"], list) and len(input[\"messages\"]) > 0:\n first_message = input[\"messages\"][0]\n if \"text\" in first_message and \"body\" in first_message[\"text\"]:\n message_body = first_message[\"text\"][\"body\"]\n\n# Construct the intent_param JSON for invokeIntent API\nintent_param = {\n\"BIS_INTENT_ID\": \"\",\n\"identity\": str(int(time.time() * 1000)),\n\"USER_MSG\": message_body,\n\"IS_BROWSER\": True,\n\"INPUT_TEXT\": message_body,\n\"USER_ID\": user_phone,\n\"UUID\": message_id\n}\n\n# Encode JSON for query parameter\nencoded_intent_param = urllib.parse.quote(json.dumps(intent_param), safe='')\n\n# Optional debug print for workflow logs\nprint(\"Raw intent_param:\", json.dumps(intent_param, indent=2))\nprint(\"Encoded intent_param:\", encoded_intent_param)\n\n# Output for the next node (API call)\noutput = {\n\"intent_param\": encoded_intent_param\n}\n",
"script":"import json\n\nresponse_data = node_outputs.vision_generate_content # your API node\nmessage_text = \"\"\n\ntry:\n # ✅ Step 1: Extract text or data from both possible response types\n data_candidate = None\n\n # Case A: Normal API success structure\n if (\n isinstance(response_data, dict)\n and \"Response\" in response_data\n and \"results\" in response_data[\"Response\"]\n and \"Root\" in response_data[\"Response\"][\"results\"]\n ):\n root = response_data[\"Response\"][\"results\"][\"Root\"]\n\n if \"Detail\" in root and \"result\" in root[\"Detail\"]:\n result = root[\"Detail\"][\"result\"]\n data_candidate = result.get(\"data\")\n\n elif \"Errors\" in root and \"error\" in root[\"Errors\"]:\n error = root[\"Errors\"][\"error\"]\n data_candidate = error.get(\"description\", \"\") or error.get(\"message\", \"\")\n\n # Case B: Text response structure\n elif isinstance(response_data, dict) and \"text\" in response_data:\n data_candidate = response_data.get(\"text\")\n\n # ✅ Step 2: Handle both plain text and table-type responses\n if isinstance(data_candidate, str):\n message_text = data_candidate.strip()\n\n elif isinstance(data_candidate, list) and len(data_candidate) > 0:\n\n # Handle table-like data (cols + rows)\n table_obj = data_candidate[0] if isinstance(data_candidate[0], dict) else None\n if table_obj and \"cols\" in table_obj and \"rows\" in table_obj:\n\n cols = table_obj.get(\"cols\", [])\n rows = table_obj.get(\"rows\", [])\n\n if cols and rows:\n # Format each row as key-value pairs with bold column names\n formatted_rows = \"\\n\".join([\n f\"*{cols[i]}* : {r[i]}\"\n for i in range(len(cols))\n for r in rows\n ])\n message_text = formatted_rows\n else:\n message_text = \"No table data found.\"\n\n else:\n # If list but not a table, convert to plain string\n message_text = str(data_candidate)\n\n # Fallback\n if not message_text:\n message_text = \"No valid response found from Vision Assistant.\"\n\n # Ensure output is plain string\n message_text = str(message_text).strip()\n\nexcept Exception as e:\n message_text = f\"Error while processing response: {str(e)}\"\n\n# Final output\noutput = {\n\"_description\": message_text\n}\n\nset_global('vision_response', message_text)\n",