omp-multi-auth 1.4.0 → 1.4.2

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 CHANGED
@@ -112,6 +112,8 @@ Run `/multi-auth limits` to inspect quota and usage information for supported pr
112
112
 
113
113
  OpenCode Go reports 5-hour, weekly, and monthly windows. The status line and ready/low/blocked state use the tighter of the 5-hour and weekly windows; the monthly window is shown for reference only, because an exhausted month can still serve requests when the account's "Use balance" fallback is on.
114
114
 
115
+ Added accounts also report usage to OMP itself, so `/usage`, `omp usage`, and status lines built on OMP's usage reports show each account (for example `anthropic-2`) next to the base provider. This works for every provider OMP has a usage fetcher for, on OMP versions with the usage registry. OMP caches, refreshes and invalidates these reports like any other provider's. Until OMP keeps a registered provider's reports across processes ([can1357/oh-my-pi#20030](https://github.com/can1357/oh-my-pi/pull/20030)), each new OMP process probes each added account once.
116
+
115
117
  ## Configuration files
116
118
 
117
119
  | Scope | Path | Contents |
@@ -119,6 +121,20 @@ OpenCode Go reports 5-hour, weekly, and monthly windows. The status line and rea
119
121
  | Global | `~/.omp/agent/multi-auth.json` | Subscriptions and presets |
120
122
  | Project | `.omp/multi-auth.json` | `allowedSubs` allow-list |
121
123
 
124
+ ### Proxy or gateway endpoints
125
+
126
+ An added account takes the `baseUrl` and `headers` that `~/.omp/agent/models.yml` sets for its base provider, so its requests go through the same gateway as the base account. omp-multi-auth reads `models.yaml` instead only when `models.yml` does not exist, as omp does; it never merges the two files, and `models.json` is not read. Each account still signs in with its own credential. Without a `baseUrl`, an account keeps the provider's built-in endpoints, so a provider with several API dialects (OpenCode Go) still uses the right URL for each model. A `baseUrl` replaces the endpoint for every model of that provider, and for GitHub Copilot accounts it replaces the endpoint derived from the sign-in token.
127
+
128
+ ```yaml
129
+ providers:
130
+ anthropic:
131
+ baseUrl: http://127.0.0.1:8080/anthropic
132
+ headers:
133
+ x-proxy-key: example
134
+ ```
135
+
136
+ With this file, `anthropic-2` and `anthropic-3` send their requests to `http://127.0.0.1:8080/anthropic` with `x-proxy-key` too. Codex accounts (`openai-codex-N`) are not covered yet. omp reads the file when it starts, so restart omp after you change it.
137
+
122
138
  ## Credits
123
139
 
