Repository navigation
feat(cli): /summary supports custom export path #8116
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
Changes from 3 commits
651da05
a8f3726
ae966bb
721ae1b
96f73b1
4e6a6f4
9fbecfe
7238cbf
25e810b
fae425d
afac1c1
f6467bf
bc46383
ddee516
6109840
cae22cd
578baa1
523cf48
5044921
543b120
f18300f
9d6d556
0ee995d
c4c268a
9a9d374
4c32ef3
812e233
63359f6
d707217
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 |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Qwen Team | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
| import * as fs from 'node:fs/promises'; | ||
| import * as os from 'node:os'; | ||
| import path from 'node:path'; | ||
| import { summaryCommand } from './summaryCommand.js'; | ||
| import { createMockCommandContext } from '../../test-utils/mockCommandContext.js'; | ||
| import type { CommandContext } from './types.js'; | ||
|
|
||
| vi.mock('@qwen-code/qwen-code-core', () => ({ | ||
| getProjectSummaryPrompt: () => 'summary prompt', | ||
| runSideQuery: vi.fn(async () => ({ text: 'SUMMARY BODY' })), | ||
| })); | ||
|
|
||
| const makeContext = (projectRoot: string): CommandContext => { | ||
| const chat = { | ||
| getHistoryShallow: () => [ | ||
| { role: 'user', parts: [{ text: 'a' }] }, | ||
| { role: 'model', parts: [{ text: 'b' }] }, | ||
| { role: 'user', parts: [{ text: 'c' }] }, | ||
| ], | ||
| getGenerationConfig: () => ({ systemInstruction: 'sys' }), | ||
| }; | ||
| const config = { | ||
| getProjectRoot: () => projectRoot, | ||
| getGeminiClient: () => ({ getChat: () => chat }), | ||
| getModel: () => 'test-model', | ||
| }; | ||
| return createMockCommandContext({ | ||
| executionMode: 'non_interactive', | ||
| services: { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| config: config as any, | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| describe('summaryCommand custom export path', () => { | ||
| let projectRoot: string; | ||
|
|
||
| beforeEach(async () => { | ||
| projectRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'summary-cmd-')); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await fs.rm(projectRoot, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| interface MessageResult { | ||
| type: string; | ||
| messageType: string; | ||
| content: string; | ||
| } | ||
|
|
||
| const run = async (args: string): Promise<MessageResult> => | ||
| (await summaryCommand.action?.( | ||
| makeContext(projectRoot), | ||
| args, | ||
| )) as MessageResult; | ||
|
|
||
| const fileExists = async (p: string): Promise<boolean> => { | ||
| try { | ||
| return (await fs.stat(p)).isFile(); | ||
| } catch { | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| it('defaults to .qwen/PROJECT_SUMMARY.md with no argument', async () => { | ||
| const result = await run(''); | ||
| const fullPath = path.join(projectRoot, '.qwen', 'PROJECT_SUMMARY.md'); | ||
| expect(await fileExists(fullPath)).toBe(true); | ||
| expect(result).toMatchObject({ type: 'message', messageType: 'info' }); | ||
| expect(result.content).toContain('.qwen/PROJECT_SUMMARY.md'); | ||
| }); | ||
|
|
||
| it('writes a relative file path as-is', async () => { | ||
| await run('notes.md'); | ||
| expect(await fileExists(path.join(projectRoot, 'notes.md'))).toBe(true); | ||
| }); | ||
|
|
||
| it('treats a relative path with a trailing separator as a directory', async () => { | ||
| // Regression: path.resolve strips the trailing separator, so the directory | ||
| // must be detected from the raw argument, not the resolved path. | ||
| await run('docs/'); | ||
| expect( | ||
| await fileExists(path.join(projectRoot, 'docs', 'PROJECT_SUMMARY.md')), | ||
| ).toBe(true); | ||
| expect(await fileExists(path.join(projectRoot, 'docs'))).toBe(false); | ||
| }); | ||
|
|
||
| it('appends the default filename for an existing directory', async () => { | ||
| await fs.mkdir(path.join(projectRoot, 'existingdir')); | ||
| await run('existingdir'); | ||
| expect( | ||
| await fileExists( | ||
| path.join(projectRoot, 'existingdir', 'PROJECT_SUMMARY.md'), | ||
| ), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('writes an absolute path as-is and reports it absolutely', async () => { | ||
| const target = path.join(projectRoot, 'abs', 'out.md'); | ||
| const result = await run(target); | ||
| expect(await fileExists(target)).toBe(true); | ||
| expect(result.content).toContain(target); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,9 +25,10 @@ export const summaryCommand: SlashCommand = { | |
| 'Generate a project summary and save it to .qwen/PROJECT_SUMMARY.md', | ||
| ); | ||
| }, | ||
| argumentHint: '[path]', | ||
| kind: CommandKind.BUILT_IN, | ||
| supportedModes: ['interactive', 'non_interactive', 'acp'] as const, | ||
| action: async (context): Promise<SlashCommandActionReturn> => { | ||
| action: async (context, args): Promise<SlashCommandActionReturn> => { | ||
| const { config } = context.services; | ||
| const { ui } = context; | ||
| const executionMode = context.executionMode ?? 'interactive'; | ||
|
|
@@ -140,29 +141,64 @@ export const summaryCommand: SlashCommand = { | |
| filePathForDisplay: string; | ||
| fullPath: string; | ||
| }> => { | ||
| // 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 | ||
| const customPath = args?.trim(); | ||
|
|
||
| let summaryPath: string; | ||
| let filePathForDisplay: string; | ||
|
|
||
| if (customPath) { | ||
| // Resolve relative to project root | ||
| const resolved = path.isAbsolute(customPath) | ||
| ? customPath | ||
| : path.resolve(projectRoot, customPath); | ||
|
|
||
| // If the path ends with a separator or is an existing directory, | ||
| // append the default filename | ||
| const isDir = | ||
| customPath.endsWith('/') || | ||
| customPath.endsWith(path.sep) || | ||
| (await fsPromises | ||
| .stat(resolved) | ||
| .then((s) => s.isDirectory()) | ||
| .catch(() => false)); | ||
|
|
||
| if (isDir) { | ||
| summaryPath = path.join(resolved, 'PROJECT_SUMMARY.md'); | ||
| } else { | ||
| summaryPath = resolved; | ||
| } | ||
|
|
||
| // Ensure parent directory exists | ||
| await fsPromises.mkdir(path.dirname(summaryPath), { recursive: true }); | ||
|
|
||
| filePathForDisplay = path.isAbsolute(customPath) | ||
| ? summaryPath | ||
| : path.relative(projectRoot, summaryPath); | ||
| } else { | ||
| // Default: save to .qwen/PROJECT_SUMMARY.md | ||
| const qwenDir = path.join(projectRoot, '.qwen'); | ||
| try { | ||
| await fsPromises.mkdir(qwenDir, { recursive: true }); | ||
| } catch (_err) { | ||
| // Directory might already exist, ignore error | ||
| } | ||
| summaryPath = path.join(qwenDir, 'PROJECT_SUMMARY.md'); | ||
| filePathForDisplay = '.qwen/PROJECT_SUMMARY.md'; | ||
| } | ||
|
|
||
| // 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()} | ||
| **Update time**: ${new Date().toISOString()} | ||
| `; | ||
|
|
||
| await fsPromises.writeFile(summaryPath, summaryContent, 'utf8'); | ||
|
Collaborator
Author
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. Declined for this PR. This matches the sibling 中文说明本 PR 中拒绝处理。这与同级的 |
||
|
|
||
| return { | ||
| filePathForDisplay: '.qwen/PROJECT_SUMMARY.md', | ||
| filePathForDisplay, | ||
| fullPath: summaryPath, | ||
| }; | ||
| }; | ||
|
|
||
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.
Declined — the suggested
realpath(resolved).catch(() => resolved)is a no-op for the described scenario: when the target file does not yet exist (the common case),realpathfails with ENOENT and the.catchfalls back to the lexical path, so the symlink is never resolved. A proper fix would need arealpathNearestExistingancestor walk likeexportCommand.ts/statsCommand.ts, which is disproportionate complexity for a non-security-boundary check (the user typing the path already has full filesystem access, consistent with the not-critical rating).中文说明
拒绝——建议的
realpath(resolved).catch(() => resolved)对所描述的场景是空操作:当目标文件尚不存在时(常见情况),realpath因 ENOENT 失败,.catch回退到词法路径,因此符号链接永远不会被解析。正确的修复需要像exportCommand.ts/statsCommand.ts那样进行realpathNearestExisting祖先遍历,这对于非安全边界检查来说复杂度过高(输入路径的用户已经拥有完整的文件系统访问权限,与非关键评级一致)。