ConversationAgentToolCallRow.tsx 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { useCallback, useMemo } from "react";
  2. import { TID_V4_ROW, TID_V4_SUBAGENT_OPEN_SIDE_PANE, testId } from "@zcode/shared";
  3. import { ToolCallBlock } from "@/ToolCallBlocks.js";
  4. import { getAgentPrimaryText } from "@/ToolCallBlocks/renderers/agentHelpers.js";
  5. import type { ConversationAssistantWorkRenderItem } from "@/v4/conversationAssistantWorkItems.js";
  6. import type { ConversationRowRenderContext } from "@/v4/conversationRowContext.js";
  7. import { toolCallRowToLegacyNode } from "@/v4/toolCallRowAdapter.js";
  8. import { runUserAction } from "@/lib/userActionTelemetry.js";
  9. export function openSubagentSessionFromSummary({
  10. backgrounded: _backgrounded = false,
  11. childSessionId,
  12. context,
  13. subagentType,
  14. title,
  15. }: {
  16. backgrounded?: boolean;
  17. childSessionId?: string;
  18. context: ConversationRowRenderContext;
  19. subagentType: string;
  20. title: string;
  21. }): boolean {
  22. const parentSessionId = context.sessionId;
  23. const onOpenSubagentSession = context.onOpenSubagentSession;
  24. if (!childSessionId || !parentSessionId || !onOpenSubagentSession) {
  25. return false;
  26. }
  27. onOpenSubagentSession({
  28. rootSessionId: context.rootSessionId ?? parentSessionId,
  29. parentSessionId,
  30. childSessionId,
  31. subagentType,
  32. title,
  33. });
  34. return true;
  35. }
  36. export function ConversationAgentToolCallRow({
  37. item,
  38. context,
  39. }: {
  40. item: Extract<ConversationAssistantWorkRenderItem, { kind: "agentToolCall" }>;
  41. context: ConversationRowRenderContext;
  42. }) {
  43. const toolCallNode = useMemo(() => toolCallRowToLegacyNode(item.row), [item.row]);
  44. const childSessionId = item.subagentRow.childSessionId;
  45. const subagentType = item.subagentRow.subagentType;
  46. // 与 Agent 摘要行复用同一 title resolver,保证右侧 tab 和用户点击的可见标题逐字一致。
  47. const title = getAgentPrimaryText(toolCallNode.toolCall, "");
  48. const canOpenChildSession = Boolean(
  49. childSessionId && context.sessionId && context.onOpenSubagentSession,
  50. );
  51. const handleOpenChildSession = useCallback(() => {
  52. runUserAction({
  53. input: { featureId: "conversation.subagent", action: "open_side_pane", trigger: "button" },
  54. operation: () =>
  55. openSubagentSessionFromSummary({ childSessionId, context, subagentType, title }),
  56. completed: { resultSource: "local_commit" },
  57. failureStage: "subagent_open",
  58. });
  59. }, [childSessionId, context, subagentType, title]);
  60. const agentSummaryAction = useMemo(
  61. () =>
  62. canOpenChildSession && childSessionId
  63. ? {
  64. onActivate: handleOpenChildSession,
  65. testId: testId(TID_V4_SUBAGENT_OPEN_SIDE_PANE, childSessionId),
  66. }
  67. : undefined,
  68. [canOpenChildSession, childSessionId, handleOpenChildSession],
  69. );
  70. // Agent 工具行和普通工具行同属工作流,不能在配对壳上额外加 px-4,
  71. // 否则会和未配对 toolCall 行产生左右缩进差异。
  72. return (
  73. <div
  74. data-row-id={item.row.rowId}
  75. data-conversation-selectable="true"
  76. data-testid={testId(TID_V4_ROW, String(item.row.rowId))}
  77. >
  78. <ToolCallBlock
  79. toolCallNode={toolCallNode}
  80. workspacePath={context.workspacePath}
  81. theme={context.theme}
  82. codePreviewSettings={context.codePreviewSettings}
  83. showTodoToolCalls={context.messageStreamShowTodos === true}
  84. onOpenCodeViewer={context.onOpenCodeViewer}
  85. onOpenFileLink={context.onOpenFileLink}
  86. onOpenBrowserUrl={context.onOpenBrowserUrl}
  87. onOpenAutomationsMain={context.onOpenAutomationsMain}
  88. agentSummaryAction={agentSummaryAction}
  89. authoritativeAgentType={subagentType}
  90. />
  91. </div>
  92. );
  93. }