Skip to content

Commit e6823d4

Browse files
authored
feat: add AI Gateway support to @netlify/dev for Vite plugin (#524)
* feat: add AI Gateway support to @netlify/dev for Vite plugin * fix build/lint * set netlify env vars
1 parent e70ad54 commit e6823d4

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
"packages/redirects",
1919
"packages/runtime",
2020
"packages/static",
21-
"packages/dev",
2221
"packages/ai",
22+
"packages/dev",
2323
"packages/nuxt-module",
2424
"packages/vite-plugin",
2525
"packages/vite-plugin-tanstack-start"

packages/dev/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"vitest": "^3.0.0"
5757
},
5858
"dependencies": {
59+
"@netlify/ai": "^0.3.3",
5960
"@netlify/blobs": "10.3.3",
6061
"@netlify/config": "^23.2.0",
6162
"@netlify/dev-utils": "4.3.1",

packages/dev/src/main.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { IncomingMessage } from 'node:http'
33
import path from 'node:path'
44
import process from 'node:process'
55

6+
import { parseAIGatewayContext, setupAIGateway } from '@netlify/ai/bootstrap'
67
import { resolveConfig } from '@netlify/config'
78
import {
89
ensureNetlifyIgnore,
@@ -459,6 +460,35 @@ export class NetlifyDev {
459460

460461
this.#cleanupJobs.push(() => runtime.stop())
461462

463+
// Bootstrap AI Gateway: Fetch AI Gateway tokens and inject them into env
464+
if (this.#features.environmentVariables && config?.api && siteID && config?.siteInfo?.url) {
465+
await setupAIGateway({
466+
api: config.api,
467+
env: config.env || {},
468+
siteID,
469+
siteURL: config.siteInfo.url,
470+
})
471+
472+
// Inject AI_GATEWAY into process.env via runtime
473+
if (config.env.AI_GATEWAY) {
474+
runtime.env.set('AI_GATEWAY', config.env.AI_GATEWAY.value)
475+
476+
// Parse and inject AI Gateway env vars
477+
const aiGatewayContext = parseAIGatewayContext(config.env.AI_GATEWAY.value)
478+
if (aiGatewayContext) {
479+
runtime.env.set('NETLIFY_AI_GATEWAY_KEY', aiGatewayContext.token)
480+
runtime.env.set('NETLIFY_AI_GATEWAY_URL', aiGatewayContext.url)
481+
482+
if (aiGatewayContext.envVars) {
483+
for (const envVar of aiGatewayContext.envVars) {
484+
runtime.env.set(envVar.key, aiGatewayContext.token)
485+
runtime.env.set(envVar.url, aiGatewayContext.url)
486+
}
487+
}
488+
}
489+
}
490+
}
491+
462492
let serverAddress: string | undefined
463493

464494
// If a custom server has been provided, use it. If not, we must stand up

packages/dev/tsup.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ export default defineConfig([
1313
watch: argv.includes('--watch'),
1414
platform: 'node',
1515
bundle: true,
16+
external: ['@netlify/ai'],
1617
},
1718
])

0 commit comments

Comments
 (0)