fix(lib): enhance target agent resolution and import fallback in herdr shim
This commit is contained in:
+53
-13
@@ -246,22 +246,58 @@ case "$cmd" in
|
||||
agent)
|
||||
sub="${1:-}"
|
||||
shift || true
|
||||
_resolve_herdr_target() {
|
||||
local raw="$1"
|
||||
local sat
|
||||
sat=$(_sanitize_herdr_agent_name "$raw")
|
||||
if _real_herdr agent get "$sat" >/dev/null 2>&1; then
|
||||
echo "$sat"
|
||||
return 0
|
||||
fi
|
||||
if _real_herdr agent get "$raw" >/dev/null 2>&1; then
|
||||
echo "$raw"
|
||||
return 0
|
||||
fi
|
||||
local from_list
|
||||
from_list=$(_real_herdr agent list 2>/dev/null | TARGET_NAME="$raw" python3 -c '
|
||||
import sys, json, os
|
||||
tn = os.environ.get("TARGET_NAME", "")
|
||||
try:
|
||||
d = json.loads(sys.stdin.read())
|
||||
for a in d.get("result", {}).get("agents", []):
|
||||
name = a.get("name", "")
|
||||
agent = a.get("agent", "")
|
||||
pane_id = a.get("pane_id", "")
|
||||
if name == tn or (not name and agent and agent in tn):
|
||||
print(pane_id or agent)
|
||||
sys.exit(0)
|
||||
except Exception:
|
||||
pass
|
||||
sys.exit(1)
|
||||
' 2>/dev/null || echo "")
|
||||
if [ -n "$from_list" ]; then
|
||||
echo "$from_list"
|
||||
return 0
|
||||
fi
|
||||
echo "$raw"
|
||||
}
|
||||
|
||||
if [ "$sub" = "prompt" ] && [ $# -ge 2 ]; then
|
||||
t="$1"
|
||||
txt="$2"
|
||||
shift 2
|
||||
at=$(_sanitize_herdr_agent_name "$t")
|
||||
_real_herdr agent prompt "$at" "$txt" "$@" 2>/dev/null || _real_herdr agent prompt "$t" "$txt" "$@"
|
||||
tgt=$(_resolve_herdr_target "$t")
|
||||
_real_herdr agent prompt "$tgt" "$txt" "$@"
|
||||
elif [ "$sub" = "get" ] && [ $# -ge 1 ]; then
|
||||
t="$1"
|
||||
shift
|
||||
at=$(_sanitize_herdr_agent_name "$t")
|
||||
_real_herdr agent get "$at" "$@" 2>/dev/null || _real_herdr agent get "$t" "$@"
|
||||
tgt=$(_resolve_herdr_target "$t")
|
||||
_real_herdr agent get "$tgt" "$@"
|
||||
elif [ "$sub" = "read" ] && [ $# -ge 1 ]; then
|
||||
t="$1"
|
||||
shift
|
||||
at=$(_sanitize_herdr_agent_name "$t")
|
||||
_real_herdr agent read "$at" "$@" 2>/dev/null || _real_herdr agent read "$t" "$@"
|
||||
tgt=$(_resolve_herdr_target "$t")
|
||||
_real_herdr agent read "$tgt" "$@"
|
||||
else
|
||||
_real_herdr agent "$sub" "$@"
|
||||
fi
|
||||
@@ -284,22 +320,26 @@ case "$cmd" in
|
||||
if _real_herdr agent get "$(_sanitize_herdr_agent_name "$sess")" >/dev/null 2>&1 || _real_herdr agent get "$sess" >/dev/null 2>&1; then
|
||||
exit 0
|
||||
fi
|
||||
if _real_herdr agent list 2>/dev/null | TARGET_NAME="$sess" python3 -c "
|
||||
if _real_herdr agent list 2>/dev/null | TARGET_NAME="$sess" python3 -c '
|
||||
import sys, json, os
|
||||
from lib_py.agents.sanitize import sanitize_herdr_agent_name
|
||||
tn = os.environ.get('TARGET_NAME', '')
|
||||
try:
|
||||
sys.path.insert(0, os.path.abspath(".agents/skills"))
|
||||
from lib_py.agents.sanitize import sanitize_herdr_agent_name
|
||||
except Exception:
|
||||
sanitize_herdr_agent_name = lambda s: s.lower()[:32] if s else "agent"
|
||||
tn = os.environ.get("TARGET_NAME", "")
|
||||
stn = sanitize_herdr_agent_name(tn)
|
||||
try:
|
||||
d = json.loads(sys.stdin.read())
|
||||
agents = d.get('result', {}).get('agents', [])
|
||||
agents = d.get("result", {}).get("agents", [])
|
||||
for a in agents:
|
||||
an = a.get('name', '')
|
||||
if an == tn or an == stn:
|
||||
an = a.get("name", "")
|
||||
if (an and (an == tn or an == stn)) or (not an and a.get("agent") and a.get("agent") in tn):
|
||||
sys.exit(0)
|
||||
except Exception:
|
||||
pass
|
||||
sys.exit(1)
|
||||
"; then
|
||||
'; then
|
||||
exit 0
|
||||
fi
|
||||
exit 1
|
||||
|
||||
Reference in New Issue
Block a user