-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstalled-catalog.ts
More file actions
137 lines (121 loc) · 3.83 KB
/
Copy pathinstalled-catalog.ts
File metadata and controls
137 lines (121 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import type { BbPluginApi, PluginMentionItem } from "@get-bb/plugin-sdk";
import {
MAX_ITEM_SUBTITLE_BYTES,
MAX_ITEM_TITLE_BYTES,
boundUntrustedText,
encodeInstalledItemId,
normalizeStableIdentity,
normalizeUntrustedText,
} from "./mention-context";
export type InstalledPluginRecord = Awaited<
ReturnType<BbPluginApi["sdk"]["plugins"]["list"]>
>["plugins"][number];
interface InstalledCandidate {
plugin: InstalledPluginRecord;
pluginId: string;
displayName: string;
description: string;
normalizedName: string;
tier: number;
}
const RESULT_LIMIT = 6;
function folded(value: string): string {
return value.toLowerCase();
}
function compareText(left: string, right: string): number {
return folded(left).localeCompare(folded(right), "en");
}
function matchTier(
query: string,
displayName: string,
pluginId: string,
description: string,
): number | null {
const foldedQuery = folded(normalizeUntrustedText(query));
if (foldedQuery.length === 0) return 2;
const name = folded(displayName);
const id = folded(pluginId);
const detail = folded(description);
if (name === foldedQuery || id === foldedQuery) return 0;
if ([name, id, detail].some((field) => field.startsWith(foldedQuery))) return 1;
if ([name, id, detail].some((field) => field.includes(foldedQuery))) return 2;
return null;
}
export function hasAgentFacingInterface(plugin: InstalledPluginRecord): boolean {
return (
plugin.cliCommand !== null ||
plugin.capabilities.some(
(capability) => capability.kind === "skill" || capability.kind === "agent-tool",
)
);
}
export function isUsableInstalledTarget(
plugin: InstalledPluginRecord,
ownerPluginId: string,
): boolean {
const pluginId = normalizeStableIdentity(plugin.id);
const ownerId = normalizeStableIdentity(ownerPluginId);
return (
pluginId !== null &&
pluginId !== ownerId &&
plugin.status === "running" &&
hasAgentFacingInterface(plugin)
);
}
export function searchInstalledPlugins(
plugins: readonly InstalledPluginRecord[],
query: string,
ownerPluginId: string,
): PluginMentionItem[] {
const eligible = plugins.flatMap((plugin): InstalledCandidate[] => {
if (!isUsableInstalledTarget(plugin, ownerPluginId)) return [];
const pluginId = normalizeStableIdentity(plugin.id);
if (pluginId === null) return [];
const displayName = normalizeUntrustedText(plugin.name ?? pluginId) || pluginId;
const description = normalizeUntrustedText(plugin.description ?? "");
const tier = matchTier(query, displayName, pluginId, description);
if (tier === null) return [];
return [
{
plugin,
pluginId,
displayName,
description,
normalizedName: folded(displayName),
tier,
},
];
});
const duplicateNames = new Set(
Array.from(
eligible.reduce((counts, candidate) => {
counts.set(candidate.normalizedName, (counts.get(candidate.normalizedName) ?? 0) + 1);
return counts;
}, new Map<string, number>()),
)
.filter(([, count]) => count > 1)
.map(([name]) => name),
);
return eligible
.sort(
(left, right) =>
left.tier - right.tier ||
compareText(left.displayName, right.displayName) ||
compareText(left.pluginId, right.pluginId),
)
.slice(0, RESULT_LIMIT)
.map((candidate) => {
const subtitleParts = duplicateNames.has(candidate.normalizedName)
? [candidate.pluginId, candidate.description]
: [candidate.description];
const subtitle = boundUntrustedText(
subtitleParts.filter(Boolean).join(" · "),
MAX_ITEM_SUBTITLE_BYTES,
);
return {
id: encodeInstalledItemId(candidate.pluginId),
title: boundUntrustedText(candidate.displayName, MAX_ITEM_TITLE_BYTES),
...(subtitle.length > 0 ? { subtitle } : {}),
};
});
}