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(web): admin tiers tab in ai center (#16116)
Louai Boumediene3d53686b231f
diff --git a/brain/knowledge/ai-intelligence/ai-providers.md b/brain/knowledge/ai-intelligence/ai-providers.mdindex 3b7f4b27265..1143171f533 100644--- a/brain/knowledge/ai-intelligence/ai-providers.md+++ b/brain/knowledge/ai-intelligence/ai-providers.md@@ -72,10 +72,15 @@ after a file without chat keys it seeds the chat list from the live flow list.**Platform tier** — an admin-built, platform-owned list of up to 5 `{configId, modelId}` entries (main + ranked fallbacks, any keys), table `platform_model_tier`, API `/v1/platform-model-tiers`. A tier follows key scope: in a project it runs only the entries whose key serves that project, and it is listed there only when its main entry can run; see decision [000048](../decisions/000048-platform-tiers-follow-key-project-scope.md) (replaces 000044's grant). _Avoid_: "model tier" alone — that is the console **credits tier** (`ModelTier`, above).+**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+- **The admin Tiers page has no sidebar or search entry until the feature launches.** The route `/platform/ai/tiers` works, but the platform sidebar and the global search `STATIC_PAGES` deliberately leave it out, so only someone with the URL finds it. To launch, add `{ to: '/platform/ai/tiers', label: t('Tiers') }` to the AI subItems in `sidebar/platform/index.tsx` and add a `page-platform-ai-tiers` entry to `static-pages.ts`.+- **The admin Tiers tab applies a key's `modelScope` itself.** `GET /v1/ai-providers/configs/:id/models` returns the unfiltered list on purpose (admins pick the allow-list from it), so the tier pickers and the Specific models list narrow it client-side in `modelMeta.scopedTextModels` (`packages/web/src/features/agents/ai-model/`). The server still refuses an out-of-scope entry on save (`assertEntriesValid`), so the client filter is convenience, not the guard.- **A key a live platform tier uses cannot be deleted, and its `modelScope` cannot drop a tier's model.** `aiProviderService.delete` and `update` call `platformModelTierService.assertKeyCanBeDeleted` / `assertKeyScopeKeepsTiers` under a `platform` row `FOR NO KEY UPDATE` lock and answer 409 with the tier names. The same lock serialises every tier write, so a tier save can't race a key delete into a dangling entry. The same update also refuses removing a typed-in model (Vertex, OpenAI-compatible, Cloudflare `config.models`) that a tier uses, even when `modelScope` is `all`, and limiting the projects of a key that a capability (web search, scraping, image) uses, or dropping the capability's `modelId` from its model list: capability keys must serve every project and keep their model (`assertCapabilitiesKeepKey`).- **A key's model list is enforced when a specific model runs, not only in pickers.** `/v1/ai/execute` checks `aiProviderService.assertModelAllowed` before queueing (the managed Activepieces key is skipped: it allows every model), and the worker's `resolveAiProvider` RPC sends `modelId` so the server checks it again against the key it resolves at run time (a model removed while the job waited fails then). `resolveNamedModelId` refuses any own-key model outside the list with no tier-id exemption: an own key never translates `smart`/`fast`, so a name that looks like a tier is just a customer model id. At platform scope, `rankRows` prefers keys open to every project; at project scope it still prefers the most specific key. Key edits and capability choices both take the `platform` row lock, and a capability save re-reads its key under that lock, so the two cannot interleave. One helper, `aiKeyScope.keyOffersModel`, decides whether a key still offers a model: its model scope, plus its typed-in `config.models` list when it has one. The tier and capability guards on a key edit both use it, and so does the locked re-check when a capability is saved. A key edit that removes a typed-in model a capability uses is refused like a model-scope change.-- **Deleting a platform tier needs a replacement, except the last live tier, and there is no usage count.** Refs to a deleted tier are never rewritten; they reach a live tier through `replacedBy` (flattened to one hop on delete). The last tier may always go with no replacement: without that exit, a platform's last tier and the key it uses could never be removed, because the key guard refuses keys a live tier uses. Tier delete is also deliberately **not** plan-gated (writes are), so a platform that downgrades can still clear its tiers and then remove its key. Its refs then point at a tier with `replacedBy: null`, and the resolver must fail them with a clear "tier removed" error. An exact "used by N flows" count needs a `LIKE` scan over every flow version's `trigger` text, plus agent jsonb and conversations, on the whole platform. We dropped it rather than pay that. If counts come back, add a denormalized `flow_version.modelTierIds` array + GIN index (the `connectionIds` pattern) instead of scanning.+- **Deleting a platform tier needs a replacement, except the last live tier, and there is no usage count.** Refs to a deleted tier are never rewritten; they reach a live tier through `replacedBy` (flattened to one hop on delete). The last tier may always go with no replacement: without that exit, a platform's last tier and the key it uses could never be removed, because the key guard refuses keys a live tier uses. Tier delete is also deliberately **not** plan-gated (writes are), so a platform that downgrades can still clear its tiers and then remove its key. Its refs then point at a tier with `replacedBy: null`, and the resolver must fail them with a clear "tier removed" error. An exact "used by N flows" count needs a `LIKE` scan over every flow version's `trigger` text, plus agent jsonb and conversations, on the whole platform. We dropped it rather than pay that. If counts come back, add a denormalized `flow_version.modelTierIds` array + GIN index (the `connectionIds` pattern) instead of scanning. Deleting the last live tier also sets `platform_configuration.aiSpecificModelsVisible = true` in the same transaction, so "hidden needs at least one tier" holds without the client flipping it first (a client-side flip left models visible when the delete then failed, #16116). The other direction holds too: `platformConfigurationService.update` saves every field in one transaction that first calls `platformModelTierService.assertSpecificModelsVisibilityAllowed` (same `platform` row lock, refuses a hide when no live tier is left), so a refused hide changes no other setting either, so a concurrent last-tier delete and hide cannot leave builders with nothing. `PlatformConfigurationSettings` (the infra Configurations form) does **not** carry the flag; only `UpdatePlatformConfigurationRequestBody` does, so saving that form never rewrites it.+- **The Tiers tab re-implements the server's project-scope rule on the client.** `modelMeta.keyServesProject` / `entryRunsIn` mirror `rowAllowsScope` + `scopeAllows`, and power the per-tier "Not available in N projects" lines, the *Preview as project* select and the confirm dialog when a key's projects are narrowed (`modelMeta.keyScopeImpact`). Change one side and change the other, or the admin sees a tier as available where the server refuses it.- **The live-name unique index is `lower("name")`, which `EntitySchema` cannot express.** The entity declares it `synchronize: false` (so `check-migrations` skips it), which means test DBs built by `synchronize` don't have it. The case-insensitive check that tests rely on is the service's `assertNameIsFree`, which is race-free because it runs under the `platform` row lock; the index is the backstop.- **`lockPlatform` takes `FOR NO KEY UPDATE`, not `FOR UPDATE`.** Every FK insert that references `platform` (projects, users, …) takes `FOR KEY SHARE` on the platform row, which `FOR UPDATE` blocks until commit. `FOR NO KEY UPDATE` serialises tier writes just the same without stalling those inserts.- **Tier entries go through the same project check as specific picks.** `platformModelTierService.getForRun` takes a `ProviderScope` and applies `aiKeyScope.rowAllowsScope` + `scopeAllows` to every entry; if the stored main entry fails, the whole tier is unavailable (it is checked before the healthy-first sort, so a down main doesn't change the answer). The `configId`s still come from a live tier on the caller's platform, never from the caller. Only the label lookups (`getManyForRun`, `firstCandidates`) use platform scope, because they return no credentials.diff --git a/brain/knowledge/design-system/index.md b/brain/knowledge/design-system/index.mdindex c21913102fb..b32c9e1c823 100644--- a/brain/knowledge/design-system/index.md+++ b/brain/knowledge/design-system/index.md@@ -34,7 +34,11 @@ rather than "what does this mean?" — projects, avatars, chart series. Not a scthemes, because we cannot recolour someone else's artwork. `<LogoPlate>` renders one.## Gotchas+- **A Radix `Popover` opened from inside a `Dialog` must be `modal`, or the mouse wheel does nothing in it.** The dialog's scroll lock treats the portaled popover as "outside" and cancels wheel events there; `<Popover modal>` installs its own lock that allows scrolling inside the content. `SearchableSelect` and `ModelPickerPopover` do this; a non-modal popover looks fine until it is used in a dialog.+- **`CommandList` (`components/ui/command.tsx`) defaults to `overflow-y-hidden`, so a cmdk list silently cannot scroll.** Every caller must add `overflow-y-auto` (or nest a `ScrollArea`); passing only a `max-h-*` looks fine with few rows and clips the rest. Bit the tiers model picker (2026-10-05).+- **`border-dashed` on anything under ~16px renders as a broken squiggle, not a dashed circle.** Browsers cannot fit whole dashes around an 8px `rounded-full`, so a tiny dashed dot reads as a glitch. Use a solid border in a lighter step (`border-gray-7`) for a "placeholder" dot and keep dashed borders for boxes.+- **`ConfirmationDeleteDialog` renders `message` inside `DialogDescription`, a `<p>`.** A select or any other form control placed in the message is invalid nesting and becomes the dialog's `aria-describedby`. A delete flow that needs an input (the tier delete's "Move them to" select) builds its own `Dialog` and keeps only the confirm button's `adminControl` id.- `styles.css` points the `--shadow-*` theme keys that change per theme at a plain custom property(`--shadow-edge: var(--edge)`). Tailwind copies a `--shadow-*` value into the utility literally, so a dark override ofthe theme key itself never lands; the indirection is what lets shadows change per theme.diff --git a/brain/knowledge/engineering/ci-pr-review-hygiene.md b/brain/knowledge/engineering/ci-pr-review-hygiene.mdindex 5dafd61710b..6a8d0f2a4e6 100644--- a/brain/knowledge/engineering/ci-pr-review-hygiene.md+++ b/brain/knowledge/engineering/ci-pr-review-hygiene.md@@ -22,6 +22,7 @@ Which team gets asked to review comes entirely from `.github/CODEOWNERS` — theEnforcement is the **`Codeowners review` repository ruleset** (active on the default branch), not classic branch protection: `require_code_owner_review: true` plus `required_approving_review_count: 1` and `required_review_thread_resolution: true`. Eight bypass actors are configured, which is why an owner-team request can look non-blocking on some PRs.## Gotchas+- **In a PR stack, resolve a shared conflict once with `git config rerere.enabled true`.** If a child branch merges a sibling PR that touches the same file as its parent, git records how you fixed the conflict. When that sibling reaches `main` and the parent merges `main`, git replays the fix ("Staged … using previous resolution"). Still read the staged result before committing.- **A clean merge of main can silently cancel your package version bump.** If main bumped the same package to the same version your branch did (seen 2026-10-08: both at core-execution `0.26.2`), `package.json` merges with no conflict. Your change then ships under a version main already published. After every main merge, compare each package your branch touches against `git show origin/main:<path>/package.json` and bump again if they match, then fix its `bun.lock` line.- **When merging main, `rerere` can replay a stale `bun.lock` resolution.** "Staged 'bun.lock' using previous resolution" means git reused an old answer, which can carry workspace versions that no longer match any `package.json` (seen: core-utils `0.8.6` in the lockfile against `0.8.5` in the manifest). If the branch adds no dependencies, take main's `bun.lock` and edit only the branch's own workspace `version` lines. Expect `bun install` to then rewrite a few lines for pieces the branch never touched: piece PRs on `main` often bump a `package.json` without its `bun.lock` entry (seen 2026-10-05: grist, microsoft-todo, ticktick, zendesk). Those lines are main catching up, not your change; keep them, because the lock should match the manifests.- **Any change to `bun.lock` runs every package's tests in `build-test`, all 700+ pieces included.** The `changes` job in `ci.yml` sets `all=true` when a changed file is outside `packages/`, `docs/`, `brain/`, `.agents/` and `.claude/` and is not a `*.md` file (so root Markdown alone does not count), and `tools/scripts/test-filters.ts` then adds every package with a `test` script **except `api`** (it lists with `--filter=!api`). API tests run in the separate `api-tests` matrix job (`api (ce)`, `api (cloud)`, `api (ee, unit, migrations)`), so look there for their results, not in `build-test`. Without that, a piece only runs when its own files or `pieces/framework` / `pieces/common` change; a `core-utils` change alone does not pull pieces in. Any workspace version bump edits `bun.lock`, so this hits most PRs that bump a package. A CPU-heavy piece test can then fail on the starved runner with no change of yours: an Odoo polling test that takes ~350ms locally passed vitest's 5s default in 3 of 3 runs, reruns included. Don't keep rerunning: give the heavy test an explicit timeout (third `it` argument). Even a test-only change needs that piece's patch bump (and its `bun.lock` line): `validate-publishable-packages` diffs the whole package folder against `origin/main`.@@ -63,6 +64,7 @@ Enforcement is the **`Codeowners review` repository ruleset** (active on the def- **Each link of a stack runs CI against its own base, so verifying the top proves nothing about the ones below it.** A refactor that lands on the bottom branch and is repaired two branches up is green where you tested and red on the PR that actually carries it, and the failure reads as unrelated because the file it names was fine when you last looked at it. Before pushing a stack, check out each branch in turn and run the package's typecheck, lint and full test suite there, not just at the tip; the suites legitimately differ (this stack's tip runs two more web tests than its base). Do the same after any rebase, since a conflict resolved in one replay can leave a lower branch importing something only a higher one defines.- **A deliberately stacked PR is charged only for its own delta, but each link pays its own `@activepieces/shared` bump.** `pr-size-check.ts` diffs `HEAD^1...HEAD^2` on the `pull_request` merge commit precisely so an ancestor PR is not re-counted — a stack member measures the same whether reviewed against `main` or against the PR below it, so splitting a large change into a stack really does buy budget rather than just moving it. Two things the split still costs you. Every PR in the stack that touches `packages/core/shared` needs its **own** version increment (0.158.0 then 0.159.0, not the same bump twice), and `bun.lock` records each workspace's version, so edit that one line **by hand** in each — regenerating sweeps `main`'s pending drift into your diff, per the regenerator trap above. And prove the split is faithful before pushing: tag the pre-split commit, then `git diff <tag>..<stack tip>` must come back empty apart from those intentional version lines. A file that lands partially in one PR and grows in the next (a service that gains its read side, a test file that gains its endpoint cases) is exactly where a hand-split silently drops a hunk.- **A migration-check failure in a stack names the PR that *introduced* the file, which is usually not the branch you are sitting on.** `check-migration-rollback.ts` lists candidates with `git diff --name-only --diff-filter=A origin/$GITHUB_BASE_REF...HEAD`, so a migration is only ever scanned on the stack member that **adds** it; every branch above inherits the file and its CI stays green on that check. So the pasted failure belongs to the lowest link, and editing the file on the tip you happen to have checked out fixes nothing the gate looks at. Confirm ownership before touching anything — `gh pr view <n> --json headRefName` for the branch the number really points at, then `git cat-file -e <branch>:<path>` across the stack for the first branch holding the file — fix it there, and rebase upward. Two things make that cheap: the file is usually byte-identical across the stack (`git diff <lower> <upper> -- <path>` comes back empty), and `git rebase` reports `skipped previously applied commit` for the lower branch's pre-rebase commits, which is the expected signal rather than a lost commit. Verify each link the same way the split rule above does — tag the old tip, then `git diff <tag> <rebased branch>` must show only the intended delta. Hit on PR 15240 (`feat/mcp-activity-recording`) while its own migration read as a failure on `feat/mcp-activity-serve` two links up.+- **A long-lived migration branch loses its number to main, so expect to renumber it.** This happened twice on #16116 (1861 → 1865 → 1866). Rename the file, the class and `name`, and the import plus `getMigrations()` entry in `postgres-connection.ts`, and keep `release`. Commit before running `check-migration-rollback.ts`. It reads `git diff origin/main...HEAD`, so a rename that is only staged fails with `ERR_MODULE_NOT_FOUND` on the old path. Anyone whose dev DB already ran the old number must rename its row in `migrations` (`name` and `timestamp`), or the migration runs again.- **A decision authored on a long-lived branch will collide on its number.** `brain/decisions/` numbers are assigned once and never reused, but the next free number is only knowable against `main` — two branches in flight both grab it. #14593 carried a `000024` that `main` had since filled, and `000025` too, so it landed as `000026`. Renumber against `main` at merge time and update every referring link; nothing in CI catches a duplicate number or a dead decision link.- **Preview environments resurrect on PR close because `setup-environment.yml` also triggers on `closed`.** Both workflows fire on the same close event; Remove Environment tears the env down correctly (compose down, nginx, repo), then Setup Environment sees the `preview` label (labels survive merge) and re-provisions the whole thing minutes later — verified on #14832: remove finished 11:20, setup rebuilt it by 11:30. This is why merged PRs kept live zombie environments on the preview box. Both workflows are thin SSH wrappers; the real setup/remove logic lives in `/root/environments` on the preview server (`secrets.PREVIEW_HOST`), not in this repo. Fixed by dropping `closed` from setup's trigger list.- **The preview-server remove tool can't clean containers once the repo dir is gone.** Its `stop()` skips `docker compose down` when `repos/<subdomain>/docker-compose.yml` doesn't exist, so an env whose repo folder was deleted first leaves containers running forever — re-running `remove` is a no-op for them. Clean those manually via compose labels: `docker ps -aq --filter "label=com.docker.compose.project=<subdomain>"` (same filter works for `docker volume ls`). When auditing envs against PR state: read the real branch from the clone's HEAD (`git -C repos/<subdomain> symbolic-ref --short HEAD`) since subdomains flatten `/` to `-`; a clone sitting on `main` means the branch was deleted after merge; and an env with **no PR at all** is a manual `workflow_dispatch` preview — don't auto-delete those (bulk cleanup 2026-08-20 removed 27 closed-PR envs, reclaimed 32.5GB).@@ -91,7 +93,7 @@ Enforcement is the **`Codeowners review` repository ruleset** (active on the def- **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. 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. A child branched off an *older* head of its parent (the parent took review fixes after the child was cut) also conflicts in files the child never touched: those are the parent's early copy against its final one on `main`. If `git log <old parent head>..HEAD -- <file>` lists none of the child's commits, `git checkout --theirs <file>` is the right answer there, because the whole file is main's (PR 4 of the model tiers work, #15866: `use-chat.ts`, `core/execution/package.json` and an agent test). For a file the child *did* change, take main's copy and replay only the child's own commit on top: `git checkout --theirs <files> && git add <files> && git show <child commit> -- <files> | git apply --3way` (it needs the files staged, and in zsh pass the list as an array, since a plain variable is not word-split); only hunks where main moved the child's own lines are left to resolve by hand (PR 5, #15871: six files, one hunk).-- **Retargeting a PR's base never starts CI, and a push made while the old base conflicts may not start it either.** `ci.yml` listens only to `pull_request` opened, synchronize and reopened; `gh pr edit --base` fires `edited`, which starts nothing. On #15866 the catch-up merge was pushed while the PR still targeted its squash-merged parent's branch, the main CI never ran on that head, and the retarget did not trigger it, so the PR showed only the small checks (title, breaking change, Greptile) and looked green. After a retarget, check `gh pr checks <n>` lists `build-test` and the `api` jobs; if not, push again (an empty `ci:` commit is enough). Never `gh run rerun`: it replays the old event.+- **Retargeting a PR's base never starts CI, and a push made while the old base conflicts may not start it either.** `ci.yml` listens only to `pull_request` opened, synchronize and reopened; `gh pr edit --base` fires `edited`, which starts nothing. On #15866 the catch-up merge was pushed while the PR still targeted its squash-merged parent's branch, the main CI never ran on that head, and the retarget did not trigger it, so the PR showed only the small checks (title, breaking change, Greptile) and looked green. After a retarget, check `gh pr checks <n>` lists `build-test` and the `api` jobs; if not, push again (an empty `ci:` commit is enough). Never `gh run rerun`: it replays the old event. Simpler when a rebuild needs a force-push anyway: retarget first, then push. The push fires `synchronize` against the new base and the main CI starts on its own (#16116, after the parent squash-merged); pushing first and retargeting after needed the empty commit (#16197).- **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/engineering/web-feature-anatomy.md b/brain/knowledge/engineering/web-feature-anatomy.mdindex a44355a55a0..8e29cafe00e 100644--- a/brain/knowledge/engineering/web-feature-anatomy.md+++ b/brain/knowledge/engineering/web-feature-anatomy.md@@ -78,6 +78,7 @@ way back was to stash the handful of intended edits, `git checkout` the package,## Gotchas+- **A web test that mocks `@tanstack/react-query` breaks at import once the component under test starts reading a `*-hooks.ts` module.** Those modules reference `QueryClient`, the partial mock doesn't export it, and the file fails to load: vitest prints `Test Files 1 failed` while `Tests N passed` still looks green, so read the `Test Files` line. Mock the hooks module the component imports (as `config-detail-save.test.tsx` does for the tier and provider hooks) instead of widening the react-query mock (#16116).- **The SPA is hardcoded to the host root, so `AP_FRONTEND_URL`'s path prefix does not reach the frontend at all.** `index.html` carries `<base href="/" />`, there is no vite `base`, and `API_BASE_URL` in `lib/api.ts` is `window.location.origin` — origin only, path dropped. An instance served at `example.com/activepieces` therefore loads its bundle, its assets and every `/api/v1/...` call from `example.com/...` at the root, and only works because the operator's proxy exposes those at root too. The server-side subpath support (`domainHelper.getPublicUrlFromRequest`, which prepends the configured base path to advertised URLs) covers what AP *tells* clients, not what the browser *fetches* — the two do not meet. So "we support subpath hosting" is only half true: it holds for what the server advertises and for anything the browser never has to fetch. A second host escapes the constraint only if it serves no SPA at all — which is exactly why MCP consent is redirected back to the frontend base rather than served on the MCP host (see [[mcp-server]]), leaving `AP_MCP_URL` free to carry a prefix.- **A disabled TanStack query keeps `isPending: true` forever, so a skeleton driven by it never resolves.** `enabled: false` means "has no data and is not fetching", and `isPending` only reports the first half — the spinner spins for the life of the page. Drive loading UI off `isLoading` (`isPending && isFetching`), which is false while disabled. This bites hardest on the plan-gated queries the repo requires an `enabled: platform.plan.<flag>` on: every one of them is permanently pending for a platform without the feature. Also decide what the surface shows in that state — a count rendered from `data ?? 0` reads as a real zero, when the honest answer is that the feature is not on the plan.- **The `showcase/*.mp4` upsell videos on the CDN are all from 2024 and show the retired Admin Console.** `cdn.activepieces.com/videos/showcase/` holds seven of them (projects, templates, pieces, appearance, api-keys, alerts, flow-issues), last touched March-June 2024, and every frame is the old sidebar, the old tables and the old purple buttons. `FeatureTeaser` renders whatever `videoUrl`/`lockVideoUrl` it is handed, and the Templates locked page still passes `templates.mp4`, so a customer on the paywall watches a product that no longer exists. Do not reach for these when building an upsell surface — either reshoot or leave the video out. Pull a frame with ffmpeg before trusting any CDN asset whose name sounds right.@@ -103,6 +104,9 @@ way back was to stash the handful of intended edits, `git checkout` the package,- **Exported types and constants belong at the *end* of the file**, after the components and logic. Reading a file should start with what it does, not its type declarations.- **The web has two independent "something went wrong" surfaces, and they cover different failures.** `GlobalErrorBoundary` (`app/components/global-error-boundary.tsx`) is a React error boundary: it catches *render* crashes and replaces the page with a reload/go-home fallback. It structurally cannot see a React Query failure — a failed query is stored as state, not thrown during render, unless the query opts into `throwOnError` or Suspense. Nothing global covers that async gap any more: a failed primary query is reported by the surface itself, through `DataFetchErrorState`. The two landed independently (the query surface first, in #12476 for tables; the boundary later, in #13743) and were never calibrated against each other, so for a long stretch a single 404 got a *blocking modal with raw JSON* while an actual app crash got a friendly reload button. Keep that ordering right: a failed fetch on a page that still renders is an in-place placeholder, a dead render tree is the full-page fallback. A modal is only correct when the error payload is something the user must read and copy — flow publish showing the trigger piece's stderr (`flow-hooks.tsx`) is the one case that still earns `ApErrorDialog`.- **`FriendlyErrorView` is the one renderer for a `FriendlyPieceError` — reach for it before hand-rolling a message line.** `app/builder/data-display/friendly-error-view.tsx` already turns the parsed payload into a status-keyed headline and hint (401 → "Authentication failed" + "Try reconnecting the account…"), an `HTTP {status}` badge, a labelled message block that prefers `apiMessage` over `message`, and its own `Technical Details` disclosure; it is wired to the run-details and test-step panels. For an auth failure the status-keyed *hint* is the part that actually prevents a misdiagnosis — naming whose credentials to fix beats quoting the third party's own sentence, which is what made a rejected Linear key read to a customer as their Activepieces session expiring (Pylon 5833). Three things make it non-trivial to drop into `ApErrorDialog` and all three are local fixes: it renders a second `Technical Details` next to the dialog's own, its disclosure shows `raw ?? payload` so it drops `standardOutput` (the piece's stderr, often the useful half of a failed trigger enable), and `useChangeFlowStatus` has only `flowId` so `pieceDisplayName` falls back to "What the service said". Fix those rather than growing a parallel renderer — a second one means the `message`-is-JSON trap (see *building pieces*) has to be fixed twice.+- **User feedback for a mutation belongs in the hook's `useMutation` options, not in per-call `mutate(vars, { onError })` callbacks.** `MutationCache.onError` in `app/query-client.ts` skips its "Something went wrong" toast only when the *options* carry `onError`, and TanStack drops per-call callbacks once the observer unmounts (dialog closed mid-request) or a newer `mutate` replaces the call. A page that toasts per call looks fine in the happy path and goes silent exactly when the request fails late. Keep per-call callbacks for UI only (close the dialog, move focus).+- **`api.extractServerErrorMessage(err, fallback)` almost never shows `fallback`.** It returns the raw Axios message ("Request failed with status code 500", "Network Error") before falling back, so translated copy is unreachable for HTTP failures. Use `api.serverErrorMessage(err) ?? t('…')`: the server's `params.message` when there is one, your copy otherwise.+- **`eslint --fix` on a `packages/web` test will rewrite a plain DOM assertion into a jest-dom matcher that does not exist here.** The `jest-dom/*` rules are on, but `@testing-library/jest-dom` matchers are not installed, so `--fix` turns `expect(el.hasAttribute('disabled')).toBe(true)` into `expect(el).toBeDisabled()`, and the test then fails at runtime with `Invalid Chai property: toBeDisabled`. Sibling suites handle it with a file-level `/* eslint-disable jest-dom/prefer-in-document, jest-dom/prefer-enabled-disabled */` and assertions on `getAttribute('data-disabled')` (Radix sets it) rather than the `disabled` attribute, which `prefer-to-have-attribute` also rewrites. Run eslint on the test file *before* vitest, not after, so a passing run is the one you commit.- **Stubbing Radix `Select` in a jsdom test: render each `SelectItem` as a button that calls the real `onValueChange`, and never match a click by button text alone.** The component under test passes `onValueChange` to `Select`, so a stub that captures it in a `vi.hoisted` box and has `SelectItem` call it with its own `value` gives you the real state update without Radix's portal, pointer and `ResizeObserver` machinery. The trap is finding the button afterwards: the dropdown items and the UI they control often carry the same words (a Text/Image type picker next to chips whose badge also reads "Text"), and a `textContent` match takes whichever comes first in the DOM — silently clicking the dropdown item and asserting a no-op. Match on something structural (`button[title="Model Type"]` inside the chip) and give each helper a name that says which one it clicks. Also note `expect(value, 'message')` — valid vitest — is rejected by the repo's `vitest/valid-expect` lint rule, so put the identifying detail in the helper name rather than the assertion message.- **`DialogDescription` renders a `<p>` (Radix `Primitive.p`), so block content in the description slot is invalid nesting.** `main` already puts a `<p>` inside it, which is one React `validateDOMNesting` warning; adding a `<div>` wrapper takes it to two (measured by rendering the dialog under jsdom and counting `console.error` calls). No visual break — this is a Vite SPA with no SSR, so nothing re-parses the HTML — but it does mean the description slot is the wrong home for a bordered, badged panel. Put that in the dialog body and leave the description a sentence.diff --git a/brain/knowledge/platform-editions-ee/platform-configuration.md b/brain/knowledge/platform-editions-ee/platform-configuration.mdindex 9c08f093a71..6c6eb86a50b 100644--- a/brain/knowledge/platform-editions-ee/platform-configuration.md+++ b/brain/knowledge/platform-editions-ee/platform-configuration.md@@ -17,6 +17,7 @@ A **Platform** is the top-level tenant namespace in Activepieces. Every install- `GET /v1/platforms/assets/:id` — public asset download.### Gotchas+- **`aiSpecificModelsVisible` lives on `platform_configuration`, not on `platform`.** The AI Center Tiers tab writes it through the existing admin-only `POST /v1/platform-configurations`, and every signed-in user can read it from `GET /` (`publicPlatform`), which is what the builder model picker needs. The rule "hidden implies at least one live tier" is held by the web client only: the switch is disabled at zero tiers and the last-tier delete turns it back on before deleting. An API caller can still hide specific models on a platform with no tiers, and builders then have nothing to pick.- **A new platform column breaks the cloud `platform.test.ts` key list.** "Always Returns non-sensitive information for platform" pins `Object.keys(response).sort()` with `toStrictEqual`, so adding a field to `PlatformWithoutSensitiveData` fails `api (cloud)` until the key is added there, in sorted order. It only runs in the cloud suite, so a CE run won't catch it.- **The platform name is editable on every edition, even though it lives inside the Appearance section.** `appearance-section.tsx` computes `brandingLocked = !platform.plan.customAppearanceEnabled` and passes `disabled={brandingLocked}` to the logo, icon, favicon and brand-colour inputs, but **not** to the `Platform Name` input, and `formdata.append('name', name)` sits outside the `if (!brandingLocked)` block. So a Community or unlicensed platform can rename itself at Settings > Platform > Account > General while every other field on that form is locked. Reading the file top-down makes the whole section look gated; it is per-field. `UpdatePlatformRequestBody.name` is likewise ungated on the API and only validated against `SAFE_STRING_PATTERN` (no `.` or `/`).- Per-project piece/action/trigger visibility is done via **piece sets**, NOT the platform.diff --git a/bun.lock b/bun.lockindex 317c5f1758c..d0cf2afa617 100644--- a/bun.lock+++ b/bun.lock@@ -167,7 +167,7 @@},"packages/core/shared": {"name": "@activepieces/shared",- "version": "0.198.1",+ "version": "0.199.0","dependencies": {"@activepieces/core-execution": "workspace:*","@activepieces/core-formula": "workspace:*",diff --git a/packages/core/shared/package.json b/packages/core/shared/package.jsonindex b0eefdc4cd8..c449a2ebee6 100644--- a/packages/core/shared/package.json+++ b/packages/core/shared/package.json@@ -1,6 +1,6 @@{"name": "@activepieces/shared",- "version": "0.198.1",+ "version": "0.199.0","type": "commonjs","sideEffects": false,"main": "./dist/src/index.js",diff --git a/packages/core/shared/src/lib/management/platform/platform-configuration.ts b/packages/core/shared/src/lib/management/platform/platform-configuration.tsindex ca7a006bdd9..2128d6321ff 100644--- a/packages/core/shared/src/lib/management/platform/platform-configuration.ts+++ b/packages/core/shared/src/lib/management/platform/platform-configuration.ts@@ -14,6 +14,7 @@ export const PlatformConfiguration = z.object({.int({ error: (issue) => isNil(issue.input) ? formErrors.required : formErrors.wholeNumber }).min(MIN_BARRIER_SIGNALS, formErrors.atLeastOne).max(MAX_BARRIER_SIGNALS, formErrors.atMostTenThousand),+ aiSpecificModelsVisible: z.boolean(),})export type PlatformConfiguration = z.infer<typeof PlatformConfiguration>@@ -24,7 +25,9 @@ export const PlatformConfigurationSettings = PlatformConfiguration.pick({})export type PlatformConfigurationSettings = z.infer<typeof PlatformConfigurationSettings>-export const UpdatePlatformConfigurationRequestBody = PlatformConfigurationSettings.partial()+export const UpdatePlatformConfigurationRequestBody = PlatformConfigurationSettings.partial().extend({+ aiSpecificModelsVisible: z.boolean().optional(),+})export type UpdatePlatformConfigurationRequestBody = z.infer<typeof UpdatePlatformConfigurationRequestBody>export const maxBarrierSignalsBounds = {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 c30e719745c..a204bb9aeae 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@@ -4,12 +4,14 @@ import { EntityManager, In } from 'typeorm'import { repoFactory } from '../core/db/repo-factory'import { transaction } from '../core/db/transaction'import { isUniqueViolation } from '../core/db/unique-violation'+import { PlatformConfigurationEntity } from '../platform/platform-configuration.entity'import { aiKeyScope, ProviderScope } from './ai-key-scope'import { AIProviderEntity, AIProviderSchema } from './ai-provider-entity'import { PlatformModelTierEntity, PlatformModelTierSchema } from './platform-model-tier-entity'const tierRepo = repoFactory<PlatformModelTierSchema>(PlatformModelTierEntity)const aiProviderRepo = repoFactory<AIProviderSchema>(AIProviderEntity)+const platformConfigurationRepo = repoFactory(PlatformConfigurationEntity)const MAX_LIVE_TIERS = 50const MAX_REPLACEMENT_HOPS = 3@@ -126,6 +128,7 @@ export const platformModelTierService = {await assertLastTierCanGo({ manager, platformId })await tierRepo(manager).update({ platformId, id }, { isDefault: false, isFast: false })await tierRepo(manager).softDelete({ platformId, id })+ await platformConfigurationRepo(manager).update({ platformId }, { aiSpecificModelsVisible: true })return}await getLiveOrThrow({ manager, platformId, id: replacedBy })@@ -144,6 +147,13 @@ export const platformModelTierService = {})},+ async assertSpecificModelsVisibilityAllowed({ manager, platformId, visible }: { manager: EntityManager, platformId: PlatformId, visible: boolean }): Promise<void> {+ await lockPlatform({ manager, platformId })+ if (!visible && (await listLive({ platformId, manager })).length === 0) {+ throw new ActivepiecesError({ code: ErrorCode.VALIDATION, params: { message: 'Add a tier before hiding specific models from builders' } })+ }+ },+async assertKeyCanBeDeleted({ manager, platformId, configId }: { manager: EntityManager, platformId: PlatformId, configId: string }): Promise<void> {await lockPlatform({ manager, platformId })const tiers = await findLiveTiersUsingKey({ manager, platformId, configId })diff --git a/packages/server/api/src/app/database/migration/postgres/1866000000000-AddAiSpecificModelsVisibleToPlatformConfiguration.ts b/packages/server/api/src/app/database/migration/postgres/1866000000000-AddAiSpecificModelsVisibleToPlatformConfiguration.tsnew file mode 100644index 00000000000..a749ced30f7--- /dev/null+++ b/packages/server/api/src/app/database/migration/postgres/1866000000000-AddAiSpecificModelsVisibleToPlatformConfiguration.ts@@ -0,0 +1,23 @@+import { QueryRunner } from 'typeorm'+import { Migration } from '../../migration'++export class AddAiSpecificModelsVisibleToPlatformConfiguration1866000000000 implements Migration {+ name = 'AddAiSpecificModelsVisibleToPlatformConfiguration1866000000000'+ breaking = false+ release = '0.93.0'+ transaction = true++ public async up(queryRunner: QueryRunner): Promise<void> {+ await queryRunner.query(`+ ALTER TABLE "platform_configuration"+ ADD COLUMN "aiSpecificModelsVisible" boolean NOT NULL DEFAULT true+ `)+ }++ public async down(queryRunner: QueryRunner): Promise<void> {+ await queryRunner.query(`+ ALTER TABLE "platform_configuration"+ DROP COLUMN "aiSpecificModelsVisible"+ `)+ }+}diff --git a/packages/server/api/src/app/database/postgres-connection.ts b/packages/server/api/src/app/database/postgres-connection.tsindex 699402cdb28..03cffcda46a 100644--- a/packages/server/api/src/app/database/postgres-connection.ts+++ b/packages/server/api/src/app/database/postgres-connection.ts@@ -453,6 +453,7 @@ import { AddEventDestinationEnabledAndFormat1862000000000 } from './migration/poimport { AddEventDestinationHeaders1863000000000 } from './migration/postgres/1863000000000-AddEventDestinationHeaders'import { AddPlatformTierFastAndConversationTier1864000000000 } from './migration/postgres/1864000000000-AddPlatformTierFastAndConversationTier'import { AddAuditLogRetentionDaysToPlatform1865000000000 } from './migration/postgres/1865000000000-AddAuditLogRetentionDaysToPlatform'+import { AddAiSpecificModelsVisibleToPlatformConfiguration1866000000000 } from './migration/postgres/1866000000000-AddAiSpecificModelsVisibleToPlatformConfiguration'const getSslConfig = (): boolean | TlsOptions => {const useSsl = system.get(AppSystemProp.POSTGRES_USE_SSL)@@ -921,6 +922,7 @@ export const getMigrations = (): (new () => Migration)[] => {AddEventDestinationHeaders1863000000000,AddPlatformTierFastAndConversationTier1864000000000,AddAuditLogRetentionDaysToPlatform1865000000000,+ AddAiSpecificModelsVisibleToPlatformConfiguration1866000000000,]return migrations}diff --git a/packages/server/api/src/app/platform/platform-configuration.controller.ts b/packages/server/api/src/app/platform/platform-configuration.controller.tsindex dfeb73aa126..51c6ba9a96d 100644--- a/packages/server/api/src/app/platform/platform-configuration.controller.ts+++ b/packages/server/api/src/app/platform/platform-configuration.controller.ts@@ -17,6 +17,7 @@ export const platformConfigurationController: FastifyPluginAsyncZod = async (appisProductTelemetryEnabled: request.body.isProductTelemetryEnabled,isInfraSetupTelemetryEnabled: request.body.isInfraSetupTelemetryEnabled,maxBarrierSignals: request.body.maxBarrierSignals,+ aiSpecificModelsVisible: request.body.aiSpecificModelsVisible,})})}diff --git a/packages/server/api/src/app/platform/platform-configuration.entity.ts b/packages/server/api/src/app/platform/platform-configuration.entity.tsindex fc31eed0245..f964958c4e0 100644--- a/packages/server/api/src/app/platform/platform-configuration.entity.ts+++ b/packages/server/api/src/app/platform/platform-configuration.entity.ts@@ -29,6 +29,11 @@ export const PlatformConfigurationEntity = new EntitySchema<PlatformConfigurationullable: false,default: 10000,},+ aiSpecificModelsVisible: {+ type: Boolean,+ nullable: false,+ default: true,+ },},indices: [{diff --git a/packages/server/api/src/app/platform/platform-configuration.service.ts b/packages/server/api/src/app/platform/platform-configuration.service.tsindex b711a384f65..9163dc8a4c6 100644--- a/packages/server/api/src/app/platform/platform-configuration.service.ts+++ b/packages/server/api/src/app/platform/platform-configuration.service.ts@@ -2,7 +2,9 @@ import { apId, chunk, isEmpty, isNil, spreadIfNotUndefined } from '@activepiecesimport { ApEdition, maxBarrierSignalsBounds, PlatformConfiguration, UpdatePlatformConfigurationRequestBody } from '@activepieces/shared'import { FastifyBaseLogger } from 'fastify'import { In } from 'typeorm'+import { platformModelTierService } from '../ai/platform-model-tier-service'import { repoFactory } from '../core/db/repo-factory'+import { transaction } from '../core/db/transaction'import { distributedLock } from '../database/redis-connections'import { system } from '../helper/system/system'import { AppSystemProp } from '../helper/system/system-props'@@ -91,15 +93,21 @@ export const platformConfigurationService = (log: FastifyBaseLogger) => ({return platformIds.filter((platformId) => !optedOutIds.has(platformId))},- async update({ platformId, isProductTelemetryEnabled, isInfraSetupTelemetryEnabled, maxBarrierSignals }: UpdateParams): Promise<PlatformConfiguration> {+ async update({ platformId, isProductTelemetryEnabled, isInfraSetupTelemetryEnabled, maxBarrierSignals, aiSpecificModelsVisible }: UpdateParams): Promise<PlatformConfiguration> {await this.getOrCreateForPlatform({ platformId })const patch = {...spreadIfNotUndefined('isProductTelemetryEnabled', isProductTelemetryEnabled),...spreadIfNotUndefined('isInfraSetupTelemetryEnabled', isInfraSetupTelemetryEnabled),...spreadIfNotUndefined('maxBarrierSignals', maxBarrierSignals),+ ...spreadIfNotUndefined('aiSpecificModelsVisible', aiSpecificModelsVisible),}if (!isEmpty(patch)) {- await platformConfigurationRepo().update({ platformId }, patch)+ await transaction(async (manager) => {+ if (!isNil(aiSpecificModelsVisible)) {+ await platformModelTierService.assertSpecificModelsVisibilityAllowed({ manager, platformId, visible: aiSpecificModelsVisible })+ }+ await platformConfigurationRepo(manager).update({ platformId }, patch)+ })}return platformConfigurationRepo().findOneByOrFail({ platformId })},diff --git a/packages/server/api/test/integration/ce/ai-provider/platform-model-tier.test.ts b/packages/server/api/test/integration/ce/ai-provider/platform-model-tier.test.tsindex 7c3a9bf6f4c..8e67efbd024 100644--- a/packages/server/api/test/integration/ce/ai-provider/platform-model-tier.test.ts+++ b/packages/server/api/test/integration/ce/ai-provider/platform-model-tier.test.ts@@ -278,6 +278,42 @@ describe('Platform model tiers API', () => {})})+ describe('last tier and specific models', () => {+ it('refuses to hide specific models once the last tier is gone, so the delete and the hide cannot race past each other', async () => {+ const key = await seedKey({ testCtx: ctx })+ const tier = await createTier({ testCtx: ctx, body: tierBody({ configId: key.id }) })+ await ctx.delete(`${TIERS}/${tier.id}`)++ const hide = await ctx.post(CONFIGURATIONS, { aiSpecificModelsVisible: false })++ expect(hide.statusCode).toBe(StatusCodes.CONFLICT)+ expect((await ctx.get(CONFIGURATIONS)).json()).toMatchObject({ aiSpecificModelsVisible: true })+ })++ it('shows specific models again in the same step that deletes the last tier', async () => {+ const key = await seedKey({ testCtx: ctx })+ const tier = await createTier({ testCtx: ctx, body: tierBody({ configId: key.id }) })+ await ctx.post(CONFIGURATIONS, { aiSpecificModelsVisible: false })++ const removed = await ctx.delete(`${TIERS}/${tier.id}`)++ expect(removed.statusCode).toBe(StatusCodes.NO_CONTENT)+ expect((await ctx.get(CONFIGURATIONS)).json()).toMatchObject({ aiSpecificModelsVisible: true })+ })++ it('keeps specific models hidden when the delete is refused', async () => {+ const key = await seedKey({ testCtx: ctx })+ const first = await createTier({ testCtx: ctx, body: tierBody({ configId: key.id, name: 'First' }) })+ await createTier({ testCtx: ctx, body: tierBody({ configId: key.id, name: 'Second' }) })+ await ctx.post(CONFIGURATIONS, { aiSpecificModelsVisible: false })++ const refused = await ctx.delete(`${TIERS}/${first.id}`)++ expect(refused.statusCode).toBe(StatusCodes.CONFLICT)+ expect((await ctx.get(CONFIGURATIONS)).json()).toMatchObject({ aiSpecificModelsVisible: false })+ })+ })+describe('key guard', () => {it('refuses to delete a key a tier uses', async () => {const key = await seedKey({ testCtx: ctx })@@ -338,3 +374,4 @@ async function findWithDeleted({ id }: { id: string }): Promise<unknown> {}const TIERS = '/v1/platform-model-tiers'+const CONFIGURATIONS = '/v1/platform-configurations'diff --git a/packages/server/api/test/integration/ce/platform/platform-configuration.test.ts b/packages/server/api/test/integration/ce/platform/platform-configuration.test.tsindex 8e832c79b02..0c1ea5f8d1c 100644--- a/packages/server/api/test/integration/ce/platform/platform-configuration.test.ts+++ b/packages/server/api/test/integration/ce/platform/platform-configuration.test.ts@@ -1,8 +1,10 @@+import { AIProviderName } from '@activepieces/core-utils'import { ApFlagId, DefaultProjectRole, maxBarrierSignalsBounds } from '@activepieces/shared'import { FastifyInstance } from 'fastify'import { StatusCodes } from 'http-status-codes'import { system } from '../../../../src/app/helper/system/system'import { AppSystemProp } from '../../../../src/app/helper/system/system-props'+import { mockAndSaveAIProvider } from '../../../helpers/mocks'import { createMemberContext, createTestContext } from '../../../helpers/test-context'import { setupTestEnvironment, teardownTestEnvironment } from '../../../helpers/test-setup'@@ -151,6 +153,48 @@ describe('platform configuration', () => {expect(ApFlagId).not.toHaveProperty('TELEMETRY_ENABLED')})+ it('shows specific AI models to builders until an admin hides them, and lets any member read the switch', async () => {+ const ctx = await createTestContext(app!)+ const memberCtx = await createMemberContext(app!, ctx, {+ projectRole: DefaultProjectRole.EDITOR,+ })++ expect((await ctx.get('/v1/platform-configurations'))!.json().aiSpecificModelsVisible).toBe(true)++ const refused = await ctx.post('/v1/platform-configurations', { aiSpecificModelsVisible: false })+ expect(refused?.statusCode).toBe(StatusCodes.CONFLICT)++ const key = await mockAndSaveAIProvider({ platformId: ctx.platform.id, provider: AIProviderName.OPENAI })+ const tier = await ctx.post('/v1/platform-model-tiers', { name: 'Expert', emoji: '🧠', description: null, entries: [{ configId: key.id, modelId: 'gpt-4o' }] })+ expect(tier?.statusCode).toBe(StatusCodes.OK)++ const hidden = await ctx.post('/v1/platform-configurations', { aiSpecificModelsVisible: false })+ expect(hidden?.statusCode).toBe(StatusCodes.OK)+ expect(hidden!.json().aiSpecificModelsVisible).toBe(false)+ expect(hidden!.json().isInfraSetupTelemetryEnabled).toBe(true)++ const memberRead = await memberCtx.get('/v1/platform-configurations')+ expect(memberRead?.statusCode).toBe(StatusCodes.OK)+ expect(memberRead!.json().aiSpecificModelsVisible).toBe(false)++ const memberWrite = await memberCtx.post('/v1/platform-configurations', { aiSpecificModelsVisible: true })+ expect(memberWrite?.statusCode).toBe(StatusCodes.FORBIDDEN)+ expect((await ctx.get('/v1/platform-configurations'))!.json().aiSpecificModelsVisible).toBe(false)+ })++ it('changes nothing when hiding specific models is refused alongside another setting', async () => {+ const ctx = await createTestContext(app!)+ const before = (await ctx.get('/v1/platform-configurations'))!.json().maxBarrierSignals+ expect(before).not.toBe(maxBarrierSignalsBounds.min)++ const refused = await ctx.post('/v1/platform-configurations', { aiSpecificModelsVisible: false, maxBarrierSignals: maxBarrierSignalsBounds.min })++ expect(refused?.statusCode).toBe(StatusCodes.CONFLICT)+ const after = (await ctx.get('/v1/platform-configurations'))!.json()+ expect(after.maxBarrierSignals).toBe(before)+ expect(after.aiSpecificModelsVisible).toBe(true)+ })+it('lets any platform member read the configuration, but only an admin write it', async () => {const ctx = await createTestContext(app!)const memberCtx = await createMemberContext(app!, ctx, {diff --git a/packages/web/public/locales/en/translation.json b/packages/web/public/locales/en/translation.jsonindex 33a644cb5cb..ea2a0505935 100644--- a/packages/web/public/locales/en/translation.json+++ b/packages/web/public/locales/en/translation.json@@ -3323,6 +3323,123 @@"{tier}: {status}": "{tier}: {status}","{tier}, {status}": "{tier}, {status}","Show full output": "Show full output",+ "{context} context": "{context} context",+ "{input} / {output} per 1M": "{input} / {output} per 1M",+ "Supports tool calling": "Supports tool calling",+ "Supports reasoning": "Supports reasoning",+ "Tiers": "Tiers",+ "No models match": "No models match",+ "Couldn't load {key} models": "Couldn't load {key} models",+ "Full matches": "Full matches",+ "With trade-offs": "With trade-offs",+ "Other models": "Other models",+ "Smaller context ({context})": "Smaller context ({context})",+ "No tool calling": "No tool calling",+ "Could not show specific models to builders": "Could not show specific models to builders",+ "Could not delete this tier": "Could not delete this tier",+ "Delete {name}?": "Delete {name}?",+ "This is your last tier.": "This is your last tier.",+ "Anything using {name} moves to the tier you pick.": "Anything using {name} moves to the tier you pick.",+ "Anything still using {name} will stop working until someone picks a new model.": "Anything still using {name} will stop working until someone picks a new model.",+ "Specific models will be shown to builders again, so they still have something to pick.": "Specific models will be shown to builders again, so they still have something to pick.",+ "Move them to": "Move them to",+ "Pick a tier": "Pick a tier",+ "{name} becomes the default tier.": "{name} becomes the default tier.",+ "{name} becomes the fast tier.": "{name} becomes the fast tier.",+ "Delete tier": "Delete tier",+ "Named model groups your builders pick. Each one is a main model plus fallbacks.": "Named model groups your builders pick. Each one is a main model plus fallbacks.",+ "New tier": "New tier",+ "tiers": "tiers",+ "Add a provider key first": "Add a provider key first",+ "Tiers are built from your keys' models.": "Tiers are built from your keys' models.",+ "Go to Providers": "Go to Providers",+ "Create your first tier": "Create your first tier",+ "Name it, pick a main model, add fallbacks.": "Name it, pick a main model, add fallbacks.",+ "Loading tiers": "Loading tiers",+ "Could not save this setting": "Could not save this setting",+ "Specific models visible to builders": "Specific models visible to builders",+ "Add a tier before hiding these.": "Add a tier before hiding these.",+ "Specific models": "Specific models",+ "Models not used as a tier's main model.": "Models not used as a tier's main model.",+ "Make a tier": "Make a tier",+ "Show {count} more": "Show {count} more",+ "Could not save this tier": "Could not save this tier",+ "Fast": "Fast",+ "Saving": "Saving",+ "Tier actions": "Tier actions",+ "Make default": "Make default",+ "Use as fast model": "Use as fast model",+ "Move up": "Move up",+ "Move down": "Move down",+ "Model & fallbacks": "Model & fallbacks",+ "Fallbacks are tried in this order when the main model fails.": "Fallbacks are tried in this order when the main model fails.",+ "Model actions": "Model actions",+ "Change main model": "Change main model",+ "Main model": "Main model",+ "No fallback model": "No fallback model",+ "A fallback is tried when the main model fails. Without one, this tier fails if {model} is unavailable.": "A fallback is tried when the main model fails. Without one, this tier fails if {model} is unavailable.",+ "Add fallback": "Add fallback",+ "Up to 4 fallbacks": "Up to 4 fallbacks",+ "Edit tier": "Edit tier",+ "Builders see this name and emoji when they pick a tier.": "Builders see this name and emoji when they pick a tier.",+ "Emoji": "Emoji",+ "e.g. Expert": "e.g. Expert",+ "Pick an emoji": "Pick an emoji",+ "More emojis": "More emojis",+ "e.g. Best for everyday use": "e.g. Best for everyday use",+ "Pick a model": "Pick a model",+ "Thinking": "Thinking",+ "Custom ({budget} tokens)": "Custom ({budget} tokens)",+ "Used only by models that support reasoning.": "Used only by models that support reasoning.",+ "Save tier": "Save tier",+ "Main model \u2014 tried first": "Main model \u2014 tried first",+ "Fallback {rank} \u2014 tried when the models above fail": "Fallback {rank} \u2014 tried when the models above fail",+ "Context": "Context",+ "Price per 1M": "Price per 1M",+ "Warnings": "Warnings",+ "Key not found \u2014 this model is skipped": "Key not found \u2014 this model is skipped",+ "Not in {key}'s model list anymore": "Not in {key}'s model list anymore",+ "Smaller context ({context}) than the main model ({main})": "Smaller context ({context}) than the main model ({main})",+ "Key status: {status} \u2014 tried last": "Key status: {status} \u2014 tried last",+ "Low (5,000 tokens)": "Low (5,000 tokens)",+ "Medium (10,000 tokens)": "Medium (10,000 tokens)",+ "High (20,000 tokens)": "High (20,000 tokens)",+ "Thinking: {budget}": "Thinking: {budget}",+ "Thinking: {level}": "Thinking: {level}",+ "Low": "Low",+ "Medium": "Medium",+ "High": "High",+ "Show all {count}": "Show all {count}",+ "No text models on your keys yet": "No text models on your keys yet",+ "Tier created": "Tier created",+ "Tier deleted": "Tier deleted",+ "Could not create this tier": "Could not create this tier",+ "Specific models are visible to builders": "Specific models are visible to builders",+ "Specific models are hidden from builders": "Specific models are hidden from builders",+ "Loading models\u2026": "Loading models\u2026",+ "Couldn't load models": "Couldn't load models",+ "No text models on this key.": "No text models on this key.",+ "Visible": "Visible",+ "Hidden": "Hidden",+ "Builders can pick these directly.": "Builders can pick these directly.",+ "Builders only see tiers.": "Builders only see tiers.",+ "Every text model on this key is a tier's main model.": "Every text model on this key is a tier's main model.",+ "Preview as project": "Preview as project",+ "As {project}": "As {project}",+ "Recommended for chat": "Recommended for chat",+ "Not offered in chat or agents — {model} can't call tools": "Not offered in chat or agents — {model} can't call tools",+ "Not available in this project — its main model's key doesn't serve it": "Not available in this project — its main model's key doesn't serve it",+ "tierUnavailableInProjects": "{count, plural, =1 {Not available in 1 project — its main model's key doesn't serve it} other {Not available in # projects — its main model's key doesn't serve them}}",+ "fallbackSkippedInProjects": "{count, plural, =1 {Fallback {model} is skipped in 1 project} other {Fallback {model} is skipped in # projects}}",+ "Skipped in this project": "Skipped in this project",+ "{key} only serves some projects. This model doesn't run in the others.": "{key} only serves some projects. This model doesn't run in the others.",+ "hidingLeavesProjectsEmpty": "{count, plural, =1 {Hiding these leaves 1 project with nothing to pick} other {Hiding these leaves # projects with nothing to pick}}",+ "projectsWithNothingToPick": "{count, plural, =1 {1 project has nothing to pick} other {# projects have nothing to pick}}",+ "Change which projects can use {name}?": "Change which projects can use {name}?",+ "Save anyway": "Save anyway",+ "tierLosesProjects": "{count, plural, =1 {{tier} won't be available in 1 project} other {{tier} won't be available in # projects}}",+ "tierFallbackSkippedInMoreProjects": "{count, plural, =1 {A fallback in {tier} will be skipped in 1 more project} other {A fallback in {tier} will be skipped in # more projects}}",+ "Could not reorder tiers": "Could not reorder tiers","Retention: {period}": "Retention: {period}","Audit log retention": "Audit log retention","Events older than the retention period are deleted every hour.": "Events older than the retention period are deleted every hour.",diff --git a/packages/web/src/app/routes/platform-routes.tsx b/packages/web/src/app/routes/platform-routes.tsxindex 5b9b93803a4..6403b4c4154 100644--- a/packages/web/src/app/routes/platform-routes.tsx+++ b/packages/web/src/app/routes/platform-routes.tsx@@ -102,7 +102,11 @@ const PlatformConnectionsPage = React.lazy(const HEALTH_TAB_PATHS = { system: '', runs: 'runs', queue: 'queue' };const WORKERS_TAB_PATHS = { health: '', 'worker-groups': 'groups' };-const AI_TAB_PATHS = { providers: '', capabilities: 'capabilities' };+const AI_TAB_PATHS = {+ providers: '',+ capabilities: 'capabilities',+ tiers: 'tiers',+};const PIECES_TAB_PATHS = { pieces: '', 'piece-sets': 'piece-sets' };export const platformRoutes = [@@ -184,6 +188,22 @@ export const platformRoutes = [</PlatformLayout>),},+ {+ path: '/platform/ai/tiers',+ element: (+ <PlatformLayout>+ <PageTitle title="AI Tiers">+ <LegacyTabRedirect basePath="/platform/ai" tabPaths={AI_TAB_PATHS}>+ <PlanFeatureSample feature="aiProviders">+ <SuspenseWrapper>+ <AIProvidersPage section="tiers" />+ </SuspenseWrapper>+ </PlanFeatureSample>+ </LegacyTabRedirect>+ </PageTitle>+ </PlatformLayout>+ ),+ },{path: '/platform/mcp',element: (diff --git a/packages/web/src/app/routes/platform/setup/ai/index.tsx b/packages/web/src/app/routes/platform/setup/ai/index.tsxindex 6ab5d379bae..3c56f88df8a 100644--- a/packages/web/src/app/routes/platform/setup/ai/index.tsx+++ b/packages/web/src/app/routes/platform/setup/ai/index.tsx@@ -5,6 +5,7 @@ import { flagsHooks } from '@/hooks/flags-hooks';import { CapabilitiesTab } from './capabilities-tab';import { ProvidersTab } from './providers-tab';+import { TiersTab } from './tiers-tab';export default function AIProvidersPage({ section }: AIProvidersPageProps) {return <AICenter section={section} />;@@ -24,6 +25,8 @@ function AICenter({ section }: { section: AISection }) {<div className="flex flex-1 flex-col"><ProvidersTab /></div>+ ) : section === 'tiers' ? (+ <TiersTab />) : (<CapabilitiesTab />)}@@ -32,7 +35,7 @@ function AICenter({ section }: { section: AISection }) {);}-type AISection = 'providers' | 'capabilities';+type AISection = 'providers' | 'capabilities' | 'tiers';type AIProvidersPageProps = {section: AISection;diff --git a/packages/web/src/app/routes/platform/setup/ai/providers-tab/config-detail.tsx b/packages/web/src/app/routes/platform/setup/ai/providers-tab/config-detail.tsxindex adcf533a2ea..7bb20a7bc0a 100644--- a/packages/web/src/app/routes/platform/setup/ai/providers-tab/config-detail.tsx+++ b/packages/web/src/app/routes/platform/setup/ai/providers-tab/config-detail.tsx@@ -33,7 +33,13 @@ import {SelectValue,} from '@/components/ui/select';import { AiProviderInfo } from '@/features/agents';+import {+ KeyScopeImpact,+ modelMeta,+} from '@/features/agents/ai-model/model-meta';import { aiProviderApi, aiProviderKeys } from '@/features/platform-admin';+import { aiProviderQueries } from '@/features/platform-admin/hooks/ai-provider-hooks';+import { platformModelTierQueries } from '@/features/platform-admin/hooks/platform-model-tier-hooks';import { AdminControl, adminControl } from '@/lib/admin-control';import { formatUtils } from '@/lib/format-utils';@@ -62,7 +68,7 @@ export function ConfigDetail({info: AiProviderInfo;projects: Project[];isSaving: boolean;- onSave: (request: UpdateAIProviderRequest) => Promise<unknown>;+ onSave: (request: UpdateAIProviderRequest) => Promise<{ error: unknown }>;onDelete: () => Promise<unknown>;onReplaceCredentials: () => void;isRechecking: boolean;@@ -71,6 +77,9 @@ export function ConfigDetail({}) {const [draft, setDraft] = useState<ConfigDraft>(draftOf(config));const [deleteOpen, setDeleteOpen] = useState(false);+ const [scopeImpact, setScopeImpact] = useState<KeyScopeImpact[]>([]);+ const { data: tiers } = platformModelTierQueries.useAdminList();+ const { data: allConfigs } = aiProviderQueries.useAiProviderConfigs();const leavingOnPurpose = useRef(false);const saveInFlight = useRef(false);@@ -120,7 +129,30 @@ export function ConfigDetail({? projects.length - draft.projectIds.length: draft.projectIds.length;- const save = async () => {+ const requestSave = () => {+ if (nameMissing || saveInFlight.current) {+ return;+ }+ const impact = modelMeta.keyScopeImpact({+ tiers: tiers ?? [],+ configsById: new Map(+ (allConfigs ?? []).map((other) => [other.id, other]),+ ),+ config: {+ ...config,+ projectScope: draft.projectScope,+ projectIds: draft.projectIds,+ },+ projectIds: projects.map((project) => project.id),+ });+ if (impact.length > 0) {+ setScopeImpact(impact);+ return;+ }+ return save();+ };++ const save = async (): Promise<boolean> => {const manualConfigParse = manualModels? ManualProviderConfig.safeParse(config.config): undefined;@@ -128,11 +160,11 @@ export function ConfigDetail({? manualConfigParse.data: undefined;if (nameMissing || saveInFlight.current) {- return;+ return false;}saveInFlight.current = true;try {- await onSave({+ const { error } = await onSave({displayName: draft.name.trim(),modelScope: draft.modelScope,modelIds: manualModels@@ -146,6 +178,7 @@ export function ConfigDetail({}: {}),});+ return error === null || error === undefined;} finally {saveInFlight.current = false;}@@ -394,8 +427,8 @@ export function ConfigDetail({loading={isSaving}disabled={nameMissing || isSaving}keyboardShortcut="S"- onKeyboardShortcut={save}- onClick={save}+ onKeyboardShortcut={requestSave}+ onClick={requestSave}{...adminControl(AdminControl.AI_PROVIDER_KEY_SETTINGS_SUBMIT)}>{t('Save')}@@ -404,6 +437,34 @@ export function ConfigDetail({</div>)}+ <ConfirmationDeleteDialog+ open={scopeImpact.length > 0}+ onOpenChange={(open) => {+ if (!open) {+ setScopeImpact([]);+ }+ }}+ title={t('Change which projects can use {name}?', {+ name: config.name,+ })}+ message={+ <ul className="flex list-disc flex-col gap-1 pl-5">+ {scopeImpact.map((impact) => (+ <li key={impact.tierName}>{scopeImpactText(impact)}</li>+ ))}+ </ul>+ }+ entityName={config.name}+ isDanger={false}+ buttonText={t('Save anyway')}+ showToast={false}+ mutationFn={async () => {+ if (!(await save())) {+ throw new Error('Saving the key failed');+ }+ }}+ />+<LeaveWithoutSavingDialogopen={leaveBlocker.state === 'blocked'}onKeepEditing={() => leaveBlocker.reset?.()}@@ -413,6 +474,19 @@ export function ConfigDetail({);}+function scopeImpactText(impact: KeyScopeImpact): string {+ if (impact.lostProjects > 0) {+ return t('tierLosesProjects', {+ tier: impact.tierName,+ count: impact.lostProjects,+ });+ }+ return t('tierFallbackSkippedInMoreProjects', {+ tier: impact.tierName,+ count: impact.skippedProjects,+ });+}+function ScopeTabs({value,onChange,diff --git a/packages/web/src/app/routes/platform/setup/ai/tiers-tab/admin-model-picker.tsx b/packages/web/src/app/routes/platform/setup/ai/tiers-tab/admin-model-picker.tsxnew file mode 100644index 00000000000..3b2a65f3ed3--- /dev/null+++ b/packages/web/src/app/routes/platform/setup/ai/tiers-tab/admin-model-picker.tsx@@ -0,0 +1,250 @@+import {+ AIProviderModel,+ AIProviderWithoutSensitiveData,+ PlatformModelTierEntry,+} from '@activepieces/shared';+import { t } from 'i18next';+import { RefreshCw } from 'lucide-react';+import { ReactNode } from 'react';++import { LogoPlate } from '@/components/custom/logo-plate';+import { Button } from '@/components/ui/button';+import { Skeleton } from '@/components/ui/skeleton';+import {+ KeyModelsById,+ modelMeta,+ PickableModel,+ RankedGroup,+ RankedModel,+ TradeOff,+} from '@/features/agents/ai-model/model-meta';+import {+ ModelPickerGroup,+ ModelPickerItem,+ ModelPickerPopover,+} from '@/features/agents/ai-model/model-picker-popover';+import { cn } from '@/lib/utils';++import { KeyStatusBadge } from '../providers-tab/key-status';+import { ProviderLogo } from '../providers-tab/provider-logo';++export function AdminModelPicker({+ configs,+ keyModels,+ exclude,+ main,+ mode,+ onPick,+ open,+ onOpenChange,+ align = 'end',+ anchorOnly = false,+ children,+}: AdminModelPickerProps) {+ const pickable: PickableModel[] = configs.flatMap((config) =>+ (keyModels[config.id]?.models ?? []).map((model) => ({ config, model })),+ );+ const toItem = (+ item: RankedModel,+ { showKey }: { showKey: boolean },+ ): ModelPickerItem<PlatformModelTierEntry> => {+ const entry = { configId: item.config.id, modelId: item.model.id };+ const taken = exclude.some((other) => modelMeta.sameEntry(other, entry));+ const info = modelMeta.providerInfoOf({ provider: item.config.provider });+ return {+ id: modelMeta.entryKey({ entry }),+ value: entry,+ name: item.model.name,+ searchText: `${item.model.name} ${item.model.id} ${item.config.name} ${item.config.provider}`,+ subtitle: showKey ? item.config.name : undefined,+ leading:+ info.logoUrl === '' ? undefined : (+ <LogoPlate src={info.logoUrl} alt={info.name} size="xxs" />+ ),+ model: item.model,+ note:+ item.tradeOffs.length === 0+ ? undefined+ : item.tradeOffs.map(tradeOffText).join(' · '),+ disabled: taken,+ selected: taken,+ };+ };+ const groups: ModelPickerGroup<PlatformModelTierEntry>[] =+ mode === 'fallback'+ ? modelMeta+ .rankForFallback({ main, candidates: pickable })+ .map((group) => ({+ id: group.kind,+ heading: rankedHeading({ kind: group.kind }),+ items: group.items.map((item) => toItem(item, { showKey: true })),+ }))+ : configs.flatMap((config) => {+ const items = pickable+ .filter((item) => item.config.id === config.id)+ .map((item) =>+ toItem({ ...item, tradeOffs: [] }, { showKey: false }),+ );+ return items.length === 0+ ? []+ : [+ {+ id: config.id,+ heading: <KeyHeading config={config} />,+ items,+ },+ ];+ });+ const loadingKeys = configs.filter(+ (config) => keyModels[config.id]?.isLoading === true,+ );+ const failedKeys = configs.filter(+ (config) => keyModels[config.id]?.isError === true,+ );++ return (+ <ModelPickerPopover+ groups={groups}+ notices={+ <KeyNotices+ loadingKeys={loadingKeys}+ failedKeys={failedKeys}+ keyModels={keyModels}+ />+ }+ emptyText={emptyTextOf({+ total: configs.length,+ loading: loadingKeys.length,+ failed: failedKeys.length,+ pickable: pickable.length,+ })}+ onPick={onPick}+ open={open}+ onOpenChange={onOpenChange}+ align={align}+ anchorOnly={anchorOnly}+ >+ {children}+ </ModelPickerPopover>+ );+}++function KeyHeading({ config }: { config: AIProviderWithoutSensitiveData }) {+ return (+ <span className="flex items-center gap-2">+ <ProviderLogo+ info={modelMeta.providerInfoOf({ provider: config.provider })}+ size="sm"+ />+ <span>{config.name}</span>+ {config.status !== 'active' && <KeyStatusBadge status={config.status} />}+ </span>+ );+}++function KeyNotices({+ loadingKeys,+ failedKeys,+ keyModels,+}: {+ loadingKeys: AIProviderWithoutSensitiveData[];+ failedKeys: AIProviderWithoutSensitiveData[];+ keyModels: KeyModelsById;+}) {+ if (loadingKeys.length === 0 && failedKeys.length === 0) {+ return null;+ }+ return (+ <div className="flex flex-col gap-1.5 border-b border-gray-6/60 px-3 py-2">+ {loadingKeys.map((config) => (+ <div key={config.id} className="flex items-center gap-2">+ <Skeleton className="size-4 rounded-sm" />+ <Skeleton className="h-3 w-40" />+ </div>+ ))}+ {failedKeys.map((config) => (+ <div+ key={config.id}+ className="flex items-center justify-between gap-2 text-xs text-gray-11"+ >+ <span>{t("Couldn't load {key} models", { key: config.name })}</span>+ <Button+ type="button"+ variant="ghost"+ size="icon-xs"+ aria-label={t('Retry')}+ disabled={keyModels[config.id]?.isFetching === true}+ onClick={() => keyModels[config.id]?.refetch()}+ >+ <RefreshCw+ className={cn(+ 'size-3',+ keyModels[config.id]?.isFetching === true && 'animate-spin',+ )}+ />+ </Button>+ </div>+ ))}+ </div>+ );+}++function emptyTextOf({+ total,+ loading,+ failed,+ pickable,+}: {+ total: number;+ loading: number;+ failed: number;+ pickable: number;+}): string {+ if (total > 0 && loading === total) {+ return t('Loading models…');+ }+ if (total > 0 && failed === total) {+ return t("Couldn't load models");+ }+ return pickable === 0+ ? t('No text models on your keys yet')+ : t('No models match');+}++function rankedHeading({ kind }: { kind: RankedGroup['kind'] }): string {+ switch (kind) {+ case 'full':+ return t('Full matches');+ case 'tradeOff':+ return t('With trade-offs');+ case 'other':+ return t('Other models');+ default:+ return t('Models');+ }+}++function tradeOffText(tradeOff: TradeOff): string {+ switch (tradeOff.code) {+ case 'smallerContext':+ return t('Smaller context ({context})', {+ context: modelMeta.formatContext({ tokens: tradeOff.contextTokens }),+ });+ case 'noToolCalling':+ return t('No tool calling');+ }+}++type AdminModelPickerProps = {+ configs: AIProviderWithoutSensitiveData[];+ keyModels: KeyModelsById;+ exclude: PlatformModelTierEntry[];+ main?: AIProviderModel;+ mode: 'main' | 'fallback';+ onPick: (entry: PlatformModelTierEntry) => void;+ open: boolean;+ onOpenChange: (open: boolean) => void;+ align?: 'start' | 'end';+ anchorOnly?: boolean;+ children: ReactNode;+};diff --git a/packages/web/src/app/routes/platform/setup/ai/tiers-tab/delete-tier-dialog.tsx b/packages/web/src/app/routes/platform/setup/ai/tiers-tab/delete-tier-dialog.tsxnew file mode 100644index 00000000000..83aacc3cf9f--- /dev/null+++ b/packages/web/src/app/routes/platform/setup/ai/tiers-tab/delete-tier-dialog.tsx@@ -0,0 +1,205 @@+import { PlatformModelTier } from '@activepieces/shared';+import { useQueryClient } from '@tanstack/react-query';+import { t } from 'i18next';+import { TriangleAlert } from 'lucide-react';+import { useState } from 'react';++import { Button } from '@/components/ui/button';+import {+ Dialog,+ DialogContent,+ DialogDescription,+ DialogFooter,+ DialogHeader,+ DialogTitle,+} from '@/components/ui/dialog';+import { Label } from '@/components/ui/label';+import {+ Select,+ SelectContent,+ SelectItem,+ SelectTrigger,+ SelectValue,+} from '@/components/ui/select';+import { platformModelTierMutations } from '@/features/platform-admin/hooks/platform-model-tier-hooks';+import { platformConfigurationHooks } from '@/hooks/platform-configuration-hooks';+import { AdminControl, adminControl } from '@/lib/admin-control';++export function DeleteTierDialog({+ tier,+ tiers,+ specificModelsVisible,+ onOpenChange,+ returnFocusTo,+ onReturnFocus,+}: DeleteTierDialogProps) {+ return (+ <Dialog open={tier !== null} onOpenChange={onOpenChange}>+ {tier !== null && (+ <DeleteTierForm+ key={tier.id}+ tier={tier}+ tiers={tiers}+ specificModelsVisible={specificModelsVisible}+ onClose={() => onOpenChange(false)}+ onReturnFocus={() => onReturnFocus({ target: returnFocusTo ?? null })}+ />+ )}+ </Dialog>+ );+}++function DeleteTierForm({+ tier,+ tiers,+ specificModelsVisible,+ onClose,+ onReturnFocus,+}: {+ tier: PlatformModelTier;+ tiers: PlatformModelTier[];+ specificModelsVisible: boolean;+ onClose: () => void;+ onReturnFocus: () => void;+}) {+ const queryClient = useQueryClient();+ const replacements = tiers.filter((candidate) => candidate.id !== tier.id);+ const isLast = replacements.length === 0;+ const [replacedBy, setReplacedBy] = useState<string | undefined>(undefined);+ const { mutate: remove, isPending: busy } =+ platformModelTierMutations.useDelete();+ const replacement = replacements.find(+ (candidate) => candidate.id === replacedBy,+ );+ const needsFlip = isLast && !specificModelsVisible;++ const keepOpen = (event: Event) => {+ if (busy) {+ event.preventDefault();+ }+ };++ const confirm = () =>+ remove(+ { id: tier.id, replacedBy },+ {+ onSuccess: () => {+ queryClient+ .invalidateQueries({+ queryKey: platformConfigurationHooks.queryKey,+ })+ .catch(() => undefined);+ onClose();+ },+ },+ );++ return (+ <DialogContent+ className="sm:max-w-md"+ showCloseButton={!busy}+ onEscapeKeyDown={keepOpen}+ onPointerDownOutside={keepOpen}+ onInteractOutside={keepOpen}+ onCloseAutoFocus={(event) => {+ event.preventDefault();+ onReturnFocus();+ }}+ >+ <div className="flex flex-col gap-5">+ <DialogHeader>+ <DialogTitle>{t('Delete {name}?', { name: tier.name })}</DialogTitle>+ <DialogDescription>+ {isLast+ ? t('This is your last tier.')+ : t('Anything using {name} moves to the tier you pick.', {+ name: tier.name,+ })}+ </DialogDescription>+ </DialogHeader>++ {isLast ? (+ <div className="flex flex-col gap-2 rounded-md border border-warning-7 bg-warning-2 p-3 text-sm text-warning-11">+ <p className="flex items-start gap-2">+ <TriangleAlert className="mt-0.5 size-4 shrink-0" />+ <span>+ {t(+ 'Anything still using {name} will stop working until someone picks a new model.',+ { name: tier.name },+ )}+ </span>+ </p>+ {needsFlip && (+ <p className="pl-6">+ {t(+ 'Specific models will be shown to builders again, so they still have something to pick.',+ )}+ </p>+ )}+ </div>+ ) : (+ <div className="flex flex-col gap-1.5">+ <Label htmlFor="tier-replacement">{t('Move them to')}</Label>+ <Select value={replacedBy} onValueChange={setReplacedBy}>+ <SelectTrigger id="tier-replacement">+ <SelectValue placeholder={t('Pick a tier')} />+ </SelectTrigger>+ <SelectContent>+ {replacements.map((candidate) => (+ <SelectItem key={candidate.id} value={candidate.id}>+ <span className="flex items-center gap-2">+ <span aria-hidden="true">{candidate.emoji}</span>+ <span>{candidate.name}</span>+ </span>+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ {replacement !== undefined && tier.isDefault && (+ <p className="text-xs text-gray-11">+ {t('{name} becomes the default tier.', {+ name: replacement.name,+ })}+ </p>+ )}+ {replacement !== undefined && tier.isFast && (+ <p className="text-xs text-gray-11">+ {t('{name} becomes the fast tier.', { name: replacement.name })}+ </p>+ )}+ </div>+ )}++ <DialogFooter>+ <Button+ type="button"+ variant="outline"+ onClick={onClose}+ disabled={busy}+ >+ {t('Cancel')}+ </Button>+ <Button+ type="button"+ variant="destructive"+ loading={busy}+ disabled={!isLast && replacedBy === undefined}+ onClick={confirm}+ {...adminControl(AdminControl.AI_TIER_DELETE_CONFIRM)}+ >+ {t('Delete tier')}+ </Button>+ </DialogFooter>+ </div>+ </DialogContent>+ );+}++type DeleteTierDialogProps = {+ tier: PlatformModelTier | null;+ tiers: PlatformModelTier[];+ specificModelsVisible: boolean;+ onOpenChange: (open: boolean) => void;+ returnFocusTo?: HTMLElement | null;+ onReturnFocus: (args: { target: HTMLElement | null }) => void;+};diff --git a/packages/web/src/app/routes/platform/setup/ai/tiers-tab/index.tsx b/packages/web/src/app/routes/platform/setup/ai/tiers-tab/index.tsxnew file mode 100644index 00000000000..82452a8b107--- /dev/null+++ b/packages/web/src/app/routes/platform/setup/ai/tiers-tab/index.tsx@@ -0,0 +1,418 @@+import {+ PlatformModelTier,+ PlatformModelTierEntry,+} from '@activepieces/shared';+import { t } from 'i18next';+import { Layers, Plus } from 'lucide-react';+import { AnimatePresence, motion, useReducedMotion } from 'motion/react';+import { useMemo, useState } from 'react';+import { Link } from 'react-router-dom';++import { DataFetchErrorState } from '@/components/custom/data-fetch-error-state';+import { Button } from '@/components/ui/button';+import {+ Select,+ SelectContent,+ SelectItem,+ SelectTrigger,+ SelectValue,+} from '@/components/ui/select';+import { Skeleton } from '@/components/ui/skeleton';+import { modelMeta } from '@/features/agents/ai-model/model-meta';+import { aiProviderQueries } from '@/features/platform-admin/hooks/ai-provider-hooks';+import {+ platformModelTierMutations,+ platformModelTierQueries,+} from '@/features/platform-admin/hooks/platform-model-tier-hooks';+import { projectCollectionUtils } from '@/features/projects';+import { platformConfigurationHooks } from '@/hooks/platform-configuration-hooks';+import { AdminControl, adminControl } from '@/lib/admin-control';++import { SectionHeader } from '../components/section-header';++import { DeleteTierDialog } from './delete-tier-dialog';+import { SpecificModelsSection } from './specific-models-section';+import { TierCard } from './tier-card';+import { TierDialog, TierDialogState } from './tier-dialog';++export function TiersTab() {+ const reducedMotion = useReducedMotion() ?? false;+ const {+ data: tiers,+ isLoading: tiersLoading,+ isError: tiersError,+ refetch: refetchTiers,+ } = platformModelTierQueries.useAdminList();+ const {+ data: configs,+ isLoading: configsLoading,+ isError: configsError,+ refetch: refetchConfigs,+ } = aiProviderQueries.useAiProviderConfigs();+ const { data: configuration, isLoading: configurationLoading } =+ platformConfigurationHooks.useCurrentPlatformConfiguration();+ const allConfigs = useMemo(() => configs ?? [], [configs]);+ const keyModels = platformModelTierQueries.useKeyModels(allConfigs);+ const { mutate: reorder, isPending: reordering } =+ platformModelTierMutations.useReorder();+ const [dialog, setDialog] = useState<TierDialogState>({ open: false });+ const [deleting, setDeleting] = useState<PlatformModelTier | null>(null);+ const [focusTarget, setFocusTarget] = useState<HTMLElement | null>(null);+ const [previewProjectId, setPreviewProjectId] = useState<string | null>(null);+ const { data: projects } = projectCollectionUtils.useAllPlatformProjects();+ const projectIds = useMemo(+ () => projects.map((project) => project.id),+ [projects],+ );++ const ownKeys = useMemo(+ () => allConfigs.filter(modelMeta.isOwnKey),+ [allConfigs],+ );+ const configsById = useMemo(+ () => new Map(allConfigs.map((config) => [config.id, config])),+ [allConfigs],+ );+ const liveTiers = tiers ?? [];+ const specificModelsVisible = configuration?.aiSpecificModelsVisible ?? true;+ const someKeyIsScoped = ownKeys.some((key) => key.projectScope !== 'all');+ const showPreview = someKeyIsScoped && projects.length > 1;++ const openCreate = ({+ trigger,+ initialMain,+ }: {+ trigger: HTMLElement | null;+ initialMain?: PlatformModelTierEntry;+ }) => {+ setFocusTarget(trigger);+ setDialog({ open: true, mode: 'create', initialMain });+ };++ if (tiersLoading || configsLoading || configurationLoading) {+ return <TiersSkeleton />;+ }++ const isError =+ (tiersError && tiers === undefined) ||+ (configsError && configs === undefined);+ const noKeys = ownKeys.length === 0 && liveTiers.length === 0;+ const retry = () => Promise.all([refetchTiers(), refetchConfigs()]);++ return (+ <div className="flex flex-col gap-6">+ <div className="flex items-start justify-between gap-3">+ <SectionHeader+ title={t('Tiers')}+ isPageTitle+ count={isError ? undefined : liveTiers.length}+ description={t(+ 'Named model groups your builders pick. Each one is a main model plus fallbacks.',+ )}+ />+ {!isError && !noKeys && (+ <div className="flex shrink-0 items-center gap-2">+ {showPreview && (+ <Select+ value={previewProjectId ?? ALL_PROJECTS}+ onValueChange={(value) =>+ setPreviewProjectId(value === ALL_PROJECTS ? null : value)+ }+ >+ <SelectTrigger+ size="sm"+ className="w-48"+ aria-label={t('Preview as project')}+ >+ <SelectValue />+ </SelectTrigger>+ <SelectContent align="end">+ <SelectItem value={ALL_PROJECTS}>+ {t('All projects')}+ </SelectItem>+ {projects.map((project) => (+ <SelectItem key={project.id} value={project.id}>+ {t('As {project}', { project: project.displayName })}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ )}+ <Button+ id={NEW_TIER_BUTTON_ID}+ size="sm"+ className="shrink-0"+ onClick={(event) => openCreate({ trigger: event.currentTarget })}+ {...adminControl(AdminControl.AI_TIER_OPEN)}+ >+ <Plus className="size-4" />+ {t('New tier')}+ </Button>+ </div>+ )}+ </div>++ {isError ? (+ <DataFetchErrorState entity={t('tiers')} onRetry={retry} />+ ) : noKeys ? (+ <NoKeysState />+ ) : (+ <>+ {liveTiers.length === 0 ? (+ <NoTiersState onCreate={(trigger) => openCreate({ trigger })} />+ ) : (+ <div className="flex flex-col gap-4">+ <AnimatePresence initial={false}>+ {liveTiers.map((tier, position) => (+ <motion.div+ key={tier.id}+ layout={!reducedMotion}+ initial={reducedMotion ? false : { opacity: 0, y: 6 }}+ animate={{ opacity: 1, y: 0 }}+ exit={reducedMotion ? undefined : { opacity: 0, height: 0 }}+ transition={{ duration: 0.18 }}+ >+ <TierCard+ tier={tier}+ configsById={configsById}+ ownKeys={ownKeys}+ keyModels={keyModels}+ projectIds={projectIds}+ previewProjectId={previewProjectId}+ reducedMotion={reducedMotion}+ onEdit={(trigger) => {+ setFocusTarget(trigger);+ setDialog({ open: true, mode: 'edit', tier });+ }}+ onDelete={(trigger) => {+ setFocusTarget(trigger);+ setDeleting(tier);+ }}+ onMoveUp={+ position === 0 || reordering+ ? undefined+ : () =>+ reorder({+ tierIds: movedIds({+ tiers: liveTiers,+ from: position,+ to: position - 1,+ }),+ })+ }+ onMoveDown={+ position === liveTiers.length - 1 || reordering+ ? undefined+ : () =>+ reorder({+ tierIds: movedIds({+ tiers: liveTiers,+ from: position,+ to: position + 1,+ }),+ })+ }+ />+ </motion.div>+ ))}+ </AnimatePresence>+ </div>+ )}+ <SpecificModelsSection+ tiers={liveTiers}+ ownKeys={ownKeys}+ keyModels={keyModels}+ visible={specificModelsVisible}+ projectsWithoutTier={modelMeta.projectsWithoutTier({+ tiers: liveTiers,+ configsById,+ projectIds,+ })}+ onMakeTier={(entry) =>+ openCreate({+ trigger:+ document.activeElement instanceof HTMLElement+ ? document.activeElement+ : null,+ initialMain: entry,+ })+ }+ />+ </>+ )}++ <TierDialog+ state={dialog}+ onOpenChange={(open) => {+ if (!open) {+ setDialog({ open: false });+ }+ }}+ ownKeys={ownKeys}+ keyModels={keyModels}+ returnFocusTo={focusTarget}+ onReturnFocus={focusBack}+ />+ <DeleteTierDialog+ tier={deleting}+ tiers={liveTiers}+ specificModelsVisible={specificModelsVisible}+ onOpenChange={(open) => {+ if (!open) {+ setDeleting(null);+ }+ }}+ returnFocusTo={focusTarget}+ onReturnFocus={focusBack}+ />+ </div>+ );+}++function movedIds({+ tiers,+ from,+ to,+}: {+ tiers: PlatformModelTier[];+ from: number;+ to: number;+}): string[] {+ const ids = tiers.map((tier) => tier.id);+ const without = ids.filter((_, index) => index !== from);+ return [...without.slice(0, to), ids[from], ...without.slice(to)];+}++function focusBack({ target }: { target: HTMLElement | null }): void {+ const alive = target !== null && document.contains(target) ? target : null;+ (alive ?? document.getElementById(NEW_TIER_BUTTON_ID))?.focus();+}++function NoKeysState() {+ return (+ <div className="flex flex-col items-center gap-4 rounded-xl border border-gray-6/60 bg-panel px-6 py-14 text-center">+ <div className="flex size-12 items-center justify-center rounded-xl bg-accent-3">+ <Layers className="size-5 text-accent-11" />+ </div>+ <div className="flex flex-col gap-1">+ <p className="text-base font-semibold tracking-tight">+ {t('Add a provider key first')}+ </p>+ <p className="max-w-md text-sm text-gray-11">+ {t("Tiers are built from your keys' models.")}+ </p>+ </div>+ <Button asChild variant="outline">+ <Link to="/platform/ai">{t('Go to Providers')}</Link>+ </Button>+ </div>+ );+}++function NoTiersState({+ onCreate,+}: {+ onCreate: (trigger: HTMLElement | null) => void;+}) {+ return (+ <div className="flex flex-col items-center gap-4 rounded-xl border border-dashed border-gray-6 bg-panel px-6 py-14 text-center">+ <div className="flex size-12 items-center justify-center rounded-xl bg-accent-3">+ <Layers className="size-5 text-accent-11" />+ </div>+ <div className="flex flex-col gap-1">+ <p className="text-base font-semibold tracking-tight">+ {t('Create your first tier')}+ </p>+ <p className="max-w-md text-sm text-gray-11">+ {t('Name it, pick a main model, add fallbacks.')}+ </p>+ </div>+ <Button+ onClick={(event) => onCreate(event.currentTarget)}+ {...adminControl(AdminControl.AI_TIER_OPEN)}+ >+ <Plus className="size-4" />+ {t('New tier')}+ </Button>+ </div>+ );+}++function TiersSkeleton() {+ return (+ <div+ className="flex flex-col gap-6"+ aria-busy="true"+ role="status"+ aria-label={t('Loading tiers')}+ >+ <div className="flex items-start justify-between gap-3">+ <div className="flex flex-col gap-2">+ <Skeleton className="h-5 w-20" />+ <Skeleton className="h-4 w-80" />+ </div>+ <Skeleton className="h-8 w-24 rounded-md" />+ </div>+ {[0, 1].map((card) => (+ <section+ key={card}+ className="rounded-xl border border-gray-6/60 bg-panel shadow-panel"+ >+ <div className="flex items-center gap-3 px-5 py-4">+ <Skeleton className="size-10 shrink-0 rounded-lg" />+ <div className="flex flex-1 flex-col gap-2">+ <Skeleton className="h-4 w-24" />+ <Skeleton className="h-3 w-40" />+ </div>+ <Skeleton className="size-8 rounded-md" />+ </div>+ <div className="flex flex-col border-t border-gray-6/60 px-3 pb-3 pt-3">+ <Skeleton className="mx-2 mb-2 h-3 w-28" />+ {[0, 1, 2].map((row) => (+ <div key={row} className="flex items-center gap-3 px-2 py-2.5">+ <Skeleton className="size-2 shrink-0 rounded-full" />+ <Skeleton className="size-8 shrink-0 rounded-lg" />+ <div className="flex flex-1 flex-col gap-1.5">+ <Skeleton className="h-3.5 w-40" />+ <Skeleton className="h-3 w-24" />+ </div>+ <div className="hidden items-center gap-4 md:flex">+ <Skeleton className="h-8 w-16" />+ <Skeleton className="h-8 w-28" />+ <Skeleton className="size-6 rounded-md" />+ </div>+ <Skeleton className="size-6 rounded-md" />+ </div>+ ))}+ <Skeleton className="mx-2 mt-1 h-8 w-28 rounded-md" />+ </div>+ </section>+ ))}+ <section className="rounded-xl border border-gray-6/60 bg-panel shadow-panel">+ <div className="flex items-start justify-between gap-3 px-5 py-4">+ <div className="flex flex-col gap-2">+ <Skeleton className="h-5 w-32" />+ <Skeleton className="h-3.5 w-56" />+ </div>+ <Skeleton className="h-8 w-40 rounded-md" />+ </div>+ {[0, 1].map((row) => (+ <div+ key={row}+ className="flex items-center gap-3 border-t border-gray-6/60 px-5 py-3"+ >+ <Skeleton className="size-8 shrink-0 rounded-lg" />+ <div className="flex flex-1 flex-col gap-1.5">+ <Skeleton className="h-3.5 w-32" />+ <Skeleton className="h-3 w-16" />+ </div>+ <Skeleton className="size-4 rounded-sm" />+ </div>+ ))}+ </section>+ </div>+ );+}++const NEW_TIER_BUTTON_ID = 'new-tier-button';+const ALL_PROJECTS = 'all-projects';diff --git a/packages/web/src/app/routes/platform/setup/ai/tiers-tab/model-detail-row.tsx b/packages/web/src/app/routes/platform/setup/ai/tiers-tab/model-detail-row.tsxnew file mode 100644index 00000000000..7bd945701f8--- /dev/null+++ b/packages/web/src/app/routes/platform/setup/ai/tiers-tab/model-detail-row.tsx@@ -0,0 +1,142 @@+import {+ AIProviderModel,+ AIProviderWithoutSensitiveData,+} from '@activepieces/shared';+import { t } from 'i18next';+import { Brain, Wrench } from 'lucide-react';+import { ReactNode } from 'react';++import { TextWithTooltip } from '@/components/custom/text-with-tooltip';+import { Skeleton } from '@/components/ui/skeleton';+import {+ Tooltip,+ TooltipContent,+ TooltipTrigger,+} from '@/components/ui/tooltip';+import { modelMeta } from '@/features/agents/ai-model/model-meta';+import { cn } from '@/lib/utils';++import { ProviderLogo } from '../providers-tab/provider-logo';++export function ModelDetailRow({+ name,+ config,+ model,+ nameSuffix,+ trailing,+ loading = false,+}: ModelDetailRowProps) {+ const metadata = model?.metadata;+ return (+ <div className="flex min-w-0 flex-1 items-center gap-3 py-2.5 pr-1">+ {config !== undefined && (+ <ProviderLogo+ info={modelMeta.providerInfoOf({ provider: config.provider })}+ />+ )}+ <div className="flex min-w-0 flex-1 flex-col">+ <div className="flex min-w-0 items-center gap-1.5">+ <TextWithTooltip tooltipMessage={name}>+ <span className="truncate text-sm font-medium">{name}</span>+ </TextWithTooltip>+ {nameSuffix}+ </div>+ {config !== undefined && (+ <span className="truncate text-xs text-gray-11">{config.name}</span>+ )}+ </div>+ {loading && model === undefined && (+ <div className="hidden items-center gap-4 md:flex">+ <Skeleton className="h-8 w-16" />+ <Skeleton className="h-8 w-28" />+ </div>+ )}+ {metadata !== undefined && (+ <div className="hidden items-center gap-4 md:flex">+ <Stat label={t('Context')} className="w-16">+ {metadata.contextTokens === undefined+ ? EMPTY+ : modelMeta.formatContext({ tokens: metadata.contextTokens })}+ </Stat>+ <Stat label={t('Price per 1M')} className="w-28">+ {metadata.inputCostPerMillionTokens === undefined ||+ metadata.outputCostPerMillionTokens === undefined ? (+ EMPTY+ ) : (+ <>+ {modelMeta.formatPrice({+ perMillion: metadata.inputCostPerMillionTokens,+ })}+ <span className="text-gray-10"> / </span>+ {modelMeta.formatPrice({+ perMillion: metadata.outputCostPerMillionTokens,+ })}+ </>+ )}+ </Stat>+ <Capabilities model={model} />+ </div>+ )}+ {trailing}+ </div>+ );+}++function Stat({+ label,+ className,+ children,+}: {+ label: string;+ className: string;+ children: ReactNode;+}) {+ return (+ <span className={cn('flex flex-col', className)}>+ <span className="text-xss uppercase tracking-wide text-gray-10">+ {label}+ </span>+ <span className="text-sm tabular-nums text-gray-12">{children}</span>+ </span>+ );+}++function Capabilities({ model }: { model: AIProviderModel | undefined }) {+ const metadata = model?.metadata;+ const items = [+ ...(metadata?.supportsToolCalling === true+ ? [{ icon: Wrench, label: t('Supports tool calling') }]+ : []),+ ...(metadata?.supportsReasoning === true+ ? [{ icon: Brain, label: t('Supports reasoning') }]+ : []),+ ];+ return (+ <span className="flex w-14 items-center gap-1">+ {items.map(({ icon: Icon, label }) => (+ <Tooltip key={label}>+ <TooltipTrigger asChild>+ <span+ className="inline-flex size-6 items-center justify-center rounded-md bg-gray-3 text-gray-11"+ aria-label={label}+ >+ <Icon className="size-3.5" />+ </span>+ </TooltipTrigger>+ <TooltipContent>{label}</TooltipContent>+ </Tooltip>+ ))}+ </span>+ );+}++const EMPTY = '—';++type ModelDetailRowProps = {+ name: string;+ config: AIProviderWithoutSensitiveData | undefined;+ model: AIProviderModel | undefined;+ nameSuffix?: ReactNode;+ trailing?: ReactNode;+ loading?: boolean;+};diff --git a/packages/web/src/app/routes/platform/setup/ai/tiers-tab/specific-models-section.tsx b/packages/web/src/app/routes/platform/setup/ai/tiers-tab/specific-models-section.tsxnew file mode 100644index 00000000000..2c08ce9bf8c--- /dev/null+++ b/packages/web/src/app/routes/platform/setup/ai/tiers-tab/specific-models-section.tsx@@ -0,0 +1,354 @@+import {+ AIProviderWithoutSensitiveData,+ PlatformModelTier,+ PlatformModelTierEntry,+} from '@activepieces/shared';+import { t } from 'i18next';+import {+ ChevronRight,+ Eye,+ EyeOff,+ Loader2,+ Plus,+ RefreshCw,+ TriangleAlert,+} from 'lucide-react';+import { useState } from 'react';++import { Button } from '@/components/ui/button';+import {+ Collapsible,+ CollapsibleContent,+ CollapsibleTrigger,+} from '@/components/ui/collapsible';+import { Input } from '@/components/ui/input';+import { Skeleton } from '@/components/ui/skeleton';+import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs';+import {+ KeyModelsById,+ modelMeta,+ PickableModel,+} from '@/features/agents/ai-model/model-meta';+import { platformModelTierMutations } from '@/features/platform-admin/hooks/platform-model-tier-hooks';+import { AdminControl, adminControl } from '@/lib/admin-control';+import { cn } from '@/lib/utils';++import { SectionHeader } from '../components/section-header';+import { KeyStatusBadge } from '../providers-tab/key-status';+import { ProviderLogo } from '../providers-tab/provider-logo';++import { ModelDetailRow } from './model-detail-row';++export function SpecificModelsSection({+ tiers,+ ownKeys,+ keyModels,+ visible,+ projectsWithoutTier,+ onMakeTier,+}: SpecificModelsSectionProps) {+ const { mutate: setVisible, isPending } =+ platformModelTierMutations.useSetSpecificModelsVisible();+ const [search, setSearch] = useState('');+ const models = modelMeta.specificModelsOf({+ configs: ownKeys,+ keyModels,+ tiers,+ });+ const cannotHide = visible && tiers.length === 0;++ const needle = search.trim().toLowerCase();+ const matches = (item: PickableModel) =>+ needle === '' ||+ item.model.name.toLowerCase().includes(needle) ||+ item.model.id.toLowerCase().includes(needle);+ const groups = ownKeys+ .map((config) => {+ const keyMatches = config.name.toLowerCase().includes(needle);+ return {+ config,+ keyMatches,+ state: keyModels[config.id],+ items: models.filter(+ (item) =>+ item.config.id === config.id && (keyMatches || matches(item)),+ ),+ };+ })+ .filter(+ (group) => needle === '' || group.items.length > 0 || group.keyMatches,+ );++ return (+ <section className="flex flex-col rounded-xl border border-gray-6/60 bg-panel shadow-panel">+ <div className="flex flex-col gap-3 px-5 py-4">+ <div className="flex items-start justify-between gap-3">+ <SectionHeader+ title={t('Specific models')}+ count={models.length}+ description={t("Models not used as a tier's main model.")}+ />+ <div className="flex shrink-0 flex-col items-end gap-1">+ <div className="flex items-center gap-2">+ {isPending && (+ <Loader2+ className="size-3.5 animate-spin text-gray-10"+ aria-label={t('Saving')}+ />+ )}+ <Tabs+ value={visible ? 'visible' : 'hidden'}+ onValueChange={(value) => setVisible(value === 'visible')}+ >+ <TabsList+ className="h-8"+ aria-label={t('Specific models visible to builders')}+ {...adminControl(+ AdminControl.AI_SPECIFIC_MODELS_VISIBLE_TOGGLE,+ )}+ >+ <TabsTrigger+ value="visible"+ className="gap-1.5 text-xs"+ disabled={isPending}+ >+ <Eye className="size-3.5" />+ {t('Visible')}+ </TabsTrigger>+ <TabsTrigger+ value="hidden"+ className="gap-1.5 text-xs"+ disabled={cannotHide || isPending}+ >+ <EyeOff className="size-3.5" />+ {t('Hidden')}+ </TabsTrigger>+ </TabsList>+ </Tabs>+ </div>+ <p className="text-xs text-gray-11">+ {cannotHide+ ? t('Add a tier before hiding these.')+ : visible+ ? t('Builders can pick these directly.')+ : t('Builders only see tiers.')}+ </p>+ {!cannotHide && projectsWithoutTier > 0 && (+ <p className="flex items-center gap-1 text-xs text-warning-11">+ <TriangleAlert className="size-3 shrink-0" />+ {visible+ ? t('hidingLeavesProjectsEmpty', {+ count: projectsWithoutTier,+ })+ : t('projectsWithNothingToPick', {+ count: projectsWithoutTier,+ })}+ </p>+ )}+ </div>+ </div>+ {models.length > SEARCH_THRESHOLD && (+ <Input+ value={search}+ onChange={(event) => setSearch(event.target.value)}+ placeholder={t('Search models')}+ aria-label={t('Search models')}+ className="max-w-sm"+ />+ )}+ </div>+ <div className="flex flex-col border-t border-gray-6/60">+ {groups.map((group) => (+ <KeyGroup+ key={group.config.id}+ config={group.config}+ items={group.items}+ total={+ models.filter((item) => item.config.id === group.config.id).length+ }+ hasTextModels={(group.state?.models?.length ?? 0) > 0}+ isLoading={group.state?.isLoading === true}+ isFetching={group.state?.isFetching === true}+ isError={group.state?.isError === true}+ onRetry={() => group.state?.refetch()}+ forceOpen={needle !== ''}+ defaultOpen={ownKeys.length === 1}+ onMakeTier={onMakeTier}+ />+ ))}+ {groups.length === 0 && (+ <p className="px-5 py-6 text-sm text-gray-11">+ {t('No models match')}+ </p>+ )}+ </div>+ </section>+ );+}++function KeyGroup({+ config,+ items,+ total,+ hasTextModels,+ isLoading,+ isFetching,+ isError,+ onRetry,+ forceOpen,+ defaultOpen,+ onMakeTier,+}: KeyGroupProps) {+ const [open, setOpen] = useState(defaultOpen);+ const [shown, setShown] = useState(PAGE_SIZE);+ const isOpen = forceOpen || open;+ const page = items.slice(0, shown);+ const retryButton = (+ <Button+ type="button"+ variant="ghost"+ size="icon-xs"+ aria-label={t('Retry')}+ disabled={isFetching}+ onClick={onRetry}+ >+ <RefreshCw className={cn('size-3', isFetching && 'animate-spin')} />+ </Button>+ );+ return (+ <Collapsible+ open={isOpen}+ onOpenChange={setOpen}+ className="border-b border-gray-6/60 last:border-b-0"+ >+ <div className="flex items-center gap-2 pr-5">+ <CollapsibleTrigger asChild>+ <button+ type="button"+ className="flex min-w-0 flex-1 items-center gap-3 px-5 py-3 text-left hover:bg-gray-2"+ >+ <ProviderLogo+ info={modelMeta.providerInfoOf({ provider: config.provider })}+ />+ <span className="flex min-w-0 flex-1 flex-col">+ <span className="truncate text-sm font-medium">+ {config.name}+ </span>+ <span className="text-xs text-gray-11">+ {isLoading ? (+ <Skeleton className="h-3 w-20" />+ ) : isError ? (+ t("Couldn't load {key} models", { key: config.name })+ ) : (+ t('modelsCount', { count: total })+ )}+ </span>+ </span>+ <ChevronRight+ className={cn(+ 'size-4 shrink-0 text-gray-10 transition-transform',+ isOpen && 'rotate-90',+ )}+ />+ </button>+ </CollapsibleTrigger>+ {config.status !== 'active' && (+ <KeyStatusBadge status={config.status} />+ )}+ {isError && retryButton}+ </div>+ <CollapsibleContent>+ <div className="flex flex-col pb-2 pl-5 pr-4">+ {isLoading &&+ [0, 1, 2].map((row) => (+ <div key={row} className="flex items-center gap-3 py-2.5">+ <Skeleton className="size-8 rounded-lg" />+ <div className="flex flex-1 flex-col gap-1.5">+ <Skeleton className="h-3.5 w-40" />+ <Skeleton className="h-3 w-24" />+ </div>+ </div>+ ))}+ {isError && (+ <div className="flex items-center gap-2 py-3 text-sm text-gray-11">+ <span>+ {t("Couldn't load {key} models", { key: config.name })}+ </span>+ {retryButton}+ </div>+ )}+ {page.map(({ model }) => (+ <ModelDetailRow+ key={model.id}+ name={model.name}+ config={config}+ model={model}+ trailing={+ <Button+ type="button"+ variant="outline"+ size="sm"+ onClick={() =>+ onMakeTier({ configId: config.id, modelId: model.id })+ }+ {...adminControl(AdminControl.AI_TIER_FROM_MODEL_OPEN)}+ >+ <Plus className="size-4" />+ {t('Make a tier')}+ </Button>+ }+ />+ ))}+ {!isLoading && !isError && items.length === 0 && (+ <p className="py-3 text-sm text-gray-11">+ {total > 0+ ? t('No models match')+ : hasTextModels+ ? t("Every text model on this key is a tier's main model.")+ : t('No text models on this key.')}+ </p>+ )}+ {items.length > page.length && (+ <Button+ type="button"+ variant="ghost"+ size="sm"+ className="self-start"+ onClick={() => setShown((current) => current + PAGE_SIZE)}+ >+ {t('Show {count} more', {+ count: Math.min(PAGE_SIZE, items.length - page.length),+ })}+ </Button>+ )}+ </div>+ </CollapsibleContent>+ </Collapsible>+ );+}++const PAGE_SIZE = 20;+const SEARCH_THRESHOLD = 10;++type SpecificModelsSectionProps = {+ tiers: PlatformModelTier[];+ ownKeys: AIProviderWithoutSensitiveData[];+ keyModels: KeyModelsById;+ visible: boolean;+ projectsWithoutTier: number;+ onMakeTier: (entry: PlatformModelTierEntry) => void;+};++type KeyGroupProps = {+ config: AIProviderWithoutSensitiveData;+ items: PickableModel[];+ total: number;+ hasTextModels: boolean;+ isLoading: boolean;+ isFetching: boolean;+ isError: boolean;+ onRetry: () => void;+ forceOpen: boolean;+ defaultOpen: boolean;+ onMakeTier: (entry: PlatformModelTierEntry) => void;+};diff --git a/packages/web/src/app/routes/platform/setup/ai/tiers-tab/tier-card.tsx b/packages/web/src/app/routes/platform/setup/ai/tiers-tab/tier-card.tsxnew file mode 100644index 00000000000..e36118a873e--- /dev/null+++ b/packages/web/src/app/routes/platform/setup/ai/tiers-tab/tier-card.tsx@@ -0,0 +1,627 @@+import {+ AIProviderWithoutSensitiveData,+ PlatformModelTier,+ PlatformModelTierEntry,+ UpdatePlatformModelTierRequest,+} from '@activepieces/shared';+import { t } from 'i18next';+import {+ ArrowDown,+ ArrowUp,+ Brain,+ Info,+ Loader2,+ MessageSquare,+ MoreHorizontal,+ Pencil,+ Plus,+ Replace,+ Star,+ Trash2,+ TriangleAlert,+ Zap,+ LucideIcon,+} from 'lucide-react';+import { AnimatePresence, motion } from 'motion/react';+import { ReactNode, useEffect, useRef, useState } from 'react';++import { TextWithTooltip } from '@/components/custom/text-with-tooltip';+import { Button } from '@/components/ui/button';+import {+ DropdownMenu,+ DropdownMenuContent,+ DropdownMenuItem,+ DropdownMenuSeparator,+ DropdownMenuTrigger,+} from '@/components/ui/dropdown-menu';+import {+ Tooltip,+ TooltipContent,+ TooltipTrigger,+} from '@/components/ui/tooltip';+import {+ KeyModelsById,+ modelMeta,+ ToolsVerdict,+} from '@/features/agents/ai-model/model-meta';+import { platformModelTierMutations } from '@/features/platform-admin/hooks/platform-model-tier-hooks';+import { AdminControl, adminControl } from '@/lib/admin-control';+import { cn } from '@/lib/utils';++import { AdminModelPicker } from './admin-model-picker';+import { TierEntryRow } from './tier-entry-row';+import { tierThinking } from './tier-thinking';++export function TierCard({+ tier,+ configsById,+ ownKeys,+ keyModels,+ projectIds,+ previewProjectId,+ reducedMotion,+ onEdit,+ onDelete,+ onMoveUp,+ onMoveDown,+}: TierCardProps) {+ const { mutate: update, isPending } = platformModelTierMutations.useUpdate();+ const [mainPickerOpen, setMainPickerOpen] = useState(false);+ const [fallbackPickerOpen, setFallbackPickerOpen] = useState(false);+ const menuTriggerRef = useRef<HTMLButtonElement>(null);+ const showSpinner = useDelayedFlag({ active: isPending, delayMs: 300 });++ const save = (request: UpdatePlatformModelTierRequest) =>+ update({ id: tier.id, request });+ const move = ({ from, to }: { from: number; to: number }) =>+ save({ entries: modelMeta.moveEntry({ entries: tier.entries, from, to }) });++ const mainEntry = tier.entries[0];+ const mainModel =+ mainEntry === undefined+ ? undefined+ : modelMeta.catalogModel({ keyModels, entry: mainEntry });+ const thinkingChip = tierThinking.chipLabel({+ budget: tier.thinkingBudget ?? null,+ });+ const tools = modelMeta.toolsVerdict({+ config:+ mainEntry === undefined ? undefined : configsById.get(mainEntry.configId),+ entry: mainEntry,+ model: mainModel,+ });+ const notices = cardNotices({+ tier,+ configsById,+ keyModels,+ projectIds,+ previewProjectId,+ tools,+ mainName: mainModel?.name ?? mainEntry?.modelId ?? '',+ });+ const unavailableHere =+ previewProjectId !== null &&+ modelMeta.tierReach({+ tier,+ configsById,+ projectIds: [previewProjectId],+ }).unavailableCount === 1;+ const canAddFallback = tier.entries.length < MAX_ENTRIES;+ const noFallbacks = tier.entries.length === 1;+ const fallbackPicker = (trigger: ReactNode) => (+ <AdminModelPicker+ configs={ownKeys}+ keyModels={keyModels}+ exclude={tier.entries}+ main={mainModel}+ mode="fallback"+ align="end"+ open={fallbackPickerOpen}+ onOpenChange={setFallbackPickerOpen}+ onPick={(picked) =>+ save({+ entries: modelMeta.addFallback({+ entries: tier.entries,+ entry: picked,+ }),+ })+ }+ >+ {trigger}+ </AdminModelPicker>+ );++ return (+ <section+ className={cn(+ 'flex flex-col rounded-xl border border-gray-6/60 bg-panel shadow-panel transition-opacity',+ unavailableHere && 'opacity-60',+ )}+ aria-label={tier.name}+ >+ <header className="flex items-center gap-3 px-5 py-4">+ <span+ className="flex size-10 shrink-0 items-center justify-center rounded-lg bg-gray-3 text-xl"+ aria-hidden="true"+ >+ {tier.emoji}+ </span>+ <div className="flex min-w-0 flex-1 flex-col gap-0.5">+ <div className="flex flex-wrap items-center gap-2">+ <TextWithTooltip tooltipMessage={tier.name}>+ <h3 className="truncate text-sm font-semibold">{tier.name}</h3>+ </TextWithTooltip>+ {tier.isDefault && (+ <TierTag+ icon={Star}+ className="border-accent-6 bg-accent-3 text-accent-11"+ >+ {t('Default')}+ </TierTag>+ )}+ {tier.isFast && (+ <TierTag+ icon={Zap}+ className="border-warning-7 bg-warning-3 text-warning-11"+ >+ {t('Fast')}+ </TierTag>+ )}+ {thinkingChip !== undefined && (+ <TierTag icon={Brain}>{thinkingChip}</TierTag>+ )}+ {tools === 'recommended' && (+ <TierTag icon={MessageSquare}>+ {t('Recommended for chat')}+ </TierTag>+ )}+ {showSpinner && (+ <Loader2+ className="size-3.5 animate-spin text-gray-10"+ aria-label={t('Saving')}+ />+ )}+ </div>+ {tier.description && (+ <p className="truncate text-xs text-gray-11">{tier.description}</p>+ )}+ {notices.length > 0 && (+ <ul className="flex flex-col gap-0.5 pt-1">+ {notices.map((notice) => (+ <li+ key={notice}+ className="flex items-start gap-1.5 text-xs text-warning-11"+ >+ <TriangleAlert className="mt-0.5 size-3 shrink-0" />+ <span>{notice}</span>+ </li>+ ))}+ </ul>+ )}+ </div>+ <DropdownMenu>+ <DropdownMenuTrigger asChild>+ <Button+ ref={menuTriggerRef}+ variant="ghost"+ size="icon-sm"+ aria-label={t('Tier actions')}+ disabled={isPending}+ >+ <MoreHorizontal className="size-4" />+ </Button>+ </DropdownMenuTrigger>+ <DropdownMenuContent+ align="end"+ onCloseAutoFocus={(event) => event.preventDefault()}+ >+ <DropdownMenuItem+ onSelect={() => onEdit(menuTriggerRef.current)}+ {...adminControl(AdminControl.AI_TIER_OPEN)}+ >+ <Pencil className="size-4" />+ {t('Edit')}+ </DropdownMenuItem>+ {!tier.isDefault && (+ <DropdownMenuItem+ onSelect={() => save({ isDefault: true })}+ {...adminControl(AdminControl.AI_TIER_DEFAULT_SELECT)}+ >+ <Star className="size-4" />+ {t('Make default')}+ </DropdownMenuItem>+ )}+ {!tier.isFast && (+ <DropdownMenuItem+ onSelect={() => save({ isFast: true })}+ {...adminControl(AdminControl.AI_TIER_FAST_SELECT)}+ >+ <Zap className="size-4" />+ {t('Use as fast model')}+ </DropdownMenuItem>+ )}+ {onMoveUp !== undefined && (+ <DropdownMenuItem onSelect={onMoveUp}>+ <ArrowUp className="size-4" />+ {t('Move up')}+ </DropdownMenuItem>+ )}+ {onMoveDown !== undefined && (+ <DropdownMenuItem onSelect={onMoveDown}>+ <ArrowDown className="size-4" />+ {t('Move down')}+ </DropdownMenuItem>+ )}+ <DropdownMenuSeparator />+ <DropdownMenuItem+ className="text-danger-11"+ onSelect={() => onDelete(menuTriggerRef.current)}+ {...adminControl(AdminControl.AI_TIER_DELETE_OPEN)}+ >+ <Trash2 className="size-4" />+ {t('Delete')}+ </DropdownMenuItem>+ </DropdownMenuContent>+ </DropdownMenu>+ </header>++ <div className="flex flex-col gap-1 border-t border-gray-6/60 px-3 pb-3 pt-3">+ <div className="flex items-center gap-1.5 px-2 pb-1">+ <span className="text-xss font-medium uppercase tracking-wide text-gray-11">+ {t('Model & fallbacks')}+ </span>+ <Tooltip>+ <TooltipTrigger asChild>+ <span className="inline-flex text-gray-10" tabIndex={0}>+ <Info className="size-3.5" />+ </span>+ </TooltipTrigger>+ <TooltipContent>+ {t(+ 'Fallbacks are tried in this order when the main model fails.',+ )}+ </TooltipContent>+ </Tooltip>+ </div>+ <div className="flex flex-col">+ <AnimatePresence initial={false}>+ {tier.entries.map((entry, position) => {+ const config = configsById.get(entry.configId);+ const model = modelMeta.catalogModel({ keyModels, entry });+ const isMain = position === 0;+ const last = !noFallbacks && position === tier.entries.length - 1;+ const row = (+ <TierEntryRow+ entry={entry}+ index={position}+ isLast={last}+ config={config}+ model={model}+ loading={keyModels[entry.configId]?.isLoading === true}+ skipped={+ previewProjectId !== null &&+ !isMain &&+ !unavailableHere &&+ !modelMeta.entryRunsIn({+ entry,+ config,+ projectId: previewProjectId,+ })+ }+ warnings={modelMeta.warningsFor({+ entry,+ isMain,+ config,+ keyModels,+ mainModel,+ })}+ menu={+ <RowMenu+ isMain={isMain}+ disabled={isPending}+ canMoveUp={position > 0}+ canMoveDown={position < tier.entries.length - 1}+ onChangeMain={() => setMainPickerOpen(true)}+ onMoveUp={() =>+ move({ from: position, to: position - 1 })+ }+ onMoveDown={() =>+ move({ from: position, to: position + 1 })+ }+ onRemove={() =>+ save({+ entries: modelMeta.removeAt({+ entries: tier.entries,+ index: position,+ }),+ })+ }+ />+ }+ />+ );+ return (+ <motion.div+ key={modelMeta.entryKey({ entry })}+ layout={!reducedMotion}+ initial={reducedMotion ? false : { opacity: 0, y: 4 }}+ animate={{ opacity: 1, y: 0 }}+ exit={reducedMotion ? undefined : { opacity: 0, y: 4 }}+ transition={{ duration: 0.18 }}+ >+ {isMain ? (+ <AdminModelPicker+ configs={ownKeys}+ keyModels={keyModels}+ exclude={tier.entries}+ mode="main"+ anchorOnly+ open={mainPickerOpen}+ onOpenChange={setMainPickerOpen}+ onPick={(picked: PlatformModelTierEntry) =>+ save({+ entries: modelMeta.replaceMain({+ entries: tier.entries,+ entry: picked,+ }),+ })+ }+ >+ <div>{row}</div>+ </AdminModelPicker>+ ) : (+ row+ )}+ </motion.div>+ );+ })}+ </AnimatePresence>+ </div>+ {noFallbacks ? (+ <div className="flex items-stretch gap-3 px-2">+ <div className="flex w-4 shrink-0 flex-col items-center">+ <span className="w-px flex-1 bg-gray-6" />+ <span className="my-1 size-2 shrink-0 rounded-full border-2 border-gray-7 bg-panel" />+ <span className="w-px flex-1" />+ </div>+ <div className="my-1 flex min-w-0 flex-1 items-center justify-between gap-3 rounded-lg border border-dashed border-gray-7 px-3 py-2.5">+ <div className="flex min-w-0 items-center gap-1.5">+ <span className="truncate text-sm font-medium text-gray-11">+ {t('No fallback model')}+ </span>+ <Tooltip>+ <TooltipTrigger asChild>+ <span className="inline-flex text-gray-10" tabIndex={0}>+ <Info className="size-3.5" />+ </span>+ </TooltipTrigger>+ <TooltipContent className="max-w-64">+ {t(+ 'A fallback is tried when the main model fails. Without one, this tier fails if {model} is unavailable.',+ { model: mainModel?.name ?? mainEntry?.modelId ?? '' },+ )}+ </TooltipContent>+ </Tooltip>+ </div>+ {fallbackPicker(+ <Button+ variant="outline"+ size="sm"+ className="shrink-0"+ disabled={isPending}+ {...adminControl(AdminControl.AI_TIER_FALLBACK_OPEN)}+ >+ <Plus className="size-4" />+ {t('Add fallback')}+ </Button>,+ )}+ </div>+ </div>+ ) : (+ <div className="px-2 pt-1">+ {canAddFallback ? (+ fallbackPicker(+ <Button+ variant="ghost"+ size="sm"+ className="text-gray-11"+ disabled={isPending}+ {...adminControl(AdminControl.AI_TIER_FALLBACK_OPEN)}+ >+ <Plus className="size-4" />+ {t('Add fallback')}+ </Button>,+ )+ ) : (+ <p className="px-2 text-xs text-gray-10">+ {t('Up to 4 fallbacks')}+ </p>+ )}+ </div>+ )}+ </div>+ </section>+ );+}++function cardNotices({+ tier,+ configsById,+ keyModels,+ projectIds,+ previewProjectId,+ tools,+ mainName,+}: {+ tier: PlatformModelTier;+ configsById: Map<string, AIProviderWithoutSensitiveData>;+ keyModels: KeyModelsById;+ projectIds: string[];+ previewProjectId: string | null;+ tools: ToolsVerdict;+ mainName: string;+}): string[] {+ const noTools =+ tools === 'noTools'+ ? [+ t("Not offered in chat or agents — {model} can't call tools", {+ model: mainName,+ }),+ ]+ : [];+ if (previewProjectId !== null) {+ const here = modelMeta.tierReach({+ tier,+ configsById,+ projectIds: [previewProjectId],+ });+ return here.unavailableCount === 1+ ? [+ t(+ "Not available in this project — its main model's key doesn't serve it",+ ),+ ]+ : noTools;+ }+ const reach = modelMeta.tierReach({ tier, configsById, projectIds });+ const unavailable =+ reach.unavailableCount > 0+ ? [t('tierUnavailableInProjects', { count: reach.unavailableCount })]+ : [];+ const skipped = reach.skippedFallbacks.map(({ entry, projectCount }) =>+ t('fallbackSkippedInProjects', {+ model:+ modelMeta.catalogModel({ keyModels, entry })?.name ?? entry.modelId,+ count: projectCount,+ }),+ );+ return [...unavailable, ...skipped, ...noTools];+}++function TierTag({+ icon: Icon,+ className,+ children,+}: {+ icon: LucideIcon;+ className?: string;+ children: ReactNode;+}) {+ return (+ <span+ className={cn(+ 'inline-flex items-center gap-1 rounded-full border border-gray-6 bg-gray-2 px-2 py-0.5 text-xs font-medium text-gray-11',+ className,+ )}+ >+ <Icon className="size-3 fill-current" />+ {children}+ </span>+ );+}++function RowMenu({+ isMain,+ disabled,+ canMoveUp,+ canMoveDown,+ onChangeMain,+ onMoveUp,+ onMoveDown,+ onRemove,+}: {+ isMain: boolean;+ disabled: boolean;+ canMoveUp: boolean;+ canMoveDown: boolean;+ onChangeMain: () => void;+ onMoveUp: () => void;+ onMoveDown: () => void;+ onRemove: () => void;+}) {+ return (+ <DropdownMenu>+ <DropdownMenuTrigger asChild>+ <Button+ variant="ghost"+ size="icon-xs"+ className="shrink-0 text-gray-10"+ aria-label={t('Model actions')}+ disabled={disabled}+ >+ <MoreHorizontal className="size-3.5" />+ </Button>+ </DropdownMenuTrigger>+ <DropdownMenuContent+ align="end"+ onCloseAutoFocus={(event) => event.preventDefault()}+ >+ {isMain && (+ <DropdownMenuItem+ onSelect={onChangeMain}+ {...adminControl(AdminControl.AI_TIER_MAIN_MODEL_OPEN)}+ >+ <Replace className="size-4" />+ {t('Change main model')}+ </DropdownMenuItem>+ )}+ {canMoveUp && (+ <DropdownMenuItem onSelect={onMoveUp}>+ <ArrowUp className="size-4" />+ {t('Move up')}+ </DropdownMenuItem>+ )}+ {canMoveDown && (+ <DropdownMenuItem onSelect={onMoveDown}>+ <ArrowDown className="size-4" />+ {t('Move down')}+ </DropdownMenuItem>+ )}+ {!isMain && (+ <>+ <DropdownMenuSeparator />+ <DropdownMenuItem className="text-danger-11" onSelect={onRemove}>+ <Trash2 className="size-4" />+ {t('Remove')}+ </DropdownMenuItem>+ </>+ )}+ </DropdownMenuContent>+ </DropdownMenu>+ );+}++function useDelayedFlag({+ active,+ delayMs,+}: {+ active: boolean;+ delayMs: number;+}): boolean {+ const [shown, setShown] = useState(false);+ useEffect(() => {+ if (!active) {+ setShown(false);+ return;+ }+ const timer = window.setTimeout(() => setShown(true), delayMs);+ return () => window.clearTimeout(timer);+ }, [active, delayMs]);+ return shown;+}++const MAX_ENTRIES = 5;++type TierCardProps = {+ tier: PlatformModelTier;+ configsById: Map<string, AIProviderWithoutSensitiveData>;+ ownKeys: AIProviderWithoutSensitiveData[];+ keyModels: KeyModelsById;+ projectIds: string[];+ previewProjectId: string | null;+ reducedMotion: boolean;+ onEdit: (trigger: HTMLElement | null) => void;+ onDelete: (trigger: HTMLElement | null) => void;+ onMoveUp: (() => void) | undefined;+ onMoveDown: (() => void) | undefined;+};diff --git a/packages/web/src/app/routes/platform/setup/ai/tiers-tab/tier-dialog.tsx b/packages/web/src/app/routes/platform/setup/ai/tiers-tab/tier-dialog.tsxnew file mode 100644index 00000000000..8cf7a2fc8ea--- /dev/null+++ b/packages/web/src/app/routes/platform/setup/ai/tiers-tab/tier-dialog.tsx@@ -0,0 +1,491 @@+import {+ AIProviderWithoutSensitiveData,+ CreatePlatformModelTierRequest,+ formErrors,+ PlatformModelTier,+ PlatformModelTierEntry,+} from '@activepieces/shared';+import { zodResolver } from '@hookform/resolvers/zod';+import { t } from 'i18next';+import { ChevronDown, Plus } from 'lucide-react';+import { KeyboardEvent, useState } from 'react';+import { useForm } from 'react-hook-form';+import { z } from 'zod';++import { Button } from '@/components/ui/button';+import {+ Dialog,+ DialogContent,+ DialogDescription,+ DialogFooter,+ DialogHeader,+ DialogTitle,+} from '@/components/ui/dialog';+import {+ Form,+ FormControl,+ FormDescription,+ FormField,+ FormItem,+ FormLabel,+ FormMessage,+} from '@/components/ui/form';+import { Input } from '@/components/ui/input';+import {+ Select,+ SelectContent,+ SelectItem,+ SelectTrigger,+ SelectValue,+} from '@/components/ui/select';+import { Textarea } from '@/components/ui/textarea';+import {+ KeyModelsById,+ modelMeta,+} from '@/features/agents/ai-model/model-meta';+import { ModelRow } from '@/features/agents/ai-model/model-row';+import { platformModelTierMutations } from '@/features/platform-admin/hooks/platform-model-tier-hooks';+import { AdminControl, adminControl } from '@/lib/admin-control';+import { cn } from '@/lib/utils';++import { AdminModelPicker } from './admin-model-picker';+import { tierEmojis } from './tier-emojis';+import { tierThinking } from './tier-thinking';++export function TierDialog({+ state,+ onOpenChange,+ ownKeys,+ keyModels,+ returnFocusTo,+ onReturnFocus,+ onSaved,+}: TierDialogProps) {+ return (+ <Dialog open={state.open} onOpenChange={onOpenChange}>+ {state.open && (+ <TierForm+ key={state.mode === 'edit' ? state.tier.id : 'new'}+ state={state}+ ownKeys={ownKeys}+ keyModels={keyModels}+ onCancel={() => onOpenChange(false)}+ onReturnFocus={() => onReturnFocus({ target: returnFocusTo ?? null })}+ onSaved={(tier) => {+ onSaved?.(tier);+ onOpenChange(false);+ }}+ />+ )}+ </Dialog>+ );+}++function TierForm({+ state,+ ownKeys,+ keyModels,+ onCancel,+ onReturnFocus,+ onSaved,+}: {+ state: OpenTierDialogState;+ ownKeys: AIProviderWithoutSensitiveData[];+ keyModels: KeyModelsById;+ onCancel: () => void;+ onReturnFocus: () => void;+ onSaved: (tier: PlatformModelTier) => void;+}) {+ const editing = state.mode === 'edit' ? state.tier : undefined;+ const [showAllEmojis, setShowAllEmojis] = useState(false);+ const [pickerOpen, setPickerOpen] = useState(false);+ const form = useForm<TierFormValues>({+ resolver: zodResolver(formSchema({ requireMain: editing === undefined })),+ mode: 'onChange',+ defaultValues: defaultValuesOf({ state }),+ });+ const { mutate: create, isPending: creating } =+ platformModelTierMutations.useCreate();+ const { mutate: update, isPending: updating } =+ platformModelTierMutations.useUpdate();+ const saving = creating || updating;+ const keepOpen = (event: Event) => {+ if (saving) {+ event.preventDefault();+ }+ };++ const submit = (values: TierFormValues) => {+ const shared = {+ name: values.name.trim(),+ emoji: values.emoji,+ description:+ values.description.trim() === '' ? null : values.description.trim(),+ thinkingBudget: tierThinking.budgetOf({+ value: values.thinking,+ current: editing?.thinkingBudget ?? null,+ }),+ };+ if (editing !== undefined) {+ update({ id: editing.id, request: shared }, { onSuccess: onSaved });+ return;+ }+ if (values.main === undefined) {+ return;+ }+ const request: CreatePlatformModelTierRequest = {+ ...shared,+ entries: [values.main],+ };+ create(request, { onSuccess: onSaved });+ };++ const currentThinking = editing?.thinkingBudget ?? null;+ const customThinking =+ tierThinking.presetOf({ budget: currentThinking }) === undefined &&+ currentThinking !== null;+ const emojis = showAllEmojis ? tierEmojis.all : tierEmojis.quick;++ return (+ <DialogContent+ className="sm:max-w-md"+ showCloseButton={!saving}+ onEscapeKeyDown={keepOpen}+ onPointerDownOutside={keepOpen}+ onInteractOutside={keepOpen}+ onCloseAutoFocus={(event) => {+ event.preventDefault();+ onReturnFocus();+ }}+ >+ <Form {...form}>+ <form+ onSubmit={form.handleSubmit(submit)}+ className="flex flex-col gap-5"+ >+ <DialogHeader>+ <DialogTitle>+ {editing ? t('Edit tier') : t('New tier')}+ </DialogTitle>+ <DialogDescription>+ {t('Builders see this name and emoji when they pick a tier.')}+ </DialogDescription>+ </DialogHeader>++ <div className="flex items-start gap-3">+ <FormField+ control={form.control}+ name="emoji"+ render={({ field }) => (+ <FormItem className="flex flex-col gap-1.5">+ <FormLabel>{t('Emoji')}</FormLabel>+ <span+ className="flex size-9 items-center justify-center rounded-md border border-gray-6 bg-panel text-xl"+ aria-hidden="true"+ >+ {field.value}+ </span>+ </FormItem>+ )}+ />+ <FormField+ control={form.control}+ name="name"+ render={({ field }) => (+ <FormItem className="flex flex-1 flex-col gap-1.5">+ <FormLabel showRequiredIndicator>{t('Name')}</FormLabel>+ <FormControl>+ <Input+ {...field}+ autoFocus+ maxLength={NAME_MAX_LENGTH}+ placeholder={t('e.g. Expert')}+ />+ </FormControl>+ <FormMessage />+ </FormItem>+ )}+ />+ </div>++ <FormField+ control={form.control}+ name="emoji"+ render={({ field }) => (+ <FormItem className="flex flex-col gap-1.5">+ <FormLabel>{t('Pick an emoji')}</FormLabel>+ <div+ role="radiogroup"+ aria-label={t('Pick an emoji')}+ className="flex flex-wrap gap-1.5"+ onKeyDown={moveFocusWithArrows}+ >+ {emojis.map((emoji) => (+ <button+ key={emoji}+ type="button"+ role="radio"+ aria-checked={field.value === emoji}+ aria-label={emoji}+ tabIndex={field.value === emoji ? 0 : -1}+ onClick={() => field.onChange(emoji)}+ className={cn(+ 'flex size-8 items-center justify-center rounded-md border text-base transition-colors hover:bg-gray-3 focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-accent-8',+ field.value === emoji+ ? 'border-accent-8 bg-accent-3'+ : 'border-gray-6 bg-panel',+ )}+ >+ {emoji}+ </button>+ ))}+ {!showAllEmojis && (+ <button+ type="button"+ aria-label={t('More emojis')}+ onClick={() => setShowAllEmojis(true)}+ className="flex size-8 items-center justify-center rounded-md border border-dashed border-gray-7 text-gray-11 hover:bg-gray-3 focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-accent-8"+ >+ <Plus className="size-4" />+ </button>+ )}+ </div>+ <FormMessage />+ </FormItem>+ )}+ />++ <FormField+ control={form.control}+ name="description"+ render={({ field }) => (+ <FormItem className="flex flex-col gap-1.5">+ <FormLabel>{t('Description')}</FormLabel>+ <FormControl>+ <Textarea+ {...field}+ rows={2}+ maxLength={DESCRIPTION_MAX_LENGTH}+ placeholder={t('e.g. Best for everyday use')}+ />+ </FormControl>+ <FormDescription className="text-right text-xs tabular-nums">+ {field.value.length}/{DESCRIPTION_MAX_LENGTH}+ </FormDescription>+ <FormMessage />+ </FormItem>+ )}+ />++ {editing === undefined && (+ <FormField+ control={form.control}+ name="main"+ render={({ field }) => {+ const pickedConfig = ownKeys.find(+ (config) => config.id === field.value?.configId,+ );+ const pickedModel =+ field.value === undefined+ ? undefined+ : modelMeta.catalogModel({ keyModels, entry: field.value });+ return (+ <FormItem className="flex flex-col gap-1.5">+ <FormLabel showRequiredIndicator>+ {t('Main model')}+ </FormLabel>+ <AdminModelPicker+ configs={ownKeys}+ keyModels={keyModels}+ exclude={[]}+ mode="main"+ align="start"+ open={pickerOpen}+ onOpenChange={setPickerOpen}+ onPick={(entry) => field.onChange(entry)}+ >+ <Button+ type="button"+ variant="outline"+ className="h-auto min-h-9 justify-between px-3 py-1.5"+ aria-label={t('Main model')}+ >+ {field.value !== undefined &&+ pickedModel !== undefined ? (+ <ModelRow+ model={pickedModel}+ info={+ pickedConfig === undefined+ ? undefined+ : modelMeta.providerInfoOf({+ provider: pickedConfig.provider,+ })+ }+ keyName={pickedConfig?.name}+ />+ ) : field.value !== undefined ? (+ <span className="truncate text-sm">+ {field.value.modelId}+ </span>+ ) : (+ <span className="text-gray-11">+ {t('Pick a model')}+ </span>+ )}+ <ChevronDown className="size-4 shrink-0 opacity-60" />+ </Button>+ </AdminModelPicker>+ <FormMessage />+ </FormItem>+ );+ }}+ />+ )}++ <FormField+ control={form.control}+ name="thinking"+ render={({ field }) => (+ <FormItem className="flex flex-col gap-1.5">+ <FormLabel>{t('Thinking')}</FormLabel>+ <Select value={field.value} onValueChange={field.onChange}>+ <FormControl>+ <SelectTrigger>+ <SelectValue />+ </SelectTrigger>+ </FormControl>+ <SelectContent>+ {customThinking && (+ <SelectItem value={tierThinking.CUSTOM_VALUE}>+ {t('Custom ({budget} tokens)', {+ budget: currentThinking.toLocaleString(),+ })}+ </SelectItem>+ )}+ {tierThinking.presets().map((preset) => (+ <SelectItem key={preset.value} value={preset.value}>+ {preset.label}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ <FormDescription className="text-xs">+ {t('Used only by models that support reasoning.')}+ </FormDescription>+ </FormItem>+ )}+ />++ <DialogFooter>+ <Button+ type="button"+ variant="outline"+ onClick={onCancel}+ disabled={saving}+ >+ {t('Cancel')}+ </Button>+ <Button+ type="submit"+ loading={saving}+ {...adminControl(AdminControl.AI_TIER_SUBMIT)}+ >+ {t('Save tier')}+ </Button>+ </DialogFooter>+ </form>+ </Form>+ </DialogContent>+ );+}++function moveFocusWithArrows(event: KeyboardEvent<HTMLDivElement>) {+ if (+ !['ArrowRight', 'ArrowLeft', 'ArrowDown', 'ArrowUp'].includes(event.key)+ ) {+ return;+ }+ const radios = Array.from(+ event.currentTarget.querySelectorAll<HTMLButtonElement>('[role="radio"]'),+ );+ const current = radios.indexOf(document.activeElement as HTMLButtonElement);+ if (current === -1) {+ return;+ }+ event.preventDefault();+ const forward = event.key === 'ArrowRight' || event.key === 'ArrowDown';+ const next =+ radios[(current + (forward ? 1 : radios.length - 1)) % radios.length];+ next.focus();+ next.click();+}++function formSchema({ requireMain }: { requireMain: boolean }) {+ return z+ .object({+ name: CreatePlatformModelTierRequest.shape.name,+ emoji: CreatePlatformModelTierRequest.shape.emoji,+ description: z+ .string()+ .trim()+ .max(DESCRIPTION_MAX_LENGTH, formErrors.tierDescriptionTooLong),+ thinking: z.string(),+ main: PlatformModelTierEntry.optional(),+ })+ .superRefine((values, context) => {+ if (requireMain && values.main === undefined) {+ context.addIssue({+ code: 'custom',+ path: ['main'],+ message: formErrors.required,+ });+ }+ });+}++function defaultValuesOf({+ state,+}: {+ state: OpenTierDialogState;+}): TierFormValues {+ if (state.mode === 'edit') {+ return {+ name: state.tier.name,+ emoji: state.tier.emoji,+ description: state.tier.description ?? '',+ thinking: tierThinking.valueOf({+ budget: state.tier.thinkingBudget ?? null,+ }),+ main: undefined,+ };+ }+ return {+ name: '',+ emoji: tierEmojis.defaultEmoji,+ description: '',+ thinking: tierThinking.valueOf({ budget: null }),+ main: state.initialMain,+ };+}++const NAME_MAX_LENGTH = 40;+const DESCRIPTION_MAX_LENGTH = 120;++type TierFormValues = z.infer<ReturnType<typeof formSchema>>;++export type TierDialogState =+ | { open: false }+ | { open: true; mode: 'create'; initialMain?: PlatformModelTierEntry }+ | { open: true; mode: 'edit'; tier: PlatformModelTier };++type OpenTierDialogState = Extract<TierDialogState, { open: true }>;++type TierDialogProps = {+ state: TierDialogState;+ onOpenChange: (open: boolean) => void;+ ownKeys: AIProviderWithoutSensitiveData[];+ keyModels: KeyModelsById;+ returnFocusTo?: HTMLElement | null;+ onReturnFocus: (args: { target: HTMLElement | null }) => void;+ onSaved?: (tier: PlatformModelTier) => void;+};diff --git a/packages/web/src/app/routes/platform/setup/ai/tiers-tab/tier-emojis.ts b/packages/web/src/app/routes/platform/setup/ai/tiers-tab/tier-emojis.tsnew file mode 100644index 00000000000..97639c9194c--- /dev/null+++ b/packages/web/src/app/routes/platform/setup/ai/tiers-tab/tier-emojis.ts@@ -0,0 +1,81 @@+const QUICK_PICKS = [+ '⚡',+ '🚀',+ '💡',+ '🧠',+ '⚖️',+ '📄',+ '🖼️',+ '🔍',+ '💬',+ '🤖',+ '✨',+ '🎯',+];++const MORE = [+ '🔥',+ '🌟',+ '💎',+ '🏆',+ '🎓',+ '🧪',+ '🔬',+ '🧮',+ '📊',+ '📈',+ '📝',+ '✏️',+ '📚',+ '🗂️',+ '🧩',+ '🛠️',+ '⚙️',+ '🔧',+ '🔑',+ '🔒',+ '🛡️',+ '🧭',+ '🗺️',+ '🌍',+ '🌙',+ '☀️',+ '🌈',+ '⭐',+ '💫',+ '🎨',+ '🎵',+ '🎬',+ '📷',+ '📣',+ '📬',+ '💼',+ '🏢',+ '🏠',+ '🚗',+ '✈️',+ '🐢',+ '🐇',+ '🦉',+ '🐙',+ '🦾',+ '🐝',+ '🍀',+ '🌱',+ '🍎',+ '☕',+ '🍕',+ '❤️',+ '👍',+ '👋',+ '🙌',+ '😀',+ '🥳',+ '😎',+];++export const tierEmojis = {+ quick: QUICK_PICKS,+ all: [...QUICK_PICKS, ...MORE],+ defaultEmoji: QUICK_PICKS[0],+};diff --git a/packages/web/src/app/routes/platform/setup/ai/tiers-tab/tier-entry-row.tsx b/packages/web/src/app/routes/platform/setup/ai/tiers-tab/tier-entry-row.tsxnew file mode 100644index 00000000000..d2f1ecfc8b4--- /dev/null+++ b/packages/web/src/app/routes/platform/setup/ai/tiers-tab/tier-entry-row.tsx@@ -0,0 +1,177 @@+import {+ AIProviderModel,+ AIProviderWithoutSensitiveData,+ PlatformModelTierEntry,+} from '@activepieces/shared';+import { t } from 'i18next';+import { TriangleAlert } from 'lucide-react';+import { ReactNode } from 'react';++import { Button } from '@/components/ui/button';+import {+ Popover,+ PopoverContent,+ PopoverTrigger,+} from '@/components/ui/popover';+import {+ Tooltip,+ TooltipContent,+ TooltipTrigger,+} from '@/components/ui/tooltip';+import { EntryWarning, modelMeta } from '@/features/agents/ai-model/model-meta';+import { cn } from '@/lib/utils';++import { keyStatusText } from '../providers-tab/key-status';++import { ModelDetailRow } from './model-detail-row';++export function TierEntryRow({+ entry,+ index,+ isLast,+ config,+ model,+ warnings,+ menu,+ loading,+ skipped,+}: TierEntryRowProps) {+ const isMain = index === 0;+ return (+ <div+ className={cn(+ 'flex items-stretch gap-3 px-2 transition-opacity',+ skipped && 'opacity-50',+ )}+ >+ <div className="flex w-4 shrink-0 flex-col items-center">+ <span className={cn('w-px flex-1', !isMain && 'bg-gray-6')} />+ <Tooltip>+ <TooltipTrigger asChild>+ <span+ tabIndex={0}+ className={cn(+ 'my-1 shrink-0 rounded-full',+ isMain+ ? 'size-2.5 bg-accent-9 ring-4 ring-accent-3'+ : 'size-2 border-2 border-gray-8 bg-panel',+ )}+ />+ </TooltipTrigger>+ <TooltipContent>+ {isMain+ ? t('Main model — tried first')+ : t('Fallback {rank} — tried when the models above fail', {+ rank: index,+ })}+ </TooltipContent>+ </Tooltip>+ <span className={cn('w-px flex-1', !isLast && 'bg-gray-6')} />+ </div>+ <ModelDetailRow+ name={model?.name ?? entry.modelId}+ config={config}+ model={model}+ nameSuffix={+ <>+ {config !== undefined && config.projectScope !== 'all' && (+ <ScopeChip config={config} />+ )}+ {skipped && (+ <span className="shrink-0 rounded-full border border-gray-6 px-1.5 text-xss text-gray-11">+ {t('Skipped in this project')}+ </span>+ )}+ {warnings.length > 0 && <WarningsPopover warnings={warnings} />}+ </>+ }+ trailing={menu}+ loading={loading}+ />+ </div>+ );+}++function ScopeChip({ config }: { config: AIProviderWithoutSensitiveData }) {+ return (+ <Tooltip>+ <TooltipTrigger asChild>+ <span+ tabIndex={0}+ className="shrink-0 rounded-full border border-gray-6 bg-gray-2 px-1.5 text-xss text-gray-11"+ >+ {config.projectScope === 'except'+ ? t('exceptProjectsCount', { count: config.projectIds.length })+ : t('projectsCount', { count: config.projectIds.length })}+ </span>+ </TooltipTrigger>+ <TooltipContent className="max-w-64">+ {t(+ "{key} only serves some projects. This model doesn't run in the others.",+ { key: config.name },+ )}+ </TooltipContent>+ </Tooltip>+ );+}++function WarningsPopover({ warnings }: { warnings: EntryWarning[] }) {+ return (+ <Popover>+ <PopoverTrigger asChild>+ <Button+ type="button"+ variant="ghost"+ size="icon-xs"+ className="size-5 shrink-0 text-warning-11 hover:text-warning-12"+ aria-label={t('Warnings')}+ >+ <TriangleAlert className="size-3.5" />+ </Button>+ </PopoverTrigger>+ <PopoverContent+ align="start"+ className="w-72 border-warning-7 bg-warning-3 p-3 text-warning-12"+ >+ <ul className="flex flex-col gap-1.5 text-xs">+ {warnings.map((warning) => (+ <li key={warning.code} className="flex items-start gap-1.5">+ <TriangleAlert className="mt-0.5 size-3 shrink-0" />+ <span>{warningText(warning)}</span>+ </li>+ ))}+ </ul>+ </PopoverContent>+ </Popover>+ );+}++function warningText(warning: EntryWarning): string {+ switch (warning.code) {+ case 'keyMissing':+ return t('Key not found — this model is skipped');+ case 'modelGone':+ return t("Not in {key}'s model list anymore", { key: warning.keyName });+ case 'smallerContext':+ return t('Smaller context ({context}) than the main model ({main})', {+ context: modelMeta.formatContext({ tokens: warning.contextTokens }),+ main: modelMeta.formatContext({ tokens: warning.mainContextTokens }),+ });+ case 'keyStatus':+ return t('Key status: {status} — tried last', {+ status: keyStatusText({ status: warning.status }) ?? warning.status,+ });+ }+}++type TierEntryRowProps = {+ entry: PlatformModelTierEntry;+ index: number;+ isLast: boolean;+ config: AIProviderWithoutSensitiveData | undefined;+ model: AIProviderModel | undefined;+ warnings: EntryWarning[];+ menu: ReactNode;+ loading: boolean;+ skipped: boolean;+};diff --git a/packages/web/src/app/routes/platform/setup/ai/tiers-tab/tier-thinking.ts b/packages/web/src/app/routes/platform/setup/ai/tiers-tab/tier-thinking.tsnew file mode 100644index 00000000000..9f9423dc316--- /dev/null+++ b/packages/web/src/app/routes/platform/setup/ai/tiers-tab/tier-thinking.ts@@ -0,0 +1,79 @@+import { t } from 'i18next';++function presets(): ThinkingPreset[] {+ return [+ { value: 'default', budget: null, label: t('Default') },+ { value: 'off', budget: 0, label: t('Off') },+ { value: 'low', budget: 5_000, label: t('Low (5,000 tokens)') },+ { value: 'medium', budget: 10_000, label: t('Medium (10,000 tokens)') },+ { value: 'high', budget: 20_000, label: t('High (20,000 tokens)') },+ ];+}++function presetOf({+ budget,+}: {+ budget: number | null;+}): ThinkingPreset | undefined {+ return presets().find((preset) => preset.budget === budget);+}++function valueOf({ budget }: { budget: number | null }): string {+ return presetOf({ budget })?.value ?? CUSTOM_VALUE;+}++function budgetOf({+ value,+ current,+}: {+ value: string;+ current: number | null;+}): number | null {+ if (value === CUSTOM_VALUE) {+ return current;+ }+ return presets().find((preset) => preset.value === value)?.budget ?? null;+}++function chipLabel({ budget }: { budget: number | null }): string | undefined {+ if (budget === null) {+ return undefined;+ }+ const preset = presetOf({ budget });+ if (preset === undefined) {+ return t('Thinking: {budget}', { budget: budget.toLocaleString() });+ }+ return t('Thinking: {level}', { level: shortLabel({ value: preset.value }) });+}++function shortLabel({ value }: { value: string }): string {+ switch (value) {+ case 'off':+ return t('Off');+ case 'low':+ return t('Low');+ case 'medium':+ return t('Medium');+ case 'high':+ return t('High');+ default:+ return t('Default');+ }+}++export const tierThinking = {+ presets,+ presetOf,+ valueOf,+ budgetOf,+ chipLabel,+ CUSTOM_VALUE: 'custom',+};++const CUSTOM_VALUE = 'custom';++export type ThinkingPreset = {+ value: 'default' | 'off' | 'low' | 'medium' | 'high';+ budget: number | null;+ label: string;+};diff --git a/packages/web/src/features/agents/ai-model/model-meta.ts b/packages/web/src/features/agents/ai-model/model-meta.tsnew file mode 100644index 00000000000..da1ed9291f3--- /dev/null+++ b/packages/web/src/features/agents/ai-model/model-meta.ts@@ -0,0 +1,513 @@+import { AIProviderName } from '@activepieces/core-utils';+import {+ AiProviderKeyStatus,+ AIProviderModel,+ AIProviderModelType,+ aiProviderUtils,+ AIProviderWithoutSensitiveData,+ PlatformModelTier,+ PlatformModelTierEntry,+} from '@activepieces/shared';+import { t } from 'i18next';++import {+ AiProviderInfo,+ SUPPORTED_AI_PROVIDERS,+} from '@/features/agents/ai-providers';+import { formatUtils } from '@/lib/format-utils';++function isOwnKey(config: AIProviderWithoutSensitiveData): boolean {+ return config.provider !== AIProviderName.ACTIVEPIECES;+}++function providerInfoOf({+ provider,+}: {+ provider: AIProviderName;+}): AiProviderInfo {+ return (+ SUPPORTED_AI_PROVIDERS.find((info) => info.provider === provider) ?? {+ provider,+ name: provider,+ markdown: '',+ logoUrl: '',+ }+ );+}++function scopedTextModels({+ config,+ models,+}: {+ config: AIProviderWithoutSensitiveData;+ models: AIProviderModel[];+}): AIProviderModel[] {+ return models.filter(+ (model) =>+ model.type === AIProviderModelType.TEXT &&+ (config.modelScope !== 'selected' || config.modelIds.includes(model.id)),+ );+}++function formatContext({ tokens }: { tokens: number }): string {+ return formatUtils.formatNumberCompact(tokens);+}++function formatPrice({ perMillion }: { perMillion: number }): string {+ return Number.isInteger(perMillion)+ ? `$${perMillion}`+ : `$${perMillion.toFixed(2)}`;+}++function metaParts({ model }: { model: AIProviderModel }): string[] {+ const metadata = model.metadata;+ if (metadata === undefined) {+ return [];+ }+ const context =+ metadata.contextTokens === undefined+ ? []+ : [+ t('{context} context', {+ context: formatContext({ tokens: metadata.contextTokens }),+ }),+ ];+ const price =+ metadata.inputCostPerMillionTokens === undefined ||+ metadata.outputCostPerMillionTokens === undefined+ ? []+ : [+ t('{input} / {output} per 1M', {+ input: formatPrice({+ perMillion: metadata.inputCostPerMillionTokens,+ }),+ output: formatPrice({+ perMillion: metadata.outputCostPerMillionTokens,+ }),+ }),+ ];+ return [...context, ...price];+}++function entryKey({ entry }: { entry: PlatformModelTierEntry }): string {+ return `${entry.configId}:${entry.modelId}`;+}++function sameEntry(+ a: PlatformModelTierEntry,+ b: PlatformModelTierEntry,+): boolean {+ return a.configId === b.configId && a.modelId === b.modelId;+}++function catalogModel({+ keyModels,+ entry,+}: {+ keyModels: KeyModelsById;+ entry: PlatformModelTierEntry;+}): AIProviderModel | undefined {+ return keyModels[entry.configId]?.models?.find(+ (model) => model.id === entry.modelId,+ );+}++function rankForFallback({+ main,+ candidates,+}: {+ main: AIProviderModel | undefined;+ candidates: PickableModel[];+}): RankedGroup[] {+ const mainMetadata = main?.metadata;+ if (mainMetadata === undefined) {+ return [+ {+ kind: 'all',+ items: candidates.map((item) => ({ ...item, tradeOffs: [] })),+ },+ ];+ }+ const ranked = candidates.map((item) => ({+ ...item,+ tradeOffs: tradeOffsAgainst({ main: mainMetadata, candidate: item.model }),+ }));+ const withMetadata = ranked.filter(+ (item) => item.model.metadata !== undefined,+ );+ const groups: RankedGroup[] = [+ {+ kind: 'full',+ items: withMetadata.filter((item) => item.tradeOffs.length === 0),+ },+ {+ kind: 'tradeOff',+ items: withMetadata.filter((item) => item.tradeOffs.length > 0),+ },+ {+ kind: 'other',+ items: ranked.filter((item) => item.model.metadata === undefined),+ },+ ];+ return groups.filter((group) => group.items.length > 0);+}++function warningsFor({+ entry,+ isMain,+ config,+ keyModels,+ mainModel,+}: {+ entry: PlatformModelTierEntry;+ isMain: boolean;+ config: AIProviderWithoutSensitiveData | undefined;+ keyModels: KeyModelsById;+ mainModel: AIProviderModel | undefined;+}): EntryWarning[] {+ if (config === undefined) {+ return [{ code: 'keyMissing' }];+ }+ const loaded = keyModels[config.id]?.models;+ const model = catalogModel({ keyModels, entry });+ const gone =+ loaded !== undefined && model === undefined+ ? [{ code: 'modelGone' as const, keyName: config.name }]+ : [];+ const smaller =+ !isMain &&+ model?.metadata?.contextTokens !== undefined &&+ mainModel?.metadata?.contextTokens !== undefined &&+ model.metadata.contextTokens < mainModel.metadata.contextTokens+ ? [+ {+ code: 'smallerContext' as const,+ contextTokens: model.metadata.contextTokens,+ mainContextTokens: mainModel.metadata.contextTokens,+ },+ ]+ : [];+ const status =+ config.status === 'active'+ ? []+ : [{ code: 'keyStatus' as const, status: config.status }];+ return [...gone, ...smaller, ...status];+}++function specificModelsOf({+ configs,+ keyModels,+ tiers,+}: {+ configs: AIProviderWithoutSensitiveData[];+ keyModels: KeyModelsById;+ tiers: PlatformModelTier[];+}): PickableModel[] {+ const mains = tiers.flatMap((tier) => tier.entries.slice(0, 1));+ return configs+ .filter(isOwnKey)+ .flatMap((config) =>+ (keyModels[config.id]?.models ?? [])+ .filter(+ (model) =>+ !mains.some((main) =>+ sameEntry(main, { configId: config.id, modelId: model.id }),+ ),+ )+ .map((model) => ({ config, model })),+ );+}++function replaceMain({+ entries,+ entry,+}: {+ entries: PlatformModelTierEntry[];+ entry: PlatformModelTierEntry;+}): PlatformModelTierEntry[] {+ return [+ entry,+ ...entries.slice(1).filter((other) => !sameEntry(other, entry)),+ ];+}++function addFallback({+ entries,+ entry,+}: {+ entries: PlatformModelTierEntry[];+ entry: PlatformModelTierEntry;+}): PlatformModelTierEntry[] {+ return entries.some((other) => sameEntry(other, entry))+ ? entries+ : [...entries, entry];+}++function removeAt({+ entries,+ index,+}: {+ entries: PlatformModelTierEntry[];+ index: number;+}): PlatformModelTierEntry[] {+ return entries.filter((_, position) => position !== index);+}++function moveEntry({+ entries,+ from,+ to,+}: {+ entries: PlatformModelTierEntry[];+ from: number;+ to: number;+}): PlatformModelTierEntry[] {+ if (+ from === to ||+ from < 0 ||+ to < 0 ||+ from >= entries.length ||+ to >= entries.length+ ) {+ return entries;+ }+ const without = entries.filter((_, position) => position !== from);+ return [...without.slice(0, to), entries[from], ...without.slice(to)];+}++function tradeOffsAgainst({+ main,+ candidate,+}: {+ main: NonNullable<AIProviderModel['metadata']>;+ candidate: AIProviderModel;+}): TradeOff[] {+ const metadata = candidate.metadata;+ if (metadata === undefined) {+ return [];+ }+ const smaller =+ metadata.contextTokens !== undefined &&+ main.contextTokens !== undefined &&+ metadata.contextTokens < main.contextTokens+ ? [+ {+ code: 'smallerContext' as const,+ contextTokens: metadata.contextTokens,+ },+ ]+ : [];+ const noTools =+ main.supportsToolCalling === true && metadata.supportsToolCalling === false+ ? [{ code: 'noToolCalling' as const }]+ : [];+ return [...smaller, ...noTools];+}++function keyServesProject({+ config,+ projectId,+}: {+ config: Pick<AIProviderWithoutSensitiveData, 'projectScope' | 'projectIds'>;+ projectId: string;+}): boolean {+ switch (config.projectScope) {+ case 'selected':+ return config.projectIds.includes(projectId);+ case 'except':+ return !config.projectIds.includes(projectId);+ default:+ return true;+ }+}++function entryRunsIn({+ entry,+ config,+ projectId,+}: {+ entry: PlatformModelTierEntry;+ config: AIProviderWithoutSensitiveData | undefined;+ projectId: string;+}): boolean {+ return (+ config !== undefined &&+ keyServesProject({ config, projectId }) &&+ (config.modelScope !== 'selected' ||+ config.modelIds.includes(entry.modelId))+ );+}++function tierReach({+ tier,+ configsById,+ projectIds,+}: {+ tier: PlatformModelTier;+ configsById: Map<string, AIProviderWithoutSensitiveData>;+ projectIds: string[];+}): TierReach {+ const [main, ...fallbacks] = tier.entries;+ const runsIn = (entry: PlatformModelTierEntry, projectId: string) =>+ entryRunsIn({ entry, config: configsById.get(entry.configId), projectId });+ const available =+ main === undefined+ ? []+ : projectIds.filter((projectId) => runsIn(main, projectId));+ const skippedFallbacks = fallbacks.flatMap((entry) => {+ const projectCount = available.filter(+ (projectId) => !runsIn(entry, projectId),+ ).length;+ return projectCount === 0 ? [] : [{ entry, projectCount }];+ });+ return {+ unavailableCount: projectIds.length - available.length,+ skippedFallbacks,+ };+}++function projectsWithoutTier({+ tiers,+ configsById,+ projectIds,+}: {+ tiers: PlatformModelTier[];+ configsById: Map<string, AIProviderWithoutSensitiveData>;+ projectIds: string[];+}): number {+ return projectIds.filter(+ (projectId) =>+ !tiers.some(+ (tier) =>+ tier.entries[0] !== undefined &&+ entryRunsIn({+ entry: tier.entries[0],+ config: configsById.get(tier.entries[0].configId),+ projectId,+ }),+ ),+ ).length;+}++function keyScopeImpact({+ tiers,+ configsById,+ config,+ projectIds,+}: {+ tiers: PlatformModelTier[];+ configsById: Map<string, AIProviderWithoutSensitiveData>;+ config: AIProviderWithoutSensitiveData;+ projectIds: string[];+}): KeyScopeImpact[] {+ const nextConfigs = new Map(configsById).set(config.id, config);+ const skippedOnKey = (reach: TierReach) =>+ reach.skippedFallbacks+ .filter(({ entry }) => entry.configId === config.id)+ .reduce((sum, { projectCount }) => sum + projectCount, 0);+ return tiers.flatMap((tier) => {+ if (!tier.entries.some((entry) => entry.configId === config.id)) {+ return [];+ }+ const before = tierReach({ tier, configsById, projectIds });+ const after = tierReach({ tier, configsById: nextConfigs, projectIds });+ const lostProjects = after.unavailableCount - before.unavailableCount;+ const skippedProjects = skippedOnKey(after) - skippedOnKey(before);+ return lostProjects > 0 || skippedProjects > 0+ ? [{ tierName: tier.name, lostProjects, skippedProjects }]+ : [];+ });+}++function toolsVerdict({+ config,+ entry,+ model,+}: {+ config: AIProviderWithoutSensitiveData | undefined;+ entry: PlatformModelTierEntry | undefined;+ model: AIProviderModel | undefined;+}): ToolsVerdict {+ if (model?.metadata?.supportsToolCalling === false) {+ return 'noTools';+ }+ const curated =+ config === undefined+ ? undefined+ : aiProviderUtils.getCuratedChatModels({ provider: config.provider });+ return entry !== undefined &&+ curated?.some((curatedModel) => curatedModel.id === entry.modelId) === true+ ? 'recommended'+ : null;+}++export const modelMeta = {+ isOwnKey,+ providerInfoOf,+ scopedTextModels,+ formatContext,+ formatPrice,+ metaParts,+ entryKey,+ sameEntry,+ catalogModel,+ rankForFallback,+ warningsFor,+ specificModelsOf,+ replaceMain,+ addFallback,+ removeAt,+ moveEntry,+ keyServesProject,+ entryRunsIn,+ tierReach,+ projectsWithoutTier,+ keyScopeImpact,+ toolsVerdict,+};++export type KeyModelsById = Record<+ string,+ {+ models: AIProviderModel[] | undefined;+ isLoading: boolean;+ isFetching: boolean;+ isError: boolean;+ refetch: () => unknown;+ }+>;++export type PickableModel = {+ config: AIProviderWithoutSensitiveData;+ model: AIProviderModel;+};++export type TradeOff =+ | { code: 'smallerContext'; contextTokens: number }+ | { code: 'noToolCalling' };++export type RankedModel = PickableModel & { tradeOffs: TradeOff[] };++export type RankedGroup = {+ kind: 'all' | 'full' | 'tradeOff' | 'other';+ items: RankedModel[];+};++export type EntryWarning =+ | { code: 'keyMissing' }+ | { code: 'modelGone'; keyName: string }+ | { code: 'smallerContext'; contextTokens: number; mainContextTokens: number }+ | { code: 'keyStatus'; status: AiProviderKeyStatus };++export type TierReach = {+ unavailableCount: number;+ skippedFallbacks: { entry: PlatformModelTierEntry; projectCount: number }[];+};++export type KeyScopeImpact = {+ tierName: string;+ lostProjects: number;+ skippedProjects: number;+};++export type ToolsVerdict = 'noTools' | 'recommended' | null;diff --git a/packages/web/src/features/agents/ai-model/model-picker-popover.tsx b/packages/web/src/features/agents/ai-model/model-picker-popover.tsxnew file mode 100644index 00000000000..a0ebb60b44e--- /dev/null+++ b/packages/web/src/features/agents/ai-model/model-picker-popover.tsx@@ -0,0 +1,234 @@+import { AIProviderModel } from '@activepieces/shared';+import { t } from 'i18next';+import { Brain, Check, Wrench } from 'lucide-react';+import { ReactNode, useDeferredValue, useMemo, useState } from 'react';++import {+ Command,+ CommandEmpty,+ CommandGroup,+ CommandInput,+ CommandItem,+ CommandList,+} from '@/components/ui/command';+import {+ Popover,+ PopoverAnchor,+ PopoverContent,+ PopoverTrigger,+} from '@/components/ui/popover';+import { cn } from '@/lib/utils';++import { modelMeta } from './model-meta';++export function ModelPickerPopover<T>({+ groups,+ notices,+ emptyText,+ onPick,+ open,+ onOpenChange,+ align = 'end',+ anchorOnly = false,+ children,+}: ModelPickerPopoverProps<T>) {+ const [search, setSearch] = useState('');+ const [expanded, setExpanded] = useState<string[]>([]);+ const needle = useDeferredValue(search.trim().toLowerCase());+ const visible = useMemo(+ () =>+ groups+ .map((group) => ({+ ...group,+ items:+ needle === ''+ ? group.items+ : group.items.filter((item) =>+ item.searchText.toLowerCase().includes(needle),+ ),+ }))+ .filter((group) => group.items.length > 0),+ [groups, needle],+ );++ const pick = (item: ModelPickerItem<T>) => {+ onPick(item.value);+ onOpenChange(false);+ };++ return (+ <Popover+ modal+ open={open}+ onOpenChange={(next) => {+ if (!next) {+ setSearch('');+ setExpanded([]);+ }+ onOpenChange(next);+ }}+ >+ {anchorOnly ? (+ <PopoverAnchor asChild>{children}</PopoverAnchor>+ ) : (+ <PopoverTrigger asChild>{children}</PopoverTrigger>+ )}+ <PopoverContent align={align} className="w-[420px] p-0">+ <Command shouldFilter={false}>+ <CommandInput+ placeholder={t('Search models')}+ value={search}+ onValueChange={(value) => {+ setSearch(value);+ setExpanded([]);+ }}+ />+ {notices}+ <CommandList className="max-h-80 overflow-y-auto">+ <CommandEmpty>{emptyText}</CommandEmpty>+ {visible.map((group) => {+ const isExpanded = expanded.includes(group.id);+ const shown = isExpanded+ ? group.items+ : group.items.slice(0, GROUP_CAP);+ return (+ <CommandGroup+ key={group.id}+ heading={+ <span className="flex items-center gap-2">+ {group.heading}+ <span className="ml-auto tabular-nums text-gray-10">+ {group.items.length}+ </span>+ </span>+ }+ className="[&_[cmdk-group-heading]]:sticky [&_[cmdk-group-heading]]:top-0 [&_[cmdk-group-heading]]:z-10 [&_[cmdk-group-heading]]:bg-panel"+ >+ {shown.map((item) => (+ <CommandItem+ key={item.id}+ value={`${group.id}:${item.id}`}+ disabled={item.disabled}+ aria-disabled={item.disabled}+ onSelect={() => pick(item)}+ >+ <PickerRow item={item} />+ </CommandItem>+ ))}+ {shown.length < group.items.length && (+ <CommandItem+ value={`${group.id}:show-all`}+ onSelect={() =>+ setExpanded((current) => [...current, group.id])+ }+ className="justify-center text-xs text-accent-11"+ >+ {t('Show all {count}', { count: group.items.length })}+ </CommandItem>+ )}+ </CommandGroup>+ );+ })}+ </CommandList>+ </Command>+ </PopoverContent>+ </Popover>+ );+}++function PickerRow<T>({ item }: { item: ModelPickerItem<T> }) {+ const metadata = item.model?.metadata;+ const meta = [+ ...(metadata?.contextTokens === undefined+ ? []+ : [modelMeta.formatContext({ tokens: metadata.contextTokens })]),+ ...(metadata?.inputCostPerMillionTokens === undefined ||+ metadata.outputCostPerMillionTokens === undefined+ ? []+ : [+ `${modelMeta.formatPrice({+ perMillion: metadata.inputCostPerMillionTokens,+ })} / ${modelMeta.formatPrice({+ perMillion: metadata.outputCostPerMillionTokens,+ })}`,+ ]),+ ];+ return (+ <span className="flex w-full min-w-0 flex-col gap-0.5">+ <span className="flex w-full min-w-0 items-center gap-2">+ {item.leading}+ <span className="truncate text-sm" title={item.name}>+ {item.name}+ </span>+ {item.subtitle !== undefined && (+ <span className="truncate text-xs text-gray-11">{item.subtitle}</span>+ )}+ <span className="ml-auto flex shrink-0 items-center gap-2">+ {meta.length > 0 && (+ <span className="text-xs tabular-nums text-gray-11">+ {meta.join(' · ')}+ </span>+ )}+ {metadata?.supportsToolCalling === true && (+ <Wrench+ className="size-3.5 text-gray-10"+ aria-label={t('Supports tool calling')}+ >+ <title>{t('Supports tool calling')}</title>+ </Wrench>+ )}+ {metadata?.supportsReasoning === true && (+ <Brain+ className="size-3.5 text-gray-10"+ aria-label={t('Supports reasoning')}+ >+ <title>{t('Supports reasoning')}</title>+ </Brain>+ )}+ <Check+ className={cn(+ 'size-4 text-gray-11',+ item.selected ? 'opacity-100' : 'opacity-0',+ )}+ />+ </span>+ </span>+ {item.note !== undefined && (+ <span className="pl-6 text-xs text-warning-11">{item.note}</span>+ )}+ </span>+ );+}++const GROUP_CAP = 30;++export type ModelPickerItem<T> = {+ id: string;+ value: T;+ name: string;+ searchText: string;+ subtitle?: string;+ leading?: ReactNode;+ model?: AIProviderModel;+ note?: string;+ disabled?: boolean;+ selected?: boolean;+};++export type ModelPickerGroup<T> = {+ id: string;+ heading: ReactNode;+ items: ModelPickerItem<T>[];+};++type ModelPickerPopoverProps<T> = {+ groups: ModelPickerGroup<T>[];+ notices?: ReactNode;+ emptyText: string;+ onPick: (value: T) => void;+ open: boolean;+ onOpenChange: (open: boolean) => void;+ align?: 'start' | 'end';+ anchorOnly?: boolean;+ children: ReactNode;+};diff --git a/packages/web/src/features/agents/ai-model/model-row.tsx b/packages/web/src/features/agents/ai-model/model-row.tsxnew file mode 100644index 00000000000..937acafd407--- /dev/null+++ b/packages/web/src/features/agents/ai-model/model-row.tsx@@ -0,0 +1,93 @@+import { AIProviderModel } from '@activepieces/shared';+import { t } from 'i18next';+import { Brain, Wrench } from 'lucide-react';+import { ReactNode } from 'react';++import { LogoPlate } from '@/components/custom/logo-plate';+import { TextWithTooltip } from '@/components/custom/text-with-tooltip';+import {+ Tooltip,+ TooltipContent,+ TooltipTrigger,+} from '@/components/ui/tooltip';+import { AiProviderInfo } from '@/features/agents/ai-providers';+import { cn } from '@/lib/utils';++import { modelMeta } from './model-meta';++export function ModelRow({+ model,+ info,+ keyName,+ nameSuffix,+ trailing,+ className,+ logoSize = 'xs',+}: ModelRowProps) {+ const parts = [+ ...(keyName === undefined ? [] : [keyName]),+ ...modelMeta.metaParts({ model }),+ ];+ const capabilities = capabilitiesOf({ model });+ return (+ <span className={cn('flex min-w-0 flex-1 items-center gap-3', className)}>+ {info !== undefined && info.logoUrl !== '' && (+ <LogoPlate src={info.logoUrl} alt={info.name} size={logoSize} />+ )}+ <span className="flex min-w-0 flex-1 flex-col text-left">+ <span className="flex min-w-0 items-center gap-1.5">+ <TextWithTooltip tooltipMessage={model.name}>+ <span className="truncate text-sm font-medium">{model.name}</span>+ </TextWithTooltip>+ {nameSuffix}+ </span>+ {(parts.length > 0 || capabilities.length > 0) && (+ <span className="flex min-w-0 items-center gap-1.5 text-xs text-gray-11">+ {parts.length > 0 && (+ <span className="truncate">{parts.join(' · ')}</span>+ )}+ {capabilities.map(({ icon: Icon, label }) => (+ <Tooltip key={label}>+ <TooltipTrigger asChild>+ <span+ className="inline-flex shrink-0 text-gray-10"+ aria-label={label}+ >+ <Icon className="size-3.5" />+ </span>+ </TooltipTrigger>+ <TooltipContent>{label}</TooltipContent>+ </Tooltip>+ ))}+ </span>+ )}+ </span>+ {trailing}+ </span>+ );+}++function capabilitiesOf({ model }: { model: AIProviderModel }) {+ const metadata = model.metadata;+ if (metadata === undefined) {+ return [];+ }+ return [+ ...(metadata.supportsToolCalling === true+ ? [{ icon: Wrench, label: t('Supports tool calling') }]+ : []),+ ...(metadata.supportsReasoning === true+ ? [{ icon: Brain, label: t('Supports reasoning') }]+ : []),+ ];+}++type ModelRowProps = {+ model: AIProviderModel;+ info?: AiProviderInfo;+ keyName?: string;+ nameSuffix?: ReactNode;+ trailing?: ReactNode;+ className?: string;+ logoSize?: 'xs' | 'sm';+};diff --git a/packages/web/src/features/platform-admin/api/platform-model-tier-api.ts b/packages/web/src/features/platform-admin/api/platform-model-tier-api.tsnew file mode 100644index 00000000000..67ee7714fc1--- /dev/null+++ b/packages/web/src/features/platform-admin/api/platform-model-tier-api.ts@@ -0,0 +1,39 @@+import {+ CreatePlatformModelTierRequest,+ PlatformModelTier,+ UpdatePlatformModelTierRequest,+} from '@activepieces/shared';++import { api } from '@/lib/api';++export const platformModelTierApi = {+ listAdmin() {+ return api.get<PlatformModelTier[]>('/v1/platform-model-tiers/admin');+ },+ create(request: CreatePlatformModelTierRequest) {+ return api.post<PlatformModelTier>('/v1/platform-model-tiers', request);+ },+ update({+ id,+ request,+ }: {+ id: string;+ request: UpdatePlatformModelTierRequest;+ }) {+ return api.post<PlatformModelTier>(+ `/v1/platform-model-tiers/${id}`,+ request,+ );+ },+ reorder({ tierIds }: { tierIds: string[] }) {+ return api.post<PlatformModelTier[]>('/v1/platform-model-tiers/reorder', {+ tierIds,+ });+ },+ remove({ id, replacedBy }: { id: string; replacedBy?: string }) {+ return api.delete<void>(+ `/v1/platform-model-tiers/${id}`,+ replacedBy === undefined ? undefined : { replacedBy },+ );+ },+};diff --git a/packages/web/src/features/platform-admin/hooks/platform-model-tier-hooks.ts b/packages/web/src/features/platform-admin/hooks/platform-model-tier-hooks.tsnew file mode 100644index 00000000000..77d270a1867--- /dev/null+++ b/packages/web/src/features/platform-admin/hooks/platform-model-tier-hooks.ts@@ -0,0 +1,358 @@+import {+ AIProviderWithoutSensitiveData,+ CreatePlatformModelTierRequest,+ PlatformConfiguration,+ PlatformModelTier,+ UpdatePlatformModelTierRequest,+} from '@activepieces/shared';+import {+ QueryClient,+ useMutation,+ useQueries,+ useQuery,+ useQueryClient,+} from '@tanstack/react-query';+import { t } from 'i18next';+import { useCallback, useMemo } from 'react';+import { toast } from 'sonner';++import { platformConfigurationApi } from '@/api/platform-configuration-api';+import {+ KeyModelsById,+ modelMeta,+} from '@/features/agents/ai-model/model-meta';+import { platformConfigurationHooks } from '@/hooks/platform-configuration-hooks';+import { api } from '@/lib/api';++import { aiProviderApi } from '../api/ai-provider-api';+import { platformModelTierApi } from '../api/platform-model-tier-api';++import { aiProviderKeys } from './ai-provider-hooks';++export const platformModelTierKeys = {+ admin: ['platform-model-tiers', 'admin'] as const,+};++export const platformModelTierQueries = {+ useAdminList: () =>+ useQuery({+ queryKey: platformModelTierKeys.admin,+ queryFn: () => platformModelTierApi.listAdmin(),+ staleTime: TIERS_STALE_MS,+ }),+ useKeyModels: (configs: AIProviderWithoutSensitiveData[]): KeyModelsById => {+ const ownKeys = useMemo(+ () => configs.filter(modelMeta.isOwnKey),+ [configs],+ );+ const combine = useCallback(+ (results: KeyModelsQueryResult[]): KeyModelsById =>+ Object.fromEntries(+ ownKeys.map((config, index) => [+ config.id,+ {+ models:+ results[index].data === undefined+ ? undefined+ : modelMeta.scopedTextModels({+ config,+ models: results[index].data,+ }),+ isLoading: results[index].isLoading,+ isFetching: results[index].isFetching,+ isError: results[index].isError,+ refetch: results[index].refetch,+ },+ ]),+ ),+ [ownKeys],+ );+ return useQueries({+ queries: ownKeys.map((config) => ({+ queryKey: aiProviderKeys.configModels(config.id),+ queryFn: () => aiProviderApi.listModelsForConfig(config.id),+ staleTime: KEY_MODELS_STALE_MS,+ })),+ combine,+ });+ },+};++export const platformModelTierMutations = {+ useCreate: () => {+ const queryClient = useQueryClient();+ return useMutation({+ mutationKey: platformModelTierKeys.admin,+ mutationFn: (request: CreatePlatformModelTierRequest) =>+ platformModelTierApi.create(request),+ onSuccess: (created) => {+ queryClient.setQueryData<PlatformModelTier[]>(+ platformModelTierKeys.admin,+ (old) => upsertTier({ tiers: old ?? [], tier: created }),+ );+ toast.success(t('Tier created'), { duration: TOAST_SUCCESS_MS });+ },+ onError: (error) =>+ toastError({ error, fallback: t('Could not create this tier') }),+ onSettled: () => settle({ queryClient }),+ });+ },+ useUpdate: () => {+ const queryClient = useQueryClient();+ return useMutation({+ mutationKey: platformModelTierKeys.admin,+ mutationFn: ({+ id,+ request,+ }: {+ id: string;+ request: UpdatePlatformModelTierRequest;+ }) => platformModelTierApi.update({ id, request }),+ onMutate: async ({ id, request }) => {+ await queryClient.cancelQueries({+ queryKey: platformModelTierKeys.admin,+ });+ const previous = queryClient.getQueryData<PlatformModelTier[]>(+ platformModelTierKeys.admin,+ );+ queryClient.setQueryData<PlatformModelTier[]>(+ platformModelTierKeys.admin,+ (old) => old && applyPatch({ tiers: old, id, request }),+ );+ return { previous };+ },+ onSuccess: (updated) => {+ queryClient.setQueryData<PlatformModelTier[]>(+ platformModelTierKeys.admin,+ (old) => upsertTier({ tiers: old ?? [], tier: updated }),+ );+ toast.success(t('Saved'), { duration: TOAST_SUCCESS_MS });+ },+ onError: (error, _variables, context) => {+ restore({ queryClient, previous: context?.previous });+ toastError({ error, fallback: t('Could not save this tier') });+ },+ onSettled: () => settle({ queryClient }),+ });+ },+ useReorder: () => {+ const queryClient = useQueryClient();+ return useMutation({+ mutationKey: platformModelTierKeys.admin,+ mutationFn: ({ tierIds }: { tierIds: string[] }) =>+ platformModelTierApi.reorder({ tierIds }),+ onMutate: async ({ tierIds }) => {+ await queryClient.cancelQueries({+ queryKey: platformModelTierKeys.admin,+ });+ const previous = queryClient.getQueryData<PlatformModelTier[]>(+ platformModelTierKeys.admin,+ );+ queryClient.setQueryData<PlatformModelTier[]>(+ platformModelTierKeys.admin,+ (old) =>+ old &&+ tierIds.flatMap((id) => old.filter((tier) => tier.id === id)),+ );+ return { previous };+ },+ onSuccess: (tiers) => {+ queryClient.setQueryData<PlatformModelTier[]>(+ platformModelTierKeys.admin,+ tiers,+ );+ toast.success(t('Saved'), { duration: TOAST_SUCCESS_MS });+ },+ onError: (error, _variables, context) => {+ restore({ queryClient, previous: context?.previous });+ toastError({ error, fallback: t('Could not reorder tiers') });+ },+ onSettled: () => settle({ queryClient }),+ });+ },+ useDelete: () => {+ const queryClient = useQueryClient();+ return useMutation({+ mutationKey: platformModelTierKeys.admin,+ mutationFn: ({ id, replacedBy }: { id: string; replacedBy?: string }) =>+ platformModelTierApi.remove({ id, replacedBy }),+ onSuccess: (_result, { id, replacedBy }) => {+ queryClient.setQueryData<PlatformModelTier[]>(+ platformModelTierKeys.admin,+ (old) => old && removeTier({ tiers: old, id, replacedBy }),+ );+ toast.success(t('Tier deleted'), { duration: TOAST_SUCCESS_MS });+ },+ onError: (error) =>+ toastError({ error, fallback: t('Could not delete this tier') }),+ onSettled: () => settle({ queryClient }),+ });+ },+ useSetSpecificModelsVisible: () => {+ const queryClient = useQueryClient();+ return useMutation({+ mutationFn: (visible: boolean) =>+ platformConfigurationApi.update({ aiSpecificModelsVisible: visible }),+ onMutate: async (visible) => {+ await queryClient.cancelQueries({+ queryKey: platformConfigurationHooks.queryKey,+ });+ const previous = queryClient.getQueryData<PlatformConfiguration>(+ platformConfigurationHooks.queryKey,+ );+ queryClient.setQueryData<PlatformConfiguration>(+ platformConfigurationHooks.queryKey,+ (old) => old && { ...old, aiSpecificModelsVisible: visible },+ );+ return { previous };+ },+ onSuccess: (configuration, visible) => {+ queryClient.setQueryData(+ platformConfigurationHooks.queryKey,+ configuration,+ );+ toast.success(+ visible+ ? t('Specific models are visible to builders')+ : t('Specific models are hidden from builders'),+ { duration: TOAST_SUCCESS_MS },+ );+ },+ onError: (error, _variables, context) => {+ if (context?.previous !== undefined) {+ queryClient.setQueryData(+ platformConfigurationHooks.queryKey,+ context.previous,+ );+ }+ toastError({ error, fallback: t('Could not save this setting') });+ },+ });+ },+};++function toastError({+ error,+ fallback,+}: {+ error: unknown;+ fallback: string;+}): void {+ toast.error(api.serverErrorMessage(error) ?? fallback, {+ duration: TOAST_ERROR_MS,+ });+}++function settle({ queryClient }: { queryClient: QueryClient }): void {+ if (+ queryClient.isMutating({ mutationKey: platformModelTierKeys.admin }) === 1+ ) {+ queryClient.invalidateQueries({ queryKey: platformModelTierKeys.admin });+ }+}++function restore({+ queryClient,+ previous,+}: {+ queryClient: QueryClient;+ previous: PlatformModelTier[] | undefined;+}): void {+ if (previous !== undefined) {+ queryClient.setQueryData(platformModelTierKeys.admin, previous);+ }+}++function upsertTier({+ tiers,+ tier,+}: {+ tiers: PlatformModelTier[];+ tier: PlatformModelTier;+}): PlatformModelTier[] {+ const exists = tiers.some((candidate) => candidate.id === tier.id);+ const merged = exists+ ? tiers.map((candidate) => (candidate.id === tier.id ? tier : candidate))+ : [...tiers, tier];+ return merged.map((candidate) =>+ candidate.id === tier.id+ ? candidate+ : {+ ...candidate,+ isDefault: tier.isDefault ? false : candidate.isDefault,+ isFast: tier.isFast ? false : candidate.isFast,+ },+ );+}++function applyPatch({+ tiers,+ id,+ request,+}: {+ tiers: PlatformModelTier[];+ id: string;+ request: UpdatePlatformModelTierRequest;+}): PlatformModelTier[] {+ return tiers.map((tier) => {+ if (tier.id === id) {+ return {+ ...tier,+ ...(request.name === undefined ? {} : { name: request.name }),+ ...(request.emoji === undefined ? {} : { emoji: request.emoji }),+ ...(request.description === undefined+ ? {}+ : { description: request.description }),+ ...(request.entries === undefined ? {} : { entries: request.entries }),+ ...(request.thinkingBudget === undefined+ ? {}+ : { thinkingBudget: request.thinkingBudget }),+ ...(request.isDefault === true ? { isDefault: true } : {}),+ ...(request.isFast === true ? { isFast: true } : {}),+ };+ }+ return {+ ...tier,+ isDefault: request.isDefault === true ? false : tier.isDefault,+ isFast: request.isFast === true ? false : tier.isFast,+ };+ });+}++function removeTier({+ tiers,+ id,+ replacedBy,+}: {+ tiers: PlatformModelTier[];+ id: string;+ replacedBy: string | undefined;+}): PlatformModelTier[] {+ const removed = tiers.find((tier) => tier.id === id);+ return tiers+ .filter((tier) => tier.id !== id)+ .map((tier) =>+ tier.id === replacedBy && removed !== undefined+ ? {+ ...tier,+ isDefault: tier.isDefault || removed.isDefault,+ isFast: tier.isFast || removed.isFast,+ }+ : tier,+ );+}++const TIERS_STALE_MS = 30 * 1000;+const TOAST_SUCCESS_MS = 3000;+const TOAST_ERROR_MS = 5000;+const KEY_MODELS_STALE_MS = 5 * 60 * 1000;++type KeyModelsQueryResult = {+ data:+ | Awaited<ReturnType<typeof aiProviderApi.listModelsForConfig>>+ | undefined;+ isLoading: boolean;+ isFetching: boolean;+ isError: boolean;+ refetch: () => unknown;+};diff --git a/packages/web/src/features/platform-admin/index.ts b/packages/web/src/features/platform-admin/index.tsindex cb286a41be5..edb2462066d 100644--- a/packages/web/src/features/platform-admin/index.ts+++ b/packages/web/src/features/platform-admin/index.ts@@ -1,4 +1,5 @@export { aiProviderApi } from './api/ai-provider-api';+export { platformModelTierApi } from './api/platform-model-tier-api';export { aiToolConfigApi } from './api/ai-tool-config-api';export { analyticsApi } from './api/analytics-api';export { apiKeyApi } from './api/api-key-api';@@ -40,6 +41,11 @@ export {aiProviderKeys,hasAnyAuthFieldFilled,} from './hooks/ai-provider-hooks';+export {+ platformModelTierQueries,+ platformModelTierMutations,+ platformModelTierKeys,+} from './hooks/platform-model-tier-hooks';export {aiToolConfigQueries,aiToolConfigMutations,diff --git a/packages/web/src/lib/admin-control.ts b/packages/web/src/lib/admin-control.tsindex cd2e7479c37..06f0ef4889b 100644--- a/packages/web/src/lib/admin-control.ts+++ b/packages/web/src/lib/admin-control.ts@@ -21,6 +21,16 @@ export enum AdminControl {AI_PROVIDER_KEY_OPEN = 'ai.provider-key.open',AI_PROVIDER_KEY_SUBMIT = 'ai.provider-key.submit',AI_PROVIDER_OPEN = 'ai.provider.open',+ AI_SPECIFIC_MODELS_VISIBLE_TOGGLE = 'ai.specific-models-visible.toggle',+ AI_TIER_DEFAULT_SELECT = 'ai.tier-default.select',+ AI_TIER_DELETE_CONFIRM = 'ai.tier-delete.confirm',+ AI_TIER_DELETE_OPEN = 'ai.tier-delete.open',+ AI_TIER_FALLBACK_OPEN = 'ai.tier-fallback.open',+ AI_TIER_FAST_SELECT = 'ai.tier-fast.select',+ AI_TIER_FROM_MODEL_OPEN = 'ai.tier-from-model.open',+ AI_TIER_MAIN_MODEL_OPEN = 'ai.tier-main-model.open',+ AI_TIER_OPEN = 'ai.tier.open',+ AI_TIER_SUBMIT = 'ai.tier.submit',API_KEYS_API_KEY_REVOKE_CONFIRM = 'api-keys.api-key-revoke.confirm',API_KEYS_API_KEY_REVOKE_OPEN = 'api-keys.api-key-revoke.open',API_KEYS_API_KEY_COPY = 'api-keys.api-key.copy',diff --git a/packages/web/test/app/routes/platform/setup/ai/config-detail-save.test.tsx b/packages/web/test/app/routes/platform/setup/ai/config-detail-save.test.tsxindex 3368707f048..6b3b97da5c6 100644--- a/packages/web/test/app/routes/platform/setup/ai/config-detail-save.test.tsx+++ b/packages/web/test/app/routes/platform/setup/ai/config-detail-save.test.tsx@@ -106,6 +106,14 @@ vi.mock('@/features/platform-admin', () => ({aiProviderKeys: { configModels: () => ['ai-provider', 'models'] },}));+vi.mock('@/features/platform-admin/hooks/platform-model-tier-hooks', () => ({+ platformModelTierQueries: { useAdminList: () => ({ data: [] }) },+}));++vi.mock('@/features/platform-admin/hooks/ai-provider-hooks', () => ({+ aiProviderQueries: { useAiProviderConfigs: () => ({ data: [] }) },+}));+vi.mock('@/lib/format-utils', () => ({formatUtils: { formatDateToAgo: () => 'just now' },}));@@ -155,7 +163,7 @@ describe('ConfigDetail save (manual models)', () => {let onSave: ReturnType<typeof vi.fn>;const render = () => {- onSave = vi.fn().mockResolvedValue(undefined);+ onSave = vi.fn().mockResolvedValue({ error: null });act(() => {root.render(<ConfigDetaildiff --git a/packages/web/test/app/routes/platform/setup/ai/tiers-tab.test.tsx b/packages/web/test/app/routes/platform/setup/ai/tiers-tab.test.tsxnew file mode 100644index 00000000000..3ccd74e1d0f--- /dev/null+++ b/packages/web/test/app/routes/platform/setup/ai/tiers-tab.test.tsx@@ -0,0 +1,331 @@+/**+ * @vitest-environment jsdom+ */+/* eslint-disable jest-dom/prefer-in-document, jest-dom/prefer-enabled-disabled */+import { AIProviderName } from '@activepieces/core-utils';+import {+ AIProviderModel,+ AIProviderModelType,+ AIProviderWithoutSensitiveData,+ PlatformModelTier,+} from '@activepieces/shared';+import { render, screen } from '@testing-library/react';+import * as React from 'react';+import { MemoryRouter } from 'react-router-dom';+import { beforeEach, describe, expect, it, vi } from 'vitest';++import { TiersTab } from '@/app/routes/platform/setup/ai/tiers-tab';+import { TooltipProvider } from '@/components/ui/tooltip';+import { KeyModelsById } from '@/features/agents/ai-model/model-meta';++const state = vi.hoisted(() => ({+ tiers: {+ data: undefined as PlatformModelTier[] | undefined,+ isLoading: false,+ isError: false,+ refetch: () => Promise.resolve(),+ },+ configs: {+ data: undefined as AIProviderWithoutSensitiveData[] | undefined,+ isLoading: false,+ isError: false,+ refetch: () => Promise.resolve(),+ },+ keyModels: {} as KeyModelsById,+ projects: [] as { id: string; displayName: string }[],+ configuration: {+ data: { aiSpecificModelsVisible: true } as+ | { aiSpecificModelsVisible: boolean }+ | undefined,+ },+}));++vi.mock('sonner', () => ({+ toast: { success: vi.fn(), error: vi.fn() },+}));++vi.mock('i18next', () => ({+ default: { language: 'en' },+ t: (key: string, vars?: Record<string, string | number>) =>+ Object.entries(vars ?? {}).reduce(+ (text, [name, value]) => text.replace(`{${name}}`, String(value)),+ key,+ ),+}));++vi.mock('@/features/platform-admin/hooks/platform-model-tier-hooks', () => ({+ platformModelTierKeys: { admin: ['platform-model-tiers', 'admin'] },+ platformModelTierQueries: {+ useAdminList: () => state.tiers,+ useKeyModels: () => state.keyModels,+ },+ platformModelTierMutations: {+ useCreate: () => mutation(),+ useUpdate: () => mutation(),+ useDelete: () => mutation(),+ useSetSpecificModelsVisible: () => mutation(),+ useReorder: () => mutation(),+ },+}));++vi.mock('@/features/platform-admin/hooks/ai-provider-hooks', () => ({+ aiProviderQueries: { useAiProviderConfigs: () => state.configs },+ aiProviderKeys: {+ configModels: (id: string) => ['ai-provider-config-models', id],+ },+}));++vi.mock('@/features/projects', () => ({+ projectCollectionUtils: {+ useAllPlatformProjects: () => ({ data: state.projects }),+ },+}));+vi.mock('@/hooks/platform-configuration-hooks', () => ({+ platformConfigurationHooks: {+ queryKey: ['platform-configuration'],+ useCurrentPlatformConfiguration: () => ({+ ...state.configuration,+ isLoading: false,+ }),+ },+}));++function mutation() {+ return { mutate: vi.fn(), mutateAsync: vi.fn(), isPending: false };+}++function renderTab() {+ return render(+ <MemoryRouter>+ <TooltipProvider>+ <TiersTab />+ </TooltipProvider>+ </MemoryRouter>,+ );+}++beforeEach(() => {+ state.tiers = {+ data: [],+ isLoading: false,+ isError: false,+ refetch: () => Promise.resolve(),+ };+ state.configs = {+ data: [key('k1')],+ isLoading: false,+ isError: false,+ refetch: () => Promise.resolve(),+ };+ state.keyModels = {+ k1: {+ models: [+ model('sonnet', 'Claude Sonnet'),+ model('haiku', 'Claude Haiku'),+ ],+ isLoading: false,+ isFetching: false,+ isError: false,+ refetch: () => undefined,+ },+ };+ state.configuration = { data: { aiSpecificModelsVisible: true } };+ state.projects = [+ { id: 'A', displayName: 'Sales' },+ { id: 'B', displayName: 'Ops' },+ ];+});++describe('TiersTab states', () => {+ it('shows skeletons while either list loads', () => {+ state.tiers = { ...state.tiers, isLoading: true };+ renderTab();+ expect(screen.getByRole('status', { name: 'Loading tiers' })).toBeDefined();+ });++ it('shows the fetch error placeholder when the tiers list fails', () => {+ state.tiers = { ...state.tiers, data: undefined, isError: true };+ renderTab();+ expect(screen.getByText('Trouble loading tiers')).toBeDefined();+ });++ it('keeps the cards when a refetch fails after data loaded', () => {+ state.tiers = {+ ...state.tiers,+ isError: true,+ data: [+ tier('expert', {+ name: 'Expert',+ entries: [{ configId: 'k1', modelId: 'sonnet' }],+ }),+ ],+ };+ renderTab();+ expect(screen.getByText('Expert')).toBeDefined();+ expect(screen.queryByText('Trouble loading tiers')).toBeNull();+ });++ it('still shows existing tiers when the platform has no own keys left', () => {+ state.configs = { ...state.configs, data: [] };+ state.tiers = {+ ...state.tiers,+ data: [tier('expert', { name: 'Expert', entries: [] })],+ };+ renderTab();+ expect(screen.getByText('Expert')).toBeDefined();+ expect(screen.queryByText('Add a provider key first')).toBeNull();+ });++ it('asks for a provider key when the platform only has the credits key', () => {+ state.configs = {+ ...state.configs,+ data: [key('ap', { provider: AIProviderName.ACTIVEPIECES })],+ };+ renderTab();+ expect(screen.getByText('Add a provider key first')).toBeDefined();+ expect(screen.queryByText('New tier')).toBeNull();+ });++ it('offers the first tier and keeps specific models visible while there are none', () => {+ renderTab();+ expect(screen.getByText('Create your first tier')).toBeDefined();+ const hide = screen.getByRole('tab', { name: 'Hidden' });+ expect(hide.getAttribute('data-disabled')).not.toBeNull();+ expect(screen.getByText('Add a tier before hiding these.')).toBeDefined();+ });++ it('renders cards with badges, the no-fallback hint and the fallback cap', () => {+ state.tiers = {+ ...state.tiers,+ data: [+ tier('expert', {+ name: 'Expert',+ isDefault: true,+ isFast: false,+ thinkingBudget: 8000,+ entries: [{ configId: 'k1', modelId: 'sonnet' }],+ }),+ tier('full', {+ name: 'Full',+ isDefault: false,+ isFast: true,+ entries: ['a', 'b', 'c', 'd', 'e'].map((modelId) => ({+ configId: 'k1',+ modelId,+ })),+ }),+ tier('orphan', {+ name: 'Orphan',+ isDefault: false,+ isFast: false,+ entries: [{ configId: 'missing', modelId: 'x' }],+ }),+ ],+ };+ renderTab();+ expect(screen.getByText('Expert')).toBeDefined();+ expect(screen.getByText('Default')).toBeDefined();+ expect(screen.getByText('Fast')).toBeDefined();+ expect(screen.getByText('Thinking: 8,000')).toBeDefined();+ expect(screen.getAllByText('No fallback model')).toHaveLength(2);+ expect(screen.getByText('Up to 4 fallbacks')).toBeDefined();+ expect(screen.getAllByText('Add fallback')).toHaveLength(2);+ expect(screen.getAllByRole('button', { name: 'Warnings' })).toHaveLength(6);+ expect(screen.getByText('Claude Haiku')).toBeDefined();+ });++ it('tells the admin builders only see tiers when specific models are hidden', () => {+ state.tiers = {+ ...state.tiers,+ data: [+ tier('expert', { entries: [{ configId: 'k1', modelId: 'sonnet' }] }),+ ],+ };+ state.configuration = { data: { aiSpecificModelsVisible: false } };+ renderTab();+ expect(screen.getByText('Builders only see tiers.')).toBeDefined();+ expect(screen.getByText('Claude Haiku')).toBeDefined();+ });++ it('warns when a tier is missing from projects its main key does not serve', () => {+ state.configs = {+ ...state.configs,+ data: [key('k1', { projectScope: 'selected', projectIds: ['A'] })],+ };+ state.tiers = {+ ...state.tiers,+ data: [+ tier('expert', { entries: [{ configId: 'k1', modelId: 'sonnet' }] }),+ ],+ };+ renderTab();+ expect(screen.getByText('tierUnavailableInProjects')).toBeDefined();+ expect(screen.getByText('projectsCount')).toBeDefined();+ expect(+ screen.getByRole('combobox', { name: 'Preview as project' }),+ ).toBeDefined();+ expect(screen.getByText('hidingLeavesProjectsEmpty')).toBeDefined();+ });++ it('shows no scope notices or preview when every key serves every project', () => {+ state.tiers = {+ ...state.tiers,+ data: [+ tier('expert', { entries: [{ configId: 'k1', modelId: 'sonnet' }] }),+ ],+ };+ renderTab();+ expect(screen.queryByText('tierUnavailableInProjects')).toBeNull();+ expect(+ screen.queryByRole('combobox', { name: 'Preview as project' }),+ ).toBeNull();+ });+});++function model(id: string, name: string): AIProviderModel {+ return { id, name, type: AIProviderModelType.TEXT };+}++function key(+ id: string,+ overrides: Partial<AIProviderWithoutSensitiveData> = {},+): AIProviderWithoutSensitiveData {+ return {+ id,+ name: `Key ${id}`,+ provider: AIProviderName.ANTHROPIC,+ config: {},+ enabledForChat: false,+ modelScope: 'all',+ modelIds: [],+ projectScope: 'all',+ projectIds: [],+ status: 'active',+ statusReason: null,+ statusUpdated: null,+ ...overrides,+ };+}++function tier(+ id: string,+ overrides: Partial<PlatformModelTier>,+): PlatformModelTier {+ return {+ id,+ created: '2026-01-01T00:00:00.000Z',+ updated: '2026-01-01T00:00:00.000Z',+ platformId: 'p',+ name: id,+ emoji: '⚡',+ description: null,+ position: 0,+ entries: [],+ isDefault: false,+ isFast: false,+ thinkingBudget: null,+ deleted: null,+ replacedBy: null,+ ...overrides,+ };+}diff --git a/packages/web/test/features/agents/ai-model/model-meta.test.ts b/packages/web/test/features/agents/ai-model/model-meta.test.tsnew file mode 100644index 00000000000..a1b6b8498a1--- /dev/null+++ b/packages/web/test/features/agents/ai-model/model-meta.test.ts@@ -0,0 +1,429 @@+import { AIProviderName } from '@activepieces/core-utils';+import {+ AIProviderModel,+ AIProviderModelType,+ AIProviderWithoutSensitiveData,+ PlatformModelTier,+} from '@activepieces/shared';+import { describe, expect, it, vi } from 'vitest';++import {+ KeyModelsById,+ modelMeta,+} from '@/features/agents/ai-model/model-meta';++vi.mock('i18next', () => ({+ default: { language: 'en' },+ t: (key: string, vars?: Record<string, string | number>) =>+ Object.entries(vars ?? {}).reduce(+ (text, [name, value]) => text.replace(`{${name}}`, String(value)),+ key,+ ),+}));++describe('modelMeta formatting', () => {+ it('formats context windows compactly and prices without noise', () => {+ expect(modelMeta.formatContext({ tokens: 200_000 })).toBe('200K');+ expect(modelMeta.formatContext({ tokens: 1_000_000 })).toBe('1M');+ expect(modelMeta.formatPrice({ perMillion: 0.8 })).toBe('$0.80');+ expect(modelMeta.formatPrice({ perMillion: 4 })).toBe('$4');+ expect(modelMeta.formatPrice({ perMillion: 1.75 })).toBe('$1.75');+ });++ it('builds the meta line only from the metadata that exists', () => {+ expect(modelMeta.metaParts({ model: model('a', {}) })).toEqual([]);+ expect(+ modelMeta.metaParts({+ model: model('a', { contextTokens: 200_000 }),+ }),+ ).toEqual(['200K context']);+ expect(+ modelMeta.metaParts({+ model: model('a', {+ contextTokens: 200_000,+ inputCostPerMillionTokens: 0.8,+ outputCostPerMillionTokens: 4,+ }),+ }),+ ).toEqual(['200K context', '$0.80 / $4 per 1M']);+ });+});++describe('modelMeta.scopedTextModels', () => {+ it('keeps text models the key allows and drops image models', () => {+ const config = key('k1', { modelScope: 'selected', modelIds: ['a'] });+ const models = [+ model('a', {}),+ model('b', {}),+ { ...model('c', {}), type: AIProviderModelType.IMAGE },+ ];+ expect(+ modelMeta.scopedTextModels({ config, models }).map((m) => m.id),+ ).toEqual(['a']);+ expect(+ modelMeta+ .scopedTextModels({ config: key('k2', {}), models })+ .map((m) => m.id),+ ).toEqual(['a', 'b']);+ });+});++describe('modelMeta.rankForFallback', () => {+ const main = model('main', {+ contextTokens: 1_000_000,+ supportsToolCalling: true,+ });+ const candidates = [+ {+ config: key('k1', {}),+ model: model('full', {+ contextTokens: 1_000_000,+ supportsToolCalling: true,+ }),+ },+ {+ config: key('k1', {}),+ model: model('small', {+ contextTokens: 200_000,+ supportsToolCalling: true,+ }),+ },+ {+ config: key('k1', {}),+ model: model('notools', {+ contextTokens: 1_000_000,+ supportsToolCalling: false,+ }),+ },+ { config: key('k1', {}), model: model('unknown', undefined) },+ ];++ it('splits full matches, trade-offs and models without metadata', () => {+ const groups = modelMeta.rankForFallback({ main, candidates });+ expect(groups.map((group) => group.kind)).toEqual([+ 'full',+ 'tradeOff',+ 'other',+ ]);+ expect(groups[0].items.map((item) => item.model.id)).toEqual(['full']);+ expect(groups[1].items.map((item) => item.model.id)).toEqual([+ 'small',+ 'notools',+ ]);+ expect(groups[1].items[0].tradeOffs).toEqual([+ { code: 'smallerContext', contextTokens: 200_000 },+ ]);+ expect(groups[1].items[1].tradeOffs).toEqual([{ code: 'noToolCalling' }]);+ expect(groups[2].items.map((item) => item.model.id)).toEqual(['unknown']);+ });++ it('falls back to one flat list when the main model has no metadata', () => {+ const groups = modelMeta.rankForFallback({+ main: model('main', undefined),+ candidates,+ });+ expect(groups.map((group) => group.kind)).toEqual(['all']);+ expect(groups[0].items).toHaveLength(4);+ });+});++describe('modelMeta.warningsFor', () => {+ const keyModels: KeyModelsById = {+ k1: {+ models: [+ model('main', { contextTokens: 1_000_000 }),+ model('small', { contextTokens: 200_000 }),+ ],+ isLoading: false,+ isFetching: false,+ isError: false,+ refetch: () => undefined,+ },+ k2: {+ models: undefined,+ isLoading: true,+ isFetching: true,+ isError: false,+ refetch: () => undefined,+ },+ };+ const mainModel = model('main', { contextTokens: 1_000_000 });++ it('warns when the model left the key, the context is smaller, or the key is unhealthy', () => {+ expect(+ modelMeta.warningsFor({+ entry: { configId: 'k1', modelId: 'gone' },+ isMain: false,+ config: key('k1', { status: 'out_of_credits' }),+ keyModels,+ mainModel,+ }),+ ).toEqual([+ { code: 'modelGone', keyName: 'Key k1' },+ { code: 'keyStatus', status: 'out_of_credits' },+ ]);+ expect(+ modelMeta.warningsFor({+ entry: { configId: 'k1', modelId: 'small' },+ isMain: false,+ config: key('k1', {}),+ keyModels,+ mainModel,+ }),+ ).toEqual([+ {+ code: 'smallerContext',+ contextTokens: 200_000,+ mainContextTokens: 1_000_000,+ },+ ]);+ });++ it('stays quiet while the key list is loading and flags a missing key', () => {+ expect(+ modelMeta.warningsFor({+ entry: { configId: 'k2', modelId: 'anything' },+ isMain: false,+ config: key('k2', {}),+ keyModels,+ mainModel,+ }),+ ).toEqual([]);+ expect(+ modelMeta.warningsFor({+ entry: { configId: 'missing', modelId: 'x' },+ isMain: true,+ config: undefined,+ keyModels,+ mainModel,+ }),+ ).toEqual([{ code: 'keyMissing' }]);+ });+});++describe('modelMeta.specificModelsOf', () => {+ it('lists scoped text models that are not a tier main model, skipping the credits key', () => {+ const configs = [+ key('k1', {}),+ key('ap', { provider: AIProviderName.ACTIVEPIECES }),+ ];+ const keyModels: KeyModelsById = {+ k1: {+ models: [model('a', {}), model('b', {})],+ isLoading: false,+ isFetching: false,+ isError: false,+ refetch: () => undefined,+ },+ ap: {+ models: [model('z', {})],+ isLoading: false,+ isFetching: false,+ isError: false,+ refetch: () => undefined,+ },+ };+ const tiers = [+ tier('t1', [+ { configId: 'k1', modelId: 'a' },+ { configId: 'k1', modelId: 'b' },+ ]),+ ];+ expect(+ modelMeta+ .specificModelsOf({ configs, keyModels, tiers })+ .map(({ model }) => model.id),+ ).toEqual(['b']);+ });+});++describe('modelMeta entry operations', () => {+ const entries = [+ { configId: 'k', modelId: 'm0' },+ { configId: 'k', modelId: 'm1' },+ { configId: 'k', modelId: 'm2' },+ ];++ it('replaces the main model and drops it from the fallbacks if it was one', () => {+ expect(+ modelMeta.replaceMain({+ entries,+ entry: { configId: 'k', modelId: 'm1' },+ }),+ ).toEqual([+ { configId: 'k', modelId: 'm1' },+ { configId: 'k', modelId: 'm2' },+ ]);+ });++ it('moves a fallback to the top so it becomes the main model', () => {+ expect(+ modelMeta.moveEntry({ entries, from: 2, to: 0 }).map((e) => e.modelId),+ ).toEqual(['m2', 'm0', 'm1']);+ expect(modelMeta.moveEntry({ entries, from: 0, to: 5 })).toBe(entries);+ });++ it('removes by index and never adds a duplicate fallback', () => {+ expect(+ modelMeta.removeAt({ entries, index: 1 }).map((e) => e.modelId),+ ).toEqual(['m0', 'm2']);+ expect(+ modelMeta.addFallback({+ entries,+ entry: { configId: 'k', modelId: 'm1' },+ }),+ ).toBe(entries);+ expect(+ modelMeta.addFallback({+ entries,+ entry: { configId: 'k', modelId: 'm3' },+ }),+ ).toHaveLength(4);+ });+});++describe('modelMeta project scope', () => {+ const open = key('open', {});+ const onlyA = key('onlyA', { projectScope: 'selected', projectIds: ['A'] });+ const notA = key('notA', { projectScope: 'except', projectIds: ['A'] });+ const configsById = new Map([open, onlyA, notA].map((config) => [config.id, config]));+ const projectIds = ['A', 'B', 'C'];++ it('mirrors the server rule for which projects a key serves', () => {+ expect(projectIds.filter((projectId) => modelMeta.keyServesProject({ config: onlyA, projectId }))).toEqual(['A']);+ expect(projectIds.filter((projectId) => modelMeta.keyServesProject({ config: notA, projectId }))).toEqual(['B', 'C']);+ expect(projectIds.filter((projectId) => modelMeta.keyServesProject({ config: open, projectId }))).toEqual(projectIds);+ });++ it('counts the projects a tier is missing from and the fallbacks skipped where it runs', () => {+ const reach = modelMeta.tierReach({+ tier: tier('t', [+ { configId: notA.id, modelId: 'main' },+ { configId: onlyA.id, modelId: 'fallback' },+ ]),+ configsById,+ projectIds,+ });++ expect(reach.unavailableCount).toBe(1);+ expect(reach.skippedFallbacks).toEqual([+ { entry: { configId: onlyA.id, modelId: 'fallback' }, projectCount: 2 },+ ]);+ });++ it('counts projects left with no tier at all', () => {+ expect(+ modelMeta.projectsWithoutTier({+ tiers: [tier('t', [{ configId: onlyA.id, modelId: 'main' }])],+ configsById,+ projectIds,+ }),+ ).toBe(2);+ });++ it('reports what narrowing a key does to the tiers that use it', () => {+ const tiers = [+ { ...tier('Expert', [{ configId: open.id, modelId: 'main' }]), name: 'Expert' },+ {+ ...tier('Fast', [+ { configId: notA.id, modelId: 'main' },+ { configId: open.id, modelId: 'fallback' },+ ]),+ name: 'Fast',+ },+ { ...tier('Other', [{ configId: notA.id, modelId: 'main' }]), name: 'Other' },+ ];++ const impact = modelMeta.keyScopeImpact({+ tiers,+ configsById,+ config: { ...open, projectScope: 'selected', projectIds: ['A'] },+ projectIds,+ });++ expect(impact).toEqual([+ { tierName: 'Expert', lostProjects: 2, skippedProjects: 0 },+ { tierName: 'Fast', lostProjects: 0, skippedProjects: 2 },+ ]);+ });++ it('flags a main model that cannot call tools and recommends a curated one', () => {+ const openAi = key('k', { provider: AIProviderName.OPENAI });+ expect(+ modelMeta.toolsVerdict({+ config: openAi,+ entry: { configId: 'k', modelId: 'x' },+ model: model('x', { supportsToolCalling: false }),+ }),+ ).toBe('noTools');+ expect(+ modelMeta.toolsVerdict({+ config: openAi,+ entry: { configId: 'k', modelId: 'gpt-4.1' },+ model: model('gpt-4.1', {}),+ }),+ ).toBe('recommended');+ expect(+ modelMeta.toolsVerdict({+ config: openAi,+ entry: { configId: 'k', modelId: 'some-model' },+ model: undefined,+ }),+ ).toBeNull();+ });+});++function model(+ id: string,+ metadata: AIProviderModel['metadata'] | Record<string, never>,+): AIProviderModel {+ return {+ id,+ name: id,+ type: AIProviderModelType.TEXT,+ ...(metadata === undefined ? {} : { metadata }),+ };+}++function key(+ id: string,+ overrides: Partial<AIProviderWithoutSensitiveData>,+): AIProviderWithoutSensitiveData {+ return {+ id,+ name: `Key ${id}`,+ provider: AIProviderName.OPENAI,+ config: {},+ enabledForChat: false,+ modelScope: 'all',+ modelIds: [],+ projectScope: 'all',+ projectIds: [],+ status: 'active',+ statusReason: null,+ statusUpdated: null,+ ...overrides,+ };+}++function tier(+ id: string,+ entries: PlatformModelTier['entries'],+): PlatformModelTier {+ return {+ id,+ created: '2026-01-01T00:00:00.000Z',+ updated: '2026-01-01T00:00:00.000Z',+ platformId: 'p',+ name: id,+ emoji: '⚡',+ description: null,+ position: 0,+ entries,+ isDefault: true,+ isFast: true,+ thinkingBudget: null,+ deleted: null,+ replacedBy: null,+ };+}diff --git a/packages/web/test/features/agents/ai-model/model-picker-popover.test.tsx b/packages/web/test/features/agents/ai-model/model-picker-popover.test.tsxnew file mode 100644index 00000000000..db2d0d3dd86--- /dev/null+++ b/packages/web/test/features/agents/ai-model/model-picker-popover.test.tsx@@ -0,0 +1,119 @@+/**+ * @vitest-environment jsdom+ */+/* eslint-disable no-var, testing-library/no-unnecessary-act */+import * as React from 'react';+import { act } from 'react';+import { createRoot, type Root } from 'react-dom/client';+import { afterEach, describe, expect, it, vi } from 'vitest';++vi.mock('i18next', () => ({+ t: (key: string, vars?: Record<string, string | number>) =>+ Object.entries(vars ?? {}).reduce(+ (text, [name, value]) => text.replace(`{${name}}`, String(value)),+ key,+ ),+}));++import {+ ModelPickerGroup,+ ModelPickerPopover,+} from '@/features/agents/ai-model/model-picker-popover';++declare global {+ var IS_REACT_ACT_ENVIRONMENT: boolean;+}++Element.prototype.scrollIntoView = () => undefined;+globalThis.ResizeObserver = class {+ observe() {}+ unobserve() {}+ disconnect() {}+} as never;+if (!globalThis.PointerEvent) {+ globalThis.PointerEvent = MouseEvent as never;+}+globalThis.IS_REACT_ACT_ENVIRONMENT = true;++const items = (prefix: string, count: number) =>+ Array.from({ length: count }, (_, index) => ({+ id: `${prefix}-${index}`,+ value: `${prefix}-${index}`,+ name: `${prefix} model ${index}`,+ searchText: `${prefix} model ${index}`,+ disabled: index === 0,+ }));++const groups: ModelPickerGroup<string>[] = [+ { id: 'small', heading: 'Small key', items: items('alpha', 3) },+ { id: 'big', heading: 'Big key', items: items('beta', 35) },+];++const rows = () => document.querySelectorAll('[cmdk-item]');+const bodyText = () => document.body.textContent ?? '';++function typeSearch(text: string) {+ const input = document.querySelector<HTMLInputElement>('[cmdk-input]');+ if (input === null) {+ throw new Error('search input missing');+ }+ const setter = Object.getOwnPropertyDescriptor(+ HTMLInputElement.prototype,+ 'value',+ )?.set;+ act(() => {+ setter?.call(input, text);+ input.dispatchEvent(new Event('input', { bubbles: true }));+ });+}++describe('ModelPickerPopover', () => {+ let root: Root | undefined;++ afterEach(() => {+ act(() => root?.unmount());+ document.body.innerHTML = '';+ });++ const mount = () => {+ act(() => {+ root = createRoot(document.body);+ root.render(+ <ModelPickerPopover+ groups={groups}+ emptyText="No models match"+ onPick={() => undefined}+ open+ onOpenChange={() => undefined}+ >+ <button type="button">Pick</button>+ </ModelPickerPopover>,+ );+ });+ };++ it('caps a long group and offers to show the rest', () => {+ mount();+ expect(rows()).toHaveLength(3 + 30 + 1);+ expect(bodyText()).toContain('Show all 35');+ expect(+ document.querySelector('[cmdk-item][aria-disabled="true"]'),+ ).not.toBeNull();+ });++ it('filters with its own matcher and drops empty groups', async () => {+ mount();+ typeSearch('alpha');+ await act(async () => {+ await Promise.resolve();+ });+ expect(rows()).toHaveLength(3);+ expect(bodyText()).not.toContain('Big key');+ typeSearch('nothing-here');+ await act(async () => {+ await Promise.resolve();+ });+ expect(rows()).toHaveLength(0);+ expect(bodyText()).toContain('No models match');+ });+});