mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 05:26:47 +03:00
fix(acp): stream running shell output on in_progress tool updates
This commit is contained in:
@@ -85,6 +85,7 @@ export function runningToolUpdate(input: {
|
||||
readonly toolCallId: string
|
||||
readonly toolName: string
|
||||
readonly state: { readonly input: ToolInput }
|
||||
readonly output?: string
|
||||
readonly cwd: string
|
||||
}): ToolCallUpdate {
|
||||
return {
|
||||
@@ -94,6 +95,7 @@ export function runningToolUpdate(input: {
|
||||
title: toolTitle(input.toolName, input.state.input),
|
||||
locations: toLocations(input.toolName, input.state.input, input.cwd),
|
||||
rawInput: rawInput(input.toolName, input.state.input, input.cwd),
|
||||
...(input.output ? { content: [{ type: "content", content: { type: "text", text: input.output } }] } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,11 +3,14 @@ import type { OpenCodeEvent } from "@opencode/client/effect"
|
||||
import type { Session } from "@opencode/schema/session"
|
||||
import type { SessionError } from "@opencode/schema/session-error"
|
||||
import type { SessionMessage } from "@opencode/schema/session-message"
|
||||
import { Shell } from "@opencode/schema/shell"
|
||||
import { TokenUsage } from "@opencode/schema/token-usage"
|
||||
import { Schema } from "effect"
|
||||
import { ACPChild } from "./child"
|
||||
import { ACPCompaction } from "./compaction"
|
||||
import { ACPError } from "./error"
|
||||
import {
|
||||
canonicalName,
|
||||
completedToolUpdate,
|
||||
errorToolUpdate,
|
||||
pendingToolCall,
|
||||
@@ -17,6 +20,7 @@ import {
|
||||
} from "./tool"
|
||||
|
||||
const RetryMeta = "opencode/retry"
|
||||
const isShellID = Schema.is(Shell.ID)
|
||||
|
||||
export type TurnStart = { readonly type: "input" | "compaction"; readonly id: SessionMessage.ID }
|
||||
|
||||
@@ -30,7 +34,7 @@ export type TurnContext = {
|
||||
readonly compaction: boolean
|
||||
}
|
||||
|
||||
type Tool = {
|
||||
export type Tool = {
|
||||
readonly sessionID: string
|
||||
readonly id: string
|
||||
readonly name: string
|
||||
@@ -73,6 +77,16 @@ export type Output =
|
||||
}
|
||||
| { readonly _tag: "FormAsk"; readonly form: FormEvent["data"]["form"]; readonly child?: ACPChild.Session }
|
||||
| { readonly _tag: "AskSettled"; readonly id: string }
|
||||
| { readonly _tag: "ShellFollow"; readonly shell: FollowedShell }
|
||||
| { readonly _tag: "ShellStop"; readonly key: string }
|
||||
|
||||
/** A running shell tool whose output the client is shown as it runs. */
|
||||
export type FollowedShell = {
|
||||
readonly key: string
|
||||
readonly shellID: Shell.ID
|
||||
readonly tool: Tool
|
||||
readonly child?: ACPChild.Session
|
||||
}
|
||||
|
||||
export type Folded = {
|
||||
readonly state: TurnState
|
||||
@@ -172,6 +186,7 @@ export function abandon(state: TurnState, ctx: TurnContext): Folded {
|
||||
return {
|
||||
state: { ...state, tools: new Map(), compactions: without(state.compactions, ctx.sessionID) },
|
||||
outputs: [
|
||||
...[...state.tools.values()].flatMap(shellStop),
|
||||
...[...state.tools.values()].flatMap((tool) =>
|
||||
route(ctx, state.children.get(tool.sessionID), {
|
||||
sessionUpdate: "tool_call_update",
|
||||
@@ -353,17 +368,24 @@ function sessionEvent(
|
||||
const key = toolKey(event.data.sessionID, event.data.id)
|
||||
const current = state.tools.get(key)
|
||||
if (!current) return { state, outputs: [] }
|
||||
const tool = { ...current, metadata: event.data.metadata }
|
||||
const shellID = canonicalName(tool.name) === "shell" ? tool.metadata.shellID : undefined
|
||||
return {
|
||||
state: { ...state, tools: new Map(state.tools).set(key, { ...current, metadata: event.data.metadata }) },
|
||||
outputs: send({
|
||||
sessionUpdate: "tool_call_update",
|
||||
...runningToolUpdate({
|
||||
toolCallId: event.data.id,
|
||||
toolName: current.name,
|
||||
state: { input: current.input },
|
||||
cwd: ctx.cwd,
|
||||
state: { ...state, tools: new Map(state.tools).set(key, tool) },
|
||||
outputs: [
|
||||
...send({
|
||||
sessionUpdate: "tool_call_update",
|
||||
...runningToolUpdate({
|
||||
toolCallId: event.data.id,
|
||||
toolName: tool.name,
|
||||
state: { input: tool.input },
|
||||
cwd: ctx.cwd,
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
...(isShellID(shellID) && shellID !== current.metadata.shellID
|
||||
? [{ _tag: "ShellFollow" as const, shell: { key, shellID, tool, child } }]
|
||||
: []),
|
||||
],
|
||||
}
|
||||
}
|
||||
case "session.tool.success": {
|
||||
@@ -371,20 +393,23 @@ function sessionEvent(
|
||||
const tool = state.tools.get(key) ?? newTool(event.data.sessionID, event.data.id)
|
||||
return {
|
||||
state: { ...state, tools: without(state.tools, key) },
|
||||
outputs: send({
|
||||
sessionUpdate: "tool_call_update",
|
||||
...completedToolUpdate({
|
||||
toolCallId: event.data.id,
|
||||
toolName: tool.name,
|
||||
input: tool.input,
|
||||
metadata: event.data.metadata,
|
||||
content: event.data.content,
|
||||
cwd: ctx.cwd,
|
||||
}),
|
||||
}).map((output) => ({
|
||||
...output,
|
||||
diff: { toolName: tool.name, input: tool.input, metadata: event.data.metadata },
|
||||
})),
|
||||
outputs: [
|
||||
...shellStop(tool),
|
||||
...send({
|
||||
sessionUpdate: "tool_call_update",
|
||||
...completedToolUpdate({
|
||||
toolCallId: event.data.id,
|
||||
toolName: tool.name,
|
||||
input: tool.input,
|
||||
metadata: event.data.metadata,
|
||||
content: event.data.content,
|
||||
cwd: ctx.cwd,
|
||||
}),
|
||||
}).map((output) => ({
|
||||
...output,
|
||||
diff: { toolName: tool.name, input: tool.input, metadata: event.data.metadata },
|
||||
})),
|
||||
],
|
||||
}
|
||||
}
|
||||
case "session.tool.failed": {
|
||||
@@ -392,18 +417,21 @@ function sessionEvent(
|
||||
const tool = state.tools.get(key) ?? newTool(event.data.sessionID, event.data.id)
|
||||
return {
|
||||
state: { ...state, tools: without(state.tools, key) },
|
||||
outputs: send({
|
||||
sessionUpdate: "tool_call_update",
|
||||
...errorToolUpdate({
|
||||
toolCallId: event.data.id,
|
||||
toolName: tool.name,
|
||||
input: tool.input,
|
||||
metadata: event.data.metadata ?? tool.metadata,
|
||||
content: event.data.content ?? [],
|
||||
error: event.data.error.message,
|
||||
cwd: ctx.cwd,
|
||||
outputs: [
|
||||
...shellStop(tool),
|
||||
...send({
|
||||
sessionUpdate: "tool_call_update",
|
||||
...errorToolUpdate({
|
||||
toolCallId: event.data.id,
|
||||
toolName: tool.name,
|
||||
input: tool.input,
|
||||
metadata: event.data.metadata ?? tool.metadata,
|
||||
content: event.data.content ?? [],
|
||||
error: event.data.error.message,
|
||||
cwd: ctx.cwd,
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
],
|
||||
}
|
||||
}
|
||||
default:
|
||||
@@ -411,6 +439,24 @@ function sessionEvent(
|
||||
}
|
||||
}
|
||||
|
||||
/** The running update that shows a shell tool's output so far. */
|
||||
export function shellOutput(ctx: TurnContext, shell: FollowedShell, output: string): Output[] {
|
||||
return route(ctx, shell.child, {
|
||||
sessionUpdate: "tool_call_update",
|
||||
...runningToolUpdate({
|
||||
toolCallId: shell.tool.id,
|
||||
toolName: shell.tool.name,
|
||||
state: { input: shell.tool.input },
|
||||
output,
|
||||
cwd: ctx.cwd,
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
function shellStop(tool: Tool): Output[] {
|
||||
return isShellID(tool.metadata.shellID) ? [{ _tag: "ShellStop", key: toolKey(tool.sessionID, tool.id) }] : []
|
||||
}
|
||||
|
||||
function newTool(sessionID: string, id: string, name = "tool"): Tool {
|
||||
return { sessionID, id, name, input: {}, metadata: {} }
|
||||
}
|
||||
|
||||
@@ -41,11 +41,20 @@ export const CancelDrainTimeout = Context.Reference<Duration.Input>("@opencode/c
|
||||
defaultValue: () => "5 seconds",
|
||||
})
|
||||
|
||||
/** How often a running shell tool's output is read once caught up, matching the TUI. */
|
||||
export const ShellOutputInterval = Context.Reference<Duration.Input>("@opencode/cli/acp/Turn/ShellOutputInterval", {
|
||||
defaultValue: () => "1 second",
|
||||
})
|
||||
|
||||
// Matches the running output V1 streamed, so a noisy command cannot flood the client.
|
||||
const ShellPreviewLength = 30_000
|
||||
|
||||
type Subscription = {
|
||||
readonly scope: Scope.Closeable
|
||||
readonly events: Queue.Dequeue<OpenCodeEvent, unknown>
|
||||
readonly askQueue: Queue.Queue<Effect.Effect<void, ACPError.Error | RequestError>>
|
||||
readonly settled: Map<string, Deferred.Deferred<void>>
|
||||
readonly shells: FiberMap.FiberMap<string>
|
||||
}
|
||||
|
||||
type Turn = {
|
||||
@@ -77,6 +86,7 @@ export const make = Effect.fnUntraced(function* (input: {
|
||||
.pipe(Stream.toQueue({ capacity: "unbounded" }), Scope.provide(subscriptionScope)),
|
||||
askQueue: yield* Queue.unbounded<Effect.Effect<void, ACPError.Error | RequestError>>(),
|
||||
settled: new Map(),
|
||||
shells: yield* FiberMap.make<string>().pipe(Scope.provide(subscriptionScope)),
|
||||
}
|
||||
yield* Queue.take(subscription.askQueue).pipe(
|
||||
Effect.flatten,
|
||||
@@ -190,9 +200,49 @@ export const make = Effect.fnUntraced(function* (input: {
|
||||
turn.subscription.settled.delete(output.id)
|
||||
return settled ? Deferred.succeed(settled, undefined) : Effect.void
|
||||
})
|
||||
case "ShellFollow":
|
||||
return FiberMap.run(turn.subscription.shells, output.shell.key, followShell(turn, output.shell)).pipe(
|
||||
Effect.asVoid,
|
||||
)
|
||||
// Waits for an in-flight output update, so none follows the tool's terminal update.
|
||||
case "ShellStop":
|
||||
return FiberMap.remove(turn.subscription.shells, output.key)
|
||||
}
|
||||
}
|
||||
|
||||
const followShell: (turn: Turn, shell: ACPTranslate.FollowedShell) => Effect.Effect<void> = Effect.fnUntraced(
|
||||
function* (turn: Turn, shell: ACPTranslate.FollowedShell) {
|
||||
const interval = yield* ShellOutputInterval
|
||||
const state = { cursor: 0, text: "", truncated: false }
|
||||
while (true) {
|
||||
const page = (yield* input.client.shell.output({
|
||||
id: shell.shellID,
|
||||
cursor: state.cursor,
|
||||
location: { directory: turn.ctx.cwd },
|
||||
})).data
|
||||
// Far behind a noisy command, skip to the output that the preview can still show.
|
||||
const skip = page.size - page.cursor > ShellPreviewLength
|
||||
const text = skip ? "" : state.text + page.output
|
||||
state.truncated ||= skip || text.length > ShellPreviewLength
|
||||
state.text = text.slice(-ShellPreviewLength)
|
||||
state.cursor = skip ? page.size - ShellPreviewLength : page.cursor
|
||||
if (!skip && page.output) {
|
||||
const preview = state.truncated ? `...\n\n${state.text}` : state.text
|
||||
yield* Effect.forEach(
|
||||
ACPTranslate.shellOutput(turn.ctx, shell, preview),
|
||||
(output) => interpret(turn, output),
|
||||
{ discard: true },
|
||||
).pipe(Effect.uninterruptible)
|
||||
}
|
||||
if (!page.output || state.cursor >= page.size) yield* Effect.sleep(interval)
|
||||
}
|
||||
},
|
||||
// The terminal update still carries the full output, so a failed read only stops the live view.
|
||||
Effect.catchCause((cause) =>
|
||||
Cause.hasInterruptsOnly(cause) ? Effect.void : Effect.logWarning("ACP shell output failed", cause),
|
||||
),
|
||||
)
|
||||
|
||||
const advance = Effect.fnUntraced(function* (turn: Turn) {
|
||||
const event = yield* take(turn.subscription)
|
||||
const folded = yield* Ref.modify(turn.state, (current) => {
|
||||
|
||||
@@ -160,6 +160,45 @@ describe("acp prompt turns over the wire", () => {
|
||||
})
|
||||
})
|
||||
|
||||
test("streams a running shell command's output until the tool completes", async () => {
|
||||
const shell = { output: "line 1\n" }
|
||||
await using acp = await startSession({
|
||||
shellOutputInterval: "10 millis",
|
||||
onPrompt: ({ sessionID, id }) => [
|
||||
delivered(sessionID, id),
|
||||
toolStarted(sessionID, "call_shell", "shell"),
|
||||
toolCalled(sessionID, "call_shell", { command: "for i in 1 2; do echo line $i; sleep 1; done" }),
|
||||
toolProgress(sessionID, "call_shell", { shellID: "sh_1" }),
|
||||
],
|
||||
fetch: (request) => {
|
||||
if (request.path !== "/api/shell/sh_1/output") return
|
||||
const output = shell.output.slice(Number(request.query.cursor ?? 0))
|
||||
return Response.json({
|
||||
location: { directory: "/workspace", project: { id: "global", directory: "/workspace" } },
|
||||
data: { output, cursor: shell.output.length, size: shell.output.length, truncated: false },
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
const prompt = acp.prompt(acp.sessionId, "run it")
|
||||
await acp.waitForUpdate((item) => runningText(item.update) === "line 1\n", "first line")
|
||||
shell.output += "line 2\n"
|
||||
await acp.waitForUpdate((item) => runningText(item.update) === "line 1\nline 2\n", "second line")
|
||||
acp.server.send(toolSucceeded(acp.sessionId, "call_shell", { exit: 0 }, "line 1\nline 2"), succeeded(acp.sessionId))
|
||||
expect((await prompt).stopReason).toBe("end_turn")
|
||||
|
||||
const updates = turnUpdates(acp.updates).map((item) => item.update)
|
||||
expect(updates.findLast((update) => runningText(update) !== undefined)).toMatchObject({
|
||||
toolCallId: "call_shell",
|
||||
status: "in_progress",
|
||||
title: "for i in 1 2; do echo line $i; sleep 1; done",
|
||||
})
|
||||
expect(updates.at(-1)).toMatchObject({ toolCallId: "call_shell", status: "completed" })
|
||||
expect(acp.server.requests.find((request) => request.path === "/api/shell/sh_1/output")?.query).toMatchObject({
|
||||
"location[directory]": "/workspace",
|
||||
})
|
||||
})
|
||||
|
||||
test("reports full-file diffs for a completed edit and patch", async () => {
|
||||
await using dir = await tmpdir()
|
||||
const file = (name: string) => path.resolve(dir.path, name)
|
||||
@@ -583,6 +622,12 @@ function trimmed(file: string, before: string, after: string) {
|
||||
}
|
||||
}
|
||||
|
||||
function runningText(update: SessionNotification["update"]) {
|
||||
if (update.sessionUpdate !== "tool_call_update" || update.status !== "in_progress") return
|
||||
const part = update.content?.[0]
|
||||
return part?.type === "content" && part.content.type === "text" ? part.content.text : undefined
|
||||
}
|
||||
|
||||
function turnUpdates(updates: readonly SessionNotification[]) {
|
||||
return updates.filter(
|
||||
(item) => item.update.sessionUpdate !== "available_commands_update" && item.update.sessionUpdate !== "usage_update",
|
||||
|
||||
@@ -146,6 +146,7 @@ export type WireOptions = {
|
||||
signal: AbortSignal,
|
||||
) => CreateElicitationResponse | Promise<CreateElicitationResponse>
|
||||
readonly cancelDrainTimeout?: Duration.Input
|
||||
readonly shellOutputInterval?: Duration.Input
|
||||
}
|
||||
|
||||
type FormReply = {
|
||||
@@ -436,6 +437,10 @@ export async function startWire(options: WireOptions = {}) {
|
||||
options.cancelDrainTimeout === undefined
|
||||
? effect
|
||||
: Effect.provideService(effect, ACPTurn.CancelDrainTimeout, options.cancelDrainTimeout),
|
||||
(effect) =>
|
||||
options.shellOutputInterval === undefined
|
||||
? effect
|
||||
: Effect.provideService(effect, ACPTurn.ShellOutputInterval, options.shellOutputInterval),
|
||||
Effect.provide(Logger.layer([Logger.make((log) => logs.push({ message: log.message, cause: log.cause }))])),
|
||||
),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user