fix(web): stop chat from saying no utility agent is configured after you save one (#3306)
Co-authored-by: nova28 <17953305+nova28@users.noreply.github.com> Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com> Co-authored-by: Carlos Florencio <carlosmflorencio@gmail.com>
This commit is contained in:
@@ -658,6 +658,7 @@ func mapUserSettingsState(response userdto.UserSettingsResponse, workspaceID str
|
||||
"sidebarTaskPrefs": mapSidebarTaskPrefs(settings.SidebarTaskPrefs),
|
||||
"taskCreateLastUsed": mapTaskCreateLastUsed(settings.TaskCreateLastUsed),
|
||||
"defaultUtilityAgentId": nullString(settings.DefaultUtilityAgentID),
|
||||
"defaultUtilityAgentProfileId": nullString(settings.DefaultUtilityAgentProfileID),
|
||||
"keyboardShortcuts": mapStringAny(settings.KeyboardShortcuts),
|
||||
"terminalLinkBehavior": terminalLinkBehavior(settings.TerminalLinkBehavior),
|
||||
"terminalFontFamily": nullString(settings.TerminalFontFamily),
|
||||
|
||||
@@ -185,6 +185,19 @@ func TestMapUserSettingsStateNormalizesLastSeenDisplay(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestMapUserSettingsStateIncludesDefaultUtilityAgentProfileID verifies boot state carries the
|
||||
// default utility agent profile id, the field the Settings UI actually writes.
|
||||
func TestMapUserSettingsStateIncludesDefaultUtilityAgentProfileID(t *testing.T) {
|
||||
state := mapUserSettingsState(userdto.UserSettingsResponse{
|
||||
Settings: userdto.UserSettingsDTO{DefaultUtilityAgentProfileID: "profile-1"},
|
||||
}, "workspace-1")
|
||||
|
||||
got, ok := state["defaultUtilityAgentProfileId"].(string)
|
||||
if !ok || got != "profile-1" {
|
||||
t.Fatalf("defaultUtilityAgentProfileId = %#v, want profile-1", state["defaultUtilityAgentProfileId"])
|
||||
}
|
||||
}
|
||||
|
||||
// TestMapUserSettingsStateIncludesSystemMetricsDisplayPreference verifies boot state carries the system metrics display preference.
|
||||
func TestMapUserSettingsStateIncludesSystemMetricsDisplayPreference(t *testing.T) {
|
||||
state := mapUserSettingsState(userdto.UserSettingsResponse{
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
|
||||
import { StateProvider, useAppStore, useAppStoreApi } from "@/components/state-provider";
|
||||
import { defaultState } from "@/lib/state/default-state";
|
||||
import { SettingsSaveProvider } from "./settings-save-provider";
|
||||
import type { UtilityAgent } from "@/lib/api/domains/utility-api";
|
||||
|
||||
const listUtilityAgents = vi.fn();
|
||||
const updateUtilityAgent = vi.fn();
|
||||
const deleteUtilityAgent = vi.fn();
|
||||
const fetchUserSettings = vi.fn();
|
||||
const updateUserSettings = vi.fn();
|
||||
|
||||
vi.mock("@/lib/api/domains/utility-api", () => ({
|
||||
listUtilityAgents: (...args: unknown[]) => listUtilityAgents(...args),
|
||||
updateUtilityAgent: (...args: unknown[]) => updateUtilityAgent(...args),
|
||||
deleteUtilityAgent: (...args: unknown[]) => deleteUtilityAgent(...args),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/api/domains/settings-api", () => ({
|
||||
fetchUserSettings: (...args: unknown[]) => fetchUserSettings(...args),
|
||||
updateUserSettings: (...args: unknown[]) => updateUserSettings(...args),
|
||||
}));
|
||||
|
||||
vi.mock("@/components/settings/config-chat-agent-section", () => ({
|
||||
ConfigChatAgentSection: () => null,
|
||||
}));
|
||||
|
||||
vi.mock("@/components/settings/utility-agent-dialog", () => ({
|
||||
UtilityAgentDialog: () => null,
|
||||
}));
|
||||
|
||||
vi.mock("@/components/settings/utility-sections", async () => {
|
||||
const actual = await vi.importActual<typeof import("./utility-sections")>("./utility-sections");
|
||||
return {
|
||||
...actual,
|
||||
DefaultModelSection: ({ onProfileChange }: { onProfileChange: (value: string) => void }) => (
|
||||
<button type="button" onClick={() => onProfileChange("profile-1")}>
|
||||
Pick default profile
|
||||
</button>
|
||||
),
|
||||
PerActionOverridesSection: () => null,
|
||||
CustomAgentsSection: () => null,
|
||||
};
|
||||
});
|
||||
|
||||
import { UtilityAgentsSection } from "./utility-agents-section";
|
||||
|
||||
function agent(id: string): UtilityAgent {
|
||||
return {
|
||||
id,
|
||||
name: id,
|
||||
description: "",
|
||||
builtin: true,
|
||||
enabled: true,
|
||||
agent_id: "agent-1",
|
||||
model: "",
|
||||
prompt: "",
|
||||
created_at: "",
|
||||
updated_at: "",
|
||||
};
|
||||
}
|
||||
|
||||
function ReadDefaultUtilityAgentProfileId() {
|
||||
const value = useAppStore((state) => state.userSettings.defaultUtilityAgentProfileId);
|
||||
return <div data-testid="stored-profile-id">{value ?? ""}</div>;
|
||||
}
|
||||
|
||||
function SetNewerDefaultUtilityAgentProfile() {
|
||||
const storeApi = useAppStoreApi();
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() =>
|
||||
storeApi.getState().setUserSettings({
|
||||
...storeApi.getState().userSettings,
|
||||
defaultUtilityAgentProfileId: "profile-2",
|
||||
revision: 2,
|
||||
})
|
||||
}
|
||||
>
|
||||
Apply newer profile update
|
||||
</button>
|
||||
);
|
||||
}
|
||||
|
||||
function renderSection() {
|
||||
return render(
|
||||
<StateProvider initialState={{ userSettings: { ...defaultState.userSettings } }}>
|
||||
<SettingsSaveProvider>
|
||||
<UtilityAgentsSection />
|
||||
</SettingsSaveProvider>
|
||||
<SetNewerDefaultUtilityAgentProfile />
|
||||
<ReadDefaultUtilityAgentProfileId />
|
||||
</StateProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
listUtilityAgents.mockReset().mockResolvedValue({ agents: [agent("commit")] });
|
||||
fetchUserSettings.mockReset().mockResolvedValue({ settings: {} });
|
||||
updateUserSettings
|
||||
.mockReset()
|
||||
.mockImplementation((payload: Record<string, unknown>) =>
|
||||
Promise.resolve({ settings: { revision: 1, ...payload } }),
|
||||
);
|
||||
updateUtilityAgent.mockReset().mockResolvedValue({});
|
||||
deleteUtilityAgent.mockReset().mockResolvedValue({});
|
||||
});
|
||||
|
||||
afterEach(cleanup);
|
||||
|
||||
describe("UtilityAgentsSection save", () => {
|
||||
it("pushes the saved default utility agent profile id into the store", async () => {
|
||||
renderSection();
|
||||
|
||||
fireEvent.click(await screen.findByRole("button", { name: "Pick default profile" }));
|
||||
fireEvent.click(await screen.findByRole("button", { name: "Save changes" }));
|
||||
|
||||
await waitFor(() =>
|
||||
expect(updateUserSettings).toHaveBeenCalledWith({
|
||||
default_utility_agent_profile_id: "profile-1",
|
||||
}),
|
||||
);
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId("stored-profile-id").textContent).toBe("profile-1"),
|
||||
);
|
||||
});
|
||||
|
||||
it("does not overwrite a newer store update when the save response arrives late", async () => {
|
||||
let resolveUpdate: (value: unknown) => void = () => undefined;
|
||||
updateUserSettings.mockReturnValueOnce(
|
||||
new Promise((resolve) => {
|
||||
resolveUpdate = resolve;
|
||||
}),
|
||||
);
|
||||
|
||||
renderSection();
|
||||
|
||||
fireEvent.click(await screen.findByRole("button", { name: "Pick default profile" }));
|
||||
fireEvent.click(await screen.findByRole("button", { name: "Save changes" }));
|
||||
await waitFor(() => expect(updateUserSettings).toHaveBeenCalledTimes(1));
|
||||
|
||||
fireEvent.click(screen.getByRole("button", { name: "Apply newer profile update" }));
|
||||
resolveUpdate({
|
||||
settings: {
|
||||
revision: 1,
|
||||
default_utility_agent_profile_id: "profile-1",
|
||||
},
|
||||
});
|
||||
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId("stored-profile-id").textContent).toBe("profile-2"),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -9,6 +9,8 @@ import {
|
||||
type UtilityAgent,
|
||||
} from "@/lib/api/domains/utility-api";
|
||||
import { fetchUserSettings, updateUserSettings } from "@/lib/api/domains/settings-api";
|
||||
import { mapUserSettingsResponse } from "@/lib/ssr/user-settings";
|
||||
import { compareUserSettingsRevisions } from "@/lib/settings/user-settings-revision";
|
||||
import { SettingsPageHeader } from "@/components/settings/settings-typography";
|
||||
import { Separator } from "@kandev/ui/separator";
|
||||
import { UtilityAgentDialog } from "@/components/settings/utility-agent-dialog";
|
||||
@@ -19,7 +21,7 @@ import {
|
||||
CustomAgentsSection,
|
||||
USE_DEFAULT,
|
||||
} from "@/components/settings/utility-sections";
|
||||
import { useAppStore } from "@/components/state-provider";
|
||||
import { useAppStore, useAppStoreApi } from "@/components/state-provider";
|
||||
import { useSettingsSaveContributor } from "./settings-save-provider";
|
||||
export { isUtilityAgentDirty } from "./utility-dirty";
|
||||
|
||||
@@ -95,6 +97,8 @@ function revision(agents: UtilityAgent[], profileId: string) {
|
||||
export function UtilityAgentsSection() {
|
||||
const { t } = useTranslation();
|
||||
const profiles = useAppStore((state) => state.agentProfiles.items);
|
||||
const setUserSettings = useAppStore((state) => state.setUserSettings);
|
||||
const storeApi = useAppStoreApi();
|
||||
const [agents, setAgents] = useState<UtilityAgent[]>([]);
|
||||
const [savedAgents, setSavedAgents] = useState<UtilityAgent[]>([]);
|
||||
const [defaultProfileId, setDefaultProfileId] = useState("");
|
||||
@@ -145,7 +149,8 @@ export function UtilityAgentsSection() {
|
||||
);
|
||||
})(),
|
||||
);
|
||||
await Promise.all([
|
||||
const settingsAtSubmit = storeApi.getState().userSettings;
|
||||
const [userSettingsResponse] = await Promise.all([
|
||||
updateUserSettings({ default_utility_agent_profile_id: defaultProfileId }),
|
||||
...changed.map((agent) =>
|
||||
updateUtilityAgent(agent.id, {
|
||||
@@ -157,6 +162,16 @@ export function UtilityAgentsSection() {
|
||||
]);
|
||||
setSavedAgents(agents);
|
||||
setSavedDefaultProfileId(defaultProfileId);
|
||||
const latestUserSettings = storeApi.getState().userSettings;
|
||||
const responseOrder = compareUserSettingsRevisions(
|
||||
userSettingsResponse.settings.revision,
|
||||
latestUserSettings.revision,
|
||||
);
|
||||
const responseIsCurrent =
|
||||
responseOrder === null ? latestUserSettings === settingsAtSubmit : responseOrder >= 0;
|
||||
if (responseIsCurrent) {
|
||||
setUserSettings(mapUserSettingsResponse(userSettingsResponse, latestUserSettings));
|
||||
}
|
||||
},
|
||||
discard: () => {
|
||||
setAgents(savedAgents);
|
||||
|
||||
@@ -227,14 +227,36 @@ test.describe("Enhance prompt button in task creation", () => {
|
||||
await expect(testPage.getByTestId("prompt-result-recovery")).toHaveCount(0);
|
||||
});
|
||||
|
||||
test("enhance button is enabled when only the utility agent profile id is configured", async ({
|
||||
testPage,
|
||||
apiClient,
|
||||
seedData,
|
||||
}) => {
|
||||
// This is the state every current user is in after using Settings > Utility
|
||||
// Agents: the legacy default_utility_agent_id field is never written by the
|
||||
// UI, only default_utility_agent_profile_id.
|
||||
await apiClient.saveUserSettings({
|
||||
default_utility_agent_id: "",
|
||||
default_utility_agent_profile_id: seedData.agentProfileId,
|
||||
});
|
||||
|
||||
await openCreateTaskDialog(testPage, seedData.workspaceId);
|
||||
|
||||
const enhanceBtn = testPage.getByTestId("enhance-prompt-button");
|
||||
await expect(enhanceBtn).toBeVisible();
|
||||
await expect(enhanceBtn).toBeEnabled();
|
||||
});
|
||||
|
||||
test("enhance button is disabled when no utility agent is configured", async ({
|
||||
testPage,
|
||||
apiClient,
|
||||
seedData,
|
||||
}) => {
|
||||
// Clear any default utility agent
|
||||
// Clear any default utility agent, both the current and legacy fields —
|
||||
// an earlier test in this worker may have set either one.
|
||||
await apiClient.saveUserSettings({
|
||||
default_utility_agent_id: "",
|
||||
default_utility_agent_profile_id: "",
|
||||
});
|
||||
|
||||
const kanban = new KanbanPage(testPage);
|
||||
|
||||
@@ -97,6 +97,7 @@ function makeUnloadedSettings(): UserSettingsState {
|
||||
gitlabSavedPresets: undefined,
|
||||
azureDevOpsBrowsePreferences: undefined,
|
||||
defaultUtilityAgentId: null,
|
||||
defaultUtilityAgentProfileId: null,
|
||||
keyboardShortcuts: {},
|
||||
terminalLinkBehavior: "new_tab",
|
||||
terminalFontFamily: null,
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import type { UserSettingsState } from "@/lib/state/slices/settings/types";
|
||||
|
||||
type MockState = { userSettings: UserSettingsState };
|
||||
|
||||
let mockState: MockState;
|
||||
|
||||
vi.mock("@/components/state-provider", () => ({
|
||||
useAppStore: (selector: (state: MockState) => unknown) => selector(mockState),
|
||||
}));
|
||||
|
||||
import { useIsUtilityConfigured } from "./use-is-utility-configured";
|
||||
|
||||
function baseSettings(): UserSettingsState {
|
||||
return {
|
||||
defaultUtilityAgentId: null,
|
||||
defaultUtilityAgentProfileId: null,
|
||||
} as UserSettingsState;
|
||||
}
|
||||
|
||||
describe("useIsUtilityConfigured", () => {
|
||||
it("is configured when only the default utility agent profile id is set", () => {
|
||||
mockState = {
|
||||
userSettings: { ...baseSettings(), defaultUtilityAgentProfileId: "profile-1" },
|
||||
};
|
||||
|
||||
expect(useIsUtilityConfigured()).toBe(true);
|
||||
});
|
||||
|
||||
it("is configured when only the legacy default utility agent id is set", () => {
|
||||
mockState = {
|
||||
userSettings: { ...baseSettings(), defaultUtilityAgentId: "legacy-agent-1" },
|
||||
};
|
||||
|
||||
expect(useIsUtilityConfigured()).toBe(true);
|
||||
});
|
||||
|
||||
it("is not configured when neither field is set", () => {
|
||||
mockState = { userSettings: baseSettings() };
|
||||
|
||||
expect(useIsUtilityConfigured()).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -2,5 +2,7 @@ import { useAppStore } from "@/components/state-provider";
|
||||
|
||||
/** Returns true when the user has configured a default utility agent. */
|
||||
export function useIsUtilityConfigured(): boolean {
|
||||
return useAppStore((s) => !!s.userSettings.defaultUtilityAgentId);
|
||||
return useAppStore(
|
||||
(s) => !!s.userSettings.defaultUtilityAgentProfileId || !!s.userSettings.defaultUtilityAgentId,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -272,6 +272,18 @@ describe("buildCoreFields", () => {
|
||||
const result = buildCoreFields(settings);
|
||||
expect(result.terminalFontFamily).toBeNull();
|
||||
});
|
||||
|
||||
it("maps default_utility_agent_profile_id to defaultUtilityAgentProfileId", () => {
|
||||
const settings = {
|
||||
workspace_id: toWorkspaceId(""),
|
||||
repository_ids: [],
|
||||
default_utility_agent_profile_id: "profile-1",
|
||||
updated_at: UPDATED_AT,
|
||||
} as unknown as Parameters<typeof buildCoreFields>[0];
|
||||
|
||||
const result = buildCoreFields(settings);
|
||||
expect(result.defaultUtilityAgentProfileId).toBe("profile-1");
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildCoreFields task-create last-used mapping", () => {
|
||||
|
||||
@@ -83,6 +83,7 @@ export function createDefaultUserSettings(): UserSettingsState {
|
||||
gitlabSavedPresets: undefined,
|
||||
azureDevOpsBrowsePreferences: undefined,
|
||||
defaultUtilityAgentId: null,
|
||||
defaultUtilityAgentProfileId: null,
|
||||
keyboardShortcuts: {},
|
||||
terminalLinkBehavior: "new_tab",
|
||||
terminalFontFamily: null,
|
||||
@@ -244,6 +245,10 @@ function buildIdentityFields(s: UserSettingsData, current: UserSettingsState) {
|
||||
s.default_utility_agent_id,
|
||||
current.defaultUtilityAgentId,
|
||||
),
|
||||
defaultUtilityAgentProfileId: mapNullableString(
|
||||
s.default_utility_agent_profile_id,
|
||||
current.defaultUtilityAgentProfileId,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -442,6 +442,7 @@ export type UserSettingsState = {
|
||||
gitlabSavedPresets: unknown;
|
||||
azureDevOpsBrowsePreferences: unknown;
|
||||
defaultUtilityAgentId: string | null;
|
||||
defaultUtilityAgentProfileId: string | null;
|
||||
keyboardShortcuts: Record<string, { key: string; modifiers?: Record<string, boolean> }>;
|
||||
terminalLinkBehavior: "new_tab" | "browser_panel";
|
||||
terminalFontFamily: string | null;
|
||||
|
||||
Reference in New Issue
Block a user