| 1 | import { type AssistantConfig } from "@continuedev/sdk"; |
| 2 | import chalk from "chalk"; |
| 3 | |
| 4 | import { |
| 5 | isAuthenticated, |
| 6 | isAuthenticatedConfig, |
| 7 | loadAuthConfig, |
| 8 | } from "./auth/workos.js"; |
| 9 | import { getAllSlashCommands } from "./commands/commands.js"; |
| 10 | import { reloadService, SERVICE_NAMES, services } from "./services/index.js"; |
| 11 | import { posthogService } from "./telemetry/posthogService.js"; |
| 12 | import { SlashCommandResult } from "./ui/hooks/useChat.types.js"; |
| 13 | |
| 14 | type CommandHandler = ( |
| 15 | args: string[], |
| 16 | assistant: AssistantConfig, |
| 17 | ) => Promise<SlashCommandResult> | SlashCommandResult; |
| 18 | |
| 19 | async function handleHelp(args: string[], assistant: AssistantConfig) { |
| 20 | const allCommands = getAllSlashCommands(assistant); |
| 21 | const helpMessage = [ |
| 22 | "Slash commands:", |
| 23 | ...allCommands |
| 24 | .sort((a, b) => a.name.localeCompare(b.name)) |
| 25 | .map( |
| 26 | (cmd) => |
| 27 | `- ${chalk.white(`/${cmd.name}:`)} ${chalk.gray(cmd.description)}`, |
| 28 | ), |
| 29 | ].join("\n"); |
| 30 | posthogService.capture("useSlashCommand", { name: "help" }); |
| 31 | return { output: helpMessage }; |
| 32 | } |
| 33 | |
| 34 | async function handleLogin() { |
| 35 | posthogService.capture("useSlashCommand", { name: "login" }); |
| 36 | try { |
| 37 | const newAuthState = await services.auth.login(); |
| 38 | await reloadService(SERVICE_NAMES.AUTH); |
| 39 | |
| 40 | const userInfo = |
| 41 | newAuthState.authConfig && isAuthenticatedConfig(newAuthState.authConfig) |
| 42 | ? newAuthState.authConfig.userEmail || newAuthState.authConfig.userId |
| 43 | : "user"; |
| 44 | |
| 45 | console.info(chalk.green(`\nLogged in as ${userInfo}`)); |
| 46 | |
| 47 | return { |
| 48 | exit: false, |
| 49 | output: "Login successful! All services updated automatically.", |
| 50 | }; |
| 51 | } catch (error: any) { |
| 52 | console.error(chalk.red(`\nLogin failed: ${error.message}`)); |
| 53 | return { |
| 54 | exit: false, |
| 55 | output: `Login failed: ${error.message}`, |
| 56 | }; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | async function handleLogout() { |
| 61 | posthogService.capture("useSlashCommand", { name: "logout" }); |
| 62 | try { |
| 63 | await services.auth.logout(); |
| 64 | return { |
| 65 | exit: true, |
| 66 | output: "Logged out successfully", |
| 67 | }; |
| 68 | } catch { |
| 69 | return { |
| 70 | exit: true, |
| 71 | output: "Logged out successfully", |
| 72 | }; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | function handleWhoami() { |
| 77 | posthogService.capture("useSlashCommand", { name: "whoami" }); |
| 78 | if (isAuthenticated()) { |
| 79 | const config = loadAuthConfig(); |
| 80 | if (config && isAuthenticatedConfig(config)) { |
| 81 | return { |
| 82 | exit: false, |
| 83 | output: `Logged in as ${config.userEmail || config.userId}`, |
| 84 | }; |
| 85 | } else { |
| 86 | return { |
| 87 | exit: false, |
| 88 | output: "Authenticated via environment variable", |
| 89 | }; |
| 90 | } |
| 91 | } else { |
| 92 | return { |
| 93 | exit: false, |
| 94 | output: "Not logged in. Use /login to authenticate.", |
| 95 | }; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | const commandHandlers: Record<string, CommandHandler> = { |
| 100 | help: handleHelp, |
| 101 | clear: () => { |
| 102 | posthogService.capture("useSlashCommand", { name: "clear" }); |
| 103 | return { clear: true, output: "Chat history cleared" }; |
| 104 | }, |
| 105 | exit: () => { |
| 106 | posthogService.capture("useSlashCommand", { name: "exit" }); |
| 107 | return { exit: true, output: "Goodbye!" }; |
| 108 | }, |
| 109 | config: () => { |
| 110 | posthogService.capture("useSlashCommand", { name: "config" }); |
| 111 | return { openConfigSelector: true }; |
| 112 | }, |
| 113 | login: handleLogin, |
| 114 | logout: handleLogout, |
| 115 | whoami: handleWhoami, |
| 116 | model: () => ({ openModelSelector: true }), |
| 117 | compact: () => { |
| 118 | posthogService.capture("useSlashCommand", { name: "compact" }); |
| 119 | return { compact: true }; |
| 120 | }, |
| 121 | mcp: () => { |
| 122 | posthogService.capture("useSlashCommand", { name: "mcp" }); |
| 123 | return { openMcpSelector: true }; |
| 124 | }, |
| 125 | resume: () => { |
| 126 | posthogService.capture("useSlashCommand", { name: "resume" }); |
| 127 | return { openSessionSelector: true }; |
| 128 | }, |
| 129 | }; |
| 130 | |
| 131 | export async function handleSlashCommands( |
| 132 | input: string, |
| 133 | assistant: AssistantConfig, |
| 134 | ): Promise<{ |
| 135 | output?: string; |
| 136 | exit?: boolean; |
| 137 | newInput?: string; |
| 138 | clear?: boolean; |
| 139 | openConfigSelector?: boolean; |
| 140 | openModelSelector?: boolean; |
| 141 | openMCPSelector?: boolean; |
| 142 | openSessionSelector?: boolean; |
| 143 | compact?: boolean; |
| 144 | } | null> { |
| 145 | // Only trigger slash commands if slash is the very first character |
| 146 | if (!input.startsWith("/") || !input.trim().startsWith("/")) { |
| 147 | return null; |
| 148 | } |
| 149 | |
| 150 | const [command, ...args] = input.slice(1).split(" "); |
| 151 | |
| 152 | const handler = commandHandlers[command]; |
| 153 | if (handler) { |
| 154 | return await handler(args, assistant); |
| 155 | } |
| 156 | |
| 157 | // Check for custom assistant prompts |
| 158 | const assistantPrompt = assistant.prompts?.find( |
| 159 | (prompt) => prompt?.name === command, |
| 160 | ); |
| 161 | if (assistantPrompt) { |
| 162 | const newInput = assistantPrompt.prompt + args.join(" "); |
| 163 | return { newInput }; |
| 164 | } |
| 165 | |
| 166 | return { output: `Unknown command: ${command}` }; |
| 167 | } |