GeezerVison is showing github.com — fix(sponsors): pin sponsor presets in ocx init and ocx provider presets · lidge-jun/opencodex@8adefd4
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -47,8 +47,8 @@ sponsor receives: | 47 | 47 | policy. A second-language blurb (for example Chinese) may run alongside the English one. | 48 | 48 | - A built-in provider preset (`ocx provider add <id>`) shipped in a public npm release, | 49 | 49 | 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. | 52 | 52 | - A detailed entry on the [providers page](https://opencodex.me/guides/providers/) of the docs | 53 | 53 | site. | 54 | 54 | - Maintenance: if a release breaks the preset or its adapter, the maintainer fixes it; issues | |
Collapse file Copy file name to clipboardExpand all lines: src/cli/init.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,6 +8,7 @@ import { InitialConfigPublicationError } from "../config/initialize"; | 8 | 8 | import { redactUserPath } from "../lib/redact"; | 9 | 9 | import { enrichProviderFromCatalog } from "../oauth/key-providers"; | 10 | 10 | import { deriveInitProviders } from "../providers/derive"; | | 11 | +import { pinSponsorsWithinKind } from "../providers/sponsor-order"; | 11 | 12 | import type { OcxConfig, OcxProviderConfig } from "../types"; | 12 | 13 |
| 13 | 14 | class InitCancelledError extends Error { | @@ -126,7 +127,7 @@ export async function runInit(): Promise<void> { | 126 | 127 | try { | 127 | 128 | console.log("\n🔧 opencodex (ocx) setup\n"); | 128 | 129 |
| 129 | | - const providers = buildInitProviders(); | | 130 | + const providers = pinSponsorsWithinKind(buildInitProviders()); | 130 | 131 | printMenu(providers); | 131 | 132 |
| 132 | 133 | const choice = await prompt.ask("\nSelect default provider (number): "); | |
Collapse file Copy file name to clipboardExpand all lines: src/cli/provider-runtime.ts +10-1Lines changed: 10 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,6 +13,7 @@ import { | 13 | 13 | type RuntimeApiDeps, | 14 | 14 | } from "./runtime-api"; | 15 | 15 | import { providerQuotaLine } from "./account-extended"; | | 16 | +import { pinSponsorRows } from "../providers/sponsor-order"; | 16 | 17 | import type { ProviderQuotaReportDto } from "./account-api"; | 17 | 18 |
| 18 | 19 | interface ProviderQuotasDto { | @@ -233,7 +234,15 @@ async function presets(argv: string[], deps: RuntimeApiDeps): Promise<void> { | 233 | 234 | rejectArgs(args, USAGE); | 234 | 235 | const result = await runtimeRequest<{ providers?: unknown[] } | unknown[]>("/api/provider-presets", {}, deps); | 235 | 236 | 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 => { | 237 | 246 | const record = row as Record<string, unknown>; | 238 | 247 | const sponsor = record.sponsor ? ` (sponsor: ${String(record.sponsor)})` : ""; | 239 | 248 | return `${String(record.id ?? record.name ?? "?")} ${String(record.label ?? record.adapter ?? "")}${sponsor}`.trimEnd(); | |
Collapse file Copy file name to clipboard +55Lines changed: 55 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 Copy file name to clipboardExpand all lines: tests/providers/sponsor-presets.test.ts +29Lines changed: 29 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { describe, expect, test } from "bun:test"; | | 2 | +import { buildInitProviders } from "../../src/cli/init"; | 2 | 3 | import { deriveProviderPresets } from "../../src/providers/derive"; | 3 | 4 | import { PROVIDER_REGISTRY } from "../../src/providers/registry"; | | 5 | +import { pinSponsorRows, pinSponsorsWithinKind } from "../../src/providers/sponsor-order"; | 4 | 6 |
| 5 | 7 | /** | 6 | 8 | * The registry `sponsor` field is the only thing that marks a paid sponsor, and SPONSORS.md | @@ -36,4 +38,31 @@ describe("sponsor presets", () => { | 36 | 38 | const deduped = registryOrder.filter(id => (seen.has(id) ? false : (seen.add(id), true))); | 37 | 39 | expect(ids).toEqual(deduped); | 38 | 40 | }); | | 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 | + }); | 39 | 68 | }); |
|