import.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. | {
  22. info: Session.Info
  23. messages: Array<{
  24. info: any
  25. parts: any[]
  26. }>
  27. }
  28. | undefined
  29. const isUrl = args.file.startsWith("http://") || args.file.startsWith("https://")
  30. if (isUrl) {
  31. const urlMatch = args.file.match(/https?:\/\/opncd\.ai\/share\/([a-zA-Z0-9_-]+)/)
  32. if (!urlMatch) {
  33. process.stdout.write(`Invalid URL format. Expected: https://opncd.ai/share/<slug>`)
  34. process.stdout.write(EOL)
  35. return
  36. }
  37. const slug = urlMatch[1]
  38. const response = await fetch(`https://opncd.ai/api/share/${slug}`)
  39. if (!response.ok) {
  40. process.stdout.write(`Failed to fetch share data: ${response.statusText}`)
  41. process.stdout.write(EOL)
  42. return
  43. }
  44. const data = await response.json()
  45. if (!data.info || !data.messages || Object.keys(data.messages).length === 0) {
  46. process.stdout.write(`Share not found: ${slug}`)
  47. process.stdout.write(EOL)
  48. return
  49. }
  50. exportData = {
  51. info: data.info,
  52. messages: Object.values(data.messages).map((msg: any) => {
  53. const { parts, ...info } = msg
  54. return {
  55. info,
  56. parts,
  57. }
  58. }),
  59. }
  60. } else {
  61. const file = Bun.file(args.file)
  62. exportData = await file.json().catch(() => {})
  63. if (!exportData) {
  64. process.stdout.write(`File not found: ${args.file}`)
  65. process.stdout.write(EOL)
  66. return
  67. }
  68. }
  69. if (!exportData) {
  70. process.stdout.write(`Failed to read session data`)
  71. process.stdout.write(EOL)
  72. return
  73. }
  74. await Storage.write(["session", Instance.project.id, exportData.info.id], exportData.info)
  75. for (const msg of exportData.messages) {
  76. await Storage.write(["message", exportData.info.id, msg.info.id], msg.info)
  77. for (const part of msg.parts) {
  78. await Storage.write(["part", msg.info.id, part.id], part)
  79. }
  80. }
  81. process.stdout.write(`Imported session: ${exportData.info.id}`)
  82. process.stdout.write(EOL)
  83. })
  84. },
  85. })