-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlegacy_agentic.py
More file actions
144 lines (111 loc) · 3.36 KB
/
legacy_agentic.py
File metadata and controls
144 lines (111 loc) · 3.36 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
138
139
140
141
142
143
144
#!/usr/bin/env python3
"""
Legacy subprocess plugin for Open Agentic 2.0.
This script:
- Reads a single JSON object from stdin:
{"op": "<operation>", "params": {...}}
- Writes a single JSON object to stdout:
{
"ok": bool,
"result": any,
"evidence": {"coverage": float, "sources": [...]},
"reasons": [str, ...]
}
It is designed to be called by LegacySubprocess in agentic2_micro_plugin.py.
"""
from __future__ import annotations
import json
import sys
import traceback
from typing import Any, Dict
Output = Dict[str, Any]
def _error(reason: str) -> Output:
return {
"ok": False,
"result": None,
"evidence": None,
"reasons": [reason],
}
def _ok(result: Any, sources=None, coverage: float = 0.80) -> Output:
if sources is None:
sources = ["legacy"]
return {
"ok": True,
"result": result,
"evidence": {
"coverage": float(coverage),
"sources": list(sources),
},
"reasons": [],
}
# ---------------------------------------------------------------------------
# Handlers
# ---------------------------------------------------------------------------
def handle_echo(params: Dict[str, Any]) -> Output:
"""
Simple echo tool for demos and tests.
"""
msg = str(params.get("msg", "") or "")
if not msg:
return _error("empty msg")
return _ok(msg, sources=["legacy", "echo"], coverage=0.85)
def handle_summarize(params: Dict[str, Any]) -> Output:
"""
Very small summarizer: just trims long strings.
"""
text = str(params.get("text", "") or "")
if not text:
return _error("missing text")
summary = (text[:200] + "…") if len(text) > 200 else text
return _ok(
{"summary": summary},
sources=["legacy", "summarize"],
coverage=0.85,
)
def handle_health(params: Dict[str, Any]) -> Output:
"""
Health-check endpoint so you can verify the plugin is alive.
"""
return _ok(
{"status": "ok", "info": "legacy_agentic alive"},
sources=["legacy", "health"],
coverage=1.0,
)
HANDLERS = {
"echo": handle_echo,
"summarize": handle_summarize,
"health": handle_health,
}
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def main() -> None:
try:
raw = sys.stdin.read()
data = json.loads(raw or "{}")
except Exception:
out = _error("invalid JSON on stdin")
print(json.dumps(out))
return
op = str(data.get("op") or data.get("operation") or "").strip()
params = data.get("params") or {}
if not op:
print(json.dumps(_error("missing op")))
return
if not isinstance(params, dict):
print(json.dumps(_error("params must be an object")))
return
handler = HANDLERS.get(op)
if handler is None:
print(json.dumps(_error(f"unknown op: {op}")))
return
try:
out = handler(params)
except Exception:
# Fail safe: log details to stderr, return a generic error to the caller.
sys.stderr.write("legacy_agentic internal error:\n")
traceback.print_exc(file=sys.stderr)
out = _error("internal error")
print(json.dumps(out))
if __name__ == "__main__":
main()