Skip to content
Merged
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
100 changes: 82 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@
"@netlify/blobs": "10.0.10",
"@netlify/build": "35.1.6",
"@netlify/build-info": "10.0.7",
"@netlify/ai": "0.2.1",
"@netlify/config": "24.0.3",
"@netlify/dev-utils": "4.1.3",
"@netlify/edge-bundler": "14.5.4",
"@netlify/edge-functions-bootstrap": "2.14.0",
"@netlify/edge-functions": "2.17.4",
"@netlify/headers-parser": "9.0.2",
"@netlify/local-functions-proxy": "2.0.3",
"@netlify/redirect-parser": "15.0.3",
Expand Down Expand Up @@ -157,7 +158,7 @@
"@bugsnag/js": "8.4.0",
"@eslint/compat": "1.3.2",
"@eslint/js": "9.24.0",
"@netlify/edge-functions": "2.17.4",
"@netlify/edge-functions-bootstrap": "2.14.0",
"@netlify/functions": "3.0.4",
"@netlify/types": "2.0.3",
"@sindresorhus/slugify": "2.2.1",
Expand Down
16 changes: 14 additions & 2 deletions src/commands/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
netlifyCommand,
} from '../../utils/command-helpers.js'
import detectServerSettings, { getConfigWithPlugins } from '../../utils/detect-server-settings.js'
import { parseAIGatewayContext, setupAIGateway } from '@netlify/ai/bootstrap'

import { UNLINKED_SITE_MOCK_ID, getDotEnvVariables, getSiteInformation, injectEnvVariables } from '../../utils/dev.js'
import { getEnvelopeEnv } from '../../utils/env/index.js'
import { ensureNetlifyIgnore } from '../../utils/gitignore.js'
Expand Down Expand Up @@ -143,8 +145,6 @@ export const dev = async (options: OptionValues, command: BaseCommand) => {
}

env = await getDotEnvVariables({ devConfig, env, site })
injectEnvVariables(env)
await promptEditorHelper({ chalk, config, log, NETLIFYDEVLOG, repositoryRoot, state })

const { accountId, addonsUrls, capabilities, siteUrl, timeouts } = await getSiteInformation({
// inherited from base command --offline
Expand All @@ -155,6 +155,14 @@ export const dev = async (options: OptionValues, command: BaseCommand) => {
siteInfo,
})

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

injectEnvVariables(env)

await promptEditorHelper({ chalk, config, log, NETLIFYDEVLOG, repositoryRoot, state })

let settings: ServerSettings
try {
settings = await detectServerSettings(devConfig, options, command)
Expand Down Expand Up @@ -204,7 +212,11 @@ export const dev = async (options: OptionValues, command: BaseCommand) => {
// FIXME(serhalp): `applyMutations` is `(any, any) => any)`. Add types in `@netlify/config`.
const mutatedConfig: typeof config = applyMutations(config, configMutations)

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const aiGatewayContext = parseAIGatewayContext(env.AI_GATEWAY?.value)

const functionsRegistry = await startFunctionsServer({
aiGatewayContext,
blobsContext,
command,
config: mutatedConfig,
Expand Down
12 changes: 11 additions & 1 deletion src/commands/functions/functions-serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { join } from 'path'

import { OptionValues } from 'commander'

import { parseAIGatewayContext, setupAIGateway } from '@netlify/ai/bootstrap'

import { getBlobsContextWithEdgeAccess } from '../../lib/blobs/blobs.js'
import { startFunctionsServer } from '../../lib/functions/server.js'
import { printBanner } from '../../utils/dev-server-banner.js'
Expand All @@ -28,7 +30,6 @@ export const functionsServe = async (options: OptionValues, command: BaseCommand
env.NETLIFY_DEV = { sources: ['internal'], value: 'true' }

env = await getDotEnvVariables({ devConfig: { ...config.dev }, env, site })
injectEnvVariables(env)

const { accountId, capabilities, siteUrl, timeouts } = await getSiteInformation({
offline: options.offline,
Expand All @@ -37,6 +38,12 @@ export const functionsServe = async (options: OptionValues, command: BaseCommand
siteInfo,
})

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

injectEnvVariables(env)

const functionsPort = await acquirePort({
configuredPort: options.port || config.dev?.functionsPort,
defaultPort: DEFAULT_PORT,
Expand All @@ -49,8 +56,11 @@ export const functionsServe = async (options: OptionValues, command: BaseCommand
siteID: site.id ?? UNLINKED_SITE_MOCK_ID,
})

const aiGatewayContext = parseAIGatewayContext(env.AI_GATEWAY?.value)

await startFunctionsServer({
loadDistFunctions: process.env.NETLIFY_FUNCTIONS_SERVE_LOAD_DIST_FUNCTIONS === 'true',
aiGatewayContext,
blobsContext,
config,
debug: options.debug,
Expand Down
10 changes: 10 additions & 0 deletions src/lib/functions/netlify-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import semver from 'semver'
import { logAndThrowError, type NormalizedCachedConfigConfig } from '../../utils/command-helpers.js'
import { BACKGROUND } from '../../utils/functions/get-functions.js'
import { type BlobsContextWithEdgeAccess, getBlobsEventProperty } from '../blobs/blobs.js'
import type { AIGatewayContext } from '@netlify/ai/bootstrap'
import type { ServerSettings } from '../../utils/types.js'

import type { BaseBuildResult, InvokeFunctionResult, Runtime } from './runtimes/index.js'
Expand Down Expand Up @@ -42,6 +43,7 @@ const getNextRun = function (schedule: string) {
}

export default class NetlifyFunction<BuildResult extends BaseBuildResult> {
private readonly aiGatewayContext?: AIGatewayContext | null
private readonly blobsContext: BlobsContextWithEdgeAccess
private readonly config: NormalizedCachedConfigConfig
private readonly directory?: string
Expand Down Expand Up @@ -74,6 +76,7 @@ export default class NetlifyFunction<BuildResult extends BaseBuildResult> {
private srcFiles = new Set<string>()

constructor({
aiGatewayContext,
blobsContext,
config,
directory,
Expand All @@ -87,6 +90,7 @@ export default class NetlifyFunction<BuildResult extends BaseBuildResult> {
timeoutBackground,
timeoutSynchronous,
}: {
aiGatewayContext?: AIGatewayContext | null
blobsContext: BlobsContextWithEdgeAccess
config: NormalizedCachedConfigConfig
directory?: string
Expand All @@ -101,6 +105,7 @@ export default class NetlifyFunction<BuildResult extends BaseBuildResult> {
timeoutBackground?: number
timeoutSynchronous?: number
}) {
this.aiGatewayContext = aiGatewayContext
this.blobsContext = blobsContext
this.config = config
this.directory = directory
Expand Down Expand Up @@ -284,6 +289,11 @@ export default class NetlifyFunction<BuildResult extends BaseBuildResult> {
event.blobs = Buffer.from(payload).toString('base64')
}

if (this.aiGatewayContext) {
const payload = JSON.stringify(this.aiGatewayContext)
event.aiGateway = Buffer.from(payload).toString('base64')
}

try {
const result = await this.runtime.invokeFunction({
context,
Expand Down
Loading
Loading