ModelTrajectoryPaneDetails.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import type { ReactNode } from "react";
  2. import type {
  3. ZCodeModelTrajectoryContentPart,
  4. ZCodeModelTrajectoryRecord,
  5. ZCodeModelTrajectoryUsage,
  6. } from "@zcode/services";
  7. import { Badge } from "@/components/ui/badge.js";
  8. import { cn } from "@/components/lib/utils.js";
  9. import type { useZCodeIntl } from "@/i18n/IntlProvider.js";
  10. type IntlShape = ReturnType<typeof useZCodeIntl>["intl"];
  11. export function ContentPartView({
  12. part,
  13. intl,
  14. sectionLabel,
  15. showToolHeader = true,
  16. }: {
  17. part: ZCodeModelTrajectoryContentPart;
  18. intl: IntlShape;
  19. sectionLabel?: string;
  20. showToolHeader?: boolean;
  21. }) {
  22. if (part.kind === "text") {
  23. if (!part.text) {
  24. return null;
  25. }
  26. const content = (
  27. <p className="whitespace-pre-wrap break-words text-ui-sm leading-relaxed text-foreground">
  28. {part.text}
  29. </p>
  30. );
  31. return sectionLabel ? <DetailSection label={sectionLabel}>{content}</DetailSection> : content;
  32. }
  33. if (part.kind === "reasoning") {
  34. if (!part.text) {
  35. return null;
  36. }
  37. return (
  38. <DetailSection label={intl.formatMessage({ id: "modelTrajectory.reasoning" })}>
  39. <p className="whitespace-pre-wrap break-words font-mono text-ui-sm leading-relaxed text-foreground-subtle">
  40. {part.text}
  41. </p>
  42. </DetailSection>
  43. );
  44. }
  45. if (part.kind === "tool-call") {
  46. return (
  47. <ToolPayload
  48. id={part.toolCallId}
  49. label={`${intl.formatMessage({ id: "modelTrajectory.toolCall" })} · ${part.toolName}`}
  50. payloadLabel={intl.formatMessage({ id: "modelTrajectory.toolInput" })}
  51. value={part.input}
  52. intl={intl}
  53. showHeader={showToolHeader}
  54. />
  55. );
  56. }
  57. if (part.kind === "tool-result") {
  58. return (
  59. <ToolPayload
  60. id={part.toolCallId}
  61. label={
  62. part.toolName
  63. ? `${intl.formatMessage({ id: "modelTrajectory.toolResult" })} · ${part.toolName}`
  64. : intl.formatMessage({ id: "modelTrajectory.toolResult" })
  65. }
  66. payloadLabel={intl.formatMessage({ id: "modelTrajectory.toolOutput" })}
  67. value={part.output}
  68. intl={intl}
  69. showHeader={showToolHeader}
  70. />
  71. );
  72. }
  73. if (part.kind === "image") {
  74. return (
  75. <span className="text-ui-sm italic text-foreground-subtle">
  76. [image{part.mediaType ? ` · ${part.mediaType}` : ""}]
  77. </span>
  78. );
  79. }
  80. return <CodeBlock value={formatValue(part.raw)} />;
  81. }
  82. function ToolPayload({
  83. label,
  84. id,
  85. payloadLabel,
  86. value,
  87. intl,
  88. showHeader,
  89. }: {
  90. label: string;
  91. id?: string;
  92. payloadLabel: string;
  93. value: unknown;
  94. intl: IntlShape;
  95. showHeader: boolean;
  96. }) {
  97. const payload = (
  98. <div className="flex flex-col gap-1.5 rounded-md border border-border bg-card px-2.5 py-2">
  99. <div className="flex min-w-0 flex-wrap items-center gap-1.5">
  100. <span className="text-ui-sm font-medium text-foreground-subtle">{payloadLabel}</span>
  101. </div>
  102. <CodeBlock value={formatValue(value)} />
  103. </div>
  104. );
  105. return showHeader ? (
  106. <DetailSection label={label} meta={id ? <ToolIdBadge id={id} intl={intl} /> : null}>
  107. {payload}
  108. </DetailSection>
  109. ) : (
  110. payload
  111. );
  112. }
  113. function ToolIdBadge({ id, intl }: { id: string; intl: IntlShape }) {
  114. return (
  115. <Badge
  116. variant="outline"
  117. className="max-w-full truncate font-mono text-foreground-subtle"
  118. title={id}
  119. >
  120. {intl.formatMessage({ id: "modelTrajectory.toolId" })}: {id}
  121. </Badge>
  122. );
  123. }
  124. function DetailSection({
  125. label,
  126. meta,
  127. children,
  128. }: {
  129. label: string;
  130. meta?: ReactNode;
  131. children: ReactNode;
  132. }) {
  133. return (
  134. <section className="flex min-w-0 flex-col gap-1">
  135. <div className="flex min-w-0 items-center gap-1 px-1.5 py-1 text-ui-sm font-medium text-foreground-subtle">
  136. <span className="min-w-0 flex-1 break-words font-mono">{label}</span>
  137. {meta ? <span className="min-w-0 shrink">{meta}</span> : null}
  138. </div>
  139. {children}
  140. </section>
  141. );
  142. }
  143. function CodeBlock({ value }: { value: string }) {
  144. return (
  145. <pre className="max-h-64 min-w-0 overflow-auto whitespace-pre rounded-md bg-surface px-2 py-1.5 font-mono text-ui-sm leading-relaxed text-foreground">
  146. {value || "—"}
  147. </pre>
  148. );
  149. }
  150. export function UsageBadges({
  151. usage,
  152. intl,
  153. }: {
  154. usage?: ZCodeModelTrajectoryUsage;
  155. intl: IntlShape;
  156. }) {
  157. if (!usage) {
  158. return null;
  159. }
  160. const parts: string[] = [];
  161. if (typeof usage.inputTokens === "number") {
  162. parts.push(
  163. intl.formatMessage(
  164. { id: "modelTrajectory.usage.input" },
  165. { count: usage.inputTokens.toLocaleString() },
  166. ),
  167. );
  168. }
  169. if (typeof usage.outputTokens === "number") {
  170. parts.push(
  171. intl.formatMessage(
  172. { id: "modelTrajectory.usage.output" },
  173. { count: usage.outputTokens.toLocaleString() },
  174. ),
  175. );
  176. }
  177. if (parts.length === 0) {
  178. return null;
  179. }
  180. return (
  181. <Badge variant="outline" className="font-mono text-foreground-subtle">
  182. {parts.join(" ")}
  183. </Badge>
  184. );
  185. }
  186. export function EmptyState({
  187. text,
  188. detail,
  189. tone,
  190. }: {
  191. text: string;
  192. detail?: string;
  193. tone?: "error";
  194. }) {
  195. return (
  196. <div className="flex flex-col items-center gap-1 px-4 py-10 text-center">
  197. <p
  198. className={cn(
  199. "text-ui-base",
  200. tone === "error" ? "text-destructive" : "text-foreground-subtle",
  201. )}
  202. >
  203. {text}
  204. </p>
  205. {detail ? (
  206. <p className="max-w-full break-words font-mono text-ui-base text-foreground-subtlest">
  207. {detail}
  208. </p>
  209. ) : null}
  210. </div>
  211. );
  212. }
  213. export function summarizeRecords(records: ZCodeModelTrajectoryRecord[]): {
  214. totalTokens: number;
  215. models: string[];
  216. } {
  217. let totalTokens = 0;
  218. const models = new Set<string>();
  219. for (const record of records) {
  220. const total = record.response?.usage?.totalTokens;
  221. if (typeof total === "number") {
  222. totalTokens += total;
  223. }
  224. const model = record.model.modelId ?? record.response?.modelId;
  225. if (model) {
  226. models.add(model);
  227. }
  228. }
  229. return { totalTokens, models: [...models] };
  230. }
  231. function formatValue(value: unknown): string {
  232. if (value === undefined || value === null) {
  233. return "";
  234. }
  235. if (typeof value === "string") {
  236. return value;
  237. }
  238. try {
  239. return JSON.stringify(value, null, 2);
  240. } catch {
  241. return String(value);
  242. }
  243. }