From ccb68cf0f5fa7297b2d7eb7e49f6c1f7dead96f2 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Sun, 18 Oct 2026 12:06:22 +0000 Subject: [PATCH 1/2] feat(scaling): add providers and make groups in Settings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Settings › Auto scaling lists the providers with their last check in words, Check again, and Remove with Undo (the delete waits a few seconds, or runs as the page is left, since sealed credentials can't be put back). Add a provider picks a driver and fills a form drawn from its fields: defaults filled, the name focused, secrets as password fields sent in `secrets`, lists one per line or ticked from their options, Enter saves, and the driver's extra and licence named; a failed check is shown in words. New group on a provider asks for a name, min, max, a budget only when the driver is priced, and the driver's template; rules stay with the assistant, MCP and the API for now. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01ALjNbLobvPe7VwvDL8j7V3 --- README.md | 17 +- changelog.d/scaling-forms.md | 6 + docs/requirements.md | 1 + docs/scaling.md | 36 +- .../__tests__/settings-scaling.test.tsx | 174 +++++- .../components/settings/bodies.tsx | 8 +- .../components/settings/scaling-panel.tsx | 586 +++++++++++++++++- 7 files changed, 801 insertions(+), 27 deletions(-) create mode 100644 changelog.d/scaling-forms.md diff --git a/README.md b/README.md index 80b144ff2..7ccf31922 100644 --- a/README.md +++ b/README.md @@ -237,13 +237,12 @@ listed under [In progress](#in-progress), and what comes next under [Planned](#p [the meter](docs/budgets.md#the-meter-and-the-costs-page); [checks](docs/requirements.md#see-what-it-costs)) - **Scale out.** Scaling groups add workers when work waits and let them go when it doesn't, by target, step and schedule rules, within min and max and a monthly budget, with every launch, join and hour of cost in the activity - ledger. A group launches nothing until you approve its terms in Settings › Auto scaling; agents propose and manage - groups over the API, MCP and the assistant, and never approve one. It runs more workers on this machine today; - AWS EC2 (spot or on demand), DigitalOcean, Hetzner Cloud, Proxmox VE and Linux hosts over SSH providers are in - progress (built, with machines that join over the mesh to transcribe and tell voices apart, not yet tried on a real - cloud), so is RunPod for GPUs that serve Lens's model calls while they run, and more model hosts are planned. - ([Auto scaling](docs/scaling.md); - [checks](docs/requirements.md#scale-out)) + ledger. You add providers and make groups in Settings › Auto scaling, and a group launches nothing until you approve + its terms there; agents propose and manage groups over the API, MCP and the assistant, and never approve one. It + runs more workers on this machine today; AWS EC2 (spot or on demand), DigitalOcean, Hetzner Cloud, Proxmox VE and + Linux hosts over SSH providers are in progress (built, with machines that join over the mesh to transcribe and tell + voices apart, not yet tried on a real cloud), so is RunPod for GPUs that serve Lens's model calls while they run, + and more model hosts are planned. ([Auto scaling](docs/scaling.md); [checks](docs/requirements.md#scale-out)) - **Run on more machines.** Add a Mac with a GPU, a box for the assistant's code or a server with more disk, and Lens joins it over a private network of its own, a Nebula mesh it issues the certificates for: one install line with a one-time token, and the machine needs nothing else. Worker nodes run jobs as their own user of the @@ -320,8 +319,8 @@ Next, and not in a pull request yet; each links to where it is tracked: topics, recordings and the other things that have pages ([notes](docs/notes.md)). - **Local models:** GPU builds of llama.cpp fetched by Lens, and local embedding models ([local models](docs/local-models.md)). -- **Auto scaling:** spreading model calls across GPUs, more model hosts (Lambda, Hugging Face, SageMaker, Bedrock), - and making providers and groups in Settings ([auto scaling](docs/scaling.md)). +- **Auto scaling:** spreading model calls across GPUs, and more model hosts (Lambda, Hugging Face, SageMaker, + Bedrock) ([auto scaling](docs/scaling.md)). - **Speech providers:** their summaries, chapters and entities as optional imports, and per-minute prices in the activity ledger ([speech providers](docs/speech-providers.md)). - **Storage:** uploads and assets' media in the file store, and a storage key to download diff --git a/changelog.d/scaling-forms.md b/changelog.d/scaling-forms.md new file mode 100644 index 000000000..b4a0cc37d --- /dev/null +++ b/changelog.d/scaling-forms.md @@ -0,0 +1,6 @@ +- **Add providers and make scaling groups in Settings.** Settings › Auto scaling lists the providers with their last + check in words, Check again, and Remove with Undo. Add a provider picks the kind, then fills a form drawn from the + driver's fields (defaults filled, credentials as password fields, Enter saves) and shows the check it runs. New group + on a provider asks for a name, the minimum and maximum, a monthly budget when the provider charges, and what each + unit is; a group made there is approved as it's made. Rules stay with the assistant, MCP and the API for now + ([In Settings](docs/scaling.md#in-settings)). diff --git a/docs/requirements.md b/docs/requirements.md index 03eefa0ad..8c51436e6 100644 --- a/docs/requirements.md +++ b/docs/requirements.md @@ -287,6 +287,7 @@ request, to another check that proves the same thing; loosening a requirement or | The scaler launches units, sees them join, drains busy workers before letting them go, and fails and pauses on units that don't come up | [test_scaling.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_scaling.py) `test_the_scaler_launches_joins_drains_and_terminates`, `test_a_unit_that_never_joins_fails_and_failures_pause_the_group`, `test_protected_and_terminated_units` | | Each unit's cost goes into the activity ledger as an estimate, and a group stops at its budget's cap | [test_scaling.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_scaling.py) `test_the_scaler_launches_joins_drains_and_terminates`, `test_at_its_budgets_cap_a_group_stops` | | Settings › Auto scaling shows each group's terms in words and approves them in one click | [settings-scaling.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/settings-scaling.test.tsx) `shows each group's terms and approves them in one click`, `puts the terms and the state in words` | +| Settings › Auto scaling adds a provider from its driver's fields (credentials sent apart, never shown), shows its check in words, removes it with Undo, and makes a group, with a budget when the provider charges | [settings-scaling.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/settings-scaling.test.tsx) `adds a provider from its driver's fields, with the secret sent apart`, `shows a failed check in words, after saving and in the list`, `makes a group with a budget on a priced provider`, `asks no budget on a free provider and ticks a list's options`, `removes a provider with Undo instead of a question` | | This machine runs more workers at no cost | [test_scaling.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_scaling.py) `test_this_machine_runs_more_workers_at_no_cost` | | DigitalOcean and Hetzner Cloud: credentials checked, list prices, machines launched with a one-use join token, watched, found by tag and deleted; a group waits for the mesh's install line before launching | [test_scaling_clouds.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_scaling_clouds.py) `test_a_digitalocean_group_waits_for_the_install_line_then_launches_droplets`, `test_hetzner_prices_in_euros_and_its_errors_read_as_hetzners`, `test_the_cloud_drivers_are_listed_and_need_the_mesh` | | A group of machines elsewhere counts only the jobs a worker node takes, and never launches one for others | [test_scaling_clouds.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_scaling_clouds.py) `test_a_group_elsewhere_counts_only_the_jobs_a_node_takes` | diff --git a/docs/scaling.md b/docs/scaling.md index da6d8c7a7..8284984e8 100644 --- a/docs/scaling.md +++ b/docs/scaling.md @@ -7,14 +7,14 @@ launched. Everything it does is in the [activity ledger](activity.md), and its c [budget](budgets.md). Status: in progress. Built: groups, rules, approvals, budgets, the scaler, the API, MCP and assistant tools, -Settings › Auto scaling (the switch, and each group's terms to approve or pause), and **This machine**, a provider that -runs more `lens worker` processes where Lens runs. In progress: **AWS EC2** (spot or on demand), **DigitalOcean** -and **Hetzner Cloud** providers are built (credentials checked, prices, launching, watching and deleting machines), -and their machines join over the [mesh](mesh.md) as worker nodes that transcribe and tell voices apart; so are -**Proxmox VE**, clones of a VM template on your own server, and **Linux hosts over SSH**, workers started on machines -you have. They haven't been tried on a real cloud machine yet. Also in progress: **RunPod**, GPUs that serve a model -with vLLM, which Lens's model calls go to while one runs. Planned: more model hosts (Lambda, Hugging Face, SageMaker, -Bedrock); and making providers and groups in Settings. +Settings › Auto scaling (the switch, [adding providers and making groups](#in-settings), and each group's terms to +approve or pause), and **This machine**, a provider that runs more `lens worker` processes where Lens runs. In +progress: **AWS EC2** (spot or on demand), **DigitalOcean** and **Hetzner Cloud** providers are built (credentials +checked, prices, launching, watching and deleting machines), and their machines join over the [mesh](mesh.md) as +worker nodes that transcribe and tell voices apart; so are **Proxmox VE**, clones of a VM template on your own server, +and **Linux hosts over SSH**, workers started on machines you have. They haven't been tried on a real cloud machine +yet. Also in progress: **RunPod**, GPUs that serve a model with vLLM, which Lens's model calls go to while one runs. +Planned: more model hosts (Lambda, Hugging Face, SageMaker, Bedrock). Auto scaling is **off** until an admin turns it on, and a group launches nothing until a person approves its terms signed in to the web app. Agents can propose and manage groups, but never approve one. @@ -40,6 +40,23 @@ One process runs the scaler at a time: the API and every `lens worker` (except t `--no-schedule`, as scaled workers are) take turns holding its lease. While scaling is off and nothing is left to let go, the scaler doesn't even take the lease. +## In Settings + +**Settings › Auto scaling** lists the providers, each with its kind, which credentials are set and whether its last +check passed (and why not, in words when it failed), with **Check again** and **Remove**. Remove takes it out of the +list at once with **Undo** on the notice; it's deleted a few seconds later, or as you leave the page, since its sealed +credentials can't be put back once it's gone. A provider a group uses can't be removed until the group goes. + +**Add a provider** asks for the kind (each with what it does), then a form drawn from the driver's `fields`: defaults +filled in, the name first and focused, credentials as password fields (sent as `secrets`, never shown again), lists +one per line, on and off settings as checkboxes, and Enter saves. A driver that needs packages says which, and their +licence, before it fetches them on the first save. The check runs as it's saved; when it fails, the dialog says why. + +**New group** on a provider asks for a name (` workers`), the minimum (0) and maximum (1), a monthly budget +in USD only when the provider charges money (then it's required), and the driver's `template` with its defaults. A +group made here is approved as it's made. Its rules are set by the assistant, MCP or the API for now; without rules it +keeps its minimum running. + ## Providers ```http @@ -271,4 +288,5 @@ preemptible and spot instances at clouds other than AWS and RunPod; draining an (the node agent reading the instance's metadata) and handing its job back at once; following the spot price as it changes; AWS sign-in with an instance role or IAM Identity Center instead of keys; mixed instance types per group; warm pools; predictive scaling; health checks beyond -joining; prices fetched from providers' pricing APIs. +joining; prices fetched from providers' pricing APIs. In Settings: rules, changing a provider or a group, removing a +group, and a provider's price and note. diff --git a/nextjs-frontend/__tests__/settings-scaling.test.tsx b/nextjs-frontend/__tests__/settings-scaling.test.tsx index 87dcd1c1b..fd373ae51 100644 --- a/nextjs-frontend/__tests__/settings-scaling.test.tsx +++ b/nextjs-frontend/__tests__/settings-scaling.test.tsx @@ -1,9 +1,9 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; -import { fireEvent, render, screen, waitFor } from "@testing-library/react"; +import { act, fireEvent, render, screen, waitFor } from "@testing-library/react"; import "@testing-library/jest-dom"; import { Scaling } from "@/app/openapi-client"; -import type { Group, Provider } from "@/app/openapi-client/types.gen"; +import type { DriverSpec, Group, Provider } from "@/app/openapi-client/types.gen"; import { groupState, termsText } from "@/components/settings/scaling-panel"; import { SettingsSection } from "@/components/settings/section"; import type { SettingsView } from "@/components/settings/model"; @@ -11,13 +11,24 @@ import { TooltipProvider } from "@/components/ui/tooltip"; jest.mock("@/app/openapi-client", () => ({ Admin: { updateSettings: jest.fn() }, - Scaling: { listGroups: jest.fn(), listProviders: jest.fn(), approveGroup: jest.fn(), pauseGroup: jest.fn() }, + Scaling: { + listGroups: jest.fn(), + listProviders: jest.fn(), + listDrivers: jest.fn(), + addProvider: jest.fn(), + checkProvider: jest.fn(), + removeProvider: jest.fn(), + createGroup: jest.fn(), + approveGroup: jest.fn(), + pauseGroup: jest.fn(), + }, Metadata: {}, })); jest.mock("next-auth/react", () => ({ useSession: () => ({ data: { accessToken: "t" } }) })); jest.mock("next/navigation", () => ({ useRouter: () => ({ push: jest.fn() }) })); jest.mock("@/lib/hooks/session", () => ({ useArchive: () => ({ namespaces: [] }) })); -jest.mock("@/components/ui/toast", () => ({ useToast: () => jest.fn() })); +const mockToast = jest.fn(); +jest.mock("@/components/ui/toast", () => ({ useToast: () => mockToast })); const ok = (data: unknown) => Promise.resolve({ data, response: { ok: true, status: 200 } }); const VIEW = { @@ -27,6 +38,34 @@ const PROVIDERS = [ { id: 1, name: "Here", driver: "local", priced: false, settings: {}, secrets: {} }, { id: 2, name: "Cloud", driver: "fake", priced: true, settings: {}, secrets: { api_key: true } }, ] as Provider[]; +const DRIVERS = [ + { + id: "local", + kind: "node", + label: "This machine", + about: "Runs more Lens workers here.", + priced: false, + ready: true, + fields: {}, + template: { steps: { label: "Steps", type: "list", options: ["embed", "transcribe"] } }, + }, + { + id: "fake", + kind: "node", + label: "Fake cloud", + about: "Machines on a pretend cloud.", + priced: true, + extra: "fakecloud", + license: "MIT", + ready: false, + fields: { + region: { label: "Region", type: "string", required: true, default: "ams3" }, + token: { label: "API token", type: "string", required: true, secret: true }, + private: { label: "Private network", type: "boolean", default: true }, + }, + template: { size: { label: "Size", type: "string", default: "small" } }, + }, +] as DriverSpec[]; const GROUP = { id: 7, name: "Transcribers", @@ -61,7 +100,9 @@ function show() { } beforeEach(() => { + jest.clearAllMocks(); (Scaling.listProviders as jest.Mock).mockImplementation(() => ok(PROVIDERS)); + (Scaling.listDrivers as jest.Mock).mockImplementation(() => ok(DRIVERS)); }); describe("auto scaling settings", () => { @@ -93,4 +134,129 @@ describe("auto scaling settings", () => { "2 of 3 running, 1 joining", ); }); + + it("adds a provider from its driver's fields, with the secret sent apart", async () => { + (Scaling.listGroups as jest.Mock).mockImplementation(() => ok([])); + (Scaling.addProvider as jest.Mock).mockImplementation(({ body }) => + ok({ id: 3, ...body, priced: true, secrets: { token: true }, check: { ok: true } }), + ); + show(); + fireEvent.click(await screen.findByRole("button", { name: "Add a provider" })); + fireEvent.click(await screen.findByRole("button", { name: /Fake cloud/ })); + expect(screen.getByText(/Needs the fakecloud packages \(MIT\)/)).toBeInTheDocument(); + expect(screen.getByLabelText("Name")).toHaveValue("Fake cloud"); + expect(screen.getByLabelText("Name")).toHaveFocus(); + expect(screen.getByLabelText(/Region/)).toHaveValue("ams3"); + const token = screen.getByLabelText(/API token/); + expect(token).toHaveAttribute("type", "password"); + fireEvent.change(token, { target: { value: "s3cret" } }); + fireEvent.keyDown(token, { key: "Enter" }); + await waitFor(() => expect(Scaling.addProvider).toHaveBeenCalled()); + expect((Scaling.addProvider as jest.Mock).mock.calls[0][0].body).toEqual({ + name: "Fake cloud", + driver: "fake", + settings: { region: "ams3", private: true }, + secrets: { token: "s3cret" }, + }); + await waitFor(() => expect(mockToast).toHaveBeenCalledWith(expect.objectContaining({ title: "Added Fake cloud" }))); + }); + + it("shows a failed check in words, after saving and in the list", async () => { + (Scaling.listGroups as jest.Mock).mockImplementation(() => ok([])); + (Scaling.listProviders as jest.Mock).mockImplementation(() => + ok([{ ...PROVIDERS[1], secrets: { token: true }, check: { ok: false, error: "the token was refused" } }]), + ); + (Scaling.addProvider as jest.Mock).mockImplementation(({ body }) => + ok({ + id: 3, + ...body, + priced: true, + secrets: { token: true }, + check: { ok: false, error: "the region is unknown" }, + }), + ); + (Scaling.checkProvider as jest.Mock).mockImplementation(() => ok({ ok: false, error: "the token was refused" })); + show(); + expect(await screen.findByText("Check failed: The token was refused.")).toBeInTheDocument(); + expect(screen.getByText(/API token set/)).toBeInTheDocument(); + fireEvent.click(screen.getByRole("button", { name: "Check Cloud again" })); + await waitFor(() => expect(Scaling.checkProvider).toHaveBeenCalled()); + expect((Scaling.checkProvider as jest.Mock).mock.calls[0][0].path).toEqual({ pid: 2 }); + + fireEvent.click(screen.getByRole("button", { name: "Add a provider" })); + fireEvent.click(await screen.findByRole("button", { name: /Fake cloud/ })); + fireEvent.click(screen.getByRole("button", { name: "Add provider" })); + expect(await screen.findByText("API token is needed.")).toBeInTheDocument(); + expect(Scaling.addProvider).not.toHaveBeenCalled(); + fireEvent.change(screen.getByLabelText(/API token/), { target: { value: "s3cret" } }); + fireEvent.click(screen.getByRole("button", { name: "Add provider" })); + expect(await screen.findByText("It’s saved, but its check failed.")).toBeInTheDocument(); + expect(screen.getByText(/The region is unknown\./)).toBeInTheDocument(); + }); + + it("makes a group with a budget on a priced provider", async () => { + (Scaling.listGroups as jest.Mock).mockImplementation(() => ok([])); + (Scaling.createGroup as jest.Mock).mockImplementation(({ body }) => + ok({ ...GROUP, ...body, budget: { ...GROUP.budget, usd: body.budget }, approved: true }), + ); + show(); + fireEvent.click(await screen.findByRole("button", { name: "New group on Cloud" })); + expect(screen.getByLabelText("Name")).toHaveValue("Cloud workers"); + expect(screen.getByLabelText("Minimum")).toHaveValue(0); + expect(screen.getByLabelText("Maximum")).toHaveValue(1); + expect(screen.getByLabelText(/Size/)).toHaveValue("small"); + expect(screen.getByText(/set by the assistant, MCP or the API for now/)).toBeInTheDocument(); + fireEvent.click(screen.getByRole("button", { name: "Make group" })); + expect(await screen.findByText(/A budget is needed/)).toBeInTheDocument(); + expect(Scaling.createGroup).not.toHaveBeenCalled(); + fireEvent.change(screen.getByLabelText("Budget, USD a month"), { target: { value: "25" } }); + fireEvent.change(screen.getByLabelText("Maximum"), { target: { value: "3" } }); + fireEvent.click(screen.getByRole("button", { name: "Make group" })); + await waitFor(() => expect(Scaling.createGroup).toHaveBeenCalled()); + expect((Scaling.createGroup as jest.Mock).mock.calls[0][0].body).toEqual({ + name: "Cloud workers", + provider: 2, + min: 0, + max: 3, + template: { size: "small" }, + budget: 25, + }); + }); + + it("asks no budget on a free provider and ticks a list's options", async () => { + (Scaling.listGroups as jest.Mock).mockImplementation(() => ok([])); + (Scaling.createGroup as jest.Mock).mockImplementation(({ body }) => ok({ ...GROUP, ...body, budget: null })); + show(); + fireEvent.click(await screen.findByRole("button", { name: "New group on Here" })); + expect(screen.queryByLabelText("Budget, USD a month")).not.toBeInTheDocument(); + fireEvent.click(screen.getByRole("checkbox", { name: "transcribe" })); + fireEvent.keyDown(screen.getByLabelText("Name"), { key: "Enter" }); + await waitFor(() => expect(Scaling.createGroup).toHaveBeenCalled()); + expect((Scaling.createGroup as jest.Mock).mock.calls[0][0].body).toEqual({ + name: "Here workers", + provider: 1, + min: 0, + max: 1, + template: { steps: ["transcribe"] }, + }); + }); + + it("removes a provider with Undo instead of a question", async () => { + (Scaling.listGroups as jest.Mock).mockImplementation(() => ok([GROUP])); + (Scaling.removeProvider as jest.Mock).mockImplementation(() => ok({ ok: true })); + const { unmount } = show(); + fireEvent.click(await screen.findByRole("button", { name: "Remove Here" })); + expect(screen.queryByText("Here")).not.toBeInTheDocument(); + const undo = mockToast.mock.calls[mockToast.mock.calls.length - 1][0]; + expect(undo.title).toBe("Removed Here"); + act(() => undo.action.onClick()); + expect(screen.getByText("Here")).toBeInTheDocument(); + expect(Scaling.removeProvider).not.toHaveBeenCalled(); + // Cloud runs Transcribers, so it can't go yet + expect(screen.getByRole("button", { name: "Remove Cloud" })).toHaveAttribute("aria-disabled", "true"); + // leaving the page removes what waits for Undo at once + fireEvent.click(screen.getByRole("button", { name: "Remove Here" })); + unmount(); + expect((Scaling.removeProvider as jest.Mock).mock.calls[0][0].path).toEqual({ pid: 1 }); + }); }); diff --git a/nextjs-frontend/components/settings/bodies.tsx b/nextjs-frontend/components/settings/bodies.tsx index e66ea4e9e..c1c73d501 100644 --- a/nextjs-frontend/components/settings/bodies.tsx +++ b/nextjs-frontend/components/settings/bodies.tsx @@ -28,7 +28,7 @@ import { type ModelProvider, } from "@/components/settings/model-providers"; import { SandboxGatewaySwitch } from "@/components/settings/sandbox-gateway"; -import { ScalingGroups } from "@/components/settings/scaling-panel"; +import { ScalingGroups, ScalingProviders } from "@/components/settings/scaling-panel"; import { SignInProviders } from "@/components/settings/sign-in-providers"; import { UpdatesPanel } from "@/components/settings/updates-panel"; import { settingHref } from "@/components/settings/find"; @@ -620,6 +620,8 @@ export function SectionBody({ ctx }: { ctx: BodyCtx }) { return ( <> + Providers + Groups A group launches nothing until you approve its terms here; agents (the assistant, MCP and API keys) make and manage groups but can’t approve them. Costs are estimates from list prices, counted against each group’s - budget. Today groups run more workers on this machine; cloud providers are planned. + budget. Today groups run more workers on this machine, and RunPod GPUs serve Lens’s model calls; AWS, + DigitalOcean, Hetzner Cloud, Proxmox VE and SSH providers launch machines that join over the mesh as worker + nodes, once the mesh is on in Settings › Network.

); diff --git a/nextjs-frontend/components/settings/scaling-panel.tsx b/nextjs-frontend/components/settings/scaling-panel.tsx index f164a4fca..5a1608887 100644 --- a/nextjs-frontend/components/settings/scaling-panel.tsx +++ b/nextjs-frontend/components/settings/scaling-panel.tsx @@ -1,14 +1,18 @@ "use client"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { useEffect, useRef, useState } from "react"; import { Scaling } from "@/app/openapi-client"; -import type { Group, Provider } from "@/app/openapi-client/types.gen"; +import type { DriverField, DriverSpec, Group, Provider } from "@/app/openapi-client/types.gen"; import { Banner } from "@/components/ui/banner"; import { Button } from "@/components/ui/button"; +import { Dialog } from "@/components/ui/dialog"; +import { Checkbox, Field, Input, Select, Textarea } from "@/components/ui/field"; import { SkeletonRows } from "@/components/ui/states"; import { useToast } from "@/components/ui/toast"; import { data, useApiClient } from "@/lib/api/browser"; +import { cn } from "@/lib/utils"; /** One of the API's reasons as a sentence: capitalised, with a full stop. */ function sentence(s: string): string { @@ -76,8 +80,8 @@ export function ScalingGroups() { if (!list.length) return (

- No scaling groups yet. Ask the assistant to set one up, or make one over the API or MCP; it shows here for you - to approve. + No scaling groups yet. Make one with New group on a provider above, or ask the assistant to set one up (or make + one over the API or MCP); it shows here for you to approve.

); return ( @@ -115,3 +119,579 @@ export function ScalingGroups() { ); } + +type Fields = Record; +/** What a person has typed for a driver's fields: text, a checkbox, or the options ticked. */ +export type SpecForm = Record; + +const picks = (f: DriverField) => (f.options ?? []).map(String); +const checkOf = (p: Provider) => p.check as { ok?: boolean; error?: string } | null | undefined; + +/** A driver's fields as a form, with their defaults filled in. */ +export function specForm(fields: Fields): SpecForm { + return Object.fromEntries( + Object.entries(fields).map(([k, f]) => { + const d = f.default; + if (f.type === "boolean") return [k, d === true]; + if (f.type === "list") + return [k, f.options?.length ? (Array.isArray(d) ? d.map(String) : []) : Array.isArray(d) ? d.join("\n") : ""]; + return [k, d == null ? "" : String(d)]; + }), + ); +} + +/** The form as the API takes it: settings (or a template) and, apart, the secrets; or what's wrong, by field. A field + * left empty is left out, so the driver's default applies. */ +export function specValues( + fields: Fields, + form: SpecForm, +): { values: Record; secrets: Record; errors: Record } { + const values: Record = {}; + const secrets: Record = {}; + const errors: Record = {}; + for (const [k, f] of Object.entries(fields)) { + const raw = form[k]; + let v: unknown; + if (f.type === "boolean") v = raw === true; + else if (f.type === "list") { + const xs = Array.isArray(raw) + ? raw + : String(raw ?? "") + .split(/[\n,]/) + .map((x) => x.trim()) + .filter(Boolean); + v = xs.length ? xs : undefined; + } else { + const t = String(raw ?? "").trim(); + if (t && (f.type === "integer" || f.type === "number")) { + const n = Number(t); + if (f.type === "integer" ? !Number.isInteger(n) : !Number.isFinite(n)) { + errors[k] = f.type === "integer" ? "A whole number." : "A number."; + continue; + } + v = n; + } else v = t || undefined; + } + if (v === undefined) { + if (f.required) errors[k] = `${f.label} is needed.`; + } else if (f.secret) secrets[k] = String(v); + else values[k] = v; + } + return { values, secrets, errors }; +} + +/** A driver's fields, drawn from its spec: secrets as passwords, lists one per line (or ticked from their options), + * booleans as checkboxes, a choice of options as a select. */ +export function SpecFields({ + fields, + form, + onChange, + errors, +}: { + fields: Fields; + form: SpecForm; + onChange: (f: SpecForm) => void; + errors: Record; +}) { + const set = (k: string, v: string | boolean | string[]) => onChange({ ...form, [k]: v }); + return ( + <> + {Object.entries(fields).map(([k, f]) => { + const help = f.help ? sentence(f.help) : undefined; + if (f.type === "boolean") + return ( +
+ set(k, v)} label={f.label} /> + {help &&

{help}

} +
+ ); + if (f.type === "list" && f.options?.length) { + const on = Array.isArray(form[k]) ? form[k] : []; + return ( +
+ + {f.label} + {!f.required && optional} + +
+ {picks(f).map((o) => ( + set(k, v ? [...on, o] : on.filter((x) => x !== o))} + /> + ))} +
+ {(errors[k] || help) && ( +

+ {errors[k] || help} +

+ )} +
+ ); + } + const hint = + f.type === "list" + ? `One per line.${help ? ` ${help}` : ""}` + : f.secret + ? `${help ? `${help} ` : ""}Sealed when saved, never shown again.` + : help; + return ( + + {({ id, describedBy, invalid }) => + f.type === "list" ? ( +