Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
15 changes: 15 additions & 0 deletions packages/cli/src/ui/hooks/slashCommandProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,21 @@ describe('useSlashCommandProcessor', () => {
);
});

it('should let slash-prefixed file paths fall through to the model', async () => {
const result = setupProcessorHook();
await waitFor(() => expect(result.current.slashCommands).toBeDefined());

let actionResult;
await act(async () => {
actionResult = await result.current.handleSlashCommand(
'/api/apiFunction/接口的实现',
);
});

expect(actionResult).toBe(false);
expect(mockAddItem).not.toHaveBeenCalled();
});

it('should display help for a parent command invoked without a subcommand', async () => {
const parentCommand: SlashCommand = {
name: 'parent',
Expand Down
8 changes: 7 additions & 1 deletion packages/cli/src/ui/hooks/slashCommandProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ import { FileCommandLoader } from '../../services/FileCommandLoader.js';
import { McpPromptLoader } from '../../services/McpPromptLoader.js';
import { SkillCommandLoader } from '../../services/SkillCommandLoader.js';
import { parseSlashCommand } from '../../utils/commands.js';
import { isBtwCommand } from '../utils/commandUtils.js';
import {
hasSlashCommandPathSeparator,
isBtwCommand,
} from '../utils/commandUtils.js';
import { clearScreen } from '../../utils/stdioHelpers.js';
import { useKeypress } from './useKeypress.js';
import {
Expand Down Expand Up @@ -448,6 +451,9 @@ export const useSlashCommandProcessor = (
if (!trimmed.startsWith('/') && !trimmed.startsWith('?')) {
return false;
}
if (trimmed.startsWith('/') && hasSlashCommandPathSeparator(trimmed)) {
Comment thread
yiliang114 marked this conversation as resolved.
Resolved

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch is unreachable in practice. Every caller of handleSlashCommand already gates on isSlashCommand(...) (useGeminiStream.ts:656, AppContainer.tsx:1257), and isSlashCommand itself returns false for path-like inputs after this PR.

Two options:

  1. Drop the check and rely on the caller-side gate.
  2. Keep it as a safety net but add a one-line comment explaining why it duplicates isSlashCommand's logic.

Without a comment, a future maintainer will likely "DRY it up" and remove it, possibly along with the safety net it was meant to provide.

return false;
}

const recordedItems: Array<Omit<HistoryItem, 'id'>> = [];
const recordItem = (item: Omit<HistoryItem, 'id'>) => {
Expand Down
9 changes: 9 additions & 0 deletions packages/cli/src/ui/utils/commandUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ describe('commandUtils', () => {
expect(isSlashCommand('/*\n * Multi-line comment\n */')).toBe(false);
expect(isSlashCommand('/*comment without space*/')).toBe(false);
});

it('should return false for slash-prefixed file paths', () => {
expect(isSlashCommand('/api/apiFunction/接口的实现')).toBe(false);
expect(isSlashCommand('/Users/me/project/src/index.ts')).toBe(false);
expect(isSlashCommand('/var/log/syslog check this')).toBe(false);
expect(isSlashCommand('/home/user/.qwen/settings.json')).toBe(false);
expect(isSlashCommand('/tmp/test.txt')).toBe(false);
expect(isSlashCommand('/tmp\\test.txt')).toBe(false);
});
});

describe('copyToClipboard', () => {
Expand Down
15 changes: 14 additions & 1 deletion packages/cli/src/ui/utils/commandUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,18 @@ export const isAtCommand = (query: string): boolean =>
// Check if starts with @ OR has a space, then @
query.startsWith('@') || /\s@/.test(query);

const SLASH_PATH_SEPARATOR_RE = /[/\\]/;

const getSlashCommandFirstToken = (query: string): string =>
query.slice(1).trimStart().split(/\s+/)[0] ?? '';
Comment thread
yiliang114 marked this conversation as resolved.
Resolved

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: String.prototype.split always returns a non-empty array, so [0] is always a string and the ?? '' never fires. Harmless, but slightly misleading — it suggests split could return [].


export const hasSlashCommandPathSeparator = (query: string): boolean =>
SLASH_PATH_SEPARATOR_RE.test(getSlashCommandFirstToken(query));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unstated precondition: getSlashCommandFirstToken calls query.slice(1) and silently strips the first character whether or not it's /. Today the only caller is hasSlashCommandPathSeparator, whose only reachable callers gate on startsWith('/') — so this is safe. But hasSlashCommandPathSeparator is exported, and a future caller passing 'foo/bar' would get back 'oo/bar' and a wrong answer.

Two cheap mitigations:

  • Add a JSDoc note ("expects a /-prefixed query") on hasSlashCommandPathSeparator, or
  • Have hasSlashCommandPathSeparator early-return false if !query.startsWith('/'), removing the precondition entirely.


/**
* Checks if a query string potentially represents an '/' command.
* It triggers if the query starts with '/' but excludes code comments like '//' and '/*'.
* It triggers if the query starts with '/' but excludes code comments like '//'
* and '/*', and file paths where the first token contains a path separator.
*
* @param query The input query string.
* @returns True if the query looks like an '/' command, false otherwise.
Expand All @@ -60,6 +69,10 @@ export const isSlashCommand = (query: string): boolean => {
return false;
}

if (hasSlashCommandPathSeparator(query)) {
return false;
}

return true;
};

Expand Down
Loading