Skip to content

Commit d84bbb9

Browse files
authored
refactor(schema): use Schema.Int and consolidate PositiveInt/NonNegativeInt (anomalyco#24029)
1 parent 316cf09 commit d84bbb9

6 files changed

Lines changed: 24 additions & 22 deletions

File tree

packages/opencode/src/config/agent.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Schema } from "effect"
44
import z from "zod"
55
import { Bus } from "@/bus"
66
import { zod } from "@/util/effect-zod"
7+
import { PositiveInt } from "@/util/schema"
78
import { Log } from "../util"
89
import { NamedError } from "@opencode-ai/shared/util/error"
910
import { Glob } from "@opencode-ai/shared/util/glob"
@@ -15,8 +16,6 @@ import { ConfigPermission } from "./permission"
1516

1617
const log = Log.create({ service: "config" })
1718

18-
const PositiveInt = Schema.Number.check(Schema.isInt()).check(Schema.isGreaterThan(0))
19-
2019
const Color = Schema.Union([
2120
Schema.String.check(Schema.isPattern(/^#[0-9a-fA-F]{6}$/)),
2221
Schema.Literals(["primary", "secondary", "accent", "success", "warning", "error", "info"]),

packages/opencode/src/config/config.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { Context, Duration, Effect, Exit, Fiber, Layer, Option, Schema } from "e
2525
import { EffectFlock } from "@opencode-ai/shared/util/effect-flock"
2626
import { InstanceRef } from "@/effect/instance-ref"
2727
import { zod, ZodOverride } from "@/util/effect-zod"
28-
import { withStatics } from "@/util/schema"
28+
import { NonNegativeInt, PositiveInt, withStatics } from "@/util/schema"
2929
import { ConfigAgent } from "./agent"
3030
import { ConfigCommand } from "./command"
3131
import { ConfigFormatter } from "./formatter"
@@ -88,9 +88,6 @@ export type Layout = ConfigLayout.Layout
8888
const AgentRef = Schema.Any.annotate({ [ZodOverride]: ConfigAgent.Info })
8989
const LogLevelRef = Schema.Any.annotate({ [ZodOverride]: Log.Level })
9090

91-
const PositiveInt = Schema.Number.check(Schema.isInt()).check(Schema.isGreaterThan(0))
92-
const NonNegativeInt = Schema.Number.check(Schema.isInt()).check(Schema.isGreaterThanOrEqualTo(0))
93-
9491
// The Effect Schema is the canonical source of truth. The `.zod` compatibility
9592
// surface is derived so existing Hono validators keep working without a parallel
9693
// Zod definition.

packages/opencode/src/config/provider.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { Schema } from "effect"
22
import { zod } from "@/util/effect-zod"
3-
import { withStatics } from "@/util/schema"
4-
5-
const PositiveInt = Schema.Number.check(Schema.isInt()).check(Schema.isGreaterThan(0))
3+
import { PositiveInt, withStatics } from "@/util/schema"
64

75
export const Model = Schema.Struct({
86
id: Schema.optional(Schema.String),

packages/opencode/src/config/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Schema } from "effect"
22
import { zod } from "@/util/effect-zod"
3-
import { withStatics } from "@/util/schema"
3+
import { PositiveInt, withStatics } from "@/util/schema"
44

55
export const Server = Schema.Struct({
6-
port: Schema.optional(Schema.Number.check(Schema.isInt()).check(Schema.isGreaterThan(0))).annotate({
6+
port: Schema.optional(PositiveInt).annotate({
77
description: "Port to listen on",
88
}),
99
hostname: Schema.optional(Schema.String).annotate({ description: "Hostname to listen on" }),

packages/opencode/src/session/message-v2.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type { Provider } from "@/provider"
1717
import { ModelID, ProviderID } from "@/provider/schema"
1818
import { Effect, Schema, Types } from "effect"
1919
import { zod, ZodOverride } from "@/util/effect-zod"
20-
import { withStatics } from "@/util/schema"
20+
import { NonNegativeInt, withStatics } from "@/util/schema"
2121
import { namedSchemaError } from "@/util/named-schema-error"
2222
import { EffectLogger } from "@/effect"
2323

@@ -64,9 +64,7 @@ export class OutputFormatText extends Schema.Class<OutputFormatText>("OutputForm
6464
export class OutputFormatJsonSchema extends Schema.Class<OutputFormatJsonSchema>("OutputFormatJsonSchema")({
6565
type: Schema.Literal("json_schema"),
6666
schema: Schema.Record(Schema.String, Schema.Any).annotate({ identifier: "JSONSchema" }),
67-
retryCount: Schema.Number.check(Schema.isInt())
68-
.check(Schema.isGreaterThanOrEqualTo(0))
69-
.pipe(Schema.optional, Schema.withDecodingDefault(Effect.succeed(2))),
67+
retryCount: NonNegativeInt.pipe(Schema.optional, Schema.withDecodingDefault(Effect.succeed(2))),
7068
}) {
7169
static readonly zod = zod(this)
7270
}
@@ -138,8 +136,8 @@ export type ReasoningPart = Types.DeepMutable<Schema.Schema.Type<typeof Reasonin
138136
const filePartSourceBase = {
139137
text: Schema.Struct({
140138
value: Schema.String,
141-
start: Schema.Number.check(Schema.isInt()),
142-
end: Schema.Number.check(Schema.isInt()),
139+
start: Schema.Int,
140+
end: Schema.Int,
143141
}).annotate({ identifier: "FilePartSourceText" }),
144142
}
145143

@@ -157,7 +155,7 @@ export const SymbolSource = Schema.Struct({
157155
path: Schema.String,
158156
range: LSP.Range,
159157
name: Schema.String,
160-
kind: Schema.Number.check(Schema.isInt()),
158+
kind: Schema.Int,
161159
})
162160
.annotate({ identifier: "SymbolSource" })
163161
.pipe(withStatics((s) => ({ zod: zod(s) })))
@@ -196,8 +194,8 @@ export const AgentPart = Schema.Struct({
196194
source: Schema.optional(
197195
Schema.Struct({
198196
value: Schema.String,
199-
start: Schema.Number.check(Schema.isInt()),
200-
end: Schema.Number.check(Schema.isInt()),
197+
start: Schema.Int,
198+
end: Schema.Int,
201199
}),
202200
),
203201
})
@@ -501,8 +499,8 @@ export const AgentPartInput = Schema.Struct({
501499
source: Schema.optional(
502500
Schema.Struct({
503501
value: Schema.String,
504-
start: Schema.Number.check(Schema.isInt()),
505-
end: Schema.Number.check(Schema.isInt()),
502+
start: Schema.Int,
503+
end: Schema.Int,
506504
}),
507505
),
508506
})

packages/opencode/src/util/schema.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import { Schema } from "effect"
22

3+
/**
4+
* Integer greater than zero.
5+
*/
6+
export const PositiveInt = Schema.Int.check(Schema.isGreaterThan(0))
7+
8+
/**
9+
* Integer greater than or equal to zero.
10+
*/
11+
export const NonNegativeInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))
12+
313
/**
414
* Attach static methods to a schema object. Designed to be used with `.pipe()`:
515
*

0 commit comments

Comments
 (0)