summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDax Raad <[email protected]>2025-11-05 18:04:31 -0500
committerDax Raad <[email protected]>2025-11-05 18:05:01 -0500
commit306f45f04a3cb282e2d88082d9afe222eb397573 (patch)
treef274c6367714a3b86d1c8e343f35960318e67081
parente006e3355cff3de25e023edcee0b59985e7db66b (diff)
downloadopencode-306f45f04a3cb282e2d88082d9afe222eb397573.tar.gz
opencode-306f45f04a3cb282e2d88082d9afe222eb397573.zip
add opencode import command to restore sessions from JSON exports
-rw-r--r--packages/opencode/src/cli/cmd/import.ts51
-rw-r--r--packages/opencode/src/index.ts2
2 files changed, 53 insertions, 0 deletions
diff --git a/packages/opencode/src/cli/cmd/import.ts b/packages/opencode/src/cli/cmd/import.ts
new file mode 100644
index 000000000..eeb2e6885
--- /dev/null
+++ b/packages/opencode/src/cli/cmd/import.ts
@@ -0,0 +1,51 @@
+import type { Argv } from "yargs"
+import { Session } from "../../session"
+import { cmd } from "./cmd"
+import { bootstrap } from "../bootstrap"
+import { UI } from "../ui"
+import { Storage } from "../../storage/storage"
+import { Instance } from "../../project/instance"
+import { EOL } from "os"
+
+export const ImportCommand = cmd({
+ command: "import <file>",
+ describe: "import session data from JSON file",
+ builder: (yargs: Argv) => {
+ return yargs.positional("file", {
+ describe: "path to JSON file to import",
+ type: "string",
+ demandOption: true,
+ })
+ },
+ handler: async (args) => {
+ await bootstrap(process.cwd(), async () => {
+ const file = Bun.file(args.file as string)
+ const exists = await file.exists()
+ if (!exists) {
+ UI.error(`File not found: ${args.file}`)
+ process.exit(1)
+ }
+
+ const exportData = (await file.json()) as {
+ info: Session.Info
+ messages: Array<{
+ info: any
+ parts: any[]
+ }>
+ }
+
+ await Storage.write(["session", Instance.project.id, exportData.info.id], exportData.info)
+
+ for (const msg of exportData.messages) {
+ await Storage.write(["message", exportData.info.id, msg.info.id], msg.info)
+
+ for (const part of msg.parts) {
+ await Storage.write(["part", msg.info.id, part.id], part)
+ }
+ }
+
+ process.stdout.write(`Imported session: ${exportData.info.id}`)
+ process.stdout.write(EOL)
+ })
+ },
+})
diff --git a/packages/opencode/src/index.ts b/packages/opencode/src/index.ts
index 7fd7aeb10..015806997 100644
--- a/packages/opencode/src/index.ts
+++ b/packages/opencode/src/index.ts
@@ -17,6 +17,7 @@ import { StatsCommand } from "./cli/cmd/stats"
import { McpCommand } from "./cli/cmd/mcp"
import { GithubCommand } from "./cli/cmd/github"
import { ExportCommand } from "./cli/cmd/export"
+import { ImportCommand } from "./cli/cmd/import"
import { AttachCommand } from "./cli/cmd/tui/attach"
import { TuiThreadCommand } from "./cli/cmd/tui/thread"
import { TuiSpawnCommand } from "./cli/cmd/tui/spawn"
@@ -87,6 +88,7 @@ const cli = yargs(hideBin(process.argv))
.command(ModelsCommand)
.command(StatsCommand)
.command(ExportCommand)
+ .command(ImportCommand)
.command(GithubCommand)
.fail((msg) => {
if (