fix(cli): /memory show respects context.fileName

This commit is contained in:
liqoingyu 2026-01-07 20:07:41 +08:00
parent 570ec432af
commit 8a15017593
2 changed files with 78 additions and 4 deletions

View file

@ -11,9 +11,12 @@ import type { SlashCommand, type CommandContext } from './types.js';
import { createMockCommandContext } from '../../test-utils/mockCommandContext.js'; import { createMockCommandContext } from '../../test-utils/mockCommandContext.js';
import { MessageType } from '../types.js'; import { MessageType } from '../types.js';
import type { LoadedSettings } from '../../config/settings.js'; import type { LoadedSettings } from '../../config/settings.js';
import { readFile } from 'node:fs/promises';
import os from 'node:os';
import { import {
getErrorMessage, getErrorMessage,
loadServerHierarchicalMemory, loadServerHierarchicalMemory,
setGeminiMdFilename,
type FileDiscoveryService, type FileDiscoveryService,
type LoadServerHierarchicalMemoryResponse, type LoadServerHierarchicalMemoryResponse,
} from '@qwen-code/qwen-code-core'; } from '@qwen-code/qwen-code-core';
@ -31,7 +34,18 @@ vi.mock('@qwen-code/qwen-code-core', async (importOriginal) => {
}; };
}); });
vi.mock('node:fs/promises', () => {
const readFile = vi.fn();
return {
readFile,
default: {
readFile,
},
};
});
const mockLoadServerHierarchicalMemory = loadServerHierarchicalMemory as Mock; const mockLoadServerHierarchicalMemory = loadServerHierarchicalMemory as Mock;
const mockReadFile = readFile as unknown as Mock;
describe('memoryCommand', () => { describe('memoryCommand', () => {
let mockContext: CommandContext; let mockContext: CommandContext;
@ -52,6 +66,10 @@ describe('memoryCommand', () => {
let mockGetGeminiMdFileCount: Mock; let mockGetGeminiMdFileCount: Mock;
beforeEach(() => { beforeEach(() => {
setGeminiMdFilename('QWEN.md');
mockReadFile.mockReset();
vi.restoreAllMocks();
showCommand = getSubCommand('show'); showCommand = getSubCommand('show');
mockGetUserMemory = vi.fn(); mockGetUserMemory = vi.fn();
@ -102,6 +120,56 @@ describe('memoryCommand', () => {
expect.any(Number), expect.any(Number),
); );
}); });
it('should show project memory from the configured context file', async () => {
const projectCommand = showCommand.subCommands?.find(
(cmd) => cmd.name === '--project',
);
if (!projectCommand?.action) throw new Error('Command has no action');
setGeminiMdFilename('AGENTS.md');
vi.spyOn(process, 'cwd').mockReturnValue('/test/project');
mockReadFile.mockResolvedValue('project memory');
await projectCommand.action(mockContext, '');
expect(mockReadFile).toHaveBeenCalledWith(
'/test/project/AGENTS.md',
'utf-8',
);
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
{
type: MessageType.INFO,
text: expect.stringContaining('/test/project/AGENTS.md'),
},
expect.any(Number),
);
});
it('should show global memory from the configured context file', async () => {
const globalCommand = showCommand.subCommands?.find(
(cmd) => cmd.name === '--global',
);
if (!globalCommand?.action) throw new Error('Command has no action');
setGeminiMdFilename('AGENTS.md');
vi.spyOn(os, 'homedir').mockReturnValue('/home/user');
mockReadFile.mockResolvedValue('global memory');
await globalCommand.action(mockContext, '');
expect(mockReadFile).toHaveBeenCalledWith(
'/home/user/.qwen/AGENTS.md',
'utf-8',
);
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
{
type: MessageType.INFO,
text: expect.stringContaining('Global memory content'),
},
expect.any(Number),
);
});
}); });
describe('/memory add', () => { describe('/memory add', () => {

View file

@ -6,12 +6,13 @@
import { import {
getErrorMessage, getErrorMessage,
getCurrentGeminiMdFilename,
loadServerHierarchicalMemory, loadServerHierarchicalMemory,
QWEN_DIR, QWEN_DIR,
} from '@qwen-code/qwen-code-core'; } from '@qwen-code/qwen-code-core';
import path from 'node:path'; import path from 'node:path';
import os from 'os'; import os from 'node:os';
import fs from 'fs/promises'; import fs from 'node:fs/promises';
import { MessageType } from '../types.js'; import { MessageType } from '../types.js';
import type { SlashCommand, SlashCommandActionReturn } from './types.js'; import type { SlashCommand, SlashCommandActionReturn } from './types.js';
import { CommandKind } from './types.js'; import { CommandKind } from './types.js';
@ -56,7 +57,12 @@ export const memoryCommand: SlashCommand = {
kind: CommandKind.BUILT_IN, kind: CommandKind.BUILT_IN,
action: async (context) => { action: async (context) => {
try { try {
const projectMemoryPath = path.join(process.cwd(), 'QWEN.md'); const workingDir =
context.services.config?.getWorkingDir?.() ?? process.cwd();
const projectMemoryPath = path.join(
workingDir,
getCurrentGeminiMdFilename(),
);
const memoryContent = await fs.readFile( const memoryContent = await fs.readFile(
projectMemoryPath, projectMemoryPath,
'utf-8', 'utf-8',
@ -104,7 +110,7 @@ export const memoryCommand: SlashCommand = {
const globalMemoryPath = path.join( const globalMemoryPath = path.join(
os.homedir(), os.homedir(),
QWEN_DIR, QWEN_DIR,
'QWEN.md', getCurrentGeminiMdFilename(),
); );
const globalMemoryContent = await fs.readFile( const globalMemoryContent = await fs.readFile(
globalMemoryPath, globalMemoryPath,