mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-03-30 13:54:01 +00:00
89 lines
2.4 KiB
TypeScript
89 lines
2.4 KiB
TypeScript
import type { Argv } from "yargs"
|
|
import { Session } from "../../session"
|
|
import { cmd } from "./cmd"
|
|
import { bootstrap } from "../bootstrap"
|
|
import { UI } from "../ui"
|
|
import * as prompts from "@clack/prompts"
|
|
import { EOL } from "os"
|
|
|
|
export const ExportCommand = cmd({
|
|
command: "export [sessionID]",
|
|
describe: "export session data as JSON",
|
|
builder: (yargs: Argv) => {
|
|
return yargs.positional("sessionID", {
|
|
describe: "session id to export",
|
|
type: "string",
|
|
})
|
|
},
|
|
handler: async (args) => {
|
|
await bootstrap(process.cwd(), async () => {
|
|
let sessionID = args.sessionID
|
|
process.stderr.write(`Exporting session: ${sessionID ?? "latest"}`)
|
|
|
|
if (!sessionID) {
|
|
UI.empty()
|
|
prompts.intro("Export session", {
|
|
output: process.stderr,
|
|
})
|
|
|
|
const sessions = []
|
|
for await (const session of Session.list()) {
|
|
sessions.push(session)
|
|
}
|
|
|
|
if (sessions.length === 0) {
|
|
prompts.log.error("No sessions found", {
|
|
output: process.stderr,
|
|
})
|
|
prompts.outro("Done", {
|
|
output: process.stderr,
|
|
})
|
|
return
|
|
}
|
|
|
|
sessions.sort((a, b) => b.time.updated - a.time.updated)
|
|
|
|
const selectedSession = await prompts.autocomplete({
|
|
message: "Select session to export",
|
|
maxItems: 10,
|
|
options: sessions.map((session) => ({
|
|
label: session.title,
|
|
value: session.id,
|
|
hint: `${new Date(session.time.updated).toLocaleString()} • ${session.id.slice(-8)}`,
|
|
})),
|
|
output: process.stderr,
|
|
})
|
|
|
|
if (prompts.isCancel(selectedSession)) {
|
|
throw new UI.CancelledError()
|
|
}
|
|
|
|
sessionID = selectedSession as string
|
|
|
|
prompts.outro("Exporting session...", {
|
|
output: process.stderr,
|
|
})
|
|
}
|
|
|
|
try {
|
|
const sessionInfo = await Session.get(sessionID!)
|
|
const messages = await Session.messages({ sessionID: sessionID! })
|
|
|
|
const exportData = {
|
|
info: sessionInfo,
|
|
messages: messages.map((msg) => ({
|
|
info: msg.info,
|
|
parts: msg.parts,
|
|
})),
|
|
}
|
|
|
|
process.stdout.write(JSON.stringify(exportData, null, 2))
|
|
process.stdout.write(EOL)
|
|
} catch (error) {
|
|
UI.error(`Session not found: ${sessionID!}`)
|
|
process.exit(1)
|
|
}
|
|
})
|
|
},
|
|
})
|