-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: refresh models from a hosted catalog #1928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sethkarten
wants to merge
13
commits into
main
Choose a base branch
from
eng-5435-hosted-model-catalog
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0a1007b
feat: refresh models from a hosted catalog
sethkarten e429b96
fix: make the hosted model list authoritative
sethkarten 871be56
fix: accept catalog metadata on known provider routes
sethkarten ee1538d
fix: harden hosted catalog validation
sethkarten aeb795a
refactor: share environment flag parsing
sethkarten b299f64
fix: reject incompatible hosted catalogs
sethkarten e4e2861
fix: fail closed on an empty OpenRouter slice
sethkarten 91ea1c7
fix: require catalog transport matches
sethkarten 0634d93
refactor: share model catalog validation schemas
sethkarten 73322c5
fix: preserve local compat validation behavior
sethkarten a1abe48
fix: validate every local compat field
sethkarten 95dfe25
fix: harden hosted catalog refreshes
sethkarten 97d6e39
fix: refresh future-dated model caches
sethkarten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| name: Refresh hosted model catalog | ||
|
|
||
| on: | ||
| schedule: | ||
| # Daily, off the top of the hour to avoid the cron rush. | ||
| - cron: "17 5 * * *" | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| concurrency: | ||
| group: refresh-hosted-model-catalog | ||
| cancel-in-progress: false | ||
|
|
||
| jobs: | ||
| publish: | ||
| name: Generate, validate, and publish | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 20 | ||
| steps: | ||
| - name: Checkout default branch | ||
| uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 | ||
| with: | ||
| ref: ${{ github.event.repository.default_branch }} | ||
| persist-credentials: false | ||
|
|
||
| - name: Setup Node.js | ||
| uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0 | ||
| with: | ||
| node-version: 22 | ||
| cache: npm | ||
|
|
||
| - name: Install dependencies | ||
| run: npm ci --ignore-scripts | ||
|
|
||
| - name: Generate aggregate catalog | ||
| working-directory: packages/ai | ||
| env: | ||
| PRIME_AGENT_MODEL_CATALOG_OUTPUT: /tmp/model-catalog.json | ||
| PRIME_AGENT_MODEL_CATALOG_STRICT: "1" | ||
| run: npm run generate-models | ||
|
|
||
| - name: Validate catalog | ||
| working-directory: packages/ai | ||
| run: npm run validate-model-catalog -- /tmp/model-catalog.json | ||
|
|
||
| - name: Publish catalog to R2 | ||
| env: | ||
| AWS_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }} | ||
| AWS_DEFAULT_REGION: auto | ||
| AWS_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }} | ||
| R2_BUCKET: ${{ secrets.R2_BUCKET }} | ||
| R2_ENDPOINT_URL: ${{ secrets.R2_ENDPOINT_URL }} | ||
| run: | | ||
| test -n "$R2_BUCKET" | ||
| test -n "$R2_ENDPOINT_URL" | ||
| aws s3 cp /tmp/model-catalog.json "s3://${R2_BUCKET}/model-catalog.json" \ | ||
| --endpoint-url "$R2_ENDPOINT_URL" \ | ||
| --content-type application/json \ | ||
| --cache-control 'public, max-age=3600, must-revalidate' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - Added a validated aggregate model catalog for daily publication from live provider catalogs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #!/usr/bin/env tsx | ||
|
|
||
| import { readFileSync } from "node:fs"; | ||
| import { parseModelCatalog } from "../src/model-catalog.js"; | ||
|
|
||
| const [candidatePath] = process.argv.slice(2); | ||
| if (!candidatePath) throw new Error("Usage: validate-model-catalog.ts <candidate-path>"); | ||
| const candidate = parseModelCatalog(JSON.parse(readFileSync(candidatePath, "utf8")) as unknown); | ||
| console.log(`Validated ${candidate.models.length} models`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { type TProperties, Type } from "typebox"; | ||
| import { Value } from "typebox/value"; | ||
| import { isModelCompat } from "./model-compat-schema.js"; | ||
| import type { Api, Model } from "./types.js"; | ||
|
|
||
| const MODEL_CATALOG_SCHEMA_VERSION = 1 as const; | ||
| const MAX_MODEL_CATALOG_MODELS = 20_000; | ||
|
|
||
| export interface ModelCatalogV1 { | ||
| schemaVersion: typeof MODEL_CATALOG_SCHEMA_VERSION; | ||
| generatedAt: string; | ||
| models: Model<Api>[]; | ||
| } | ||
|
|
||
| function strictObject<T extends TProperties>(properties: T) { | ||
| return Type.Object(properties, { additionalProperties: false }); | ||
| } | ||
|
|
||
| const ThinkingLevelValueSchema = Type.Union([Type.String({ minLength: 1, maxLength: 128 }), Type.Null()]); | ||
| const ThinkingLevelMapSchema = strictObject({ | ||
| off: Type.Optional(ThinkingLevelValueSchema), | ||
| minimal: Type.Optional(ThinkingLevelValueSchema), | ||
| low: Type.Optional(ThinkingLevelValueSchema), | ||
| medium: Type.Optional(ThinkingLevelValueSchema), | ||
| high: Type.Optional(ThinkingLevelValueSchema), | ||
| xhigh: Type.Optional(ThinkingLevelValueSchema), | ||
| max: Type.Optional(ThinkingLevelValueSchema), | ||
| }); | ||
| const CostSchema = Type.Number({ minimum: 0, maximum: 1_000_000 }); | ||
| const CatalogModelSchema = strictObject({ | ||
| id: Type.String({ minLength: 1, maxLength: 1_024 }), | ||
| name: Type.String({ minLength: 1, maxLength: 1_024 }), | ||
| api: Type.String({ minLength: 1, maxLength: 128 }), | ||
| provider: Type.String({ minLength: 1, maxLength: 128 }), | ||
| baseUrl: Type.String({ maxLength: 2_048 }), | ||
| reasoning: Type.Boolean(), | ||
| thinkingLevelMap: Type.Optional(ThinkingLevelMapSchema), | ||
| input: Type.Array(Type.Union([Type.Literal("text"), Type.Literal("image")]), { minItems: 1, maxItems: 2 }), | ||
| cost: Type.Object({ | ||
| input: CostSchema, | ||
| output: CostSchema, | ||
| cacheRead: CostSchema, | ||
| cacheWrite: CostSchema, | ||
| }), | ||
| contextWindow: Type.Integer({ minimum: 1, maximum: 100_000_000 }), | ||
| maxTokens: Type.Integer({ minimum: 1, maximum: 100_000_000 }), | ||
| featured: Type.Optional(Type.Boolean()), | ||
| compat: Type.Optional(Type.Unknown()), | ||
| }); | ||
| const ModelCatalogEnvelopeSchema = Type.Object({ | ||
| schemaVersion: Type.Literal(MODEL_CATALOG_SCHEMA_VERSION), | ||
| generatedAt: Type.String(), | ||
| models: Type.Array(Type.Unknown(), { minItems: 1, maxItems: MAX_MODEL_CATALOG_MODELS }), | ||
| }); | ||
|
|
||
| function isRecord(value: unknown): value is Record<string, unknown> { | ||
| return typeof value === "object" && value !== null && !Array.isArray(value); | ||
| } | ||
|
|
||
| export function createModelCatalog(models: readonly Model<Api>[], generatedAt = new Date()): ModelCatalogV1 { | ||
| return { | ||
| schemaVersion: MODEL_CATALOG_SCHEMA_VERSION, | ||
| generatedAt: generatedAt.toISOString(), | ||
| models: models | ||
| .map((model) => { | ||
| const catalogModel = structuredClone(model); | ||
| delete catalogModel.headers; | ||
| return catalogModel; | ||
| }) | ||
| .sort((left, right) => left.provider.localeCompare(right.provider) || left.id.localeCompare(right.id)), | ||
| }; | ||
| } | ||
|
|
||
| export function parseModelCatalog(value: unknown, options: { skipInvalidModels?: boolean } = {}): ModelCatalogV1 { | ||
| if (!isRecord(value) || value.schemaVersion !== MODEL_CATALOG_SCHEMA_VERSION) { | ||
| throw new Error("Unsupported model catalog schema version"); | ||
| } | ||
| if (typeof value.generatedAt !== "string" || !Number.isFinite(Date.parse(value.generatedAt))) { | ||
| throw new Error("Invalid model catalog timestamp"); | ||
| } | ||
| if (!Array.isArray(value.models) || value.models.length === 0 || value.models.length > MAX_MODEL_CATALOG_MODELS) { | ||
| throw new Error("Invalid model catalog model count"); | ||
| } | ||
| if (!Value.Check(ModelCatalogEnvelopeSchema, value)) throw new Error("Invalid model catalog entry"); | ||
|
|
||
| const models: Model<Api>[] = []; | ||
| const seen = new Set<string>(); | ||
| for (const candidate of value.models) { | ||
| if (!Value.Check(CatalogModelSchema, candidate)) { | ||
| if (options.skipInvalidModels) continue; | ||
| throw new Error("Invalid model catalog entry"); | ||
| } | ||
| const model = candidate as Model<Api>; | ||
| if (!isModelCompat(model.api, model.compat)) { | ||
| if (options.skipInvalidModels) continue; | ||
| throw new Error("Invalid model catalog entry"); | ||
| } | ||
| const key = JSON.stringify([model.provider, model.id]); | ||
| if (seen.has(key)) throw new Error(`Duplicate model catalog entry ${key}`); | ||
| seen.add(key); | ||
| models.push(model); | ||
| } | ||
| if (models.length === 0) throw new Error("Model catalog has no compatible entries"); | ||
| return { schemaVersion: MODEL_CATALOG_SCHEMA_VERSION, generatedAt: value.generatedAt, models }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The R2 credential used here should be confirmed bucket-scoped and write-only, since a broader token would make this workflow the catalog's weakest link. Nothing in-repo can verify this — flagging for a one-time check in the Cloudflare dashboard.
[written by prime-agent, checked by snimu]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. This cannot be verified from the repository. I am leaving this thread open and escalating a one-time Cloudflare dashboard check that the workflow credential is restricted to the intended bucket with the narrowest available object-write permission.