(host: SlashCommandHost, args: string)
| 88 | } |
| 89 | |
| 90 | export async function handleExportMdCommand(host: SlashCommandHost, args: string): Promise<void> { |
| 91 | const session = host.session; |
| 92 | if (session === undefined) { |
| 93 | host.showError(NO_ACTIVE_SESSION_MESSAGE); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | host.showStatus('Exporting session as Markdown…'); |
| 98 | try { |
| 99 | const context = await session.getContext(); |
| 100 | if (context.history.length === 0) { |
| 101 | host.showError('No messages to export.'); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | const now = new Date(); |
| 106 | const shortId = session.id.slice(0, 8); |
| 107 | const timestamp = now.toISOString().replaceAll(/[-:]/g, '').replace(/T/, '-').slice(0, 15); |
| 108 | const defaultName = `kimi-export-${shortId}-${timestamp}.md`; |
| 109 | |
| 110 | const trimmedArgs = args.trim(); |
| 111 | const outputPath = trimmedArgs.length > 0 |
| 112 | ? resolve(trimmedArgs) |
| 113 | : resolve(host.state.appState.workDir, defaultName); |
| 114 | |
| 115 | const md = buildExportMarkdown({ |
| 116 | sessionId: session.id, |
| 117 | workDir: host.state.appState.workDir, |
| 118 | history: context.history, |
| 119 | tokenCount: context.tokenCount, |
| 120 | now, |
| 121 | }); |
| 122 | |
| 123 | await mkdir(dirname(outputPath), { recursive: true }); |
| 124 | await writeFile(outputPath, md, 'utf-8'); |
| 125 | |
| 126 | const linked = toTerminalHyperlink(outputPath, pathToFileURL(outputPath).href); |
| 127 | host.showNotice(`Exported ${String(context.history.length)} messages`, linked); |
| 128 | } catch (error) { |
| 129 | const msg = formatErrorMessage(error); |
| 130 | host.showError(`Failed to export session: ${msg}`); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | export async function handleExportDebugZipCommand(host: SlashCommandHost): Promise<void> { |
| 135 | const session = host.session; |
no test coverage detected