app-sidebar-api.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import type { ModelUsageSummary } from "@zcode/contracts";
  2. import type { TuiCopy } from "@zcode/i18n";
  3. import React from "react";
  4. import type { NetworkRequest } from "./app-model.js";
  5. import { palette } from "./app-model.js";
  6. import { SIDEBAR_CONTENT_WIDTH } from "./app-sidebar-layout.js";
  7. import { SidebarSectionHeader } from "./app-sidebar-section-header.js";
  8. import { displayWidth, truncateDisplay } from "./app-terminal-width.js";
  9. import { formatDuration } from "./state.js";
  10. const h = React.createElement as (
  11. type: React.ElementType | string,
  12. props?: Record<string, unknown> | null,
  13. ...children: React.ReactNode[]
  14. ) => React.ReactElement;
  15. const SIDEBAR_LABEL_WIDTH = 9;
  16. const SIDEBAR_ROW_VALUE_WIDTH = SIDEBAR_CONTENT_WIDTH - SIDEBAR_LABEL_WIDTH - 1;
  17. const COMPACT_IDENTIFIER_PREFIX_LENGTH = 8;
  18. const COMPACT_IDENTIFIER_SUFFIX_LENGTH = 4;
  19. const UUID_IDENTIFIER_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
  20. const SIDEBAR_TEXT_ROW_STYLE = { flexShrink: 0, height: 1, truncate: true, wrapMode: "none" };
  21. export function ApiSection(props: {
  22. copy: TuiCopy;
  23. expanded: boolean;
  24. networkRequests: NetworkRequest[];
  25. onToggle?: () => void;
  26. usage?: ModelUsageSummary;
  27. }): React.ReactElement {
  28. return h(
  29. "box",
  30. {
  31. style: {
  32. flexDirection: "column",
  33. flexShrink: 0,
  34. marginTop: 1,
  35. },
  36. },
  37. SidebarSectionHeader({
  38. expanded: props.expanded,
  39. onToggle: props.onToggle,
  40. title: props.copy.sidebar.sections.apis,
  41. }),
  42. ...(props.expanded ? apiLines(props) : []),
  43. );
  44. }
  45. function apiLines(props: {
  46. copy: TuiCopy;
  47. networkRequests: NetworkRequest[];
  48. usage?: ModelUsageSummary;
  49. }): React.ReactNode[] {
  50. const usage = props.usage;
  51. const pending = props.networkRequests.filter((request) => request.status === "pending").length;
  52. const failed = props.networkRequests.filter((request) => request.status === "error").length;
  53. const lines = [
  54. rowLine(
  55. props.copy.sidebar.api.model,
  56. usage ? String(usage.modelRequestCount) : "-",
  57. "api-model",
  58. ),
  59. rowLine(
  60. props.copy.sidebar.api.server,
  61. usage ? `fetch ${usage.webFetchRequests} / search ${usage.webSearchRequests}` : "-",
  62. "api-server",
  63. ),
  64. rowLine(
  65. props.copy.sidebar.api.requests,
  66. props.networkRequests.length > 0
  67. ? `${pending} pending / ${failed} error / ${props.networkRequests.length} recent`
  68. : "-",
  69. "api-requests",
  70. ),
  71. ];
  72. if (props.networkRequests.length === 0) {
  73. lines.push(textLine(props.copy.sidebar.api.empty, palette.muted, "api-empty"));
  74. return lines;
  75. }
  76. for (const request of props.networkRequests.slice(0, 5)) {
  77. lines.push(
  78. textLine(formatRequestLine(request, props.copy), requestColor(request), `api-${request.id}`),
  79. );
  80. }
  81. if (props.networkRequests.length > 5) {
  82. lines.push(
  83. textLine(
  84. props.copy.sidebar.api.more(props.networkRequests.length - 5),
  85. palette.muted,
  86. "api-more",
  87. ),
  88. );
  89. }
  90. return lines;
  91. }
  92. function rowLine(label: string, value: string, key: string): React.ReactElement {
  93. return textLine(
  94. `${padEndDisplay(label, SIDEBAR_LABEL_WIDTH)} ${truncateDisplay(value, SIDEBAR_ROW_VALUE_WIDTH)}`,
  95. palette.text,
  96. key,
  97. );
  98. }
  99. function textLine(value: string, color: string, key: string): React.ReactElement {
  100. return h("text", { key, style: { ...SIDEBAR_TEXT_ROW_STYLE, fg: color } }, value);
  101. }
  102. function padEndDisplay(value: string, targetCells: number): string {
  103. const visible = truncateDisplay(value, targetCells);
  104. return `${visible}${" ".repeat(Math.max(0, targetCells - displayWidth(visible)))}`;
  105. }
  106. function formatRequestLine(request: NetworkRequest, copy: TuiCopy): string {
  107. const status = requestStatusLabel(request, copy);
  108. const method = request.method.toUpperCase();
  109. const target = requestTargetLabel(request);
  110. const duration = request.durationMs === undefined ? "" : ` ${formatDuration(request.durationMs)}`;
  111. const prefix = `${status} ${method} `;
  112. if (!duration) return truncateDisplay(`${prefix}${target}`, SIDEBAR_CONTENT_WIDTH);
  113. const urlWidth = SIDEBAR_CONTENT_WIDTH - prefix.length - duration.length;
  114. if (urlWidth <= 0) {
  115. return truncateDisplay(`${prefix}${target}${duration}`, SIDEBAR_CONTENT_WIDTH);
  116. }
  117. return `${prefix}${truncateDisplay(target, urlWidth)}${duration}`;
  118. }
  119. function requestTargetLabel(request: NetworkRequest): string {
  120. if (!request.provider) return request.url;
  121. const providerLabel = requestProviderLabel(request.provider);
  122. if (request.url.startsWith(`${request.provider} `)) {
  123. return `${providerLabel}${request.url.slice(request.provider.length)}`;
  124. }
  125. if (request.url === request.provider) return providerLabel;
  126. if (request.url === `${request.provider}/${request.model}`) {
  127. return `${providerLabel}/${request.model}`;
  128. }
  129. return `${providerLabel} ${request.url}`;
  130. }
  131. function requestProviderLabel(provider: string): string {
  132. if (!UUID_IDENTIFIER_PATTERN.test(provider)) return provider;
  133. return `${provider.slice(0, COMPACT_IDENTIFIER_PREFIX_LENGTH)}...${provider.slice(
  134. -COMPACT_IDENTIFIER_SUFFIX_LENGTH,
  135. )}`;
  136. }
  137. function requestColor(request: NetworkRequest): string {
  138. if (request.status === "error") return palette.danger;
  139. if (request.status === "pending") return palette.accent;
  140. return palette.muted;
  141. }
  142. function requestStatusLabel(request: NetworkRequest, copy: TuiCopy): string {
  143. if (request.status === "pending") return copy.sidebar.request.pending;
  144. if (request.status === "error") {
  145. return request.statusCode === undefined
  146. ? copy.sidebar.request.error
  147. : copy.sidebar.request.errorWithStatus(request.statusCode);
  148. }
  149. return copy.sidebar.request.complete;
  150. }