| 1 | import { ILLM } from "core"; |
| 2 | import { EXTENSION_NAME } from "core/control-plane/env"; |
| 3 | import * as vscode from "vscode"; |
| 4 | |
| 5 | import { Battery } from "../util/battery"; |
| 6 | import { getMetaKeyLabel } from "../util/util"; |
| 7 | import { |
| 8 | CONTINUE_WORKSPACE_KEY, |
| 9 | getContinueWorkspaceConfig, |
| 10 | } from "../util/workspaceConfig"; |
| 11 | |
| 12 | export enum StatusBarStatus { |
| 13 | Disabled, |
| 14 | Enabled, |
| 15 | Paused, |
| 16 | } |
| 17 | |
| 18 | export const quickPickStatusText = (status: StatusBarStatus | undefined) => { |
| 19 | switch (status) { |
| 20 | case undefined: |
| 21 | case StatusBarStatus.Disabled: |
| 22 | return "$(circle-slash) Disable autocomplete"; |
| 23 | case StatusBarStatus.Enabled: |
| 24 | return "$(check) Enable autocomplete"; |
| 25 | case StatusBarStatus.Paused: |
| 26 | return "$(debug-pause) Pause autocomplete"; |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | export const getStatusBarStatusFromQuickPickItemLabel = ( |
| 31 | label: string, |
| 32 | ): StatusBarStatus | undefined => { |
| 33 | switch (label) { |
| 34 | case "$(circle-slash) Disable autocomplete": |
| 35 | return StatusBarStatus.Disabled; |
| 36 | case "$(check) Enable autocomplete": |
| 37 | return StatusBarStatus.Enabled; |
| 38 | case "$(debug-pause) Pause autocomplete": |
| 39 | return StatusBarStatus.Paused; |
| 40 | default: |
| 41 | return undefined; |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | const statusBarItemText = ( |
| 46 | status: StatusBarStatus | undefined, |
| 47 | loading?: boolean, |
| 48 | error?: boolean, |
| 49 | ) => { |
| 50 | if (error) { |
| 51 | return "$(alert) Continue (config error)"; |
| 52 | } |
| 53 | |
| 54 | let text: string; |
| 55 | switch (status) { |
| 56 | case undefined: |
| 57 | if (loading) { |
| 58 | text = "$(loading~spin) Continue"; |
| 59 | } else { |
| 60 | text = "Continue"; |
| 61 | } |
| 62 | break; |
| 63 | case StatusBarStatus.Disabled: |
| 64 | text = "$(circle-slash) Continue"; |
| 65 | break; |
| 66 | case StatusBarStatus.Enabled: |
| 67 | text = "$(check) Continue"; |
| 68 | break; |
| 69 | case StatusBarStatus.Paused: |
| 70 | text = "$(debug-pause) Continue"; |
| 71 | break; |
| 72 | default: |
| 73 | text = "Continue"; |
| 74 | } |
| 75 | |
| 76 | // Append Next Edit indicator if enabled. |
| 77 | const config = vscode.workspace.getConfiguration(EXTENSION_NAME); |
| 78 | const nextEditEnabled = config.get<boolean>("enableNextEdit") ?? false; |
| 79 | if (nextEditEnabled) { |
| 80 | text += " (NE)"; |
| 81 | } |
| 82 | |
| 83 | return text; |
| 84 | }; |
| 85 | |
| 86 | const statusBarItemTooltip = (status: StatusBarStatus | undefined) => { |
| 87 | switch (status) { |
| 88 | case undefined: |
| 89 | case StatusBarStatus.Disabled: |
| 90 | return "Click to enable tab autocomplete"; |
| 91 | case StatusBarStatus.Enabled: |
| 92 | const config = vscode.workspace.getConfiguration(EXTENSION_NAME); |
| 93 | const nextEditEnabled = config.get<boolean>("enableNextEdit") ?? false; |
| 94 | return nextEditEnabled |
| 95 | ? "Next Edit is enabled" |
| 96 | : "Tab autocomplete is enabled"; |
| 97 | case StatusBarStatus.Paused: |
| 98 | return "Tab autocomplete is paused"; |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | let statusBarStatus: StatusBarStatus | undefined = undefined; |
| 103 | let statusBarItem: vscode.StatusBarItem | undefined = undefined; |
| 104 | let statusBarFalseTimeout: NodeJS.Timeout | undefined = undefined; |
| 105 | let statusBarError: boolean = false; |
| 106 | |
| 107 | export function stopStatusBarLoading() { |
| 108 | statusBarFalseTimeout = setTimeout(() => { |
| 109 | setupStatusBar(StatusBarStatus.Enabled, false); |
| 110 | }, 100); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * TODO: We should clean up how status bar is handled. |
| 115 | * Ideally, there should be a single 'status' value without |
| 116 | * 'loading' and 'error' booleans. |
| 117 | */ |
| 118 | export function setupStatusBar( |
| 119 | status: StatusBarStatus | undefined, |
| 120 | loading?: boolean, |
| 121 | error?: boolean, |
| 122 | ) { |
| 123 | if (loading !== false) { |
| 124 | clearTimeout(statusBarFalseTimeout); |
| 125 | statusBarFalseTimeout = undefined; |
| 126 | } |
| 127 | |
| 128 | // If statusBarItem hasn't been defined yet, create it |
| 129 | if (!statusBarItem) { |
| 130 | statusBarItem = vscode.window.createStatusBarItem( |
| 131 | vscode.StatusBarAlignment.Right, |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | if (error !== undefined) { |
| 136 | statusBarError = error; |
| 137 | |
| 138 | if (status === undefined) { |
| 139 | status = statusBarStatus; |
| 140 | } |
| 141 | |
| 142 | if (loading === undefined) { |
| 143 | loading = loading; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | statusBarItem.text = statusBarItemText(status, loading, statusBarError); |
| 148 | statusBarItem.tooltip = statusBarItemTooltip(status ?? statusBarStatus); |
| 149 | statusBarItem.command = "continue.openTabAutocompleteConfigMenu"; |
| 150 | |
| 151 | statusBarItem.show(); |
| 152 | if (status !== undefined) { |
| 153 | statusBarStatus = status; |
| 154 | } |
| 155 | |
| 156 | vscode.workspace.onDidChangeConfiguration((event) => { |
| 157 | if (event.affectsConfiguration(CONTINUE_WORKSPACE_KEY)) { |
| 158 | const enabled = getContinueWorkspaceConfig().get<boolean>( |
| 159 | "enableTabAutocomplete", |
| 160 | ); |
| 161 | if (enabled && statusBarStatus === StatusBarStatus.Paused) { |
| 162 | return; |
| 163 | } |
| 164 | setupStatusBar( |
| 165 | enabled ? StatusBarStatus.Enabled : StatusBarStatus.Disabled, |
| 166 | ); |
| 167 | } |
| 168 | }); |
| 169 | } |
| 170 | |
| 171 | export function getStatusBarStatus(): StatusBarStatus | undefined { |
| 172 | return statusBarStatus; |
| 173 | } |
| 174 | |
| 175 | export function monitorBatteryChanges(battery: Battery): vscode.Disposable { |
| 176 | return battery.onChangeAC((acConnected: boolean) => { |
| 177 | const config = vscode.workspace.getConfiguration(EXTENSION_NAME); |
| 178 | const enabled = config.get<boolean>("enableTabAutocomplete"); |
| 179 | if (!!enabled) { |
| 180 | const pauseOnBattery = config.get<boolean>( |
| 181 | "pauseTabAutocompleteOnBattery", |
| 182 | ); |
| 183 | setupStatusBar( |
| 184 | acConnected || !pauseOnBattery |
| 185 | ? StatusBarStatus.Enabled |
| 186 | : StatusBarStatus.Paused, |
| 187 | ); |
| 188 | } |
| 189 | }); |
| 190 | } |
| 191 | |
| 192 | export function getAutocompleteStatusBarDescription( |
| 193 | selected: string | undefined, |
| 194 | { title, apiKey, providerName }: ILLM, |
| 195 | ): string | undefined { |
| 196 | if (title !== selected) { |
| 197 | return undefined; |
| 198 | } |
| 199 | |
| 200 | let description = "Current autocomplete model"; |
| 201 | |
| 202 | // Only set for Mistral since our default config includes Codestral without |
| 203 | // an API key |
| 204 | if ((apiKey === undefined || apiKey === "") && providerName === "mistral") { |
| 205 | description += " (Missing API key)"; |
| 206 | } |
| 207 | |
| 208 | return description; |
| 209 | } |
| 210 | |
| 211 | export function getAutocompleteStatusBarTitle( |
| 212 | selected: string | undefined, |
| 213 | { title }: ILLM, |
| 214 | ): string { |
| 215 | if (!title) { |
| 216 | return "Unnamed Model"; |
| 217 | } |
| 218 | |
| 219 | if (title === selected) { |
| 220 | return `$(check) ${title}`; |
| 221 | } |
| 222 | |
| 223 | return title; |
| 224 | } |
| 225 | |
| 226 | const USE_FIM_MENU_ITEM_LABEL = "$(export) Use FIM autocomplete over Next Edit"; |
| 227 | const USE_NEXT_EDIT_MENU_ITEM_LABEL = |
| 228 | "$(sparkle) Use Next Edit over FIM autocomplete"; |
| 229 | |
| 230 | // Shows what items get rendered in the autocomplete menu. |
| 231 | export function getNextEditMenuItems( |
| 232 | currentStatus: StatusBarStatus | undefined, |
| 233 | nextEditEnabled: boolean, |
| 234 | ): vscode.QuickPickItem[] { |
| 235 | if (currentStatus !== StatusBarStatus.Enabled) return []; |
| 236 | |
| 237 | return [ |
| 238 | { |
| 239 | label: nextEditEnabled |
| 240 | ? USE_FIM_MENU_ITEM_LABEL |
| 241 | : USE_NEXT_EDIT_MENU_ITEM_LABEL, |
| 242 | description: getMetaKeyLabel() + " + K, " + getMetaKeyLabel() + " + N", |
| 243 | }, |
| 244 | ]; |
| 245 | } |
| 246 | |
| 247 | // Checks if the current selected option is a Next Edit toggle label. |
| 248 | export function isNextEditToggleLabel(label: string): boolean { |
| 249 | return ( |
| 250 | label === USE_FIM_MENU_ITEM_LABEL || label === USE_NEXT_EDIT_MENU_ITEM_LABEL |
| 251 | ); |
| 252 | } |
| 253 | |
| 254 | // Updates the config once Next Edit is toggled. |
| 255 | export function handleNextEditToggle( |
| 256 | label: string, |
| 257 | config: vscode.WorkspaceConfiguration, |
| 258 | ) { |
| 259 | const isEnabling = label === USE_NEXT_EDIT_MENU_ITEM_LABEL; |
| 260 | |
| 261 | config.update( |
| 262 | "enableNextEdit", |
| 263 | isEnabling, |
| 264 | vscode.ConfigurationTarget.Global, |
| 265 | ); |
| 266 | } |