feat: slash commands and skills in the composer, readable turn errors

- Typing `/` opens a palette above the composer, dsh-Go's shape: icon, the
  first-party Chinese label, /name, a one-line description, the first match
  highlighted and a return hint. It lists the directory's commands
  (/api/command) and skills (/api/skill), filters by name, label or
  description, and keeps the field focused while it opens.
- Picking completes `/name ` when the command takes arguments (its
  description shows a `[...]` form) and runs argument-free commands at once;
  skills complete and ride the next prompt as a `skills` attachment. Enter
  picks the highlighted row, arrows move, Esc closes.
- Sends route by name: commands go through POST /api/session/:id/command so
  the server expands the template (and writes the user message), skills go
  through the normal prompt with their id attached, anything else stays a
  plain message. A failed command keeps the draft for a retry.
- A failed turn now says so in the transcript: the last step's error is
  rendered under it as a quiet red block with the 中文 reading (auth, retry,
  stopped, send failure) and the provider's raw response behind 显示详情.
  Failed compactions and idle errors not already carried by the step are
  shown too; an error that a later step recovered from is not the turn's
  outcome and stays hidden.
- The tail is refetched when a turn starts, so a command's server-written
  user message (or another client's) appears without reopening the session;
  a successful send clears the stale error banner.
Version 0.3.6 (code 306).
This commit is contained in:
blumia committed 2026-11-20 02:20:35 +08:00
1 parent d7cec9d155
commit 6c00e5e026
7 files changed
+725 -14

No files matched your search

+151 -4
View File
@@ -479,6 +479,25 @@ class AppState extends ChangeNotifier {
// The open chat.
// ---------------------------------------------------------------------------
/// One entry of the composer's `/` palette: a command registered for the
/// directory, or a skill opencode can attach to a prompt.
class SlashItem {
final String name;
final String description;
final bool skill;
final String id;
const SlashItem({
required this.name,
this.description = '',
this.skill = false,
this.id = '',
});
/// Commands whose description shows an argument form (`[commit|branch|pr]`)
/// expect one; the palette completes those and runs the rest outright.
bool get takesArgs => description.contains('[');
}
class ChatState extends ChangeNotifier {
final AppState app;
final Session session;
@@ -498,6 +517,11 @@ class ChatState extends ChangeNotifier {
ModelRef? selectedModel;
String selectedAgent;
/// `/` palette contents, loaded once per open chat.
List<SlashItem> slashItems = [];
bool slashLoading = false;
bool _slashLoaded = false;
ChatState(this.app, this.session)
: selectedModel = session.model,
selectedAgent = session.agent;
@@ -520,6 +544,7 @@ class ChatState extends ChangeNotifier {
olderCursor = page.next;
hasMore = page.next != null && page.data.length >= _pageSize;
loading = false;
error = null;
notifyListeners();
await reloadTail();
} catch (e) {
@@ -529,6 +554,54 @@ class ChatState extends ChangeNotifier {
}
}
/// Loads the directory's commands and skills for the `/` palette. Called
/// when the palette first opens and before a `/name` send, so a typed
/// command never falls through to the model as plain text.
Future<void> ensureSlash() async {
if (_slashLoaded || slashLoading) return;
final cl = app.client;
if (cl == null) return;
slashLoading = true;
notifyListeners();
try {
final results = await Future.wait([
cl.commands(directory: dir),
cl.skills(directory: dir),
]);
slashItems = [
for (final c in results[0])
if ((c['name'] ?? '').isNotEmpty)
SlashItem(
name: c['name']!,
description: c['description'] ?? '',
),
for (final k in results[1])
if ((k['name'] ?? '').isNotEmpty)
SlashItem(
name: k['name']!,
description: k['description'] ?? '',
skill: true,
id: k['id'] ?? '',
),
];
_slashLoaded = true;
} catch (_) {
// Keep the palette empty; a send falls back to a plain prompt.
}
slashLoading = false;
notifyListeners();
}
SlashItem? slashFor(String text) {
final m = RegExp(r'^/([^\s/]+)').firstMatch(text.trimLeft());
if (m == null) return null;
final name = m.group(1)!;
for (final item in slashItems) {
if (item.name == name) return item;
}
return null;
}
/// Refreshes the newest page without dropping the older pages that were
/// already loaded; the server's copy wins for messages it returns.
Future<void> reloadTail() async {
@@ -591,8 +664,61 @@ class ChatState extends ChangeNotifier {
Future<String?> send({String? text, List<Map<String, String>>? files}) async {
final t = (text ?? draft).trim();
if (t.isEmpty && (files == null || files.isEmpty)) return null;
if (t.startsWith('/')) {
await ensureSlash();
final item = slashFor(t);
if (item != null) {
final args =
(RegExp(r'^/\S+\s*([\s\S]*)$').firstMatch(t)?.group(1) ?? '').trim();
draft = '';
if (!item.skill) {
// A command is expanded by the server, which also writes the user
// message; there is no id to show optimistically.
try {
await app.client!.command(this.id,
name: item.name, text: args, directory: dir, files: files);
error = null;
await reloadTail();
return '';
} catch (e) {
draft = t;
error = e.toString();
notifyListeners();
return null;
}
}
final id = _newMsgId();
msgs.add(Message(
id: id,
type: 'user',
text: t,
created: DateTime.now().millisecondsSinceEpoch,
files: files?.map((f) => FileAttachment(f['name'] ?? '附件', '')).toList() ?? const [],
));
_sortMsgs();
notifyListeners();
try {
// The skill is attached by id; the text still names it, as the
// official composer sends it.
await app.client!.prompt(this.id,
text: t, messageId: id, directory: dir, files: files,
skillIds: [item.id]);
error = null;
notifyListeners();
return id;
} catch (e) {
msgs.removeWhere((m) => m.id == id);
_reindex();
draft = t;
error = e.toString();
notifyListeners();
return null;
}
}
}
final id = _newMsgId();
draft = '';
error = null;
msgs.add(Message(
id: id,
type: 'user',
@@ -604,6 +730,10 @@ class ChatState extends ChangeNotifier {
notifyListeners();
try {
await app.client!.prompt(this.id, text: t, messageId: id, directory: dir, files: files);
if (error != null) {
error = null;
notifyListeners();
}
return id;
} catch (e) {
// The prompt did not reach the server: take the optimistic message back
@@ -719,6 +849,9 @@ class ChatState extends ChangeNotifier {
switch (type) {
case 'session.execution.started':
running = true;
// A command's user message is written by the server, and another
// client may have sent it: take the tail from the server.
reloadTail();
break;
case 'session.execution.succeeded':
case 'session.execution.failed':
@@ -745,7 +878,12 @@ class ChatState extends ChangeNotifier {
cost: (data['cost'] as num?)?.toDouble() ?? m.cost,
tokens: data['tokens'] == null
? m.tokens
: TokenUsage.fromJson(data['tokens'] as Map<String, dynamic>)));
: TokenUsage.fromJson(data['tokens'] as Map<String, dynamic>),
error: data['error'] == null
? m.error
: MessageError.fromJson(data['error'] as Map<String, dynamic>),
status: m.status,
reason: m.reason));
// 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.
@@ -841,7 +979,10 @@ class ChatState extends ChangeNotifier {
files: m.files,
finish: m.finish,
cost: m.cost,
tokens: m.tokens);
tokens: m.tokens,
error: m.error,
status: m.status,
reason: m.reason);
});
}
@@ -901,7 +1042,10 @@ class ChatState extends ChangeNotifier {
files: m.files,
finish: m.finish,
cost: m.cost,
tokens: m.tokens);
tokens: m.tokens,
error: m.error,
status: m.status,
reason: m.reason);
}
}
content.add(part);
@@ -917,7 +1061,10 @@ class ChatState extends ChangeNotifier {
files: m.files,
finish: m.finish,
cost: m.cost,
tokens: m.tokens);
tokens: m.tokens,
error: m.error,
status: m.status,
reason: m.reason);
});
}