refactor: 优化file_read统计逻辑 - 紧凑化+路径修复+编码安全

This commit is contained in:
Jiaqing Liang
2026-02-27 23:01:30 +08:00
parent bb2d595fb6
commit 66e3ccbf0d

15
ga.py
View File

@@ -1,4 +1,5 @@
import sys, os, re, json, time, threading import sys, os, re, json, time, threading
from datetime import datetime
from pathlib import Path from pathlib import Path
import tempfile, traceback, subprocess, itertools, collections import tempfile, traceback, subprocess, itertools, collections
if sys.stdout is None: sys.stdout = open(os.devnull, "w") if sys.stdout is None: sys.stdout = open(os.devnull, "w")
@@ -129,8 +130,7 @@ def web_scan(tabs_only=False, switch_tab_id=None):
result = { result = {
"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
} }
} }
@@ -148,6 +148,16 @@ def format_error(e):
return f"{exc_type.__name__}: {str(e)} @ {fname}:{f.lineno}, {f.name} -> `{f.line}`" return f"{exc_type.__name__}: {str(e)} @ {fname}:{f.lineno}, {f.name} -> `{f.line}`"
return f"{exc_type.__name__}: {str(e)}" return f"{exc_type.__name__}: {str(e)}"
def log_memory_access(path):
if 'memory' not in path: return
stats_file = 'memory/file_access_stats.json'
try:
with open(stats_file, 'r', encoding='utf-8') as f: stats = json.load(f)
except: stats = {}
fname = os.path.basename(path)
stats[fname] = {'count': stats.get(fname, {}).get('count', 0) + 1, 'last': datetime.now().strftime('%Y-%m-%d')}
with open(stats_file, 'w', encoding='utf-8') as f: json.dump(stats, f, indent=2, ensure_ascii=False)
def web_execute_js(script, switch_tab_id=None): def web_execute_js(script, switch_tab_id=None):
""" """
执行 JS 脚本来控制浏览器,并捕获结果和页面变化。 执行 JS 脚本来控制浏览器,并捕获结果和页面变化。
@@ -384,6 +394,7 @@ class GenericAgentHandler(BaseHandler):
if ' ... [TRUNCATED]' in result: if ' ... [TRUNCATED]' in result:
result += '\n\n(某些行被截断,如需完整内容可改用 code_run 读取)' result += '\n\n(某些行被截断,如需完整内容可改用 code_run 读取)'
next_prompt = self._get_anchor_prompt() next_prompt = self._get_anchor_prompt()
log_memory_access(path)
if 'memory' in path or 'sop' in path: if 'memory' in path or 'sop' in path:
next_prompt += "\n[SYSTEM TIPS] 正在读取记忆或SOP文件若决定按sop执行请提取sop中的关键点特别是靠后的update working memory." next_prompt += "\n[SYSTEM TIPS] 正在读取记忆或SOP文件若决定按sop执行请提取sop中的关键点特别是靠后的update working memory."
return StepOutcome(result, next_prompt=next_prompt) return StepOutcome(result, next_prompt=next_prompt)