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

+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;
}
}