UNPKG

@utcp/sdk

Version:

Universal Tool Calling Protocol (UTCP) client library for TypeScript

68 lines 2.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TagSearchStrategy = void 0; /** * Implements a tool search strategy based on tag and description matching. * Tools are scored based on the occurrence of query words in their tags and description. */ class TagSearchStrategy { /** * Creates an instance of TagSearchStrategy. * * @param toolRepository The repository to search for tools. * @param descriptionWeight The weight to apply to words found in the tool's description. */ constructor(toolRepository, descriptionWeight = 0.3) { this.toolRepository = toolRepository; this.descriptionWeight = descriptionWeight; } /** * Searches for tools by matching tags and description content against a query. * * @param query The search query string. * @param limit The maximum number of tools to return. If 0, all matched tools are returned. * @returns A promise that resolves to a list of tools ordered by relevance. */ async searchTools(query, limit = 10) { const queryLower = query.toLowerCase(); const queryWords = new Set(queryLower.match(/\w+/g) || []); const tools = await this.toolRepository.getTools(); const toolScores = tools.map(tool => { let score = 0.0; // Score from explicit tags (weight 1.0) if (tool.tags) { for (const tag of tool.tags) { const tagLower = tag.toLowerCase(); if (queryLower.includes(tagLower)) { score += 1.0; } // Score from words within the tag const tagWords = new Set(tagLower.match(/\w+/g) || []); for (const word of tagWords) { if (queryWords.has(word)) { score += this.descriptionWeight; // Partial match for tag words } } } } // Score from description (with lower weight) if (tool.description) { const descriptionWords = new Set(tool.description.toLowerCase().match(/\w+/g) || []); for (const word of descriptionWords) { if (queryWords.has(word) && word.length > 2) { score += this.descriptionWeight; } } } return { tool, score }; }); // Sort tools by score in descending order const sortedTools = toolScores .sort((a, b) => b.score - a.score) .map(item => item.tool); // Return up to 'limit' tools, or all if limit is 0 return limit > 0 ? sortedTools.slice(0, limit) : sortedTools; } } exports.TagSearchStrategy = TagSearchStrategy; //# sourceMappingURL=tag-search.js.map