feat(config): integrate Zod schema validation into config loading
- Replace type assertion with safeParse validation - Add error reporting for invalid config values - Import types from centralized config module
This commit is contained in:
34
src/index.ts
34
src/index.ts
@@ -1,18 +1,13 @@
|
|||||||
import type { Plugin } from "@opencode-ai/plugin"
|
import type { Plugin } from "@opencode-ai/plugin"
|
||||||
import { createBuiltinAgents, type AgentName, type AgentOverrides } from "./agents"
|
import { createBuiltinAgents } from "./agents"
|
||||||
import { createTodoContinuationEnforcer, createContextWindowMonitorHook, createSessionRecoveryHook } from "./hooks"
|
import { createTodoContinuationEnforcer, createContextWindowMonitorHook, createSessionRecoveryHook } from "./hooks"
|
||||||
import { updateTerminalTitle } from "./features/terminal"
|
import { updateTerminalTitle } from "./features/terminal"
|
||||||
import { builtinTools } from "./tools"
|
import { builtinTools } from "./tools"
|
||||||
import { createBuiltinMcps, type McpName } from "./mcp"
|
import { createBuiltinMcps } from "./mcp"
|
||||||
|
import { OhMyOpenCodeConfigSchema, type OhMyOpenCodeConfig } from "./config"
|
||||||
import * as fs from "fs"
|
import * as fs from "fs"
|
||||||
import * as path from "path"
|
import * as path from "path"
|
||||||
|
|
||||||
interface OhMyOpenCodeConfig {
|
|
||||||
disabled_mcps?: McpName[]
|
|
||||||
disabled_agents?: AgentName[]
|
|
||||||
agents?: AgentOverrides
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadPluginConfig(directory: string): OhMyOpenCodeConfig {
|
function loadPluginConfig(directory: string): OhMyOpenCodeConfig {
|
||||||
const configPaths = [
|
const configPaths = [
|
||||||
path.join(directory, "oh-my-opencode.json"),
|
path.join(directory, "oh-my-opencode.json"),
|
||||||
@@ -23,7 +18,18 @@ function loadPluginConfig(directory: string): OhMyOpenCodeConfig {
|
|||||||
try {
|
try {
|
||||||
if (fs.existsSync(configPath)) {
|
if (fs.existsSync(configPath)) {
|
||||||
const content = fs.readFileSync(configPath, "utf-8")
|
const content = fs.readFileSync(configPath, "utf-8")
|
||||||
return JSON.parse(content) as OhMyOpenCodeConfig
|
const rawConfig = JSON.parse(content)
|
||||||
|
const result = OhMyOpenCodeConfigSchema.safeParse(rawConfig)
|
||||||
|
|
||||||
|
if (!result.success) {
|
||||||
|
console.error(`[oh-my-opencode] Config validation error in ${configPath}:`)
|
||||||
|
for (const issue of result.error.issues) {
|
||||||
|
console.error(` - ${issue.path.join(".")}: ${issue.message}`)
|
||||||
|
}
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.data
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// Ignore parse errors, use defaults
|
// Ignore parse errors, use defaults
|
||||||
@@ -184,5 +190,11 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => {
|
|||||||
|
|
||||||
export default OhMyOpenCodePlugin
|
export default OhMyOpenCodePlugin
|
||||||
|
|
||||||
export type { AgentName, AgentOverrideConfig, AgentOverrides } from "./agents"
|
export { OhMyOpenCodeConfigSchema } from "./config"
|
||||||
export type { McpName } from "./mcp"
|
export type {
|
||||||
|
OhMyOpenCodeConfig,
|
||||||
|
AgentName,
|
||||||
|
AgentOverrideConfig,
|
||||||
|
AgentOverrides,
|
||||||
|
McpName,
|
||||||
|
} from "./config"
|
||||||
|
|||||||
Reference in New Issue
Block a user