fix: handle todo_write string inputs for issue 340

Co-authored-by: gui-yue <yuemeng.gui@gmail.com>
This commit is contained in:
Haoran
2026-06-02 23:11:30 +08:00
committed by gui-yue
parent 595a52a17f
commit ee9a747e09
8 changed files with 403 additions and 172 deletions

View File

@@ -26,7 +26,7 @@ Run: python s07_skill_loading/code.py
Needs: pip install anthropic python-dotenv pyyaml + ANTHROPIC_API_KEY in .env
"""
import os, subprocess
import ast, json, os, subprocess
from pathlib import Path
import yaml
@@ -168,13 +168,31 @@ def run_glob(pattern: str) -> str:
except Exception as e:
return f"Error: {e}"
def _normalize_todos(todos):
if isinstance(todos, str):
try:
todos = json.loads(todos)
except json.JSONDecodeError:
try:
todos = ast.literal_eval(todos)
except (SyntaxError, ValueError):
return None, "Error: todos must be a list or JSON array string"
if not isinstance(todos, list):
return None, "Error: todos must be a list"
for i, t in enumerate(todos):
if not isinstance(t, dict):
return None, f"Error: todos[{i}] must be an object"
if "content" not in t or "status" not in t:
return None, f"Error: todos[{i}] missing 'content' or 'status'"
if t["status"] not in ("pending", "in_progress", "completed"):
return None, f"Error: todos[{i}] has invalid status '{t['status']}'"
return todos, None
def run_todo_write(todos: list) -> str:
global CURRENT_TODOS
for i, t in enumerate(todos):
if "content" not in t or "status" not in t:
return f"Error: todos[{i}] missing 'content' or 'status'"
if t["status"] not in ("pending", "in_progress", "completed"):
return f"Error: todos[{i}] has invalid status '{t['status']}'"
todos, error = _normalize_todos(todos)
if error:
return error
CURRENT_TODOS = todos
lines = ["\n\033[33m## Current Tasks\033[0m"]
for t in CURRENT_TODOS: