|
| 1 | +export const LOGIN_HELP_TEXT = `autoctx login — Store provider credentials persistently |
| 2 | +
|
| 3 | +Usage: autoctx login [options] |
| 4 | +
|
| 5 | +Options: |
| 6 | + --provider <type> Provider name: anthropic, openai, gemini, ollama, groq, etc. |
| 7 | + --key <api-key> API key (omit to be prompted interactively) |
| 8 | + --model <name> Default model for this provider |
| 9 | + --base-url <url> Custom base URL (for Ollama, vLLM, proxies) |
| 10 | + --config-dir <path> Config directory (default: ~/.config/autoctx) |
| 11 | +
|
| 12 | +Without flags, prompts interactively for provider and key. |
| 13 | +Keys starting with ! are executed as shell commands (e.g. !security find-generic-password). |
| 14 | +
|
| 15 | +Examples: |
| 16 | + autoctx login --provider anthropic --key YOUR_ANTHROPIC_API_KEY |
| 17 | + autoctx login --provider ollama --base-url http://localhost:11434 |
| 18 | + autoctx login # interactive prompt |
| 19 | +
|
| 20 | +See also: whoami, logout, providers, models`; |
| 21 | + |
| 22 | +export const LOGOUT_HELP_TEXT = [ |
| 23 | + "autoctx logout [--config-dir <path>]", |
| 24 | + "Clears stored provider credentials.", |
| 25 | +].join("\n"); |
| 26 | + |
| 27 | +export interface LoginCommandValues { |
| 28 | + provider?: string; |
| 29 | + key?: string; |
| 30 | + model?: string; |
| 31 | + "base-url"?: string; |
| 32 | + "config-dir"?: string; |
| 33 | +} |
| 34 | + |
| 35 | +export interface ResolvedLoginCommand { |
| 36 | + provider: string; |
| 37 | + apiKey?: string; |
| 38 | + model?: string; |
| 39 | + baseUrl?: string; |
| 40 | + configDir?: string; |
| 41 | +} |
| 42 | + |
| 43 | +export interface ProviderSummary { |
| 44 | + provider: string; |
| 45 | + hasApiKey: boolean; |
| 46 | + source?: "stored" | "env"; |
| 47 | + model?: string; |
| 48 | + baseUrl?: string; |
| 49 | + savedAt?: string; |
| 50 | +} |
| 51 | + |
| 52 | +export interface KnownProviderSummary { |
| 53 | + id: string; |
| 54 | + displayName: string; |
| 55 | + requiresKey: boolean; |
| 56 | +} |
| 57 | + |
| 58 | +export async function resolveLoginCommandRequest( |
| 59 | + values: LoginCommandValues, |
| 60 | + deps: { |
| 61 | + promptForValue(label: string): Promise<string>; |
| 62 | + normalizeOllamaBaseUrl(baseUrl?: string): string; |
| 63 | + validateOllamaConnection(baseUrl: string): Promise<void>; |
| 64 | + env: Record<string, string | undefined>; |
| 65 | + }, |
| 66 | +): Promise<ResolvedLoginCommand> { |
| 67 | + let provider = values.provider?.trim(); |
| 68 | + if (!provider) { |
| 69 | + provider = await deps.promptForValue("Provider"); |
| 70 | + } |
| 71 | + if (!provider) { |
| 72 | + throw new Error("Error: provider is required"); |
| 73 | + } |
| 74 | + provider = provider.toLowerCase(); |
| 75 | + |
| 76 | + let apiKey = values.key?.trim(); |
| 77 | + let baseUrl = values["base-url"]?.trim(); |
| 78 | + const model = values.model?.trim(); |
| 79 | + |
| 80 | + if (provider === "ollama") { |
| 81 | + baseUrl = deps.normalizeOllamaBaseUrl( |
| 82 | + baseUrl ?? |
| 83 | + deps.env.AUTOCONTEXT_AGENT_BASE_URL ?? |
| 84 | + deps.env.AUTOCONTEXT_BASE_URL ?? |
| 85 | + "http://localhost:11434", |
| 86 | + ); |
| 87 | + await deps.validateOllamaConnection(baseUrl); |
| 88 | + } else { |
| 89 | + if (!apiKey) { |
| 90 | + apiKey = await deps.promptForValue("API key"); |
| 91 | + } |
| 92 | + if (!apiKey) { |
| 93 | + throw new Error("Error: --key is required for this provider"); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return { |
| 98 | + provider, |
| 99 | + apiKey, |
| 100 | + model, |
| 101 | + baseUrl, |
| 102 | + configDir: values["config-dir"]?.trim() || undefined, |
| 103 | + }; |
| 104 | +} |
| 105 | + |
| 106 | +export function buildStoredProviderCredentials(request: { |
| 107 | + apiKey?: string; |
| 108 | + model?: string; |
| 109 | + baseUrl?: string; |
| 110 | +}): Record<string, string> { |
| 111 | + const creds: Record<string, string> = {}; |
| 112 | + if (request.apiKey) creds.apiKey = request.apiKey; |
| 113 | + if (request.model) creds.model = request.model; |
| 114 | + if (request.baseUrl) creds.baseUrl = request.baseUrl; |
| 115 | + return creds; |
| 116 | +} |
| 117 | + |
| 118 | +export function buildLoginSuccessMessage(request: { |
| 119 | + provider: string; |
| 120 | + baseUrl?: string; |
| 121 | +}): string { |
| 122 | + if (request.provider === "ollama") { |
| 123 | + return `Connected to Ollama at ${request.baseUrl}`; |
| 124 | + } |
| 125 | + return `Credentials saved for ${request.provider}`; |
| 126 | +} |
| 127 | + |
| 128 | +export function buildWhoamiPayload(input: { |
| 129 | + provider: string; |
| 130 | + model: string; |
| 131 | + authenticated: boolean; |
| 132 | + baseUrl?: string; |
| 133 | + configuredProviders: ProviderSummary[]; |
| 134 | +}): { |
| 135 | + provider: string; |
| 136 | + model: string; |
| 137 | + authenticated: boolean; |
| 138 | + baseUrl?: string; |
| 139 | + configuredProviders?: ProviderSummary[]; |
| 140 | +} { |
| 141 | + return { |
| 142 | + provider: input.provider, |
| 143 | + model: input.model, |
| 144 | + authenticated: input.authenticated, |
| 145 | + ...(input.baseUrl ? { baseUrl: input.baseUrl } : {}), |
| 146 | + ...(input.configuredProviders.length > 0 |
| 147 | + ? { configuredProviders: input.configuredProviders } |
| 148 | + : {}), |
| 149 | + }; |
| 150 | +} |
| 151 | + |
| 152 | +export function buildProvidersPayload( |
| 153 | + knownProviders: KnownProviderSummary[], |
| 154 | + discoveredProviders: ProviderSummary[], |
| 155 | +): Array<{ |
| 156 | + id: string; |
| 157 | + displayName: string; |
| 158 | + requiresKey: boolean; |
| 159 | + authenticated: boolean; |
| 160 | + source?: "stored" | "env"; |
| 161 | + model?: string; |
| 162 | + baseUrl?: string; |
| 163 | +}> { |
| 164 | + const discoveredMap = new Map(discoveredProviders.map((provider) => [provider.provider, provider])); |
| 165 | + return knownProviders.map((provider) => { |
| 166 | + const discovered = discoveredMap.get(provider.id); |
| 167 | + return { |
| 168 | + id: provider.id, |
| 169 | + displayName: provider.displayName, |
| 170 | + requiresKey: provider.requiresKey, |
| 171 | + authenticated: discovered |
| 172 | + ? discovered.hasApiKey || !provider.requiresKey |
| 173 | + : !provider.requiresKey, |
| 174 | + ...(discovered?.source ? { source: discovered.source } : {}), |
| 175 | + ...(discovered?.model ? { model: discovered.model } : {}), |
| 176 | + ...(discovered?.baseUrl ? { baseUrl: discovered.baseUrl } : {}), |
| 177 | + }; |
| 178 | + }); |
| 179 | +} |
| 180 | + |
| 181 | +export function renderModelsResult(models: unknown[]): string[] { |
| 182 | + if (models.length === 0) { |
| 183 | + return [ |
| 184 | + JSON.stringify([]), |
| 185 | + "\nNo authenticated providers found. Run `autoctx login` to configure a provider.", |
| 186 | + ]; |
| 187 | + } |
| 188 | + return [JSON.stringify(models, null, 2)]; |
| 189 | +} |
| 190 | + |
| 191 | +export function buildLogoutMessage(existingProvider?: string): string { |
| 192 | + return existingProvider ? `Logged out from ${existingProvider}` : "Logged out."; |
| 193 | +} |
0 commit comments