-
Notifications
You must be signed in to change notification settings - Fork 4.2k
feat(cli): export and import sessions #11075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,5 +1,10 @@ | ||||||
| import fs from "fs"; | ||||||
|
|
||||||
| import { type AssistantConfig } from "@continuedev/sdk"; | ||||||
| import chalk from "chalk"; | ||||||
| import type { Session } from "core/index.js"; | ||||||
| import historyManager from "core/util/history.js"; | ||||||
| import { v4 as uuidv4 } from "uuid"; | ||||||
|
|
||||||
| import { | ||||||
| isAuthenticated, | ||||||
|
|
@@ -173,6 +178,83 @@ function handleJobs() { | |||||
| return { openJobsSelector: true }; | ||||||
| } | ||||||
|
|
||||||
| interface ExportedSession { | ||||||
| version: 1; | ||||||
| exportedAt: string; | ||||||
| session: Session; | ||||||
| } | ||||||
|
|
||||||
| function handleExport(_args: string[]): SlashCommandResult { | ||||||
| posthogService.capture("useSlashCommand", { name: "export" }); | ||||||
|
|
||||||
| return { | ||||||
| exit: false, | ||||||
| openExportSelector: true, | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| function handleImport(args: string[]): SlashCommandResult { | ||||||
| posthogService.capture("useSlashCommand", { name: "import" }); | ||||||
|
|
||||||
| const filePath = args[0]; | ||||||
| if (!filePath) { | ||||||
| return { | ||||||
| exit: false, | ||||||
| output: chalk.yellow( | ||||||
| "Please provide a file path. Usage: /import <file-path>", | ||||||
| ), | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| if (!fs.existsSync(filePath)) { | ||||||
| return { | ||||||
| exit: false, | ||||||
| output: chalk.red(`File not found: ${filePath}`), | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| try { | ||||||
| const fileContent = fs.readFileSync(filePath, "utf-8"); | ||||||
| const exportedData: ExportedSession = JSON.parse(fileContent); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Import command lacks runtime validation of exported session format/version before persistence, allowing incompatible or malformed session data to be saved. Prompt for AI agents |
||||||
|
|
||||||
| let session = exportedData.session; | ||||||
|
|
||||||
| const existingSessions = historyManager.list({ limit: 1000 }); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Import collision detection only checks 1000 sessions, so duplicate IDs outside that window can be missed and existing sessions overwritten. Prompt for AI agents
Suggested change
|
||||||
| const sessionExists = existingSessions.some( | ||||||
| (s) => s.sessionId === session.sessionId, | ||||||
| ); | ||||||
|
|
||||||
| if (sessionExists) { | ||||||
| const originalId = session.sessionId; | ||||||
| session = { | ||||||
| ...session, | ||||||
| sessionId: uuidv4(), | ||||||
| }; | ||||||
| historyManager.save(session); | ||||||
| return { | ||||||
| exit: false, | ||||||
| output: chalk.green( | ||||||
| `Session imported with new ID: ${session.sessionId}\n` + | ||||||
| chalk.gray(`(original ID: ${originalId} already existed)`), | ||||||
| ), | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| historyManager.save(session); | ||||||
| return { | ||||||
| exit: false, | ||||||
| output: chalk.green( | ||||||
| `Session imported: ${session.sessionId} (${session.title})`, | ||||||
| ), | ||||||
| }; | ||||||
| } catch (error: any) { | ||||||
| return { | ||||||
| exit: false, | ||||||
| output: chalk.red(`Failed to import session: ${error.message}`), | ||||||
| }; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| const commandHandlers: Record<string, CommandHandler> = { | ||||||
| help: handleHelp, | ||||||
| clear: () => { | ||||||
|
|
@@ -208,6 +290,8 @@ const commandHandlers: Record<string, CommandHandler> = { | |||||
| return { openUpdateSelector: true }; | ||||||
| }, | ||||||
| jobs: handleJobs, | ||||||
| export: handleExport, | ||||||
| import: handleImport, | ||||||
| }; | ||||||
|
|
||||||
| export async function handleSlashCommands( | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2:
/importonly usesargs[0], so file paths containing spaces are truncated and reported as missing.Prompt for AI agents