|
| 1 | +import type { ToolResult } from '../../types/tool-result.js'; |
| 2 | +import { SpaceSearchTool, type SpaceSearchResult } from '../../space-search.js'; |
| 3 | +import { escapeMarkdown } from '../../utilities.js'; |
| 4 | + |
| 5 | +// Default number of results to return |
| 6 | +const DEFAULT_RESULTS_LIMIT = 10; |
| 7 | + |
| 8 | +/** |
| 9 | + * Prompt configuration for discover operation |
| 10 | + * These prompts can be easily tweaked to adjust the search behavior |
| 11 | + */ |
| 12 | +const DISCOVER_PROMPTS = { |
| 13 | + // Task hints shown when called with blank query |
| 14 | + TASK_HINTS: `Here are some examples of tasks that dynamic spaces can perform: |
| 15 | +
|
| 16 | +- Image Generation |
| 17 | +- Video Generation |
| 18 | +- Text Generation |
| 19 | +- Visual QA |
| 20 | +- Language Translation |
| 21 | +- Speech Synthesis |
| 22 | +- 3D Modeling |
| 23 | +- Object Detection |
| 24 | +- Text Analysis |
| 25 | +- Image Editing |
| 26 | +- Code Generation |
| 27 | +- Question Answering |
| 28 | +- Data Visualization |
| 29 | +- Voice Cloning |
| 30 | +- Background Removal |
| 31 | +- OCR |
| 32 | +- Image Captioning |
| 33 | +- Sentiment Analysis |
| 34 | +- Music Generation |
| 35 | +- Style Transfer |
| 36 | +
|
| 37 | +To discover MCP-enabled Spaces for a specific task, call this operation with a search query: |
| 38 | +
|
| 39 | +**Example:** |
| 40 | +\`\`\`json |
| 41 | +{ |
| 42 | + "operation": "discover", |
| 43 | + "search_query": "image generation", |
| 44 | + "limit": 10 |
| 45 | +} |
| 46 | +\`\`\``, |
| 47 | + |
| 48 | + // Header for search results |
| 49 | + RESULTS_HEADER: (query: string, showing: number, total: number) => { |
| 50 | + const showingText = showing < total ? `Showing ${showing} of ${total} results` : `All ${showing} results`; |
| 51 | + return `# MCP Space Discovery Results for "${query}" (${showingText}) |
| 52 | +
|
| 53 | +These MCP-enabled Spaces can be invoked using the \`dynamic_space\` tool. |
| 54 | +Use \`"operation": "view_parameters"\` to inspect a space's parameters before invoking. |
| 55 | +
|
| 56 | +`; |
| 57 | + }, |
| 58 | + |
| 59 | + // No results message |
| 60 | + NO_RESULTS: (query: string) => |
| 61 | + `No MCP-enabled Spaces found for "${query}". |
| 62 | +
|
| 63 | +Try: |
| 64 | +- Broader search terms (e.g., "image generation" instead of specific model names) |
| 65 | +- Task-focused queries (e.g., "text generation", "object detection") |
| 66 | +- Different task categories (e.g., "video generation", "image classification")`, |
| 67 | +}; |
| 68 | + |
| 69 | +/** |
| 70 | + * Discovers MCP-enabled Spaces based on search criteria |
| 71 | + * |
| 72 | + * @param searchQuery - The search query or task category |
| 73 | + * @param limit - Maximum number of results to return |
| 74 | + * @param hfToken - Optional HuggingFace API token |
| 75 | + * @returns Formatted search results |
| 76 | + */ |
| 77 | +export async function discoverSpaces( |
| 78 | + searchQuery?: string, |
| 79 | + limit: number = DEFAULT_RESULTS_LIMIT, |
| 80 | + hfToken?: string |
| 81 | +): Promise<ToolResult> { |
| 82 | + // Return task hints when called with blank query |
| 83 | + if (!searchQuery || searchQuery.trim() === '') { |
| 84 | + return { |
| 85 | + formatted: DISCOVER_PROMPTS.TASK_HINTS, |
| 86 | + totalResults: 0, |
| 87 | + resultsShared: 0, |
| 88 | + }; |
| 89 | + } |
| 90 | + |
| 91 | + try { |
| 92 | + // Use SpaceSearchTool to search for MCP-enabled spaces only |
| 93 | + const searchTool = new SpaceSearchTool(hfToken); |
| 94 | + const { results, totalCount } = await searchTool.search( |
| 95 | + searchQuery, |
| 96 | + limit, |
| 97 | + true // mcp = true (only MCP-enabled spaces) |
| 98 | + ); |
| 99 | + |
| 100 | + // Format and return results |
| 101 | + return formatDiscoverResults(searchQuery, results, totalCount); |
| 102 | + } catch (error) { |
| 103 | + const errorMessage = error instanceof Error ? error.message : String(error); |
| 104 | + return { |
| 105 | + formatted: `Error discovering spaces: ${errorMessage}`, |
| 106 | + totalResults: 0, |
| 107 | + resultsShared: 0, |
| 108 | + isError: true, |
| 109 | + }; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Formats discover results as a markdown table |
| 115 | + * Note: Author column is omitted as it's superfluous for invocation purposes |
| 116 | + * Duplication is OK for the mean time; space_search will be rolled in to a general tool |
| 117 | + */ |
| 118 | +function formatDiscoverResults(query: string, results: SpaceSearchResult[], totalCount: number): ToolResult { |
| 119 | + if (results.length === 0) { |
| 120 | + return { |
| 121 | + formatted: DISCOVER_PROMPTS.NO_RESULTS(query), |
| 122 | + totalResults: 0, |
| 123 | + resultsShared: 0, |
| 124 | + }; |
| 125 | + } |
| 126 | + |
| 127 | + let markdown = DISCOVER_PROMPTS.RESULTS_HEADER(query, results.length, totalCount); |
| 128 | + |
| 129 | + // Table header (without Author column) |
| 130 | + markdown += '| Space | Description | Space ID | Category | Likes | Trending | Relevance |\n'; |
| 131 | + markdown += '|-------|-------------|----------|----------|-------|----------|----------|\n'; |
| 132 | + |
| 133 | + // Table rows |
| 134 | + for (const result of results) { |
| 135 | + const title = result.title || 'Untitled'; |
| 136 | + const description = result.shortDescription || result.ai_short_description || 'No description'; |
| 137 | + const id = result.id || ''; |
| 138 | + const emoji = result.emoji ? escapeMarkdown(result.emoji) + ' ' : ''; |
| 139 | + const relevance = result.semanticRelevancyScore ? (result.semanticRelevancyScore * 100).toFixed(1) + '%' : 'N/A'; |
| 140 | + |
| 141 | + markdown += |
| 142 | + `| ${emoji}[${escapeMarkdown(title)}](https://hf.co/spaces/${id}) ` + |
| 143 | + `| ${escapeMarkdown(description)} ` + |
| 144 | + `| \`${escapeMarkdown(id)}\` ` + |
| 145 | + `| \`${escapeMarkdown(result.ai_category ?? '-')}\` ` + |
| 146 | + `| ${escapeMarkdown(result.likes?.toString() ?? '-')} ` + |
| 147 | + `| ${escapeMarkdown(result.trendingScore?.toString() ?? '-')} ` + |
| 148 | + `| ${relevance} |\n`; |
| 149 | + } |
| 150 | + |
| 151 | + return { |
| 152 | + formatted: markdown, |
| 153 | + totalResults: totalCount, |
| 154 | + resultsShared: results.length, |
| 155 | + }; |
| 156 | +} |
0 commit comments