Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/commands/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export const dev = async (options: OptionValues, command: BaseCommand) => {
siteInfo,
})

if (!options.offline && !options.offlineEnv) {
if (!options.offline && !options.offlineEnv && !capabilities.aiGatewayDisabled) {
await setupAIGateway({ api, env, siteID: site.id, siteURL: siteUrl })

// Parse AI Gateway context and inject provider API keys
Expand All @@ -169,6 +169,8 @@ export const dev = async (options: OptionValues, command: BaseCommand) => {
}
}
}
} else if (capabilities.aiGatewayDisabled) {
log(`${NETLIFYDEVLOG} AI Gateway is disabled for this account`)
}

injectEnvVariables(env)
Expand Down
5 changes: 4 additions & 1 deletion src/commands/functions/functions-serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { parseAIGatewayContext, setupAIGateway } from '@netlify/ai/bootstrap'

import { getBlobsContextWithEdgeAccess } from '../../lib/blobs/blobs.js'
import { startFunctionsServer } from '../../lib/functions/server.js'
import { NETLIFYDEVLOG, log } from '../../utils/command-helpers.js'
import { printBanner } from '../../utils/dev-server-banner.js'
import {
UNLINKED_SITE_MOCK_ID,
Expand Down Expand Up @@ -38,8 +39,10 @@ export const functionsServe = async (options: OptionValues, command: BaseCommand
siteInfo,
})

if (!options.offline) {
if (!options.offline && !capabilities.aiGatewayDisabled) {
await setupAIGateway({ api, env, siteID: site.id, siteURL: siteUrl })
} else if (capabilities.aiGatewayDisabled) {
log(`${NETLIFYDEVLOG} AI Gateway is disabled for this account`)
}

injectEnvVariables(env)
Expand Down
10 changes: 9 additions & 1 deletion src/utils/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ type Capabilities = NonNullable<ApiAccount['capabilities']> & {
included?: boolean | undefined
}
| undefined
ai_gateway_disabled?:
| {
included?: boolean | undefined
}
| undefined
}
export type Capability = keyof Capabilities
export type Account = ApiAccount & {
Expand Down Expand Up @@ -138,6 +143,7 @@ export const getSiteInformation = async ({ api, offline, site, siteInfo }) => {
accountId: account?.id,
capabilities: {
backgroundFunctions: supportsBackgroundFunctions(account),
aiGatewayDisabled: account?.capabilities?.ai_gateway_disabled?.included ?? false,
},
timeouts: {
syncFunctions: siteInfo.functions_timeout ?? siteInfo.functions_config?.timeout ?? SYNCHRONOUS_FUNCTION_TIMEOUT,
Expand All @@ -150,7 +156,9 @@ export const getSiteInformation = async ({ api, offline, site, siteInfo }) => {
return {
addonsUrls: {},
siteUrl: '',
capabilities: {},
capabilities: {
aiGatewayDisabled: false,
},
timeouts: {
syncFunctions: SYNCHRONOUS_FUNCTION_TIMEOUT,
backgroundFunctions: BACKGROUND_FUNCTION_TIMEOUT,
Expand Down
37 changes: 37 additions & 0 deletions tests/integration/commands/dev/ai-gateway.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { withSiteBuilder } from '../../utils/site-builder.js'
import {
assertAIGatewayValue,
createAIGatewayCheckFunction,
createAIGatewayDisabledTestData,
createAIGatewayTestData,
createMockApiFailureRoutes,
} from '../../utils/ai-gateway-helpers.js'
Expand Down Expand Up @@ -191,6 +192,42 @@ describe.concurrent('AI Gateway Integration', () => {
})
})

test('should not setup AI Gateway when account has ai_gateway_disabled', async (t) => {
await withSiteBuilder(t, async (builder) => {
const { siteInfo, routes } = createAIGatewayDisabledTestData()
const checkFunction = createAIGatewayCheckFunction()

await builder
.withContentFile({
path: checkFunction.path,
content: checkFunction.content,
})
.build()

await withMockApi(routes, async ({ apiUrl }) => {
await withDevServer(
{
cwd: builder.directory,
offline: false,
env: {
NETLIFY_API_URL: apiUrl,
NETLIFY_SITE_ID: siteInfo.id,
NETLIFY_AUTH_TOKEN: 'fake-token',
AI_GATEWAY: undefined,
},
},
async (server) => {
const response = await fetch(`${server.url}${checkFunction.urlPath}`)
const result = (await response.json()) as { hasAIGateway: boolean }

t.expect(response.status).toBe(200)
t.expect(result.hasAIGateway).toBe(false)
},
)
})
})
})

test('should work with V2 functions', async (t) => {
await withSiteBuilder(t, async (builder) => {
const { siteInfo, aiGatewayToken, routes } = createAIGatewayTestData()
Expand Down
21 changes: 21 additions & 0 deletions tests/integration/utils/ai-gateway-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,24 @@ export const createMockApiFailureRoutes = (siteInfo: {
{ path: 'sites/test-site-id/ai-gateway/token', status: 404, response: { message: 'Not Found' } },
{ path: 'ai-gateway/providers', status: 404, response: { message: 'Not Found' } },
]

export const createAIGatewayDisabledTestData = () => {
const siteInfo = {
account_slug: 'test-account',
id: 'test-site-id',
name: 'site-name',
ssl_url: 'https://test-site.netlify.app',
}

const routes = [
{ path: 'sites/test-site-id', response: siteInfo },
{ path: 'sites/test-site-id/service-instances', response: [] },
{
path: 'accounts',
response: [{ slug: siteInfo.account_slug, capabilities: { ai_gateway_disabled: { included: true } } }],
},
{ path: 'accounts/test-account/env', response: [] },
]

return { siteInfo, routes }
}
Loading