mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-19 09:33:53 +00:00
feat: add confirmation prompt for /init command when context file exists
- Add confirmation dialog when QWEN.md already exists and has content - Use React.createElement to maintain .ts file compatibility - Allow users to choose whether to regenerate existing context file
This commit is contained in:
@@ -53,7 +53,7 @@ describe('initCommand', () => {
|
|||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`should inform the user if ${DEFAULT_CONTEXT_FILENAME} already exists and is non-empty`, async () => {
|
it(`should ask for confirmation if ${DEFAULT_CONTEXT_FILENAME} already exists and is non-empty`, async () => {
|
||||||
// Arrange: Simulate that the file exists
|
// Arrange: Simulate that the file exists
|
||||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||||
vi.spyOn(fs, 'readFileSync').mockReturnValue('# Existing content');
|
vi.spyOn(fs, 'readFileSync').mockReturnValue('# Existing content');
|
||||||
@@ -61,13 +61,15 @@ describe('initCommand', () => {
|
|||||||
// Act: Run the command's action
|
// Act: Run the command's action
|
||||||
const result = await initCommand.action!(mockContext, '');
|
const result = await initCommand.action!(mockContext, '');
|
||||||
|
|
||||||
// Assert: Check for the correct informational message
|
// Assert: Check for the correct confirmation request
|
||||||
expect(result).toEqual({
|
expect(result).toEqual(
|
||||||
type: 'message',
|
expect.objectContaining({
|
||||||
messageType: 'info',
|
type: 'confirm_action',
|
||||||
content: `A ${DEFAULT_CONTEXT_FILENAME} file already exists in this directory. No changes were made.`,
|
prompt: expect.anything(), // React element, not a string
|
||||||
});
|
originalInvocation: expect.anything(),
|
||||||
// Assert: Ensure no file was written
|
}),
|
||||||
|
);
|
||||||
|
// Assert: Ensure no file was written yet
|
||||||
expect(fs.writeFileSync).not.toHaveBeenCalled();
|
expect(fs.writeFileSync).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -91,9 +93,13 @@ describe('initCommand', () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Assert: Check that the correct prompt is submitted
|
// Assert: Check that the correct prompt is submitted
|
||||||
expect(result.type).toBe('submit_prompt');
|
expect(result).toEqual(
|
||||||
expect(result.content).toContain(
|
expect.objectContaining({
|
||||||
'You are Qwen Code, an interactive CLI agent',
|
type: 'submit_prompt',
|
||||||
|
content: expect.stringContaining(
|
||||||
|
'You are Qwen Code, an interactive CLI agent',
|
||||||
|
),
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -104,7 +110,43 @@ describe('initCommand', () => {
|
|||||||
const result = await initCommand.action!(mockContext, '');
|
const result = await initCommand.action!(mockContext, '');
|
||||||
|
|
||||||
expect(fs.writeFileSync).toHaveBeenCalledWith(geminiMdPath, '', 'utf8');
|
expect(fs.writeFileSync).toHaveBeenCalledWith(geminiMdPath, '', 'utf8');
|
||||||
expect(result.type).toBe('submit_prompt');
|
expect(result).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
type: 'submit_prompt',
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it(`should regenerate ${DEFAULT_CONTEXT_FILENAME} when overwrite is confirmed`, async () => {
|
||||||
|
// Arrange: Simulate that the file exists and overwrite is confirmed
|
||||||
|
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||||
|
vi.spyOn(fs, 'readFileSync').mockReturnValue('# Existing content');
|
||||||
|
mockContext.overwriteConfirmed = true;
|
||||||
|
|
||||||
|
// Act: Run the command's action
|
||||||
|
const result = await initCommand.action!(mockContext, '');
|
||||||
|
|
||||||
|
// Assert: Check that writeFileSync was called correctly
|
||||||
|
expect(fs.writeFileSync).toHaveBeenCalledWith(geminiMdPath, '', 'utf8');
|
||||||
|
|
||||||
|
// Assert: Check that an informational message was added to the UI
|
||||||
|
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
||||||
|
{
|
||||||
|
type: 'info',
|
||||||
|
text: `Empty ${DEFAULT_CONTEXT_FILENAME} created. Now analyzing the project to populate it.`,
|
||||||
|
},
|
||||||
|
expect.any(Number),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert: Check that the correct prompt is submitted
|
||||||
|
expect(result).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
type: 'submit_prompt',
|
||||||
|
content: expect.stringContaining(
|
||||||
|
'You are Qwen Code, an interactive CLI agent',
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return an error if config is not available', async () => {
|
it('should return an error if config is not available', async () => {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* @license
|
* @license
|
||||||
* Copyright 2025 Google LLC
|
* Copyright 2025 Qwen
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -13,6 +13,8 @@ import {
|
|||||||
CommandKind,
|
CommandKind,
|
||||||
} from './types.js';
|
} from './types.js';
|
||||||
import { getCurrentGeminiMdFilename } from '@qwen-code/qwen-code-core';
|
import { getCurrentGeminiMdFilename } from '@qwen-code/qwen-code-core';
|
||||||
|
import { Text } from 'ink';
|
||||||
|
import React from 'react';
|
||||||
|
|
||||||
export const initCommand: SlashCommand = {
|
export const initCommand: SlashCommand = {
|
||||||
name: 'init',
|
name: 'init',
|
||||||
@@ -35,15 +37,27 @@ export const initCommand: SlashCommand = {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (fs.existsSync(contextFilePath)) {
|
if (fs.existsSync(contextFilePath)) {
|
||||||
// If file exists but is empty (or whitespace), continue to initialize; otherwise, bail out
|
// If file exists but is empty (or whitespace), continue to initialize
|
||||||
try {
|
try {
|
||||||
const existing = fs.readFileSync(contextFilePath, 'utf8');
|
const existing = fs.readFileSync(contextFilePath, 'utf8');
|
||||||
if (existing && existing.trim().length > 0) {
|
if (existing && existing.trim().length > 0) {
|
||||||
return {
|
// File exists and has content - ask for confirmation to overwrite
|
||||||
type: 'message',
|
if (!context.overwriteConfirmed) {
|
||||||
messageType: 'info',
|
return {
|
||||||
content: `A ${contextFileName} file already exists in this directory. No changes were made.`,
|
type: 'confirm_action',
|
||||||
};
|
// TODO: Move to .tsx file to use JSX syntax instead of React.createElement
|
||||||
|
// For now, using React.createElement to maintain .ts compatibility for PR review
|
||||||
|
prompt: React.createElement(
|
||||||
|
Text,
|
||||||
|
null,
|
||||||
|
`A ${contextFileName} file already exists in this directory. Do you want to regenerate it?`,
|
||||||
|
),
|
||||||
|
originalInvocation: {
|
||||||
|
raw: context.invocation?.raw || '/init',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
// User confirmed overwrite, continue with regeneration
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// If we fail to read, conservatively proceed to (re)create the file
|
// If we fail to read, conservatively proceed to (re)create the file
|
||||||
|
|||||||
Reference in New Issue
Block a user