Initial commit: Captain Claude Mobile App

- Flutter app with chat and terminal screens
- WebSocket integration for real-time chat
- xterm integration for screen sessions
- Markdown rendering with code blocks
- JWT authentication

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
ARCHITECT
2026-01-16 18:34:02 +00:00
commit 3663e4c622
31 changed files with 2343 additions and 0 deletions

View File

@@ -0,0 +1,244 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/auth_provider.dart';
import '../providers/chat_provider.dart';
import '../widgets/message_bubble.dart';
import '../services/chat_service.dart';
import 'sessions_screen.dart';
class ChatScreen extends StatefulWidget {
const ChatScreen({super.key});
@override
State<ChatScreen> createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
final _messageController = TextEditingController();
final _scrollController = ScrollController();
final _focusNode = FocusNode();
@override
void dispose() {
_messageController.dispose();
_scrollController.dispose();
_focusNode.dispose();
super.dispose();
}
void _sendMessage() {
final message = _messageController.text.trim();
if (message.isEmpty) return;
context.read<ChatProvider>().sendMessage(message);
_messageController.clear();
_scrollToBottom();
}
void _scrollToBottom() {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (_scrollController.hasClients) {
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: const Duration(milliseconds: 300),
curve: Curves.easeOut,
);
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Captain Claude'),
actions: [
// Connection status
Consumer<ChatProvider>(
builder: (context, chat, _) {
final color = switch (chat.connectionState) {
ChatConnectionState.connected => Colors.green,
ChatConnectionState.connecting => Colors.orange,
_ => Colors.red,
};
return Padding(
padding: const EdgeInsets.only(right: 8),
child: Icon(Icons.circle, size: 12, color: color),
);
},
),
// Terminal sessions
IconButton(
icon: const Icon(Icons.terminal),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (_) => const SessionsScreen()),
);
},
tooltip: 'Screen Sessions',
),
// New conversation
IconButton(
icon: const Icon(Icons.add),
onPressed: () {
context.read<ChatProvider>().clearMessages();
},
tooltip: 'New Conversation',
),
// Logout
PopupMenuButton(
itemBuilder: (context) => [
const PopupMenuItem(
value: 'logout',
child: Row(
children: [
Icon(Icons.logout),
SizedBox(width: 8),
Text('Logout'),
],
),
),
],
onSelected: (value) {
if (value == 'logout') {
context.read<AuthProvider>().logout();
}
},
),
],
),
body: Column(
children: [
// Messages
Expanded(
child: Consumer<ChatProvider>(
builder: (context, chat, _) {
if (chat.messages.isEmpty) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.chat_bubble_outline,
size: 64,
color: Colors.grey.shade600,
),
const SizedBox(height: 16),
Text(
'Start a conversation',
style: TextStyle(
color: Colors.grey.shade500,
fontSize: 16,
),
),
],
),
);
}
WidgetsBinding.instance.addPostFrameCallback((_) {
_scrollToBottom();
});
return ListView.builder(
controller: _scrollController,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
itemCount: chat.messages.length,
itemBuilder: (context, index) {
final message = chat.messages[index];
return MessageBubble(message: message);
},
);
},
),
),
// Typing indicator
Consumer<ChatProvider>(
builder: (context, chat, _) {
if (!chat.isTyping) return const SizedBox.shrink();
return Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
alignment: Alignment.centerLeft,
child: Row(
children: [
SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(
strokeWidth: 2,
color: Theme.of(context).colorScheme.primary,
),
),
const SizedBox(width: 8),
Text(
'Captain is typing...',
style: TextStyle(color: Colors.grey.shade500),
),
],
),
);
},
),
// Input
Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: const Color(0xFF2D2D2D),
border: Border(
top: BorderSide(color: Colors.grey.shade800),
),
),
child: SafeArea(
child: Row(
children: [
// Attach file button
IconButton(
icon: const Icon(Icons.attach_file),
onPressed: () {
// TODO: Implement file picker
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('File attachment coming soon')),
);
},
),
// Text input
Expanded(
child: TextField(
controller: _messageController,
focusNode: _focusNode,
decoration: const InputDecoration(
hintText: 'Message Captain Claude...',
border: InputBorder.none,
contentPadding: EdgeInsets.symmetric(
horizontal: 16,
vertical: 12,
),
),
maxLines: 5,
minLines: 1,
textInputAction: TextInputAction.send,
onSubmitted: (_) => _sendMessage(),
),
),
// Send button
Consumer<ChatProvider>(
builder: (context, chat, _) {
return IconButton(
icon: const Icon(Icons.send),
onPressed: chat.isConnected ? _sendMessage : null,
color: Theme.of(context).colorScheme.primary,
);
},
),
],
),
),
),
],
),
);
}
}

View File

