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.
@@ -0,0 +1,101 @@
1
+ // Shared TUI select helpers.
2
+ import type { ExtensionCommandContext } from "@oh-my-pi/pi-coding-agent";
3
+ import { DynamicBorder, keyHint } from "@oh-my-pi/pi-coding-agent";
4
+ import { Container, Key, SelectList, Text, matchesKey, type SelectItem } from "@oh-my-pi/pi-tui";
5
+
6
+ export function getWrappedSelectIndex(items: SelectItem[], value: string | undefined): number {
7
+ if (!value) return 0;
8
+ const index = items.findIndex((item) => item.value === value);
9
+ return index >= 0 ? index : 0;
10
+ }
11
+
12
+ export async function showWrappedSelect(
13
+ ctx: ExtensionCommandContext,
14
+ options: {
15
+ title: string;
16
+ items: SelectItem[];
17
+ subtitle?: string;
18
+ initialValue?: string;
19
+ confirmHint?: string;
20
+ cancelHint?: string;
21
+ },
22
+ ): Promise<string | undefined> {
23
+ if (options.items.length === 0) return undefined;
24
+
25
+ if (!ctx.hasUI) {
26
+ const renderedItems = options.items.map((item) =>
27
+ item.description ? `${item.label} — ${item.description}` : item.label,
28
+ );
29
+ const selected = await ctx.ui.select(options.title, renderedItems);
30
+ if (!selected) return undefined;
31
+ const index = renderedItems.indexOf(selected);
32
+ return index >= 0 ? options.items[index]?.value : undefined;
33
+ }
34
+
35
+ const confirmHint = options.confirmHint || "select";
36
+ const cancelHint = options.cancelHint || "close";
37
+
38
+ const selectedValue = await ctx.ui.custom<string | null>((tui, theme, _kb, done) => {
39
+ const container = new Container();
40
+ const footer = [
41
+ keyHint("tui.select.confirm", confirmHint),
42
+ keyHint("tui.select.cancel", cancelHint),
43
+ ].join(" • ");
44
+
45
+ container.addChild(new DynamicBorder((s: string) => theme.fg("accent", s)));
46
+ container.addChild(new Text(theme.fg("accent", theme.bold(options.title))));
47
+ if (options.subtitle) {
48
+ container.addChild(new Text(theme.fg("dim", options.subtitle)));
49
+ }
50
+
51
+ const selectList = new SelectList(options.items, Math.min(options.items.length, 10), {
52
+ selectedPrefix: (text) => theme.fg("accent", text),
53
+ selectedText: (text) => theme.fg("accent", text),
54
+ description: (text) => theme.fg("muted", text),
55
+ scrollInfo: (text) => theme.fg("dim", text),
56
+ noMatch: (text) => theme.fg("warning", text),
57
+ });
58
+ selectList.setSelectedIndex(getWrappedSelectIndex(options.items, options.initialValue));
59
+ selectList.onSelect = (item) => done(item.value);
60
+ selectList.onCancel = () => done(null);
61
+ container.addChild(selectList);
62
+ container.addChild(new Text(theme.fg("dim", footer)));
63
+ container.addChild(new DynamicBorder((s: string) => theme.fg("accent", s)));
64
+
65
+ return {
66
+ render(width: number) {
67
+ return container.render(width);
68
+ },
69
+ invalidate() {
70
+ container.invalidate();
71
+ },
72
+ handleInput(data: string) {
73
+ const current = selectList.getSelectedItem();
74
+ const currentIndex = current
75
+ ? options.items.findIndex((item) => item.value === current.value)
76
+ : 0;
77
+
78
+ if (matchesKey(data, Key.up) && options.items.length > 1 && currentIndex === 0) {
79
+ selectList.setSelectedIndex(options.items.length - 1);
80
+ tui.requestRender();
81
+ return;
82
+ }
83
+
84
+ if (
85
+ matchesKey(data, Key.down)
86
+ && options.items.length > 1
87
+ && currentIndex === options.items.length - 1
88
+ ) {
89
+ selectList.setSelectedIndex(0);
90
+ tui.requestRender();
91
+ return;
92
+ }
93
+
94
+ selectList.handleInput(data);
95
+ tui.requestRender();
96
+ },
97
+ };
98
+ });
99
+
100
+ return selectedValue ?? undefined;
101
+ }
@@ -0,0 +1,189 @@
1
+ import type { ExtensionAPI, ExtensionCommandContext, ExtensionContext } from "@oh-my-pi/pi-coding-agent";
2
+ import { loadGlobalConfig, parseEnvConfig, mergeConfigs, normalizeEntries, loadEffectiveConfig, formatAllowedProviderSummary, getProjectScopedProviderNames, findSelectableModelForProvider, getProviderDisplayName } from "./lib/config.ts";
3
+ import { registerSub, rewriteAntigravitySystemInstruction } from "./lib/providers.ts";
4
+ import { handleSubsMenu, handleSubsList, handleSubsAdd, handleSubsRemove, handleSubsLogin, handleSubsLogout, handleSubsSwitch, handleSubsStatus } from "./lib/commands-subs.ts";
5
+ import { handleSubsLimits } from "./lib/quota.ts";
6
+ import { handlePresetActivate, handlePresetCreate, handlePresetList, handlePresetToggle, handlePresetRemove, handlePresetMenu } from "./lib/commands-preset.ts";
7
+
8
+ // ========================================================================
9
+ // Extension entry point
10
+ // ========================================================================
11
+
12
+ export default function multiSub(pi: ExtensionAPI) {
13
+
14
+ const config = loadGlobalConfig();
15
+ const envEntries = parseEnvConfig();
16
+ const all = normalizeEntries(mergeConfigs(config, envEntries));
17
+
18
+ // Register all subscriptions (always global)
19
+ for (const entry of all) {
20
+ registerSub(pi, entry);
21
+ }
22
+
23
+ // Apply same tag-only fingerprint workaround at OMP payload seam.
24
+ pi.on("before_provider_request", (event, ctx) => {
25
+ if (ctx.model?.provider === "google-antigravity") {
26
+ return rewriteAntigravitySystemInstruction(event.payload);
27
+ }
28
+ });
29
+
30
+ let projectRestrictionSwitchInFlight = false;
31
+ const enforceProjectRestriction = async (
32
+ ctx: ExtensionContext | ExtensionCommandContext,
33
+ reason: "session" | "model" | "input",
34
+ ): Promise<boolean> => {
35
+ if (projectRestrictionSwitchInFlight) return true;
36
+ const effective = loadEffectiveConfig(ctx.cwd);
37
+ const allowedSummary = formatAllowedProviderSummary(effective);
38
+ if (!allowedSummary) {
39
+ return true;
40
+ }
41
+ if (ctx.model && effective.allowedProviderNames?.includes(ctx.model.provider)) {
42
+ return true;
43
+ }
44
+
45
+ for (const providerName of getProjectScopedProviderNames(ctx, effective)) {
46
+ const model = findSelectableModelForProvider(ctx, providerName, ctx.model?.id);
47
+ if (!model) continue;
48
+
49
+ projectRestrictionSwitchInFlight = true;
50
+ try {
51
+ const success = await pi.setModel(model);
52
+ if (!success) continue;
53
+ const displayName = getProviderDisplayName(providerName, effective.subscriptions);
54
+ ctx.ui.notify(
55
+ `multi-auth: project restricted to ${allowedSummary}; switched to ${displayName} (${model.id}).`,
56
+ "info",
57
+ );
58
+ return true;
59
+ } finally {
60
+ projectRestrictionSwitchInFlight = false;
61
+ }
62
+ }
63
+
64
+ const currentModel = ctx.model ? `${ctx.model.provider}/${ctx.model.id}` : "the current model";
65
+ ctx.ui.notify(
66
+ `multi-auth: project restricted to ${allowedSummary}, but no authenticated allowed provider can serve ${currentModel}.`,
67
+ "warning",
68
+ );
69
+ return false;
70
+ };
71
+
72
+ // On session start, apply project restriction and show allowed providers.
73
+ pi.on("session_start", async (_event, ctx) => {
74
+ const effective = loadEffectiveConfig(ctx.cwd);
75
+ const allowedSummary = formatAllowedProviderSummary(effective);
76
+ if (allowedSummary) {
77
+ ctx.ui.setStatus("multi-auth", `allowed ${allowedSummary}`);
78
+ }
79
+
80
+ await enforceProjectRestriction(ctx, "session");
81
+ });
82
+
83
+ pi.on("model_select", async (_event, ctx) => {
84
+ await enforceProjectRestriction(ctx, "model");
85
+ });
86
+
87
+ pi.on("input", async (event, ctx) => {
88
+ if (event.text.trimStart().startsWith("/")) {
89
+ return { action: "continue" as const };
90
+ }
91
+ const ok = await enforceProjectRestriction(ctx, "input");
92
+ return ok ? { action: "continue" as const } : { action: "handled" as const };
93
+ });
94
+
95
+ // Register /multi-auth command
96
+ pi.registerCommand("multi-auth", {
97
+ description: "Manage multi-account OAuth subscriptions and accounts",
98
+ getArgumentCompletions: (prefix: string) => {
99
+ const subcommands = ["list", "add", "remove", "login", "logout", "switch", "status", "limits"];
100
+ const filtered = subcommands.filter((s) => s.startsWith(prefix));
101
+ return filtered.length > 0
102
+ ? filtered.map((s) => ({ value: s, label: s }))
103
+ : null;
104
+ },
105
+ handler: async (args: string, ctx: ExtensionCommandContext) => {
106
+ const config = loadGlobalConfig();
107
+ const parts = args.trim().split(/\s+/).filter(Boolean);
108
+ const subcommand = (parts[0] || "").toLowerCase();
109
+ const rest = parts.slice(1).join(" ");
110
+ switch (subcommand) {
111
+ case "list":
112
+ case "ls":
113
+ return handleSubsList(pi, ctx, config);
114
+ case "add":
115
+ case "new":
116
+ return handleSubsAdd(pi, ctx);
117
+ case "remove":
118
+ case "rm":
119
+ case "delete":
120
+ return handleSubsRemove(pi, ctx);
121
+ case "login":
122
+ return handleSubsLogin(ctx);
123
+ case "logout":
124
+ return handleSubsLogout(ctx);
125
+ case "switch":
126
+ return handleSubsSwitch(pi, ctx, rest || undefined);
127
+ case "status":
128
+ case "info":
129
+ return handleSubsStatus(ctx);
130
+ case "limits":
131
+ case "quota":
132
+ case "usage":
133
+ return handleSubsLimits(ctx);
134
+ default:
135
+ return handleSubsMenu(pi, ctx);
136
+ }
137
+ },
138
+ });
139
+
140
+ // Register /multi-auth-preset command
141
+ pi.registerCommand("multi-auth-preset", {
142
+ description: "Manage model presets across providers",
143
+ getArgumentCompletions: (prefix: string) => {
144
+ const subcommands = ["activate", "create", "list", "toggle", "remove"];
145
+ const filtered = subcommands.filter((s) => s.startsWith(prefix));
146
+ if (filtered.length > 0) {
147
+ return filtered.map((s) => ({ value: s, label: s }));
148
+ }
149
+ const config = loadGlobalConfig();
150
+ const presetNames = config.presets
151
+ .filter((p) => p.enabled && p.name.startsWith(prefix))
152
+ .map((p) => ({ value: p.name, label: p.name }));
153
+ return presetNames.length > 0 ? presetNames : null;
154
+ },
155
+ handler: async (args: string, ctx: ExtensionCommandContext) => {
156
+ const parts = args.trim().split(/\s+/).filter(Boolean);
157
+ const subcommand = (parts[0] || "").toLowerCase();
158
+ const rest = parts.slice(1).join(" ");
159
+ switch (subcommand) {
160
+ case "activate":
161
+ case "use":
162
+ return handlePresetActivate(pi, ctx, rest || undefined);
163
+ case "create":
164
+ case "new":
165
+ return handlePresetCreate(ctx);
166
+ case "list":
167
+ case "ls":
168
+ return handlePresetList(ctx);
169
+ case "toggle":
170
+ return handlePresetToggle(ctx);
171
+ case "remove":
172
+ case "rm":
173
+ case "delete":
174
+ return handlePresetRemove(ctx);
175
+ default:
176
+ if (subcommand) {
177
+ const config = loadGlobalConfig();
178
+ const preset = config.presets.find(
179
+ (p) => p.name.toLowerCase() === subcommand && p.enabled,
180
+ );
181
+ if (preset) {
182
+ return handlePresetActivate(pi, ctx, preset.name);
183
+ }
184
+ }
185
+ return handlePresetMenu(pi, ctx);
186
+ }
187
+ },
188
+ });
189
+ }
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "omp-multi-auth",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "description": "Multi-account OAuth login for omp (Oh My Pi) -- add and manage multiple accounts per provider from one place.",
6
+ "keywords": [
7
+ "omp-package",
8
+ "omp-extension",
9
+ "oauth",
10
+ "multi-account",
11
+ "codex",
12
+ "anthropic",
13
+ "copilot",
14
+ "kimi",
15
+ "xai",
16
+ "gemini",
17
+ "minimax"
18
+ ],
19
+ "license": "MIT",
20
+ "author": "hjanuschka",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://github.com/tuandinh0801/omp-multi-auth"
24
+ },
25
+ "files": [
26
+ "extensions",
27
+ "README.md"
28
+ ],
29
+ "publishConfig": {
30
+ "access": "public"
31
+ },
32
+ "omp": {
33
+ "extensions": [
34
+ "./extensions/multi-auth.ts"
35
+ ]
36
+ }
37
+ }