feat: watch background commands, open their output and stop them

A command the agent detached keeps running after its tool call returns;
the chat now shows it above the composer: one row per command with its
clock and a stop, folded under "正在执行 N 条命令" when there are
several, with a subagent tag for a child session's command.

Tapping a row opens the live output in a sheet (a page a second from
shell.output, tail kept), following the tail with a way back to the
newest line. The tool row that started it carries a 后台 tag and shows
the same output when expanded, falling back to the completion note once
the shell is gone; a command stopped from the app says so instead of
showing the Shell.NotFoundError it leaves behind.

The list comes from opencode's own shell jobs (experimental/job,
descendants included), so a foreground command never appears, and an
older server falls back to the shell list narrowed by the detached
calls in the transcript. Stopping confirms, removes the shell, and
leaves the model a note that the stop was the user's, as the
first-party client does.

0.4.14 (414).
This commit is contained in:
blumia committed 2026-11-24 19:31:22 +08:00
1 parent 809e0a747a
commit c4de5e84d8
11 files changed
+1749 -7

No files matched your search

+78
View File
@@ -8,6 +8,7 @@ import 'dart:typed_data';
import '../version.dart';
import 'models.dart';
import 'shells.dart';
import 'terminal.dart';
import 'transport.dart';
@@ -292,6 +293,83 @@ class Client {
directory: directory);
}
/// The shells started in [directory], running ones and ones that just
/// exited (the list keeps an exited shell for a while). A background command
/// is one the transcript marks with `{status: running, shellID}`.
Future<List<ShellTask>> listShells({String? directory}) async {
final r = await _json('GET', '/api/shell', directory: directory);
return shellTasksFromList(r);
}
/// The running jobs of a session (and its children with [descendants]):
/// shell commands opencode detached, plus unrelated kinds the caller
/// filters out. Empty on an opencode without the experimental endpoint
/// (404).
Future<List<ShellTask>> listShellJobs(String sessionId,
{bool descendants = true}) async {
final r = await _json('GET', '/api/experimental/job', query: {
'sessionID': sessionId,
if (descendants) 'descendants': 'true',
'status': 'running',
});
return shellTasksFromJobs(r);
}
/// One page of a shell's output. Without [cursor] the server reads from the
/// start; [size] in the answer says how much there is, so the first call can
/// ask again from `size - tail` for a long command.
Future<ShellOutputPage> shellOutput(String shellId,
{int? cursor, int? limit, String? directory}) async {
final query = <String, String>{
if (cursor != null) 'cursor': '$cursor',
if (limit != null) 'limit': '$limit',
};
final r = await _json(
'GET', '/api/shell/${Uri.encodeComponent(shellId)}/output',
query: query.isEmpty ? null : query, directory: directory);
return ShellOutputPage.fromJson(r);
}
/// One shell by id; null when the server no longer knows it (removed).
Future<ShellTask?> shell(String shellId, {String? directory}) async {
try {
final r = await _json(
'GET', '/api/shell/${Uri.encodeComponent(shellId)}',
directory: directory);
return ShellTask.fromWire(r['data']);
} on ApiError catch (e) {
if (e.status == 404) return null;
rethrow;
}
}
/// Stops a background shell (SIGTERM through its pid). The command's
/// completion notice then reports a `Shell.NotFoundError`-like failure, so
/// [syntheticNote] tells the model that the user stopped it.
Future<void> stopShell(String shellId, {String? directory}) async {
await _json('DELETE', '/api/shell/${Uri.encodeComponent(shellId)}',
directory: directory);
}
/// Writes a note into the session without starting a turn: how the model
/// learns that a command was stopped by the user (or anything else the
/// client did), as the first-party client does.
Future<void> syntheticNote(String sessionId,
{required String text,
String? description,
Map<String, dynamic>? metadata,
String? directory}) async {
await _json('POST',
'/api/session/${Uri.encodeComponent(sessionId)}/synthetic',
directory: directory,
body: {
'text': text,
if (description != null) 'description': description,
if (metadata != null) 'metadata': metadata,
'resume': false,
});
}
/// Moves a waiting prompt between `steer` and `queue`.
Future<void> updateInbox(String sessionId, String inboxId, String delivery,
{String? directory}) async {