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
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
feat(agents): each chat picks its own model and key, with no single chat key (#16474)
Louai Boumedienebab4588c5ebd
diff --git a/brain/knowledge/ai-intelligence/ai-agents.md b/brain/knowledge/ai-intelligence/ai-agents.mdindex fd063620bdb..25a1c3c6e5b 100644--- a/brain/knowledge/ai-intelligence/ai-agents.md+++ b/brain/knowledge/ai-intelligence/ai-agents.md@@ -31,6 +31,8 @@ Three things follow. `run_agent` has two modes: pointed at a saved agent (`agent### Gotchas+- **A chat stores its own model pick, and a chat saved before that pins its key on its next turn.** `agent_conversation` holds a tier (`modelTierId`) or a key + model (`providerConfigId` + `modelName`); a Credits pick is the managed row id plus a credits tier id. A row with only `modelName` (old web client, or any chat from before 0.93.0) runs on today's Default-AI-key path once, then `agentChatModel.legacyPin` stores the key and the model it resolved to. A published tier slug (`fast`/`smart`/`premium`) stays a slug on every key, or an own-key `premium` chat would think at the default budget from turn 2; an unknown slash-free name on an Activepieces/OpenRouter key stores the tier that actually ran, because the pinned path has no fallback. The pin is saved under a `FOR SHARE` lock on the key row and only if the key still serves the chat, and key delete removes the row before it unpins, so a key deleted mid-turn is never pinned. The write is conditional on the row still being unpinned, so a model change made during the turn wins. A chat with nothing stored gets the Default tier, else the Default AI key path, else the picker's first option, and stores it.+- **Only a pinned chat fails on an unknown model.** A pinned pick whose model or key went away fails with "The model this chat uses isn't available anymore" (S11). A legacy row keeps the old silent fallback (`curated[0]` / default tier), so an upgrade never breaks a chat. Deleting a key, or narrowing its projects or models, unpins the chats it no longer serves (`aiUsageHooks.unpinConversationsFromKey`), and they re-pin on the next turn. The worker's `modelName` write-back skips pinned rows: it sends the tier slug back, which would replace a real model id. A chat run reads its model and key from the freshly loaded conversation, never from the job's `modelName`: the job carries the model from enqueue time, and a pick changed while the message waited would pair an old model with a new key. Billing labels a turn by the worker's `answeredBy` (sent on every turn), not by the conversation's current pick, which can change mid-turn. A pinned Activepieces/OpenRouter tier slug never falls back: an unpublished slug, or one whose model the key's `modelIds` excludes, fails with S11. A key pick needs a chat with a project: the offer check (`aiModelOptionsService.list`) is per project, and the run path passes own-key names straight through.- **Deleting an agent locks its row before it checks for published flows.** `agentService.delete` takes `FOR UPDATE` on the row (`lockedAgentInProjectOrThrow`) and only then runs the `NOT EXISTS` delete as a fresh statement. Publishing takes `FOR SHARE` in `assertAgentsResolveInProject`. A bare `DELETE ... WHERE NOT EXISTS` is not enough: if it blocks on a publisher's `FOR SHARE`, it wakes with the snapshot it started with and deletes an agent the publish just linked. The integration tests run on PGlite, one connection, so the race cannot be reproduced there; the tests assert the lock modes are requested instead.- **A managed agent row or Run Agent step stores a tier id (`smart`) from ENG-754 on; nothing converts the rows saved before.** `AIModelSelector` (`web/src/features/agents/ai-model/`) lists the published flow tiers by id for the managed provider and never calls `/models` for it; a blank picker pre-fills the published `flow.defaultTierId` once the tiers query settles (it used to take the first tier, Fast), and `withDefaultModel` (`agent-service.ts`) seeds the same default tier id on create. Own-key providers still store concrete ids: `resolveNamedModelId` passes their name through verbatim, so a tier id there would reach the provider as a model name. An older row keeps its concrete id: it runs that model (slash rule), shows the raw id in the picker, and does not follow a re-point until the ENG-753 migration.- **A flow-surface run takes its thinking budget from the tier it names, not the default tier.** `resolveRunTier` (`agent-model-resolution.ts`) builds `config.tier` for FLOW_STEP and AGENT runs: a managed tier id gives that tier's `thinkingBudget`, `id` and `modelId`; a concrete id, an own-key model, a tier the file dropped, or no model at all gives the flow default, as every flow-surface run did before ENG-754. So a Heavy agent thinks at Heavy's budget and reports `premium` in `chat.tier`, while a Heavy row saved as `anthropic/claude-opus-4.8` still thinks at Expert's until it is migrated.@@ -111,6 +113,7 @@ Paths verified 2026-07-17. An earlier version pointed at `packages/core/shared/s### Knowledge base gotchas+- **Upload and query embed with one key: the Embeddings capability, else the Default AI key** (`knowledgeBaseService.embeddingModelFor`). The query ignores the run key the worker sends. Changing that key does not re-embed anything: files uploaded before keep the old provider's vectors (same 768 dimensions, so nothing errors) and match poorly until they are uploaded again.- **An upload is embedded when it arrives, or refused.** `knowledgeBaseService.uploadFile` chunks and embeds the file in memory before writing anything, then saves the blob and writes the file record and its chunks in one transaction (the blob is deleted if that fails). It refuses with `KNOWLEDGE_BASE_NEEDS_AI_PROVIDER` when the project has no chat provider and `KNOWLEDGE_BASE_FILE_HAS_NO_TEXT` when nothing searchable comes out; the upload dialog maps both to translated messages. Embeddings bill like any other managed-provider call. Files uploaded before this fix are indexed on the agent's first search of them (`embedMissingChunks`, run only from the agent's knowledge-base tool, never from the read-only `/search` route). If that fails, the agent says the file could not be indexed, and uploading it again shows why.- **`knowledge_base_chunk` is created by a migration that records itself as run even when pgvector is absent.** A database that gains pgvector later never gets the table, because the migration is already marked complete. Deleting its row from `migrations` replays it safely, since the DDL is `CREATE TABLE IF NOT EXISTS`.- **Embeddings are stored at a fixed 768 dimensions, and most models do not return that.** `text-embedding-3-small` answers 1536, and the `dimensions` provider option is namespaced under `openai`, so the OpenRouter and managed paths never see it. `aiUtils.toStorageEmbedding` truncates and re-normalises instead, which is what the option does server-side and works whatever the provider returns. This only holds for Matryoshka-trained models — adding a model that is not one will truncate badly and silently.@@ -124,3 +127,5 @@ Paths verified 2026-07-17. An earlier version pointed at `packages/core/shared/s- **A step only persists the inputs its pinned piece version declares.** `validateProps` in `flow-version-validator-util.ts` builds `cleanInput` from `Object.keys(propsSchema.shape)`, so any key the pinned version does not know is dropped on save with no error. This bit the agent picker: `AgentLink` rendered on every Run Agent step, but `agentId` only exists from `@activepieces/[email protected]`, so on an older pinned step linking looked like it worked and was gone on reload. Anything that writes a new prop from a custom component has to gate on that prop being in the step's `selectedAction.props`, not on a plan flag. Two dev-environment corollaries: `packages/web` consumes `@activepieces/core-piece-types` from its **built dist**, so a branch that adds an enum member needs that package rebuilt before web typechecks, and the API caches dev piece metadata in memory, so a piece version bump needs the API restarted before the builder pins the new version. **You cannot fake the new version locally to test the prop**: inserting the row into `piece_metadata` by hand works for a few minutes and then `pieceSyncService` reconciles the table against the published registry and deletes a version that is not there, so the builder goes back to pinning the old one. A prop added to a piece is only exercisable end to end in the builder once that piece version is actually published, which is why the linked-step behaviour is pinned by `agent-run-link.test.ts` at the API level rather than by a browser pass.- **A queue failure reading `RPC [getAgentConfig] handler threw: VALIDATION` hides the real reason.** The thrown `Error`'s message is only the error code, so BullMQ's `failedReason` shows the same string for every validation throw in the config path. The full text does cross the RPC in `__rpcApError`: the worker reads it with `apErrorOf(err)?.message` and writes it to the conversation's saved failure, the client error event and the flow step output, and the API logs it as `RPC handler threw`. Read one of those, not the queue. Group a backlog by `platformId` and `modelName` from `job.data` before calling it one incident: the Sep 13 to Oct 1 2026 wave of 28.7k was 3 platforms.++- **Unit tests do not run on the shipped tier list.** `test/unit/app/ee/agent/model-tier-fixture.ts` publishes chat tiers `fast` / `smart` / `turbo` (no `premium`) and different model ids, so a test that hard-codes `premium` or a shipped model id fails on CI. Read ids from the fixture or `modelTierCatalog.current(surface)`.diff --git a/brain/knowledge/ai-intelligence/ai-providers.md b/brain/knowledge/ai-intelligence/ai-providers.mdindex 2222224e89a..7c11cce84c1 100644--- a/brain/knowledge/ai-intelligence/ai-providers.md+++ b/brain/knowledge/ai-intelligence/ai-providers.md@@ -75,6 +75,7 @@ after a file without chat keys it seeds the chat list from the live flow list.**Specific model** — a model a builder picks directly from one key (`{provider, configId, model}`), as opposed to a tier. The admin switch `platform_configuration.aiSpecificModelsVisible` (Tiers tab, "Visible to builders") hides them from builders. _Avoid_: "Advanced" (an early mockup word for the same section)### Gotchas+- **`enabledForChat` now means "the Default AI key", not "the key every chat runs on".** Chats pin their own key (see `ai-agents.md`); the flag only seeds the legacy and default paths, knowledge-base embeddings (when no Embeddings capability is set), and platform-wide work. Memory and personalization run on it, else on the Default tier's main model when that key serves every project, else on Credits (`agentHelpers.resolvePlatformWorkModel`). `listForProject` still reports the flag because the pre-7b chat page gates on it.- **The replay eval can't judge a model swap where the new model takes a different tool path.** Two separate things cause it:- **Replay divergence.** Every fixture gets the same tool set from `buildEvalToolSet` (local, display, cross-project and thinking tools). The recording only fixes which results come back and in what order. Calling a registered tool out of the recorded order returns `__evalDivergence`.- **Tools the eval doesn't have.** The flow and table tools (`ap_duplicate_flow`, `ap_build_flow`, `ap_find_records`) aren't in that set at all, so `ap_get_tool_schema` answers "There is no tool named". The tool does exist in production.diff --git a/brain/knowledge/ai-intelligence/model-picker.md b/brain/knowledge/ai-intelligence/model-picker.mdindex d729fe71480..604ae697bab 100644--- a/brain/knowledge/ai-intelligence/model-picker.md+++ b/brain/knowledge/ai-intelligence/model-picker.md@@ -37,8 +37,9 @@ On `agent` and `chat` a tier whose main model can't call tools is hidden, and so- The Run Agent step does not go through `/v1/ai/execute`. Its piece posts to `/v1/agents/runs`, and the agent runtime resolves the model (`getAgentConfig` → `agentModelTier.resolveRun`). Steps pin an exact piece version, so a field the piece learns to forward reaches only steps on that version or newer.- The Run Agent step stores a tier as `{ modelTierId }` and only piece-ai 0.14.1+ forwards it. On an older step `AgentModelField` hides tiers and shows a one-line hint to update the step. Downgrading a step below 0.14.1 keeps the stored tier, and the old piece drops it, so that run uses the Default AI key.- A flow step has no server create hook, so `AgentModelField` writes `defaultChoice` once when the stored value is empty and the step is editable. On an old step it writes the credits default or the first key model instead of a tier.-- The knowledge-base section (and the agent panel's embedding note) checks the project's chat key for every pick, never the picked provider. Uploads always embed with that key (`knowledgeBaseService.embedderFor` → `getChatProvider`: Default AI key, else the credits key when visible), whatever the step or agent picked, so a chat key that can't embed (Anthropic) breaks uploads. The web reads it from `useChatProvider()`, whose `/v1/ai-providers` row already applies the same `pickChatRow` fallback for the session project that uploads use.+- The knowledge-base section (and the agent panel's embedding note) checks the project's chat key for every pick, never the picked provider. Uploads embed with `knowledgeBaseService.embeddingModelFor`: the Embeddings capability pick when one is set, else that chat key (Default AI key, else the credits key when visible), whatever the step or agent picked, so a chat key that can't embed (Anthropic) breaks uploads. The web check does not see the Embeddings pick yet; 7b adds it with the Embeddings row. The web reads it from `useChatProvider()`, whose `/v1/ai-providers` row already applies the same `pickChatRow` fallback for the session project that uploads use.- An older managed pick can store a concrete id (`anthropic/claude-sonnet-4.6`) instead of a credits tier id. It still runs while the server's managed list has it (`managedChatModelIds()` plus the published credits tiers' models), so the picker checks that same list: a listed id shows as a Credits pick with its raw id, a retired one as Unavailable.+- **Don't use `defaultChoice` on a hot run path.** `aiModelOptionsService.list` fetches every key's model list and skips a key whose list fails, so a flaky provider API (or a fake test key) can leave `defaultChoice` null, or point it somewhere else. The chat run path (`agentChatModel.defaultPick`) checks the Default tier and the Default AI key without listing models, and only falls back to `list` when neither exists.- The picker never writes a default by itself. An agent gets its default model from the server when it is created. An unknown pick shows an amber "Unavailable" and is never silently swapped (S11).## Key filesdiff --git a/bun.lock b/bun.lockindex 7fe601b000d..47178ea7827 100644--- a/bun.lock+++ b/bun.lock@@ -167,7 +167,7 @@},"packages/core/shared": {"name": "@activepieces/shared",- "version": "0.201.3",+ "version": "0.202.0","dependencies": {"@activepieces/core-execution": "workspace:*","@activepieces/core-formula": "workspace:*",diff --git a/docs/install/reference/breaking-changes-unreleased.mdx b/docs/install/reference/breaking-changes-unreleased.mdxindex d266bc3ec0a..bed2749ba3b 100644--- a/docs/install/reference/breaking-changes-unreleased.mdx+++ b/docs/install/reference/breaking-changes-unreleased.mdx@@ -8,6 +8,16 @@ Entries here move to [Breaking Changes](/install/reference/breaking-changes) aut## What has changed?+#### Each chat keeps the AI model and key it started on++A chat used to run on whatever key was the **Default AI key** at the time of each message. Now a chat remembers the key and model it first ran on. Changing the Default AI key affects new chats only. An existing chat moves to the new default only when its key is deleted, or no longer covers the chat's project or model.++Message credits for AI agent turns, in chat, agents and flow steps, now follow the key that actually answered the turn. A turn answered by your own key costs one message credit, and a turn answered by Activepieces AI credits costs none (its AI usage is billed as before), whatever the Default AI key is.++#### What you need to do++Nothing to configure or migrate. To move an existing chat to another model, pick it in the chat. If you switch the Default AI key to stop using a key, delete that key or narrow its projects so existing chats move too.+#### Firecrawl's Scrape Website stops reusing cached pages, and Crawl keeps its pages when a screenshot failsThree changes in Firecrawl 0.6.0 alter what an existing step does once it is upgraded to that version. Steps keep running on the version they were built with until you upgrade them.diff --git a/packages/core/shared/package.json b/packages/core/shared/package.jsonindex 74df801e327..d2ad105f6c0 100644--- a/packages/core/shared/package.json+++ b/packages/core/shared/package.json@@ -1,6 +1,6 @@{"name": "@activepieces/shared",- "version": "0.201.3",+ "version": "0.202.0","type": "commonjs","sideEffects": false,"main": "./dist/src/index.js",diff --git a/packages/core/shared/src/lib/ee/agent/index.ts b/packages/core/shared/src/lib/ee/agent/index.tsindex 17c612ac9b7..76d7ab95c85 100644--- a/packages/core/shared/src/lib/ee/agent/index.ts+++ b/packages/core/shared/src/lib/ee/agent/index.ts@@ -206,6 +206,7 @@ export const AgentConversation = z.object({title: Nullable(z.string()),modelName: Nullable(z.string()),modelTierId: Nullable(ApId),+ providerConfigId: Nullable(ApId),status: z.nativeEnum(AgentConversationStatus).default(AgentConversationStatus.IDLE),activeRunId: Nullable(z.string()),messages: z.array(z.record(z.string(), z.unknown())).default([]),@@ -225,6 +226,7 @@ export const CreateAgentConversationRequest = z.object({title: z.optional(Nullable(z.string())),modelName: z.optional(Nullable(z.string())),modelTierId: z.optional(Nullable(ApId)),+ providerConfigId: z.optional(Nullable(ApId)),agentId: z.optional(z.string()),builder: z.optional(z.boolean()),projectId: z.optional(z.string()),@@ -235,6 +237,7 @@ export const UpdateAgentConversationRequest = z.object({title: z.optional(Nullable(z.string())),modelName: z.optional(Nullable(z.string())),modelTierId: z.optional(Nullable(ApId)),+ providerConfigId: z.optional(Nullable(ApId)),})export type UpdateAgentConversationRequest = z.infer<typeof UpdateAgentConversationRequest>diff --git a/packages/core/shared/src/lib/management/ai-tools/index.ts b/packages/core/shared/src/lib/management/ai-tools/index.tsindex a5f16a2051b..b8c1bc9645e 100644--- a/packages/core/shared/src/lib/management/ai-tools/index.ts+++ b/packages/core/shared/src/lib/management/ai-tools/index.ts@@ -6,6 +6,7 @@ export enum AiToolCapability {WEB_SEARCH = 'WEB_SEARCH',WEB_SCRAPING = 'WEB_SCRAPING',IMAGE_GENERATION = 'IMAGE_GENERATION',+ EMBEDDINGS = 'EMBEDDINGS',}export enum AiToolProvider {@@ -84,4 +85,5 @@ export type GetEnabledAiToolsResponse = z.infer<typeof GetEnabledAiToolsResponseexport type AiProviderToolChoices = {webSearch?: AiProviderToolConfigimageGeneration?: AiProviderToolConfig+ embeddings?: AiProviderToolConfig}diff --git a/packages/server/api/src/app/ai/ai-provider-service.ts b/packages/server/api/src/app/ai/ai-provider-service.tsindex 35da566ac31..909aa55a43b 100644--- a/packages/server/api/src/app/ai/ai-provider-service.ts+++ b/packages/server/api/src/app/ai/ai-provider-service.ts@@ -16,6 +16,7 @@ import { aiKeyScope, ProviderScope } from './ai-key-scope'import { AIProviderEntity, AIProviderSchema } from './ai-provider-entity'import { aiProviderHealth } from './ai-provider-health'import { AiToolConfigEntity } from './ai-tool-config-entity'+import { aiUsageHooks } from './ai-usage-hooks'import { platformModelTierService } from './platform-model-tier-service'import { aiProviders } from './providers'@@ -200,6 +201,10 @@ export const aiProviderService = (log: FastifyBaseLogger) => ({await manager.update(AIProviderEntity, { platformId }, { enabledForChat: false })}await manager.update(AIProviderEntity, providerId, updates)+ if (changesModelScope || !isNil(request.projectScope) || !isNil(request.projectIds)) {+ const key = await manager.findOneByOrFail(AIProviderEntity, { platformId, id: providerId })+ await aiUsageHooks.get(log).unpinConversationsFromKey({ manager, platformId, configId: providerId, key })+ }})},@@ -228,6 +233,22 @@ export const aiProviderService = (log: FastifyBaseLogger) => ({return { ...aiProviderCredentials({ provider: chatProvider.provider, auth, config: chatProvider.config }), configId: chatProvider.id, platformId, modelScope: chatProvider.modelScope, modelIds: chatProvider.modelIds }},+ async findKeyConfig({ platformId, configId, scope }: { platformId: PlatformId, configId: string, scope: ProviderScope }): Promise<GetProviderConfigResponse | null> {+ const row = await aiProviderRepo().findOneBy({ platformId, id: configId })+ if (isNil(row) || !aiKeyScope.rowAllowsScope({ row, scope })) {+ return null+ }+ if (row.provider === AIProviderName.ACTIVEPIECES && await isActivepiecesAiProviderHidden({ platformId, log })) {+ return null+ }+ return this.configForTierKey({ platformId, key: row })+ },++ async findProviderName({ platformId, configId }: { platformId: PlatformId, configId: string }): Promise<AIProviderName | null> {+ const row = await aiProviderRepo().findOne({ where: { platformId, id: configId }, select: { provider: true } })+ return row?.provider ?? null+ },+async keyServesScope({ platformId, provider, configId, resolvedFor, target }: { platformId: PlatformId, provider?: AIProviderName, configId?: string, resolvedFor: ProviderScope, target: ProviderScope }): Promise<boolean> {const candidates = await findRunKeyCandidates({ platformId, provider, configId, scope: resolvedFor, log })return candidates.every((row) => target.type === 'platform'@@ -243,7 +264,9 @@ export const aiProviderService = (log: FastifyBaseLogger) => ({async delete(platformId: PlatformId, providerId: string): Promise<void> {const deleted = await transaction(async (manager) => {await platformModelTierService.removeKeyFromTiers({ manager, platformId, configId: providerId })- return deleteRowForUpdate({ manager, where: { platformId, id: providerId } })+ const row = await deleteRowForUpdate({ manager, where: { platformId, id: providerId } })+ await aiUsageHooks.get(log).unpinConversationsFromKey({ manager, platformId, configId: providerId, key: null })+ return row})await revokeKeyIfManaged({ row: deleted, log })},@@ -251,6 +274,9 @@ export const aiProviderService = (log: FastifyBaseLogger) => ({const deleted = await transaction(async (manager) => {await lockPlatformForUpdate({ manager, platformId })const row = await deleteRowForUpdate({ manager, where: { platformId, provider: AIProviderName.ACTIVEPIECES } })+ if (!isNil(row)) {+ await aiUsageHooks.get(log).unpinConversationsFromKey({ manager, platformId, configId: row.id, key: null })+ }await inSameTransaction?.(manager)return row})@@ -380,6 +406,7 @@ const CAPABILITY_LABELS: Record<AiToolCapability, string> = {[AiToolCapability.WEB_SEARCH]: 'web search',[AiToolCapability.WEB_SCRAPING]: 'web scraping',[AiToolCapability.IMAGE_GENERATION]: 'image generation',+ [AiToolCapability.EMBEDDINGS]: 'embeddings',}const PLATFORM_SCOPE_PREFERENCE: Record<AiProviderProjectScope, number> = {diff --git a/packages/server/api/src/app/ai/ai-tool-config-service.ts b/packages/server/api/src/app/ai/ai-tool-config-service.tsindex abfcc859910..8788c0fa65c 100644--- a/packages/server/api/src/app/ai/ai-tool-config-service.ts+++ b/packages/server/api/src/app/ai/ai-tool-config-service.ts@@ -1,6 +1,6 @@import { ActivepiecesError, AIProviderName, apId, ErrorCode, isNil, PlatformId, spreadIfDefined } from '@activepieces/core-utils'import { aiUtils } from '@activepieces/server-utils'-import { AIProviderModelType, AiProviderToolChoices, AiProviderToolConfig, AiToolAuthConfig, AiToolCapability, AiToolConfigWithoutSensitiveData, AiToolProvider, AiToolProviderConfig, CreateAiToolConfigRequest, GetEnabledAiToolsResponse, ResolvedAiTool, UpdateAiToolConfigRequest } from '@activepieces/shared'+import { AI_PROVIDER_CAPABILITIES, AIProviderModelType, AiProviderToolChoices, AiProviderToolConfig, AiToolAuthConfig, AiToolCapability, AiToolConfigWithoutSensitiveData, AiToolProvider, AiToolProviderConfig, CreateAiToolConfigRequest, GetEnabledAiToolsResponse, ResolvedAiTool, UpdateAiToolConfigRequest } from '@activepieces/shared'import { FastifyBaseLogger } from 'fastify'import { EntityManager } from 'typeorm'import { repoFactory } from '../core/db/repo-factory'@@ -22,6 +22,7 @@ export const aiToolConfigService = (log: FastifyBaseLogger) => ({async upsert(platformId: PlatformId, request: CreateAiToolConfigRequest): Promise<void> {const existing = await aiToolConfigRepo().findOneBy({ platformId, capability: request.capability })+ assertProviderKindFits({ capability: request.capability, provider: request.provider })if (request.provider === AiToolProvider.AI_PROVIDER) {await assertProviderChoice({ platformId, capability: request.capability, config: request.config, log })}@@ -53,6 +54,7 @@ export const aiToolConfigService = (log: FastifyBaseLogger) => ({})}const provider = request.provider ?? config.provider+ assertProviderKindFits({ capability: config.capability, provider })if (provider === AiToolProvider.AI_PROVIDER && (!isNil(request.provider) || !isNil(request.config))) {await assertProviderChoice({ platformId, capability: config.capability, config: request.config ?? config.config, log })}@@ -95,6 +97,8 @@ export const aiToolConfigService = (log: FastifyBaseLogger) => ({case AiToolCapability.IMAGE_GENERATION:result.imageGeneration = resolvedbreak+ case AiToolCapability.EMBEDDINGS:+ break}}return result@@ -136,10 +140,19 @@ async function assertProviderChoice({ platformId, capability, config, log }: { pthrow new ActivepiecesError({ code: ErrorCode.VALIDATION, params: { message: 'This AI provider cannot be used for this capability' } })}+function assertProviderKindFits({ capability, provider }: { capability: AiToolCapability, provider: AiToolProvider }): void {+ if (capability === AiToolCapability.EMBEDDINGS && provider !== AiToolProvider.AI_PROVIDER) {+ throw new ActivepiecesError({ code: ErrorCode.VALIDATION, params: { message: 'Embeddings must use one of your AI provider keys' } })+ }+}+async function providerCovers({ platformId, capability, aiProvider, modelId, log }: { platformId: PlatformId, capability: AiToolCapability, aiProvider: { id: string, provider: AIProviderName }, modelId: string | undefined, log: FastifyBaseLogger }): Promise<boolean> {if (capability === AiToolCapability.WEB_SEARCH) {return aiUtils.supportsWebSearch(aiProvider.provider)}+ if (capability === AiToolCapability.EMBEDDINGS) {+ return AI_PROVIDER_CAPABILITIES[aiProvider.provider].supportsEmbedding+ }if (capability !== AiToolCapability.IMAGE_GENERATION || isNil(modelId)) {return false}@@ -173,4 +186,5 @@ async function toResolvedTool(config: AiToolConfigSchema): Promise<ResolvedAiTooconst CHOICE_KEY_BY_CAPABILITY: Partial<Record<AiToolCapability, keyof AiProviderToolChoices>> = {[AiToolCapability.WEB_SEARCH]: 'webSearch',[AiToolCapability.IMAGE_GENERATION]: 'imageGeneration',+ [AiToolCapability.EMBEDDINGS]: 'embeddings',}diff --git a/packages/server/api/src/app/ai/ai-usage-hooks.ts b/packages/server/api/src/app/ai/ai-usage-hooks.tsindex c781f510a61..720f7161ab6 100644--- a/packages/server/api/src/app/ai/ai-usage-hooks.ts+++ b/packages/server/api/src/app/ai/ai-usage-hooks.ts@@ -1,9 +1,14 @@+import { EntityManager } from 'typeorm'import { hooksFactory } from '../helper/hooks-factory'+import { AIProviderSchema } from './ai-provider-entity'export const aiUsageHooks = hooksFactory.create<AiUsageHooks>(() => ({addConversationCredits: async (_params: AddConversationCreditsParams) => {return},+ unpinConversationsFromKey: async (_params: UnpinConversationsFromKeyParams) => {+ return+ },}))export type AddConversationCreditsParams = {@@ -11,6 +16,14 @@ export type AddConversationCreditsParams = {credits: number}+export type UnpinConversationsFromKeyParams = {+ manager: EntityManager+ platformId: string+ configId: string+ key: AIProviderSchema | null+}+export type AiUsageHooks = {addConversationCredits(params: AddConversationCreditsParams): Promise<void>+ unpinConversationsFromKey(params: UnpinConversationsFromKeyParams): Promise<void>}diff --git a/packages/server/api/src/app/ai/platform-model-tier-service.ts b/packages/server/api/src/app/ai/platform-model-tier-service.tsindex 97750be1a4e..29f4a1bae47 100644--- a/packages/server/api/src/app/ai/platform-model-tier-service.ts+++ b/packages/server/api/src/app/ai/platform-model-tier-service.ts@@ -212,6 +212,15 @@ export const platformModelTierService = {})},+ async getDefaultForRun({ platformId, scope }: { platformId: PlatformId, scope: ProviderScope }): Promise<TierForRun | null> {+ const defaultTier = await tierRepo().findOneBy({ platformId, isDefault: true })+ if (isNil(defaultTier)) {+ return null+ }+ const { data } = await tryCatch(() => platformModelTierService.getForRun({ platformId, id: defaultTier.id, scope }))+ return data ?? null+ },+async getFastForRun({ platformId, scope }: { platformId: PlatformId, scope: ProviderScope }): Promise<TierForRun | null> {const fastTier = await tierRepo().findOneBy({ platformId, isFast: true })if (isNil(fastTier)) {diff --git a/packages/server/api/src/app/database/migration/postgres/1868000000000-AddProviderConfigIdToAgentConversation.ts b/packages/server/api/src/app/database/migration/postgres/1868000000000-AddProviderConfigIdToAgentConversation.tsnew file mode 100644index 00000000000..e94ac9cd7ed--- /dev/null+++ b/packages/server/api/src/app/database/migration/postgres/1868000000000-AddProviderConfigIdToAgentConversation.ts@@ -0,0 +1,21 @@+import { QueryRunner } from 'typeorm'+import { Migration } from '../../migration'++export class AddProviderConfigIdToAgentConversation1868000000000 implements Migration {+ name = 'AddProviderConfigIdToAgentConversation1868000000000'+ breaking = false+ release = '0.93.0'++ public async up(queryRunner: QueryRunner): Promise<void> {+ await queryRunner.query(`+ ALTER TABLE "agent_conversation"+ ADD "providerConfigId" character varying(21)+ `)+ }++ public async down(queryRunner: QueryRunner): Promise<void> {+ await queryRunner.query(`+ ALTER TABLE "agent_conversation" DROP COLUMN "providerConfigId"+ `)+ }+}diff --git a/packages/server/api/src/app/database/migration/postgres/background/1869000000000-AddAgentConversationProviderConfigIdIndex.ts b/packages/server/api/src/app/database/migration/postgres/background/1869000000000-AddAgentConversationProviderConfigIdIndex.tsnew file mode 100644index 00000000000..e517db7a236--- /dev/null+++ b/packages/server/api/src/app/database/migration/postgres/background/1869000000000-AddAgentConversationProviderConfigIdIndex.ts@@ -0,0 +1,23 @@+import { QueryRunner } from 'typeorm'+import { BackgroundMigration } from '../../../background-migration'+import { migrationHelpers } from '../../../migration-helpers'++export class AddAgentConversationProviderConfigIdIndex1869000000000 implements BackgroundMigration {+ name = 'AddAgentConversationProviderConfigIdIndex1869000000000'+ release = '0.93.0'+ transaction = false as const++ public async up(queryRunner: QueryRunner): Promise<void> {+ await migrationHelpers.createIndexConcurrently({+ queryRunner,+ name: 'idx_agent_conversation_platform_provider_config',+ table: 'agent_conversation',+ columns: '"platformId", "providerConfigId"',+ where: '"providerConfigId" IS NOT NULL',+ })+ }++ public async down(): Promise<void> {+ return+ }+}diff --git a/packages/server/api/src/app/database/postgres-connection.ts b/packages/server/api/src/app/database/postgres-connection.tsindex 4374d32ee0f..e22a6c2dae3 100644--- a/packages/server/api/src/app/database/postgres-connection.ts+++ b/packages/server/api/src/app/database/postgres-connection.ts@@ -456,6 +456,8 @@ import { AddPlatformTierFastAndConversationTier1864000000000 } from './migrationimport { AddAuditLogRetentionDaysToPlatform1865000000000 } from './migration/postgres/1865000000000-AddAuditLogRetentionDaysToPlatform'import { AddAiSpecificModelsVisibleToPlatformConfiguration1866000000000 } from './migration/postgres/1866000000000-AddAiSpecificModelsVisibleToPlatformConfiguration'import { AddAgentTask1867000000000 } from './migration/postgres/1867000000000-AddAgentTask'+import { AddProviderConfigIdToAgentConversation1868000000000 } from './migration/postgres/1868000000000-AddProviderConfigIdToAgentConversation'+import { AddAgentConversationProviderConfigIdIndex1869000000000 } from './migration/postgres/background/1869000000000-AddAgentConversationProviderConfigIdIndex'const getSslConfig = (): boolean | TlsOptions => {const useSsl = system.get(AppSystemProp.POSTGRES_USE_SSL)@@ -468,7 +470,9 @@ const getSslConfig = (): boolean | TlsOptions => {}export const getBackgroundMigrations = (): (new () => BackgroundMigration)[] => {- return []+ return [+ AddAgentConversationProviderConfigIdIndex1869000000000,+ ]}export const getMigrations = (): (new () => Migration)[] => {@@ -930,6 +934,7 @@ export const getMigrations = (): (new () => Migration)[] => {AddAuditLogRetentionDaysToPlatform1865000000000,AddAiSpecificModelsVisibleToPlatformConfiguration1866000000000,AddAgentTask1867000000000,+ AddProviderConfigIdToAgentConversation1868000000000,]return migrations}diff --git a/packages/server/api/src/app/ee/agent/agent-chat-model.ts b/packages/server/api/src/app/ee/agent/agent-chat-model.tsnew file mode 100644index 00000000000..ed8ccd274a7--- /dev/null+++ b/packages/server/api/src/app/ee/agent/agent-chat-model.ts@@ -0,0 +1,129 @@+import { ActivepiecesError, AIProviderName, ErrorCode, isNil, tryCatch } from '@activepieces/core-utils'+import { AgentConversation, AI_PROVIDER_ENTITY_TYPES, GetProviderConfigResponse } from '@activepieces/shared'+import { FastifyBaseLogger } from 'fastify'+import { IsNull } from 'typeorm'+import { aiKeyScope, ProviderScope } from '../../ai/ai-key-scope'+import { aiModelOptionsService } from '../../ai/ai-model-options-service'+import { AIProviderEntity, AIProviderSchema } from '../../ai/ai-provider-entity'+import { aiProviderService } from '../../ai/ai-provider-service'+import { platformModelTierService } from '../../ai/platform-model-tier-service'+import { transaction } from '../../core/db/transaction'+import { AgentConversationEntity } from './agent-conversation-entity'+import { agentHelpers } from './agent-helpers'+import { agentModelResolution } from './agent-model-resolution'+import { agentModelTier } from './agent-model-tier'++export const agentChatModel = (log: FastifyBaseLogger) => ({+ async defaultPick({ platformId, projectId }: { platformId: string, projectId: string }): Promise<ChatPick | null> {+ const scope: ProviderScope = { type: 'project', projectId }+ const defaultTier = await platformModelTierService.getDefaultForRun({ platformId, scope })+ if (!isNil(defaultTier)) {+ const { error } = await tryCatch(() => agentModelTier(log).assertUsable({ platformId, tierId: defaultTier.tier.id, scope }))+ if (isNil(error)) {+ return { modelTierId: defaultTier.tier.id, providerConfigId: null, modelName: null }+ }+ }+ if (!isNil(await aiProviderService(log).getChatKey({ platformId, scope }))) {+ return null+ }+ const { defaultChoice } = await aiModelOptionsService(log).list({ platformId, projectId, surface: 'chat' })+ if (isNil(defaultChoice)) {+ throw new ActivepiecesError({+ code: ErrorCode.ENTITY_NOT_FOUND,+ params: { entityId: platformId, entityType: AI_PROVIDER_ENTITY_TYPES.chatProvider },+ }, 'no AI model is available to this project')+ }+ return defaultChoice.type === 'tier'+ ? { modelTierId: defaultChoice.tierId, providerConfigId: null, modelName: null }+ : { modelTierId: null, providerConfigId: defaultChoice.providerConfigId, modelName: defaultChoice.modelId }+ },++ async needsDefaultPick({ platformId, conversation, scope }: { platformId: string, conversation: ChatPick, scope: ProviderScope }): Promise<boolean> {+ if (!isNil(conversation.modelTierId) || !isNil(conversation.providerConfigId)) {+ return false+ }+ return isNil(conversation.modelName) || isNil(await aiProviderService(log).getChatKey({ platformId, scope }))+ },++ async pinnedConfig({ platformId, configId, modelName, scope }: { platformId: string, configId: string, modelName: string | null, scope: ProviderScope }): Promise<PinnedConfig> {+ const config = await aiProviderService(log).findKeyConfig({ platformId, configId, scope })+ const modelId = isNil(config) || isNil(modelName)+ ? null+ : agentModelResolution.resolvePinnedModelId({ provider: config.provider, modelName, config: config.config, modelScope: config.modelScope, modelIds: config.modelIds, log })+ if (isNil(config) || isNil(modelId)) {+ throw unavailableError()+ }+ return { config, modelId }+ },++ async assertOffered({ platformId, projectId, providerConfigId, modelName }: { platformId: string, projectId: string, providerConfigId: string, modelName: string }): Promise<void> {+ const options = await aiModelOptionsService(log).list({ platformId, projectId, surface: 'chat' })+ const onKey = options.keys.some((key) => key.providerConfigId === providerConfigId && key.models.some((model) => model.id === modelName))+ const onCredits = options.credits?.providerConfigId === providerConfigId && options.credits.tiers.some((tier) => tier.id === modelName)+ if (!onKey && !onCredits) {+ throw unavailableError()+ }+ },++ async assertRunnable({ platformId, conversation, scope }: { platformId: string, conversation: ChatPick, scope: Extract<ProviderScope, { type: 'project' }> }): Promise<void> {+ if (!isNil(conversation.modelTierId)) {+ await agentModelTier(log).assertUsable({ platformId, tierId: conversation.modelTierId, scope })+ return+ }+ if (!isNil(conversation.providerConfigId)) {+ await this.pinnedConfig({ platformId, configId: conversation.providerConfigId, modelName: conversation.modelName ?? null, scope })+ return+ }+ if (await this.needsDefaultPick({ platformId, conversation, scope }) && !isNil(await this.defaultPick({ platformId, projectId: scope.projectId }))) {+ return+ }+ await agentHelpers.assertRunProviderConfigured({ platformId, scope, log })+ },++ legacyPin({ config, selectedModel, runTierId, resolvedModelId }: { config: GetProviderConfigResponse, selectedModel: string | null, runTierId: string, resolvedModelId: string }): ChatPick {+ const managed = config.provider === AIProviderName.ACTIVEPIECES || config.provider === AIProviderName.OPENROUTER+ const keepsTier = isNil(selectedModel)+ ? managed+ : managed+ ? agentModelResolution.isTierId({ modelName: selectedModel })+ : !isNil(agentModelResolution.findTier({ tierId: selectedModel, surface: 'chat' }))+ return { modelTierId: null, providerConfigId: config.configId, modelName: keepsTier ? runTierId : resolvedModelId }+ },++ keyServes({ key, conversation }: { key: AIProviderSchema, conversation: Pick<AgentConversation, 'projectId' | 'modelName'> }): boolean {+ const servesProject = isNil(conversation.projectId)+ ? key.projectScope === 'all'+ : aiKeyScope.rowAllowsScope({ row: key, scope: { type: 'project', projectId: conversation.projectId } })+ const servesModel = isNil(conversation.modelName)+ || !isNil(agentModelResolution.resolvePinnedModelId({ provider: key.provider, modelName: conversation.modelName, config: key.config, modelScope: key.modelScope, modelIds: key.modelIds, log }))+ return servesProject && servesModel+ },++ async persistPick({ platformId, projectId, conversationId, loadedModelName, pick }: { platformId: string, projectId: string, conversationId: string, loadedModelName: string | null, pick: ChatPick }): Promise<void> {+ await transaction(async (manager) => {+ if (!isNil(pick.providerConfigId)) {+ const key = await manager.getRepository(AIProviderEntity).findOne({ where: { platformId, id: pick.providerConfigId }, lock: { mode: 'pessimistic_read' } })+ if (isNil(key) || !this.keyServes({ key, conversation: { projectId, modelName: pick.modelName } })) {+ return+ }+ }+ await manager.getRepository(AgentConversationEntity).update(+ { id: conversationId, providerConfigId: IsNull(), modelTierId: IsNull(), modelName: isNil(loadedModelName) ? IsNull() : loadedModelName },+ pick,+ )+ })+ },+})++function unavailableError(): ActivepiecesError {+ return new ActivepiecesError({ code: ErrorCode.VALIDATION, params: { message: CHAT_MODEL_UNAVAILABLE_MESSAGE } })+}++const CHAT_MODEL_UNAVAILABLE_MESSAGE = 'The model this chat uses isn\'t available anymore. Pick another model.'++export type ChatPick = Pick<AgentConversation, 'modelTierId' | 'providerConfigId' | 'modelName'>++type PinnedConfig = {+ config: GetProviderConfigResponse+ modelId: string+}diff --git a/packages/server/api/src/app/ee/agent/agent-conversation-controller.ts b/packages/server/api/src/app/ee/agent/agent-conversation-controller.tsindex 074915871f3..a76a7f828e6 100644--- a/packages/server/api/src/app/ee/agent/agent-conversation-controller.ts+++ b/packages/server/api/src/app/ee/agent/agent-conversation-controller.ts@@ -9,6 +9,7 @@ import { mcpUtils } from '../../mcp/tools/mcp-utils'import { assertCreditsAndAppSumoNotExceeded } from '../../platform/billing-provider'import { jobQueue, JobType } from '../../workers/job-queue/job-queue'import { agentApprovalGate } from './agent-approval-gate'+import { agentChatModel } from './agent-chat-model'import { agentConversationService } from './agent-conversation-service'import { agentHelpers } from './agent-helpers'import { agentMemoryAi } from './agent-memory-ai'@@ -131,14 +132,19 @@ export const agentConversationController: FastifyPluginAsyncZod = async (app) =>projects: await agentHelpers.getUserProjects({ platformId, userId, log }),})const runScope = agentHelpers.runScopeOrThrow({ projectId: runProjectId })- await agentHelpers.assertRunProviderConfigured({- platformId,- log,- scope: runScope,- modelTierId: runTierId,- ...spreadIfDefined('provider', agentConfig?.provider ?? undefined),- ...spreadIfDefined('providerConfigId', agentConfig?.providerConfigId ?? undefined),- })+ if (runsOnAgentModel) {+ await agentHelpers.assertRunProviderConfigured({+ platformId,+ log,+ scope: runScope,+ modelTierId: runTierId,+ ...spreadIfDefined('provider', agentConfig.provider ?? undefined),+ ...spreadIfDefined('providerConfigId', agentConfig.providerConfigId ?? undefined),+ })+ }+ else {+ await agentChatModel(log).assertRunnable({ platformId, conversation, scope: runScope })+ }await assertCreditsAndAppSumoNotExceeded({ platformId, log })const carriesConfiguredTools = !isNil(agentConfig) && !isBuilderdiff --git a/packages/server/api/src/app/ee/agent/agent-conversation-credits.ts b/packages/server/api/src/app/ee/agent/agent-conversation-credits.tsindex 231b9a3bed5..49ba858112b 100644--- a/packages/server/api/src/app/ee/agent/agent-conversation-credits.ts+++ b/packages/server/api/src/app/ee/agent/agent-conversation-credits.ts@@ -1,9 +1,11 @@+import { isNil } from '@activepieces/core-utils'import { FastifyBaseLogger } from 'fastify'-import { AddConversationCreditsParams, AiUsageHooks } from '../../ai/ai-usage-hooks'+import { AddConversationCreditsParams, AiUsageHooks, UnpinConversationsFromKeyParams } from '../../ai/ai-usage-hooks'+import { agentChatModel } from './agent-chat-model'import { AgentConversationEntity } from './agent-conversation-entity'import { agentHelpers } from './agent-helpers'-export const agentConversationCreditsHooks = (_log: FastifyBaseLogger): AiUsageHooks => ({+export const agentConversationCreditsHooks = (log: FastifyBaseLogger): AiUsageHooks => ({async addConversationCredits({ conversationId, credits }: AddConversationCreditsParams): Promise<void> {const wholeCredits = Math.round(credits)if (wholeCredits === 0) {@@ -17,4 +19,28 @@ export const agentConversationCreditsHooks = (_log: FastifyBaseLogger): AiUsageH.where('id = :conversationId', { conversationId }).execute()},+ async unpinConversationsFromKey({ manager, platformId, configId, key }: UnpinConversationsFromKeyParams): Promise<void> {+ const repo = manager.getRepository(AgentConversationEntity)+ const pinned = await repo.find({+ where: { platformId, providerConfigId: configId },+ select: { id: true, projectId: true, modelName: true },+ })+ const stale = isNil(key) ? pinned : pinned.filter((conversation) => !agentChatModel(log).keyServes({ key, conversation }))+ if (stale.length === 0) {+ return+ }+ await manager.query(+ `+ UPDATE "agent_conversation" AS conversation+ SET "providerConfigId" = NULL+ FROM unnest($3::varchar[], $4::varchar[]) AS stale(id, "modelName")+ WHERE conversation."platformId" = $1+ AND conversation."providerConfigId" = $2+ AND conversation.id = stale.id+ AND conversation."modelName" IS NOT DISTINCT FROM stale."modelName"+ `,+ [platformId, configId, stale.map((conversation) => conversation.id), stale.map((conversation) => conversation.modelName ?? null)],+ )+ },})+diff --git a/packages/server/api/src/app/ee/agent/agent-conversation-entity.ts b/packages/server/api/src/app/ee/agent/agent-conversation-entity.tsindex 47269b50927..ee5f5eac237 100644--- a/packages/server/api/src/app/ee/agent/agent-conversation-entity.ts+++ b/packages/server/api/src/app/ee/agent/agent-conversation-entity.ts@@ -54,6 +54,10 @@ export const AgentConversationEntity = new EntitySchema<AgentConversationWithRel...ApIdSchema,nullable: true,},+ providerConfigId: {+ ...ApIdSchema,+ nullable: true,+ },status: {type: String,nullable: false,@@ -106,6 +110,11 @@ export const AgentConversationEntity = new EntitySchema<AgentConversationWithRelcolumns: ['updated'],where: `status = '${AgentConversationStatus.STREAMING}'`,},+ {+ name: 'idx_agent_conversation_platform_provider_config',+ columns: ['platformId', 'providerConfigId'],+ where: '"providerConfigId" IS NOT NULL',+ },],relations: {platform: {diff --git a/packages/server/api/src/app/ee/agent/agent-conversation-service.ts b/packages/server/api/src/app/ee/agent/agent-conversation-service.tsindex 5b04b672b9a..64263ade728 100644--- a/packages/server/api/src/app/ee/agent/agent-conversation-service.ts+++ b/packages/server/api/src/app/ee/agent/agent-conversation-service.ts@@ -11,6 +11,7 @@ import { buildPaginator } from '../../helper/pagination/build-paginator'import { paginationHelper } from '../../helper/pagination/pagination-utils'import { Order } from '../../helper/pagination/paginator'import { agentApprovalGate } from './agent-approval-gate'+import { agentChatModel } from './agent-chat-model'import { AgentConversationEntity } from './agent-conversation-entity'import { AgentEntity } from './agent-entity'import { agentHelpers, EVAL_CONVERSATION_ID_PREFIX, isEvalConversationId } from './agent-helpers'@@ -56,17 +57,29 @@ async function projectStillHoldingAgent({ agentId, authorisedProjectId, entityMareturn locked.projectId}-async function modelChoiceFrom({ platformId, projectId, modelName, modelTierId, log }: { platformId: string, projectId: string | null, modelName: string | null | undefined, modelTierId: string | null | undefined, log: FastifyBaseLogger }): Promise<ModelChoice> {+async function modelChoiceFrom({ platformId, projectId, request, log }: { platformId: string, projectId: string | null, request: ModelChoiceRequest, log: FastifyBaseLogger }): Promise<ModelChoice> {+ const { modelName, modelTierId, providerConfigId } = requestif (!isNil(modelTierId)) {await agentModelTier(log).assertUsable({ platformId, tierId: modelTierId, scope: isNil(projectId) ? { type: 'platform' } : { type: 'project', projectId } })- return { modelTierId, modelName: null }+ return { modelTierId, modelName: null, providerConfigId: null }+ }+ if (!isNil(providerConfigId)) {+ if (isNil(modelName)) {+ throw new ActivepiecesError({ code: ErrorCode.VALIDATION, params: { message: 'A model picked on a key needs its model name' } })+ }+ if (isNil(projectId)) {+ throw new ActivepiecesError({ code: ErrorCode.VALIDATION, params: { message: 'A chat needs a project before it can use a model on a key' } })+ }+ await agentChatModel(log).assertOffered({ platformId, projectId, providerConfigId, modelName })+ return { providerConfigId, modelName, modelTierId: null }}if (!isNil(modelName)) {- return { modelName, modelTierId: null }+ return { modelName, modelTierId: null, providerConfigId: null }}return {- ...(modelName === null ? { modelName: null } : {}),+ ...(modelName === null ? { modelName: null, providerConfigId: null } : {}),...(modelTierId === null ? { modelTierId: null } : {}),+ ...(providerConfigId === null ? { providerConfigId: null } : {}),}}@@ -79,12 +92,15 @@ export const agentConversationService = (log: FastifyBaseLogger) => ({const builderProjectId = builder? await resolveBuilderProject({ agent, requestedProjectId: request.projectId, platformId, userId, log }): null- const modelChoice = await modelChoiceFrom({ platformId, projectId: agent?.projectId ?? builderProjectId, modelName: request.modelName, modelTierId: request.modelTierId, log })+ const chatProjectId = !builder && isNil(agent) && !isNil(request.projectId)+ ? await reachableProjectOrThrow({ projectId: request.projectId, platformId, userId, log })+ : null+ const modelChoice = await modelChoiceFrom({ platformId, projectId: agent?.projectId ?? builderProjectId ?? chatProjectId, request, log })const conversation = await transaction(async (entityManager) => entityManager.getRepository(AgentConversationEntity).save({id: id ?? apId(),platformId,projectId: isNil(agent)- ? builderProjectId+ ? builderProjectId ?? chatProjectId: await projectStillHoldingAgent({ agentId: agent.id, authorisedProjectId: agent.projectId, entityManager }),userId,agentId: agent?.id ?? null,@@ -92,6 +108,7 @@ export const agentConversationService = (log: FastifyBaseLogger) => ({title: request.title ?? null,modelName: modelChoice.modelName ?? null,modelTierId: modelChoice.modelTierId ?? null,+ providerConfigId: modelChoice.providerConfigId ?? null,messages: [],}))log.info({ conversation: { id: conversation.id }, platform: { id: platformId }, user: { id: userId } }, '[agentConversationService] Conversation created')@@ -223,7 +240,7 @@ export const agentConversationService = (log: FastifyBaseLogger) => ({const conversation = await this.getConversationOrThrow({ id, platformId, userId })const updates = {...spreadIfDefined('title', request.title),- ...await modelChoiceFrom({ platformId, projectId: conversation.projectId ?? null, modelName: request.modelName, modelTierId: request.modelTierId, log }),+ ...await modelChoiceFrom({ platformId, projectId: conversation.projectId ?? null, request, log }),}if (Object.keys(updates).length > 0) {@@ -320,14 +337,18 @@ async function resolveBuilderProject({ agent, requestedProjectId, platformId, usparams: { message: 'A builder conversation needs either an agentId to change or a projectId to build in' },})}+ return reachableProjectOrThrow({ projectId: requestedProjectId, platformId, userId, log })+}++async function reachableProjectOrThrow({ projectId, platformId, userId, log }: { projectId: string, platformId: string, userId: string, log: FastifyBaseLogger }): Promise<string> {const projects = await agentHelpers.getUserProjects({ platformId, userId, log })- if (!projects.some((project) => project.id === requestedProjectId)) {+ if (!projects.some((project) => project.id === projectId)) {throw new ActivepiecesError({code: ErrorCode.ENTITY_NOT_FOUND,- params: { entityId: requestedProjectId, entityType: 'Project' },+ params: { entityId: projectId, entityType: 'Project' },})}- return requestedProjectId+ return projectId}type ConversationIdentifier = {@@ -348,4 +369,7 @@ type SetMessageFeedbackParams = ConversationIdentifier & {type ModelChoice = {modelName?: string | nullmodelTierId?: string | null+ providerConfigId?: string | null}++type ModelChoiceRequest = Pick<UpdateAgentConversationRequest, 'modelName' | 'modelTierId' | 'providerConfigId'>diff --git a/packages/server/api/src/app/ee/agent/agent-helpers.ts b/packages/server/api/src/app/ee/agent/agent-helpers.tsindex 98e8328e0b0..4a639a4a7dd 100644--- a/packages/server/api/src/app/ee/agent/agent-helpers.ts+++ b/packages/server/api/src/app/ee/agent/agent-helpers.ts@@ -1,14 +1,14 @@import { setTimeout as delay } from 'node:timers/promises'import { ExecuteAgentRunJobData } from '@activepieces/core-execution'-import { ActivepiecesAiBilling, ActivepiecesError, AIProviderName, apId, assertNotNullOrUndefined, ErrorCode, isNil, spreadIfDefined, tryCatch, tryCatchSync, unique } from '@activepieces/core-utils'+import { ActivepiecesError, AIProviderName, apId, assertNotNullOrUndefined, ErrorCode, isNil, spreadIfDefined, tryCatch, tryCatchSync, unique } from '@activepieces/core-utils'import { aiUtils, modelTierCatalog, ModelTierSurface } from '@activepieces/server-utils'import { AgentConfig, AgentConversation, AgentConversationStatus, AgentFlowTool, AgentTool, AgentToolType, AI_PROVIDER_CAPABILITIES, AI_PROVIDER_ENTITY_TYPES, AIProviderModelType, FlowVersionState, GetAgentMemoryResponse, GetProviderConfigResponse, Project, ProjectType, ResolvedAgentFlowTool, UserMemory } from '@activepieces/shared'-import { SharedV3ProviderOptions } from '@ai-sdk/provider'-import { EmbeddingModel, LanguageModel } from 'ai'+import { LanguageModel } from 'ai'import { FastifyBaseLogger } from 'fastify'import { Repository } from 'typeorm'import { z } from 'zod'import { aiProviderService, ProviderScope } from '../../ai/ai-provider-service'+import { platformModelTierService, TierForRun } from '../../ai/platform-model-tier-service'import { repoFactory } from '../../core/db/repo-factory'import { transaction } from '../../core/db/transaction'import { redisConnections } from '../../database/redis-connections'@@ -130,6 +130,48 @@ async function resolveChatProvider({ platformId, scope, log }: { platformId: strreturn chatProvider}+async function resolvePlatformWorkModel({ platformId, log }: { platformId: string, log: FastifyBaseLogger }): Promise<PlatformWorkModel | null> {+ const scope: ProviderScope = { type: 'platform' }+ const defaultKey = await aiProviderService(log).getChatKey({ platformId, scope })+ if (defaultKey?.enabledForChat === true) {+ return { config: await aiProviderService(log).configForTierKey({ platformId, key: defaultKey }), modelId: null }+ }+ const mainEntry = allProjectsMainEntry({ forRun: await platformModelTierService.getDefaultForRun({ platformId, scope }) })+ if (!isNil(mainEntry)) {+ return { config: await aiProviderService(log).configForTierKey({ platformId, key: mainEntry.key }), modelId: mainEntry.modelId }+ }+ return isNil(defaultKey) ? null : { config: await aiProviderService(log).configForTierKey({ platformId, key: defaultKey }), modelId: null }+}++function allProjectsMainEntry({ forRun }: { forRun: TierForRun | null }): TierForRun['entries'][number] | undefined {+ const main = forRun?.tier.entries[0]+ return isNil(main) ? undefined : forRun?.entries.find((entry) => entry.key.id === main.configId && entry.modelId === main.modelId && entry.key.projectScope === 'all')+}++async function resolvePlatformWorkModelOrThrow({ platformId, log }: { platformId: string, log: FastifyBaseLogger }): Promise<PlatformWorkModel> {+ const work = await resolvePlatformWorkModel({ platformId, log })+ if (isNil(work)) {+ throw new ActivepiecesError({+ code: ErrorCode.ENTITY_NOT_FOUND,+ params: { entityId: platformId, entityType: AI_PROVIDER_ENTITY_TYPES.chatProvider },+ }, 'no AI provider on this platform can run platform-wide work')+ }+ return work+}++async function resolvePlatformFastModel({ platformId, log }: { platformId: string, log: FastifyBaseLogger }): Promise<LanguageModel> {+ const scope: ProviderScope = { type: 'platform' }+ const defaultKey = await aiProviderService(log).getChatKey({ platformId, scope })+ const fastEntry = defaultKey?.enabledForChat === true ? undefined : allProjectsMainEntry({ forRun: await platformModelTierService.getFastForRun({ platformId, scope }) })+ const { config, modelId } = !isNil(fastEntry)+ ? { config: await aiProviderService(log).configForTierKey({ platformId, key: fastEntry.key }), modelId: fastEntry.modelId }+ : !isNil(defaultKey)+ ? { config: await aiProviderService(log).configForTierKey({ platformId, key: defaultKey }), modelId: null }+ : await resolvePlatformWorkModelOrThrow({ platformId, log })+ const fastModelId = modelId ?? await resolveFastModelId({ platformId, providerConfig: config, surface: 'chat', scope, log })+ return aiUtils.createModel({ credentials: config, modelId: fastModelId, platformId, providerConfigId: config.configId })+}+async function assertRunProviderConfigured({ platformId, provider, providerConfigId, modelTierId, scope, log }: { platformId: string, provider?: AIProviderName | null, providerConfigId?: string | null, modelTierId?: string | null, scope: ProviderScope, log: FastifyBaseLogger }): Promise<void> {if (!isNil(modelTierId)) {await agentModelTier(log).assertUsable({ platformId, tierId: modelTierId, scope })@@ -221,16 +263,6 @@ async function resolveFastModel({ platformId, provider, providerConfigId, surfac}-async function resolveEmbeddingModel({ platformId, provider, providerConfigId, scope, billing, log }: { platformId: string, provider?: AIProviderName, providerConfigId?: string, scope: ProviderScope, billing?: ActivepiecesAiBilling, log: FastifyBaseLogger }): Promise<{ model: EmbeddingModel, providerOptions: SharedV3ProviderOptions }> {- const providerConfig = await resolveRunProvider({ platformId, scope, log, ...spreadIfDefined('provider', provider), ...spreadIfDefined('providerConfigId', providerConfigId) })- return aiUtils.createEmbeddingModel({- credentials: providerConfig,- platformId,- providerConfigId: providerConfig.configId,- ...spreadIfDefined('billing', billing),- })-}-// Analytics and billing label a turn with the provider that served it. A conversation with no// project has no key to name, and guessing platform-wide would report a credential the run was// never allowed to use, so the provider stays unknown.@@ -241,6 +273,11 @@ async function resolveChatProviderName({ platformId, projectId, log }: { platforreturn aiProviderService(log).getChatProviderName({ platformId, scope: runScopeOrThrow({ projectId }) })}+async function resolveConversationProviderName({ platformId, projectId, providerConfigId, log }: { platformId: string, projectId: string | null, providerConfigId: string | null, log: FastifyBaseLogger }): Promise<AIProviderName | null> {+ const pinned = isNil(providerConfigId) ? null : await aiProviderService(log).findProviderName({ platformId, configId: providerConfigId })+ return pinned ?? resolveChatProviderName({ platformId, projectId, log })+}+async function recoverAllStaleStreamingConversations({ log }: { log: FastifyBaseLogger }): Promise<{ recovered: number }> {// Compare against the DB clock (NOW()) rather than a bound JS Date, so the sweep is immune// to app/DB clock skew and driver timezone handling.@@ -446,6 +483,9 @@ export const agentHelpers = {getConversationOrThrow,getUserProjects,resolveChatProvider,+ resolvePlatformWorkModel,+ resolvePlatformWorkModelOrThrow,+ resolvePlatformFastModel,assertRunProviderConfigured,...agentModelResolution,resolveFastModel,@@ -453,8 +493,8 @@ export const agentHelpers = {resolveImageModelId,resolveModelId,resolveRunProvider,- resolveEmbeddingModel,resolveChatProviderName,+ resolveConversationProviderName,runScopeOrThrow,selectRunProject,assertProjectSwitchKeepsKey,@@ -470,3 +510,8 @@ export const agentHelpers = {type AgentJobConfigFields = Pick<ExecuteAgentRunJobData, 'tools' | 'structuredOutput' | 'maxSteps' | 'modelName' | 'provider' | 'providerConfigId' | 'modelTierId' | 'promptOverride'>export type StreamingLockResult = 'acquired' | 'busy' | 'superseded' | 'duplicate'++export type PlatformWorkModel = {+ config: GetProviderConfigResponse+ modelId: string | null+}diff --git a/packages/server/api/src/app/ee/agent/agent-memory-ai.ts b/packages/server/api/src/app/ee/agent/agent-memory-ai.tsindex ae8c8f7a959..edeaed2984c 100644--- a/packages/server/api/src/app/ee/agent/agent-memory-ai.ts+++ b/packages/server/api/src/app/ee/agent/agent-memory-ai.ts@@ -53,7 +53,7 @@ async function runMemoryLlm<T>({ platformId, instructions, prompt, schema, log }log: FastifyBaseLogger}): Promise<T | null> {const { data, error } = await tryCatch(async () => {- const { text: raw } = await generateText({ model: await agentHelpers.resolveFastModel({ platformId, surface: 'chat', scope: { type: 'platform' }, log }), instructions, prompt, temperature: 0, telemetry: agentAiUtils.buildTelemetry({ functionId: 'agent-memory' }) })+ const { text: raw } = await generateText({ model: await agentHelpers.resolvePlatformFastModel({ platformId, log }), instructions, prompt, temperature: 0, telemetry: agentAiUtils.buildTelemetry({ functionId: 'agent-memory' }) })return parseJsonObject(raw, schema)})if (!isNil(error)) {diff --git a/packages/server/api/src/app/ee/agent/agent-model-resolution.ts b/packages/server/api/src/app/ee/agent/agent-model-resolution.tsindex f450281df4f..68df4abc08c 100644--- a/packages/server/api/src/app/ee/agent/agent-model-resolution.ts+++ b/packages/server/api/src/app/ee/agent/agent-model-resolution.ts@@ -93,6 +93,32 @@ function resolveNamedModelId({ provider, modelName, surface, modelScope, modelIdreturn requested}+function resolvePinnedModelId({ provider, modelName, config, modelScope, modelIds, log }: { provider: AIProviderName, modelName: string, config?: AiProviderCredentials['config'], modelScope?: AiProviderModelScope, modelIds?: string[], log: FastifyBaseLogger }): string | null {+ const managed = provider === AIProviderName.ACTIVEPIECES || provider === AIProviderName.OPENROUTER+ if (managed && isTierId({ modelName })) {+ const modelId = findTier({ tierId: modelName, surface: 'chat' })?.modelId+ const excluded = modelScope === 'selected' && !isNil(modelIds) && !isNil(modelId) && !modelIds.includes(modelId)+ return isNil(modelId) || excluded ? null : modelId+ }+ if (!managed && !isNil(findTier({ tierId: modelName, surface: 'chat' }))) {+ const { data } = tryCatchSync(() => resolveModelIdForProvider({ provider, selectedModel: modelName, surface: 'chat', config, modelScope, modelIds }))+ return data ?? null+ }+ const catalog = manualTextModelCatalog({ config })+ if (!isNil(catalog) && !catalog.includes(modelName)) {+ return null+ }+ if (isNil(catalog) && isRetiredCuratedModel({ provider, modelId: modelName })) {+ return null+ }+ const { data } = tryCatchSync(() => resolveNamedModelId({ provider, modelName, surface: 'chat', modelScope, modelIds, log }))+ return data ?? null+}++function isRetiredCuratedModel({ provider, modelId }: { provider: AIProviderName, modelId: string }): boolean {+ return !isNil(aiProviderUtils.getCuratedChatModels({ provider })) && !aiProviderUtils.runnableChatModelIds({ provider }).includes(modelId)+}+function resolveModelIdForProvider({ provider, selectedModel, surface, config, modelScope, modelIds }: { provider: AIProviderName, selectedModel: string | null, surface: ModelTierSurface, config?: AiProviderCredentials['config'], modelScope?: AiProviderModelScope, modelIds?: string[] }): string {const catalog = manualTextModelCatalog({ config })if (!isNil(catalog)) {@@ -112,17 +138,22 @@ function defaultModelIdForProvider({ provider, surface, config, modelScope, modereturn data}-// Analytics and billing report the model a turn ran on. The provider is unknown when a platform's-// chat provider no longer resolves, so fall back to the stored selection — but only when it is one-// of our own ids, never echoing an arbitrary stored string out to the analytics sink.-function resolveModelIdForAnalytics({ provider, selectedModel, surface }: { provider: AIProviderName | null, selectedModel: string | null, surface: ModelTierSurface }): string | null {+// Analytics and billing report the model a turn ran on. A pinned chat stored a model id that was+// checked when it was picked, so it is reported as is. Otherwise the provider is unknown when a+// platform's chat provider no longer resolves, so fall back to the stored selection, but only when it+// is one of our own ids, never echoing an arbitrary stored string out to the analytics sink.+function resolveModelIdForAnalytics({ provider, selectedModel, surface, pinned }: { provider: AIProviderName | null, selectedModel: string | null, surface: ModelTierSurface, pinned: boolean }): string | null {if (isNil(selectedModel)) {return null}+ const tier = findTier({ tierId: selectedModel, surface })+ if (pinned && isNil(tier)) {+ return selectedModel+ }if (!isNil(provider)) {- return resolveModelIdForProvider({ provider, selectedModel, surface })+ const { data } = tryCatchSync(() => resolveModelIdForProvider({ provider, selectedModel, surface }))+ return data ?? null}- const tier = findTier({ tierId: selectedModel, surface })if (!isNil(tier)) {return tier.modelId}@@ -136,6 +167,8 @@ export const agentModelResolution = {resolveRunTier,nativeModelIdFor,resolveNamedModelId,+ resolvePinnedModelId,+ isTierId,resolveModelIdForProvider,defaultModelIdForProvider,resolveModelIdForAnalytics,diff --git a/packages/server/api/src/app/ee/agent/chat-analytics-sync.ts b/packages/server/api/src/app/ee/agent/chat-analytics-sync.tsindex 04a5c8cb3f1..5d6d518836f 100644--- a/packages/server/api/src/app/ee/agent/chat-analytics-sync.ts+++ b/packages/server/api/src/app/ee/agent/chat-analytics-sync.ts@@ -91,7 +91,7 @@ async function resolveLicenseKeysByPlatform({ platformIds }: {}function chatProviderCacheKey(conversation: AgentConversation): string {- return `${conversation.platformId}:${conversation.projectId ?? ''}`+ return `${conversation.platformId}:${conversation.projectId ?? ''}:${conversation.providerConfigId ?? ''}`}async function resolveLookups({ conversations, log }: {@@ -107,7 +107,7 @@ async function resolveLookups({ conversations, log }: {Promise.all(uniquePlatformIds.map(async (platformId): Promise<[string, string | null]> => [platformId, await resolvePlatformName({ platformId, log })])),Promise.all(uniqueScopes.map(async (conversation): Promise<[string, AIProviderName | null]> => [chatProviderCacheKey(conversation),- await resolveProviderName({ platformId: conversation.platformId, projectId: conversation.projectId ?? null, log }),+ await resolveProviderName({ conversation, log }),])),agentModelTier(log).mainModelsOf({ conversations }),])@@ -186,7 +186,7 @@ async function toSyncPayload({ conversation, licenseKey, log, userCache, platforconst platformName = platformCache?.get(conversation.platformId) ?? await resolvePlatformName({ platformId: conversation.platformId, log })const tierKey = agentTierCandidates.tierCacheKey({ platformId: conversation.platformId, tierId: conversation.modelTierId ?? '' })const tierModel = tierCache?.has(tierKey) ? tierCache.get(tierKey) ?? null : await agentModelTier(log).mainModelOf({ conversation })- const provider = tierModel?.provider ?? providerCache?.get(chatProviderCacheKey(conversation)) ?? await resolveProviderName({ platformId: conversation.platformId, projectId: conversation.projectId ?? null, log })+ const provider = tierModel?.provider ?? providerCache?.get(chatProviderCacheKey(conversation)) ?? await resolveProviderName({ conversation, log })const messages = agentHistory.resolveMessages({ conversation, log })@@ -198,7 +198,7 @@ async function toSyncPayload({ conversation, licenseKey, log, userCache, platforuserId: conversation.userId,userEmail,title: conversation.title,- modelName: tierModel?.modelId ?? agentHelpers.resolveModelIdForAnalytics({ selectedModel: conversation.modelName ?? null, provider, surface: 'chat' }),+ modelName: tierModel?.modelId ?? agentHelpers.resolveModelIdForAnalytics({ selectedModel: conversation.modelName ?? null, provider, surface: 'chat', pinned: !isNil(conversation.providerConfigId) }),provider,messages,messageCount: messages.length,@@ -244,8 +244,8 @@ async function resolvePlatformName({ platformId, log }: { platformId: string, loreturn result.error ? null : result.data.name}-async function resolveProviderName({ platformId, projectId, log }: { platformId: string, projectId: string | null, log: FastifyBaseLogger }): Promise<AIProviderName | null> {- const result = await tryCatch(() => agentHelpers.resolveChatProviderName({ platformId, projectId, log }))+async function resolveProviderName({ conversation, log }: { conversation: AgentConversation, log: FastifyBaseLogger }): Promise<AIProviderName | null> {+ const result = await tryCatch(() => agentHelpers.resolveConversationProviderName({ platformId: conversation.platformId, projectId: conversation.projectId ?? null, providerConfigId: conversation.providerConfigId ?? null, log }))return result.error ? null : result.data}diff --git a/packages/server/api/src/app/ee/agent/chat-tool-billing.ts b/packages/server/api/src/app/ee/agent/chat-tool-billing.tsindex e734543b9cf..c9715a3b26a 100644--- a/packages/server/api/src/app/ee/agent/chat-tool-billing.ts+++ b/packages/server/api/src/app/ee/agent/chat-tool-billing.ts@@ -34,14 +34,15 @@ async function chargeForLatestTurn({ conversation, runId, answeredBy, log }: Chaconst messages = agentHistory.resolveMessages({ conversation, log })const turnIndex = messages.filter((message) => message.role === PersistedAgentRole.USER).lengthconst idempotencyScope = runId ?? turnIndex- const tierModel = isNil(conversation.modelTierId) ? null : answeredBy ?? await agentModelTier(log).mainModelOf({ conversation })- const provider = tierModel?.provider ?? await agentHelpers.resolveChatProviderName({+ const tierModel = answeredBy ?? (isNil(conversation.modelTierId) ? null : await agentModelTier(log).mainModelOf({ conversation }))+ const provider = tierModel?.provider ?? await agentHelpers.resolveConversationProviderName({platformId: conversation.platformId,projectId: conversation.projectId ?? null,+ providerConfigId: conversation.providerConfigId ?? null,log,})const surface = agentHelpers.surfaceOf({ source: conversation.source })- const model = tierModel?.modelId ?? agentHelpers.resolveModelIdForAnalytics({ selectedModel: conversation.modelName ?? null, provider, surface })+ const model = tierModel?.modelId ?? agentHelpers.resolveModelIdForAnalytics({ selectedModel: conversation.modelName ?? null, provider, surface, pinned: !isNil(conversation.providerConfigId) })const tier = agentHelpers.resolveTier({ tierId: conversation.modelName ?? null, surface })const platformPlan = await platformPlanService(log).getOrCreateForPlatform(conversation.platformId)diff --git a/packages/server/api/src/app/ee/agent/personalization/chat-personalization-service.ts b/packages/server/api/src/app/ee/agent/personalization/chat-personalization-service.tsindex 38de2127e34..9f20d13c820 100644--- a/packages/server/api/src/app/ee/agent/personalization/chat-personalization-service.ts+++ b/packages/server/api/src/app/ee/agent/personalization/chat-personalization-service.ts@@ -244,19 +244,19 @@ export const chatPersonalizationService = (log: FastifyBaseLogger) => ({}const userRow = await findRow({ platformId, userId })const [provider, user, platform, companyRow, enabledTools] = await Promise.all([- agentHelpers.resolveChatProvider({ platformId, scope: PERSONALIZATION_PROVIDER_SCOPE, log }),+ agentHelpers.resolvePlatformWorkModelOrThrow({ platformId, log }),userService(log).getMetaInformation({ id: userId }),platformService(log).getOneOrThrow(platformId),findRow({ platformId, userId: null }),tryCatch(() => aiToolConfigService(log).getEnabledTools({ platformId })),])const webSearch = enabledTools.data?.webSearch ?? null- const modelId = await agentHelpers.resolveModelId({ platformId, providerConfig: provider, selectedModel: null, surface: 'chat', scope: PERSONALIZATION_PROVIDER_SCOPE, log })+ const modelId = provider.modelId ?? await agentHelpers.resolveModelId({ platformId, providerConfig: provider.config, selectedModel: null, surface: 'chat', scope: PERSONALIZATION_PROVIDER_SCOPE, log })return {claimed: true,- credentials: provider,+ credentials: provider.config,modelId,- fastModelId: await agentHelpers.resolveFastModelId({ platformId, providerConfig: provider, surface: 'chat', scope: PERSONALIZATION_PROVIDER_SCOPE, log }),+ fastModelId: provider.modelId ?? await agentHelpers.resolveFastModelId({ platformId, providerConfig: provider.config, surface: 'chat', scope: PERSONALIZATION_PROVIDER_SCOPE, log }),user: { firstName: user.firstName, lastName: user.lastName, email: user.email },platformName: platform.name,website: companyRow?.domain ?? null,diff --git a/packages/server/api/src/app/ee/agent/personalization/personalization-research.ts b/packages/server/api/src/app/ee/agent/personalization/personalization-research.tsindex a4d0d425f76..15e643981b8 100644--- a/packages/server/api/src/app/ee/agent/personalization/personalization-research.ts+++ b/packages/server/api/src/app/ee/agent/personalization/personalization-research.ts@@ -15,7 +15,7 @@ import {import { FastifyBaseLogger } from 'fastify'import { IsNull } from 'typeorm'import { z } from 'zod'-import { aiProviderService, ProviderScope } from '../../../ai/ai-provider-service'+import { ProviderScope } from '../../../ai/ai-provider-service'import { redisConnections } from '../../../database/redis-connections'import { system } from '../../../helper/system/system'import { AppSystemProp } from '../../../helper/system/system-props'@@ -185,7 +185,7 @@ export async function claimForResearch({ platformId, userId, scope, researchTokeexport async function guardsAllowResearch({ platformId, log }: { platformId: string, log: FastifyBaseLogger }): Promise<boolean> {- const chatProvider = await tryCatch(() => aiProviderService(log).getChatProvider({ platformId, scope: PERSONALIZATION_PROVIDER_SCOPE }))+ const chatProvider = await tryCatch(() => agentHelpers.resolvePlatformWorkModel({ platformId, log }))if (chatProvider.error) {log.warn({ platform: { id: platformId }, error: chatProvider.error }, '[chatPersonalization] Chat AI provider failed to load, skipping research')return falsediff --git a/packages/server/api/src/app/ee/agent/rpc/agent-config-rpc.ts b/packages/server/api/src/app/ee/agent/rpc/agent-config-rpc.tsindex 3081b0f4482..d6d537a1e75 100644--- a/packages/server/api/src/app/ee/agent/rpc/agent-config-rpc.ts+++ b/packages/server/api/src/app/ee/agent/rpc/agent-config-rpc.ts@@ -4,6 +4,7 @@ import { AgentConfigResponse, AgentConversation, AgentRunSource, aiProviderUtilsimport { LanguageModel, ModelMessage, UserContent } from 'ai'import { FastifyBaseLogger } from 'fastify'import { agentApprovalGate } from '.././agent-approval-gate'+import { agentChatModel } from '.././agent-chat-model'import { agentCompaction } from '.././agent-compaction'import { buildAttachmentNote, buildUserContentWithFiles, persistAgentAttachments } from '.././agent-file-utils'import { agentHelpers } from '.././agent-helpers'@@ -89,11 +90,20 @@ export const agentConfigRpc = (log: FastifyBaseLogger) => ({const runProjectId = isFlowStep ? conversation.projectId ?? null : selectedProjectIdconst runScope = agentHelpers.runScopeOrThrow({ projectId: runProjectId })const surface = agentHelpers.surfaceOf({ source: requestedSource })- const modelTierId = tierIdForRun({ conversation, requestedTierId: input.modelTierId ?? null })+ const choiceLivesOnConversation = pickLivesOnConversation({ conversation })+ const needsDefaultPick = choiceLivesOnConversation && await agentChatModel(log).needsDefaultPick({ platformId, conversation, scope: runScope })+ const defaultPick = needsDefaultPick ? await agentChatModel(log).defaultPick({ platformId, projectId: runScope.projectId }) : null+ const picked = isNil(defaultPick) ? conversation : { ...conversation, ...defaultPick }+ const selectedModel = choiceLivesOnConversation ? picked.modelName ?? null : modelName ?? picked.modelName ?? null+ const modelTierId = tierIdForRun({ conversation: picked, requestedTierId: input.modelTierId ?? null })const tierRun = isNil(modelTierId) ? null : await agentModelTier(log).resolveRun({ platformId, tierId: modelTierId, surface, scope: runScope })- const providerConfig = isNil(tierRun)- ? await agentHelpers.resolveRunProvider({ platformId, log, scope: runScope, ...spreadIfDefined('provider', input.provider), ...spreadIfDefined('providerConfigId', input.providerConfigId) })- : tierRun.candidates[0].config+ const pinnedConfigId = choiceLivesOnConversation && isNil(tierRun) ? picked.providerConfigId ?? null : null+ const pinned = isNil(pinnedConfigId) ? null : await agentChatModel(log).pinnedConfig({ platformId, configId: pinnedConfigId, modelName: selectedModel, scope: runScope })+ const providerConfig = !isNil(tierRun)+ ? tierRun.candidates[0].config+ : !isNil(pinned)+ ? pinned.config+ : await agentHelpers.resolveRunProvider({ platformId, log, scope: runScope, ...spreadIfDefined('provider', input.provider), ...spreadIfDefined('providerConfigId', input.providerConfigId) })const attachmentRefs = files && files.length > 0 && !isNil(selectedProjectId)? await persistAgentAttachments({ files, projectId: selectedProjectId, platformId, conversationId, log })@@ -142,23 +152,32 @@ export const agentConfigRpc = (log: FastifyBaseLogger) => ({if (!dryRun && isNil(validCandidateProjectId) && !isNil(selectedProjectId)) {await agentHelpers.conversationRepo().update(conversationId, { projectId: selectedProjectId })}- if (!dryRun && (conversation.modelTierId ?? null) !== modelTierId) {+ if (!dryRun && (picked.modelTierId ?? null) !== modelTierId) {await agentHelpers.conversationRepo().update(conversationId, { modelTierId })}- const selectedModel = modelName ?? conversation.modelName ?? nullconst namesItsOwnModel = surface === 'flow'const tier = isNil(tierRun)? agentHelpers.resolveRunTier({ provider: providerConfig.provider, modelName: modelName ?? null, selectedModel, surface }): { ...agentHelpers.resolveRunTier({ provider: providerConfig.provider, modelName: null, selectedModel: null, surface }), thinkingBudget: tierRun.candidates[0].thinkingBudget, modelId: tierRun.candidates[0].modelId }const resolvedModelId = !isNil(tierRun)? tierRun.candidates[0].modelId- : namesItsOwnModel && !isNil(modelName)- ? agentHelpers.resolveNamedModelId({ provider: providerConfig.provider, modelName, surface, modelScope: providerConfig.modelScope, modelIds: providerConfig.modelIds, log })- : await agentHelpers.resolveModelId({ platformId, providerConfig, selectedModel, surface, scope: runScope, log })+ : !isNil(pinned)+ ? pinned.modelId+ : namesItsOwnModel && !isNil(modelName)+ ? agentHelpers.resolveNamedModelId({ provider: providerConfig.provider, modelName, surface, modelScope: providerConfig.modelScope, modelIds: providerConfig.modelIds, log })+ : await agentHelpers.resolveModelId({ platformId, providerConfig, selectedModel, surface, scope: runScope, log })const fastModelId = isNil(tierRun)? await agentHelpers.resolveFastModelId({ platformId, providerConfig, surface, scope: runScope, fallbackModelId: resolvedModelId, log }): resolvedModelId+ const pickToPersist = !isNil(defaultPick)+ ? defaultPick+ : choiceLivesOnConversation && isNil(tierRun) && isNil(pinned)+ ? agentChatModel(log).legacyPin({ config: providerConfig, selectedModel, runTierId: tier.id, resolvedModelId })+ : null+ if (!dryRun && !isNil(pickToPersist)) {+ await agentChatModel(log).persistPick({ platformId, projectId: runScope.projectId, conversationId, loadedModelName: conversation.modelName ?? null, pick: pickToPersist })+ }// Inject an inventory of the project's existing connections into context so the agent// never has to *guess* an app name to find out what's connected. Without this, discovery@@ -329,8 +348,11 @@ function withLeadingNote({ content, note }: { content: UserContent, note: string}function tierIdForRun({ conversation, requestedTierId }: { conversation: AgentConversation, requestedTierId: string | null }): string | null {- const choiceLivesOnConversation = conversation.source === AgentRunSource.CHAT || conversation.source === AgentRunSource.AGENT_BUILDER- return choiceLivesOnConversation ? conversation.modelTierId ?? null : requestedTierId+ return pickLivesOnConversation({ conversation }) ? conversation.modelTierId ?? null : requestedTierId+}++function pickLivesOnConversation({ conversation }: { conversation: AgentConversation }): boolean {+ return conversation.source === AgentRunSource.CHAT || conversation.source === AgentRunSource.AGENT_BUILDER}function smallestContextProvider({ configs }: { configs: GetProviderConfigResponse[] }): AIProviderName {diff --git a/packages/server/api/src/app/ee/agent/rpc/conversation-rpc.ts b/packages/server/api/src/app/ee/agent/rpc/conversation-rpc.tsindex ca77b157c73..c5541c72f74 100644--- a/packages/server/api/src/app/ee/agent/rpc/conversation-rpc.ts+++ b/packages/server/api/src/app/ee/agent/rpc/conversation-rpc.ts@@ -76,7 +76,7 @@ export const conversationRpc = (log: FastifyBaseLogger) => ({updates.messages = input.messagesupdates.uiMessages = sanitizeObjectForPostgresql(input.uiMessages)if (input.title) updates.title = input.title- if (input.modelName && isNil(stored?.modelTierId)) updates.modelName = input.modelName+ if (input.modelName && isNil(stored?.modelTierId) && isNil(stored?.providerConfigId)) updates.modelName = input.modelName}const failureAppend = isNil(input.failure) ? null : failureAppendFor({ stored: stored?.uiMessages ?? [], failure: input.failure })if (!isNil(failureAppend)) {diff --git a/packages/server/api/src/app/ee/agent/rpc/tool-execution-rpc.ts b/packages/server/api/src/app/ee/agent/rpc/tool-execution-rpc.tsindex 72e3cd1df7e..13375ac4a95 100644--- a/packages/server/api/src/app/ee/agent/rpc/tool-execution-rpc.ts+++ b/packages/server/api/src/app/ee/agent/rpc/tool-execution-rpc.ts@@ -72,13 +72,10 @@ export const toolExecutionRpc = (log: FastifyBaseLogger) => ({await markTurnAsHavingRead({ conversationId: input.conversationId, ...spreadIfDefined('runId', input.runId) })const { projectId, platformId } = conversationconst file = await knowledgeBaseService(log).getFileOrThrow({ projectId, id: input.knowledgeBaseFileId })- const embeddingModel = () => agentHelpers.resolveEmbeddingModel({+ const embeddingModel = () => knowledgeBaseService(log).embeddingModelFor({platformId,- scope: { type: 'project', projectId },+ projectId,billing: { source: ActivepiecesAiConsumerSource.CHAT, platformId, projectId, conversationId: input.conversationId },- log,- ...spreadIfDefined('provider', input.provider),- ...spreadIfDefined('providerConfigId', input.providerConfigId),})const { error: backfillError } = await tryCatch(() => knowledgeBaseService(log).embedMissingChunks({projectId,diff --git a/packages/server/api/src/app/knowledge-base/knowledge-base.service.ts b/packages/server/api/src/app/knowledge-base/knowledge-base.service.tsindex 187e0d77e36..456b050ef7c 100644--- a/packages/server/api/src/app/knowledge-base/knowledge-base.service.ts+++ b/packages/server/api/src/app/knowledge-base/knowledge-base.service.ts@@ -1,4 +1,4 @@-import { ActivepiecesAiConsumerSource, ActivepiecesError, apId, ErrorCode, isNil, spreadIfDefined, tryCatch } from '@activepieces/core-utils'+import { ActivepiecesAiBilling, ActivepiecesAiConsumerSource, ActivepiecesError, apId, ErrorCode, isNil, spreadIfDefined, tryCatch } from '@activepieces/core-utils'import { aiUtils } from '@activepieces/server-utils'import { FileCompression, FileType, KnowledgeBaseFile } from '@activepieces/shared'import { SharedV3ProviderOptions } from '@ai-sdk/provider'@@ -7,6 +7,7 @@ import { parse as parseCsv } from 'csv-parse/sync'import { FastifyBaseLogger } from 'fastify'import { In, IsNull, Not } from 'typeorm'import { aiProviderService } from '../ai/ai-provider-service'+import { aiToolConfigService } from '../ai/ai-tool-config-service'import { repoFactory } from '../core/db/repo-factory'import { transaction } from '../core/db/transaction'import { databaseConnection } from '../database/database-connection'@@ -147,20 +148,26 @@ export const knowledgeBaseService = (log: FastifyBaseLogger) => ({async embedderFor(params: { projectId: string, platformId: string }): Promise<EmbedFn> {const { projectId, platformId } = params- const provider = await aiProviderService(log).getChatProvider({ platformId, scope: { type: 'project', projectId } })+ const { model, providerOptions } = await this.embeddingModelFor({+ platformId,+ projectId,+ billing: { source: ActivepiecesAiConsumerSource.CHAT, platformId, projectId, conversationId: KNOWLEDGE_BASE_BILLING_CONVERSATION },+ })+ return this.embedFnOf({ model, providerOptions })+ },++ async embeddingModelFor({ platformId, projectId, billing }: { platformId: string, projectId: string, billing: ActivepiecesAiBilling }): Promise<{ model: EmbeddingModel, providerOptions: SharedV3ProviderOptions }> {+ const scope = { type: 'project' as const, projectId }+ const choice = (await aiToolConfigService(log).getProviderChoices({ platformId })).embeddings+ const chosen = isNil(choice) ? null : await aiProviderService(log).findKeyConfig({ platformId, configId: choice.aiProviderId, scope })+ const provider = chosen ?? await aiProviderService(log).getChatProvider({ platformId, scope })if (isNil(provider)) {throw new ActivepiecesError({code: ErrorCode.VALIDATION,params: { message: KNOWLEDGE_BASE_NEEDS_AI_PROVIDER },})}- const { model, providerOptions } = aiUtils.createEmbeddingModel({- credentials: provider,- platformId,- providerConfigId: provider.configId,- billing: { source: ActivepiecesAiConsumerSource.CHAT, platformId, projectId, conversationId: KNOWLEDGE_BASE_BILLING_CONVERSATION },- })- return this.embedFnOf({ model, providerOptions })+ return aiUtils.createEmbeddingModel({ credentials: provider, platformId, providerConfigId: provider.configId, billing })},embedFnOf(params: { model: EmbeddingModel, providerOptions: SharedV3ProviderOptions }): EmbedFn {diff --git a/packages/server/api/test/integration/ee/agent/agent-chat-model.test.ts b/packages/server/api/test/integration/ee/agent/agent-chat-model.test.tsnew file mode 100644index 00000000000..e4795057d75--- /dev/null+++ b/packages/server/api/test/integration/ee/agent/agent-chat-model.test.ts@@ -0,0 +1,373 @@+import { AIProviderName, assertNotNullOrUndefined, ErrorCode } from '@activepieces/core-utils'+import { aiUtils, modelTierCatalog } from '@activepieces/server-utils'+import { AgentConversation, AIProviderModelType, aiProviderUtils, AiToolCapability, AiToolProvider, WorkerJobType } from '@activepieces/shared'+import { FastifyInstance } from 'fastify'+import { StatusCodes } from 'http-status-codes'+import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest'+import { aiProviderService } from '../../../../src/app/ai/ai-provider-service'+import { aiToolConfigService } from '../../../../src/app/ai/ai-tool-config-service'+import { agentChatModel } from '../../../../src/app/ee/agent/agent-chat-model'+import { agentHelpers } from '../../../../src/app/ee/agent/agent-helpers'+import { agentRpcHandlers } from '../../../../src/app/ee/agent/agent-rpc-handlers'+import { knowledgeBaseService } from '../../../../src/app/knowledge-base/knowledge-base.service'+import * as jobQueueModule from '../../../../src/app/workers/job-queue/job-queue'+import { db } from '../../../helpers/db'+import { mockAndSaveAIProvider } from '../../../helpers/mocks'+import { createTestContext, TestContext } from '../../../helpers/test-context'+import { setupTestEnvironment, teardownTestEnvironment } from '../../../helpers/test-setup'++let app: FastifyInstance++const originalJobQueue = jobQueueModule.jobQueue++beforeAll(async () => {+ app = await setupTestEnvironment()+})++afterAll(async () => {+ await teardownTestEnvironment()+})++afterEach(() => {+ vi.restoreAllMocks()+})++describe('a chat pinned to a key', () => {+ it('runs on that key even when another key is the Default AI key', async () => {+ const ctx = await context()+ await mockAndSaveAIProvider({ platformId: ctx.platform.id, provider: AIProviderName.OPENAI, enabledForChat: true })+ const key = await customKey({ ctx, name: 'Team', models: ['team-model'] })++ const conversation = await createConversation({ ctx, body: { projectId: ctx.project.id, providerConfigId: key.id, modelName: 'team-model' } })+ const config = await getConfig({ ctx, conversationId: conversation.id })++ expect(config.providerConfigId).toBe(key.id)+ expect(config.modelId).toBe('team-model')+ })++ it('is refused when the key does not serve the chat\'s project', async () => {+ const ctx = await context()+ const key = await customKey({ ctx, name: 'Elsewhere', models: ['team-model'], projectScope: 'selected', projectIds: [] })++ const response = await ctx.post(CONVERSATIONS_URL, { projectId: ctx.project.id, providerConfigId: key.id, modelName: 'team-model' })++ expect(response.statusCode).toBe(StatusCodes.CONFLICT)+ })++ it('is refused on a chat that has no project yet', async () => {+ const ctx = await context()+ const key = await customKey({ ctx, name: 'Team', models: ['team-model'] })++ const response = await ctx.post(CONVERSATIONS_URL, { providerConfigId: key.id, modelName: 'team-model' })++ expect(response.statusCode).toBe(StatusCodes.CONFLICT)+ })++ it('fails clearly when its curated model is retired, instead of sending it to the vendor', async () => {+ const ctx = await context()+ const key = await mockAndSaveAIProvider({ platformId: ctx.platform.id, provider: AIProviderName.OPENAI })+ const conversation = await createConversation({ ctx, body: { projectId: ctx.project.id } })+ await db.update('agent_conversation', conversation.id, { providerConfigId: key.id, modelName: 'gpt-retired-model' })++ await expect(getConfig({ ctx, conversationId: conversation.id })).rejects.toMatchObject({ error: { code: ErrorCode.VALIDATION } })+ })++ it('is refused for a model the key does not offer', async () => {+ const ctx = await context()+ const key = await customKey({ ctx, name: 'Team', models: ['team-model'] })++ const response = await ctx.post(CONVERSATIONS_URL, { projectId: ctx.project.id, providerConfigId: key.id, modelName: 'not-offered' })++ expect(response.statusCode).toBe(StatusCodes.CONFLICT)+ })++ it('fails clearly when its model has left the key, instead of swapping it', async () => {+ const ctx = await context()+ const key = await customKey({ ctx, name: 'Team', models: ['team-model'] })+ const conversation = await createConversation({ ctx, body: { projectId: ctx.project.id, providerConfigId: key.id, modelName: 'team-model' } })+ await db.update('agent_conversation', conversation.id, { modelName: 'retired-model' })++ await expect(getConfig({ ctx, conversationId: conversation.id })).rejects.toMatchObject({ error: { code: ErrorCode.VALIDATION } })+ })++ it('runs the model stored on the conversation, not an older one the queued job carried', async () => {+ const ctx = await context()+ const key = await customKey({ ctx, name: 'Team', models: ['team-model'] })+ const conversation = await createConversation({ ctx, body: { projectId: ctx.project.id, providerConfigId: key.id, modelName: 'team-model' } })++ const config = await getConfig({ ctx, conversationId: conversation.id, modelName: 'model-from-before-the-switch' })++ expect(config.modelId).toBe('team-model')+ })++ it('keeps its model when the worker saves the turn with a tier name', async () => {+ const ctx = await context()+ const key = await customKey({ ctx, name: 'Team', models: ['team-model'] })+ const conversation = await createConversation({ ctx, body: { projectId: ctx.project.id, providerConfigId: key.id, modelName: 'team-model' } })++ await agentRpcHandlers(app.log).saveAgentMessages({ conversationId: conversation.id, messages: [{ role: 'user', content: 'hi' }], uiMessages: [], modelName: 'smart' })++ const stored = await db.findOneByOrFail<AgentConversation>('agent_conversation', { id: conversation.id })+ expect(stored).toMatchObject({ providerConfigId: key.id, modelName: 'team-model' })+ })++ it('goes back to the legacy path when its key is deleted', async () => {+ const ctx = await context()+ const key = await customKey({ ctx, name: 'Team', models: ['team-model'] })+ const conversation = await createConversation({ ctx, body: { projectId: ctx.project.id, providerConfigId: key.id, modelName: 'team-model' } })++ await aiProviderService(app.log).delete(ctx.platform.id, key.id)++ const stored = await db.findOneByOrFail<AgentConversation>('agent_conversation', { id: conversation.id })+ expect(stored.providerConfigId).toBeNull()+ })++ it('is unpinned when its key stops serving the chat\'s project', async () => {+ const ctx = await context()+ const key = await customKey({ ctx, name: 'Team', models: ['team-model'] })+ const conversation = await createConversation({ ctx, body: { projectId: ctx.project.id, providerConfigId: key.id, modelName: 'team-model' } })++ await aiProviderService(app.log).update(ctx.platform.id, key.id, { displayName: 'Team', projectScope: 'selected', projectIds: [] })++ const stored = await db.findOneByOrFail<AgentConversation>('agent_conversation', { id: conversation.id })+ expect(stored.providerConfigId).toBeNull()+ })+})++describe('a legacy chat that stores only a model name', () => {+ it('keeps its model and pins the Default AI key after the first turn', async () => {+ const ctx = await context()+ const chatKey = await mockAndSaveAIProvider({ platformId: ctx.platform.id, provider: AIProviderName.OPENAI, enabledForChat: true })+ const model = curatedOpenAiModel()+ const conversation = await createConversation({ ctx, body: { modelName: model } })++ const first = await getConfig({ ctx, conversationId: conversation.id, dryRun: false })+ await releaseRun({ conversationId: conversation.id })+ const second = await getConfig({ ctx, conversationId: conversation.id })++ const stored = await db.findOneByOrFail<AgentConversation>('agent_conversation', { id: conversation.id })+ expect(stored).toMatchObject({ providerConfigId: chatKey.id, modelName: model })+ expect([first.modelId, second.modelId]).toEqual([model, model])+ })++ it('keeps a tier name on an own key, so later turns keep its model and thinking', async () => {+ const ctx = await context()+ await mockAndSaveAIProvider({ platformId: ctx.platform.id, provider: AIProviderName.OPENAI, enabledForChat: true })+ const tierId = nonDefaultChatTierId()+ const conversation = await createConversation({ ctx, body: { modelName: tierId } })++ const first = await getConfig({ ctx, conversationId: conversation.id, dryRun: false })+ await releaseRun({ conversationId: conversation.id })+ const second = await getConfig({ ctx, conversationId: conversation.id, dryRun: false })++ const stored = await db.findOneByOrFail<AgentConversation>('agent_conversation', { id: conversation.id })+ expect(stored.modelName).toBe(tierId)+ expect(second.modelId).toBe(first.modelId)+ expect(second.tier).toEqual(first.tier)+ expect(first.tier.id).toBe(tierId)+ })++ it('pins the tier that ran when an OpenRouter chat\'s model name is unknown, so the next turn still works', async () => {+ const ctx = await context()+ await mockAndSaveAIProvider({ platformId: ctx.platform.id, provider: AIProviderName.OPENROUTER, enabledForChat: true })+ const conversation = await createConversation({ ctx, body: { modelName: 'a-retired-tier' } })++ const first = await getConfig({ ctx, conversationId: conversation.id, dryRun: false })+ await releaseRun({ conversationId: conversation.id })+ const second = await getConfig({ ctx, conversationId: conversation.id, dryRun: false })++ const stored = await db.findOneByOrFail<AgentConversation>('agent_conversation', { id: conversation.id })+ expect(stored.modelName).toBe(first.tier.id)+ expect(second.modelId).toBe(first.modelId)+ })++ it('is not pinned to a key deleted while its turn was being set up', async () => {+ const ctx = await context()+ const chatKey = await mockAndSaveAIProvider({ platformId: ctx.platform.id, provider: AIProviderName.OPENAI, enabledForChat: true })+ const model = curatedOpenAiModel()+ const conversation = await createConversation({ ctx, body: { modelName: model } })+ await aiProviderService(app.log).delete(ctx.platform.id, chatKey.id)++ await agentChatModel(app.log).persistPick({ platformId: ctx.platform.id, projectId: ctx.project.id, conversationId: conversation.id, loadedModelName: model, pick: { modelTierId: null, providerConfigId: chatKey.id, modelName: model } })++ const stored = await db.findOneByOrFail<AgentConversation>('agent_conversation', { id: conversation.id })+ expect(stored.providerConfigId).toBeNull()+ })++ it('moves to the Default tier when the platform no longer has a Default AI key', async () => {+ const ctx = await context()+ const key = await customKey({ ctx, name: 'Team', models: ['team-model'] })+ await createTier({ ctx, entries: [{ configId: key.id, modelId: 'team-model' }] })+ const conversation = await createConversation({ ctx, body: { modelName: 'a-model-from-the-deleted-key' } })++ const config = await getConfig({ ctx, conversationId: conversation.id })++ expect(config.providerConfigId).toBe(key.id)+ expect(config.modelId).toBe('team-model')+ })++ it('still falls back when its model is unknown, so an upgrade never breaks it', async () => {+ const ctx = await context()+ await mockAndSaveAIProvider({ platformId: ctx.platform.id, provider: AIProviderName.OPENAI, enabledForChat: true })+ const conversation = await createConversation({ ctx, body: { modelName: 'a-retired-model' } })++ const config = await getConfig({ ctx, conversationId: conversation.id })++ expect(config.modelId).not.toBe('a-retired-model')+ })+})++describe('a chat with no model picked', () => {+ it('stores the platform default on its first turn', async () => {+ const ctx = await context()+ const key = await customKey({ ctx, name: 'Team', models: ['team-model'] })+ const tier = await createTier({ ctx, entries: [{ configId: key.id, modelId: 'team-model' }] })+ const conversation = await createConversation({ ctx, body: {} })++ await getConfig({ ctx, conversationId: conversation.id, dryRun: false })++ const stored = await db.findOneByOrFail<AgentConversation>('agent_conversation', { id: conversation.id })+ expect(stored).toMatchObject({ modelTierId: tier.id, providerConfigId: null, modelName: null })+ })++ it('can be talked to on a platform that has tiers but no Default AI key', async () => {+ const ctx = await context()+ const key = await customKey({ ctx, name: 'Team', models: ['team-model'] })+ await createTier({ ctx, entries: [{ configId: key.id, modelId: 'team-model' }] })+ const add = vi.fn()+ vi.spyOn(jobQueueModule, 'jobQueue').mockImplementation((log) => ({ ...originalJobQueue(log), add }))+ const conversation = await createConversation({ ctx, body: {} })++ const response = await ctx.post(`${CONVERSATIONS_URL}/${conversation.id}/messages`, { content: 'hello' })++ expect(response.statusCode).toBe(StatusCodes.OK)+ expect(add.mock.calls.some(([call]) => call.data.jobType === WorkerJobType.EXECUTE_AGENT_RUN)).toBe(true)+ })+})++describe('platform-wide AI work', () => {+ it('runs on the Default tier\'s main model when there is no Default AI key and its key serves every project', async () => {+ const ctx = await context()+ const key = await customKey({ ctx, name: 'Team', models: ['team-model'] })+ await createTier({ ctx, entries: [{ configId: key.id, modelId: 'team-model' }] })++ const work = await agentHelpers.resolvePlatformWorkModel({ platformId: ctx.platform.id, log: app.log })++ expect(work).toMatchObject({ config: { configId: key.id }, modelId: 'team-model' })+ })++ it('runs memory on the Fast tier\'s main model, not the Default tier\'s', async () => {+ const ctx = await context()+ const key = await customKey({ ctx, name: 'Team', models: ['big-model', 'small-model'] })+ await createTier({ ctx, entries: [{ configId: key.id, modelId: 'big-model' }] })+ const fast = await createTier({ ctx, name: 'Quick', entries: [{ configId: key.id, modelId: 'small-model' }] })+ expect((await ctx.post(`/v1/platform-model-tiers/${fast.id}`, { isFast: true })).statusCode).toBe(StatusCodes.OK)+ const createModel = vi.spyOn(aiUtils, 'createModel')++ await agentHelpers.resolvePlatformFastModel({ platformId: ctx.platform.id, log: app.log })++ expect(createModel).toHaveBeenCalledWith(expect.objectContaining({ providerConfigId: key.id, modelId: 'small-model' }))+ })++ it('does not use a Default tier whose main key is limited to some projects', async () => {+ const ctx = await context()+ const key = await customKey({ ctx, name: 'Team', models: ['team-model'], projectScope: 'selected', projectIds: [ctx.project.id] })+ await createTier({ ctx, entries: [{ configId: key.id, modelId: 'team-model' }] })++ const work = await agentHelpers.resolvePlatformWorkModel({ platformId: ctx.platform.id, log: app.log })++ expect(work).toBeNull()+ })+})++describe('knowledge base embeddings', () => {+ it('upload and query both embed with the Embeddings key, not the Default AI key', async () => {+ const ctx = await context()+ await mockAndSaveAIProvider({ platformId: ctx.platform.id, provider: AIProviderName.ANTHROPIC, enabledForChat: true })+ const embeddingsKey = await mockAndSaveAIProvider({ platformId: ctx.platform.id, provider: AIProviderName.OPENAI })+ await aiToolConfigService(app.log).upsert(ctx.platform.id, { capability: AiToolCapability.EMBEDDINGS, provider: AiToolProvider.AI_PROVIDER, config: { aiProviderId: embeddingsKey.id } })+ const createEmbeddingModel = vi.spyOn(aiUtils, 'createEmbeddingModel')++ await knowledgeBaseService(app.log).embedderFor({ platformId: ctx.platform.id, projectId: ctx.project.id })++ expect(createEmbeddingModel).toHaveBeenCalledWith(expect.objectContaining({ providerConfigId: embeddingsKey.id }))+ })++ it('refuses an Embeddings choice that is not one of the platform\'s AI keys', async () => {+ const ctx = await context()++ await expect(aiToolConfigService(app.log).upsert(ctx.platform.id, { capability: AiToolCapability.EMBEDDINGS, provider: AiToolProvider.TAVILY, auth: { apiKey: 'key' } }))+ .rejects.toMatchObject({ error: { code: ErrorCode.VALIDATION } })+ })+})++describe('the provider a chat turn is billed under', () => {+ it('is the pinned key\'s provider, not the Default AI key\'s', async () => {+ const ctx = await context()+ await mockAndSaveAIProvider({ platformId: ctx.platform.id, provider: AIProviderName.OPENAI, enabledForChat: true })+ const key = await customKey({ ctx, name: 'Team', models: ['team-model'] })++ const provider = await agentHelpers.resolveConversationProviderName({ platformId: ctx.platform.id, projectId: ctx.project.id, providerConfigId: key.id, log: app.log })++ expect(provider).toBe(AIProviderName.CUSTOM)+ })+})++async function context(): Promise<TestContext> {+ return createTestContext(app, { plan: { agentsEnabled: true, chatEnabled: true, aiProvidersEnabled: true } })+}++async function customKey({ ctx, name, models, projectScope, projectIds }: { ctx: TestContext, name: string, models: string[], projectScope?: 'all' | 'selected' | 'except', projectIds?: string[] }): Promise<{ id: string }> {+ return mockAndSaveAIProvider({+ platformId: ctx.platform.id,+ provider: AIProviderName.CUSTOM,+ displayName: name,+ config: {+ baseUrl: 'https://api.example.com/v1',+ apiKeyHeader: 'Authorization',+ models: models.map((modelId) => ({ modelId, modelName: modelId, modelType: AIProviderModelType.TEXT })),+ },+ ...(projectScope ? { projectScope } : {}),+ ...(projectIds ? { projectIds } : {}),+ })+}++async function createTier({ ctx, entries, name = 'Expert' }: { ctx: TestContext, entries: { configId: string, modelId: string }[], name?: string }): Promise<{ id: string }> {+ const response = await ctx.post('/v1/platform-model-tiers', { name, emoji: '🧠', description: null, entries })+ expect(response.statusCode).toBe(StatusCodes.OK)+ return response.json()+}++async function createConversation({ ctx, body }: { ctx: TestContext, body: Record<string, unknown> }): Promise<AgentConversation> {+ const response = await ctx.post(CONVERSATIONS_URL, body)+ expect(response.statusCode).toBe(StatusCodes.CREATED)+ return response.json()+}++async function getConfig({ ctx, conversationId, dryRun = true, modelName = null }: { ctx: TestContext, conversationId: string, dryRun?: boolean, modelName?: string | null }): ReturnType<ReturnType<typeof agentRpcHandlers>['getAgentConfig']> {+ return agentRpcHandlers(app.log).getAgentConfig({+ conversationId,+ platformId: ctx.platform.id,+ userId: ctx.user.id,+ userMessage: 'hello',+ modelName,+ dryRun,+ })+}++async function releaseRun({ conversationId }: { conversationId: string }): Promise<void> {+ await db.update('agent_conversation', conversationId, { status: 'IDLE', activeRunId: null })+}++function nonDefaultChatTierId(): string {+ const catalog = modelTierCatalog.current('chat')+ const tier = catalog.tiers.find((candidate) => candidate.id !== catalog.defaultTierId)+ assertNotNullOrUndefined(tier, 'chat tier other than the default')+ return tier.id+}++function curatedOpenAiModel(): string {+ return aiProviderUtils.runnableChatModelIds({ provider: AIProviderName.OPENAI })[0]+}++const CONVERSATIONS_URL = '/v1/agents/conversations'diff --git a/packages/server/api/test/unit/app/ee/agent/agent-model-resolution.test.ts b/packages/server/api/test/unit/app/ee/agent/agent-model-resolution.test.tsindex 15a9f404042..0b372163263 100644--- a/packages/server/api/test/unit/app/ee/agent/agent-model-resolution.test.ts+++ b/packages/server/api/test/unit/app/ee/agent/agent-model-resolution.test.ts@@ -151,9 +151,35 @@ describe('resolveModelIdForProvider', () => {})})+describe('resolvePinnedModelId', () => {+ const pinned = ({ provider, modelName, modelScope, modelIds }: { provider: AIProviderName, modelName: string, modelScope?: 'all' | 'selected', modelIds?: string[] }) =>+ agentModelResolution.resolvePinnedModelId({ provider, modelName, modelScope, modelIds, log })++ it('refuses a managed tier the catalog no longer publishes, instead of running the default', () => {+ expect(pinned({ provider: AIProviderName.ACTIVEPIECES, modelName: 'a-retired-tier' })).toBeNull()+ expect(pinned({ provider: AIProviderName.OPENROUTER, modelName: 'a-retired-tier' })).toBeNull()+ })++ it('refuses a managed tier whose model the key no longer allows', () => {+ const modelId = pinned({ provider: AIProviderName.OPENROUTER, modelName: 'smart' })+ expect(modelId).not.toBeNull()+ expect(pinned({ provider: AIProviderName.OPENROUTER, modelName: 'smart', modelScope: 'selected', modelIds: ['some/other-model'] })).toBeNull()+ expect(pinned({ provider: AIProviderName.OPENROUTER, modelName: 'smart', modelScope: 'selected', modelIds: [modelId ?? ''] })).toBe(modelId)+ })+})+describe('resolveModelIdForAnalytics', () => {- const forAnalytics = ({ provider, selectedModel, surface = 'chat' }: { provider: AIProviderName | null, selectedModel: string | null, surface?: ModelTierSurface }) =>- agentModelResolution.resolveModelIdForAnalytics({ provider, selectedModel, surface })+ const forAnalytics = ({ provider, selectedModel, surface = 'chat', pinned = false }: { provider: AIProviderName | null, selectedModel: string | null, surface?: ModelTierSurface, pinned?: boolean }) =>+ agentModelResolution.resolveModelIdForAnalytics({ provider, selectedModel, surface, pinned })++ it('reports a pinned model as stored, even on a key with no curated list', () => {+ expect(forAnalytics({ provider: AIProviderName.CUSTOM, selectedModel: 'my-model', pinned: true })).toBe('my-model')+ expect(forAnalytics({ provider: AIProviderName.OPENAI, selectedModel: 'smart', pinned: true })).toBe('gpt-5.5')+ })++ it('reports nothing instead of throwing when the provider has no model to name', () => {+ expect(forAnalytics({ provider: AIProviderName.CUSTOM, selectedModel: 'my-model' })).toBeNull()+ })it('reports the model the turn ran on when the provider is known', () => {expect(forAnalytics({ provider: AIProviderName.OPENAI, selectedModel: 'gpt-4.1' })).toBe('gpt-4.1')diff --git a/packages/server/api/test/unit/app/ee/agent/agent-rpc-handlers.test.ts b/packages/server/api/test/unit/app/ee/agent/agent-rpc-handlers.test.tsindex 3b2168a7733..11206fa1eff 100644--- a/packages/server/api/test/unit/app/ee/agent/agent-rpc-handlers.test.ts+++ b/packages/server/api/test/unit/app/ee/agent/agent-rpc-handlers.test.ts@@ -40,7 +40,7 @@ const { mockGetFileOrThrow, mockKbSearch, mockIsSearchable } = vi.hoisted(() =>}))vi.mock('../../../../../src/app/knowledge-base/knowledge-base.service', () => ({- knowledgeBaseService: () => ({ getFileOrThrow: mockGetFileOrThrow, search: mockKbSearch, isSearchable: mockIsSearchable }),+ knowledgeBaseService: () => ({ getFileOrThrow: mockGetFileOrThrow, search: mockKbSearch, isSearchable: mockIsSearchable, embeddingModelFor: async () => ({ model: {}, providerOptions: {} }) }),}))const { mockEmbed } = vi.hoisted(() => ({@@ -119,7 +119,6 @@ vi.mock('../../../../../src/app/ee/agent/agent-helpers', () => ({assertProjectSwitchKeepsKey: mockAssertProjectSwitchKeepsKey,surfaceOf: () => 'flow',resolveFastModel: mockResolveFastModel,- resolveEmbeddingModel: () => ({ model: {}, providerOptions: {} }),conversationRepo: () => ({findOneBy: mockFindOneBy,findOneByOrFail: mockFindOneBy,diff --git a/packages/server/worker/src/lib/execute/jobs/ee/agent/execute-agent-run.ts b/packages/server/worker/src/lib/execute/jobs/ee/agent/execute-agent-run.tsindex 5d9e6f5976e..cef1c4b0d88 100644--- a/packages/server/worker/src/lib/execute/jobs/ee/agent/execute-agent-run.ts+++ b/packages/server/worker/src/lib/execute/jobs/ee/agent/execute-agent-run.ts@@ -356,7 +356,7 @@ export const executeAgentRunJob: JobHandler<ExecuteAgentRunJobData, FireAndForge...(config.previousUiMessages as PersistedAgentMessage[]),...(uiParts.length > 0 ? [{ role: PersistedAgentRole.ASSISTANT, parts: uiParts, thinkingDurationMs, tainted: taintState.readInThisReply() }] : []),],- ...answeredByOf({ config, answeredBy: turn.answeredBy }),+ ...answeredByOf({ answeredBy: turn.answeredBy }),}const { error: cancelSaveError } = await tryCatch(() => ctx.apiClient.saveAgentMessages(cancelSavePayload))if (cancelSaveError) {@@ -409,7 +409,7 @@ export const executeAgentRunJob: JobHandler<ExecuteAgentRunJobData, FireAndForge],...spreadIfDefined('title', autoTitle),...spreadIfDefined('modelName', isNil(data.modelName) && isNil(config.platformTier) ? config.tier.id : undefined),- ...answeredByOf({ config, answeredBy: turn.answeredBy }),+ ...answeredByOf({ answeredBy: turn.answeredBy }),}await retryWithBackoff({ fn: () => ctx.apiClient.saveAgentMessages(savePayload), description: 'Saving the transcript', throwOnExhausted: true, log })@@ -504,8 +504,8 @@ export const executeAgentRunJob: JobHandler<ExecuteAgentRunJobData, FireAndForge},}-function answeredByOf({ config, answeredBy }: { config: AgentConfigResponse, answeredBy: TurnModel }): Pick<SaveAgentMessagesRequest, 'answeredBy'> {- return isNil(config.platformTier) ? {} : { answeredBy: { provider: answeredBy.provider, modelId: answeredBy.modelId } }+function answeredByOf({ answeredBy }: { answeredBy: TurnModel }): Pick<SaveAgentMessagesRequest, 'answeredBy'> {+ return { answeredBy: { provider: answeredBy.provider, modelId: answeredBy.modelId } }}function turnModelsFor({ config, model, billing, metadata }: { config: AgentConfigResponse, model: LanguageModel, billing: ActivepiecesAiBilling, metadata: ModelMetadata }): [TurnModel, ...TurnModel[]] {diff --git a/packages/web/src/app/routes/platform/setup/ai-capabilities/catalog.ts b/packages/web/src/app/routes/platform/setup/ai-capabilities/catalog.tsindex 340a3f7e7bb..f2bf378769b 100644--- a/packages/web/src/app/routes/platform/setup/ai-capabilities/catalog.ts+++ b/packages/web/src/app/routes/platform/setup/ai-capabilities/catalog.ts@@ -90,6 +90,8 @@ function providerCovers({return !isNil(providerCapabilities.webSearch);case AiToolCapability.IMAGE_GENERATION:return !isNil(providerCapabilities.defaultImageModel);+ case AiToolCapability.EMBEDDINGS:+ return providerCapabilities.supportsEmbedding;case AiToolCapability.WEB_SCRAPING:return false;}diff --git a/packages/web/src/app/routes/platform/setup/ai/capabilities-tab/index.tsx b/packages/web/src/app/routes/platform/setup/ai/capabilities-tab/index.tsxindex 471333cbd1e..89bc89e4699 100644--- a/packages/web/src/app/routes/platform/setup/ai/capabilities-tab/index.tsx+++ b/packages/web/src/app/routes/platform/setup/ai/capabilities-tab/index.tsx@@ -6,7 +6,14 @@ import {AiToolConfigWithoutSensitiveData,} from '@activepieces/shared';import { t } from 'i18next';-import { Globe, Image, LucideIcon, Search, Trash2 } from 'lucide-react';+import {+ Database,+ Globe,+ Image,+ LucideIcon,+ Search,+ Trash2,+} from 'lucide-react';import {AdminPageHeader,@@ -220,4 +227,5 @@ const CAPABILITY_ICON: Record<AiToolCapability, LucideIcon> = {[AiToolCapability.WEB_SEARCH]: Search,[AiToolCapability.WEB_SCRAPING]: Globe,[AiToolCapability.IMAGE_GENERATION]: Image,+ [AiToolCapability.EMBEDDINGS]: Database,};