449 lines
14 KiB
Dart
449 lines
14 KiB
Dart
// The new-session draft. Nothing exists on the server until the first message
|
|||
|
|
// creates the session; this screen only holds the project, the model and what
|
||
|
|
// has been typed.
|
||
|
|
import 'package:flutter/material.dart';
|
||
|
|
import 'package:flutter/services.dart';
|
||
|
|
|
||
|
|
import '../api/models.dart';
|
||
|
|
import '../state.dart';
|
||
|
|
import '../widgets/composer_attachment.dart';
|
||
|
|
import '../widgets/model_picker.dart';
|
||
|
|
import 'files_panel.dart';
|
||
|
|
|
||
|
|
class NewSessionScreen extends StatefulWidget {
|
||
|
|
final AppState app;
|
||
|
|
|
||
|
|
/// Opens the session drawer; null where the list is always on screen.
|
||
|
|
final VoidCallback? onOpenSessions;
|
||
|
|
|
||
|
|
const NewSessionScreen({
|
||
|
|
super.key,
|
||
|
|
required this.app,
|
||
|
|
this.onOpenSessions,
|
||
|
|
});
|
||
|
|
|
||
|
|
@override
|
||
|
|
State<NewSessionScreen> createState() => _NewSessionScreenState();
|
||
|
|
}
|
||
|
|
|
||
|
|
class _NewSessionScreenState extends State<NewSessionScreen> {
|
||
|
|
SessionDraft get _draft => widget.app.draft!;
|
||
|
|
final TextEditingController _text = TextEditingController();
|
||
|
|
final FocusNode _inputFocus = FocusNode();
|
||
|
|
List<ComposerAttachment> _attachments = [];
|
||
|
|
bool _attaching = false;
|
||
|
|
|
||
|
|
@override
|
||
|
|
void initState() {
|
||
|
|
super.initState();
|
||
|
|
_text.text = _draft.text;
|
||
|
|
_text.addListener(_syncText);
|
||
|
|
_attachments = [];
|
||
|
|
_draft.attachments = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
void dispose() {
|
||
|
|
_text.removeListener(_syncText);
|
||
|
|
_text.dispose();
|
||
|
|
_inputFocus.dispose();
|
||
|
|
super.dispose();
|
||
|
|
}
|
||
|
|
|
||
|
|
void _syncText() {
|
||
|
|
if (_draft.text == _text.text) return;
|
||
|
|
widget.app.updateDraftText(_text.text);
|
||
|
|
}
|
||
|
|
|
||
|
|
bool get _canSend =>
|
||
|
|
_text.text.trim().isNotEmpty || _attachments.isNotEmpty;
|
||
|
|
|
||
|
|
Future<void> _send() async {
|
||
|
|
if (!_canSend || widget.app.sendingDraft) return;
|
||
|
|
final text = _text.text.trim();
|
||
|
|
final files = [
|
||
|
|
for (final a in _attachments) {'uri': a.dataUrl, 'name': a.name}
|
||
|
|
];
|
||
|
|
final images = [
|
||
|
|
for (final a in _attachments)
|
||
|
|
if (a.isImage && a.path != null) a.path!
|
||
|
|
];
|
||
|
|
FocusScope.of(context).unfocus();
|
||
|
|
await widget.app.sendDraft(text: text, files: files, images: images);
|
||
|
|
if (!mounted) return;
|
||
|
|
final draft = widget.app.draft;
|
||
|
|
if (draft != null) {
|
||
|
|
// The create failed: the draft is still here, keep what was typed.
|
||
|
|
_text.text = draft.text;
|
||
|
|
} else {
|
||
|
|
HapticFeedback.lightImpact();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<void> _pickFiles() async {
|
||
|
|
setState(() => _attaching = true);
|
||
|
|
try {
|
||
|
|
final picked = await pickComposerAttachments(context);
|
||
|
|
if (picked.isEmpty) return;
|
||
|
|
setState(() {
|
||
|
|
for (final file in picked) {
|
||
|
|
if (_attachments.length >= 5) break;
|
||
|
|
_attachments.add(file);
|
||
|
|
}
|
||
|
|
_draft.attachments = _attachments.length;
|
||
|
|
});
|
||
|
|
widget.app.touch();
|
||
|
|
} catch (e) {
|
||
|
|
if (mounted) {
|
||
|
|
ScaffoldMessenger.of(context)
|
||
|
|
.showSnackBar(SnackBar(content: Text('选择失败:$e')));
|
||
|
|
}
|
||
|
|
} finally {
|
||
|
|
if (mounted) setState(() => _attaching = false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void _removeAttachment(int index) {
|
||
|
|
setState(() {
|
||
|
|
_attachments.removeAt(index);
|
||
|
|
_draft.attachments = _attachments.length;
|
||
|
|
});
|
||
|
|
widget.app.touch();
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
Widget build(BuildContext context) {
|
||
|
|
final app = widget.app;
|
||
|
|
final theme = Theme.of(context);
|
||
|
|
final scheme = theme.colorScheme;
|
||
|
|
return ListenableBuilder(
|
||
|
|
listenable: app,
|
||
|
|
builder: (context, _) {
|
||
|
|
final draft = app.draft;
|
||
|
|
if (draft == null) return const SizedBox.shrink();
|
||
|
|
return Scaffold(
|
||
|
|
appBar: AppBar(
|
||
|
|
leading: widget.onOpenSessions == null
|
||
|
|
? null
|
||
|
|
: IconButton(
|
||
|
|
icon: const Icon(Icons.menu),
|
||
|
|
tooltip: '会话',
|
||
|
|
onPressed: widget.onOpenSessions,
|
||
|
|
),
|
||
|
|
title: const Text('新会话'),
|
||
|
|
actions: [
|
||
|
|
IconButton(
|
||
|
|
icon: const Icon(Icons.folder_open_outlined),
|
||
|
|
tooltip: '文件',
|
||
|
|
onPressed: () => showFilesPanel(context, app, draft.directory),
|
||
|
|
),
|
||
|
|
],
|
||
|
|
),
|
||
|
|
body: Column(
|
||
|
|
children: [
|
||
|
|
Expanded(
|
||
|
|
child: Center(
|
||
|
|
child: Padding(
|
||
|
|
padding: const EdgeInsets.all(32),
|
||
|
|
child: Text(
|
||
|
|
'我们今天要做什么?',
|
||
|
|
textAlign: TextAlign.center,
|
||
|
|
style: TextStyle(
|
||
|
|
fontSize: 26,
|
||
|
|
fontWeight: FontWeight.w600,
|
||
|
|
color: scheme.onSurface,
|
||
|
|
),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
Padding(
|
||
|
|
padding: const EdgeInsets.fromLTRB(12, 0, 12, 6),
|
||
|
|
child: Column(
|
||
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||
|
|
children: [
|
||
|
|
if (draft.error != null)
|
||
|
|
Padding(
|
||
|
|
padding: const EdgeInsets.only(bottom: 6),
|
||
|
|
child: Text(
|
||
|
|
draft.error!,
|
||
|
|
style: TextStyle(
|
||
|
|
fontSize: 12.5, color: scheme.error),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
Row(
|
||
|
|
children: [
|
||
|
|
_projectChip(draft),
|
||
|
|
const Spacer(),
|
||
|
|
_modelChip(draft),
|
||
|
|
],
|
||
|
|
),
|
||
|
|
const SizedBox(height: 6),
|
||
|
|
if (_attachments.isNotEmpty)
|
||
|
|
AttachmentTray(
|
||
|
|
items: _attachments,
|
||
|
|
onRemove: _removeAttachment,
|
||
|
|
),
|
||
|
|
_composer(theme),
|
||
|
|
],
|
||
|
|
),
|
||
|
|
),
|
||
|
|
],
|
||
|
|
),
|
||
|
|
);
|
||
|
|
},
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
Widget _projectChip(SessionDraft draft) {
|
||
|
|
final theme = Theme.of(context);
|
||
|
|
final label = widget.app.projectLabelFor(draft.directory);
|
||
|
|
return InkWell(
|
||
|
|
borderRadius: BorderRadius.circular(18),
|
||
|
|
onTap: _pickProject,
|
||
|
|
child: Padding(
|
||
|
|
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||
|
|
child: Row(
|
||
|
|
mainAxisSize: MainAxisSize.min,
|
||
|
|
children: [
|
||
|
|
Icon(Icons.folder_outlined,
|
||
|
|
size: 15, color: theme.hintColor),
|
||
|
|
const SizedBox(width: 5),
|
||
|
|
ConstrainedBox(
|
||
|
|
constraints: const BoxConstraints(maxWidth: 170),
|
||
|
|
child: Text(
|
||
|
|
label.isEmpty ? '选择项目' : label,
|
||
|
|
maxLines: 1,
|
||
|
|
overflow: TextOverflow.ellipsis,
|
||
|
|
style: TextStyle(fontSize: 12.5, color: theme.hintColor),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
Icon(Icons.expand_more, size: 16, color: theme.hintColor),
|
||
|
|
],
|
||
|
|
),
|
||
|
|
),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
Widget _modelChip(SessionDraft draft) {
|
||
|
|
final app = widget.app;
|
||
|
|
final model = draft.model == null
|
||
|
|
? '模型'
|
||
|
|
: app.modelName(draft.model!);
|
||
|
|
final label =
|
||
|
|
draft.agent.isEmpty ? model : '$model · ${draft.agent}';
|
||
|
|
return InkWell(
|
||
|
|
borderRadius: BorderRadius.circular(18),
|
||
|
|
onTap: () => showModelPickerSheet(
|
||
|
|
context,
|
||
|
|
app: app,
|
||
|
|
kind: 'model',
|
||
|
|
model: draft.model,
|
||
|
|
agent: draft.agent,
|
||
|
|
onModel: (m) => app.updateDraftModel(m),
|
||
|
|
onAgent: (a) => app.updateDraftModel(null, agent: a),
|
||
|
|
),
|
||
|
|
child: Padding(
|
||
|
|
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||
|
|
child: Row(
|
||
|
|
mainAxisSize: MainAxisSize.min,
|
||
|
|
children: [
|
||
|
|
Icon(Icons.smart_toy_outlined,
|
||
|
|
size: 15, color: Theme.of(context).hintColor),
|
||
|
|
const SizedBox(width: 5),
|
||
|
|
ConstrainedBox(
|
||
|
|
constraints: const BoxConstraints(maxWidth: 170),
|
||
|
|
child: Text(
|
||
|
|
label,
|
||
|
|
maxLines: 1,
|
||
|
|
overflow: TextOverflow.ellipsis,
|
||
|
|
style: TextStyle(
|
||
|
|
fontSize: 12.5, color: Theme.of(context).hintColor),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
],
|
||
|
|
),
|
||
|
|
),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
Widget _composer(ThemeData theme) {
|
||
|
|
final scheme = theme.colorScheme;
|
||
|
|
final sending = widget.app.sendingDraft;
|
||
|
|
return Container(
|
||
|
|
padding: const EdgeInsets.all(6),
|
||
|
|
decoration: BoxDecoration(
|
||
|
|
color: scheme.surfaceContainerHighest,
|
||
|
|
borderRadius: BorderRadius.circular(26),
|
||
|
|
),
|
||
|
|
child: Row(
|
||
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
||
|
|
children: [
|
||
|
|
_roundKey(
|
||
|
|
icon: Icons.attach_file,
|
||
|
|
label: '添加附件',
|
||
|
|
onTap: _pickFiles,
|
||
|
|
busy: _attaching,
|
||
|
|
),
|
||
|
|
const SizedBox(width: 4),
|
||
|
|
Expanded(
|
||
|
|
child: TextField(
|
||
|
|
controller: _text,
|
||
|
|
focusNode: _inputFocus,
|
||
|
|
minLines: 1,
|
||
|
|
maxLines: 5,
|
||
|
|
keyboardType: TextInputType.multiline,
|
||
|
|
onChanged: (_) => setState(() {}),
|
||
|
|
decoration: const InputDecoration(
|
||
|
|
isCollapsed: true,
|
||
|
|
border: InputBorder.none,
|
||
|
|
hintText: '规划、构建或提问',
|
||
|
|
contentPadding: EdgeInsets.symmetric(vertical: 8),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
const SizedBox(width: 4),
|
||
|
|
_sendKey(theme, sending),
|
||
|
|
],
|
||
|
|
),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
Widget _roundKey(
|
||
|
|
{required IconData icon,
|
||
|
|
required String label,
|
||
|
|
required VoidCallback onTap,
|
||
|
|
bool busy = false}) {
|
||
|
|
final theme = Theme.of(context);
|
||
|
|
return Tooltip(
|
||
|
|
message: label,
|
||
|
|
child: InkWell(
|
||
|
|
borderRadius: BorderRadius.circular(18),
|
||
|
|
onTap: busy ? null : onTap,
|
||
|
|
child: Container(
|
||
|
|
width: 36,
|
||
|
|
height: 36,
|
||
|
|
alignment: Alignment.center,
|
||
|
|
decoration: BoxDecoration(
|
||
|
|
color: theme.colorScheme.surface,
|
||
|
|
shape: BoxShape.circle,
|
||
|
|
),
|
||
|
|
child: busy
|
||
|
|
? const SizedBox(
|
||
|
|
width: 16,
|
||
|
|
height: 16,
|
||
|
|
child: CircularProgressIndicator(strokeWidth: 2))
|
||
|
|
: Icon(icon, size: 19, color: theme.hintColor),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
Widget _sendKey(ThemeData theme, bool sending) {
|
||
|
|
final scheme = theme.colorScheme;
|
||
|
|
return InkWell(
|
||
|
|
borderRadius: BorderRadius.circular(18),
|
||
|
|
onTap: sending || !_canSend ? null : _send,
|
||
|
|
child: Container(
|
||
|
|
width: 36,
|
||
|
|
height: 36,
|
||
|
|
alignment: Alignment.center,
|
||
|
|
decoration: BoxDecoration(
|
||
|
|
color: _canSend || sending
|
||
|
|
? scheme.primary
|
||
|
|
: scheme.surface,
|
||
|
|
shape: BoxShape.circle,
|
||
|
|
),
|
||
|
|
child: sending
|
||
|
|
? SizedBox(
|
||
|
|
width: 16,
|
||
|
|
height: 16,
|
||
|
|
child: CircularProgressIndicator(
|
||
|
|
strokeWidth: 2, color: scheme.onPrimary))
|
||
|
|
: Icon(Icons.arrow_upward,
|
||
|
|
size: 20,
|
||
|
|
color: _canSend ? scheme.onPrimary : theme.hintColor),
|
||
|
|
),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<void> _pickProject() async {
|
||
|
|
final app = widget.app;
|
||
|
|
await app.refreshProjects();
|
||
|
|
if (!mounted) return;
|
||
|
|
final draft = app.draft;
|
||
|
|
if (draft == null) return;
|
||
|
|
final projects = _selectableProjects(app.projects);
|
||
|
|
await showModalBottomSheet<void>(
|
||
|
|
context: context,
|
||
|
|
isScrollControlled: true,
|
||
|
|
builder: (context) => DraggableScrollableSheet(
|
||
|
|
expand: false,
|
||
|
|
initialChildSize: 0.6,
|
||
|
|
maxChildSize: 0.9,
|
||
|
|
builder: (context, controller) => Column(
|
||
|
|
children: [
|
||
|
|
const Padding(
|
||
|
|
padding: EdgeInsets.fromLTRB(16, 14, 16, 6),
|
||
|
|
child: Text('选择项目',
|
||
|
|
style:
|
||
|
|
TextStyle(fontSize: 17, fontWeight: FontWeight.w600)),
|
||
|
|
),
|
||
|
|
Expanded(
|
||
|
|
child: ListView(
|
||
|
|
controller: controller,
|
||
|
|
children: [
|
||
|
|
for (final project in projects)
|
||
|
|
ListTile(
|
||
|
|
dense: true,
|
||
|
|
leading: Icon(
|
||
|
|
project.vcs.isNotEmpty
|
||
|
|
? Icons.account_tree_outlined
|
||
|
|
: Icons.folder_outlined,
|
||
|
|
size: 20),
|
||
|
|
title: Text(project.label,
|
||
|
|
maxLines: 1, overflow: TextOverflow.ellipsis),
|
||
|
|
subtitle: Text(project.canonical,
|
||
|
|
maxLines: 1, overflow: TextOverflow.ellipsis),
|
||
|
|
trailing: project.canonical == draft.directory
|
||
|
|
? Icon(Icons.check,
|
||
|
|
size: 18,
|
||
|
|
color: Theme.of(context).colorScheme.primary)
|
||
|
|
: null,
|
||
|
|
onTap: () {
|
||
|
|
app.updateDraftTarget(project.canonical);
|
||
|
|
Navigator.pop(context);
|
||
|
|
},
|
||
|
|
),
|
||
|
|
if (projects.isEmpty)
|
||
|
|
const Padding(
|
||
|
|
padding: EdgeInsets.all(24),
|
||
|
|
child: Text('没有可用的项目', textAlign: TextAlign.center),
|
||
|
|
),
|
||
|
|
],
|
||
|
|
),
|
||
|
|
),
|
||
|
|
],
|
||
|
|
),
|
||
|
|
),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// The picker shows checkouts, not the server's chat folders (those are
|
||
|
|
/// reached by just starting a chat, not by choosing a project). The server
|
||
|
|
/// can list the same path twice (a project record and a sandbox), so one
|
||
|
|
/// path is one row, preferring the one that knows its VCS.
|
||
|
|
List<ProjectInfo> _selectableProjects(List<ProjectInfo> projects) {
|
||
|
|
final byPath = <String, ProjectInfo>{};
|
||
|
|
for (final project in projects) {
|
||
|
|
if (project.canonical.isEmpty) continue;
|
||
|
|
if (project.canonical.contains('/.config/openchamber/')) continue;
|
||
|
|
final existing = byPath[project.canonical];
|
||
|
|
if (existing == null ||
|
||
|
|
(existing.vcs.isEmpty && project.vcs.isNotEmpty)) {
|
||
|
|
byPath[project.canonical] = project;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return byPath.values.toList();
|
||
|
|
}
|
||
|
|
}
|