|
| 1 | +import { z } from 'zod'; |
| 2 | +import type { ToolResult } from './types/tool-result.js'; |
| 3 | +import { ModelDetailTool } from './model-detail.js'; |
| 4 | +import { DatasetDetailTool } from './dataset-detail.js'; |
| 5 | +import { spaceInfo } from '@huggingface/hub'; |
| 6 | +import { formatDate } from './utilities.js'; |
| 7 | + |
| 8 | +export const HUB_INSPECT_TOOL_CONFIG = { |
| 9 | + name: 'hub_inspect', |
| 10 | + description: |
| 11 | + 'Get details for one or more Hugging Face repos (model, dataset, or space). ' + |
| 12 | + 'Auto-detects type unless specified.', |
| 13 | + schema: z.object({ |
| 14 | + repo_ids: z |
| 15 | + .array(z.string().min(1)) |
| 16 | + .min(1, 'Provide at least one id') |
| 17 | + .max(10, 'Provide at most 10 repo ids') |
| 18 | + .describe('Repo IDs for (models|dataset/space) - usually in author/name format (e.g. openai/gpt-oss-120b)'), |
| 19 | + repo_type: z.enum(['model', 'dataset', 'space']).optional().describe('Specify lookup type; otherwise auto-detects'), |
| 20 | + include_readme: z.boolean().default(true).describe('Include README from the repo'), |
| 21 | + }), |
| 22 | + annotations: { |
| 23 | + title: 'Hub Inspect', |
| 24 | + destructiveHint: false, |
| 25 | + readOnlyHint: true, |
| 26 | + openWorldHint: false, |
| 27 | + }, |
| 28 | +} as const; |
| 29 | + |
| 30 | +export type HubInspectParams = z.infer<typeof HUB_INSPECT_TOOL_CONFIG.schema>; |
| 31 | + |
| 32 | +export class HubInspectTool { |
| 33 | + private readonly modelDetail: ModelDetailTool; |
| 34 | + private readonly datasetDetail: DatasetDetailTool; |
| 35 | + private readonly hubUrl?: string; |
| 36 | + |
| 37 | + constructor(hfToken?: string, hubUrl?: string) { |
| 38 | + this.modelDetail = new ModelDetailTool(hfToken, hubUrl); |
| 39 | + this.datasetDetail = new DatasetDetailTool(hfToken, hubUrl); |
| 40 | + this.hubUrl = hubUrl; |
| 41 | + } |
| 42 | + |
| 43 | + async inspect(params: HubInspectParams, includeReadme: boolean = false): Promise<ToolResult> { |
| 44 | + const parts: string[] = []; |
| 45 | + let successCount = 0; |
| 46 | + |
| 47 | + for (const id of params.repo_ids) { |
| 48 | + try { |
| 49 | + const section = await this.inspectSingle(id, params.repo_type, includeReadme); |
| 50 | + parts.push(section); |
| 51 | + successCount += 1; |
| 52 | + } catch (err) { |
| 53 | + const msg = err instanceof Error ? err.message : String(err); |
| 54 | + parts.push(`# ${id}\n\n- Error: ${msg}`); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return { |
| 59 | + formatted: parts.join('\n\n---\n\n'), |
| 60 | + totalResults: params.repo_ids.length, |
| 61 | + resultsShared: successCount, |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + private async inspectSingle( |
| 66 | + repoId: string, |
| 67 | + type: 'model' | 'dataset' | 'space' | undefined, |
| 68 | + includeReadme: boolean |
| 69 | + ): Promise<string> { |
| 70 | + // If caller constrained the type, do only that |
| 71 | + if (type === 'model') { |
| 72 | + return (await this.modelDetail.getDetails(repoId, includeReadme)).formatted; |
| 73 | + } |
| 74 | + if (type === 'dataset') { |
| 75 | + return (await this.datasetDetail.getDetails(repoId, includeReadme)).formatted; |
| 76 | + } |
| 77 | + if (type === 'space') { |
| 78 | + return await this.getSpaceDetails(repoId); |
| 79 | + } |
| 80 | + |
| 81 | + // Auto-detect: attempt all three and aggregate. The same id may exist for multiple types. |
| 82 | + const matches: string[] = []; |
| 83 | + |
| 84 | + try { |
| 85 | + const r = await this.modelDetail.getDetails(repoId, includeReadme); |
| 86 | + matches.push(`**Type: Model**\n\n${r.formatted}`); |
| 87 | + } catch { |
| 88 | + /* not a model */ |
| 89 | + } |
| 90 | + |
| 91 | + try { |
| 92 | + const r = await this.datasetDetail.getDetails(repoId, includeReadme); |
| 93 | + matches.push(`**Type: Dataset**\n\n${r.formatted}`); |
| 94 | + } catch { |
| 95 | + /* not a dataset */ |
| 96 | + } |
| 97 | + |
| 98 | + try { |
| 99 | + const r = await this.getSpaceDetails(repoId); |
| 100 | + matches.push(`**Type: Space**\n\n${r}`); |
| 101 | + } catch { |
| 102 | + /* not a space */ |
| 103 | + } |
| 104 | + |
| 105 | + if (matches.length === 0) { |
| 106 | + throw new Error(`Could not find repo '${repoId}' as model, dataset, or space.`); |
| 107 | + } |
| 108 | + |
| 109 | + return matches.join('\n\n---\n\n'); |
| 110 | + } |
| 111 | + |
| 112 | + private async getSpaceDetails(spaceId: string): Promise<string> { |
| 113 | + const additionalFields = ['author', 'tags', 'runtime', 'subdomain', 'sha'] as const; |
| 114 | + const info = await spaceInfo<(typeof additionalFields)[number]>({ |
| 115 | + name: spaceId, |
| 116 | + additionalFields: Array.from(additionalFields), |
| 117 | + ...(this.hubUrl && { hubUrl: this.hubUrl }), |
| 118 | + }); |
| 119 | + |
| 120 | + const lines: string[] = []; |
| 121 | + lines.push(`# ${info.name}`); |
| 122 | + lines.push(''); |
| 123 | + lines.push('## Overview'); |
| 124 | + interface SpaceExtra { |
| 125 | + author?: string; |
| 126 | + tags?: readonly string[] | string[]; |
| 127 | + runtime?: unknown; |
| 128 | + subdomain?: string; |
| 129 | + sha?: string; |
| 130 | + } |
| 131 | + const extra = info as Partial<SpaceExtra>; |
| 132 | + if (extra.author) lines.push(`- **Author:** ${extra.author}`); |
| 133 | + if (info.sdk) lines.push(`- **SDK:** ${info.sdk}`); |
| 134 | + lines.push(`- **Likes:** ${info.likes}`); |
| 135 | + lines.push(`- **Updated:** ${formatDate(info.updatedAt)}`); |
| 136 | + const tags = Array.isArray(extra.tags) ? extra.tags : undefined; |
| 137 | + if (tags && tags.length) lines.push(`- **Tags:** ${tags.join(', ')}`); |
| 138 | + lines.push(''); |
| 139 | + lines.push(`**Link:** [https://hf.co/spaces/${info.name}](https://hf.co/spaces/${info.name})`); |
| 140 | + return lines.join('\n'); |
| 141 | + } |
| 142 | +} |
0 commit comments