Add export command to CLI tool
This commit is contained in:
@@ -10,3 +10,7 @@
|
|||||||
- AVOID `let` statements
|
- AVOID `let` statements
|
||||||
- PREFER single word variable names where possible
|
- PREFER single word variable names where possible
|
||||||
- Use as many bun apis as possible like Bun.file()
|
- Use as many bun apis as possible like Bun.file()
|
||||||
|
|
||||||
|
## Debugging
|
||||||
|
|
||||||
|
- To test opencode in the `packages/opencode` directory you can run `bun dev`
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
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"
|
||||||
|
|
||||||
|
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({ cwd: process.cwd() }, async () => {
|
||||||
|
let sessionID = args.sessionID
|
||||||
|
|
||||||
|
if (!sessionID) {
|
||||||
|
UI.empty()
|
||||||
|
prompts.intro("Export session")
|
||||||
|
|
||||||
|
const sessions = []
|
||||||
|
for await (const session of Session.list()) {
|
||||||
|
sessions.push(session)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sessions.length === 0) {
|
||||||
|
prompts.log.error("No sessions found")
|
||||||
|
prompts.outro("Done")
|
||||||
|
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)}`,
|
||||||
|
})),
|
||||||
|
})
|
||||||
|
|
||||||
|
if (prompts.isCancel(selectedSession)) {
|
||||||
|
throw new UI.CancelledError()
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionID = selectedSession as string
|
||||||
|
|
||||||
|
prompts.outro("Exporting session...")
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const sessionInfo = await Session.get(sessionID!)
|
||||||
|
const messages = await Session.messages(sessionID!)
|
||||||
|
|
||||||
|
const exportData = {
|
||||||
|
info: sessionInfo,
|
||||||
|
messages: messages.map((msg) => ({
|
||||||
|
info: msg.info,
|
||||||
|
parts: msg.parts,
|
||||||
|
})),
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(JSON.stringify(exportData, null, 2))
|
||||||
|
} catch (error) {
|
||||||
|
UI.error(`Session not found: ${sessionID!}`)
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
})
|
||||||
@@ -18,6 +18,7 @@ import { DebugCommand } from "./cli/cmd/debug"
|
|||||||
import { StatsCommand } from "./cli/cmd/stats"
|
import { StatsCommand } from "./cli/cmd/stats"
|
||||||
import { McpCommand } from "./cli/cmd/mcp"
|
import { McpCommand } from "./cli/cmd/mcp"
|
||||||
import { GithubCommand } from "./cli/cmd/github"
|
import { GithubCommand } from "./cli/cmd/github"
|
||||||
|
import { ExportCommand } from "./cli/cmd/export"
|
||||||
|
|
||||||
const cancel = new AbortController()
|
const cancel = new AbortController()
|
||||||
|
|
||||||
@@ -80,6 +81,7 @@ const cli = yargs(hideBin(process.argv))
|
|||||||
.command(ServeCommand)
|
.command(ServeCommand)
|
||||||
.command(ModelsCommand)
|
.command(ModelsCommand)
|
||||||
.command(StatsCommand)
|
.command(StatsCommand)
|
||||||
|
.command(ExportCommand)
|
||||||
.command(GithubCommand)
|
.command(GithubCommand)
|
||||||
.fail((msg) => {
|
.fail((msg) => {
|
||||||
if (msg.startsWith("Unknown argument") || msg.startsWith("Not enough non-option arguments")) {
|
if (msg.startsWith("Unknown argument") || msg.startsWith("Not enough non-option arguments")) {
|
||||||
|
|||||||
Reference in New Issue
Block a user