omp-multi-auth 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,109 @@
1
+ # omp-multi-auth
2
+
3
+ Multi-account OAuth login for [omp (Oh My Pi)](https://github.com/can1357/oh-my-pi). Add, authenticate, and switch between multiple accounts per provider. OMP's built-in fallback handles rate-limit retry and model fallback.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ omp install npm:omp-multi-auth
9
+ ```
10
+
11
+ Or install from GitHub:
12
+
13
+ ```bash
14
+ omp install git:github.com/tuandinh0801/omp-multi-auth
15
+ ```
16
+
17
+ ## Features
18
+
19
+ - Multiple OAuth accounts per provider
20
+ - Switch active account with `/multi-auth switch`
21
+ - Cross-provider model presets with `/multi-auth-preset`
22
+ - Built-in quota checks with `/multi-auth limits`
23
+ - Project affinity through `.omp/multi-auth.json` and `allowedSubs`
24
+ - Labels for organizing accounts
25
+ - Interactive TUI management
26
+
27
+ ## Commands
28
+
29
+ ### `/multi-auth`
30
+
31
+ | Command | Description |
32
+ |---|---|
33
+ | `/multi-auth` | Open account management menu |
34
+ | `/multi-auth list` | List configured accounts |
35
+ | `/multi-auth add` | Add an account |
36
+ | `/multi-auth remove` | Remove an account |
37
+ | `/multi-auth login` | Authenticate an account |
38
+ | `/multi-auth logout` | Sign out an account |
39
+ | `/multi-auth switch` | Switch active account/provider |
40
+ | `/multi-auth status` | Show account and authentication status |
41
+ | `/multi-auth limits` | Check provider quota and usage |
42
+
43
+ ### `/multi-auth-preset`
44
+
45
+ | Command | Description |
46
+ |---|---|
47
+ | `/multi-auth-preset` | Open preset menu |
48
+ | `/multi-auth-preset activate` | Activate a preset's best available entry |
49
+ | `/multi-auth-preset <name>` | Activate preset by name |
50
+ | `/multi-auth-preset create` | Create a preset |
51
+ | `/multi-auth-preset list` | List presets |
52
+ | `/multi-auth-preset toggle` | Enable or disable a preset |
53
+ | `/multi-auth-preset remove` | Delete a preset |
54
+
55
+ Presets select models across providers. On rate limits, requests fall through to OMP's built-in fallback.
56
+
57
+ ## Project-level configuration
58
+
59
+ Create `.omp/multi-auth.json` in a project to restrict which subscription provider names are available. `allowedSubs` is an exact allow-list; omit it to allow all configured accounts.
60
+
61
+ ```json
62
+ {
63
+ "allowedSubs": ["openai-codex-2", "anthropic-2"]
64
+ }
65
+ ```
66
+
67
+ ## Supported providers
68
+
69
+ ### OAuth
70
+
71
+ | Provider | Service |
72
+ |---|---|
73
+ | `anthropic` | Anthropic (Claude Pro/Max) |
74
+ | `openai-codex` | ChatGPT Plus/Pro (Codex) |
75
+ | `github-copilot` | GitHub Copilot |
76
+ | `google-gemini-cli` | Google Cloud Code Assist |
77
+ | `google-antigravity` | Antigravity |
78
+ | `kimi-code` | Kimi Code |
79
+ | `xai-oauth` | xAI Grok OAuth |
80
+
81
+ ### API key
82
+
83
+ | Provider | Service | Environment variable |
84
+ |---|---|---|
85
+ | `minimax` | MiniMax (Global) | `$MINIMAX_API_KEY` |
86
+ | `minimax-cn` | MiniMax (China) | `$MINIMAX_CN_API_KEY` |
87
+
88
+ MiniMax providers use API keys, with one key per environment; they do not support multiple accounts. Cursor and GitLab Duo require custom handlers and are not exposed.
89
+
90
+ ## Built-in limits
91
+
92
+ Run `/multi-auth limits` to inspect quota and usage information for supported providers. This currently includes built-in quota checks for Codex and Google providers. Results depend on provider availability and authenticated account state.
93
+
94
+ ## Configuration files
95
+
96
+ | Scope | Path | Contents |
97
+ |---|---|---|
98
+ | Global | `~/.omp/agent/multi-auth.json` | Subscriptions and presets |
99
+ | Project | `.omp/multi-auth.json` | `allowedSubs` allow-list |
100
+
101
+ ## Environment variable
102
+
103
+ `MULTI_SUB` identifies the subscription provider name used by the extension when configured by omp.
104
+
105
+ Originally based on [`pi-multi-pass`](https://github.com/hjanuschka/pi-multi-pass).
106
+
107
+ ## License
108
+
109
+ MIT
@@ -0,0 +1,286 @@
1
+ // ========================================================================
2
+ // /multi-auth-preset command handlers
3
+ // ========================================================================
4
+ import type { ExtensionAPI, ExtensionCommandContext, ExtensionContext, AuthStorage } from "@oh-my-pi/pi-coding-agent";
5
+ import type { Api, Model } from "@oh-my-pi/pi-ai";
6
+ import { getAuthStorage, getModels, type MultiAuthConfig, type PresetEntry, type PresetConfig, type SubEntry, subProviderName } from "./core.ts";
7
+ import { loadGlobalConfig, saveGlobalConfig, findSelectableModelForProvider, parseEnvConfig, normalizeEntries, mergeConfigs, getProviderDisplayName } from "./config.ts";
8
+ import { subDisplayName, getBaseProvider, SUPPORTED_PROVIDERS, PROVIDER_TEMPLATES } from "./providers.ts";
9
+ import { showWrappedSelect } from "./ui.ts";
10
+ import type { SelectItem } from "@oh-my-pi/pi-tui";
11
+
12
+ export function formatPresetEntry(entry: PresetEntry): string {
13
+ const config = loadGlobalConfig();
14
+ const envEntries = parseEnvConfig();
15
+ const allSubs = normalizeEntries(mergeConfigs(config, envEntries));
16
+ const displayName = getProviderDisplayName(entry.provider, allSubs);
17
+ return `${displayName} / ${entry.model}`;
18
+ }
19
+
20
+ /** Lightweight version that takes pre-loaded subs to avoid re-reading config per entry. */
21
+ export function formatPresetEntryWith(entry: PresetEntry, allSubs: SubEntry[]): string {
22
+ const displayName = getProviderDisplayName(entry.provider, allSubs);
23
+ return `${displayName} / ${entry.model}`;
24
+ }
25
+
26
+ export async function handlePresetCreate(
27
+ ctx: ExtensionCommandContext,
28
+ ): Promise<void> {
29
+ const presetName = await ctx.ui.input("Preset name", "e.g. coding-premium, coding-budget, fastest");
30
+ if (!presetName?.trim()) return;
31
+
32
+ const config = loadGlobalConfig();
33
+ if (config.presets.find((p) => p.name === presetName.trim())) {
34
+ const overwrite = await ctx.ui.confirm("Preset exists", `Overwrite "${presetName.trim()}"?`);
35
+ if (!overwrite) return;
36
+ }
37
+
38
+ const envEntries = parseEnvConfig();
39
+ const allSubs = normalizeEntries(mergeConfigs(config, envEntries));
40
+ const allProviders: string[] = [];
41
+ for (const provider of SUPPORTED_PROVIDERS) {
42
+ allProviders.push(provider);
43
+ }
44
+ for (const entry of allSubs) {
45
+ allProviders.push(subProviderName(entry));
46
+ }
47
+
48
+ const entries: PresetEntry[] = [];
49
+ let adding = true;
50
+ while (adding) {
51
+ const providerOptions = [
52
+ `--- Entries (${entries.length}): ${entries.map((e) => formatPresetEntryWith(e, allSubs)).join(", ") || "none"} ---`,
53
+ ...allProviders.map((p) => {
54
+ const template = PROVIDER_TEMPLATES[p];
55
+ const display = template?.displayName || p;
56
+ const sub = allSubs.find((s) => subProviderName(s) === p);
57
+ const label = sub ? subDisplayName(sub) : display;
58
+ return `${p} -- ${label}`;
59
+ }),
60
+ "[Done - save preset]",
61
+ ];
62
+
63
+ const picked = await ctx.ui.select("Add entry (Esc cancels)", providerOptions);
64
+ if (!picked) return;
65
+ if (picked.startsWith("---")) continue;
66
+ if (picked === "[Done - save preset]") {
67
+ if (entries.length === 0) {
68
+ ctx.ui.notify("Add at least one entry.", "warning");
69
+ continue;
70
+ }
71
+ adding = false;
72
+ continue;
73
+ }
74
+
75
+ const provider = picked.split(" -- ")[0].trim();
76
+ const base = getBaseProvider(provider);
77
+ if (!base) continue;
78
+
79
+ const models = (getModels(base as any) as Model<Api>[]).map((m) => m.id);
80
+ if (models.length === 0) {
81
+ ctx.ui.notify(`No models available for ${provider}.`, "warning");
82
+ continue;
83
+ }
84
+
85
+ const model = await ctx.ui.select(`Model for ${provider}`, models);
86
+ if (!model) continue;
87
+
88
+ entries.push({ provider, model, enabled: true });
89
+ }
90
+
91
+ const preset: PresetConfig = {
92
+ name: presetName.trim(),
93
+ entries,
94
+ enabled: true,
95
+ };
96
+ const existingIdx = config.presets.findIndex((p) => p.name === preset.name);
97
+ if (existingIdx >= 0) {
98
+ config.presets[existingIdx] = preset;
99
+ } else {
100
+ config.presets.push(preset);
101
+ }
102
+ saveGlobalConfig(config);
103
+ ctx.ui.notify(
104
+ `Preset "${preset.name}" saved with ${entries.length} ${entries.length === 1 ? "entry" : "entries"}: ${entries.map((e) => formatPresetEntryWith(e, allSubs)).join(", ")}`,
105
+ "info",
106
+ );
107
+ }
108
+
109
+ export async function handlePresetList(ctx: ExtensionCommandContext): Promise<void> {
110
+ const config = loadGlobalConfig();
111
+ if (config.presets.length === 0) {
112
+ ctx.ui.notify("No presets configured. Use /multi-auth-preset create to add one.", "info");
113
+ return;
114
+ }
115
+
116
+ const envEntries = parseEnvConfig();
117
+ const allSubs = normalizeEntries(mergeConfigs(config, envEntries));
118
+ const items: SelectItem[] = config.presets.map((preset) => ({
119
+ value: preset.name,
120
+ label: `${preset.enabled ? "+" : "-"} ${preset.name}`,
121
+ description: preset.entries.map((e) => formatPresetEntryWith(e, allSubs)).join(" -> "),
122
+ }));
123
+
124
+ await showWrappedSelect(ctx, {
125
+ title: "Model Presets",
126
+ subtitle: "Presets are named routing shortcuts across providers.",
127
+ items,
128
+ confirmHint: "back",
129
+ cancelHint: "close",
130
+ });
131
+ }
132
+
133
+ export async function handlePresetActivate(
134
+ pi: ExtensionAPI,
135
+ ctx: ExtensionCommandContext,
136
+ requestedName?: string,
137
+ ): Promise<void> {
138
+ const config = loadGlobalConfig();
139
+ const envEntries = parseEnvConfig();
140
+ const allSubs = normalizeEntries(mergeConfigs(config, envEntries));
141
+ const enabled = config.presets.filter((p) => p.enabled);
142
+ if (enabled.length === 0) {
143
+ ctx.ui.notify("No enabled presets. Use /multi-auth-preset create to add one.", "info");
144
+ return;
145
+ }
146
+
147
+ let presetName = requestedName?.trim();
148
+ if (!presetName) {
149
+ presetName = await showWrappedSelect(ctx, {
150
+ title: "Activate Preset",
151
+ subtitle: "Select a preset to switch to its best available entry.",
152
+ items: enabled.map((p) => ({
153
+ value: p.name,
154
+ label: p.name,
155
+ description: p.entries.filter((e) => e.enabled).map((e) => formatPresetEntryWith(e, allSubs)).join(" -> "),
156
+ })),
157
+ confirmHint: "activate",
158
+ cancelHint: "back",
159
+ });
160
+ }
161
+ if (!presetName) return;
162
+
163
+ const preset = enabled.find((p) => p.name === presetName);
164
+ if (!preset) {
165
+ ctx.ui.notify(`Preset "${presetName}" not found.`, "error");
166
+ return;
167
+ }
168
+
169
+ for (const entry of preset.entries) {
170
+ if (!entry.enabled) continue;
171
+ if (!getAuthStorage(ctx).hasAuth(entry.provider)) continue;
172
+ const model = ctx.modelRegistry.find(entry.provider, entry.model);
173
+ if (!model) continue;
174
+
175
+ const success = await pi.setModel(model);
176
+ if (!success) continue;
177
+
178
+ const prettyEntry = formatPresetEntryWith(entry, allSubs);
179
+ ctx.ui.notify(`Preset "${preset.name}": switched to ${prettyEntry}`, "info");
180
+ ctx.ui.setStatus("multi-auth", `preset:${preset.name} | ${prettyEntry}`);
181
+ return;
182
+ }
183
+
184
+ ctx.ui.notify(
185
+ `Preset "${preset.name}": no entry has a logged-in provider with the required model available.`,
186
+ "warning",
187
+ );
188
+ }
189
+
190
+ export async function handlePresetRemove(ctx: ExtensionCommandContext): Promise<void> {
191
+ const config = loadGlobalConfig();
192
+ if (config.presets.length === 0) {
193
+ ctx.ui.notify("No presets to remove.", "info");
194
+ return;
195
+ }
196
+
197
+ const selected = await showWrappedSelect(ctx, {
198
+ title: "Remove Preset",
199
+ items: config.presets.map((p) => ({
200
+ value: p.name,
201
+ label: p.name,
202
+ description: `${p.entries.length} entries`,
203
+ })),
204
+ confirmHint: "remove",
205
+ cancelHint: "back",
206
+ });
207
+ if (!selected) return;
208
+
209
+ const confirmed = await ctx.ui.confirm("Confirm", `Remove preset "${selected}"?`);
210
+ if (!confirmed) return;
211
+
212
+ config.presets = config.presets.filter((p) => p.name !== selected);
213
+ saveGlobalConfig(config);
214
+ ctx.ui.notify(`Removed preset "${selected}".`, "info");
215
+ }
216
+
217
+ export async function handlePresetToggle(ctx: ExtensionCommandContext): Promise<void> {
218
+ const config = loadGlobalConfig();
219
+ if (config.presets.length === 0) {
220
+ ctx.ui.notify("No presets configured.", "info");
221
+ return;
222
+ }
223
+
224
+ const selected = await showWrappedSelect(ctx, {
225
+ title: "Toggle Preset",
226
+ items: config.presets.map((p) => ({
227
+ value: p.name,
228
+ label: `${p.enabled ? "+" : "-"} ${p.name}`,
229
+ description: p.enabled ? "enabled" : "disabled",
230
+ })),
231
+ confirmHint: "toggle",
232
+ cancelHint: "back",
233
+ });
234
+ if (!selected) return;
235
+
236
+ const preset = config.presets.find((p) => p.name === selected);
237
+ if (!preset) return;
238
+
239
+ preset.enabled = !preset.enabled;
240
+ saveGlobalConfig(config);
241
+ ctx.ui.notify(`Preset "${preset.name}" is now ${preset.enabled ? "enabled" : "disabled"}.`, "info");
242
+ }
243
+
244
+ export async function handlePresetMenu(
245
+ pi: ExtensionAPI,
246
+ ctx: ExtensionCommandContext,
247
+ ): Promise<void> {
248
+ const actions: SelectItem[] = [
249
+ { value: "activate", label: "activate", description: "Switch to a preset's best available entry" },
250
+ { value: "create", label: "create", description: "Create a new preset" },
251
+ { value: "list", label: "list", description: "Show all presets" },
252
+ { value: "toggle", label: "toggle", description: "Enable/disable a preset" },
253
+ { value: "remove", label: "remove", description: "Delete a preset" },
254
+ ];
255
+
256
+ let preferredAction = "activate";
257
+ while (true) {
258
+ const action = await showWrappedSelect(ctx, {
259
+ title: "Model Presets",
260
+ items: actions,
261
+ initialValue: preferredAction,
262
+ confirmHint: "open",
263
+ cancelHint: "close",
264
+ });
265
+ if (!action) return;
266
+
267
+ preferredAction = action;
268
+ switch (action) {
269
+ case "activate":
270
+ await handlePresetActivate(pi, ctx);
271
+ break;
272
+ case "create":
273
+ await handlePresetCreate(ctx);
274
+ break;
275
+ case "list":
276
+ await handlePresetList(ctx);
277
+ break;
278
+ case "toggle":
279
+ await handlePresetToggle(ctx);
280
+ break;
281
+ case "remove":
282
+ await handlePresetRemove(ctx);
283
+ break;
284
+ }
285
+ }
286
+ }