feat: built-in /compact and compaction rows in the transcript

- /compact is not one of the directory's commands: OpenChamber's own
  client supplies it and runs POST /api/session/:id/compact. The palette now
  starts with that local command (压缩), so picking it compacts without the
  model seeing anything.
- A compaction message (status running/completed/failed, reason, summary,
  tokens) is part of the timeline. It now shows as the official client's
  row: a scissors glyph and "正在压缩对话" / "对话已压缩", a 显示摘要 toggle,
  and the summary as Markdown (revealed with the same height animation as
  the other folds). Failed compactions keep the error block.
- Live compaction events are subscribed: started adds the running record
  (or refreshes the tail when it names no id), delta appends the streaming
  summary, ended/failed fetch the server's settled record.
- Version 0.3.8 (code 308).
This commit is contained in:
blumia committed 2026-11-20 12:26:15 +08:00
1 parent fbadf6f858
commit b9433c30cb
7 files changed
+214 -14

No files matched your search

+100 -5
View File
@@ -37,6 +37,10 @@ const _eventTypes = [
'session.tool.called',
'session.tool.success',
'session.tool.failed',
'session.compaction.started',
'session.compaction.delta',
'session.compaction.ended',
'session.compaction.failed',
'permission.asked',
'permission.replied',
];
@@ -486,11 +490,15 @@ class SlashItem {
final String description;
final bool skill;
final String id;
/// A command the client runs itself (`compact`), rather than a prompt.
final String action;
const SlashItem({
required this.name,
this.description = '',
this.skill = false,
this.id = '',
this.action = '',
});
/// Commands whose description shows an argument form (`[commit|branch|pr]`)
@@ -568,9 +576,18 @@ class ChatState extends ChangeNotifier {
cl.commands(directory: dir),
cl.skills(directory: dir),
]);
const local = [
SlashItem(
name: 'compact',
description: '使用 AI 压缩会话历史以减少上下文大小',
action: 'compact',
),
];
final localNames = {for (final c in local) c.name};
slashItems = [
...local,
for (final c in results[0])
if ((c['name'] ?? '').isNotEmpty)
if ((c['name'] ?? '').isNotEmpty && !localNames.contains(c['name']))
SlashItem(
name: c['name']!,
description: c['description'] ?? '',
@@ -639,6 +656,7 @@ class ChatState extends ChangeNotifier {
error: server.error,
status: server.status,
reason: server.reason,
summary: server.summary,
);
}
@@ -715,6 +733,30 @@ class ChatState extends ChangeNotifier {
final args =
(RegExp(r'^/\S+\s*([\s\S]*)$').firstMatch(t)?.group(1) ?? '').trim();
draft = '';
if (item.action == 'compact') {
try {
final cid = await app.client!.compact(this.id, directory: dir);
error = null;
if (cid != null && byId[cid] == null) {
msgs.add(Message(
id: cid,
type: 'compaction',
status: 'running',
reason: 'manual',
created: DateTime.now().millisecondsSinceEpoch,
));
_sortMsgs();
}
notifyListeners();
await reloadTail();
return '';
} catch (e) {
draft = t;
error = e.toString();
notifyListeners();
return null;
}
}
if (!item.skill) {
// A command is expanded by the server, which also writes the user
// message; there is no id to show optimistically.
@@ -927,7 +969,8 @@ class ChatState extends ChangeNotifier {
? m.error
: MessageError.fromJson(data['error'] as Map<String, dynamic>),
status: m.status,
reason: m.reason));
reason: m.reason,
summary: m.summary));
// Live events name a tool only by id, so a streamed step would keep
// a generic label ("工具") until the session was reopened. The
// finished message has the names, inputs and attachments.
@@ -956,6 +999,55 @@ class ChatState extends ChangeNotifier {
case 'session.tool.progress':
if (mid != null) _applyTool(mid, type, data);
break;
case 'session.compaction.started':
final cid = data['inputID'] as String?;
if (cid != null && byId[cid] == null) {
msgs.add(Message(
id: cid,
type: 'compaction',
status: 'running',
reason: data['reason'] as String? ?? '',
created: created,
));
_sortMsgs();
} else if (cid == null) {
reloadTail();
}
break;
case 'session.compaction.delta':
final text = data['text'] as String? ?? '';
if (text.isNotEmpty) {
for (var i = msgs.length - 1; i >= 0; i--) {
final m = msgs[i];
if (m.type == 'compaction' && m.status == 'running') {
msgs[i] = Message(
id: m.id,
type: m.type,
created: m.created,
completed: m.completed,
agent: m.agent,
model: m.model,
content: m.content,
text: m.text,
files: m.files,
finish: m.finish,
cost: m.cost,
tokens: m.tokens,
error: m.error,
status: m.status,
reason: m.reason,
summary: m.summary + text,
);
_reindex();
break;
}
}
}
break;
case 'session.compaction.ended':
case 'session.compaction.failed':
reloadTail();
break;
case 'session.model.selected':
final model = data['model'];
if (model is Map<String, dynamic>) {
@@ -1026,7 +1118,8 @@ class ChatState extends ChangeNotifier {
tokens: m.tokens,
error: m.error,
status: m.status,
reason: m.reason);
reason: m.reason,
summary: m.summary);
});
}
@@ -1089,7 +1182,8 @@ class ChatState extends ChangeNotifier {
tokens: m.tokens,
error: m.error,
status: m.status,
reason: m.reason);
reason: m.reason,
summary: m.summary);
}
}
content.add(part);
@@ -1108,7 +1202,8 @@ class ChatState extends ChangeNotifier {
tokens: m.tokens,
error: m.error,
status: m.status,
reason: m.reason);
reason: m.reason,
summary: m.summary);
});
}