UNPKG

weapp-vite

Version:

weapp-vite 一个现代化的小程序打包工具

129 lines (128 loc) 4.55 kB
import { r as logger_default } from "./logger-mt4mSTqV.mjs"; import process from "node:process"; import { connectMiniProgram } from "weapp-ide-cli"; import { determineAgent } from "@vercel/detect-agent"; import { DEFAULT_MCP_ENDPOINT, DEFAULT_MCP_HOST, DEFAULT_MCP_PORT, DEFAULT_RUNTIME_REST_ENDPOINT, createWeappViteMcpServer, startWeappViteMcpServer } from "@weapp-vite/mcp"; //#region src/aiEnvironment.ts const AI_ENV_KEYS = [ "CODEX_HOME", "CODEX_SANDBOX", "CODEX_USER_AGENT", "CLAUDECODE", "CLAUDE_CODE", "CURSOR_AGENT", "GITHUB_COPILOT_AGENT", "OPENAI_AGENT" ]; function isTruthyEnvValue(value) { if (value === void 0) return false; const normalized = value.trim().toLowerCase(); return normalized !== "" && normalized !== "0" && normalized !== "false" && normalized !== "no"; } function isNonEmptyEnvValue(value) { return isTruthyEnvValue(value); } /** * @description 判断当前命令是否由 AI 开发代理触发。 */ function isAiDevelopmentEnvironment(env = process.env) { if (isTruthyEnvValue(env.WEAPP_VITE_AI)) return true; return AI_ENV_KEYS.some((key) => isTruthyEnvValue(env[key])); } function resolveAiDevelopmentEnvironmentFromEnv(env = process.env) { const explicitAgentName = env.WEAPP_VITE_AI; if (isNonEmptyEnvValue(explicitAgentName)) return { agentName: explicitAgentName.trim(), isAgent: true }; if (isAiDevelopmentEnvironment(env)) return { isAgent: true }; return { isAgent: false }; } /** * @description 使用标准 agent 检测库识别 AI 终端,并保留 weapp-vite 显式环境变量兜底。 */ async function detectAiDevelopmentEnvironment(env = process.env) { const envResult = resolveAiDevelopmentEnvironmentFromEnv(env); if (envResult.isAgent) return envResult; try { const result = await determineAgent(); return { agentName: result.isAgent ? result.agent.name : void 0, isAgent: result.isAgent }; } catch { return envResult; } } function resolveBooleanLikeEnv(value) { if (value === void 0) return; const normalized = value.trim().toLowerCase(); if (!normalized) return; if (normalized === "ai") return "ai"; if ([ "1", "true", "yes", "on" ].includes(normalized)) return true; if ([ "0", "false", "no", "off" ].includes(normalized)) return false; } //#endregion //#region src/mcp.ts function normalizeEndpoint(input) { const value = typeof input === "string" ? input.trim() : ""; if (!value) return DEFAULT_MCP_ENDPOINT; return value.startsWith("/") ? value : `/${value}`; } function resolveProjectMcpPort(projectRoot = process.cwd()) { let hash = 0; for (const char of projectRoot) hash = hash * 31 + char.charCodeAt(0) >>> 0; return DEFAULT_MCP_PORT + hash % 2e4; } function normalizePort(input, cwd) { if (input === void 0 || input === "auto") return resolveProjectMcpPort(cwd); if (typeof input === "number" && Number.isInteger(input) && input > 0 && input <= 65535) return input; return DEFAULT_MCP_PORT; } function resolveAutoStart(input, options) { const env = options.env ?? process.env; const value = resolveBooleanLikeEnv(env.WEAPP_VITE_MCP) ?? input ?? "ai"; if (value === "ai") return options.isAgent ?? resolveAiDevelopmentEnvironmentFromEnv(env).isAgent; return value === true; } function resolveWeappMcpConfig(config, options = {}) { if (config === false) return { enabled: false, autoStart: false, host: DEFAULT_MCP_HOST, port: DEFAULT_MCP_PORT, endpoint: DEFAULT_MCP_ENDPOINT, restEndpoint: DEFAULT_RUNTIME_REST_ENDPOINT }; const record = typeof config === "object" && config ? config : {}; return { agentName: options.agentName, enabled: record.enabled !== false, autoStart: resolveAutoStart(record.autoStart, options), host: typeof record.host === "string" && record.host.trim().length > 0 ? record.host.trim() : DEFAULT_MCP_HOST, port: normalizePort(record.port, options.cwd), endpoint: normalizeEndpoint(record.endpoint), restEndpoint: record.restEndpoint === false ? false : normalizeEndpoint(record.restEndpoint ?? DEFAULT_RUNTIME_REST_ENDPOINT) }; } async function startWeappViteMcpServer$1(options) { return startWeappViteMcpServer({ runtimeHooks: { connectMiniProgram }, ...options, onReady: options?.onReady ?? ((message) => { logger_default.info(message); }) }); } //#endregion export { createWeappViteMcpServer as a, startWeappViteMcpServer$1 as c, DEFAULT_RUNTIME_REST_ENDPOINT as i, detectAiDevelopmentEnvironment as l, DEFAULT_MCP_HOST as n, resolveProjectMcpPort as o, DEFAULT_MCP_PORT as r, resolveWeappMcpConfig as s, DEFAULT_MCP_ENDPOINT as t };