2026-05-29 17:22:32 +08:00

133 lines
4.1 KiB
TypeScript

import {
AGENT_PRESET_LIBRARY_CONFIG_KEY,
} from "@/lib/agents/preset-library-config";
import {
AGENT_PSD_TEMPLATE_SECONDARY_CATEGORY_CONFIG_KEY,
} from "@/lib/agents/psd-template-secondary-category-config";
import {
AGENT_PSD_TEXT_COLOR_CONFIG_KEY,
} from "@/lib/agents/psd-text-color-config";
import {
AGENT_WORKFLOW_BRAND_MODEL_CONFIG_KEY,
} from "@/lib/agents/workflow-brand-model-config";
import {
AGENT_WORKFLOW_GOODS_INFO_API_CONFIG_KEY,
} from "@/lib/agents/workflow-goods-info-config";
import {
AGENT_WORKFLOW_MATERIAL_API_CONFIG_KEY,
} from "@/lib/agents/workflow-material-config";
import {
AGENT_WORKFLOW_SHOWCASE_CONFIG_KEY,
} from "@/lib/agents/workflow-showcase-config";
import {
AGENT_WORKFLOW_UI_CONFIG_KEY,
} from "@/lib/agents/workflow-ui-config";
export const AGENT_WORKFLOW_SNAPSHOT_SCOPE = "agent-workflow";
export const SYSTEM_SETTING_SNAPSHOT_SCOPES = {
[AGENT_WORKFLOW_SNAPSHOT_SCOPE]: [
AGENT_PRESET_LIBRARY_CONFIG_KEY,
AGENT_WORKFLOW_UI_CONFIG_KEY,
AGENT_WORKFLOW_MATERIAL_API_CONFIG_KEY,
AGENT_WORKFLOW_GOODS_INFO_API_CONFIG_KEY,
AGENT_WORKFLOW_BRAND_MODEL_CONFIG_KEY,
AGENT_WORKFLOW_SHOWCASE_CONFIG_KEY,
AGENT_PSD_TEXT_COLOR_CONFIG_KEY,
AGENT_PSD_TEMPLATE_SECONDARY_CATEGORY_CONFIG_KEY,
],
} as const;
export const SYSTEM_SETTING_SNAPSHOT_SITE_FIELDS = {
[AGENT_WORKFLOW_SNAPSHOT_SCOPE]: ["agentWorkflowDefaultModelId"],
} as const;
export type SystemSettingSnapshotScope = keyof typeof SYSTEM_SETTING_SNAPSHOT_SCOPES;
export function getSystemSettingSnapshotScopeKeys(
scope: SystemSettingSnapshotScope
) {
return SYSTEM_SETTING_SNAPSHOT_SCOPES[scope];
}
export function getSystemSettingSnapshotScopeSiteFields(
scope: SystemSettingSnapshotScope
) {
return SYSTEM_SETTING_SNAPSHOT_SITE_FIELDS[scope];
}
export function createSystemSettingSnapshotData(
scope: SystemSettingSnapshotScope,
valueByKey: ReadonlyMap<string, unknown>,
siteSettingsValues: Record<string, unknown> = {}
) {
const keys = getSystemSettingSnapshotScopeKeys(scope);
const siteFields = getSystemSettingSnapshotScopeSiteFields(scope);
return {
version: 2,
scope,
systemSettings: Object.fromEntries(keys.map((key) => [key, valueByKey.get(key) ?? null])),
siteSettings: Object.fromEntries(
siteFields.map((field) => [field, siteSettingsValues[field] ?? null])
),
};
}
export function readSystemSettingSnapshotData(
scope: SystemSettingSnapshotScope,
rawData: unknown
) {
const normalized =
rawData && typeof rawData === "object" && !Array.isArray(rawData)
? (rawData as Record<string, unknown>)
: {};
if (
normalized.systemSettings &&
typeof normalized.systemSettings === "object" &&
!Array.isArray(normalized.systemSettings)
) {
return {
systemSettings: normalized.systemSettings as Record<string, unknown>,
siteSettings:
normalized.siteSettings &&
typeof normalized.siteSettings === "object" &&
!Array.isArray(normalized.siteSettings)
? (normalized.siteSettings as Record<string, unknown>)
: {},
};
}
const keys = new Set<string>(getSystemSettingSnapshotScopeKeys(scope));
return {
systemSettings: Object.fromEntries(
Object.entries(normalized).filter(([key]) => keys.has(key))
),
siteSettings: {},
};
}
export function summarizeSystemSettingSnapshotData(
data: Record<string, unknown>
) {
const systemEntries =
data.systemSettings &&
typeof data.systemSettings === "object" &&
!Array.isArray(data.systemSettings)
? Object.entries(data.systemSettings as Record<string, unknown>)
: Object.entries(data);
const siteEntries =
data.siteSettings &&
typeof data.siteSettings === "object" &&
!Array.isArray(data.siteSettings)
? Object.entries(data.siteSettings as Record<string, unknown>)
: [];
const entries = [...systemEntries, ...siteEntries];
return {
keyCount: entries.length,
nonNullKeyCount: entries.filter(([, value]) => value !== null && value !== undefined).length,
byteLength: Buffer.byteLength(JSON.stringify(data), "utf8"),
siteFieldCount: siteEntries.length,
};
}