@@ -25,6 +25,7 @@ interface PromptInputProps {
2525 currentParams ?: ModelParams ;
2626 currentImagenParams ?: ImagenModelParams ;
2727 selectedFile : File | null ;
28+ onCancel ?: ( ) => void ; // New prop for cancellation
2829}
2930
3031const PromptInput : React . FC < PromptInputProps > = ( {
@@ -39,6 +40,7 @@ const PromptInput: React.FC<PromptInputProps> = ({
3940 aiInstance,
4041 currentParams,
4142 selectedFile,
43+ onCancel,
4244} ) => {
4345 const [ tokenCount , setTokenCount ] = useState < CountTokensResponse | null > (
4446 null ,
@@ -47,7 +49,6 @@ const PromptInput: React.FC<PromptInputProps> = ({
4749 const [ tokenCountError , setTokenCountError ] = useState < string | null > ( null ) ;
4850
4951 const handleCountTokensClick = useCallback ( async ( ) => {
50- // Prevent counting if main action is loading or already counting
5152 if ( isLoading || isCountingTokens ) return ;
5253
5354 setIsCountingTokens ( true ) ;
@@ -57,12 +58,10 @@ const PromptInput: React.FC<PromptInputProps> = ({
5758 const currentPromptText = prompt . trim ( ) ;
5859 const parts : Part [ ] = [ ] ;
5960
60- // Add text part if present
6161 if ( currentPromptText ) {
6262 parts . push ( { text : currentPromptText } ) ;
6363 }
6464
65- // Add file part if present
6665 if ( selectedFile ) {
6766 try {
6867 const filePart = await fileToGenerativePart ( selectedFile ) ;
@@ -75,7 +74,6 @@ const PromptInput: React.FC<PromptInputProps> = ({
7574 }
7675 }
7776
78- // Don't count if there's nothing to count
7977 if ( parts . length === 0 ) {
8078 setTokenCountError ( "Nothing to count tokens for (no text or file)." ) ;
8179 setIsCountingTokens ( false ) ;
@@ -104,7 +102,7 @@ const PromptInput: React.FC<PromptInputProps> = ({
104102 } catch ( err : unknown ) {
105103 console . error ( "Error counting tokens:" , err ) ;
106104 setTokenCountError ( `Token count failed` ) ;
107- setTokenCount ( null ) ; // Clear previous count on error
105+ setTokenCount ( null ) ;
108106 } finally {
109107 setIsCountingTokens ( false ) ;
110108 }
@@ -117,6 +115,14 @@ const PromptInput: React.FC<PromptInputProps> = ({
117115 isCountingTokens ,
118116 ] ) ;
119117
118+ const handlePrimaryAction = ( ) => {
119+ if ( isLoading && onCancel ) {
120+ onCancel ( ) ;
121+ } else {
122+ onSubmit ( ) ;
123+ }
124+ } ;
125+
120126 return (
121127 < div className = { styles . promptContainer } >
122128 { /* Suggestions */ }
@@ -127,7 +133,7 @@ const PromptInput: React.FC<PromptInputProps> = ({
127133 key = { index }
128134 className = { styles . suggestionButton }
129135 onClick = { ( ) => onSuggestionClick ( suggestion ) }
130- disabled = { isLoading } // Disable buttons while loading
136+ disabled = { isLoading || isCountingTokens }
131137 >
132138 { suggestion }
133139 </ button >
@@ -142,17 +148,21 @@ const PromptInput: React.FC<PromptInputProps> = ({
142148 value = { prompt }
143149 onChange = { ( e ) => onPromptChange ( e . target . value ) }
144150 placeholder = { placeholder }
145- disabled = { isLoading || isCountingTokens } // Disable if main loading OR counting
151+ disabled = { isLoading || isCountingTokens }
146152 rows = { 3 }
147153 aria-label = "Prompt input"
148154 />
149155 < button
150156 className = { styles . runButton }
151- onClick = { onSubmit }
152- disabled = { isLoading || isCountingTokens || ! prompt . trim ( ) } // Disable if loading, counting, or empty
153- aria-label = "Submit prompt"
157+ onClick = { handlePrimaryAction }
158+ disabled = {
159+ isLoading
160+ ? false // Cancel button is always enabled when loading
161+ : isCountingTokens || ( ! prompt . trim ( ) && ! selectedFile )
162+ }
163+ aria-label = { isLoading ? "Cancel prompt" : "Submit prompt" }
154164 >
155- { isLoading ? "Running... " : "Run ➤" }
165+ { isLoading ? "Cancel ✕ " : "Run ➤" }
156166 </ button >
157167 </ div >
158168
0 commit comments