refactor: move /chat summary to standalone /summary command

- Extract summary logic from chatCommand to new summaryCommand
- Update /quit-confirm to use /summary instead of /chat summary
- Update documentation and UI labels
- Fix copyright headers for new files
This commit is contained in:
pomelo-nwu
2025-09-11 13:40:26 +08:00
parent e0eec9d5de
commit f6cbb410ae
6 changed files with 209 additions and 204 deletions

View File

@@ -28,6 +28,7 @@ import { privacyCommand } from '../ui/commands/privacyCommand.js';
import { quitCommand, quitConfirmCommand } from '../ui/commands/quitCommand.js';
import { restoreCommand } from '../ui/commands/restoreCommand.js';
import { statsCommand } from '../ui/commands/statsCommand.js';
import { summaryCommand } from '../ui/commands/summaryCommand.js';
import { themeCommand } from '../ui/commands/themeCommand.js';
import { toolsCommand } from '../ui/commands/toolsCommand.js';
import { settingsCommand } from '../ui/commands/settingsCommand.js';
@@ -73,6 +74,7 @@ export class BuiltinCommandLoader implements ICommandLoader {
quitConfirmCommand,
restoreCommand(this.config),
statsCommand,
summaryCommand,
themeCommand,
toolsCommand,
settingsCommand,

View File

@@ -15,16 +15,9 @@ import {
CommandKind,
SlashCommandActionReturn,
} from './types.js';
import {
decodeTagName,
getProjectSummaryPrompt,
} from '@qwen-code/qwen-code-core';
import { decodeTagName } from '@qwen-code/qwen-code-core';
import path from 'path';
import {
HistoryItemWithoutId,
HistoryItemSummary,
MessageType,
} from '../types.js';
import { HistoryItemWithoutId, MessageType } from '../types.js';
interface ChatDetail {
name: string;
@@ -279,189 +272,9 @@ const deleteCommand: SlashCommand = {
},
};
const summaryCommand: SlashCommand = {
name: 'summary',
description:
'Generate a project summary and save it to .qwen/PROJECT_SUMMARY.md',
kind: CommandKind.BUILT_IN,
action: async (context): Promise<SlashCommandActionReturn> => {
const { config } = context.services;
const { ui } = context;
if (!config) {
return {
type: 'message',
messageType: 'error',
content: 'Config not loaded.',
};
}
const geminiClient = config.getGeminiClient();
if (!geminiClient) {
return {
type: 'message',
messageType: 'error',
content: 'No chat client available to generate summary.',
};
}
// Check if already generating summary
if (ui.pendingItem) {
ui.addItem(
{
type: 'error' as const,
text: 'Already generating summary, wait for previous request to complete',
},
Date.now(),
);
return {
type: 'message',
messageType: 'error',
content:
'Already generating summary, wait for previous request to complete',
};
}
try {
// Get the current chat history
const chat = geminiClient.getChat();
const history = chat.getHistory();
if (history.length <= 2) {
return {
type: 'message',
messageType: 'info',
content: 'No conversation found to summarize.',
};
}
// Show loading state
const pendingMessage: HistoryItemSummary = {
type: 'summary',
summary: {
isPending: true,
stage: 'generating',
},
};
ui.setPendingItem(pendingMessage);
// Build the conversation context for summary generation
const conversationContext = history.map((message) => ({
role: message.role,
parts: message.parts,
}));
// Use generateContent with chat history as context
const response = await geminiClient.generateContent(
[
...conversationContext,
{
role: 'user',
parts: [
{
text: getProjectSummaryPrompt(),
},
],
},
],
{},
new AbortController().signal,
);
// Extract text from response
const parts = response.candidates?.[0]?.content?.parts;
const markdownSummary =
parts
?.map((part) => part.text)
.filter((text): text is string => typeof text === 'string')
.join('') || '';
if (!markdownSummary) {
throw new Error(
'Failed to generate summary - no text content received from LLM response',
);
}
// Update loading message to show saving progress
ui.setPendingItem({
type: 'summary',
summary: {
isPending: true,
stage: 'saving',
},
});
// Ensure .qwen directory exists
const projectRoot = config.getProjectRoot();
const qwenDir = path.join(projectRoot, '.qwen');
try {
await fsPromises.mkdir(qwenDir, { recursive: true });
} catch (_err) {
// Directory might already exist, ignore error
}
// Save the summary to PROJECT_SUMMARY.md
const summaryPath = path.join(qwenDir, 'PROJECT_SUMMARY.md');
const summaryContent = `${markdownSummary}
---
## Summary Metadata
**Update time**: ${new Date().toISOString()}
`;
await fsPromises.writeFile(summaryPath, summaryContent, 'utf8');
// Clear pending item and show success message
ui.setPendingItem(null);
const completedSummaryItem: HistoryItemSummary = {
type: 'summary',
summary: {
isPending: false,
stage: 'completed',
filePath: '.qwen/PROJECT_SUMMARY.md',
},
};
ui.addItem(completedSummaryItem, Date.now());
return {
type: 'message',
messageType: 'info',
content: '', // Empty content since we show the message in UI component
};
} catch (error) {
// Clear pending item on error
ui.setPendingItem(null);
ui.addItem(
{
type: 'error' as const,
text: `❌ Failed to generate project context summary: ${
error instanceof Error ? error.message : String(error)
}`,
},
Date.now(),
);
return {
type: 'message',
messageType: 'error',
content: `Failed to generate project context summary: ${
error instanceof Error ? error.message : String(error)
}`,
};
}
},
};
export const chatCommand: SlashCommand = {
name: 'chat',
description: 'Manage conversation history.',
kind: CommandKind.BUILT_IN,
subCommands: [
listCommand,
saveCommand,
resumeCommand,
deleteCommand,
summaryCommand,
],
subCommands: [listCommand, saveCommand, resumeCommand, deleteCommand],
};

View File

@@ -0,0 +1,189 @@
/**
* @license
* Copyright 2025 Qwen
* SPDX-License-Identifier: Apache-2.0
*/
import * as fsPromises from 'fs/promises';
import path from 'path';
import {
SlashCommand,
CommandKind,
SlashCommandActionReturn,
} from './types.js';
import { getProjectSummaryPrompt } from '@qwen-code/qwen-code-core';
import { HistoryItemSummary } from '../types.js';
export const summaryCommand: SlashCommand = {
name: 'summary',
description:
'Generate a project summary and save it to .qwen/PROJECT_SUMMARY.md',
kind: CommandKind.BUILT_IN,
action: async (context): Promise<SlashCommandActionReturn> => {
const { config } = context.services;
const { ui } = context;
if (!config) {
return {
type: 'message',
messageType: 'error',
content: 'Config not loaded.',
};
}
const geminiClient = config.getGeminiClient();
if (!geminiClient) {
return {
type: 'message',
messageType: 'error',
content: 'No chat client available to generate summary.',
};
}
// Check if already generating summary
if (ui.pendingItem) {
ui.addItem(
{
type: 'error' as const,
text: 'Already generating summary, wait for previous request to complete',
},
Date.now(),
);
return {
type: 'message',
messageType: 'error',
content:
'Already generating summary, wait for previous request to complete',
};
}
try {
// Get the current chat history
const chat = geminiClient.getChat();
const history = chat.getHistory();
if (history.length <= 2) {
return {
type: 'message',
messageType: 'info',
content: 'No conversation found to summarize.',
};
}
// Show loading state
const pendingMessage: HistoryItemSummary = {
type: 'summary',
summary: {
isPending: true,
stage: 'generating',
},
};
ui.setPendingItem(pendingMessage);
// Build the conversation context for summary generation
const conversationContext = history.map((message) => ({
role: message.role,
parts: message.parts,
}));
// Use generateContent with chat history as context
const response = await geminiClient.generateContent(
[
...conversationContext,
{
role: 'user',
parts: [
{
text: getProjectSummaryPrompt(),
},
],
},
],
{},
new AbortController().signal,
);
// Extract text from response
const parts = response.candidates?.[0]?.content?.parts;
const markdownSummary =
parts
?.map((part) => part.text)
.filter((text): text is string => typeof text === 'string')
.join('') || '';
if (!markdownSummary) {
throw new Error(
'Failed to generate summary - no text content received from LLM response',
);
}
// Update loading message to show saving progress
ui.setPendingItem({
type: 'summary',
summary: {
isPending: true,
stage: 'saving',
},
});
// Ensure .qwen directory exists
const projectRoot = config.getProjectRoot();
const qwenDir = path.join(projectRoot, '.qwen');
try {
await fsPromises.mkdir(qwenDir, { recursive: true });
} catch (_err) {
// Directory might already exist, ignore error
}
// Save the summary to PROJECT_SUMMARY.md
const summaryPath = path.join(qwenDir, 'PROJECT_SUMMARY.md');
const summaryContent = `${markdownSummary}
---
## Summary Metadata
**Update time**: ${new Date().toISOString()}
`;
await fsPromises.writeFile(summaryPath, summaryContent, 'utf8');
// Clear pending item and show success message
ui.setPendingItem(null);
const completedSummaryItem: HistoryItemSummary = {
type: 'summary',
summary: {
isPending: false,
stage: 'completed',
filePath: '.qwen/PROJECT_SUMMARY.md',
},
};
ui.addItem(completedSummaryItem, Date.now());
return {
type: 'message',
messageType: 'info',
content: '', // Empty content since we show the message in UI component
};
} catch (error) {
// Clear pending item on error
ui.setPendingItem(null);
ui.addItem(
{
type: 'error' as const,
text: `❌ Failed to generate project context summary: ${
error instanceof Error ? error.message : String(error)
}`,
},
Date.now(),
);
return {
type: 'message',
messageType: 'error',
content: `Failed to generate project context summary: ${
error instanceof Error ? error.message : String(error)
}`,
};
}
},
};

View File

@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2025 Google LLC
* Copyright 2025 Qwen
* SPDX-License-Identifier: Apache-2.0
*/
@@ -42,7 +42,7 @@ export const QuitConfirmationDialog: React.FC<QuitConfirmationDialogProps> = ({
value: QuitChoice.QUIT,
},
{
label: 'Generate summary and quit (/chat summary)',
label: 'Generate summary and quit (/summary)',
value: QuitChoice.SUMMARY_AND_QUIT,
},
{

View File

@@ -434,7 +434,7 @@ export const useSlashCommandProcessor = (
setTimeout(() => handleSlashCommand('/quit'), 100);
} else if (action === 'summary_and_quit') {
// Generate summary and then quit
handleSlashCommand('/chat summary')
handleSlashCommand('/summary')
.then(() => {
// Wait for user to see the summary result
setTimeout(() => {