import type { DiscoveryResult, ModelBrowserOptions, ModelCapability, ModelInfo } from "./types.js"; import { ModelRegistry } from "./model-registry.js"; export const DEFAULT_MODEL_LIMIT = 20; export const MAX_MODEL_LIMIT = 50; export function renderModelBrowser(result: DiscoveryResult, options: ModelBrowserOptions): string { const limit = Math.min(Math.max(1, options.limit), MAX_MODEL_LIMIT); const page = Math.max(1, options.page); const registry = new ModelRegistry(result.models); const all = registry.all(); const keyword = options.search?.trim().toLowerCase(); const imageModels = all.filter((model) => model.capabilities.some((capability) => capability === "image_generation" || capability === "image_edit" || capability === "image_variation")); const visibleModels = options.includeText || keyword ? all : imageModels; const filtered = keyword ? visibleModels.filter((model) => `${model.id} ${JSON.stringify(model.rawMetadata)}`.toLowerCase().includes(keyword)) : visibleModels; const start = (page - 1) * limit; const shown = filtered.slice(start, start + limit); const totalPages = Math.max(1, Math.ceil(filtered.length / limit)); const lines = [ `提供商:${result.providerName} (${result.providerId})`, `模型总数:${all.length}`, keyword ? `搜索:${keyword}` : options.includeText ? "视图:显示全部模型" : "视图:只显示图片相关模型", `显示:${filtered.length} 个,第 ${Math.min(page, totalPages)} / ${totalPages} 页,每页 ${limit} 个`, "", "模型列表(每个模型只显示一次;能力仅表示服务端声明、名称推测或手动配置,并非实际接口验证):", ]; if (shown.length === 0) lines.push("- 无"); else shown.forEach((model, index) => lines.push(`${start + index + 1}. ${formatModel(model)}`)); if (filtered.length > start + shown.length) lines.push(`... 还有 ${filtered.length - start - shown.length} 个。调整 page 查看。`); const summary = registry.summary(); lines.push("", `能力汇总:生图 ${summary.image_generation.length} · 编辑 ${summary.image_edit.length} · 变体 ${summary.image_variation.length} · 视觉 ${summary.vision.length} · 未知 ${summary.unknown.length}`); try { lines.push(`当前默认生图模型:${registry.select("image_generation").id}`); } catch { lines.push("当前默认生图模型:无"); } if (!options.includeText && !keyword && all.length > imageModels.length) lines.push(`已隐藏 ${all.length - imageModels.length} 个非图片模型;传 include_text: true 可查看全部。`); return truncateLines(lines, 180, 12_000); } export function renderRefreshSummary(result: DiscoveryResult): string { const summary = new ModelRegistry(result.models).summary(); return [ `提供商已刷新:${result.providerName} (${result.providerId})`, `模型总数:${result.models.length}`, `图片生成:${summary.image_generation.length}`, `图片编辑:${summary.image_edit.length}`, `图片变体:${summary.image_variation.length}`, `视觉理解:${summary.vision.length}`, `未知能力:${summary.unknown.length}`, ].join("\n"); } function formatModel(model: ModelInfo): string { const labels: Record = { image_generation: "生图", image_edit: "编辑", image_variation: "变体", vision: "视觉", text: "文本", unknown: "未知", }; const source: Record = { metadata: "服务端声明", heuristic: "名称推测", manual: "手动配置", unknown: "无法判断", }; return `${model.id} [${model.capabilities.map((capability) => labels[capability]).join("、")}] [${source[model.capabilitySource]}]`; } function truncateLines(lines: string[], maxLines: number, maxChars: number): string { const out: string[] = []; let chars = 0; for (const line of lines) { if (out.length >= maxLines || chars + line.length + 1 > maxChars) { out.push("... 输出已截断。请在 /image-config 中使用搜索、分页或选择提供商缩小结果。"); break; } out.push(line); chars += line.length + 1; } return out.join("\n"); }