Improve OpenRouter model lists in extensions (#5459) * fix: extensions OpenRouter model lists * fix: update JSDoc for optional mapping function parameter in fetchModelsByModality * fix: update JSDoc to clarify return type of fetchModelsByModality function * fix: encode output modality in fetchModelsByModality API request
Signed| @@ -38,11 +38,11 @@ router.post('/models/providers', async (req, res) => { | ||
| 38 | 38 | * @param {string} endpoint - The API endpoint to fetch from |
| 39 | 39 | * @param {string} inputModality - Required input modality |
| 40 | 40 | * @param {string} outputModality - Required output modality |
| 41 | 41 | * @param {boolean((model: any) => any) | null} [idsOnlymapFn=falsenull] - WhetherOptional tomapping returnfunction onlyto modeltransform IDsthe results |
| 42 | 42 | * @returns {Promise<any[]>} Filtered models and/or modelmapped IDsmodels |
| 43 | 43 | */ |
| 44 | 44 | async function fetchModelsByModality(endpoint, inputModality, outputModality, idsOnlymapFn = falsenull) { |
| 45 | 45 | const response = await fetch(`${API_OPENROUTER}${endpoint}?output_modalities=${encodeURIComponent(outputModality)}`, { |
| 46 | 46 | method: 'GET', |
| 47 | 47 | headers: { 'Accept': 'application/json' }, |
| 48 | 48 | }); |
| @@ -67,12 +67,12 @@ async function fetchModelsByModality(endpoint, inputModality, outputModality, id | ||
| 67 | 67 | .filter(m => m.architecture.output_modalities.includes(outputModality)) |
| 68 | 68 | .sort((a, b) => a?.id && b?.id ? a.id.localeCompare(b.id) : 0); |
| 69 | 69 | |
| 70 | 70 | return idsOnlytypeof mapFn === 'function' ? filtered.map(m => m.idmapFn) : filtered; |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | router.post('/models/multimodal', async (_req, res) => { |
| 74 | 74 | try { |
| 75 | 75 | const models = await fetchModelsByModality('/models', 'image', 'text', truem => m.id); |
| 76 | 76 | return res.json(models); |
| 77 | 77 | } catch (error) { |
| 78 | 78 | console.error(error); |
| @@ -82,7 +82,7 @@ router.post('/models/multimodal', async (_req, res) => { | ||
| 82 | 82 | |
| 83 | 83 | router.post('/models/embedding', async (_req, res) => { |
| 84 | 84 | try { |
| 85 | 85 | const models = await fetchModelsByModality('/embeddings/models', 'text', 'embeddings', m => ({ id: m.id, name: m.name })); |
| 86 | 86 | return res.json(models); |
| 87 | 87 | } catch (error) { |
| 88 | 88 | console.error(error); |
| @@ -92,8 +92,8 @@ router.post('/models/embedding', async (_req, res) => { | ||
| 92 | 92 | |
| 93 | 93 | router.post('/models/image', async (_req, res) => { |
| 94 | 94 | try { |
| 95 | 95 | const models = await fetchModelsByModality('/models', 'text', 'image', m => ({ value: m.id, text: m.name || m.id })); |
| 96 | - return res.json(models.map(m => ({ value: m.id, text: m.name || m.id }))); | |
| 96 | + return res.json(models); | |
| 97 | 97 | } catch (error) { |
| 98 | 98 | console.error(error); |
| 99 | 99 | return res.sendStatus(500); |
| @@ -132,7 +132,7 @@ router.post('/image/generate', async (req, res) => { | ||
| 132 | 132 | content: prompt, |
| 133 | 133 | }, |
| 134 | 134 | ], |
| 135 | 135 | modalities: ['image', 'text'], |
| 136 | 136 | image_config: { |
| 137 | 137 | aspect_ratio: req.body.aspect_ratio || '1:1', |
| 138 | 138 | }, |