scratchpad.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. html += `<div class="structured-row">
  61. <div class="structured-label">action</div>
  62. <div class="structured-value">
  63. ${item.action.description ? `<span class="data-type-badge">${String(item.action.description).replace(/</g, '&lt;').replace(/>/g, '&gt;')}</span>` : ''}
  64. ${item.action.reasoning ? `<span style="margin-left:6px;">${String(item.action.reasoning).replace(/</g, '&lt;').replace(/>/g, '&gt;')}</span>` : ''}
  65. </div>
  66. </div>`;
  67. }
  68. // Render stage
  69. if (item.stage) {
  70. let stages = Array.isArray(item.stage) ? item.stage : [item.stage];
  71. if (stages.length > 0) {
  72. html += `<div class="structured-row">
  73. <div class="structured-label">stage</div>
  74. <div class="structured-value">`;
  75. stages.forEach(st => {
  76. const stageUpper = st.toUpperCase();
  77. const stageClass = stageUpper === 'GENERATE' ? 'stage-blue' : 'stage-purple';
  78. html += `<span class="structured-badge ${stageClass}">${stageUpper}</span>`;
  79. });
  80. html += `</div></div>`;
  81. }
  82. }
  83. // Render effects
  84. if (item.effects && Array.isArray(item.effects) && item.effects.length > 0) {
  85. html += `<div class="structured-row">
  86. <div class="structured-label">effects</div>
  87. <div class="structured-value">
  88. <ul class="effects-list">
  89. ${item.effects.map(li => `<li>${li.replace(/</g, '&lt;').replace(/>/g, '&gt;')}</li>`).join('')}
  90. </ul>
  91. </div>
  92. </div>`;
  93. }
  94. // Render body
  95. if (item.body && typeof item.body === 'string') {
  96. html += `<div class="structured-row">
  97. <div class="structured-label">body</div>
  98. <div class="structured-value">${item.body.replace(/</g, '&lt;').replace(/>/g, '&gt;')}</div>
  99. </div>`;
  100. }
  101. // Render steps array specially
  102. if (item.steps && Array.isArray(item.steps)) {
  103. html += `<div class="structured-row">
  104. <div class="structured-label">steps</div>
  105. <div class="structured-value">
  106. <div class="steps-container">`;
  107. item.steps.forEach((step, stepIdx) => {
  108. const stepTitle = step.method || step.description || `步骤 ${step.order || stepIdx + 1}`;
  109. html += `
  110. <div class="step-item">
  111. <div class="step-header">
  112. <span class="step-number">${step.order || stepIdx + 1}</span>
  113. <span class="step-title">${stepTitle.replace(/</g, '&lt;').replace(/>/g, '&gt;')}</span>
  114. </div>
  115. <div class="step-body">
  116. ${step.body ? step.body.replace(/</g, '&lt;').replace(/>/g, '&gt;') : ''}
  117. </div>
  118. ${step.tools && step.tools.length > 0 ? `<div class="step-tools">
  119. ${step.tools.map(t => `<span class="structured-badge tool-badge">${t}</span>`).join('')}
  120. </div>` : ''}
  121. </div>
  122. `;
  123. });
  124. html += `</div></div></div>`;
  125. }
  126. // Helper for inputs/outputs
  127. const renderDataObjList = (list) => {
  128. return list.map(io => {
  129. const dt = io.data_type || '未知';
  130. const desc = io.description || '';
  131. return `<div class="io-item"><span class="data-type-badge">${dt}</span><span class="io-desc">${desc}</span></div>`;
  132. }).join('');
  133. };
  134. // Render inputs
  135. if (item.inputs && Array.isArray(item.inputs) && item.inputs.length > 0) {
  136. html += `<div class="structured-row">
  137. <div class="structured-label">inputs</div>
  138. <div class="structured-value" style="display:flex; flex-direction:column; gap:4px;">
  139. ${renderDataObjList(item.inputs)}
  140. </div>
  141. </div>`;
  142. } else if (item.inputs && typeof item.inputs === 'object' && Object.keys(item.inputs).length > 0 && !Array.isArray(item.inputs)) {
  143. // Fallback for old schema
  144. html += `<div class="structured-row"><div class="structured-label">inputs</div><div class="structured-value">`;
  145. Object.entries(item.inputs).forEach(([k, v]) => {
  146. html += `<div class="io-item"><span class="data-type-badge">${k}</span><span class="io-desc">${v}</span></div>`;
  147. });
  148. html += `</div></div>`;
  149. }
  150. // Render outputs
  151. if (item.outputs && Array.isArray(item.outputs) && item.outputs.length > 0) {
  152. html += `<div class="structured-row">
  153. <div class="structured-label">outputs</div>
  154. <div class="structured-value" style="display:flex; flex-direction:column; gap:4px;">
  155. ${renderDataObjList(item.outputs)}
  156. </div>
  157. </div>`;
  158. } else if (item.outputs && typeof item.outputs === 'object' && Object.keys(item.outputs).length > 0 && !Array.isArray(item.outputs)) {
  159. // Fallback for old schema
  160. html += `<div class="structured-row"><div class="structured-label">outputs</div><div class="structured-value">`;
  161. Object.entries(item.outputs).forEach(([k, v]) => {
  162. html += `<div class="io-item"><span class="data-type-badge">${k}</span><span class="io-desc">${v}</span></div>`;
  163. });
  164. html += `</div></div>`;
  165. }
  166. // Render tools
  167. if (item.tools && Array.isArray(item.tools) && item.tools.length > 0) {
  168. html += `<div class="structured-row">
  169. <div class="structured-label">tools</div>
  170. <div class="structured-value">
  171. ${item.tools.map(t => `<span class="structured-badge tool-badge">${t}</span>`).join('')}
  172. </div>
  173. </div>`;
  174. }
  175. html += `</div>`;
  176. });
  177. return html;
  178. }