Skip to content

Commit d11268e

Browse files
authored
refactor(config): migrate permission Action/Object/Rule leaves to Effect Schema (#23168)
1 parent 650a13a commit d11268e

1 file changed

Lines changed: 33 additions & 30 deletions

File tree

packages/opencode/src/config/permission.ts

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
export * as ConfigPermission from "./permission"
2+
import { Schema } from "effect"
23
import z from "zod"
4+
import { zod } from "@/util/effect-zod"
5+
import { withStatics } from "@/util/schema"
36

47
const permissionPreprocess = (val: unknown) => {
58
if (typeof val === "object" && val !== null && !Array.isArray(val)) {
@@ -8,20 +11,20 @@ const permissionPreprocess = (val: unknown) => {
811
return val
912
}
1013

11-
export const Action = z.enum(["ask", "allow", "deny"]).meta({
12-
ref: "PermissionActionConfig",
13-
})
14-
export type Action = z.infer<typeof Action>
14+
export const Action = Schema.Literals(["ask", "allow", "deny"])
15+
.annotate({ identifier: "PermissionActionConfig" })
16+
.pipe(withStatics((s) => ({ zod: zod(s) })))
17+
export type Action = Schema.Schema.Type<typeof Action>
1518

16-
export const Object = z.record(z.string(), Action).meta({
17-
ref: "PermissionObjectConfig",
18-
})
19-
export type Object = z.infer<typeof Object>
19+
export const Object = Schema.Record(Schema.String, Action)
20+
.annotate({ identifier: "PermissionObjectConfig" })
21+
.pipe(withStatics((s) => ({ zod: zod(s) })))
22+
export type Object = Schema.Schema.Type<typeof Object>
2023

21-
export const Rule = z.union([Action, Object]).meta({
22-
ref: "PermissionRuleConfig",
23-
})
24-
export type Rule = z.infer<typeof Rule>
24+
export const Rule = Schema.Union([Action, Object])
25+
.annotate({ identifier: "PermissionRuleConfig" })
26+
.pipe(withStatics((s) => ({ zod: zod(s) })))
27+
export type Rule = Schema.Schema.Type<typeof Rule>
2528

2629
const transform = (x: unknown): Record<string, Rule> => {
2730
if (typeof x === "string") return { "*": x as Action }
@@ -41,25 +44,25 @@ export const Info = z
4144
z
4245
.object({
4346
__originalKeys: z.string().array().optional(),
44-
read: Rule.optional(),
45-
edit: Rule.optional(),
46-
glob: Rule.optional(),
47-
grep: Rule.optional(),
48-
list: Rule.optional(),
49-
bash: Rule.optional(),
50-
task: Rule.optional(),
51-
external_directory: Rule.optional(),
52-
todowrite: Action.optional(),
53-
question: Action.optional(),
54-
webfetch: Action.optional(),
55-
websearch: Action.optional(),
56-
codesearch: Action.optional(),
57-
lsp: Rule.optional(),
58-
doom_loop: Action.optional(),
59-
skill: Rule.optional(),
47+
read: Rule.zod.optional(),
48+
edit: Rule.zod.optional(),
49+
glob: Rule.zod.optional(),
50+
grep: Rule.zod.optional(),
51+
list: Rule.zod.optional(),
52+
bash: Rule.zod.optional(),
53+
task: Rule.zod.optional(),
54+
external_directory: Rule.zod.optional(),
55+
todowrite: Action.zod.optional(),
56+
question: Action.zod.optional(),
57+
webfetch: Action.zod.optional(),
58+
websearch: Action.zod.optional(),
59+
codesearch: Action.zod.optional(),
60+
lsp: Rule.zod.optional(),
61+
doom_loop: Action.zod.optional(),
62+
skill: Rule.zod.optional(),
6063
})
61-
.catchall(Rule)
62-
.or(Action),
64+
.catchall(Rule.zod)
65+
.or(Action.zod),
6366
)
6467
.transform(transform)
6568
.meta({

0 commit comments

Comments
 (0)