Skip to content

Commit dbd9b18

Browse files
authored
refactor(cli): move import and export under session (#48055)
1 parent b43e1c6 commit dbd9b18

5 files changed

Lines changed: 52 additions & 40 deletions

File tree

‎packages/cli/src/commands/commands.ts‎

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -268,28 +268,6 @@ const Root = Spec.make(typeof OPENCODE_CLI_NAME === "string" ? OPENCODE_CLI_NAME
268268
json: Flag.boolean("json").pipe(Flag.withDescription("Output statistics as JSON"), Flag.withDefault(false)),
269269
},
270270
}),
271-
Spec.make("export", {
272-
description: "Export session data as JSON",
273-
params: {
274-
...ServerParams,
275-
session: Argument.string("session").pipe(Argument.withDescription("Session ID to export"), Argument.optional),
276-
sanitize: Flag.boolean("sanitize").pipe(
277-
Flag.withDescription("Redact sensitive transcript and file data"),
278-
Flag.withDefault(false),
279-
),
280-
},
281-
}),
282-
Spec.make("import", {
283-
description: "Import session data from a JSON file or URL",
284-
params: {
285-
...ServerParams,
286-
file: Argument.string("file").pipe(Argument.withDescription("JSON file or URL to import")),
287-
directory: Flag.string("directory").pipe(
288-
Flag.withDescription("Directory in which to import the session"),
289-
Flag.optional,
290-
),
291-
},
292-
}),
293271
Spec.make("mini", {
294272
description: "Start the minimal interactive interface",
295273
params: {
@@ -394,6 +372,31 @@ const Root = Spec.make(typeof OPENCODE_CLI_NAME === "string" ? OPENCODE_CLI_NAME
394372
sessionID: Argument.string("sessionID").pipe(Argument.withDescription("Session ID to delete")),
395373
},
396374
}),
375+
Spec.make("export", {
376+
description: "Export session data as JSON",
377+
params: {
378+
...ServerParams,
379+
session: Argument.string("session").pipe(
380+
Argument.withDescription("Session ID to export"),
381+
Argument.optional,
382+
),
383+
sanitize: Flag.boolean("sanitize").pipe(
384+
Flag.withDescription("Redact sensitive transcript and file data"),
385+
Flag.withDefault(false),
386+
),
387+
},
388+
}),
389+
Spec.make("import", {
390+
description: "Import session data from a JSON file or URL",
391+
params: {
392+
...ServerParams,
393+
file: Argument.string("file").pipe(Argument.withDescription("JSON file or URL to import")),
394+
directory: Flag.string("directory").pipe(
395+
Flag.withDescription("Directory in which to import the session"),
396+
Flag.optional,
397+
),
398+
},
399+
}),
397400
],
398401
}),
399402
Spec.make("service", {

packages/cli/src/commands/handlers/export.ts renamed to packages/cli/src/commands/handlers/session/export.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { OpenCode } from "@opencode/client"
33
import { Service } from "@opencode/client/effect/service"
44
import { Effect, Option } from "effect"
55
import { EOL } from "node:os"
6-
import { Commands } from "../commands"
7-
import { Runtime } from "../../framework/runtime"
8-
import { ServerConnection } from "../../services/server-connection"
9-
import { errorMessage } from "../../util/error"
6+
import { Commands } from "../../commands"
7+
import { Runtime } from "../../../framework/runtime"
8+
import { ServerConnection } from "../../../services/server-connection"
9+
import { errorMessage } from "../../../util/error"
1010

1111
export default Runtime.handler(
12-
Commands.commands.export,
13-
Effect.fn("cli.export")((input) =>
12+
Commands.commands.session.commands.export,
13+
Effect.fn("cli.session.export")((input) =>
1414
Effect.gen(function* () {
1515
const requested = Option.getOrUndefined(input.session)
1616
if (!requested && !process.stdin.isTTY) {

packages/cli/src/commands/handlers/import.ts renamed to packages/cli/src/commands/handlers/session/import.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import { SessionTransfer } from "@opencode/schema/session-transfer"
55
import { Effect, Option, Schema } from "effect"
66
import { EOL } from "node:os"
77
import path from "node:path"
8-
import { Commands } from "../commands"
9-
import { Runtime } from "../../framework/runtime"
10-
import { ServerConnection } from "../../services/server-connection"
8+
import { Commands } from "../../commands"
9+
import { Runtime } from "../../../framework/runtime"
10+
import { ServerConnection } from "../../../services/server-connection"
1111

1212
export default Runtime.handler(
13-
Commands.commands.import,
14-
Effect.fn("cli.import")(function* (input) {
13+
Commands.commands.session.commands.import,
14+
Effect.fn("cli.session.import")(function* (input) {
1515
const text = yield* Effect.tryPromise({
1616
try: () =>
1717
input.file.startsWith("http://") || input.file.startsWith("https://")

‎packages/cli/src/index.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ const Handlers = Runtime.handlers(Commands, {
5151
},
5252
models: () => import("./commands/handlers/models"),
5353
stats: () => import("./commands/handlers/stats"),
54-
export: () => import("./commands/handlers/export"),
55-
import: () => import("./commands/handlers/import"),
5654
mini: () => import("./commands/handlers/mini"),
5755
run: () => import("./commands/handlers/run"),
5856
pair: () => import("./commands/handlers/pair"),
5957
session: {
6058
list: () => import("./commands/handlers/session/list"),
6159
delete: () => import("./commands/handlers/session/delete"),
60+
export: () => import("./commands/handlers/session/export"),
61+
import: () => import("./commands/handlers/session/import"),
6262
},
6363
service: {
6464
start: () => import("./commands/handlers/service/start"),

‎packages/cli/test/import-export.test.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,14 @@ test("export is raw by default and supports explicit sanitization", async () =>
7171
})
7272

7373
try {
74-
const [stdout, , exitCode] = await run(["export", info.id, "--server", server.url.toString()])
74+
const [stdout, , exitCode] = await run(["session", "export", info.id, "--server", server.url.toString()])
7575
const exported = JSON.parse(stdout)
7676

7777
expect(exitCode).toBe(0)
7878
expect(exported).toEqual(transfer)
7979

8080
const [sanitized, , sanitizedExitCode] = await run([
81+
"session",
8182
"export",
8283
info.id,
8384
"--sanitize",
@@ -110,7 +111,7 @@ test("export requires a session outside an interactive terminal", async () => {
110111
})
111112

112113
try {
113-
const [stdout, stderr, exitCode] = await run(["export", "--server", server.url.toString()])
114+
const [stdout, stderr, exitCode] = await run(["session", "export", "--server", server.url.toString()])
114115

115116
expect(exitCode).toBe(1)
116117
expect(stdout).toBe("")
@@ -138,7 +139,7 @@ test("export reports a missing session without a stack trace", async () => {
138139
})
139140

140141
try {
141-
const [stdout, stderr, exitCode] = await run(["export", sessionID, "--server", server.url.toString()])
142+
const [stdout, stderr, exitCode] = await run(["session", "export", sessionID, "--server", server.url.toString()])
142143

143144
expect(exitCode).toBe(1)
144145
expect(stdout).toBe("")
@@ -173,7 +174,15 @@ test("import validates a file and sends it to the resolved location", async () =
173174
})
174175

175176
try {
176-
const [stdout, , exitCode] = await run(["import", file, "--directory", root, "--server", server.url.toString()])
177+
const [stdout, , exitCode] = await run([
178+
"session",
179+
"import",
180+
file,
181+
"--directory",
182+
root,
183+
"--server",
184+
server.url.toString(),
185+
])
177186

178187
expect(exitCode).toBe(0)
179188
expect(stdout).toBe(`Imported session: ${info.id}${os.EOL}`)
@@ -205,7 +214,7 @@ test("import reports an existing session without a stack trace", async () => {
205214
})
206215

207216
try {
208-
const [stdout, stderr, exitCode] = await run(["import", file, "--server", server.url.toString()])
217+
const [stdout, stderr, exitCode] = await run(["session", "import", file, "--server", server.url.toString()])
209218

210219
expect(exitCode).toBe(0)
211220
expect(stdout).toBe("")

0 commit comments

Comments
 (0)