Merge pull request #227 from continuedev/context-percentage-display
feat: add context percentage display to status bar
This commit is contained in:
commit
87eafa8a46
7 changed files with 483 additions and 366 deletions
|
|
@ -1,9 +1,7 @@
|
|||
import { Box, Text } from "ink";
|
||||
import React, { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import React, { useEffect, useMemo } from "react";
|
||||
|
||||
import { useServices } from "../hooks/useService.js";
|
||||
import type { PermissionMode } from "../permissions/types.js";
|
||||
import { modeService } from "../services/ModeService.js";
|
||||
import {
|
||||
ApiClientServiceState,
|
||||
AuthServiceState,
|
||||
|
|
@ -11,174 +9,24 @@ import {
|
|||
MCPServiceState,
|
||||
ModelServiceState,
|
||||
} from "../services/types.js";
|
||||
import { getGitRemoteUrl, isGitRepo } from "../util/git.js";
|
||||
|
||||
import { ModeIndicator } from "./components/ModeIndicator.js";
|
||||
import { BottomStatusBar } from "./components/BottomStatusBar.js";
|
||||
import { ScreenContent } from "./components/ScreenContent.js";
|
||||
import { StaticChatContent } from "./components/StaticChatContent.js";
|
||||
import { ToolPermissionSelector } from "./components/ToolPermissionSelector.js";
|
||||
import { ConfigSelector } from "./ConfigSelector.js";
|
||||
import {
|
||||
useNavigation,
|
||||
type NavigationScreen,
|
||||
} from "./context/NavigationContext.js";
|
||||
import { useNavigation } from "./context/NavigationContext.js";
|
||||
import { startFileIndexing } from "./FileSearchUI.js";
|
||||
import { FreeTrialStatus } from "./FreeTrialStatus.js";
|
||||
import { FreeTrialTransitionUI } from "./FreeTrialTransitionUI.js";
|
||||
import { useChat } from "./hooks/useChat.js";
|
||||
import { useConfigSelector } from "./hooks/useConfigSelector.js";
|
||||
import { useContextPercentage } from "./hooks/useContextPercentage.js";
|
||||
import { useMessageRenderer } from "./hooks/useMessageRenderer.js";
|
||||
import { useModelSelector } from "./hooks/useModelSelector.js";
|
||||
import { useOrganizationSelector } from "./hooks/useOrganizationSelector.js";
|
||||
import {
|
||||
getRepoUrlText,
|
||||
useCurrentMode,
|
||||
useIntroMessage,
|
||||
useLoginHandlers,
|
||||
useSelectors,
|
||||
} from "./hooks/useTUIChatHooks.js";
|
||||
import { LoadingAnimation } from "./LoadingAnimation.js";
|
||||
import { MCPSelector } from "./MCPSelector.js";
|
||||
import { ModelSelector } from "./ModelSelector.js";
|
||||
import { OrganizationSelector } from "./OrganizationSelector.js";
|
||||
import type { SelectorOption } from "./Selector.js";
|
||||
import { Timer } from "./Timer.js";
|
||||
import { UpdateNotification } from "./UpdateNotification.js";
|
||||
import { UserInput } from "./UserInput.js";
|
||||
|
||||
// Type definitions for selectors
|
||||
interface ConfigOption extends SelectorOption {
|
||||
type: "local" | "assistant" | "create";
|
||||
slug?: string;
|
||||
}
|
||||
|
||||
interface ModelOption extends SelectorOption {
|
||||
index: number;
|
||||
provider: string;
|
||||
}
|
||||
|
||||
// Helper function to get repo URL text
|
||||
function getRepoUrlText(remoteUrl?: string): string {
|
||||
let url = remoteUrl ?? "";
|
||||
if (!url) {
|
||||
const isGit = isGitRepo();
|
||||
if (isGit) {
|
||||
const gitUrl = getGitRemoteUrl();
|
||||
if (gitUrl) {
|
||||
url = gitUrl;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!url) {
|
||||
url = process.cwd();
|
||||
}
|
||||
|
||||
url = url.replace(/\.git$/, "");
|
||||
url = url.replace(/^(https|http):\/\/.*?\//, "");
|
||||
return url;
|
||||
}
|
||||
|
||||
// Custom hook for intro message
|
||||
function useIntroMessage(
|
||||
isRemoteMode: boolean,
|
||||
services: any,
|
||||
allServicesReady: boolean,
|
||||
) {
|
||||
const [showIntroMessage, setShowIntroMessage] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const shouldShow =
|
||||
!isRemoteMode &&
|
||||
allServicesReady &&
|
||||
!!services.config?.config &&
|
||||
!!services.model?.model &&
|
||||
!!services.mcp?.mcpService;
|
||||
|
||||
setShowIntroMessage(shouldShow);
|
||||
}, [
|
||||
isRemoteMode,
|
||||
allServicesReady,
|
||||
services.config?.config,
|
||||
services.model?.model,
|
||||
services.mcp?.mcpService,
|
||||
]);
|
||||
|
||||
return [showIntroMessage, setShowIntroMessage] as const;
|
||||
}
|
||||
|
||||
// Custom hook for login handling
|
||||
function useLoginHandlers(
|
||||
navigateTo: any,
|
||||
navState: any,
|
||||
closeCurrentScreen: () => void,
|
||||
) {
|
||||
const handleLoginPrompt = useCallback(
|
||||
(promptText: string): Promise<string> => {
|
||||
return new Promise((resolve) => {
|
||||
navigateTo("login", { text: promptText, resolve });
|
||||
});
|
||||
},
|
||||
[navigateTo],
|
||||
);
|
||||
|
||||
const handleLoginTokenSubmit = useCallback(
|
||||
(token: string) => {
|
||||
if (navState.screenData?.resolve) {
|
||||
navState.screenData.resolve(token);
|
||||
closeCurrentScreen();
|
||||
}
|
||||
},
|
||||
[navState.screenData, closeCurrentScreen],
|
||||
);
|
||||
|
||||
return { handleLoginPrompt, handleLoginTokenSubmit };
|
||||
}
|
||||
|
||||
// Custom hook for mode tracking
|
||||
function useCurrentMode() {
|
||||
const [currentMode, setCurrentMode] = useState<PermissionMode>(
|
||||
modeService.getCurrentMode(),
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
const handleModeChange = (newMode: PermissionMode) => {
|
||||
setCurrentMode(newMode);
|
||||
};
|
||||
|
||||
modeService.on("modeChanged", handleModeChange);
|
||||
return () => {
|
||||
modeService.off("modeChanged", handleModeChange);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return currentMode;
|
||||
}
|
||||
|
||||
// Custom hook to combine all selector logic
|
||||
function useSelectors(
|
||||
configPath: string | undefined,
|
||||
setMessages: React.Dispatch<React.SetStateAction<any[]>>,
|
||||
resetChatHistory: () => void,
|
||||
) {
|
||||
const { handleOrganizationSelect } = useOrganizationSelector({
|
||||
onMessage: (message) => {
|
||||
setMessages((prev) => [...prev, message]);
|
||||
},
|
||||
onChatReset: resetChatHistory,
|
||||
});
|
||||
|
||||
const { handleConfigSelect } = useConfigSelector({
|
||||
configPath,
|
||||
onMessage: (message) => {
|
||||
setMessages((prev) => [...prev, message]);
|
||||
},
|
||||
onChatReset: resetChatHistory,
|
||||
});
|
||||
|
||||
const { handleModelSelect } = useModelSelector({
|
||||
onMessage: (message) => {
|
||||
setMessages((prev) => [...prev, message]);
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
handleOrganizationSelect,
|
||||
handleConfigSelect,
|
||||
handleModelSelect,
|
||||
};
|
||||
}
|
||||
|
||||
interface TUIChatProps {
|
||||
// Remote mode props
|
||||
|
|
@ -191,205 +39,6 @@ interface TUIChatProps {
|
|||
additionalRules?: string[];
|
||||
}
|
||||
|
||||
// Bottom status bar component
|
||||
interface BottomStatusBarProps {
|
||||
currentMode: PermissionMode;
|
||||
repoURlText: string;
|
||||
isRemoteMode: boolean;
|
||||
services: any;
|
||||
navState: any;
|
||||
navigateTo: (screen: NavigationScreen, data?: any) => void;
|
||||
closeCurrentScreen: () => void;
|
||||
}
|
||||
|
||||
const BottomStatusBar: React.FC<BottomStatusBarProps> = ({
|
||||
currentMode,
|
||||
repoURlText,
|
||||
isRemoteMode,
|
||||
services,
|
||||
navState,
|
||||
navigateTo,
|
||||
closeCurrentScreen,
|
||||
}) => (
|
||||
<Box flexDirection="row" justifyContent="space-between" alignItems="center">
|
||||
<Box marginLeft={2} flexDirection="row" alignItems="center">
|
||||
{currentMode === "normal" && (
|
||||
<>
|
||||
<Text color="dim" wrap="truncate-start">
|
||||
{repoURlText}
|
||||
</Text>
|
||||
<Text> </Text>
|
||||
</>
|
||||
)}
|
||||
<ModeIndicator />
|
||||
</Box>
|
||||
<Box>
|
||||
{!isRemoteMode && services.model?.model && (
|
||||
<FreeTrialStatus
|
||||
apiClient={services.apiClient?.apiClient || undefined}
|
||||
model={services.model.model}
|
||||
onTransitionStateChange={(shouldShow) => {
|
||||
if (shouldShow && navState.currentScreen === "chat") {
|
||||
navigateTo("free-trial");
|
||||
} else if (!shouldShow && navState.currentScreen === "free-trial") {
|
||||
closeCurrentScreen();
|
||||
}
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</Box>
|
||||
<Box marginRight={2} marginLeft={2}>
|
||||
<UpdateNotification isRemoteMode={isRemoteMode} />
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
|
||||
// Component to handle all screen-specific rendering
|
||||
interface ScreenContentProps {
|
||||
isScreenActive: (screen: NavigationScreen) => boolean;
|
||||
navState: any;
|
||||
services: any;
|
||||
handleLoginTokenSubmit: (token: string) => void;
|
||||
handleOrganizationSelect: (
|
||||
organizationId: string | null,
|
||||
organizationName: string,
|
||||
) => Promise<void>;
|
||||
handleConfigSelect: (config: ConfigOption) => Promise<void>;
|
||||
handleModelSelect: (model: ModelOption) => Promise<void>;
|
||||
handleReload: () => Promise<void>;
|
||||
closeCurrentScreen: () => void;
|
||||
activePermissionRequest: any;
|
||||
handleToolPermissionResponse: (
|
||||
requestId: string,
|
||||
approved: boolean,
|
||||
createPolicy?: boolean,
|
||||
stopStream?: boolean,
|
||||
) => void;
|
||||
handleUserMessage: (message: string) => void;
|
||||
isWaitingForResponse: boolean;
|
||||
inputMode: boolean;
|
||||
handleInterrupt: () => void;
|
||||
handleFileAttached: (filePath: string, content: string) => void;
|
||||
isInputDisabled: boolean;
|
||||
isRemoteMode: boolean;
|
||||
}
|
||||
|
||||
const ScreenContent: React.FC<ScreenContentProps> = ({
|
||||
isScreenActive,
|
||||
navState,
|
||||
services,
|
||||
handleLoginTokenSubmit,
|
||||
handleOrganizationSelect,
|
||||
handleConfigSelect,
|
||||
handleModelSelect,
|
||||
handleReload,
|
||||
closeCurrentScreen,
|
||||
activePermissionRequest,
|
||||
handleToolPermissionResponse,
|
||||
handleUserMessage,
|
||||
isWaitingForResponse,
|
||||
inputMode,
|
||||
handleInterrupt,
|
||||
handleFileAttached,
|
||||
isInputDisabled,
|
||||
isRemoteMode,
|
||||
}) => {
|
||||
// Login prompt
|
||||
if (isScreenActive("login") && navState.screenData) {
|
||||
return (
|
||||
<Box
|
||||
paddingX={1}
|
||||
borderStyle="round"
|
||||
borderColor="yellow"
|
||||
flexDirection="column"
|
||||
gap={1}
|
||||
>
|
||||
<Text color="yellow" bold>
|
||||
Login Required
|
||||
</Text>
|
||||
<Text>{navState.screenData.text}</Text>
|
||||
<UserInput
|
||||
onSubmit={handleLoginTokenSubmit}
|
||||
isWaitingForResponse={false}
|
||||
inputMode={true}
|
||||
assistant={services.config?.config || undefined}
|
||||
disabled={false}
|
||||
placeholder="Enter your token..."
|
||||
hideNormalUI={true}
|
||||
/>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
// Organization selector
|
||||
if (isScreenActive("organization")) {
|
||||
return (
|
||||
<OrganizationSelector
|
||||
onSelect={handleOrganizationSelect}
|
||||
onCancel={closeCurrentScreen}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
// Config selector
|
||||
if (isScreenActive("config")) {
|
||||
return (
|
||||
<ConfigSelector
|
||||
onSelect={handleConfigSelect}
|
||||
onCancel={closeCurrentScreen}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
if (isScreenActive("mcp")) {
|
||||
return <MCPSelector onCancel={closeCurrentScreen} />;
|
||||
}
|
||||
|
||||
// Model selector
|
||||
if (isScreenActive("model")) {
|
||||
return (
|
||||
<ModelSelector
|
||||
onSelect={handleModelSelect}
|
||||
onCancel={closeCurrentScreen}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
// Free trial transition UI
|
||||
if (isScreenActive("free-trial")) {
|
||||
return <FreeTrialTransitionUI onReload={handleReload} />;
|
||||
}
|
||||
|
||||
// Chat screen with input area
|
||||
if (isScreenActive("chat")) {
|
||||
if (activePermissionRequest) {
|
||||
return (
|
||||
<ToolPermissionSelector
|
||||
toolName={activePermissionRequest.toolName}
|
||||
toolArgs={activePermissionRequest.toolArgs}
|
||||
requestId={activePermissionRequest.requestId}
|
||||
toolCallPreview={activePermissionRequest.toolCallPreview}
|
||||
onResponse={handleToolPermissionResponse}
|
||||
/>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<UserInput
|
||||
onSubmit={handleUserMessage}
|
||||
isWaitingForResponse={isWaitingForResponse}
|
||||
inputMode={inputMode}
|
||||
onInterrupt={handleInterrupt}
|
||||
assistant={services.config?.config || undefined}
|
||||
onFileAttached={handleFileAttached}
|
||||
disabled={isInputDisabled}
|
||||
isRemoteMode={isRemoteMode}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
const TUIChat: React.FC<TUIChatProps> = ({
|
||||
remoteUrl,
|
||||
configPath,
|
||||
|
|
@ -458,6 +107,7 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
|||
const {
|
||||
messages,
|
||||
setMessages,
|
||||
chatHistory,
|
||||
isWaitingForResponse,
|
||||
responseStartTime,
|
||||
inputMode,
|
||||
|
|
@ -485,6 +135,12 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
|||
remoteUrl,
|
||||
});
|
||||
|
||||
// Calculate context percentage
|
||||
const contextData = useContextPercentage({
|
||||
chatHistory,
|
||||
model: services.model?.model || undefined,
|
||||
});
|
||||
|
||||
const { renderMessage } = useMessageRenderer();
|
||||
|
||||
const { handleOrganizationSelect, handleConfigSelect, handleModelSelect } =
|
||||
|
|
@ -528,9 +184,13 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
|||
{isWaitingForResponse && responseStartTime && (
|
||||
<Box paddingX={1} flexDirection="row" gap={1}>
|
||||
<LoadingAnimation visible={isWaitingForResponse} />
|
||||
<Text color="gray">(</Text>
|
||||
<Text key="loading-start" color="gray">
|
||||
(
|
||||
</Text>
|
||||
<Timer startTime={responseStartTime} />
|
||||
<Text color="gray">• esc to interrupt )</Text>
|
||||
<Text key="loading-end" color="gray">
|
||||
• esc to interrupt )
|
||||
</Text>
|
||||
</Box>
|
||||
)}
|
||||
|
||||
|
|
@ -559,12 +219,13 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
|||
{/* Free trial status and Continue CLI info - always show */}
|
||||
<BottomStatusBar
|
||||
currentMode={currentMode}
|
||||
repoURlText={repoURlText}
|
||||
repoURLText={repoURlText}
|
||||
isRemoteMode={isRemoteMode}
|
||||
services={services}
|
||||
navState={navState}
|
||||
navigateTo={navigateTo}
|
||||
closeCurrentScreen={closeCurrentScreen}
|
||||
contextPercentage={contextData?.percentage}
|
||||
/>
|
||||
</Box>
|
||||
</Box>
|
||||
|
|
|
|||
70
src/ui/components/BottomStatusBar.tsx
Normal file
70
src/ui/components/BottomStatusBar.tsx
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
import { Box, Text } from "ink";
|
||||
import React from "react";
|
||||
|
||||
import type { PermissionMode } from "../../permissions/types.js";
|
||||
import type { NavigationScreen } from "../context/NavigationContext.js";
|
||||
import { FreeTrialStatus } from "../FreeTrialStatus.js";
|
||||
import { UpdateNotification } from "../UpdateNotification.js";
|
||||
|
||||
import { ContextPercentageDisplay } from "./ContextPercentageDisplay.js";
|
||||
import { ModeIndicator } from "./ModeIndicator.js";
|
||||
|
||||
interface BottomStatusBarProps {
|
||||
currentMode: PermissionMode;
|
||||
repoURLText: string;
|
||||
isRemoteMode: boolean;
|
||||
services: any;
|
||||
navState: any;
|
||||
navigateTo: (screen: NavigationScreen, data?: any) => void;
|
||||
closeCurrentScreen: () => void;
|
||||
contextPercentage?: number;
|
||||
}
|
||||
|
||||
export const BottomStatusBar: React.FC<BottomStatusBarProps> = ({
|
||||
currentMode,
|
||||
repoURLText,
|
||||
isRemoteMode,
|
||||
services,
|
||||
navState,
|
||||
navigateTo,
|
||||
closeCurrentScreen,
|
||||
contextPercentage,
|
||||
}) => (
|
||||
<Box flexDirection="row" justifyContent="space-between" alignItems="center">
|
||||
<Box marginLeft={2} flexDirection="row" alignItems="center">
|
||||
{currentMode === "normal" && (
|
||||
<React.Fragment>
|
||||
<Text key="repo-url" color="dim" wrap="truncate-start">
|
||||
{repoURLText}
|
||||
</Text>
|
||||
<Text key="repo-separator"> </Text>
|
||||
</React.Fragment>
|
||||
)}
|
||||
<ModeIndicator />
|
||||
{contextPercentage !== undefined && contextPercentage > 75 && (
|
||||
<React.Fragment>
|
||||
<Text key="context-separator"> • </Text>
|
||||
<ContextPercentageDisplay percentage={contextPercentage} />
|
||||
</React.Fragment>
|
||||
)}
|
||||
</Box>
|
||||
<Box>
|
||||
{!isRemoteMode && services.model?.model && (
|
||||
<FreeTrialStatus
|
||||
apiClient={services.apiClient?.apiClient || undefined}
|
||||
model={services.model.model}
|
||||
onTransitionStateChange={(shouldShow) => {
|
||||
if (shouldShow && navState.currentScreen === "chat") {
|
||||
navigateTo("free-trial");
|
||||
} else if (!shouldShow && navState.currentScreen === "free-trial") {
|
||||
closeCurrentScreen();
|
||||
}
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</Box>
|
||||
<Box marginRight={2} marginLeft={2}>
|
||||
<UpdateNotification isRemoteMode={isRemoteMode} />
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
22
src/ui/components/ContextPercentageDisplay.tsx
Normal file
22
src/ui/components/ContextPercentageDisplay.tsx
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import { Text } from "ink";
|
||||
import React from "react";
|
||||
|
||||
interface ContextPercentageDisplayProps {
|
||||
percentage: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Component to display the current context usage percentage
|
||||
* Shows the percentage in gray color for consistency with other status indicators
|
||||
* Only displays when percentage exceeds 75% threshold
|
||||
*/
|
||||
export const ContextPercentageDisplay: React.FC<
|
||||
ContextPercentageDisplayProps
|
||||
> = ({ percentage }) => {
|
||||
// Only show if percentage is greater than 75%
|
||||
if (percentage <= 75) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return <Text color="dim">Context: {percentage}%</Text>;
|
||||
};
|
||||
158
src/ui/components/ScreenContent.tsx
Normal file
158
src/ui/components/ScreenContent.tsx
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
import { Box, Text } from "ink";
|
||||
import React from "react";
|
||||
|
||||
import { ConfigSelector } from "../ConfigSelector.js";
|
||||
import type { NavigationScreen } from "../context/NavigationContext.js";
|
||||
import { FreeTrialTransitionUI } from "../FreeTrialTransitionUI.js";
|
||||
import { MCPSelector } from "../MCPSelector.js";
|
||||
import { ModelSelector } from "../ModelSelector.js";
|
||||
import { OrganizationSelector } from "../OrganizationSelector.js";
|
||||
import type { ConfigOption, ModelOption } from "../types/selectorTypes.js";
|
||||
import { UserInput } from "../UserInput.js";
|
||||
|
||||
import { ToolPermissionSelector } from "./ToolPermissionSelector.js";
|
||||
|
||||
interface ScreenContentProps {
|
||||
isScreenActive: (screen: NavigationScreen) => boolean;
|
||||
navState: any;
|
||||
services: any;
|
||||
handleLoginTokenSubmit: (token: string) => void;
|
||||
handleOrganizationSelect: (
|
||||
organizationId: string | null,
|
||||
organizationName: string,
|
||||
) => Promise<void>;
|
||||
handleConfigSelect: (config: ConfigOption) => Promise<void>;
|
||||
handleModelSelect: (model: ModelOption) => Promise<void>;
|
||||
handleReload: () => Promise<void>;
|
||||
closeCurrentScreen: () => void;
|
||||
activePermissionRequest: any;
|
||||
handleToolPermissionResponse: (
|
||||
requestId: string,
|
||||
approved: boolean,
|
||||
createPolicy?: boolean,
|
||||
stopStream?: boolean,
|
||||
) => void;
|
||||
handleUserMessage: (message: string) => void;
|
||||
isWaitingForResponse: boolean;
|
||||
inputMode: boolean;
|
||||
handleInterrupt: () => void;
|
||||
handleFileAttached: (filePath: string, content: string) => void;
|
||||
isInputDisabled: boolean;
|
||||
isRemoteMode: boolean;
|
||||
}
|
||||
|
||||
export const ScreenContent: React.FC<ScreenContentProps> = ({
|
||||
isScreenActive,
|
||||
navState,
|
||||
services,
|
||||
handleLoginTokenSubmit,
|
||||
handleOrganizationSelect,
|
||||
handleConfigSelect,
|
||||
handleModelSelect,
|
||||
handleReload,
|
||||
closeCurrentScreen,
|
||||
activePermissionRequest,
|
||||
handleToolPermissionResponse,
|
||||
handleUserMessage,
|
||||
isWaitingForResponse,
|
||||
inputMode,
|
||||
handleInterrupt,
|
||||
handleFileAttached,
|
||||
isInputDisabled,
|
||||
isRemoteMode,
|
||||
}) => {
|
||||
// Login prompt
|
||||
if (isScreenActive("login") && navState.screenData) {
|
||||
return (
|
||||
<Box
|
||||
paddingX={1}
|
||||
borderStyle="round"
|
||||
borderColor="yellow"
|
||||
flexDirection="column"
|
||||
gap={1}
|
||||
>
|
||||
<Text color="yellow" bold>
|
||||
Login Required
|
||||
</Text>
|
||||
<Text>{navState.screenData.text}</Text>
|
||||
<UserInput
|
||||
onSubmit={handleLoginTokenSubmit}
|
||||
isWaitingForResponse={false}
|
||||
inputMode={true}
|
||||
assistant={services.config?.config || undefined}
|
||||
disabled={false}
|
||||
placeholder="Enter your token..."
|
||||
hideNormalUI={true}
|
||||
/>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
// Organization selector
|
||||
if (isScreenActive("organization")) {
|
||||
return (
|
||||
<OrganizationSelector
|
||||
onSelect={handleOrganizationSelect}
|
||||
onCancel={closeCurrentScreen}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
// Config selector
|
||||
if (isScreenActive("config")) {
|
||||
return (
|
||||
<ConfigSelector
|
||||
onSelect={handleConfigSelect}
|
||||
onCancel={closeCurrentScreen}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
if (isScreenActive("mcp")) {
|
||||
return <MCPSelector onCancel={closeCurrentScreen} />;
|
||||
}
|
||||
|
||||
// Model selector
|
||||
if (isScreenActive("model")) {
|
||||
return (
|
||||
<ModelSelector
|
||||
onSelect={handleModelSelect}
|
||||
onCancel={closeCurrentScreen}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
// Free trial transition UI
|
||||
if (isScreenActive("free-trial")) {
|
||||
return <FreeTrialTransitionUI onReload={handleReload} />;
|
||||
}
|
||||
|
||||
// Chat screen with input area
|
||||
if (isScreenActive("chat")) {
|
||||
if (activePermissionRequest) {
|
||||
return (
|
||||
<ToolPermissionSelector
|
||||
toolName={activePermissionRequest.toolName}
|
||||
toolArgs={activePermissionRequest.toolArgs}
|
||||
requestId={activePermissionRequest.requestId}
|
||||
toolCallPreview={activePermissionRequest.toolCallPreview}
|
||||
onResponse={handleToolPermissionResponse}
|
||||
/>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<UserInput
|
||||
onSubmit={handleUserMessage}
|
||||
isWaitingForResponse={isWaitingForResponse}
|
||||
inputMode={inputMode}
|
||||
onInterrupt={handleInterrupt}
|
||||
assistant={services.config?.config || undefined}
|
||||
onFileAttached={handleFileAttached}
|
||||
disabled={isInputDisabled}
|
||||
isRemoteMode={isRemoteMode}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
41
src/ui/hooks/useContextPercentage.ts
Normal file
41
src/ui/hooks/useContextPercentage.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import { ModelConfig } from "@continuedev/config-yaml";
|
||||
import { ChatCompletionMessageParam } from "openai/resources.mjs";
|
||||
import { useMemo } from "react";
|
||||
|
||||
import {
|
||||
calculateContextUsagePercentage,
|
||||
countChatHistoryTokens,
|
||||
} from "../../util/tokenizer.js";
|
||||
|
||||
interface UseContextPercentageProps {
|
||||
chatHistory: ChatCompletionMessageParam[];
|
||||
model?: ModelConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to calculate the current context usage percentage
|
||||
* This uses the same logic as the compaction trigger to show users
|
||||
* how close they are to the context limit
|
||||
*/
|
||||
export function useContextPercentage({
|
||||
chatHistory,
|
||||
model,
|
||||
}: UseContextPercentageProps) {
|
||||
const contextData = useMemo(() => {
|
||||
// Return null if no model is available
|
||||
if (!model) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Calculate token count and percentage
|
||||
const tokenCount = countChatHistoryTokens(chatHistory);
|
||||
const percentage = calculateContextUsagePercentage(tokenCount, model);
|
||||
|
||||
return {
|
||||
tokenCount,
|
||||
percentage,
|
||||
};
|
||||
}, [chatHistory, model]);
|
||||
|
||||
return contextData;
|
||||
}
|
||||
148
src/ui/hooks/useTUIChatHooks.ts
Normal file
148
src/ui/hooks/useTUIChatHooks.ts
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
import { useCallback, useEffect, useState } from "react";
|
||||
|
||||
import type { PermissionMode } from "../../permissions/types.js";
|
||||
import { modeService } from "../../services/ModeService.js";
|
||||
import { getGitRemoteUrl, isGitRepo } from "../../util/git.js";
|
||||
import type { ConfigOption, ModelOption } from "../types/selectorTypes.js";
|
||||
|
||||
import { useConfigSelector } from "./useConfigSelector.js";
|
||||
import { useModelSelector } from "./useModelSelector.js";
|
||||
import { useOrganizationSelector } from "./useOrganizationSelector.js";
|
||||
|
||||
// Helper function to get repo URL text
|
||||
export function getRepoUrlText(remoteUrl?: string): string {
|
||||
let url = remoteUrl ?? "";
|
||||
if (!url) {
|
||||
const isGit = isGitRepo();
|
||||
if (isGit) {
|
||||
const gitUrl = getGitRemoteUrl();
|
||||
if (gitUrl) {
|
||||
url = gitUrl;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!url) {
|
||||
url = process.cwd();
|
||||
}
|
||||
|
||||
url = url.replace(/\.git$/, "");
|
||||
url = url.replace(/^(https|http):\/\/.*?\//, "");
|
||||
return url;
|
||||
}
|
||||
|
||||
// Custom hook for intro message
|
||||
export function useIntroMessage(
|
||||
isRemoteMode: boolean,
|
||||
services: any,
|
||||
allServicesReady: boolean,
|
||||
) {
|
||||
const [showIntroMessage, setShowIntroMessage] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const shouldShow =
|
||||
!isRemoteMode &&
|
||||
allServicesReady &&
|
||||
!!services.config?.config &&
|
||||
!!services.model?.model &&
|
||||
!!services.mcp?.mcpService;
|
||||
|
||||
setShowIntroMessage(shouldShow);
|
||||
}, [
|
||||
isRemoteMode,
|
||||
allServicesReady,
|
||||
services.config?.config,
|
||||
services.model?.model,
|
||||
services.mcp?.mcpService,
|
||||
]);
|
||||
|
||||
return [showIntroMessage, setShowIntroMessage] as const;
|
||||
}
|
||||
|
||||
// Custom hook for login handling
|
||||
export function useLoginHandlers(
|
||||
navigateTo: any,
|
||||
navState: any,
|
||||
closeCurrentScreen: () => void,
|
||||
) {
|
||||
const handleLoginPrompt = useCallback(
|
||||
(promptText: string): Promise<string> => {
|
||||
return new Promise((resolve) => {
|
||||
navigateTo("login", { text: promptText, resolve });
|
||||
});
|
||||
},
|
||||
[navigateTo],
|
||||
);
|
||||
|
||||
const handleLoginTokenSubmit = useCallback(
|
||||
(token: string) => {
|
||||
if (navState.screenData?.resolve) {
|
||||
navState.screenData.resolve(token);
|
||||
closeCurrentScreen();
|
||||
}
|
||||
},
|
||||
[navState.screenData, closeCurrentScreen],
|
||||
);
|
||||
|
||||
return { handleLoginPrompt, handleLoginTokenSubmit };
|
||||
}
|
||||
|
||||
// Custom hook for mode tracking
|
||||
export function useCurrentMode() {
|
||||
const [currentMode, setCurrentMode] = useState<PermissionMode>(
|
||||
modeService.getCurrentMode(),
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
const handleModeChange = (newMode: PermissionMode) => {
|
||||
setCurrentMode(newMode);
|
||||
};
|
||||
|
||||
modeService.on("modeChanged", handleModeChange);
|
||||
return () => {
|
||||
modeService.off("modeChanged", handleModeChange);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return currentMode;
|
||||
}
|
||||
|
||||
// Custom hook to combine all selector logic
|
||||
export function useSelectors(
|
||||
configPath: string | undefined,
|
||||
setMessages: React.Dispatch<React.SetStateAction<any[]>>,
|
||||
resetChatHistory: () => void,
|
||||
): {
|
||||
handleOrganizationSelect: (
|
||||
organizationId: string | null,
|
||||
organizationName: string,
|
||||
) => Promise<void>;
|
||||
handleConfigSelect: (config: ConfigOption) => Promise<void>;
|
||||
handleModelSelect: (model: ModelOption) => Promise<void>;
|
||||
} {
|
||||
const { handleOrganizationSelect } = useOrganizationSelector({
|
||||
onMessage: (message) => {
|
||||
setMessages((prev) => [...prev, message]);
|
||||
},
|
||||
onChatReset: resetChatHistory,
|
||||
});
|
||||
|
||||
const { handleConfigSelect } = useConfigSelector({
|
||||
configPath,
|
||||
onMessage: (message) => {
|
||||
setMessages((prev) => [...prev, message]);
|
||||
},
|
||||
onChatReset: resetChatHistory,
|
||||
});
|
||||
|
||||
const { handleModelSelect } = useModelSelector({
|
||||
onMessage: (message) => {
|
||||
setMessages((prev) => [...prev, message]);
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
handleOrganizationSelect,
|
||||
handleConfigSelect,
|
||||
handleModelSelect,
|
||||
};
|
||||
}
|
||||
17
src/ui/types/selectorTypes.ts
Normal file
17
src/ui/types/selectorTypes.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
/**
|
||||
* Type definitions for selectors - compatible with existing hook interfaces
|
||||
*/
|
||||
|
||||
import type { SelectorOption } from "../Selector.js";
|
||||
|
||||
export interface ConfigOption extends SelectorOption {
|
||||
type: "local" | "assistant" | "create";
|
||||
slug?: string;
|
||||
}
|
||||
|
||||
export interface ModelOption {
|
||||
id: string;
|
||||
name: string;
|
||||
index: number;
|
||||
provider: string;
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue