| 1 | import baselineJson from "@/data/release-baseline.json"; |
| 2 | import { FEATURE_CATALOGUE, compareVersions, type Feature } from "./features"; |
| 3 | import { FOUNDER_SWITCH_VALUES, type FounderSwitchValues } from "./founder-switches"; |
| 4 | import type { ModelFact, ProviderFact, RepoFacts } from "./facts"; |
| 5 | |
| 6 | export interface BaselineFeature { |
| 7 | id: string; |
| 8 | name: string; |
| 9 | summary: string; |
| 10 | status: string; |
| 11 | since: string | null; |
| 12 | } |
| 13 | |
| 14 | export interface ReleaseBaseline { |
| 15 | format: string; |
| 16 | tag: string; |
| 17 | version: string; |
| 18 | features: BaselineFeature[]; |
| 19 | providers: ProviderFact[]; |
| 20 | models: ModelFact[]; |
| 21 | toolCount: number | null; |
| 22 | } |
| 23 | |
| 24 | export type CapabilityAvailability = "released" | "in_source" | "hidden"; |
| 25 | |
| 26 | export interface CapabilityContext { |
| 27 | release: { tag: string; version: string } | null; |
| 28 | baseline: ReleaseBaseline | null; |
| 29 | switches?: FounderSwitchValues; |
| 30 | } |
| 31 | |
| 32 | export interface ReleaseCounts { |
| 33 | providerRoutes: number; |
| 34 | models: number; |
| 35 | tools: number | null; |
| 36 | } |
| 37 | |
| 38 | export interface ReleaseView { |
| 39 | providers: ProviderFact[]; |
| 40 | models: ModelFact[]; |
| 41 | counts: ReleaseCounts | null; |
| 42 | } |
| 43 | |
| 44 | const BASELINE_FORMAT = "codewhale.release-baseline.v1"; |
| 45 | |
| 46 | const isString = (value: unknown): value is string => typeof value === "string" && value.length > 0; |
| 47 | |
| 48 | export function parseReleaseBaseline(value: unknown): ReleaseBaseline | null { |
| 49 | if (!value || typeof value !== "object") return null; |
| 50 | const raw = value as Record<string, unknown>; |
| 51 | if (raw.format !== BASELINE_FORMAT || !isString(raw.tag) || !isString(raw.version) || raw.tag !== `v${raw.version}` |
| 52 | || !Array.isArray(raw.features) || !Array.isArray(raw.providers) || !Array.isArray(raw.models) |
| 53 | || !(raw.toolCount === null || (Number.isSafeInteger(raw.toolCount) && (raw.toolCount as number) > 0))) return null; |
| 54 | const features = raw.features.every((row) => row && isString(row.id) && isString(row.name) && isString(row.summary) && isString(row.status) |
| 55 | && (row.since === null || typeof row.since === "string")); |
| 56 | const providers = raw.providers.every((row) => row && isString(row.id) && isString(row.label) && typeof row.env === "string"); |
| 57 | const models = raw.models.every((row) => row && isString(row.id)); |
| 58 | if (!features || !providers || !models || raw.providers.length === 0 || raw.models.length === 0) return null; |
| 59 | return raw as unknown as ReleaseBaseline; |
| 60 | } |
| 61 | |
| 62 | export const RELEASE_BASELINE: ReleaseBaseline | null = parseReleaseBaseline(baselineJson); |
| 63 | |
| 64 | const VENDOR_AGENT_ACP = /(?:\b(?:Claude Code|Codex|Antigravity|Gemini CLI)\b[^.]*\bACP\b|\bACP\b[^.]*\b(?:Claude Code|Codex|Antigravity|Gemini CLI)\b)/i; |
| 65 | |
| 66 | const CODEWHALE_PLANS = /\bcode ?whale (?:plans?|tiers?|pricing|subscriptions?|allowances?)\b/i; |
| 67 | |
| 68 | const HELD_BY_SWITCH: Record<string, keyof FounderSwitchValues> = { |
| 69 | "cloud-computer-dispatch": "cloudComputersBeyondInvite", |
| 70 | "account-computer-files": "cloudComputersBeyondInvite", |
| 71 | }; |
| 72 | |
| 73 | export function isFounderHeld(feature: Pick<Feature, "id" | "name" | "summary">, switches: FounderSwitchValues = FOUNDER_SWITCH_VALUES): boolean { |
| 74 | const key = HELD_BY_SWITCH[feature.id]; |
| 75 | if (key) return !switches[key]; |
| 76 | if (VENDOR_AGENT_ACP.test(`${feature.name}. ${feature.summary}`)) return !switches.namedVendorAgentsOverAcp; |
| 77 | if (CODEWHALE_PLANS.test(`${feature.name}. ${feature.summary}`)) return !switches.plansPublished; |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | export function baselineFor(context: CapabilityContext): ReleaseBaseline | null { |
| 82 | const { baseline, release } = context; |
| 83 | return baseline && release && baseline.tag === release.tag ? baseline : null; |
| 84 | } |
| 85 | |
| 86 | export function capabilityAvailability( |
| 87 | feature: Pick<Feature, "id" | "name" | "summary" | "since">, |
| 88 | context: CapabilityContext, |
| 89 | ): CapabilityAvailability { |
| 90 | if (isFounderHeld(feature, context.switches)) return "hidden"; |
| 91 | const { release } = context; |
| 92 | if (!release) return "hidden"; |
| 93 | const { since } = feature; |
| 94 | if (since === "unreleased") return "in_source"; |
| 95 | if (since) { |
| 96 | const comparison = compareVersions(since, release.version); |
| 97 | if (comparison === null) return "hidden"; |
| 98 | return comparison <= 0 ? "released" : "in_source"; |
| 99 | } |
| 100 | const baseline = baselineFor(context); |
| 101 | if (!baseline) return "hidden"; |
| 102 | return baseline.features.some((row) => row.id === feature.id) ? "released" : "in_source"; |
| 103 | } |
| 104 | |
| 105 | export interface CapabilityView { |
| 106 | feature: Feature; |
| 107 | availability: Exclude<CapabilityAvailability, "hidden">; |
| 108 | name: string; |
| 109 | summary: string; |
| 110 | } |
| 111 | |
| 112 | export function capabilityView(feature: Feature, context: CapabilityContext): CapabilityView | null { |
| 113 | const availability = capabilityAvailability(feature, context); |
| 114 | if (availability === "hidden") return null; |
| 115 | const tagged = availability === "released" ? baselineFor(context)?.features.find((row) => row.id === feature.id) : undefined; |
| 116 | return { feature, availability, name: tagged?.name ?? feature.name, summary: tagged?.summary ?? feature.summary }; |
| 117 | } |
| 118 | |
| 119 | export function capabilityViews(context: CapabilityContext, features: readonly Feature[] = FEATURE_CATALOGUE.features): CapabilityView[] { |
| 120 | return features.flatMap((feature) => { |
| 121 | const view = capabilityView(feature, context); |
| 122 | return view ? [view] : []; |
| 123 | }); |
| 124 | } |
| 125 | |
| 126 | export interface CapabilityClaim { |
| 127 | feature: string; |
| 128 | describesCurrentRow?: boolean; |
| 129 | } |
| 130 | |
| 131 | export function claimAvailability( |
| 132 | claim: CapabilityClaim, |
| 133 | context: CapabilityContext, |
| 134 | features: readonly Feature[] = FEATURE_CATALOGUE.features, |
| 135 | ): CapabilityAvailability { |
| 136 | const feature = features.find((row) => row.id === claim.feature); |
| 137 | if (!feature) return "hidden"; |
| 138 | const availability = capabilityAvailability(feature, context); |
| 139 | if (availability !== "released" || !claim.describesCurrentRow) return availability; |
| 140 | const tagged = baselineFor(context)?.features.find((row) => row.id === feature.id); |
| 141 | return tagged && tagged.summary === feature.summary ? "released" : "in_source"; |
| 142 | } |
| 143 | |
| 144 | export function runtimeCapabilityContext(facts: Pick<RepoFacts, "latestPublishedRelease">): CapabilityContext { |
| 145 | const release = facts.latestPublishedRelease; |
| 146 | return { release: release ? { tag: release.tag, version: release.version } : null, baseline: RELEASE_BASELINE }; |
| 147 | } |
| 148 | |
| 149 | export function releaseView(facts: Pick<RepoFacts, "latestPublishedRelease">, baseline: ReleaseBaseline | null = RELEASE_BASELINE): ReleaseView { |
| 150 | const matched = baselineFor({ release: facts.latestPublishedRelease, baseline }); |
| 151 | if (!matched) return { providers: [], models: [], counts: null }; |
| 152 | return { |
| 153 | providers: matched.providers, |
| 154 | models: matched.models, |
| 155 | counts: { providerRoutes: matched.providers.length, models: matched.models.length, tools: matched.toolCount }, |
| 156 | }; |
| 157 | } |
| 158 | |
| 159 | export function releasedFeatureNames(version: string): string[] { |
| 160 | const context: CapabilityContext = { release: { tag: `v${version}`, version }, baseline: RELEASE_BASELINE }; |
| 161 | return capabilityViews(context) |
| 162 | .filter((view) => view.availability === "released" && view.feature.status === "stable" && view.feature.since) |
| 163 | .map((view) => view.name); |
| 164 | } |
| 165 | |
| 166 | export function claimedItems<T extends { capability?: CapabilityClaim }>( |
| 167 | items: readonly T[], |
| 168 | context: CapabilityContext, |
| 169 | features: readonly Feature[] = FEATURE_CATALOGUE.features, |
| 170 | ): { item: T; availability: "released" | "in_source" }[] { |
| 171 | return items.flatMap((item) => { |
| 172 | const availability = item.capability ? claimAvailability(item.capability, context, features) : "released"; |
| 173 | return availability === "hidden" ? [] : [{ item, availability }]; |
| 174 | }); |
| 175 | } |
| 176 |