diff --git a/PLAN.md b/PLAN.md index 05db7d9..8f0aa42 100644 --- a/PLAN.md +++ b/PLAN.md @@ -18,6 +18,7 @@ | 复用 | Go 客户端无法复用;`lib/api/` 按同一协议重写(HTTP/SOCKS5 代理、自签/pinning、SSE、part-shells);聊天 Markdown 直接采用 **dsh-Go 同款的 `gpt_markdown`**(BSD-3,流式、代码高亮/复制、表格、LaTeX、任务列表) | | 踩坑 | SSE 的 `types` 是重复 query 参数,Dart 的 `Map` 会把它压成最后一个 → 只订阅到一种事件、聊天不更新;改为手动拼 query 后实时流恢复 | | 可继续复用 | dsh-Go 的 `app/lib/ui/chat/render/`(markdown_body、code_block、markdown_blocks 等)文件头多为 MIT,可保留 SPDX 头后搬运,但依赖其 Riverpod/DSH 协议类型;`scrollable_positioned_list`、`xterm` 等 pub 包也可直接用 | +| 输入区(参考 dsh-Go) | 圆角胶囊 + 左侧 36 圆附件键 + 无边框自动增高输入 + 上下文占用环 + 右侧 36 圆发送/停止键(运行中变为停止、hint 变「生成中,仍可输入…」);附件以缩略图 chip 展开在胶囊上方;顶栏模型 chip(点击打开模型/智能体分段选择) | | 最低 Android | **24(7.0)** | | 明文 HTTP | Dart `HttpClient` 直接建 socket,release 连 `http://` 无需 cleartext 开关(已实测) | | 模拟器实测 | 服务器列表→添加/连接(无密码自动跳过)→会话列表(运行点/模型·时间)→聊天:流式 Markdown(gpt_markdown,代码块/表格实测)、思考/工具行、页脚、上下文占用环、输入与实时回复(SSE) | diff --git a/README.md b/README.md index b341969..db9a3eb 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,11 @@ revert, attachments (photos/files), permissions. Android 7.0 (API 24)+. Markdown rendering reuses **`gpt_markdown`** (BSD-3), the Flutter AI-output renderer that [dsh-Go](https://github.com/ccch1mneyyy/dsh-go) uses: streaming-friendly, -syntax-highlighted code blocks with copy, tables, LaTeX and task lists. Read-aloud -(TTS) is intentionally **not** implemented (matching the original). +syntax-highlighted code blocks with copy, tables, LaTeX and task lists. The input +bar and the model chip follow dsh-Go's composer design (MIT, referenced rather +than copied): a rounded capsule, a leading attach key, an auto-growing borderless +field, the usage ring, and a 36px send/stop key that morphs while a turn runs. +Read-aloud (TTS) is intentionally **not** implemented (matching the original). ## Run diff --git a/lib/screens/chat_screen.dart b/lib/screens/chat_screen.dart index 50944b8..bdb9869 100644 --- a/lib/screens/chat_screen.dart +++ b/lib/screens/chat_screen.dart @@ -79,10 +79,18 @@ class _ChatScreenState extends State { title: Text(widget.session.title.isEmpty ? '会话' : widget.session.title, maxLines: 1, overflow: TextOverflow.ellipsis), actions: [ - IconButton( - icon: const Icon(Icons.tune), - tooltip: '模型与智能体', - onPressed: () => _openPicker('model'), + Padding( + padding: const EdgeInsets.only(right: 4), + child: ActionChip( + visualDensity: VisualDensity.compact, + avatar: const Icon(Icons.smart_toy_outlined, size: 16), + label: ConstrainedBox( + constraints: const BoxConstraints(maxWidth: 120), + child: Text(_modelLabel(), + maxLines: 1, overflow: TextOverflow.ellipsis), + ), + onPressed: () => _openPicker('model'), + ), ), IconButton( icon: const Icon(Icons.more_vert), @@ -518,87 +526,86 @@ class _ChatScreenState extends State { // ------------------------------------------------------------ composer + // The composer follows dsh-Go's input bar: a rounded capsule holding a + // leading key, an auto-growing borderless field, the usage ring and a + // trailing send/stop key, all 36px round keys (MIT, app/lib/ui/chat/ + // composer.dart, referenced rather than copied). Widget _composer() { final usage = chat.usageReading(); + final scheme = Theme.of(context).colorScheme; + final canSend = _draftCtrl.text.trim().isNotEmpty || _attachments.isNotEmpty; return SafeArea( top: false, child: Padding( - padding: const EdgeInsets.fromLTRB(8, 6, 8, 4), + padding: const EdgeInsets.fromLTRB(8, 6, 8, 6), child: Column( mainAxisSize: MainAxisSize.min, children: [ - if (_attachments.isNotEmpty) - SizedBox( - height: 34, - child: ListView( - scrollDirection: Axis.horizontal, - children: [ - for (var i = 0; i < _attachments.length; i++) - Padding( - padding: const EdgeInsets.only(right: 6), - child: InputChip( - label: Text(_attachments[i].name, - maxLines: 1, overflow: TextOverflow.ellipsis), - onDeleted: () => setState(() => _attachments.removeAt(i)), + if (_attachments.isNotEmpty) _attachmentTray(), + 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: _draftCtrl, + minLines: 1, + maxLines: 5, + keyboardType: TextInputType.multiline, + textInputAction: TextInputAction.newline, + onChanged: (v) { + chat.draft = v; + setState(() {}); + }, + style: const TextStyle(fontSize: 15, height: 1.45), + decoration: InputDecoration( + hintText: chat.running ? '生成中,仍可输入…' : '发消息…', + hintMaxLines: 1, + isDense: true, + border: InputBorder.none, + contentPadding: const EdgeInsets.symmetric(vertical: 8), + ), + ), + ), + if (usage.$5) + InkWell( + onTap: () => _usageSheet(context), + borderRadius: BorderRadius.circular(20), + child: Padding( + padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 8), + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + UsageRing(pct: usage.$4, size: 16, accent: scheme.primary), + const SizedBox(width: 3), + Text('${usage.$4.round()}%', + style: TextStyle(fontSize: 12, color: scheme.onSurfaceVariant)), + ], ), ), - ], - ), + ), + const SizedBox(width: 2), + _roundKey( + icon: chat.running ? Icons.stop_rounded : Icons.arrow_upward_rounded, + label: chat.running ? '停止' : '发送', + filled: true, + enabled: chat.running || canSend, + onTap: chat.running ? chat.stop : _send, + ), + ], ), - Row( - crossAxisAlignment: CrossAxisAlignment.end, - children: [ - _attaching - ? const Padding( - padding: EdgeInsets.all(12), - child: SizedBox(width: 20, height: 20, child: CircularProgressIndicator(strokeWidth: 2))) - : IconButton( - icon: const Icon(Icons.attach_file), - tooltip: '添加附件', - onPressed: _pickFiles, - ), - Expanded( - child: TextField( - controller: _draftCtrl, - minLines: 1, - maxLines: 5, - onChanged: (v) => chat.draft = v, - decoration: const InputDecoration( - hintText: '发消息…', - border: OutlineInputBorder(borderRadius: BorderRadius.all(Radius.circular(20))), - contentPadding: EdgeInsets.symmetric(horizontal: 12, vertical: 10), - ), - ), - ), - if (usage.$5) - InkWell( - onTap: () => _usageSheet(context), - child: Padding( - padding: const EdgeInsets.symmetric(horizontal: 8), - child: Row( - mainAxisSize: MainAxisSize.min, - children: [ - UsageRing(pct: usage.$4, accent: Theme.of(context).colorScheme.primary), - const SizedBox(width: 4), - Text('${usage.$4.round()}%', - style: const TextStyle(fontSize: 13, color: Colors.grey)), - ], - ), - ), - ), - if (chat.running) - IconButton( - icon: const Icon(Icons.stop), - tooltip: '停止', - onPressed: chat.stop, - ) - else - IconButton.filled( - icon: const Icon(Icons.send), - tooltip: '发送', - onPressed: () => _send(), - ), - ], ), ], ), @@ -606,6 +613,94 @@ class _ChatScreenState extends State { ); } + /// One 36px round key of the input bar. + Widget _roundKey({ + required IconData icon, + required String label, + required VoidCallback onTap, + bool filled = false, + bool enabled = true, + bool busy = false, + }) { + final scheme = Theme.of(context).colorScheme; + final on = enabled && !busy; + final bg = filled + ? (on ? scheme.primary : scheme.onSurface.withValues(alpha: 0.12)) + : scheme.onSurface.withValues(alpha: 0.06); + final fg = filled + ? (on ? scheme.onPrimary : scheme.onSurface.withValues(alpha: 0.38)) + : scheme.onSurface; + return Semantics( + button: true, + enabled: on, + label: label, + child: Material( + color: bg, + shape: const CircleBorder(), + child: InkWell( + customBorder: const CircleBorder(), + onTap: on ? onTap : null, + child: SizedBox( + width: 36, + height: 36, + child: Center( + child: busy + ? SizedBox( + width: 16, + height: 16, + child: CircularProgressIndicator(strokeWidth: 2, color: fg), + ) + : Icon(icon, size: 20, color: fg), + ), + ), + ), + ), + ); + } + + /// Attachment chips with a thumbnail, unfolded above the input bar. + Widget _attachmentTray() { + return SizedBox( + height: 46, + child: ListView( + scrollDirection: Axis.horizontal, + children: [ + for (var i = 0; i < _attachments.length; i++) + Padding( + padding: const EdgeInsets.only(right: 6), + child: InputChip( + avatar: _attachments[i].path != null + ? CircleAvatar( + backgroundImage: FileImage(File(_attachments[i].path!)), + ) + : const Icon(Icons.insert_drive_file, size: 16), + label: ConstrainedBox( + constraints: const BoxConstraints(maxWidth: 160), + child: Text(_attachments[i].name, + maxLines: 1, overflow: TextOverflow.ellipsis), + ), + onDeleted: () => setState(() => _attachments.removeAt(i)), + ), + ), + ], + ), + ); + } + + String _modelLabel() { + ModelRef? m; + for (var i = chat.msgs.length - 1; i >= 0; i--) { + if (chat.msgs[i].model != null) { + m = chat.msgs[i].model; + break; + } + } + m ??= widget.session.model; + final model = m == null ? '模型' : widget.app.modelName(m); + final agent = widget.session.agent; + return agent.isEmpty ? model : '$model · $agent'; + } + Future _send() async { final text = _draftCtrl.text.trim(); if (text.isEmpty && _attachments.isEmpty) return; @@ -630,6 +725,7 @@ class _ChatScreenState extends State { chat.imagePaths[id] = images; chat.touch(); } + HapticFeedback.lightImpact(); } Future _pickFiles() async { @@ -763,11 +859,26 @@ class _ChatScreenState extends State { return Column( children: [ Padding( - padding: const EdgeInsets.all(12), + padding: const EdgeInsets.fromLTRB(12, 12, 12, 4), child: Text(title, style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), ), Padding( padding: const EdgeInsets.symmetric(horizontal: 12), + child: SegmentedButton( + segments: const [ + ButtonSegment(value: 'model', label: Text('模型')), + ButtonSegment(value: 'agent', label: Text('智能体')), + ], + selected: {_pickerKind}, + showSelectedIcon: false, + onSelectionChanged: (s) { + setState(() => _pickerKind = s.first); + widget.app.refreshModels(); + }, + ), + ), + Padding( + padding: const EdgeInsets.all(12), child: TextField( onChanged: (v) => setState(() => _pickerFilter = v), decoration: const InputDecoration(hintText: '搜索', border: OutlineInputBorder(), isDense: true),