mirror of
https://github.com/anomalyco/opencode.git
synced 2026-06-01 19:05:38 +00:00
refactor(config): migrate permission Action/Object/Rule leaves to Effect Schema (#23168)
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
export * as ConfigPermission from "./permission"
|
export * as ConfigPermission from "./permission"
|
||||||
|
import { Schema } from "effect"
|
||||||
import z from "zod"
|
import z from "zod"
|
||||||
|
import { zod } from "@/util/effect-zod"
|
||||||
|
import { withStatics } from "@/util/schema"
|
||||||
|
|
||||||
const permissionPreprocess = (val: unknown) => {
|
const permissionPreprocess = (val: unknown) => {
|
||||||
if (typeof val === "object" && val !== null && !Array.isArray(val)) {
|
if (typeof val === "object" && val !== null && !Array.isArray(val)) {
|
||||||
@@ -8,20 +11,20 @@ const permissionPreprocess = (val: unknown) => {
|
|||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Action = z.enum(["ask", "allow", "deny"]).meta({
|
export const Action = Schema.Literals(["ask", "allow", "deny"])
|
||||||
ref: "PermissionActionConfig",
|
.annotate({ identifier: "PermissionActionConfig" })
|
||||||
})
|
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
||||||
export type Action = z.infer<typeof Action>
|
export type Action = Schema.Schema.Type<typeof Action>
|
||||||
|
|
||||||
export const Object = z.record(z.string(), Action).meta({
|
export const Object = Schema.Record(Schema.String, Action)
|
||||||
ref: "PermissionObjectConfig",
|
.annotate({ identifier: "PermissionObjectConfig" })
|
||||||
})
|
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
||||||
export type Object = z.infer<typeof Object>
|
export type Object = Schema.Schema.Type<typeof Object>
|
||||||
|
|
||||||
export const Rule = z.union([Action, Object]).meta({
|
export const Rule = Schema.Union([Action, Object])
|
||||||
ref: "PermissionRuleConfig",
|
.annotate({ identifier: "PermissionRuleConfig" })
|
||||||
})
|
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
||||||
export type Rule = z.infer<typeof Rule>
|
export type Rule = Schema.Schema.Type<typeof Rule>
|
||||||
|
|
||||||
const transform = (x: unknown): Record<string, Rule> => {
|
const transform = (x: unknown): Record<string, Rule> => {
|
||||||
if (typeof x === "string") return { "*": x as Action }
|
if (typeof x === "string") return { "*": x as Action }
|
||||||
@@ -41,25 +44,25 @@ export const Info = z
|
|||||||
z
|
z
|
||||||
.object({
|
.object({
|
||||||
__originalKeys: z.string().array().optional(),
|
__originalKeys: z.string().array().optional(),
|
||||||
read: Rule.optional(),
|
read: Rule.zod.optional(),
|
||||||
edit: Rule.optional(),
|
edit: Rule.zod.optional(),
|
||||||
glob: Rule.optional(),
|
glob: Rule.zod.optional(),
|
||||||
grep: Rule.optional(),
|
grep: Rule.zod.optional(),
|
||||||
list: Rule.optional(),
|
list: Rule.zod.optional(),
|
||||||
bash: Rule.optional(),
|
bash: Rule.zod.optional(),
|
||||||
task: Rule.optional(),
|
task: Rule.zod.optional(),
|
||||||
external_directory: Rule.optional(),
|
external_directory: Rule.zod.optional(),
|
||||||
todowrite: Action.optional(),
|
todowrite: Action.zod.optional(),
|
||||||
question: Action.optional(),
|
question: Action.zod.optional(),
|
||||||
webfetch: Action.optional(),
|
webfetch: Action.zod.optional(),
|
||||||
websearch: Action.optional(),
|
websearch: Action.zod.optional(),
|
||||||
codesearch: Action.optional(),
|
codesearch: Action.zod.optional(),
|
||||||
lsp: Rule.optional(),
|
lsp: Rule.zod.optional(),
|
||||||
doom_loop: Action.optional(),
|
doom_loop: Action.zod.optional(),
|
||||||
skill: Rule.optional(),
|
skill: Rule.zod.optional(),
|
||||||
})
|
})
|
||||||
.catchall(Rule)
|
.catchall(Rule.zod)
|
||||||
.or(Action),
|
.or(Action.zod),
|
||||||
)
|
)
|
||||||
.transform(transform)
|
.transform(transform)
|
||||||
.meta({
|
.meta({
|
||||||
|
|||||||
Reference in New Issue
Block a user