history.ts 853 B

123456789101112131415161718192021222324
  1. import type { TuiPromptInput } from "@zcode/tui";
  2. import type { SlashCommand } from "./slash-command-types.js";
  3. import type { CommandCenterDeps } from "./types.js";
  4. const API_KEY_LOGIN_PATTERN = /(?:^|\s)(?:bigmodel|zai)-coding-plan-api-key(?:\s|$)/u;
  5. export async function recordSlashCommandInHistory(
  6. deps: CommandCenterDeps,
  7. input: TuiPromptInput,
  8. command: SlashCommand,
  9. ): Promise<void> {
  10. if (!deps.recordInputHistory || !shouldRecordSlashCommand(command)) return;
  11. try {
  12. await deps.recordInputHistory(input, "slash_command");
  13. } catch {
  14. // Input history is recall UX; command execution must not depend on it.
  15. }
  16. }
  17. function shouldRecordSlashCommand(command: SlashCommand): boolean {
  18. if (command.type !== "known") return true;
  19. if (command.name !== "login") return true;
  20. return !API_KEY_LOGIN_PATTERN.test(command.args);
  21. }