@@ -0,0 +1,174 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/auth_provider.dart';
class LoginScreen extends StatefulWidget {
const LoginScreen({super.key});
@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final _usernameController = TextEditingController();
final _passwordController = TextEditingController();
final _formKey = GlobalKey<FormState>();
bool _obscurePassword = true;
@override
void dispose() {
_usernameController.dispose();
_passwordController.dispose();
super.dispose();
}
Future<void> _login() async {
if (!_formKey.currentState!.validate()) return;
final auth = context.read<AuthProvider>();
final success = await auth.login(
_usernameController.text,
_passwordController.text,
);
if (!success && mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(auth.error ?? 'Login failed'),
backgroundColor: Colors.red,
),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Logo/Icon
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.primary.withOpacity(0.2),
borderRadius: BorderRadius.circular(24),
),
child: Icon(
Icons.terminal_rounded,
size: 50,
color: Theme.of(context).colorScheme.primary,
),
),
const SizedBox(height: 24),
// Title
Text(
'Captain Claude',
style: Theme.of(context).textTheme.headlineMedium?.copyWith(
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 8),
Text(
'TZZR Mobile Interface',
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: Colors.grey,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 48),
// Username
TextFormField(
controller: _usernameController,
decoration: const InputDecoration(
labelText: 'Username',
prefixIcon: Icon(Icons.person_outline),
),
textInputAction: TextInputAction.next,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Enter username';
}
return null;
},
),
const SizedBox(height: 16),
// Password
TextFormField(
controller: _passwordController,
decoration: InputDecoration(
labelText: 'Password',
prefixIcon: const Icon(Icons.lock_outline),
suffixIcon: IconButton(
icon: Icon(
_obscurePassword ? Icons.visibility : Icons.visibility_off,
),
onPressed: () {
setState(() {
_obscurePassword = !_obscurePassword;
});
},
),
),
obscureText: _obscurePassword,
textInputAction: TextInputAction.done,
onFieldSubmitted: (_) => _login(),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Enter password';
}
return null;
},
),
const SizedBox(height: 32),
// Login Button
Consumer<AuthProvider>(
builder: (context, auth, _) {
return ElevatedButton(
onPressed: auth.isLoading ? null : _login,
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
backgroundColor: Theme.of(context).colorScheme.primary,
foregroundColor: Colors.white,
),
child: auth.isLoading
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
color: Colors.white,
),
)
: const Text(
'Login',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
);
},
),
],
),
),
),
),
),
);
}
}

View File

@@ -0,0 +1,147 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/auth_provider.dart';
import '../models/session.dart';
import 'terminal_screen.dart';
class SessionsScreen extends StatefulWidget {
const SessionsScreen({super.key});
@override
State<SessionsScreen> createState() => _SessionsScreenState();
}
class _SessionsScreenState extends State<SessionsScreen> {
List<ScreenSession>? _sessions;
bool _isLoading = true;
String? _error;
@override
void initState() {
super.initState();
_loadSessions();
}
Future<void> _loadSessions() async {
setState(() {
_isLoading = true;
_error = null;
});
try {
final auth = context.read<AuthProvider>();
final sessions = await auth.apiService.getSessions();
setState(() {
_sessions = sessions;
_isLoading = false;
});
} catch (e) {
setState(() {
_error = e.toString();
_isLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Screen Sessions'),
actions: [
IconButton(
icon: const Icon(Icons.refresh),
onPressed: _loadSessions,
),
],
),
body: _buildBody(),
);
}
Widget _buildBody() {
if (_isLoading) {
return const Center(child: CircularProgressIndicator());
}
if (_error != null) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.error_outline, size: 48, color: Colors.red.shade400),
const SizedBox(height: 16),
Text(_error!, style: const TextStyle(color: Colors.red)),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _loadSessions,
child: const Text('Retry'),
),
],
),
);
}
if (_sessions == null || _sessions!.isEmpty) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.terminal, size: 64, color: Colors.grey.shade600),
const SizedBox(height: 16),
Text(
'No active screen sessions',
style: TextStyle(color: Colors.grey.shade500, fontSize: 16),
),
],
),
);
}
return RefreshIndicator(
onRefresh: _loadSessions,
child: ListView.builder(
padding: const EdgeInsets.all(16),
itemCount: _sessions!.length,
itemBuilder: (context, index) {
final session = _sessions![index];
return Card(
margin: const EdgeInsets.only(bottom: 12),
child: ListTile(
leading: Container(
width: 48,
height: 48,
decoration: BoxDecoration(
color: session.attached
? Colors.green.withOpacity(0.2)
: Colors.grey.withOpacity(0.2),
borderRadius: BorderRadius.circular(8),
),
child: Icon(
Icons.terminal,
color: session.attached ? Colors.green : Colors.grey,
),
),
title: Text(
session.name,
style: const TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text(
'PID: ${session.pid} - ${session.attached ? "Attached" : "Detached"}',
),
trailing: const Icon(Icons.chevron_right),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => TerminalScreen(sessionName: session.name),
),
);
},
),
);
},
),
);
}
}

View File

