Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions apps/web/src/components/settings/toolsSettings.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ProviderInstanceId,
type McpServerConfig,
type ServerProvider,
type SkillFolderInfo,
} from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";

Expand Down Expand Up @@ -192,10 +193,10 @@ describe("mergeEnvironmentSkillRows", () => {
});

describe("skill row details", () => {
const row = (paths: ReadonlyArray<string>): SkillRow => ({
const row = (paths: ReadonlyArray<string>, group: SkillRow["group"] = "personal"): SkillRow => ({
name: "review",
description: undefined,
group: "personal",
group,
paths,
providers: [],
disabledByProvider: false,
Expand Down Expand Up @@ -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<string, SkillFolderInfo>([
[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<string, SkillFolderInfo>([
[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,
Expand Down
28 changes: 21 additions & 7 deletions apps/web/src/components/settings/toolsSettings.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export function filterSkillRows(rows: ReadonlyArray<SkillRow>, 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<SkillFolderInfo>;
Expand All @@ -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<SkillGroupKind, NonNullable<SkillFolderInfo["location"]>["kind"]>
> = {
project: "project",
personal: "environment",
};

export function skillRowDetails(
row: SkillRow,
folders: ReadonlyMap<string, SkillFolderInfo>,
Expand All @@ -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,
};
}

Expand Down
Loading