Merge branch 'worktree-agent-a672970b929d7358c'
# Conflicts: # internal/pipeline/store.go # internal/server/chat_hub.go
This commit is contained in:
@@ -32,6 +32,8 @@ import 'composer_focus_request.dart';
|
||||
import 'composer_focus_restore.dart';
|
||||
import 'composer_focus_ring.dart';
|
||||
import '../ui/sessions/chat_color_palette.dart';
|
||||
import '../shell/chat_shell_command.dart';
|
||||
import '../shell/chat_shell_mode_indicator.dart';
|
||||
|
||||
/// The composer's placeholder when the chat names no working directory.
|
||||
const String chatComposerPlaceholder = 'Message the agent';
|
||||
@@ -74,6 +76,9 @@ double chatComposerBottomMargin([TargetPlatform? platform]) =>
|
||||
const String chatSendLabel = 'Send';
|
||||
const String chatStopLabel = 'Stop';
|
||||
|
||||
/// The primary control's words while the draft is a `!` shell command.
|
||||
const String chatRunShellLabel = 'Run command';
|
||||
|
||||
/// The attach control's accessible name, verbatim from the web composer.
|
||||
const String chatAttachLabel = 'Attach file';
|
||||
|
||||
@@ -122,6 +127,7 @@ class ChatComposerBar extends StatefulWidget {
|
||||
this.paste,
|
||||
this.pasteTarget = true,
|
||||
this.backgroundTasks,
|
||||
this.runShell,
|
||||
this.placeholder = chatComposerPlaceholder,
|
||||
this.enterSends,
|
||||
this.images,
|
||||
@@ -161,6 +167,11 @@ class ChatComposerBar extends StatefulWidget {
|
||||
final bool pasteTarget;
|
||||
final Widget? backgroundTasks;
|
||||
|
||||
/// Runs a `!` draft's command in the chat's folder (never a prompt);
|
||||
/// resolves to the server's refusal, or null once it started. Null (no
|
||||
/// session) reports shell mode as unavailable and sends nothing.
|
||||
final Future<String?> Function(String command)? runShell;
|
||||
|
||||
@override
|
||||
State<ChatComposerBar> createState() => _ChatComposerBarState();
|
||||
}
|
||||
@@ -206,6 +217,9 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
String? _commandOffer;
|
||||
bool _commandFailed = false;
|
||||
|
||||
/// True while a `!` command is being started.
|
||||
bool _shellStarting = false;
|
||||
|
||||
ChatAttachmentPicker get _picker => widget.picker ?? hostChatAttachmentPicker;
|
||||
ChatClipboardPaste get _paste => widget.paste ?? hostChatClipboardPaste;
|
||||
|
||||
@@ -487,6 +501,10 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
_completeCommand(commands[_selectedCommand % commands.length]);
|
||||
return;
|
||||
}
|
||||
if (chatDraftIsShell(widget.composer.draft)) {
|
||||
unawaited(_runShellDraft());
|
||||
return;
|
||||
}
|
||||
if (chatCommandWord(widget.composer.draft) != null) {
|
||||
_runSlashDraft();
|
||||
return;
|
||||
@@ -494,6 +512,38 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
unawaited(_send());
|
||||
}
|
||||
|
||||
/// A `!` draft runs as a shell command and is never sent to the agent. The
|
||||
/// draft is cleared once the server started it; a refusal keeps it.
|
||||
Future<void> _runShellDraft() async {
|
||||
final draft = widget.composer.draft;
|
||||
final command = chatShellCommandOf(draft) ?? '';
|
||||
final run = widget.runShell;
|
||||
if (run == null) {
|
||||
_showCommandNotice(
|
||||
'Shell commands are not available here, so nothing was sent.',
|
||||
failed: true,
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (command.isEmpty) {
|
||||
_showCommandNotice('Type a command after !.', failed: true);
|
||||
return;
|
||||
}
|
||||
if (_shellStarting) return;
|
||||
setState(() {
|
||||
_shellStarting = true;
|
||||
_dismissCommandReport();
|
||||
});
|
||||
final refusal = await run(command);
|
||||
if (!mounted) return;
|
||||
setState(() => _shellStarting = false);
|
||||
if (refusal != null) {
|
||||
_showCommandNotice(refusal, failed: true);
|
||||
return;
|
||||
}
|
||||
if (widget.composer.draft == draft) _consumeCommandDraft();
|
||||
}
|
||||
|
||||
/// Routes a slash draft locally or through the existing Stop controller path.
|
||||
void _runSlashDraft() {
|
||||
final draft = widget.composer.draft;
|
||||
@@ -767,6 +817,8 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
// inside the glass with the field, as one floating composer; only the
|
||||
// command palette, a separate menu, floats above it.
|
||||
final extras = <Widget>[
|
||||
if (chatDraftIsShell(composer.draft))
|
||||
ChatShellModeIndicator(cupertino: cupertino),
|
||||
if (!_attachments.isEmpty) _attachmentStrip(context, cupertino),
|
||||
if (_uploading)
|
||||
_notice(
|
||||
@@ -1261,7 +1313,10 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
final composer = widget.composer;
|
||||
final enabled = stop
|
||||
? !composer.stopping
|
||||
: !_uploading && !composer.sending && composer.draft.trim().isNotEmpty;
|
||||
: !_uploading &&
|
||||
!_shellStarting &&
|
||||
!composer.sending &&
|
||||
composer.draft.trim().isNotEmpty;
|
||||
void press() {
|
||||
if (stop) {
|
||||
unawaited(composer.stop());
|
||||
@@ -1270,6 +1325,8 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
final commands = _paletteCommands();
|
||||
if (commands.isNotEmpty) {
|
||||
_completeCommand(commands[_selectedCommand % commands.length]);
|
||||
} else if (chatDraftIsShell(composer.draft)) {
|
||||
unawaited(_runShellDraft());
|
||||
} else if (chatCommandWord(composer.draft) != null) {
|
||||
_runSlashDraft();
|
||||
} else {
|
||||
@@ -1279,7 +1336,11 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
|
||||
// Sending while the turn runs uses the server's existing queued-prompt path.
|
||||
// Stopping is always a separate, explicitly named action.
|
||||
final label = stop ? chatStopLabel : chatSendLabel;
|
||||
final label = stop
|
||||
? chatStopLabel
|
||||
: chatDraftIsShell(composer.draft)
|
||||
? chatRunShellLabel
|
||||
: chatSendLabel;
|
||||
final key = Key(stop ? 'chat-composer-stop' : 'chat-composer-primary');
|
||||
if (cupertino) {
|
||||
return Semantics(
|
||||
|
||||
@@ -59,6 +59,11 @@ import 'chat_transcript_cache.dart';
|
||||
import '../files/files_controller.dart';
|
||||
import '../files/files_panel.dart';
|
||||
import '../files/files_preferences.dart';
|
||||
import '../shell/chat_shell_command.dart';
|
||||
import '../shell/chat_shell_run.dart';
|
||||
import '../shell/chat_shell_run_card.dart';
|
||||
import '../shell/chat_shell_runs_controller.dart';
|
||||
import '../shell/chat_shell_thread_support.dart';
|
||||
import 'chat_transcript_scroll_view.dart';
|
||||
import 'chat_permission_card_model.dart';
|
||||
import 'chat_permission_group.dart';
|
||||
@@ -184,6 +189,11 @@ class ChatThreadView extends StatefulWidget {
|
||||
class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
late ChatThreadController _controller = _attach();
|
||||
late ChatComposerController _composer = _attachComposer();
|
||||
|
||||
/// The operator's own shell commands in this chat (see `../shell/`).
|
||||
late ChatShellRunsController _shellRuns = _attachShellRuns();
|
||||
ChatShellRunsController _attachShellRuns() =>
|
||||
ChatShellRunsController(widget.gateway, widget.sessionId);
|
||||
late ChatRowKeys _rowKeys = ChatRowKeys();
|
||||
final _sentEntrances = ChatSentEntrances();
|
||||
final _rowWidgets = ChatRowWidgets();
|
||||
@@ -577,6 +587,8 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
}
|
||||
_controller.dispose();
|
||||
_composer.dispose();
|
||||
_shellRuns.dispose();
|
||||
_shellRuns = _attachShellRuns();
|
||||
_files.removeListener(_persistFilesPreferences);
|
||||
_files.dispose();
|
||||
_files = _newFilesController()..addListener(_persistFilesPreferences);
|
||||
@@ -613,6 +625,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
// so no earlier dismissal survives and nothing recorded before the gap is
|
||||
// painted while the repair runs.
|
||||
_dismissals.clear();
|
||||
unawaited(_shellRuns.refresh());
|
||||
_preGapMessages = _controller.messages;
|
||||
_reconnecting = true;
|
||||
final generation = ++_syncGeneration;
|
||||
@@ -650,6 +663,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
_scroll.dispose();
|
||||
_composer.dispose();
|
||||
_controller.dispose();
|
||||
_shellRuns.dispose();
|
||||
_files.removeListener(_persistFilesPreferences);
|
||||
_files.dispose();
|
||||
super.dispose();
|
||||
@@ -965,6 +979,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
animation: Listenable.merge([
|
||||
_controller.transcriptChanges,
|
||||
_composer.deliveries,
|
||||
_shellRuns.membership,
|
||||
]),
|
||||
builder: (context, _) {
|
||||
final cupertino =
|
||||
@@ -1020,10 +1035,11 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
);
|
||||
final notices = _dismissals.visible(allNotices);
|
||||
final compactionNotice = _noticeOf(notices, ChatNoticeKind.compaction);
|
||||
final rows = [
|
||||
// The operator's own shell commands sit among the turns by start time.
|
||||
final rows = withChatShellRunRows([
|
||||
for (final row in allRows)
|
||||
if (!_hiddenRunningCompaction(row)) row,
|
||||
];
|
||||
], _shellRuns.runs);
|
||||
// Older pages prepend messages and a live frame can append one, so a
|
||||
// folded assistant turn's first message id is not a stable identity.
|
||||
// ChatRowKeys carries a turn's key forward while it still holds any of
|
||||
@@ -1072,6 +1088,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
),
|
||||
chatColor: chatSessionColor(widget.session),
|
||||
pasteTarget: widget.pasteTarget,
|
||||
runShell: _shellRuns.start,
|
||||
backgroundTasks: ChatBackgroundTasks(
|
||||
key: ValueKey('chat-background-tasks-${widget.sessionId}'),
|
||||
gateway: widget.gateway,
|
||||
@@ -1368,7 +1385,11 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
: CupertinoIcons.folder,
|
||||
),
|
||||
],
|
||||
moreActions: [?widget.sidebarControl, ...widget.paneControls],
|
||||
moreActions: [
|
||||
?widget.sidebarControl,
|
||||
...widget.paneControls,
|
||||
chatShellRunMenuAction(onPressed: _runShellFromMenu),
|
||||
],
|
||||
controls: [
|
||||
if (hasModel)
|
||||
ChatModelControl(
|
||||
@@ -1395,6 +1416,15 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
onChanged: widget.onModeChanged,
|
||||
);
|
||||
|
||||
void _runShellFromMenu() => unawaited(
|
||||
runChatShellFromMenu(
|
||||
context,
|
||||
_shellRuns,
|
||||
cupertino: chatControlsStyleOf(context) == ChatControlsStyle.cupertino,
|
||||
showNotice: _showCommandNotice,
|
||||
),
|
||||
);
|
||||
|
||||
ChatNotice? _noticeOf(List<ChatNotice> notices, ChatNoticeKind kind) {
|
||||
for (final notice in notices) {
|
||||
if (notice.kind == kind) return notice;
|
||||
@@ -1446,6 +1476,12 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
return ChatAgentReportCard(row: row, textBuilder: chatMarkdownText);
|
||||
}
|
||||
if (kind == 'system_notice') return ChatSystemNoticeLine(row: row);
|
||||
if (kind == chatShellRunRowKind) {
|
||||
return ChatShellRunCard(
|
||||
controller: _shellRuns,
|
||||
runId: '${row['run_id']}',
|
||||
);
|
||||
}
|
||||
if (kind == 'task_notices') return ChatTaskNoticesGroup(row: row);
|
||||
if (kind == 'questions') return ChatQuestionsRow(row: row);
|
||||
if (kind == 'permission' || kind == 'question') {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import '../shell/chat_shell_command.dart';
|
||||
|
||||
/// One command the chat composer offers. [name] is the word after the slash,
|
||||
/// [usage] is what the operator types (including any argument) and [summary] is
|
||||
/// the few-word description shown beside it. [takesArgument] is false for the
|
||||
@@ -113,10 +115,12 @@ String chatCommandArgument(String draft) {
|
||||
return draft.trimLeft().substring(word.length).trim();
|
||||
}
|
||||
|
||||
/// `/help`'s local listing, in the same order and wording as the palette.
|
||||
String chatCommandHelpText() => chatCommands
|
||||
.map((command) => '${command.usage} — ${command.summary}')
|
||||
.join('\n');
|
||||
/// `/help`'s local listing, in the same order and wording as the palette,
|
||||
/// then the `!` shell-command hint.
|
||||
String chatCommandHelpText() => [
|
||||
for (final command in chatCommands) '${command.usage} — ${command.summary}',
|
||||
chatShellHelpLine,
|
||||
].join('\n');
|
||||
|
||||
/// The local no-send report for a slash draft naming no recorded command,
|
||||
/// verbatim from the web client's unknown-command notice. The report offers the
|
||||
|
||||
@@ -0,0 +1,177 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import 'chat_shell_metrics.dart';
|
||||
import 'chat_shell_run.dart';
|
||||
import 'chat_shell_runs_controller.dart';
|
||||
|
||||
const String chatShellStopLabel = 'Stop';
|
||||
const String chatShellStoppingLabel = 'Stopping…';
|
||||
const String chatShellCopyLabel = 'Copy output';
|
||||
const String chatShellCopiedLabel = 'Copied';
|
||||
const String chatShellShareLabel = 'Send to agent';
|
||||
const String chatShellSharingLabel = 'Sending…';
|
||||
const String chatShellSharedLabel = 'Sent to agent';
|
||||
const String chatShellExpandLabel = 'Expand';
|
||||
const String chatShellCollapseLabel = 'Collapse';
|
||||
|
||||
/// The card's host buttons: Stop while running; Copy output, Send to agent
|
||||
/// once finished; Expand/Collapse for the output area.
|
||||
class ChatShellCardActions extends StatefulWidget {
|
||||
const ChatShellCardActions({
|
||||
super.key,
|
||||
required this.run,
|
||||
required this.controller,
|
||||
required this.cupertino,
|
||||
required this.expanded,
|
||||
required this.onToggleExpanded,
|
||||
});
|
||||
|
||||
final ChatShellRun run;
|
||||
final ChatShellRunsController controller;
|
||||
final bool cupertino;
|
||||
final bool expanded;
|
||||
final VoidCallback onToggleExpanded;
|
||||
|
||||
@override
|
||||
State<ChatShellCardActions> createState() => _ChatShellCardActionsState();
|
||||
}
|
||||
|
||||
class _ChatShellCardActionsState extends State<ChatShellCardActions> {
|
||||
bool _copied = false;
|
||||
Timer? _reset;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_reset?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
/// Copies everything the server kept, reading it first when this card
|
||||
/// holds only the newest part.
|
||||
Future<void> _copy() async {
|
||||
final full = await widget.controller.loadAll(widget.run.id);
|
||||
if (full == null) return;
|
||||
await Clipboard.setData(ClipboardData(text: full.output));
|
||||
if (!mounted) return;
|
||||
setState(() => _copied = true);
|
||||
_reset?.cancel();
|
||||
_reset = Timer(const Duration(seconds: 2), () {
|
||||
if (mounted) setState(() => _copied = false);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final run = widget.run;
|
||||
final controller = widget.controller;
|
||||
final share = controller.shareState(run.id);
|
||||
final stopping = controller.stopping(run.id);
|
||||
return Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 4,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: [
|
||||
if (run.running)
|
||||
_button(
|
||||
key: const Key('chat-shell-run-stop'),
|
||||
label: stopping ? chatShellStoppingLabel : chatShellStopLabel,
|
||||
material: Icons.stop,
|
||||
cupertino: CupertinoIcons.stop_fill,
|
||||
destructive: true,
|
||||
onPressed: stopping
|
||||
? null
|
||||
: () => unawaited(controller.stop(run.id)),
|
||||
),
|
||||
if (!run.running && run.outputBytes > 0)
|
||||
_button(
|
||||
key: const Key('chat-shell-run-copy'),
|
||||
label: _copied ? chatShellCopiedLabel : chatShellCopyLabel,
|
||||
material: _copied ? Icons.check : Icons.copy_outlined,
|
||||
cupertino: _copied
|
||||
? CupertinoIcons.checkmark
|
||||
: CupertinoIcons.doc_on_doc,
|
||||
onPressed: () => unawaited(_copy()),
|
||||
),
|
||||
if (!run.running)
|
||||
_button(
|
||||
key: const Key('chat-shell-run-share'),
|
||||
label: switch (share) {
|
||||
ChatShellShareState.idle => chatShellShareLabel,
|
||||
ChatShellShareState.sending => chatShellSharingLabel,
|
||||
ChatShellShareState.sent => chatShellSharedLabel,
|
||||
},
|
||||
material: share == ChatShellShareState.sent
|
||||
? Icons.check
|
||||
: Icons.send_outlined,
|
||||
cupertino: share == ChatShellShareState.sent
|
||||
? CupertinoIcons.checkmark
|
||||
: CupertinoIcons.paperplane,
|
||||
onPressed: share == ChatShellShareState.idle
|
||||
? () => unawaited(controller.share(run.id))
|
||||
: null,
|
||||
),
|
||||
if (widget.expanded || chatShellOutputMayOverflow(run.output))
|
||||
_button(
|
||||
key: const Key('chat-shell-run-expand'),
|
||||
label: widget.expanded
|
||||
? chatShellCollapseLabel
|
||||
: chatShellExpandLabel,
|
||||
material: widget.expanded ? Icons.unfold_less : Icons.unfold_more,
|
||||
cupertino: widget.expanded
|
||||
? CupertinoIcons.chevron_up
|
||||
: CupertinoIcons.chevron_down,
|
||||
onPressed: widget.onToggleExpanded,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _button({
|
||||
required Key key,
|
||||
required String label,
|
||||
required IconData material,
|
||||
required IconData cupertino,
|
||||
required VoidCallback? onPressed,
|
||||
bool destructive = false,
|
||||
}) {
|
||||
if (widget.cupertino) {
|
||||
final color = destructive
|
||||
? CupertinoColors.systemRed.resolveFrom(context)
|
||||
: null;
|
||||
return CupertinoButton(
|
||||
key: key,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8),
|
||||
onPressed: onPressed,
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(cupertino, color: onPressed == null ? null : color),
|
||||
const SizedBox(width: 6),
|
||||
Text(
|
||||
label,
|
||||
style: onPressed == null || color == null
|
||||
? null
|
||||
: TextStyle(color: color),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
final style = destructive
|
||||
? TextButton.styleFrom(
|
||||
foregroundColor: Theme.of(context).colorScheme.error,
|
||||
)
|
||||
: null;
|
||||
return TextButton.icon(
|
||||
key: key,
|
||||
style: style,
|
||||
onPressed: onPressed,
|
||||
icon: Icon(material),
|
||||
label: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../domain/chat_transcript.dart';
|
||||
import 'chat_shell_run.dart';
|
||||
|
||||
/// The composer's shell prefix: a draft starting with it runs as a command in
|
||||
/// the chat's directory instead of being sent to the agent.
|
||||
const String chatShellPrefix = '!';
|
||||
|
||||
/// The `/help` line for shell mode.
|
||||
const String chatShellHelpLine =
|
||||
'!<command> — Run a shell command in this chat’s folder; '
|
||||
'the agent does not see it unless you send it';
|
||||
|
||||
/// Shell mode's visible label and its explanation beside the composer.
|
||||
const String chatShellModeLabel = 'Shell';
|
||||
const String chatShellModeHint =
|
||||
'Runs in this chat’s folder on its machine. The agent does not see it.';
|
||||
|
||||
/// The chat actions menu entry and its dialog.
|
||||
const String chatShellRunActionLabel = 'Run command…';
|
||||
const String chatShellRunDialogTitle = 'Run command';
|
||||
const String chatShellRunDialogConfirm = 'Run';
|
||||
|
||||
/// Whether [draft] is in shell mode: its first non-blank character is `!`.
|
||||
bool chatDraftIsShell(String draft) =>
|
||||
draft.trimLeft().startsWith(chatShellPrefix);
|
||||
|
||||
/// The command a shell-mode draft runs (trimmed, without the `!`), or null
|
||||
/// when [draft] is not in shell mode. An empty string means only `!` was typed.
|
||||
String? chatShellCommandOf(String draft) {
|
||||
if (!chatDraftIsShell(draft)) return null;
|
||||
return draft.trimLeft().substring(chatShellPrefix.length).trim();
|
||||
}
|
||||
|
||||
/// [rows] with one card row per run, placed by start time among the rows
|
||||
/// that carry a recorded time. Rows without a time (a just-sent message, the
|
||||
/// pending reply) keep their place; runs newer than every timed row follow.
|
||||
List<JsonMap> withChatShellRunRows(
|
||||
List<JsonMap> rows,
|
||||
List<ChatShellRun> runs,
|
||||
) {
|
||||
if (runs.isEmpty) return rows;
|
||||
final ordered = [...runs]..sort((a, b) => a.startedAt.compareTo(b.startedAt));
|
||||
final merged = <JsonMap>[];
|
||||
var next = 0;
|
||||
for (final row in rows) {
|
||||
final time = _rowTime(row);
|
||||
while (time != null &&
|
||||
next < ordered.length &&
|
||||
ordered[next].startedAt < time) {
|
||||
merged.add(ordered[next++].toRow());
|
||||
}
|
||||
merged.add(row);
|
||||
}
|
||||
while (next < ordered.length) {
|
||||
merged.add(ordered[next++].toRow());
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
|
||||
int? _rowTime(JsonMap row) {
|
||||
final created = row['created_at'];
|
||||
if (created is int && created > 0) return created;
|
||||
final message = row['message'];
|
||||
if (message is Map) {
|
||||
return chatMessageCreatedAt(Map<String, dynamic>.from(message));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// The finished card's status words.
|
||||
String chatShellStatusLabel(ChatShellRun run) => switch (run.state) {
|
||||
ChatShellRunState.running => 'Running',
|
||||
ChatShellRunState.succeeded => 'Exit 0',
|
||||
ChatShellRunState.stopped => 'Stopped',
|
||||
ChatShellRunState.interrupted => 'Interrupted',
|
||||
ChatShellRunState.failed =>
|
||||
run.signal.isNotEmpty
|
||||
? 'Killed (${run.signal})'
|
||||
: run.exitCode != null
|
||||
? 'Exit ${run.exitCode}'
|
||||
: 'Failed',
|
||||
};
|
||||
|
||||
/// How long the command ran, as the status chip's tail.
|
||||
String chatShellDuration(int milliseconds) {
|
||||
if (milliseconds < 1000) return '$milliseconds ms';
|
||||
final seconds = milliseconds / 1000;
|
||||
if (seconds < 60) return '${seconds.toStringAsFixed(1)} s';
|
||||
final minutes = seconds ~/ 60;
|
||||
return '${minutes}m ${(seconds % 60).round()}s';
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
/// The one layout bound of a shell command card: how tall its output area
|
||||
/// may grow. Collapsed it shows about [kChatShellOutputCollapsedLines] lines
|
||||
/// of the host's (scaled) monospace text; expanded it may take
|
||||
/// [kChatShellOutputExpandedShare] of the window height. Everything else in
|
||||
/// the card is content-sized.
|
||||
const int kChatShellOutputCollapsedLines = 12;
|
||||
const double kChatShellOutputExpandedShare = 0.6;
|
||||
|
||||
/// The monospace line height factor the output text is set at.
|
||||
const double kChatShellOutputLineHeight = 1.35;
|
||||
|
||||
/// Characters of output drawn at once. A command can print far more than a
|
||||
/// text layout should carry; the card says when it shows only the newest
|
||||
/// part, and Copy output still copies everything the server kept.
|
||||
const int kChatShellRenderedOutputLimit = 64 * 1024;
|
||||
|
||||
/// Whether [output] may be taller than the collapsed area, so Expand is worth
|
||||
/// offering. Counted in lines, with long lines counted as wrapping.
|
||||
bool chatShellOutputMayOverflow(String output) {
|
||||
var lines = 0;
|
||||
for (final line in output.split('\n')) {
|
||||
lines += 1 + line.length ~/ 80;
|
||||
if (lines > kChatShellOutputCollapsedLines) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// The output area's maximum height for this window, text scale and font.
|
||||
double chatShellOutputMaxHeight(
|
||||
BuildContext context, {
|
||||
required double fontSize,
|
||||
required bool expanded,
|
||||
}) {
|
||||
if (expanded) {
|
||||
return MediaQuery.sizeOf(context).height * kChatShellOutputExpandedShare;
|
||||
}
|
||||
final line =
|
||||
MediaQuery.textScalerOf(context).scale(fontSize) *
|
||||
kChatShellOutputLineHeight;
|
||||
return line * kChatShellOutputCollapsedLines;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../conversation/chat_card_styles.dart';
|
||||
import 'chat_shell_command.dart';
|
||||
|
||||
/// Shown inside the composer while its draft starts with `!`: the draft is a
|
||||
/// shell command for this chat's folder, not a message to the agent.
|
||||
class ChatShellModeIndicator extends StatelessWidget {
|
||||
const ChatShellModeIndicator({super.key, required this.cupertino});
|
||||
|
||||
final bool cupertino;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final accent = cupertino
|
||||
? CupertinoTheme.of(context).primaryColor
|
||||
: Theme.of(context).colorScheme.primary;
|
||||
final label = (chatCardBodyStyle(context, cupertino) ?? const TextStyle())
|
||||
.copyWith(color: accent, fontWeight: FontWeight.w600);
|
||||
return Semantics(
|
||||
key: const Key('chat-composer-shell-mode'),
|
||||
container: true,
|
||||
liveRegion: true,
|
||||
label: '$chatShellModeLabel mode. $chatShellModeHint',
|
||||
child: ExcludeSemantics(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8),
|
||||
child: Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 2,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
cupertino
|
||||
? CupertinoIcons.chevron_left_slash_chevron_right
|
||||
: Icons.terminal,
|
||||
color: accent,
|
||||
size: MediaQuery.textScalerOf(
|
||||
context,
|
||||
).scale(label.fontSize ?? 14),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(chatShellModeLabel, style: label),
|
||||
],
|
||||
),
|
||||
Text(
|
||||
chatShellModeHint,
|
||||
style: chatCardSecondaryStyle(context, cupertino),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../ui/text/monospace.dart';
|
||||
import '../conversation/chat_card_styles.dart';
|
||||
import 'chat_shell_metrics.dart';
|
||||
import 'chat_shell_run.dart';
|
||||
|
||||
/// The terminal-style output area of one command card: the host's monospace
|
||||
/// body on a quiet inset surface, scrollable inside a bounded height and
|
||||
/// anchored at the newest line while the command runs.
|
||||
class ChatShellOutputView extends StatefulWidget {
|
||||
const ChatShellOutputView({
|
||||
super.key,
|
||||
required this.run,
|
||||
required this.cupertino,
|
||||
required this.expanded,
|
||||
});
|
||||
|
||||
final ChatShellRun run;
|
||||
final bool cupertino;
|
||||
final bool expanded;
|
||||
|
||||
@override
|
||||
State<ChatShellOutputView> createState() => _ChatShellOutputViewState();
|
||||
}
|
||||
|
||||
class _ChatShellOutputViewState extends State<ChatShellOutputView> {
|
||||
final ScrollController _scroll = ScrollController();
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_scroll.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final run = widget.run;
|
||||
final cupertino = widget.cupertino;
|
||||
final body = chatCardBodyStyle(context, cupertino) ?? const TextStyle();
|
||||
final fontSize = (body.fontSize ?? 14) * 0.95;
|
||||
final style = body.copyWith(
|
||||
fontFamily: appMonospaceFamily,
|
||||
fontSize: fontSize,
|
||||
height: kChatShellOutputLineHeight,
|
||||
);
|
||||
final secondary = chatCardSecondaryStyle(context, cupertino);
|
||||
var text = run.output;
|
||||
final clipped = text.length > kChatShellRenderedOutputLimit;
|
||||
if (clipped) {
|
||||
text = text.substring(text.length - kChatShellRenderedOutputLimit);
|
||||
}
|
||||
// The final newline ends the last line; it is not an empty line to draw.
|
||||
if (text.endsWith('\n')) text = text.substring(0, text.length - 1);
|
||||
final notes = [
|
||||
if (run.truncated) 'Earlier output was dropped.',
|
||||
if (clipped)
|
||||
'Showing only the newest part; Copy output copies all of it.',
|
||||
];
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
if (notes.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 4),
|
||||
child: Text(
|
||||
notes.join(' '),
|
||||
key: const Key('chat-shell-output-truncated'),
|
||||
style: secondary,
|
||||
),
|
||||
),
|
||||
DecoratedBox(
|
||||
decoration: BoxDecoration(
|
||||
color: chatCardFillColor(context, cupertino),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(
|
||||
maxHeight: chatShellOutputMaxHeight(
|
||||
context,
|
||||
fontSize: fontSize,
|
||||
expanded: widget.expanded,
|
||||
),
|
||||
),
|
||||
child: _scrollable(
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(10),
|
||||
child: text.isEmpty
|
||||
? Text(
|
||||
run.running ? 'No output yet.' : 'No output.',
|
||||
style: secondary,
|
||||
)
|
||||
: Text(
|
||||
text,
|
||||
key: const Key('chat-shell-output-text'),
|
||||
style: style,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// A short output sizes the box to itself; a long one scrolls inside it,
|
||||
/// starting from (and following) the newest line.
|
||||
Widget _scrollable(Widget child) {
|
||||
final scroll = SingleChildScrollView(
|
||||
controller: _scroll,
|
||||
reverse: true,
|
||||
child: SizedBox(width: double.infinity, child: child),
|
||||
);
|
||||
return widget.cupertino
|
||||
? CupertinoScrollbar(controller: _scroll, child: scroll)
|
||||
: Scrollbar(controller: _scroll, child: scroll);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
|
||||
/// How an operator shell command stands, as the card shows it.
|
||||
enum ChatShellRunState { running, succeeded, failed, stopped, interrupted }
|
||||
|
||||
/// Characters of live output one card keeps; older text is dropped (and said
|
||||
/// to be) so a flooding command cannot grow the transcript without bound. The
|
||||
/// server keeps its own 1 MiB tail, which Copy output and Load all read.
|
||||
const int chatShellLiveOutputLimit = 256 * 1024;
|
||||
|
||||
/// One command the operator ran in this chat (`chat_shell_run`), as recorded
|
||||
/// by the server: never part of the agent's transcript.
|
||||
class ChatShellRun {
|
||||
const ChatShellRun({
|
||||
required this.id,
|
||||
required this.command,
|
||||
required this.directory,
|
||||
required this.status,
|
||||
required this.startedAt,
|
||||
this.exitCode,
|
||||
this.signal = '',
|
||||
this.error = '',
|
||||
this.endedAt = 0,
|
||||
this.durationMs = 0,
|
||||
this.output = '',
|
||||
this.outputStart = 0,
|
||||
this.outputEnd = 0,
|
||||
this.outputBytes = 0,
|
||||
this.gap = false,
|
||||
});
|
||||
|
||||
final String id;
|
||||
final String command;
|
||||
final String directory;
|
||||
|
||||
/// The server's status word: running, exited, stopped, failed, interrupted.
|
||||
final String status;
|
||||
final int startedAt;
|
||||
final int? exitCode;
|
||||
final String signal;
|
||||
final String error;
|
||||
final int endedAt;
|
||||
final int durationMs;
|
||||
|
||||
/// The text held here, covering absolute output bytes from [outputStart]
|
||||
/// to [outputEnd] (0 when unknown, for a readback).
|
||||
final String output;
|
||||
final int outputStart;
|
||||
final int outputEnd;
|
||||
final int outputBytes;
|
||||
|
||||
/// True when live frames were missed, so [output] is not one contiguous
|
||||
/// stretch of what the command wrote.
|
||||
final bool gap;
|
||||
|
||||
/// Whether earlier output is not shown here.
|
||||
bool get truncated => outputStart > 0 || gap;
|
||||
|
||||
ChatShellRunState get state => switch (status) {
|
||||
'running' => ChatShellRunState.running,
|
||||
'stopped' => ChatShellRunState.stopped,
|
||||
'interrupted' => ChatShellRunState.interrupted,
|
||||
'exited' when exitCode == 0 && signal.isEmpty =>
|
||||
ChatShellRunState.succeeded,
|
||||
_ => ChatShellRunState.failed,
|
||||
};
|
||||
|
||||
bool get running => state == ChatShellRunState.running;
|
||||
|
||||
static ChatShellRun? fromJson(Object? value) {
|
||||
if (value is! Map) return null;
|
||||
final id = '${value['id'] ?? ''}'.trim();
|
||||
if (id.isEmpty) return null;
|
||||
final output = '${value['output'] ?? ''}';
|
||||
final start = _int(value['output_start']);
|
||||
final bytes = _int(value['output_bytes']);
|
||||
return ChatShellRun(
|
||||
id: id,
|
||||
command: '${value['command'] ?? ''}',
|
||||
directory: '${value['directory'] ?? ''}',
|
||||
status: '${value['status'] ?? ''}',
|
||||
startedAt: _int(value['started_at']),
|
||||
exitCode: value['exit_code'] is num
|
||||
? (value['exit_code'] as num).toInt()
|
||||
: null,
|
||||
signal: '${value['signal'] ?? ''}',
|
||||
error: '${value['error'] ?? ''}',
|
||||
endedAt: _int(value['ended_at']),
|
||||
durationMs: _int(value['duration_ms']),
|
||||
output: output,
|
||||
outputStart: start,
|
||||
// A readback's text runs to the newest byte the server had.
|
||||
outputEnd: output.isEmpty && start == 0 ? 0 : bytes,
|
||||
outputBytes: bytes,
|
||||
);
|
||||
}
|
||||
|
||||
/// This run with a lifecycle frame's facts; the held output stays.
|
||||
ChatShellRun withLifecycle(ChatShellRun next) => ChatShellRun(
|
||||
id: id,
|
||||
command: next.command.isEmpty ? command : next.command,
|
||||
directory: next.directory.isEmpty ? directory : next.directory,
|
||||
status: next.status,
|
||||
startedAt: next.startedAt == 0 ? startedAt : next.startedAt,
|
||||
exitCode: next.exitCode,
|
||||
signal: next.signal,
|
||||
error: next.error,
|
||||
endedAt: next.endedAt,
|
||||
durationMs: next.durationMs,
|
||||
output: output,
|
||||
outputStart: outputStart,
|
||||
outputEnd: outputEnd,
|
||||
outputBytes: next.outputBytes > outputBytes
|
||||
? next.outputBytes
|
||||
: outputBytes,
|
||||
gap: gap,
|
||||
);
|
||||
|
||||
/// This run with one live output chunk covering bytes [offset, end). A
|
||||
/// chunk this copy already holds is ignored; one after a hole starts a new
|
||||
/// stretch and marks the gap rather than joining unrelated text.
|
||||
ChatShellRun withChunk(String text, int offset, int end) {
|
||||
if (end <= outputEnd) return this;
|
||||
final contiguous = offset == outputEnd;
|
||||
var joined = contiguous ? output + text : text;
|
||||
var dropped = !contiguous && outputEnd > 0;
|
||||
if (joined.length > chatShellLiveOutputLimit) {
|
||||
joined = joined.substring(joined.length - chatShellLiveOutputLimit);
|
||||
dropped = true;
|
||||
}
|
||||
return ChatShellRun(
|
||||
id: id,
|
||||
command: command,
|
||||
directory: directory,
|
||||
status: status,
|
||||
startedAt: startedAt,
|
||||
exitCode: exitCode,
|
||||
signal: signal,
|
||||
error: error,
|
||||
endedAt: endedAt,
|
||||
durationMs: durationMs,
|
||||
output: joined,
|
||||
outputStart: contiguous ? outputStart : offset,
|
||||
outputEnd: end,
|
||||
outputBytes: end > outputBytes ? end : outputBytes,
|
||||
gap: gap || dropped || (!contiguous && offset > 0),
|
||||
);
|
||||
}
|
||||
|
||||
JsonMap toRow() => {
|
||||
'kind': chatShellRunRowKind,
|
||||
'key': 'chat-shell-run-$id',
|
||||
'run_id': id,
|
||||
'created_at': startedAt,
|
||||
};
|
||||
}
|
||||
|
||||
/// The transcript row kind of one operator command card.
|
||||
const String chatShellRunRowKind = 'shell_run';
|
||||
|
||||
int _int(Object? value) => value is num ? value.toInt() : 0;
|
||||
@@ -0,0 +1,149 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../ui/text/monospace.dart';
|
||||
import '../../../ui/theme/app_theme.dart';
|
||||
import '../conversation/chat_card_styles.dart';
|
||||
import '../ui/sessions/chat_adaptive.dart';
|
||||
import 'chat_shell_card_actions.dart';
|
||||
import 'chat_shell_output_view.dart';
|
||||
import 'chat_shell_run.dart';
|
||||
import 'chat_shell_runs_controller.dart';
|
||||
import 'chat_shell_status.dart';
|
||||
|
||||
/// One operator command in the transcript, as a terminal-style card in the
|
||||
/// reading column: the command and folder, its live output, a spinner and
|
||||
/// Stop while it runs, then its exit status, Copy output and Send to agent.
|
||||
///
|
||||
/// It listens to [controller] itself, so streamed output rebuilds only this
|
||||
/// card, never the transcript.
|
||||
class ChatShellRunCard extends StatefulWidget {
|
||||
const ChatShellRunCard({
|
||||
super.key,
|
||||
required this.controller,
|
||||
required this.runId,
|
||||
});
|
||||
|
||||
final ChatShellRunsController controller;
|
||||
final String runId;
|
||||
|
||||
@override
|
||||
State<ChatShellRunCard> createState() => _ChatShellRunCardState();
|
||||
}
|
||||
|
||||
class _ChatShellRunCardState extends State<ChatShellRunCard> {
|
||||
bool _expanded = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
return ListenableBuilder(
|
||||
listenable: widget.controller,
|
||||
builder: (context, _) {
|
||||
final run = widget.controller.run(widget.runId);
|
||||
if (run == null) return const SizedBox.shrink();
|
||||
final body = _body(context, run, cupertino);
|
||||
return Semantics(
|
||||
identifier: 'chat-shell-run-card',
|
||||
container: true,
|
||||
child: cupertino
|
||||
? CupertinoListSection.insetGrouped(
|
||||
backgroundColor: AppColors.groupedBackground,
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
children: [
|
||||
Padding(padding: const EdgeInsets.all(16), child: body),
|
||||
],
|
||||
)
|
||||
: Card(
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: body,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _body(BuildContext context, ChatShellRun run, bool cupertino) {
|
||||
final controller = widget.controller;
|
||||
final error = controller.errorFor(run.id);
|
||||
final command = (chatCardBodyStyle(context, cupertino) ?? const TextStyle())
|
||||
.copyWith(fontFamily: appMonospaceFamily, fontWeight: FontWeight.w600);
|
||||
return Column(
|
||||
key: ValueKey<String>('chat-shell-run-${run.id}'),
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsetsDirectional.only(end: 12),
|
||||
child: Icon(
|
||||
cupertino
|
||||
? CupertinoIcons.chevron_left_slash_chevron_right
|
||||
: Icons.terminal,
|
||||
color: chatCardSecondaryColor(context, cupertino),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'\$ ${run.command}',
|
||||
key: const Key('chat-shell-run-command'),
|
||||
style: command,
|
||||
),
|
||||
if (run.directory.isNotEmpty)
|
||||
Text(
|
||||
run.directory,
|
||||
style: chatCardSecondaryStyle(context, cupertino),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
ChatShellStatus(run: run, cupertino: cupertino),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
ChatShellOutputView(
|
||||
run: run,
|
||||
cupertino: cupertino,
|
||||
expanded: _expanded,
|
||||
),
|
||||
if (run.error.isNotEmpty && !run.running)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Text(
|
||||
run.error,
|
||||
style: chatCardSecondaryStyle(context, cupertino),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
ChatShellCardActions(
|
||||
run: run,
|
||||
controller: controller,
|
||||
cupertino: cupertino,
|
||||
expanded: _expanded,
|
||||
onToggleExpanded: () => setState(() => _expanded = !_expanded),
|
||||
),
|
||||
if (error != null)
|
||||
Semantics(
|
||||
liveRegion: true,
|
||||
child: Text(
|
||||
error,
|
||||
key: const Key('chat-shell-run-error'),
|
||||
style:
|
||||
(chatCardBodyStyle(context, cupertino) ?? const TextStyle())
|
||||
.copyWith(color: chatCardErrorColor(context, cupertino)),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../ui/text/monospace.dart';
|
||||
import 'chat_shell_command.dart';
|
||||
|
||||
/// The chat actions menu's "Run command…": a small host dialog with one
|
||||
/// command field. Returns the trimmed command, or null when cancelled.
|
||||
Future<String?> showChatShellRunDialog(
|
||||
BuildContext context, {
|
||||
required bool cupertino,
|
||||
}) => cupertino
|
||||
? showCupertinoDialog<String>(
|
||||
context: context,
|
||||
barrierDismissible: true,
|
||||
builder: (_) => const ChatShellRunDialog(cupertino: true),
|
||||
)
|
||||
: showDialog<String>(
|
||||
context: context,
|
||||
builder: (_) => const ChatShellRunDialog(cupertino: false),
|
||||
);
|
||||
|
||||
class ChatShellRunDialog extends StatefulWidget {
|
||||
const ChatShellRunDialog({super.key, required this.cupertino});
|
||||
|
||||
final bool cupertino;
|
||||
|
||||
@override
|
||||
State<ChatShellRunDialog> createState() => _ChatShellRunDialogState();
|
||||
}
|
||||
|
||||
class _ChatShellRunDialogState extends State<ChatShellRunDialog> {
|
||||
final TextEditingController _text = TextEditingController();
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_text.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _submit() {
|
||||
final command = _text.text.trim();
|
||||
Navigator.pop(context, command.isEmpty ? null : command);
|
||||
}
|
||||
|
||||
void _cancel() => Navigator.pop(context);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
const mono = TextStyle(fontFamily: appMonospaceFamily);
|
||||
if (widget.cupertino) {
|
||||
return CupertinoAlertDialog(
|
||||
title: const Text(chatShellRunDialogTitle),
|
||||
content: Column(
|
||||
children: [
|
||||
const Text(chatShellModeHint),
|
||||
const SizedBox(height: 12),
|
||||
CupertinoTextField(
|
||||
key: const Key('chat-shell-run-field'),
|
||||
controller: _text,
|
||||
autofocus: true,
|
||||
placeholder: 'Command',
|
||||
style: mono,
|
||||
onSubmitted: (_) => _submit(),
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
CupertinoDialogAction(
|
||||
onPressed: _cancel,
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
CupertinoDialogAction(
|
||||
key: const Key('chat-shell-run-confirm'),
|
||||
isDefaultAction: true,
|
||||
onPressed: _submit,
|
||||
child: const Text(chatShellRunDialogConfirm),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
return AlertDialog(
|
||||
title: const Text(chatShellRunDialogTitle),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
const Text(chatShellModeHint),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
key: const Key('chat-shell-run-field'),
|
||||
controller: _text,
|
||||
autofocus: true,
|
||||
style: mono,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Command',
|
||||
prefixText: '\$ ',
|
||||
),
|
||||
onSubmitted: (_) => _submit(),
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(onPressed: _cancel, child: const Text('Cancel')),
|
||||
FilledButton(
|
||||
key: const Key('chat-shell-run-confirm'),
|
||||
onPressed: _submit,
|
||||
child: const Text(chatShellRunDialogConfirm),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../../../core/error_reason.dart';
|
||||
import 'chat_shell_run.dart';
|
||||
import 'chat_shell_runs_repository.dart';
|
||||
|
||||
/// Where one run's "Send to agent" stands.
|
||||
enum ChatShellShareState { idle, sending, sent }
|
||||
|
||||
/// One chat's operator shell commands: the recorded runs, their live output
|
||||
/// from `chat_shell_run_event` frames, and the card actions.
|
||||
///
|
||||
/// It notifies on every change (cards listen to it); [membership] notifies
|
||||
/// only when the set of runs changes, so the transcript is not rebuilt for
|
||||
/// every output chunk.
|
||||
class ChatShellRunsController extends ChangeNotifier {
|
||||
ChatShellRunsController(this.gateway, this.sessionId)
|
||||
: _repository = ChatShellRunsRepository(gateway) {
|
||||
_frames = gateway.frames.listen(_onFrame, onError: (_) {});
|
||||
unawaited(refresh());
|
||||
}
|
||||
|
||||
final ApiGateway gateway;
|
||||
final String sessionId;
|
||||
final ChatShellRunsRepository _repository;
|
||||
StreamSubscription<JsonMap>? _frames;
|
||||
final Map<String, ChatShellRun> _runs = {};
|
||||
final ValueNotifier<int> _membership = ValueNotifier<int>(0);
|
||||
final Set<String> _stopping = {};
|
||||
final Map<String, ChatShellShareState> _shares = {};
|
||||
final Map<String, String> _errors = {};
|
||||
bool _disposed = false;
|
||||
int _generation = 0;
|
||||
|
||||
Listenable get membership => _membership;
|
||||
List<ChatShellRun> get runs => List.unmodifiable(_runs.values);
|
||||
ChatShellRun? run(String id) => _runs[id];
|
||||
bool stopping(String id) => _stopping.contains(id);
|
||||
ChatShellShareState shareState(String id) =>
|
||||
_shares[id] ?? ChatShellShareState.idle;
|
||||
|
||||
/// The last refused card action of [id], in the server's words.
|
||||
String? errorFor(String id) => _errors[id];
|
||||
|
||||
/// Reads the recorded runs again (open, reconnect). Runs this copy holds
|
||||
/// with newer output than the readback keep their own copy.
|
||||
Future<void> refresh() async {
|
||||
final token = ++_generation;
|
||||
try {
|
||||
final listed = await _repository.runs(sessionId);
|
||||
if (_disposed || token != _generation) return;
|
||||
for (final run in listed) {
|
||||
final held = _runs[run.id];
|
||||
_runs[run.id] = held != null && held.outputEnd > run.outputEnd
|
||||
? held.withLifecycle(run)
|
||||
: run;
|
||||
}
|
||||
_changed(membership: true);
|
||||
} catch (_) {
|
||||
// An older server has no shell commands; the transcript is unchanged.
|
||||
}
|
||||
}
|
||||
|
||||
/// Starts [command] in the chat's directory. Returns the refusal, or null.
|
||||
Future<String?> start(String command) async {
|
||||
try {
|
||||
final run = await _repository.run(sessionId, command);
|
||||
if (_disposed) return null;
|
||||
_runs.putIfAbsent(run.id, () => run);
|
||||
_changed(membership: true);
|
||||
return null;
|
||||
} catch (error) {
|
||||
return _reason(error);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> stop(String id) async {
|
||||
if (_stopping.contains(id)) return;
|
||||
_stopping.add(id);
|
||||
_errors.remove(id);
|
||||
_changed();
|
||||
try {
|
||||
await _repository.stop(sessionId, id);
|
||||
} catch (error) {
|
||||
_stopping.remove(id);
|
||||
_errors[id] = _reason(error);
|
||||
_changed();
|
||||
}
|
||||
}
|
||||
|
||||
/// Queues the run's command and output tail as an ordinary prompt.
|
||||
Future<void> share(String id) async {
|
||||
if (shareState(id) == ChatShellShareState.sending) return;
|
||||
_shares[id] = ChatShellShareState.sending;
|
||||
_errors.remove(id);
|
||||
_changed();
|
||||
try {
|
||||
await _repository.share(sessionId, id);
|
||||
_shares[id] = ChatShellShareState.sent;
|
||||
} catch (error) {
|
||||
_shares.remove(id);
|
||||
_errors[id] = _reason(error);
|
||||
}
|
||||
_changed();
|
||||
}
|
||||
|
||||
/// Every byte the server retained for [id]: what Copy output copies and
|
||||
/// Show all draws. Null when it cannot be read (the error is recorded).
|
||||
Future<ChatShellRun?> loadAll(String id) async {
|
||||
final held = _runs[id];
|
||||
if (held == null) return null;
|
||||
if (!held.truncated && !held.running) return held;
|
||||
try {
|
||||
final full = await _repository.output(sessionId, id);
|
||||
if (_disposed) return null;
|
||||
final latest = _runs[id] ?? held;
|
||||
// A frame newer than the read keeps the card's own lifecycle facts.
|
||||
final merged = full.withLifecycle(latest.running ? full : latest);
|
||||
_runs[id] = merged;
|
||||
_changed();
|
||||
return merged;
|
||||
} catch (error) {
|
||||
_errors[id] = _reason(error);
|
||||
_changed();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
void _onFrame(JsonMap envelope) {
|
||||
if (_disposed || envelope['type'] != 'chat_shell_run_event') return;
|
||||
final frame = envelope['chat_shell_run_event'];
|
||||
if (frame is! Map || frame['session_id'] != sessionId) return;
|
||||
final id = '${frame['run_id'] ?? ''}';
|
||||
final record = ChatShellRun.fromJson(frame['run']);
|
||||
if (record != null) {
|
||||
final held = _runs[record.id];
|
||||
_runs[record.id] = held == null ? record : held.withLifecycle(record);
|
||||
if (!record.running) _stopping.remove(record.id);
|
||||
_changed(membership: held == null);
|
||||
return;
|
||||
}
|
||||
final held = _runs[id];
|
||||
if (held == null) {
|
||||
// Output for a run this copy has not seen yet: read the record.
|
||||
unawaited(refresh());
|
||||
return;
|
||||
}
|
||||
_runs[id] = held.withChunk(
|
||||
'${frame['output'] ?? ''}',
|
||||
(frame['offset'] as num?)?.toInt() ?? 0,
|
||||
(frame['end'] as num?)?.toInt() ?? 0,
|
||||
);
|
||||
_changed();
|
||||
}
|
||||
|
||||
void _changed({bool membership = false}) {
|
||||
if (_disposed) return;
|
||||
if (membership) _membership.value++;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
String _reason(Object error) =>
|
||||
describeErrorReason(error, fallback: 'The server did not answer.');
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
_generation++;
|
||||
unawaited(_frames?.cancel());
|
||||
_membership.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
import 'chat_shell_run.dart';
|
||||
|
||||
/// Wire adapter for the server's operator shell commands.
|
||||
class ChatShellRunsRepository {
|
||||
const ChatShellRunsRepository(this.gateway);
|
||||
final ApiGateway gateway;
|
||||
|
||||
JsonMap _body(String sessionId, {String? command, String? runId}) => {
|
||||
'chat_shell_run_request': {
|
||||
'session_id': sessionId,
|
||||
'command': ?command,
|
||||
'run_id': ?runId,
|
||||
},
|
||||
};
|
||||
|
||||
/// Starts [command]; the reply is its running record.
|
||||
Future<ChatShellRun> run(String sessionId, String command) async => _record(
|
||||
await gateway.call('chat_shell_run', _body(sessionId, command: command)),
|
||||
);
|
||||
|
||||
Future<void> stop(String sessionId, String runId) =>
|
||||
gateway.call('chat_shell_stop', _body(sessionId, runId: runId));
|
||||
|
||||
/// The session's recorded runs, oldest first, each with its output tail.
|
||||
Future<List<ChatShellRun>> runs(String sessionId) async {
|
||||
final reply = await gateway.call('chat_shell_runs', {
|
||||
'chat_query': {'session_id': sessionId},
|
||||
});
|
||||
final value = reply['chat_shell_runs'];
|
||||
return [
|
||||
if (value is List)
|
||||
for (final item in value) ?ChatShellRun.fromJson(item),
|
||||
];
|
||||
}
|
||||
|
||||
/// One run with the whole output the server retained.
|
||||
Future<ChatShellRun> output(String sessionId, String runId) async => _record(
|
||||
await gateway.call('chat_shell_run_output', _body(sessionId, runId: runId)),
|
||||
);
|
||||
|
||||
/// Queues an ordinary prompt carrying the run for the agent.
|
||||
Future<void> share(String sessionId, String runId) =>
|
||||
gateway.call('chat_shell_share', _body(sessionId, runId: runId));
|
||||
|
||||
ChatShellRun _record(JsonMap reply) {
|
||||
final run = ChatShellRun.fromJson(reply['chat_shell_run']);
|
||||
if (run == null) {
|
||||
throw const FormatException('The server did not return the command.');
|
||||
}
|
||||
return run;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../conversation/chat_card_styles.dart';
|
||||
import 'chat_shell_command.dart';
|
||||
import 'chat_shell_run.dart';
|
||||
|
||||
/// A command's state at the card's top end: the host's activity indicator
|
||||
/// while it runs, then a status chip (exit code, stopped, interrupted) with
|
||||
/// how long it ran. The words carry the state; colour only repeats it.
|
||||
class ChatShellStatus extends StatelessWidget {
|
||||
const ChatShellStatus({
|
||||
super.key,
|
||||
required this.run,
|
||||
required this.cupertino,
|
||||
});
|
||||
|
||||
final ChatShellRun run;
|
||||
final bool cupertino;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (run.running) {
|
||||
return Semantics(
|
||||
key: const Key('chat-shell-run-running'),
|
||||
label: chatShellStatusLabel(run),
|
||||
child: cupertino
|
||||
? const CupertinoActivityIndicator()
|
||||
// As tall as the line of text beside it, at any text scale.
|
||||
: SizedBox.square(
|
||||
dimension: MediaQuery.textScalerOf(context).scale(20),
|
||||
child: const CircularProgressIndicator(strokeWidth: 2),
|
||||
),
|
||||
);
|
||||
}
|
||||
final label = run.durationMs > 0
|
||||
? '${chatShellStatusLabel(run)} · ${chatShellDuration(run.durationMs)}'
|
||||
: chatShellStatusLabel(run);
|
||||
final color = switch (run.state) {
|
||||
ChatShellRunState.succeeded =>
|
||||
cupertino
|
||||
? CupertinoColors.systemGreen.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.primary,
|
||||
ChatShellRunState.failed => chatCardErrorColor(context, cupertino),
|
||||
_ => chatCardSecondaryColor(context, cupertino),
|
||||
};
|
||||
final icon = switch (run.state) {
|
||||
ChatShellRunState.succeeded =>
|
||||
cupertino
|
||||
? CupertinoIcons.checkmark_circle
|
||||
: Icons.check_circle_outline,
|
||||
ChatShellRunState.failed =>
|
||||
cupertino ? CupertinoIcons.xmark_circle : Icons.error_outline,
|
||||
_ => cupertino ? CupertinoIcons.stop_circle : Icons.stop_circle_outlined,
|
||||
};
|
||||
if (cupertino) {
|
||||
return Row(
|
||||
key: const Key('chat-shell-run-status'),
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(icon, color: color, size: 18),
|
||||
const SizedBox(width: 4),
|
||||
Text(label, style: chatCardSecondaryStyle(context, true)),
|
||||
],
|
||||
);
|
||||
}
|
||||
return Chip(
|
||||
key: const Key('chat-shell-run-status'),
|
||||
avatar: Icon(icon, color: color),
|
||||
label: Text(label),
|
||||
visualDensity: VisualDensity.compact,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../ui/adaptive/segmented_bar_overflow.dart';
|
||||
import 'chat_shell_command.dart';
|
||||
import 'chat_shell_run_dialog.dart';
|
||||
import 'chat_shell_runs_controller.dart';
|
||||
|
||||
/// The conversation's "Run command…" entry in its actions (More) menu.
|
||||
Widget chatShellRunMenuAction({required VoidCallback onPressed}) =>
|
||||
SegmentedOverflowAction(
|
||||
key: const ValueKey<String>('chat-shell-run-action'),
|
||||
overflowEntry: SegmentedOverflowEntry(
|
||||
label: chatShellRunActionLabel,
|
||||
icon: Icons.terminal,
|
||||
onPressed: onPressed,
|
||||
),
|
||||
child: const SizedBox.shrink(),
|
||||
);
|
||||
|
||||
/// Asks for a command and starts it; a refusal becomes the conversation's
|
||||
/// own failed notice. Nothing reaches the agent.
|
||||
Future<void> runChatShellFromMenu(
|
||||
BuildContext context,
|
||||
ChatShellRunsController controller, {
|
||||
required bool cupertino,
|
||||
required void Function(String text, {bool failed}) showNotice,
|
||||
}) async {
|
||||
final command = await showChatShellRunDialog(context, cupertino: cupertino);
|
||||
if (command == null) return;
|
||||
final refusal = await controller.start(command);
|
||||
if (refusal != null) {
|
||||
showNotice("Couldn't run the command: $refusal", failed: true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
# Chat shell commands
|
||||
|
||||
The operator can run their own shell commands inside a chat, like Codex's
|
||||
`!cmd` or Claude Code's `!` bash mode. A command runs in the chat's working
|
||||
directory on the chat's machine (the Pipelines server that owns the chat). It
|
||||
is Pipelines' own feature and does not go through the harness. The model never
|
||||
sees a command or its output unless the operator chooses **Send to agent**.
|
||||
|
||||
## Behaviour
|
||||
|
||||
- **Composer.** If a draft starts with `!` (leading blanks allowed), the
|
||||
composer shows a **Shell** indicator: "Runs in this chat's folder on its
|
||||
machine. The agent does not see it." The primary control's name changes to
|
||||
"Run command". Enter or the button runs the text after the `!` through
|
||||
`chat_shell_run`. Such a draft is never sent as a prompt. The draft is
|
||||
cleared once the server has started the command. If the server refuses it
|
||||
(limit reached, missing folder), the draft stays and the reason appears as
|
||||
a failed command notice. A bare `!` reports "Type a command after !." and
|
||||
sends nothing. `/help` lists `!<command>` after the slash commands.
|
||||
- **Actions menu.** The conversation's More menu has **Run command…**. It
|
||||
opens a small Material or Cupertino dialog with a monospace field and the
|
||||
same hint. A refusal appears as the conversation's failed notice.
|
||||
- **Transcript card.** Each run is a card in the normal reading column. Runs
|
||||
are placed by start time among the timed turns, and a run newer than every
|
||||
turn comes last. The card shows:
|
||||
- `$ command` in monospace, with the resolved folder under it.
|
||||
- The host's activity indicator and **Stop** while the command runs.
|
||||
"Stopping…" shows until the final frame arrives.
|
||||
- A status chip when it ends: `Exit 0 · 37 ms`, `Exit 1 · 4.2 s`,
|
||||
`Stopped · 15.2 s`, `Interrupted` or `Killed (signal)`. The words carry
|
||||
the state and colour only repeats it.
|
||||
- Live output in the host's monospace on an inset surface. It scrolls
|
||||
inside a bounded height, stays anchored at the newest line, and has
|
||||
**Expand**/**Collapse** when it can overflow.
|
||||
- **Copy output**, which copies the server's whole retained output and
|
||||
reads it first when the card holds only a tail.
|
||||
- **Send to agent**: Sending…, then Sent to agent. A second tap does
|
||||
nothing.
|
||||
- A line saying when earlier output was dropped.
|
||||
- **Several clients.** Output and lifecycle frames go to every client
|
||||
watching the chat (`chat_subscribe` room). A reload or reconnect reads the
|
||||
runs back with `chat_shell_runs`.
|
||||
|
||||
## Wire (internal/protocol/chat_shell_run.go)
|
||||
|
||||
| request | body | reply |
|
||||
| --- | --- | --- |
|
||||
| `chat_shell_run` | `chat_shell_run_request{session_id, command}` | `chat_shell_run` (the running record) |
|
||||
| `chat_shell_stop` | `chat_shell_run_request{session_id, run_id}` | `chat_shell_run` |
|
||||
| `chat_shell_runs` | `chat_query{session_id}` | `chat_shell_runs` (oldest first, ≤64 KiB tail each) |
|
||||
| `chat_shell_run_output` | `chat_shell_run_request{session_id, run_id}` | `chat_shell_run` with the full retained output |
|
||||
| `chat_shell_share` | `chat_shell_run_request{session_id, run_id[, operation_id]}` | the ordinary `chat_prompt` ack |
|
||||
|
||||
Pushed: `chat_shell_run_event{session_id, run_id, run}` for each lifecycle
|
||||
change, and `{session_id, run_id, output, offset, end}` for output chunks.
|
||||
`offset` and `end` are absolute byte offsets. A client whose copy ends before
|
||||
`offset` marks a gap instead of joining unrelated text.
|
||||
|
||||
## Server (internal/server/chat_shell_*.go)
|
||||
|
||||
- The server runs `$SHELL -lc <command>`. It uses `/bin/sh` when `$SHELL` is
|
||||
not an absolute path to an existing file. The command gets the server's
|
||||
environment, stdin `/dev/null` and its own process group (`Setpgid`), with
|
||||
stdout and stderr merged into one pipe.
|
||||
- The working directory is the chat session's recorded directory from the
|
||||
operator-session registry. Symlinks are resolved, and the directory must
|
||||
still exist and be a directory. Otherwise the run is refused ("the chat's
|
||||
directory … is not available").
|
||||
- Output is sent in batches every 100 ms, at most 64 KiB per frame. If more
|
||||
is waiting, the newest bytes are sent and the client sees a gap. A
|
||||
character is never split. Invalid UTF-8 is replaced by U+FFFD on the wire.
|
||||
- **Stop** sends SIGINT to the group, then SIGKILL after 3 s. After the shell
|
||||
itself exits, output from its descendants is still read for 1 s, then
|
||||
anything left in the group is killed. Nothing in the group outlives the run
|
||||
(use `setsid`/`nohup` from a terminal for daemons).
|
||||
- **Statuses:**
|
||||
- `exited` with `exit_code`, or with `signal` if a signal killed it.
|
||||
- `stopped`: an operator Stop. This status wins over the exit it caused.
|
||||
- `failed`: a wait error.
|
||||
- `interrupted`: the server stopped while the command ran. A server
|
||||
shutdown kills running groups and records them as interrupted. A starting
|
||||
server also marks any run still recorded as running as `interrupted`
|
||||
("The server stopped while this command was running.").
|
||||
- **Persistence:** the `chat_shell_runs` table in the server store. It holds
|
||||
the command, folder, status, exit code or signal, start and end times,
|
||||
duration, the retained output tail, its start offset and total bytes. The
|
||||
row is written at start, every 2 s while the command runs, and at the end.
|
||||
The 50 newest runs per session are kept, and a running run is never
|
||||
pruned.
|
||||
- **Share:** `chat_shell_share` refuses a run that is still running. It then
|
||||
composes an ordinary prompt: the folder, the command in a fence, how the run
|
||||
ended, and at most the last 16 KiB of output. Fences are longer than any
|
||||
backtick run in the content. The prompt goes through the normal admission
|
||||
path (queue, steer, durable queue). The user turn is echoed to the sender
|
||||
too, because the sender has no local copy.
|
||||
|
||||
## Limits
|
||||
|
||||
| limit | value |
|
||||
| --- | --- |
|
||||
| concurrent runs per chat | 4 |
|
||||
| command length | 16 KiB |
|
||||
| retained/persisted output per run | 1 MiB (tail) |
|
||||
| output per readback list entry | 64 KiB (tail) |
|
||||
| live frame | 64 KiB, 100 ms batching |
|
||||
| shared with the agent | last 16 KiB |
|
||||
| runs kept per chat | 50 |
|
||||
| Stop grace before SIGKILL | 3 s |
|
||||
| client live text per card | 256 K chars; 64 K chars drawn |
|
||||
|
||||
## Security
|
||||
|
||||
- This is a terminal for the authenticated operator, with the server user's
|
||||
full privileges. It is reachable only through the existing authenticated
|
||||
`/connect` websocket. No new HTTP endpoint or listener was added.
|
||||
- Every request must name a syntactically valid chat session id that the
|
||||
operator-session registry records. Other requests are refused before
|
||||
anything runs: unknown ids, pipeline stage sessions, and requests with
|
||||
malformed ids or no body. Runs are looked up by session and id. Another
|
||||
session cannot stop, read or share a run, even with its id.
|
||||
- The command runs on the server that owns the chat, in that chat's
|
||||
recorded directory, and nowhere else. No remote host is ever named. A
|
||||
command can still `cd` anywhere the server user may, as in any terminal.
|
||||
The directory is not restricted to the home folder because chats may
|
||||
legitimately live outside it (`chat_create` accepts any absolute
|
||||
directory). Restricting only the starting folder would not contain a
|
||||
shell, so the honest boundary is the authenticated session.
|
||||
- Commands and output never reach the model implicitly. Only an explicit
|
||||
`chat_shell_share` queues a prompt, and it is bounded.
|
||||
- The process group is killed on Stop, on server shutdown and after the
|
||||
command exits, so a run cannot leave an orphan holding the server's pipe.
|
||||
|
||||
## Files
|
||||
|
||||
- Server: `internal/protocol/chat_shell_run.go`,
|
||||
`internal/pipeline/chat_shell_runs.go`,
|
||||
`internal/server/chat_shell_{runner,supervise,run_output,run_handlers,share}.go`,
|
||||
and small hooks in `chat.go`, `chat_hub.go`, `chat_stream.go`
|
||||
(`admitChatPrompt`, `echoSender`) and `websocket.go`.
|
||||
- App: `lib/features/chat/shell/`: the model, controller, repository, card,
|
||||
output view, status, actions, dialog, mode indicator and metrics. The
|
||||
centralized height bound is `chat_shell_metrics.dart`. There are small
|
||||
hooks in `conversation/chat_thread_view.dart` (controller, rows, card, menu
|
||||
entry), `conversation/chat_composer_bar.dart` (`runShell`, Shell indicator,
|
||||
Run label) and `domain/chat_commands.dart` (`/help` line).
|
||||
- Capture fixture: `tool/perf/chat_shell_fixture.dart` (`?shell=1` with
|
||||
`?chats=N`).
|
||||
|
||||
## Verification
|
||||
|
||||
- `go test ./internal/protocol ./internal/pipeline ./internal/server`:
|
||||
`ok …/internal/protocol 0.327s`, `ok …/internal/pipeline 8.092s`,
|
||||
`ok …/internal/server 82.708s`.
|
||||
- The new tests also pass under `go test -race -run 'ChatShell(Run|Stop|Share|Retains|Shutdown|Output|SharePrompt)'`.
|
||||
The race failures in that pattern are the pre-existing
|
||||
`TestChatShellOutputRealHandler*` tests, which race in
|
||||
`internal/opencode/connection.go`. They are unrelated to these files.
|
||||
- New Go tests:
|
||||
- streaming to two watchers plus persistence (`pwd; echo; >&2; exit 3`);
|
||||
- Stop reaching the child `sleep` in the group;
|
||||
- the 4-run limit, blank, oversized, unknown-session, malformed and
|
||||
foreign-stop refusals, and refusing to share a running run;
|
||||
- a deleted folder refused;
|
||||
- share echoing the composed prompt to both clients;
|
||||
- SIGKILL after the grace period for a SIGINT-ignoring command;
|
||||
- the 1 MiB tail with a 64 KiB list tail, and no frame over 64 KiB;
|
||||
- shutdown and restart marking runs interrupted;
|
||||
- UTF-8 boundaries in chunking;
|
||||
- share fencing;
|
||||
- store persistence, pruning and interruption.
|
||||
- `tool/memwatch/full.sh test/features/chat`:
|
||||
`memwatch: PASS: 1079 passed, 0 failed, 0 skipped; peak 2206 MB; 186s; exit 0 (test/features/chat)`.
|
||||
- New widget tests in `test/features/chat/shell/`:
|
||||
- `!` detection, the Shell indicator and Run label, and that Enter runs
|
||||
without a `chat_prompt`;
|
||||
- a refusal keeping the draft, and a bare `!`;
|
||||
- Run command… from the More menu;
|
||||
- card states (running with streamed output, stopped, failed with Exit 1,
|
||||
succeeded with Exit 0) in Material and Cupertino;
|
||||
- Stop, Send to agent (once), Copy output, and a 2× text scale with
|
||||
Expand.
|
||||
- `tool/analyze.sh`: `analyze: 0 errors, 8 warnings, 0 infos`. All 8
|
||||
warnings are pre-existing, in the untouched
|
||||
`integration_test/discovery_probe_test.dart`.
|
||||
- Real Chrome captures used `tool/capture` over
|
||||
`tool/search_selection_probe_entry.dart` with `chats=1&msgs=2&shell=1`.
|
||||
There were two sets: Material light at 880x1000 and Cupertino dark at
|
||||
560x1000. Each set has the recorded cards, the Shell indicator with
|
||||
`!git status --short`, the run started by Enter and finished by its
|
||||
frames, the More menu with Run command…, and the dialog. The PNGs were
|
||||
inspected.
|
||||
|
||||
Not verified: a live server and browser session running a real command
|
||||
end to end. The Go websocket tests cover the server with real processes, and
|
||||
the browser captures use the fixture channel. Mobile devices were not tested.
|
||||
@@ -256,7 +256,9 @@ void main() {
|
||||
'/close — Finish this session for every client, keeping its history\n'
|
||||
'/rename <title> — Rename this session\n'
|
||||
'/stop — Stop the turn that is running\n'
|
||||
'/help — List every command',
|
||||
'/help — List every command\n'
|
||||
'!<command> — Run a shell command in this chat’s folder; '
|
||||
'the agent does not see it unless you send it',
|
||||
),
|
||||
findsOneWidget,
|
||||
);
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_commands.dart';
|
||||
import 'package:pipelines_app/features/chat/shell/chat_shell_command.dart';
|
||||
import 'package:pipelines_app/features/chat/shell/chat_shell_run.dart';
|
||||
|
||||
ChatShellRun _run(String id, int started, {String status = 'running'}) =>
|
||||
ChatShellRun(
|
||||
id: id,
|
||||
command: 'ls',
|
||||
directory: '/w',
|
||||
status: status,
|
||||
startedAt: started,
|
||||
);
|
||||
|
||||
void main() {
|
||||
test('a leading ! (after blanks) is shell mode and names the command', () {
|
||||
expect(chatDraftIsShell('!ls -la'), isTrue);
|
||||
expect(chatDraftIsShell(' ! git status'), isTrue);
|
||||
expect(chatDraftIsShell('say !hi'), isFalse);
|
||||
expect(chatDraftIsShell('/help'), isFalse);
|
||||
expect(chatShellCommandOf(' ! git status '), 'git status');
|
||||
expect(chatShellCommandOf('!'), '');
|
||||
expect(chatShellCommandOf('hello'), isNull);
|
||||
});
|
||||
|
||||
test('/help lists the shell hint after the commands', () {
|
||||
expect(chatCommandHelpText().split('\n').last, chatShellHelpLine);
|
||||
});
|
||||
|
||||
test('runs are placed among timed rows by start time', () {
|
||||
final rows = [
|
||||
{
|
||||
'kind': 'user',
|
||||
'key': 'u1',
|
||||
'message': {'created_at': 100},
|
||||
},
|
||||
{'kind': 'assistant', 'key': 'a1', 'created_at': 200},
|
||||
{'kind': 'user', 'key': 'local', 'message': <String, dynamic>{}},
|
||||
{'kind': 'assistant', 'key': 'a2', 'created_at': 400},
|
||||
];
|
||||
final merged = withChatShellRunRows(rows, [
|
||||
_run('late', 500),
|
||||
_run('early', 150),
|
||||
_run('mid', 300),
|
||||
]);
|
||||
expect(merged.map((row) => row['key']), [
|
||||
'u1',
|
||||
'chat-shell-run-early',
|
||||
'a1',
|
||||
'local',
|
||||
'chat-shell-run-mid',
|
||||
'a2',
|
||||
'chat-shell-run-late',
|
||||
]);
|
||||
expect(withChatShellRunRows(rows, const []), same(rows));
|
||||
});
|
||||
|
||||
test('status words follow the recorded outcome', () {
|
||||
ChatShellRun ended(String status, {int? code, String signal = ''}) =>
|
||||
ChatShellRun(
|
||||
id: 'r',
|
||||
command: 'x',
|
||||
directory: '/',
|
||||
status: status,
|
||||
startedAt: 1,
|
||||
exitCode: code,
|
||||
signal: signal,
|
||||
);
|
||||
expect(ended('exited', code: 0).state, ChatShellRunState.succeeded);
|
||||
expect(chatShellStatusLabel(ended('exited', code: 2)), 'Exit 2');
|
||||
expect(ended('exited', code: 2).state, ChatShellRunState.failed);
|
||||
expect(
|
||||
chatShellStatusLabel(ended('exited', signal: 'killed')),
|
||||
'Killed (killed)',
|
||||
);
|
||||
expect(chatShellStatusLabel(ended('stopped')), 'Stopped');
|
||||
expect(chatShellStatusLabel(ended('interrupted')), 'Interrupted');
|
||||
expect(chatShellDuration(250), '250 ms');
|
||||
expect(chatShellDuration(1500), '1.5 s');
|
||||
expect(chatShellDuration(125000), '2m 5s');
|
||||
});
|
||||
|
||||
test('live chunks join only when contiguous and mark a gap otherwise', () {
|
||||
var run = _run('r', 1);
|
||||
run = run.withChunk('abc', 0, 3).withChunk('def', 3, 6);
|
||||
expect(run.output, 'abcdef');
|
||||
expect(run.truncated, isFalse);
|
||||
expect(run.withChunk('abc', 0, 3).output, 'abcdef', reason: 'a repeat');
|
||||
final gapped = run.withChunk('xyz', 10, 13);
|
||||
expect(gapped.output, 'xyz');
|
||||
expect(gapped.truncated, isTrue);
|
||||
final read = ChatShellRun.fromJson({
|
||||
'id': 'r',
|
||||
'status': 'exited',
|
||||
'exit_code': 0,
|
||||
'output': 'tail',
|
||||
'output_start': 6,
|
||||
'output_bytes': 10,
|
||||
})!;
|
||||
expect(read.truncated, isTrue);
|
||||
expect(read.withChunk('more', 10, 14).output, 'tailmore');
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/ui/adaptive/segmented_bar_overflow.dart';
|
||||
|
||||
import 'chat_shell_fixtures.dart';
|
||||
|
||||
/// A desktop keyboard, where Enter sends.
|
||||
final _desktop = TargetPlatformVariant.only(TargetPlatform.linux);
|
||||
|
||||
void main() {
|
||||
late ShellGateway gateway;
|
||||
|
||||
setUp(() => gateway = ShellGateway());
|
||||
tearDown(() => gateway.close());
|
||||
|
||||
final field = find.byKey(const Key('chat-composer-field'));
|
||||
final shellMode = find.byKey(const Key('chat-composer-shell-mode'));
|
||||
|
||||
testWidgets('a leading ! shows Shell mode and runs instead of sending', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(shellThread(gateway));
|
||||
await tester.pump();
|
||||
|
||||
await tester.enterText(field, 'hello');
|
||||
await tester.pump();
|
||||
expect(shellMode, findsNothing);
|
||||
await tester.enterText(field, '!git status');
|
||||
await tester.pump();
|
||||
expect(shellMode, findsOneWidget);
|
||||
expect(find.text('Shell'), findsOneWidget);
|
||||
expect(find.byTooltip('Run command'), findsOneWidget);
|
||||
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
expect(gateway.bodies('chat_shell_run').single['command'], 'git status');
|
||||
expect(gateway.calls.where((c) => c.type == 'chat_prompt'), isEmpty);
|
||||
expect(shellMode, findsNothing, reason: 'the draft was consumed');
|
||||
// The started run is drawn as a card at once.
|
||||
expect(find.text('\$ git status'), findsOneWidget);
|
||||
}, variant: _desktop);
|
||||
|
||||
testWidgets('a refused command keeps the draft and says why', (tester) async {
|
||||
gateway.refusal = 'this chat already runs 4 commands';
|
||||
await tester.pumpWidget(shellThread(gateway));
|
||||
await tester.pump();
|
||||
await tester.enterText(field, '!sleep 9');
|
||||
await tester.pump();
|
||||
await tester.tap(find.byKey(const Key('chat-composer-primary')));
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
expect(find.textContaining('already runs 4 commands'), findsOneWidget);
|
||||
expect(shellMode, findsOneWidget);
|
||||
expect(gateway.calls.where((c) => c.type == 'chat_prompt'), isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('a bare ! asks for a command and sends nothing', (tester) async {
|
||||
await tester.pumpWidget(shellThread(gateway));
|
||||
await tester.pump();
|
||||
await tester.enterText(field, '!');
|
||||
await tester.pump();
|
||||
await tester.tap(find.byKey(const Key('chat-composer-primary')));
|
||||
await tester.pump();
|
||||
expect(find.text('Type a command after !.'), findsOneWidget);
|
||||
expect(gateway.bodies('chat_shell_run'), isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('Run command… in the actions menu runs the typed command', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(shellThread(gateway));
|
||||
await tester.pump();
|
||||
await tester.tap(find.byKey(segmentedMoreKey).hitTestable().first);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('Run command…'));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.enterText(
|
||||
find.byKey(const Key('chat-shell-run-field')),
|
||||
' ls -la ',
|
||||
);
|
||||
await tester.tap(find.byKey(const Key('chat-shell-run-confirm')));
|
||||
// The running card's indicator never settles; let the dialog close.
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(seconds: 1));
|
||||
expect(gateway.bodies('chat_shell_run').single['command'], 'ls -la');
|
||||
expect(find.text('\$ ls -la'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
|
||||
|
||||
import '../conversation/chat_thread_fixtures.dart';
|
||||
|
||||
/// A gateway for one open conversation (session `s`) that answers the shell
|
||||
/// command requests from recorded state and lets a test push their frames.
|
||||
class ShellGateway implements ApiGateway {
|
||||
final calls = <({String type, JsonMap fields})>[];
|
||||
final _frames = StreamController<JsonMap>.broadcast();
|
||||
List<JsonMap> runs = const [];
|
||||
String? refusal;
|
||||
int _serial = 0;
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => _frames.stream;
|
||||
|
||||
List<JsonMap> bodies(String type) => [
|
||||
for (final call in calls)
|
||||
if (call.type == type)
|
||||
(call.fields['chat_shell_run_request'] ??
|
||||
call.fields['chat_prompt'] ??
|
||||
call.fields['chat_query'])
|
||||
as JsonMap,
|
||||
];
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
calls.add((type: type, fields: fields));
|
||||
final body = fields['chat_shell_run_request'] as JsonMap? ?? const {};
|
||||
switch (type) {
|
||||
case 'chat_messages':
|
||||
return chatPage(const []);
|
||||
case 'chat_shell_runs':
|
||||
return {'chat_shell_runs': runs};
|
||||
case 'chat_shell_run':
|
||||
if (refusal != null) throw StateError(refusal!);
|
||||
return {
|
||||
'chat_shell_run': shellRecord(
|
||||
'run_${++_serial}',
|
||||
command: '${body['command']}',
|
||||
),
|
||||
};
|
||||
case 'chat_shell_run_output':
|
||||
return {
|
||||
'chat_shell_run': runs.firstWhere((r) => r['id'] == body['run_id']),
|
||||
};
|
||||
}
|
||||
return <String, dynamic>{};
|
||||
}
|
||||
|
||||
void emitRun(JsonMap run) => _frames.add({
|
||||
'type': 'chat_shell_run_event',
|
||||
'chat_shell_run_event': {
|
||||
'session_id': 's',
|
||||
'run_id': run['id'],
|
||||
'run': run,
|
||||
},
|
||||
});
|
||||
|
||||
void emitOutput(String id, String text, int offset) => _frames.add({
|
||||
'type': 'chat_shell_run_event',
|
||||
'chat_shell_run_event': {
|
||||
'session_id': 's',
|
||||
'run_id': id,
|
||||
'output': text,
|
||||
'offset': offset,
|
||||
'end': offset + text.length,
|
||||
},
|
||||
});
|
||||
|
||||
void close() => _frames.close();
|
||||
}
|
||||
|
||||
JsonMap shellRecord(
|
||||
String id, {
|
||||
String command = 'make test',
|
||||
String status = 'running',
|
||||
int? exitCode,
|
||||
String output = '',
|
||||
int durationMs = 0,
|
||||
}) => {
|
||||
'id': id,
|
||||
'session_id': 's',
|
||||
'command': command,
|
||||
'directory': '/home/op/project',
|
||||
'status': status,
|
||||
'exit_code': ?exitCode,
|
||||
'started_at': 1000,
|
||||
'ended_at': ?(status == 'running' ? null : 1000 + durationMs),
|
||||
'duration_ms': durationMs,
|
||||
'output': output,
|
||||
'output_bytes': output.length,
|
||||
};
|
||||
|
||||
Widget shellThread(ShellGateway gateway, {bool cupertino = false}) {
|
||||
final thread = ChatThreadView(
|
||||
gateway: gateway,
|
||||
sessionId: 's',
|
||||
syncToken: 0,
|
||||
onBack: () {},
|
||||
drafts: MemoryChatDraftStore(),
|
||||
);
|
||||
return cupertino
|
||||
? CupertinoApp(home: CupertinoPageScaffold(child: thread))
|
||||
: MaterialApp(home: Scaffold(body: thread));
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import 'chat_shell_fixtures.dart';
|
||||
|
||||
void main() {
|
||||
late ShellGateway gateway;
|
||||
|
||||
setUp(() => gateway = ShellGateway());
|
||||
tearDown(() => gateway.close());
|
||||
|
||||
Finder card() => find.bySemanticsIdentifier('chat-shell-run-card');
|
||||
|
||||
for (final cupertino in [false, true]) {
|
||||
final host = cupertino ? 'Cupertino' : 'Material';
|
||||
|
||||
testWidgets('$host: a running command streams output and stops', (
|
||||
tester,
|
||||
) async {
|
||||
gateway.runs = [shellRecord('run_a', output: 'line 1\n')];
|
||||
await tester.pumpWidget(shellThread(gateway, cupertino: cupertino));
|
||||
await tester.pump();
|
||||
|
||||
expect(card(), findsOneWidget);
|
||||
expect(find.text('\$ make test'), findsOneWidget);
|
||||
expect(find.byKey(const Key('chat-shell-run-running')), findsOneWidget);
|
||||
gateway.emitOutput('run_a', 'line 2\n', 7);
|
||||
await tester.pump();
|
||||
expect(find.text('line 1\nline 2'), findsOneWidget);
|
||||
expect(find.byKey(const Key('chat-shell-run-share')), findsNothing);
|
||||
|
||||
await tester.tap(find.byKey(const Key('chat-shell-run-stop')));
|
||||
await tester.pump();
|
||||
expect(gateway.bodies('chat_shell_stop').single['run_id'], 'run_a');
|
||||
expect(find.text('Stopping…'), findsOneWidget);
|
||||
|
||||
gateway.emitRun(
|
||||
shellRecord('run_a', status: 'stopped', durationMs: 2400),
|
||||
);
|
||||
await tester.pump();
|
||||
expect(find.byKey(const Key('chat-shell-run-running')), findsNothing);
|
||||
expect(find.text('Stopped · 2.4 s'), findsOneWidget);
|
||||
expect(find.byKey(const Key('chat-shell-run-stop')), findsNothing);
|
||||
// The streamed output stays after the lifecycle frame.
|
||||
expect(find.text('line 1\nline 2'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('$host: finished commands show their exit status', (
|
||||
tester,
|
||||
) async {
|
||||
gateway.runs = [
|
||||
shellRecord(
|
||||
'run_ok',
|
||||
command: 'true',
|
||||
status: 'exited',
|
||||
exitCode: 0,
|
||||
output: 'fine\n',
|
||||
durationMs: 120,
|
||||
),
|
||||
shellRecord(
|
||||
'run_bad',
|
||||
command: 'false',
|
||||
status: 'exited',
|
||||
exitCode: 1,
|
||||
output: 'broken\n',
|
||||
durationMs: 3000,
|
||||
),
|
||||
];
|
||||
await tester.pumpWidget(shellThread(gateway, cupertino: cupertino));
|
||||
await tester.pump();
|
||||
|
||||
expect(card(), findsNWidgets(2));
|
||||
expect(find.text('Exit 0 · 120 ms'), findsOneWidget);
|
||||
expect(find.text('Exit 1 · 3.0 s'), findsOneWidget);
|
||||
expect(find.byKey(const Key('chat-shell-run-copy')), findsNWidgets(2));
|
||||
});
|
||||
|
||||
testWidgets('$host: Send to agent shares the run once', (tester) async {
|
||||
gateway.runs = [
|
||||
shellRecord('run_a', status: 'exited', exitCode: 0, output: 'ok\n'),
|
||||
];
|
||||
await tester.pumpWidget(shellThread(gateway, cupertino: cupertino));
|
||||
await tester.pump();
|
||||
|
||||
expect(gateway.calls.where((c) => c.type == 'chat_prompt'), isEmpty);
|
||||
await tester.tap(find.byKey(const Key('chat-shell-run-share')));
|
||||
await tester.pump();
|
||||
expect(gateway.bodies('chat_shell_share').single['run_id'], 'run_a');
|
||||
expect(find.text('Sent to agent'), findsOneWidget);
|
||||
await tester.tap(find.byKey(const Key('chat-shell-run-share')));
|
||||
await tester.pump();
|
||||
expect(gateway.bodies('chat_shell_share'), hasLength(1));
|
||||
});
|
||||
}
|
||||
|
||||
testWidgets('Copy output copies the retained output', (tester) async {
|
||||
final copied = <String>[];
|
||||
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
||||
SystemChannels.platform,
|
||||
(call) async {
|
||||
if (call.method == 'Clipboard.setData') {
|
||||
copied.add('${(call.arguments as Map)['text']}');
|
||||
}
|
||||
return null;
|
||||
},
|
||||
);
|
||||
addTearDown(
|
||||
() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
||||
SystemChannels.platform,
|
||||
null,
|
||||
),
|
||||
);
|
||||
gateway.runs = [
|
||||
shellRecord('run_a', status: 'exited', exitCode: 0, output: 'a\nb\n'),
|
||||
];
|
||||
await tester.pumpWidget(shellThread(gateway));
|
||||
await tester.pump();
|
||||
await tester.tap(find.byKey(const Key('chat-shell-run-copy')));
|
||||
await tester.pump();
|
||||
expect(copied, ['a\nb\n']);
|
||||
expect(find.text('Copied'), findsOneWidget);
|
||||
await tester.pump(const Duration(seconds: 3));
|
||||
});
|
||||
|
||||
testWidgets('a large text scale keeps the card readable', (tester) async {
|
||||
gateway.runs = [
|
||||
shellRecord(
|
||||
'run_a',
|
||||
status: 'exited',
|
||||
exitCode: 2,
|
||||
output: List.generate(80, (i) => 'line $i').join('\n'),
|
||||
),
|
||||
];
|
||||
tester.view.physicalSize = const Size(400, 900);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
await tester.pumpWidget(
|
||||
MediaQuery(
|
||||
data: const MediaQueryData(
|
||||
size: Size(400, 900),
|
||||
textScaler: TextScaler.linear(2),
|
||||
),
|
||||
child: shellThread(gateway),
|
||||
),
|
||||
);
|
||||
await tester.pump();
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(find.textContaining('Exit 2'), findsOneWidget);
|
||||
final expand = find.byKey(const Key('chat-shell-run-expand'));
|
||||
await tester.ensureVisible(expand);
|
||||
await tester.pump();
|
||||
await tester.tap(expand);
|
||||
await tester.pump();
|
||||
expect(find.text('Collapse'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
// Operator shell commands for the full-app probe (`?shell=1`, with
|
||||
// `?chats=N`): every fixture chat lists four recorded runs (succeeded, failed,
|
||||
// stopped and one still running), and chat_shell_run/stop/share answer as the
|
||||
// server does, with the frames that follow.
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
|
||||
bool _enabled = false;
|
||||
DateTime _base = DateTime.now();
|
||||
|
||||
void configureChatShellFixture(Map<String, String> params) {
|
||||
_enabled = params['shell'] == '1';
|
||||
_base = DateTime.now().subtract(const Duration(minutes: 3));
|
||||
}
|
||||
|
||||
int _at(int seconds) =>
|
||||
_base.add(Duration(seconds: seconds)).millisecondsSinceEpoch;
|
||||
|
||||
String _session(JsonMap request) {
|
||||
final body = request['chat_shell_run_request'] ?? request['chat_query'];
|
||||
return body is Map ? '${body['session_id'] ?? ''}' : '';
|
||||
}
|
||||
|
||||
JsonMap _run(
|
||||
String session,
|
||||
String id,
|
||||
String command,
|
||||
int started, {
|
||||
String status = 'exited',
|
||||
int? exitCode,
|
||||
int duration = 0,
|
||||
String output = '',
|
||||
}) => {
|
||||
'id': id,
|
||||
'session_id': session,
|
||||
'command': command,
|
||||
'directory': '/Users/operator/src/pipelines',
|
||||
'status': status,
|
||||
'exit_code': ?exitCode,
|
||||
'started_at': _at(started),
|
||||
if (status != 'running') 'ended_at': _at(started) + duration,
|
||||
'duration_ms': duration,
|
||||
'output': output,
|
||||
'output_bytes': output.length,
|
||||
};
|
||||
|
||||
List<JsonMap> _runs(String session) => [
|
||||
_run(
|
||||
session,
|
||||
'run_status',
|
||||
'git status --short',
|
||||
0,
|
||||
exitCode: 0,
|
||||
duration: 84,
|
||||
output:
|
||||
' M app/lib/features/chat/shell/chat_shell_run_card.dart\n'
|
||||
'?? app/notes/chat-shell-commands.md\n',
|
||||
),
|
||||
_run(
|
||||
session,
|
||||
'run_tests',
|
||||
'go test ./internal/server -run ChatShell',
|
||||
20,
|
||||
exitCode: 1,
|
||||
duration: 4210,
|
||||
output:
|
||||
'${[for (final name in _tests) '=== RUN TestChatShell$name\n--- PASS: TestChatShell$name (0.0${name.length % 9}s)\n'].join()}'
|
||||
'--- FAIL: TestChatShellStopInterruptsTheGroup (0.21s)\n'
|
||||
' chat_shell_run_test.go:112: stopped record = {Status:exited}\n'
|
||||
'FAIL\n'
|
||||
'FAIL\tgit.a-wan.net/alan/pipelines/internal/server\t0.611s\n'
|
||||
'FAIL\n',
|
||||
),
|
||||
_run(
|
||||
session,
|
||||
'run_watch',
|
||||
'tail -f /var/log/system.log',
|
||||
40,
|
||||
status: 'stopped',
|
||||
duration: 15200,
|
||||
output: 'Nov 9 15:58:01 host kernel[0]: wake reason: EC.LidOpen\n',
|
||||
),
|
||||
_run(
|
||||
session,
|
||||
'run_build',
|
||||
'flutter build web --target tool/search_selection_probe_entry.dart',
|
||||
60,
|
||||
status: 'running',
|
||||
output:
|
||||
'Compiling lib/main.dart for the Web...\n'
|
||||
'Font asset "CupertinoIcons.ttf" was tree-shaken.\n',
|
||||
),
|
||||
];
|
||||
|
||||
JsonMap? chatShellReply(JsonMap request) {
|
||||
if (!_enabled) return null;
|
||||
final id = request['id'];
|
||||
final session = _session(request);
|
||||
final body = request['chat_shell_run_request'];
|
||||
final runId = body is Map ? '${body['run_id'] ?? ''}' : '';
|
||||
switch ('${request['type']}') {
|
||||
case 'chat_shell_runs':
|
||||
return {
|
||||
'id': id,
|
||||
'type': 'chat_shell_runs',
|
||||
'chat_shell_runs': _runs(session),
|
||||
};
|
||||
case 'chat_shell_run':
|
||||
return {
|
||||
'id': id,
|
||||
'type': 'chat_shell_run',
|
||||
'chat_shell_run': _run(
|
||||
session,
|
||||
'run_new',
|
||||
body is Map ? '${body['command'] ?? ''}' : '',
|
||||
180,
|
||||
status: 'running',
|
||||
),
|
||||
};
|
||||
case 'chat_shell_stop':
|
||||
return {'id': id, 'type': 'chat_shell_stop'};
|
||||
case 'chat_shell_run_output':
|
||||
return {
|
||||
'id': id,
|
||||
'type': 'chat_shell_run_output',
|
||||
'chat_shell_run': _runs(session).firstWhere(
|
||||
(run) => run['id'] == runId,
|
||||
orElse: () => _runs(session).first,
|
||||
),
|
||||
};
|
||||
case 'chat_shell_share':
|
||||
return {'id': id, 'type': 'chat_prompt'};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// The frames the server pushes after a run starts or is stopped.
|
||||
List<JsonMap> chatShellFollowUps(JsonMap request) {
|
||||
if (!_enabled) return const [];
|
||||
final session = _session(request);
|
||||
JsonMap frame(JsonMap body) => {
|
||||
'type': 'chat_shell_run_event',
|
||||
'chat_shell_run_event': {'session_id': session, ...body},
|
||||
};
|
||||
switch ('${request['type']}') {
|
||||
case 'chat_shell_run':
|
||||
const text = 'hello from the fixture shell\n';
|
||||
final body = request['chat_shell_run_request'];
|
||||
return [
|
||||
frame({
|
||||
'run_id': 'run_new',
|
||||
'output': text,
|
||||
'offset': 0,
|
||||
'end': text.length,
|
||||
}),
|
||||
frame({
|
||||
'run_id': 'run_new',
|
||||
'run': _run(
|
||||
session,
|
||||
'run_new',
|
||||
body is Map ? '${body['command'] ?? ''}' : '',
|
||||
180,
|
||||
exitCode: 0,
|
||||
duration: 37,
|
||||
),
|
||||
}),
|
||||
];
|
||||
case 'chat_shell_stop':
|
||||
final run = _runs(session).last
|
||||
..['status'] = 'stopped'
|
||||
..['duration_ms'] = 9100;
|
||||
return [
|
||||
frame({'run_id': run['id'], 'run': run}),
|
||||
];
|
||||
}
|
||||
return const [];
|
||||
}
|
||||
|
||||
const _tests = [
|
||||
'RunStreamsToEveryWatcherAndPersists',
|
||||
'RunLimitsAndRefusals',
|
||||
'ShareQueuesAnOrdinaryPrompt',
|
||||
'RetainsBoundedTail',
|
||||
'ShutdownAndRestartInterrupt',
|
||||
'OutputKeepsCharactersWhole',
|
||||
];
|
||||
@@ -16,6 +16,8 @@
|
||||
// `?msgs=`, `?rich=1`, `?older=1`, `?live=N` vary the chat transcripts for
|
||||
// scroll captures (tool/perf/chat_scroll_fixture.dart).
|
||||
// `?question=` pushes an agent question (tool/perf/chat_question_fixture.dart).
|
||||
// `?shell=1` lists operator shell commands in every chat
|
||||
// (tool/perf/chat_shell_fixture.dart).
|
||||
// `/new` in a fixture chat moves it to a fresh, empty session
|
||||
// (tool/perf/chat_new_session_fixture.dart).
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
@@ -23,6 +25,7 @@ import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'chat_new_session_fixture.dart';
|
||||
import 'chat_turn_diff_fixture.dart';
|
||||
import 'chat_question_fixture.dart';
|
||||
import 'chat_shell_fixture.dart';
|
||||
import 'chat_scroll_fixture.dart';
|
||||
|
||||
Map<String, String> _params = const {};
|
||||
@@ -36,6 +39,7 @@ void configurePerfFixture(Map<String, String> params) {
|
||||
);
|
||||
configureChatScrollFixture(params);
|
||||
configureChatQuestionFixture(params);
|
||||
configureChatShellFixture(params);
|
||||
}
|
||||
|
||||
int get _hits => int.tryParse(_params['hits'] ?? '') ?? 0;
|
||||
@@ -127,6 +131,8 @@ bool get _scrollVariety => ['msgs', 'rich', 'older'].any(_params.containsKey);
|
||||
List<JsonMap> perfChatFollowUps(JsonMap request) {
|
||||
final answered = chatQuestionFollowUps(request);
|
||||
if (answered.isNotEmpty) return answered;
|
||||
final shell = chatShellFollowUps(request);
|
||||
if (shell.isNotEmpty) return shell;
|
||||
final quota = _params['quota'];
|
||||
if (_chats == 0 || quota == null || request['type'] != 'chat_subscribe') {
|
||||
return const [];
|
||||
@@ -227,6 +233,8 @@ JsonMap? perfChatReply(JsonMap request) {
|
||||
if (_chats == 0) return null;
|
||||
final question = chatQuestionReply(request);
|
||||
if (question != null) return question;
|
||||
final shell = chatShellReply(request);
|
||||
if (shell != null) return shell;
|
||||
final id = request['id'];
|
||||
switch ('${request['type']}') {
|
||||
case 'chat_sessions':
|
||||
|
||||
Reference in New Issue
Block a user