Files
pipelines/app/lib/features/chat/conversation/chat_harness_command_loader.dart
T
alan d1390994cc Chat: offer the session's Claude Code skills and slash commands in the palette
The server reads the commands Claude Code's initialize response lists (bundled,
user, project, plugin and claude.ai-synced skills plus CLI commands, with
descriptions and argument hints), drops terminal-only ones, and serves them per
chat over a new chat_commands request without waiting for a CLI start. The
composer palette lists them after the recorded commands; choosing one inserts
"/<name> " and sending passes it to the harness as an ordinary prompt.
Codex, OpenCode and Pi chats reply with an empty list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 20:53:57 +00:00

58 lines
1.9 KiB
Dart

import 'dart:async';
import '../domain/chat_commands.dart';
/// Loads the session's harness commands (skills) for the composer palette the
/// first time a slash draft asks for them, and keeps them once found.
///
/// An empty reply is not final: the server answers at once from what it knows
/// and starts the session's CLI in the background, so the commands may arrive
/// on a later ask. An empty or failed load is retried after [retryAfter] on the
/// next slash draft; meanwhile the palette offers the recorded commands alone.
class ChatHarnessCommandLoader {
ChatHarnessCommandLoader(
this._load, {
this.retryAfter = const Duration(seconds: 3),
});
final Future<List<ChatCommand>> Function()? _load;
final Duration retryAfter;
List<ChatCommand> _commands = const <ChatCommand>[];
bool _loading = false;
DateTime? _lastEmpty;
/// The commands loaded so far, empty until a load found some.
List<ChatCommand> get commands => _commands;
/// Starts a load unless commands are known, one is running, or the last one
/// came back empty too recently; [onLoaded] runs when commands arrive.
void request(void Function() onLoaded) {
final load = _load;
if (load == null || _commands.isNotEmpty || _loading) return;
final lastEmpty = _lastEmpty;
if (lastEmpty != null &&
DateTime.now().difference(lastEmpty) < retryAfter) {
return;
}
_loading = true;
unawaited(() async {
try {
final commands = await load();
if (commands.isEmpty) {
_lastEmpty = DateTime.now();
return;
}
_commands = List<ChatCommand>.unmodifiable(commands);
onLoaded();
} catch (_) {
// The palette's recorded commands still work, and a typed skill can
// still be sent through the unknown-command offer.
_lastEmpty = DateTime.now();
} finally {
_loading = false;
}
}());
}
}