| 1 | import { Box, Text } from "ink"; |
| 2 | import React from "react"; |
| 3 | |
| 4 | import { UpdateServiceState } from "src/services/types.js"; |
| 5 | |
| 6 | import { listSessions } from "../../session.js"; |
| 7 | import { ConfigSelector } from "../ConfigSelector.js"; |
| 8 | import type { NavigationScreen } from "../context/NavigationContext.js"; |
| 9 | import { FreeTrialTransitionUI } from "../FreeTrialTransitionUI.js"; |
| 10 | import { MCPSelector } from "../MCPSelector.js"; |
| 11 | import { ModelSelector } from "../ModelSelector.js"; |
| 12 | import { SessionSelector } from "../SessionSelector.js"; |
| 13 | import type { ConfigOption, ModelOption } from "../types/selectorTypes.js"; |
| 14 | import { UpdateSelector } from "../UpdateSelector.js"; |
| 15 | import { UserInput } from "../UserInput.js"; |
| 16 | |
| 17 | import { ToolPermissionSelector } from "./ToolPermissionSelector.js"; |
| 18 | |
| 19 | interface ScreenContentProps { |
| 20 | isScreenActive: (screen: NavigationScreen) => boolean; |
| 21 | navState: any; |
| 22 | services: any; |
| 23 | handleLoginTokenSubmit: (token: string) => void; |
| 24 | handleConfigSelect: (config: ConfigOption) => Promise<void>; |
| 25 | handleModelSelect: (model: ModelOption) => Promise<void>; |
| 26 | handleSessionSelect: (sessionId: string) => Promise<void>; |
| 27 | handleReload: () => Promise<void>; |
| 28 | closeCurrentScreen: () => void; |
| 29 | activePermissionRequest: any; |
| 30 | handleToolPermissionResponse: ( |
| 31 | requestId: string, |
| 32 | approved: boolean, |
| 33 | createPolicy?: boolean, |
| 34 | stopStream?: boolean, |
| 35 | ) => void; |
| 36 | handleUserMessage: (message: string, imageMap?: Map<string, Buffer>) => void; |
| 37 | isWaitingForResponse: boolean; |
| 38 | isCompacting: boolean; |
| 39 | inputMode: boolean; |
| 40 | handleInterrupt: () => void; |
| 41 | handleFileAttached: (filePath: string, content: string) => void; |
| 42 | isInputDisabled: boolean; |
| 43 | wasInterrupted?: boolean; |
| 44 | isRemoteMode: boolean; |
| 45 | onImageInClipboardChange?: (hasImage: boolean) => void; |
| 46 | } |
| 47 | |
| 48 | function hideScreenContent(state?: UpdateServiceState) { |
| 49 | return ( |
| 50 | (state?.status === "checking" && state?.autoUpdate) || |
| 51 | (state?.isAutoUpdate && |
| 52 | (state?.status === "updating" || state?.status === "updated")) |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | export const ScreenContent: React.FC<ScreenContentProps> = ({ |
| 57 | isScreenActive, |
| 58 | navState, |
| 59 | services, |
| 60 | handleLoginTokenSubmit, |
| 61 | handleConfigSelect, |
| 62 | handleModelSelect, |
| 63 | handleSessionSelect, |
| 64 | handleReload, |
| 65 | closeCurrentScreen, |
| 66 | activePermissionRequest, |
| 67 | handleToolPermissionResponse, |
| 68 | handleUserMessage, |
| 69 | isWaitingForResponse, |
| 70 | isCompacting, |
| 71 | inputMode, |
| 72 | handleInterrupt, |
| 73 | handleFileAttached, |
| 74 | isInputDisabled, |
| 75 | wasInterrupted = false, |
| 76 | isRemoteMode, |
| 77 | onImageInClipboardChange, |
| 78 | }) => { |
| 79 | if (hideScreenContent(services.update)) { |
| 80 | return null; |
| 81 | } |
| 82 | |
| 83 | // Login prompt |
| 84 | if (isScreenActive("login") && navState.screenData) { |
| 85 | return ( |
| 86 | <Box |
| 87 | paddingX={1} |
| 88 | borderStyle="round" |
| 89 | borderColor="yellow" |
| 90 | flexDirection="column" |
| 91 | gap={1} |
| 92 | > |
| 93 | <Text color="yellow" bold> |
| 94 | Login Required |
| 95 | </Text> |
| 96 | <Text>{navState.screenData.text}</Text> |
| 97 | <UserInput |
| 98 | onSubmit={handleLoginTokenSubmit} |
| 99 | isWaitingForResponse={false} |
| 100 | inputMode={true} |
| 101 | assistant={services.config?.config || undefined} |
| 102 | disabled={false} |
| 103 | placeholder="Enter your token..." |
| 104 | hideNormalUI={true} |
| 105 | /> |
| 106 | </Box> |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | // Config selector |
| 111 | if (isScreenActive("config")) { |
| 112 | return ( |
| 113 | <ConfigSelector |
| 114 | onSelect={handleConfigSelect} |
| 115 | onCancel={closeCurrentScreen} |
| 116 | /> |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | if (isScreenActive("mcp")) { |
| 121 | return <MCPSelector onCancel={closeCurrentScreen} />; |
| 122 | } |
| 123 | |
| 124 | if (isScreenActive("update")) { |
| 125 | return <UpdateSelector onCancel={closeCurrentScreen} />; |
| 126 | } |
| 127 | |
| 128 | // Model selector |
| 129 | if (isScreenActive("model")) { |
| 130 | return ( |
| 131 | <ModelSelector |
| 132 | onSelect={handleModelSelect} |
| 133 | onCancel={closeCurrentScreen} |
| 134 | /> |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | // Session selector |
| 139 | if (isScreenActive("session")) { |
| 140 | const sessions = listSessions(20); |
| 141 | return ( |
| 142 | <SessionSelector |
| 143 | sessions={sessions} |
| 144 | onSelect={handleSessionSelect} |
| 145 | onExit={closeCurrentScreen} |
| 146 | /> |
| 147 | ); |
| 148 | } |
| 149 | |
| 150 | // Free trial transition UI |
| 151 | if (isScreenActive("free-trial")) { |
| 152 | return <FreeTrialTransitionUI onReload={handleReload} />; |
| 153 | } |
| 154 | |
| 155 | // Chat screen with input area |
| 156 | if (isScreenActive("chat")) { |
| 157 | if (activePermissionRequest) { |
| 158 | return ( |
| 159 | <ToolPermissionSelector |
| 160 | toolName={activePermissionRequest.toolName} |
| 161 | toolArgs={activePermissionRequest.toolArgs} |
| 162 | requestId={activePermissionRequest.requestId} |
| 163 | toolCallPreview={activePermissionRequest.toolCallPreview} |
| 164 | hasDynamicEvaluation={activePermissionRequest.hasDynamicEvaluation} |
| 165 | onResponse={handleToolPermissionResponse} |
| 166 | /> |
| 167 | ); |
| 168 | } |
| 169 | return ( |
| 170 | <UserInput |
| 171 | onSubmit={handleUserMessage} |
| 172 | isWaitingForResponse={isWaitingForResponse} |
| 173 | isCompacting={isCompacting} |
| 174 | inputMode={inputMode} |
| 175 | onInterrupt={handleInterrupt} |
| 176 | assistant={services.config?.config || undefined} |
| 177 | wasInterrupted={wasInterrupted} |
| 178 | onFileAttached={handleFileAttached} |
| 179 | disabled={isInputDisabled} |
| 180 | isRemoteMode={isRemoteMode} |
| 181 | onImageInClipboardChange={onImageInClipboardChange} |
| 182 | /> |
| 183 | ); |
| 184 | } |
| 185 | |
| 186 | return null; |
| 187 | }; |