DeveloperToolsPane.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* oxlint-disable eslint(max-lines) -- 开发者工具面板集中展示 token 表和网络 headers,后续继续扩展时再按区块拆分。 */
  2. import { ActivityIcon, BugIcon, NetworkIcon } from "lucide-react";
  3. import type { SessionDebugNetworkEntry } from "@zcode/shared";
  4. import { useZCodeIntl } from "@/i18n/IntlProvider.js";
  5. import { useSessionDebug } from "@/hooks/useSessionDebug.js";
  6. interface DeveloperToolsPaneProps {
  7. workspacePath: string;
  8. workspaceIdentity?: string;
  9. taskId: string | null;
  10. enabled?: boolean;
  11. }
  12. function formatNumber(locale: string, value: number | undefined): string {
  13. if (value === undefined || !Number.isFinite(value)) {
  14. return "-";
  15. }
  16. return new Intl.NumberFormat(locale).format(value);
  17. }
  18. function formatPercent(locale: string, value: number | null | undefined): string {
  19. if (value === null || value === undefined || !Number.isFinite(value)) {
  20. return "-";
  21. }
  22. return new Intl.NumberFormat(locale, {
  23. maximumFractionDigits: 1,
  24. style: "percent",
  25. }).format(Math.max(0, value));
  26. }
  27. function formatMilliseconds(locale: string, value: number | undefined): string {
  28. if (value === undefined || !Number.isFinite(value)) {
  29. return "-";
  30. }
  31. return `${new Intl.NumberFormat(locale, { maximumFractionDigits: 0 }).format(value)} ms`;
  32. }
  33. function formatTimestamp(locale: string, value: string | undefined, fallback: number): string {
  34. const parsedTimestamp = value ? Date.parse(value) : Number.NaN;
  35. const timestamp = Number.isFinite(parsedTimestamp) ? parsedTimestamp : fallback;
  36. return new Intl.DateTimeFormat(locale, {
  37. hour: "2-digit",
  38. minute: "2-digit",
  39. second: "2-digit",
  40. }).format(new Date(timestamp));
  41. }
  42. function networkStatusLabelId(statusType: SessionDebugNetworkEntry["statusType"]): string {
  43. switch (statusType) {
  44. case "model_request_started":
  45. return "developerTools.network.status.started";
  46. case "model_request_completed":
  47. return "developerTools.network.status.completed";
  48. case "model_request_failed":
  49. return "developerTools.network.status.failed";
  50. case "model_retry_scheduled":
  51. return "developerTools.network.status.retry";
  52. case "model_stream_stalled":
  53. return "developerTools.network.status.stalled";
  54. }
  55. }
  56. function HeaderDetails({
  57. title,
  58. headers,
  59. emptyLabel,
  60. }: {
  61. title: string;
  62. headers: Record<string, string>;
  63. emptyLabel: string;
  64. }) {
  65. const entries = Object.entries(headers).sort(([left], [right]) => left.localeCompare(right));
  66. return (
  67. <details className="border-t border-border px-3 py-2">
  68. <summary className="cursor-default select-none text-ui-xs font-medium text-foreground-subtle">
  69. {title}
  70. </summary>
  71. {entries.length === 0 ? (
  72. <div className="pt-2 text-ui-xs text-foreground-subtle">{emptyLabel}</div>
  73. ) : (
  74. <dl className="grid grid-cols-[minmax(7rem,0.35fr)_minmax(0,1fr)] gap-x-3 gap-y-1 pt-2 text-ui-xs">
  75. {entries.map(([key, value]) => (
  76. <div key={key} className="contents">
  77. <dt className="min-w-0 break-all font-mono text-foreground-subtle">{key}</dt>
  78. <dd className="min-w-0 break-all font-mono text-foreground">{value}</dd>
  79. </div>
  80. ))}
  81. </dl>
  82. )}
  83. </details>
  84. );
  85. }
  86. export function DeveloperToolsPane({
  87. workspacePath,
  88. workspaceIdentity,
  89. taskId,
  90. enabled = true,
  91. }: DeveloperToolsPaneProps) {
  92. const { intl, locale } = useZCodeIntl();
  93. const debugState = useSessionDebug({ workspacePath, workspaceIdentity, taskId, enabled });
  94. const networkEntries = [...debugState.networkEntries].reverse();
  95. return (
  96. <div className="flex h-full min-h-0 flex-col bg-background" data-testid="developer-tools-pane">
  97. <div className="flex shrink-0 items-center gap-2 border-b border-border px-4 py-3">
  98. <BugIcon className="size-4 text-foreground-subtle" aria-hidden="true" />
  99. <h2 className="text-ui-base font-semibold text-foreground">
  100. {intl.formatMessage({ id: "developerTools.title" })}
  101. </h2>
  102. </div>
  103. <div className="min-h-0 flex-1 space-y-5 overflow-auto px-4 py-4">
  104. {debugState.error ? (
  105. <div role="alert" className="text-ui-base text-foreground-subtle">
  106. {intl.formatMessage({ id: "developerTools.loadError" })}
  107. </div>
  108. ) : null}
  109. <section className="space-y-3">
  110. <div className="flex items-center gap-2">
  111. <ActivityIcon className="size-4 text-foreground-subtle" aria-hidden="true" />
  112. <h3 className="text-ui-base font-semibold text-foreground">
  113. {intl.formatMessage({ id: "developerTools.tokenSection" })}
  114. </h3>
  115. </div>
  116. <div className="grid grid-cols-2 gap-x-4 gap-y-1 text-ui-base text-foreground-subtle">
  117. <span>{intl.formatMessage({ id: "tokenDebug.summary.requests" })}</span>
  118. <span className="font-mono text-foreground">
  119. {formatNumber(locale, debugState.cache?.hitRateRequestCount)}
  120. </span>
  121. <span>{intl.formatMessage({ id: "tokenDebug.summary.average" })}</span>
  122. <span className="font-mono text-foreground">
  123. {formatPercent(locale, debugState.cache?.hitRate)}
  124. </span>
  125. <span>{intl.formatMessage({ id: "tokenDebug.summary.input" })}</span>
  126. <span className="font-mono text-foreground">
  127. {formatNumber(locale, debugState.cache?.totalInputTokens)}
  128. </span>
  129. <span>{intl.formatMessage({ id: "tokenDebug.summary.cacheRead" })}</span>
  130. <span className="font-mono text-foreground">
  131. {formatNumber(locale, debugState.cache?.totalCacheReadTokens)}
  132. </span>
  133. </div>
  134. <div className="overflow-auto rounded-md border border-border">
  135. <table className="w-full min-w-[38rem] border-collapse text-ui-xs">
  136. <thead className="sticky top-0 bg-background text-foreground-subtle">
  137. <tr className="border-b border-border">
  138. <th className="px-2 py-1.5 text-left font-medium">
  139. {intl.formatMessage({ id: "tokenDebug.column.round" })}
  140. </th>
  141. <th className="px-2 py-1.5 text-right font-medium">
  142. {intl.formatMessage({ id: "tokenDebug.column.input" })}
  143. </th>
  144. <th className="px-2 py-1.5 text-right font-medium">
  145. {intl.formatMessage({ id: "tokenDebug.column.output" })}
  146. </th>
  147. <th
  148. className="px-2 py-1.5 text-right font-medium"
  149. title={intl.formatMessage({ id: "tokenDebug.tpsDescription" })}
  150. >
  151. {intl.formatMessage({ id: "tokenDebug.column.tps" })}
  152. </th>
  153. <th className="px-2 py-1.5 text-right font-medium">
  154. {intl.formatMessage({ id: "tokenDebug.column.total" })}
  155. </th>
  156. <th className="px-2 py-1.5 text-right font-medium">
  157. {intl.formatMessage({ id: "tokenDebug.column.reasoning" })}
  158. </th>
  159. <th className="px-2 py-1.5 text-right font-medium">
  160. {intl.formatMessage({ id: "tokenDebug.column.cacheRead" })}
  161. </th>
  162. <th className="px-2 py-1.5 text-right font-medium">
  163. {intl.formatMessage({ id: "tokenDebug.column.cacheWrite" })}
  164. </th>
  165. <th className="px-2 py-1.5 text-right font-medium">
  166. {intl.formatMessage({ id: "tokenDebug.column.hitRate" })}
  167. </th>
  168. </tr>
  169. </thead>
  170. <tbody>
  171. {debugState.rounds.length === 0 ? (
  172. <tr>
  173. <td className="px-2 py-3 text-center text-foreground-subtle" colSpan={9}>
  174. {intl.formatMessage({ id: "tokenDebug.empty" })}
  175. </td>
  176. </tr>
  177. ) : (
  178. debugState.rounds.map((round) => (
  179. <tr key={round.eventKey} className="border-b border-border last:border-b-0">
  180. <td className="px-2 py-1.5 font-mono text-foreground">
  181. {formatNumber(locale, round.requestIndex)}
  182. </td>
  183. <td className="px-2 py-1.5 text-right font-mono text-foreground">
  184. {formatNumber(locale, round.usage.inputTokens)}
  185. </td>
  186. <td className="px-2 py-1.5 text-right font-mono text-foreground">
  187. {formatNumber(locale, round.usage.outputTokens)}
  188. </td>
  189. <td
  190. className="px-2 py-1.5 text-right font-mono text-foreground"
  191. data-testid="token-debug-tps"
  192. >
  193. {round.tokensPerSecond === null
  194. ? "—"
  195. : new Intl.NumberFormat(locale, { maximumFractionDigits: 1 }).format(
  196. round.tokensPerSecond,
  197. )}
  198. </td>
  199. <td className="px-2 py-1.5 text-right font-mono text-foreground">
  200. {formatNumber(locale, round.usage.totalTokens)}
  201. </td>
  202. <td className="px-2 py-1.5 text-right font-mono text-foreground">
  203. {formatNumber(locale, round.usage.reasoningTokens)}
  204. </td>
  205. <td className="px-2 py-1.5 text-right font-mono text-foreground">
  206. {formatNumber(locale, round.usage.cachedInputTokens)}
  207. </td>
  208. <td className="px-2 py-1.5 text-right font-mono text-foreground">
  209. {formatNumber(locale, round.usage.cachedWriteInputTokens)}
  210. </td>
  211. <td className="px-2 py-1.5 text-right font-mono text-foreground">
  212. {formatPercent(locale, round.hitRate)}
  213. </td>
  214. </tr>
  215. ))
  216. )}
  217. </tbody>
  218. </table>
  219. </div>
  220. </section>
  221. <section className="space-y-3">
  222. <div className="flex items-center gap-2">
  223. <NetworkIcon className="size-4 text-foreground-subtle" aria-hidden="true" />
  224. <h3 className="text-ui-base font-semibold text-foreground">
  225. {intl.formatMessage({ id: "developerTools.networkSection" })}
  226. </h3>
  227. </div>
  228. {networkEntries.length === 0 ? (
  229. <div className="rounded-md border border-border px-3 py-4 text-center text-ui-base text-foreground-subtle">
  230. {intl.formatMessage({ id: "developerTools.network.empty" })}
  231. </div>
  232. ) : (
  233. <div className="space-y-2">
  234. {networkEntries.map((entry) => (
  235. <div
  236. key={entry.eventKey}
  237. className="overflow-hidden rounded-md border border-border"
  238. >
  239. <div className="space-y-2 px-3 py-2">
  240. <div className="flex min-w-0 items-center justify-between gap-3">
  241. <div className="min-w-0">
  242. <div className="truncate text-ui-base font-semibold text-foreground">
  243. {intl.formatMessage({ id: networkStatusLabelId(entry.statusType) })}
  244. </div>
  245. <div className="truncate font-mono text-ui-xs text-foreground-subtle">
  246. {entry.requestId ?? entry.eventKey}
  247. </div>
  248. </div>
  249. <div className="shrink-0 font-mono text-ui-xs text-foreground-subtle">
  250. {formatTimestamp(locale, entry.timestamp, entry.recordedAt)}
  251. </div>
  252. </div>
  253. <div className="grid grid-cols-2 gap-x-3 gap-y-1 text-ui-xs text-foreground-subtle">
  254. <span>{intl.formatMessage({ id: "developerTools.network.model" })}</span>
  255. <span className="min-w-0 truncate font-mono text-foreground">
  256. {entry.modelId ?? "-"}
  257. </span>
  258. <span>{intl.formatMessage({ id: "developerTools.network.provider" })}</span>
  259. <span className="min-w-0 truncate font-mono text-foreground">
  260. {entry.providerId ?? entry.providerKind ?? "-"}
  261. </span>
  262. <span>{intl.formatMessage({ id: "developerTools.network.attempt" })}</span>
  263. <span className="font-mono text-foreground">
  264. {entry.attempt !== undefined && entry.maxAttempts !== undefined
  265. ? `${entry.attempt}/${entry.maxAttempts === 0 ? "∞" : entry.maxAttempts}`
  266. : "-"}
  267. </span>
  268. <span>{intl.formatMessage({ id: "developerTools.network.http" })}</span>
  269. <span className="font-mono text-foreground">
  270. {formatNumber(locale, entry.statusCode)}
  271. </span>
  272. <span>{intl.formatMessage({ id: "developerTools.network.duration" })}</span>
  273. <span className="font-mono text-foreground">
  274. {formatMilliseconds(locale, entry.durationMs ?? entry.idleMs)}
  275. </span>
  276. <span>{intl.formatMessage({ id: "developerTools.network.retryDelay" })}</span>
  277. <span className="font-mono text-foreground">
  278. {formatMilliseconds(locale, entry.delayMs)}
  279. </span>
  280. </div>
  281. {entry.baseURL ? (
  282. <div className="min-w-0 truncate font-mono text-ui-xs text-foreground-subtle">
  283. {entry.baseURL}
  284. </div>
  285. ) : null}
  286. {entry.message ? (
  287. <div className="break-words rounded-md bg-surface px-2 py-1.5 text-ui-xs text-foreground">
  288. {entry.message}
  289. </div>
  290. ) : null}
  291. </div>
  292. <HeaderDetails
  293. title={intl.formatMessage(
  294. { id: "developerTools.network.requestHeaders" },
  295. { count: formatNumber(locale, entry.requestHeaderCount) },
  296. )}
  297. headers={entry.requestHeaders}
  298. emptyLabel={intl.formatMessage({ id: "developerTools.network.noHeaders" })}
  299. />
  300. <HeaderDetails
  301. title={intl.formatMessage(
  302. { id: "developerTools.network.responseHeaders" },
  303. { count: formatNumber(locale, entry.responseHeaderCount) },
  304. )}
  305. headers={entry.responseHeaders}
  306. emptyLabel={intl.formatMessage({ id: "developerTools.network.noHeaders" })}
  307. />
  308. </div>
  309. ))}
  310. </div>
  311. )}
  312. </section>
  313. </div>
  314. </div>
  315. );
  316. }