fix_compat.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import json, re
  2. path = r'c:/Users/aiac_dev/eks_relay/flowise-tools/get_product_compatibility.json'
  3. with open(path, 'r', encoding='utf-8') as f:
  4. data = json.load(f)
  5. # ── 1. Schema: insert product_id before language ────────────────────
  6. schema = json.loads(data['schema'])
  7. for s in schema:
  8. if s['property'] == 'language':
  9. s['id'] = 6
  10. schema.insert(5, {
  11. 'id': 5,
  12. 'property': 'product_id',
  13. 'description': 'ID produktu — podaj zamiast product_name gdy uzytkownik wybral z listy (uzyj mapy Number->ID)',
  14. 'type': 'number',
  15. 'required': False,
  16. })
  17. data['schema'] = json.dumps(schema, ensure_ascii=False)
  18. # ── 2. Func: work on the actual decoded JS string ───────────────────
  19. func = data['func']
  20. # 2a. Add product_id variable
  21. OLD_A = "const language = typeof $language !== 'undefined' && $language ? String($language) : ''\n\nif (!car_brand"
  22. NEW_A = "const language = typeof $language !== 'undefined' && $language ? String($language) : ''\nconst product_id = typeof $product_id !== 'undefined' && $product_id ? Number($product_id) : 0\n\nif (!car_brand"
  23. assert OLD_A in func, 'pattern 2a not found'
  24. func = func.replace(OLD_A, NEW_A)
  25. # 2b. Body construction: product_id takes priority over product_name
  26. OLD_B = ' if (car_engine) body.car_engine = car_engine\n if (product_name) body.product_name = product_name\n if (language) body.language = language'
  27. NEW_B = ' if (car_engine) body.car_engine = car_engine\n if (product_id > 0) body.product_id = product_id\n else if (product_name) body.product_name = product_name\n if (language) body.language = language'
  28. assert OLD_B in func, 'pattern 2b not found'
  29. func = func.replace(OLD_B, NEW_B)
  30. # 2c. Replace options handler block
  31. match = re.search(r' // Opcje do wyboru \(np\. silnik.*? \}', func, re.DOTALL)
  32. assert match, 'options handler not found'
  33. old_c = match.group(0)
  34. # In the actual JS string (after JSON.load), \n inside template literals
  35. # is the two-char sequence \n. Python represents this as \\n in string literals.
  36. new_c = (
  37. " // Opcje do wyboru\n"
  38. " if (result && result.error === 'options') {\n"
  39. " const fieldName = result.field || 'pole'\n"
  40. " const rawOptions = result.options || []\n"
  41. "\n"
  42. " // Product options with IDs — use ID map like get_product_data\n"
  43. " if (fieldName === 'product_name' && rawOptions.length > 0 && typeof rawOptions[0] === 'object') {\n"
  44. " const idMap = rawOptions.map((o, i) => `${i + 1}=${o.id}`).join(', ')\n"
  45. " const list = rawOptions.map((o, i) => `${i + 1}. ${o.title}`).join('\\n')\n"
  46. " return `Found ${rawOptions.length} products:\\n${list}\\n\\n[INSTRUCTION: Show the numbered list to the user. When user picks a number N, use the Number->ID map to get the product_id, then call this tool again with product_id=<that id> and the same car parameters. NEVER search by name again.\\nNumber->ID: ${idMap}]`\n"
  47. " }\n"
  48. "\n"
  49. " // Generic options (brand, model, engine)\n"
  50. " const options = rawOptions.map(o => typeof o === 'object' ? o.title : String(o).trim()).filter(Boolean)\n"
  51. " return `Selection required for field\"${fieldName}\". Available options:\\n${options.map((o, i) => `${i + 1}. ${o}`).join('\\n')}\\nAsk the user to choose, then call the tool again with the selected value as parameter\"${fieldName}\".`\n"
  52. " }"
  53. )
  54. func = func.replace(old_c, new_c)
  55. data['func'] = func
  56. with open(path, 'w', encoding='utf-8') as f:
  57. json.dump(data, f, ensure_ascii=False, indent=2)
  58. print('OK')