command-center-custom.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import type { TuiSlashCommandSuggestion } from "@zcode/tui";
  2. import { expandCliCustomCommandPrompt } from "./custom-command-expand.js";
  3. const customCommandNotFoundPattern = /not found/i;
  4. export type CommandCenterCustomCommandListOutcome = {
  5. commands: CommandCenterCustomCommand[];
  6. diagnostics: unknown[];
  7. totalDiscovered: number;
  8. };
  9. export type CommandCenterCustomCommand = {
  10. allowedTools?: string[];
  11. argumentHint?: string;
  12. description: string;
  13. disableNonInteractive?: boolean;
  14. frontmatterKeys?: string[];
  15. model?: string;
  16. name: string;
  17. path: string;
  18. plugin?: { dataPath: string; id: string; name: string; rootPath: string };
  19. rootPath?: string;
  20. scope: string;
  21. skills?: string[];
  22. source: string;
  23. };
  24. export type CommandCenterCustomCommandContent = {
  25. bytesRead?: number;
  26. content: string;
  27. metadata: CommandCenterCustomCommand;
  28. sizeBytes?: number;
  29. truncated?: boolean;
  30. };
  31. interface CommandCenterCustomCommandDeps {
  32. listCustomCommands?: () => Promise<CommandCenterCustomCommandListOutcome>;
  33. loadCustomCommand?: (name: string) => Promise<CommandCenterCustomCommandContent>;
  34. }
  35. export function listCustomCommandSuggestions(
  36. customCommands?: CommandCenterCustomCommandListOutcome,
  37. ): TuiSlashCommandSuggestion[] {
  38. return (customCommands?.commands ?? []).map((command) => ({
  39. name: command.name,
  40. summary: `${command.description} (${command.scope}/${command.source})`,
  41. usage: `/${command.name}${command.argumentHint ? ` ${command.argumentHint}` : ""}`,
  42. }));
  43. }
  44. export function findCustomCommandHelpEntry(
  45. name: string,
  46. customCommands?: CommandCenterCustomCommandListOutcome,
  47. ): CommandCenterCustomCommand | undefined {
  48. return customCommands?.commands.find((command) => command.name === name);
  49. }
  50. export function formatCustomCommandHelpEntry(command: CommandCenterCustomCommand): string {
  51. return [
  52. `/${command.name}${command.argumentHint ? ` ${command.argumentHint}` : ""}`,
  53. command.description,
  54. `Source: ${command.scope}/${command.source}`,
  55. `Path: ${command.path}`,
  56. ].join("\n");
  57. }
  58. export function formatAvailableCommandNames(
  59. builtInCommands: readonly string[],
  60. customCommands?: CommandCenterCustomCommandListOutcome,
  61. ): string {
  62. const names = [
  63. ...builtInCommands,
  64. ...(customCommands?.commands.map((command) => `/${command.name}`) ?? []),
  65. ];
  66. return names.join(", ");
  67. }
  68. export async function buildCustomCommandPrompt(
  69. name: string,
  70. args: string,
  71. deps: CommandCenterCustomCommandDeps,
  72. ): Promise<string | undefined> {
  73. if (!deps.loadCustomCommand) return undefined;
  74. try {
  75. const command = await deps.loadCustomCommand(name);
  76. return expandCliCustomCommandPrompt({ args, command }).prompt;
  77. } catch (error) {
  78. if (error instanceof Error && customCommandNotFoundPattern.test(error.message)) {
  79. return undefined;
  80. }
  81. throw error;
  82. }
  83. }
  84. export async function listCustomCommandsForHelp(
  85. deps: CommandCenterCustomCommandDeps,
  86. ): Promise<CommandCenterCustomCommandListOutcome | undefined> {
  87. if (!deps.listCustomCommands) return undefined;
  88. try {
  89. return await deps.listCustomCommands();
  90. } catch {
  91. return undefined;
  92. }
  93. }