Skip to content

"CLI-Anything: Making ALL Software Agent-Native" -- CLI-Hub: https://clianything.cc/

Python

@ fix: traverse doc-tree children recursively before rendering

hitoaec937128ede
diff --git a/siyuan/agent-harness/cli_anything/siyuan/siyuan_cli.py b/siyuan/agent-harness/cli_anything/siyuan/siyuan_cli.py
index dcef50e5c..de4d1f345 100644
--- a/siyuan/agent-harness/cli_anything/siyuan/siyuan_cli.py
+++ b/siyuan/agent-harness/cli_anything/siyuan/siyuan_cli.py
@@ -104,6 +104,21 @@ def cli(ctx: click.Context, json_output: bool, host: str, port: int,
ctx.invoke(repl)
+def _walk_tree(items: list[dict], depth: int = 0) -> list[dict]:
+ """Recursively flatten a doc tree, setting depth on each entry.
+
+ Returns new dicts; does not mutate the originals.
+ """
+ result: list[dict] = []
+ for t in items:
+ entry = {**t, "depth": depth}
+ result.append(entry)
+ children = t.get("children")
+ if children:
+ result.extend(_walk_tree(children, depth + 1))
+ return result
+
+
# ── REPL ───────────────────────────────────────────────────────────────
def _build_repl_commands() -> dict[str, str]:
@@ -295,8 +310,8 @@ def _handle_doc_repl(skin: Any, client: SiYuanClient,
click.echo(json.dumps(items, ensure_ascii=False))
else:
skin.table(["ID", "Name", "Path"],
- [[t.get("id", ""), t.get("name", ""), t.get("path", "")]
- for t in items])
+ [[t.get("id", ""), " " * t.get("depth", 0) + t.get("name", ""), t.get("path", "")]
+ for t in _walk_tree(items)])
elif sub == "get" and len(parts) >= 3:
hpath = client.get_hpath_by_id(parts[2])
if json_mode:
@@ -546,7 +561,7 @@ def doc_tree(ctx: SiYuanContext, notebook_id: str, path: str, depth: int):
if ctx.json_output:
click.echo(json.dumps(items, ensure_ascii=False))
else:
- for t in items:
+ for t in _walk_tree(items):
indent = " " * t.get("depth", 0)
click.echo(f"{indent}{t.get('name', '')} ({t.get('id', '')})")
diff --git a/siyuan/agent-harness/cli_anything/siyuan/tests/test_cli_commands.py b/siyuan/agent-harness/cli_anything/siyuan/tests/test_cli_commands.py
index 57703dbbd..0aa3b2116 100644
--- a/siyuan/agent-harness/cli_anything/siyuan/tests/test_cli_commands.py
+++ b/siyuan/agent-harness/cli_anything/siyuan/tests/test_cli_commands.py
@@ -144,16 +144,25 @@ class TestDocTreeCommand:
def test_doc_tree_returns_files(self, runner, mock_ctx):
"""doc tree handles API returning dict with 'files' key."""
mock_ctx.client.list_doc_tree.return_value = {
- "files": [
- {"id": "doc1", "name": "根文档", "depth": 0},
- {"id": "doc2", "name": "子文档", "depth": 1},
+ "tree": [
+ {"id": "doc1", "name": "根文档"},
+ {"id": "doc2", "name": "父文档", "children": [
+ {"id": "doc3", "name": "子文档"},
+ {"id": "doc4", "name": "孙文档", "children": [
+ {"id": "doc5", "name": "曾孙文档"},
+ ]},
+ ]},
],
}
with patch("cli_anything.siyuan.siyuan_cli.SiYuanContext", return_value=mock_ctx):
result = runner.invoke(cli, ["doc", "tree", "nb1"])
assert result.exit_code == 0
assert "根文档" in result.output
+ assert "doc1" in result.output
+ assert "父文档" in result.output
assert "子文档" in result.output
+ assert "孙文档" in result.output
+ assert "曾孙文档" in result.output
def test_doc_tree_json(self, runner, mock_ctx):
"""--json doc tree returns raw files array."""