ConversationWorkflowDigests.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import type { TimelinePill } from "@/components/workflow-timeline/timeline-model.js";
  2. import { WorkflowRunDigest } from "@/components/workflow-timeline/WorkflowRunDigest.js";
  3. import type { WorkflowRunSettingsHost } from "@/components/workflow-timeline/WorkflowRunSettingsPopover.js";
  4. import { WorkflowSettingsChangeRow } from "@/components/workflow-timeline/WorkflowSettingsChangeRow.js";
  5. import { isWorkflowRunConfigurable } from "@/components/workflow-timeline/workflowRunSettings.js";
  6. import { useWorkflowSubagentModelProviderName } from "@/hooks/useWorkflowSubagentModelProviderName.js";
  7. import { useZCodeIntl } from "@/i18n/IntlProvider.js";
  8. import type { ConversationRowRenderContext } from "@/v4/conversationRowContext.js";
  9. import type { WorkflowTurnDigest } from "@/v4/workflowTurnDigests.js";
  10. import { resolveWorkflowRunOpenToolCallId } from "@/v4/workflowRunCardJoin.js";
  11. /**
  12. * 轮尾摘要的落位:把解析出的摘要接上
  13. * 宿主回调。回调的存在即门控(不变式 7):打开详情要 `onOpenWorkflowRun` + sessionId;Resume 要
  14. * `onResumeWorkflowRun` + 联接摘要说可恢复;药丸要 `onOpenWorkflowActor` + 活投影。与
  15. * `ToolCallRowView` 给工具卡接线的路径一字不差——同一个 run 从两处打开的是同一个 tab。
  16. */
  17. export function ConversationWorkflowDigests({
  18. context,
  19. digests,
  20. turnKey,
  21. }: {
  22. digests: readonly WorkflowTurnDigest[];
  23. context: ConversationRowRenderContext;
  24. turnKey: string;
  25. }) {
  26. const { intl } = useZCodeIntl();
  27. // 子代理模型名里的 provider 名从会话的模型清单来(卡本身不碰 store,宿主把查找函数递进去)。
  28. const subagentModelProviderName = useWorkflowSubagentModelProviderName(
  29. context.workspacePath,
  30. context.workspaceIdentity,
  31. );
  32. if (digests.length === 0) return null;
  33. const fallbackName = intl.formatMessage({ id: "chat.toolCall.workflow.fallbackName" });
  34. return (
  35. <div className="flex flex-col gap-3" data-testid={`workflow-run-digests-${turnKey}`}>
  36. {digests.map((digest) => {
  37. const { runId, summary } = digest;
  38. const name = digest.name ?? fallbackName;
  39. const sessionId = context.sessionId;
  40. // 就地生效的设置轮(docs/dynamic-workflow/presentation.md「The settings turn」):那一行就是
  41. // 全部呈现。提前返回,下面整套卡的接线(打开、Resume、Stop、药丸、「配置」)一条都不建——
  42. // 那些都是卡上的控件,而这一轮没有卡。
  43. if (digest.rowOnly && digest.settings !== undefined) {
  44. return (
  45. <WorkflowSettingsChangeRow
  46. amend={digest.settings.amend}
  47. key={digest.key}
  48. {...(digest.settings.at === undefined ? {} : { at: digest.settings.at })}
  49. {...(subagentModelProviderName === undefined
  50. ? {}
  51. : { providerName: subagentModelProviderName })}
  52. />
  53. );
  54. }
  55. // 「还有 n 个」那一行带落点(追记「五枚药丸与一扇门」);⤢ 与问题芯片不带。
  56. const onOpenRun =
  57. context.onOpenWorkflowRun && sessionId
  58. ? (landing?: { phaseId: string }) =>
  59. context.onOpenWorkflowRun?.({
  60. parentSessionId: sessionId,
  61. toolCallId: resolveWorkflowRunOpenToolCallId(digest.toolCallId, summary),
  62. runId,
  63. workflowName: name,
  64. ...(landing === undefined ? {} : { phaseId: landing.phaseId }),
  65. })
  66. : undefined;
  67. const onResume =
  68. context.onResumeWorkflowRun && summary?.resumable
  69. ? () => context.onResumeWorkflowRun?.(runId, name)
  70. : undefined;
  71. // 取消只有一条路径:详情页也走的 cancelBackgroundWork {workId ≡ runId}。
  72. const onCancel =
  73. context.onCancelBackgroundWork && summary?.status === "running"
  74. ? () => context.onCancelBackgroundWork?.(runId)
  75. : undefined;
  76. const onOpenPill =
  77. context.onOpenWorkflowActor && sessionId && summary?.run
  78. ? (pill: TimelinePill) => {
  79. // 槽位身份:会话 id 有则随行,没有就开占位 tab。
  80. const slot = pill.slot;
  81. if (slot === undefined) return;
  82. const actorSessionId = pill.instance?.sessionId;
  83. const actorName = pill.runtimeName ?? pill.lane.name;
  84. context.onOpenWorkflowActor?.({
  85. parentSessionId: sessionId,
  86. ordinal: slot.ordinal,
  87. runId,
  88. siteId: slot.siteId,
  89. ...(actorSessionId === undefined ? {} : { actorSessionId }),
  90. ...(actorName === undefined ? {} : { actorName }),
  91. });
  92. }
  93. : undefined;
  94. // 脚本药丸:与工具卡同一条路,开同一个 tab。
  95. const onOpenWorkspace =
  96. context.onOpenWorkflowWorkspace && sessionId && summary?.run
  97. ? (pill: TimelinePill) => {
  98. const phaseId = pill.workspace?.phaseId;
  99. if (phaseId === undefined) return;
  100. context.onOpenWorkflowWorkspace?.({
  101. parentSessionId: sessionId,
  102. toolCallId: resolveWorkflowRunOpenToolCallId(digest.toolCallId, summary),
  103. runId,
  104. workflowName: name,
  105. phaseId,
  106. });
  107. }
  108. : undefined;
  109. const onOpenArtifact =
  110. context.onOpenWorkflowArtifact && sessionId
  111. ? (artifactId: string) => {
  112. // 活投影的产物摘要带最新版的 `contentType`,宿主据它把 html 产物直接开成浏览器
  113. // tab;`sourcePath` 那份摘要刻意不带(高频状态键),缺席时宿主自己查 journal。
  114. const artifact = summary?.run?.artifacts?.find(
  115. (candidate) => candidate.id === artifactId,
  116. );
  117. context.onOpenWorkflowArtifact?.({
  118. parentSessionId: sessionId,
  119. runId,
  120. artifactId,
  121. ...(artifact?.title === undefined ? {} : { title: artifact.title }),
  122. ...(artifact?.contentType === undefined
  123. ? {}
  124. : { contentType: artifact.contentType }),
  125. });
  126. }
  127. : undefined;
  128. const pendingQuestions = context.workflowRunPendingQuestionsByRunId?.get(runId)?.size ?? 0;
  129. // 「配置」:宿主回调在场(只读 /
  130. // 灰度两道门已在宿主裁过)且这条 run 能配置时才有。弹层的模型清单按本会话的作用域读。
  131. const amendSettings = context.onAmendWorkflowRunSettings;
  132. const settingsHost: WorkflowRunSettingsHost | undefined =
  133. amendSettings !== undefined && isWorkflowRunConfigurable(summary?.run)
  134. ? {
  135. workspacePath: context.workspacePath,
  136. ...(context.workspaceIdentity
  137. ? { workspaceIdentity: context.workspaceIdentity }
  138. : {}),
  139. ...(context.workspaceRemoteSessionId
  140. ? { remoteSessionId: context.workspaceRemoteSessionId }
  141. : {}),
  142. ...(context.workflowSessionModel === undefined
  143. ? {}
  144. : { sessionModel: context.workflowSessionModel }),
  145. apply: (change) => amendSettings(runId, change),
  146. }
  147. : undefined;
  148. const card = (
  149. <WorkflowRunDigest
  150. graph={digest.graph}
  151. {...(digest.holeLabels === undefined ? {} : { holeLabels: digest.holeLabels })}
  152. key={digest.key}
  153. name={name}
  154. pendingQuestions={pendingQuestions}
  155. runId={runId}
  156. summary={summary}
  157. testIdKey={`${turnKey}-${digest.toolCallId}`}
  158. {...(subagentModelProviderName === undefined ? {} : { subagentModelProviderName })}
  159. {...(onOpenRun === undefined ? {} : { onOpenRun })}
  160. {...(onResume === undefined ? {} : { onResume })}
  161. {...(onCancel === undefined ? {} : { onCancel })}
  162. {...(onOpenPill === undefined ? {} : { onOpenPill })}
  163. {...(onOpenWorkspace === undefined ? {} : { onOpenWorkspace })}
  164. {...(onOpenArtifact === undefined ? {} : { onOpenArtifact })}
  165. {...(settingsHost === undefined ? {} : { settingsHost })}
  166. />
  167. );
  168. // 设置轮:卡上方一行说改了什么。
  169. if (digest.settings === undefined) return card;
  170. return (
  171. <div className="flex flex-col gap-1.5" key={digest.key}>
  172. <WorkflowSettingsChangeRow
  173. amend={digest.settings.amend}
  174. {...(digest.settings.at === undefined ? {} : { at: digest.settings.at })}
  175. {...(subagentModelProviderName === undefined
  176. ? {}
  177. : { providerName: subagentModelProviderName })}
  178. />
  179. {card}
  180. </div>
  181. );
  182. })}
  183. </div>
  184. );
  185. }