send_test1.sh 5.44 KB
#!/usr/bin/env bash
# 模拟用户消息发送到 Agent 服务
#
# 用法 1: 普通问题
#   ./send_test1.sh "你的问题"
#   例如: ./send_test1.sh "帮我整理上周的会议纪要"
#
# 用法 2: 纯文本会议记录(从 stdin 读取)
#   cat minutes.txt | ./send_test1.sh --from-stdin [--template A/B/C/D/E]
#   echo "会议记录内容..." | ./send_test1.sh --from-stdin
#
# 注意:不要用反引号包 URL(例如 `https://...`),反引号在 bash 中会触发命令替换。
# 如果你一定要保留反引号,请用单引号包住整个参数:bash ./send_test1.sh '... `https://...` ...'

FROM_STDIN=false
TEMPLATE=""
QUERY_ARGS=()

# 解析参数
while [[ $# -gt 0 ]]; do
  case $1 in
    --from-stdin)
      FROM_STDIN=true
      shift
      ;;
    --template)
      TEMPLATE="$2"
      shift 2
      ;;
    *)
      QUERY_ARGS+=("$1")
      shift
      ;;
  esac
done

# 根据模式构造查询内容
if [ "$FROM_STDIN" = true ]; then
  # 从 stdin 读取会议记录文本
  echo "读取 stdin 中的会议记录文本..." >&2
  STDIN_TEXT=$(cat)
  
  if [ -z "$STDIN_TEXT" ]; then
    echo "错误: stdin 为空,请提供会议记录文本" >&2
    exit 1
  fi
  
  # 构造查询内容
  if [ -n "$TEMPLATE" ]; then
    QUERY="请根据以下会议记录生成纪要(使用模板 ${TEMPLATE}):\n\n${STDIN_TEXT}"
  else
    QUERY="请根据以下会议记录生成纪要:\n\n${STDIN_TEXT}"
  fi
else
  # 普通模式:使用命令行参数
  QUERY="${QUERY_ARGS[0]:-你好,请问你能帮我什么?}"
fi

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_type = "text"
     | .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"] = "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=$(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 "=========================================="