fix: Terminal阻塞修复—cmd state+onPress+fetch /tools/exec

- IIFE内useState: cmd/out/running
- Run按钮→POST /tools/exec→显示stdout/stderr
- onSubmitEditing键盘发送
- loading disabled反馈
This commit is contained in:
LukeMackin
2026-07-22 17:53:44 +08:00
parent c407f6c483
commit 02967ae5c3

View File

@@ -130,25 +130,36 @@ export default function App() {
{/* ── Terminal Tab ── */} {/* ── Terminal Tab ── */}
{tab === 'terminal' && ( {tab === 'terminal' && (
<View style={styles.chatContainer}> (() => {
<View style={styles.termOutput}> const [cmd, setCmd] = useState('');
<ScrollView> const [out, setOut] = useState('Execute remote commands via Gateway MCP.\nType a shell command and press Execute.\n');
<Text style={styles.termText} selectable>Execute remote commands via Gateway MCP.{'\n'}Type a shell command and press Execute.{'\n'}</Text> const [running, setRunning] = useState(false);
</ScrollView> const runCmd = async () => {
</View> if (!cmd.trim() || running) return;
<View style={styles.inputBar}> const c = cmd.trim(); setCmd(''); setRunning(true);
<TextInput setOut(o => o + `$ ${c}\n`);
style={styles.chatInput} try {
placeholder="ls -la /home" const r = await fetch(`${gatewayUrl}/tools/exec`, { method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify({command:c}), signal:AbortSignal.timeout(30000) });
placeholderTextColor="#475569" if (!r.ok) throw new Error(`HTTP ${r.status}`);
autoCapitalize="none" const d = await r.json() as any;
autoCorrect={false} setOut(o => o + (d.stdout||d.stderr||JSON.stringify(d)) + '\n');
/> } catch(e:any) { setOut(o => o + `Error: ${e.message}\n`); }
<TouchableOpacity style={styles.sendBtn}> finally { setRunning(false); }
<Text style={styles.sendBtnText}>Run</Text> };
</TouchableOpacity> return (
</View> <View style={styles.chatContainer}>
</View> <ScrollView style={styles.termOutput}>
<Text style={styles.termText} selectable>{out}</Text>
</ScrollView>
<View style={styles.inputBar}>
<TextInput style={styles.chatInput} value={cmd} onChangeText={setCmd} placeholder="ls -la /home" placeholderTextColor="#475569" autoCapitalize="none" autoCorrect={false} onSubmitEditing={runCmd} returnKeyType="send" />
<TouchableOpacity style={[styles.sendBtn, running && styles.sendBtnDisabled]} onPress={runCmd} disabled={running}>
<Text style={styles.sendBtnText}>{running ? '...' : 'Run'}</Text>
</TouchableOpacity>
</View>
</View>
);
})()
)} )}
{/* ── Settings Tab ── */} {/* ── Settings Tab ── */}