mirror of
https://github.com/langgenius/dify.git
synced 2026-06-08 00:41:55 +08:00
Co-authored-by: GareArc <garethcxy@dify.ai> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: L1nSn0w <l1nsn0w@qq.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Co-authored-by: gigglewang <gigglewang@dify.ai> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com>
59 lines
1.7 KiB
TypeScript
59 lines
1.7 KiB
TypeScript
import type { ConfigFile } from './schema.js'
|
|
import { readFile } from 'node:fs/promises'
|
|
import { join } from 'node:path'
|
|
import { load as parseYaml } from 'js-yaml'
|
|
import { newError } from '../errors/base.js'
|
|
import { ErrorCode } from '../errors/codes.js'
|
|
import {
|
|
|
|
ConfigFileSchema,
|
|
CURRENT_SCHEMA_VERSION,
|
|
FILE_NAME,
|
|
} from './schema.js'
|
|
|
|
export type LoadResult
|
|
= | { found: false }
|
|
| { found: true, config: ConfigFile }
|
|
|
|
export async function loadConfig(dir: string): Promise<LoadResult> {
|
|
const path = join(dir, FILE_NAME)
|
|
let raw: string
|
|
try {
|
|
raw = await readFile(path, 'utf8')
|
|
}
|
|
catch (err) {
|
|
if ((err as NodeJS.ErrnoException).code === 'ENOENT')
|
|
return { found: false }
|
|
throw newError(ErrorCode.Unknown, `read ${path}: ${(err as Error).message}`)
|
|
.wrap(err)
|
|
}
|
|
|
|
let parsed: unknown
|
|
try {
|
|
parsed = parseYaml(raw)
|
|
}
|
|
catch (err) {
|
|
throw newError(
|
|
ErrorCode.ConfigSchemaUnsupported,
|
|
`parse ${path}: ${(err as Error).message}`,
|
|
).wrap(err).withHint('config.yml is not valid YAML')
|
|
}
|
|
|
|
const result = ConfigFileSchema.safeParse(parsed ?? {})
|
|
if (!result.success) {
|
|
throw newError(
|
|
ErrorCode.ConfigSchemaUnsupported,
|
|
`validate ${path}: ${result.error.issues.map(i => i.message).join('; ')}`,
|
|
).withHint('config.yml does not match the v1 schema')
|
|
}
|
|
|
|
if (result.data.schema_version > CURRENT_SCHEMA_VERSION) {
|
|
throw newError(
|
|
ErrorCode.ConfigSchemaUnsupported,
|
|
`config.yml schema_version=${result.data.schema_version} is newer than this binary supports (max=${CURRENT_SCHEMA_VERSION})`,
|
|
).withHint('upgrade difyctl, or remove config.yml')
|
|
}
|
|
|
|
return { found: true, config: result.data }
|
|
}
|