Fix DashScope env loading for scene image generation

This commit is contained in:
2026-04-06 15:01:15 +08:00
parent fcd8d727b0
commit d678929064
23 changed files with 4943 additions and 138 deletions

View File

@@ -8,7 +8,7 @@ import http, {
import https from 'node:https';
import path from 'node:path';
import type { Plugin } from 'vite';
import { loadEnv, type Plugin } from 'vite';
const LLM_PROXY_PATH = '/api/llm/chat/completions';
const ITEM_CATALOG_PATH = '/api/item-catalog';
@@ -92,6 +92,17 @@ function normalizeDashScopeBaseUrl(value: string) {
return value.replace(/\/$/u, '');
}
function resolveRuntimeEnv(
rootDir: string,
mode: string,
env: Record<string, string>,
) {
return {
...env,
...loadEnv(mode, rootDir, ''),
};
}
function extractApiErrorMessage(responseText: string, fallbackMessage: string) {
if (!responseText.trim()) {
return fallbackMessage;
@@ -327,16 +338,24 @@ function proxyStreamingRequest(
});
}
function createLlmProxyPlugin(env: Record<string, string>): Plugin {
const upstreamBaseUrl = normalizeUpstreamBaseUrl(
env.VITE_LLM_BASE_URL ||
env.LLM_BASE_URL ||
'https://ark.cn-beijing.volces.com/api/v3',
);
const apiKey =
env.LLM_API_KEY || env.ARK_API_KEY || env.VITE_LLM_API_KEY || '';
function createLlmProxyPlugin(
rootDir: string,
mode: string,
env: Record<string, string>,
): Plugin {
const handler = async (req: IncomingMessage, res: ServerResponse) => {
const runtimeEnv = resolveRuntimeEnv(rootDir, mode, env);
const upstreamBaseUrl = normalizeUpstreamBaseUrl(
runtimeEnv.VITE_LLM_BASE_URL ||
runtimeEnv.LLM_BASE_URL ||
'https://ark.cn-beijing.volces.com/api/v3',
);
const apiKey =
runtimeEnv.LLM_API_KEY ||
runtimeEnv.ARK_API_KEY ||
runtimeEnv.VITE_LLM_API_KEY ||
'';
if (req.method !== 'POST') {
sendJson(res, 405, { error: { message: 'Method Not Allowed' } });
return;
@@ -783,21 +802,23 @@ function createStateFunctionOverridesPlugin(rootDir: string): Plugin {
function createCustomWorldSceneImagePlugin(
rootDir: string,
mode: string,
env: Record<string, string>,
): Plugin {
const baseUrl = normalizeDashScopeBaseUrl(
env.DASHSCOPE_BASE_URL || DEFAULT_DASHSCOPE_BASE_URL,
);
const apiKey = env.DASHSCOPE_API_KEY || '';
const defaultModel =
env.DASHSCOPE_IMAGE_MODEL || DEFAULT_DASHSCOPE_SCENE_IMAGE_MODEL;
const taskTimeoutMs = Number(
env.DASHSCOPE_IMAGE_REQUEST_TIMEOUT_MS ||
env.VITE_SCENE_IMAGE_REQUEST_TIMEOUT_MS ||
DASHSCOPE_TASK_TIMEOUT_MS,
);
const handler = async (req: IncomingMessage, res: ServerResponse) => {
const runtimeEnv = resolveRuntimeEnv(rootDir, mode, env);
const baseUrl = normalizeDashScopeBaseUrl(
runtimeEnv.DASHSCOPE_BASE_URL || DEFAULT_DASHSCOPE_BASE_URL,
);
const apiKey = runtimeEnv.DASHSCOPE_API_KEY || '';
const defaultModel =
runtimeEnv.DASHSCOPE_IMAGE_MODEL || DEFAULT_DASHSCOPE_SCENE_IMAGE_MODEL;
const taskTimeoutMs = Number(
runtimeEnv.DASHSCOPE_IMAGE_REQUEST_TIMEOUT_MS ||
runtimeEnv.VITE_SCENE_IMAGE_REQUEST_TIMEOUT_MS ||
DASHSCOPE_TASK_TIMEOUT_MS,
);
if (req.method !== 'POST') {
sendJson(res, 405, { error: { message: 'Method Not Allowed' } });
return;
@@ -1451,11 +1472,12 @@ function createCharacterAnimationPublishPlugin(rootDir: string): Plugin {
export function createLocalApiPlugins(
rootDir: string,
mode: string,
env: Record<string, string>,
): Plugin[] {
return [
createLlmProxyPlugin(env),
createCustomWorldSceneImagePlugin(rootDir, env),
createLlmProxyPlugin(rootDir, mode, env),
createCustomWorldSceneImagePlugin(rootDir, mode, env),
createItemCatalogPlugin(rootDir),
createItemOverridesPlugin(rootDir),
createNpcVisualOverridePlugin(rootDir),