refactor(cli): convert import command to effectCmd (#25467)
This commit is contained in:
@@ -1,17 +1,15 @@
|
|||||||
import type { Argv } from "yargs"
|
|
||||||
import type { Session as SDKSession, Message, Part } from "@opencode-ai/sdk/v2"
|
import type { Session as SDKSession, Message, Part } from "@opencode-ai/sdk/v2"
|
||||||
import { Session } from "@/session/session"
|
import { Session } from "@/session/session"
|
||||||
import { MessageV2 } from "../../session/message-v2"
|
import { MessageV2 } from "../../session/message-v2"
|
||||||
import { cmd } from "./cmd"
|
import { CliError, effectCmd } from "../effect-cmd"
|
||||||
import { bootstrap } from "../bootstrap"
|
|
||||||
import { Database } from "@/storage/db"
|
import { Database } from "@/storage/db"
|
||||||
import { SessionTable, MessageTable, PartTable } from "../../session/session.sql"
|
import { SessionTable, MessageTable, PartTable } from "../../session/session.sql"
|
||||||
import { Instance } from "../../project/instance"
|
import { InstanceRef } from "@/effect/instance-ref"
|
||||||
|
import { InstanceStore } from "@/project/instance-store"
|
||||||
import { ShareNext } from "@/share/share-next"
|
import { ShareNext } from "@/share/share-next"
|
||||||
import { EOL } from "os"
|
import { EOL } from "os"
|
||||||
import { Filesystem } from "@/util/filesystem"
|
import { Filesystem } from "@/util/filesystem"
|
||||||
import { AppRuntime } from "@/effect/app-runtime"
|
import { Effect, Schema } from "effect"
|
||||||
import { Schema } from "effect"
|
|
||||||
|
|
||||||
const decodeMessageInfo = Schema.decodeUnknownSync(MessageV2.Info)
|
const decodeMessageInfo = Schema.decodeUnknownSync(MessageV2.Info)
|
||||||
const decodePart = Schema.decodeUnknownSync(MessageV2.Part)
|
const decodePart = Schema.decodeUnknownSync(MessageV2.Part)
|
||||||
@@ -78,53 +76,62 @@ export function transformShareData(shareData: ShareData[]): {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ImportCommand = cmd({
|
type ExportData = { info: SDKSession; messages: Array<{ info: Message; parts: Part[] }> }
|
||||||
|
|
||||||
|
export const ImportCommand = effectCmd({
|
||||||
command: "import <file>",
|
command: "import <file>",
|
||||||
describe: "import session data from JSON file or URL",
|
describe: "import session data from JSON file or URL",
|
||||||
builder: (yargs: Argv) => {
|
builder: (yargs) =>
|
||||||
return yargs.positional("file", {
|
yargs.positional("file", {
|
||||||
describe: "path to JSON file or share URL",
|
describe: "path to JSON file or share URL",
|
||||||
type: "string",
|
type: "string",
|
||||||
demandOption: true,
|
demandOption: true,
|
||||||
|
}),
|
||||||
|
handler: Effect.fn("Cli.import")(function* (args) {
|
||||||
|
// effectCmd always provides InstanceRef via InstanceStore.Service.provide; this is an invariant.
|
||||||
|
const ctx = yield* InstanceRef
|
||||||
|
if (!ctx) return yield* Effect.die("InstanceRef not provided")
|
||||||
|
const store = yield* InstanceStore.Service
|
||||||
|
// Ensure store.dispose runs disposers and emits server.instance.disposed
|
||||||
|
// on every exit path: success, early return, typed failure, defect, interrupt.
|
||||||
|
return yield* runImport(args.file, ctx.project.id).pipe(Effect.ensuring(store.dispose(ctx)))
|
||||||
|
}),
|
||||||
})
|
})
|
||||||
},
|
|
||||||
handler: async (args) => {
|
|
||||||
await bootstrap(process.cwd(), async () => {
|
|
||||||
let exportData:
|
|
||||||
| {
|
|
||||||
info: SDKSession
|
|
||||||
messages: Array<{
|
|
||||||
info: Message
|
|
||||||
parts: Part[]
|
|
||||||
}>
|
|
||||||
}
|
|
||||||
| undefined
|
|
||||||
|
|
||||||
const isUrl = args.file.startsWith("http://") || args.file.startsWith("https://")
|
const runImport = Effect.fn("Cli.import.body")(function* (file: string, projectID: string) {
|
||||||
|
const share = yield* ShareNext.Service
|
||||||
|
|
||||||
|
let exportData: ExportData | undefined
|
||||||
|
|
||||||
|
const isUrl = file.startsWith("http://") || file.startsWith("https://")
|
||||||
|
|
||||||
if (isUrl) {
|
if (isUrl) {
|
||||||
const slug = parseShareUrl(args.file)
|
const slug = parseShareUrl(file)
|
||||||
if (!slug) {
|
if (!slug) {
|
||||||
const baseUrl = await AppRuntime.runPromise(ShareNext.Service.use((svc) => svc.url()))
|
const baseUrl = yield* Effect.orDie(share.url())
|
||||||
process.stdout.write(`Invalid URL format. Expected: ${baseUrl}/share/<slug>`)
|
process.stdout.write(`Invalid URL format. Expected: ${baseUrl}/share/<slug>`)
|
||||||
process.stdout.write(EOL)
|
process.stdout.write(EOL)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const parsed = new URL(args.file)
|
const baseUrl = new URL(file).origin
|
||||||
const baseUrl = parsed.origin
|
const req = yield* Effect.orDie(share.request())
|
||||||
const req = await AppRuntime.runPromise(ShareNext.Service.use((svc) => svc.request()))
|
const headers = shouldAttachShareAuthHeaders(file, req.baseUrl) ? req.headers : {}
|
||||||
const headers = shouldAttachShareAuthHeaders(args.file, req.baseUrl) ? req.headers : {}
|
|
||||||
|
const tryFetch = (url: string) =>
|
||||||
|
Effect.tryPromise({
|
||||||
|
try: () => fetch(url, { headers }),
|
||||||
|
catch: (e) =>
|
||||||
|
new CliError({
|
||||||
|
message: `Failed to fetch share data: ${e instanceof Error ? e.message : String(e)}`,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
const dataPath = req.api.data(slug)
|
const dataPath = req.api.data(slug)
|
||||||
let response = await fetch(`${baseUrl}${dataPath}`, {
|
let response = yield* tryFetch(`${baseUrl}${dataPath}`)
|
||||||
headers,
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!response.ok && dataPath !== `/api/share/${slug}/data`) {
|
if (!response.ok && dataPath !== `/api/share/${slug}/data`) {
|
||||||
response = await fetch(`${baseUrl}/api/share/${slug}/data`, {
|
response = yield* tryFetch(`${baseUrl}/api/share/${slug}/data`)
|
||||||
headers,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
@@ -133,7 +140,10 @@ export const ImportCommand = cmd({
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const shareData: ShareData[] = await response.json()
|
const shareData = yield* Effect.tryPromise({
|
||||||
|
try: () => response.json() as Promise<ShareData[]>,
|
||||||
|
catch: () => new CliError({ message: "Share data was not valid JSON" }),
|
||||||
|
})
|
||||||
const transformed = transformShareData(shareData)
|
const transformed = transformShareData(shareData)
|
||||||
|
|
||||||
if (!transformed) {
|
if (!transformed) {
|
||||||
@@ -144,9 +154,11 @@ export const ImportCommand = cmd({
|
|||||||
|
|
||||||
exportData = transformed
|
exportData = transformed
|
||||||
} else {
|
} else {
|
||||||
exportData = await Filesystem.readJson<NonNullable<typeof exportData>>(args.file).catch(() => undefined)
|
exportData = yield* Effect.promise(() =>
|
||||||
|
Filesystem.readJson<NonNullable<typeof exportData>>(file).catch(() => undefined),
|
||||||
|
)
|
||||||
if (!exportData) {
|
if (!exportData) {
|
||||||
process.stdout.write(`File not found: ${args.file}`)
|
process.stdout.write(`File not found: ${file}`)
|
||||||
process.stdout.write(EOL)
|
process.stdout.write(EOL)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -160,7 +172,7 @@ export const ImportCommand = cmd({
|
|||||||
|
|
||||||
const info = Schema.decodeUnknownSync(Session.Info)({
|
const info = Schema.decodeUnknownSync(Session.Info)({
|
||||||
...exportData.info,
|
...exportData.info,
|
||||||
projectID: Instance.project.id,
|
projectID,
|
||||||
}) as Session.Info
|
}) as Session.Info
|
||||||
const row = Session.toRow(info)
|
const row = Session.toRow(info)
|
||||||
Database.use((db) =>
|
Database.use((db) =>
|
||||||
@@ -208,5 +220,3 @@ export const ImportCommand = cmd({
|
|||||||
process.stdout.write(`Imported session: ${exportData.info.id}`)
|
process.stdout.write(`Imported session: ${exportData.info.id}`)
|
||||||
process.stdout.write(EOL)
|
process.stdout.write(EOL)
|
||||||
})
|
})
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|||||||
Reference in New Issue
Block a user