logLabels.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * 执行日志类型 → 中文标签 / CSS 类名 工具。
  3. *
  4. * <p>三处显示执行日志(ChatMessageList / WorkflowEditor / RunHistory)共用,
  5. * 统一颜色与中文文案。同时兼容两套命名:
  6. * <ul>
  7. * <li>chat 端 SSE 事件 {@code kind}:小写下划线(thinking / status / step / tool_progress /
  8. * tool_call / tool_result / todo_update / clarify_request)</li>
  9. * <li>工作流端 ExecutionLog {@code type}:大写下划线(INFO / TOOL_CALL / TOOL_RESULT /
  10. * THINKING / ERROR / STATUS / STEP / TOOL_PROGRESS / TODO_UPDATE / CLARIFY_REQUEST)</li>
  11. * </ul>
  12. *
  13. * <p>映射规则:忽略大小写 + 下划线/连字符归一化后匹配;查不到回退到 'info'。
  14. */
  15. /** 全量类型 → 中文标签(小写形式为 key) */
  16. const LOG_LABELS = {
  17. info: '信息',
  18. thinking: '思考',
  19. status: '状态',
  20. step: '步骤',
  21. tool_progress: '工具进度',
  22. tool_call: '调用',
  23. tool_result: '返回',
  24. todo_update: '待办',
  25. clarify_request: '提问',
  26. error: '错误'
  27. }
  28. /**
  29. * 把任意形式的类型({@code 'TOOL_CALL'} / {@code 'tool_call'} / {@code 'tool-call'})
  30. * 归一化为小写下划线形式。
  31. */
  32. function normalizeType(type) {
  33. if (!type) return 'info'
  34. return String(type).trim().toLowerCase().replace(/-/g, '_')
  35. }
  36. /**
  37. * 取类型的中文标签。
  38. *
  39. * @param {string} type 任意形式的类型字符串
  40. * @returns {string} 中文标签;未知类型回退到 type 原文或 '日志'
  41. */
  42. export function logTypeLabel(type) {
  43. const key = normalizeType(type)
  44. return LOG_LABELS[key] || type || '日志'
  45. }
  46. /**
  47. * 取类型的 CSS 类名后缀(不含前缀)。
  48. * 例:{@code 'TOOL_CALL'} → {@code 'tool_call'},配合模板里 {@code 'log-type-' + suffix} 使用。
  49. */
  50. export function logTypeSuffix(type) {
  51. return normalizeType(type)
  52. }
  53. /**
  54. * 是否为工具类日志(TOOL_CALL / TOOL_RESULT / TOOL_PROGRESS)。
  55. *
  56. * <p>用于执行日志渲染分支:工作流端 {@code ExecutionLog} 把工具名放在 {@code message}、
  57. * 参数摘要放在 {@code detail};与 chat 端 {@code ChatMessageLog} 的 {@code toolName + content}
  58. * 字段语义不同。渲染时对工具类日志把 {@code message} 高亮为 tool 列、{@code detail} 作为 msg 列,
  59. * 即可与 chat 端工具类日志的视觉完全一致。</p>
  60. */
  61. export function isToolLogType(type) {
  62. const t = normalizeType(type)
  63. return t === 'tool_call' || t === 'tool_result' || t === 'tool_progress'
  64. }