diff --git a/apps/web/src/components/settings/toolsSettings.logic.test.ts b/apps/web/src/components/settings/toolsSettings.logic.test.ts index 23051a0cbd104..04e372dbc92c9 100644 --- a/apps/web/src/components/settings/toolsSettings.logic.test.ts +++ b/apps/web/src/components/settings/toolsSettings.logic.test.ts @@ -4,6 +4,7 @@ import { ProviderInstanceId, type McpServerConfig, type ServerProvider, + type SkillFolderInfo, } from "@t3tools/contracts"; import { describe, expect, it } from "vite-plus/test"; @@ -192,10 +193,10 @@ describe("mergeEnvironmentSkillRows", () => { }); describe("skill row details", () => { - const row = (paths: ReadonlyArray): SkillRow => ({ + const row = (paths: ReadonlyArray, group: SkillRow["group"] = "personal"): SkillRow => ({ name: "review", description: undefined, - group: "personal", + group, paths, providers: [], disabledByProvider: false, @@ -238,6 +239,46 @@ describe("skill row details", () => { expect(details.scripts).toBe(true); }); + it("acts on the copy at the row's own level when home and project share a name", () => { + const environment = { kind: "environment" } as const; + const project = { kind: "project", cwd: "/h/app" } as const; + // Sorted paths put the home copy first, ahead of a project under the same home. + const homePath = "/h/.agents/skills/review/SKILL.md"; + const projectPath = "/h/app/.agents/skills/review/SKILL.md"; + const homeCopy = { + ...folder(homePath, "/h/.agents/skills/review", "a"), + location: environment, + installed: { target: environment, source: "acme/global" }, + }; + const handPlaced = { + ...folder(projectPath, "/h/app/.agents/skills/review", "b"), + location: project, + }; + const copies = new Map([ + [homePath, homeCopy], + [projectPath, { ...handPlaced, installed: { target: project, source: "acme/project" } }], + ]); + const paths = [homePath, projectPath]; + + const inProject = skillRowDetails(row(paths, "project"), copies); + expect(inProject.folder?.folder).toBe("/h/app/.agents/skills/review"); + expect(inProject.installed).toEqual({ target: project, source: "acme/project" }); + expect(inProject.removable).toEqual(project); + + const atHome = skillRowDetails(row(paths, "personal"), copies); + expect(atHome.installed).toEqual({ target: environment, source: "acme/global" }); + expect(atHome.removable).toEqual(environment); + + // A hand-placed project copy can't be updated, and must not update the home one. + const mixed = new Map([ + [homePath, homeCopy], + [projectPath, handPlaced], + ]); + const unmanaged = skillRowDetails(row(paths, "project"), mixed); + expect(unmanaged.installed).toBeNull(); + expect(unmanaged.removable).toEqual(project); + }); + it("knows nothing about a row whose folders weren't inspected", () => { expect(skillRowDetails(row(["/x/SKILL.md"]), new Map())).toEqual({ folder: null, diff --git a/apps/web/src/components/settings/toolsSettings.logic.ts b/apps/web/src/components/settings/toolsSettings.logic.ts index ba449397cc820..c66d7b866c9ff 100644 --- a/apps/web/src/components/settings/toolsSettings.logic.ts +++ b/apps/web/src/components/settings/toolsSettings.logic.ts @@ -256,7 +256,7 @@ export function filterSkillRows(rows: ReadonlyArray, query: string) { /** What the server knows about a row's folders: see `skills.inspect`. */ export interface SkillRowDetails { - /** The copy shown first: the installed one, else the first found; null when none was inspected. */ + /** The copy shown first: the row's own level's, installed first; null when none was inspected. */ readonly folder: SkillFolderInfo | null; /** Each distinct folder holding a skill of this name, the shown one first. */ readonly copies: ReadonlyArray; @@ -283,6 +283,14 @@ const NO_DETAILS: SkillRowDetails = { removable: null, }; +/** The install level a group's skills live at; plugin and built-in skills have none. */ +const GROUP_LEVEL: Partial< + Record["kind"]> +> = { + project: "project", + personal: "environment", +}; + export function skillRowDetails( row: SkillRow, folders: ReadonlyMap, @@ -293,18 +301,24 @@ export function skillRowDetails( }); if (infos.length === 0) return NO_DETAILS; // Two agents reaching one folder through a link report two paths for one copy. - const byFolder = new Map(infos.map((info) => [info.folder, info])); - const copies = [...byFolder.values()].toSorted( - (left, right) => Number(right.installed !== undefined) - Number(left.installed !== undefined), - ); - const installed = copies.find((info) => info.installed !== undefined)?.installed ?? null; + const found = [...new Map(infos.map((info) => [info.folder, info])).values()]; + // A name can sit at home and in the project at once: a row acts only on its own level's copy. + const level = GROUP_LEVEL[row.group]; + const own = + level === undefined + ? found + : found.filter((info) => (info.installed?.target ?? info.location)?.kind === level); + const rank = (info: SkillFolderInfo) => + (own.includes(info) ? 0 : 2) + (info.installed === undefined ? 1 : 0); + const copies = found.toSorted((left, right) => rank(left) - rank(right)); + const installed = own.find((info) => info.installed !== undefined)?.installed ?? null; return { folder: copies[0] ?? null, copies, scripts: copies.some((info) => info.scripts), conflict: new Set(copies.map((info) => info.hash)).size > 1, installed, - removable: installed?.target ?? copies.find((info) => info.location)?.location ?? null, + removable: installed?.target ?? own.find((info) => info.location)?.location ?? null, }; }