fix: switching a session's model, and put the model row in the composer

- POST /api/session/:id/model takes the reference under `model`; sending it
  at the top level was rejected with "Missing key at [model]".
- The chat now shows the model/agent row above the input (revealed while
  typing or running, as dsh-Go does) instead of in the app bar; the draft
  keeps the same row, with its project chip first.
- Thinking levels: /api/model's variants become a 思考 chip and sheet; the
  choice travels as ModelRef.variant on the switch or the create, and the
  last level per model is remembered on the device.
- The model picker groups providers, adds 收藏 and 最近 (device-local), shows
  each model's context size and reasoning mark, and collapses sections.
- The `/` palette is shared by the chat and the draft (new slash_palette
  widget); the draft lists commands and skills but not the client-local
  /compact, which needs an existing session.
- 0.3.14 (314).
This commit is contained in:
blumia committed 2026-11-21 17:03:22 +08:00
1 parent fff6562cbc
commit 7f2f454539
12 files changed
+962 -344

No files matched your search

+151 -51
View File
@@ -7,7 +7,8 @@ import 'package:flutter/services.dart';
import '../api/models.dart';
import '../state.dart';
import '../widgets/composer_attachment.dart';
import '../widgets/model_picker.dart';
import '../widgets/model_controls.dart';
import '../widgets/slash_palette.dart';
import 'files_panel.dart';
class NewSessionScreen extends StatefulWidget {
@@ -33,6 +34,14 @@ class _NewSessionScreenState extends State<NewSessionScreen> {
List<ComposerAttachment> _attachments = [];
bool _attaching = false;
/// The `/` palette for the draft's directory. It carries commands and
/// skills, not the client-local `compact` that needs an existing session.
List<SlashItem> _slashItems = [];
bool _slashLoading = false;
bool _slashLoaded = false;
int _slashIndex = 0;
String? _slashDismissed;
@override
void initState() {
super.initState();
@@ -58,6 +67,70 @@ class _NewSessionScreenState extends State<NewSessionScreen> {
bool get _canSend =>
_text.text.trim().isNotEmpty || _attachments.isNotEmpty;
List<SlashItem>? _slashMatches() => matchSlashItems(
items: _slashItems,
text: _text.text,
dismissed: _slashDismissed,
);
Future<void> _ensureSlash() async {
if (_slashLoaded || _slashLoading) return;
_slashLoading = true;
if (mounted) setState(() {});
try {
_slashItems = await widget.app.slashItemsFor(_draft.directory,
includeLocal: false);
_slashLoaded = true;
} catch (_) {
// An empty palette; a send falls back to a plain prompt.
}
_slashLoading = false;
if (mounted) setState(() {});
}
void _moveSlash(List<SlashItem> items, int delta) {
if (items.isEmpty) return;
setState(() {
_slashIndex = (_slashIndex + delta).clamp(0, items.length - 1);
});
}
void _dismissSlash() => setState(() => _slashDismissed = _text.text);
/// A row was picked: no-argument commands run at once (creating the session
/// on the spot), the rest complete the name and wait for arguments.
void _pickSlash(SlashItem item) {
HapticFeedback.selectionClick();
final text = _text.text;
final lead = text.length - text.trimLeft().length;
final rest = text.trimLeft();
final match = RegExp(r'\s').firstMatch(rest);
final suffix = match == null ? '' : rest.substring(match.start);
final completes = item.skill || item.takesArgs;
if (!completes) {
final command = '/${item.name}';
_text.value = TextEditingValue(
text: command,
selection: TextSelection.collapsed(offset: command.length),
);
widget.app.updateDraftText(command);
setState(() {});
_send();
return;
}
final prefix = '${text.substring(0, lead)}/${item.name}';
final separator =
suffix.isEmpty || !RegExp(r'^\s').hasMatch(suffix) ? ' ' : '';
final next = '$prefix$separator$suffix';
_text.value = TextEditingValue(
text: next,
selection: TextSelection.collapsed(offset: prefix.length + 1),
);
widget.app.updateDraftText(next);
_inputFocus.requestFocus();
setState(() {});
}
Future<void> _send() async {
if (!_canSend || widget.app.sendingDraft) return;
final text = _text.text.trim();
@@ -171,12 +244,14 @@ class _NewSessionScreenState extends State<NewSessionScreen> {
fontSize: 12.5, color: scheme.error),
),
),
Row(
children: [
_projectChip(draft),
const Spacer(),
_modelChip(draft),
],
ModelControlsBar(
app: widget.app,
model: draft.model,
agent: draft.agent,
leading: _projectChip(draft),
onModel: (m) => widget.app.updateDraftModel(m),
onAgent: (a) =>
widget.app.updateDraftModel(null, agent: a),
),
const SizedBox(height: 6),
if (_attachments.isNotEmpty)
@@ -225,52 +300,12 @@ class _NewSessionScreenState extends State<NewSessionScreen> {
);
}
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(
final slash = _slashMatches();
final slashRows = slash ?? const <SlashItem>[];
Widget body = Container(
padding: const EdgeInsets.all(6),
decoration: BoxDecoration(
color: scheme.surfaceContainerHighest,
@@ -293,7 +328,34 @@ class _NewSessionScreenState extends State<NewSessionScreen> {
minLines: 1,
maxLines: 5,
keyboardType: TextInputType.multiline,
onChanged: (_) => setState(() {}),
textInputAction: slash != null
? TextInputAction.send
: TextInputAction.newline,
onSubmitted: slash == null || slashRows.isEmpty
? null
: (_) => _pickSlash(
slashRows[_slashIndex.clamp(0, slashRows.length - 1)]),
onChanged: (v) {
widget.app.updateDraftText(v);
_slashIndex = 0;
if (_slashDismissed != null && v != _slashDismissed) {
_slashDismissed = null;
}
if (v.trimLeft().startsWith('/')) _ensureSlash();
setState(() {});
if (slash != null && v.endsWith('\n')) {
final trimmed = v.trimRight();
_text.value = TextEditingValue(
text: trimmed,
selection: TextSelection.collapsed(offset: trimmed.length),
);
widget.app.updateDraftText(trimmed);
final rows = _slashMatches() ?? const <SlashItem>[];
if (rows.isNotEmpty) {
_pickSlash(rows[_slashIndex.clamp(0, rows.length - 1)]);
}
}
},
decoration: const InputDecoration(
isCollapsed: true,
border: InputBorder.none,
@@ -307,6 +369,42 @@ class _NewSessionScreenState extends State<NewSessionScreen> {
],
),
);
return Column(
mainAxisSize: MainAxisSize.min,
children: [
if (slash != null) ...[
KeyedSubtree(
key: const ValueKey('draft-slash-palette'),
child: SlashPalette(
items: slashRows,
index: _slashIndex,
loading: _slashLoading,
onPick: _pickSlash,
),
),
const SizedBox(height: 6),
],
CallbackShortcuts(
bindings: slash == null
? const {}
: {
const SingleActivator(LogicalKeyboardKey.arrowDown): () =>
_moveSlash(slashRows, 1),
const SingleActivator(LogicalKeyboardKey.arrowUp): () =>
_moveSlash(slashRows, -1),
const SingleActivator(LogicalKeyboardKey.enter): () =>
_pickSlash(slashRows[
_slashIndex.clamp(0, slashRows.length - 1)]),
const SingleActivator(LogicalKeyboardKey.numpadEnter): () =>
_pickSlash(slashRows[
_slashIndex.clamp(0, slashRows.length - 1)]),
const SingleActivator(LogicalKeyboardKey.escape):
_dismissSlash,
},
child: body,
),
],
);
}
Widget _roundKey(
@@ -412,6 +510,8 @@ class _NewSessionScreenState extends State<NewSessionScreen> {
: null,
onTap: () {
app.updateDraftTarget(project.canonical);
_slashLoaded = false;
_slashItems = [];
Navigator.pop(context);
},
),