forked from bai/curriculum-project-hub
feat(hub): derive admin model picker from org provider connection via OpenRouter API
The admin role model picker was hardcoded to the env-default model registry (createDefaultModelRegistry), which only ever returned a single Sonnet model. Roles could not select any other model regardless of what the org's provider connection supported. Replace the env-only model list with a ProviderModelCatalog that: - Resolves the org's ACTIVE provider connection credential (BYOK or platform-managed, encrypted via ADR-0024 envelope) - Calls OpenRouter GET /v1/models?supported_parameters=tools to list tool-capable models available to that org - Caches results in-memory with a 5-minute TTL per organization - Falls back to the env-default registry when no ACTIVE provider exists The runtime modelRegistry no longer validates role.defaultModel against the env model list — the admin already validated by selection from the provider catalog. The env list remains as the fallback for roles with null defaultModel. The admin roles page loads models independently (non-blocking) so roles remain editable even if the provider API is slow or unreachable.
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
/**
|
||||
* Org-admin Agent configuration routes (ADR-0017 / ADR-0018).
|
||||
*
|
||||
* Surface for browsing and editing Organization-scoped Agent roles and skills.
|
||||
* Roles are org-owned data; the default-model picker is constrained to the
|
||||
* env-default model registry (ADR-0017: there is no org-scoped model list —
|
||||
* `OrganizationAgentRole.defaultModel` selects from the platform-enabled set).
|
||||
* Surface for browsing and editing Organization-scoped Agent roles, the skills
|
||||
* bound to them, and the model picker. The model list is **derived** from the
|
||||
* org's ACTIVE provider connection via OpenRouter's /v1/models API — there is
|
||||
* no separate model table to maintain. When no ACTIVE provider exists, the
|
||||
* route falls back to the env-default model registry so the admin surface
|
||||
* remains usable.
|
||||
*
|
||||
* Skill management (create, read, edit, disable) is performed in-band through
|
||||
* the deep module `OrganizationAgentConfiguration`, which writes to the same
|
||||
@@ -17,7 +19,9 @@
|
||||
import type { PrismaClient } from "@prisma/client";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { OrganizationAgentConfiguration } from "../../agent/configuration.js";
|
||||
import { ProviderModelCatalog } from "../../connections/providerModelCatalog.js";
|
||||
import { createDefaultModelRegistry } from "../../settings/runtime.js";
|
||||
import type { LocalSecretEnvelope } from "../../security/secretEnvelope.js";
|
||||
import { requireOrgRole, type GuardDeps } from "../auth/guards.js";
|
||||
import { handleRouteError } from "../errors.js";
|
||||
|
||||
@@ -25,6 +29,7 @@ export interface AgentConfigRouteConfig {
|
||||
readonly prisma: PrismaClient;
|
||||
readonly sessionSecret: string;
|
||||
readonly skillStoreRoot: string;
|
||||
readonly secretEnvelope: LocalSecretEnvelope;
|
||||
}
|
||||
|
||||
export async function registerAgentConfigRoutes(
|
||||
@@ -33,6 +38,7 @@ export async function registerAgentConfigRoutes(
|
||||
): Promise<void> {
|
||||
const guardDeps: GuardDeps = { prisma: config.prisma, sessionSecret: config.sessionSecret };
|
||||
const agentConfig = new OrganizationAgentConfiguration(config.prisma, config.skillStoreRoot);
|
||||
const modelCatalog = new ProviderModelCatalog(config.prisma, config.secretEnvelope);
|
||||
|
||||
app.get("/api/org/:orgSlug/agent-roles", async (request, reply) => {
|
||||
try {
|
||||
@@ -226,8 +232,13 @@ export async function registerAgentConfigRoutes(
|
||||
const { orgSlug } = request.params as { orgSlug: string };
|
||||
const auth = await requireOrgRole(request, reply, guardDeps, { orgSlug });
|
||||
if (auth === null) return;
|
||||
const registry = createDefaultModelRegistry(process.env);
|
||||
return { models: registry.listModels() };
|
||||
const providerModels = await modelCatalog.listModels(auth.organization.id);
|
||||
// Fall back to env-default registry when the org has no ACTIVE provider
|
||||
// (or the provider API is unreachable on first load).
|
||||
const models = providerModels.length > 0
|
||||
? providerModels
|
||||
: createDefaultModelRegistry(process.env).listModels();
|
||||
return { models };
|
||||
} catch (err) {
|
||||
return handleRouteError(reply, err);
|
||||
}
|
||||
|
||||
@@ -116,6 +116,7 @@ export async function registerOrgRoutes(app: FastifyInstance, config: OrgRouteCo
|
||||
prisma: config.prisma,
|
||||
sessionSecret: config.sessionSecret,
|
||||
skillStoreRoot: readSkillStoreRoot(),
|
||||
secretEnvelope: config.secretEnvelope,
|
||||
});
|
||||
await registerSessionsAndUsageRoutes(app, {
|
||||
prisma: config.prisma,
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
/**
|
||||
* Provider model catalog: fetches the list of models available to an
|
||||
* Organization from its ACTIVE provider connection (OpenRouter), with an
|
||||
* in-memory TTL cache so the admin model picker stays responsive without
|
||||
* hammering the upstream API on every page load.
|
||||
*
|
||||
* The model list is **derived** from the provider connection — there is no
|
||||
* separate model table to maintain. When an org activates a provider, its
|
||||
* models become selectable; when the provider is disabled, the list empties.
|
||||
*/
|
||||
import type { PrismaClient } from "@prisma/client";
|
||||
import type { LocalSecretEnvelope } from "../security/secretEnvelope.js";
|
||||
import { decryptStoredProviderCredential } from "./providerConnections.js";
|
||||
|
||||
/** A model entry surfaced to the admin model picker. */
|
||||
export interface ProviderModelEntry {
|
||||
readonly id: string;
|
||||
readonly label: string;
|
||||
readonly toolCapable: boolean;
|
||||
}
|
||||
|
||||
/** A model entry as returned by OpenRouter's /v1/models endpoint. */
|
||||
interface OpenRouterModel {
|
||||
readonly id: string;
|
||||
readonly name: string;
|
||||
readonly supported_parameters: readonly string[];
|
||||
}
|
||||
|
||||
interface OpenRouterModelsResponse {
|
||||
readonly data: readonly OpenRouterModel[];
|
||||
}
|
||||
|
||||
/** Cache entry: models + expiry timestamp. */
|
||||
interface CacheEntry {
|
||||
readonly models: readonly ProviderModelEntry[];
|
||||
readonly expiresAt: number;
|
||||
}
|
||||
|
||||
const DEFAULT_TTL_MS = 5 * 60 * 1000; // 5 minutes
|
||||
|
||||
/**
|
||||
* Per-organization model catalog cache. Keyed by organizationId so that
|
||||
* multiple orgs in the same Silo don't cross-pollute. The cache is intentionally
|
||||
* process-local: it survives for the lifetime of the Hub process and is
|
||||
* invalidated by TTL, not by DB events. A cache miss re-fetches from the
|
||||
* provider API.
|
||||
*/
|
||||
export class ProviderModelCatalog {
|
||||
private readonly cache = new Map<string, CacheEntry>();
|
||||
private readonly fetchImpl: typeof fetch;
|
||||
|
||||
constructor(
|
||||
private readonly prisma: PrismaClient,
|
||||
private readonly secrets: LocalSecretEnvelope,
|
||||
private readonly ttlMs: number = DEFAULT_TTL_MS,
|
||||
fetchImpl: typeof fetch = fetch,
|
||||
) {
|
||||
this.fetchImpl = fetchImpl;
|
||||
}
|
||||
|
||||
/**
|
||||
* List tool-capable models available to the organization's ACTIVE provider
|
||||
* connection. Returns cached results when fresh; otherwise fetches from the
|
||||
* provider API. Falls back to an empty list (not an error) when the org has
|
||||
* no ACTIVE provider — the admin sees the env-default model from the
|
||||
* separate env fallback in the route.
|
||||
*/
|
||||
async listModels(organizationId: string): Promise<readonly ProviderModelEntry[]> {
|
||||
const cached = this.cache.get(organizationId);
|
||||
if (cached !== undefined && cached.expiresAt > Date.now()) {
|
||||
return cached.models;
|
||||
}
|
||||
|
||||
const credential = await this.resolveActiveProviderCredential(organizationId);
|
||||
if (credential === null) return [];
|
||||
|
||||
const models = await this.fetchModelsFromProvider(credential);
|
||||
this.cache.set(organizationId, {
|
||||
models,
|
||||
expiresAt: Date.now() + this.ttlMs,
|
||||
});
|
||||
return models;
|
||||
}
|
||||
|
||||
/** Force a cache invalidation (e.g. after provider connection changes). */
|
||||
invalidate(organizationId: string): void {
|
||||
this.cache.delete(organizationId);
|
||||
}
|
||||
|
||||
private async resolveActiveProviderCredential(
|
||||
organizationId: string,
|
||||
): Promise<{ readonly baseUrl: string; readonly authToken: string; readonly anthropicApiKey: string } | null> {
|
||||
const connection = await this.prisma.organizationProviderConnection.findFirst({
|
||||
where: { organizationId, status: "ACTIVE" },
|
||||
include: { activeSecretVersion: true },
|
||||
});
|
||||
const version = connection?.activeSecretVersion;
|
||||
if (connection === null || version === null || version === undefined || version.retiredAt !== null) {
|
||||
return null;
|
||||
}
|
||||
const payload = decryptStoredProviderCredential(this.secrets, {
|
||||
organizationId,
|
||||
connectionId: connection.id,
|
||||
providerId: connection.providerId,
|
||||
secretVersionId: version.id,
|
||||
envelopeVersion: version.envelopeVersion,
|
||||
keyId: version.keyId,
|
||||
envelope: version.envelope,
|
||||
});
|
||||
return {
|
||||
baseUrl: payload.baseUrl,
|
||||
authToken: payload.authToken,
|
||||
anthropicApiKey: payload.anthropicApiKey,
|
||||
};
|
||||
}
|
||||
|
||||
private async fetchModelsFromProvider(
|
||||
credential: { readonly baseUrl: string; readonly authToken: string; readonly anthropicApiKey: string },
|
||||
): Promise<readonly ProviderModelEntry[]> {
|
||||
const base = credential.baseUrl.endsWith("/") ? credential.baseUrl : `${credential.baseUrl}/`;
|
||||
// Filter to models that support tool calling — the agent runner requires it.
|
||||
const url = new URL("v1/models?supported_parameters=tools", base);
|
||||
const headers: Record<string, string> = {
|
||||
authorization: `Bearer ${credential.authToken}`,
|
||||
accept: "application/json",
|
||||
};
|
||||
if (credential.anthropicApiKey !== "") headers["x-api-key"] = credential.anthropicApiKey;
|
||||
|
||||
const response = await this.fetchImpl(url, {
|
||||
method: "GET",
|
||||
headers,
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`provider models request failed: status ${response.status}`);
|
||||
}
|
||||
const body = (await response.json()) as OpenRouterModelsResponse;
|
||||
return body.data.map((m) => ({
|
||||
id: m.id,
|
||||
label: m.name,
|
||||
toolCapable: m.supported_parameters.includes("tools"),
|
||||
}));
|
||||
}
|
||||
}
|
||||
@@ -182,12 +182,16 @@ export class DatabaseRuntimeSettings implements RuntimeSettings {
|
||||
throw new Error(`organization ${project.organization.id} must have exactly one active default Agent role`);
|
||||
}
|
||||
|
||||
// The model list is the env-based fallback used when a role has no
|
||||
// explicit defaultModel. Role defaultModels are chosen from the provider
|
||||
// model catalog (admin surface) and may not be in the env list — we don't
|
||||
// validate against it at runtime; the admin already validated by selection.
|
||||
const defaults = await this.envSettings.modelRegistry(scope);
|
||||
const enabledModels = new Set(defaults.listModels().map((model) => model.id));
|
||||
const fallbackModels = defaults.listModels();
|
||||
const roles: RoleEntry[] = project.organization.agentRoles.map((role) => ({
|
||||
id: role.roleId,
|
||||
label: role.label,
|
||||
defaultModel: validateRoleModel(role.roleId, role.defaultModel, enabledModels),
|
||||
defaultModel: role.defaultModel ?? undefined,
|
||||
...(role.systemPrompt !== null ? { systemPrompt: role.systemPrompt } : {}),
|
||||
...(role.tools !== null ? { tools: roleToolsFromJson(role.roleId, role.tools) } : {}),
|
||||
skills: role.skillBindings.map((binding): RoleSkillEntry => {
|
||||
@@ -210,7 +214,7 @@ export class DatabaseRuntimeSettings implements RuntimeSettings {
|
||||
};
|
||||
}),
|
||||
}));
|
||||
return new InMemoryModelRegistry(defaults.listModels(), roles);
|
||||
return new InMemoryModelRegistry(fallbackModels, roles);
|
||||
}
|
||||
|
||||
runPolicy(input: RunPolicyInput): Promise<RunPolicy> {
|
||||
@@ -225,15 +229,6 @@ function roleToolsFromJson(roleId: string, value: Prisma.JsonValue): readonly st
|
||||
return value as string[];
|
||||
}
|
||||
|
||||
function validateRoleModel(
|
||||
roleId: string,
|
||||
model: string | null,
|
||||
enabledModels: ReadonlySet<string>,
|
||||
): string | undefined {
|
||||
if (model === null) return undefined;
|
||||
if (!enabledModels.has(model)) throw new Error(`role ${roleId} selects unavailable model ${model}`);
|
||||
return model;
|
||||
}
|
||||
|
||||
async function loadActiveProviderSecret(
|
||||
tx: Prisma.TransactionClient,
|
||||
|
||||
Reference in New Issue
Block a user