app-tool-components.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import React from "react";
  2. import type { ToolResultDisplayLine, ToolTranscriptPart } from "./app-model.js";
  3. import { ShikiDiffView, diffViewForWidth } from "./app-shiki-diff-view.js";
  4. import { palette } from "./app-model.js";
  5. import { truncateDisplay } from "./app-terminal-width.js";
  6. import { activeTuiTheme } from "./theme/index.js";
  7. const MAX_OUTPUT_LINES = 8;
  8. const OUTPUT_LINE_INDENT_WIDTH = 4;
  9. const TOOL_DETAIL_INDENT = " ";
  10. const TOOL_OUTPUT_LINE_INDENT = " ";
  11. const h = React.createElement as (
  12. type: React.ElementType | string,
  13. props?: Record<string, unknown> | null,
  14. ...children: React.ReactNode[]
  15. ) => React.ReactElement;
  16. export function ToolTranscriptPartView({
  17. part,
  18. terminalWidth = 100,
  19. }: {
  20. part: ToolTranscriptPart;
  21. terminalWidth?: number;
  22. }): React.ReactElement {
  23. const statusColor = colorForStatus(part.status);
  24. const title = part.title ?? `Tool ${part.toolName} ${part.status}`;
  25. const outputLines = part.output ? restoredOutputLines(part.output, terminalWidth) : [];
  26. // tool rows should align with assistant text; child detail rows carry their own indent.
  27. return h(
  28. "box",
  29. {
  30. style: {
  31. backgroundColor: "transparent",
  32. flexDirection: "column",
  33. marginTop: 1,
  34. width: "100%",
  35. },
  36. },
  37. h("text", { style: { fg: part.title ? palette.muted : statusColor } }, title),
  38. ...part.detailLines.map((line, index) =>
  39. h(
  40. "text",
  41. { key: `detail-${index}`, style: { fg: palette.muted } },
  42. `${TOOL_DETAIL_INDENT}${line}`,
  43. ),
  44. ),
  45. ...(part.resultDisplay
  46. ? [
  47. ...(part.resultDisplay.title
  48. ? [
  49. h(
  50. "text",
  51. { key: "result-title", style: { fg: palette.muted } },
  52. `${TOOL_DETAIL_INDENT}${part.resultDisplay.title}`,
  53. ),
  54. ]
  55. : []),
  56. ...resultDisplayNodes(part.resultDisplay, terminalWidth),
  57. ]
  58. : []),
  59. ...(outputLines.length > 0
  60. ? [
  61. h(
  62. "text",
  63. { key: "output-title", style: { fg: palette.muted } },
  64. `${TOOL_DETAIL_INDENT}output:`,
  65. ),
  66. ...outputLines.map((line, index) =>
  67. h(
  68. "text",
  69. { key: `output-${index}`, style: { fg: palette.muted } },
  70. `${TOOL_OUTPUT_LINE_INDENT}${line}`,
  71. ),
  72. ),
  73. ]
  74. : []),
  75. ...(part.error ? [h("text", { key: "error", style: { fg: palette.danger } }, part.error)] : []),
  76. );
  77. }
  78. function resultDisplayNodes(
  79. display: NonNullable<ToolTranscriptPart["resultDisplay"]>,
  80. terminalWidth: number,
  81. ): React.ReactElement[] {
  82. if (display.structuredPatch?.length) {
  83. return [
  84. h(ShikiDiffView, {
  85. filePath: display.filePath,
  86. key: "result-shiki-diff",
  87. structuredPatch: display.structuredPatch,
  88. terminalWidth,
  89. truncated: display.truncated,
  90. view: diffViewForWidth(terminalWidth),
  91. }),
  92. ];
  93. }
  94. return display.lines.map((line, index) =>
  95. h(
  96. "text",
  97. { key: `result-${index}`, style: styleForDiffLine(line) },
  98. `${TOOL_DETAIL_INDENT}${line.text}`,
  99. ),
  100. );
  101. }
  102. function colorForStatus(status: ToolTranscriptPart["status"]): string {
  103. if (status === "completed") return palette.success;
  104. if (status === "failed") return palette.danger;
  105. if (status === "running") return palette.accent;
  106. return palette.warning;
  107. }
  108. function styleForDiffLine(line: ToolResultDisplayLine): Record<string, string> {
  109. const theme = activeTuiTheme();
  110. if (line.tone === "addition") {
  111. return { bg: theme.diffAddedBg, fg: theme.diffAdded };
  112. }
  113. if (line.tone === "deletion") {
  114. return { bg: theme.diffRemovedBg, fg: theme.diffRemoved };
  115. }
  116. return { fg: line.tone === "meta" ? theme.diffLineNumber : theme.diffContext };
  117. }
  118. function restoredOutputLines(output: string, terminalWidth: number): string[] {
  119. const width = Math.max(20, terminalWidth - OUTPUT_LINE_INDENT_WIDTH);
  120. const lines = output.split(/\r?\n/u);
  121. const visible = lines.slice(0, MAX_OUTPUT_LINES).map((line) => truncateDisplay(line, width));
  122. if (lines.length > MAX_OUTPUT_LINES) {
  123. visible.push(`[truncated ${lines.length - MAX_OUTPUT_LINES} lines]`);
  124. }
  125. return visible.filter((line) => line.trim().length > 0);
  126. }