templateData.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. templateData.py - 生成 Trace 可视化的 Mock 数据
  5. """
  6. import json
  7. from datetime import datetime
  8. from typing import Dict, List, Any
  9. def generate_mock_trace_list() -> Dict[str, Any]:
  10. """生成 Trace 列表的 Mock 数据"""
  11. return {
  12. "traces": [
  13. {
  14. "trace_id": "trace_001",
  15. "mode": "agent",
  16. "task": "实现用户登录功能",
  17. "status": "completed",
  18. "total_messages": 15,
  19. "total_tokens": 3500,
  20. "total_cost": 0.05,
  21. "current_goal_id": "goal_005",
  22. "created_at": "2024-01-15T10:30:00Z"
  23. },
  24. {
  25. "trace_id": "trace_002",
  26. "mode": "agent",
  27. "task": "优化数据库查询性能",
  28. "status": "running",
  29. "total_messages": 8,
  30. "total_tokens": 2100,
  31. "total_cost": 0.03,
  32. "current_goal_id": "goal_003",
  33. "created_at": "2024-01-15T11:00:00Z"
  34. },
  35. {
  36. "trace_id": "trace_003",
  37. "mode": "call",
  38. "task": "修复支付接口bug",
  39. "status": "failed",
  40. "total_messages": 5,
  41. "total_tokens": 1200,
  42. "total_cost": 0.02,
  43. "current_goal_id": "goal_002",
  44. "created_at": "2024-01-15T09:45:00Z"
  45. }
  46. ],
  47. "total": 3
  48. }
  49. def generate_mock_trace_detail(trace_id: str = "trace_001") -> Dict[str, Any]:
  50. """生成 Trace 详情的 Mock 数据(包含 GoalTree)"""
  51. return {
  52. "trace_id": trace_id,
  53. "mode": "agent",
  54. "task": "实现用户登录功能",
  55. "status": "completed",
  56. "total_messages": 15,
  57. "total_tokens": 3500,
  58. "total_cost": 0.05,
  59. "created_at": "2024-01-15T10:30:00Z",
  60. "completed_at": "2024-01-15T11:45:00Z",
  61. "goal_tree": {
  62. "mission": "实现用户登录功能",
  63. "current_id": "goal_005",
  64. "goals": [
  65. {
  66. "id": "goal_001",
  67. "parent_id": None,
  68. "branch_id": None,
  69. "type": "normal",
  70. "description": "分析登录功能需求",
  71. "reason": "需要先理解需求才能开始实现",
  72. "status": "completed",
  73. "summary": "已完成需求分析,确定需要实现用户名密码登录和第三方登录",
  74. "self_stats": {
  75. "message_count": 3,
  76. "total_tokens": 800,
  77. "total_cost": 0.01,
  78. "preview": "read × 2 → analyze"
  79. },
  80. "cumulative_stats": {
  81. "message_count": 15,
  82. "total_tokens": 3500,
  83. "total_cost": 0.05,
  84. "preview": "read × 5 → write × 3 → test × 2"
  85. }
  86. },
  87. {
  88. "id": "goal_002",
  89. "parent_id": "goal_001",
  90. "branch_id": None,
  91. "type": "normal",
  92. "description": "设计数据库表结构",
  93. "reason": "需要存储用户信息和登录凭证",
  94. "status": "completed",
  95. "summary": "已创建 users 表和 auth_tokens 表",
  96. "self_stats": {
  97. "message_count": 2,
  98. "total_tokens": 500,
  99. "total_cost": 0.008,
  100. "preview": "design → create"
  101. },
  102. "cumulative_stats": {
  103. "message_count": 12,
  104. "total_tokens": 2700,
  105. "total_cost": 0.04,
  106. "preview": "design → write × 3 → test × 2"
  107. }
  108. },
  109. {
  110. "id": "goal_003",
  111. "parent_id": "goal_002",
  112. "branch_id": None,
  113. "type": "explore_start",
  114. "description": "探索不同的认证方案",
  115. "reason": "需要选择最合适的认证方式",
  116. "status": "completed",
  117. "summary": "决定使用 JWT 作为认证方案",
  118. "self_stats": {
  119. "message_count": 4,
  120. "total_tokens": 900,
  121. "total_cost": 0.012,
  122. "preview": "research × 3 → compare"
  123. },
  124. "cumulative_stats": {
  125. "message_count": 10,
  126. "total_tokens": 2200,
  127. "total_cost": 0.032,
  128. "preview": "research × 3 → write × 2 → test"
  129. },
  130. "branch_ids": ["branch_001", "branch_002"]
  131. },
  132. {
  133. "id": "goal_004",
  134. "parent_id": "goal_003",
  135. "branch_id": None,
  136. "type": "explore_merge",
  137. "description": "整合认证方案",
  138. "reason": "需要将选定的方案整合到系统中",
  139. "status": "completed",
  140. "summary": "已完成 JWT 认证的集成",
  141. "self_stats": {
  142. "message_count": 3,
  143. "total_tokens": 700,
  144. "total_cost": 0.01,
  145. "preview": "integrate × 2 → test"
  146. },
  147. "cumulative_stats": {
  148. "message_count": 6,
  149. "total_tokens": 1300,
  150. "total_cost": 0.02,
  151. "preview": "integrate × 2 → test × 2"
  152. },
  153. "explore_start_id": "goal_003",
  154. "merge_summary": "JWT 方案性能最优,安全性高",
  155. "selected_branch": "branch_001"
  156. },
  157. {
  158. "id": "goal_005",
  159. "parent_id": "goal_004",
  160. "branch_id": None,
  161. "type": "normal",
  162. "description": "实现登录API接口",
  163. "reason": "需要提供登录的HTTP接口",
  164. "status": "completed",
  165. "summary": "已完成 /api/login 接口的实现和测试",
  166. "self_stats": {
  167. "message_count": 3,
  168. "total_tokens": 600,
  169. "total_cost": 0.009,
  170. "preview": "write × 2 → test"
  171. },
  172. "cumulative_stats": {
  173. "message_count": 3,
  174. "total_tokens": 600,
  175. "total_cost": 0.009,
  176. "preview": "write × 2 → test"
  177. }
  178. }
  179. ]
  180. },
  181. "branches": {
  182. "branch_001": {
  183. "id": "branch_001",
  184. "explore_start_id": "goal_003",
  185. "description": "JWT 认证方案",
  186. "status": "completed",
  187. "summary": "JWT 方案实现完成,性能测试通过",
  188. "cumulative_stats": {
  189. "message_count": 5,
  190. "total_tokens": 1100,
  191. "total_cost": 0.015,
  192. "preview": "implement × 3 → test × 2"
  193. },
  194. "goal_count": 3,
  195. "last_message": {
  196. "message_id": "msg_015",
  197. "role": "assistant",
  198. "content": "JWT 认证方案实现完成"
  199. }
  200. },
  201. "branch_002": {
  202. "id": "branch_002",
  203. "explore_start_id": "goal_003",
  204. "description": "Session 认证方案",
  205. "status": "abandoned",
  206. "summary": "Session 方案在分布式环境下存在问题,已放弃",
  207. "cumulative_stats": {
  208. "message_count": 3,
  209. "total_tokens": 600,
  210. "total_cost": 0.008,
  211. "preview": "implement × 2 → test"
  212. },
  213. "goal_count": 2,
  214. "last_message": {
  215. "message_id": "msg_010",
  216. "role": "assistant",
  217. "content": "Session 方案不适合当前架构"
  218. }
  219. }
  220. }
  221. }
  222. def generate_mock_messages(trace_id: str = "trace_001", goal_id: str = "goal_001") -> Dict[str, Any]:
  223. """生成 Messages 的 Mock 数据"""
  224. return {
  225. "messages": [
  226. {
  227. "message_id": "msg_001",
  228. "role": "assistant",
  229. "sequence": 1,
  230. "goal_id": goal_id,
  231. "branch_id": None,
  232. "description": "开始分析需求文档",
  233. "content": {
  234. "text": "我将开始分析登录功能的需求文档",
  235. "tool_calls": [
  236. {
  237. "id": "call_001",
  238. "name": "read_file",
  239. "arguments": {
  240. "path": "docs/requirements.md"
  241. }
  242. }
  243. ]
  244. },
  245. "tokens": 150,
  246. "cost": 0.002,
  247. "created_at": "2024-01-15T10:30:05Z"
  248. },
  249. {
  250. "message_id": "msg_002",
  251. "role": "tool",
  252. "sequence": 2,
  253. "goal_id": goal_id,
  254. "branch_id": None,
  255. "tool_call_id": "call_001",
  256. "tokens": 200,
  257. "cost": 0.003,
  258. "created_at": "2024-01-15T10:30:06Z"
  259. },
  260. {
  261. "message_id": "msg_003",
  262. "role": "assistant",
  263. "sequence": 3,
  264. "goal_id": goal_id,
  265. "branch_id": None,
  266. "description": "分析需求并总结",
  267. "content": {
  268. "text": "根据需求文档,需要实现以下功能:\n1. 用户名密码登录\n2. 第三方登录(微信、支付宝)\n3. 记住登录状态\n4. 登录失败重试限制",
  269. "tool_calls": []
  270. },
  271. "tokens": 180,
  272. "cost": 0.0025,
  273. "created_at": "2024-01-15T10:30:10Z"
  274. }
  275. ],
  276. "total": 3
  277. }
  278. def generate_mock_branch_detail(trace_id: str = "trace_001", branch_id: str = "branch_001") -> Dict[str, Any]:
  279. """生成分支详情的 Mock 数据"""
  280. return {
  281. "id": branch_id,
  282. "explore_start_id": "goal_003",
  283. "description": "JWT 认证方案",
  284. "status": "completed",
  285. "summary": "JWT 方案实现完成,性能测试通过",
  286. "goal_tree": {
  287. "mission": "实现 JWT 认证",
  288. "current_id": "branch_goal_003",
  289. "goals": [
  290. {
  291. "id": "branch_goal_001",
  292. "parent_id": None,
  293. "branch_id": branch_id,
  294. "type": "normal",
  295. "description": "研究 JWT 原理",
  296. "reason": "需要理解 JWT 的工作机制",
  297. "status": "completed",
  298. "summary": "已完成 JWT 原理学习",
  299. "self_stats": {
  300. "message_count": 2,
  301. "total_tokens": 400,
  302. "total_cost": 0.005,
  303. "preview": "research × 2"
  304. },
  305. "cumulative_stats": {
  306. "message_count": 5,
  307. "total_tokens": 1100,
  308. "total_cost": 0.015,
  309. "preview": "research × 2 → implement × 3"
  310. }
  311. },
  312. {
  313. "id": "branch_goal_002",
  314. "parent_id": "branch_goal_001",
  315. "branch_id": branch_id,
  316. "type": "normal",
  317. "description": "实现 JWT 生成和验证",
  318. "reason": "需要实现核心功能",
  319. "status": "completed",
  320. "summary": "已完成 JWT 的生成和验证逻辑",
  321. "self_stats": {
  322. "message_count": 2,
  323. "total_tokens": 500,
  324. "total_cost": 0.007,
  325. "preview": "implement × 2"
  326. },
  327. "cumulative_stats": {
  328. "message_count": 3,
  329. "total_tokens": 700,
  330. "total_cost": 0.01,
  331. "preview": "implement × 2 → test"
  332. }
  333. },
  334. {
  335. "id": "branch_goal_003",
  336. "parent_id": "branch_goal_002",
  337. "branch_id": branch_id,
  338. "type": "normal",
  339. "description": "测试 JWT 性能",
  340. "reason": "需要验证性能是否满足要求",
  341. "status": "completed",
  342. "summary": "性能测试通过,QPS 达到 5000+",
  343. "self_stats": {
  344. "message_count": 1,
  345. "total_tokens": 200,
  346. "total_cost": 0.003,
  347. "preview": "test"
  348. },
  349. "cumulative_stats": {
  350. "message_count": 1,
  351. "total_tokens": 200,
  352. "total_cost": 0.003,
  353. "preview": "test"
  354. }
  355. }
  356. ]
  357. },
  358. "cumulative_stats": {
  359. "message_count": 5,
  360. "total_tokens": 1100,
  361. "total_cost": 0.015,
  362. "preview": "research × 2 → implement × 2 → test"
  363. }
  364. }
  365. def save_mock_data_to_file():
  366. """将 Mock 数据保存到文件"""
  367. import os
  368. # 创建 mock_data 目录
  369. mock_dir = os.path.join(os.path.dirname(__file__), "mock_data")
  370. os.makedirs(mock_dir, exist_ok=True)
  371. # 保存各类 Mock 数据
  372. with open(os.path.join(mock_dir, "trace_list.json"), "w", encoding="utf-8") as f:
  373. json.dump(generate_mock_trace_list(), f, ensure_ascii=False, indent=2)
  374. with open(os.path.join(mock_dir, "trace_detail.json"), "w", encoding="utf-8") as f:
  375. json.dump(generate_mock_trace_detail(), f, ensure_ascii=False, indent=2)
  376. with open(os.path.join(mock_dir, "messages.json"), "w", encoding="utf-8") as f:
  377. json.dump(generate_mock_messages(), f, ensure_ascii=False, indent=2)
  378. with open(os.path.join(mock_dir, "branch_detail.json"), "w", encoding="utf-8") as f:
  379. json.dump(generate_mock_branch_detail(), f, ensure_ascii=False, indent=2)
  380. print(f"Mock 数据已保存到: {mock_dir}")
  381. if __name__ == "__main__":
  382. save_mock_data_to_file()