scratchpad.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. function renderStructuredData(items, type) {
  2. if (!items || items.length === 0) {
  3. return `<div style="color:var(--text-muted); padding: 1rem;">暂无${type === 'workflow' ? '工序' : '能力'}数据</div>`;
  4. }
  5. let html = '';
  6. items.forEach((item, idx) => {
  7. let title = item.method || item.name || (item.action && item.action.description) || (type === 'workflow' ? '工作流' : `节点 ${idx + 1}`);
  8. // Unstructured what tags
  9. let unstructTags = '';
  10. if (item.unstructured_what && Array.isArray(item.unstructured_what)) {
  11. unstructTags = item.unstructured_what.map(t => `<span class="unstruct-badge">${t}</span>`).join('');
  12. }
  13. html += `<div class="structured-card">
  14. <div class="structured-card-title-row" style="display:flex; align-items:center; gap: 8px; margin-bottom: 1rem;">
  15. <div class="structured-card-title" style="margin:0;">${title}</div>
  16. ${unstructTags}
  17. </div>
  18. `;
  19. // Render apply_to or apply_to_draft
  20. const applyTo = item.apply_to_draft || item.apply_to;
  21. const suggestApplyTo = item.apply_to_draft ? null : item.suggest_apply_to;
  22. if (applyTo && typeof applyTo === 'object' && Object.keys(applyTo).length > 0) {
  23. html += `<div class="structured-row">
  24. <div class="structured-label">apply_to</div>
  25. <div class="structured-value" style="display:flex; flex-direction:column; gap:6px;">`;
  26. const renderPathBadge = (path, highlight = false) => {
  27. const pathStr = typeof path === 'object' && path !== null
  28. ? (path.element || path.category_path || path.path || '')
  29. : String(path || '');
  30. if (!pathStr) return '';
  31. const parts = pathStr.split('/');
  32. const leaf = parts.pop();
  33. const prefix = parts.length > 0 ? parts.join('/') + '/' : '';
  34. const leafStyle = highlight ? 'background:#eff6ff; color:#2563eb; border:1px solid #bfdbfe;' : '';
  35. return `<span class="apply-to-path-item" ${highlight ? 'style="border: 2px dashed #94a3b8; background: transparent;"' : ''}>
  36. ${prefix ? `<span class="apply-to-path-prefix">${prefix}</span>` : ''}
  37. <span class="apply-to-path-leaf" style="${leafStyle}">${leaf}</span>
  38. </span>`;
  39. };
  40. Object.entries(applyTo).forEach(([k, v]) => {
  41. if (Array.isArray(v) && v.length > 0) {
  42. html += `<div class="apply-to-subrow">
  43. <span class="apply-to-key-badge">${k}</span>
  44. <div class="apply-to-values">`;
  45. v.forEach(path => {
  46. html += renderPathBadge(path);
  47. });
  48. html += `</div></div>`;
  49. }
  50. });
  51. if (typeof suggestApplyTo === 'string' && suggestApplyTo.trim()) {
  52. html += `<div class="apply-to-subrow">
  53. <span class="apply-to-key-badge">最优</span>
  54. <div class="apply-to-values">${renderPathBadge(suggestApplyTo, true)}</div>
  55. </div>`;
  56. }
  57. html += `</div></div>`;
  58. }
  59. if (item.action && typeof item.action === 'object' && (item.action.description || item.action.reasoning)) {
  60. const actionDescription = item.action.description ? String(item.action.description).replace(/</g, '&lt;').replace(/>/g, '&gt;') : '';
  61. const actionReasoning = item.action.reasoning ? String(item.action.reasoning).replace(/</g, '&lt;').replace(/>/g, '&gt;') : '';
  62. html += `<div class="structured-row">
  63. <div class="structured-label">action</div>
  64. <div class="structured-value">
  65. <style>
  66. .action-description-tooltip:hover .action-reasoning-popover { display:block !important; }
  67. </style>
  68. ${actionDescription ? `<span class="action-description-tooltip" style="position:relative; display:inline-block;">
  69. <span class="data-type-badge">${actionDescription}</span>
  70. ${actionReasoning ? `<span class="action-reasoning-popover" style="display:none; position:absolute; left:0; top:calc(100% + 6px); z-index:50; width:300px; max-width:60vw; padding:8px 10px; border-radius:8px; background:#0f172a; color:#f8fafc; box-shadow:0 10px 25px rgba(15,23,42,0.18); font-size:0.86em; line-height:1.5; white-space:normal; font-weight:400;">${actionReasoning}</span>` : ''}
  71. </span>` : ''}
  72. </div>
  73. </div>`;
  74. }
  75. // Render stage
  76. if (item.stage) {
  77. let stages = Array.isArray(item.stage) ? item.stage : [item.stage];
  78. if (stages.length > 0) {
  79. html += `<div class="structured-row">
  80. <div class="structured-label">stage</div>
  81. <div class="structured-value">`;
  82. stages.forEach(st => {
  83. const stageUpper = st.toUpperCase();
  84. const stageClass = stageUpper === 'GENERATE' ? 'stage-blue' : 'stage-purple';
  85. html += `<span class="structured-badge ${stageClass}">${stageUpper}</span>`;
  86. });
  87. html += `</div></div>`;
  88. }
  89. }
  90. // Render effects
  91. if (item.effects && Array.isArray(item.effects) && item.effects.length > 0) {
  92. html += `<div class="structured-row">
  93. <div class="structured-label">effects</div>
  94. <div class="structured-value">
  95. <ul class="effects-list">
  96. ${item.effects.map(li => `<li>${li.replace(/</g, '&lt;').replace(/>/g, '&gt;')}</li>`).join('')}
  97. </ul>
  98. </div>
  99. </div>`;
  100. }
  101. // Render body
  102. if (item.body && typeof item.body === 'string') {
  103. html += `<div class="structured-row">
  104. <div class="structured-label">body</div>
  105. <div class="structured-value">${item.body.replace(/</g, '&lt;').replace(/>/g, '&gt;')}</div>
  106. </div>`;
  107. }
  108. // Render steps array specially
  109. if (item.steps && Array.isArray(item.steps)) {
  110. html += `<div class="structured-row">
  111. <div class="structured-label">steps</div>
  112. <div class="structured-value">
  113. <div class="steps-container">`;
  114. item.steps.forEach((step, stepIdx) => {
  115. const stepTitle = step.method || step.description || `步骤 ${step.order || stepIdx + 1}`;
  116. html += `
  117. <div class="step-item">
  118. <div class="step-header">
  119. <span class="step-number">${step.order || stepIdx + 1}</span>
  120. <span class="step-title">${stepTitle.replace(/</g, '&lt;').replace(/>/g, '&gt;')}</span>
  121. </div>
  122. <div class="step-body">
  123. ${step.body ? step.body.replace(/</g, '&lt;').replace(/>/g, '&gt;') : ''}
  124. </div>
  125. ${step.tools && step.tools.length > 0 ? `<div class="step-tools">
  126. ${step.tools.map(t => `<span class="structured-badge tool-badge">${t}</span>`).join('')}
  127. </div>` : ''}
  128. </div>
  129. `;
  130. });
  131. html += `</div></div></div>`;
  132. }
  133. // Helper for inputs/outputs
  134. const renderDataObjList = (list) => {
  135. return list.map(io => {
  136. const dt = io.data_type || '未知';
  137. const desc = io.description || '';
  138. return `<div class="io-item"><span class="data-type-badge">${dt}</span><span class="io-desc">${desc}</span></div>`;
  139. }).join('');
  140. };
  141. // Render inputs
  142. if (item.inputs && Array.isArray(item.inputs) && item.inputs.length > 0) {
  143. html += `<div class="structured-row">
  144. <div class="structured-label">inputs</div>
  145. <div class="structured-value" style="display:flex; flex-direction:column; gap:4px;">
  146. ${renderDataObjList(item.inputs)}
  147. </div>
  148. </div>`;
  149. } else if (item.inputs && typeof item.inputs === 'object' && Object.keys(item.inputs).length > 0 && !Array.isArray(item.inputs)) {
  150. // Fallback for old schema
  151. html += `<div class="structured-row"><div class="structured-label">inputs</div><div class="structured-value">`;
  152. Object.entries(item.inputs).forEach(([k, v]) => {
  153. html += `<div class="io-item"><span class="data-type-badge">${k}</span><span class="io-desc">${v}</span></div>`;
  154. });
  155. html += `</div></div>`;
  156. }
  157. // Render outputs
  158. if (item.outputs && Array.isArray(item.outputs) && item.outputs.length > 0) {
  159. html += `<div class="structured-row">
  160. <div class="structured-label">outputs</div>
  161. <div class="structured-value" style="display:flex; flex-direction:column; gap:4px;">
  162. ${renderDataObjList(item.outputs)}
  163. </div>
  164. </div>`;
  165. } else if (item.outputs && typeof item.outputs === 'object' && Object.keys(item.outputs).length > 0 && !Array.isArray(item.outputs)) {
  166. // Fallback for old schema
  167. html += `<div class="structured-row"><div class="structured-label">outputs</div><div class="structured-value">`;
  168. Object.entries(item.outputs).forEach(([k, v]) => {
  169. html += `<div class="io-item"><span class="data-type-badge">${k}</span><span class="io-desc">${v}</span></div>`;
  170. });
  171. html += `</div></div>`;
  172. }
  173. // Render tools
  174. if (item.tools && Array.isArray(item.tools) && item.tools.length > 0) {
  175. html += `<div class="structured-row">
  176. <div class="structured-label">tools</div>
  177. <div class="structured-value">
  178. ${item.tools.map(t => `<span class="structured-badge tool-badge">${t}</span>`).join('')}
  179. </div>
  180. </div>`;
  181. }
  182. html += `</div>`;
  183. });
  184. return html;
  185. }