Files
AgentCompany/src/main/connector-model-policy.ts
T
ZacharyZhang-NY d75bfd5851 refactor(model-pool): scope disabled-models by company; cascade on delete (v42)
The per-connector "enabled/disabled model" settings lived under a
global key in the settings key-value table
(`disabled_models:${connectorId}`). No company dimension. Deleting a
company left the pool choices behind, and creating a new company
silently inherited them — the visible symptom was "onboarding's team-
model configuration survives company deletion and pollutes the next
company's CEO model list."

Root fix, not a cleanup hook: the storage becomes a real relational
table with `(company_id, connector_id)` primary key and an
`ON DELETE CASCADE` FK from `companies`. Company deletion wipes pool
config by construction; no cleanup step in `deleteCompany()` can
drift.

Schema (migration v42):
- CREATE TABLE connector_model_pool_settings (company_id, connector_id,
  disabled_models_json, updated_at), PK (company_id, connector_id),
  FK companies(id) ON DELETE CASCADE.
- Drop every `settings.disabled_models:*` key — these were the bug's
  residue; there is no right company to attribute them to, so the
  migration clears them. Users re-select on next onboarding.

API surgery — every reader/writer now takes companyId:
- db.getDisabledModels(companyId, connectorId)
- db.setDisabledModels(companyId, connectorId, models)
- db.hasModelPoolSettings(companyId, connectorId)
- connector-model-policy: getConnectorModelPolicy(deps, companyId,
  connectorId); validateConnectorModelSelection(deps, companyId,
  connectorId, model)
- model-dispatch: listEnabledModels(companyId, deps);
  pickModelForAgent resolves companyId from agent.companyId
- orchestration heartbeat/task-runner/turn-context/run-dispatch pass
  the agent's or run's companyId through to the policy deps
- IPC schema: connectorDisabledModelsSchema gains companyId; new
  connectorModelPoolQuerySchema pairs companyId+connectorId for reads

Onboarding becomes commit-on-launch:
- useConnectorModelSelection no longer persists via IPC during
  onboarding. Toggles mutate local state; it exposes `poolSelections`
  to the wizard.
- bootstrapOnboardingSchema gains connectorPoolSelections. The
  onboarding service saves the company first, then persists each
  per-connector disabled list scoped to the new company id — CEO
  model validation runs against the company's freshly-persisted pool,
  not some stale global.
- IPC setConnectorDisabledModels now requires companyId; the only
  consumer was onboarding, which no longer calls it at all.
- Main-side listConnectorModels returns the raw provider catalog
  (no pool filtering), since that IPC is used from onboarding where
  no company exists yet; pool filtering happens at dispatch time.

saveConnector IPC drops the per-company pool validation — connector
config is a global hint, company-scoped enforcement belongs at run
dispatch where the company is known.

Tests: 603/603 green. Typecheck clean. Integration tests rewritten
to set the pool via connectorPoolSelections on bootstrap input, not
a pre-bootstrap global setDisabledModels call.
2026-04-18 22:14:09 -04:00

104 lines
3.6 KiB
TypeScript

import type { ConnectorId } from "@shared/types";
import { getConnectorDefinition } from "./connectors";
import { resolveModels } from "./model-resolver";
interface ConnectorModelPolicyDeps {
getConnector: (id: ConnectorId) => { id: ConnectorId; label?: string | null; model: string | null };
getDisabledModels: (companyId: string, connectorId: string) => Array<{ id: string; label: string }>;
}
export interface ConnectorModelPolicyResult {
connectorId: ConnectorId;
connectorLabel: string;
enabledModels: Array<{ id: string; label: string }>;
defaultModel: string | null;
}
function uniqueModels(models: Array<{ id: string; label: string }>) {
const byId = new Map<string, { id: string; label: string }>();
for (const model of models) {
const id = model.id.trim();
if (!id) continue;
if (!byId.has(id)) {
byId.set(id, { id, label: model.label || id });
}
}
return Array.from(byId.values());
}
/**
* Returns the models available for a given pi-ai provider id, going through
* `resolveModels` so that providers not in pi-ai's built-in catalog (currently
* the two Fireworks connectors, which serve `accounts/fireworks/routers/...`
* and curated `accounts/fireworks/models/...` ids) are represented in the UI
* model picker instead of collapsing to an empty list.
*/
function listProviderModels(provider: string): Array<{ id: string; label: string }> {
return resolveModels(provider).map((model) => ({ id: model.id, label: model.name }));
}
export function getConnectorModelPolicy(
deps: ConnectorModelPolicyDeps,
companyId: string,
connectorId: ConnectorId,
): ConnectorModelPolicyResult {
const connector = deps.getConnector(connectorId);
const definition = getConnectorDefinition(connectorId);
const disabledModels = new Set(deps.getDisabledModels(companyId, connectorId).map((model) => model.id));
const catalog = listProviderModels(definition.provider);
const enabledModels = uniqueModels(catalog).filter((model) => !disabledModels.has(model.id));
return {
connectorId,
connectorLabel: connector.label?.trim() || definition.label,
enabledModels,
defaultModel: connector.model?.trim() || definition.defaultModel,
};
}
export function validateConnectorModelSelection(
deps: ConnectorModelPolicyDeps,
companyId: string,
connectorId: ConnectorId,
model: string | null | undefined,
): { ok: true; effectiveModel: string | null } | { ok: false; error: string } {
const policy = getConnectorModelPolicy(deps, companyId, connectorId);
const selectedModel = model?.trim() || null;
if (policy.enabledModels.length === 0) {
return {
ok: false,
error: `${policy.connectorLabel} has no enabled models available.`,
};
}
if (selectedModel) {
if (!policy.enabledModels.some((entry) => entry.id === selectedModel)) {
return {
ok: false,
error: `${policy.connectorLabel} does not allow model "${selectedModel}".`,
};
}
return { ok: true, effectiveModel: selectedModel };
}
if (policy.defaultModel && policy.enabledModels.some((entry) => entry.id === policy.defaultModel)) {
return { ok: true, effectiveModel: policy.defaultModel };
}
return { ok: true, effectiveModel: policy.enabledModels[0]?.id ?? null };
}
/**
* Pi-mono catalog does not have free-form compat connectors; every connector
* has a fixed pi-ai provider. The configuration-level model validator is a
* no-op retained for IPC contract compatibility.
*/
export function validateConnectorConfigurationModel(
_connectorId: ConnectorId,
_model: string | null | undefined,
): { ok: true } | { ok: false; error: string } {
return { ok: true };
}