refactor: web_scan focus_item -> tabs_only, simplify tab-only mode

This commit is contained in:
Liang Jiaqing
2026-02-15 10:05:55 +08:00
parent f0b7c00876
commit caa39776ce
2 changed files with 17 additions and 15 deletions

View File

@@ -34,9 +34,9 @@
}}, }},
{"type": "function", "function": { {"type": "function", "function": {
"name": "web_scan", "name": "web_scan",
"description": "获取当前网页的清洗后内容,并列出所有已打开的标签页。支持切换标签页。切换页面后一般应先调用查看。", "description": "获取当前页面的简化HTML内容和标签页列表。注意简化会过滤边栏、浮动元素等非主体内容如需查看被过滤内容请用execute_js。切换页面后一般应先调用查看。",
"parameters": {"type": "object", "properties": { "parameters": {"type": "object", "properties": {
"focus_item": {"type": "string", "description": "语义过滤指令,用于在长列表中优先保留与该关键词相关的项。"}, "tabs_only": {"type": "boolean", "description": "仅返回标签页列表和当前标签信息不获取HTML内容。", "default": false},
"switch_tab_id": {"type": "string", "description": "可选的标签页 ID。如果提供系统将在扫描前切换到该标签页。"}}} "switch_tab_id": {"type": "string", "description": "可选的标签页 ID。如果提供系统将在扫描前切换到该标签页。"}}}
}}, }},
{"type": "function", "function": { {"type": "function", "function": {

28
ga.py
View File

@@ -106,10 +106,10 @@ def first_init_driver():
driver.newtab() driver.newtab()
time.sleep(5) time.sleep(5)
def web_scan(focus_item="", switch_tab_id=None): def web_scan(tabs_only=False, switch_tab_id=None):
""" """
利用 get_html 获取清洗后的网页内容。 获取当前页面的简化HTML内容和标签页列表。注意简化过程会过滤边栏、浮动元素等非主体内容。
focus_item: 语义过滤指令。如果用户在找特定内容(如“小米汽车”),算法会优先保留包含该关键词的列表项 tabs_only: 仅返回标签页列表不获取HTML内容节省token
switch_tab_id: 可选参数,如果提供,则在扫描前切换到该标签页。 switch_tab_id: 可选参数,如果提供,则在扫描前切换到该标签页。
应当多用execute_js少全量观察html。 应当多用execute_js少全量观察html。
""" """
@@ -125,16 +125,16 @@ def web_scan(focus_item="", switch_tab_id=None):
sess['url'] = sess.get('url', '')[:50] + ("..." if len(sess.get('url', '')) > 50 else "") sess['url'] = sess.get('url', '')[:50] + ("..." if len(sess.get('url', '')) > 50 else "")
tabs.append(sess) tabs.append(sess)
if switch_tab_id: driver.default_session_id = switch_tab_id if switch_tab_id: driver.default_session_id = switch_tab_id
content = get_html(driver, cutlist=True, instruction=focus_item, maxchars=23000) result = {
return {
"status": "success", "status": "success",
"metadata": { "metadata": {
"tabs_count": len(tabs), "tabs_count": len(tabs),
"tabs": tabs, "tabs": tabs,
"active_tab": driver.default_session_id "active_tab": driver.default_session_id
}, }
"content": content
} }
if not tabs_only: result["content"] = get_html(driver, cutlist=True, maxchars=23000)
return result
except Exception as e: except Exception as e:
return {"status": "error", "msg": format_error(e)} return {"status": "error", "msg": format_error(e)}
@@ -278,15 +278,17 @@ class GenericAgentHandler(BaseHandler):
return StepOutcome(result, next_prompt="", should_exit=True) return StepOutcome(result, next_prompt="", should_exit=True)
def do_web_scan(self, args, response): def do_web_scan(self, args, response):
'''focus_item仅用于在长列表中模糊搜寻相关item '''获取当前页面内容和标签页列表。也可用于切换标签页。
此工具也提供标签页查看和标签页切换功能 注意HTML经过简化边栏/浮动元素等可能被过滤。如需查看被过滤的内容请用execute_js
tabs_only=true时仅返回标签页列表不获取HTML省token
''' '''
focus_item = args.get("focus_item", "") tabs_only = args.get("tabs_only", False)
switch_tab_id = args.get("switch_tab_id", None) switch_tab_id = args.get("switch_tab_id", None)
result = web_scan(focus_item, switch_tab_id=switch_tab_id) result = web_scan(tabs_only=tabs_only, switch_tab_id=switch_tab_id)
content = result.pop("content", None) content = result.pop("content", None)
yield f'[Info] {str(result)}\n' yield f'[Info] {str(result)}\n'
next_prompt = f"```html\n{content}\n```" if content: next_prompt = f"```html\n{content}\n```"
else: next_prompt = "标签页列表如上\n"
return StepOutcome(result, next_prompt=next_prompt) return StepOutcome(result, next_prompt=next_prompt)
def do_web_execute_js(self, args, response): def do_web_execute_js(self, args, response):