TS-based next.config & ~/server/env build

This commit is contained in:
Enrico Ros
2025-04-18 19:06:43 -07:00
parent 6c5db40bd0
commit d308739643
12 changed files with 33 additions and 21 deletions
+1 -1
View File
@@ -3,7 +3,7 @@
This document provides an explanation of the environment variables used in the big-AGI application.
**All variables are optional**; and _UI options_ take precedence over _backend environment variables_,
which take place over _defaults_. This file is kept in sync with [`../src/server/env.mjs`](../src/server/env.mjs).
which take place over _defaults_. This file is kept in sync with [`../src/server/env.ts`](../src/server/env.ts).
### Setting Environment Variables
+11 -10
View File
@@ -1,5 +1,6 @@
import type { NextConfig } from 'next';
import { execSync } from 'node:child_process';
import { readFile } from 'node:fs/promises';
import { readFileSync } from 'node:fs';
// Build information: from CI, or git commit hash
let buildHash = process.env.NEXT_PUBLIC_BUILD_HASH || process.env.GITHUB_SHA || process.env.VERCEL_GIT_COMMIT_SHA; // Docker or custom, GitHub Actions, Vercel
@@ -13,21 +14,21 @@ try {
}
// The following are used by/available to Release.buildInfo(...)
process.env.NEXT_PUBLIC_BUILD_HASH = (buildHash || '').slice(0, 10);
process.env.NEXT_PUBLIC_BUILD_PKGVER = JSON.parse('' + await readFile(new URL('./package.json', import.meta.url))).version;
process.env.NEXT_PUBLIC_BUILD_PKGVER = JSON.parse('' + readFileSync(new URL('./package.json', import.meta.url))).version;
process.env.NEXT_PUBLIC_BUILD_TIMESTAMP = new Date().toISOString();
process.env.NEXT_PUBLIC_DEPLOYMENT_TYPE = process.env.NEXT_PUBLIC_DEPLOYMENT_TYPE || (process.env.VERCEL_ENV ? `vercel-${process.env.VERCEL_ENV}` : 'local'); // Docker or custom, Vercel
console.log(` 🧠 \x1b[1mbig-AGI\x1b[0m v${process.env.NEXT_PUBLIC_BUILD_PKGVER} (@${process.env.NEXT_PUBLIC_BUILD_HASH})`);
// Non-default build types
const buildType =
process.env.BIG_AGI_BUILD === 'standalone' ? 'standalone'
: process.env.BIG_AGI_BUILD === 'static' ? 'export'
process.env.BIG_AGI_BUILD === 'standalone' ? 'standalone' as const
: process.env.BIG_AGI_BUILD === 'static' ? 'export' as const
: undefined;
buildType && console.log(` 🧠 big-AGI: building for ${buildType}...\n`);
/** @type {import('next').NextConfig} */
let nextConfig = {
let nextConfig: NextConfig = {
reactStrictMode: true,
// [exports] https://nextjs.org/docs/advanced-features/static-html-export
@@ -46,7 +47,7 @@ let nextConfig = {
// NOTE: we may not be needing this anymore, as we use '@cloudflare/puppeteer'
serverExternalPackages: ['puppeteer-core'],
webpack: (config, { isServer }) => {
webpack: (config: any, { isServer }: { isServer: boolean }) => {
// @mui/joy: anything material gets redirected to Joy
config.resolve.alias['@mui/material'] = '@mui/joy';
@@ -104,13 +105,13 @@ let nextConfig = {
};
// Validate environment variables, if set at build time. Will be actually read and used at runtime.
// This is the reason both this file and the servr/env.mjs files have this extension.
await import('./src/server/env.mjs');
import { verifyBuildTimeVars } from '~/server/env';
verifyBuildTimeVars();
// conditionally enable the nextjs bundle analyzer
import withBundleAnalyzer from '@next/bundle-analyzer';
if (process.env.ANALYZE_BUNDLE) {
const { default: withBundleAnalyzer } = await import('@next/bundle-analyzer');
nextConfig = withBundleAnalyzer({ openAnalyzer: true })(nextConfig);
nextConfig = withBundleAnalyzer({ openAnalyzer: true })(nextConfig) as NextConfig;
}
export default nextConfig;
+1 -1
View File
@@ -3,7 +3,7 @@ import { z } from 'zod';
import { Release } from '~/common/app.release';
import { createTRPCRouter, publicProcedure } from '~/server/trpc/trpc.server';
import { env } from '~/server/env.mjs';
import { env } from '~/server/env';
import { fetchJsonOrTRPCThrow } from '~/server/trpc/trpc.router.fetchers';
// critical to make sure we `import type` here
+1 -1
View File
@@ -6,7 +6,7 @@ import { default as TurndownService } from 'turndown';
import { load as cheerioLoad } from 'cheerio';
import { createTRPCRouter, publicProcedure } from '~/server/trpc/trpc.server';
import { env } from '~/server/env.mjs';
import { env } from '~/server/env';
import { workerPuppeteerDownloadFileOrThrow } from './browse.files';
+1 -1
View File
@@ -1,7 +1,7 @@
import { z } from 'zod';
import { createTRPCRouter, publicProcedure } from '~/server/trpc/trpc.server';
import { env } from '~/server/env.mjs';
import { env } from '~/server/env';
import { fetchJsonOrTRPCThrow, fetchResponseOrTRPCThrow } from '~/server/trpc/trpc.router.fetchers';
+1 -1
View File
@@ -2,7 +2,7 @@ import { TRPCError } from '@trpc/server';
import { z } from 'zod';
import { createTRPCRouter, publicProcedure } from '~/server/trpc/trpc.server';
import { env } from '~/server/env.mjs';
import { env } from '~/server/env';
import { fetchJsonOrTRPCThrow } from '~/server/trpc/trpc.router.fetchers';
import { Search } from './search.types';
@@ -1,7 +1,7 @@
import { z } from 'zod';
import { createTRPCRouter, publicProcedure } from '~/server/trpc/trpc.server';
import { env } from '~/server/env.mjs';
import { env } from '~/server/env';
import { fetchJsonOrTRPCThrow } from '~/server/trpc/trpc.router.fetchers';
import { LLM_IF_ANT_PromptCaching, LLM_IF_OAI_Chat, LLM_IF_OAI_Fn, LLM_IF_OAI_Vision } from '~/common/stores/llms/llms.types';
@@ -1,5 +1,5 @@
import { z } from 'zod';
import { env } from '~/server/env.mjs';
import { env } from '~/server/env';
import packageJson from '../../../../../package.json';
@@ -1,7 +1,7 @@
import { z } from 'zod';
import { createTRPCRouter, publicProcedure } from '~/server/trpc/trpc.server';
import { env } from '~/server/env.mjs';
import { env } from '~/server/env';
import { fetchJsonOrTRPCThrow, fetchTextOrTRPCThrow } from '~/server/trpc/trpc.router.fetchers';
import { LLM_IF_OAI_Chat, LLM_IF_OAI_Fn, LLM_IF_OAI_Vision } from '~/common/stores/llms/llms.types';
@@ -2,7 +2,7 @@ import { z } from 'zod';
import { TRPCError } from '@trpc/server';
import { createTRPCRouter, publicProcedure } from '~/server/trpc/trpc.server';
import { env } from '~/server/env.mjs';
import { env } from '~/server/env';
import { fetchJsonOrTRPCThrow } from '~/server/trpc/trpc.router.fetchers';
import { serverCapitalizeFirstLetter } from '~/server/wire';
+1 -1
View File
@@ -1,7 +1,7 @@
import { z } from 'zod';
import { createTRPCRouter, publicProcedure } from '~/server/trpc/trpc.server';
import { env } from '~/server/env.mjs';
import { env } from '~/server/env';
import { fetchJsonOrTRPCThrow } from '~/server/trpc/trpc.router.fetchers';
import { getPngDimensionsFromBytes, t2iCreateImagesOutputSchema } from '../t2i.server';
+12 -1
View File
@@ -1,4 +1,5 @@
import { createEnv } from '@t3-oss/env-nextjs';
// noinspection ES6PreferShortImport - because the build would not find this file with ~/...
import { createEnv } from '../modules/3rdparty/t3-env';
import { z } from 'zod';
export const env = createEnv({
@@ -123,3 +124,13 @@ export const env = createEnv({
NEXT_PUBLIC_PLANTUML_SERVER_URL: process.env.NEXT_PUBLIC_PLANTUML_SERVER_URL,
},
});
/**
* Dummy function to validate any build-time environment variables.
* Does nothing really, but forces the creation of the `env` object.
*
* At runtime the `env` object is actually used.
*/
export function verifyBuildTimeVars(): number {
return Object.keys(env).length;
}