@@ -0,0 +1,201 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:xterm/xterm.dart';
import '../providers/auth_provider.dart';
import '../services/terminal_service.dart';
class TerminalScreen extends StatefulWidget {
final String sessionName;
const TerminalScreen({super.key, required this.sessionName});
@override
State<TerminalScreen> createState() => _TerminalScreenState();
}
class _TerminalScreenState extends State<TerminalScreen> {
late Terminal _terminal;
late TerminalService _terminalService;
StreamSubscription? _outputSubscription;
StreamSubscription? _stateSubscription;
final _terminalController = TerminalController();
@override
void initState() {
super.initState();
_terminal = Terminal(
maxLines: 10000,
);
_terminalService = TerminalService();
_connect();
}
void _connect() {
final auth = context.read<AuthProvider>();
_terminalService.setToken(auth.token);
_outputSubscription = _terminalService.output.listen((data) {
_terminal.write(data);
});
_stateSubscription = _terminalService.connectionState.listen((state) {
setState(() {});
if (state == TerminalConnectionState.connected) {
_terminal.write('Connected to ${widget.sessionName}\r\n');
} else if (state == TerminalConnectionState.error) {
_terminal.write('\r\n[Connection Error]\r\n');
}
});
_terminal.onOutput = (data) {
_terminalService.sendInput(data);
};
_terminal.onResize = (width, height, pixelWidth, pixelHeight) {
_terminalService.resize(height, width);
};
_terminalService.connect(widget.sessionName);
}
@override
void dispose() {
_outputSubscription?.cancel();
_stateSubscription?.cancel();
_terminalService.dispose();
_terminalController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.sessionName),
actions: [
// Connection status indicator
Padding(
padding: const EdgeInsets.only(right: 8),
child: Icon(
Icons.circle,
size: 12,
color: switch (_terminalService.currentState) {
TerminalConnectionState.connected => Colors.green,
TerminalConnectionState.connecting => Colors.orange,
_ => Colors.red,
},
),
),
// Copy button
IconButton(
icon: const Icon(Icons.copy),
onPressed: () {
final selection = _terminalController.selection;
if (selection != null) {
final text = _terminal.buffer.getText(selection);
Clipboard.setData(ClipboardData(text: text));
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Copied to clipboard')),
);
}
},
tooltip: 'Copy Selection',
),
// Paste button
IconButton(
icon: const Icon(Icons.paste),
onPressed: () async {
final data = await Clipboard.getData(Clipboard.kTextPlain);
if (data?.text != null) {
_terminalService.sendInput(data!.text!);
}
},
tooltip: 'Paste',
),
// Reconnect button
IconButton(
icon: const Icon(Icons.refresh),
onPressed: () {
_terminalService.disconnect();
_terminal.write('\r\n[Reconnecting...]\r\n');
_connect();
},
tooltip: 'Reconnect',
),
],
),
body: SafeArea(
child: TerminalView(
_terminal,
controller: _terminalController,
textStyle: const TerminalStyle(
fontSize: 13,
fontFamily: 'JetBrainsMono',
),
theme: const TerminalTheme(
cursor: Color(0xFFD97706),
selection: Color(0x40D97706),
foreground: Color(0xFFE5E5E5),
background: Color(0xFF1A1A1A),
black: Color(0xFF1A1A1A),
red: Color(0xFFEF4444),
green: Color(0xFF22C55E),
yellow: Color(0xFFEAB308),
blue: Color(0xFF3B82F6),
magenta: Color(0xFFA855F7),
cyan: Color(0xFF06B6D4),
white: Color(0xFFE5E5E5),
brightBlack: Color(0xFF6B7280),
brightRed: Color(0xFFF87171),
brightGreen: Color(0xFF4ADE80),
brightYellow: Color(0xFFFDE047),
brightBlue: Color(0xFF60A5FA),
brightMagenta: Color(0xFFC084FC),
brightCyan: Color(0xFF22D3EE),
brightWhite: Color(0xFFFFFFFF),
searchHitBackground: Color(0xFFD97706),
searchHitBackgroundCurrent: Color(0xFFEF4444),
searchHitForeground: Color(0xFF1A1A1A),
),
autofocus: true,
alwaysShowCursor: true,
),
),
// Quick action buttons for mobile
bottomNavigationBar: Container(
height: 48,
color: const Color(0xFF2D2D2D),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
_buildQuickButton('Ctrl+C', () => _terminalService.sendInput('\x03')),
_buildQuickButton('Ctrl+D', () => _terminalService.sendInput('\x04')),
_buildQuickButton('Tab', () => _terminalService.sendInput('\t')),
_buildQuickButton('Esc', () => _terminalService.sendInput('\x1b')),
_buildQuickButton('Up', () => _terminalService.sendInput('\x1b[A')),
_buildQuickButton('Down', () => _terminalService.sendInput('\x1b[B')),
],
),
),
);
}
Widget _buildQuickButton(String label, VoidCallback onTap) {
return InkWell(
onTap: onTap,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
child: Text(
label,
style: TextStyle(
color: Colors.grey.shade400,
fontSize: 12,
fontWeight: FontWeight.w500,
),
),
),
);
}
}