Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/bright-terminals-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kilocode/cli": minor
---

Run commands that require human interaction in an embedded CLI terminal dialog and return their output to the model when complete.
2 changes: 2 additions & 0 deletions packages/opencode/src/agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export const layer = Layer.effect(
},
suggest: "deny", // kilocode_change
question: "deny",
interactive_terminal: "deny", // kilocode_change - human-driven tools are primary-agent only
plan_enter: "deny",
plan_exit: "deny",
repo_clone: "deny",
Expand Down Expand Up @@ -159,6 +160,7 @@ export const layer = Layer.effect(
defaults,
Permission.fromConfig({
question: "allow",
interactive_terminal: "allow", // kilocode_change
suggest: "allow", // kilocode_change
plan_enter: "allow",
}),
Expand Down
5 changes: 5 additions & 0 deletions packages/opencode/src/cli/cmd/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@ export const RunCommand = effectCmd({
action: "deny",
pattern: "*",
},
{
permission: "interactive_terminal", // kilocode_change - non-interactive runs cannot take over a terminal
action: "deny",
pattern: "*",
},
{
permission: "plan_enter",
action: "deny",
Expand Down
13 changes: 12 additions & 1 deletion packages/opencode/src/cli/cmd/run/footer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { createComponent, createSignal, type Accessor, type Setter } from "solid
import { createStore, reconcile } from "solid-js/store"
import { withRunSpan } from "./otel"
import { RUN_COMMAND_PANEL_ROWS, RUN_SUBAGENT_PANEL_ROWS } from "./footer.command"
import { RUN_INTERACTIVE_TERMINAL_ROWS } from "@/kilocode/cli/cmd/run/interactive-terminal" // kilocode_change
import { SUBAGENT_INSPECTOR_ROWS } from "./footer.subagent"
import { PROMPT_MAX_ROWS, TEXTAREA_MIN_ROWS } from "./footer.prompt"
import { printableBinding } from "./prompt.shared"
Expand Down Expand Up @@ -85,6 +86,9 @@ type RunFooterOptions = {
onPermissionReply: (input: PermissionReply) => void | Promise<void>
onQuestionReply: (input: QuestionReply) => void | Promise<void>
onQuestionReject: (input: QuestionReject) => void | Promise<void>
onTerminalWrite: (input: { terminalID: string; data: string }) => Promise<void> // kilocode_change
onTerminalResize: (input: { terminalID: string; cols: number; rows: number }) => Promise<void> // kilocode_change
onTerminalClose: (terminalID: string) => Promise<void> // kilocode_change
onCycleVariant?: () => CycleResult | void
onModelSelect?: (model: NonNullable<RunInput["model"]>) => CycleResult | void | Promise<CycleResult | void>
onVariantSelect?: (variant: string | undefined) => CycleResult | void | Promise<CycleResult | void>
Expand Down Expand Up @@ -283,6 +287,11 @@ export class RunFooter implements FooterApi {
onPermissionReply: this.handlePermissionReply,
onQuestionReply: this.handleQuestionReply,
onQuestionReject: this.handleQuestionReject,
// kilocode_change start
onTerminalWrite: options.onTerminalWrite,
onTerminalResize: options.onTerminalResize,
onTerminalClose: options.onTerminalClose,
// kilocode_change end
onCycle: this.handleCycle,
onInterrupt: this.handleInterrupt,
onInputClear: this.handleInputClear,
Expand Down Expand Up @@ -561,7 +570,9 @@ export class RunFooter implements FooterApi {
? this.base + PERMISSION_ROWS
: type === "question"
? this.base + QUESTION_ROWS
: this.promptRoute.type === "command"
: type === "interactive_terminal" // kilocode_change
? this.base + RUN_INTERACTIVE_TERMINAL_ROWS
: this.promptRoute.type === "command"
? 1 + COMMAND_ROWS
: this.promptRoute.type === "model"
? 1 + MODEL_ROWS
Expand Down
21 changes: 21 additions & 0 deletions packages/opencode/src/cli/cmd/run/footer.view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useKeyboard, useTerminalDimensions } from "@opentui/solid"
import { Match, Show, Switch, createEffect, createMemo, createSignal, onCleanup } from "solid-js"
import "opentui-spinner/solid"
import { createColors, createFrames } from "../tui/ui/spinner"
import { RunInteractiveTerminalBody } from "@/kilocode/cli/cmd/run/interactive-terminal" // kilocode_change
import {
RUN_SUBAGENT_PANEL_ROWS,
RunCommandMenuBody,
Expand Down Expand Up @@ -82,6 +83,9 @@ type RunFooterViewProps = {
onPermissionReply: (input: PermissionReply) => void | Promise<void>
onQuestionReply: (input: QuestionReply) => void | Promise<void>
onQuestionReject: (input: QuestionReject) => void | Promise<void>
onTerminalWrite: (input: { terminalID: string; data: string }) => Promise<void> // kilocode_change
onTerminalResize: (input: { terminalID: string; cols: number; rows: number }) => Promise<void> // kilocode_change
onTerminalClose: (terminalID: string) => Promise<void> // kilocode_change
onCycle: () => void
onInterrupt: () => boolean
onInputClear: () => void
Expand Down Expand Up @@ -187,6 +191,12 @@ export function RunFooterView(props: RunFooterViewProps) {
const view = active()
return view.type === "question" ? view : undefined
})
// kilocode_change start
const terminal = createMemo<Extract<FooterView, { type: "interactive_terminal" }> | undefined>(() => {
const view = active()
return view.type === "interactive_terminal" ? view : undefined
})
// kilocode_change end
const promptView = createMemo(() => {
if (active().type !== "prompt") {
return active().type
Expand Down Expand Up @@ -491,6 +501,17 @@ export function RunFooterView(props: RunFooterViewProps) {
onReject={props.onQuestionReject}
/>
</Match>
{/* kilocode_change start */}
<Match when={active().type === "interactive_terminal"}>
<RunInteractiveTerminalBody
terminal={() => terminal()!.terminal}
theme={theme()}
onWrite={props.onTerminalWrite}
onResize={props.onTerminalResize}
onClose={props.onTerminalClose}
/>
</Match>
{/* kilocode_change end */}
</Switch>
</box>

Expand Down
10 changes: 9 additions & 1 deletion packages/opencode/src/cli/cmd/run/runtime.lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ export type LifecycleInput = {
onPermissionReply: (input: PermissionReply) => void | Promise<void>
onQuestionReply: (input: QuestionReply) => void | Promise<void>
onQuestionReject: (input: QuestionReject) => void | Promise<void>
onTerminalWrite: (input: { terminalID: string; data: string }) => Promise<void> // kilocode_change
onTerminalResize: (input: { terminalID: string; cols: number; rows: number }) => Promise<void> // kilocode_change
onTerminalClose: (terminalID: string) => Promise<void> // kilocode_change
onCycleVariant?: () => CycleResult | void
onModelSelect?: (model: NonNullable<RunInput["model"]>) => CycleResult | void | Promise<CycleResult | void>
onVariantSelect?: (variant: string | undefined) => CycleResult | void | Promise<CycleResult | void>
Expand Down Expand Up @@ -175,7 +178,7 @@ export async function createRuntimeLifecycle(input: LifecycleInput): Promise<Lif
stdin: source.stdin,
targetFps: 30,
maxFps: 60,
useMouse: false,
useMouse: true, // kilocode_change - interactive terminal close and scroll controls

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[WARNING]: useMouse: true is now enabled for every kilo run interactive session, not only while the terminal dialog is open.

This flips mouse capture on globally for the run renderer (previously false). When the interactive terminal isn't active, mouse events will still be captured by OpenTUI, which can regress native terminal mouse behaviors in the rest of the run footer (text selection, scrollback wheel scrolling). Consider enabling mouse only while the terminal view is mounted, or scope the flag to terminal-active state, so non-terminal run sessions keep the previous no-mouse-capture behavior.


Reply with @kilocode-bot fix it to have Kilo Code address this issue.

autoFocus: false,
openConsoleOnError: false,
exitOnCtrlC: false,
Expand Down Expand Up @@ -235,6 +238,11 @@ export async function createRuntimeLifecycle(input: LifecycleInput): Promise<Lif
onPermissionReply: input.onPermissionReply,
onQuestionReply: input.onQuestionReply,
onQuestionReject: input.onQuestionReject,
// kilocode_change start
onTerminalWrite: input.onTerminalWrite,
onTerminalResize: input.onTerminalResize,
onTerminalClose: input.onTerminalClose,
// kilocode_change end
onCycleVariant: input.onCycleVariant,
onModelSelect: input.onModelSelect,
onVariantSelect: input.onVariantSelect,
Expand Down
17 changes: 17 additions & 0 deletions packages/opencode/src/cli/cmd/run/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,23 @@ async function runInteractiveRuntime(input: RunRuntimeInput): Promise<void> {

await ctx.sdk.question.reject(next)
},
// kilocode_change start - human-driven terminal in direct interactive mode
onTerminalWrite: async (next) => {
await ctx.sdk.interactiveTerminal.write({
terminalID: next.terminalID,
interactiveTerminalWriteInput: { data: next.data },
})
},
onTerminalResize: async (next) => {
await ctx.sdk.interactiveTerminal.resize({
terminalID: next.terminalID,
interactiveTerminalResizeInput: { cols: next.cols, rows: next.rows },
})
},
onTerminalClose: async (terminalID) => {
await ctx.sdk.interactiveTerminal.close({ terminalID })
},
// kilocode_change end
onCycleVariant: () => {
if (!state.model || state.variants.length === 0) {
return {
Expand Down
60 changes: 59 additions & 1 deletion packages/opencode/src/cli/cmd/run/session-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
// event arrives, the queue entry is removed and the footer falls back
// to the next pending request or to the prompt view.
import type { Part, PermissionRequest, QuestionRequest, ToolPart } from "@kilocode/sdk/v2"
import type { RunInteractiveTerminalSnapshot } from "@/kilocode/cli/cmd/run/types" // kilocode_change
import type { Event } from "./event"
import * as Locale from "@/util/locale"
import { appendTerminalOutput } from "@/kilocode/interactive-terminal/output" // kilocode_change
import { toolView } from "./tool"
import type { FooterOutput, FooterPatch, FooterView, StreamCommit } from "./types"

Expand Down Expand Up @@ -78,6 +80,7 @@ export type SessionData = {
shell: Map<string, ShellCall>
permissions: PermissionRequest[]
questions: QuestionRequest[]
terminal?: RunInteractiveTerminalSnapshot // kilocode_change
role: Map<string, MessageRole>
msg: Map<string, string>
part: Map<string, PartKind>
Expand Down Expand Up @@ -214,7 +217,17 @@ function out(data: SessionData, commits: SessionCommit[], footer?: FooterOutput)
}
}

export function pickBlockerView(input: { permission?: PermissionRequest; question?: QuestionRequest }): FooterView {
export function pickBlockerView(input: {
permission?: PermissionRequest
question?: QuestionRequest
terminal?: RunInteractiveTerminalSnapshot // kilocode_change
}): FooterView {
// kilocode_change start
if (input.terminal) {
return { type: "interactive_terminal", terminal: input.terminal }
}
// kilocode_change end

if (input.permission) {
return { type: "permission", request: input.permission }
}
Expand All @@ -227,6 +240,12 @@ export function pickBlockerView(input: { permission?: PermissionRequest; questio
}

export function blockerStatus(view: FooterView) {
// kilocode_change start
if (view.type === "interactive_terminal") {
return "interactive terminal"
}
// kilocode_change end

if (view.type === "permission") {
return "awaiting permission"
}
Expand All @@ -240,6 +259,7 @@ export function blockerStatus(view: FooterView) {

function pickSessionView(data: SessionData): FooterView {
return pickBlockerView({
terminal: data.terminal, // kilocode_change
permission: data.permissions[0],
question: data.questions[0],
})
Expand Down Expand Up @@ -1049,6 +1069,44 @@ export function reduceSessionData(input: SessionDataInput): SessionDataOutput {
return out(data, commits)
}

// kilocode_change start - direct interactive mode terminal footer
if (event.type === "interactive_terminal.updated") {
if (event.properties.info.sessionID !== input.sessionID) {
return out(data, commits)
}

const current = data.terminal
data.terminal = {
info: event.properties.info,
output: current?.info.id === event.properties.info.id ? current.output : "",
cursor: current?.info.id === event.properties.info.id ? current.cursor : 0,
}
return queueOut(data, commits)
}

if (event.type === "interactive_terminal.data") {
if (event.properties.sessionID !== input.sessionID || data.terminal?.info.id !== event.properties.terminalID) {
return out(data, commits)
}

data.terminal = {
...data.terminal,
output: appendTerminalOutput(data.terminal.output, event.properties.data),
cursor: event.properties.cursor,
}
return queueOut(data, commits)
}

if (event.type === "interactive_terminal.deleted") {
if (event.properties.sessionID !== input.sessionID || data.terminal?.info.id !== event.properties.terminalID) {
return out(data, commits)
}

data.terminal = undefined
return queueOut(data, commits)
}
// kilocode_change end

if (event.type === "permission.asked") {
if (event.properties.sessionID !== input.sessionID) {
return out(data, commits)
Expand Down
12 changes: 12 additions & 0 deletions packages/opencode/src/cli/cmd/run/stream.transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ function sid(event: Event): string | undefined {
return event.properties.part.sessionID
}

if (event.type === "interactive_terminal.updated") return event.properties.info.sessionID // kilocode_change

if (
event.type === "session.next.shell.started" ||
event.type === "session.next.shell.ended" ||
Expand All @@ -142,6 +144,8 @@ function sid(event: Event): string | undefined {
event.type === "question.asked" ||
event.type === "question.replied" ||
event.type === "question.rejected" ||
event.type === "interactive_terminal.data" || // kilocode_change
event.type === "interactive_terminal.deleted" || // kilocode_change
event.type === "session.error" ||
event.type === "session.status"
) {
Expand Down Expand Up @@ -268,6 +272,13 @@ function sameView(a: FooterView, b: FooterView) {
return false
}

// kilocode_change start
if (a.type === "interactive_terminal" && b.type === "interactive_terminal") {
return a.terminal === b.terminal
}

if (a.type === "interactive_terminal" || b.type === "interactive_terminal") return false
// kilocode_change end
return a.request === b.request
}

Expand All @@ -288,6 +299,7 @@ function firstByOrder<T extends { id: string }>(left: T[], right: T[], order: Ma

function pickView(data: SessionData, subagent: SubagentData, order: Map<string, number>): FooterView {
return pickBlockerView({
terminal: data.terminal, // kilocode_change
permission: firstByOrder(data.permissions, listSubagentPermissions(subagent), order),
question: firstByOrder(data.questions, listSubagentQuestions(subagent), order),
})
Expand Down
42 changes: 42 additions & 0 deletions packages/opencode/src/cli/cmd/run/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type { GrepTool } from "@/tool/grep"
import type { InvalidTool } from "@/tool/invalid"
import type { LspTool } from "@/tool/lsp"
import type { PlanExitTool } from "@/tool/plan"
import type { InteractiveTerminalTool } from "@/kilocode/tool/interactive-terminal" // kilocode_change
import type { QuestionTool } from "@/tool/question"
import type { ReadTool } from "@/tool/read"
import type { SkillTool } from "@/tool/skill"
Expand Down Expand Up @@ -101,6 +102,7 @@ type ToolDefs = {
task: typeof TaskTool
todowrite: typeof TodoWriteTool
question: typeof QuestionTool
interactive_terminal: typeof InteractiveTerminalTool // kilocode_change
read: typeof ReadTool
glob: typeof GlobTool
grep: typeof GrepTool
Expand Down Expand Up @@ -423,6 +425,33 @@ function runQuestion(p: ToolProps<typeof QuestionTool>): ToolInline {
}
}

// kilocode_change start
function runInteractiveTerminal(p: ToolProps<typeof InteractiveTerminalTool>): ToolInline {
const command = p.input.command ?? ""
const description = p.input.description || command || "Interactive terminal"
return {
icon: "$",
title: description,
description: command && command !== description ? `$ ${command}` : undefined,
}
}

function scrollInteractiveTerminalStart(p: ToolProps<typeof InteractiveTerminalTool>) {
const command = p.input.command ?? ""
const description = p.input.description || command || "Interactive terminal"
return command && command !== description
? `# Interactive terminal: ${description}\n$ ${command}`
: `# ${description}`
}

function scrollInteractiveTerminalFinal(p: ToolProps<typeof InteractiveTerminalTool>) {
if (p.metadata.closedBy === "user") return "interactive terminal closed by user"
if (p.metadata.closedBy === "abort") return "interactive terminal cancelled"
const code = p.metadata.exitCode
return typeof code === "number" ? `interactive terminal completed (exit ${code})` : "interactive terminal completed"
}
// kilocode_change end

function runInvalid(p: ToolProps<typeof InvalidTool>): ToolInline {
return {
icon: "✗",
Expand Down Expand Up @@ -1134,6 +1163,19 @@ const TOOL_RULES = {
final: scrollQuestionFinal,
},
},
// kilocode_change start
interactive_terminal: {
view: {
output: false,
final: true,
},
run: runInteractiveTerminal,
scroll: {
start: scrollInteractiveTerminalStart,
final: scrollInteractiveTerminalFinal,
},
},
// kilocode_change end
read: {
view: {
output: false,
Expand Down
Loading
Loading