|
@@ -1,5 +1,7 @@
|
|
|
-import { mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
|
|
|
|
|
+import { mkdir } from "node:fs/promises";
|
|
|
import { join, resolve } from "node:path";
|
|
import { join, resolve } from "node:path";
|
|
|
|
|
+import { z } from "zod";
|
|
|
|
|
+import { zcodePluginUserConfigOptionSchema } from "@zcode/shared";
|
|
|
import type {
|
|
import type {
|
|
|
CustomCommandRoot,
|
|
CustomCommandRoot,
|
|
|
HookConfig,
|
|
HookConfig,
|
|
@@ -14,7 +16,6 @@ import type {
|
|
|
PluginManifest,
|
|
PluginManifest,
|
|
|
PluginUiSurfaceDefinition,
|
|
PluginUiSurfaceDefinition,
|
|
|
PluginMetadata,
|
|
PluginMetadata,
|
|
|
- PluginOperationOptions,
|
|
|
|
|
PluginPort,
|
|
PluginPort,
|
|
|
SkillRoot,
|
|
SkillRoot,
|
|
|
} from "@zcode/contracts";
|
|
} from "@zcode/contracts";
|
|
@@ -25,27 +26,35 @@ import {
|
|
|
ZCODE_OFFICIAL_PLUGIN_MARKETPLACE,
|
|
ZCODE_OFFICIAL_PLUGIN_MARKETPLACE,
|
|
|
} from "@zcode/contracts";
|
|
} from "@zcode/contracts";
|
|
|
import {
|
|
import {
|
|
|
- directoryExists,
|
|
|
|
|
- fileExists,
|
|
|
|
|
- isMissingPath,
|
|
|
|
|
|
|
+ directoryExistsAsync,
|
|
|
isNotFoundError,
|
|
isNotFoundError,
|
|
|
isRecord,
|
|
isRecord,
|
|
|
parsePathList,
|
|
parsePathList,
|
|
|
resolveInside,
|
|
resolveInside,
|
|
|
sanitizePluginId,
|
|
sanitizePluginId,
|
|
|
throwIfAborted,
|
|
throwIfAborted,
|
|
|
|
|
+ PluginMaterialCache,
|
|
|
|
|
+ type PluginMaterialReader,
|
|
|
|
|
+ pluginSourcesChanged,
|
|
|
|
|
+ isPluginMaterialChange,
|
|
|
|
|
+ rethrowPluginMaterialInterrupt,
|
|
|
|
|
+ waitForPluginQuery,
|
|
|
} from "./helpers.js";
|
|
} from "./helpers.js";
|
|
|
-import { scanSkillFilesUnderRootSync } from "../skills/scan.js";
|
|
|
|
|
import { loadPluginMcpServerDefinitions, resolvePluginMcpServers } from "./mcp.js";
|
|
import { loadPluginMcpServerDefinitions, resolvePluginMcpServers } from "./mcp.js";
|
|
|
import { parsePluginUiSurfaces } from "./ui-surfaces.js";
|
|
import { parsePluginUiSurfaces } from "./ui-surfaces.js";
|
|
|
-import { listPluginHookSources } from "./hook-sources.js";
|
|
|
|
|
-import { enumeratePluginComponents } from "./plugin-components.js";
|
|
|
|
|
|
|
+import { createHookPluginContext, listPluginHookSources } from "./hook-sources.js";
|
|
|
import {
|
|
import {
|
|
|
- listInstalledPluginRecords,
|
|
|
|
|
|
|
+ countSkillFiles,
|
|
|
|
|
+ materializeCommandMetadataRoot,
|
|
|
|
|
+ warnEmptyDeclaredSkillRoots,
|
|
|
|
|
+ enumeratePluginComponents,
|
|
|
|
|
+} from "./plugin-components.js";
|
|
|
|
|
+import {
|
|
|
|
|
+ listInstalledPluginRecordsAsync,
|
|
|
normalizeAuthorValue,
|
|
normalizeAuthorValue,
|
|
|
- resolveInstalledPluginRoot,
|
|
|
|
|
|
|
+ resolveInstalledPluginRootAsync,
|
|
|
} from "./marketplace.js";
|
|
} from "./marketplace.js";
|
|
|
-import { loadBundledOfficialPluginRootsSync } from "./official-marketplace.js";
|
|
|
|
|
|
|
+import { loadBundledOfficialPluginRoots } from "./official-marketplace.js";
|
|
|
import type {
|
|
import type {
|
|
|
LoadedPlugin,
|
|
LoadedPlugin,
|
|
|
PluginAbortOptions,
|
|
PluginAbortOptions,
|
|
@@ -66,6 +75,7 @@ export {
|
|
|
loadKnownMarketplacesSync,
|
|
loadKnownMarketplacesSync,
|
|
|
loadMarketplaceManifestSync,
|
|
loadMarketplaceManifestSync,
|
|
|
normalizeAuthorValue,
|
|
normalizeAuthorValue,
|
|
|
|
|
+ parseEntryExtensionSummary,
|
|
|
parseEntryStoreListing,
|
|
parseEntryStoreListing,
|
|
|
parsePluginIconSources,
|
|
parsePluginIconSources,
|
|
|
parseMarketplaceSourceInput,
|
|
parseMarketplaceSourceInput,
|
|
@@ -84,14 +94,15 @@ export {
|
|
|
type PluginComponentGroup,
|
|
type PluginComponentGroup,
|
|
|
type PluginComponentItem,
|
|
type PluginComponentItem,
|
|
|
type PluginComponentKind,
|
|
type PluginComponentKind,
|
|
|
|
|
+ type PluginExtensionSummary,
|
|
|
type PluginManifestDisplayMetadata,
|
|
type PluginManifestDisplayMetadata,
|
|
|
type PluginMarketplaceEntry,
|
|
type PluginMarketplaceEntry,
|
|
|
type PluginMarketplaceManifest,
|
|
type PluginMarketplaceManifest,
|
|
|
} from "./marketplace.js";
|
|
} from "./marketplace.js";
|
|
|
|
|
|
|
|
export {
|
|
export {
|
|
|
- writeBundledOfficialMarketplacePartitionSync,
|
|
|
|
|
- writeCdnOfficialMarketplacePartitionSync,
|
|
|
|
|
|
|
+ writeBundledOfficialMarketplacePartition,
|
|
|
|
|
+ writeCdnOfficialMarketplacePartition,
|
|
|
} from "./official-marketplace.js";
|
|
} from "./official-marketplace.js";
|
|
|
|
|
|
|
|
export { getPluginSourceDiagnosticCode } from "./source-errors.js";
|
|
export { getPluginSourceDiagnosticCode } from "./source-errors.js";
|
|
@@ -106,8 +117,11 @@ const ZCODE_MANIFEST_PATH = join(".zcode-plugin", "plugin.json");
|
|
|
const CLAUDE_MANIFEST_PATH = join(".claude-plugin", "plugin.json");
|
|
const CLAUDE_MANIFEST_PATH = join(".claude-plugin", "plugin.json");
|
|
|
const CODEX_MANIFEST_PATH = join(".codex-plugin", "plugin.json");
|
|
const CODEX_MANIFEST_PATH = join(".codex-plugin", "plugin.json");
|
|
|
const DEFAULT_VERSION = "0.0.0";
|
|
const DEFAULT_VERSION = "0.0.0";
|
|
|
|
|
+// 外部清单的扩展字段不能穿透到 strict 协议;复用已知字段校验,尤其不能丢掉 sensitive。
|
|
|
|
|
+const pluginUserConfigSchema = z.record(z.string(), zcodePluginUserConfigOptionSchema.strip());
|
|
|
const FIRST_PLUGIN_PRIORITY = 1_000;
|
|
const FIRST_PLUGIN_PRIORITY = 1_000;
|
|
|
const PRIORITY_STEP = 10;
|
|
const PRIORITY_STEP = 10;
|
|
|
|
|
+const PLUGIN_DISCOVERY_REBUILD_LIMIT = 3;
|
|
|
const PLUGIN_NAME_PATTERN = /^[a-z0-9][a-z0-9._-]{0,127}$/;
|
|
const PLUGIN_NAME_PATTERN = /^[a-z0-9][a-z0-9._-]{0,127}$/;
|
|
|
const UNSUPPORTED_COMPONENT_KEYS = ["channels", "lspServers", "outputStyles", "settings"] as const;
|
|
const UNSUPPORTED_COMPONENT_KEYS = ["channels", "lspServers", "outputStyles", "settings"] as const;
|
|
|
const SUPPORTED_HOOK_EVENTS = new Set<string>(Object.values(HookEventNameValue));
|
|
const SUPPORTED_HOOK_EVENTS = new Set<string>(Object.values(HookEventNameValue));
|
|
@@ -122,24 +136,61 @@ export interface NodePluginAdapterOptions {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export class NodePluginAdapter implements PluginPort {
|
|
export class NodePluginAdapter implements PluginPort {
|
|
|
- constructor(private readonly options: NodePluginAdapterOptions) {}
|
|
|
|
|
|
|
+ private readonly materials = new PluginMaterialCache();
|
|
|
|
|
+ invalidate(): void {
|
|
|
|
|
+ this.materials.invalidate();
|
|
|
|
|
+ }
|
|
|
|
|
+ dispose(): void {
|
|
|
|
|
+ this.materials.dispose();
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
async discoverPlugins(
|
|
async discoverPlugins(
|
|
|
request: PluginDiscoverRequest,
|
|
request: PluginDiscoverRequest,
|
|
|
- options?: PluginOperationOptions,
|
|
|
|
|
|
|
+ options?: PluginAbortOptions,
|
|
|
): Promise<PluginLoadOutcome> {
|
|
): Promise<PluginLoadOutcome> {
|
|
|
- return this.discoverPluginsSync(request, options);
|
|
|
|
|
|
|
+ const signal = options?.signal
|
|
|
|
|
+ ? AbortSignal.any([options.signal, this.materials.signal])
|
|
|
|
|
+ : this.materials.signal;
|
|
|
|
|
+ return waitForPluginQuery(this.discoverCurrent(request, options), signal);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- discoverPluginsSync(
|
|
|
|
|
|
|
+ private async discoverCurrent(
|
|
|
request: PluginDiscoverRequest,
|
|
request: PluginDiscoverRequest,
|
|
|
options?: PluginAbortOptions,
|
|
options?: PluginAbortOptions,
|
|
|
- ): PluginLoadOutcome {
|
|
|
|
|
|
|
+ ): Promise<PluginLoadOutcome> {
|
|
|
|
|
+ for (let attempt = 0; attempt < PLUGIN_DISCOVERY_REBUILD_LIMIT; attempt += 1) {
|
|
|
|
|
+ this.materials.signal.throwIfAborted();
|
|
|
|
|
+ throwIfAborted(options);
|
|
|
|
|
+ const scope = JSON.stringify([
|
|
|
|
|
+ resolve(request.workingDirectory),
|
|
|
|
|
+ request.workspaceIdentity?.trim() || resolve(request.workingDirectory),
|
|
|
|
|
+ request.remoteSessionId?.trim() ?? "",
|
|
|
|
|
+ resolve(request.storageRoot || this.options.storageRoot),
|
|
|
|
|
+ ]);
|
|
|
|
|
+ const materials = this.materials.reader(scope);
|
|
|
|
|
+ try {
|
|
|
|
|
+ const outcome = await this.loadDiscovery(request, options, materials);
|
|
|
|
|
+ throwIfAborted(options);
|
|
|
|
|
+ if (await materials.validate()) return outcome;
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ if (!isPluginMaterialChange(error)) throw error;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ throw pluginSourcesChanged();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ constructor(private readonly options: NodePluginAdapterOptions) {}
|
|
|
|
|
+
|
|
|
|
|
+ private async loadDiscovery(
|
|
|
|
|
+ request: PluginDiscoverRequest,
|
|
|
|
|
+ options: PluginAbortOptions | undefined,
|
|
|
|
|
+ materials: PluginMaterialReader,
|
|
|
|
|
+ ): Promise<PluginLoadOutcome> {
|
|
|
if (!request.config.enabled) return emptyOutcome();
|
|
if (!request.config.enabled) return emptyOutcome();
|
|
|
|
|
|
|
|
const diagnostics: PluginDiagnostic[] = [];
|
|
const diagnostics: PluginDiagnostic[] = [];
|
|
|
const dataRoot = join(request.storageRoot || this.options.storageRoot, "data");
|
|
const dataRoot = join(request.storageRoot || this.options.storageRoot, "data");
|
|
|
- const candidates = this.resolveCandidates(request, diagnostics, options);
|
|
|
|
|
|
|
+ const candidates = await this.resolveCandidates(request, diagnostics, materials, options);
|
|
|
const commandRoots: CustomCommandRoot[] = [];
|
|
const commandRoots: CustomCommandRoot[] = [];
|
|
|
const hooks: PluginLoadOutcome["hooks"] = {};
|
|
const hooks: PluginLoadOutcome["hooks"] = {};
|
|
|
const mcpServers: PluginLoadOutcome["mcpServers"] = {};
|
|
const mcpServers: PluginLoadOutcome["mcpServers"] = {};
|
|
@@ -150,7 +201,7 @@ export class NodePluginAdapter implements PluginPort {
|
|
|
|
|
|
|
|
for (const candidate of candidates) {
|
|
for (const candidate of candidates) {
|
|
|
throwIfAborted(options);
|
|
throwIfAborted(options);
|
|
|
- const loaded = loadPlugin(candidate, diagnostics);
|
|
|
|
|
|
|
+ const loaded = await loadPlugin(candidate, diagnostics, materials);
|
|
|
if (!loaded) continue;
|
|
if (!loaded) continue;
|
|
|
// 内置(官方)插件被「卸载」后只在 user config 写 suppressedBuiltins 标记。这里在发现层
|
|
// 内置(官方)插件被「卸载」后只在 user config 写 suppressedBuiltins 标记。这里在发现层
|
|
|
// 用插件的权威 id(manifest 名 @ marketplace)过滤,不依赖缓存文件是否已被物理删除——
|
|
// 用插件的权威 id(manifest 名 @ marketplace)过滤,不依赖缓存文件是否已被物理删除——
|
|
@@ -182,16 +233,16 @@ export class NodePluginAdapter implements PluginPort {
|
|
|
// 只从启用后解析出的 component.mcpServers 生成 mcpServerNames,
|
|
// 只从启用后解析出的 component.mcpServers 生成 mcpServerNames,
|
|
|
// 未启用插件的内置 MCP 就会在管理页完全不可见。这里先读取声明名给 UI 只读展示,
|
|
// 未启用插件的内置 MCP 就会在管理页完全不可见。这里先读取声明名给 UI 只读展示,
|
|
|
// 实际 runtime 注入仍只使用 enabled 分支解析出的 component.mcpServers。
|
|
// 实际 runtime 注入仍只使用 enabled 分支解析出的 component.mcpServers。
|
|
|
- const mcpServerDefinitions = loadPluginMcpServerDefinitions({ diagnostics, loaded });
|
|
|
|
|
|
|
+ const mcpServerDefinitions = await loadPluginMcpServerDefinitions({ diagnostics, loaded });
|
|
|
const hooksRunnable = canRunPluginHooks(loaded);
|
|
const hooksRunnable = canRunPluginHooks(loaded);
|
|
|
- const hookInspection = inspectPluginHooks({
|
|
|
|
|
|
|
+ const hookInspection = await inspectPluginHooks({
|
|
|
dataPath,
|
|
dataPath,
|
|
|
diagnostics,
|
|
diagnostics,
|
|
|
loaded,
|
|
loaded,
|
|
|
runnable: hooksRunnable,
|
|
runnable: hooksRunnable,
|
|
|
});
|
|
});
|
|
|
const component = enabled
|
|
const component = enabled
|
|
|
- ? resolveEnabledComponents({
|
|
|
|
|
|
|
+ ? await resolveEnabledComponents({
|
|
|
dataPath,
|
|
dataPath,
|
|
|
diagnostics,
|
|
diagnostics,
|
|
|
env: request.env ?? {},
|
|
env: request.env ?? {},
|
|
@@ -212,7 +263,7 @@ export class NodePluginAdapter implements PluginPort {
|
|
|
commandRoots.push(...component.commandRoots);
|
|
commandRoots.push(...component.commandRoots);
|
|
|
const declaredMcpServerNames = Object.keys(mcpServerDefinitions);
|
|
const declaredMcpServerNames = Object.keys(mcpServerDefinitions);
|
|
|
plugins.push(
|
|
plugins.push(
|
|
|
- createPluginMetadata(
|
|
|
|
|
|
|
+ await createPluginMetadata(
|
|
|
loaded,
|
|
loaded,
|
|
|
component,
|
|
component,
|
|
|
dataPath,
|
|
dataPath,
|
|
@@ -235,11 +286,12 @@ export class NodePluginAdapter implements PluginPort {
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private resolveCandidates(
|
|
|
|
|
|
|
+ private async resolveCandidates(
|
|
|
request: Pick<PluginDiscoverRequest, "config" | "officialPluginRoots" | "storageRoot">,
|
|
request: Pick<PluginDiscoverRequest, "config" | "officialPluginRoots" | "storageRoot">,
|
|
|
diagnostics: PluginDiagnostic[],
|
|
diagnostics: PluginDiagnostic[],
|
|
|
|
|
+ materials: PluginMaterialReader,
|
|
|
options?: PluginAbortOptions,
|
|
options?: PluginAbortOptions,
|
|
|
- ): PluginCandidate[] {
|
|
|
|
|
|
|
+ ): Promise<PluginCandidate[]> {
|
|
|
const candidates: PluginCandidate[] = [];
|
|
const candidates: PluginCandidate[] = [];
|
|
|
for (const rootPath of request.config.dirs) {
|
|
for (const rootPath of request.config.dirs) {
|
|
|
candidates.push({
|
|
candidates.push({
|
|
@@ -258,18 +310,31 @@ export class NodePluginAdapter implements PluginPort {
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
candidates.push(
|
|
candidates.push(
|
|
|
- ...scanOfficialCache(request.storageRoot, diagnostics, options).map((rootPath) => ({
|
|
|
|
|
- defaultEnabled: false,
|
|
|
|
|
- marketplace: ZCODE_OFFICIAL_PLUGIN_MARKETPLACE,
|
|
|
|
|
- rootPath,
|
|
|
|
|
- source: "official" as const,
|
|
|
|
|
- })),
|
|
|
|
|
|
|
+ ...(await scanOfficialCache(request.storageRoot, diagnostics, materials, options)).map(
|
|
|
|
|
+ (rootPath) => ({
|
|
|
|
|
+ defaultEnabled: false,
|
|
|
|
|
+ marketplace: ZCODE_OFFICIAL_PLUGIN_MARKETPLACE,
|
|
|
|
|
+ rootPath,
|
|
|
|
|
+ source: "official" as const,
|
|
|
|
|
+ }),
|
|
|
|
|
+ ),
|
|
|
);
|
|
);
|
|
|
- for (const installed of listInstalledPluginRecords(request.storageRoot)) {
|
|
|
|
|
|
|
+ for (const name of [
|
|
|
|
|
+ "installed_plugins.json",
|
|
|
|
|
+ ".installed_plugins.json.backup",
|
|
|
|
|
+ ".installed_plugins.json.transaction.json",
|
|
|
|
|
+ ]) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ await materials.observe(join(request.storageRoot, name));
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ rethrowPluginMaterialInterrupt(error);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ for (const installed of await listInstalledPluginRecordsAsync(request.storageRoot)) {
|
|
|
candidates.push({
|
|
candidates.push({
|
|
|
defaultEnabled: false,
|
|
defaultEnabled: false,
|
|
|
marketplace: installed.marketplace,
|
|
marketplace: installed.marketplace,
|
|
|
- rootPath: resolveInstalledPluginRoot(request.storageRoot, installed),
|
|
|
|
|
|
|
+ rootPath: await resolveInstalledPluginRootAsync(request.storageRoot, installed),
|
|
|
source: "cache",
|
|
source: "cache",
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
@@ -281,17 +346,19 @@ export function createNodePluginAdapter(options: NodePluginAdapterOptions): Node
|
|
|
return new NodePluginAdapter(options);
|
|
return new NodePluginAdapter(options);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-export function discoverNodePluginsSync(
|
|
|
|
|
|
|
+export async function discoverNodePlugins(
|
|
|
request: PluginDiscoverRequest,
|
|
request: PluginDiscoverRequest,
|
|
|
options?: PluginAbortOptions,
|
|
options?: PluginAbortOptions,
|
|
|
-): PluginLoadOutcome {
|
|
|
|
|
- return createNodePluginAdapter({ storageRoot: request.storageRoot }).discoverPluginsSync(
|
|
|
|
|
- request,
|
|
|
|
|
- options,
|
|
|
|
|
- );
|
|
|
|
|
|
|
+): Promise<PluginLoadOutcome> {
|
|
|
|
|
+ const adapter = createNodePluginAdapter({ storageRoot: request.storageRoot });
|
|
|
|
|
+ try {
|
|
|
|
|
+ return await adapter.discoverPlugins(request, options);
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ adapter.dispose();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function createPluginMetadata(
|
|
|
|
|
|
|
+async function createPluginMetadata(
|
|
|
loaded: LoadedPlugin,
|
|
loaded: LoadedPlugin,
|
|
|
component: PluginComponents,
|
|
component: PluginComponents,
|
|
|
dataPath: string,
|
|
dataPath: string,
|
|
@@ -299,7 +366,7 @@ function createPluginMetadata(
|
|
|
declaredMcpServerNames: string[],
|
|
declaredMcpServerNames: string[],
|
|
|
configuredOptions: Record<string, string | number | boolean>,
|
|
configuredOptions: Record<string, string | number | boolean>,
|
|
|
uiSurfaces: PluginUiSurfaceDefinition[],
|
|
uiSurfaces: PluginUiSurfaceDefinition[],
|
|
|
-): PluginMetadata {
|
|
|
|
|
|
|
+): Promise<PluginMetadata> {
|
|
|
// manifest 的 author/homepage 作为详情页信息区的回退来源(商店 listing 优先)。
|
|
// manifest 的 author/homepage 作为详情页信息区的回退来源(商店 listing 优先)。
|
|
|
const author = normalizeAuthorValue(loaded.manifest.author);
|
|
const author = normalizeAuthorValue(loaded.manifest.author);
|
|
|
const homepage =
|
|
const homepage =
|
|
@@ -316,8 +383,8 @@ function createPluginMetadata(
|
|
|
// 且 UI join 对停用插件不产出名称(skillsService 里 `if (!enabled) continue`),导致:停用时
|
|
// 且 UI join 对停用插件不产出名称(skillsService 里 `if (!enabled) continue`),导致:停用时
|
|
|
// 整个技能分组消失、启用时只有数量没有名称。这里改为对插件根目录做权威枚举(与启用态无关),
|
|
// 整个技能分组消失、启用时只有数量没有名称。这里改为对插件根目录做权威枚举(与启用态无关),
|
|
|
// 直接把名称+描述随 list 下发,UI 不再需要脆弱的 join。
|
|
// 直接把名称+描述随 list 下发,UI 不再需要脆弱的 join。
|
|
|
- components: enumeratePluginComponents(loaded.rootPath, loaded.manifest, { loaded }),
|
|
|
|
|
- configuredOptions,
|
|
|
|
|
|
|
+ components: await enumeratePluginComponents(loaded.rootPath, loaded.manifest, { loaded }),
|
|
|
|
|
+ configuredOptions: { ...configuredOptions },
|
|
|
dataPath,
|
|
dataPath,
|
|
|
declaredMcpServerNames,
|
|
declaredMcpServerNames,
|
|
|
description: loaded.manifest.description,
|
|
description: loaded.manifest.description,
|
|
@@ -338,7 +405,7 @@ function createPluginMetadata(
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function resolveEnabledComponents(input: {
|
|
|
|
|
|
|
+async function resolveEnabledComponents(input: {
|
|
|
dataPath: string;
|
|
dataPath: string;
|
|
|
diagnostics: PluginDiagnostic[];
|
|
diagnostics: PluginDiagnostic[];
|
|
|
env: Record<string, string | undefined>;
|
|
env: Record<string, string | undefined>;
|
|
@@ -349,12 +416,12 @@ function resolveEnabledComponents(input: {
|
|
|
options: Record<string, string | number | boolean>;
|
|
options: Record<string, string | number | boolean>;
|
|
|
priority: number;
|
|
priority: number;
|
|
|
workingDirectory: string;
|
|
workingDirectory: string;
|
|
|
-}): PluginComponents {
|
|
|
|
|
- mkdirSync(input.dataPath, { recursive: true });
|
|
|
|
|
|
|
+}): Promise<PluginComponents> {
|
|
|
|
|
+ await mkdir(input.dataPath, { recursive: true });
|
|
|
|
|
|
|
|
- const skillRoots = resolveSkillRoots(input, input.priority);
|
|
|
|
|
|
|
+ const skillRoots = await resolveSkillRoots(input, input.priority);
|
|
|
return {
|
|
return {
|
|
|
- commandRoots: resolveCommandRoots(input, input.priority + 1),
|
|
|
|
|
|
|
+ commandRoots: await resolveCommandRoots(input, input.priority + 1),
|
|
|
hooks: input.hookEvents,
|
|
hooks: input.hookEvents,
|
|
|
hookDetails: input.hookDetails,
|
|
hookDetails: input.hookDetails,
|
|
|
mcpServers: resolvePluginMcpServers({
|
|
mcpServers: resolvePluginMcpServers({
|
|
@@ -363,7 +430,7 @@ function resolveEnabledComponents(input: {
|
|
|
}),
|
|
}),
|
|
|
// 插件页要展示真实技能数量。之前只统计 skills root 数,
|
|
// 插件页要展示真实技能数量。之前只统计 skills root 数,
|
|
|
// document-skills 这种一个 root 下有多个 SKILL.md 的插件会被显示成 1。
|
|
// document-skills 这种一个 root 下有多个 SKILL.md 的插件会被显示成 1。
|
|
|
- skillCount: countSkillFiles(skillRoots),
|
|
|
|
|
|
|
+ skillCount: await countSkillFiles(skillRoots, input.loaded.materials),
|
|
|
skillRoots,
|
|
skillRoots,
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
@@ -389,89 +456,37 @@ function warnUnsupportedComponents(loaded: LoadedPlugin, diagnostics: PluginDiag
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function resolveSkillRoots(
|
|
|
|
|
|
|
+async function resolveSkillRoots(
|
|
|
input: { diagnostics: PluginDiagnostic[]; loaded: LoadedPlugin },
|
|
input: { diagnostics: PluginDiagnostic[]; loaded: LoadedPlugin },
|
|
|
priority: number,
|
|
priority: number,
|
|
|
-): SkillRoot[] {
|
|
|
|
|
- warnEmptyDeclaredSkillRoots(input);
|
|
|
|
|
- return resolveComponentRoots("skills", input, priority);
|
|
|
|
|
|
|
+): Promise<SkillRoot[]> {
|
|
|
|
|
+ await warnEmptyDeclaredSkillRoots(input);
|
|
|
|
|
+ return await resolveComponentRoots("skills", input, priority);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/**
|
|
|
|
|
- * manifest 显式声明的 skills 路径没有可用技能时发出诊断,避免路径配置错误静默失败。
|
|
|
|
|
- * 声明集合独立于 roots 列表计算,默认 skills/ 目录为空时不误报;路径缺失、目录为空和
|
|
|
|
|
- * 符号链接越界分别保留可操作的诊断信息,权限错误交给实际扫描链路报告。
|
|
|
|
|
- */
|
|
|
|
|
-function warnEmptyDeclaredSkillRoots(input: {
|
|
|
|
|
- diagnostics: PluginDiagnostic[];
|
|
|
|
|
- loaded: LoadedPlugin;
|
|
|
|
|
-}): void {
|
|
|
|
|
- const declared = parsePathList(input.loaded.manifest.skills);
|
|
|
|
|
- if (declared.length === 0) return;
|
|
|
|
|
- const seenPaths = new Set<string>();
|
|
|
|
|
- for (const rawPath of declared) {
|
|
|
|
|
- const resolved = resolveInside(input.loaded.rootPath, rawPath);
|
|
|
|
|
- if (!resolved || seenPaths.has(resolved)) continue;
|
|
|
|
|
- seenPaths.add(resolved);
|
|
|
|
|
- // 路径缺失判定只认 ENOENT/ENOTDIR(statSync 精确分类),EACCES 等权限
|
|
|
|
|
- // 错误不得误报成「不存在」——此时没有证据下结论,跳过告警,由 skill adapter
|
|
|
|
|
- // 扫描同一目录时发 skill_scan_failed。
|
|
|
|
|
- // message 按原因区分:「路径不存在」是 manifest 配错,「存在但没有技能」
|
|
|
|
|
- // 是内容问题,「symlink 逃逸出插件根」是安全拒绝,三者修复方式不同;
|
|
|
|
|
- // code 保持单一,UI 无需感知分类。
|
|
|
|
|
- if (isMissingPath(resolved)) {
|
|
|
|
|
- input.diagnostics.push({
|
|
|
|
|
- code: "plugin_skill_root_empty",
|
|
|
|
|
- message: `Plugin skills path does not exist: ${rawPath}`,
|
|
|
|
|
- path: resolved,
|
|
|
|
|
- pluginId: input.loaded.id,
|
|
|
|
|
- severity: "warning",
|
|
|
|
|
- });
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
- // 信任边界:声明路径是插件内容,扫描不跟随符号链接(目录级/文件级逃逸
|
|
|
|
|
- // 一并拒绝,含 Windows junction)。链接根/链接 SKILL.md 扫描为空后落入下方
|
|
|
|
|
- // 「没有任何技能」告警,不误报「不存在」(词法路径本身存在)。
|
|
|
|
|
- let skillFiles: string[];
|
|
|
|
|
- try {
|
|
|
|
|
- skillFiles = scanSkillFilesUnderRootSync(resolved, { followSymbolicLinks: false });
|
|
|
|
|
- } catch {
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
- if (skillFiles.length > 0) continue;
|
|
|
|
|
- input.diagnostics.push({
|
|
|
|
|
- code: "plugin_skill_root_empty",
|
|
|
|
|
- message: `Plugin skills path does not contain any skills: ${rawPath}`,
|
|
|
|
|
- path: resolved,
|
|
|
|
|
- pluginId: input.loaded.id,
|
|
|
|
|
- severity: "warning",
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-function resolveCommandRoots(
|
|
|
|
|
|
|
+async function resolveCommandRoots(
|
|
|
input: { dataPath: string; diagnostics: PluginDiagnostic[]; loaded: LoadedPlugin },
|
|
input: { dataPath: string; diagnostics: PluginDiagnostic[]; loaded: LoadedPlugin },
|
|
|
priority: number,
|
|
priority: number,
|
|
|
-): CustomCommandRoot[] {
|
|
|
|
|
- const roots = resolveComponentRoots<CustomCommandRoot>(
|
|
|
|
|
|
|
+): Promise<CustomCommandRoot[]> {
|
|
|
|
|
+ const roots = await resolveComponentRoots<CustomCommandRoot>(
|
|
|
"commands",
|
|
"commands",
|
|
|
input,
|
|
input,
|
|
|
priority,
|
|
priority,
|
|
|
createHookPluginContext(input.loaded, input.dataPath),
|
|
createHookPluginContext(input.loaded, input.dataPath),
|
|
|
);
|
|
);
|
|
|
- const generatedRoot = materializeCommandMetadataRoot(input, priority + 1);
|
|
|
|
|
|
|
+ const generatedRoot = await materializeCommandMetadataRoot(input, priority + 1);
|
|
|
if (generatedRoot) roots.push(generatedRoot);
|
|
if (generatedRoot) roots.push(generatedRoot);
|
|
|
return roots;
|
|
return roots;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function inspectPluginHooks(input: {
|
|
|
|
|
|
|
+async function inspectPluginHooks(input: {
|
|
|
dataPath: string;
|
|
dataPath: string;
|
|
|
diagnostics: PluginDiagnostic[];
|
|
diagnostics: PluginDiagnostic[];
|
|
|
loaded: LoadedPlugin;
|
|
loaded: LoadedPlugin;
|
|
|
runnable: boolean;
|
|
runnable: boolean;
|
|
|
-}): PluginHookInspection {
|
|
|
|
|
|
|
+}): Promise<PluginHookInspection> {
|
|
|
const inspection = emptyHookInspection();
|
|
const inspection = emptyHookInspection();
|
|
|
- for (const source of listPluginHookSources({
|
|
|
|
|
|
|
+ for (const source of await listPluginHookSources({
|
|
|
diagnostics: input.diagnostics,
|
|
diagnostics: input.diagnostics,
|
|
|
loaded: input.loaded,
|
|
loaded: input.loaded,
|
|
|
})) {
|
|
})) {
|
|
@@ -605,20 +620,6 @@ function toPluginHookDetail(input: {
|
|
|
return detail;
|
|
return detail;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function createHookPluginContext(
|
|
|
|
|
- loaded: LoadedPlugin,
|
|
|
|
|
- dataPath: string,
|
|
|
|
|
- sourcePath?: string,
|
|
|
|
|
-): HookPluginContext {
|
|
|
|
|
- return {
|
|
|
|
|
- dataPath,
|
|
|
|
|
- id: loaded.id,
|
|
|
|
|
- name: loaded.manifest.name,
|
|
|
|
|
- rootPath: loaded.rootPath,
|
|
|
|
|
- ...(sourcePath ? { sourcePath } : {}),
|
|
|
|
|
- };
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
function attachPluginToHook(hook: HookConfig, plugin: HookPluginContext): HookConfig {
|
|
function attachPluginToHook(hook: HookConfig, plugin: HookPluginContext): HookConfig {
|
|
|
return {
|
|
return {
|
|
|
...hook,
|
|
...hook,
|
|
@@ -651,38 +652,20 @@ function emptyHookInspection(): PluginHookInspection {
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function countSkillFiles(skillRoots: SkillRoot[]): number {
|
|
|
|
|
- // 技能识别规则收敛到共享 scan helper(根自身含
|
|
|
|
|
- // SKILL.md 时根自身是一个技能);同时声明根与
|
|
|
|
|
- // 默认 skills/ 根会命中同一个 SKILL.md,必须按文件路径去重,否则计数翻倍。
|
|
|
|
|
- // 顺带修正漂移:只认 isDirectory() 会漏掉 symlink 技能子目录,统一 helper 后一并计入。
|
|
|
|
|
- // helper 只吞 ENOENT;权限错误(EACCES 等)会抛出,这里按原语义把该根计 0,
|
|
|
|
|
- // 真正的 skill_scan_failed 诊断由 skill adapter 在运行时扫描同一目录时发出。
|
|
|
|
|
- // 信任边界:这里只消费 plugin roots(resolveSkillRoots 产物),不跟随符号链接。
|
|
|
|
|
- const seenFiles = new Set<string>();
|
|
|
|
|
- for (const skillRoot of skillRoots) {
|
|
|
|
|
- try {
|
|
|
|
|
- for (const file of scanSkillFilesUnderRootSync(skillRoot.path, {
|
|
|
|
|
- followSymbolicLinks: skillRoot.source !== "plugin",
|
|
|
|
|
- })) {
|
|
|
|
|
- seenFiles.add(file);
|
|
|
|
|
- }
|
|
|
|
|
- } catch {
|
|
|
|
|
- // 概览计数降级为 0;扫描诊断由 skill adapter 负责。
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return seenFiles.size;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-function resolveComponentRoots<T extends CustomCommandRoot | SkillRoot>(
|
|
|
|
|
|
|
+async function resolveComponentRoots<T extends CustomCommandRoot | SkillRoot>(
|
|
|
key: "commands" | "skills",
|
|
key: "commands" | "skills",
|
|
|
input: { diagnostics: PluginDiagnostic[]; loaded: LoadedPlugin },
|
|
input: { diagnostics: PluginDiagnostic[]; loaded: LoadedPlugin },
|
|
|
priority: number,
|
|
priority: number,
|
|
|
plugin?: HookPluginContext,
|
|
plugin?: HookPluginContext,
|
|
|
-): T[] {
|
|
|
|
|
|
|
+): Promise<T[]> {
|
|
|
const paths = parsePathList(input.loaded.manifest[key]);
|
|
const paths = parsePathList(input.loaded.manifest[key]);
|
|
|
const defaultPath = join(input.loaded.rootPath, key);
|
|
const defaultPath = join(input.loaded.rootPath, key);
|
|
|
- if (directoryExists(defaultPath)) {
|
|
|
|
|
|
|
+ // skills 的显式声明是完整入口清单,不能再补扫未声明的默认目录。
|
|
|
|
|
+ if (
|
|
|
|
|
+ (key !== "skills" || input.loaded.manifest.skills === undefined) &&
|
|
|
|
|
+ (await (input.loaded.materials?.directoryExists(defaultPath) ??
|
|
|
|
|
+ directoryExistsAsync(defaultPath)))
|
|
|
|
|
+ ) {
|
|
|
paths.unshift(key);
|
|
paths.unshift(key);
|
|
|
}
|
|
}
|
|
|
const roots: T[] = [];
|
|
const roots: T[] = [];
|
|
@@ -713,177 +696,48 @@ function resolveComponentRoots<T extends CustomCommandRoot | SkillRoot>(
|
|
|
return roots;
|
|
return roots;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function materializeCommandMetadataRoot(
|
|
|
|
|
- input: { dataPath: string; diagnostics: PluginDiagnostic[]; loaded: LoadedPlugin },
|
|
|
|
|
- priority: number,
|
|
|
|
|
-): CustomCommandRoot | null {
|
|
|
|
|
- const spec = input.loaded.manifest.commands;
|
|
|
|
|
- if (!isRecord(spec)) return null;
|
|
|
|
|
-
|
|
|
|
|
- const generatedRoot = join(input.dataPath, "generated-commands");
|
|
|
|
|
- let wroteCommand = false;
|
|
|
|
|
- mkdirSync(generatedRoot, { recursive: true });
|
|
|
|
|
-
|
|
|
|
|
- for (const [rawName, rawMetadata] of Object.entries(spec)) {
|
|
|
|
|
- if (!isRecord(rawMetadata)) {
|
|
|
|
|
- input.diagnostics.push({
|
|
|
|
|
- code: "plugin_manifest_invalid",
|
|
|
|
|
- message: `Plugin command metadata must be an object: ${rawName}`,
|
|
|
|
|
- path: input.loaded.manifestPath,
|
|
|
|
|
- pluginId: input.loaded.id,
|
|
|
|
|
- severity: "error",
|
|
|
|
|
- });
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- const name = normalizeGeneratedCommandName(rawName);
|
|
|
|
|
- if (!name) {
|
|
|
|
|
- input.diagnostics.push({
|
|
|
|
|
- code: "plugin_manifest_invalid",
|
|
|
|
|
- message: `Invalid plugin command name: ${rawName}`,
|
|
|
|
|
- path: input.loaded.manifestPath,
|
|
|
|
|
- pluginId: input.loaded.id,
|
|
|
|
|
- severity: "error",
|
|
|
|
|
- });
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- const source = typeof rawMetadata.source === "string" ? rawMetadata.source : undefined;
|
|
|
|
|
- const content = typeof rawMetadata.content === "string" ? rawMetadata.content : undefined;
|
|
|
|
|
- if ((source && content) || (!source && !content)) {
|
|
|
|
|
- input.diagnostics.push({
|
|
|
|
|
- code: "plugin_manifest_invalid",
|
|
|
|
|
- message: `Plugin command '${rawName}' must provide exactly one of source or content`,
|
|
|
|
|
- path: input.loaded.manifestPath,
|
|
|
|
|
- pluginId: input.loaded.id,
|
|
|
|
|
- severity: "error",
|
|
|
|
|
- });
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- let markdown = content;
|
|
|
|
|
- if (source) {
|
|
|
|
|
- const sourcePath = resolveInside(input.loaded.rootPath, trimRelativePrefix(source));
|
|
|
|
|
- if (!sourcePath) {
|
|
|
|
|
- input.diagnostics.push({
|
|
|
|
|
- code: "plugin_component_path_invalid",
|
|
|
|
|
- message: `Plugin command source escapes plugin root: ${source}`,
|
|
|
|
|
- path: input.loaded.manifestPath,
|
|
|
|
|
- pluginId: input.loaded.id,
|
|
|
|
|
- severity: "error",
|
|
|
|
|
- });
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
- if (!fileExists(sourcePath)) {
|
|
|
|
|
- input.diagnostics.push({
|
|
|
|
|
- code: "plugin_component_path_invalid",
|
|
|
|
|
- message: `Plugin command source file not found: ${source}`,
|
|
|
|
|
- path: sourcePath,
|
|
|
|
|
- pluginId: input.loaded.id,
|
|
|
|
|
- severity: "error",
|
|
|
|
|
- });
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
- markdown = readFileSync(sourcePath, "utf8");
|
|
|
|
|
- }
|
|
|
|
|
- if (markdown === undefined) continue;
|
|
|
|
|
-
|
|
|
|
|
- // 市场清单支持 commands object mapping 和 inline content。
|
|
|
|
|
- // ZCode 的 custom command loader 只扫描 markdown 根目录,因此把低风险命令内容
|
|
|
|
|
- // materialize 到插件 data 目录;生成路径不在 plugin root 外暴露,也不执行命令本身。
|
|
|
|
|
- writeFileSync(
|
|
|
|
|
- join(generatedRoot, `${name}.md`),
|
|
|
|
|
- applyCommandMetadataFrontmatter(markdown, rawMetadata),
|
|
|
|
|
- "utf8",
|
|
|
|
|
- );
|
|
|
|
|
- wroteCommand = true;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return wroteCommand
|
|
|
|
|
- ? {
|
|
|
|
|
- path: generatedRoot,
|
|
|
|
|
- plugin: createHookPluginContext(input.loaded, input.dataPath),
|
|
|
|
|
- priority,
|
|
|
|
|
- scope: input.loaded.source === "official" ? "system" : "user",
|
|
|
|
|
- source: "plugin",
|
|
|
|
|
- }
|
|
|
|
|
- : null;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-function normalizeGeneratedCommandName(name: string): string | null {
|
|
|
|
|
- const normalized = name.trim().replace(/^\/+/, "").toLowerCase();
|
|
|
|
|
- if (!/^[a-z0-9][a-z0-9_:-]{0,63}$/.test(normalized)) return null;
|
|
|
|
|
- return normalized;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-function trimRelativePrefix(path: string): string {
|
|
|
|
|
- return path.replace(/^\.\//, "");
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-function applyCommandMetadataFrontmatter(
|
|
|
|
|
- markdown: string,
|
|
|
|
|
- metadata: Record<string, unknown>,
|
|
|
|
|
-): string {
|
|
|
|
|
- const frontmatter = new Map<string, string>();
|
|
|
|
|
- if (typeof metadata.description === "string" && metadata.description.trim()) {
|
|
|
|
|
- frontmatter.set("description", metadata.description.trim());
|
|
|
|
|
- }
|
|
|
|
|
- if (typeof metadata.argumentHint === "string" && metadata.argumentHint.trim()) {
|
|
|
|
|
- frontmatter.set("argument-hint", metadata.argumentHint.trim());
|
|
|
|
|
- }
|
|
|
|
|
- if (typeof metadata.model === "string" && metadata.model.trim()) {
|
|
|
|
|
- frontmatter.set("model", metadata.model.trim());
|
|
|
|
|
- }
|
|
|
|
|
- if (Array.isArray(metadata.allowedTools)) {
|
|
|
|
|
- const allowedTools = metadata.allowedTools
|
|
|
|
|
- .filter((tool): tool is string => typeof tool === "string" && tool.trim().length > 0)
|
|
|
|
|
- .map((tool) => tool.trim());
|
|
|
|
|
- if (allowedTools.length > 0) frontmatter.set("allowed-tools", allowedTools.join(", "));
|
|
|
|
|
- }
|
|
|
|
|
- if (frontmatter.size === 0) return markdown;
|
|
|
|
|
- const body = stripMarkdownFrontmatter(markdown).trimStart();
|
|
|
|
|
- return `---\n${Array.from(frontmatter, ([key, value]) => `${key}: ${value}`).join("\n")}\n---\n\n${body}`;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-function stripMarkdownFrontmatter(markdown: string): string {
|
|
|
|
|
- const normalized = markdown.replace(/^\uFEFF/, "");
|
|
|
|
|
- if (!normalized.startsWith("---")) return markdown;
|
|
|
|
|
- const lines = normalized.split(/\r?\n/);
|
|
|
|
|
- if (lines[0]?.trim() !== "---") return markdown;
|
|
|
|
|
- const endIndex = lines.findIndex((line, index) => index > 0 && line.trim() === "---");
|
|
|
|
|
- return endIndex > 0 ? lines.slice(endIndex + 1).join("\n") : markdown;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-function scanOfficialCache(
|
|
|
|
|
|
|
+async function scanOfficialCache(
|
|
|
storageRoot: string,
|
|
storageRoot: string,
|
|
|
diagnostics: PluginDiagnostic[],
|
|
diagnostics: PluginDiagnostic[],
|
|
|
|
|
+ materials: PluginMaterialReader,
|
|
|
options?: PluginAbortOptions,
|
|
options?: PluginAbortOptions,
|
|
|
-): string[] {
|
|
|
|
|
|
|
+): Promise<string[]> {
|
|
|
// 官方插件升级会保留旧版本缓存目录;若遍历全部目录再按插件 id
|
|
// 官方插件升级会保留旧版本缓存目录;若遍历全部目录再按插件 id
|
|
|
// “先到先得”,旧版本会抢在 bundled marketplace 指向的当前版本前被加载。
|
|
// “先到先得”,旧版本会抢在 bundled marketplace 指向的当前版本前被加载。
|
|
|
// bundled 分片是当前随应用发布资产的权威清单;存在时只加载其 cachePath。
|
|
// bundled 分片是当前随应用发布资产的权威清单;存在时只加载其 cachePath。
|
|
|
// 不能简单选择最高 semver,否则官方回滚版本时仍会错误加载旧缓存。
|
|
// 不能简单选择最高 semver,否则官方回滚版本时仍会错误加载旧缓存。
|
|
|
- const bundledRoots = loadBundledOfficialPluginRootsSync(storageRoot);
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ await materials.observe(
|
|
|
|
|
+ join(
|
|
|
|
|
+ storageRoot,
|
|
|
|
|
+ "marketplaces",
|
|
|
|
|
+ ZCODE_OFFICIAL_PLUGIN_MARKETPLACE,
|
|
|
|
|
+ "bundled-marketplace.json",
|
|
|
|
|
+ ),
|
|
|
|
|
+ );
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ rethrowPluginMaterialInterrupt(error);
|
|
|
|
|
+ }
|
|
|
|
|
+ const bundledRoots = await loadBundledOfficialPluginRoots(storageRoot);
|
|
|
if (bundledRoots !== undefined) {
|
|
if (bundledRoots !== undefined) {
|
|
|
- for (const rootPath of bundledRoots) {
|
|
|
|
|
- throwIfAborted(options);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ throwIfAborted(options);
|
|
|
return bundledRoots;
|
|
return bundledRoots;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const cacheRoot = join(storageRoot, "cache", ZCODE_OFFICIAL_PLUGIN_MARKETPLACE);
|
|
const cacheRoot = join(storageRoot, "cache", ZCODE_OFFICIAL_PLUGIN_MARKETPLACE);
|
|
|
try {
|
|
try {
|
|
|
const roots: string[] = [];
|
|
const roots: string[] = [];
|
|
|
- for (const pluginEntry of readdirSync(cacheRoot, { withFileTypes: true })) {
|
|
|
|
|
|
|
+ for (const pluginEntry of await materials.directory(cacheRoot)) {
|
|
|
throwIfAborted(options);
|
|
throwIfAborted(options);
|
|
|
if (!pluginEntry.isDirectory()) continue;
|
|
if (!pluginEntry.isDirectory()) continue;
|
|
|
const pluginDir = join(cacheRoot, pluginEntry.name);
|
|
const pluginDir = join(cacheRoot, pluginEntry.name);
|
|
|
- for (const versionEntry of readdirSync(pluginDir, { withFileTypes: true })) {
|
|
|
|
|
|
|
+ for (const versionEntry of await materials.directory(pluginDir)) {
|
|
|
if (versionEntry.isDirectory()) roots.push(join(pluginDir, versionEntry.name));
|
|
if (versionEntry.isDirectory()) roots.push(join(pluginDir, versionEntry.name));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
return roots;
|
|
return roots;
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
|
|
+ rethrowPluginMaterialInterrupt(error);
|
|
|
if (isNotFoundError(error)) return [];
|
|
if (isNotFoundError(error)) return [];
|
|
|
diagnostics.push({
|
|
diagnostics.push({
|
|
|
code: "plugin_root_not_found",
|
|
code: "plugin_root_not_found",
|
|
@@ -895,11 +749,12 @@ function scanOfficialCache(
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function loadPlugin(
|
|
|
|
|
|
|
+async function loadPlugin(
|
|
|
candidate: PluginCandidate,
|
|
candidate: PluginCandidate,
|
|
|
diagnostics: PluginDiagnostic[],
|
|
diagnostics: PluginDiagnostic[],
|
|
|
-): LoadedPlugin | null {
|
|
|
|
|
- if (!directoryExists(candidate.rootPath)) {
|
|
|
|
|
|
|
+ materials: PluginMaterialReader,
|
|
|
|
|
+): Promise<LoadedPlugin | null> {
|
|
|
|
|
+ if (!(await materials.directoryExists(candidate.rootPath))) {
|
|
|
diagnostics.push({
|
|
diagnostics.push({
|
|
|
code: "plugin_root_not_found",
|
|
code: "plugin_root_not_found",
|
|
|
message: `Plugin root does not exist: ${candidate.rootPath}`,
|
|
message: `Plugin root does not exist: ${candidate.rootPath}`,
|
|
@@ -909,7 +764,13 @@ function loadPlugin(
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const manifestPath = findManifest(candidate.rootPath);
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ await materials.directory(candidate.rootPath);
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ // 列目录只是减少缺失入口探测;无枚举权限时仍允许按已知路径读取 manifest。
|
|
|
|
|
+ rethrowPluginMaterialInterrupt(error);
|
|
|
|
|
+ }
|
|
|
|
|
+ const manifestPath = await findManifest(candidate.rootPath, materials);
|
|
|
if (!manifestPath) {
|
|
if (!manifestPath) {
|
|
|
diagnostics.push({
|
|
diagnostics.push({
|
|
|
code: "plugin_manifest_not_found",
|
|
code: "plugin_manifest_not_found",
|
|
@@ -920,47 +781,63 @@ function loadPlugin(
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const manifest = readManifest(manifestPath, diagnostics);
|
|
|
|
|
|
|
+ const manifest = await readManifest(manifestPath, diagnostics, materials, candidate.marketplace);
|
|
|
if (!manifest) return null;
|
|
if (!manifest) return null;
|
|
|
return {
|
|
return {
|
|
|
id: `${manifest.name}@${candidate.marketplace}`,
|
|
id: `${manifest.name}@${candidate.marketplace}`,
|
|
|
manifest,
|
|
manifest,
|
|
|
manifestPath,
|
|
manifestPath,
|
|
|
|
|
+ materials,
|
|
|
marketplace: candidate.marketplace,
|
|
marketplace: candidate.marketplace,
|
|
|
rootPath: candidate.rootPath,
|
|
rootPath: candidate.rootPath,
|
|
|
source: candidate.source,
|
|
source: candidate.source,
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function findManifest(rootPath: string): string | null {
|
|
|
|
|
|
|
+async function findManifest(
|
|
|
|
|
+ rootPath: string,
|
|
|
|
|
+ materials: PluginMaterialReader,
|
|
|
|
|
+): Promise<string | null> {
|
|
|
const zcodePath = join(rootPath, ZCODE_MANIFEST_PATH);
|
|
const zcodePath = join(rootPath, ZCODE_MANIFEST_PATH);
|
|
|
- if (fileExists(zcodePath)) {
|
|
|
|
|
|
|
+ if (await materials.fileExists(zcodePath)) {
|
|
|
return zcodePath;
|
|
return zcodePath;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 兼容不同 manifest 目录约定,发现阶段按稳定优先级回退。
|
|
// 兼容不同 manifest 目录约定,发现阶段按稳定优先级回退。
|
|
|
const claudePath = join(rootPath, CLAUDE_MANIFEST_PATH);
|
|
const claudePath = join(rootPath, CLAUDE_MANIFEST_PATH);
|
|
|
- if (fileExists(claudePath)) {
|
|
|
|
|
|
|
+ if (await materials.fileExists(claudePath)) {
|
|
|
return claudePath;
|
|
return claudePath;
|
|
|
}
|
|
}
|
|
|
const codexPath = join(rootPath, CODEX_MANIFEST_PATH);
|
|
const codexPath = join(rootPath, CODEX_MANIFEST_PATH);
|
|
|
- return fileExists(codexPath) ? codexPath : null;
|
|
|
|
|
|
|
+ return (await materials.fileExists(codexPath)) ? codexPath : null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function readManifest(path: string, diagnostics: PluginDiagnostic[]): PluginManifest | null {
|
|
|
|
|
|
|
+async function readManifest(
|
|
|
|
|
+ path: string,
|
|
|
|
|
+ diagnostics: PluginDiagnostic[],
|
|
|
|
|
+ materials: PluginMaterialReader,
|
|
|
|
|
+ marketplace: string,
|
|
|
|
|
+): Promise<PluginManifest | null> {
|
|
|
|
|
+ let pluginId: string | undefined;
|
|
|
try {
|
|
try {
|
|
|
- const parsed = JSON.parse(readFileSync(path, "utf8")) as unknown;
|
|
|
|
|
|
|
+ const parsed = await materials.json(path);
|
|
|
if (!isRecord(parsed)) throw new Error("Manifest must be a JSON object");
|
|
if (!isRecord(parsed)) throw new Error("Manifest must be a JSON object");
|
|
|
const name = typeof parsed.name === "string" ? parsed.name.trim() : "";
|
|
const name = typeof parsed.name === "string" ? parsed.name.trim() : "";
|
|
|
if (!PLUGIN_NAME_PATTERN.test(name)) throw new Error(`Invalid plugin name: ${name}`);
|
|
if (!PLUGIN_NAME_PATTERN.test(name)) throw new Error(`Invalid plugin name: ${name}`);
|
|
|
|
|
+ pluginId = `${name}@${marketplace}`;
|
|
|
return {
|
|
return {
|
|
|
...parsed,
|
|
...parsed,
|
|
|
name,
|
|
name,
|
|
|
version: typeof parsed.version === "string" ? parsed.version : DEFAULT_VERSION,
|
|
version: typeof parsed.version === "string" ? parsed.version : DEFAULT_VERSION,
|
|
|
|
|
+ ...(parsed.userConfig !== undefined
|
|
|
|
|
+ ? { userConfig: pluginUserConfigSchema.parse(parsed.userConfig) }
|
|
|
|
|
+ : {}),
|
|
|
} as PluginManifest;
|
|
} as PluginManifest;
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
|
|
+ rethrowPluginMaterialInterrupt(error);
|
|
|
diagnostics.push({
|
|
diagnostics.push({
|
|
|
code: "plugin_manifest_invalid",
|
|
code: "plugin_manifest_invalid",
|
|
|
|
|
+ ...(pluginId ? { pluginId } : {}),
|
|
|
message: error instanceof Error ? error.message : `Invalid plugin manifest: ${path}`,
|
|
message: error instanceof Error ? error.message : `Invalid plugin manifest: ${path}`,
|
|
|
path,
|
|
path,
|
|
|
severity: "error",
|
|
severity: "error",
|