app-view.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import type { ModelUsageSummary, TodoItem, TurnId } from "@zcode/contracts";
  2. import React from "react";
  3. import { ApprovalArea } from "./app-approval-panel.js";
  4. import {
  5. actionPanelContentWidthForTerminal,
  6. AppShell,
  7. LoginRequiredPanel,
  8. SlashSuggestionPanel,
  9. } from "./app-components.js";
  10. import { SelectionPanel } from "./app-selection-panel.js";
  11. import { ContentPane } from "./app-transcript-components.js";
  12. import { FileMentionPanel } from "./app-file-mention-panel.js";
  13. import type { FileMentionState } from "./app-file-mentions.js";
  14. import { EffortSuggestionPanel } from "./app-effort-suggestion-panel.js";
  15. import { InputPane, type PromptInputEditor } from "./app-input-pane.js";
  16. import { InputActiveStatus } from "./app-input-status.js";
  17. import { QueuedInputPanel } from "./app-queued-inputs.js";
  18. import { ModeSuggestionPanel } from "./app-mode-suggestion-panel.js";
  19. import { ModelSuggestionPanel } from "./app-model-suggestion-panel.js";
  20. import { useMcpSidebarStatus } from "./app-mcp-status.js";
  21. import { Sidebar } from "./app-sidebar.js";
  22. import {
  23. type SidebarLayout,
  24. type SidebarSectionId,
  25. type SidebarSectionExpansion,
  26. } from "./app-sidebar-layout.js";
  27. import type {
  28. ApprovalPrompt,
  29. CacheStats,
  30. ContextUsage,
  31. EffortCommandSelectionState,
  32. Message,
  33. ModifiedFileStat,
  34. ModeCommandSelectionState,
  35. ModelCommandSelectionState,
  36. NetworkRequest,
  37. QueuedInput,
  38. SelectionState,
  39. SlashSelectionState,
  40. } from "./app-model.js";
  41. import type { TuiEffortOption, TuiModeOption, TuiOptions } from "./types.js";
  42. import type { TuiWorkflowCard } from "./app-workflow-mirror.js";
  43. import type { SubagentItem, SubagentsController } from "./app-subagents.js";
  44. import { SubagentView } from "./app-subagent-view.js";
  45. const h = React.createElement as (
  46. type: React.ElementType | string,
  47. props?: Record<string, unknown> | null,
  48. ...children: React.ReactNode[]
  49. ) => React.ReactElement;
  50. export function AppView(props: {
  51. subagents?: SubagentsController;
  52. toggleSidebar?: () => boolean;
  53. activeTurnId?: TurnId;
  54. approvalQueue: ApprovalPrompt[];
  55. busy: boolean;
  56. cacheStats?: CacheStats;
  57. contextUsage: ContextUsage;
  58. copy: ReturnType<typeof import("@zcode/i18n").getZCodeCopy>["tui"];
  59. copyCurrentSelection: () => boolean;
  60. draft: string;
  61. editorRef: React.MutableRefObject<PromptInputEditor | null>;
  62. effortOptions: readonly TuiEffortOption[];
  63. effortSelection?: EffortCommandSelectionState;
  64. fileMention?: FileMentionState;
  65. inputCursorToEndVersion: number;
  66. lastError?: string;
  67. lastEvent: string;
  68. loginRequired: boolean;
  69. liveModelText: string;
  70. mode: string;
  71. modeOptions: readonly TuiModeOption[];
  72. modeSelection?: ModeCommandSelectionState;
  73. model: string;
  74. modifiedFiles: ModifiedFileStat[];
  75. modelOptions: readonly NonNullable<TuiOptions["modelOptions"]>[number][];
  76. modelSelection?: ModelCommandSelectionState;
  77. messages: Message[];
  78. networkRequests: NetworkRequest[];
  79. options: TuiOptions;
  80. queuedInputs?: QueuedInput[];
  81. selection?: SelectionState;
  82. setDraftValue: (value: string) => void;
  83. sidebarLayout: SidebarLayout;
  84. sidebarSections: SidebarSectionExpansion;
  85. slashCommands: readonly NonNullable<TuiOptions["slashCommands"]>[number][];
  86. slashSelection?: SlashSelectionState;
  87. status: string;
  88. statusDetails: string[];
  89. submitValue: (value: string) => void;
  90. thoughtLevel: string;
  91. todos: TodoItem[];
  92. toggleSidebarSection?: (section: SidebarSectionId) => boolean;
  93. traceId?: string;
  94. terminalWidth: number;
  95. usage?: ModelUsageSummary;
  96. workflowCardsByToolCallId?: ReadonlyMap<string, TuiWorkflowCard>;
  97. expandedWorkflowRunIds?: ReadonlySet<string>;
  98. }): React.ReactElement {
  99. const readOnly = Boolean(props.subagents?.selected);
  100. const handleShellMouseUp = React.useCallback(() => {
  101. props.copyCurrentSelection();
  102. if (!readOnly) props.editorRef.current?.focus();
  103. }, [props.copyCurrentSelection, props.editorRef, readOnly]);
  104. const actionPanelContentWidth = actionPanelContentWidthForTerminal(
  105. props.terminalWidth,
  106. props.sidebarLayout.reservedWidth,
  107. );
  108. const mcpStatus = useMcpSidebarStatus(
  109. props.loginRequired ? undefined : props.options.listMcpServers,
  110. );
  111. const transcriptMessages = props.liveModelText
  112. ? [
  113. ...props.messages,
  114. {
  115. content: props.liveModelText,
  116. role: "agent" as const,
  117. streaming: true,
  118. },
  119. ]
  120. : props.messages;
  121. // 旧的测试入口和嵌入式调用不会传 queuedInputs;默认空队列,避免队列面板在无数据时打断焦点交互。
  122. const queuedInputs = props.queuedInputs ?? [];
  123. const composerSelection = props.selection?.placement === "composer" ? props.selection : undefined;
  124. const actionSelection = props.selection && !composerSelection ? props.selection : undefined;
  125. const sidebar = props.sidebarLayout.visible
  126. ? h(Sidebar, {
  127. subagents: props.subagents,
  128. onOpenSubagent: (item: SubagentItem) => {
  129. props.subagents?.open(item);
  130. if (props.sidebarLayout.overlay) props.toggleSidebar?.();
  131. },
  132. activeTurnId: props.activeTurnId,
  133. busy: props.busy,
  134. cacheStats: props.cacheStats,
  135. contextUsage: props.contextUsage,
  136. draft: props.draft,
  137. lastError: props.lastError,
  138. lastEvent: props.lastEvent,
  139. messageCount: transcriptMessages.length,
  140. mcpStatus,
  141. mode: props.mode,
  142. model: props.model,
  143. modifiedFiles: props.modifiedFiles,
  144. networkRequests: props.networkRequests,
  145. onToggleSection: props.toggleSidebarSection,
  146. sectionExpansion: props.sidebarSections,
  147. status: props.status,
  148. statusDetails: props.statusDetails,
  149. thoughtLevel: props.thoughtLevel,
  150. todos: props.todos,
  151. traceId: props.traceId,
  152. usage: props.usage,
  153. developerMode: props.options.developerMode,
  154. version: props.options.version,
  155. workspaceDirectory: props.options.workspaceDirectory,
  156. workspaceGitBranch: props.options.workspaceGitBranch,
  157. copy: props.copy,
  158. })
  159. : null;
  160. return h(
  161. AppShell,
  162. { onMouseUp: handleShellMouseUp, sidebar, sidebarLayout: props.sidebarLayout },
  163. readOnly && props.subagents
  164. ? h(SubagentView, {
  165. controller: props.subagents,
  166. copy: props.copy,
  167. contentWidth: actionPanelContentWidth,
  168. pendingMain: props.approvalQueue.length > 0 || Boolean(props.selection),
  169. })
  170. : null,
  171. h(
  172. "box",
  173. {
  174. id: "main-conversation-view",
  175. visible: !readOnly,
  176. style: { flexDirection: "column", flexGrow: 1, minHeight: 0 },
  177. },
  178. h(ContentPane, {
  179. id: "main-transcript",
  180. animateEmptyLogo: !props.options.noColor,
  181. copy: props.copy,
  182. focused: false,
  183. messages: transcriptMessages,
  184. terminalWidth: actionPanelContentWidth,
  185. workflowCardsByToolCallId: props.workflowCardsByToolCallId,
  186. expandedWorkflowRunIds: props.expandedWorkflowRunIds,
  187. }),
  188. props.approvalQueue[0]
  189. ? h(ApprovalArea, {
  190. approval: props.approvalQueue[0],
  191. contentWidth: actionPanelContentWidth,
  192. copy: props.copy,
  193. mainSessionId: props.options.getMainSessionId?.(),
  194. })
  195. : actionSelection
  196. ? h(SelectionPanel, {
  197. contentWidth: actionPanelContentWidth,
  198. copy: props.copy,
  199. selection: actionSelection,
  200. })
  201. : h(ComposerInputArea, {
  202. focused: !readOnly,
  203. busy: props.busy,
  204. contentWidth: actionPanelContentWidth,
  205. contextUsage: props.contextUsage,
  206. copy: props.copy,
  207. draft: props.draft,
  208. editorRef: props.editorRef,
  209. effortOptions: props.effortOptions,
  210. effortSelection: props.effortSelection,
  211. fileMention: props.fileMention,
  212. inputCursorToEndVersion: props.inputCursorToEndVersion,
  213. loginRequired: props.loginRequired,
  214. mode: props.mode,
  215. modeOptions: props.modeOptions,
  216. modeSelection: props.modeSelection,
  217. model: props.model,
  218. modelOptions: props.modelOptions,
  219. modelSelection: props.modelSelection,
  220. queuedInputs,
  221. selection: composerSelection,
  222. setDraftValue: readOnly ? () => {} : props.setDraftValue,
  223. slashCommands: props.slashCommands,
  224. slashSelection: props.slashSelection,
  225. submitValue: readOnly ? () => {} : props.submitValue,
  226. thoughtLevel: props.thoughtLevel,
  227. }),
  228. ),
  229. );
  230. }
  231. export function ComposerInputArea(props: {
  232. focused?: boolean;
  233. busy: boolean;
  234. contentWidth: number;
  235. contextUsage: ContextUsage;
  236. copy: ReturnType<typeof import("@zcode/i18n").getZCodeCopy>["tui"];
  237. draft: string;
  238. editorRef: React.MutableRefObject<PromptInputEditor | null>;
  239. effortOptions: readonly TuiEffortOption[];
  240. effortSelection?: EffortCommandSelectionState;
  241. fileMention?: FileMentionState;
  242. inputCursorToEndVersion: number;
  243. loginRequired: boolean;
  244. mode: string;
  245. modeOptions: readonly TuiModeOption[];
  246. modeSelection?: ModeCommandSelectionState;
  247. model: string;
  248. modelOptions: readonly NonNullable<TuiOptions["modelOptions"]>[number][];
  249. modelSelection?: ModelCommandSelectionState;
  250. queuedInputs: QueuedInput[];
  251. selection?: SelectionState;
  252. setDraftValue: (value: string) => void;
  253. slashCommands: readonly NonNullable<TuiOptions["slashCommands"]>[number][];
  254. slashSelection?: SlashSelectionState;
  255. submitValue: (value: string) => void;
  256. thoughtLevel: string;
  257. }): React.ReactElement {
  258. return h(
  259. React.Fragment,
  260. null,
  261. props.loginRequired ? h(LoginRequiredPanel, { copy: props.copy }) : null,
  262. props.fileMention
  263. ? h(FileMentionPanel, {
  264. contentWidth: props.contentWidth,
  265. copy: props.copy,
  266. state: props.fileMention,
  267. })
  268. : null,
  269. props.selection
  270. ? h(SelectionPanel, {
  271. contentWidth: props.contentWidth,
  272. copy: props.copy,
  273. selection: props.selection,
  274. })
  275. : null,
  276. props.modelSelection
  277. ? h(ModelSuggestionPanel, {
  278. contentWidth: props.contentWidth,
  279. currentModel: props.model,
  280. models: props.modelOptions,
  281. selectedIndex: props.modelSelection.selectedIndex,
  282. })
  283. : null,
  284. props.effortSelection
  285. ? h(EffortSuggestionPanel, {
  286. contentWidth: props.contentWidth,
  287. currentEffort: props.thoughtLevel,
  288. efforts: props.effortOptions,
  289. selectedIndex: props.effortSelection.selectedIndex,
  290. })
  291. : null,
  292. props.modeSelection
  293. ? h(ModeSuggestionPanel, {
  294. contentWidth: props.contentWidth,
  295. currentMode: props.mode,
  296. modes: props.modeOptions,
  297. selectedIndex: props.modeSelection.selectedIndex,
  298. })
  299. : null,
  300. props.slashSelection
  301. ? h(SlashSuggestionPanel, {
  302. contentWidth: props.contentWidth,
  303. commands: props.slashCommands,
  304. copy: props.copy,
  305. selectedIndex: props.slashSelection.selectedIndex,
  306. })
  307. : null,
  308. h(QueuedInputPanel, {
  309. contentWidth: props.contentWidth,
  310. copy: props.copy,
  311. inputs: props.queuedInputs,
  312. }),
  313. h(InputPane, {
  314. busy: props.busy,
  315. contentWidth: props.contentWidth,
  316. copy: props.copy,
  317. editorRef: props.editorRef,
  318. focused: props.focused ?? true,
  319. mode: props.mode,
  320. model: props.model,
  321. onInput: props.setDraftValue,
  322. onSubmit: props.submitValue,
  323. resetCursorToEndVersion: props.inputCursorToEndVersion,
  324. thoughtLevel: props.thoughtLevel,
  325. value: props.draft,
  326. }),
  327. h(InputActiveStatus, {
  328. active: props.busy,
  329. contentWidth: props.contentWidth,
  330. contextUsage: props.contextUsage,
  331. copy: props.copy,
  332. }),
  333. );
  334. }