Files
yvgude--lean-ctx/integrations/hermes-lean-ctx/tools.py
T
wehub-resource-sync 26382a7ac6
CI / Clippy (push) Failing after 15m13s
CI / Test (ubuntu-latest) (push) Failing after 16m1s
CI / Test (macos-latest) (push) Has been cancelled
CI / Test (windows-latest) (push) Has been cancelled
CI / Build (no embeddings / no ORT) (push) Has been cancelled
CI / Format (push) Has been cancelled
CI / Cookbook (Node) (push) Has been cancelled
CI / Pi Extension (Node) (push) Has been cancelled
CI / Rust SDK (lean-ctx-client) (push) Has been cancelled
CI / Embed SDK (lean-ctx-sdk) (push) Has been cancelled
CI / Python SDK (leanctx) (push) Has been cancelled
CI / Hermes Plugin (Python) (push) Has been cancelled
CI / SDK Conformance Matrix (push) Has been cancelled
CI / Coverage (push) Has been cancelled
CI / cargo-deny (push) Has been cancelled
CI / Adversarial Safety (push) Has been cancelled
CI / Benchmarks (push) Has been cancelled
CI / Output-Quality Gate (eval A/B) (push) Has been cancelled
CI / Documentation (push) Has been cancelled
CI / CI Green (push) Has been cancelled
JetBrains Plugin / Actionlint (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
CodeQL / Analyze (rust) (push) Has been cancelled
JetBrains Plugin / Validation (push) Has been cancelled
JetBrains Plugin / Build (push) Has been cancelled
JetBrains Plugin / Test (push) Has been cancelled
Security Check / Security Scan (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:35:30 +08:00

61 lines
1.7 KiB
Python

"""Native engine tool dispatch.
``get_tool_schemas()`` advertises lean-ctx's recall surface; ``handle_tool_call``
proxies the call to the daemon over ``/v1`` and returns the tool's text result.
"""
from __future__ import annotations
import json
from typing import Any, Dict, List, Optional
from .config import LeanCtxConfig
from .schemas import ALL_SCHEMAS, TOOL_NAMES
from .transport import ToolGateway
def get_tool_schemas(config: LeanCtxConfig) -> List[Dict[str, Any]]:
"""Return engine tool schemas, or ``[]`` when tools are disabled."""
if not config.enable_tools:
return []
return [dict(schema) for schema in ALL_SCHEMAS]
def _coerce_args(raw: Any) -> Dict[str, Any]:
if raw is None:
return {}
if isinstance(raw, dict):
return raw
if isinstance(raw, str):
text = raw.strip()
if not text:
return {}
try:
parsed = json.loads(text)
except json.JSONDecodeError:
return {}
return parsed if isinstance(parsed, dict) else {}
return {}
def handle_tool_call(
gateway: ToolGateway,
name: str,
args: Any,
**_: Any,
) -> str:
"""Dispatch one engine tool call and return a string result.
Unknown tools and daemon failures return a clear error string rather than
raising, so the agent loop is never broken by the engine.
"""
if name not in TOOL_NAMES:
return json.dumps({"error": f"Unknown tool: {name}"})
arguments: Optional[Dict[str, Any]] = _coerce_args(args)
text = gateway.call_text(name, arguments)
if text is None:
return json.dumps(
{"error": f"lean-ctx daemon unavailable; '{name}' could not be executed."}
)
return text