camel-ai 0.2.65__py3-none-any.whl → 0.2.83a6__py3-none-any.whl

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.

Potentially problematic release.


This version of camel-ai might be problematic. Click here for more details.

Files changed (509) hide show
  1. camel/__init__.py +3 -3
  2. camel/agents/__init__.py +2 -2
  3. camel/agents/_types.py +9 -4
  4. camel/agents/_utils.py +40 -2
  5. camel/agents/base.py +2 -2
  6. camel/agents/chat_agent.py +5107 -995
  7. camel/agents/critic_agent.py +2 -2
  8. camel/agents/deductive_reasoner_agent.py +56 -56
  9. camel/agents/embodied_agent.py +2 -2
  10. camel/agents/knowledge_graph_agent.py +20 -20
  11. camel/agents/mcp_agent.py +35 -36
  12. camel/agents/multi_hop_generator_agent.py +3 -3
  13. camel/agents/programmed_agent_instruction.py +2 -2
  14. camel/agents/repo_agent.py +4 -3
  15. camel/agents/role_assignment_agent.py +2 -2
  16. camel/agents/search_agent.py +2 -2
  17. camel/agents/task_agent.py +2 -2
  18. camel/agents/tool_agents/__init__.py +2 -2
  19. camel/agents/tool_agents/base.py +2 -2
  20. camel/agents/tool_agents/hugging_face_tool_agent.py +3 -3
  21. camel/benchmarks/__init__.py +2 -2
  22. camel/benchmarks/apibank.py +5 -5
  23. camel/benchmarks/apibench.py +2 -2
  24. camel/benchmarks/base.py +2 -2
  25. camel/benchmarks/browsecomp.py +44 -33
  26. camel/benchmarks/gaia.py +17 -13
  27. camel/benchmarks/mock_website/README.md +1 -3
  28. camel/benchmarks/mock_website/mock_web.py +2 -2
  29. camel/benchmarks/mock_website/requirements.txt +1 -1
  30. camel/benchmarks/mock_website/shopping_mall/app.py +2 -2
  31. camel/benchmarks/mock_website/task.json +1 -1
  32. camel/benchmarks/nexus.py +3 -3
  33. camel/benchmarks/ragbench.py +2 -2
  34. camel/bots/__init__.py +2 -2
  35. camel/bots/discord/__init__.py +2 -2
  36. camel/bots/discord/discord_app.py +2 -2
  37. camel/bots/discord/discord_installation.py +2 -2
  38. camel/bots/discord/discord_store.py +3 -3
  39. camel/bots/slack/__init__.py +2 -2
  40. camel/bots/slack/models.py +4 -4
  41. camel/bots/slack/slack_app.py +2 -2
  42. camel/bots/telegram_bot.py +2 -2
  43. camel/configs/__init__.py +29 -2
  44. camel/configs/aihubmix_config.py +90 -0
  45. camel/configs/aiml_config.py +2 -2
  46. camel/configs/amd_config.py +70 -0
  47. camel/configs/anthropic_config.py +2 -2
  48. camel/configs/base_config.py +2 -2
  49. camel/configs/bedrock_config.py +5 -3
  50. camel/configs/cerebras_config.py +98 -0
  51. camel/configs/cohere_config.py +2 -2
  52. camel/configs/cometapi_config.py +106 -0
  53. camel/configs/crynux_config.py +2 -2
  54. camel/configs/deepseek_config.py +9 -8
  55. camel/configs/function_gemma_config.py +59 -0
  56. camel/configs/gemini_config.py +6 -4
  57. camel/configs/groq_config.py +6 -4
  58. camel/configs/internlm_config.py +6 -4
  59. camel/configs/litellm_config.py +2 -2
  60. camel/configs/lmstudio_config.py +6 -4
  61. camel/configs/minimax_config.py +95 -0
  62. camel/configs/mistral_config.py +2 -2
  63. camel/configs/modelscope_config.py +5 -3
  64. camel/configs/moonshot_config.py +2 -2
  65. camel/configs/nebius_config.py +105 -0
  66. camel/configs/netmind_config.py +2 -2
  67. camel/configs/novita_config.py +2 -2
  68. camel/configs/nvidia_config.py +2 -2
  69. camel/configs/ollama_config.py +2 -2
  70. camel/configs/openai_config.py +5 -3
  71. camel/configs/openrouter_config.py +6 -4
  72. camel/configs/ppio_config.py +2 -2
  73. camel/configs/qianfan_config.py +85 -0
  74. camel/configs/qwen_config.py +2 -2
  75. camel/configs/reka_config.py +2 -2
  76. camel/configs/samba_config.py +6 -4
  77. camel/configs/sglang_config.py +2 -2
  78. camel/configs/siliconflow_config.py +2 -2
  79. camel/configs/togetherai_config.py +2 -2
  80. camel/configs/vllm_config.py +4 -2
  81. camel/configs/watsonx_config.py +2 -2
  82. camel/configs/yi_config.py +6 -4
  83. camel/configs/zhipuai_config.py +6 -4
  84. camel/data_collectors/__init__.py +2 -2
  85. camel/data_collectors/alpaca_collector.py +18 -9
  86. camel/data_collectors/base.py +2 -2
  87. camel/data_collectors/sharegpt_collector.py +2 -2
  88. camel/datagen/__init__.py +2 -2
  89. camel/datagen/cot_datagen.py +3 -3
  90. camel/datagen/evol_instruct/__init__.py +2 -2
  91. camel/datagen/evol_instruct/evol_instruct.py +2 -2
  92. camel/datagen/evol_instruct/scorer.py +12 -12
  93. camel/datagen/evol_instruct/templates.py +16 -16
  94. camel/datagen/self_improving_cot.py +5 -5
  95. camel/datagen/self_instruct/__init__.py +2 -2
  96. camel/datagen/self_instruct/filter/__init__.py +2 -2
  97. camel/datagen/self_instruct/filter/filter_function.py +2 -2
  98. camel/datagen/self_instruct/filter/filter_registry.py +2 -2
  99. camel/datagen/self_instruct/filter/instruction_filter.py +2 -2
  100. camel/datagen/self_instruct/self_instruct.py +2 -2
  101. camel/datagen/self_instruct/templates.py +47 -47
  102. camel/datagen/source2synth/__init__.py +2 -2
  103. camel/datagen/source2synth/data_processor.py +2 -2
  104. camel/datagen/source2synth/models.py +2 -2
  105. camel/datagen/source2synth/user_data_processor_config.py +2 -2
  106. camel/datahubs/__init__.py +2 -2
  107. camel/datahubs/base.py +2 -2
  108. camel/datahubs/huggingface.py +2 -2
  109. camel/datahubs/models.py +2 -2
  110. camel/datasets/__init__.py +2 -2
  111. camel/datasets/base_generator.py +41 -12
  112. camel/datasets/few_shot_generator.py +18 -18
  113. camel/datasets/models.py +2 -2
  114. camel/datasets/self_instruct_generator.py +2 -2
  115. camel/datasets/static_dataset.py +2 -2
  116. camel/embeddings/__init__.py +2 -2
  117. camel/embeddings/azure_embedding.py +2 -2
  118. camel/embeddings/base.py +2 -2
  119. camel/embeddings/gemini_embedding.py +2 -2
  120. camel/embeddings/jina_embedding.py +2 -2
  121. camel/embeddings/mistral_embedding.py +2 -2
  122. camel/embeddings/openai_compatible_embedding.py +2 -2
  123. camel/embeddings/openai_embedding.py +2 -2
  124. camel/embeddings/sentence_transformers_embeddings.py +2 -2
  125. camel/embeddings/together_embedding.py +2 -2
  126. camel/embeddings/vlm_embedding.py +2 -2
  127. camel/environments/__init__.py +14 -2
  128. camel/environments/models.py +2 -2
  129. camel/environments/multi_step.py +2 -2
  130. camel/environments/rlcards_env.py +860 -0
  131. camel/environments/single_step.py +30 -5
  132. camel/environments/tic_tac_toe.py +3 -3
  133. camel/extractors/__init__.py +2 -2
  134. camel/extractors/base.py +2 -2
  135. camel/extractors/python_strategies.py +2 -2
  136. camel/generators.py +2 -2
  137. camel/human.py +2 -2
  138. camel/interpreters/__init__.py +4 -2
  139. camel/interpreters/base.py +2 -2
  140. camel/interpreters/docker/Dockerfile +14 -24
  141. camel/interpreters/docker_interpreter.py +5 -4
  142. camel/interpreters/e2b_interpreter.py +36 -3
  143. camel/interpreters/internal_python_interpreter.py +53 -4
  144. camel/interpreters/interpreter_error.py +2 -2
  145. camel/interpreters/ipython_interpreter.py +2 -2
  146. camel/interpreters/microsandbox_interpreter.py +395 -0
  147. camel/interpreters/subprocess_interpreter.py +2 -2
  148. camel/loaders/__init__.py +13 -4
  149. camel/loaders/apify_reader.py +2 -2
  150. camel/loaders/base_io.py +2 -2
  151. camel/loaders/base_loader.py +85 -0
  152. camel/loaders/chunkr_reader.py +11 -2
  153. camel/loaders/crawl4ai_reader.py +2 -2
  154. camel/loaders/firecrawl_reader.py +6 -6
  155. camel/loaders/jina_url_reader.py +2 -2
  156. camel/loaders/markitdown.py +2 -2
  157. camel/loaders/mineru_extractor.py +2 -2
  158. camel/loaders/mistral_reader.py +2 -2
  159. camel/loaders/scrapegraph_reader.py +2 -2
  160. camel/loaders/unstructured_io.py +2 -2
  161. camel/logger.py +5 -5
  162. camel/memories/__init__.py +2 -2
  163. camel/memories/agent_memories.py +86 -3
  164. camel/memories/base.py +36 -2
  165. camel/memories/blocks/__init__.py +2 -2
  166. camel/memories/blocks/chat_history_block.py +125 -7
  167. camel/memories/blocks/vectordb_block.py +10 -3
  168. camel/memories/context_creators/__init__.py +2 -2
  169. camel/memories/context_creators/score_based.py +109 -230
  170. camel/memories/records.py +90 -10
  171. camel/messages/__init__.py +2 -2
  172. camel/messages/base.py +178 -43
  173. camel/messages/conversion/__init__.py +2 -2
  174. camel/messages/conversion/alpaca.py +2 -2
  175. camel/messages/conversion/conversation_models.py +2 -2
  176. camel/messages/conversion/sharegpt/__init__.py +2 -2
  177. camel/messages/conversion/sharegpt/function_call_formatter.py +2 -2
  178. camel/messages/conversion/sharegpt/hermes/__init__.py +2 -2
  179. camel/messages/conversion/sharegpt/hermes/hermes_function_formatter.py +2 -2
  180. camel/messages/func_message.py +54 -17
  181. camel/models/__init__.py +18 -2
  182. camel/models/_utils.py +3 -3
  183. camel/models/aihubmix_model.py +83 -0
  184. camel/models/aiml_model.py +11 -18
  185. camel/models/amd_model.py +101 -0
  186. camel/models/anthropic_model.py +127 -20
  187. camel/models/aws_bedrock_model.py +12 -35
  188. camel/models/azure_openai_model.py +214 -115
  189. camel/models/base_audio_model.py +5 -3
  190. camel/models/base_model.py +378 -31
  191. camel/models/cerebras_model.py +83 -0
  192. camel/models/cohere_model.py +18 -49
  193. camel/models/cometapi_model.py +83 -0
  194. camel/models/crynux_model.py +11 -18
  195. camel/models/deepseek_model.py +20 -84
  196. camel/models/fish_audio_model.py +8 -2
  197. camel/models/function_gemma_model.py +889 -0
  198. camel/models/gemini_model.py +391 -52
  199. camel/models/groq_model.py +11 -19
  200. camel/models/internlm_model.py +11 -18
  201. camel/models/litellm_model.py +57 -49
  202. camel/models/lmstudio_model.py +17 -20
  203. camel/models/minimax_model.py +83 -0
  204. camel/models/mistral_model.py +20 -47
  205. camel/models/model_factory.py +39 -3
  206. camel/models/model_manager.py +26 -8
  207. camel/models/modelscope_model.py +13 -193
  208. camel/models/moonshot_model.py +183 -21
  209. camel/models/nebius_model.py +83 -0
  210. camel/models/nemotron_model.py +19 -9
  211. camel/models/netmind_model.py +11 -18
  212. camel/models/novita_model.py +11 -18
  213. camel/models/nvidia_model.py +11 -18
  214. camel/models/ollama_model.py +14 -21
  215. camel/models/openai_audio_models.py +2 -2
  216. camel/models/openai_compatible_model.py +190 -71
  217. camel/models/openai_model.py +192 -86
  218. camel/models/openrouter_model.py +11 -19
  219. camel/models/ppio_model.py +11 -18
  220. camel/models/qianfan_model.py +89 -0
  221. camel/models/qwen_model.py +13 -193
  222. camel/models/reka_model.py +23 -49
  223. camel/models/reward/__init__.py +2 -2
  224. camel/models/reward/base_reward_model.py +2 -2
  225. camel/models/reward/evaluator.py +2 -2
  226. camel/models/reward/nemotron_model.py +2 -2
  227. camel/models/reward/skywork_model.py +2 -2
  228. camel/models/samba_model.py +50 -75
  229. camel/models/sglang_model.py +90 -68
  230. camel/models/siliconflow_model.py +12 -35
  231. camel/models/stub_model.py +10 -7
  232. camel/models/togetherai_model.py +11 -18
  233. camel/models/vllm_model.py +10 -18
  234. camel/models/volcano_model.py +158 -19
  235. camel/models/watsonx_model.py +9 -47
  236. camel/models/yi_model.py +11 -18
  237. camel/models/zhipuai_model.py +70 -18
  238. camel/parsers/__init__.py +18 -0
  239. camel/parsers/mcp_tool_call_parser.py +176 -0
  240. camel/personas/__init__.py +2 -2
  241. camel/personas/persona.py +2 -2
  242. camel/personas/persona_hub.py +2 -2
  243. camel/prompts/__init__.py +2 -2
  244. camel/prompts/ai_society.py +2 -2
  245. camel/prompts/base.py +2 -2
  246. camel/prompts/code.py +2 -2
  247. camel/prompts/evaluation.py +2 -2
  248. camel/prompts/generate_text_embedding_data.py +2 -2
  249. camel/prompts/image_craft.py +2 -2
  250. camel/prompts/misalignment.py +2 -2
  251. camel/prompts/multi_condition_image_craft.py +2 -2
  252. camel/prompts/object_recognition.py +2 -2
  253. camel/prompts/persona_hub.py +3 -3
  254. camel/prompts/prompt_templates.py +2 -2
  255. camel/prompts/role_description_prompt_template.py +2 -2
  256. camel/prompts/solution_extraction.py +8 -8
  257. camel/prompts/task_prompt_template.py +2 -2
  258. camel/prompts/translation.py +2 -2
  259. camel/prompts/video_description_prompt.py +3 -3
  260. camel/responses/__init__.py +2 -2
  261. camel/responses/agent_responses.py +2 -2
  262. camel/retrievers/__init__.py +2 -2
  263. camel/retrievers/auto_retriever.py +3 -2
  264. camel/retrievers/base.py +2 -2
  265. camel/retrievers/bm25_retriever.py +2 -2
  266. camel/retrievers/cohere_rerank_retriever.py +2 -2
  267. camel/retrievers/hybrid_retrival.py +2 -2
  268. camel/retrievers/vector_retriever.py +2 -2
  269. camel/runtimes/Dockerfile.multi-toolkit +90 -0
  270. camel/runtimes/__init__.py +2 -2
  271. camel/runtimes/api.py +79 -23
  272. camel/runtimes/base.py +2 -2
  273. camel/runtimes/configs.py +13 -13
  274. camel/runtimes/daytona_runtime.py +17 -18
  275. camel/runtimes/docker_runtime.py +12 -12
  276. camel/runtimes/llm_guard_runtime.py +26 -26
  277. camel/runtimes/remote_http_runtime.py +11 -11
  278. camel/runtimes/ubuntu_docker_runtime.py +2 -2
  279. camel/runtimes/utils/__init__.py +2 -2
  280. camel/runtimes/utils/function_risk_toolkit.py +2 -2
  281. camel/runtimes/utils/ignore_risk_toolkit.py +2 -2
  282. camel/schemas/__init__.py +2 -2
  283. camel/schemas/base.py +2 -2
  284. camel/schemas/openai_converter.py +3 -3
  285. camel/schemas/outlines_converter.py +2 -2
  286. camel/services/agent_openapi_server.py +380 -0
  287. camel/societies/__init__.py +4 -2
  288. camel/societies/babyagi_playing.py +2 -2
  289. camel/societies/role_playing.py +201 -80
  290. camel/societies/workforce/__init__.py +10 -3
  291. camel/societies/workforce/base.py +2 -2
  292. camel/societies/workforce/events.py +145 -0
  293. camel/societies/workforce/prompts.py +259 -33
  294. camel/societies/workforce/role_playing_worker.py +88 -31
  295. camel/societies/workforce/single_agent_worker.py +638 -40
  296. camel/societies/workforce/structured_output_handler.py +512 -0
  297. camel/societies/workforce/task_channel.py +182 -38
  298. camel/societies/workforce/utils.py +780 -65
  299. camel/societies/workforce/worker.py +92 -26
  300. camel/societies/workforce/workflow_memory_manager.py +1746 -0
  301. camel/societies/workforce/workforce.py +5354 -372
  302. camel/societies/workforce/workforce_callback.py +103 -0
  303. camel/societies/workforce/workforce_logger.py +647 -0
  304. camel/societies/workforce/workforce_metrics.py +33 -0
  305. camel/storages/__init__.py +6 -2
  306. camel/storages/graph_storages/__init__.py +2 -2
  307. camel/storages/graph_storages/base.py +2 -2
  308. camel/storages/graph_storages/graph_element.py +2 -2
  309. camel/storages/graph_storages/nebula_graph.py +4 -4
  310. camel/storages/graph_storages/neo4j_graph.py +7 -7
  311. camel/storages/key_value_storages/__init__.py +2 -2
  312. camel/storages/key_value_storages/base.py +2 -2
  313. camel/storages/key_value_storages/in_memory.py +2 -2
  314. camel/storages/key_value_storages/json.py +17 -4
  315. camel/storages/key_value_storages/mem0_cloud.py +50 -49
  316. camel/storages/key_value_storages/redis.py +2 -2
  317. camel/storages/object_storages/__init__.py +2 -2
  318. camel/storages/object_storages/amazon_s3.py +2 -2
  319. camel/storages/object_storages/azure_blob.py +2 -2
  320. camel/storages/object_storages/base.py +2 -2
  321. camel/storages/object_storages/google_cloud.py +3 -3
  322. camel/storages/vectordb_storages/__init__.py +8 -2
  323. camel/storages/vectordb_storages/base.py +2 -2
  324. camel/storages/vectordb_storages/chroma.py +731 -0
  325. camel/storages/vectordb_storages/faiss.py +2 -2
  326. camel/storages/vectordb_storages/milvus.py +2 -2
  327. camel/storages/vectordb_storages/oceanbase.py +15 -15
  328. camel/storages/vectordb_storages/pgvector.py +349 -0
  329. camel/storages/vectordb_storages/qdrant.py +6 -6
  330. camel/storages/vectordb_storages/surreal.py +372 -0
  331. camel/storages/vectordb_storages/tidb.py +11 -8
  332. camel/storages/vectordb_storages/weaviate.py +2 -2
  333. camel/tasks/__init__.py +2 -2
  334. camel/tasks/task.py +348 -26
  335. camel/tasks/task_prompt.py +3 -3
  336. camel/terminators/__init__.py +2 -2
  337. camel/terminators/base.py +2 -2
  338. camel/terminators/response_terminator.py +2 -2
  339. camel/terminators/token_limit_terminator.py +2 -2
  340. camel/toolkits/__init__.py +57 -10
  341. camel/toolkits/aci_toolkit.py +66 -21
  342. camel/toolkits/arxiv_toolkit.py +8 -8
  343. camel/toolkits/ask_news_toolkit.py +2 -2
  344. camel/toolkits/async_browser_toolkit.py +4 -4
  345. camel/toolkits/audio_analysis_toolkit.py +3 -3
  346. camel/toolkits/base.py +106 -6
  347. camel/toolkits/bohrium_toolkit.py +2 -2
  348. camel/toolkits/browser_toolkit.py +34 -21
  349. camel/toolkits/browser_toolkit_commons.py +4 -4
  350. camel/toolkits/code_execution.py +31 -4
  351. camel/toolkits/context_summarizer_toolkit.py +684 -0
  352. camel/toolkits/craw4ai_toolkit.py +93 -0
  353. camel/toolkits/dappier_toolkit.py +12 -8
  354. camel/toolkits/data_commons_toolkit.py +2 -2
  355. camel/toolkits/dingtalk.py +1135 -0
  356. camel/toolkits/earth_science_toolkit.py +5367 -0
  357. camel/toolkits/edgeone_pages_mcp_toolkit.py +49 -0
  358. camel/toolkits/excel_toolkit.py +905 -71
  359. camel/toolkits/file_toolkit.py +1402 -0
  360. camel/toolkits/function_tool.py +205 -27
  361. camel/toolkits/github_toolkit.py +109 -22
  362. camel/toolkits/gmail_toolkit.py +1839 -0
  363. camel/toolkits/google_calendar_toolkit.py +40 -6
  364. camel/toolkits/google_drive_mcp_toolkit.py +54 -0
  365. camel/toolkits/google_maps_toolkit.py +2 -2
  366. camel/toolkits/google_scholar_toolkit.py +2 -2
  367. camel/toolkits/human_toolkit.py +36 -12
  368. camel/toolkits/hybrid_browser_toolkit/__init__.py +18 -0
  369. camel/toolkits/hybrid_browser_toolkit/config_loader.py +185 -0
  370. camel/toolkits/hybrid_browser_toolkit/hybrid_browser_toolkit.py +246 -0
  371. camel/toolkits/hybrid_browser_toolkit/hybrid_browser_toolkit_ts.py +1958 -0
  372. camel/toolkits/hybrid_browser_toolkit/installer.py +203 -0
  373. camel/toolkits/hybrid_browser_toolkit/ts/package-lock.json +4589 -0
  374. camel/toolkits/hybrid_browser_toolkit/ts/package.json +33 -0
  375. camel/toolkits/hybrid_browser_toolkit/ts/src/browser-scripts.js +125 -0
  376. camel/toolkits/hybrid_browser_toolkit/ts/src/browser-session.ts +1940 -0
  377. camel/toolkits/hybrid_browser_toolkit/ts/src/config-loader.ts +233 -0
  378. camel/toolkits/hybrid_browser_toolkit/ts/src/hybrid-browser-toolkit.ts +589 -0
  379. camel/toolkits/hybrid_browser_toolkit/ts/src/index.ts +7 -0
  380. camel/toolkits/hybrid_browser_toolkit/ts/src/parent-child-filter.ts +226 -0
  381. camel/toolkits/hybrid_browser_toolkit/ts/src/snapshot-parser.ts +219 -0
  382. camel/toolkits/hybrid_browser_toolkit/ts/src/som-screenshot-injected.ts +543 -0
  383. camel/toolkits/hybrid_browser_toolkit/ts/src/types.ts +129 -0
  384. camel/toolkits/hybrid_browser_toolkit/ts/tsconfig.json +27 -0
  385. camel/toolkits/hybrid_browser_toolkit/ts/websocket-server.js +325 -0
  386. camel/toolkits/hybrid_browser_toolkit/ws_wrapper.py +1037 -0
  387. camel/toolkits/hybrid_browser_toolkit_py/__init__.py +17 -0
  388. camel/toolkits/hybrid_browser_toolkit_py/actions.py +575 -0
  389. camel/toolkits/hybrid_browser_toolkit_py/agent.py +311 -0
  390. camel/toolkits/hybrid_browser_toolkit_py/browser_session.py +787 -0
  391. camel/toolkits/hybrid_browser_toolkit_py/config_loader.py +490 -0
  392. camel/toolkits/hybrid_browser_toolkit_py/hybrid_browser_toolkit.py +2390 -0
  393. camel/toolkits/hybrid_browser_toolkit_py/snapshot.py +233 -0
  394. camel/toolkits/hybrid_browser_toolkit_py/stealth_script.js +0 -0
  395. camel/toolkits/hybrid_browser_toolkit_py/unified_analyzer.js +1043 -0
  396. camel/toolkits/image_analysis_toolkit.py +3 -6
  397. camel/toolkits/image_generation_toolkit.py +390 -0
  398. camel/toolkits/jina_reranker_toolkit.py +5 -6
  399. camel/toolkits/klavis_toolkit.py +7 -3
  400. camel/toolkits/linkedin_toolkit.py +2 -2
  401. camel/toolkits/markitdown_toolkit.py +104 -0
  402. camel/toolkits/math_toolkit.py +66 -12
  403. camel/toolkits/mcp_toolkit.py +412 -36
  404. camel/toolkits/memory_toolkit.py +7 -3
  405. camel/toolkits/meshy_toolkit.py +2 -2
  406. camel/toolkits/message_agent_toolkit.py +608 -0
  407. camel/toolkits/message_integration.py +728 -0
  408. camel/toolkits/microsoft_outlook_mail_toolkit.py +1885 -0
  409. camel/toolkits/mineru_toolkit.py +2 -2
  410. camel/toolkits/minimax_mcp_toolkit.py +195 -0
  411. camel/toolkits/networkx_toolkit.py +2 -2
  412. camel/toolkits/note_taking_toolkit.py +277 -0
  413. camel/toolkits/notion_mcp_toolkit.py +224 -0
  414. camel/toolkits/notion_toolkit.py +2 -2
  415. camel/toolkits/open_api_specs/biztoc/__init__.py +2 -2
  416. camel/toolkits/open_api_specs/biztoc/ai-plugin.json +1 -1
  417. camel/toolkits/open_api_specs/coursera/__init__.py +2 -2
  418. camel/toolkits/open_api_specs/create_qr_code/__init__.py +2 -2
  419. camel/toolkits/open_api_specs/klarna/__init__.py +2 -2
  420. camel/toolkits/open_api_specs/nasa_apod/__init__.py +2 -2
  421. camel/toolkits/open_api_specs/outschool/__init__.py +2 -2
  422. camel/toolkits/open_api_specs/outschool/ai-plugin.json +1 -1
  423. camel/toolkits/open_api_specs/outschool/openapi.yaml +1 -1
  424. camel/toolkits/open_api_specs/outschool/paths/__init__.py +2 -2
  425. camel/toolkits/open_api_specs/outschool/paths/get_classes.py +2 -2
  426. camel/toolkits/open_api_specs/outschool/paths/search_teachers.py +2 -2
  427. camel/toolkits/open_api_specs/security_config.py +2 -2
  428. camel/toolkits/open_api_specs/speak/__init__.py +2 -2
  429. camel/toolkits/open_api_specs/web_scraper/__init__.py +2 -2
  430. camel/toolkits/open_api_specs/web_scraper/ai-plugin.json +1 -1
  431. camel/toolkits/open_api_specs/web_scraper/paths/__init__.py +2 -2
  432. camel/toolkits/open_api_specs/web_scraper/paths/scraper.py +2 -2
  433. camel/toolkits/open_api_toolkit.py +2 -2
  434. camel/toolkits/openbb_toolkit.py +7 -3
  435. camel/toolkits/origene_mcp_toolkit.py +56 -0
  436. camel/toolkits/page_script.js +53 -53
  437. camel/toolkits/playwright_mcp_toolkit.py +13 -31
  438. camel/toolkits/pptx_toolkit.py +36 -23
  439. camel/toolkits/pubmed_toolkit.py +2 -2
  440. camel/toolkits/pulse_mcp_search_toolkit.py +2 -2
  441. camel/toolkits/pyautogui_toolkit.py +2 -2
  442. camel/toolkits/reddit_toolkit.py +2 -2
  443. camel/toolkits/resend_toolkit.py +168 -0
  444. camel/toolkits/retrieval_toolkit.py +2 -2
  445. camel/toolkits/screenshot_toolkit.py +213 -0
  446. camel/toolkits/search_toolkit.py +606 -156
  447. camel/toolkits/searxng_toolkit.py +2 -2
  448. camel/toolkits/semantic_scholar_toolkit.py +2 -2
  449. camel/toolkits/slack_toolkit.py +108 -58
  450. camel/toolkits/sql_toolkit.py +712 -0
  451. camel/toolkits/stripe_toolkit.py +2 -2
  452. camel/toolkits/sympy_toolkit.py +3 -3
  453. camel/toolkits/task_planning_toolkit.py +5 -5
  454. camel/toolkits/terminal_toolkit/__init__.py +18 -0
  455. camel/toolkits/terminal_toolkit/terminal_toolkit.py +1281 -0
  456. camel/toolkits/terminal_toolkit/utils.py +659 -0
  457. camel/toolkits/thinking_toolkit.py +3 -3
  458. camel/toolkits/twitter_toolkit.py +2 -2
  459. camel/toolkits/vertex_ai_veo_toolkit.py +590 -0
  460. camel/toolkits/video_analysis_toolkit.py +109 -29
  461. camel/toolkits/video_download_toolkit.py +19 -16
  462. camel/toolkits/weather_toolkit.py +2 -2
  463. camel/toolkits/web_deploy_toolkit.py +1219 -0
  464. camel/toolkits/wechat_official_toolkit.py +483 -0
  465. camel/toolkits/whatsapp_toolkit.py +2 -2
  466. camel/toolkits/wolfram_alpha_toolkit.py +2 -2
  467. camel/toolkits/zapier_toolkit.py +7 -3
  468. camel/types/__init__.py +4 -4
  469. camel/types/agents/__init__.py +2 -2
  470. camel/types/agents/tool_calling_record.py +6 -3
  471. camel/types/enums.py +381 -41
  472. camel/types/mcp_registries.py +2 -2
  473. camel/types/openai_types.py +4 -4
  474. camel/types/unified_model_type.py +46 -10
  475. camel/utils/__init__.py +5 -2
  476. camel/utils/agent_context.py +41 -0
  477. camel/utils/async_func.py +2 -2
  478. camel/utils/chunker/__init__.py +2 -2
  479. camel/utils/chunker/base.py +2 -2
  480. camel/utils/chunker/code_chunker.py +2 -2
  481. camel/utils/chunker/uio_chunker.py +2 -2
  482. camel/utils/commons.py +38 -7
  483. camel/utils/constants.py +5 -2
  484. camel/utils/context_utils.py +1134 -0
  485. camel/utils/deduplication.py +2 -2
  486. camel/utils/filename.py +2 -2
  487. camel/utils/langfuse.py +18 -10
  488. camel/utils/mcp.py +140 -6
  489. camel/utils/mcp_client.py +48 -38
  490. camel/utils/message_summarizer.py +148 -0
  491. camel/utils/response_format.py +2 -2
  492. camel/utils/token_counting.py +45 -22
  493. camel/utils/tool_result.py +44 -0
  494. camel/verifiers/__init__.py +2 -2
  495. camel/verifiers/base.py +2 -2
  496. camel/verifiers/math_verifier.py +2 -2
  497. camel/verifiers/models.py +2 -2
  498. camel/verifiers/physics_verifier.py +2 -2
  499. camel/verifiers/python_verifier.py +2 -2
  500. {camel_ai-0.2.65.dist-info → camel_ai-0.2.83a6.dist-info}/METADATA +355 -117
  501. camel_ai-0.2.83a6.dist-info/RECORD +511 -0
  502. {camel_ai-0.2.65.dist-info → camel_ai-0.2.83a6.dist-info}/WHEEL +1 -1
  503. {camel_ai-0.2.65.dist-info → camel_ai-0.2.83a6.dist-info}/licenses/LICENSE +1 -1
  504. camel/loaders/pandas_reader.py +0 -368
  505. camel/toolkits/dalle_toolkit.py +0 -175
  506. camel/toolkits/file_write_toolkit.py +0 -444
  507. camel/toolkits/openai_agent_toolkit.py +0 -135
  508. camel/toolkits/terminal_toolkit.py +0 -1037
  509. camel_ai-0.2.65.dist-info/RECORD +0 -426
