send_test1.sh
4.41 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/bash
# 模拟用户消息发送到 Agent 服务
# 用法: ./send_test.sh "你的问题"
# 例如: ./send_test.sh "帮我整理上周的会议纪要"
if grep -q $'\r' "$0" 2>/dev/null; then
if command -v mktemp >/dev/null 2>&1; then
_tmp="$(mktemp)"
else
_tmp="/tmp/send_test.$$.sh"
fi
tr -d '\r' < "$0" > "$_tmp" && chmod +x "$_tmp" && exec bash "$_tmp" "$@"
fi
QUERY="${1:-你好,请问你能帮我什么?}"
QUERY="${QUERY//\`/}"
API_URL="${API_URL:-http://localhost:8000/api/v1/chat/completions}"
SESSION_ID="${SESSION_ID:-test_sess_001}"
POSITION_CODE="${POSITION_CODE:-conference_assistant}"
POSITION_ID="${POSITION_ID:-1}"
pick_python() {
if [ -n "${CONDA_PREFIX:-}" ]; then
CP_U="${CONDA_PREFIX}"
if command -v cygpath >/dev/null 2>&1; then
CP_U=$(cygpath -u "${CONDA_PREFIX}")
fi
if [ -x "${CP_U}/python" ]; then
echo "${CP_U}/python"
return 0
fi
if [ -x "${CP_U}/python.exe" ]; then
echo "${CP_U}/python.exe"
return 0
fi
fi
if command -v python >/dev/null 2>&1; then
P=$(command -v python)
case "$P" in
*/WindowsApps/*) ;;
*) echo "python"; return 0 ;;
esac
fi
if command -v python3 >/dev/null 2>&1; then
P=$(command -v python3)
case "$P" in
*/WindowsApps/*) ;;
*) echo "python3"; return 0 ;;
esac
fi
return 1
}
echo "=========================================="
echo " 发送测试消息到 AgentHub"
echo "=========================================="
echo ""
echo "提问: ${QUERY}"
echo ""
if command -v jq >/dev/null 2>&1; then
PAYLOAD=$(jq \
--arg q "${QUERY}" \
--arg sid "${SESSION_ID}" \
--arg pc "${POSITION_CODE}" \
--argjson pid "${POSITION_ID}" \
'.session_id = $sid
| .position = {position_id: $pid, position_code: $pc, position_name: ""}
| .messages[0].msg_content = $q' \
test_request.json)
else
PY=$(pick_python) || {
echo "ERROR: python/python3 not found in PATH. Please run in an environment with python (e.g., conda activate ai-workflow), or install Python, or install jq." 1>&2
exit 1
}
PAYLOAD=$(QUERY="$QUERY" SESSION_ID="$SESSION_ID" POSITION_CODE="$POSITION_CODE" POSITION_ID="$POSITION_ID" "$PY" - <<'PY'
import json
import os
from pathlib import Path
query = os.environ.get("QUERY", "")
session_id = os.environ.get("SESSION_ID", "test_sess_001")
position_code = os.environ.get("POSITION_CODE", "conference_assistant")
position_id = int(os.environ.get("POSITION_ID", "1"))
data = json.loads(Path("test_request.json").read_text(encoding="utf-8"))
data["session_id"] = session_id
data["position"] = {
"position_id": position_id,
"position_code": position_code,
"position_name": "",
}
msgs = data.get("messages") or []
if not msgs:
msgs = [
{
"msg_id": "m1",
"msg_time": "2026-05-23T10:00:00Z",
"sender_id": "test_user_001",
"sender_name": "测试用户",
"msg_type": "text",
"msg_content": "",
"payload": {},
}
]
data["messages"] = msgs
data["messages"][0]["msg_type"] = data["messages"][0].get("msg_type") or "text"
data["messages"][0]["msg_content"] = query
print(json.dumps(data, ensure_ascii=False, separators=(",", ":")))
PY
)
fi
if [ -z "$PAYLOAD" ]; then
echo "ERROR: payload is empty" 1>&2
exit 1
fi
echo "--- 请求体 ---"
if command -v jq >/dev/null 2>&1; then
echo "${PAYLOAD}" | jq .
else
PY=$(pick_python) && printf '%s' "${PAYLOAD}" | "$PY" -c 'import json,sys; obj=json.load(sys.stdin); print(json.dumps(obj, ensure_ascii=False, indent=2))'
fi
echo ""
echo "--- 开始请求 ---"
echo "POST ${API_URL}"
echo ""
HTTP_CODE=$(printf '%s' "${PAYLOAD}" | curl -s -w "\n%{http_code}" -X POST "${API_URL}" \
-H "Content-Type: application/json; charset=utf-8" \
--data-binary @-)
# 分离 body 和 status code
BODY=$(echo "${HTTP_CODE}" | sed '$d')
STATUS=$(echo "${HTTP_CODE}" | tail -n1)
echo "--- 响应 (HTTP ${STATUS}) ---"
if command -v jq >/dev/null 2>&1; then
echo "${BODY}" | jq . 2>/dev/null || echo "${BODY}"
else
PY=$(pick_python) && printf '%s' "${BODY}" | "$PY" -c 'import json,sys; s=sys.stdin.read();
try:
obj=json.loads(s) if s.strip() else None
print(json.dumps(obj, ensure_ascii=False, indent=2)) if obj is not None else None
except Exception:
sys.stdout.write(s)'
fi
echo ""
echo "=========================================="
echo " 完成"
echo "=========================================="