Improve WebUI streaming and Python tooling

This commit is contained in:
武阳
2026-05-06 20:37:17 +08:00
parent a0da3c2423
commit 9ca675be0d
13 changed files with 930 additions and 147 deletions
+20 -3
View File
@@ -22,6 +22,7 @@ class PromptContext:
is_git_repo: bool
is_git_worktree: bool
scratchpad_directory: str | None = None
python_env_directory: str | None = None
additional_working_directories: tuple[str, ...] = ()
user_context: dict[str, str] = field(default_factory=dict)
system_context: dict[str, str] = field(default_factory=dict)
@@ -56,6 +57,11 @@ def build_prompt_context(
is_git_repo=snapshot.is_git_repo,
is_git_worktree=snapshot.is_git_worktree,
scratchpad_directory=snapshot.scratchpad_directory,
python_env_directory=(
str(runtime_config.python_env_dir.resolve())
if runtime_config.python_env_dir is not None
else None
),
additional_working_directories=snapshot.additional_working_directories,
user_context=snapshot.user_context,
system_context=snapshot.system_context,
@@ -154,7 +160,9 @@ def get_doing_tasks_section() -> str:
'除非确实需要新文件,否则优先编辑现有文件。',
'对于文件解析、表格转换、JSON 或 JSONL 处理、日志分析、批量校验、数据抽样等任务,如果小型 Python 脚本比手工文本处理更可靠,可以编写小型 Python 脚本。',
'小脚本应短小、可读,并明确输入和输出。需要执行 Python 代码或 Python 脚本时,使用 python_exec;不要通过 bash 执行 python、python3 或 .venv/bin/python。',
'一次性脚本放到临时目录或任务专属目录;只有用户需要长期复用时,才加入项目代码',
'一次性 Python 分析优先直接传给 python_exec.code,不要为了临时分析创建项目文件',
'如果确实需要临时脚本、缓存或中间产物,必须写入当前会话 scratchpad 目录,或写入明确的任务产物目录;禁止在项目根目录创建 analyze_*.py、tmp_*.py、scratch_*.py 等临时脚本。',
'只有用户明确要求长期复用或该脚本属于产品代码时,才把 Python 脚本加入项目源码目录。',
'当事情失败时,先诊断原因再改变方向。不要对同一个失败动作反复循环。',
'注意不要引入命令注入、SQL 注入、XSS 或不安全 shell 行为等安全问题。',
'如实汇报结果。如果没有运行某个验证步骤,需要明确说明。',
@@ -201,10 +209,17 @@ def get_using_your_tools_section(enabled_tool_names: set[str]) -> str:
'需要结构化文件分析、批量数据处理、JSON/JSONL 转换、抽样、校验或快速计算时,必须优先使用 python_exec,而不是通过 bash 手写 python 命令。'
)
items.append(
'python_exec 默认使用项目 .venv/bin/python;不要用它安装依赖。缺少依赖时先说明缺失包并向用户确认包管理方式'
'python_exec 默认使用当前用户独立 Python venv,不使用项目 .venv;不要用 bash 执行 python 或 pip'
)
items.append(
'如果已经用 write_file 生成了临时 Python 脚本,下一步也应使用 python_exec 的 script_path 执行该脚本,不要改用 bash'
'python_exec 会注入 PYTHON_EXEC_SCRATCHPAD 环境变量,指向当前用户当前会话隔离的 scratchpad。一次性脚本、缓存和中间输出都应写入这里'
)
if 'python_package' in enabled_tool_names:
items.append(
'当 python_exec 因缺少 pandas、pyarrow、openpyxl 等 Python 包失败时,使用 python_package 在当前用户独立 venv 中安装缺失包,然后重试;不要安装到系统 Python 或项目 .venv。'
)
items.append(
'不要用 write_file 在项目根目录生成临时 Python 脚本;如果用户明确要求保留脚本,才写入合适的项目路径,并用 python_exec 的 script_path 执行。'
)
if 'bash' in enabled_tool_names:
items.append(
@@ -444,6 +459,8 @@ def compute_simple_env_info(prompt_context: PromptContext) -> str:
items.append(list(prompt_context.additional_working_directories))
if prompt_context.scratchpad_directory:
items.append(f'会话 scratchpad 目录: {prompt_context.scratchpad_directory}')
if prompt_context.python_env_directory:
items.append(f'当前用户 Python venv: {prompt_context.python_env_directory}')
items.extend(
[
f'平台: {prompt_context.platform_name}',