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(ai): serve the published model tiers to the chat and agent pickers (#15838)
Louai Boumedienef8cd637d018b
diff --git a/brain/knowledge/ai-intelligence/ai-providers.md b/brain/knowledge/ai-intelligence/ai-providers.mdindex 0f4148f782b..96f0e697cd9 100644--- a/brain/knowledge/ai-intelligence/ai-providers.md+++ b/brain/knowledge/ai-intelligence/ai-providers.md@@ -87,6 +87,7 @@ the URL. The file is published from the console in `ap-analytics`; see decision- **The api test suite runs on the bundled tiers, never the live file.** `packages/server/api/vitest.setup.ts` points `AP_MODEL_TIERS_URL` at `model-tiers.invalid`, so boot warm-up fails fast and every test resolves against `ACTIVEPIECES_CHAT_TIERS`. CI runners have egress, and the first time a resolver read the file in CI the live `defaultTierId` was `fast`, which turned a test asserting the release default (Expert) red with no code change behind it. A test that wants the published shape mocks `modelTierCatalog` (`test/unit/app/ee/agent/model-tier-fixture.ts`); an integration test that boots the app cannot be handed a file.- **A published tier without `nativeModelId` keeps every BYOK key on the release's model for that tier id.** `nativeModelIdFor` (`ee/agent/agent-model-resolution.ts`) takes the tier's own `nativeModelId`, else the `nativeModelId` of the bundled `ACTIVEPIECES_CHAT_TIERS` entry with the same id, else null, which sends a curated provider to its first curated model and a catalogue-less provider to the first text model its key lists. The fallback is keyed by tier id only, so two lists sharing an id resolve to the same native model on every own-key provider, whatever their managed models are. The BYOK tier map (PR 6 of the configurable tiers work) is the real fix.- **`premium` pairs `modelId: anthropic/claude-opus-4.8` with `nativeModelId: claude-opus-4-7` on purpose.** The managed tier runs Opus 4.8 through OpenRouter; a customer's own Anthropic key runs Opus 4.7. The reason is cost on the customer's key: Heavy on a BYOK key must not silently move the customer onto the most expensive model without them choosing it. The console's `defaults.ts` carries the same pair. Do not "fix" either copy.+- **Whatever the console publishes is in every Cloud customer's picker within about 20 minutes on a fresh page load, and about 35 minutes on a page left open: no draft, no staging, no filter.** `GET /v1/ai-providers/tiers` (`ListModelTiers` in `ai-provider-controller.ts`) returns `modelTierCatalog.current(surface)` for both lists, keyed `flow` and `chat`, to USER and ENGINE principals with no platform or project scope, because the tiers are global; the response schema puts only `id`, `label` and `modelId` on the wire. The web reads it through one hook, `aiProviderQueries.useModelTiers(surface)` (`features/platform-admin/hooks/ai-provider-hooks.ts`): the chat selector passes `chat`, the agent picker passes `flow`, and nothing but a test checks which one a caller passed, since the surface argument the server requires does not reach this layer. Both pickers show the release's bundled tiers while the call is in flight or has failed, so they never render empty. The web query is stale after 15 minutes and refetches on that interval while the tab is visible (`staleTime` alone only gates the mount, focus and reconnect triggers, so a tab left open would never refetch), which is where the two bounds come from: CDN `max-age` 5 min plus the server's 15-min TTL for a fresh load, plus one 15-min client tick for an open page, and the server serves its old copy once while it refreshes. The AI piece's own model dropdown still reads the bundled constant (the props.ts bullet above) until it stores tier ids.- **Every provider auth schema is written twice, and only one copy is the one the server reads.** `packages/core/utils/src/lib/ai-provider-credentials.ts` (plain zod) backs the API and the web app; `packages/core/piece-types/src/lib/ai-providers.ts` (`zod/mini`, so `.check(z.minLength(1))` and `z.optional(x)` instead of `.min(1)` / `.optional()`) is what `@activepieces/pieces-framework` re-exports to pieces and the engine. Adding a credential field to only one leaves the other silently stripping it — zod objects drop unknown keys, so the upsert still returns 200 and the failure surfaces later as a provider-side auth error. Change both.- **A schema change in `packages/core/utils` is invisible to another package's tests until you build it.** Sibling packages resolve `@activepieces/core-utils` through its `main` (`dist/src/index.js`), so vitest in `core/ai-providers` keeps parsing against the stale build and a new optional field reads back as dropped. `cd packages/core/utils && npm run build` first. The API dev server is the opposite: `serve` runs under tsx with the tsconfig path alias, so it reads `src` directly — but it only watches `packages/core/shared/src/**`, not `core/utils`, so a core-utils-only edit needs a restart to take effect. The same staleness makes a **branch** look green when it is not: `dist` is gitignored, so after `git checkout` it still holds whatever the previous branch built, and a test that depends on a field added by a sibling PR passes locally and fails in CI. Rebuild `core/utils` after every branch switch before trusting a run.@@ -173,7 +174,7 @@ the URL. The file is published from the console in `ap-analytics`; see decision- **Chat model tiers are Activepieces-only.** `ACTIVEPIECES_CHAT_TIERS` (`fast`/`smart`/`premium`, shown as Fast/Expert/Heavy) hold OpenRouter-shaped Anthropic ids, so they only mean anything for the ACTIVEPIECES and OPENROUTER chat providers. Any provider that declares `ALLOWED_CHAT_MODELS_BY_PROVIDER` (openai, anthropic, google) picks a real model id from that list instead. Naively stripping the tier's vendor prefix for every provider is what once sent `claude-haiku-4-5` to OpenAI and broke every message.- **A short model list in the key's picker is the vendor's catalog, not a truncation.** `listModels` returns whatever the provider's own `/models` endpoint gives and filters nothing except the key's `modelScope` allow-list. Anthropic ships roughly a dozen models, OpenAI ~80 (mostly embeddings/tts/whisper), while OpenRouter is an aggregator and returns 400+ from every vendor it proxies — so the counts differ by an order of magnitude by design. Anthropic pages at 20 by default, which is why its request pins `?limit=1000`. If the list shows exactly three Claude models, that is the *chat* dropdown reading the curated `ANTHROPIC_CHAT_MODELS`, a different surface from the admin picker.- **Read the chat model list through `aiProviderUtils.getCuratedChatModels({ provider })`.** It is the one accessor the server resolver (`agentHelpers.resolveModelIdForProvider`) and the chat dropdown share, so the two cannot drift; it returns `{ id, label }` or `undefined` — never an empty list, so callers may treat a result as non-empty. Labels come from the hardcoded `CHAT_MODEL_LABELS` (falling back to the id) rather than `AIProviderModel.name`, because the live `listModels` response cannot supply one for every provider: anthropic returns `display_name` and google `displayName`, but OpenAI's `/v1/models` returns ids only.-- **`conversation.modelName` carries either a tier id or a real model id** — it is a free string with no discriminator. A legacy tier id resolves to the tier's equivalent model when the provider ships it, else the provider's first curated model, so old conversations keep working after a provider switch. Note `premium` maps to opus 4.8, which the native anthropic list does not carry, so a legacy `premium` on anthropic lands on Sonnet.+- **`conversation.modelName` carries either a tier id or a real model id** — it is a free string with no discriminator. A legacy tier id resolves to the tier's equivalent model when the provider ships it, else the provider's first curated model, so old conversations keep working after a provider switch. Note `premium` maps to opus 4.8, which the native anthropic list does not carry, so a legacy `premium` on anthropic lands on Sonnet. A new web chat sends no model at all until the user picks one (`use-chat.ts` starts `modelName` at null): the server resolves the published chat default for that turn and the worker stamps the tier it ran onto the conversation, so the chat default lives in one place, the published file, and the picker shows it as selected by reading `chat.defaultTierId` from the tiers endpoint. Before that change the web sent `smart` explicitly on every new chat, which is why a published `defaultTierId` never reached web chats. **That holds only where the picker shows tiers.** An own-key provider with a curated model list shows concrete models, but a null model still resolves the published chat default tier and then that tier's `nativeModelId`, which is in the Anthropic curated list: a published default of `fast` ran Haiku on every Anthropic-key chat while the picker showed Sonnet, and the worker's tier stamp kept it there. So a new chat on a curated key sends the model the picker shows (`chatUtils.newChatModelName`); the tier picker sends null, and so does any chat whose picker is hidden (an agent or the agent builder: a saved agent's job carries the agent's own model anyway). The chat box does not mount until the chat provider has loaded, or a message sent in that window would go out with no model. Caught in review on #15838.- Chat-provider resolution is **first `enabledForChat` row wins, else the visible managed ACTIVEPIECES row** — `pickChatRow`, shared by `findAvailableChatProviderRow` and both list endpoints. So a platform with `[openai, activepieces]` both chat-enabled still resolves to **openai** (an explicit pick beats the fallback), while a platform with nothing flagged resolves to the managed row instead of to nothing. The flag a list endpoint reports is therefore the *resolved* one, not the stored column — `ai_provider.enabledForChat` can say `false` on the row chat actually runs on, and `ensureManagedProviderRow` no longer writes it. The client mirror is `aiProviderQueries.useChatProvider()` (`providers.find((p) => p.enabledForChat)`) — always read the resolved chat provider through it rather than re-deriving the rule inline. A project entry is chat-enabled when it owns the picked row, whichever of its keys carries the flag: ranking (`selected` > `except` > `all`, newest first) and chat selection answer different questions, so reading `rows[0].enabledForChat` makes the client report "no provider configured" whenever the chat-enabled key is not the ranking winner. `findBy()` has no `ORDER BY`, so "first" is not guaranteed stable when several providers are chat-enabled.- Listing providers is **not a pure read**: both `listConfigs` and `listForProject` go through `listVisibleRows`, which inserts the ACTIVEPIECES provider row when `aiCreditsEnabled && !activepiecesExists`. A `GET /v1/ai-providers` can therefore create a row. It also applies the hidden-provider filter (`plan.embeddingEnabled` hides the managed provider), which is why the client can trust its output without re-checking flags.- **A managed chat turn is charged in two events, not one.** The model cost arrives per model call fromdiff --git a/brain/knowledge/ai-intelligence/testing-model-tiers-locally.md b/brain/knowledge/ai-intelligence/testing-model-tiers-locally.mdnew file mode 100644index 00000000000..05ce0b69e12--- /dev/null+++ b/brain/knowledge/ai-intelligence/testing-model-tiers-locally.md@@ -0,0 +1,15 @@+---+icon: 🧪+---+# Testing Model Tiers Locally++How to see the published tier file drive chat, agents and the pickers on a dev box, without publishing anything. The server fetches through `safeHttp`, which blocks loopback, so the allow-list line is not optional.++1. Write a tier file with both lists. Put something in it that the release does not ship, such as a chat-only tier `turbo` and a relabelled `smart` ("Expert (from file)"), so you can tell the file from the bundled constants at a glance. Every tier needs `id`, `label`, `modelId` (an OpenRouter id) and `thinkingBudget`; the file needs `version`, `publishedAt`, `publishedBy`, `tiers`, `defaultTierId`, and `chatTiers` with `chatDefaultTierId` together or not at all.+2. Serve it: `python3 -m http.server 8787` in that folder.+3. In `.env.dev`: `AP_MODEL_TIERS_URL=http://127.0.0.1:8787/tiers.json` and `AP_SSRF_ALLOW_LIST=127.0.0.1`. Start with `npm run dev`. The boot log must not say "Failed to load the AI model tiers file".+4. Chat page: the picker shows the chat list, with the chat default selected on a fresh chat. Agent model picker: the flow list only. A tier on one list and not the other proves the surfaces are told apart.+5. Send one chat message without picking. The `Chat config resolved` log line carries `tier.id`, `surface` and the model id the turn ran on.+6. To see a re-point, change a `modelId` in the file, restart the backend (the catalog cache is in-process, 15 minutes) and reload the page (the web refetches every 15 minutes). Same label, new model in the log.++The Activepieces provider must be enabled for chat under Platform, AI, which needs `AP_EDITION=cloud` and `AP_OPENROUTER_PROVISION_KEY` on the dev box.diff --git a/brain/knowledge/engineering/ci-pr-review-hygiene.md b/brain/knowledge/engineering/ci-pr-review-hygiene.mdindex 29c60561a18..e0ff28107a1 100644--- a/brain/knowledge/engineering/ci-pr-review-hygiene.md+++ b/brain/knowledge/engineering/ci-pr-review-hygiene.md@@ -84,7 +84,7 @@ Enforcement is the **`Codeowners review` repository ruleset** (active on the def- **Docs videos belong on `cdn.activepieces.com/videos/docs/`, never in the repo — and `docs/resources/` is the legacy path that suggests otherwise.** Every `<video>` already in the wiki points at that CDN prefix, but `docs/resources/` still holds ~43 MB of in-repo gifs from before the convention, so the tree itself gives mixed signals and a contributor reasonably copies the wrong one. [#15399](https://github.com/activepieces/activepieces/pull/15399) added a fresh `docs/videos/` folder with 155 MB of `.mp4` against an 880 MB `.git`, including one byte-identical duplicate. Nothing in CI catches it: `PR size` exempts unmatched areas and counts lines, not bytes, and a binary blob contributes zero lines. Review the *file list* of a docs PR, not just its diff. Once such a branch is pushed the blobs are already on the remote — dropping them at the tip only keeps them out of `main` if the PR is **squash**-merged and the branch deleted, so a merge commit ships them permanently. Because the blobs stay reachable until the branch is deleted, nobody has to re-request the assets from whoever recorded them — pull them straight back out of history with `git archive <commit> docs/videos | tar -x --strip-components=2 -C <dir>`, picking the commit *before* the removal, which is also how you get them under the post-rename filenames the pages already reference.- **Mintlify-editor PRs arrive with `untitled-page*.mdx` at the docs root and `(1)`/`(2)`-suffixed media, wired into `docs.json` under those names.** The bot commits whatever the web editor autosaved, so pages land as `docs/untitled-page-5.mdx` rather than in the folder their nav group implies, and re-uploaded assets keep the browser's download-collision suffix (`data-flow-(1).mp4`, `01---insert-panel-(1.2x).mp4`). Mintlify's own link check passes — the names are internally consistent — so nothing flags it. Rename the files, fix the `docs.json` entries, then diff the *flattened nav tree* against `main` rather than reading the raw JSON diff: the editor also reindents, which buried a real change (the Enterprise Control `Security` group demoted to a child of `Guides`) inside 242 lines of whitespace churn on that PR.- **After rewriting a lower branch of a stack, the `--onto` upstream you need is that branch's *old* tip, and `<branch>@{1}` is where to read it.** Every commit above a rewritten branch has to be replayed with `git rebase --onto <new lower tip> <old lower tip> <upper>`, and the old tip stops being reachable by name the moment the rebase finishes. Guessing a SHA from an earlier terminal scroll either fails outright with `invalid upstream` or, worse, succeeds against the wrong base and replays a handful of commits instead of all of them, which surfaces as a pile of conflicts the branch has with itself. `git rev-parse <branch>@{1}` reads it from the reflog every time. Re-run the whole `--onto` chain top-down after any lower-branch amend, and confirm afterwards that `git log --format=%s main..<top>` has no duplicate subjects.-- **When a stacked PR's parent is squash-merged, merge `main` into the child and retarget; do not rebase.** The parent's commits are on `main` by content but not by SHA, so `git merge origin/main` on the child sees both sides make the same change and auto-merges it; only the lines the child *rewrote* from the parent conflict, plus anything `main` changed in the same functions since (PR 2 of the model tiers work, #15825: three brain and docs pages where PR 2 had rewritten PR 1's sentences, and `agent-helpers.ts` where main's `resolveImageModelId` landed next to the surface-threaded fast-model helper). No history rewrite, a normal push, and the PR above it keeps its base untouched; a `--onto` rebase would replay every commit up the stack and force-push each. Two traps in the resolution. `git checkout --ours <file>` on a conflicted file takes the *whole* file from your side and silently discards main's auto-merged hunks in it, so resolve hunk by hunk and check `git diff origin/main -- <file>` afterwards shows only your lines. And `tsc` against the merged tree resolves `@activepieces/shared` through the built dist, which still holds the pre-merge exports: three phantom "has no exported member" errors vanished after `npx turbo run build --filter=@activepieces/server-utils`. Rebuild the core libs before believing a typecheck, then run the child's own tests.+- **When a stacked PR's parent is squash-merged, merge `main` into the child and retarget; do not rebase.** The parent's commits are on `main` by content but not by SHA, so `git merge origin/main` on the child sees both sides make the same change and auto-merges it; only the lines the child *rewrote* from the parent conflict, plus anything `main` changed in the same functions since (PR 2 of the model tiers work, #15825: three brain and docs pages where PR 2 had rewritten PR 1's sentences, and `agent-helpers.ts` where main's `resolveImageModelId` landed next to the surface-threaded fast-model helper). No history rewrite, a normal push, and the PR above it keeps its base untouched; a `--onto` rebase would replay every commit up the stack and force-push each. After the retarget, the PR's Commits tab still lists the parent's original SHAs (they reached `main` only as a squash) while Files changed shows only the child's files; the second view is the one that matters, so do not "fix" the first. Two traps in the resolution. `git checkout --ours <file>` on a conflicted file takes the *whole* file from your side and silently discards main's auto-merged hunks in it, so resolve hunk by hunk and check `git diff origin/main -- <file>` afterwards shows only your lines. And `tsc` against the merged tree resolves `@activepieces/shared` through the built dist, which still holds the pre-merge exports: three phantom "has no exported member" errors vanished after `npx turbo run build --filter=@activepieces/server-utils`. Rebuild the core libs before believing a typecheck, then run the child's own tests.- **Never commit a shared file onto a lower branch of a stack by copying it in from the stack top — you drag the upper branches' edits down with it.** Adding one gotcha bullet to a brain page while sitting on the top branch, then `git checkout <stash-or-top> -- <file>` after switching to the base branch, brings the *whole* file across, so every bullet the upper branches added to that page is silently duplicated onto the base. The commit stat is the tell: a one-line edit that reports six insertions is not a one-line edit. Switch branches first, re-apply the edit against **that branch's own copy** of the file, and re-check the stat before committing.- **Cherry-picking a merged PR onto a release branch: the commit-msg hook rejects it, and `breaking-changes.mdx` conflicts pull in other people's entries.** GitHub writes the squash header server-side, so `fix(flows): … instead of reverting them (#14899)` never faced commitlint — replaying it locally trips the 100-character header limit and `git cherry-pick --continue` dies in husky with the working tree half-applied. Finish it with `git commit --no-verify -C <original-sha>` rather than rewording, so the release branch keeps the same subject as `main`. The `Unreleased` section of `docs/install/reference/breaking-changes.mdx` conflicts on almost every pick because it accumulates entries from unrelated `main` work: take only the section your own commit added. **Take it by its own heading, not by "everything after the marker" — the incoming side of that hunk usually runs on into `main`'s copy of an already-published `## X.Y.Z` section, and pasting the tail duplicates ~200 lines of published notes that no test, lint or typecheck will ever notice.** The reliable move is to rebuild the file: `git checkout origin/release/<version> -- docs/install/reference/breaking-changes.mdx`, then insert your entry under its own `## <new version>` heading above the previous release's, and confirm with `git diff --stat` that only your lines are added and `grep -c '^## '` that no version heading appears twice. Then run `bun install` before pushing — the release branch's `bun.lock` is usually stale against its own `package.json`s, and the pick drags those stale piece versions along until install corrects them; `git diff --stat origin/release/<version> -- bun.lock` should come back as just the packages your pick actually bumped. Expect the **PR size** gate to block it too: the picked commits drag their own upstream tests along (769/600 in `server/api` for two picks), and splitting a replay of already-reviewed work is the wrong answer — add `large-pr-ok`.- **`api#test-ce` can exit 1 with every test passing — read the `Errors` line, not just `Tests`.** Vitest fails the run on *unhandled* errors too, so the summary reads `Test Files 92 passed`, `Tests 1076 passed`, `Errors 3 errors` and turbo reports `Failed: api#test-ce`. The recurring one is `TypeError: socket.destroySoon is not a function` at `Timeout.forceClose` in `@hono/node-server`, attributed to `test/integration/ce/mcp/mcp-platform-project-selection.test.ts` — a teardown race after the test already finished, not a failure of whatever you changed. Before chasing it, scroll to the `Unhandled Errors` block and check whether any test actually failed; if none did and the stack is in `forceClose`, re-run the job (`gh run rerun <id> --failed`) rather than editing code. Note the log section only has the whole `main` job, so `gh run view --job <id> --log-failed` buries it under thousands of lines of passing-test stderr — grep for `Unhandled Errors` or `Test Files`.diff --git a/brain/knowledge/memory.md b/brain/knowledge/memory.mdindex 22b8c5941cb..90679dcc461 100644--- a/brain/knowledge/memory.md+++ b/brain/knowledge/memory.md@@ -1,5 +1,6 @@- 2026-09-26 — PR 2 of the configurable tiers work (`feat/tier-resolution`, ENG-767) moved chat, agent and Run Agent model resolution onto the published tier file, with a required `surface` argument on the whole `agent-model-resolution` family and `surfaceOf({ source })` as the one source-to-list rule. Three calls made that day, recorded on the AI Providers page: a removed tier resolves to its surface default with a warning on every door; the `chat-tool-billing.ts` tier-label and FLOW_STEP-provider bug stays as it is and lives in ENG-772; the end-to-end re-point check runs on canary after merge with `AP_MODEL_TIERS_URL`, not locally.- 2026-09-24 — `test-unit` (api unit tests) only joined the PR pipeline on 2026-09-20 (#15653). A branch whose last CI run predates that has never had its unit tests run in CI; expect first-time failures when it merges `main`, as #15550 did (a test mock disagreeing with the real code, not a merge problem).+- 2026-09-27 — PR 3 of the configurable tiers work (`feat/tier-endpoint`, ENG-768): `GET /v1/ai-providers/tiers` serves both lists and both web pickers read it; a new chat no longer hardcodes `smart`. Found on the way: the live `ai/pricing.json` republished on 2026-09-26 carried `defaultTierId: fast`, which never reached web chats only because the web sent `smart` explicitly; it is being republished as `smart`.- 2026-09-20 — Found while planning the AI tier work: managed AI piece steps under-billed for about 23 days, between #14942 (2026-08-24) and #15494 (2026-09-16). #14942 changed the AI piece's `provider` prop to store an object (`{ provider, configId }`), but `flow-run-ai-usage-extractor` read that field with a string-only helper, so it resolved to `'unknown'`, failed the `provider !== ACTIVEPIECES` check in `resolveAiCreditWeight`, and charged **1 credit** per step instead of the model's table weight. It hit the five non-agent AI steps only, and only those configured or re-saved inside that window — a step saved earlier still held a plain string and billed correctly. `run_agent` was never affected, because it stores `aiProviderModel` instead. Nothing logged an error, and every test in the file passed a string provider, so the suite was green the whole time. The code is gone (#15494 deleted the extractor and bills on real provider cost now), so there is nothing left to fix — but the cloud credit events for that window are still wrong, and nobody has reconciled them.- 2026-09-20 — The live `ai/pricing.json` (version 1, published 2026-09-14) carried a leftover test tier `tier-4` / "New tier" on `anthropic/claude-opus-4.8`, created by clicking "add tier" in the pricing console and publishing. It was removed on 2026-09-24 by deleting the whole object, so the key is 404 until someone publishes again from the console; every server falls back to the bundled tiers and warns every five minutes until then. Next time, edit the row and publish; do not delete the object.- 2026-09-06 — Follow-up (Option B, "stub-writer cascade"): fix `engine-run-callback-service.ts` `ensureLogsFileExists` (line 84-86) so a fresh log-file stub isn't written with `{ steps: {}, tags: [] }` when the underlying file is missing — that stub is what the next BullMQ retry downloads and trips `EmptyResumeStateError` on. Options: (a) store `internalError` on `flow_run` itself so no stub is needed; (b) use a distinguishable stub shape (`executionState: null`) that the engine reader rejects fast; (c) `UnrecoverableError` in worker on state-loss errors so BullMQ doesn't retry the stub. Handles manual-admin-ops case (case #5 in the retention-hunt discussion) and any non-retention internal-error that would otherwise loop. Ping user once C.2/W.1/#2/#4 have landed.diff --git a/packages/server/api/src/app/ai/ai-provider-controller.ts b/packages/server/api/src/app/ai/ai-provider-controller.tsindex a991a7bc626..f8dce07f219 100644--- a/packages/server/api/src/app/ai/ai-provider-controller.ts+++ b/packages/server/api/src/app/ai/ai-provider-controller.ts@@ -1,4 +1,5 @@import { AiProviderKeyStatus, AIProviderName } from '@activepieces/core-utils'+import { modelTierCatalog } from '@activepieces/server-utils'import { AIProviderModel, CreateAIProviderRequest, PrincipalType, spreadIfDefined, UpdateAIProviderRequest } from '@activepieces/shared'import { FastifyPluginAsyncZod } from 'fastify-type-provider-zod'import { StatusCodes } from 'http-status-codes'@@ -23,6 +24,10 @@ export const aiProviderController: FastifyPluginAsyncZod = async (app) => {configId: request.params.id,})})+ app.get('/tiers', ListModelTiers, async () => ({+ flow: modelTierCatalog.current('flow'),+ chat: modelTierCatalog.current('chat'),+ }))app.get('/:provider/models', ListModels, async (request) => {return aiProviderService(app.log).listModels({platformId: request.principal.platform.id,@@ -84,6 +89,29 @@ const ListModelsForConfig = {},}+const ModelTierList = z.object({+ tiers: z.array(z.object({+ id: z.string(),+ label: z.string(),+ modelId: z.string(),+ })),+ defaultTierId: z.string(),+})++const ListModelTiers = {+ config: {+ security: securityAccess.unscoped([PrincipalType.USER, PrincipalType.ENGINE]),+ },+ schema: {+ response: {+ [StatusCodes.OK]: z.object({+ flow: ModelTierList,+ chat: ModelTierList,+ }),+ },+ },+}+const ListModels = {config: {security: securityAccess.project([PrincipalType.USER, PrincipalType.ENGINE], undefined, { type: ProjectResourceType.QUERY }),diff --git a/packages/server/api/test/integration/ce/ai-provider/model-tiers.test.ts b/packages/server/api/test/integration/ce/ai-provider/model-tiers.test.tsnew file mode 100644index 00000000000..767967b21a1--- /dev/null+++ b/packages/server/api/test/integration/ce/ai-provider/model-tiers.test.ts@@ -0,0 +1,129 @@+import { apId } from '@activepieces/core-utils'+import { ModelTier, modelTierCatalog, ModelTierReader, ModelTierSurface } from '@activepieces/server-utils'+import { PrincipalType } from '@activepieces/shared'+import { FastifyInstance } from 'fastify'+import { StatusCodes } from 'http-status-codes'+import { generateMockToken } from '../../../helpers/auth'+import { createTestContext, TestContext } from '../../../helpers/test-context'+import { setupTestEnvironment, teardownTestEnvironment } from '../../../helpers/test-setup'++let app: FastifyInstance | null = null+let ctx: TestContext++beforeAll(async () => {+ app = await setupTestEnvironment()+})++afterAll(async () => {+ await teardownTestEnvironment()+})++beforeEach(async () => {+ ctx = await createTestContext(app!)+})++afterEach(() => {+ vi.restoreAllMocks()+})++describe('GET /v1/ai-providers/tiers', () => {+ it('serves a flow list and a chat list, each with its default inside it and only id, label and modelId per tier', async () => {+ const response = await ctx.get('/v1/ai-providers/tiers')++ expect(response?.statusCode).toBe(StatusCodes.OK)+ const body = response?.json<ModelTiersResponse>()+ for (const surface of SURFACES) {+ const list = body?.[surface]+ const ids = list?.tiers.map((tier) => tier.id) ?? []+ expect(ids.length).toBeGreaterThan(0)+ expect(new Set(ids).size).toBe(ids.length)+ expect(ids).toContain(list?.defaultTierId)+ for (const tier of list?.tiers ?? []) {+ expect(Object.keys(tier).sort()).toEqual(['id', 'label', 'modelId'])+ }+ }+ })++ it('keeps the flow list and the chat list apart', async () => {+ vi.spyOn(modelTierCatalog, 'current').mockImplementation((surface) => READERS[surface])++ const response = await ctx.get('/v1/ai-providers/tiers')++ expect(response?.statusCode).toBe(StatusCodes.OK)+ const body = response?.json<ModelTiersResponse>()+ expect(body?.flow.tiers.map((tier) => tier.id)).toEqual(['fast', 'smart'])+ expect(body?.chat.tiers.map((tier) => tier.id)).toEqual(['fast', 'smart', 'turbo'])+ expect(body?.flow.defaultTierId).toBe('smart')+ expect(body?.chat.defaultTierId).toBe('turbo')+ expect(body?.flow.tiers.find((tier) => tier.id === 'smart')?.modelId).toBe('anthropic/claude-sonnet-4.6')+ expect(body?.chat.tiers.find((tier) => tier.id === 'smart')?.modelId).toBe('google/gemini-3.7-flash')+ })++ it('lets an engine token read them', async () => {+ const engineToken = await generateMockToken({+ type: PrincipalType.ENGINE,+ id: apId(),+ projectId: ctx.project.id,+ platform: { id: ctx.platform.id },+ })++ const response = await app!.inject({+ method: 'GET',+ url: '/api/v1/ai-providers/tiers',+ headers: { authorization: `Bearer ${engineToken}` },+ })++ expect(response?.statusCode).toBe(StatusCodes.OK)+ expect(response?.json<ModelTiersResponse>().flow.tiers.length).toBeGreaterThan(0)+ })++ it('refuses a request without a token', async () => {+ const response = await app!.inject({+ method: 'GET',+ url: '/api/v1/ai-providers/tiers',+ })++ expect(response?.statusCode).toBe(StatusCodes.FORBIDDEN)+ })+})++function reader({ tiers, defaultTierId }: { tiers: ModelTier[], defaultTierId: string }): ModelTierReader {+ const tiersById = new Map(tiers.map((tier) => [tier.id, tier]))+ const defaultTier = tiers.find((tier) => tier.id === defaultTierId) ?? tiers[0]+ return {+ tiers,+ defaultTierId: defaultTier.id,+ fastTierId: 'fast',+ findTier: ({ tierId }) => tiersById.get(tierId),+ resolveTier: ({ tierId }) => (tierId ? tiersById.get(tierId) : undefined) ?? defaultTier,+ }+}++function tier({ id, label, modelId }: { id: string, label: string, modelId: string }): ModelTier {+ return { id, label, modelId, thinkingBudget: 1024 }+}++const SURFACES: ModelTierSurface[] = ['flow', 'chat']++const READERS: Record<ModelTierSurface, ModelTierReader> = {+ flow: reader({+ tiers: [+ tier({ id: 'fast', label: 'Fast', modelId: 'anthropic/claude-haiku-4.5' }),+ tier({ id: 'smart', label: 'Expert', modelId: 'anthropic/claude-sonnet-4.6' }),+ ],+ defaultTierId: 'smart',+ }),+ chat: reader({+ tiers: [+ tier({ id: 'fast', label: 'Fast', modelId: 'anthropic/claude-haiku-4.5' }),+ tier({ id: 'smart', label: 'Expert', modelId: 'google/gemini-3.7-flash' }),+ tier({ id: 'turbo', label: 'Turbo', modelId: 'openai/gpt-5.5' }),+ ],+ defaultTierId: 'turbo',+ }),+}++type ModelTiersResponse = Record<ModelTierSurface, {+ tiers: { id: string, label: string, modelId: string }[]+ defaultTierId: string+}>diff --git a/packages/web/src/app/routes/chat-with-ai/ai-chat-box.tsx b/packages/web/src/app/routes/chat-with-ai/ai-chat-box.tsxindex fd453f4fe0f..c8367e20421 100644--- a/packages/web/src/app/routes/chat-with-ai/ai-chat-box.tsx+++ b/packages/web/src/app/routes/chat-with-ai/ai-chat-box.tsx@@ -25,6 +25,7 @@ import {useChatStoreContext,} from '@/features/chat/lib/chat-store-context';import { ChatUIMessage, chatPartUtils } from '@/features/chat/lib/chat-types';+import { chatUtils } from '@/features/chat/lib/chat-utils';import { onboardingPrefillUtils } from '@/features/chat/lib/onboarding-prefill';import { useAgentChat } from '@/features/chat/lib/use-chat';import { useCreditsState } from '@/features/chat/lib/use-credits-state';@@ -67,7 +68,11 @@ export function AIChatBox({const { data: chatProvider, isLoading: isLoadingProviders } =aiProviderQueries.useChatProvider();- if (!isLoadingProviders && !chatProvider) {+ if (isLoadingProviders) {+ return <MessageSkeletons />;+ }++ if (!chatProvider) {return <SetupRequiredState />;}@@ -75,7 +80,7 @@ export function AIChatBox({<ChatStoreProvider><ChatBoxContentincognito={incognito}- initialPrompt={isLoadingProviders ? undefined : initialPrompt}+ initialPrompt={initialPrompt}agentId={agentId}builder={builder}onTurnEnd={onTurnEnd}@@ -105,6 +110,7 @@ function ChatBoxContent({}: AIChatBoxProps) {const queryClient = useQueryClient();const credits = useCreditsState();+ const { data: chatProvider } = aiProviderQueries.useChatProvider();const {conversationId,@@ -123,6 +129,10 @@ function ChatBoxContent({} = useAgentChat({...(agentId === undefined ? {} : { agentId }),...(builder === undefined ? {} : { builder }),+ defaultModelName:+ agentId === undefined+ ? chatUtils.newChatModelName({ provider: chatProvider?.provider })+ : null,onTitleUpdate,onConversationCreated,onTurnEnd,diff --git a/packages/web/src/app/routes/chat-with-ai/components/chat-model-selector.tsx b/packages/web/src/app/routes/chat-with-ai/components/chat-model-selector.tsxindex 9e8a80ab8d8..55c8fc2ffc1 100644--- a/packages/web/src/app/routes/chat-with-ai/components/chat-model-selector.tsx+++ b/packages/web/src/app/routes/chat-with-ai/components/chat-model-selector.tsx@@ -1,6 +1,5 @@import { AIProviderName, isNil } from '@activepieces/core-utils';import {- ACTIVEPIECES_CHAT_TIERS,aiProviderUtils,CHAT_CREDITS_PER_TOOL_CALL,} from '@activepieces/shared';@@ -27,12 +26,14 @@ import {import { aiProviderQueries } from '@/features/platform-admin';import { cn } from '@/lib/utils';-const TIER_CONFIG: Record<- string,- {- icon: React.ComponentType<{ className?: string }>;- description: string;- }+const TIER_CONFIG: Partial<+ Record<+ string,+ {+ icon: React.ComponentType<{ className?: string }>;+ description: string;+ }+ >> = {fast: {icon: Equal,@@ -48,24 +49,34 @@ const TIER_CONFIG: Record<},};-function useModelOptions(): ModelOption[] {+function useModelOptions(): {+ options: ModelOption[];+ defaultOptionId: string;+} {const { data: chatProvider } = aiProviderQueries.useChatProvider();+ const chatTiers = aiProviderQueries.useModelTiers('chat');const curatedModels = isNil(chatProvider)? undefined: aiProviderUtils.getCuratedChatModels({ provider: chatProvider.provider });if (isNil(curatedModels)) {- return ACTIVEPIECES_CHAT_TIERS.map((tier) => ({- id: tier.id,- ...TIER_CONFIG[tier.id],- displayLabel: tier.label,- }));+ return {+ options: chatTiers.tiers.map((tier) => ({+ id: tier.id,+ ...(TIER_CONFIG[tier.id] ?? { icon: Sparkles, description: null }),+ displayLabel: tier.label,+ })),+ defaultOptionId: chatTiers.defaultTierId,+ };}- return curatedModels.map((model) => ({- id: model.id,- icon: Sparkles,- displayLabel: model.label,- description: null,- }));+ return {+ options: curatedModels.map((model) => ({+ id: model.id,+ icon: Sparkles,+ displayLabel: model.label,+ description: null,+ })),+ defaultOptionId: curatedModels[0].id,+ };}export function ChatModelSelector({@@ -81,9 +92,11 @@ export function ChatModelSelector({const { data: chatProvider } = aiProviderQueries.useChatProvider();const showCredits = chatProvider?.provider === AIProviderName.ACTIVEPIECES;- const options = useModelOptions();+ const { options, defaultOptionId } = useModelOptions();const selectedOption =- options.find((option) => option.id === selectedModel) ?? options[0];+ options.find((option) => option.id === selectedModel) ??+ options.find((option) => option.id === defaultOptionId) ??+ options[0];const focused =focusedIndex === -1 ? options.indexOf(selectedOption) : focusedIndex;diff --git a/packages/web/src/features/agents/ai-model/hooks.ts b/packages/web/src/features/agents/ai-model/hooks.tsindex 5645a1147e4..6b5887571b5 100644--- a/packages/web/src/features/agents/ai-model/hooks.ts+++ b/packages/web/src/features/agents/ai-model/hooks.ts@@ -1,24 +1,34 @@import { AIProviderName, isNil } from '@activepieces/core-utils';import {- ACTIVEPIECES_CHAT_TIERS,AIProviderModel,ALLOWED_CHAT_MODELS_BY_PROVIDER,} from '@activepieces/shared';import { useQuery } from '@tanstack/react-query';+import { useCallback } from 'react';-import { aiProviderApi } from '@/features/platform-admin/api/ai-provider-api';+import {+ aiProviderApi,+ ModelTier,+} from '@/features/platform-admin/api/ai-provider-api';+import { aiProviderQueries } from '@/features/platform-admin/hooks/ai-provider-hooks';import { authenticationSession } from '@/lib/authentication-session';type AIModelType = 'text' | 'image';-function getAllowedModelsForProvider(- provider: AIProviderName,- allModels: AIProviderModel[],- modelType: AIModelType,-): AIProviderModel[] {+function getAllowedModelsForProvider({+ provider,+ allModels,+ modelType,+ flowTiers,+}: {+ provider: AIProviderName;+ allModels: AIProviderModel[];+ modelType: AIModelType;+ flowTiers: ModelTier[];+}): AIProviderModel[] {const allowedIds =provider === AIProviderName.ACTIVEPIECES- ? ACTIVEPIECES_CHAT_TIERS.map((tier) => tier.modelId)+ ? flowTiers.map((tier) => tier.modelId): ALLOWED_CHAT_MODELS_BY_PROVIDER[provider];return allModels@@ -40,14 +50,23 @@ function getAllowedModelsForProvider(}).map((model) =>provider === AIProviderName.ACTIVEPIECES- ? { ...model, name: managedTierLabel(model.id) ?? model.name }+ ? {+ ...model,+ name:+ managedTierLabel({ modelId: model.id, flowTiers }) ?? model.name,+ }: model,);}-function managedTierLabel(modelId: string): string | undefined {- return ACTIVEPIECES_CHAT_TIERS.find((tier) => tier.modelId === modelId)- ?.label;+function managedTierLabel({+ modelId,+ flowTiers,+}: {+ modelId: string;+ flowTiers: ModelTier[];+}): string | undefined {+ return flowTiers.find((tier) => tier.modelId === modelId)?.label;}export const aiModelHooks = {@@ -63,20 +82,27 @@ export const aiModelHooks = {useGetModelsForProvider: (provider?: AIProviderName, configId?: string) => {const projectId = authenticationSession.getProjectId();+ const { tiers: flowTiers } = aiProviderQueries.useModelTiers('flow');+ const select = useCallback(+ (allModels: AIProviderModel[]) =>+ isNil(provider)+ ? []+ : getAllowedModelsForProvider({+ provider,+ allModels,+ modelType: 'text',+ flowTiers,+ }),+ [provider, flowTiers],+ );return useQuery({queryKey: ['ai-models', provider, configId, projectId],enabled: !isNil(provider) && !isNil(projectId),- queryFn: async () => {- if (isNil(provider) || isNil(projectId)) return [];-- const allModels = await aiProviderApi.listModelsForProvider(- provider,- projectId,- configId,- );-- return getAllowedModelsForProvider(provider, allModels, 'text');- },+ queryFn: () =>+ isNil(provider) || isNil(projectId)+ ? []+ : aiProviderApi.listModelsForProvider(provider, projectId, configId),+ select,});},};diff --git a/packages/web/src/features/chat/lib/chat-utils.ts b/packages/web/src/features/chat/lib/chat-utils.tsindex f709da74ded..8532d7ca086 100644--- a/packages/web/src/features/chat/lib/chat-utils.ts+++ b/packages/web/src/features/chat/lib/chat-utils.ts@@ -1,5 +1,6 @@-import { isObject } from '@activepieces/core-utils';+import { AIProviderName, isObject } from '@activepieces/core-utils';import {+ aiProviderUtils,ActionReceiptEvent,BuildPlanEvent,BuildPlanStep,@@ -577,8 +578,20 @@ function reopensSameConversation({return current === next;}+function newChatModelName({+ provider,+}: {+ provider: AIProviderName | undefined;+}): string | null {+ if (provider === undefined) {+ return null;+ }+ return aiProviderUtils.getCuratedChatModels({ provider })?.[0]?.id ?? null;+}+export const chatUtils = {newChatEvent: 'ap:new-chat',+ newChatModelName,reopensSameConversation,sanitizeTitle,formatToolLabel: ({ part }: { part: AnyToolPart }) =>diff --git a/packages/web/src/features/chat/lib/use-chat.ts b/packages/web/src/features/chat/lib/use-chat.tsindex fd3a298dbc4..c9da2c785c0 100644--- a/packages/web/src/features/chat/lib/use-chat.ts+++ b/packages/web/src/features/chat/lib/use-chat.ts@@ -9,7 +9,6 @@ import {AgentConversationStatus,AgentHistoryMessage,CHAT_ALLOWED_MIME_TYPES,- DEFAULT_CHAT_TIER_ID,PersistedAgentMessage,ToolProgressEvent,AgentMessageSource,@@ -265,6 +264,7 @@ type SendStatus =export function useAgentChat({agentId,builder,+ defaultModelName,onTitleUpdate,onConversationCreated,onCreditsExhausted,@@ -272,6 +272,7 @@ export function useAgentChat({}: {agentId?: string;builder?: boolean;+ defaultModelName?: string | null;onTitleUpdate?: (title: string) => void;onConversationCreated?: (conversationId: string) => void;onCreditsExhausted?: () => void;@@ -282,9 +283,7 @@ export function useAgentChat({const [conversationId, setConversationIdState] = useState<string | null>(null,);- const [modelName, setModelNameState] = useState<string | null>(- DEFAULT_CHAT_TIER_ID,- );+ const [modelName, setModelNameState] = useState<string | null>(null);const [isLoadingHistory, setIsLoadingHistory] = useState(false);const [isPollingForAgentReply, setIsPollingForAgentReply] = useState(false);const [sendStatus, setSendStatus] = useState<SendStatus>({ type: 'idle' });@@ -307,7 +306,9 @@ export function useAgentChat({>(undefined);const lastSentFileNamesRef = useRef<string[]>([]);const conversationIdRef = useRef<string | null>(null);- const modelNameRef = useRef<string | null>(DEFAULT_CHAT_TIER_ID);+ const modelNameRef = useRef<string | null>(null);+ const defaultModelNameRef = useRef(defaultModelName ?? null);+ defaultModelNameRef.current = defaultModelName ?? null;const onTitleUpdateRef = useRef(onTitleUpdate);onTitleUpdateRef.current = onTitleUpdate;const onConversationCreatedRef = useRef(onConversationCreated);@@ -699,7 +700,7 @@ export function useAgentChat({const { error: convError } = await tryCatch(async () => {const conv = await createConversation({title: content.slice(0, 100),- modelName: modelNameRef.current,+ modelName: modelNameRef.current ?? defaultModelNameRef.current,});onConversationCreatedRef.current?.(conv.id);});diff --git a/packages/web/src/features/platform-admin/api/ai-provider-api.ts b/packages/web/src/features/platform-admin/api/ai-provider-api.tsindex cabf32fd546..32186107144 100644--- a/packages/web/src/features/platform-admin/api/ai-provider-api.ts+++ b/packages/web/src/features/platform-admin/api/ai-provider-api.ts@@ -33,6 +33,9 @@ export const aiProviderApi = {`/v1/ai-providers/configs/${configId}/models`,);},+ listModelTiers() {+ return api.get<ModelTiersResponse>('/v1/ai-providers/tiers');+ },upsert(request: CreateAIProviderRequest) {return api.post<AIProviderWithoutSensitiveData>('/v1/ai-providers',@@ -52,3 +55,18 @@ export const aiProviderApi = {return api.delete(`/v1/ai-providers/${providerId}`);},};++export type ModelTierSurface = 'chat' | 'flow';++export type ModelTier = {+ id: string;+ label: string;+ modelId: string;+};++export type ModelTierList = {+ tiers: ModelTier[];+ defaultTierId: string;+};++export type ModelTiersResponse = Record<ModelTierSurface, ModelTierList>;diff --git a/packages/web/src/features/platform-admin/hooks/ai-provider-hooks.ts b/packages/web/src/features/platform-admin/hooks/ai-provider-hooks.tsindex 53beba242d7..d0d61503754 100644--- a/packages/web/src/features/platform-admin/hooks/ai-provider-hooks.ts+++ b/packages/web/src/features/platform-admin/hooks/ai-provider-hooks.ts@@ -1,20 +1,28 @@import { isNil } from '@activepieces/core-utils';import {+ ACTIVEPIECES_CHAT_TIERS,AIProviderAuthConfig,AiProviderKeyStatus,AIProviderWithoutSensitiveData,CreateAIProviderRequest,+ DEFAULT_CHAT_TIER_ID,UpdateAIProviderRequest,} from '@activepieces/shared';-import { useMutation, useQuery } from '@tanstack/react-query';+import { queryOptions, useMutation, useQuery } from '@tanstack/react-query';import { AxiosError } from 'axios';import { authenticationSession } from '@/lib/authentication-session';-import { aiProviderApi } from '../api/ai-provider-api';+import {+ aiProviderApi,+ ModelTierList,+ ModelTiersResponse,+ ModelTierSurface,+} from '../api/ai-provider-api';export const aiProviderKeys = {configs: ['ai-provider-configs'] as const,+ modelTiers: ['ai-provider-model-tiers'] as const,forProject: (projectId: string | null) =>['ai-providers', projectId] as const,configModels: (configId?: string) =>@@ -43,6 +51,17 @@ export const aiProviderQueries = {aiProviderQueries.useProjectAiProviders();return { ...rest, data: providers?.find((p) => p.enabledForChat) };},+ modelTiersOptions: () =>+ queryOptions({+ queryKey: aiProviderKeys.modelTiers,+ queryFn: () => aiProviderApi.listModelTiers(),+ staleTime: MODEL_TIERS_STALE_MS,+ refetchInterval: MODEL_TIERS_STALE_MS,+ }),+ useModelTiers: (surface: ModelTierSurface): ModelTierList => {+ const { data } = useQuery(aiProviderQueries.modelTiersOptions());+ return (data ?? BUNDLED_MODEL_TIERS)[surface];+ },};export const aiProviderMutations = {@@ -133,6 +152,22 @@ export const hasAnyAuthFieldFilled = ();};+const BUNDLED_LIST: ModelTierList = {+ tiers: ACTIVEPIECES_CHAT_TIERS.map(({ id, label, modelId }) => ({+ id,+ label,+ modelId,+ })),+ defaultTierId: DEFAULT_CHAT_TIER_ID,+};++const BUNDLED_MODEL_TIERS: ModelTiersResponse = {+ flow: BUNDLED_LIST,+ chat: BUNDLED_LIST,+};++const MODEL_TIERS_STALE_MS = 15 * 60 * 1000;+type UpsertAiProviderOptions = {providerId?: string;onSuccess: (created?: AIProviderWithoutSensitiveData) => void;diff --git a/packages/web/test/app/routes/chat-with-ai/components/chat-model-selector.test.tsx b/packages/web/test/app/routes/chat-with-ai/components/chat-model-selector.test.tsxnew file mode 100644index 00000000000..e4f8db0f990--- /dev/null+++ b/packages/web/test/app/routes/chat-with-ai/components/chat-model-selector.test.tsx@@ -0,0 +1,76 @@+// @vitest-environment jsdom+import { AIProviderName } from '@activepieces/core-utils';+import { aiProviderUtils } from '@activepieces/shared';+import { render } from '@testing-library/react';+import { beforeEach, describe, expect, it, vi } from 'vitest';++vi.mock('i18next', () => ({ t: (key: string) => key }));++const state = vi.hoisted(() => ({+ provider: '',+ useModelTiers: vi.fn(),+}));++vi.mock('@/features/platform-admin', () => ({+ aiProviderQueries: {+ useChatProvider: () => ({ data: { provider: state.provider } }),+ useModelTiers: (surface: string) => state.useModelTiers(surface),+ },+}));++import { ChatModelSelector } from '@/app/routes/chat-with-ai/components/chat-model-selector';++describe('ChatModelSelector', () => {+ beforeEach(() => {+ state.provider = AIProviderName.ACTIVEPIECES;+ state.useModelTiers.mockReset();+ state.useModelTiers.mockReturnValue(CHAT_TIERS);+ });++ it('reads the chat tiers and shows the published default when nothing is picked', () => {+ render(<ChatModelSelector selectedModel={null} onModelChange={noop} />);++ expect(state.useModelTiers).toHaveBeenCalledWith('chat');+ expect(triggerText()).toBe('Turbo');+ });++ it('shows the picked tier by its published label', () => {+ render(<ChatModelSelector selectedModel="smart" onModelChange={noop} />);++ expect(triggerText()).toBe('Expert');+ });++ it('falls back to the published default for a tier that is no longer listed', () => {+ render(<ChatModelSelector selectedModel="gone" onModelChange={noop} />);++ expect(triggerText()).toBe('Turbo');+ });++ it('shows the first curated model on an own-key provider with a curated list', () => {+ state.provider = AIProviderName.OPENAI;+ const curated = aiProviderUtils.getCuratedChatModels({+ provider: AIProviderName.OPENAI,+ });++ render(<ChatModelSelector selectedModel={null} onModelChange={noop} />);++ expect(triggerText()).toBe(curated?.[0].label);+ });+});++function triggerText(): string | undefined {+ return document.querySelector('button[role="combobox"]')?.textContent?.trim();+}++function noop(): void {+ return undefined;+}++const CHAT_TIERS = {+ tiers: [+ { id: 'fast', label: 'Fast', modelId: 'anthropic/claude-haiku-4.5' },+ { id: 'smart', label: 'Expert', modelId: 'google/gemini-3.7-flash' },+ { id: 'turbo', label: 'Turbo', modelId: 'openai/gpt-5.5' },+ ],+ defaultTierId: 'turbo',+};diff --git a/packages/web/test/features/agents/ai-model/hooks.test.tsx b/packages/web/test/features/agents/ai-model/hooks.test.tsxnew file mode 100644index 00000000000..239c2fd2b67--- /dev/null+++ b/packages/web/test/features/agents/ai-model/hooks.test.tsx@@ -0,0 +1,163 @@+// @vitest-environment jsdom+import { AIProviderName } from '@activepieces/core-utils';+import {+ AIProviderModel,+ AIProviderModelType,+ ALLOWED_CHAT_MODELS_BY_PROVIDER,+} from '@activepieces/shared';+import { QueryClient, QueryClientProvider } from '@tanstack/react-query';+import { renderHook, waitFor } from '@testing-library/react';+import { ReactNode } from 'react';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++vi.mock('@/lib/authentication-session', () => ({+ authenticationSession: { getProjectId: () => 'project-1' },+}));++const listModelTiers = vi.fn();+const listModelsForProvider = vi.fn();+vi.mock('@/features/platform-admin/api/ai-provider-api', () => ({+ aiProviderApi: {+ listModelTiers: () => listModelTiers(),+ listModelsForProvider: (provider: string) =>+ listModelsForProvider(provider),+ },+}));++import { aiModelHooks } from '@/features/agents/ai-model/hooks';+import type { ModelTiersResponse } from '@/features/platform-admin/api/ai-provider-api';++describe('aiModelHooks.useGetModelsForProvider', () => {+ let queryClient: QueryClient;++ beforeEach(() => {+ listModelTiers.mockReset();+ listModelsForProvider.mockReset();+ queryClient = new QueryClient({+ defaultOptions: { queries: { retry: false } },+ });+ });++ afterEach(() => {+ queryClient.clear();+ });++ it('offers the published flow tiers, labelled by tier, and never a chat-only model', async () => {+ listModelTiers.mockResolvedValue(PUBLISHED);+ listModelsForProvider.mockResolvedValue(MANAGED_MODELS);++ const { result } = renderModels(AIProviderName.ACTIVEPIECES);++ await waitFor(() =>+ expect(result.current.data?.map((model) => model.id)).toEqual([+ 'anthropic/claude-haiku-4.5',+ 'anthropic/claude-sonnet-5',+ ]),+ );+ expect(result.current.data?.map((model) => model.name)).toEqual([+ 'Fast',+ 'Expert',+ ]);+ });++ it('falls back to the bundled tiers when the tiers request fails', async () => {+ listModelTiers.mockRejectedValue(new Error('tiers endpoint down'));+ listModelsForProvider.mockResolvedValue(MANAGED_MODELS);++ const { result } = renderModels(AIProviderName.ACTIVEPIECES);++ await waitFor(() =>+ expect(result.current.data?.map((model) => model.id)).toEqual([+ 'anthropic/claude-haiku-4.5',+ 'anthropic/claude-sonnet-4.6',+ ]),+ );+ expect(result.current.data?.map((model) => model.name)).toEqual([+ 'Fast',+ 'Expert',+ ]);+ });++ it('keeps the same list object across renders once resolved', async () => {+ listModelTiers.mockResolvedValue(PUBLISHED);+ listModelsForProvider.mockResolvedValue(MANAGED_MODELS);++ const { result, rerender } = renderModels(AIProviderName.ACTIVEPIECES);+ await waitFor(() => expect(result.current.data).toHaveLength(2));+ const resolved = result.current.data;++ rerender();++ expect(result.current.data).toBe(resolved);+ });++ it('leaves a curated own-key provider on its curated list, unlabelled', async () => {+ const curatedId =+ ALLOWED_CHAT_MODELS_BY_PROVIDER[AIProviderName.OPENAI]![0];+ listModelTiers.mockResolvedValue(PUBLISHED);+ listModelsForProvider.mockResolvedValue([+ model({ id: curatedId, name: 'Curated model' }),+ model({ id: 'not-curated', name: 'Not curated' }),+ ]);++ const { result } = renderModels(AIProviderName.OPENAI);++ await waitFor(() =>+ expect(result.current.data?.map((model) => model.name)).toEqual([+ 'Curated model',+ ]),+ );+ });++ function renderModels(provider: AIProviderName) {+ const wrapper = ({ children }: { children: ReactNode }) => (+ <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>+ );+ return renderHook(() => aiModelHooks.useGetModelsForProvider(provider), {+ wrapper,+ });+ }+});++function model({+ id,+ name,+ type = AIProviderModelType.TEXT,+}: {+ id: string;+ name: string;+ type?: AIProviderModelType;+}): AIProviderModel {+ return { id, name, type };+}++const MANAGED_MODELS: AIProviderModel[] = [+ model({ id: 'anthropic/claude-sonnet-4.6', name: 'Claude Sonnet 4.6' }),+ model({ id: 'anthropic/claude-sonnet-5', name: 'Claude Sonnet 5' }),+ model({ id: 'google/gemini-3.7-flash', name: 'Gemini 3.7 Flash' }),+ model({ id: 'anthropic/claude-haiku-4.5', name: 'Claude Haiku 4.5' }),+ model({+ id: 'google/gemini-3.1-flash-image',+ name: 'Gemini Image',+ type: AIProviderModelType.IMAGE,+ }),+];++const PUBLISHED: ModelTiersResponse = {+ flow: {+ tiers: [+ { id: 'fast', label: 'Fast', modelId: 'anthropic/claude-haiku-4.5' },+ { id: 'smart', label: 'Expert', modelId: 'anthropic/claude-sonnet-5' },+ { id: 'premium', label: 'Heavy', modelId: 'anthropic/claude-opus-4.8' },+ ],+ defaultTierId: 'smart',+ },+ chat: {+ tiers: [+ { id: 'fast', label: 'Fast', modelId: 'anthropic/claude-haiku-4.5' },+ { id: 'smart', label: 'Expert', modelId: 'google/gemini-3.7-flash' },+ { id: 'turbo', label: 'Turbo', modelId: 'openai/gpt-5.5' },+ ],+ defaultTierId: 'turbo',+ },+};diff --git a/packages/web/test/features/chat/lib/new-chat-model-name.test.ts b/packages/web/test/features/chat/lib/new-chat-model-name.test.tsnew file mode 100644index 00000000000..e25e4862855--- /dev/null+++ b/packages/web/test/features/chat/lib/new-chat-model-name.test.ts@@ -0,0 +1,28 @@+import { AIProviderName } from '@activepieces/core-utils';+import { aiProviderUtils } from '@activepieces/shared';+import { describe, expect, it } from 'vitest';++import { chatUtils } from '@/features/chat/lib/chat-utils';++describe('chatUtils.newChatModelName', () => {+ it('sends no model on a tier picker, so the published chat default decides', () => {+ expect(+ chatUtils.newChatModelName({ provider: AIProviderName.ACTIVEPIECES }),+ ).toBeNull();+ expect(+ chatUtils.newChatModelName({ provider: AIProviderName.BEDROCK }),+ ).toBeNull();+ expect(chatUtils.newChatModelName({ provider: undefined })).toBeNull();+ });++ it('sends the model a curated own-key picker shows, so the turn cannot run the published default instead', () => {+ const shown = aiProviderUtils.getCuratedChatModels({+ provider: AIProviderName.ANTHROPIC,+ })?.[0]?.id;++ expect(shown).toBeDefined();+ expect(+ chatUtils.newChatModelName({ provider: AIProviderName.ANTHROPIC }),+ ).toBe(shown);+ });+});diff --git a/packages/web/test/features/platform-admin/ai-provider-hooks.test.tsx b/packages/web/test/features/platform-admin/ai-provider-hooks.test.tsxnew file mode 100644index 00000000000..c5d36c6c694--- /dev/null+++ b/packages/web/test/features/platform-admin/ai-provider-hooks.test.tsx@@ -0,0 +1,143 @@+// @vitest-environment jsdom+import {+ ACTIVEPIECES_CHAT_TIERS,+ DEFAULT_CHAT_TIER_ID,+} from '@activepieces/shared';+import { QueryClient, QueryClientProvider } from '@tanstack/react-query';+import { renderHook, waitFor } from '@testing-library/react';+import { ReactNode } from 'react';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++vi.mock('@/lib/authentication-session', () => ({+ authenticationSession: { getProjectId: () => 'project-1' },+}));++const listModelTiers = vi.fn();+vi.mock('@/features/platform-admin/api/ai-provider-api', () => ({+ aiProviderApi: { listModelTiers: () => listModelTiers() },+}));++import type {+ ModelTierList,+ ModelTiersResponse,+ ModelTierSurface,+} from '@/features/platform-admin/api/ai-provider-api';+import {+ aiProviderKeys,+ aiProviderQueries,+} from '@/features/platform-admin/hooks/ai-provider-hooks';++describe('aiProviderQueries.useModelTiers', () => {+ let queryClient: QueryClient;++ beforeEach(() => {+ listModelTiers.mockReset();+ queryClient = new QueryClient({+ defaultOptions: { queries: { retry: false } },+ });+ });++ afterEach(() => {+ queryClient.clear();+ });++ it('refetches on the same cadence as the server, so an open page does not keep stale tiers', () => {+ const options = aiProviderQueries.modelTiersOptions();++ expect(options.staleTime).toBe(FIFTEEN_MINUTES_MS);+ expect(options.refetchInterval).toBe(FIFTEEN_MINUTES_MS);+ });++ it('serves the chat list for chat and the flow list for flow from one request', async () => {+ listModelTiers.mockResolvedValue(PUBLISHED);++ const { result: chat } = renderTiers('chat');+ await waitFor(() => expect(chat.current.defaultTierId).toBe('turbo'));+ expect(chat.current.tiers.map((tier) => tier.id)).toEqual([+ 'fast',+ 'smart',+ 'turbo',+ ]);+ expect(modelIdOf(chat.current, 'smart')).toBe('google/gemini-3.7-flash');++ const { result: flow } = renderTiers('flow');+ await waitFor(() =>+ expect(modelIdOf(flow.current, 'smart')).toBe(+ 'anthropic/claude-sonnet-5',+ ),+ );+ expect(flow.current.tiers.map((tier) => tier.id)).toEqual([+ 'fast',+ 'smart',+ 'premium',+ ]);+ expect(flow.current.defaultTierId).toBe('smart');+ expect(listModelTiers).toHaveBeenCalledTimes(1);+ });++ it('serves the bundled list before the response lands', () => {+ listModelTiers.mockReturnValue(new Promise(() => undefined));++ const { result } = renderTiers('chat');++ expect(result.current).toEqual(BUNDLED);+ });++ it('serves the bundled list on both surfaces when the request fails', async () => {+ listModelTiers.mockRejectedValue(new Error('tiers endpoint down'));++ const { result: chat } = renderTiers('chat');+ const { result: flow } = renderTiers('flow');+ await waitFor(() =>+ expect(queryClient.getQueryState(aiProviderKeys.modelTiers)?.status).toBe(+ 'error',+ ),+ );++ expect(chat.current).toEqual(BUNDLED);+ expect(flow.current).toEqual(BUNDLED);+ });++ function renderTiers(surface: ModelTierSurface) {+ const wrapper = ({ children }: { children: ReactNode }) => (+ <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>+ );+ return renderHook(() => aiProviderQueries.useModelTiers(surface), {+ wrapper,+ });+ }+});++function modelIdOf(list: ModelTierList, tierId: string): string | undefined {+ return list.tiers.find((tier) => tier.id === tierId)?.modelId;+}++const PUBLISHED: ModelTiersResponse = {+ flow: {+ tiers: [+ { id: 'fast', label: 'Fast', modelId: 'anthropic/claude-haiku-4.5' },+ { id: 'smart', label: 'Expert', modelId: 'anthropic/claude-sonnet-5' },+ { id: 'premium', label: 'Heavy', modelId: 'anthropic/claude-opus-4.8' },+ ],+ defaultTierId: 'smart',+ },+ chat: {+ tiers: [+ { id: 'fast', label: 'Fast', modelId: 'anthropic/claude-haiku-4.5' },+ { id: 'smart', label: 'Expert', modelId: 'google/gemini-3.7-flash' },+ { id: 'turbo', label: 'Turbo', modelId: 'openai/gpt-5.5' },+ ],+ defaultTierId: 'turbo',+ },+};++const FIFTEEN_MINUTES_MS = 15 * 60 * 1000;++const BUNDLED: ModelTierList = {+ tiers: ACTIVEPIECES_CHAT_TIERS.map(({ id, label, modelId }) => ({+ id,+ label,+ modelId,+ })),+ defaultTierId: DEFAULT_CHAT_TIER_ID,+};