Reduce WhatsApp ops tool friction with looser xlsx/file schemas and short-intent playbooks.

Production usage showed schema mismatches and multi-tool loops; accept common arg aliases and route agents to <=3-call alarm recipes.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
oliver 2026-08-10 22:32:44 +08:00
parent 685e3ffa8d
commit aff11c1eae
11 changed files with 276 additions and 24 deletions

View file

@ -6,12 +6,30 @@ from runtime.tools.base import ToolSpec
from runtime.tools.path_guard import resolve_workspace_path
def _resolve_write_path(args: dict[str, Any]) -> str:
for key in ("path", "file", "filename", "file_path", "filepath", "name"):
raw = str(args.get(key) or "").strip().strip('"').strip("'")
if raw:
return raw
return ""
def write_file_tool() -> ToolSpec:
def _handler(args: dict[str, Any]) -> dict[str, Any]:
raw = str(args.get("path") or "").strip().strip('"').strip("'")
raw = _resolve_write_path(args)
if not raw:
return {"ok": False, "error": "path_required"}
content = str(args.get("content") or "")
return {
"ok": False,
"error": "path_required",
"hint": "Pass path (or file/filename) relative to workspace root.",
"example": {"path": "tmp/notes.txt", "content": "hello", "mode": "overwrite"},
}
content = args.get("content")
if content is None:
content = args.get("text")
if content is None:
content = args.get("body")
content_s = "" if content is None else str(content)
mode = str(args.get("mode") or "overwrite").strip().lower()
try:
p = resolve_workspace_path(raw)
@ -21,22 +39,32 @@ def write_file_tool() -> ToolSpec:
if mode not in ("overwrite", "append"):
return {"ok": False, "error": "invalid_mode", "allowed": ["overwrite", "append"]}
if mode == "append":
p.write_text(p.read_text(encoding="utf-8", errors="replace") + content, encoding="utf-8")
p.write_text(p.read_text(encoding="utf-8", errors="replace") + content_s, encoding="utf-8")
else:
p.write_text(content, encoding="utf-8")
p.write_text(content_s, encoding="utf-8")
return {"ok": True, "path": str(p), "bytes": p.stat().st_size}
return ToolSpec(
name="write_file",
description="Write text content to a workspace file (overwrite or append).",
description=(
"Write text content to a workspace file (overwrite or append). "
"Path aliases: file, filename, file_path."
),
parameters={
"type": "object",
"properties": {
"path": {"type": "string", "description": "File path, relative to workspace root."},
"file": {"type": "string", "description": "Alias for path."},
"filename": {"type": "string", "description": "Alias for path."},
"file_path": {"type": "string", "description": "Alias for path."},
"filepath": {"type": "string", "description": "Alias for path."},
"name": {"type": "string", "description": "Alias for path (when it looks like a relative file path)."},
"content": {"type": "string", "description": "Full text content to write."},
"text": {"type": "string", "description": "Alias for content."},
"body": {"type": "string", "description": "Alias for content."},
"mode": {"type": "string", "enum": ["overwrite", "append"], "default": "overwrite"},
},
"required": ["path", "content"],
"required": [],
"additionalProperties": False,
},
handler=_handler,

View file

@ -97,7 +97,7 @@ def _build_workbook(sheets: list[dict[str, Any]], *, freeze_header: bool, auto_w
for i, sheet in enumerate(sheets, start=1):
if not isinstance(sheet, dict):
continue
rows, inferred_cols = _normalize_rows(sheet.get("rows"))
rows, inferred_cols = _normalize_rows(sheet.get("rows") if sheet.get("rows") is not None else sheet.get("data"))
headers_raw = sheet.get("headers")
if isinstance(headers_raw, list) and headers_raw:
col_count = max(len(headers_raw), inferred_cols, 1)
@ -129,15 +129,62 @@ def _build_workbook(sheets: list[dict[str, Any]], *, freeze_header: bool, auto_w
return buf.getvalue(), summary
_XLSX_EXAMPLE = {
"name": "alarm_summary.xlsx",
"sheets": [
{
"name": "by_host",
"headers": ["host_name", "severity", "count"],
"rows": [["NE-A", "critical", 12], ["NE-B", "major", 5]],
}
],
"freeze_header": True,
"auto_width": True,
}
def _coerce_sheets(args: dict[str, Any]) -> list[Any] | None:
"""Accept common agent shapes: sheets[], or top-level headers/rows[/name]."""
sheets_raw = args.get("sheets")
if isinstance(sheets_raw, list) and sheets_raw:
return sheets_raw
# Single sheet mistaken as top-level object
if isinstance(sheets_raw, dict) and (
"rows" in sheets_raw or "headers" in sheets_raw or "data" in sheets_raw
):
return [sheets_raw]
headers = args.get("headers")
rows = args.get("rows")
if rows is None:
rows = args.get("data")
if isinstance(rows, list):
sheet: dict[str, Any] = {"rows": rows}
if isinstance(headers, list):
sheet["headers"] = headers
title = str(args.get("sheet_name") or args.get("sheet") or "").strip()
if title:
sheet["name"] = title
return [sheet]
return None
def write_xlsx_tool() -> ToolSpec:
def _handler(args: dict[str, Any]) -> dict[str, Any]:
sheets_raw = args.get("sheets")
if not isinstance(sheets_raw, list) or not sheets_raw:
return {"ok": False, "error": "sheets_required"}
sheets_raw = _coerce_sheets(args)
if not sheets_raw:
return {
"ok": False,
"error": "sheets_required",
"hint": "Pass sheets=[{name, headers, rows}] (or top-level headers+rows).",
"example": _XLSX_EXAMPLE,
}
if len(sheets_raw) > _MAX_SHEETS:
return {"ok": False, "error": "too_many_sheets", "max_sheets": _MAX_SHEETS}
filename = str(args.get("name") or "").strip() or "report.xlsx"
filename = (
str(args.get("name") or args.get("filename") or args.get("file_name") or "").strip()
or "report.xlsx"
)
if not filename.lower().endswith(".xlsx"):
filename = f"{filename}.xlsx"
freeze_header = args.get("freeze_header") is not False
@ -200,7 +247,10 @@ def write_xlsx_tool() -> ToolSpec:
"properties": {
"sheets": {
"type": "array",
"description": "One or more sheets. Each item: {name, headers[], rows[][]}.",
"description": (
"Preferred: one or more sheets [{name, headers[], rows[][]}]. "
"If omitted, top-level headers+rows are accepted as a single sheet."
),
"items": {
"type": "object",
"properties": {
@ -213,17 +263,72 @@ def write_xlsx_tool() -> ToolSpec:
"rows": {
"type": "array",
"description": "Data rows: each row is an array of cell values (string/number/bool/null).",
"items": {"type": "array"},
"items": {
"anyOf": [
{"type": "array"},
{"type": "object"},
]
},
},
"data": {
"type": "array",
"description": "Alias for rows.",
"items": {
"anyOf": [
{"type": "array"},
{"type": "object"},
]
},
},
},
"required": ["rows"],
"additionalProperties": False,
"additionalProperties": True,
},
},
"headers": {
"type": "array",
"items": {"type": "string"},
"description": "Shortcut when sheets omitted: column headers for a single sheet.",
},
"rows": {
"type": "array",
"description": "Shortcut when sheets omitted: data rows for a single sheet.",
"items": {
"anyOf": [
{"type": "array"},
{"type": "object"},
]
},
},
"data": {
"type": "array",
"description": "Alias for top-level rows.",
"items": {
"anyOf": [
{"type": "array"},
{"type": "object"},
]
},
},
"sheet_name": {
"type": "string",
"description": "Shortcut sheet tab name when using top-level headers/rows.",
},
"sheet": {
"type": "string",
"description": "Alias for sheet_name.",
},
"name": {
"type": "string",
"description": "Download filename, e.g. alarm_summary.xlsx",
},
"filename": {
"type": "string",
"description": "Alias for name.",
},
"file_name": {
"type": "string",
"description": "Alias for name.",
},
"path": {
"type": "string",
"description": "Optional workspace path to also write the .xlsx file (mirror).",
@ -239,7 +344,7 @@ def write_xlsx_tool() -> ToolSpec:
"description": "Best-effort column width from sample cells (default true).",
},
},
"required": ["sheets"],
"required": [],
"additionalProperties": False,
},
handler=_handler,