Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

providerSkills.ts

126 lines3.8 KB

1import type {
2 ServerProvider,
3 ServerProviderSkill,
4 ServerProviderSlashCommand,
5} from "@t3tools/contracts";
6​
7export type ProviderSkillSourceKind = "app" | "repo" | "project" | "personal" | "system" | "other";
8​
9function titleCaseWords(value: string): string {
10 const words: string[] = [];
11 for (const segment of value.split(/[\s:_-]+/)) {
12 if (segment.length === 0) continue;
13 words.push(segment.charAt(0).toUpperCase() + segment.slice(1));
14 }
15 return words.join(" ");
17​
18function normalizePathSeparators(pathValue: string): string {
19 return pathValue.replaceAll("\\", "/");
21​
22export function formatProviderSkillDisplayName(
23 skill: Pick<ServerProviderSkill, "name" | "displayName">,
24): string {
25 const displayName = skill.displayName?.trim();
26 if (displayName) {
27 return displayName;
28 }
29 return titleCaseWords(skill.name);
31​
32export function dedupeProviderSkillsByName(
33 skills: ReadonlyArray<ServerProviderSkill>,
34): ServerProviderSkill[] {
35 const seenNames = new Set<string>();
36 return skills.filter((skill) => {
37 const normalizedName = skill.name.trim().toLowerCase();
38 if (seenNames.has(normalizedName)) {
39 return false;
40 }
41 seenNames.add(normalizedName);
42 return true;
43 });
45​
46/**
47 * Whether a composer pick can start this skill. A skill switched off in the
48 * provider's settings will not run, and one the provider reserves for the
49 * agent (Claude Code's `user-invocable: false`) rejects a user invocation.
50 * Everything else, including skills the agent may not start on its own, is
51 * fair game: the server dispatches the pick in the provider's native form.
52 */
53export function isProviderSkillUserInvocable(
54 skill: Pick<ServerProviderSkill, "enabled" | "userInvocable">,
55): boolean {
56 return skill.enabled && skill.userInvocable !== false;
58​
59export function getProviderSkillsForSlashMenu(
60 skills: ReadonlyArray<ServerProviderSkill>,
61 showSkillsInSlashMenu: boolean,
62): ServerProviderSkill[] {
63 return showSkillsInSlashMenu
64 ? dedupeProviderSkillsByName(skills.filter(isProviderSkillUserInvocable))
65 : [];
67​
68export function getProviderSlashCommandsForSlashMenu(
69 slashCommands: ReadonlyArray<ServerProviderSlashCommand>,
70 visibleSkills: ReadonlyArray<ServerProviderSkill>,
71): ServerProviderSlashCommand[] {
72 const skillNames = new Set(visibleSkills.map((skill) => skill.name.trim().toLowerCase()));
73 return slashCommands.filter((command) => !skillNames.has(command.name.trim().toLowerCase()));
75​
76export function resolveProviderSkillSourceKind(
77 skill: Pick<ServerProviderSkill, "path" | "scope">,
78): ProviderSkillSourceKind {
79 const normalizedPath = normalizePathSeparators(skill.path);
80 if (normalizedPath.includes("/.codex/plugins/") || normalizedPath.includes("/.agents/plugins/")) {
81 return "app";
82 }
83​
84 const normalizedScope = skill.scope?.trim().toLowerCase();
85 switch (normalizedScope) {
86 case "repo":
87 case "repository":
88 return "repo";
89 case "project":
90 case "workspace":
91 case "local":
92 return "project";
93 case "user":
94 case "personal":
95 return "personal";
96 case "system":
97 return "system";
98 case undefined:
99 case "":
100 return "other";
101 default:
102 return "other";
103 }
105​
106function resolveProviderWorkspaceSnapshot(
107 provider: ServerProvider,
108 cwd: string | null | undefined,
109) {
110 if (!cwd) return undefined;
111 return provider.workspaceSnapshots?.find((snapshot) => snapshot.cwd === cwd);
113​
114export function resolveProviderSkillsForCwd(
115 provider: ServerProvider,
116 cwd: string | null | undefined,
117): ServerProvider["skills"] {
118 return resolveProviderWorkspaceSnapshot(provider, cwd)?.skills ?? provider.skills;
120​
121export function resolveProviderSlashCommandsForCwd(
122 provider: ServerProvider,
123 cwd: string | null | undefined,
124): ServerProvider["slashCommands"] {
125 return resolveProviderWorkspaceSnapshot(provider, cwd)?.slashCommands ?? provider.slashCommands;