-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.ts
More file actions
244 lines (213 loc) · 7.41 KB
/
Copy pathserver.ts
File metadata and controls
244 lines (213 loc) · 7.41 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import type { BbPluginApi } from "@get-bb/plugin-sdk";
import {
COMMUNITY_MARKETPLACE,
type CommunityCatalogRecord,
searchCommunityPlugins,
} from "./community-catalog";
import {
type InstalledPluginRecord,
hasAgentFacingInterface,
searchInstalledPlugins,
} from "./installed-catalog";
import {
MAX_ITEM_TITLE_BYTES,
boundUntrustedText,
buildCommunityPluginContext,
buildInstalledPluginContext,
decodeCommunityItemId,
decodeInstalledItemId,
} from "./mention-context";
export const SDK_READ_TIMEOUT_MS = 1_500;
class SdkReadTimeoutError extends Error {
constructor() {
super("SDK read timed out");
this.name = "SdkReadTimeoutError";
}
}
async function boundedSdkRead<T>(read: (signal: AbortSignal) => Promise<T>): Promise<T> {
const controller = new AbortController();
let timer: ReturnType<typeof setTimeout> | undefined;
try {
return await new Promise<T>((resolve, reject) => {
timer = setTimeout(() => {
controller.abort();
reject(new SdkReadTimeoutError());
}, SDK_READ_TIMEOUT_MS);
Promise.resolve()
.then(() => read(controller.signal))
.then(resolve, reject);
});
} finally {
if (timer !== undefined) clearTimeout(timer);
}
}
function targetName(plugin: InstalledPluginRecord): string {
return (
boundUntrustedText(plugin.name ?? "", MAX_ITEM_TITLE_BYTES) ||
boundUntrustedText(plugin.id, MAX_ITEM_TITLE_BYTES) ||
"This plugin"
);
}
function fallbackTarget(pluginId: string): string {
return boundUntrustedText(pluginId, MAX_ITEM_TITLE_BYTES) || "This plugin";
}
function missingInstalledError(target: string): Error {
return new Error(
`${target} is no longer installed. Reinstall it in Plugins settings or remove @${target}, then retry.`,
);
}
function unusableInstalledError(target: string): Error {
return new Error(
`${target} is not currently usable. Restore it in Plugins settings or remove @${target}, then retry.`,
);
}
function noAgentCapabilityError(target: string): Error {
return new Error(
`${target} no longer exposes an agent capability. Reload or update it, or remove @${target}, then retry.`,
);
}
function inventoryVerificationError(target: string): Error {
return new Error(
`${target} could not be verified right now. Retry, or remove @${target} to send without it.`,
);
}
function communityMissingError(target: string): Error {
return new Error(
`${target} is no longer available in bb Community. Remove @${target} or choose a current result, then retry.`,
);
}
function communityIncompatibleError(target: string): Error {
return new Error(
`${target} is no longer listed for this version of bb. Remove @${target} or choose a current result, then retry.`,
);
}
function communityVerificationError(target: string): Error {
return new Error(
`${target} could not be verified in bb Community right now. Retry, or remove @${target} to send without it.`,
);
}
function invalidInstalledReferenceError(): Error {
return new Error(
"This Installed plugin reference is invalid. Remove the mention and choose the plugin again.",
);
}
function invalidCommunityReferenceError(): Error {
return new Error(
"This Community plugin reference is invalid. Remove the mention and choose the plugin again.",
);
}
function findInstalledPlugin(
plugins: readonly InstalledPluginRecord[],
pluginId: string,
): InstalledPluginRecord | undefined {
return plugins.find((plugin) => plugin.id === pluginId);
}
function resolveInstalledRecord(plugin: InstalledPluginRecord): { context: string } {
const target = targetName(plugin);
if (plugin.status !== "running") throw unusableInstalledError(target);
if (!hasAgentFacingInterface(plugin)) throw noAgentCapabilityError(target);
return {
context: buildInstalledPluginContext({ name: target, pluginId: plugin.id }),
};
}
function exactCommunityEntry(
entries: readonly CommunityCatalogRecord[],
identity: { pluginId: string; marketplace: string; entryId: string },
): CommunityCatalogRecord | undefined {
return entries.find(
(entry) =>
entry.pluginId === identity.pluginId &&
entry.marketplace === identity.marketplace &&
entry.entryId === identity.entryId,
);
}
export default async function plugin(bb: BbPluginApi) {
bb.ui.registerMentionProvider({
id: "installed",
label: "Installed",
async search({ query }) {
try {
const inventory = await boundedSdkRead((signal) => bb.sdk.plugins.list({ signal }));
return searchInstalledPlugins(inventory.plugins, query, bb.pluginId);
} catch {
return [];
}
},
async resolve(itemId) {
let pluginId: string;
try {
pluginId = decodeInstalledItemId(itemId).pluginId;
} catch {
throw invalidInstalledReferenceError();
}
const fallback = fallbackTarget(pluginId);
let inventory: Awaited<ReturnType<BbPluginApi["sdk"]["plugins"]["list"]>>;
try {
inventory = await boundedSdkRead((signal) => bb.sdk.plugins.list({ signal }));
} catch {
throw inventoryVerificationError(fallback);
}
const installed = findInstalledPlugin(inventory.plugins, pluginId);
if (installed === undefined) throw missingInstalledError(fallback);
return resolveInstalledRecord(installed);
},
});
bb.ui.registerMentionProvider({
id: "community",
label: "Community",
async search({ query }) {
try {
const entries = await boundedSdkRead((signal) =>
bb.sdk.plugins.catalog.search({ query, signal }),
);
return searchCommunityPlugins(entries, query);
} catch {
return [];
}
},
async resolve(itemId) {
let identity: ReturnType<typeof decodeCommunityItemId>;
try {
identity = decodeCommunityItemId(itemId);
if (identity.marketplace !== COMMUNITY_MARKETPLACE) {
throw invalidCommunityReferenceError();
}
} catch {
throw invalidCommunityReferenceError();
}
const fallback = fallbackTarget(identity.pluginId);
let inventory: Awaited<ReturnType<BbPluginApi["sdk"]["plugins"]["list"]>>;
try {
inventory = await boundedSdkRead((signal) => bb.sdk.plugins.list({ signal }));
} catch {
throw inventoryVerificationError(fallback);
}
const installed = findInstalledPlugin(inventory.plugins, identity.pluginId);
if (installed !== undefined) return resolveInstalledRecord(installed);
let entries: Awaited<
ReturnType<BbPluginApi["sdk"]["plugins"]["catalog"]["search"]>
>;
try {
entries = await boundedSdkRead((signal) =>
bb.sdk.plugins.catalog.search({ query: identity.pluginId, signal }),
);
} catch {
throw communityVerificationError(fallback);
}
const entry = exactCommunityEntry(entries, identity);
if (entry === undefined) throw communityMissingError(fallback);
const liveTarget = boundUntrustedText(entry.displayName, MAX_ITEM_TITLE_BYTES);
if (liveTarget.length === 0) throw communityMissingError(fallback);
if (!entry.compatible) throw communityIncompatibleError(liveTarget);
if (entry.installed) throw communityMissingError(liveTarget);
return {
context: buildCommunityPluginContext({
name: liveTarget,
pluginId: entry.pluginId,
marketplace: entry.marketplace,
entryId: entry.entryId,
}),
};
},
});
}