"CLI-Anything: Making ALL Software Agent-Native" -- CLI-Hub: https://clianything.cc/
Python
fix(zotero): address PR review feedback
zhiwuyazhe_fjr478ee47794d3
diff --git a/zotero/agent-harness/cli_anything/zotero/skills/SKILL.md b/zotero/agent-harness/cli_anything/zotero/skills/SKILL.mdindex a62cf1af2..ad9c107c9 100644--- a/zotero/agent-harness/cli_anything/zotero/skills/SKILL.md+++ b/zotero/agent-harness/cli_anything/zotero/skills/SKILL.md@@ -24,46 +24,31 @@ python -m cli_anything.zotero## Command Groups-### AppApplication and runtime inspection commands.| Command | Description ||---------|-------------|-| `status` | Execute `status`. |-| `version` | Execute `version`. |-| `launch` | Execute `launch`. |-| `enable-local-api` | Execute `enable-local-api`. |-| `ping` | Execute `ping`. |--### CollectionCollection inspection and selection commands.| Command | Description ||---------|-------------|-| `list` | Execute `list`. |-| `find` | Execute `find`. |-| `tree` | Execute `tree`. |-| `get` | Execute `get`. |-| `items` | Execute `items`. |-| `use-selected` | Execute `use-selected`. |--+| `create` | Execute `create`. |### Item@@ -71,97 +56,65 @@ Item inspection and rendering commands.| Command | Description ||---------|-------------|-| `list` | Execute `list`. |-| `find` | Execute `find`. |-| `get` | Execute `get`. |-| `children` | Execute `children`. |-| `notes` | Execute `notes`. |-| `attachments` | Execute `attachments`. |-| `file` | Execute `file`. |-+| `export` | Execute `export`. || `citation` | Execute `citation`. |-| `bibliography` | Execute `bibliography`. |-| `context` | Execute `context`. |-| `analyze` | Execute `analyze`. |-| `add-to-collection` | Execute `add-to-collection`. |-| `move-to-collection` | Execute `move-to-collection`. |--### SearchSaved-search inspection commands.| Command | Description ||---------|-------------|-| `list` | Execute `list`. |-| `get` | Execute `get`. |-| `items` | Execute `items`. |--### TagTag inspection commands.| Command | Description ||---------|-------------|-| `list` | Execute `list`. |-| `items` | Execute `items`. |--### StyleInstalled CSL style inspection commands.| Command | Description ||---------|-------------|-| `list` | Execute `list`. |--### ImportOfficial Zotero import and write commands.| Command | Description ||---------|-------------|-| `file` | Execute `file`. |-| `json` | Execute `json`. |--### NoteRead and add child notes.| Command | Description ||---------|-------------|-| `get` | Execute `get`. |--+| `add` | Execute `add`. |### Session@@ -169,30 +122,18 @@ Session and REPL context commands.| Command | Description ||---------|-------------|-| `status` | Execute `status`. |-| `use-library` | Execute `use-library`. |-| `use-collection` | Execute `use-collection`. |-| `use-item` | Execute `use-item`. |-| `use-selected` | Execute `use-selected`. |-| `clear-library` | Execute `clear-library`. |-| `clear-collection` | Execute `clear-collection`. |-| `clear-item` | Execute `clear-item`. |-| `history` | Execute `history`. |--## Examples-### Runtime StatusInspect Zotero paths and backend availability.@@ -201,7 +142,6 @@ Inspect Zotero paths and backend availability.cli-anything-zotero app status --json```-### Read Selected CollectionPersist the collection selected in the Zotero GUI.@@ -210,7 +150,6 @@ Persist the collection selected in the Zotero GUI.cli-anything-zotero collection use-selected --json```-### Render CitationRender a citation using Zotero's Local API.@@ -219,7 +158,6 @@ Render a citation using Zotero's Local API.cli-anything-zotero item citation <item-key> --style apa --locale en-US --json```-### Add Child NoteCreate a child note under an existing Zotero item.@@ -228,7 +166,6 @@ Create a child note under an existing Zotero item.cli-anything-zotero note add <item-key> --text "Key takeaway" --json```-### Build LLM ContextAssemble structured context for downstream model analysis.@@ -237,7 +174,6 @@ Assemble structured context for downstream model analysis.cli-anything-zotero item context <item-key> --include-notes --include-links --json```-## Version-0.1.0\ No newline at end of file+0.1.0diff --git a/zotero/agent-harness/cli_anything/zotero/tests/test_agent_harness.py b/zotero/agent-harness/cli_anything/zotero/tests/test_agent_harness.pyindex f29f57508..dda7c1a9d 100644--- a/zotero/agent-harness/cli_anything/zotero/tests/test_agent_harness.py+++ b/zotero/agent-harness/cli_anything/zotero/tests/test_agent_harness.py@@ -51,5 +51,7 @@ class AgentHarnessPackagingTests(unittest.TestCase):self.assertIn("## Command Groups", content)self.assertIn("### App", content)self.assertIn("### Item", content)+ self.assertIn("### Note", content)+ self.assertIn("| `add` |", content)finally:output_path.unlink(missing_ok=True)diff --git a/zotero/agent-harness/cli_anything/zotero/utils/zotero_sqlite.py b/zotero/agent-harness/cli_anything/zotero/utils/zotero_sqlite.pyindex f19e8281c..682de467a 100644--- a/zotero/agent-harness/cli_anything/zotero/utils/zotero_sqlite.py+++ b/zotero/agent-harness/cli_anything/zotero/utils/zotero_sqlite.py@@ -524,7 +524,8 @@ def resolve_attachment_real_path(item: dict[str, Any], data_dir: Path | str) ->parsed = urlparse(raw_path)decoded_path = unquote(parsed.path)if parsed.netloc and parsed.netloc.lower() != "localhost":- unc_path = f"\\\\{parsed.netloc}{decoded_path.replace('/', '\\')}"+ normalized_unc_path = decoded_path.replace("/", "\\")+ unc_path = f"\\\\{parsed.netloc}{normalized_unc_path}"return str(PureWindowsPath(unc_path))if re.match(r"^/[A-Za-z]:", decoded_path):return str(PureWindowsPath(decoded_path.lstrip("/")))diff --git a/zotero/agent-harness/setup.py b/zotero/agent-harness/setup.pyindex 6c7712517..e14e21fe5 100644--- a/zotero/agent-harness/setup.py+++ b/zotero/agent-harness/setup.py@@ -58,9 +58,11 @@ setup(],},package_data={- "cli_anything.zotero": ["README.md"],- "cli_anything.zotero.skills": ["SKILL.md"],- "cli_anything.zotero.tests": ["TEST.md"],+ "cli_anything.zotero": [+ "README.md",+ "skills/SKILL.md",+ "tests/TEST.md",+ ],},include_package_data=True,zip_safe=False,diff --git a/zotero/agent-harness/skill_generator.py b/zotero/agent-harness/skill_generator.pyindex 243d294b5..ee860c5f8 100644--- a/zotero/agent-harness/skill_generator.py+++ b/zotero/agent-harness/skill_generator.py@@ -5,6 +5,7 @@ SKILL.md Generator for CLI-Anything harnesses.from __future__ import annotationsimport argparse+import astimport refrom dataclasses import dataclass, fieldfrom pathlib import Path@@ -75,49 +76,73 @@ def extract_version_from_setup(setup_path: Path) -> str:return match.group(1) if match else "1.0.0"+def _string_literal(node: ast.AST | None) -> str | None:+ if isinstance(node, ast.Constant) and isinstance(node.value, str):+ return node.value+ return None+++def _click_decorator_info(decorator: ast.AST) -> tuple[str | None, str | None, str | None]:+ target: ast.AST+ explicit_name: str | None = None+ if isinstance(decorator, ast.Call):+ target = decorator.func+ if decorator.args:+ explicit_name = _string_literal(decorator.args[0])+ if explicit_name is None:+ for keyword in decorator.keywords:+ if keyword.arg == "name":+ explicit_name = _string_literal(keyword.value)+ break+ else:+ target = decorator+ if isinstance(target, ast.Attribute) and isinstance(target.value, ast.Name):+ return target.value.id, target.attr, explicit_name+ return None, None, explicit_name+++def _default_group_name(function_name: str) -> str:+ return re.sub(r"_group$", "", function_name).replace("_", " ")+++def _default_command_name(function_name: str) -> str:+ return re.sub(r"_command$", "", function_name).replace("_", "-")++def extract_commands_from_cli(cli_path: Path) -> list[CommandGroup]:- content = cli_path.read_text(encoding="utf-8")+ module = ast.parse(cli_path.read_text(encoding="utf-8"), filename=str(cli_path))groups: list[CommandGroup] = []group_name_by_function: dict[str, str] = {}+ group_by_display_name: dict[str, CommandGroup] = {}+ functions = [node for node in module.body if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))]++ for node in functions:+ for decorator in node.decorator_list:+ owner_name, decorator_name, explicit_name = _click_decorator_info(decorator)+ if owner_name != "cli" or decorator_name != "group":+ continue+ raw_name = explicit_name or _default_group_name(node.name)+ display_name = raw_name.replace("-", " ").title()+ group = CommandGroup(+ name=display_name,+ description=ast.get_docstring(node) or f"Commands for {raw_name}.",+ )+ group_name_by_function[node.name] = display_name+ group_by_display_name[display_name] = group+ groups.append(group)+ break- group_pattern = (- r'@cli\.group(?:\(([^)]*)\))?'- r'(?:\s*@[\w.]+(?:\([^)]*\))?)*'- r'\s*def\s+(\w+)\([^)]*\)'- r'(?:\s*->\s*[^:]+)?'- r':\s*'- r'(?:"""([\s\S]*?)"""|\'\'\'([\s\S]*?)\'\'\')?'- )- for match in re.finditer(group_pattern, content):- decorator_args = match.group(1) or ""- func_name = match.group(2)- doc = (match.group(3) or match.group(4) or "").strip()- explicit_name = re.search(r'["\']([^"\']+)["\']', decorator_args)- name = explicit_name.group(1) if explicit_name else func_name.replace("_", " ")- display_name = name.replace("-", " ").title()- group_name_by_function[func_name] = display_name- groups.append(CommandGroup(name=display_name, description=doc or f"Commands for {name}."))-- command_pattern = (- r'@(\w+)\.command(?:\(([^)]*)\))?'- r'(?:\s*@[\w.]+(?:\([^)]*\))?)*'- r'\s*def\s+(\w+)\([^)]*\)'- r'(?:\s*->\s*[^:]+)?'- r':\s*'- r'(?:"""([\s\S]*?)"""|\'\'\'([\s\S]*?)\'\'\')?'- )- for match in re.finditer(command_pattern, content):- group_func = match.group(1)- decorator_args = match.group(2) or ""- func_name = match.group(3)- doc = (match.group(4) or match.group(5) or "").strip()- explicit_name = re.search(r'["\']([^"\']+)["\']', decorator_args)- cmd_name = explicit_name.group(1) if explicit_name else func_name.replace("_", "-")- title = group_name_by_function.get(group_func, group_func.replace("_", " ").replace("-", " ").title())- for group in groups:- if group.name == title:- group.commands.append(CommandInfo(cmd_name, doc or f"Execute `{cmd_name}`."))- break+ for node in functions:+ for decorator in node.decorator_list:+ owner_name, decorator_name, explicit_name = _click_decorator_info(decorator)+ if decorator_name != "command" or owner_name not in group_name_by_function:+ continue+ display_name = group_name_by_function[owner_name]+ cmd_name = explicit_name or _default_command_name(node.name)+ group_by_display_name[display_name].commands.append(+ CommandInfo(cmd_name, ast.get_docstring(node) or f"Execute `{cmd_name}`.")+ )+ breakreturn groups@@ -188,7 +213,14 @@ def generate_skill_md_simple(metadata: SkillMetadata) -> str:for example in metadata.examples:lines.extend([f"### {example.title}", "", example.description, "", "```bash", example.code, "```", ""])lines.extend(["## Version", "", metadata.version, ""])- return "\n".join(lines)+ return _normalize_generated_markdown("\n".join(lines))+++def _normalize_generated_markdown(content: str) -> str:+ content = re.sub(r"(\|\s*\n)(#{2,3}\s)", r"\1\n\2", content)+ content = re.sub(r"(```\n)(#{2,3}\s)", r"\1\n\2", content)+ content = re.sub(r"\n{3,}", "\n\n", content)+ return content.strip() + "\n"def generate_skill_md(metadata: SkillMetadata, template_path: Optional[str] = None) -> str:@@ -200,9 +232,9 @@ def generate_skill_md(metadata: SkillMetadata, template_path: Optional[str] = Notemplate = Path(template_path) if template_path else Path(__file__).parent / "templates" / "SKILL.md.template"if not template.exists():return generate_skill_md_simple(metadata)- env = Environment(loader=FileSystemLoader(template.parent))+ env = Environment(loader=FileSystemLoader(template.parent), trim_blocks=True, lstrip_blocks=True)tpl = env.get_template(template.name)- return tpl.render(+ rendered = tpl.render(skill_name=metadata.skill_name,skill_description=metadata.skill_description,software_name=metadata.software_name,@@ -214,6 +246,7 @@ def generate_skill_md(metadata: SkillMetadata, template_path: Optional[str] = No],examples=[{"title": ex.title, "description": ex.description, "code": ex.code} for ex in metadata.examples],)+ return _normalize_generated_markdown(rendered)def generate_skill_file(harness_path: str, output_path: Optional[str] = None, template_path: Optional[str] = None) -> str:diff --git a/zotero/agent-harness/templates/SKILL.md.template b/zotero/agent-harness/templates/SKILL.md.templateindex a5e820398..85a1f2c24 100644--- a/zotero/agent-harness/templates/SKILL.md.template+++ b/zotero/agent-harness/templates/SKILL.md.template@@ -31,7 +31,7 @@ python -m cli_anything.{{ software_name }}| Command | Description ||---------|-------------|-{% for cmd in group.commands %}+{% for cmd in group.commands -%}| `{{ cmd.name }}` | {{ cmd.description }} |{% endfor %}