import.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import type { Argv } from "yargs"
  2. import { Session } from "../../session"
  3. import { cmd } from "./cmd"
  4. import { bootstrap } from "../bootstrap"
  5. import { Storage } from "../../storage/storage"
  6. import { Instance } from "../../project/instance"
  7. import { EOL } from "os"
  8. export const ImportCommand = cmd({
  9. command: "import <file>",
  10. describe: "import session data from JSON file or URL",
  11. builder: (yargs: Argv) => {
  12. return yargs.positional("file", {
  13. describe: "path to JSON file or opencode.ai share URL",
  14. type: "string",
  15. demandOption: true,
  16. })
  17. },
  18. handler: async (args) => {
  19. await bootstrap(process.cwd(), async () => {
  20. let exportData: {
  21. info: Session.Info
  22. messages: Array<{
  23. info: any
  24. parts: any[]
  25. }>
  26. } | undefined
  27. const isUrl = args.file.startsWith("http://") || args.file.startsWith("https://")
  28. if (isUrl) {
  29. const urlMatch = args.file.match(/https?:\/\/opencode\.ai\/s\/([a-zA-Z0-9_-]+)/)
  30. if (!urlMatch) {
  31. process.stdout.write(`Invalid URL format. Expected: https://opencode.ai/s/<slug>`)
  32. process.stdout.write(EOL)
  33. return
  34. }
  35. const slug = urlMatch[1]
  36. const response = await fetch(`https://api.opencode.ai/share_data?id=${slug}`)
  37. if (!response.ok) {
  38. process.stdout.write(`Failed to fetch share data: ${response.statusText}`)
  39. process.stdout.write(EOL)
  40. return
  41. }
  42. const data = await response.json()
  43. if (!data.info || !data.messages || Object.keys(data.messages).length === 0) {
  44. process.stdout.write(`Share not found: ${slug}`)
  45. process.stdout.write(EOL)
  46. return
  47. }
  48. exportData = {
  49. info: data.info,
  50. messages: Object.values(data.messages).map((msg: any) => {
  51. const { parts, ...info } = msg
  52. return {
  53. info,
  54. parts,
  55. }
  56. }),
  57. }
  58. } else {
  59. const file = Bun.file(args.file)
  60. exportData = await file.json().catch(() => {})
  61. if (!exportData) {
  62. process.stdout.write(`File not found: ${args.file}`)
  63. process.stdout.write(EOL)
  64. return
  65. }
  66. }
  67. if (!exportData) {
  68. process.stdout.write(`Failed to read session data`)
  69. process.stdout.write(EOL)
  70. return
  71. }
  72. await Storage.write(["session", Instance.project.id, exportData.info.id], exportData.info)
  73. for (const msg of exportData.messages) {
  74. await Storage.write(["message", exportData.info.id, msg.info.id], msg.info)
  75. for (const part of msg.parts) {
  76. await Storage.write(["part", msg.info.id, part.id], part)
  77. }
  78. }
  79. process.stdout.write(`Imported session: ${exportData.info.id}`)
  80. process.stdout.write(EOL)
  81. })
  82. },
  83. })