mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 09:10:45 +00:00
refactor: compact generated protocol metadata
This commit is contained in:
@@ -160,12 +160,16 @@ async function compareGeneratedProtocolMirror(
|
||||
failures.push(`protocol-generated/json/${schema}: missing local schema (${String(error)})`);
|
||||
continue;
|
||||
}
|
||||
if (source !== target) {
|
||||
if (normalizeJsonSchema(source) !== normalizeJsonSchema(target)) {
|
||||
failures.push(`protocol-generated/json/${schema}: differs from source schema`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeJsonSchema(source: string): string {
|
||||
return JSON.stringify(JSON.parse(source));
|
||||
}
|
||||
|
||||
async function listFiles(root: string, suffix: string): Promise<string[]> {
|
||||
const files: string[] = [];
|
||||
async function visit(dir: string): Promise<void> {
|
||||
|
||||
@@ -6,6 +6,7 @@ import { loadChannelConfigSurfaceModule } from "./load-channel-config-surface.ts
|
||||
|
||||
const GENERATED_BY = "scripts/generate-bundled-channel-config-metadata.ts";
|
||||
const DEFAULT_OUTPUT_PATH = "src/config/bundled-channel-config-metadata.generated.ts";
|
||||
const GENERATED_JSON_CHUNK_SIZE = 16 * 1024;
|
||||
|
||||
type BundledPluginSource = {
|
||||
dirName: string;
|
||||
@@ -141,6 +142,15 @@ function formatTypeScriptModule(source: string, outputPath: string, repoRoot: st
|
||||
});
|
||||
}
|
||||
|
||||
function formatJsonStringChunks(value: unknown): string {
|
||||
const json = JSON.stringify(value);
|
||||
const chunks: string[] = [];
|
||||
for (let index = 0; index < json.length; index += GENERATED_JSON_CHUNK_SIZE) {
|
||||
chunks.push(JSON.stringify(json.slice(index, index + GENERATED_JSON_CHUNK_SIZE)));
|
||||
}
|
||||
return chunks.join(",\n ");
|
||||
}
|
||||
|
||||
function resolveChannelUnsupportedSecretRefSurfacePatterns(
|
||||
source: BundledPluginSource,
|
||||
channelId: string,
|
||||
@@ -223,10 +233,27 @@ export async function writeBundledChannelConfigMetadataModule(params?: {
|
||||
const repoRoot = path.resolve(params?.repoRoot ?? process.cwd());
|
||||
const outputPath = params?.outputPath ?? DEFAULT_OUTPUT_PATH;
|
||||
const entries = await collectBundledChannelConfigMetadata({ repoRoot });
|
||||
const chunks = formatJsonStringChunks(entries);
|
||||
const next = formatTypeScriptModule(
|
||||
`// Auto-generated by ${GENERATED_BY}. Do not edit directly.
|
||||
|
||||
export const GENERATED_BUNDLED_CHANNEL_CONFIG_METADATA = ${JSON.stringify(entries, null, 2)} as const;
|
||||
type BundledChannelConfigMetadata = {
|
||||
pluginId: string;
|
||||
channelId: string;
|
||||
label?: string;
|
||||
description?: string;
|
||||
schema: Record<string, unknown>;
|
||||
uiHints?: Record<string, unknown>;
|
||||
unsupportedSecretRefSurfacePatterns?: readonly string[];
|
||||
};
|
||||
|
||||
const RAW_BUNDLED_CHANNEL_CONFIG_METADATA = [
|
||||
${chunks},
|
||||
].join("");
|
||||
|
||||
export const GENERATED_BUNDLED_CHANNEL_CONFIG_METADATA = JSON.parse(
|
||||
RAW_BUNDLED_CHANNEL_CONFIG_METADATA,
|
||||
) as readonly BundledChannelConfigMetadata[];
|
||||
`,
|
||||
outputPath,
|
||||
repoRoot,
|
||||
|
||||
@@ -102,19 +102,7 @@ export function readGeneratedBundledChannelConfigs(repoRoot) {
|
||||
return new Map();
|
||||
}
|
||||
const source = fs.readFileSync(metadataPath, "utf8");
|
||||
const match = source.match(
|
||||
/export const GENERATED_BUNDLED_CHANNEL_CONFIG_METADATA = ([\s\S]*?) as const;/u,
|
||||
);
|
||||
if (!match?.[1]) {
|
||||
return new Map();
|
||||
}
|
||||
|
||||
let entries;
|
||||
try {
|
||||
entries = JSON5.parse(match[1]);
|
||||
} catch {
|
||||
return new Map();
|
||||
}
|
||||
const entries = readGeneratedBundledChannelConfigEntries(source);
|
||||
if (!Array.isArray(entries)) {
|
||||
return new Map();
|
||||
}
|
||||
@@ -145,6 +133,35 @@ export function readGeneratedBundledChannelConfigs(repoRoot) {
|
||||
return byPlugin;
|
||||
}
|
||||
|
||||
function readGeneratedBundledChannelConfigEntries(source) {
|
||||
const legacyMatch = source.match(
|
||||
/export const GENERATED_BUNDLED_CHANNEL_CONFIG_METADATA = ([\s\S]*?) as const;/u,
|
||||
);
|
||||
if (legacyMatch?.[1]) {
|
||||
try {
|
||||
return JSON5.parse(legacyMatch[1]);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
const compactMatch = source.match(
|
||||
/const RAW_BUNDLED_CHANNEL_CONFIG_METADATA = \[([\s\S]*?)\]\.join\(""\);/u,
|
||||
);
|
||||
if (!compactMatch?.[1]) {
|
||||
return undefined;
|
||||
}
|
||||
try {
|
||||
const chunks = JSON5.parse(`[${compactMatch[1]}]`);
|
||||
if (!Array.isArray(chunks) || chunks.some((chunk) => typeof chunk !== "string")) {
|
||||
return undefined;
|
||||
}
|
||||
return JSON.parse(chunks.join(""));
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export function mergeGeneratedChannelConfigs(manifest, generatedChannelConfigs) {
|
||||
if (!generatedChannelConfigs || Object.keys(generatedChannelConfigs).length === 0) {
|
||||
return manifest;
|
||||
|
||||
@@ -20,7 +20,11 @@ try {
|
||||
|
||||
for (const schema of selectedCodexAppServerJsonSchemas) {
|
||||
await fs.mkdir(path.dirname(path.join(targetRoot, "json", schema)), { recursive: true });
|
||||
await fs.copyFile(path.join(source.jsonRoot, schema), path.join(targetRoot, "json", schema));
|
||||
const schemaSource = await fs.readFile(path.join(source.jsonRoot, schema), "utf8");
|
||||
await fs.writeFile(
|
||||
path.join(targetRoot, "json", schema),
|
||||
`${JSON.stringify(JSON.parse(schemaSource))}\n`,
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
await source.cleanup();
|
||||
|
||||
Reference in New Issue
Block a user