refactor(schema): use Schema.Int and consolidate PositiveInt/NonNegativeInt (#24029)
This commit is contained in:
@@ -4,6 +4,7 @@ import { Schema } from "effect"
|
|||||||
import z from "zod"
|
import z from "zod"
|
||||||
import { Bus } from "@/bus"
|
import { Bus } from "@/bus"
|
||||||
import { zod } from "@/util/effect-zod"
|
import { zod } from "@/util/effect-zod"
|
||||||
|
import { PositiveInt } from "@/util/schema"
|
||||||
import { Log } from "../util"
|
import { Log } from "../util"
|
||||||
import { NamedError } from "@opencode-ai/shared/util/error"
|
import { NamedError } from "@opencode-ai/shared/util/error"
|
||||||
import { Glob } from "@opencode-ai/shared/util/glob"
|
import { Glob } from "@opencode-ai/shared/util/glob"
|
||||||
@@ -15,8 +16,6 @@ import { ConfigPermission } from "./permission"
|
|||||||
|
|
||||||
const log = Log.create({ service: "config" })
|
const log = Log.create({ service: "config" })
|
||||||
|
|
||||||
const PositiveInt = Schema.Number.check(Schema.isInt()).check(Schema.isGreaterThan(0))
|
|
||||||
|
|
||||||
const Color = Schema.Union([
|
const Color = Schema.Union([
|
||||||
Schema.String.check(Schema.isPattern(/^#[0-9a-fA-F]{6}$/)),
|
Schema.String.check(Schema.isPattern(/^#[0-9a-fA-F]{6}$/)),
|
||||||
Schema.Literals(["primary", "secondary", "accent", "success", "warning", "error", "info"]),
|
Schema.Literals(["primary", "secondary", "accent", "success", "warning", "error", "info"]),
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ import { Context, Duration, Effect, Exit, Fiber, Layer, Option, Schema } from "e
|
|||||||
import { EffectFlock } from "@opencode-ai/shared/util/effect-flock"
|
import { EffectFlock } from "@opencode-ai/shared/util/effect-flock"
|
||||||
import { InstanceRef } from "@/effect/instance-ref"
|
import { InstanceRef } from "@/effect/instance-ref"
|
||||||
import { zod, ZodOverride } from "@/util/effect-zod"
|
import { zod, ZodOverride } from "@/util/effect-zod"
|
||||||
import { withStatics } from "@/util/schema"
|
import { NonNegativeInt, PositiveInt, withStatics } from "@/util/schema"
|
||||||
import { ConfigAgent } from "./agent"
|
import { ConfigAgent } from "./agent"
|
||||||
import { ConfigCommand } from "./command"
|
import { ConfigCommand } from "./command"
|
||||||
import { ConfigFormatter } from "./formatter"
|
import { ConfigFormatter } from "./formatter"
|
||||||
@@ -88,9 +88,6 @@ export type Layout = ConfigLayout.Layout
|
|||||||
const AgentRef = Schema.Any.annotate({ [ZodOverride]: ConfigAgent.Info })
|
const AgentRef = Schema.Any.annotate({ [ZodOverride]: ConfigAgent.Info })
|
||||||
const LogLevelRef = Schema.Any.annotate({ [ZodOverride]: Log.Level })
|
const LogLevelRef = Schema.Any.annotate({ [ZodOverride]: Log.Level })
|
||||||
|
|
||||||
const PositiveInt = Schema.Number.check(Schema.isInt()).check(Schema.isGreaterThan(0))
|
|
||||||
const NonNegativeInt = Schema.Number.check(Schema.isInt()).check(Schema.isGreaterThanOrEqualTo(0))
|
|
||||||
|
|
||||||
// The Effect Schema is the canonical source of truth. The `.zod` compatibility
|
// The Effect Schema is the canonical source of truth. The `.zod` compatibility
|
||||||
// surface is derived so existing Hono validators keep working without a parallel
|
// surface is derived so existing Hono validators keep working without a parallel
|
||||||
// Zod definition.
|
// Zod definition.
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
import { Schema } from "effect"
|
import { Schema } from "effect"
|
||||||
import { zod } from "@/util/effect-zod"
|
import { zod } from "@/util/effect-zod"
|
||||||
import { withStatics } from "@/util/schema"
|
import { PositiveInt, withStatics } from "@/util/schema"
|
||||||
|
|
||||||
const PositiveInt = Schema.Number.check(Schema.isInt()).check(Schema.isGreaterThan(0))
|
|
||||||
|
|
||||||
export const Model = Schema.Struct({
|
export const Model = Schema.Struct({
|
||||||
id: Schema.optional(Schema.String),
|
id: Schema.optional(Schema.String),
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { Schema } from "effect"
|
import { Schema } from "effect"
|
||||||
import { zod } from "@/util/effect-zod"
|
import { zod } from "@/util/effect-zod"
|
||||||
import { withStatics } from "@/util/schema"
|
import { PositiveInt, withStatics } from "@/util/schema"
|
||||||
|
|
||||||
export const Server = Schema.Struct({
|
export const Server = Schema.Struct({
|
||||||
port: Schema.optional(Schema.Number.check(Schema.isInt()).check(Schema.isGreaterThan(0))).annotate({
|
port: Schema.optional(PositiveInt).annotate({
|
||||||
description: "Port to listen on",
|
description: "Port to listen on",
|
||||||
}),
|
}),
|
||||||
hostname: Schema.optional(Schema.String).annotate({ description: "Hostname to listen on" }),
|
hostname: Schema.optional(Schema.String).annotate({ description: "Hostname to listen on" }),
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import type { Provider } from "@/provider"
|
|||||||
import { ModelID, ProviderID } from "@/provider/schema"
|
import { ModelID, ProviderID } from "@/provider/schema"
|
||||||
import { Effect, Schema, Types } from "effect"
|
import { Effect, Schema, Types } from "effect"
|
||||||
import { zod, ZodOverride } from "@/util/effect-zod"
|
import { zod, ZodOverride } from "@/util/effect-zod"
|
||||||
import { withStatics } from "@/util/schema"
|
import { NonNegativeInt, withStatics } from "@/util/schema"
|
||||||
import { namedSchemaError } from "@/util/named-schema-error"
|
import { namedSchemaError } from "@/util/named-schema-error"
|
||||||
import { EffectLogger } from "@/effect"
|
import { EffectLogger } from "@/effect"
|
||||||
|
|
||||||
@@ -64,9 +64,7 @@ export class OutputFormatText extends Schema.Class<OutputFormatText>("OutputForm
|
|||||||
export class OutputFormatJsonSchema extends Schema.Class<OutputFormatJsonSchema>("OutputFormatJsonSchema")({
|
export class OutputFormatJsonSchema extends Schema.Class<OutputFormatJsonSchema>("OutputFormatJsonSchema")({
|
||||||
type: Schema.Literal("json_schema"),
|
type: Schema.Literal("json_schema"),
|
||||||
schema: Schema.Record(Schema.String, Schema.Any).annotate({ identifier: "JSONSchema" }),
|
schema: Schema.Record(Schema.String, Schema.Any).annotate({ identifier: "JSONSchema" }),
|
||||||
retryCount: Schema.Number.check(Schema.isInt())
|
retryCount: NonNegativeInt.pipe(Schema.optional, Schema.withDecodingDefault(Effect.succeed(2))),
|
||||||
.check(Schema.isGreaterThanOrEqualTo(0))
|
|
||||||
.pipe(Schema.optional, Schema.withDecodingDefault(Effect.succeed(2))),
|
|
||||||
}) {
|
}) {
|
||||||
static readonly zod = zod(this)
|
static readonly zod = zod(this)
|
||||||
}
|
}
|
||||||
@@ -138,8 +136,8 @@ export type ReasoningPart = Types.DeepMutable<Schema.Schema.Type<typeof Reasonin
|
|||||||
const filePartSourceBase = {
|
const filePartSourceBase = {
|
||||||
text: Schema.Struct({
|
text: Schema.Struct({
|
||||||
value: Schema.String,
|
value: Schema.String,
|
||||||
start: Schema.Number.check(Schema.isInt()),
|
start: Schema.Int,
|
||||||
end: Schema.Number.check(Schema.isInt()),
|
end: Schema.Int,
|
||||||
}).annotate({ identifier: "FilePartSourceText" }),
|
}).annotate({ identifier: "FilePartSourceText" }),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,7 +155,7 @@ export const SymbolSource = Schema.Struct({
|
|||||||
path: Schema.String,
|
path: Schema.String,
|
||||||
range: LSP.Range,
|
range: LSP.Range,
|
||||||
name: Schema.String,
|
name: Schema.String,
|
||||||
kind: Schema.Number.check(Schema.isInt()),
|
kind: Schema.Int,
|
||||||
})
|
})
|
||||||
.annotate({ identifier: "SymbolSource" })
|
.annotate({ identifier: "SymbolSource" })
|
||||||
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
||||||
@@ -196,8 +194,8 @@ export const AgentPart = Schema.Struct({
|
|||||||
source: Schema.optional(
|
source: Schema.optional(
|
||||||
Schema.Struct({
|
Schema.Struct({
|
||||||
value: Schema.String,
|
value: Schema.String,
|
||||||
start: Schema.Number.check(Schema.isInt()),
|
start: Schema.Int,
|
||||||
end: Schema.Number.check(Schema.isInt()),
|
end: Schema.Int,
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
@@ -501,8 +499,8 @@ export const AgentPartInput = Schema.Struct({
|
|||||||
source: Schema.optional(
|
source: Schema.optional(
|
||||||
Schema.Struct({
|
Schema.Struct({
|
||||||
value: Schema.String,
|
value: Schema.String,
|
||||||
start: Schema.Number.check(Schema.isInt()),
|
start: Schema.Int,
|
||||||
end: Schema.Number.check(Schema.isInt()),
|
end: Schema.Int,
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,5 +1,15 @@
|
|||||||
import { Schema } from "effect"
|
import { Schema } from "effect"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Integer greater than zero.
|
||||||
|
*/
|
||||||
|
export const PositiveInt = Schema.Int.check(Schema.isGreaterThan(0))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Integer greater than or equal to zero.
|
||||||
|
*/
|
||||||
|
export const NonNegativeInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attach static methods to a schema object. Designed to be used with `.pipe()`:
|
* Attach static methods to a schema object. Designed to be used with `.pipe()`:
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user