feat: AI-output Markdown, live SSE fixes, faster builds

- render assistant Markdown with gpt_markdown (the same renderer dsh-Go
  uses): streaming, syntax-highlighted code with copy, tables, LaTeX and
  task lists, replacing flutter_markdown
- fix the event stream: Dart's Map collapsed the repeated `types` query
  into one value, so only one event type was subscribed and chats never
  updated; build the query by hand
- keep the optimistic prompt on failure out of the transcript and put the
  text back for a retry
- clamp negative turn durations
- build: Aliyun Maven mirrors, pre-seeded Gradle 9.5.0, and a complete
  NDK 28.2 install to avoid multi-hour downloads
This commit is contained in:
blumia committed 2026-11-18 20:11:49 +08:00
1 parent aee560172c
commit 885a27c6a9
7 files changed
+186 -46

No files matched your search

+5 -3
View File
@@ -15,12 +15,14 @@
| 环境 | Flutter **3.50.0 stable** 装在 `~/SDK/flutter`;`FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn`、`PUB_HOSTED_URL=https://pub.flutter-io.cn` |
| 构建坑 | Gradle 发行包(gradle-9.5.0-all,230MB)从 services.gradle.org/GitHub 下载一小时级 → 用腾讯镜像预置;AGP 又重装 NDK 28.2(688MB,dl.google.com 慢)→ 腾讯 AndroidSDK 镜像重装完整 NDK;另在 settings/build.gradle.kts 前加**阿里云 Maven 镜像** |
| 依赖坑 | file_picker 8.x 硬编码 compileSdk 34,与 flutter_plugin_android_lifecycle 要求的 36 冲突 → 升 file_picker 13.4(联邦版 API)+ flutter_secure_storage 11.2 |
| 复用 | Go 客户端无法复用;`lib/api/` 按同一协议重写(HTTP/SOCKS5 代理、自签/pinning、SSE、part-shells) |
| 复用 | 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 包也可直接用 |
| 最低 Android | **24(7.0)** |
| 明文 HTTP | Dart `HttpClient` 直接建 socket,release 连 `http://` 无需 cleartext 开关(已实测) |
| 模拟器实测 | 服务器列表→添加/连接(无密码自动跳过)→会话列表(运行点/模型·时间)→聊天:流式 Markdown、思考/工具行、页脚、上下文占用环(65%/18%)、输入区;发送与回复正常 |
| 模拟器实测 | 服务器列表→添加/连接(无密码自动跳过)→会话列表(运行点/模型·时间)→聊天:流式 Markdown(gpt_markdown,代码块/表格实测)、思考/工具行、页脚、上下文占用环、输入与实时回复(SSE) |
| 产物 | `dist/OpenChamber-0.3.0-flutter-release.apk`(0.3.0 / code 300,`~/SDK/test-upload.jks` 签名,`OPENCHAMBER_KEYSTORE*` 可覆盖) |
| 未做 | 朗读(按需求去掉)、附件/图片预览虽已实现但未在模拟器逐项点验、i18n、启动图 |
| 未做 | 朗读(按需求去掉)、附件/图片预览虽已实现但未逐项点验、i18n、启动图;APK 71MB(gpt_markdown 带字体/LaTeX,后续可按 ABI 拆分) |
---
+5 -2
View File
@@ -18,10 +18,13 @@ revert, attachments (photos/files), permissions. Android 7.0 (API 24)+.
| `lib/api/` | Dart OpenChamber client: auth, sessions, messages, SSE, models/agents, permissions, prompt/interrupt/fork/revert, `/api/fs/raw`; HTTP proxy via `HttpClient.findProxy`, SOCKS5 via dart:io's SOCKS, pinning/self-signed via `badCertificateCallback` |
| `lib/storage.dart` | Saved servers in `shared_preferences`, tokens in `flutter_secure_storage` (Android Keystore) |
| `lib/state.dart` | The app state: connections, live sessions, the open chat, and SSE event folding |
| `lib/screens/` | Servers, sessions, chat (streaming Markdown via `flutter_markdown`, composer, sheets) |
| `lib/screens/` | Servers, sessions, chat (streaming Markdown via `gpt_markdown`, composer, sheets) |
| `lib/widgets/` | The context usage ring |
Read-aloud (TTS) is intentionally **not** implemented (matching the original).
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).
## Run
+12 -11
View File
@@ -232,11 +232,12 @@ class Client {
/// Subscribes to GET /api/event, yielding each SSE event. The stream ends
/// when the connection drops; the caller restarts it with backoff.
Stream<Map<String, dynamic>> streamEvents({List<String>? types}) async* {
final query = <String, String>{
if (types != null) for (final t in types) 'types': t,
};
final uri = base.resolve('/api/event')
.replace(queryParameters: query.isEmpty ? null : query);
// `types` repeats: a Map would keep only the last one.
var uri = base.resolve('/api/event');
if (types != null && types.isNotEmpty) {
final q = types.map((t) => 'types=${Uri.encodeQueryComponent(t)}').join('&');
uri = Uri.parse('$uri?$q');
}
final req = await _http.openUrl('GET', uri);
req.headers.set('Accept', 'text/event-stream');
req.headers.set('Cache-Control', 'no-cache');
@@ -297,12 +298,9 @@ class Client {
final q = Uri(queryParameters: query ?? {});
final uri = base.resolve(path).replace(queryParameters: q.queryParameters.isEmpty ? null : q.queryParameters);
final req = await _http.openUrl(method, uri);
req.headers.set('Accept', 'application/json');
// Every header must be set before the body, which finalizes them.
req.headers.set('Accept', path == '/api/event' ? 'text/event-stream' : 'application/json');
req.headers.set('User-Agent', 'openchamber-native/0.3');
if (body != null) {
req.headers.contentType = ContentType.json;
req.write(jsonEncode(body));
}
if (token != null && token!.isNotEmpty) {
req.headers.set('Authorization', 'Bearer $token');
}
@@ -314,9 +312,12 @@ class Client {
req.headers.set('x-openchamber-part-shells', 'v1');
}
if (path == '/api/event') {
req.headers.set('Accept', 'text/event-stream');
req.headers.set('Cache-Control', 'no-cache');
}
if (body != null) {
req.headers.contentType = ContentType.json;
req.write(jsonEncode(body));
}
final res = await req.close();
final bodyBytes = await res.fold<List<int>>(<int>[], (a, b) => a..addAll(b));
return _Response(res.statusCode, bodyBytes);
+14 -10
View File
@@ -6,7 +6,7 @@ import 'dart:typed_data';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:gpt_markdown/gpt_markdown.dart';
import 'package:path_provider/path_provider.dart';
import '../api/models.dart';
@@ -301,12 +301,11 @@ class _ChatScreenState extends State<ChatScreen> {
final text = m.content[part].text;
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 2),
child: MarkdownBody(
data: text,
selectable: true,
styleSheet: MarkdownStyleSheet.fromTheme(Theme.of(context)).copyWith(
p: const TextStyle(fontSize: 15, height: 1.45),
),
child: GptMarkdown(
text,
// The server already streams deltas; show them as they arrive.
isStreaming: false,
style: const TextStyle(fontSize: 15, height: 1.45),
),
);
}
@@ -453,6 +452,7 @@ class _ChatScreenState extends State<ChatScreen> {
}
if (start <= 0) return '';
final secs = ((m.completed - start) / 1000).round();
if (secs <= 0) return '';
if (secs < 60) return '$secs 秒';
return '${secs ~/ 60}:${(secs % 60).toString().padLeft(2, '0')}';
}
@@ -618,11 +618,15 @@ class _ChatScreenState extends State<ChatScreen> {
images.add(a.path!);
}
}
chat.draft = '';
final id = await chat.send(text: text, files: files);
if (id == null) {
// The prompt failed: keep the text for a retry.
_draftCtrl.text = chat.draft;
return;
}
_draftCtrl.clear();
setState(() => _attachments = []);
final id = await chat.send(text: text, files: files);
if (id != null && images.isNotEmpty) {
if (images.isNotEmpty) {
chat.imagePaths[id] = images;
chat.touch();
}
+6 -1
View File
@@ -536,9 +536,14 @@ class ChatState extends ChangeNotifier {
await app.client!.prompt(this.id, text: t, messageId: id, directory: dir, files: files);
return id;
} catch (e) {
// The prompt did not reach the server: take the optimistic message back
// and keep what was typed, so a retry is not a duplicate.
msgs.removeWhere((m) => m.id == id);
_reindex();
draft = t;
error = e.toString();
notifyListeners();
return id;
return null;
}
}
+143 -18
View File
@@ -41,6 +41,14 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.4.1"
cli_util:
dependency: transitive
description:
name: cli_util
sha256: "89b8801ba49f8b593fbf2d3e59efd0f566c46cd4ba6837ae2610c72b52a0f899"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.6.0"
clock:
dependency: transitive
description:
@@ -89,6 +97,14 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.9"
cupertino_ui:
dependency: transitive
description:
name: cupertino_ui
sha256: c3eaf7f5bdfee36deb0b14a4cbf7602f1b6b8d8b8576b81f38fc08c25881c94c
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.7"
dbus:
dependency: transitive
description:
@@ -182,14 +198,11 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "6.0.0"
flutter_markdown:
dependency: "direct main"
description:
name: flutter_markdown
sha256: "08fb8315236099ff8e90cb87bb2b935e0a724a3af1623000a9cec930468e0f27"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.7.7+1"
flutter_localizations:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
flutter_secure_storage:
dependency: "direct main"
description:
@@ -238,6 +251,14 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "4.2.2"
flutter_svg:
dependency: transitive
description:
name: flutter_svg
sha256: "455a22036f8d1b7c74f53283c8644d6af2f6f7c9ec72c7b06a2ff70fb3954a54"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.4.0"
flutter_test:
dependency: "direct dev"
description: flutter
@@ -248,6 +269,14 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
gpt_markdown:
dependency: "direct main"
description:
name: gpt_markdown
sha256: "948469e671fb6bce1d7ecdfb5b01b4ca5686741514f1274bf53b72452fecf07b"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.4.1"
hooks:
dependency: transitive
description:
@@ -256,6 +285,30 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.2.0"
http:
dependency: transitive
description:
name: http
sha256: "87721a4a50b19c7f1d49001e51409bddc46303966ce89a65af4f4e6004896412"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.6.0"
http_parser:
dependency: transitive
description:
name: http_parser
sha256: "178d74305e7866013777bab2c3d8726205dc5a4dd935297175b19a23a2e66571"
url: "https://pub.flutter-io.cn"
source: hosted
version: "4.1.2"
intl:
dependency: transitive
description:
name: intl
sha256: "1ca20c894b1717686a2319b8548763d812bc0aabdac580420a44c5178c57a867"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.20.3"
jni:
dependency: transitive
description:
@@ -328,14 +381,6 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.3.0"
markdown:
dependency: transitive
description:
name: markdown
sha256: ee85086ad7698b42522c6ad42fe195f1b9898e4d974a1af4576c1a3a176cada9
url: "https://pub.flutter-io.cn"
source: hosted
version: "7.3.1"
matcher:
dependency: transitive
description:
@@ -352,6 +397,14 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.13.0"
material_ui:
dependency: transitive
description:
name: material_ui
sha256: a6b19012df437a356cbc01f8ea22a105e07194c47c9a6009da0032a0ce35c4ab
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.11.1"
meta:
dependency: transitive
description:
@@ -384,6 +437,14 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.9.1"
path_parsing:
dependency: transitive
description:
name: path_parsing
sha256: "883402936929eac138ee0a45da5b0f2c80f89913e6dc3bf77eb65b84b409c6ca"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.0"
path_provider:
dependency: "direct main"
description:
@@ -581,6 +642,70 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.7.14"
typed_data:
dependency: transitive
description:
name: typed_data
sha256: f9049c039ebfeb4cf7a7104a675823cd72dba8297f264b6637062516699fa006
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.4.0"
val_highlight:
dependency: transitive
description:
name: val_highlight
sha256: dbb36108dfc96aa09f544dc25dc769be0a60fa1c420a53bf23fadfafd7707049
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.1.1"
val_highlight_flutter:
dependency: transitive
description:
name: val_highlight_flutter
sha256: "68269b7050fce3f26e20d8884394fed1647153bc0b838bbd519ac8cc73612bb8"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.1.2"
val_latex:
dependency: transitive
description:
name: val_latex
sha256: ff7f4d937d4b84df4d79c3046aaeba85e1c198cc9f9ed2495567213865a5234c
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.1.4"
val_latex_flutter:
dependency: transitive
description:
name: val_latex_flutter
sha256: "7e80cd9c113e257c3f4ad779adb8a386e33e05ee2946960e3b96317ea6f47089"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.1.4"
vector_graphics:
dependency: transitive
description:
name: vector_graphics
sha256: "06081e4f2d8f17c306aa58989520694b463ad411bc7c7b93e76afa3e41ca17ac"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.3.1"
vector_graphics_codec:
dependency: transitive
description:
name: vector_graphics_codec
sha256: "023f3afd1b08d040406e7ccc324f5173c04762a59e374f29d04cef00a4940742"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.2.0"
vector_graphics_compiler:
dependency: transitive
description:
name: vector_graphics_compiler
sha256: "7d3d604c1ede26711f865f74e9273c5c5504ac449f3d92faa722878dbac88807"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.3.6"
vector_math:
dependency: transitive
description:
@@ -633,10 +758,10 @@ packages:
dependency: transitive
description:
name: xml
sha256: "0fe8cc98946def31cbcbc8734aa4067031e6f65ea3a78a508060bfed2200bfeb"
sha256: "67f0aff7be013d107995e9b75bf4e7f2c3ef2dfdb2c8e68024bba0a7fd5756a4"
url: "https://pub.flutter-io.cn"
source: hosted
version: "7.1.0"
version: "7.0.1"
yaml:
dependency: transitive
description:
+1 -1
View File
@@ -13,9 +13,9 @@ dependencies:
cupertino_icons: ^1.0.8
shared_preferences: ^2.3.0
flutter_secure_storage: ^11.2.0
flutter_markdown: ^0.7.0
file_picker: ^13.0.0
path_provider: ^2.1.0
gpt_markdown: ^1.4.0
dev_dependencies:
flutter_test: