Skip to content

Commit dc27fdf

Browse files
committed
new: reasoning content display
1 parent 3e412a1 commit dc27fdf

File tree

8 files changed

+156
-124
lines changed

8 files changed

+156
-124
lines changed

lib/data/model/chat/history/history.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,24 @@ final class ChatHistoryItem {
8282
final List<ChatCompletionMessageToolCall>? toolCalls;
8383
@HiveField(6)
8484
@JsonKey(includeIfNull: false)
85-
final String? thinkContent;
85+
String? reasoning;
8686

87-
const ChatHistoryItem({
87+
ChatHistoryItem({
8888
required this.role,
8989
required this.content,
9090
required this.createdAt,
9191
required this.id,
9292
this.toolCallId,
9393
this.toolCalls,
94-
this.thinkContent,
94+
this.reasoning,
9595
});
9696

9797
ChatHistoryItem.gen({
9898
required this.role,
9999
required this.content,
100100
this.toolCallId,
101101
this.toolCalls,
102-
this.thinkContent
102+
this.reasoning
103103
}) : createdAt = DateTime.now(),
104104
id = shortid.generate();
105105

@@ -110,7 +110,7 @@ final class ChatHistoryItem {
110110
DateTime? createdAt,
111111
this.toolCallId,
112112
this.toolCalls,
113-
this.thinkContent,
113+
this.reasoning,
114114
}) : content = [ChatContent.noid(type: type, raw: raw)],
115115
createdAt = createdAt ?? DateTime.now(),
116116
id = shortid.generate();

lib/data/model/chat/history/history.ext.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,15 @@ extension ChatHistoryItemX on ChatHistoryItem {
7575
DateTime? createdAt,
7676
@protected String? id,
7777
String? toolCallId,
78+
String? reasoning,
7879
}) {
7980
return ChatHistoryItem(
8081
role: role ?? this.role,
8182
content: content ?? this.content,
8283
createdAt: createdAt ?? this.createdAt,
8384
id: id ?? this.id,
8485
toolCallId: toolCallId ?? this.toolCallId,
86+
reasoning: reasoning ?? this.reasoning,
8587
);
8688
}
8789

lib/data/model/chat/history/history.g.dart

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/data/model/chat/history/view.dart

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -83,39 +83,42 @@ final class ChatHistoryContentView extends StatelessWidget {
8383
return text;
8484
}
8585

86+
final children = chatItem.content.map((e) {
87+
final fn = switch (e.type) {
88+
// ChatContentType.audio => _buildAudio(e),
89+
ChatContentType.image => _buildImage,
90+
ChatContentType.file => _buildFile,
91+
_ => _buildText,
92+
};
93+
return fn(context, e);
94+
}).toList();
95+
96+
final reasoningContent = chatItem.reasoning;
97+
if (reasoningContent != null) {
98+
final reasoning = ExpandTile(
99+
title: Text(
100+
libL10n.thinking,
101+
maxLines: 1,
102+
overflow: TextOverflow.ellipsis,
103+
),
104+
tilePadding: const EdgeInsets.symmetric(horizontal: 17, vertical: 0),
105+
childrenPadding: const EdgeInsets.symmetric(horizontal: 9, vertical: 9),
106+
children: [
107+
_buildMarkdown(context, reasoningContent),
108+
],
109+
).cardx;
110+
children.insert(0, reasoning);
111+
}
112+
86113
return Column(
87114
crossAxisAlignment: CrossAxisAlignment.start,
88115
mainAxisSize: MainAxisSize.min,
89-
children: chatItem.content
90-
.map((e) => switch (e.type) {
91-
// ChatContentType.audio => _buildAudio(e),
92-
ChatContentType.image => _buildImage,
93-
ChatContentType.file => _buildFile,
94-
_ => _buildText,
95-
}(context, e))
96-
.toList()
97-
.joinWith(UIs.height13),
116+
children: children.joinWith(UIs.height13),
98117
);
99118
}
100119

101120
Widget _buildText(BuildContext context, ChatContent content) {
102-
return MarkdownBody(
103-
data: content.raw,
104-
builders: {
105-
'code': CodeElementBuilder(onCopy: Pfs.copy),
106-
'latex': LatexElementBuilder(),
107-
},
108-
styleSheet: MarkdownStyleSheet.fromTheme(context.theme).copyWith(
109-
a: TextStyle(color: UIs.primaryColor),
110-
),
111-
extensionSet: MarkdownUtils.extensionSet,
112-
onTapLink: MarkdownUtils.onLinkTap,
113-
shrinkWrap: false,
114-
// Keep it false, or the ScrollView's height calculation will be wrong.
115-
fitContent: false,
116-
// User experience is better when this is false.
117-
selectable: isDesktop,
118-
);
121+
return _buildMarkdown(context, content.raw);
119122
}
120123

121124
Widget _buildImage(BuildContext context, ChatContent content) {
@@ -137,6 +140,28 @@ final class ChatHistoryContentView extends StatelessWidget {
137140
// return AudioCard(id: content.id, path: content.raw);
138141
// }
139142

143+
Widget _buildMarkdown(BuildContext context, String content) {
144+
return MarkdownBody(
145+
data: content,
146+
builders: {
147+
'code': CodeElementBuilder(onCopy: Pfs.copy),
148+
'latex': LatexElementBuilder(),
149+
},
150+
styleSheet: MarkdownStyleSheet.fromTheme(context.theme).copyWith(
151+
a: TextStyle(color: UIs.primaryColor),
152+
),
153+
extensionSet: MarkdownUtils.extensionSet,
154+
onTapLink: MarkdownUtils.onLinkTap,
155+
shrinkWrap: false,
156+
// Keep it false, or the ScrollView's height calculation will be wrong.
157+
fitContent: false,
158+
// User experience is better when this is false.
159+
selectable: isDesktop,
160+
);
161+
}
162+
}
163+
164+
extension on ChatHistoryContentView {
140165
void _onImgRet(ImagePageRet ret, String raw) async {
141166
if (ret.isDeleted) {
142167
FileApi.delete([raw]);

lib/view/page/home/req.dart

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,23 @@ Future<void> _onCreateText(
229229
try {
230230
final sub = chatStream.listen(
231231
(eve) async {
232-
final first = eve.choices.firstOrNull;
233-
final delta = first?.delta.content;
234-
if (delta != null) {
235-
assistReply.content.first.raw += delta;
232+
final delta = eve.choices.firstOrNull?.delta;
233+
if (delta == null) return;
234+
235+
final content = delta.content;
236+
if (content != null) {
237+
assistReply.content.first.raw += content;
238+
_chatItemRNMap[assistReply.id]?.notify();
239+
}
240+
241+
final deltaResoningContent = delta.reasoningContent;
242+
if (deltaResoningContent != null) {
243+
final originReasoning = assistReply.reasoning ?? '';
244+
final newReasoning = '$originReasoning$deltaResoningContent';
245+
assistReply.reasoning = newReasoning;
236246
_chatItemRNMap[assistReply.id]?.notify();
237247
}
248+
// print('Reasoning: ${assistReply.reasoning}');
238249

239250
_autoScroll(chatId);
240251
},

lib/view/widget/code.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class CodeElementBuilder extends MarkdownElementBuilder {
5252
// /// If [textContent] contains any of the above, it will be rendered as
5353
// /// plain text.
5454
// final isUnsupported = textContent.contains(r'\documentclass') ||
55-
// textContent.contains(r'\title') ||
55+
// textContent.contains(r'\title') || zagu lollipopkit
5656
// textContent.contains(r'\author') ||
5757
// textContent.contains(r'\begin');
5858
// if (!isUnsupported) {

0 commit comments

Comments
 (0)