diff options
| author | Aiden Cline <[email protected]> | 2025-11-06 11:55:57 -0600 |
|---|---|---|
| committer | Aiden Cline <[email protected]> | 2025-11-06 11:55:57 -0600 |
| commit | 8729edc5e059a9a29346044b016a6e05d9aca835 (patch) | |
| tree | c67bbfe7f28105b03355002c81e91ae8774b079c /packages | |
| parent | d8bcf1f5f3923f6130cd8be02b67a565da1dfa19 (diff) | |
| download | opencode-8729edc5e059a9a29346044b016a6e05d9aca835.tar.gz opencode-8729edc5e059a9a29346044b016a6e05d9aca835.zip | |
update import command to accept share links
Diffstat (limited to 'packages')
| -rw-r--r-- | packages/opencode/src/cli/cmd/import.ts | 57 |
1 files changed, 52 insertions, 5 deletions
diff --git a/packages/opencode/src/cli/cmd/import.ts b/packages/opencode/src/cli/cmd/import.ts index afebadc3d..ef7d243f5 100644 --- a/packages/opencode/src/cli/cmd/import.ts +++ b/packages/opencode/src/cli/cmd/import.ts @@ -8,26 +8,73 @@ import { EOL } from "os" export const ImportCommand = cmd({ command: "import <file>", - describe: "import session data from JSON file", + describe: "import session data from JSON file or URL", builder: (yargs: Argv) => { return yargs.positional("file", { - describe: "path to JSON file to import", + describe: "path to JSON file or opencode.ai share URL", type: "string", demandOption: true, }) }, handler: async (args) => { await bootstrap(process.cwd(), async () => { - const file = Bun.file(args.file) - const exportData = (await file.json().catch(() => {})) as { + let exportData: { info: Session.Info messages: Array<{ info: any parts: any[] }> + } | undefined + + const isUrl = args.file.startsWith("http://") || args.file.startsWith("https://") + + if (isUrl) { + const urlMatch = args.file.match(/https?:\/\/opencode\.ai\/s\/([a-zA-Z0-9_-]+)/) + if (!urlMatch) { + process.stdout.write(`Invalid URL format. Expected: https://opencode.ai/s/<slug>`) + process.stdout.write(EOL) + return + } + + const slug = urlMatch[1] + const response = await fetch(`https://api.opencode.ai/share_data?id=${slug}`) + + if (!response.ok) { + process.stdout.write(`Failed to fetch share data: ${response.statusText}`) + process.stdout.write(EOL) + return + } + + const data = await response.json() + + if (!data.info || !data.messages || Object.keys(data.messages).length === 0) { + process.stdout.write(`Share not found: ${slug}`) + process.stdout.write(EOL) + return + } + + exportData = { + info: data.info, + messages: Object.values(data.messages).map((msg: any) => { + const { parts, ...info } = msg + return { + info, + parts, + } + }), + } + } else { + const file = Bun.file(args.file) + exportData = await file.json().catch(() => {}) + if (!exportData) { + process.stdout.write(`File not found: ${args.file}`) + process.stdout.write(EOL) + return + } } + if (!exportData) { - process.stdout.write(`File not found: ${args.file}`) + process.stdout.write(`Failed to read session data`) process.stdout.write(EOL) return } |
