-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
137 lines (116 loc) · 4.26 KB
/
cli.py
File metadata and controls
137 lines (116 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""CLI commands for the TableStore memory provider."""
from __future__ import annotations
import json
import sys
import uuid
def _parse_metadata(values: list[str] | None) -> dict[str, str]:
metadata: dict[str, str] = {}
for item in values or []:
key, sep, value = item.partition("=")
key = key.strip()
value = value.strip()
if not sep or not key or not value:
raise ValueError(f"Invalid metadata item '{item}'. Expected KEY=VALUE.")
metadata[key] = value
return metadata
def _make_provider():
from plugins.memory import load_memory_provider
provider = load_memory_provider("tablestore-mem")
if not provider:
raise RuntimeError("TableStore provider could not be loaded.")
provider.initialize(
f"cli-tablestore-mem-{uuid.uuid4().hex[:8]}",
platform="cli",
agent_identity="hermes",
)
return provider
def _print_json(payload: dict) -> None:
print(json.dumps(payload, ensure_ascii=False, indent=2))
def tablestore_command(args) -> None:
sub = getattr(args, "tablestore_command", None)
if sub not in {"add", "search", "doctor"}:
print("Usage: hermes tablestore-mem <add|search|doctor>")
sys.exit(2)
return
try:
provider = _make_provider()
except Exception as exc:
print(f"Failed to initialize tablestore provider: {exc}")
sys.exit(1)
return
try:
if sub == "add":
payload = {
"content": args.content,
"sync": bool(getattr(args, "sync_write", False)),
}
metadata = _parse_metadata(getattr(args, "metadata", None))
if metadata:
payload["metadata"] = metadata
result = json.loads(provider.handle_tool_call("tablestore_remember", payload))
_print_json(result)
if isinstance(result, dict) and result.get("error"):
sys.exit(1)
return
if sub == "search":
payload = {
"query": args.query,
"top_k": args.top_k,
}
metadata = _parse_metadata(getattr(args, "metadata", None))
if metadata:
payload["metadata"] = metadata
result = json.loads(provider.handle_tool_call("tablestore_search", payload))
_print_json(result)
if isinstance(result, dict) and result.get("error"):
sys.exit(1)
return
if sub == "doctor":
result = provider.run_doctor()
_print_json(result)
if isinstance(result, dict) and not result.get("ok", False):
sys.exit(1)
return
except ValueError as exc:
print(str(exc))
sys.exit(2)
except Exception as exc:
print(f"TableStore command failed: {exc}")
sys.exit(1)
finally:
provider.shutdown()
def register_cli(subparser) -> None:
"""Build the ``hermes tablestore-mem`` argparse tree."""
subs = subparser.add_subparsers(dest="tablestore_command")
add_parser = subs.add_parser("add", help="Store a memory in TableStore")
add_parser.add_argument("content", help="Content to persist")
add_parser.add_argument(
"--metadata",
action="append",
metavar="KEY=VALUE",
help="Optional metadata pair. Repeat for multiple values.",
)
add_parser.add_argument(
"--sync",
dest="sync_write",
action="store_true",
help="Wait for the write to finish before returning.",
)
add_parser.set_defaults(func=tablestore_command)
search_parser = subs.add_parser("search", help="Search memories in TableStore")
search_parser.add_argument("query", help="Search query")
search_parser.add_argument(
"--top-k",
type=int,
default=5,
help="Maximum results to return (default: 5)",
)
search_parser.add_argument(
"--metadata",
action="append",
metavar="KEY=VALUE",
help="Optional metadata filter. Repeat for multiple values.",
)
search_parser.set_defaults(func=tablestore_command)
doctor_parser = subs.add_parser("doctor", help="Run read-only diagnostics for TableStore")
doctor_parser.set_defaults(func=tablestore_command)