|
|
|
|
@@ -52,35 +52,24 @@ function splitServer(canonical: string): { server: string; short: string } {
|
|
|
|
|
: { server: canonical.slice(0, i), short: canonical.slice(i + 1) };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createFavouriteIndexPlugin(config: FavouriteIndexConfig): ProxyModelPlugin {
|
|
|
|
|
const favourites = config.favourites ?? [];
|
|
|
|
|
const cap = config.maxFavourites && config.maxFavourites > 0 ? config.maxFavourites : undefined;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
name: 'favourite-index',
|
|
|
|
|
description:
|
|
|
|
|
'Two-namespace tool presentation: curated favourite/<tool> + full all/<server>/<tool>, prefer favourites.',
|
|
|
|
|
|
|
|
|
|
async onInitialize() {
|
|
|
|
|
return { instructions: FAVOURITE_INDEX_INSTRUCTION };
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async onToolsList(tools: ToolDefinition[], ctx: PluginSessionContext): Promise<ToolDefinition[]> {
|
|
|
|
|
// Distinguish upstream tools (in the discovered catalog) from virtual
|
|
|
|
|
// tools (gate/agent) that must pass through untouched.
|
|
|
|
|
const upstream = await ctx.discoverTools();
|
|
|
|
|
const upstreamNames = new Set(upstream.map((t) => t.name));
|
|
|
|
|
const upstreamTools = tools.filter((t) => upstreamNames.has(t.name));
|
|
|
|
|
const passthrough = tools.filter((t) => !upstreamNames.has(t.name));
|
|
|
|
|
|
|
|
|
|
// Gated / empty catalog → no-op (leave the list exactly as gate produced it).
|
|
|
|
|
if (upstreamTools.length === 0) {
|
|
|
|
|
ctx.state.delete(RESOLVER_KEY);
|
|
|
|
|
return tools;
|
|
|
|
|
interface Presentation {
|
|
|
|
|
tools: ToolDefinition[];
|
|
|
|
|
/** presented name → canonical `server/tool` */
|
|
|
|
|
resolver: Map<string, string>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reshape an upstream catalog into favourites-first + full all/ catalog, and
|
|
|
|
|
* the presented→canonical routing map. Pure — shared by onToolsList and the
|
|
|
|
|
* lazy rebuild in onToolCallBefore.
|
|
|
|
|
*/
|
|
|
|
|
function buildPresentation(
|
|
|
|
|
upstreamTools: ToolDefinition[],
|
|
|
|
|
favourites: string[],
|
|
|
|
|
cap?: number,
|
|
|
|
|
): Presentation {
|
|
|
|
|
const byName = new Map(upstreamTools.map((t) => [t.name, t]));
|
|
|
|
|
const resolver = new Map<string, string>(); // presented → canonical
|
|
|
|
|
const resolver = new Map<string, string>();
|
|
|
|
|
const usedPresented = new Set<string>();
|
|
|
|
|
const seenCanonical = new Set<string>();
|
|
|
|
|
|
|
|
|
|
@@ -118,15 +107,63 @@ export function createFavouriteIndexPlugin(config: FavouriteIndexConfig): ProxyM
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return { tools: [...favTools, ...allTools], resolver };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createFavouriteIndexPlugin(config: FavouriteIndexConfig): ProxyModelPlugin {
|
|
|
|
|
const favourites = config.favourites ?? [];
|
|
|
|
|
const cap = config.maxFavourites && config.maxFavourites > 0 ? config.maxFavourites : undefined;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
name: 'favourite-index',
|
|
|
|
|
description:
|
|
|
|
|
'Two-namespace tool presentation: curated favourite/<tool> + full all/<server>/<tool>, prefer favourites.',
|
|
|
|
|
|
|
|
|
|
async onInitialize() {
|
|
|
|
|
return { instructions: FAVOURITE_INDEX_INSTRUCTION };
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async onToolsList(tools: ToolDefinition[], ctx: PluginSessionContext): Promise<ToolDefinition[]> {
|
|
|
|
|
// Distinguish upstream tools (in the discovered catalog) from virtual
|
|
|
|
|
// tools (gate/agent) that must pass through untouched.
|
|
|
|
|
const upstream = await ctx.discoverTools();
|
|
|
|
|
const upstreamNames = new Set(upstream.map((t) => t.name));
|
|
|
|
|
const upstreamTools = tools.filter((t) => upstreamNames.has(t.name));
|
|
|
|
|
const passthrough = tools.filter((t) => !upstreamNames.has(t.name));
|
|
|
|
|
|
|
|
|
|
// Gated / empty catalog → no-op (leave the list exactly as gate produced it).
|
|
|
|
|
if (upstreamTools.length === 0) {
|
|
|
|
|
ctx.state.delete(RESOLVER_KEY);
|
|
|
|
|
return tools;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { tools: presented, resolver } = buildPresentation(upstreamTools, favourites, cap);
|
|
|
|
|
ctx.state.set(RESOLVER_KEY, resolver);
|
|
|
|
|
return [...favTools, ...allTools, ...passthrough];
|
|
|
|
|
return [...presented, ...passthrough];
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async onToolCallBefore(toolName, _args, request, ctx) {
|
|
|
|
|
const resolver = ctx.state.get(RESOLVER_KEY) as Map<string, string> | undefined;
|
|
|
|
|
const canonical =
|
|
|
|
|
resolver?.get(toolName) ??
|
|
|
|
|
(toolName.startsWith('all/') ? toolName.slice('all/'.length) : undefined);
|
|
|
|
|
if (!toolName.startsWith('favourite/') && !toolName.startsWith('all/')) return null;
|
|
|
|
|
|
|
|
|
|
let resolver = ctx.state.get(RESOLVER_KEY) as Map<string, string> | undefined;
|
|
|
|
|
let canonical = resolver?.get(toolName);
|
|
|
|
|
|
|
|
|
|
// Resolver missing (a call arrived before tools/list this session): rebuild
|
|
|
|
|
// it so favourite/ names — which, unlike all/, can't be prefix-stripped —
|
|
|
|
|
// still route.
|
|
|
|
|
if (canonical === undefined) {
|
|
|
|
|
const upstream = await ctx.discoverTools();
|
|
|
|
|
if (upstream.length > 0) {
|
|
|
|
|
resolver = buildPresentation(upstream, favourites, cap).resolver;
|
|
|
|
|
ctx.state.set(RESOLVER_KEY, resolver);
|
|
|
|
|
canonical = resolver.get(toolName);
|
|
|
|
|
}
|
|
|
|
|
// all/ is always derivable by stripping the prefix.
|
|
|
|
|
if (canonical === undefined && toolName.startsWith('all/')) {
|
|
|
|
|
canonical = toolName.slice('all/'.length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (canonical && canonical !== toolName) {
|
|
|
|
|
// Rewrite presented name → canonical `server/tool`, then continue the
|
|
|
|
|
// chain (return null) so normal routing + content-pipeline still run.
|
|
|
|
|
|