mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 14:50:42 +00:00
test: lighten fast lane imports
This commit is contained in:
155
src/cli/run-main-policy.ts
Normal file
155
src/cli/run-main-policy.ts
Normal file
@@ -0,0 +1,155 @@
|
||||
import type { OpenClawConfig } from "../config/types.openclaw.js";
|
||||
import {
|
||||
resolveManifestCommandAliasOwnerInRegistry,
|
||||
type PluginManifestCommandAliasRecord,
|
||||
type PluginManifestCommandAliasRegistry,
|
||||
} from "../plugins/manifest-command-aliases.js";
|
||||
import {
|
||||
normalizeLowercaseStringOrEmpty,
|
||||
normalizeOptionalLowercaseString,
|
||||
} from "../shared/string-coerce.js";
|
||||
import { resolveCliArgvInvocation } from "./argv-invocation.js";
|
||||
import { resolveCliCommandPathPolicy } from "./command-path-policy.js";
|
||||
|
||||
export function rewriteUpdateFlagArgv(argv: string[]): string[] {
|
||||
const index = argv.indexOf("--update");
|
||||
if (index === -1) {
|
||||
return argv;
|
||||
}
|
||||
|
||||
const next = [...argv];
|
||||
next.splice(index, 1, "update");
|
||||
return next;
|
||||
}
|
||||
|
||||
export function shouldEnsureCliPath(argv: string[]): boolean {
|
||||
const invocation = resolveCliArgvInvocation(argv);
|
||||
if (invocation.hasHelpOrVersion || shouldStartCrestodianForBareRoot(argv)) {
|
||||
return false;
|
||||
}
|
||||
return resolveCliCommandPathPolicy(invocation.commandPath).ensureCliPath;
|
||||
}
|
||||
|
||||
export function shouldUseRootHelpFastPath(
|
||||
argv: string[],
|
||||
env: NodeJS.ProcessEnv = process.env,
|
||||
): boolean {
|
||||
const invocation = resolveCliArgvInvocation(argv);
|
||||
return (
|
||||
env.OPENCLAW_DISABLE_CLI_STARTUP_HELP_FAST_PATH !== "1" &&
|
||||
(invocation.isRootHelpInvocation ||
|
||||
(invocation.commandPath.length === 1 &&
|
||||
invocation.commandPath[0] === "help" &&
|
||||
invocation.hasHelpOrVersion))
|
||||
);
|
||||
}
|
||||
|
||||
export function shouldUseBrowserHelpFastPath(
|
||||
argv: string[],
|
||||
env: NodeJS.ProcessEnv = process.env,
|
||||
): boolean {
|
||||
if (env.OPENCLAW_DISABLE_CLI_STARTUP_HELP_FAST_PATH === "1") {
|
||||
return false;
|
||||
}
|
||||
const invocation = resolveCliArgvInvocation(argv);
|
||||
return (
|
||||
invocation.commandPath.length === 1 &&
|
||||
invocation.commandPath[0] === "browser" &&
|
||||
invocation.hasHelpOrVersion
|
||||
);
|
||||
}
|
||||
|
||||
export function shouldStartCrestodianForBareRoot(argv: string[]): boolean {
|
||||
const invocation = resolveCliArgvInvocation(argv);
|
||||
return invocation.commandPath.length === 0 && !invocation.hasHelpOrVersion;
|
||||
}
|
||||
|
||||
export function shouldStartCrestodianForModernOnboard(argv: string[]): boolean {
|
||||
const invocation = resolveCliArgvInvocation(argv);
|
||||
return (
|
||||
invocation.commandPath[0] === "onboard" &&
|
||||
argv.includes("--modern") &&
|
||||
!invocation.hasHelpOrVersion
|
||||
);
|
||||
}
|
||||
|
||||
export function resolveMissingPluginCommandMessage(
|
||||
pluginId: string,
|
||||
config?: OpenClawConfig,
|
||||
options?: {
|
||||
registry?: PluginManifestCommandAliasRegistry;
|
||||
resolveCommandAliasOwner?: (params: {
|
||||
command: string | undefined;
|
||||
config?: OpenClawConfig;
|
||||
registry?: PluginManifestCommandAliasRegistry;
|
||||
}) => PluginManifestCommandAliasRecord | undefined;
|
||||
},
|
||||
): string | null {
|
||||
const normalizedPluginId = normalizeLowercaseStringOrEmpty(pluginId);
|
||||
if (!normalizedPluginId) {
|
||||
return null;
|
||||
}
|
||||
const allow =
|
||||
Array.isArray(config?.plugins?.allow) && config.plugins.allow.length > 0
|
||||
? config.plugins.allow
|
||||
.filter((entry): entry is string => typeof entry === "string")
|
||||
.map((entry) => normalizeOptionalLowercaseString(entry))
|
||||
.filter(Boolean)
|
||||
: [];
|
||||
const commandAlias = options?.registry
|
||||
? resolveManifestCommandAliasOwnerInRegistry({
|
||||
command: normalizedPluginId,
|
||||
registry: options.registry,
|
||||
})
|
||||
: options?.resolveCommandAliasOwner?.({
|
||||
command: normalizedPluginId,
|
||||
config,
|
||||
...(options?.registry ? { registry: options.registry } : {}),
|
||||
});
|
||||
const parentPluginId = commandAlias?.pluginId;
|
||||
if (parentPluginId) {
|
||||
if (allow.length > 0 && !allow.includes(parentPluginId)) {
|
||||
return (
|
||||
`"${normalizedPluginId}" is not a plugin; it is a command provided by the ` +
|
||||
`"${parentPluginId}" plugin. Add "${parentPluginId}" to \`plugins.allow\` ` +
|
||||
`instead of "${normalizedPluginId}".`
|
||||
);
|
||||
}
|
||||
if (config?.plugins?.entries?.[parentPluginId]?.enabled === false) {
|
||||
return (
|
||||
`The \`openclaw ${normalizedPluginId}\` command is unavailable because ` +
|
||||
`\`plugins.entries.${parentPluginId}.enabled=false\`. Re-enable that entry if you want ` +
|
||||
"the bundled plugin command surface."
|
||||
);
|
||||
}
|
||||
if (commandAlias.kind === "runtime-slash") {
|
||||
const cliHint = commandAlias.cliCommand
|
||||
? `Use \`openclaw ${commandAlias.cliCommand}\` for related CLI operations, or `
|
||||
: "Use ";
|
||||
return (
|
||||
`"${normalizedPluginId}" is a runtime slash command (/${normalizedPluginId}), not a CLI command. ` +
|
||||
`It is provided by the "${parentPluginId}" plugin. ` +
|
||||
`${cliHint}\`/${normalizedPluginId}\` in a chat session.`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (allow.length > 0 && !allow.includes(normalizedPluginId)) {
|
||||
if (parentPluginId && allow.includes(parentPluginId)) {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
`The \`openclaw ${normalizedPluginId}\` command is unavailable because ` +
|
||||
`\`plugins.allow\` excludes "${normalizedPluginId}". Add "${normalizedPluginId}" to ` +
|
||||
`\`plugins.allow\` if you want that bundled plugin CLI surface.`
|
||||
);
|
||||
}
|
||||
if (config?.plugins?.entries?.[normalizedPluginId]?.enabled === false) {
|
||||
return (
|
||||
`The \`openclaw ${normalizedPluginId}\` command is unavailable because ` +
|
||||
`\`plugins.entries.${normalizedPluginId}.enabled=false\`. Re-enable that entry if you want ` +
|
||||
"the bundled plugin CLI surface."
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { PluginManifestRegistry } from "../plugins/manifest-registry.js";
|
||||
import type { PluginManifestCommandAliasRegistry } from "../plugins/manifest-command-aliases.js";
|
||||
import {
|
||||
rewriteUpdateFlagArgv,
|
||||
resolveMissingPluginCommandMessage,
|
||||
@@ -8,44 +8,24 @@ import {
|
||||
shouldStartCrestodianForModernOnboard,
|
||||
shouldUseBrowserHelpFastPath,
|
||||
shouldUseRootHelpFastPath,
|
||||
} from "./run-main.js";
|
||||
} from "./run-main-policy.js";
|
||||
|
||||
const memoryWikiCommandAliasRegistry: PluginManifestRegistry = {
|
||||
const memoryWikiCommandAliasRegistry: PluginManifestCommandAliasRegistry = {
|
||||
plugins: [
|
||||
{
|
||||
id: "memory-wiki",
|
||||
channels: [],
|
||||
providers: [],
|
||||
cliBackends: [],
|
||||
skills: [],
|
||||
hooks: [],
|
||||
origin: "bundled",
|
||||
rootDir: "/tmp/memory-wiki",
|
||||
source: "bundled",
|
||||
manifestPath: "/tmp/memory-wiki/openclaw.plugin.json",
|
||||
commandAliases: [{ name: "wiki" }],
|
||||
},
|
||||
],
|
||||
diagnostics: [],
|
||||
};
|
||||
|
||||
const memoryCoreCommandAliasRegistry: PluginManifestRegistry = {
|
||||
const memoryCoreCommandAliasRegistry: PluginManifestCommandAliasRegistry = {
|
||||
plugins: [
|
||||
{
|
||||
id: "memory-core",
|
||||
channels: [],
|
||||
providers: [],
|
||||
cliBackends: [],
|
||||
skills: [],
|
||||
hooks: [],
|
||||
origin: "bundled",
|
||||
rootDir: "/tmp/memory-core",
|
||||
source: "bundled",
|
||||
manifestPath: "/tmp/memory-core/openclaw.plugin.json",
|
||||
commandAliases: [{ name: "dreaming", kind: "runtime-slash", cliCommand: "memory" }],
|
||||
},
|
||||
],
|
||||
diagnostics: [],
|
||||
};
|
||||
|
||||
describe("rewriteUpdateFlagArgv", () => {
|
||||
|
||||
@@ -20,23 +20,36 @@ import {
|
||||
finalizeDebugProxyCapture,
|
||||
initializeDebugProxyCapture,
|
||||
} from "../proxy-capture/runtime.js";
|
||||
import {
|
||||
normalizeLowercaseStringOrEmpty,
|
||||
normalizeOptionalLowercaseString,
|
||||
normalizeOptionalString,
|
||||
} from "../shared/string-coerce.js";
|
||||
import { normalizeOptionalString } from "../shared/string-coerce.js";
|
||||
import { resolveCliArgvInvocation } from "./argv-invocation.js";
|
||||
import {
|
||||
shouldRegisterPrimaryCommandOnly,
|
||||
shouldSkipPluginCommandRegistration,
|
||||
} from "./command-registration-policy.js";
|
||||
import { shouldEnsureCliPathForCommandPath } from "./command-startup-policy.js";
|
||||
import { maybeRunCliInContainer, parseCliContainerArgs } from "./container-target.js";
|
||||
import { applyCliProfileEnv, parseCliProfileArgs } from "./profile.js";
|
||||
import { createCliProgress } from "./progress.js";
|
||||
import { tryRouteCli } from "./route.js";
|
||||
import {
|
||||
resolveMissingPluginCommandMessage as resolveMissingPluginCommandMessageFromPolicy,
|
||||
rewriteUpdateFlagArgv,
|
||||
shouldEnsureCliPath,
|
||||
shouldStartCrestodianForBareRoot,
|
||||
shouldStartCrestodianForModernOnboard,
|
||||
shouldUseBrowserHelpFastPath,
|
||||
shouldUseRootHelpFastPath,
|
||||
} from "./run-main-policy.js";
|
||||
import { normalizeWindowsArgv } from "./windows-argv.js";
|
||||
|
||||
export {
|
||||
rewriteUpdateFlagArgv,
|
||||
shouldEnsureCliPath,
|
||||
shouldStartCrestodianForBareRoot,
|
||||
shouldStartCrestodianForModernOnboard,
|
||||
shouldUseBrowserHelpFastPath,
|
||||
shouldUseRootHelpFastPath,
|
||||
} from "./run-main-policy.js";
|
||||
|
||||
async function closeCliMemoryManagers(): Promise<void> {
|
||||
if (!hasMemoryRuntime()) {
|
||||
return;
|
||||
@@ -49,129 +62,15 @@ async function closeCliMemoryManagers(): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
export function rewriteUpdateFlagArgv(argv: string[]): string[] {
|
||||
const index = argv.indexOf("--update");
|
||||
if (index === -1) {
|
||||
return argv;
|
||||
}
|
||||
|
||||
const next = [...argv];
|
||||
next.splice(index, 1, "update");
|
||||
return next;
|
||||
}
|
||||
|
||||
export function shouldEnsureCliPath(argv: string[]): boolean {
|
||||
const invocation = resolveCliArgvInvocation(argv);
|
||||
if (invocation.hasHelpOrVersion || shouldStartCrestodianForBareRoot(argv)) {
|
||||
return false;
|
||||
}
|
||||
return shouldEnsureCliPathForCommandPath(invocation.commandPath);
|
||||
}
|
||||
|
||||
export function shouldUseRootHelpFastPath(argv: string[]): boolean {
|
||||
const invocation = resolveCliArgvInvocation(argv);
|
||||
return (
|
||||
process.env.OPENCLAW_DISABLE_CLI_STARTUP_HELP_FAST_PATH !== "1" &&
|
||||
(invocation.isRootHelpInvocation ||
|
||||
(invocation.commandPath.length === 1 &&
|
||||
invocation.commandPath[0] === "help" &&
|
||||
invocation.hasHelpOrVersion))
|
||||
);
|
||||
}
|
||||
|
||||
export function shouldUseBrowserHelpFastPath(argv: string[]): boolean {
|
||||
if (process.env.OPENCLAW_DISABLE_CLI_STARTUP_HELP_FAST_PATH === "1") {
|
||||
return false;
|
||||
}
|
||||
const invocation = resolveCliArgvInvocation(argv);
|
||||
return (
|
||||
invocation.commandPath.length === 1 &&
|
||||
invocation.commandPath[0] === "browser" &&
|
||||
invocation.hasHelpOrVersion
|
||||
);
|
||||
}
|
||||
|
||||
export function shouldStartCrestodianForBareRoot(argv: string[]): boolean {
|
||||
const invocation = resolveCliArgvInvocation(argv);
|
||||
return invocation.commandPath.length === 0 && !invocation.hasHelpOrVersion;
|
||||
}
|
||||
|
||||
export function shouldStartCrestodianForModernOnboard(argv: string[]): boolean {
|
||||
const invocation = resolveCliArgvInvocation(argv);
|
||||
return (
|
||||
invocation.commandPath[0] === "onboard" &&
|
||||
argv.includes("--modern") &&
|
||||
!invocation.hasHelpOrVersion
|
||||
);
|
||||
}
|
||||
|
||||
export function resolveMissingPluginCommandMessage(
|
||||
pluginId: string,
|
||||
config?: OpenClawConfig,
|
||||
options?: { registry?: PluginManifestCommandAliasRegistry },
|
||||
): string | null {
|
||||
const normalizedPluginId = normalizeLowercaseStringOrEmpty(pluginId);
|
||||
if (!normalizedPluginId) {
|
||||
return null;
|
||||
}
|
||||
const allow =
|
||||
Array.isArray(config?.plugins?.allow) && config.plugins.allow.length > 0
|
||||
? config.plugins.allow
|
||||
.filter((entry): entry is string => typeof entry === "string")
|
||||
.map((entry) => normalizeOptionalLowercaseString(entry))
|
||||
.filter(Boolean)
|
||||
: [];
|
||||
const commandAlias = resolveManifestCommandAliasOwner({
|
||||
command: normalizedPluginId,
|
||||
config,
|
||||
registry: options?.registry,
|
||||
return resolveMissingPluginCommandMessageFromPolicy(pluginId, config, {
|
||||
...(options?.registry ? { registry: options.registry } : {}),
|
||||
resolveCommandAliasOwner: resolveManifestCommandAliasOwner,
|
||||
});
|
||||
const parentPluginId = commandAlias?.pluginId;
|
||||
if (parentPluginId) {
|
||||
if (allow.length > 0 && !allow.includes(parentPluginId)) {
|
||||
return (
|
||||
`"${normalizedPluginId}" is not a plugin; it is a command provided by the ` +
|
||||
`"${parentPluginId}" plugin. Add "${parentPluginId}" to \`plugins.allow\` ` +
|
||||
`instead of "${normalizedPluginId}".`
|
||||
);
|
||||
}
|
||||
if (config?.plugins?.entries?.[parentPluginId]?.enabled === false) {
|
||||
return (
|
||||
`The \`openclaw ${normalizedPluginId}\` command is unavailable because ` +
|
||||
`\`plugins.entries.${parentPluginId}.enabled=false\`. Re-enable that entry if you want ` +
|
||||
"the bundled plugin command surface."
|
||||
);
|
||||
}
|
||||
if (commandAlias.kind === "runtime-slash") {
|
||||
const cliHint = commandAlias.cliCommand
|
||||
? `Use \`openclaw ${commandAlias.cliCommand}\` for related CLI operations, or `
|
||||
: "Use ";
|
||||
return (
|
||||
`"${normalizedPluginId}" is a runtime slash command (/${normalizedPluginId}), not a CLI command. ` +
|
||||
`It is provided by the "${parentPluginId}" plugin. ` +
|
||||
`${cliHint}\`/${normalizedPluginId}\` in a chat session.`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (allow.length > 0 && !allow.includes(normalizedPluginId)) {
|
||||
if (parentPluginId && allow.includes(parentPluginId)) {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
`The \`openclaw ${normalizedPluginId}\` command is unavailable because ` +
|
||||
`\`plugins.allow\` excludes "${normalizedPluginId}". Add "${normalizedPluginId}" to ` +
|
||||
`\`plugins.allow\` if you want that bundled plugin CLI surface.`
|
||||
);
|
||||
}
|
||||
if (config?.plugins?.entries?.[normalizedPluginId]?.enabled === false) {
|
||||
return (
|
||||
`The \`openclaw ${normalizedPluginId}\` command is unavailable because ` +
|
||||
`\`plugins.entries.${normalizedPluginId}.enabled=false\`. Re-enable that entry if you want ` +
|
||||
"the bundled plugin CLI surface."
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function shouldLoadCliDotEnv(env: NodeJS.ProcessEnv = process.env): boolean {
|
||||
|
||||
Reference in New Issue
Block a user