🙌 OpenHands: AI-Driven Development
feat(automations): select saved agent profiles (#17396)
Graham Neubiga0403035a20c
diff --git a/__tests__/components/automations/detail/edit-automation-modal.test.tsx b/__tests__/components/automations/detail/edit-automation-modal.test.tsxindex 6dc89f4ded..c4188614f9 100644--- a/__tests__/components/automations/detail/edit-automation-modal.test.tsx+++ b/__tests__/components/automations/detail/edit-automation-modal.test.tsx@@ -32,6 +32,14 @@ vi.mock("#/api/profiles-service/profiles-service.api", () => ({},}));+vi.mock("#/hooks/query/use-agent-profiles", () => ({+ useAgentProfiles: () => ({+ data: { profiles: [{ id: "review-profile", name: "Reviewer" }] },+ isLoading: false,+ isError: false,+ }),+}));+vi.mock("#/utils/custom-toast-handlers", () => ({displaySuccessToast: vi.fn(),displayErrorToast: vi.fn(),@@ -693,4 +701,37 @@ describe("EditAutomationModal", () => {expect(body).toMatchObject({ name: "Renamed digest" });expect(body).not.toHaveProperty("timeout");});++ it("stores an optional agent profile instead of a competing model", async () => {+ vi.mocked(AutomationService.getCapabilities).mockResolvedValue({+ ready: true,+ triggerKinds: ["cron"],+ eventSources: [],+ eventTypes: [],+ triggers: {},+ features: ["agentProfiles"],+ });+ vi.mocked(ProfilesService.listProfiles).mockResolvedValue(profilesResponse);+ vi.mocked(AutomationService.updateAutomation).mockResolvedValue({+ ...modeledAutomation,+ agent_profile_id: "review-profile",+ model: null,+ });+ const user = userEvent.setup();+ renderModal(modeledAutomation);++ await user.click(await screen.findByTestId("automation-agent-profile"));+ await user.click(await screen.findByRole("option", { name: "Reviewer" }));+ await user.click(screen.getByTestId("edit-automation-save"));++ await waitFor(() =>+ expect(AutomationService.updateAutomation).toHaveBeenCalledTimes(1),+ );+ expect(+ vi.mocked(AutomationService.updateAutomation).mock.calls[0][1],+ ).toMatchObject({+ agent_profile_id: "review-profile",+ model: null,+ });+ });});diff --git a/__tests__/components/manifest/manifest-setup-dialog.test.tsx b/__tests__/components/manifest/manifest-setup-dialog.test.tsxindex 4cba8318e6..974d60a461 100644--- a/__tests__/components/manifest/manifest-setup-dialog.test.tsx+++ b/__tests__/components/manifest/manifest-setup-dialog.test.tsx@@ -54,6 +54,13 @@ vi.mock("#/hooks/query/use-manifest-prerequisites", () => ({useSetupPrerequisites: () => mocks.prerequisites(),}));+vi.mock("#/hooks/query/use-agent-profiles", () => ({+ useAgentProfiles: () => ({+ data: { profiles: [{ id: "review-profile", name: "Reviewer" }] },+ isLoading: false,+ }),+}));+vi.mock("#/hooks/query/use-llm-profiles", () => ({useLlmProfiles: (options: { enabled?: boolean } = {}) =>mocks.llmProfiles(options),@@ -510,3 +517,34 @@ describe("SetupDialog", () => {expect(mocks.navigate).not.toHaveBeenCalled();});});++it("creates a bundle with an optional selected agent profile", async () => {+ mocks.capabilities.mockReturnValue({+ capabilities: {+ ...CRON_ONLY_CAPABILITIES,+ features: ["agentProfiles", "customTarball"],+ },+ supported: true,+ unmet: [],+ isLoading: false,+ });+ const entry = {+ ...BUNDLE_ENTRY,+ requires: {+ ...BUNDLE_ENTRY.requires,+ features: ["agentProfiles", "customTarball"],+ },+ };+ const { user } = renderDialog(entry);+ await fillForm(user);+ await user.click(screen.getByTestId("automation-agent-profile"));+ await user.click(await screen.findByRole("option", { name: "Reviewer" }));+ await user.click(screen.getByTestId("setup-continue-button"));+ await waitFor(() =>+ expect(screen.getByTestId("setup-review")).toBeInTheDocument(),+ );+ await user.click(screen.getByTestId("setup-continue-button"));+ expect(mocks.runAction.mock.calls[0][1]).toMatchObject({+ agent_profile_id: "review-profile",+ });+});diff --git a/__tests__/manifests/manifest-bundle.test.ts b/__tests__/manifests/manifest-bundle.test.tsindex 970d8e0118..1e3bc21db6 100644--- a/__tests__/manifests/manifest-bundle.test.ts+++ b/__tests__/manifests/manifest-bundle.test.ts@@ -5,8 +5,10 @@ const BUNDLE_FILES: Record<string, Record<string, string>> = {"widget-monitor": { "main.py": "print('watching')\n" },};-vi.mock("@openhands/extensions/automations", () => ({- AUTOMATION_CATALOG: [],+vi.mock("@openhands/extensions/automations", async (importOriginal) => ({+ ...(await importOriginal<+ typeof import("@openhands/extensions/automations")+ >()),getAutomationBundleFiles: (id: string) => BUNDLE_FILES[id],}));@@ -73,6 +75,27 @@ function bundleEntry(overrides = {}) {const VALUES = { repository: "OpenHands/automation", schedule: "*/15 * * * *" };+describe("bundle profile selection", () => {+ it("preserves the profile in preflight and create without a competing model", async () => {+ const { buildCreatePayload, buildPreflightBody } =+ await import("#/manifests/automation-setup");+ const values = {+ ...VALUES,+ agent_profile_id: "11111111-1111-4111-8111-111111111111",+ model: "another-model",+ };+ const entry = bundleEntry();+ const create = buildCreatePayload(entry, values);+ const preflight = buildPreflightBody(entry, values);+ expect(create?.agent_profile_id).toBe(values.agent_profile_id);+ expect(create).not.toHaveProperty("model");+ expect(preflight?.draft).toMatchObject({+ agent_profile_id: values.agent_profile_id,+ });+ expect(preflight?.draft).not.toHaveProperty("model");+ });+});+describe("packBundle", () => {it("packs the entry's files with the config the form rendered", async () => {// Actdiff --git a/src/components/features/automations/agent-profile-selector.test.tsx b/src/components/features/automations/agent-profile-selector.test.tsxnew file mode 100644index 0000000000..805154038b--- /dev/null+++ b/src/components/features/automations/agent-profile-selector.test.tsx@@ -0,0 +1,65 @@+import { fireEvent, render, screen } from "@testing-library/react";+import { describe, expect, it, vi } from "vitest";+import { AutomationAgentProfileSelector } from "./agent-profile-selector";++vi.mock("#/hooks/query/use-agent-profiles", () => ({+ useAgentProfiles: () => ({+ data: { profiles: [{ id: "review-profile", name: "Reviewer" }] },+ isLoading: false,+ }),+}));+vi.mock("react-i18next", () => ({+ useTranslation: () => ({+ t: (key: string, args?: { id?: string }) =>+ args?.id ? `Unavailable profile (${args.id})` : key,+ }),+}));+vi.mock("#/components/features/settings/settings-dropdown-input", () => ({+ SettingsDropdownInput: ({+ items,+ selectedKey,+ onSelectionChange,+ }: {+ items: { key: string; label: string }[];+ selectedKey: string;+ onSelectionChange: (key: string) => void;+ }) => (+ <select+ value={selectedKey}+ onChange={(event) => onSelectionChange(event.target.value)}+ >+ {items.map((item) => (+ <option key={item.key} value={item.key}>+ {item.label}+ </option>+ ))}+ </select>+ ),+}));++describe("automation agent profile selection", () => {+ it("saves the stable profile ID and clears explicitly to the deployment default", () => {+ const onChange = vi.fn();+ render(<AutomationAgentProfileSelector value={null} onChange={onChange} />);+ fireEvent.change(screen.getByRole("combobox"), {+ target: { value: "review-profile" },+ });+ expect(onChange).toHaveBeenLastCalledWith("review-profile");+ fireEvent.change(screen.getByRole("combobox"), {+ target: { value: "__default__" },+ });+ expect(onChange).toHaveBeenLastCalledWith(null);+ });+ it("retains a deleted profile instead of silently selecting another", () => {+ render(+ <AutomationAgentProfileSelector+ value="deleted-profile"+ onChange={vi.fn()}+ />,+ );+ expect(screen.getByRole("combobox")).toHaveValue("deleted-profile");+ expect(+ screen.getByText("Unavailable profile (deleted-profile)"),+ ).toBeInTheDocument();+ });+});diff --git a/src/components/features/automations/agent-profile-selector.tsx b/src/components/features/automations/agent-profile-selector.tsxnew file mode 100644index 0000000000..4152284ac2--- /dev/null+++ b/src/components/features/automations/agent-profile-selector.tsx@@ -0,0 +1,47 @@+import { useTranslation } from "react-i18next";+import { I18nKey } from "#/i18n/declaration";+import { useAgentProfiles } from "#/hooks/query/use-agent-profiles";+import { SettingsDropdownInput } from "#/components/features/settings/settings-dropdown-input";++interface Props {+ value: string | null;+ onChange: (value: string | null) => void;+}++/** One selector for automation setup and editing, backed by the profile library. */+export function AutomationAgentProfileSelector({ value, onChange }: Props) {+ const { t } = useTranslation("openhands");+ const { data, isLoading, isError } = useAgentProfiles();+ const profiles = data?.profiles ?? [];+ const selected = value+ ? profiles.find((profile) => profile.id === value)+ : undefined;+ const defaultLabel = t(I18nKey.SETTINGS$PROFILE_DEFAULT);+ const label =+ selected?.name ??+ (value+ ? t(I18nKey.AUTOMATIONS$UNAVAILABLE_AGENT_PROFILE, { id: value })+ : defaultLabel);+ const items = [+ { key: "__default__", label: defaultLabel },+ ...profiles.flatMap((profile) =>+ profile.id ? [{ key: profile.id, label: profile.name }] : [],+ ),+ ...(value && !selected ? [{ key: value, label }] : []),+ ];+ return (+ <SettingsDropdownInput+ testId="automation-agent-profile"+ name="agent_profile_id"+ label={t(I18nKey.CHAT$AGENT_PROFILE_PLACEHOLDER)}+ items={items}+ selectedKey={value ?? "__default__"}+ isClearable+ isLoading={isLoading}+ isDisabled={isError}+ onSelectionChange={(key) =>+ onChange(key && key !== "__default__" ? String(key) : null)+ }+ />+ );+}diff --git a/src/components/features/automations/detail/edit-automation-modal.tsx b/src/components/features/automations/detail/edit-automation-modal.tsxindex f6f487796a..24eedf2600 100644--- a/src/components/features/automations/detail/edit-automation-modal.tsx+++ b/src/components/features/automations/detail/edit-automation-modal.tsx@@ -1,3 +1,4 @@+import { AutomationAgentProfileSelector } from "../agent-profile-selector";import { useEffect, useMemo, useState } from "react";import { useTranslation } from "react-i18next";import { I18nKey } from "#/i18n/declaration";@@ -64,6 +65,7 @@ const WEEKDAY_KEYS: I18nKey[] = [];interface FormState {+ agentProfileId: string | null;name: string;prompt: string;model: string;@@ -81,6 +83,7 @@ function buildInitialState(automation: Automation): FormState {return {name: automation.name,prompt: automation.prompt ?? "",+ agentProfileId: automation.agent_profile_id ?? null,model: automation.model ?? "",frequency: "custom",weekday: 1,@@ -95,6 +98,7 @@ function buildInitialState(automation: Automation): FormState {return {name: automation.name,prompt: automation.prompt ?? "",+ agentProfileId: automation.agent_profile_id ?? null,model: automation.model ?? "",frequency: "custom",weekday: 1,@@ -110,6 +114,7 @@ function buildInitialState(automation: Automation): FormState {return {name: automation.name,prompt: automation.prompt ?? "",+ agentProfileId: automation.agent_profile_id ?? null,model: automation.model ?? "",frequency: parsed.kind,weekday: parsed.kind === "weekly" ? (parsed.weekday ?? 1) : 1,@@ -224,7 +229,10 @@ export function EditAutomationModal({body.prompt = trimmedPrompt.length === 0 ? null : trimmedPrompt;}- const selectedModel = form.model.trim();+ if (form.agentProfileId !== (automation.agent_profile_id ?? null)) {+ body.agent_profile_id = form.agentProfileId;+ }+ const selectedModel = form.agentProfileId ? "" : form.model.trim();const initialModel = automation.model ?? "";if (selectedModel !== initialModel) {body.model = selectedModel === "" ? null : selectedModel;@@ -351,23 +359,33 @@ export function EditAutomationModal({</label>)}- {modelSpec.present && (isLoadingProfiles || profiles.length > 0) && (- <SettingsDropdownInput- testId="edit-automation-model"- name="model"- label={modelSpec.label}- items={modelItems}- selectedKey={form.model || ACTIVE_PROFILE_KEY}- isLoading={isLoadingProfiles}- placeholder={t(I18nKey.COMMON$ACTIVE_PROFILE)}- onSelectionChange={(key) =>- setForm((f) => ({- ...f,- model: key && key !== ACTIVE_PROFILE_KEY ? String(key) : "",- }))+ {capabilities?.features.includes("agentProfiles") && (+ <AutomationAgentProfileSelector+ value={form.agentProfileId}+ onChange={(agentProfileId) =>+ setForm((current) => ({ ...current, agentProfileId }))}/>)}+ {!form.agentProfileId &&+ modelSpec.present &&+ (isLoadingProfiles || profiles.length > 0) && (+ <SettingsDropdownInput+ testId="edit-automation-model"+ name="model"+ label={modelSpec.label}+ items={modelItems}+ selectedKey={form.model || ACTIVE_PROFILE_KEY}+ isLoading={isLoadingProfiles}+ placeholder={t(I18nKey.COMMON$ACTIVE_PROFILE)}+ onSelectionChange={(key) =>+ setForm((f) => ({+ ...f,+ model: key && key !== ACTIVE_PROFILE_KEY ? String(key) : "",+ }))+ }+ />+ )}{timeoutSpec.present && (<div className="flex flex-col gap-2.5 w-full min-w-0">diff --git a/src/components/features/manifest/manifest-setup-dialog.tsx b/src/components/features/manifest/manifest-setup-dialog.tsxindex d340a00aae..812faf35d7 100644--- a/src/components/features/manifest/manifest-setup-dialog.tsx+++ b/src/components/features/manifest/manifest-setup-dialog.tsx@@ -1,3 +1,4 @@+import { AutomationAgentProfileSelector } from "#/components/features/automations/agent-profile-selector";import { useEffect, useMemo, useRef, useState } from "react";import { useTranslation } from "react-i18next";import { useNavigate } from "react-router";@@ -165,6 +166,13 @@ export function SetupDialog({ entry, onClose }: SetupDialogProps) {() => collectFields(entry.setup, selectedTrigger, selectedAction),[entry, selectedTrigger, selectedAction],);+ const visibleFields = values.agent_profile_id+ ? Object.fromEntries(+ Object.entries(fields).filter(+ ([, field]) => field.type !== "llm-profile",+ ),+ )+ : fields;const hasLlmProfileField = useMemo(() => Object.values(fields).some((field) => field.type === "llm-profile"),[fields],@@ -531,7 +539,21 @@ export function SetupDialog({ entry, onClose }: SetupDialogProps) {</p></div>)}- {Object.entries(fields).map(([name, field]) => (+ {capabilities.capabilities?.features.includes(+ "agentProfiles",+ ) && (+ <AutomationAgentProfileSelector+ value={+ typeof values.agent_profile_id === "string"+ ? values.agent_profile_id+ : null+ }+ onChange={(value) =>+ setFieldValue("agent_profile_id", value ?? "")+ }+ />+ )}+ {Object.entries(visibleFields).map(([name, field]) => (<SetupFormFieldkey={name}name={name}diff --git a/src/i18n/translation.json b/src/i18n/translation.jsonindex fe1b9edbda..90627f4f58 100644--- a/src/i18n/translation.json+++ b/src/i18n/translation.json@@ -1,4 +1,21 @@{+ "AUTOMATIONS$UNAVAILABLE_AGENT_PROFILE": {+ "en": "Unavailable profile ({{id}})",+ "ja": "利用できないプロファイル ({{id}})",+ "zh-CN": "不可用的配置文件 ({{id}})",+ "zh-TW": "無法使用的設定檔 ({{id}})",+ "ko-KR": "사용할 수 없는 프로필 ({{id}})",+ "no": "Utilgjengelig profil ({{id}})",+ "ar": "ملف تعريف غير متاح ({{id}})",+ "de": "Nicht verfügbares Profil ({{id}})",+ "fr": "Profil indisponible ({{id}})",+ "it": "Profilo non disponibile ({{id}})",+ "pt": "Perfil indisponível ({{id}})",+ "es": "Perfil no disponible ({{id}})",+ "ca": "Perfil no disponible ({{id}})",+ "tr": "Kullanılamayan profil ({{id}})",+ "uk": "Недоступний профіль ({{id}})"+ },"COMMON$PATTERN": {"ar": "النمط:","ca": "Patró:",diff --git a/src/manifests/automation-setup.test.ts b/src/manifests/automation-setup.test.tsindex 138095c85a..dd6e41ba1b 100644--- a/src/manifests/automation-setup.test.ts+++ b/src/manifests/automation-setup.test.ts@@ -10,6 +10,23 @@ const customAutomation = AUTOMATION_CATALOG.find() as SetupEntry | undefined;describe("custom automation setup actions", () => {+ it("uses the selected agent profile instead of a second model authority", () => {+ const scenario = customAutomationFixture.scenarios[0];+ const values = {+ ...scenario.formValues,+ model: "other-model",+ agent_profile_id: "11111111-1111-4111-8111-111111111111",+ } as unknown as SetupFormValues;+ const payload = buildCreatePayload(+ customAutomation!,+ values,+ undefined,+ scenario.selectedTrigger,+ scenario.selectedAction,+ );+ expect(payload?.agent_profile_id).toBe(values.agent_profile_id);+ expect(payload).not.toHaveProperty("model");+ });it("admits the custom automation action manifest", () => {expect(customAutomation).toBeDefined();expect(validateSetupEntry(customAutomation).errors).toEqual([]);diff --git a/src/manifests/automation-setup.ts b/src/manifests/automation-setup.tsindex 5864acedc4..b98fdd72ab 100644--- a/src/manifests/automation-setup.ts+++ b/src/manifests/automation-setup.ts@@ -163,7 +163,11 @@ const TRIGGER_PROPERTIES: Record<SetupTriggerKind, readonly string[]> = {event: ["source", "on"],};-const OPTIONAL_CREATE_PROPERTIES = ["model", "timeout"] as const;+const OPTIONAL_CREATE_PROPERTIES = [+ "model",+ "timeout",+ "agent_profile_id",+] as const;/*** Repository properties a form field may fill, read the same way@@ -271,6 +275,7 @@ function optionalCreateProperties(): SetupRequestBody {return Object.fromEntries(OPTIONAL_CREATE_PROPERTIES.flatMap((name) => {+ if (name === "model" && values.agent_profile_id) return [];const field = collectFields(setup, null, selectedAction)[name];const value = fieldPayloadValue(field?.type, values[name]);return hasPayloadValue(value) ? [[name, value]] : [];@@ -535,6 +540,7 @@ function buildBundlePayload(const payload: SetupRequestBody = {name: deriveName(entry, values),+ ...optionalCreateProperties(entry.setup, values),};const trigger = buildTrigger(entry, values, selectedTrigger);diff --git a/src/manifests/manifest-local-validation.ts b/src/manifests/manifest-local-validation.tsindex 4505f64af8..5bf7eedeb8 100644--- a/src/manifests/manifest-local-validation.ts+++ b/src/manifests/manifest-local-validation.ts@@ -268,6 +268,7 @@ export function validateFormValues(return Object.entries(collectFields(setup, selectedTrigger, selectedAction),).reduce<SetupFieldErrors>((errors, [name, field]) => {+ if (values.agent_profile_id && field.type === "llm-profile") return errors;const error = validateField(name, field, values[name], overrides);return error ? { ...errors, [name]: error } : errors;}, {});diff --git a/src/types/automation.ts b/src/types/automation.tsindex 4bd66bf5f5..55b61d2c5e 100644--- a/src/types/automation.ts+++ b/src/types/automation.ts@@ -56,7 +56,9 @@ export interface Automation {*/user_id?: string;repository?: string;- /** LLM/model profile name used for automation runs. */+ /** Saved agent profile controlling model, tools, and selected secrets. */+ agent_profile_id?: string | null;+ /** LLM/model profile name used when no agent profile is selected. */model?: string | null;/*** Maximum run time in seconds. `null`/omitted uses the server default