GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(sponsors): pin sponsor presets in ocx init and ocx provider presets · lidge-jun/opencodex@8adefd4

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • SPONSORS.md
  • src
    • cli
      • init.ts
      • provider-runtime.ts
    • providers
      • sponsor-order.ts
  • tests/providers
    • sponsor-presets.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎SPONSORS.md‎

Copy file name to clipboardExpand all lines: SPONSORS.md
+2-2Lines changed: 2 additions & 2 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ sponsor receives:
4747
policy. A second-language blurb (for example Chinese) may run alongside the English one.
4848
- A built-in provider preset (`ocx provider add <id>`) shipped in a public npm release,
4949
listed near the top of the provider picker in the dashboard and CLI and marked as a sponsor
50-
there. The dashboard picker lists sponsor rows Main before Standard, then alphabetically by
51-
label, an order no sponsor can buy.
50+
there. The dashboard picker, `ocx init` and `ocx provider presets` list sponsor rows Main
51+
before Standard, then alphabetically by label, an order no sponsor can buy.
5252
- A detailed entry on the [providers page](https://opencodex.me/guides/providers/) of the docs
5353
site.
5454
- Maintenance: if a release breaks the preset or its adapter, the maintainer fixes it; issues
Collapse file

‎src/cli/init.ts‎

Copy file name to clipboardExpand all lines: src/cli/init.ts
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { InitialConfigPublicationError } from "../config/initialize";
88
import { redactUserPath } from "../lib/redact";
99
import { enrichProviderFromCatalog } from "../oauth/key-providers";
1010
import { deriveInitProviders } from "../providers/derive";
11+
import { pinSponsorsWithinKind } from "../providers/sponsor-order";
1112
import type { OcxConfig, OcxProviderConfig } from "../types";
1213

1314
class InitCancelledError extends Error {
@@ -126,7 +127,7 @@ export async function runInit(): Promise<void> {
126127
try {
127128
console.log("\n🔧 opencodex (ocx) setup\n");
128129

129-
const providers = buildInitProviders();
130+
const providers = pinSponsorsWithinKind(buildInitProviders());
130131
printMenu(providers);
131132

132133
const choice = await prompt.ask("\nSelect default provider (number): ");
Collapse file

‎src/cli/provider-runtime.ts‎

Copy file name to clipboardExpand all lines: src/cli/provider-runtime.ts
+10-1Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
type RuntimeApiDeps,
1414
} from "./runtime-api";
1515
import { providerQuotaLine } from "./account-extended";
16+
import { pinSponsorRows } from "../providers/sponsor-order";
1617
import type { ProviderQuotaReportDto } from "./account-api";
1718

1819
interface ProviderQuotasDto {
@@ -233,7 +234,15 @@ async function presets(argv: string[], deps: RuntimeApiDeps): Promise<void> {
233234
rejectArgs(args, USAGE);
234235
const result = await runtimeRequest<{ providers?: unknown[] } | unknown[]>("/api/provider-presets", {}, deps);
235236
const rows = Array.isArray(result) ? result : result.providers ?? [];
236-
printData(result, wantsJson, rows.map(row => {
237+
const pinned = pinSponsorRows(
238+
rows,
239+
row => {
240+
const tier = (row as Record<string, unknown>)?.sponsor;
241+
return tier === "main" || tier === "standard" ? tier : undefined;
242+
},
243+
row => String((row as Record<string, unknown>)?.label ?? (row as Record<string, unknown>)?.id ?? ""),
244+
);
245+
printData(result, wantsJson, pinned.map(row => {
237246
const record = row as Record<string, unknown>;
238247
const sponsor = record.sponsor ? ` (sponsor: ${String(record.sponsor)})` : "";
239248
return `${String(record.id ?? record.name ?? "?")} ${String(record.label ?? record.adapter ?? "")}${sponsor}`.trimEnd();
Collapse file

‎src/providers/sponsor-order.ts‎

Copy file name to clipboard
+55Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { PROVIDER_REGISTRY } from "./registry";
2+
3+
export type SponsorTier = "main" | "standard";
4+
5+
const SPONSOR_RANK: Record<SponsorTier, number> = { main: 0, standard: 1 };
6+
7+
/**
8+
* SPONSORS.md promises sponsor presets sit near the top of the picker "in the dashboard and CLI".
9+
* The dashboard applies that in gui/src/components/provider-catalog/provider-presets.ts
10+
* (`pinSponsors`); this is the same order for the CLI surfaces: sponsor rows first, Main before
11+
* Standard, alphabetical by label within a tier, then every other row in the caller's order.
12+
* Alphabetical among sponsors is deliberate: it is the one order no sponsor can buy.
13+
*/
14+
export function pinSponsorRows<T>(
15+
rows: readonly T[],
16+
tierOf: (row: T) => SponsorTier | undefined,
17+
labelOf: (row: T) => string,
18+
): T[] {
19+
const sponsors = rows.filter(row => tierOf(row) !== undefined);
20+
if (sponsors.length === 0) return [...rows];
21+
sponsors.sort((a, b) =>
22+
SPONSOR_RANK[tierOf(a)!] - SPONSOR_RANK[tierOf(b)!]
23+
|| labelOf(a).localeCompare(labelOf(b), undefined, { sensitivity: "base" }));
24+
return [...sponsors, ...rows.filter(row => tierOf(row) === undefined)];
25+
}
26+
27+
/** Registry sponsor tier by preset id; the registry field is the only thing that marks a sponsor. */
28+
export function registrySponsorTier(id: string): SponsorTier | undefined {
29+
return PROVIDER_REGISTRY.find(entry => entry.id === id)?.sponsor?.tier;
30+
}
31+
32+
/**
33+
* The `ocx init` menu prints a heading whenever the provider kind changes, and registry order
34+
* interleaves kinds, so the same heading appears more than once. Sponsor rows move, in pinned
35+
* order, to the start of the first run of their own kind: they stay under the right heading, sit
36+
* near the top of the menu, and no new heading is introduced. Every other row keeps its order.
37+
*/
38+
export function pinSponsorsWithinKind<T extends { id: string; label: string; kind: string }>(rows: readonly T[]): T[] {
39+
const tierOf = (row: T) => registrySponsorTier(row.id);
40+
const sponsors = pinSponsorRows(rows.filter(row => tierOf(row) !== undefined), tierOf, row => row.label);
41+
if (sponsors.length === 0) return [...rows];
42+
const out: T[] = [];
43+
const placedKinds = new Set<string>();
44+
for (const row of rows) {
45+
if (tierOf(row) !== undefined) continue;
46+
if (!placedKinds.has(row.kind)) {
47+
placedKinds.add(row.kind);
48+
out.push(...sponsors.filter(sponsor => sponsor.kind === row.kind));
49+
}
50+
out.push(row);
51+
}
52+
// A sponsor whose kind has no other row still appears, at the end, rather than vanishing.
53+
out.push(...sponsors.filter(sponsor => !placedKinds.has(sponsor.kind)));
54+
return out;
55+
}
Collapse file

‎tests/providers/sponsor-presets.test.ts‎

Copy file name to clipboardExpand all lines: tests/providers/sponsor-presets.test.ts
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { describe, expect, test } from "bun:test";
2+
import { buildInitProviders } from "../../src/cli/init";
23
import { deriveProviderPresets } from "../../src/providers/derive";
34
import { PROVIDER_REGISTRY } from "../../src/providers/registry";
5+
import { pinSponsorRows, pinSponsorsWithinKind } from "../../src/providers/sponsor-order";
46

57
/**
68
* The registry `sponsor` field is the only thing that marks a paid sponsor, and SPONSORS.md
@@ -36,4 +38,31 @@ describe("sponsor presets", () => {
3638
const deduped = registryOrder.filter(id => (seen.has(id) ? false : (seen.add(id), true)));
3739
expect(ids).toEqual(deduped);
3840
});
41+
42+
test("ocx init moves sponsors to the start of the first run of their kind, alphabetical by label", () => {
43+
const rows = pinSponsorsWithinKind(buildInitProviders());
44+
const sponsorIds = PROVIDER_REGISTRY.filter(entry => entry.sponsor).map(entry => entry.id);
45+
expect(sponsorIds.length).toBeGreaterThan(0);
46+
const firstKey = rows.findIndex(row => row.kind === "key");
47+
const leading = rows.slice(firstKey, firstKey + sponsorIds.length);
48+
expect(leading.map(row => row.id).sort()).toEqual([...sponsorIds].sort());
49+
const labels = leading.map(row => row.label);
50+
expect(labels).toEqual([...labels].sort((a, b) => a.localeCompare(b, undefined, { sensitivity: "base" })));
51+
// Same rows; non-sponsor rows keep their order, so the heading sequence is unchanged.
52+
expect(rows.map(row => row.id).sort()).toEqual(buildInitProviders().map(row => row.id).sort());
53+
const rest = (list: { id: string }[]) => list.map(row => row.id).filter(id => !sponsorIds.includes(id));
54+
expect(rest(rows)).toEqual(rest(buildInitProviders()));
55+
const kinds = (list: { kind: string }[]) => list.map(row => row.kind).filter((kind, i, all) => kind !== all[i - 1]);
56+
expect(kinds(rows)).toEqual(kinds(buildInitProviders()));
57+
});
58+
59+
test("pinSponsorRows orders Main before Standard and keeps the rest in caller order", () => {
60+
const rows = [
61+
{ id: "a", label: "Zed" }, { id: "b", label: "beta", tier: "standard" as const },
62+
{ id: "c", label: "Mid" }, { id: "d", label: "Alpha", tier: "standard" as const },
63+
{ id: "e", label: "Omega", tier: "main" as const },
64+
];
65+
const pinned = pinSponsorRows(rows, row => row.tier, row => row.label).map(row => row.id);
66+
expect(pinned).toEqual(["e", "d", "b", "a", "c"]);
67+
});
3968
});

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.