Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions packages/app/src/context/terminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type LocalPTY = {
buffer?: string
scrollY?: number
cursor?: number
source?: "user" | "agent"
}

const WORKSPACE_KEY = "__workspace__"
Expand Down Expand Up @@ -185,6 +186,19 @@ function createWorkspaceTerminalSession(sdk: ReturnType<typeof useSDK>, dir: str
})
onCleanup(unsub)

const unsubCreated = sdk.event.on("pty.created", (event: { properties: { info: LocalPTY } }) => {
const { info } = event.properties
if (store.all.findIndex((x) => x.id === info.id) >= 0) return
const newTerminal = {
id: info.id,
title: info.title,
titleNumber: info.titleNumber ?? numberFromTitle(info.title) ?? 0,
source: info.source,
}
setStore("all", store.all.length, newTerminal)
})
onCleanup(unsubCreated)

const update = (client: ReturnType<typeof useSDK>["client"], pty: Partial<LocalPTY> & { id: string }) => {
const index = store.all.findIndex((x) => x.id === pty.id)
const previous = index >= 0 ? store.all[index] : undefined
Expand Down
43 changes: 29 additions & 14 deletions packages/app/src/pages/session/terminal-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,39 @@ export function TerminalPanel() {
setStore("autoCreated", true)
})

// Auto-open terminal panel when agent creates a new PTY session
createEffect(
on(
() => terminal.all().length,
(count, prevCount) => {
if (prevCount === undefined || prevCount <= 0 || count !== 0) return
if (!opened()) return
close()
if (prevCount === undefined || count <= prevCount) return
const all = terminal.all()
const last = all[all.length - 1]
if (last?.source !== "agent") return
if (!opened()) view().terminal.open()
terminal.open(last.id)
},
),
)

// Close terminal panel when last terminal disappears
createEffect(
on(
() => terminal.all().length,
(count, prevCount) => {
if (prevCount === undefined || count !== 0 || prevCount === 0) return
if (opened()) view().terminal.close()
},
),
)

createEffect(
on(
() => [opened(), terminal.active()] as const,
([next, id]) => {
if (!next || !id) return
const stop = focus(id)
onCleanup(stop)
},
),
)
Expand Down Expand Up @@ -99,17 +125,6 @@ export function TerminalPanel() {
}
}

createEffect(
on(
() => [opened(), terminal.active()] as const,
([next, id]) => {
if (!next || !id) return
const stop = focus(id)
onCleanup(stop)
},
),
)

createEffect(() => {
if (opened()) return
const active = document.activeElement
Expand Down
1 change: 1 addition & 0 deletions packages/opencode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
},
"devDependencies": {
"@babel/core": "7.28.4",
"@babel/types": "7.29.0",
"@effect/language-service": "0.84.2",
"@octokit/webhooks-types": "7.6.1",
"@opencode-ai/script": "workspace:*",
Expand Down
3 changes: 3 additions & 0 deletions packages/opencode/src/pty/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const Info = Schema.Struct({
cwd: Schema.String,
status: Schema.Literals(["running", "exited"]),
pid: Schema.Number,
source: Schema.optional(Schema.Literals(["user", "agent"])),
})
.annotate({ identifier: "Pty" })
.pipe(withStatics((s) => ({ zod: zod(s) })))
Expand All @@ -74,6 +75,7 @@ export const CreateInput = Schema.Struct({
cwd: Schema.optional(Schema.String),
title: Schema.optional(Schema.String),
env: Schema.optional(Schema.Record(Schema.String, Schema.String)),
source: Schema.optional(Schema.Literals(["user", "agent"])),
}).pipe(withStatics((s) => ({ zod: zod(s) })))

export type CreateInput = Types.DeepMutable<Schema.Schema.Type<typeof CreateInput>>
Expand Down Expand Up @@ -215,6 +217,7 @@ export const layer = Layer.effect(
cwd,
status: "running",
pid: proc.pid,
source: input.source ?? ("user" as const),
} as const
const session: Active = {
info,
Expand Down
7 changes: 7 additions & 0 deletions packages/opencode/src/tool/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PlanExitTool } from "./plan"
import { Session } from "../session"
import { QuestionTool } from "./question"
import { BashTool } from "./bash"
import { TerminalTool } from "./terminal"
import { EditTool } from "./edit"
import { GlobTool } from "./glob"
import { GrepTool } from "./grep"
Expand Down Expand Up @@ -32,6 +33,7 @@ import { Glob } from "@opencode-ai/shared/util/glob"
import path from "path"
import { pathToFileURL } from "url"
import { Effect, Layer, Context } from "effect"
import { Pty } from "@/pty"
import { FetchHttpClient, HttpClient } from "effect/unstable/http"
import { ChildProcessSpawner } from "effect/unstable/process/ChildProcessSpawner"
import * as CrossSpawnSpawner from "@/effect/cross-spawn-spawner"
Expand Down Expand Up @@ -86,6 +88,7 @@ export const layer: Layer.Layer<
| Bus.Service
| HttpClient.HttpClient
| ChildProcessSpawner
| Pty.Service
| Ripgrep.Service
| Format.Service
| Truncate.Service
Expand All @@ -108,6 +111,7 @@ export const layer: Layer.Layer<
const webfetch = yield* WebFetchTool
const websearch = yield* WebSearchTool
const bash = yield* BashTool
const terminal = yield* TerminalTool
const codesearch = yield* CodeSearchTool
const globtool = yield* GlobTool
const writetool = yield* WriteTool
Expand Down Expand Up @@ -189,6 +193,7 @@ export const layer: Layer.Layer<
const tool = yield* Effect.all({
invalid: Tool.init(invalid),
bash: Tool.init(bash),
terminal: Tool.init(terminal),
read: Tool.init(read),
glob: Tool.init(globtool),
grep: Tool.init(greptool),
Expand All @@ -212,6 +217,7 @@ export const layer: Layer.Layer<
tool.invalid,
...(questionEnabled ? [tool.question] : []),
tool.bash,
tool.terminal,
tool.read,
tool.glob,
tool.grep,
Expand Down Expand Up @@ -344,6 +350,7 @@ export const defaultLayer = Layer.suspend(() =>
Layer.provide(Format.defaultLayer),
Layer.provide(CrossSpawnSpawner.defaultLayer),
Layer.provide(Ripgrep.defaultLayer),
Layer.provide(Pty.defaultLayer),
Layer.provide(Truncate.defaultLayer),
),
)
Loading
Loading