AgentEvalTool/scripts/mock_call.py
sinohqb a77cd83e6a v0.2.0-dev: 文件管理 + 页面布局统一 + 6 个 bug 修复
## 新增功能
- 文件管理模块:分类树 + 文件上传/下载/删除
- 文件上传支持拖拽(Dragger)+ 手动上传(customRequest 模式)

## 页面布局统一(参照评测执行页)
- 仪表盘/评测对象/评测场景/评测报告 全部改为全高 flex 布局
- 统一内联页头样式(h2 + 竖线分隔 + 描述)
- 表格撑满高度、overflow 处理
- 每页添加刷新按钮

## Bug 修复
- 分类树操作按钮 hover 不可见(CSS 规则缺失)
- 文件上传失败(multipart boundary 缺失)
- LLM API 响应 content blocks 数组格式支持(_extract_content_from_api_response)
- response_time_max_ms 被静默忽略(隐式规则传空 params)
- 空 messages 导致 IndexError 崩溃
- poll_reply 异常中止整个 run(缺 try/catch)
- engine finally 未关闭 session
- 3 个页面 UTC 时间戳解析偏差 8 小时

## 后端
- EvalEngine: poll_reply 异常保护、空 dialog 保护、session 关闭
- LLM API 响应解析支持 content-block-array 格式
- 隐式 response_time 规则正确传递 max_ms 参数

## 前端
- api.ts: 移除手动 Content-Type(让浏览器自动添加 boundary)
- Files.tsx: customRequest 替代 beforeUpload、布局优化
- index.css: 分类树 hover 规则
- Targets/Scenarios/Home/Reports: 全高布局改造
- 3 个页面时间戳改用 formatDateTime()(修复 UTC 偏差)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-16 15:25:22 +08:00

156 lines
5.2 KiB
Python

import argparse
import json
import sys
from datetime import datetime
import requests
def load_config(path="config/config.json"):
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
def build_url(config, path):
return f"{config['base_url']}/api/{config['tenant']}/{path}"
def build_headers(config, accept="*/*"):
return {
"accept": accept,
"Authorization": f"Bearer {config['token']}",
"Content-Type": "application/json",
}
def cmd_send(config, args):
"""POST /v1/chat/message/sendMsg - 发送普通消息"""
payload = {
"chatChannelId": config["chat_channel_id"],
"chatContactType": "EXTERNAL",
"chatContactId": config["chat_contact_id"],
"msgType": args.msg_type or "text",
"msgBody": json.dumps({"content": args.content}, ensure_ascii=False),
"actualSenderType": args.sender_type or "WORK_WE_CUSTOMER",
"sender": {"type": "EXTERNAL"},
}
url = build_url(config, "v1/chat/message/sendMsg")
resp = requests.post(url, headers=build_headers(config), json=payload, timeout=15)
return resp
def cmd_stream(config, args):
"""POST /v1/chat/message/sendMsgStream - 发送消息(流式返回SSE)"""
payload = {
"chatChannelId": config["chat_channel_id"],
"chatContactType": "EXTERNAL",
"chatContactId": config["chat_contact_id"],
"msgType": args.msg_type or "text",
"msgBody": json.dumps({"content": args.content}, ensure_ascii=False),
"actualSenderType": args.sender_type or "WORK_WE_CUSTOMER",
"sender": {"type": "EXTERNAL"},
}
url = build_url(config, "v1/chat/message/sendMsgStream")
resp = requests.post(
url,
headers=build_headers(config, accept="text/event-stream"),
json=payload,
timeout=60,
stream=True,
)
if resp.status_code == 200:
print(f"[{now()}] 流式响应:")
for line in resp.iter_lines(decode_unicode=True):
if line:
print(f" {line}")
return resp
def cmd_history(config, args):
"""GET /v1/chat/message - 获取聊天记录"""
params = {
"chatChannelId": config["chat_channel_id"],
"chatContactId": config["chat_contact_id"],
"page": args.page or 0,
"size": args.size or 10,
}
if args.start_time:
params["startMsgTime"] = args.start_time
if args.end_time:
params["endMsgTime"] = args.end_time
url = build_url(config, "v1/chat/message")
resp = requests.get(url, headers=build_headers(config), params=params, timeout=15)
if resp.status_code == 200:
data = resp.json()
records = data.get("data", [])
total = data.get("total", 0)
print(f"[{now()}] 共 {total} 条记录, 当前页 {len(records)} 条:")
for msg in records:
sender = msg.get("senderName") or msg.get("actualSenderName") or msg.get("sender", "?")
print(f" [{msg.get('msgTime', '')}] {sender}: {json.dumps(msg.get('msgBody', {}), ensure_ascii=False)}")
return resp
def now():
return datetime.now().strftime("%H:%M:%S")
def print_result(resp):
print(f"[{now()}] HTTP {resp.status_code}")
if resp.status_code == 200:
try:
data = resp.json()
print(f"[{now()}] 响应: {json.dumps(data, ensure_ascii=False, indent=2)}")
status = data.get("status")
if status:
print(f"[{now()}] 消息状态: {status}")
except Exception:
print(f"[{now()}] 响应: {resp.text[:500]}")
else:
print(f"[{now()}] 错误: {resp.text[:500] if resp.text else '(空响应)'}")
def main():
parser = argparse.ArgumentParser(description="Tutu API 模拟调用工具")
sub = parser.add_subparsers(dest="command")
p_send = sub.add_parser("send", help="发送普通消息")
p_send.add_argument("content", help="消息内容")
p_send.add_argument("--msg-type", default="text", help="消息类型 (默认 text)")
p_send.add_argument("--sender-type", help="发送者类型 (默认 WORK_WE_CUSTOMER)")
p_stream = sub.add_parser("stream", help="发送流式消息 (SSE)")
p_stream.add_argument("content", help="消息内容")
p_stream.add_argument("--msg-type", default="text", help="消息类型")
p_stream.add_argument("--sender-type", help="发送者类型")
p_hist = sub.add_parser("history", help="获取聊天记录")
p_hist.add_argument("--page", type=int, default=0, help="页码 (默认 0)")
p_hist.add_argument("--size", type=int, default=10, help="每页条数 (默认 10)")
p_hist.add_argument("--start-time", help="开始时间 (yyyy-MM-dd HH:mm:ss)")
p_hist.add_argument("--end-time", help="结束时间")
args = parser.parse_args()
if not args.command:
parser.print_help()
sys.exit(1)
config = load_config()
commands = {
"send": cmd_send,
"stream": cmd_stream,
"history": cmd_history,
}
print(f"[{now()}] 命令: {args.command}")
resp = commands[args.command](config, args)
if args.command != "stream" or resp.status_code != 200:
print_result(resp)
if __name__ == "__main__":
main()