feat: tabs own the panel header, a Termux key bar, and agent memory

- The panel's top row is now the tab group plus the close button; each tab
  keeps its own header: the files tab has the breadcrumb with new/more (or
  the name and edit/download while previewing), knowledge its search and
  refresh, the terminal its status.
- The key bar is Termux's two rows of seven: ESC / — HOME ↑ END PGUP, then
  TAB CTRL ALT ← ↓ → PGDN; the modifiers still apply to what the phone
  keyboard types.
- The panel is a dialog, so it gives the IME's height back itself
  (viewInsets.bottom). The key bar and the terminal stay above the
  keyboard, and the PTY reflows to the smaller terminal.
- Project knowledge gains the memory tab: both scopes from
  /api/agent-memory/all, absent when the server has memory switched off,
  with type, edit and delete.
- File preview editing passes minLines: null alongside expands.
- 0.3.12 (312).
This commit is contained in:
blumia committed 2026-11-21 11:09:44 +08:00
1 parent 5f95e2da97
commit 5a371e4faf
8 files changed
+675 -231

No files matched your search

+316 -17
View File
@@ -18,9 +18,13 @@ class KnowledgePanel extends StatefulWidget {
class _KnowledgePanelState extends State<KnowledgePanel> {
ProjectContext? _context;
/// Null when the server has agent memory switched off, in which case the
/// memory section is not offered at all.
AgentMemorySnapshot? _memory;
bool _loading = true;
String? _error;
int _section = 0;
String _section = 'notes';
String _query = '';
final TextEditingController _search = TextEditingController();
@@ -53,11 +57,19 @@ class _KnowledgePanelState extends State<KnowledgePanel> {
_error = null;
});
try {
final context = await client.projectContext(_projectId,
directory: widget.directory);
// Both reads in one go: the memory tab appears with the rest instead of
// popping in a moment later.
final results = await Future.wait([
client.projectContext(_projectId, directory: widget.directory),
client.agentMemory(_projectId, directory: widget.directory),
]);
if (!mounted) return;
final context = results[0] as ProjectContext;
final memory = results[1] as AgentMemorySnapshot?;
setState(() {
_context = context;
_memory = memory;
if (memory == null && _section == 'memory') _section = 'notes';
_loading = false;
});
} catch (error) {
@@ -93,7 +105,7 @@ class _KnowledgePanelState extends State<KnowledgePanel> {
return Column(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(12, 0, 4, 0),
padding: const EdgeInsets.fromLTRB(12, 6, 4, 0),
child: Row(
children: [
Expanded(
@@ -103,7 +115,9 @@ class _KnowledgePanelState extends State<KnowledgePanel> {
style: const TextStyle(fontSize: 13),
decoration: InputDecoration(
isDense: true,
hintText: '搜索笔记、待办与计划',
hintText: _memory == null
? '搜索笔记、待办与计划'
: '搜索笔记、待办、计划与记忆',
prefixIcon: const Icon(Icons.search, size: 18),
suffixIcon: _query.isEmpty
? null
@@ -134,25 +148,31 @@ class _KnowledgePanelState extends State<KnowledgePanel> {
);
}
Widget _sections(ThemeData theme) {
final context_ = _context;
final counts = [
context_?.notes.length ?? 0,
context_?.todos.length ?? 0,
context_?.plans.length ?? 0,
List<(String, String, int)> get _sectionsList {
final context = _context;
return [
('notes', '笔记', context?.notes.length ?? 0),
('todos', '待办', context?.todos.length ?? 0),
('plans', '计划', context?.plans.length ?? 0),
if (_memory != null)
('memory', '记忆',
(_memory!.global.length + _memory!.project.length)),
];
const names = ['笔记', '待办', '计划'];
}
Widget _sections(ThemeData theme) {
final sections = _sectionsList;
return Padding(
padding: const EdgeInsets.fromLTRB(12, 8, 12, 6),
child: SegmentedButton<int>(
child: SegmentedButton<String>(
showSelectedIcon: false,
style: const ButtonStyle(
visualDensity: VisualDensity.compact,
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
segments: [
for (var i = 0; i < names.length; i++)
ButtonSegment(value: i, label: Text('${names[i]} ${counts[i]}')),
for (final (key, label, count) in sections)
ButtonSegment(value: key, label: Text('$label $count')),
],
selected: {_section},
onSelectionChanged: (value) => setState(() => _section = value.first),
@@ -180,10 +200,12 @@ class _KnowledgePanelState extends State<KnowledgePanel> {
);
}
switch (_section) {
case 1:
case 'todos':
return _todos(theme);
case 2:
case 'plans':
return _plans(theme);
case 'memory':
return _memoryBody(theme);
default:
return _notes(theme);
}
@@ -480,6 +502,195 @@ class _KnowledgePanelState extends State<KnowledgePanel> {
);
}
// -------------------------------------------------------------- memory
Widget _memoryBody(ThemeData theme) {
final snapshot = _memory;
if (snapshot == null) {
return _empty(theme, Icons.psychology_outlined, '记忆功能未启用');
}
final global = snapshot.global
.where((m) => _matches([m.title, m.body]))
.toList();
final project = snapshot.project
.where((m) => _matches([m.title, m.body]))
.toList();
if (global.isEmpty && project.isEmpty) {
return _empty(
theme,
Icons.psychology_outlined,
_query.isEmpty ? '还没有记忆' : '没有匹配的记忆');
}
return ListView(
padding: const EdgeInsets.fromLTRB(12, 0, 12, 12),
children: [
_memoryGroup(theme, '全局记忆', global, 'global',
failed: snapshot.globalFailed),
const SizedBox(height: 8),
_memoryGroup(theme, '项目记忆', project, 'project',
failed: snapshot.projectFailed),
Padding(
padding: const EdgeInsets.only(top: 6),
child: Text(
'记忆由 agent 在会话中记录;你可以在这里修改或删除它。',
style: TextStyle(fontSize: 11.5, color: theme.hintColor),
),
),
],
);
}
Widget _memoryGroup(
ThemeData theme, String title, List<AgentMemoryEntry> entries, String scope,
{required bool failed}) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 4),
child: Text('$title(${entries.length})',
style: TextStyle(fontSize: 12, color: theme.hintColor)),
),
if (failed)
Padding(
padding: const EdgeInsets.only(bottom: 4),
child: Text('读取失败,请刷新',
style: TextStyle(
fontSize: 12, color: theme.colorScheme.error)),
)
else if (entries.isEmpty)
Padding(
padding: const EdgeInsets.only(bottom: 4),
child: Text('暂无', style: TextStyle(fontSize: 12, color: theme.hintColor)),
),
for (final entry in entries) _memoryCard(theme, entry, scope),
],
);
}
Widget _memoryCard(ThemeData theme, AgentMemoryEntry entry, String scope) {
final scheme = theme.colorScheme;
return Card(
margin: const EdgeInsets.only(bottom: 8),
child: InkWell(
borderRadius: BorderRadius.circular(12),
onTap: () => _editMemory(entry, scope),
child: Padding(
padding: const EdgeInsets.fromLTRB(12, 8, 4, 8),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Flexible(
child: Text(entry.title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: const TextStyle(
fontSize: 13.5, fontWeight: FontWeight.w600)),
),
const SizedBox(width: 6),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 5, vertical: 1),
decoration: BoxDecoration(
color: scheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(4),
),
child: Text(entry.typeLabel,
style: TextStyle(
fontSize: 10, color: theme.hintColor)),
),
if (entry.flagged)
Padding(
padding: const EdgeInsets.only(left: 4),
child: Icon(Icons.flag_outlined,
size: 13, color: scheme.error),
),
],
),
const SizedBox(height: 3),
Text(entry.body,
maxLines: 4,
overflow: TextOverflow.ellipsis,
style: const TextStyle(fontSize: 12.5, height: 1.35)),
],
),
),
PopupMenuButton<String>(
icon: const Icon(Icons.more_vert, size: 18),
onSelected: (value) async {
if (value == 'edit') {
await _editMemory(entry, scope);
} else if (value == 'delete') {
await _deleteMemory(entry, scope);
}
},
itemBuilder: (_) => const [
PopupMenuItem(value: 'edit', child: Text('编辑')),
PopupMenuItem(value: 'delete', child: Text('删除')),
],
),
],
),
),
),
);
}
Future<void> _editMemory(AgentMemoryEntry entry, String scope) async {
final client = _client;
if (client == null) return;
final result = await showDialog<(String, String, String)>(
context: context,
builder: (_) => _MemoryDialog(entry: entry),
);
if (result == null) return;
try {
await client.updateMemory(entry.id,
scope: scope,
projectId: _projectId,
title: result.$1,
body: result.$2,
type: result.$3,
directory: widget.directory);
await _reloadMemory();
} catch (error) {
_fail(error);
}
}
Future<void> _deleteMemory(AgentMemoryEntry entry, String scope) async {
final client = _client;
if (client == null) return;
try {
await client.deleteMemory(entry.id,
scope: scope,
projectId: _projectId,
directory: widget.directory);
await _reloadMemory();
} catch (error) {
_fail(error);
}
}
Future<void> _reloadMemory() async {
final client = _client;
if (client == null) return;
try {
final memory = await client.agentMemory(_projectId,
directory: widget.directory);
if (!mounted) return;
setState(() => _memory = memory);
} catch (error) {
_fail(error);
}
}
// -------------------------------------------------------------- dialogs
Widget _sectionHeader(
@@ -840,3 +1051,91 @@ class _NewPlanDialogState extends State<_NewPlanDialog> {
);
}
}
/// Edit one memory entry: its title, its text, and what kind it is.
class _MemoryDialog extends StatefulWidget {
final AgentMemoryEntry entry;
const _MemoryDialog({required this.entry});
@override
State<_MemoryDialog> createState() => _MemoryDialogState();
}
class _MemoryDialogState extends State<_MemoryDialog> {
late final TextEditingController _title =
TextEditingController(text: widget.entry.title);
late final TextEditingController _body =
TextEditingController(text: widget.entry.body);
late String _type = widget.entry.type;
@override
void dispose() {
_title.dispose();
_body.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('编辑记忆'),
content: SizedBox(
width: double.maxFinite,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
controller: _title,
autofocus: true,
maxLength: 120,
decoration: const InputDecoration(
labelText: '标题',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 8),
SegmentedButton<String>(
showSelectedIcon: false,
style: const ButtonStyle(
visualDensity: VisualDensity.compact,
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
segments: const [
ButtonSegment(value: 'fact', label: Text('事实')),
ButtonSegment(value: 'preference', label: Text('偏好')),
ButtonSegment(value: 'reference', label: Text('参考')),
],
selected: {_type},
onSelectionChanged: (value) =>
setState(() => _type = value.first),
),
const SizedBox(height: 8),
TextField(
controller: _body,
maxLength: 2000,
maxLines: 6,
minLines: 4,
decoration: const InputDecoration(
alignLabelWithHint: true,
labelText: '内容',
border: OutlineInputBorder(),
),
),
],
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context), child: const Text('取消')),
FilledButton(
onPressed: () {
final title = _title.text.trim();
if (title.isEmpty) return;
Navigator.pop(context, (title, _body.text.trim(), _type));
},
child: const Text('保存'),
),
],
);
}
}