@@ -6,6 +6,8 @@ import { NativeHeaderToolbar, NativeStackScreenOptions } from "../../native/Stac
66import { StackActions , useNavigation , type StaticScreenProps } from "@react-navigation/native" ;
77import { useCallback , useEffect , useMemo , useRef , useState } from "react" ;
88import { Platform , Pressable , View } from "react-native" ;
9+ import * as Clipboard from "expo-clipboard" ;
10+ import * as Schema from "effect/Schema" ;
911import {
1012 KeyboardController ,
1113 KeyboardEvents ,
@@ -27,6 +29,7 @@ import { environmentCatalog } from "../../connection/catalog";
2729import { useEnvironmentPresentation } from "../../state/presentation" ;
2830import { terminalEnvironment } from "../../state/terminal" ;
2931import { useAtomCommand } from "../../state/use-atom-command" ;
32+ import { useServerConfigs } from "../../state/entities" ;
3033import { useWorkspaceState } from "../../state/workspace" ;
3134import {
3235 MAX_TERMINAL_FONT_SIZE ,
@@ -65,19 +68,33 @@ import {
6568 resolveTerminalSessionLabel ,
6669 type TerminalMenuSession ,
6770} from "./terminalMenu" ;
71+ import {
72+ hostPlatformFromOs ,
73+ resolveModifiedTerminalInput ,
74+ type HostPlatform ,
75+ type PendingModifier ,
76+ } from "./terminalInput" ;
77+ import { createTerminalPasteSession } from "./terminalPaste" ;
6878import { cacheTerminalGridSize , getCachedTerminalGridSize } from "./terminalUiState" ;
6979
7080const DEFAULT_TERMINAL_COLS = 80 ;
7181const DEFAULT_TERMINAL_ROWS = 24 ;
7282const TERMINAL_ACCESSORY_HEIGHT = 52 ;
7383const SHOWCASE_ENABLED = process . env . EXPO_PUBLIC_SHOWCASE === "1" ;
7484
75- type PendingModifier = "ctrl" | "meta" ;
76- type HostPlatform = "mac" | "linux" | "windows" | "unknown" ;
85+ class TerminalClipboardReadError extends Schema . TaggedErrorClass < TerminalClipboardReadError > ( ) (
86+ "TerminalClipboardReadError" ,
87+ { terminalId : Schema . String , cause : Schema . Defect ( ) } ,
88+ ) {
89+ override get message ( ) : string {
90+ return `Failed to read the clipboard for a paste into terminal ${ this . terminalId } .` ;
91+ }
92+ }
7793
7894type TerminalToolbarAction =
7995 | { readonly kind : "send" ; readonly key : string ; readonly label : string ; readonly data : string }
8096 | { readonly kind : "clear" ; readonly key : string ; readonly label : string }
97+ | { readonly kind : "paste" ; readonly key : string ; readonly label : string }
8198 | {
8299 readonly kind : "modifier" ;
83100 readonly key : string ;
@@ -114,28 +131,6 @@ function inferHostPlatform(environmentLabel: string | null): HostPlatform {
114131 return "unknown" ;
115132}
116133
117- function applyCtrlModifier ( input : string ) : string {
118- const firstCharacter = input [ 0 ] ;
119- if ( ! firstCharacter ) {
120- return input ;
121- }
122-
123- const lowerCharacter = firstCharacter . toLowerCase ( ) ;
124- if ( lowerCharacter >= "a" && lowerCharacter <= "z" ) {
125- return String . fromCharCode ( lowerCharacter . charCodeAt ( 0 ) - 96 ) ;
126- }
127-
128- if ( firstCharacter === "@" ) return "\u0000" ;
129- if ( firstCharacter === "[" ) return "\u001b" ;
130- if ( firstCharacter === "\\" ) return "\u001c" ;
131- if ( firstCharacter === "]" ) return "\u001d" ;
132- if ( firstCharacter === "^" ) return "\u001e" ;
133- if ( firstCharacter === "_" ) return "\u001f" ;
134- if ( firstCharacter === "?" ) return "\u007f" ;
135-
136- return input ;
137- }
138-
139134function pickRunningTerminalSessionForBootstrap (
140135 sessions : ReadonlyArray < KnownTerminalSession > ,
141136) : KnownTerminalSession | null {
@@ -462,9 +457,17 @@ export function ThreadTerminalRouteScreen(props: ThreadTerminalRouteScreenProps)
462457 } ) ;
463458 } , [ terminal . buffer , terminal . buffer . length , terminalKey ] ) ;
464459 const cwd = terminal . summary ?. cwd ?? selectedThreadProject ?. workspaceRoot ?? null ;
460+ const serverConfigs = useServerConfigs ( ) ;
461+ const hostOs =
462+ routeEnvironmentId === null
463+ ? null
464+ : ( serverConfigs . get ( routeEnvironmentId ) ?. environment . platform . os ?? null ) ;
465+ // The descriptor is authoritative; the label is only a hint until it arrives.
465466 const hostPlatform = useMemo (
466- ( ) => inferHostPlatform ( selectedEnvironmentConnection ?. environmentLabel ?? null ) ,
467- [ selectedEnvironmentConnection ?. environmentLabel ] ,
467+ ( ) =>
468+ hostPlatformFromOs ( hostOs ) ??
469+ inferHostPlatform ( selectedEnvironmentConnection ?. environmentLabel ?? null ) ,
470+ [ hostOs , selectedEnvironmentConnection ?. environmentLabel ] ,
468471 ) ;
469472
470473 const terminalTheme = getMobileTerminalTheme ( themeId , appearanceScheme ) ;
@@ -488,6 +491,7 @@ export function ThreadTerminalRouteScreen(props: ThreadTerminalRouteScreenProps)
488491 { kind : "send" , key : "esc" , label : "esc" , data : "\u001b" } ,
489492 ...modifierActions ,
490493 { kind : "send" , key : "tab" , label : "tab" , data : "\t" } ,
494+ { kind : "paste" , key : "paste" , label : "paste" } ,
491495 { kind : "clear" , key : "clear" , label : "clear" } ,
492496 { kind : "send" , key : "up" , label : "↑" , data : "\u001b[A" } ,
493497 { kind : "send" , key : "down" , label : "↓" , data : "\u001b[B" } ,
@@ -693,41 +697,82 @@ export function ThreadTerminalRouteScreen(props: ThreadTerminalRouteScreenProps)
693697 setHasMeasuredSurface ( true ) ;
694698 } , [ routeEnvironmentId , routeThreadId , terminalId ] ) ;
695699
700+ /** Resolves true once the pty accepted the write, false if it was skipped or rejected. */
696701 const writeInput = useCallback (
697- ( data : string ) => {
702+ async ( data : string ) : Promise < boolean > => {
698703 if ( ! selectedThread || ! isRunning ) {
699- return ;
704+ return false ;
700705 }
701706
702- void writeTerminal ( {
707+ const result = await writeTerminal ( {
703708 environmentId : selectedThread . environmentId ,
704709 input : {
705710 threadId : selectedThread . id ,
706711 terminalId,
707712 data,
708713 } ,
709714 } ) ;
715+ return result . _tag === "Success" ;
710716 } ,
711717 [ isRunning , selectedThread , terminalId , writeTerminal ] ,
712718 ) ;
713719
720+ const pasteSessionRef = useRef < ReturnType < typeof createTerminalPasteSession > | null > ( null ) ;
721+ if ( pasteSessionRef . current === null ) {
722+ pasteSessionRef . current = createTerminalPasteSession ( ) ;
723+ }
724+ const pasteSession = pasteSessionRef . current ;
725+
726+ // Drop delayed clipboard reads whenever the route or attached pty changes.
727+ useEffect ( ( ) => {
728+ pasteSession . reset ( isRunning ) ;
729+ return ( ) => {
730+ pasteSession . reset ( false ) ;
731+ } ;
732+ } , [ isRunning , pasteSession , terminal . lifecycleVersion , terminalKey ] ) ;
733+
734+ const pasteFromClipboard = useCallback ( async ( ) => {
735+ await pasteSession . paste ( {
736+ readText : Clipboard . getStringAsync ,
737+ write : writeInput ,
738+ onReadError : ( cause ) => {
739+ console . error ( new TerminalClipboardReadError ( { terminalId, cause } ) ) ;
740+ } ,
741+ } ) ;
742+ } , [ pasteSession , terminalId , writeInput ] ) ;
743+
744+ /** Sends a key through the armed toolbar modifier, if any, and disarms it. */
745+ const writeModifiedInput = useCallback (
746+ ( data : string ) => {
747+ if ( pendingModifier === null ) {
748+ void writeInput ( data ) ;
749+ return ;
750+ }
751+
752+ setPendingModifierState ( { terminalId, value : null } ) ;
753+ const resolved = resolveModifiedTerminalInput ( {
754+ data,
755+ modifier : pendingModifier ,
756+ hostPlatform,
757+ } ) ;
758+ if ( resolved . kind === "paste" ) {
759+ void pasteFromClipboard ( ) ;
760+ return ;
761+ }
762+ void writeInput ( resolved . data ) ;
763+ } ,
764+ [ hostPlatform , pasteFromClipboard , pendingModifier , terminalId , writeInput ] ,
765+ ) ;
766+
714767 const handleInput = useCallback (
715768 ( data : string ) => {
716769 if ( data . length === 0 ) {
717770 return ;
718771 }
719772
720- if ( pendingModifier === "ctrl" ) {
721- setPendingModifierState ( { terminalId, value : null } ) ;
722- writeInput ( applyCtrlModifier ( data ) ) ;
723- } else if ( pendingModifier === "meta" ) {
724- setPendingModifierState ( { terminalId, value : null } ) ;
725- writeInput ( `\u001b${ data } ` ) ;
726- } else {
727- writeInput ( data ) ;
728- }
773+ writeModifiedInput ( data ) ;
729774 } ,
730- [ pendingModifier , terminalId , writeInput ] ,
775+ [ writeModifiedInput ] ,
731776 ) ;
732777
733778 const handleResize = useCallback (
@@ -1021,16 +1066,15 @@ export function ThreadTerminalRouteScreen(props: ThreadTerminalRouteScreenProps)
10211066 return ;
10221067 }
10231068
1024- setPendingModifierState ( { terminalId, value : null } ) ;
1025- if ( pendingModifier === "ctrl" ) {
1026- writeInput ( applyCtrlModifier ( action . data ) ) ;
1027- } else if ( pendingModifier === "meta" ) {
1028- writeInput ( `\u001b${ action . data } ` ) ;
1029- } else {
1030- writeInput ( action . data ) ;
1069+ if ( action . kind === "paste" ) {
1070+ setPendingModifierState ( { terminalId, value : null } ) ;
1071+ void pasteFromClipboard ( ) ;
1072+ return ;
10311073 }
1074+
1075+ writeModifiedInput ( action . data ) ;
10321076 } ,
1033- [ handleClearTerminal , pendingModifier , terminalId , writeInput ] ,
1077+ [ handleClearTerminal , pasteFromClipboard , terminalId , writeModifiedInput ] ,
10341078 ) ;
10351079
10361080 const handleDismissKeyboard = useCallback ( ( ) => {
0 commit comments