mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-28 18:33:37 +00:00
refactor: share plugin setup helpers
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { consumeRootOptionToken, FLAG_TERMINATOR } from "../infra/cli-root-options.js";
|
||||
import { getPrimaryCommand } from "./argv.js";
|
||||
import { forwardConsumedCliRootOption } from "./root-option-forward.js";
|
||||
import { takeCliRootOptionValue } from "./root-option-value.js";
|
||||
|
||||
type CliContainerParseResult =
|
||||
@@ -56,14 +57,8 @@ export function parseCliContainerArgs(argv: string[]): CliContainerParseResult {
|
||||
continue;
|
||||
}
|
||||
|
||||
const consumedRootOption = consumeRootOptionToken(args, i);
|
||||
const consumedRootOption = forwardConsumedCliRootOption(args, i, out);
|
||||
if (consumedRootOption > 0) {
|
||||
for (let offset = 0; offset < consumedRootOption; offset += 1) {
|
||||
const token = args[i + offset];
|
||||
if (token !== undefined) {
|
||||
out.push(token);
|
||||
}
|
||||
}
|
||||
i += consumedRootOption - 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { consumeRootOptionToken, FLAG_TERMINATOR } from "../infra/cli-root-options.js";
|
||||
import { FLAG_TERMINATOR } from "../infra/cli-root-options.js";
|
||||
import { resolveRequiredHomeDir } from "../infra/home-dir.js";
|
||||
import { getPrimaryCommand } from "./argv.js";
|
||||
import { isValidProfileName } from "./profile-utils.js";
|
||||
import { forwardConsumedCliRootOption } from "./root-option-forward.js";
|
||||
import { takeCliRootOptionValue } from "./root-option-value.js";
|
||||
|
||||
export type CliProfileParseResult =
|
||||
@@ -65,14 +66,8 @@ export function parseCliProfileArgs(argv: string[]): CliProfileParseResult {
|
||||
continue;
|
||||
}
|
||||
|
||||
const consumedRootOption = consumeRootOptionToken(args, i);
|
||||
const consumedRootOption = forwardConsumedCliRootOption(args, i, out);
|
||||
if (consumedRootOption > 0) {
|
||||
for (let offset = 0; offset < consumedRootOption; offset += 1) {
|
||||
const token = args[i + offset];
|
||||
if (token !== undefined) {
|
||||
out.push(token);
|
||||
}
|
||||
}
|
||||
i += consumedRootOption - 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
21
src/cli/root-option-forward.ts
Normal file
21
src/cli/root-option-forward.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { consumeRootOptionToken } from "../infra/cli-root-options.js";
|
||||
|
||||
export function forwardConsumedCliRootOption(
|
||||
args: readonly string[],
|
||||
index: number,
|
||||
out: string[],
|
||||
): number {
|
||||
const consumedRootOption = consumeRootOptionToken(args, index);
|
||||
if (consumedRootOption <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (let offset = 0; offset < consumedRootOption; offset += 1) {
|
||||
const token = args[index + offset];
|
||||
if (token !== undefined) {
|
||||
out.push(token);
|
||||
}
|
||||
}
|
||||
|
||||
return consumedRootOption;
|
||||
}
|
||||
@@ -1,22 +1 @@
|
||||
import { isWSLEnv } from "../infra/wsl.js";
|
||||
|
||||
export function isRemoteEnvironment(): boolean {
|
||||
if (process.env.SSH_CLIENT || process.env.SSH_TTY || process.env.SSH_CONNECTION) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (process.env.REMOTE_CONTAINERS || process.env.CODESPACES) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (
|
||||
process.platform === "linux" &&
|
||||
!process.env.DISPLAY &&
|
||||
!process.env.WAYLAND_DISPLAY &&
|
||||
!isWSLEnv()
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
export { isRemoteEnvironment } from "../infra/remote-env.js";
|
||||
|
||||
22
src/infra/remote-env.ts
Normal file
22
src/infra/remote-env.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { isWSLEnv } from "./wsl.js";
|
||||
|
||||
export function isRemoteEnvironment(): boolean {
|
||||
if (process.env.SSH_CLIENT || process.env.SSH_TTY || process.env.SSH_CONNECTION) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (process.env.REMOTE_CONTAINERS || process.env.CODESPACES) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (
|
||||
process.platform === "linux" &&
|
||||
!process.env.DISPLAY &&
|
||||
!process.env.WAYLAND_DISPLAY &&
|
||||
!isWSLEnv()
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
import type { messagingApi } from "@line/bot-sdk";
|
||||
|
||||
type QuickReply = messagingApi.QuickReply;
|
||||
type QuickReplyItem = messagingApi.QuickReplyItem;
|
||||
type TextMessage = messagingApi.TextMessage;
|
||||
|
||||
export function createQuickReplyItems(labels: string[]): QuickReply {
|
||||
const items: QuickReplyItem[] = labels.slice(0, 13).map((label) => ({
|
||||
type: "action",
|
||||
action: {
|
||||
type: "message",
|
||||
label: label.slice(0, 20),
|
||||
text: label,
|
||||
},
|
||||
}));
|
||||
return { items };
|
||||
}
|
||||
|
||||
export function createTextMessageWithQuickReplies(
|
||||
text: string,
|
||||
quickReplyLabels: string[],
|
||||
): TextMessage & { quickReply: QuickReply } {
|
||||
return {
|
||||
type: "text",
|
||||
text,
|
||||
quickReply: createQuickReplyItems(quickReplyLabels),
|
||||
};
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { PluginEntryConfig } from "../config/types.plugins.js";
|
||||
import { hasExplicitPluginConfig } from "./config-state.js";
|
||||
import type { PluginLoadOptions } from "./loader.js";
|
||||
|
||||
export function withBundledPluginAllowlistCompat(params: {
|
||||
@@ -63,3 +64,32 @@ export function withBundledPluginEnablementCompat(params: {
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function withBundledPluginVitestCompat(params: {
|
||||
config: PluginLoadOptions["config"];
|
||||
pluginIds: readonly string[];
|
||||
env?: PluginLoadOptions["env"];
|
||||
}): PluginLoadOptions["config"] {
|
||||
const env = params.env ?? process.env;
|
||||
const isVitest = Boolean(env.VITEST || process.env.VITEST);
|
||||
if (
|
||||
!isVitest ||
|
||||
hasExplicitPluginConfig(params.config?.plugins) ||
|
||||
params.pluginIds.length === 0
|
||||
) {
|
||||
return params.config;
|
||||
}
|
||||
|
||||
return {
|
||||
...params.config,
|
||||
plugins: {
|
||||
...params.config?.plugins,
|
||||
enabled: true,
|
||||
allow: [...params.pluginIds],
|
||||
slots: {
|
||||
...params.config?.plugins?.slots,
|
||||
memory: "none",
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { normalizeProviderId } from "../agents/provider-id.js";
|
||||
import { hasExplicitPluginConfig } from "./config-state.js";
|
||||
import { withBundledPluginVitestCompat } from "./bundled-compat.js";
|
||||
import { normalizePluginsConfig, resolveEffectiveEnableState } from "./config-state.js";
|
||||
import type { PluginLoadOptions } from "./loader.js";
|
||||
import { loadPluginManifestRegistry } from "./manifest-registry.js";
|
||||
@@ -9,27 +9,7 @@ export function withBundledProviderVitestCompat(params: {
|
||||
pluginIds: readonly string[];
|
||||
env?: PluginLoadOptions["env"];
|
||||
}): PluginLoadOptions["config"] {
|
||||
const env = params.env ?? process.env;
|
||||
if (
|
||||
!env.VITEST ||
|
||||
hasExplicitPluginConfig(params.config?.plugins) ||
|
||||
params.pluginIds.length === 0
|
||||
) {
|
||||
return params.config;
|
||||
}
|
||||
|
||||
return {
|
||||
...params.config,
|
||||
plugins: {
|
||||
...params.config?.plugins,
|
||||
enabled: true,
|
||||
allow: [...params.pluginIds],
|
||||
slots: {
|
||||
...params.config?.plugins?.slots,
|
||||
memory: "none",
|
||||
},
|
||||
},
|
||||
};
|
||||
return withBundledPluginVitestCompat(params);
|
||||
}
|
||||
|
||||
export function resolveBundledProviderCompatPluginIds(params: {
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import { isWSL, isWSLEnv } from "../infra/wsl.js";
|
||||
import { isRemoteEnvironment } from "../infra/remote-env.js";
|
||||
import { isWSL } from "../infra/wsl.js";
|
||||
import { runCommandWithTimeout } from "../process/exec.js";
|
||||
import { detectBinary } from "./setup-binary.js";
|
||||
|
||||
export { isRemoteEnvironment } from "../infra/remote-env.js";
|
||||
|
||||
function shouldSkipBrowserOpenInTests(): boolean {
|
||||
if (process.env.VITEST) {
|
||||
return true;
|
||||
@@ -61,27 +64,6 @@ async function resolveBrowserOpenCommand(): Promise<BrowserOpenCommand> {
|
||||
return { argv: null };
|
||||
}
|
||||
|
||||
export function isRemoteEnvironment(): boolean {
|
||||
if (process.env.SSH_CLIENT || process.env.SSH_TTY || process.env.SSH_CONNECTION) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (process.env.REMOTE_CONTAINERS || process.env.CODESPACES) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (
|
||||
process.platform === "linux" &&
|
||||
!process.env.DISPLAY &&
|
||||
!process.env.WAYLAND_DISPLAY &&
|
||||
!isWSLEnv()
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export async function openUrl(url: string): Promise<boolean> {
|
||||
if (shouldSkipBrowserOpenInTests()) {
|
||||
return false;
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
import {
|
||||
withBundledPluginAllowlistCompat,
|
||||
withBundledPluginEnablementCompat,
|
||||
withBundledPluginVitestCompat,
|
||||
} from "./bundled-compat.js";
|
||||
import { resolveBundledWebSearchPluginIds } from "./bundled-web-search.js";
|
||||
import {
|
||||
hasExplicitPluginConfig,
|
||||
normalizePluginsConfig,
|
||||
type NormalizedPluginsConfig,
|
||||
} from "./config-state.js";
|
||||
import { normalizePluginsConfig, type NormalizedPluginsConfig } from "./config-state.js";
|
||||
import type { PluginLoadOptions } from "./loader.js";
|
||||
import type { PluginWebSearchProviderEntry } from "./types.js";
|
||||
|
||||
@@ -23,35 +20,6 @@ function resolveBundledWebSearchCompatPluginIds(params: {
|
||||
});
|
||||
}
|
||||
|
||||
function withBundledWebSearchVitestCompat(params: {
|
||||
config: PluginLoadOptions["config"];
|
||||
pluginIds: readonly string[];
|
||||
env?: PluginLoadOptions["env"];
|
||||
}): PluginLoadOptions["config"] {
|
||||
const env = params.env ?? process.env;
|
||||
const isVitest = Boolean(env.VITEST || process.env.VITEST);
|
||||
if (
|
||||
!isVitest ||
|
||||
hasExplicitPluginConfig(params.config?.plugins) ||
|
||||
params.pluginIds.length === 0
|
||||
) {
|
||||
return params.config;
|
||||
}
|
||||
|
||||
return {
|
||||
...params.config,
|
||||
plugins: {
|
||||
...params.config?.plugins,
|
||||
enabled: true,
|
||||
allow: [...params.pluginIds],
|
||||
slots: {
|
||||
...params.config?.plugins?.slots,
|
||||
memory: "none",
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function compareWebSearchProvidersAlphabetically(
|
||||
left: Pick<PluginWebSearchProviderEntry, "id" | "pluginId">,
|
||||
right: Pick<PluginWebSearchProviderEntry, "id" | "pluginId">,
|
||||
@@ -102,7 +70,7 @@ export function resolveBundledWebSearchResolutionConfig(params: {
|
||||
config: allowlistCompat,
|
||||
pluginIds: bundledCompatPluginIds,
|
||||
});
|
||||
const config = withBundledWebSearchVitestCompat({
|
||||
const config = withBundledPluginVitestCompat({
|
||||
config: enablementCompat,
|
||||
pluginIds: bundledCompatPluginIds,
|
||||
env: params.env,
|
||||
|
||||
Reference in New Issue
Block a user