qwen-code/packages/web-shell/client/utils/slash-command-action.ts
ytahdn 3586f48171
feat(web-shell): support custom slash command actions (#7267)
* feat(web-shell): support custom slash command actions

* fix(web-shell): apply slash handlers consistently

---------

Co-authored-by: 钉萁 <dingqi.jww@alibaba-inc.com>
Co-authored-by: 易良 <1204183885@qq.com>
2026-07-20 11:23:55 +00:00

25 lines
695 B
TypeScript

import type { WebShellSlashCommandHandler } from '../App';
export const SLASH_COMMAND_PATTERN = /^\/([\w-]+)(?=\s|$)/;
export function invokeSlashCommandHandler(
input: string,
handler: WebShellSlashCommandHandler | undefined,
reportError: (error: unknown, fallback: string) => void,
): boolean {
if (!handler) return false;
const match = input.match(SLASH_COMMAND_PATTERN);
if (!match) return false;
try {
return (
handler({
command: match[1].toLowerCase(),
args: input.slice(match[0].length).trim(),
input,
}) === true
);
} catch (error: unknown) {
reportError(error, 'onSlashCommand callback failed');
return false;
}
}