{
  "name": "Browser Use",
  "settings": {
    "executionOrder": "v1",
    "callerPolicy": "workflowsFromSameOwner",
    "availableInMCP": false
  },
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Route Browser Action",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "nodes": [
    {
      "parameters": {
        "inputSource": "passthrough"
      },
      "name": "Start",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1.1,
      "position": [
        0,
        0
      ],
      "id": "browser-use-trigger"
    },
    {
      "parameters": {
        "jsCode": "// Parse incoming params from the toolWorkflow caller.\n// LLM may pass them as a JSON string in `query`, or as direct fields.\nconst raw = $input.first().json;\nlet action = '', task = '', url = '', domain = '', maxSteps = 25, timeoutS = 300;\ntry {\n  const p = JSON.parse(raw.query || '{}');\n  action = p.action || '';\n  task = p.task || '';\n  url = p.url || '';\n  domain = p.domain || '';\n  maxSteps = Number(p.max_steps) || 25;\n  timeoutS = Number(p.timeout_s) || 300;\n} catch(e) {\n  action = raw.action || '';\n  task = raw.task || '';\n  url = raw.url || '';\n  domain = raw.domain || '';\n  maxSteps = Number(raw.max_steps) || 25;\n  timeoutS = Number(raw.timeout_s) || 300;\n}\n\nconst userId = raw.userId || raw.user_id || 'telegram:{{TELEGRAM_CHAT_ID}}';\nconst BRIDGE = 'http://browser-bridge:3400';\n\nfunction errorResponse(msg) {\n  return [{ json: { success: false, error: msg } }];\n}\n\nif (!action) {\n  return errorResponse('Parameter \"action\" is required. One of: task, list_sessions, close_session.');\n}\n\nif (action === 'task') {\n  if (!task) return errorResponse('Parameter \"task\" is required for action=task.');\n  try {\n    const res = await helpers.httpRequest({\n      method: 'POST',\n      url: `${BRIDGE}/tasks`,\n      headers: { 'Content-Type': 'application/json' },\n      body: {\n        user_id: userId,\n        task,\n        url: url || null,\n        domain: domain || null,\n        max_steps: maxSteps,\n        timeout_s: timeoutS,\n      },\n      json: true,\n      timeout: (timeoutS + 30) * 1000,\n    });\n    return [{ json: { success: res.status === 'completed', ...res } }];\n  } catch(e) {\n    return errorResponse(`Browser task failed: ${e.message}`);\n  }\n}\n\nif (action === 'list_sessions') {\n  try {\n    const res = await helpers.httpRequest({\n      method: 'GET',\n      url: `${BRIDGE}/sessions/${encodeURIComponent(userId)}`,\n      json: true,\n      timeout: 10000,\n    });\n    const sessions = res.sessions || [];\n    if (sessions.length === 0) {\n      return [{ json: { success: true, sessions: [], message: 'No active browser sessions.' } }];\n    }\n    const lines = sessions.map(s => {\n      const ageMin = Math.round((Date.now() / 1000 - s.last_used_at) / 60);\n      return `- ${s.domain} (idle ${ageMin}m, ${s.n_tasks} tasks)`;\n    });\n    return [{ json: { success: true, sessions, message: `Active browser sessions:\\n${lines.join('\\n')}` } }];\n  } catch(e) {\n    return errorResponse(`Could not list sessions: ${e.message}`);\n  }\n}\n\nif (action === 'close_session') {\n  if (!domain) return errorResponse('Parameter \"domain\" is required for action=close_session.');\n  try {\n    const res = await helpers.httpRequest({\n      method: 'DELETE',\n      url: `${BRIDGE}/sessions/${encodeURIComponent(userId)}/${encodeURIComponent(domain)}`,\n      json: true,\n      timeout: 10000,\n    });\n    return [{ json: { success: true, ...res, message: `Browser session for ${domain} closed.` } }];\n  } catch(e) {\n    if (e.message && e.message.includes('404')) {\n      return errorResponse(`No active session for domain \"${domain}\".`);\n    }\n    return errorResponse(`Could not close session: ${e.message}`);\n  }\n}\n\nreturn errorResponse(`Unknown action \"${action}\". Use: task, list_sessions, close_session.`);"
      },
      "name": "Route Browser Action",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        224,
        0
      ],
      "id": "browser-use-router"
    }
  ]
}