Skip to content

Commit e66d0ce

Browse files
fix(tui): enter new session directly (#12)
1 parent 700f120 commit e66d0ce

7 files changed

Lines changed: 68 additions & 38 deletions

File tree

‎.changeset/direct-session-entry.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"opencode": patch
3+
---
4+
5+
Open Redcode directly in a new full TUI session and use Redcode branding in terminal titles.

‎packages/opencode/src/cli/cmd/tui.ts‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { errorMessage } from "@opencode-ai/tui/util/error"
88
import { withTimeout } from "@/util/timeout"
99
import { withNetworkOptions, resolveNetworkOptionsNoConfig, hasArg } from "@/cli/network"
1010
import { Filesystem } from "@/util/filesystem"
11-
import type { GlobalEvent } from "@opencode-ai/sdk/v2"
11+
import { createOpencodeClient, type GlobalEvent } from "@opencode-ai/sdk/v2"
1212
import type { EventSource } from "@opencode-ai/tui/context/sdk"
1313
import { writeHeapSnapshot } from "v8"
1414
import { ServerAuth } from "@/server/auth"
@@ -69,6 +69,17 @@ export function resolveThreadDirectory(project?: string, envPWD = process.env.PW
6969
return Filesystem.resolve(cwd)
7070
}
7171

72+
export async function resolveStartupSession(input: {
73+
sessionID?: string
74+
continue?: boolean
75+
prompt?: string
76+
create: () => Promise<string>
77+
}) {
78+
if (input.sessionID) return input.sessionID
79+
if (input.continue || input.prompt) return
80+
return input.create()
81+
}
82+
7283
export const TuiThreadCommand = cmd({
7384
command: "$0 [project]",
7485
describe: "start opencode tui",
@@ -248,10 +259,26 @@ export const TuiThreadCommand = cmd({
248259
events: createEventSource(client),
249260
}
250261

262+
const sessionID = await resolveStartupSession({
263+
sessionID: args.session,
264+
continue: args.continue,
265+
prompt,
266+
create: async () => {
267+
const result = await createOpencodeClient({
268+
baseUrl: transport.url,
269+
directory: cwd,
270+
fetch: transport.fetch,
271+
headers: transport.headers,
272+
}).session.create({ directory: cwd }, { throwOnError: true })
273+
if (!result.data?.id) throw new Error("Failed to create startup session")
274+
return result.data.id
275+
},
276+
})
277+
251278
try {
252279
await validateSession({
253280
url: transport.url,
254-
sessionID: args.session,
281+
sessionID,
255282
directory: cwd,
256283
fetch: transport.fetch,
257284
headers,
@@ -286,7 +313,7 @@ export const TuiThreadCommand = cmd({
286313
events: transport.events,
287314
args: {
288315
continue: args.continue,
289-
sessionID: args.session,
316+
sessionID,
290317
agent: args.agent,
291318
model: args.model,
292319
prompt,

‎packages/opencode/test/cli/tui/thread.test.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import fs from "fs/promises"
44
import path from "path"
55
import yargs from "yargs"
66
import { tmpdir } from "../../fixture/fixture"
7-
import { TuiThreadCommand, resolveThreadDirectory } from "../../../src/cli/cmd/tui"
7+
import { resolveStartupSession, TuiThreadCommand, resolveThreadDirectory } from "../../../src/cli/cmd/tui"
88
import { cliIt } from "../../lib/cli-process"
99

1010
describe("tui thread", () => {
@@ -51,6 +51,29 @@ describe("tui thread", () => {
5151
expect(resolveThreadDirectory(undefined, pwd.path, cwd.path)).toBe(cwd.path)
5252
})
5353

54+
test("creates a session before opening the default TUI route", async () => {
55+
const calls: string[] = []
56+
const sessionID = await resolveStartupSession({
57+
create: async () => {
58+
calls.push("create")
59+
return "ses_direct"
60+
},
61+
})
62+
63+
expect(sessionID).toBe("ses_direct")
64+
expect(calls).toEqual(["create"])
65+
})
66+
67+
test("preserves explicit startup routing", async () => {
68+
const create = async () => {
69+
throw new Error("should not create a session")
70+
}
71+
72+
expect(await resolveStartupSession({ sessionID: "ses_existing", create })).toBe("ses_existing")
73+
expect(await resolveStartupSession({ continue: true, create })).toBeUndefined()
74+
expect(await resolveStartupSession({ prompt: "fix it", create })).toBeUndefined()
75+
})
76+
5477
test("parses supported --no-replay forms", async () => {
5578
for (const option of ["--no-replay", "--no-replay=true", "--noReplay"]) {
5679
const args = await yargs([])

‎packages/tui/src/app.tsx‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -465,24 +465,24 @@ function App(props: { onSnapshot?: () => Promise<string[]>; pluginHost: TuiPlugi
465465
if (!terminalTitleEnabled() || Flag.OPENCODE_DISABLE_TERMINAL_TITLE) return
466466

467467
if (route.data.type === "home") {
468-
renderer.setTerminalTitle("OpenCode")
468+
renderer.setTerminalTitle("Redcode")
469469
return
470470
}
471471

472472
if (route.data.type === "session") {
473473
const session = sync.session.get(route.data.sessionID)
474474
if (!session || isDefaultTitle(session.title)) {
475-
renderer.setTerminalTitle("OpenCode")
475+
renderer.setTerminalTitle("Redcode")
476476
return
477477
}
478478

479479
const title = session.title.length > 40 ? session.title.slice(0, 37) + "..." : session.title
480-
renderer.setTerminalTitle(`OC | ${title}`)
480+
renderer.setTerminalTitle(`Redcode | ${title}`)
481481
return
482482
}
483483

484484
if (route.data.type === "plugin") {
485-
renderer.setTerminalTitle(`OC | ${route.data.id}`)
485+
renderer.setTerminalTitle(`Redcode | ${route.data.id}`)
486486
}
487487
})
488488

@@ -1126,7 +1126,7 @@ function App(props: { onSnapshot?: () => Promise<string[]>; pluginHost: TuiPlugi
11261126
await DialogAlert.show(
11271127
dialog,
11281128
"Update Complete",
1129-
`Successfully updated to OpenCode v${result.data.version}. Please restart the application.`,
1129+
`Successfully updated to Redcode v${result.data.version}. Please restart the application.`,
11301130
)
11311131

11321132
void exit()
Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,12 @@
1-
const logo = {
2-
left: [" ", "█▀▀█ █▀▀█ █▀▀█ █▀▀▄", "█__█ █__█ █^^^ █__█", "▀▀▀▀ █▀▀▀ ▀▀▀▀ ▀~~▀"],
3-
right: [" ▄ ", "█▀▀▀ █▀▀█ █▀▀█ █▀▀█", "█___ █__█ █__█ █^^^", "▀▀▀▀ ▀▀▀▀ ▀▀▀▀ ▀▀▀▀"],
4-
}
5-
61
const reset = "\x1b[0m"
72
const bold = "\x1b[1m"
83
const dim = "\x1b[90m"
94

10-
function wordmark(pad = "") {
11-
const draw = (line: string, fg: string, shadow: string, bg: string) =>
12-
[...line]
13-
.map((char) => {
14-
if (char === "_") return `${bg} ${reset}`
15-
if (char === "^") return `${fg}${bg}▀${reset}`
16-
if (char === "~") return `${shadow}▀${reset}`
17-
if (char === " ") return " "
18-
return `${fg}${char}${reset}`
19-
})
20-
.join("")
21-
22-
return logo.left.map((line, index) => {
23-
const left = draw(line, dim, "\x1b[38;5;235m", "\x1b[48;5;235m")
24-
const right = draw(logo.right[index] ?? "", reset, "\x1b[38;5;238m", "\x1b[48;5;238m")
25-
return `${pad}${left} ${right}`
26-
})
27-
}
28-
295
export function sessionEpilogue(input: { title: string; sessionID?: string }) {
306
const weak = (text: string) => `${dim}${text.padEnd(10, " ")}${reset}`
317
return [
32-
...wordmark(" "),
33-
"",
348
` ${weak("Session")}${bold}${input.title}${reset}`,
35-
` ${weak("Continue")}${bold}opencode -s ${input.sessionID}${reset}`,
9+
` ${weak("Continue")}${bold}redcode -s ${input.sessionID}${reset}`,
3610
"",
3711
].join("\n")
3812
}

‎packages/tui/test/app-lifecycle.test.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ test("app.exit prints the session epilogue after scoped cleanup", async () => {
119119
await task
120120

121121
expect(stdout).toContain("Demo session")
122-
expect(stdout).toContain("opencode -s dummy")
122+
expect(stdout).toContain("redcode -s dummy")
123123
} finally {
124124
process.stdout.write = originalWrite
125125
if (!setup.renderer.isDestroyed) setup.renderer.destroy()

‎packages/tui/test/util/presentation.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ import { sessionEpilogue } from "../../src/util/presentation"
44
test("formats session continuation summary", () => {
55
const epilogue = sessionEpilogue({ title: "A session", sessionID: "ses_123" })
66
expect(epilogue).toContain("A session")
7-
expect(epilogue).toContain("opencode -s ses_123")
7+
expect(epilogue).toContain("redcode -s ses_123")
8+
expect(epilogue).not.toContain("█▀▀█")
89
})

0 commit comments

Comments
 (0)