@@ -0,0 +1,1958 @@
1
+ # ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+ # ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========
14
+ # =========
15
+
16
+ import asyncio
17
+ import contextlib
18
+ import time
19
+ from typing import (
20
+ Any,
21
+ Callable,
22
+ ClassVar,
23
+ Dict,
24
+ List,
25
+ Optional,
26
+ TypedDict,
27
+ cast,
28
+ )
29
+
30
+ from camel.logger import get_logger
31
+ from camel.toolkits.base import BaseToolkit, RegisteredAgentToolkit
32
+ from camel.toolkits.function_tool import FunctionTool
33
+ from camel.utils.tool_result import ToolResult
34
+
35
+ from .config_loader import ConfigLoader
36
+ from .ws_wrapper import WebSocketBrowserWrapper, high_level_action
37
+
38
+ logger = get_logger(__name__)
39
+
40
+
41
+ class SheetCell(TypedDict):
42
+ """Type definition for a sheet cell input."""
43
+
44
+ row: int
45
+ col: int
46
+ text: str
47
+
48
+
49
+ class HybridBrowserToolkit(BaseToolkit, RegisteredAgentToolkit):
50
+ r"""A hybrid browser toolkit that combines non-visual, DOM-based browser
51
+ automation with visual, screenshot-based capabilities.
52
+
53
+ This toolkit now uses TypeScript implementation with Playwright's
54
+ _snapshotForAI functionality for enhanced AI integration.
55
+ """
56
+
57
+ DEFAULT_TOOLS: ClassVar[List[str]] = [
58
+ "browser_open",
59
+ "browser_close",
60
+ "browser_visit_page",
61
+ "browser_back",
62
+ "browser_forward",
63
+ "browser_click",
64
+ "browser_type",
65
+ "browser_switch_tab",
66
+ ]
67
+
68
+ ALL_TOOLS: ClassVar[List[str]] = [
69
+ "browser_open",
70
+ "browser_close",
71
+ "browser_visit_page",
72
+ "browser_back",
73
+ "browser_forward",
74
+ "browser_get_page_snapshot",
75
+ "browser_get_som_screenshot",
76
+ "browser_click",
77
+ "browser_type",
78
+ "browser_select",
79
+ "browser_scroll",
80
+ "browser_enter",
81
+ "browser_mouse_control",
82
+ "browser_mouse_drag",
83
+ "browser_press_key",
84
+ "browser_wait_user",
85
+ "browser_switch_tab",
86
+ "browser_close_tab",
87
+ "browser_get_tab_info",
88
+ "browser_console_view",
89
+ "browser_console_exec",
90
+ "browser_sheet_input",
91
+ "browser_sheet_read",
92
+ ]
93
+
94
+ def __init__(
95
+ self,
96
+ *,
97
+ headless: bool = True,
98
+ user_data_dir: Optional[str] = None,
99
+ stealth: bool = False,
100
+ cache_dir: Optional[str] = None,
101
+ enabled_tools: Optional[List[str]] = None,
102
+ browser_log_to_file: bool = False,
103
+ log_dir: Optional[str] = None,
104
+ session_id: Optional[str] = None,
105
+ default_start_url: Optional[str] = None,
106
+ default_timeout: Optional[int] = None,
107
+ short_timeout: Optional[int] = None,
108
+ navigation_timeout: Optional[int] = None,
109
+ network_idle_timeout: Optional[int] = None,
110
+ screenshot_timeout: Optional[int] = None,
111
+ page_stability_timeout: Optional[int] = None,
112
+ dom_content_loaded_timeout: Optional[int] = None,
113
+ viewport_limit: bool = False,
114
+ connect_over_cdp: bool = False,
115
+ cdp_url: Optional[str] = None,
116
+ cdp_keep_current_page: bool = False,
117
+ full_visual_mode: bool = False,
118
+ ) -> None:
119
+ r"""Initialize the HybridBrowserToolkit.
120
+
121
+ Args:
122
+ headless (bool): Whether to run browser in headless mode.
123
+ Defaults to True.
124
+ user_data_dir (Optional[str]): Directory for user data
125
+ persistence. Defaults to None.
126
+ stealth (bool): Whether to enable stealth mode. Defaults to
127
+ False.
128
+ cache_dir (str): Directory for caching. Defaults to "tmp/".
129
+ enabled_tools (Optional[List[str]]): List of enabled tools.
130
+ Defaults to None.
131
+ browser_log_to_file (bool): Whether to log browser actions to
132
+ file. Defaults to False.
133
+ log_dir (Optional[str]): Custom directory path for log files.
134
+ If None, defaults to "browser_log". Defaults to None.
135
+ session_id (Optional[str]): Session identifier. Defaults to None.
136
+ default_start_url (str): Default URL to start with. Defaults
137
+ to "https://google.com/".
138
+ default_timeout (Optional[int]): Default timeout in
139
+ milliseconds. Defaults to None.
140
+ short_timeout (Optional[int]): Short timeout in milliseconds.
141
+ Defaults to None.
142
+ navigation_timeout (Optional[int]): Navigation timeout in
143
+ milliseconds. Defaults to None.
144
+ network_idle_timeout (Optional[int]): Network idle timeout in
145
+ milliseconds. Defaults to None.
146
+ screenshot_timeout (Optional[int]): Screenshot timeout in
147
+ milliseconds. Defaults to None.
148
+ page_stability_timeout (Optional[int]): Page stability timeout
149
+ in milliseconds. Defaults to None.
150
+ dom_content_loaded_timeout (Optional[int]): DOM content loaded
151
+ timeout in milliseconds. Defaults to None.
152
+ viewport_limit (bool): Whether to filter page snapshot
153
+ elements to only those visible in the current viewport.
154
+ When True, only elements within the current viewport
155
+ bounds will be included in snapshots.
156
+ When False (default), all elements on the page are
157
+ included. Defaults to False.
158
+ connect_over_cdp (bool): Whether to connect to an existing
159
+ browser via Chrome DevTools Protocol. Defaults to False.
160
+ cdp_url (Optional[str]): WebSocket endpoint URL for CDP
161
+ connection (e.g., 'ws://localhost:9222/devtools/browser/...').
162
+ Required when connect_over_cdp is True. Defaults to None.
163
+ cdp_keep_current_page (bool): When True and using CDP mode,
164
+ won't create new pages but use the existing one. Defaults to False.
165
+ full_visual_mode (bool): When True, browser actions like click,
166
+ browser_open, visit_page, etc. will not return snapshots.
167
+ Defaults to False.
168
+ """
169
+ super().__init__()
170
+ RegisteredAgentToolkit.__init__(self)
171
+
172
+ self.config_loader = ConfigLoader.from_kwargs(
173
+ headless=headless,
174
+ user_data_dir=user_data_dir,
175
+ stealth=stealth,
176
+ default_start_url=default_start_url,
177
+ default_timeout=default_timeout,
178
+ short_timeout=short_timeout,
179
+ navigation_timeout=navigation_timeout,
180
+ network_idle_timeout=network_idle_timeout,
181
+ screenshot_timeout=screenshot_timeout,
182
+ page_stability_timeout=page_stability_timeout,
183
+ dom_content_loaded_timeout=dom_content_loaded_timeout,
184
+ viewport_limit=viewport_limit,
185
+ cache_dir=cache_dir,
186
+ browser_log_to_file=browser_log_to_file,
187
+ log_dir=log_dir,
188
+ session_id=session_id,
189
+ enabled_tools=enabled_tools,
190
+ connect_over_cdp=connect_over_cdp,
191
+ cdp_url=cdp_url,
192
+ cdp_keep_current_page=cdp_keep_current_page,
193
+ full_visual_mode=full_visual_mode,
194
+ )
195
+
196
+ browser_config = self.config_loader.get_browser_config()
197
+ toolkit_config = self.config_loader.get_toolkit_config()
198
+
199
+ if (
200
+ browser_config.cdp_keep_current_page
201
+ and default_start_url is not None
202
+ ):
203
+ raise ValueError(
204
+ "Cannot use default_start_url with "
205
+ "cdp_keep_current_page=True. When cdp_keep_current_page "
206
+ "is True, the browser will keep the current page and not "
207
+ "navigate to any URL."
208
+ )
209
+
210
+ self._headless = browser_config.headless
211
+ self._user_data_dir = browser_config.user_data_dir
212
+ self._stealth = browser_config.stealth
213
+ self._cache_dir = toolkit_config.cache_dir
214
+ self._browser_log_to_file = toolkit_config.browser_log_to_file
215
+ self._default_start_url = browser_config.default_start_url
216
+ self._session_id = toolkit_config.session_id or "default"
217
+ self._viewport_limit = browser_config.viewport_limit
218
+ self._full_visual_mode = browser_config.full_visual_mode
219
+
220
+ self._default_timeout = browser_config.default_timeout
221
+ self._short_timeout = browser_config.short_timeout
222
+ self._navigation_timeout = browser_config.navigation_timeout
223
+ self._network_idle_timeout = browser_config.network_idle_timeout
224
+ self._screenshot_timeout = browser_config.screenshot_timeout
225
+ self._page_stability_timeout = browser_config.page_stability_timeout
226
+ self._dom_content_loaded_timeout = (
227
+ browser_config.dom_content_loaded_timeout
228
+ )
229
+
230
+ if enabled_tools is None:
231
+ self.enabled_tools = self.DEFAULT_TOOLS.copy()
232
+ else:
233
+ invalid_tools = [
234
+ tool for tool in enabled_tools if tool not in self.ALL_TOOLS
235
+ ]
236
+ if invalid_tools:
237
+ raise ValueError(
238
+ f"Invalid tools specified: {invalid_tools}. "
239
+ f"Available tools: {self.ALL_TOOLS}"
240
+ )
241
+ self.enabled_tools = enabled_tools.copy()
242
+
243
+ logger.info(f"Enabled tools: {self.enabled_tools}")
244
+
245
+ self._ws_wrapper: Optional[WebSocketBrowserWrapper] = None
246
+ self._ws_config = self.config_loader.to_ws_config()
247
+
248
+ async def _ensure_ws_wrapper(self):
249
+ """Ensure WebSocket wrapper is initialized."""
250
+ if self._ws_wrapper is None:
251
+ self._ws_wrapper = WebSocketBrowserWrapper(self._ws_config)
252
+ await self._ws_wrapper.start()
253
+
254
+ async def _get_ws_wrapper(self) -> WebSocketBrowserWrapper:
255
+ """Get the WebSocket wrapper, initializing if needed."""
256
+ await self._ensure_ws_wrapper()
257
+ if self._ws_wrapper is None:
258
+ raise RuntimeError("Failed to initialize WebSocket wrapper")
259
+ return self._ws_wrapper
260
+
261
+ def __del__(self):
262
+ r"""Cleanup browser resources on garbage collection."""
263
+ try:
264
+ import sys
265
+
266
+ if getattr(sys, "is_finalizing", lambda: False)():
267
+ return
268
+
269
+ import asyncio
270
+
271
+ is_cdp = (
272
+ self._ws_config.get('connectOverCdp', False)
273
+ if hasattr(self, '_ws_config')
274
+ else False
275
+ )
276
+
277
+ try:
278
+ loop = asyncio.get_event_loop()
279
+ if not loop.is_closed() and not loop.is_running():
280
+ try:
281
+ if is_cdp:
282
+ # CDP: disconnect only
283
+ loop.run_until_complete(
284
+ asyncio.wait_for(
285
+ self.disconnect_websocket(), timeout=2.0
286
+ )
287
+ )
288
+ else:
289
+ loop.run_until_complete(
290
+ asyncio.wait_for(
291
+ self.browser_close(), timeout=2.0
292
+ )
293
+ )
294
+ except asyncio.TimeoutError:
295
+ pass
296
+ except (RuntimeError, ImportError):
297
+ pass
298
+ except Exception:
299
+ pass
300
+
301
+ @property
302
+ def cache_dir(self) -> str:
303
+ """Get the cache directory."""
304
+ return self._cache_dir
305
+
306
+ async def browser_open(self) -> Dict[str, Any]:
307
+ r"""Starts a new browser session. This must be the first browser
308
+ action.
309
+
310
+ This method initializes the browser and navigates to a default start
311
+ page. To visit a specific URL, use `visit_page` after this.
312
+
313
+ Returns:
314
+ Dict[str, Any]: A dictionary with the result of the action:
315
+ - "result" (str): Confirmation of the action.
316
+ - "snapshot" (str): A textual snapshot of interactive
317
+ elements.
318
+ - "tabs" (List[Dict]): Information about all open tabs.
319
+ - "current_tab" (int): Index of the active tab.
320
+ - "total_tabs" (int): Total number of open tabs.
321
+ """
322
+ try:
323
+ ws_wrapper = await self._get_ws_wrapper()
324
+ result = await ws_wrapper.open_browser(self._default_start_url)
325
+
326
+ tab_info = await ws_wrapper.get_tab_info()
327
+ result.update(
328
+ {
329
+ "tabs": tab_info,
330
+ "current_tab": next(
331
+ (
332
+ i
333
+ for i, tab in enumerate(tab_info)
334
+ if tab.get("is_current")
335
+ ),
336
+ 0,
337
+ ),
338
+ "total_tabs": len(tab_info),
339
+ }
340
+ )
341
+
342
+ return result
343
+ except Exception as e:
344
+ logger.error(f"Failed to open browser: {e}")
345
+ return {
346
+ "result": f"Error opening browser: {e}",
347
+ "snapshot": "",
348
+ "tabs": [],
349
+ "current_tab": 0,
350
+ "total_tabs": 0,
351
+ }
352
+
353
+ async def browser_close(self) -> str:
354
+ r"""Closes the browser session, releasing all resources.
355
+
356
+ This should be called at the end of a task for cleanup.
357
+
358
+ Returns:
359
+ str: A confirmation message.
360
+ """
361
+ try:
362
+ if self._ws_wrapper:
363
+ await self._ws_wrapper.stop()
364
+ self._ws_wrapper = None
365
+ return "Browser session closed."
366
+ except Exception as e:
367
+ logger.error(f"Failed to close browser: {e}")
368
+ return f"Error closing browser: {e}"
369
+
370
+ async def disconnect_websocket(self) -> str:
371
+ r"""Disconnects the WebSocket connection without closing the browser.
372
+
373
+ This is useful when using CDP mode where the browser should
374
+ remain open.
375
+
376
+ Returns:
377
+ str: A confirmation message.
378
+ """
379
+ try:
380
+ if self._ws_wrapper:
381
+ is_cdp = self._ws_config.get('connectOverCdp', False)
382
+
383
+ if is_cdp:
384
+ # CDP: disconnect only
385
+ await self._ws_wrapper.disconnect_only()
386
+ else:
387
+ await self._ws_wrapper.stop()
388
+
389
+ self._ws_wrapper = None
390
+ return "WebSocket disconnected."
391
+ except Exception as e:
392
+ logger.error(f"Failed to disconnect WebSocket: {e}")
393
+ return f"Error disconnecting WebSocket: {e}"
394
+
395
+ async def browser_visit_page(self, url: str) -> Dict[str, Any]:
396
+ r"""Opens a URL in a new browser tab and switches to it.
397
+
398
+ Args:
399
+ url (str): The web address to load. This should be a valid and
400
+ existing URL.
401
+
402
+ Returns:
403
+ Dict[str, Any]: A dictionary with the result of the action:
404
+ - "result" (str): Confirmation of the action.
405
+ - "snapshot" (str): A textual snapshot of the new page.
406
+ - "tabs" (List[Dict]): Information about all open tabs.
407
+ - "current_tab" (int): Index of the new active tab.
408
+ - "total_tabs" (int): Total number of open tabs.
409
+ """
410
+ try:
411
+ ws_wrapper = await self._get_ws_wrapper()
412
+ result = await ws_wrapper.visit_page(url)
413
+
414
+ tab_info = await ws_wrapper.get_tab_info()
415
+ result.update(
416
+ {
417
+ "tabs": tab_info,
418
+ "current_tab": next(
419
+ (
420
+ i
421
+ for i, tab in enumerate(tab_info)
422
+ if tab.get("is_current")
423
+ ),
424
+ 0,
425
+ ),
426
+ "total_tabs": len(tab_info),
427
+ }
428
+ )
429
+
430
+ return result
431
+ except Exception as e:
432
+ logger.error(f"Failed to visit page: {e}")
433
+ return {
434
+ "result": f"Error visiting page: {e}",
435
+ "snapshot": "",
436
+ "tabs": [],
437
+ "current_tab": 0,
438
+ "total_tabs": 0,
439
+ }
440
+
441
+ async def browser_back(self) -> Dict[str, Any]:
442
+ r"""Goes back to the previous page in the browser history.
443
+
444
+ This action simulates using the browser's "back" button in the
445
+ currently active tab.
446
+
447
+ Returns:
448
+ Dict[str, Any]: A dictionary with the result of the action:
449
+ - "result" (str): Confirmation of the action.
450
+ - "snapshot" (str): A textual snapshot of the previous page.
451
+ - "tabs" (List[Dict]): Information about all open tabs.
452
+ - "current_tab" (int): Index of the active tab.
453
+ - "total_tabs" (int): Total number of open tabs.
454
+ """
455
+ try:
456
+ ws_wrapper = await self._get_ws_wrapper()
457
+ result = await ws_wrapper.back()
458
+
459
+ tab_info = await ws_wrapper.get_tab_info()
460
+ result.update(
461
+ {
462
+ "tabs": tab_info,
463
+ "current_tab": next(
464
+ (
465
+ i
466
+ for i, tab in enumerate(tab_info)
467
+ if tab.get("is_current")
468
+ ),
469
+ 0,
470
+ ),
471
+ "total_tabs": len(tab_info),
472
+ }
473
+ )
474
+
475
+ return result
476
+ except Exception as e:
477
+ logger.error(f"Failed to navigate back: {e}")
478
+ return {
479
+ "result": f"Error navigating back: {e}",
480
+ "snapshot": "",
481
+ "tabs": [],
482
+ "current_tab": 0,
483
+ "total_tabs": 0,
484
+ }
485
+
486
+ async def browser_forward(self) -> Dict[str, Any]:
487
+ r"""Goes forward to the next page in the browser history.
488
+
489
+ This action simulates using the browser's "forward" button in the
490
+ currently active tab.
491
+
492
+ Returns:
493
+ Dict[str, Any]: A dictionary with the result of the action:
494
+ - "result" (str): Confirmation of the action.
495
+ - "snapshot" (str): A textual snapshot of the next page.
496
+ - "tabs" (List[Dict]): Information about all open tabs.
497
+ - "current_tab" (int): Index of the active tab.
498
+ - "total_tabs" (int): Total number of open tabs.
499
+ """
500
+ try:
501
+ ws_wrapper = await self._get_ws_wrapper()
502
+ result = await ws_wrapper.forward()
503
+
504
+ tab_info = await ws_wrapper.get_tab_info()
505
+ result.update(
506
+ {
507
+ "tabs": tab_info,
508
+ "current_tab": next(
509
+ (
510
+ i
511
+ for i, tab in enumerate(tab_info)
512
+ if tab.get("is_current")
513
+ ),
514
+ 0,
515
+ ),
516
+ "total_tabs": len(tab_info),
517
+ }
518
+ )
519
+
520
+ return result
521
+ except Exception as e:
522
+ logger.error(f"Failed to navigate forward: {e}")
523
+ return {
524
+ "result": f"Error navigating forward: {e}",
525
+ "snapshot": "",
526
+ "tabs": [],
527
+ "current_tab": 0,
528
+ "total_tabs": 0,
529
+ }
530
+
531
+ async def browser_get_page_snapshot(self) -> str:
532
+ r"""Gets a textual snapshot of the page's interactive elements.
533
+
534
+ The snapshot lists elements like buttons, links, and inputs,
535
+ each with
536
+ a unique `ref` ID. This ID is used by other tools (e.g., `click`,
537
+ `type`) to interact with a specific element. This tool provides no
538
+ visual information.
539
+
540
+ If viewport_limit is enabled, only elements within the current
541
+ viewport
542
+ will be included in the snapshot.
543
+
544
+ Returns:
545
+ str: A formatted string representing the interactive elements and
546
+ their `ref` IDs. For example:
547
+ '- link "Sign In" [ref=1]'
548
+ '- textbox "Username" [ref=2]'
549
+ """
550
+ try:
551
+ ws_wrapper = await self._get_ws_wrapper()
552
+ return await ws_wrapper.get_page_snapshot(self._viewport_limit)
553
+ except Exception as e:
554
+ logger.error(f"Failed to get page snapshot: {e}")
555
+ return f"Error capturing snapshot: {e}"
556
+
557
+ async def browser_get_som_screenshot(
558
+ self,
559
+ read_image: bool = True,
560
+ ) -> "str | ToolResult":
561
+ r"""Captures a screenshot with interactive elements highlighted.
562
+
563
+ "SoM" stands for "Set of Marks". This tool takes a screenshot and
564
+ draws
565
+ boxes around clickable elements, overlaying a `ref` ID on each. Use
566
+ this for a visual understanding of the page, especially when the
567
+ textual snapshot is not enough.
568
+
569
+ Args:
570
+ read_image (bool, optional): If `True`, the screenshot image will
571
+ be included in the agent's context for direct visual analysis.
572
+ If `False`, only a text message (including the saved file
573
+ path) will be returned.
574
+ (default: :obj:`True`)
575
+
576
+ Returns:
577
+ str | ToolResult: If `read_image` is `True`, returns a ToolResult
578
+ containing the text message and the screenshot image (which
579
+ will be automatically added to agent's context). If `False`,
580
+ returns a string with the file path only.
581
+ """
582
+ import base64
583
+ import datetime
584
+ import os
585
+ import urllib.parse
586
+
587
+ from camel.utils import sanitize_filename
588
+
589
+ try:
590
+ ws_wrapper = await self._get_ws_wrapper()
591
+ result = await ws_wrapper.get_som_screenshot()
592
+
593
+ result_text = result.text
594
+ file_path = None
595
+
596
+ if result.images:
597
+ cache_dir = os.path.abspath(self._cache_dir)
598
+ os.makedirs(cache_dir, exist_ok=True)
599
+
600
+ try:
601
+ page_info = await ws_wrapper.get_tab_info()
602
+ current_tab = next(
603
+ (tab for tab in page_info if tab.get('is_current')),
604
+ None,
605
+ )
606
+ url = current_tab['url'] if current_tab else 'unknown'
607
+ except Exception:
608
+ url = 'unknown'
609
+
610
+ parsed_url = urllib.parse.urlparse(url)
611
+ url_name = sanitize_filename(
612
+ str(parsed_url.path) or 'homepage', max_length=241
613
+ )
614
+ timestamp = datetime.datetime.now().strftime("%m%d%H%M%S")
615
+ file_path = os.path.join(
616
+ cache_dir, f"{url_name}_{timestamp}_som.png"
617
+ )
618
+
619
+ for _, image_data in enumerate(result.images):
620
+ if image_data.startswith('data:image/png;base64,'):
621
+ base64_data = image_data.split(',', 1)[1]
622
+
623
+ image_bytes = base64.b64decode(base64_data)
624
+ with open(file_path, 'wb') as f:
625
+ f.write(image_bytes)
626
+
627
+ logger.info(f"Screenshot saved to: {file_path}")
628
+
629
+ result_text += f" (saved to: {file_path})"
630
+ break
631
+
632
+ # Return ToolResult with image if read_image is True
633
+ if read_image and result.images:
634
+ logger.info(
635
+ f"Returning ToolResult with {len(result.images)} image(s) "
636
+ "for agent context"
637
+ )
638
+ return ToolResult(
639
+ text=result_text,
640
+ images=result.images, # Base64 images from WebSocket
641
+ )
642
+ else:
643
+ # Return plain text if read_image is False
644
+ return result_text
645
+ except Exception as e:
646
+ logger.error(f"Failed to get screenshot: {e}")
647
+ return f"Error capturing screenshot: {e}"
648
+
649
+ async def browser_click(self, *, ref: str) -> Dict[str, Any]:
650
+ r"""Performs a click on an element on the page.
651
+
652
+ Args:
653
+ ref (str): The `ref` ID of the element to click. This ID is
654
+ obtained from a page snapshot (`get_page_snapshot` or
655
+ `get_som_screenshot`).
656
+
657
+ Returns:
658
+ Dict[str, Any]: A dictionary with the result of the action:
659
+ - "result" (str): Confirmation of the action.
660
+ - "snapshot" (str): A textual snapshot of the page after the
661
+ click.
662
+ - "tabs" (List[Dict]): Information about all open tabs.
663
+ - "current_tab" (int): Index of the active tab.
664
+ - "total_tabs" (int): Total number of open tabs.
665
+ """
666
+ try:
667
+ ws_wrapper = await self._get_ws_wrapper()
668
+ result = await ws_wrapper.click(ref)
669
+
670
+ tab_info = await ws_wrapper.get_tab_info()
671
+
672
+ response = {
673
+ "result": result.get("result", ""),
674
+ "snapshot": result.get("snapshot", ""),
675
+ "tabs": tab_info,
676
+ "current_tab": next(
677
+ (
678
+ i
679
+ for i, tab in enumerate(tab_info)
680
+ if tab.get("is_current")
681
+ ),
682
+ 0,
683
+ ),
684
+ "total_tabs": len(tab_info),
685
+ }
686
+
687
+ if "newTabId" in result:
688
+ response["newTabId"] = result["newTabId"]
689
+
690
+ if "timing" in result:
691
+ response["timing"] = result["timing"]
692
+
693
+ return response
694
+ except Exception as e:
695
+ logger.error(f"Failed to click element: {e}")
696
+ return {
697
+ "result": f"Error clicking element: {e}",
698
+ "snapshot": "",
699
+ "tabs": [],
700
+ "current_tab": 0,
701
+ "total_tabs": 0,
702
+ }
703
+
704
+ async def browser_type(
705
+ self,
706
+ *,
707
+ ref: Optional[str] = None,
708
+ text: Optional[str] = None,
709
+ inputs: Optional[List[Dict[str, str]]] = None,
710
+ ) -> Dict[str, Any]:
711
+ r"""Types text into one or more input elements on the page.
712
+
713
+ This method supports two modes:
714
+ 1. Single input mode (backward compatible): Provide 'ref' and 'text'
715
+ 2. Multiple inputs mode: Provide 'inputs' as a list of dictionaries
716
+ with 'ref' and 'text' keys
717
+
718
+ Args:
719
+ ref (Optional[str]): The `ref` ID of the input element, from a
720
+ snapshot. Required when using single input mode.
721
+ text (Optional[str]): The text to type into the element. Required
722
+ when using single input mode.
723
+ inputs (Optional[List[Dict[str, str]]]): List of dictionaries,
724
+ each containing 'ref' and 'text' keys for typing into multiple
725
+ elements. Example: [{'ref': '1', 'text': 'username'},
726
+ {'ref': '2', 'text': 'password'}]
727
+
728
+ Returns:
729
+ Dict[str, Any]: A dictionary with the result of the action:
730
+ - "result" (str): Confirmation of the action.
731
+ - "snapshot" (str): A textual snapshot of the page after
732
+ typing.
733
+ - "tabs" (List[Dict]): Information about all open tabs.
734
+ - "current_tab" (int): Index of the active tab.
735
+ - "total_tabs" (int): Total number of open tabs.
736
+ - "details" (Dict[str, Any]): When using multiple inputs,
737
+ contains success/error status for each ref.
738
+ """
739
+ try:
740
+ ws_wrapper = await self._get_ws_wrapper()
741
+
742
+ if ref is not None and text is not None:
743
+ result = await ws_wrapper.type(ref, text)
744
+ elif inputs is not None:
745
+ result = await ws_wrapper.type_multiple(inputs)
746
+ else:
747
+ raise ValueError(
748
+ "Either provide 'ref' and 'text' for single input, "
749
+ "or 'inputs' for multiple inputs"
750
+ )
751
+
752
+ tab_info = await ws_wrapper.get_tab_info()
753
+ result.update(
754
+ {
755
+ "tabs": tab_info,
756
+ "current_tab": next(
757
+ (
758
+ i
759
+ for i, tab in enumerate(tab_info)
760
+ if tab.get("is_current")
761
+ ),
762
+ 0,
763
+ ),
764
+ "total_tabs": len(tab_info),
765
+ }
766
+ )
767
+
768
+ return result
769
+ except Exception as e:
770
+ logger.error(f"Failed to type text: {e}")
771
+ return {
772
+ "result": f"Error typing text: {e}",
773
+ "snapshot": "",
774
+ "tabs": [],
775
+ "current_tab": 0,
776
+ "total_tabs": 0,
777
+ }
778
+
779
+ async def browser_select(self, *, ref: str, value: str) -> Dict[str, Any]:
780
+ r"""Selects an option in a dropdown (`<select>`) element.
781
+
782
+ Args:
783
+ ref (str): The `ref` ID of the `<select>` element.
784
+ value (str): The `value` attribute of the `<option>` to select,
785
+ not its visible text.
786
+
787
+ Returns:
788
+ Dict[str, Any]: A dictionary with the result of the action:
789
+ - "result" (str): Confirmation of the action.
790
+ - "snapshot" (str): A snapshot of the page after the
791
+ selection.
792
+ - "tabs" (List[Dict]): Information about all open tabs.
793
+ - "current_tab" (int): Index of the active tab.
794
+ - "total_tabs" (int): Total number of open tabs.
795
+ """
796
+ try:
797
+ ws_wrapper = await self._get_ws_wrapper()
798
+ result = await ws_wrapper.select(ref, value)
799
+
800
+ tab_info = await ws_wrapper.get_tab_info()
801
+ result.update(
802
+ {
803
+ "tabs": tab_info,
804
+ "current_tab": next(
805
+ (
806
+ i
807
+ for i, tab in enumerate(tab_info)
808
+ if tab.get("is_current")
809
+ ),
810
+ 0,
811
+ ),
812
+ "total_tabs": len(tab_info),
813
+ }
814
+ )
815
+
816
+ return result
817
+ except Exception as e:
818
+ logger.error(f"Failed to select option: {e}")
819
+ return {
820
+ "result": f"Error selecting option: {e}",
821
+ "snapshot": "",
822
+ "tabs": [],
823
+ "current_tab": 0,
824
+ "total_tabs": 0,
825
+ }
826
+
827
+ async def browser_scroll(
828
+ self, *, direction: str, amount: int = 500
829
+ ) -> Dict[str, Any]:
830
+ r"""Scrolls the current page window.
831
+
832
+ Args:
833
+ direction (str): The direction to scroll: 'up' or 'down'.
834
+ amount (int): The number of pixels to scroll, default is 500.
835
+
836
+ Returns:
837
+ Dict[str, Any]: A dictionary with the result of the action:
838
+ - "result" (str): Confirmation of the action.
839
+ - "snapshot" (str): A snapshot of the page after scrolling.
840
+ - "tabs" (List[Dict]): Information about all open tabs.
841
+ - "current_tab" (int): Index of the active tab.
842
+ - "total_tabs" (int): Total number of open tabs.
843
+ """
844
+ try:
845
+ ws_wrapper = await self._get_ws_wrapper()
846
+ result = await ws_wrapper.scroll(direction, amount)
847
+
848
+ tab_info = await ws_wrapper.get_tab_info()
849
+ result.update(
850
+ {
851
+ "tabs": tab_info,
852
+ "current_tab": next(
853
+ (
854
+ i
855
+ for i, tab in enumerate(tab_info)
856
+ if tab.get("is_current")
857
+ ),
858
+ 0,
859
+ ),
860
+ "total_tabs": len(tab_info),
861
+ }
862
+ )
863
+
864
+ return result
865
+ except Exception as e:
866
+ logger.error(f"Failed to scroll: {e}")
867
+ return {
868
+ "result": f"Error scrolling: {e}",
869
+ "snapshot": "",
870
+ "tabs": [],
871
+ "current_tab": 0,
872
+ "total_tabs": 0,
873
+ }
874
+
875
+ async def browser_enter(self) -> Dict[str, Any]:
876
+ r"""Simulates pressing the Enter key on the currently focused
877
+ element.
878
+
879
+ This is useful for submitting forms or search queries after using the
880
+ `type` tool.
881
+
882
+ Returns:
883
+ Dict[str, Any]: A dictionary with the result of the action:
884
+ - "result" (str): Confirmation of the action.
885
+ - "snapshot" (str): A new page snapshot, as this action often
886
+ triggers navigation.
887
+ - "tabs" (List[Dict]): Information about all open tabs.
888
+ - "current_tab" (int): Index of the active tab.
889
+ - "total_tabs" (int): Total number of open tabs.
890
+ """
891
+ try:
892
+ ws_wrapper = await self._get_ws_wrapper()
893
+ result = await ws_wrapper.enter()
894
+
895
+ tab_info = await ws_wrapper.get_tab_info()
896
+ result.update(
897
+ {
898
+ "tabs": tab_info,
899
+ "current_tab": next(
900
+ (
901
+ i
902
+ for i, tab in enumerate(tab_info)
903
+ if tab.get("is_current")
904
+ ),
905
+ 0,
906
+ ),
907
+ "total_tabs": len(tab_info),
908
+ }
909
+ )
910
+
911
+ return result
912
+ except Exception as e:
913
+ logger.error(f"Failed to press enter: {e}")
914
+ return {
915
+ "result": f"Error pressing enter: {e}",
916
+ "snapshot": "",
917
+ "tabs": [],
918
+ "current_tab": 0,
919
+ "total_tabs": 0,
920
+ }
921
+
922
+ async def browser_mouse_control(
923
+ self, *, control: str, x: float, y: float
924
+ ) -> Dict[str, Any]:
925
+ r"""Control the mouse to interact with browser with x, y coordinates
926
+
927
+ Args:
928
+ control ([str]): The action to perform: 'click', 'right_click'
929
+ or 'dblclick'.
930
+ x (float): x-coordinate for the control action.
931
+ y (float): y-coordinate for the control action.
932
+
933
+ Returns:
934
+ Dict[str, Any]: A dictionary with the result of the action:
935
+ - "result" (str): Confirmation of the action.
936
+ - "snapshot" (str): A snapshot of the page after mouse
937
+ control action.
938
+ - "tabs" (List[Dict]): Information about all open tabs.
939
+ - "current_tab" (int): Index of the active tab.
940
+ - "total_tabs" (int): Total number of open tabs.
941
+ """
942
+ try:
943
+ ws_wrapper = await self._get_ws_wrapper()
944
+ result = await ws_wrapper.mouse_control(control, x, y)
945
+
946
+ tab_info = await ws_wrapper.get_tab_info()
947
+ result.update(
948
+ {
949
+ "tabs": tab_info,
950
+ "current_tab": next(
951
+ (
952
+ i
953
+ for i, tab in enumerate(tab_info)
954
+ if tab.get("is_current")
955
+ ),
956
+ 0,
957
+ ),
958
+ "total_tabs": len(tab_info),
959
+ }
960
+ )
961
+
962
+ return result
963
+ except Exception as e:
964
+ logger.error(f"Failed to control mouse: {e}")
965
+ return {
966
+ "result": f"Error with mouse control: {e}",
967
+ "snapshot": "",
968
+ "tabs": [],
969
+ "current_tab": 0,
970
+ "total_tabs": 0,
971
+ }
972
+
973
+ async def browser_mouse_drag(
974
+ self, *, from_ref: str, to_ref: str
975
+ ) -> Dict[str, Any]:
976
+ r"""Control the mouse to drag and drop in the browser using ref IDs.
977
+
978
+ Args:
979
+ from_ref (str): The `ref` ID of the source element to drag from.
980
+ to_ref (str): The `ref` ID of the target element to drag to.
981
+
982
+ Returns:
983
+ Dict[str, Any]: A dictionary with the result of the action:
984
+ - "result" (str): Confirmation of the action.
985
+ - "snapshot" (str): A new page snapshot.
986
+ - "tabs" (List[Dict]): Information about all open tabs.
987
+ - "current_tab" (int): Index of the active tab.
988
+ - "total_tabs" (int): Total number of open tabs.
989
+ """
990
+ try:
991
+ ws_wrapper = await self._get_ws_wrapper()
992
+ result = await ws_wrapper.mouse_drag(from_ref, to_ref)
993
+
994
+ tab_info = await ws_wrapper.get_tab_info()
995
+ result.update(
996
+ {
997
+ "tabs": tab_info,
998
+ "current_tab": next(
999
+ (
1000
+ i
1001
+ for i, tab in enumerate(tab_info)
1002
+ if tab.get("is_current")
1003
+ ),
1004
+ 0,
1005
+ ),
1006
+ "total_tabs": len(tab_info),
1007
+ }
1008
+ )
1009
+
1010
+ return result
1011
+ except Exception as e:
1012
+ logger.error(f"Error with mouse drag and drop: {e}")
1013
+ return {
1014
+ "result": f"Error with mouse drag and drop: {e}",
1015
+ "snapshot": "",
1016
+ "tabs": [],
1017
+ "current_tab": 0,
1018
+ "total_tabs": 0,
1019
+ }
1020
+
1021
+ async def browser_press_key(self, *, keys: List[str]) -> Dict[str, Any]:
1022
+ r"""Press key and key combinations.
1023
+ Supports single key press or combination of keys by concatenating
1024
+ them with '+' separator.
1025
+
1026
+ Args:
1027
+ keys (List[str]): key or list of keys.
1028
+
1029
+ Returns:
1030
+ Dict[str, Any]: A dictionary with the result of the action:
1031
+ - "result" (str): Confirmation of the action.
1032
+ - "snapshot" (str): A snapshot of the page after
1033
+ press key action.
1034
+ - "tabs" (List[Dict]): Information about all open tabs.
1035
+ - "current_tab" (int): Index of the active tab.
1036
+ - "total_tabs" (int): Total number of open tabs.
1037
+ """
1038
+ try:
1039
+ ws_wrapper = await self._get_ws_wrapper()
1040
+ result = await ws_wrapper.press_key(keys)
1041
+
1042
+ tab_info = await ws_wrapper.get_tab_info()
1043
+ result.update(
1044
+ {
1045
+ "tabs": tab_info,
1046
+ "current_tab": next(
1047
+ (
1048
+ i
1049
+ for i, tab in enumerate(tab_info)
1050
+ if tab.get("is_current")
1051
+ ),
1052
+ 0,
1053
+ ),
1054
+ "total_tabs": len(tab_info),
1055
+ }
1056
+ )
1057
+
1058
+ return result
1059
+ except Exception as e:
1060
+ logger.error(f"Failed to press key: {e}")
1061
+ return {
1062
+ "result": f"Error with press key: {e}",
1063
+ "snapshot": "",
1064
+ "tabs": [],
1065
+ "current_tab": 0,
1066
+ "total_tabs": 0,
1067
+ }
1068
+
1069
+ async def browser_switch_tab(self, *, tab_id: str) -> Dict[str, Any]:
1070
+ r"""Switches to a different browser tab using its ID.
1071
+
1072
+ After switching, all actions will apply to the new tab. Use
1073
+ `get_tab_info` to find the ID of the tab you want to switch to.
1074
+
1075
+ Args:
1076
+ tab_id (str): The ID of the tab to activate.
1077
+
1078
+ Returns:
1079
+ Dict[str, Any]: A dictionary with the result of the action:
1080
+ - "result" (str): Confirmation of the action.
1081
+ - "snapshot" (str): A snapshot of the newly active tab.
1082
+ - "tabs" (List[Dict]): Information about all open tabs.
1083
+ - "current_tab" (int): Index of the new active tab.
1084
+ - "total_tabs" (int): Total number of open tabs.
1085
+ """
1086
+ try:
1087
+ ws_wrapper = await self._get_ws_wrapper()
1088
+ result = await ws_wrapper.switch_tab(tab_id)
1089
+
1090
+ tab_info = await ws_wrapper.get_tab_info()
1091
+ result.update(
1092
+ {
1093
+ "tabs": tab_info,
1094
+ "current_tab": next(
1095
+ (
1096
+ i
1097
+ for i, tab in enumerate(tab_info)
1098
+ if tab.get("is_current")
1099
+ ),
1100
+ 0,
1101
+ ),
1102
+ "total_tabs": len(tab_info),
1103
+ }
1104
+ )
1105
+
1106
+ return result
1107
+ except Exception as e:
1108
+ logger.error(f"Failed to switch tab: {e}")
1109
+ return {
1110
+ "result": f"Error switching tab: {e}",
1111
+ "snapshot": "",
1112
+ "tabs": [],
1113
+ "current_tab": 0,
1114
+ "total_tabs": 0,
1115
+ }
1116
+
1117
+ async def browser_close_tab(self, *, tab_id: str) -> Dict[str, Any]:
1118
+ r"""Closes a browser tab using its ID.
1119
+
1120
+ Use `get_tab_info` to find the ID of the tab to close. After
1121
+ closing, the browser will switch to another tab if available.
1122
+
1123
+ Args:
1124
+ tab_id (str): The ID of the tab to close.
1125
+
1126
+ Returns:
1127
+ Dict[str, Any]: A dictionary with the result of the action:
1128
+ - "result" (str): Confirmation of the action.
1129
+ - "snapshot" (str): A snapshot of the active tab after
1130
+ closure.
1131
+ - "tabs" (List[Dict]): Information about remaining tabs.
1132
+ - "current_tab" (int): Index of the new active tab.
1133
+ - "total_tabs" (int): Total number of remaining tabs.
1134
+ """
1135
+ try:
1136
+ ws_wrapper = await self._get_ws_wrapper()
1137
+ result = await ws_wrapper.close_tab(tab_id)
1138
+
1139
+ tab_info = await ws_wrapper.get_tab_info()
1140
+ result.update(
1141
+ {
1142
+ "tabs": tab_info,
1143
+ "current_tab": next(
1144
+ (
1145
+ i
1146
+ for i, tab in enumerate(tab_info)
1147
+ if tab.get("is_current")
1148
+ ),
1149
+ 0,
1150
+ ),
1151
+ "total_tabs": len(tab_info),
1152
+ }
1153
+ )
1154
+
1155
+ return result
1156
+ except Exception as e:
1157
+ logger.error(f"Failed to close tab: {e}")
1158
+ return {
1159
+ "result": f"Error closing tab: {e}",
1160
+ "snapshot": "",
1161
+ "tabs": [],
1162
+ "current_tab": 0,
1163
+ "total_tabs": 0,
1164
+ }
1165
+
1166
+ async def browser_get_tab_info(self) -> Dict[str, Any]:
1167
+ r"""Gets a list of all open browser tabs and their information.
1168
+
1169
+ This includes each tab's index, title, and URL, and indicates which
1170
+ tab is currently active. Use this to manage multiple tabs.
1171
+
1172
+ Returns:
1173
+ Dict[str, Any]: A dictionary with tab information:
1174
+ - "tabs" (List[Dict]): A list of open tabs, each with:
1175
+ - "index" (int): The tab's zero-based index.
1176
+ - "title" (str): The page title.
1177
+ - "url" (str): The current URL.
1178
+ - "is_current" (bool): True if the tab is active.
1179
+ - "current_tab" (int): Index of the active tab.
1180
+ - "total_tabs" (int): Total number of open tabs.
1181
+ """
1182
+ try:
1183
+ ws_wrapper = await self._get_ws_wrapper()
1184
+ tab_info = await ws_wrapper.get_tab_info()
1185
+
1186
+ return {
1187
+ "tabs": tab_info,
1188
+ "current_tab": next(
1189
+ (
1190
+ i
1191
+ for i, tab in enumerate(tab_info)
1192
+ if tab.get("is_current")
1193
+ ),
1194
+ 0,
1195
+ ),
1196
+ "total_tabs": len(tab_info),
1197
+ }
1198
+ except Exception as e:
1199
+ logger.error(f"Failed to get tab info: {e}")
1200
+ return {
1201
+ "tabs": [],
1202
+ "current_tab": 0,
1203
+ "total_tabs": 0,
1204
+ }
1205
+
1206
+ async def browser_console_view(self) -> Dict[str, Any]:
1207
+ r"""View current page console logs.
1208
+
1209
+ Returns:
1210
+ Dict[str, Any]: A dictionary with tab information:
1211
+ - "console_messages" (List[Dict]) : List of messages logged
1212
+ in the current page
1213
+
1214
+ """
1215
+ try:
1216
+ ws_wrapper = await self._get_ws_wrapper()
1217
+ console_logs = await ws_wrapper.console_view()
1218
+
1219
+ return {"console_messages": console_logs}
1220
+ except Exception as e:
1221
+ logger.error(f"Failed to get console view: {e}")
1222
+ return {"console_messages": []}
1223
+
1224
+ async def browser_console_exec(self, code: str) -> Dict[str, Any]:
1225
+ r"""Execute javascript code in the console of the current page and get
1226
+ results.
1227
+
1228
+ Args:
1229
+ code (str): JavaScript code to execute in the browser console.
1230
+
1231
+ Returns:
1232
+ Dict[str, Any]: A dictionary with the result of the action:
1233
+ - "result" (str): Confirmation of the action.
1234
+ - "snapshot" (str): A snapshot of the active tab after
1235
+ console execute action.
1236
+ - "tabs" (List[Dict]): Information about remaining tabs.
1237
+ - "current_tab" (int): Index of the new active tab.
1238
+ - "total_tabs" (int): Total number of remaining tabs.
1239
+ """
1240
+ try:
1241
+ ws_wrapper = await self._get_ws_wrapper()
1242
+ result = await ws_wrapper.console_exec(code)
1243
+
1244
+ tab_info = await ws_wrapper.get_tab_info()
1245
+ result.update(
1246
+ {
1247
+ "tabs": tab_info,
1248
+ "current_tab": next(
1249
+ (
1250
+ i
1251
+ for i, tab in enumerate(tab_info)
1252
+ if tab.get("is_current")
1253
+ ),
1254
+ 0,
1255
+ ),
1256
+ "total_tabs": len(tab_info),
1257
+ }
1258
+ )
1259
+
1260
+ return result
1261
+ except Exception as e:
1262
+ logger.error(f"Failed to execute javascript in console: {e}")
1263
+ return {
1264
+ "result": f"Error in code execution: {e}",
1265
+ "snapshot": "",
1266
+ "tabs": [],
1267
+ "current_tab": 0,
1268
+ "total_tabs": 0,
1269
+ }
1270
+
1271
+ @high_level_action
1272
+ async def browser_sheet_input(
1273
+ self, *, cells: List[SheetCell]
1274
+ ) -> Dict[str, Any]:
1275
+ r"""Input text into multiple cells in a spreadsheet (e.g., Google
1276
+ Sheets).
1277
+
1278
+ Args:
1279
+ cells (List[Dict[str, Any]]): List of cells to input, each
1280
+ containing:
1281
+ - "row" (int): Row index (0-based). Row 0 = first row,
1282
+ Row 1 = second row, etc.
1283
+ - "col" (int): Column index (0-based). Col 0 = Column A,
1284
+ Col 1 = Column B, etc.
1285
+ - "text" (str): Text to input into the cell
1286
+
1287
+ Returns:
1288
+ Dict[str, Any]: A dictionary with the result of the action:
1289
+ - "result" (str): Confirmation of the action with details.
1290
+ - "content" (str): The updated spreadsheet content (auto-read
1291
+ after input).
1292
+ - "snapshot" (str): Always empty string (sheet tools don't
1293
+ return snapshots).
1294
+ - "tabs" (List[Dict]): Information about all open tabs.
1295
+ - "current_tab" (int): Index of the active tab.
1296
+ - "total_tabs" (int): Total number of open tabs.
1297
+
1298
+ Example:
1299
+ >>> cells = [
1300
+ ... {"row": 0, "col": 0, "text": "Name"},
1301
+ ... {"row": 0, "col": 1, "text": "Age"},
1302
+ ... {"row": 1, "col": 0, "text": "Alice"},
1303
+ ... {"row": 1, "col": 1, "text": "30"},
1304
+ ... ]
1305
+ """
1306
+ try:
1307
+ import platform
1308
+
1309
+ ws_wrapper = await self._get_ws_wrapper()
1310
+ system = platform.system()
1311
+
1312
+ # Normalize cells: convert column labels to indices if needed
1313
+ normalized_cells = []
1314
+ for cell in cells:
1315
+ normalized_cell = cell.copy()
1316
+
1317
+ # Convert column label (A, B, C, ...) to index if it's a string
1318
+ col = cell.get("col", 0)
1319
+ if isinstance(col, str):
1320
+ col = col.strip().upper()
1321
+ # Convert A->0, B->1, ..., Z->25, AA->26, AB->27, etc.
1322
+ col_index = 0
1323
+ for char in col:
1324
+ col_index = col_index * 26 + (ord(char) - ord('A') + 1)
1325
+ normalized_cell["col"] = col_index - 1
1326
+ else:
1327
+ normalized_cell["col"] = int(col)
1328
+
1329
+ # Row is always used as-is (should be 0-based integer)
1330
+ normalized_cell["row"] = int(cell.get("row", 0))
1331
+ normalized_cell["text"] = str(cell.get("text", ""))
1332
+ normalized_cells.append(normalized_cell)
1333
+
1334
+ # Perform batch input
1335
+ input_result = await self._sheet_input_batch_js(
1336
+ normalized_cells, ws_wrapper, system
1337
+ )
1338
+
1339
+ # Read sheet content after input
1340
+ try:
1341
+ read_result = await self.browser_sheet_read()
1342
+ return {
1343
+ "result": input_result["result"],
1344
+ "content": read_result.get("content", ""),
1345
+ "snapshot": "",
1346
+ "tabs": input_result.get("tabs", []),
1347
+ "current_tab": input_result.get("current_tab", 0),
1348
+ "total_tabs": input_result.get("total_tabs", 0),
1349
+ }
1350
+ except Exception as read_error:
1351
+ logger.warning(f"Failed to auto-read sheet: {read_error}")
1352
+ input_result["snapshot"] = ""
1353
+ return input_result
1354
+
1355
+ except Exception as e:
1356
+ logger.error(f"Failed to input to sheet: {e}")
1357
+ return {
1358
+ "result": f"Error inputting to sheet: {e}",
1359
+ "content": "",
1360
+ "snapshot": "",
1361
+ "tabs": [],
1362
+ "current_tab": 0,
1363
+ "total_tabs": 0,
1364
+ }
1365
+
1366
+ async def _sheet_input_batch_js(
1367
+ self,
1368
+ cells: List[SheetCell],
1369
+ ws_wrapper: Any,
1370
+ system: str,
1371
+ ) -> Dict[str, Any]:
1372
+ r"""Input to sheet using batch keyboard input with absolute positioning
1373
+ via Name Box (Cmd+J).
1374
+
1375
+ This is more robust than relative navigation (arrow keys) because it
1376
+ handles hidden rows/columns and merged cells correctly.
1377
+ """
1378
+ operations: List[Dict[str, Any]] = []
1379
+
1380
+ def col_to_letter(col_idx: int) -> str:
1381
+ """Convert 0-based column index to letter (0->A, 25->Z, 26->AA)."""
1382
+ result = ""
1383
+ col_idx += 1 # Convert to 1-based for calculation
1384
+ while col_idx > 0:
1385
+ col_idx, remainder = divmod(col_idx - 1, 26)
1386
+ result = chr(65 + remainder) + result
1387
+ return result
1388
+
1389
+ for cell in cells:
1390
+ target_row = cell.get("row", 0)
1391
+ target_col = cell.get("col", 0)
1392
+ text = cell.get("text", "")
1393
+
1394
+ # Convert to A1 notation
1395
+ col_letter = col_to_letter(target_col)
1396
+ row_number = target_row + 1
1397
+ cell_address = f"{col_letter}{row_number}"
1398
+
1399
+ # 1. Focus Name Box
1400
+ if system == "Darwin":
1401
+ operations.append({"type": "press", "keys": ["Meta", "j"]})
1402
+ else:
1403
+ # On Windows/Linux, it's usually Ctrl+J or Alt+D
1404
+ # The snapshot showed Cmd+J for Mac.
1405
+ # Standard Google Sheets shortcut for
1406
+ # "Go to range" is F5 or Ctrl+J
1407
+ operations.append({"type": "press", "keys": ["Control", "j"]})
1408
+
1409
+ operations.append({"type": "wait", "delay": 500})
1410
+
1411
+ # 2. Type Address
1412
+ operations.append(
1413
+ {"type": "type", "text": cell_address, "delay": 0}
1414
+ )
1415
+ operations.append({"type": "wait", "delay": 200})
1416
+ operations.append({"type": "press", "keys": ["Enter"]})
1417
+ operations.append({"type": "wait", "delay": 500})
1418
+
1419
+ # 3. Clear content (Delete/Backspace)
1420
+ # Just in case, press Delete to clear existing content
1421
+ operations.append({"type": "press", "keys": ["Delete"]})
1422
+ operations.append({"type": "wait", "delay": 100})
1423
+
1424
+ # 4. Type Text
1425
+ if text:
1426
+ operations.append({"type": "type", "text": text, "delay": 0})
1427
+ operations.append({"type": "wait", "delay": 200})
1428
+ # Press Enter to confirm input
1429
+ operations.append({"type": "press", "keys": ["Enter"]})
1430
+ operations.append({"type": "wait", "delay": 300})
1431
+
1432
+ # Chunk operations to avoid 100-op limit in TypeScript backend
1433
+ # Each cell update takes ~10 ops, so 100 ops is only ~10 cells.
1434
+ # We split into chunks of 50 ops to be safe.
1435
+ CHUNK_SIZE = 50
1436
+
1437
+ try:
1438
+ for i in range(0, len(operations), CHUNK_SIZE):
1439
+ chunk = operations[i : i + CHUNK_SIZE]
1440
+ await ws_wrapper._send_command(
1441
+ 'batch_keyboard_input',
1442
+ {'operations': chunk, 'skipStabilityWait': True},
1443
+ )
1444
+ # Small delay between chunks
1445
+ await asyncio.sleep(0.2)
1446
+
1447
+ # Wait a bit for the last input to settle
1448
+ await asyncio.sleep(1.0)
1449
+
1450
+ tab_info = await ws_wrapper.get_tab_info()
1451
+
1452
+ return {
1453
+ "result": (
1454
+ f"Successfully input to {len(cells)} cells "
1455
+ "using absolute navigation"
1456
+ ),
1457
+ "snapshot": "",
1458
+ "tabs": tab_info,
1459
+ "current_tab": next(
1460
+ (
1461
+ i
1462
+ for i, tab in enumerate(tab_info)
1463
+ if tab.get("is_current")
1464
+ ),
1465
+ 0,
1466
+ ),
1467
+ "total_tabs": len(tab_info),
1468
+ }
1469
+
1470
+ except Exception as e:
1471
+ logger.error(f"Batch keyboard execution failed: {e}")
1472
+ return {
1473
+ "result": f"Error in batch keyboard execution: {e}",
1474
+ "snapshot": "",
1475
+ "tabs": [],
1476
+ "current_tab": 0,
1477
+ "total_tabs": 0,
1478
+ }
1479
+
1480
+ def _trim_sheet_content(self, content: str) -> str:
1481
+ """Trim sheet content and add row/column labels.
1482
+
1483
+ Remove all empty rows and columns, then add:
1484
+ - Column headers: A, B, C, D...
1485
+ - Row numbers: 0, 1, 2, 3...
1486
+
1487
+ Args:
1488
+ content (str): Raw sheet content with tabs and newlines.
1489
+
1490
+ Returns:
1491
+ str: Trimmed content with row/column labels.
1492
+ """
1493
+ if not content or not content.strip():
1494
+ return ""
1495
+
1496
+ # Split into rows and parse into 2D array
1497
+ rows = content.split('\n')
1498
+ grid: List[List[str]] = []
1499
+ max_cols = 0
1500
+ for row_str in rows:
1501
+ cells = row_str.split('\t')
1502
+ grid.append(cells)
1503
+ max_cols = max(max_cols, len(cells))
1504
+
1505
+ # Pad rows to same length
1506
+ for row_list in grid:
1507
+ while len(row_list) < max_cols:
1508
+ row_list.append('')
1509
+
1510
+ if not grid:
1511
+ return ""
1512
+
1513
+ # Find non-empty rows and columns (keep original indices)
1514
+ non_empty_rows = []
1515
+ for i, row_cells in enumerate(grid):
1516
+ if any(cell.strip() for cell in row_cells):
1517
+ non_empty_rows.append(i)
1518
+
1519
+ non_empty_cols = []
1520
+ for j in range(max_cols):
1521
+ if any(grid[i][j].strip() for i in range(len(grid))):
1522
+ non_empty_cols.append(j)
1523
+
1524
+ # If no content found
1525
+ if not non_empty_rows or not non_empty_cols:
1526
+ return ""
1527
+
1528
+ # Extract non-empty rows and columns
1529
+ filtered_grid = []
1530
+ for i in non_empty_rows:
1531
+ filtered_row = [grid[i][j] for j in non_empty_cols]
1532
+ filtered_grid.append(filtered_row)
1533
+
1534
+ # Generate column labels using original column indices
1535
+ def col_label(index):
1536
+ label = ""
1537
+ while True:
1538
+ label = chr(65 + (index % 26)) + label
1539
+ index = index // 26
1540
+ if index == 0:
1541
+ break
1542
+ index -= 1
1543
+ return label
1544
+
1545
+ col_headers = [col_label(j) for j in non_empty_cols]
1546
+
1547
+ # Add column headers as first row
1548
+ result_rows = ['\t'.join(['', *col_headers])]
1549
+
1550
+ # Add data rows with original row numbers (0-based)
1551
+ for row_idx, row_data in zip(non_empty_rows, filtered_grid):
1552
+ result_rows.append('\t'.join([str(row_idx), *row_data]))
1553
+
1554
+ return '\n'.join(result_rows)
1555
+
1556
+ @high_level_action
1557
+ async def browser_sheet_read(self) -> Dict[str, Any]:
1558
+ r"""Read content from a spreadsheet.
1559
+
1560
+ This tool reads spreadsheet content and returns it in a structured
1561
+ format with row/column labels. Empty rows and columns are
1562
+ automatically removed.
1563
+
1564
+ Output format:
1565
+ - First row: Column labels (A, B, C, ..., Z, AA, AB, ...)
1566
+ - First column: Row numbers (0, 1, 2, 3, ...) - 0-based
1567
+ - Labels show ORIGINAL positions in the spreadsheet (before removing
1568
+ empty rows/columns)
1569
+
1570
+ Row/column indices match browser_sheet_input directly:
1571
+ - Row label "0" in output = row index 0 in browser_sheet_input
1572
+ - Column label "A" in output = col index 0 in browser_sheet_input
1573
+ - Column label "C" in output = col index 2 in browser_sheet_input
1574
+
1575
+ Returns:
1576
+ Dict[str, Any]: A dictionary with the result of the action:
1577
+ - "result" (str): Confirmation message.
1578
+ - "content" (str): Tab-separated spreadsheet content with
1579
+ row/column labels. Format:
1580
+ Line 1: "\tA\tB\tC" (column headers)
1581
+ Line 2+: "0\tdata1\tdata2\tdata3" (row number + data)
1582
+ - "snapshot" (str): Always empty string (sheet tools don't
1583
+ return snapshots).
1584
+ - "tabs" (List[Dict]): Information about all open tabs.
1585
+ - "current_tab" (int): Index of the active tab.
1586
+ - "total_tabs" (int): Total number of open tabs.
1587
+
1588
+ Example output:
1589
+ A B
1590
+ 0 Name Age
1591
+ 1 Alice 30
1592
+ 2 Bob 25
1593
+ """
1594
+ import platform
1595
+ import uuid
1596
+
1597
+ ws_wrapper = await self._get_ws_wrapper()
1598
+
1599
+ # Use unique ID to avoid conflicts in parallel execution
1600
+ request_id = str(uuid.uuid4())
1601
+ var_name = f"__sheetCopy_{request_id.replace('-', '_')}"
1602
+
1603
+ try:
1604
+ # Step 1: Setup copy interception with multiple captures
1605
+ js_inject = f"""
1606
+ window.{var_name} = [];
1607
+ let copyCount = 0;
1608
+ const copyListener = function(e) {{
1609
+ try {{
1610
+ // Intercept clipboard data before system clipboard write
1611
+ // Capture from Google Sheets' setData call
1612
+ const originalSetData = e.clipboardData.setData.bind(
1613
+ e.clipboardData
1614
+ );
1615
+ let capturedText = '';
1616
+
1617
+ e.clipboardData.setData = function(type, data) {{
1618
+ if (type === 'text/plain') {{
1619
+ capturedText = data;
1620
+ }}
1621
+ // Prevent system clipboard write
1622
+ }};
1623
+
1624
+ // Let Google Sheets process event (calls setData)
1625
+ // Event propagates and Sheets tries to set clipboard
1626
+ setTimeout(() => {{
1627
+ copyCount++;
1628
+ window.{var_name}.push(capturedText);
1629
+ }}, 0);
1630
+
1631
+ // Prevent the default browser copy behavior
1632
+ e.preventDefault();
1633
+ }} catch (err) {{
1634
+ console.error(
1635
+ '[SheetRead] Failed to intercept copy data:', err
1636
+ );
1637
+ }}
1638
+ }};
1639
+
1640
+ document.addEventListener('copy', copyListener, true);
1641
+ window.{var_name}_removeListener = () => {{
1642
+ document.removeEventListener('copy', copyListener, true);
1643
+ }};
1644
+
1645
+ 'Copy listener installed';
1646
+ """
1647
+ await ws_wrapper.console_exec(js_inject)
1648
+
1649
+ system = platform.system()
1650
+ import asyncio
1651
+
1652
+ if system == "Darwin":
1653
+ select_all_copy_ops: List[Dict[str, Any]] = [
1654
+ {"type": "press", "keys": ["Meta", "a"]},
1655
+ {"type": "wait", "delay": 100},
1656
+ {"type": "press", "keys": ["Meta", "c"]},
1657
+ ]
1658
+ await ws_wrapper._send_command(
1659
+ 'batch_keyboard_input',
1660
+ {
1661
+ 'operations': select_all_copy_ops,
1662
+ 'skipStabilityWait': True,
1663
+ },
1664
+ )
1665
+ await asyncio.sleep(0.2)
1666
+
1667
+ # Repeat to capture correct one
1668
+ await ws_wrapper._send_command(
1669
+ 'batch_keyboard_input',
1670
+ {
1671
+ 'operations': select_all_copy_ops,
1672
+ 'skipStabilityWait': True,
1673
+ },
1674
+ )
1675
+ await asyncio.sleep(0.2)
1676
+ else:
1677
+ select_all_copy_ops = [
1678
+ {"type": "press", "keys": ["Control", "a"]},
1679
+ {"type": "wait", "delay": 100},
1680
+ {"type": "press", "keys": ["Control", "c"]},
1681
+ ]
1682
+ await ws_wrapper._send_command(
1683
+ 'batch_keyboard_input',
1684
+ {
1685
+ 'operations': select_all_copy_ops,
1686
+ 'skipStabilityWait': True,
1687
+ },
1688
+ )
1689
+ await asyncio.sleep(0.2)
1690
+
1691
+ # Repeat to capture correct one
1692
+ await ws_wrapper._send_command(
1693
+ 'batch_keyboard_input',
1694
+ {
1695
+ 'operations': select_all_copy_ops,
1696
+ 'skipStabilityWait': True,
1697
+ },
1698
+ )
1699
+ await asyncio.sleep(0.2)
1700
+
1701
+ js_check = f"window.{var_name} || []"
1702
+ content_result = await ws_wrapper.console_exec(js_check)
1703
+ result_str = content_result.get("result", "[]")
1704
+
1705
+ import json
1706
+
1707
+ if isinstance(result_str, list):
1708
+ captured_contents = result_str
1709
+ elif isinstance(result_str, str):
1710
+ if result_str.startswith("Console execution result: "):
1711
+ result_str = result_str[
1712
+ len("Console execution result: ") :
1713
+ ]
1714
+ result_str = result_str.strip()
1715
+
1716
+ try:
1717
+ captured_contents = json.loads(result_str)
1718
+ except json.JSONDecodeError:
1719
+ captured_contents = []
1720
+ else:
1721
+ captured_contents = []
1722
+
1723
+ if not captured_contents:
1724
+ sheet_content = ""
1725
+ elif len(captured_contents) == 1:
1726
+ sheet_content = captured_contents[0]
1727
+ else:
1728
+
1729
+ def count_non_empty_cells(content):
1730
+ if not content:
1731
+ return 0
1732
+ count = 0
1733
+ for line in content.split('\n'):
1734
+ for cell in line.split('\t'):
1735
+ if cell.strip():
1736
+ count += 1
1737
+ return count
1738
+
1739
+ counts = [
1740
+ count_non_empty_cells(content)
1741
+ for content in captured_contents[:2]
1742
+ ]
1743
+ best_idx = 0 if counts[0] > counts[1] else 1
1744
+ sheet_content = captured_contents[best_idx]
1745
+
1746
+ sheet_content = self._trim_sheet_content(sheet_content)
1747
+
1748
+ tab_info = await ws_wrapper.get_tab_info()
1749
+
1750
+ return {
1751
+ "result": "Successfully read spreadsheet content",
1752
+ "content": sheet_content,
1753
+ "snapshot": "", # Sheet tools don't return snapshots
1754
+ "tabs": tab_info,
1755
+ "current_tab": next(
1756
+ (
1757
+ i
1758
+ for i, tab in enumerate(tab_info)
1759
+ if tab.get("is_current")
1760
+ ),
1761
+ 0,
1762
+ ),
1763
+ "total_tabs": len(tab_info),
1764
+ }
1765
+
1766
+ except Exception as e:
1767
+ logger.error(f"Failed to read sheet: {e}")
1768
+ return {
1769
+ "result": f"Error reading sheet: {e}",
1770
+ "content": "",
1771
+ "snapshot": "",
1772
+ "tabs": [],
1773
+ "current_tab": 0,
1774
+ "total_tabs": 0,
1775
+ }
1776
+ finally:
1777
+ js_cleanup = f"""
1778
+ if (window.{var_name}_removeListener) {{
1779
+ window.{var_name}_removeListener();
1780
+ }}
1781
+ delete window.{var_name};
1782
+ delete window.{var_name}_removeListener;
1783
+ 'cleaned'
1784
+ """
1785
+ with contextlib.suppress(Exception):
1786
+ await ws_wrapper.console_exec(js_cleanup)
1787
+
1788
+ # Additional methods for backward compatibility
1789
+ async def browser_wait_user(
1790
+ self, timeout_sec: Optional[float] = None
1791
+ ) -> Dict[str, Any]:
1792
+ r"""Pauses execution and waits for human input from the console.
1793
+
1794
+ Use this for tasks requiring manual steps, like solving a CAPTCHA.
1795
+ The
1796
+ agent will resume after the user presses Enter in the console.
1797
+
1798
+ Args:
1799
+ timeout_sec (Optional[float]): Max time to wait in seconds. If
1800
+ `None`, it will wait indefinitely.
1801
+
1802
+ Returns:
1803
+ Dict[str, Any]: A dictionary with the result of the action:
1804
+ - "result" (str): A message indicating how the wait ended.
1805
+ - "snapshot" (str): The page snapshot after the wait.
1806
+ - "tabs" (List[Dict]): Information about all open tabs.
1807
+ - "current_tab" (int): Index of the active tab.
1808
+ - "total_tabs" (int): Total number of open tabs.
1809
+ """
1810
+ import asyncio
1811
+
1812
+ prompt = (
1813
+ "🕑 Agent waiting for human input. "
1814
+ "Complete action in browser, then press Enter..."
1815
+ )
1816
+ logger.info(f"\n{prompt}\n")
1817
+
1818
+ async def _await_enter():
1819
+ try:
1820
+ await asyncio.to_thread(
1821
+ input, ">>> Press Enter to resume <<<\n"
1822
+ )
1823
+ except (asyncio.CancelledError, Exception):
1824
+ # Handle cancellation gracefully
1825
+ pass
1826
+
1827
+ try:
1828
+ if timeout_sec is not None:
1829
+ logger.info(
1830
+ f"Waiting for user input with timeout: {timeout_sec}s"
1831
+ )
1832
+ start_time = time.time()
1833
+ task = asyncio.create_task(_await_enter())
1834
+ try:
1835
+ await asyncio.wait_for(task, timeout=timeout_sec)
1836
+ wait_time = time.time() - start_time
1837
+ logger.info(f"User input received after {wait_time:.2f}s")
1838
+ result_msg = "User resumed."
1839
+ except asyncio.TimeoutError:
1840
+ task.cancel()
1841
+ # Wait for task to be cancelled properly
1842
+ try:
1843
+ await task
1844
+ except asyncio.CancelledError:
1845
+ pass
1846
+ raise
1847
+ else:
1848
+ logger.info("Waiting for user input (no timeout)")
1849
+ start_time = time.time()
1850
+ await _await_enter()
1851
+ wait_time = time.time() - start_time
1852
+ logger.info(f"User input received after {wait_time:.2f}s")
1853
+ result_msg = "User resumed."
1854
+ except asyncio.TimeoutError:
1855
+ wait_time = timeout_sec or 0.0
1856
+ logger.info(
1857
+ f"User input timeout reached after {wait_time}s, "
1858
+ f"auto-resuming"
1859
+ )
1860
+ result_msg = f"Timeout {timeout_sec}s reached, auto-resumed."
1861
+
1862
+ try:
1863
+ snapshot = await self.browser_get_page_snapshot()
1864
+ tab_info = await self.browser_get_tab_info()
1865
+ return {"result": result_msg, "snapshot": snapshot, **tab_info}
1866
+ except Exception as e:
1867
+ logger.warning(f"Failed to get snapshot after wait: {e}")
1868
+ return {
1869
+ "result": result_msg,
1870
+ "snapshot": "",
1871
+ "tabs": [],
1872
+ "current_tab": 0,
1873
+ "total_tabs": 0,
1874
+ }
1875
+
1876
+ def clone_for_new_session(
1877
+ self, new_session_id: Optional[str] = None
1878
+ ) -> "HybridBrowserToolkit":
1879
+ r"""Create a new instance of HybridBrowserToolkit with a unique
1880
+ session.
1881
+
1882
+ Args:
1883
+ new_session_id: Optional new session ID. If None, a UUID will be
1884
+ generated.
1885
+
1886
+ Returns:
1887
+ A new HybridBrowserToolkit instance with the same configuration
1888
+ but a different session.
1889
+ """
1890
+ import uuid
1891
+
1892
+ if new_session_id is None:
1893
+ new_session_id = str(uuid.uuid4())[:8]
1894
+
1895
+ return HybridBrowserToolkit(
1896
+ headless=self._headless,
1897
+ user_data_dir=self._user_data_dir,
1898
+ stealth=self._stealth,
1899
+ cache_dir=f"{self._cache_dir.rstrip('/')}_clone_"
1900
+ f"{new_session_id}/",
1901
+ enabled_tools=self.enabled_tools.copy(),
1902
+ browser_log_to_file=self._browser_log_to_file,
1903
+ session_id=new_session_id,
1904
+ default_start_url=self._default_start_url,
1905
+ default_timeout=self._default_timeout,
1906
+ short_timeout=self._short_timeout,
1907
+ navigation_timeout=self._navigation_timeout,
1908
+ network_idle_timeout=self._network_idle_timeout,
1909
+ screenshot_timeout=self._screenshot_timeout,
1910
+ page_stability_timeout=self._page_stability_timeout,
1911
+ dom_content_loaded_timeout=self._dom_content_loaded_timeout,
1912
+ viewport_limit=self._viewport_limit,
1913
+ full_visual_mode=self._full_visual_mode,
1914
+ )
1915
+
1916
+ def get_tools(self) -> List[FunctionTool]:
1917
+ r"""Get available function tools based
1918
+ on enabled_tools configuration."""
1919
+ # Map tool names to their corresponding methods
1920
+ tool_map = {
1921
+ "browser_open": self.browser_open,
1922
+ "browser_close": self.browser_close,
1923
+ "browser_visit_page": self.browser_visit_page,
1924
+ "browser_back": self.browser_back,
1925
+ "browser_forward": self.browser_forward,
1926
+ "browser_get_page_snapshot": self.browser_get_page_snapshot,
1927
+ "browser_get_som_screenshot": self.browser_get_som_screenshot,
1928
+ "browser_click": self.browser_click,
1929
+ "browser_type": self.browser_type,
1930
+ "browser_select": self.browser_select,
1931
+ "browser_scroll": self.browser_scroll,
1932
+ "browser_enter": self.browser_enter,
1933
+ "browser_mouse_control": self.browser_mouse_control,
1934
+ "browser_mouse_drag": self.browser_mouse_drag,
1935
+ "browser_press_key": self.browser_press_key,
1936
+ "browser_wait_user": self.browser_wait_user,
1937
+ "browser_switch_tab": self.browser_switch_tab,
1938
+ "browser_close_tab": self.browser_close_tab,
1939
+ "browser_get_tab_info": self.browser_get_tab_info,
1940
+ "browser_console_view": self.browser_console_view,
1941
+ "browser_console_exec": self.browser_console_exec,
1942
+ "browser_sheet_input": self.browser_sheet_input,
1943
+ "browser_sheet_read": self.browser_sheet_read,
1944
+ }
1945
+
1946
+ enabled_tools = []
1947
+
1948
+ for tool_name in self.enabled_tools:
1949
+ if tool_name in tool_map:
1950
+ tool = FunctionTool(
1951
+ cast(Callable[..., Any], tool_map[tool_name])
1952
+ )
1953
+ enabled_tools.append(tool)
1954
+ else:
1955
+ logger.warning(f"Unknown tool name: {tool_name}")
1956
+
1957
+ logger.info(f"Returning {len(enabled_tools)} enabled tools")
1958
+ return enabled_tools