|
Expand Up
|
@@ -26,6 +26,7 @@ import { toolFailure } from "../runtime.js" |
|
|
|
|
|
export const name = "shell" |
|
|
export const DEFAULT_TIMEOUT_MS = 2 * 60 * 1_000 |
|
|
const CODE_MODE_TIMEOUT_MS = 10 * 60 * 1_000 |
|
|
// How long a call the user interrupted waits for its stopped command to exit before settling with the output so far. |
|
|
const INTERRUPT_WAIT = "500 millis" |
|
|
|
|
Expand All
|
@@ -39,7 +40,7 @@ const OS = |
|
|
: process.platform === "linux" |
|
|
? "Linux" |
|
|
: process.platform |
|
|
const description = (shell?: string) => |
|
|
const description = (shell?: string, codeMode = false) => |
|
|
[ |
|
|
"Execute a shell command and return its output.", |
|
|
...(shell ? [`Commands run on ${OS} using ${shell}.`] : []), |
|
Expand All
|
@@ -48,33 +49,55 @@ const description = (shell?: string) => |
|
|
"When output is large, the full result is saved to a file and a truncated preview is returned.", |
|
|
"Rely on automatic truncation unless filtering the output is more useful.", |
|
|
"Commands still running after their timeout move to the background.", |
|
|
"Background commands return immediately, and you will be notified when they complete.", |
|
|
// Code Mode has no background parameter. |
|
|
...(codeMode ? [] : ["Background commands return immediately, and you will be notified when they complete."]), |
|
|
"The current session ID is available in the OPENCODE_SESSION_ID environment variable.", |
|
|
].join(" ") |
|
|
|
|
|
const timeoutInput = (foreground: number) => |
|
|
Schema.optionalKey(NonNegativeInt).annotate({ |
|
|
description: `Optional timeout in milliseconds (default ${foreground}). When it expires, a foreground command moves to the background and a background command is stopped.`, |
|
|
}) |
|
|
|
|
|
export const Input = Schema.Struct({ |
|
|
command: Schema.String.annotate({ description: "Shell command string to execute" }), |
|
|
workdir: Schema.optionalKey(Schema.String).annotate({ |
|
|
description: |
|
|
"Working directory to execute the command in. Defaults to the current working directory. When possible, avoid changing directories in the command and set the working directory here instead.", |
|
|
}), |
|
|
timeout: Schema.optionalKey(NonNegativeInt).annotate({ |
|
|
description: `Optional timeout in milliseconds (default ${DEFAULT_TIMEOUT_MS}). When it expires, a foreground command moves to the background and a background command is stopped.`, |
|
|
}), |
|
|
timeout: timeoutInput(DEFAULT_TIMEOUT_MS), |
|
|
background: Schema.optionalKey(Schema.Boolean).annotate({ |
|
|
description: |
|
|
"Run the command in the background and return immediately (useful for dev servers and long-running builds). You do not need to use '&' at the end of the command when using this parameter. You will be notified when it completes. DO NOT poll for completion.", |
|
|
}), |
|
|
}) |
|
|
|
|
|
const CodeModeInput = Schema.Struct({ |
Comment thread
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With background gone, Code Mode has no way to start a dev server or watcher in the background. The call holds execute open until the 10-minute default runs out, unless the model knows to pass a short timeout. A model used to the direct tool may still pass background: true, and input decoding drops unknown keys silently, so that call blocks for 10 minutes with no error to react to. Could Code Mode either keep background, or reject the key so the model learns to use timeout instead?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
|
...Struct.omit(Input.fields, ["background"]), |
|
|
timeout: timeoutInput(CODE_MODE_TIMEOUT_MS), |
|
|
}) |
|
|
|
|
|
const StructuredOutput = Schema.Struct({ |
|
|
output: Schema.String, |
|
|
exitCode: Schema.NullOr(Schema.Number), |
|
|
wallTimeSeconds: Schema.Number, |
|
|
outputPath: Schema.optionalKey(Schema.String), |
|
|
signal: Schema.optionalKey(Schema.String), |
|
|
shellID: Schema.optionalKey(Schema.String), |
|
|
pid: Schema.optionalKey(NonNegativeInt), |
|
|
shellID: Schema.optionalKey(Schema.String).annotate({ |
|
|
description: |
|
|
"Present only while the command is still running in the background. You will be notified when it exits. A result without shellID is final.", |
|
|
}), |
|
|
exitCode: Schema.NullOr(Schema.Number).annotate({ |
|
|
description: |
|
|
"The exit code once the command has exited: 0 means success. null while it is still running, or when a signal ended it.", |
|
|
}), |
|
|
signal: Schema.optionalKey(Schema.String).annotate({ description: "The signal that ended the command, if any" }), |
|
|
output: Schema.String.annotate({ |
|
|
description: |
|
|
"Combined stdout and stderr as one string; there are no separate stdout or stderr fields. While shellID is present it is a notice instead, and the output streams to outputPath.", |
|
|
}), |
|
|
outputPath: Schema.optionalKey(Schema.String).annotate({ |
|
|
description: "File with the full output, when output was truncated or the command is running in the background", |
|
|
}), |
|
|
pid: Schema.optionalKey(NonNegativeInt).annotate({ |
|
|
description: "Process ID while the command is running in the background", |
|
|
}), |
|
|
wallTimeSeconds: Schema.Number.annotate({ description: "Seconds since the command started" }), |
|
|
}) |
|
|
|
|
|
type Output = ReturnType<typeof ShellResult.output> & { |
|
Expand Down
Expand Up
|
@@ -287,7 +310,6 @@ export const Plugin = { |
|
|
// Internal plugins are not re-run on config changes, so reload the tool transform instead. |
|
|
const loaded = yield* ConfigEntryObserver.observe(config, ctx.event, ctx.tool.reload()) |
|
|
const flags = yield* Flag.Service |
|
|
const fullCodeMode = () => flags.resolve(loaded.entries).full_codemode.enabled |
|
|
|
|
|
const prepare = Effect.fn("ShellTool.prepare")(function* (invocation: ShellCreateBefore, context: Tool.Context) { |
|
|
const target = yield* access.resolve({ path: invocation.cwd, kind: "directory" }) |
|
Expand Down
Expand Up
|
@@ -351,18 +373,22 @@ export const Plugin = { |
|
|
}) |
|
|
|
|
|
yield* ctx.tool |
|
|
.transform((editor) => |
|
|
.transform((editor) => { |
|
|
const codeMode = flags.resolve(loaded.entries).full_codemode.enabled |
|
|
editor.add({ |
|
|
name, |
|
|
// With full_codemode, shell is called as `tools.shell` inside `execute` instead of as a direct tool. |
|
|
options: { codemode: fullCodeMode() }, |
|
|
description: description(), |
|
|
input: Input, |
|
|
// With full_codemode, shell is called as `tools.shell` inside `execute` instead of as a direct tool, and is |
|
|
// pinned so the Code Mode catalog always lists it, however many other tools there are. |
|
|
options: codeMode ? { codemode: true, pinned: true } : { codemode: false }, |
|
|
description: description(undefined, codeMode), |
|
|
input: codeMode ? CodeModeInput : Input, |
|
|
output: StructuredOutput, |
|
|
execute: (input, context) => { |
|
|
// Code Mode input has no background parameter. |
|
|
const background = "background" in input && input.background === true |
|
|
// A foreground timeout of 0 falls back to the default, so a call can't block until the command exits. |
|
|
const foreground = input.timeout || DEFAULT_TIMEOUT_MS |
|
|
const timeout = input.background === true ? (input.timeout ?? 0) : foreground |
|
|
const foreground = input.timeout || (codeMode ? CODE_MODE_TIMEOUT_MS : DEFAULT_TIMEOUT_MS) |
|
|
const timeout = background ? (input.timeout ?? 0) : foreground |
|
|
let finalTimeout = timeout |
|
|
// Uninterruptible from spawning to blocking, so an interrupt cannot orphan a started command. |
|
|
return Effect.uninterruptibleMask((restore) => |
|
Expand All
|
@@ -385,7 +411,7 @@ export const Plugin = { |
|
|
timeout, |
|
|
shell: yield* compatibleShell, |
|
|
metadata: { sessionID: context.sessionID }, |
|
|
waited: input.background !== true, |
|
|
waited: !background, |
|
|
toolCall: { |
|
|
sessionID: context.sessionID, |
|
|
assistantMessageID: context.messageID, |
|
Expand All
|
@@ -412,7 +438,7 @@ export const Plugin = { |
|
|
), |
|
|
) |
|
|
// A foreground timeout moves the command to the background instead of stopping it. |
|
|
if (input.background !== true) invocation.timeout = 0 |
|
|
if (!background) invocation.timeout = 0 |
|
|
}), |
|
|
) |
|
|
yield* context.progress({ shellID: info.id }) |
|
Expand All
|
@@ -425,7 +451,7 @@ export const Plugin = { |
|
|
// Stopped while the command was starting: stop it now. |
|
|
if (yield* Deferred.isDone(stopped)) return yield* cancelled(info, job.id) |
|
|
|
|
|
if (input.background === true) { |
|
|
if (background) { |
|
|
yield* jobs.background(job.id) |
|
|
yield* notifyWhenDone(context.sessionID, job.id, info.id, info.command, settled) |
|
|
return backgroundResult(info, yield* Clock.currentTimeMillis) |
|
Expand Down
Expand Up
|
@@ -482,14 +508,14 @@ export const Plugin = { |
|
|
// Shell spans, including the job that waits for a backgrounded command, name the call that started them. |
|
|
Effect.annotateSpans({ |
|
|
"gen_ai.tool.call.id": context.id, |
|
|
...(input.background === true ? { "opencode.shell.background": true } : {}), |
|
|
...(background ? { "opencode.shell.background": true } : {}), |
|
|
}), |
|
|
Effect.map((output) => toolResult(output, finalTimeout)), |
|
|
Effect.mapError(toolFailure(`Unable to execute command: ${input.command}`)), |
|
|
) |
|
|
}, |
|
|
}), |
|
|
) |
|
|
}) |
|
|
}) |
|
|
.pipe(Effect.orDie) |
|
|
|
|
|
const hook = (event: SessionHooks["context"]) => |
|
Expand Down
|
|