learn-claude-code icon indicating copy to clipboard operation
learn-claude-code copied to clipboard

s06硬编码

Open Opao-heng opened this issue 7 months ago • 1 comments

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)})

Opao-heng avatar Apr 20 '26 12:04 Opao-heng

你好,可以看新版s08_context_compact

Gui-Yue avatar May 22 '26 14:05 Gui-Yue