UNPKG

@mastra/core

Version:

Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.

164 lines (162 loc) • 5.79 kB
import { MastraModelGateway } from './chunk-BJ6XIEC6.js'; import { InMemoryServerCache } from './chunk-3NTOFNIU.js'; import { MastraError } from './chunk-PZUZNPFM.js'; import { createAnthropic } from '@ai-sdk/anthropic-v5'; import { createGoogleGenerativeAI } from '@ai-sdk/google-v5'; import { createOpenAICompatible } from '@ai-sdk/openai-compatible-v5'; import { createOpenAI } from '@ai-sdk/openai-v5'; var NetlifyGateway = class extends MastraModelGateway { id = "netlify"; name = "Netlify AI Gateway"; tokenCache = new InMemoryServerCache(); async fetchProviders() { const response = await fetch("https://api.netlify.com/api/v1/ai-gateway/providers"); if (!response.ok) { throw new Error(`Failed to fetch from Netlify: ${response.statusText}`); } const data = await response.json(); const config = { apiKeyEnvVar: ["NETLIFY_TOKEN", "NETLIFY_SITE_ID"], apiKeyHeader: "Authorization", name: `Netlify`, gateway: `netlify`, models: [], docUrl: "https://docs.netlify.com/build/ai-gateway/overview/" }; for (const [providerId, provider] of Object.entries(data.providers)) { for (const model of provider.models) { config.models.push(`${providerId}/${model}`); } } return { netlify: config }; } async buildUrl(routerId, envVars) { const siteId = envVars?.["NETLIFY_SITE_ID"] || process.env["NETLIFY_SITE_ID"]; const netlifyToken = envVars?.["NETLIFY_TOKEN"] || process.env["NETLIFY_TOKEN"]; if (!netlifyToken) { throw new MastraError({ id: "NETLIFY_GATEWAY_NO_TOKEN", domain: "LLM", category: "UNKNOWN", text: `Missing NETLIFY_TOKEN environment variable required for model: ${routerId}` }); } if (!siteId) { throw new MastraError({ id: "NETLIFY_GATEWAY_NO_SITE_ID", domain: "LLM", category: "UNKNOWN", text: `Missing NETLIFY_SITE_ID environment variable required for model: ${routerId}` }); } try { const tokenData = await this.getOrFetchToken(siteId, netlifyToken); return tokenData.url.endsWith(`/`) ? tokenData.url.substring(0, tokenData.url.length - 1) : tokenData.url; } catch (error) { throw new MastraError({ id: "NETLIFY_GATEWAY_TOKEN_ERROR", domain: "LLM", category: "UNKNOWN", text: `Failed to get Netlify AI Gateway token for model ${routerId}: ${error instanceof Error ? error.message : String(error)}` }); } } /** * Get cached token or fetch a new site-specific AI Gateway token from Netlify */ async getOrFetchToken(siteId, netlifyToken) { const cacheKey = `netlify-token:${siteId}:${netlifyToken}`; const cached = await this.tokenCache.get(cacheKey); if (cached && cached.expiresAt > Date.now() / 1e3 + 60) { return { token: cached.token, url: cached.url }; } const response = await fetch(`https://api.netlify.com/api/v1/sites/${siteId}/ai-gateway/token`, { method: "GET", headers: { Authorization: `Bearer ${netlifyToken}` } }); if (!response.ok) { const error = await response.text(); throw new Error(`Failed to get Netlify AI Gateway token: ${response.status} ${error}`); } const tokenResponse = await response.json(); await this.tokenCache.set(cacheKey, { token: tokenResponse.token, url: tokenResponse.url, expiresAt: tokenResponse.expires_at }); return { token: tokenResponse.token, url: tokenResponse.url }; } /** * Get cached token or fetch a new site-specific AI Gateway token from Netlify */ async getApiKey(modelId) { const siteId = process.env["NETLIFY_SITE_ID"]; const netlifyToken = process.env["NETLIFY_TOKEN"]; if (!netlifyToken) { throw new MastraError({ id: "NETLIFY_GATEWAY_NO_TOKEN", domain: "LLM", category: "UNKNOWN", text: `Missing NETLIFY_TOKEN environment variable required for model: ${modelId}` }); } if (!siteId) { throw new MastraError({ id: "NETLIFY_GATEWAY_NO_SITE_ID", domain: "LLM", category: "UNKNOWN", text: `Missing NETLIFY_SITE_ID environment variable required for model: ${modelId}` }); } try { return (await this.getOrFetchToken(siteId, netlifyToken)).token; } catch (error) { throw new MastraError({ id: "NETLIFY_GATEWAY_TOKEN_ERROR", domain: "LLM", category: "UNKNOWN", text: `Failed to get Netlify AI Gateway token for model ${modelId}: ${error instanceof Error ? error.message : String(error)}` }); } } async resolveLanguageModel({ modelId, providerId, apiKey, headers }) { const baseURL = await this.buildUrl(`${providerId}/${modelId}`); switch (providerId) { case "openai": return createOpenAI({ apiKey, baseURL, headers }).responses(modelId); case "gemini": return createGoogleGenerativeAI({ baseURL: `${baseURL}/v1beta/`, apiKey, headers: { "user-agent": "google-genai-sdk/", ...headers ? headers : {} } }).chat(modelId); case "anthropic": return createAnthropic({ apiKey, baseURL: `${baseURL}/v1/`, headers: { "anthropic-version": "2023-06-01", "user-agent": "anthropic/", ...headers ? headers : {} } })(modelId); default: return createOpenAICompatible({ name: providerId, apiKey, baseURL, supportsStructuredOutputs: true }).chatModel( modelId ); } } }; export { NetlifyGateway }; //# sourceMappingURL=chunk-HSX2K7HB.js.map //# sourceMappingURL=chunk-HSX2K7HB.js.map