fix(webui): refresh skill suggestions when opening the picker
Refresh the shared skill list when a dollar-reference picker opens so skills installed by the agent appear without reloading the page. Coalesce in-flight requests and preserve the last known list on transient failures. Cover post-load discovery, Tab completion, removals, request deduplication, cleanup, and retry behavior with regression tests.
This commit is contained in:
1 parent
b5d2a3fb8e
commit
27518ca9fd
4 files changed
+120
-3
No files matched your search
@@ -108,6 +108,7 @@ import {
|
|||||||
logoFallbackUrls,
|
logoFallbackUrls,
|
||||||
} from "@/lib/provider-brand";
|
} from "@/lib/provider-brand";
|
||||||
import { sessionHandleColor } from "@/lib/session-handle";
|
import { sessionHandleColor } from "@/lib/session-handle";
|
||||||
|
import { requestSkillsRefresh } from "@/lib/skill-events";
|
||||||
import {
|
import {
|
||||||
isSideChannelLifecycle,
|
isSideChannelLifecycle,
|
||||||
slashCommandLifecycle,
|
slashCommandLifecycle,
|
||||||
@@ -1121,6 +1122,12 @@ export function ThreadComposer({
|
|||||||
};
|
};
|
||||||
}, [cursorPosition, interactionDisabled, slashMenuDismissed, value]);
|
}, [cursorPosition, interactionDisabled, slashMenuDismissed, value]);
|
||||||
|
|
||||||
|
const skillMenuActive = skillQuery !== null;
|
||||||
|
useEffect(() => {
|
||||||
|
// Also refresh an empty menu: skills may have been installed by the agent.
|
||||||
|
if (skillMenuActive) requestSkillsRefresh();
|
||||||
|
}, [skillMenuActive]);
|
||||||
|
|
||||||
const visibleSlashCommands = useMemo(() => {
|
const visibleSlashCommands = useMemo(() => {
|
||||||
if (!(isStreaming && onStop)) return slashCommands;
|
if (!(isStreaming && onStop)) return slashCommands;
|
||||||
const stopCommand = slashCommands.find((command) => command.command === "/stop");
|
const stopCommand = slashCommands.find((command) => command.command === "/stop");
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
import { fetchSkills } from "@/lib/api";
|
import { fetchSkills } from "@/lib/api";
|
||||||
import { isSkillsPayload, SKILLS_CHANGED_EVENT } from "@/lib/skill-events";
|
import { isSkillsPayload, SKILLS_CHANGED_EVENT, SKILLS_REFRESH_EVENT } from "@/lib/skill-events";
|
||||||
import type { SkillSummary } from "@/lib/types";
|
import type { SkillSummary } from "@/lib/types";
|
||||||
|
|
||||||
export function useSkills(getToken: () => string): SkillSummary[] {
|
export function useSkills(getToken: () => string): SkillSummary[] {
|
||||||
@@ -10,14 +10,20 @@ export function useSkills(getToken: () => string): SkillSummary[] {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let cancelled = false;
|
let cancelled = false;
|
||||||
let payloadVersion = 0;
|
let payloadVersion = 0;
|
||||||
|
let refreshing = false;
|
||||||
const refresh = () => {
|
const refresh = () => {
|
||||||
|
if (cancelled || refreshing) return;
|
||||||
|
refreshing = true;
|
||||||
const version = payloadVersion;
|
const version = payloadVersion;
|
||||||
fetchSkills(getToken())
|
fetchSkills(getToken())
|
||||||
.then(({ skills: nextSkills }) => {
|
.then(({ skills: nextSkills }) => {
|
||||||
if (!cancelled && version === payloadVersion) setSkills(nextSkills);
|
if (!cancelled && version === payloadVersion) setSkills(nextSkills);
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
if (!cancelled && version === payloadVersion) setSkills([]);
|
// Keep the last known list usable during transient refresh failures.
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
refreshing = false;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
const onSkillsChanged = (event: Event) => {
|
const onSkillsChanged = (event: Event) => {
|
||||||
@@ -30,9 +36,11 @@ export function useSkills(getToken: () => string): SkillSummary[] {
|
|||||||
|
|
||||||
refresh();
|
refresh();
|
||||||
window.addEventListener(SKILLS_CHANGED_EVENT, onSkillsChanged);
|
window.addEventListener(SKILLS_CHANGED_EVENT, onSkillsChanged);
|
||||||
|
window.addEventListener(SKILLS_REFRESH_EVENT, refresh);
|
||||||
return () => {
|
return () => {
|
||||||
cancelled = true;
|
cancelled = true;
|
||||||
window.removeEventListener(SKILLS_CHANGED_EVENT, onSkillsChanged);
|
window.removeEventListener(SKILLS_CHANGED_EVENT, onSkillsChanged);
|
||||||
|
window.removeEventListener(SKILLS_REFRESH_EVENT, refresh);
|
||||||
};
|
};
|
||||||
}, [getToken]);
|
}, [getToken]);
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
import type { SkillsPayload } from "@/lib/types";
|
import type { SkillsPayload } from "@/lib/types";
|
||||||
|
|
||||||
export const SKILLS_CHANGED_EVENT = "nanobot:skills-changed";
|
export const SKILLS_CHANGED_EVENT = "nanobot:skills-changed";
|
||||||
|
export const SKILLS_REFRESH_EVENT = "nanobot:skills-refresh";
|
||||||
|
|
||||||
|
export function requestSkillsRefresh(): void {
|
||||||
|
if (typeof window === "undefined") return;
|
||||||
|
window.dispatchEvent(new Event(SKILLS_REFRESH_EVENT));
|
||||||
|
}
|
||||||
|
|
||||||
export function isSkillsPayload(value: unknown): value is SkillsPayload {
|
export function isSkillsPayload(value: unknown): value is SkillsPayload {
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -2,13 +2,14 @@ import { act, fireEvent, render, renderHook, screen } from "@testing-library/rea
|
|||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
import { SkillsMarketplace } from "@/components/settings/SkillsMarketplace";
|
import { SkillsMarketplace } from "@/components/settings/SkillsMarketplace";
|
||||||
|
import { ThreadComposer } from "@/components/thread/ThreadComposer";
|
||||||
import {
|
import {
|
||||||
fetchSkills,
|
fetchSkills,
|
||||||
fetchTrendingMarketplaceSkills,
|
fetchTrendingMarketplaceSkills,
|
||||||
searchMarketplaceSkills,
|
searchMarketplaceSkills,
|
||||||
} from "@/lib/api";
|
} from "@/lib/api";
|
||||||
import type { NanobotClient } from "@/lib/nanobot-client";
|
import type { NanobotClient } from "@/lib/nanobot-client";
|
||||||
import { SKILLS_CHANGED_EVENT } from "@/lib/skill-events";
|
import { requestSkillsRefresh, SKILLS_CHANGED_EVENT } from "@/lib/skill-events";
|
||||||
import { ClientProvider } from "@/providers/ClientProvider";
|
import { ClientProvider } from "@/providers/ClientProvider";
|
||||||
import { useSkills } from "@/hooks/useSkills";
|
import { useSkills } from "@/hooks/useSkills";
|
||||||
|
|
||||||
@@ -37,6 +38,101 @@ function marketplace(token: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe("useSkills", () => {
|
describe("useSkills", () => {
|
||||||
|
it("discovers skills added after page load when opening the $ menu, not on each keystroke", async () => {
|
||||||
|
const installed = {
|
||||||
|
name: "simple-pr-review",
|
||||||
|
description: "Review pull requests.",
|
||||||
|
source: "workspace",
|
||||||
|
enabled: true,
|
||||||
|
available: true,
|
||||||
|
};
|
||||||
|
vi.mocked(fetchSkills).mockReset().mockResolvedValueOnce({ skills: [] })
|
||||||
|
.mockResolvedValue({ skills: [installed] });
|
||||||
|
const getToken = () => "tok";
|
||||||
|
function Composer() {
|
||||||
|
const skills = useSkills(getToken);
|
||||||
|
return <ThreadComposer onSend={vi.fn()} skills={skills} />;
|
||||||
|
}
|
||||||
|
render(<Composer />);
|
||||||
|
await act(async () => {});
|
||||||
|
expect(fetchSkills).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
|
const input = screen.getByLabelText("Message input");
|
||||||
|
fireEvent.change(input, { target: { value: "/", selectionStart: 1 } });
|
||||||
|
fireEvent.change(input, { target: { value: "@", selectionStart: 1 } });
|
||||||
|
expect(fetchSkills).toHaveBeenCalledTimes(1);
|
||||||
|
fireEvent.change(input, { target: { value: "$", selectionStart: 1 } });
|
||||||
|
expect(await screen.findByRole("option", { name: /simple-pr-review/ })).toBeInTheDocument();
|
||||||
|
expect(fetchSkills).toHaveBeenCalledTimes(2);
|
||||||
|
|
||||||
|
fireEvent.change(input, { target: { value: "$simple", selectionStart: 7 } });
|
||||||
|
await act(async () => {});
|
||||||
|
expect(fetchSkills).toHaveBeenCalledTimes(2);
|
||||||
|
fireEvent.keyDown(input, { key: "Tab" });
|
||||||
|
expect(input).toHaveValue("$simple-pr-review ");
|
||||||
|
|
||||||
|
// A later open must also refresh removals without remounting the page.
|
||||||
|
vi.mocked(fetchSkills).mockResolvedValue({ skills: [] });
|
||||||
|
fireEvent.change(input, { target: { value: "$", selectionStart: 1 } });
|
||||||
|
await act(async () => {});
|
||||||
|
expect(fetchSkills).toHaveBeenCalledTimes(3);
|
||||||
|
expect(screen.queryByRole("listbox", { name: "Slash commands" })).not.toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("coalesces in-flight refreshes and stops listening after unmount", async () => {
|
||||||
|
let resolveSkills!: (value: Awaited<ReturnType<typeof fetchSkills>>) => void;
|
||||||
|
vi.mocked(fetchSkills).mockReset().mockImplementationOnce(
|
||||||
|
() => new Promise((resolve) => {
|
||||||
|
resolveSkills = resolve;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const getToken = () => "tok";
|
||||||
|
const { result, unmount } = renderHook(() => useSkills(getToken));
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
requestSkillsRefresh();
|
||||||
|
requestSkillsRefresh();
|
||||||
|
});
|
||||||
|
expect(fetchSkills).toHaveBeenCalledTimes(1);
|
||||||
|
await act(async () => {
|
||||||
|
resolveSkills({ skills: [] });
|
||||||
|
});
|
||||||
|
expect(result.current).toEqual([]);
|
||||||
|
|
||||||
|
unmount();
|
||||||
|
requestSkillsRefresh();
|
||||||
|
expect(fetchSkills).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps existing skills on refresh failure and retries on the next open", async () => {
|
||||||
|
const installed = {
|
||||||
|
name: "simple-pr-review",
|
||||||
|
description: "Review pull requests.",
|
||||||
|
source: "workspace",
|
||||||
|
available: true,
|
||||||
|
};
|
||||||
|
vi.mocked(fetchSkills).mockReset()
|
||||||
|
.mockResolvedValueOnce({ skills: [installed] })
|
||||||
|
.mockRejectedValueOnce(new Error("Temporarily offline"))
|
||||||
|
.mockResolvedValueOnce({ skills: [] });
|
||||||
|
const getToken = () => "tok";
|
||||||
|
const { result } = renderHook(() => useSkills(getToken));
|
||||||
|
await act(async () => {});
|
||||||
|
expect(result.current).toEqual([installed]);
|
||||||
|
|
||||||
|
await act(async () => {
|
||||||
|
requestSkillsRefresh();
|
||||||
|
});
|
||||||
|
expect(fetchSkills).toHaveBeenCalledTimes(2);
|
||||||
|
expect(result.current).toEqual([installed]);
|
||||||
|
|
||||||
|
await act(async () => {
|
||||||
|
requestSkillsRefresh();
|
||||||
|
});
|
||||||
|
expect(fetchSkills).toHaveBeenCalledTimes(3);
|
||||||
|
expect(result.current).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
it("does not let an older request overwrite a newer skill event", async () => {
|
it("does not let an older request overwrite a newer skill event", async () => {
|
||||||
let resolveSkills!: (value: Awaited<ReturnType<typeof fetchSkills>>) => void;
|
let resolveSkills!: (value: Awaited<ReturnType<typeof fetchSkills>>) => void;
|
||||||
vi.mocked(fetchSkills).mockReset().mockImplementationOnce(
|
vi.mocked(fetchSkills).mockReset().mockImplementationOnce(
|
||||||
|
|||||||
Reference in new issue
Block a user