import { useEffect, useRef, useState } from 'react'; import { AppState, Button, KeyboardAvoidingView, Platform, ScrollView, StatusBar, StyleSheet, Text, TextInput, View, } from 'react-native'; import { SafeAreaProvider, SafeAreaView } from 'react-native-safe-area-context'; import { buildStartTurn, newCommandId } from '@/contracts/parse/turn-command'; import { TurnRuntimeClient, type RuntimeConnectionState, } from '@/features/chat/transport/TurnRuntimeClient'; type Message = { id: string; question: string; answer: string }; type Connection = { origin: string; status: 'login' | 'chat'; }; const defaultAddress = __DEV__ ? Platform.OS === 'android' ? 'http://10.0.2.2:18001' : 'http://localhost:18001' : ''; function serverOrigin(address: string): string { const match = /^(https?):\/\/([^/?#\s@]+)\/?$/i.exec(address.trim()); if (!match) { throw new Error( 'Enter the server address, for example https://tutor.example.com.', ); } const [, protocol, host] = match; const local = /^(localhost|127\.0\.0\.1|10\.0\.2\.2)(:\d+)?$/i.test(host); if (protocol.toLowerCase() !== 'https' && !(__DEV__ && local)) { throw new Error('Use HTTPS to connect to this server.'); } return `${protocol.toLowerCase()}://${host}`; } function App() { return ( ); } function DeepTutor() { const [address, setAddress] = useState(defaultAddress); const [connection, setConnection] = useState(null); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [checking, setChecking] = useState(false); const [error, setError] = useState(''); async function connect(signIn: boolean) { setChecking(true); setError(''); try { const origin = serverOrigin(address); if (signIn) { const login = await fetch(`${origin}/api/auth/login`, { method: 'POST', credentials: 'include', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: username.trim(), password }), }); if (login.status === 401) throw new Error('Check your username and password.'); if (!login.ok) throw new Error(`Sign-in failed (HTTP ${login.status}).`); } const response = await fetch(`${origin}/api/auth/status`, { credentials: 'include', }); if (!response.ok) throw new Error(`Server check failed (HTTP ${response.status}).`); const status = await response.json(); if ( typeof status.enabled !== 'boolean' || typeof status.authenticated !== 'boolean' ) { throw new Error('This server did not return a DeepTutor session.'); } const needsLogin = status.enabled && !status.authenticated; setConnection({ origin, status: needsLogin ? 'login' : 'chat' }); if (signIn && needsLogin) throw new Error('The server session could not be established.'); } catch (reason) { setError(reason instanceof Error ? reason.message : 'Unable to connect.'); } finally { setChecking(false); if (signIn) setPassword(''); } } if (connection?.status === 'chat') { return ( setConnection(null)} /> ); } const showLogin = connection?.status === 'login'; return ( DeepTutor Server address { setAddress(value); setConnection(null); setPassword(''); setError(''); }} autoCapitalize="none" autoCorrect={false} keyboardType="url" editable={!checking} placeholder="https://tutor.example.com" /> {showLogin && ( <> )}