返回 CodeWhale
diff.ts
1 /** Compare raw plugin configs using schema metadata without executing config hooks or validators. */
2 import type { Plugin } from '@deepseek-ai/cordis'
3 import { deepEqual } from '@deepseek-ai/cosmokit'
4 import type Schema from '@deepseek-ai/schemastery'
5 import { isJsExpr } from './utils.ts'
6
7 function isSchemastery(schema: Plugin.Runtime['Config']): schema is Schema {
8 return schema?.['~standard'].vendor === 'schemastery'
9 }
10
11 function isRecord(value: unknown): value is Record<string, unknown> {
12 if (!value || typeof value !== 'object' || isJsExpr(value)) return false
13 const prototype = Object.getPrototypeOf(value)
14 return prototype === Object.prototype || prototype === null
15 }
16
17 function equal(a: unknown, b: unknown, schema: Schema | undefined, ancestors: Set<Schema>): boolean {
18 if (schema?.meta?.volatile) return true
19 if (schema?.type !== 'object' || !schema.dict || ancestors.has(schema)) return deepEqual(a, b, true)
20 const left = a ?? schema.meta?.default
21 const right = b ?? schema.meta?.default
22 if (!isRecord(left) || !isRecord(right)) return deepEqual(left, right, true)
23 const { dict } = schema
24 ancestors.add(schema)
25 try {
26 return Object.keys({ ...left, ...right }).every(key =>
27 equal(left[key], right[key], Object.hasOwn(dict, key) ? dict[key] : undefined, ancestors),
28 )
29 } finally {
30 ancestors.delete(schema)
31 }
32 }
33
34 /**
35 * Compare two raw configs, treating schema-declared volatile fields at fixed object paths as equal and absent objects as their schema default.
36 * Schema backedges, expressions, unknown fields and opaque values keep strict raw equality; an absent or non-Schemastery schema compares everything raw.
37 * @param previous - previous raw config.
38 * @param next - next raw config.
39 * @param schema - the plugin's config schema.
40 * @returns Whether the configs differ at most in volatile fields, without evaluating expressions, validating config or modifying inputs.
41 * @internal
42 */
43 export function equalExceptVolatile(previous: unknown, next: unknown, schema: Plugin.Runtime['Config']): boolean {
44 return isSchemastery(schema) ? equal(previous, next, schema, new Set()) : deepEqual(previous, next, true)
45 }
46
46 lines TYPESCRIPT