Skip to content

s06硬编码 #248

@Opao-heng

Description

@Opao-heng

for block in response.content:
if block.type == "tool_use":
if block.name == "compact":
manual_compact = True
output = "Compressing..."
else:
handler = TOOL_HANDLERS.get(block.name)
try:
output = handler(**block.input) if handler else f"Unknown tool: {block.name}"
except Exception as e:
output = f"Error: {e}"
print(f"> {block.name}:")
print(str(output)[:200])
results.append({"type": "tool_result", "tool_use_id": block.id, "content": str(output)})

这里的output = "Compressing...",为什么不使用代码上面定义的映射函数:

TOOL_HANDLERS = {
"bash": lambda **kw: run_bash(kw["command"]),
"read_file": lambda **kw: run_read(kw["path"], kw.get("limit")),
"write_file": lambda **kw: run_write(kw["path"], kw["content"]),
"edit_file": lambda **kw: run_edit(kw["path"], kw["old_text"], kw["new_text"]),
"compact": lambda **kw: "Manual compression requested.",
}

中的"compact": lambda **kw: "Manual compression requested."。
目前代码这样写,那么 Manual compression requested 根本没用到吧。

改成下面这样合适吗???
for block in response.content:
if block.type == "tool_use":
# 统一获取工具处理器
handler = TOOL_HANDLERS.get(block.name)

    # 专门标记 compact 工具
    if block.name == "compact":
        manual_compact = True

    # 统一执行工具(不管是什么工具,都这样写!)
    try:
        output = handler(**block.input) if handler else f"Unknown tool: {block.name}"
    except Exception as e:
        output = f"Error: {e}"

    # 打印输出
    print(f"> {block.name}:")
    print(str(output)[:200])
    results.append({"type": "tool_result", "tool_use_id": block.id, "content": str(output)})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions