Files
Davide Silvestri 22cd0eda09 feat: warn about AI model usage instead of silently breaking consumers (#6075)
* feat: report AI provider model usage per feature type

Feature types now count the rows they store referencing a provider model,
so the admin can be warned before disabling, deleting or narrowing one
instead of being blocked. Counting is by provider type and model
identifier because consumers do not store the provider row id, which
makes the result an upper bound: fit to warn, never to block.

Default-model features are excluded, their RESTRICT foreign key already
blocks the change.

* feat: count AI field and AI Agent references to a provider model

Both features store a provider type and model identifier per row rather
than a reference to the model, so nothing told an administrator what a
model change would break. They now count their own rows, skipping every
consumer whose owner is trashed: a field's table, database or workspace,
and an agent service's automation node, builder workflow action,
integration or application.

* feat: expose AI provider model usage over the API

GET /ai-providers/models/<id>/usage/ reports the per-consumer counts to
warn about and, separately, the default-model features that refuse the
change outright. Kuma holds a restricted foreign key, so naming it up
front lets the client say which selection to repoint instead of letting
the admin discover it through a generic error.

Permissions and scoping mirror the sibling model endpoints, so a
workspace-scoped caller cannot read an instance model's usage.

* feat: warn before an AI model change breaks its consumers

Disabling, deleting, renaming or narrowing a model now names what
depends on it and proceeds on confirmation. Disabling was entirely
unguarded before, and a rename orphans consumers just as thoroughly as
a delete because they persist the identifier string.

The counts are an upper bound: an instance model and a workspace model
sharing an identifier are indistinguishable to a consumer, so the copy
says a feature uses the model rather than claiming it will break. A
failed lookup degrades to the plain confirmation instead of trapping
the administrator.

* fix: keep an unavailable AI model visible in the AI field form

The form dropped a saved provider or model as soon as it stopped being
available, leaving empty dropdowns that hid what the field had been
configured with. It now retains the saved selection, disabled and marked
unavailable, the way the AI Agent node form already does, while the
validator keeps reading the genuine list so the field cannot be saved
until a real model is picked.

* docs: describe the AI provider model usage warning

* refactor: drop redundant blocking feature types from model usage

The usage endpoint pre-announced the features whose selection refuses a
change, but ERROR_AI_PROVIDER_MODEL_IN_USE already names them and the
frontend already renders that detail. Worse, the modal applied the list
only on rename, the one path that is never refused: Kuma holds a foreign
key to the model row, so the selection follows the rename instead of
breaking. The same claim was in the installation docs; both are corrected
to say what actually happens.

Also drop the `trashed=False` filters from both model reference counts.
AIAgentService.objects and AIField.objects are no-trash managers, so the
generated SQL already excludes trashed rows.

Rename the endpoint's operation id to get_ai_provider_model_usage: the
documented response is an object, not a list.

* fix: address Qodo review of the model usage warning

Count a service only while a live owner still reaches it. The previous
exclusion dropped the whole service as soon as any related builder action
was trashed, so a service shared by a live and a trashed action vanished
from the warning. Both owner managers already encode which ancestors count
as trashed, so the check reuses them through EXISTS instead of restating
the rules.

Treat a failed usage lookup as unknown rather than zero. Returning empty
counts made every caller skip its confirmation, letting a rename orphan
consumers with no warning at all; the dialog now says the check failed and
still lets the admin proceed.

Cover fetchModelUsage directly: payload forms, workspace scoping and the
snake_case to camelCase mapping had no store test.
2026-09-17 16:31:08 +02:00

59 lines
2.1 KiB
JavaScript

/**
* Extract the user facing message of an AI provider API error.
*
* The backend spells out which model an AI feature still resolves through, so
* that detail is preferred over the generic fallback the caller provides.
*/
export function aiProviderErrorMessage(error, fallbackMessage = '') {
const detail = error.response?.data?.detail
return detail?.message || detail || fallbackMessage
}
/**
* Name one AI feature the way its own module spells it.
*
* @param {Object} registry The application registry.
* @param {string} featureType The registered feature identifier.
* @returns {string} The feature display name, or the raw type when the feature
* is not loaded in this installation.
*/
export function aiProviderFeatureName(registry, featureType) {
return registry.exists('aiProviderModelFeature', featureType)
? registry.get('aiProviderModelFeature', featureType).getName()
: featureType
}
/**
* Prefix a confirmation description with what still depends on a model.
*
* @param {{usage: Array<{featureType: string, count: number}>,
* failed: boolean}} result The model usage lookup.
* @param {string} description The action description shown after the counts.
* @param {Function} $t The translation function of the calling component.
* @param {Object} registry The application registry.
* @returns {string} The description, prefixed with the dependants.
*/
export function aiProviderModelUsageMessage(result, description, $t, registry) {
const used = result.usage.filter((entry) => entry.count > 0)
const sentences = []
if (result.failed) {
sentences.push($t('aiProviderAdmin.modelUsageUnknown'))
}
if (used.length > 0) {
sentences.push(
$t('aiProviderAdmin.modelInUse', {
features: used
.map((entry) =>
$t('aiProviderAdmin.modelUsageFeature', {
count: entry.count,
feature: aiProviderFeatureName(registry, entry.featureType),
})
)
.join(', '),
count: used.reduce((total, entry) => total + entry.count, 0),
})
)
}
return [...sentences, description].join(' ')
}