| type CommandContext = { |
| reset: () => void | boolean | Promise<void | boolean>; |
| openHelp?: () => void; |
| toggleStatus?: () => void | Promise<void>; |
| toggleDocs?: () => void | Promise<void>; |
| focusComposer?: () => void; |
| setToast?: (message: string) => void; |
| }; |
|
|
| function normalize(input: string): string { |
| return String(input || "").trim().toLowerCase(); |
| } |
|
|
| function commandList(ctx: CommandContext): string { |
| const cmds = ["/reset", "/help", "/status", "/focus"]; |
| if (ctx.toggleDocs) cmds.push("/docs"); |
| return cmds.join(", "); |
| } |
|
|
| export async function runLocalChatCommand(rawMessage: string, ctx: CommandContext): Promise<boolean> { |
| const msg = normalize(rawMessage); |
| if (!msg.startsWith("/")) return false; |
|
|
| if (msg === "/reset") { |
| const didReset = await ctx.reset(); |
| if (didReset !== false) { |
| ctx.setToast?.("Conversation reset"); |
| } |
| return true; |
| } |
| if (msg === "/help") { |
| ctx.openHelp?.(); |
| return true; |
| } |
| if (msg === "/status") { |
| if (ctx.toggleStatus) { |
| await ctx.toggleStatus(); |
| return true; |
| } |
| ctx.setToast?.("Status panel is not available on this tab."); |
| return true; |
| } |
| if (msg === "/docs") { |
| if (ctx.toggleDocs) { |
| await ctx.toggleDocs(); |
| return true; |
| } |
| ctx.setToast?.("Docs panel is not available on this tab."); |
| return true; |
| } |
| if (msg === "/focus") { |
| if (ctx.focusComposer) { |
| ctx.focusComposer(); |
| return true; |
| } |
| ctx.setToast?.("Composer focus is not available on this tab."); |
| return true; |
| } |
| if (msg === "/commands") { |
| ctx.setToast?.(`Commands: ${commandList(ctx)}`); |
| return true; |
| } |
|
|
| ctx.setToast?.(`Unknown command. Try: ${commandList(ctx)}, /commands`); |
| return true; |
| } |
|
|