Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

fix(chat): stop asking cloud users to set up an AI provider they already have (#15594)

Hazem Adel4dd1d0e65986
diff --git a/brain/knowledge/ai-intelligence/ai-providers.md b/brain/knowledge/ai-intelligence/ai-providers.md
index 0b7a200cace..cf3b4470af8 100644
--- a/brain/knowledge/ai-intelligence/ai-providers.md
+++ b/brain/knowledge/ai-intelligence/ai-providers.md
@@ -59,6 +59,7 @@ renders and preserves every real price; the cheapest in the set is 0.01.
- **The Agent and the Direct AI steps disagree about whether OpenAI does web search, and both are right.** `WEB_SEARCH_MODE_BY_PROVIDER` has no `OPENAI` entry, so `supportsWebSearch(OPENAI)` is `false` — the Agent asks that question before offering the tool and therefore has never had OpenAI web search (deliberately: it needs the Responses API, which breaks legacy BYOK models). Ask AI never asks it; it builds the tool straight from its own props and has had OpenAI web search all along. So `supportsWebSearch` is the *agent's* policy, not a statement of fact about the provider — reusing it as a general gate silently removes a capability Ask AI users have today. Whoever unifies the two paths has to keep the OpenAI branch reachable without routing it through that gate. The two paths also disagree about the unsupported tail: the piece's `webSearch` prop is a plain checkbox with no provider gating, and `createWebSearchTool` ends in `default: throw new Error('Provider X is not supported for web search')` — so ticking it on Azure, Mistral, xAI, or a Cloudflare Gateway model whose submodel prefix is not one of the four in `CF_GATEWAY_SUBMODEL_TO_PROVIDER` **fails the step**. The server's `buildWebSearchTools` returns `{}` for the same input, because the agent gates before calling it. The builder cannot simply be made to throw: `supportsWebSearch` is true for the `plugin` providers (`openrouter`, `activepieces`), the agent calls the builder for them, and there is no native entry for either because the plugin rides on the model settings instead — so `{}` is the correct answer there. The piece has the same shape, returning early for OpenRouter before the switch that throws. Hence two entry points: `buildWebSearchTools` stays total for callers that gate first, `buildWebSearchToolsOrThrow` reproduces the piece's error for callers that do not.
- **`aiProviderModule` is registered unconditionally, `agentModule` is not.** In `app.ts`, `aiProviderModule` sits outside the edition switch, while `agentModule` appears only in the `CLOUD` and `ENTERPRISE` arms — so an AI route hung off the provider module is on Community for free, and the same route hung off the agent module 404s there. That asymmetry is the cheapest way to satisfy [000036](../decisions/000036-the-worker-side-ai-path-is-community-edition-core.md): pick the module and there is no edition branch left to get wrong.
+- **Chat falls back to the managed Activepieces row, so "no key is flagged for chat" is no longer a wall.** `pickChatRow` takes the `enabledForChat` row if there is one and otherwise the visible `ACTIVEPIECES` row; `findAvailableChatProviderRow` and both list endpoints share it, so what the picker shows is what a turn will actually run on. Before that, a platform whose managed row sat at `enabledForChat = false` — and nothing else flagged — got `SetupRequiredState` ("Set up an AI provider to get started") a second after the chat loaded. Migration `1802…AddChatRolloutFreeCreditGrant` was meant to repair exactly that state and matched zero rows: it compared `"provider" = 'ACTIVEPIECES'` while `AIProviderName` values are stored lowercase (`'activepieces'`). Enum values in raw SQL are the lowercase string, never the TS member name — grep the enum before writing the WHERE clause.
- **Multi-key resolution is deterministic, not configurable.** When several keys of one provider are eligible for a project, `resolveEligibleRow` picks by most specific `projectScope` (`selected` > `except` > `all`), newest `created` breaking ties — there is no priority/default field (decision: [providers-redesign-before-routing](../decisions/providers-redesign-before-routing.md)). The ACTIVEPIECES provider stays a singleton — `create()` rejects it (`aiProvider.activepiecesIsManaged`). That ranking is the *fallback*: a step or agent may also pin a key outright (decision: [000030](../decisions/000030-a-step-may-pin-an-ai-provider-key-and-omitting-one-can-only-narrow.md)), in which case `resolveRowForScope` serves that row after checking it is eligible for the caller's project.
- **Every resolver takes a required `ProviderScope`; there is no "no project" default** (decision: [000027](../decisions/000027-ai-provider-resolution-takes-a-required-scope-and-splits-reads-by-trust-level.md)). `getConfigOrThrow` / `getChatProvider` / `getChatProviderName` / `listModels` all take `scope: { type: 'project', projectId } | { type: 'platform' }`. This is deliberate and load-bearing: the first cut made `projectId` optional and treated its absence as "every key is eligible", so each new call site that forgot to thread it silently bypassed project scoping — the agent piece/knowledge-base tool handlers, the chat model picker, and the `configId` model lookup each reopened the same hole in turn. Making the argument required turns an omission into a compile error, and `{ type: 'platform' }` at a call site is a reviewable claim rather than an accident. Only three consumers are legitimately platform-wide: the tool-search embedder, chat memory extraction, and the managed-ACTIVEPIECES singleton.
@@ -129,7 +130,7 @@ renders and preserves every real price; the cheapest in the set is 0.01.
- **A short model list in the key's picker is the vendor's catalog, not a truncation.** `listModels` returns whatever the provider's own `/models` endpoint gives and filters nothing except the key's `modelScope` allow-list. Anthropic ships roughly a dozen models, OpenAI ~80 (mostly embeddings/tts/whisper), while OpenRouter is an aggregator and returns 400+ from every vendor it proxies — so the counts differ by an order of magnitude by design. Anthropic pages at 20 by default, which is why its request pins `?limit=1000`. If the list shows exactly three Claude models, that is the *chat* dropdown reading the curated `ANTHROPIC_CHAT_MODELS`, a different surface from the admin picker.
- **Read the chat model list through `aiProviderUtils.getCuratedChatModels({ provider })`.** It is the one accessor the server resolver (`agentHelpers.resolveModelIdForProvider`) and the chat dropdown share, so the two cannot drift; it returns `{ id, label }` or `undefined` — never an empty list, so callers may treat a result as non-empty. Labels come from the hardcoded `CHAT_MODEL_LABELS` (falling back to the id) rather than `AIProviderModel.name`, because the live `listModels` response cannot supply one for every provider: anthropic returns `display_name` and google `displayName`, but OpenAI's `/v1/models` returns ids only.
- **`conversation.modelName` carries either a tier id or a real model id** — it is a free string with no discriminator. A legacy tier id resolves to the tier's equivalent model when the provider ships it, else the provider's first curated model, so old conversations keep working after a provider switch. Note `premium` maps to opus 4.8, which the native anthropic list does not carry, so a legacy `premium` on anthropic lands on Sonnet.
-- Chat-provider resolution is **first `enabledForChat` row wins**, *not* "prefer ACTIVEPIECES". All three branches of `findAvailableChatProviderRow` reduce to that: when the managed provider is visible the function returns `chatProviders[0]` whatever it is, so a platform with `[openai, activepieces]` both chat-enabled resolves to **openai**. The client mirror is `aiProviderQueries.useChatProvider()` (`providers.find((p) => p.enabledForChat)`) — always read the resolved chat provider through it rather than re-deriving the rule inline. **`enabledForChat` on a deduped project entry must be an OR across that provider's keys, never the top-ranked key's flag** — ranking (`selected` > `except` > `all`, newest first) and chat selection answer different questions, so reading `rows[0].enabledForChat` makes the client report "no provider configured" whenever the chat-enabled key is not the ranking winner, while the server (`findAvailableChatProviderRow`, which queries `enabledForChat: true` directly) happily serves the turn. Invisible with one key per provider. Both sides lean on an unordered `findBy()`: there is no `ORDER BY`, so "first" is not guaranteed stable when several providers are chat-enabled.
+- Chat-provider resolution is **first `enabledForChat` row wins, else the visible managed ACTIVEPIECES row** — `pickChatRow`, shared by `findAvailableChatProviderRow` and both list endpoints. So a platform with `[openai, activepieces]` both chat-enabled still resolves to **openai** (an explicit pick beats the fallback), while a platform with nothing flagged resolves to the managed row instead of to nothing. The flag a list endpoint reports is therefore the *resolved* one, not the stored column — `ai_provider.enabledForChat` can say `false` on the row chat actually runs on, and `ensureManagedProviderRow` no longer writes it. The client mirror is `aiProviderQueries.useChatProvider()` (`providers.find((p) => p.enabledForChat)`) — always read the resolved chat provider through it rather than re-deriving the rule inline. A project entry is chat-enabled when it owns the picked row, whichever of its keys carries the flag: ranking (`selected` > `except` > `all`, newest first) and chat selection answer different questions, so reading `rows[0].enabledForChat` makes the client report "no provider configured" whenever the chat-enabled key is not the ranking winner. `findBy()` has no `ORDER BY`, so "first" is not guaranteed stable when several providers are chat-enabled.
- Listing providers is **not a pure read**: both `listConfigs` and `listForProject` go through `listVisibleRows`, which inserts the ACTIVEPIECES provider row when `aiCreditsEnabled && !activepiecesExists`. A `GET /v1/ai-providers` can therefore create a row. It also applies the hidden-provider filter (`plan.embeddingEnabled` hides the managed provider), which is why the client can trust its output without re-checking flags.
- **A managed chat turn is charged in two events, not one.** The model cost arrives per model call from
the billing wrapper, and the turn's own-key credit and tool calls are charged by `chatToolBilling.chargeForLatestTurn`
diff --git a/packages/server/api/src/app/ai/ai-provider-service.ts b/packages/server/api/src/app/ai/ai-provider-service.ts
index 82706fb738e..bbde89f949a 100644
--- a/packages/server/api/src/app/ai/ai-provider-service.ts
+++ b/packages/server/api/src/app/ai/ai-provider-service.ts
@@ -36,19 +36,21 @@ export const aiProviderService = (log: FastifyBaseLogger) => ({
async listConfigs(platformId: PlatformId): Promise<AIProviderWithoutSensitiveData[]> {
const rows = await listVisibleRows({ platformId, log })
- return rows.map(toConfigResponse)
+ const chatRowId = pickChatRow(rows)?.id
+ return rows.map((row) => toConfigResponse({ row, enabledForChat: row.id === chatRowId }))
},
async listForProject({ platformId, projectId }: { platformId: PlatformId, projectId: string }): Promise<ProjectAIProvider[]> {
const rows = await listVisibleRows({ platformId, log })
const eligible = rows.filter((row) => rowAllowsScope({ row, scope: { type: 'project', projectId } }))
const ranked = rankRows(eligible)
+ const chatRow = pickChatRow(ranked)
return unique(ranked.map((row) => row.provider)).map((provider) => {
const rows = ranked.filter((row) => row.provider === provider)
return {
provider,
name: aiProviders[provider].name,
- enabledForChat: rows.some((row) => row.enabledForChat === true),
+ enabledForChat: provider === chatRow?.provider,
keys: rows.map((row) => ({ id: row.id, name: row.displayName })),
}
})
@@ -91,7 +93,7 @@ export const aiProviderService = (log: FastifyBaseLogger) => ({
statusReason: null,
statusUpdated: new Date().toISOString(),
})
- return toConfigResponse(saved)
+ return toConfigResponse({ row: saved, enabledForChat: false })
},
async update(platformId: PlatformId, providerId: string, request: UpdateAIProviderRequest): Promise<void> {
const aiProvider = await aiProviderRepo().findOneBy({
@@ -291,13 +293,13 @@ function provedHealthy(): { status: AiProviderKeyStatus, statusReason: null, sta
return { status: 'active', statusReason: null, statusUpdated: () => 'now()', statusVersion: () => '"statusVersion" + 1' }
}
-function toConfigResponse(row: AIProviderSchema): AIProviderWithoutSensitiveData {
+function toConfigResponse({ row, enabledForChat }: { row: AIProviderSchema, enabledForChat: boolean }): AIProviderWithoutSensitiveData {
return {
id: row.id,
name: row.displayName,
provider: row.provider,
config: row.config,
- enabledForChat: row.enabledForChat ?? false,
+ enabledForChat,
modelScope: row.modelScope,
modelIds: row.modelIds,
projectScope: row.projectScope,
@@ -313,7 +315,6 @@ async function ensureManagedProviderRow({ platformId }: { platformId: PlatformId
if (exists) {
return
}
- const hasChatProvider = await aiProviderRepo().existsBy({ platformId, enabledForChat: true })
// Two concurrent readers both miss the existsBy above, so the row is inserted with
// ON CONFLICT DO NOTHING and idx_ai_provider_platform_id_managed - a partial unique
// index on (platformId) WHERE provider = 'activepieces' - decides which one lands.
@@ -326,7 +327,6 @@ async function ensureManagedProviderRow({ platformId }: { platformId: PlatformId
provider: AIProviderName.ACTIVEPIECES,
displayName: 'Activepieces',
platformId,
- enabledForChat: !hasChatProvider,
modelScope: 'all',
modelIds: [],
projectScope: 'all',
@@ -461,13 +461,23 @@ async function decryptRowAuth({ aiProvider, platformId }: { aiProvider: AIProvid
}
async function findAvailableChatProviderRow({ platformId, scope, log }: { platformId: PlatformId, scope: ProviderScope, log: FastifyBaseLogger }): Promise<AIProviderSchema | null> {
- const allChatProviders = await aiProviderRepo().findBy({ platformId, enabledForChat: true })
- const chatProviders = allChatProviders.filter((row) => rowAllowsScope({ row, scope }))
- if (!chatProviders.some((chatProvider) => chatProvider.provider === AIProviderName.ACTIVEPIECES)) {
- return chatProviders[0] ?? null
+ const candidates = await aiProviderRepo().findBy([
+ { platformId, enabledForChat: true },
+ { platformId, provider: AIProviderName.ACTIVEPIECES },
+ ])
+ const rows = candidates.filter((row) => rowAllowsScope({ row, scope }))
+ const chatRow = pickChatRow(rows)
+ if (chatRow?.provider !== AIProviderName.ACTIVEPIECES) {
+ return chatRow
}
const activepiecesHidden = await isActivepiecesAiProviderHidden({ platformId, log })
- return chatProviders.find((chatProvider) => chatProvider.provider !== AIProviderName.ACTIVEPIECES || !activepiecesHidden) ?? null
+ return activepiecesHidden ? pickChatRow(rows.filter((row) => row.provider !== AIProviderName.ACTIVEPIECES)) : chatRow
+}
+
+function pickChatRow(rows: AIProviderSchema[]): AIProviderSchema | null {
+ return rows.find((row) => row.enabledForChat)
+ ?? rows.find((row) => row.provider === AIProviderName.ACTIVEPIECES)
+ ?? null
}
async function isActivepiecesAiProviderHidden({ platformId, log }: { platformId: PlatformId, log: FastifyBaseLogger }): Promise<boolean> {
diff --git a/packages/server/api/test/integration/ce/ai-provider/ai-provider.test.ts b/packages/server/api/test/integration/ce/ai-provider/ai-provider.test.ts
index d8bf2719760..727230e66ce 100644
--- a/packages/server/api/test/integration/ce/ai-provider/ai-provider.test.ts
+++ b/packages/server/api/test/integration/ce/ai-provider/ai-provider.test.ts
@@ -518,6 +518,39 @@ describe('AI Providers API', () => {
expect(customRow.enabledForChat).toBe(true)
})
+ it('serves chat from the managed Activepieces key when no key is enabled for chat', async () => {
+ process.env.AP_OPENROUTER_PROVISION_KEY = 'test-provision-key'
+ try {
+ await mockAndSaveAIProvider({
+ platformId: ctx.platform.id,
+ provider: AIProviderName.ACTIVEPIECES,
+ displayName: 'Activepieces',
+ enabledForChat: false,
+ })
+ await mockAndSaveAIProvider({
+ platformId: ctx.platform.id,
+ provider: AIProviderName.CUSTOM,
+ displayName: 'Not picked for chat',
+ config: customConfig('https://byo.example.com'),
+ })
+
+ const response = await engineGet('/api/v1/ai-providers', ctx.project.id)
+
+ expect(response?.statusCode).toBe(StatusCodes.OK)
+ const chatProviders = response?.json().filter((p: { enabledForChat: boolean }) => p.enabledForChat)
+ expect(chatProviders.map((p: { provider: string }) => p.provider)).toEqual([AIProviderName.ACTIVEPIECES])
+
+ const served = await aiProviderService(app!.log).getChatProvider({
+ platformId: ctx.platform.id,
+ scope: { type: 'project', projectId: ctx.project.id },
+ })
+ expect(served?.provider).toBe(AIProviderName.ACTIVEPIECES)
+ }
+ finally {
+ delete process.env.AP_OPENROUTER_PROVISION_KEY
+ }
+ })
+
it('lists a specific key models through the admin configuration route', async () => {
const olderModels = [
{ modelId: 'older-model', modelName: 'Older model', modelType: AIProviderModelType.TEXT },