Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/cli/src/ui/commands/exportCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('exportCommand', () => {
it('should have correct name and description', () => {
expect(exportCommand.name).toBe('export');
expect(exportCommand.description).toBe(
'Export current session message history to a file',
'Export current session to HTML by default. Other formats: md, json, jsonl.',
);
});

Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/ui/commands/exportCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,9 @@ async function exportJsonlAction(
export const exportCommand: SlashCommand = {
name: 'export',
get description() {
return t('Export current session message history to a file');
return t(
'Export current session to HTML by default. Other formats: md, json, jsonl.',
);
},
kind: CommandKind.BUILT_IN,
subCommands: [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/**
* @license
* Copyright 2025 Qwen Team
* SPDX-License-Identifier: Apache-2.0
*/

import { beforeEach, describe, expect, it, vi } from 'vitest';

const {
mockLoadSession,
mockCollectSessionData,
mockNormalizeSessionData,
mockToHtml,
mockToMarkdown,
mockToJson,
mockToJsonl,
mockGenerateExportFilename,
mockShowSaveDialog,
mockWriteFile,
} = vi.hoisted(() => ({
mockLoadSession: vi.fn(),
mockCollectSessionData: vi.fn(),
mockNormalizeSessionData: vi.fn(),
mockToHtml: vi.fn(),
mockToMarkdown: vi.fn(),
mockToJson: vi.fn(),
mockToJsonl: vi.fn(),
mockGenerateExportFilename: vi.fn(),
mockShowSaveDialog: vi.fn(),
mockWriteFile: vi.fn(),
}));

vi.mock('@qwen-code/qwen-code-core', () => {
class SessionService {
constructor(_cwd: string) {}

async loadSession(_sessionId: string) {
return mockLoadSession();
}
}

return {
SessionService,
};
});

vi.mock('../../../cli/src/ui/utils/export/index.js', () => ({
collectSessionData: mockCollectSessionData,
normalizeSessionData: mockNormalizeSessionData,
toHtml: mockToHtml,
toMarkdown: mockToMarkdown,
toJson: mockToJson,
toJsonl: mockToJsonl,
generateExportFilename: mockGenerateExportFilename,
}));

vi.mock('node:fs/promises', () => ({
writeFile: mockWriteFile,
}));

vi.mock('vscode', () => ({
Uri: {
file: (fsPath: string) => ({ fsPath }),
},
window: {
showSaveDialog: mockShowSaveDialog,
},
}));

import {
exportSessionToFile,
parseExportSlashCommand,
} from './sessionExportService.js';

describe('sessionExportService', () => {
beforeEach(() => {
vi.clearAllMocks();

mockLoadSession.mockResolvedValue({
conversation: {
sessionId: 'session-1',
startTime: '2025-01-01T00:00:00Z',
messages: [],
},
});
mockCollectSessionData.mockResolvedValue({
sessionId: 'session-1',
startTime: '2025-01-01T00:00:00Z',
messages: [],
});
mockNormalizeSessionData.mockImplementation((data) => data);
mockToHtml.mockReturnValue('<html>export</html>');
mockToMarkdown.mockReturnValue('# export');
mockToJson.mockReturnValue('{"ok":true}');
mockToJsonl.mockReturnValue('{"ok":true}');
mockGenerateExportFilename.mockImplementation(
(format: string) => `qwen-export.${format}`,
);
});

describe('parseExportSlashCommand', () => {
it('returns null for non-export input', () => {
expect(parseExportSlashCommand('hello')).toBeNull();
expect(parseExportSlashCommand('/model')).toBeNull();
});

it('defaults to html for bare /export', () => {
expect(parseExportSlashCommand('/export')).toBe('html');
expect(parseExportSlashCommand('/export ')).toBe('html');
});

it('returns the requested export format', () => {
expect(parseExportSlashCommand('/export md')).toBe('md');
expect(parseExportSlashCommand('/export JSON')).toBe('json');
});

it('rejects unsupported export arguments', () => {
expect(() => parseExportSlashCommand('/export csv')).toThrow(
'Unsupported /export format',
);
expect(() => parseExportSlashCommand('/export md extra')).toThrow(
'Unsupported /export format',
);
});
});

describe('exportSessionToFile', () => {
it('writes the exported session to the user-selected file', async () => {
mockShowSaveDialog.mockResolvedValue({
fsPath: '/workspace/custom-export.html',
});

const result = await exportSessionToFile({
sessionId: 'session-1',
cwd: '/workspace',
format: 'html',
});

expect(mockCollectSessionData).toHaveBeenCalledWith(
expect.objectContaining({ sessionId: 'session-1' }),
expect.anything(),
);
expect(mockNormalizeSessionData).toHaveBeenCalled();
expect(mockToHtml).toHaveBeenCalled();
expect(mockWriteFile).toHaveBeenCalledWith(
'/workspace/custom-export.html',
'<html>export</html>',
'utf-8',
);
expect(result).toEqual({
cancelled: false,
filename: 'custom-export.html',
uri: { fsPath: '/workspace/custom-export.html' },
});
});

it('returns cancelled when the save dialog is dismissed', async () => {
mockShowSaveDialog.mockResolvedValue(undefined);

const result = await exportSessionToFile({
sessionId: 'session-1',
cwd: '/workspace',
format: 'md',
});

expect(mockWriteFile).not.toHaveBeenCalled();
expect(result).toEqual({ cancelled: true });
});

it('throws when the target session cannot be loaded', async () => {
mockLoadSession.mockResolvedValue(undefined);

await expect(
exportSessionToFile({
sessionId: 'missing-session',
cwd: '/workspace',
format: 'json',
}),
).rejects.toThrow('No active session found to export.');
});
});
});
140 changes: 140 additions & 0 deletions packages/vscode-ide-companion/src/services/sessionExportService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/**
* @license
* Copyright 2025 Qwen Team
* SPDX-License-Identifier: Apache-2.0
*/

import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import * as vscode from 'vscode';
import { SessionService, type Config } from '@qwen-code/qwen-code-core';
import {
collectSessionData,
generateExportFilename,
normalizeSessionData,
toHtml,
toJson,
toJsonl,
toMarkdown,
} from '../../../cli/src/ui/utils/export/index.js';

export const SESSION_EXPORT_FORMATS = ['html', 'md', 'json', 'jsonl'] as const;

export type SessionExportFormat = (typeof SESSION_EXPORT_FORMATS)[number];

export interface SessionExportResult {
cancelled: boolean;
filename?: string;
uri?: vscode.Uri;
}

const EXPORT_CONFIG = {
getChannel: () => 'vscode-companion',
getToolRegistry: () => undefined,
} as unknown as Config;

const SAVE_DIALOG_FILTERS: Record<
SessionExportFormat,
Record<string, string[]>
> = {
html: { HTML: ['html'] },
md: { Markdown: ['md'] },
json: { JSON: ['json'] },
jsonl: { JSONL: ['jsonl'] },
};

function isSessionExportFormat(value: string): value is SessionExportFormat {
return SESSION_EXPORT_FORMATS.includes(value as SessionExportFormat);
}

export function parseExportSlashCommand(
text: string,
): SessionExportFormat | null {
const trimmed = text.replace(/\u200B/g, '').trim();
if (!trimmed.startsWith('/')) {
return null;
}

const parts = trimmed.split(/\s+/).filter(Boolean);
const [command, format, ...rest] = parts;
if (command !== '/export') {
return null;
}

if (!format) {
return 'html';
}

const normalizedFormat = format.toLowerCase();
if (rest.length === 0 && isSessionExportFormat(normalizedFormat)) {
return normalizedFormat;
}

throw new Error(
'Unsupported /export format. Use /export, /export html, /export md, /export json, or /export jsonl.',
);
}

function renderExportContent(
format: SessionExportFormat,
normalizedData: Awaited<ReturnType<typeof normalizeSessionData>>,
): string {
switch (format) {
case 'html':
return toHtml(normalizedData);
case 'md':
return toMarkdown(normalizedData);
case 'json':
return toJson(normalizedData);
case 'jsonl':
return toJsonl(normalizedData);
default: {
const unreachableFormat: never = format;
throw new Error(`Unsupported export format: ${unreachableFormat}`);
}
}
}

export async function exportSessionToFile(options: {
sessionId: string;
cwd: string;
format: SessionExportFormat;
}): Promise<SessionExportResult> {
const { cwd, format, sessionId } = options;
const sessionService = new SessionService(cwd);
const sessionData = await sessionService.loadSession(sessionId);

if (!sessionData) {
throw new Error('No active session found to export.');
}

const exportData = await collectSessionData(
sessionData.conversation,
EXPORT_CONFIG,
);
const normalizedData = normalizeSessionData(
exportData,
sessionData.conversation.messages,
EXPORT_CONFIG,
);
const content = renderExportContent(format, normalizedData);
const filename = generateExportFilename(format);
const uri = await vscode.window.showSaveDialog({
defaultUri: vscode.Uri.file(path.join(cwd, filename)),
filters: SAVE_DIALOG_FILTERS[format],
saveLabel: 'Export Session',
title: 'Export Session',
});

if (!uri) {
return { cancelled: true };
}

await fs.writeFile(uri.fsPath, content, 'utf-8');

return {
cancelled: false,
filename: path.basename(uri.fsPath),
uri,
};
}
Loading
Loading