124
140
  Originally based on [`pi-multi-pass`](https://github.com/hjanuschka/pi-multi-pass).
@@ -1,10 +1,13 @@
1
1
  // ========================================================================
2
2
  // Provider templates
3
3
  // ========================================================================
4
- import type { ExtensionAPI, ProviderConfig, ProviderModelConfig } from "@oh-my-pi/pi-coding-agent";
4
+ import { YAML } from "bun";
5
+ import { existsSync, readFileSync } from "fs";
6
+ import { join } from "path";
7
+ import { getAgentDir, type ExtensionAPI, type ProviderConfig, type ProviderModelConfig } from "@oh-my-pi/pi-coding-agent";
5
8
  import { getProviderDefinition } from "@oh-my-pi/pi-ai/registry";
6
9
  import type { OAuthCredentials, OAuthLoginCallbacks } from "@oh-my-pi/pi-ai/oauth";
7
- import { AssistantMessageEventStream, streamSimple, type Api, type AssistantMessageEvent, type Context, type Model, type SimpleStreamOptions } from "@oh-my-pi/pi-ai";
10
+ import { AssistantMessageEventStream, streamSimple, type Api, type AssistantMessageEvent, type Context, type Model, type SimpleStreamOptions, type UsageProvider, type UsageReport } from "@oh-my-pi/pi-ai";
8
11
  import { buildModel } from "@oh-my-pi/pi-catalog/build";
9
12
  import { fetchCodexModels } from "@oh-my-pi/pi-catalog/discovery/codex";
10
13
  import { CODEX_BASE_URL, getCodexAccountId } from "@oh-my-pi/pi-catalog/wire/codex";
@@ -249,12 +252,25 @@ export function restoreSubscriptionProvider(event: AssistantMessageEvent, provid
249
252
  return { ...event, partial: { ...event.partial, provider } };
250
253
  }
251
254
 
252
- export function createSubscriptionStream(canonicalProvider: string, builtinApi: Api | ((modelId: string) => Api)): SubscriptionStream {
255
+ export function createSubscriptionStream(
256
+ canonicalProvider: string,
257
+ builtinApi: Api | ((modelId: string) => Api),
258
+ baseUrl?: string,
259
+ ): SubscriptionStream {
253
260
  return (model, context, options) => {
254
261
  const internalApi = typeof builtinApi === "function" ? builtinApi(model.id) : builtinApi;
255
262
  const baseModel = getModels(canonicalProvider).find(candidate => candidate.id === model.id);
256
263
  if (!baseModel) throw new Error(`Missing bundled model ${canonicalProvider}/${model.id}`);
257
- const internalModel = { ...baseModel, api: internalApi, provider: canonicalProvider } as Model<Api>;
264
+ // The host resolves model.resolveHeaders into model.headers before dispatching here, so the clone's
265
+ // headers must ride along. Without an explicit baseUrl keep the catalog's per-model URL (mixed dialects).
266
+ const internalModel = {
267
+ ...baseModel,
268
+ api: internalApi,
269
+ provider: canonicalProvider,
270
+ baseUrl: baseUrl ?? baseModel.baseUrl,
271
+ headers: model.headers ?? baseModel.headers,
272
+ resolveHeaders: model.resolveHeaders,
273
+ } as Model<Api>;
258
274
  const internalOptions: SimpleStreamOptions = {
259
275
  ...(options ?? {}),
260
276
  headers: options?.headers,
@@ -286,9 +302,10 @@ export function createSubscriptionStream(canonicalProvider: string, builtinApi:
286
302
  };
287
303
  }
288
304
  export const SUBSCRIPTION_STREAMS: Record<string, SubscriptionStream> = {};
289
- export function getSubscriptionStream(baseProvider: string): SubscriptionStream | undefined {
305
+ export function getSubscriptionStream(baseProvider: string, baseUrl?: string): SubscriptionStream | undefined {
290
306
  const config = SUB_TRANSPORT_CONFIG[baseProvider];
291
307
  if (!config) return undefined;
308
+ if (baseUrl) return createSubscriptionStream(baseProvider, config.builtinApi, baseUrl);
292
309
  let stream = SUBSCRIPTION_STREAMS[baseProvider];
293
310
  if (!stream) {
294
311
  stream = createSubscriptionStream(baseProvider, config.builtinApi);
@@ -453,11 +470,39 @@ export function cloneModels(originalProvider: string, index: number): ProviderMo
453
470
  // Register a single subscription as a provider
454
471
  // ==========================================================================
455
472
 
473
+ // pi-ai added the usage registry after 18.1.20, the lowest host this extension
474
+ // supports. A static import would stop the whole extension from loading there,
475
+ // so older hosts load it and their added accounts get no usage report.
476
+ const usageRegistry = await import("@oh-my-pi/pi-ai/usage/registry").catch(() => undefined);
477
+
478
+ // Built-in usage fetchers only accept their own provider id, so a sub would get no
479
+ // usage report. Probe as the base provider and relabel the report as the sub. The
480
+ // host caches, invalidates and retains the relabeled report under the sub's id.
481
+ function subUsageProvider(baseProvider: string, name: string): UsageProvider | undefined {
482
+ const base = usageRegistry?.defaultUsageProvider(baseProvider);
483
+ if (!base) return undefined;
484
+ const asSub = (report: UsageReport | null): UsageReport | null =>
485
+ report && {
486
+ ...report,
487
+ provider: name,
488
+ limits: report.limits.map(limit => ({ ...limit, scope: { ...limit.scope, provider: name } })),
489
+ };
490
+ const { supports, parseRateLimitHeaders } = base;
491
+ return {
492
+ ...base,
493
+ id: name,
494
+ fetchUsage: async (params, ctx) => asSub(await base.fetchUsage({ ...params, provider: baseProvider }, ctx)),
495
+ supports: supports && (params => supports({ ...params, provider: baseProvider })),
496
+ parseRateLimitHeaders: parseRateLimitHeaders && ((...args) => asSub(parseRateLimitHeaders(...args))),
497
+ };
498
+ }
499
+
456
500
  function registerCodexSub(pi: ExtensionAPI, name: string, index: number, oauth: ProviderOAuth): void {
457
501
  pi.registerProvider(name, {
458
502
  baseUrl: CODEX_BASE_URL,
459
503
  api: "openai-codex-responses",
460
504
  oauth,
505
+ usage: subUsageProvider("openai-codex", name),
461
506
  async fetchDynamicModels(accessToken) {
462
507
  // OMP peeks at this provider's credential without refreshing it.
463
508
  if (!accessToken) {
@@ -487,6 +532,27 @@ function registerCodexSub(pi: ExtensionAPI, name: string, index: number, oauth:
487
532
  });
488
533
  }
489
534
 
535
+ // An account takes the endpoint its base provider is configured with in
536
+ // models.yml (a gateway or proxy baseUrl and its headers), because
537
+ // models.yml overrides are keyed by provider id and never reach the
538
+ // `<provider>-N` ids registered here. Credentials stay the account's own.
539
+ function baseProviderTransport(provider: string): { baseUrl?: string; headers?: Record<string, string> } {
540
+ let parsed: { providers?: Record<string, { baseUrl?: unknown; headers?: unknown }> } | null;
541
+ // Same lookup as the host: models.yaml is read only when models.yml is absent.
542
+ const yml = join(getAgentDir(), "models.yml");
543
+ try {
544
+ parsed = YAML.parse(readFileSync(existsSync(yml) ? yml : join(getAgentDir(), "models.yaml"), "utf8")) as typeof parsed;
545
+ } catch {
546
+ return {};
547
+ }
548
+ const override = parsed?.providers?.[provider];
549
+ const headers = override?.headers;
550
+ return {
551
+ baseUrl: typeof override?.baseUrl === "string" ? override.baseUrl : undefined,
552
+ headers: headers && typeof headers === "object" ? { ...(headers as Record<string, string>) } : undefined,
553
+ };
554
+ }
555
+
490
556
  export function registerSub(pi: ExtensionAPI, entry: SubEntry): void {
491
557
  const template = PROVIDER_TEMPLATES[entry.provider];
492
558
  if (!template) return;
@@ -496,20 +562,24 @@ export function registerSub(pi: ExtensionAPI, entry: SubEntry): void {
496
562
  registerCodexSub(pi, name, entry.index, oauth);
497
563
  return;
498
564
  }
499
- const modifyModels = template.buildModifyModels?.(name);
565
+ const transport = baseProviderTransport(entry.provider);
566
+ // An explicit baseUrl wins over the token-derived Copilot endpoint.
567
+ const modifyModels = transport.baseUrl ? undefined : template.buildModifyModels?.(name);
500
568
 
501
569
  const builtinModels = getModels(entry.provider);
502
570
  const transportApi = SUB_TRANSPORT_CONFIG[entry.provider]?.customApiId;
503
- const streamSimple = getSubscriptionStream(entry.provider);
504
- const baseUrl = builtinModels[0]?.baseUrl || "";
571
+ const streamSimple = getSubscriptionStream(entry.provider, transport.baseUrl);
572
+ const baseUrl = transport.baseUrl ?? (builtinModels[0]?.baseUrl || "");
505
573
  const models = cloneModels(entry.provider, entry.index);
506
574
 
507
575
  // Static `models` provide startup catalog for this subscription.
508
576
  pi.registerProvider(name, {
509
577
  baseUrl,
578
+ headers: transport.headers,
510
579
  api: transportApi ?? builtinModels[0]?.api,
511
580
  streamSimple,
512
581
  oauth: oauth && modifyModels ? { ...oauth, modifyModels } : oauth,
582
+ usage: subUsageProvider(entry.provider, name),
513
583
  models,
514
584
  });
515
585
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "omp-multi-auth",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "type": "module",
5
5
  "description": "Multi-account OAuth login for omp (Oh My Pi) -- add and manage multiple accounts per provider from one place.",
6
6
  "keywords": [