refactor(ripgrep): migrate result schemas to effect (#24213)

This commit is contained in:
Kit Langton
2026-04-24 17:42:52 -04:00
committed by GitHub
parent bb3509b5ff
commit 5a04de231e
2 changed files with 63 additions and 66 deletions
+62 -65
View File
@@ -1,7 +1,6 @@
import path from "path" import path from "path"
import z from "zod"
import { AppFileSystem } from "@opencode-ai/shared/filesystem" import { AppFileSystem } from "@opencode-ai/shared/filesystem"
import { Cause, Context, Effect, Fiber, Layer, Queue, Stream } from "effect" import { Cause, Context, Effect, Fiber, Layer, Queue, Schema, Stream } from "effect"
import type { PlatformError } from "effect/PlatformError" import type { PlatformError } from "effect/PlatformError"
import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/unstable/http" import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/unstable/http"
import { ChildProcess } from "effect/unstable/process" import { ChildProcess } from "effect/unstable/process"
@@ -12,6 +11,8 @@ import { Global } from "@/global"
import { Log } from "@/util" import { Log } from "@/util"
import { sanitizedProcessEnv } from "@/util/opencode-process" import { sanitizedProcessEnv } from "@/util/opencode-process"
import { which } from "@/util/which" import { which } from "@/util/which"
import { zod } from "@/util/effect-zod"
import { withStatics } from "@/util/schema"
const log = Log.create({ service: "ripgrep" }) const log = Log.create({ service: "ripgrep" })
const VERSION = "15.1.0" const VERSION = "15.1.0"
@@ -25,83 +26,82 @@ const PLATFORM = {
"x64-win32": { platform: "x86_64-pc-windows-msvc", extension: "zip" }, "x64-win32": { platform: "x86_64-pc-windows-msvc", extension: "zip" },
} as const } as const
const Stats = z.object({ const TimeStats = Schema.Struct({
elapsed: z.object({ secs: Schema.Number,
secs: z.number(), nanos: Schema.Number,
nanos: z.number(), human: Schema.String,
human: z.string(),
}),
searches: z.number(),
searches_with_match: z.number(),
bytes_searched: z.number(),
bytes_printed: z.number(),
matched_lines: z.number(),
matches: z.number(),
}) })
const Begin = z.object({ const Stats = Schema.Struct({
type: z.literal("begin"), elapsed: TimeStats,
data: z.object({ searches: Schema.Number,
path: z.object({ searches_with_match: Schema.Number,
text: z.string(), bytes_searched: Schema.Number,
}), bytes_printed: Schema.Number,
matched_lines: Schema.Number,
matches: Schema.Number,
})
const PathText = Schema.Struct({
text: Schema.String,
})
const Begin = Schema.Struct({
type: Schema.Literal("begin"),
data: Schema.Struct({
path: PathText,
}), }),
}) })
export const Match = z.object({ export const SearchMatch = Schema.Struct({
type: z.literal("match"), path: PathText,
data: z.object({ lines: Schema.Struct({
path: z.object({ text: Schema.String,
text: z.string(), }),
}), line_number: Schema.Number,
lines: z.object({ absolute_offset: Schema.Number,
text: z.string(), submatches: Schema.Array(
}), Schema.Struct({
line_number: z.number(), match: Schema.Struct({
absolute_offset: z.number(), text: Schema.String,
submatches: z.array(
z.object({
match: z.object({
text: z.string(),
}),
start: z.number(),
end: z.number(),
}), }),
), start: Schema.Number,
}), end: Schema.Number,
}),
),
}).pipe(withStatics((s) => ({ zod: zod(s) })))
export const Match = Schema.Struct({
type: Schema.Literal("match"),
data: SearchMatch,
}) })
const End = z.object({ const End = Schema.Struct({
type: z.literal("end"), type: Schema.Literal("end"),
data: z.object({ data: Schema.Struct({
path: z.object({ path: PathText,
text: z.string(), binary_offset: Schema.NullOr(Schema.Number),
}),
binary_offset: z.number().nullable(),
stats: Stats, stats: Stats,
}), }),
}) })
const Summary = z.object({ const Summary = Schema.Struct({
type: z.literal("summary"), type: Schema.Literal("summary"),
data: z.object({ data: Schema.Struct({
elapsed_total: z.object({ elapsed_total: TimeStats,
human: z.string(),
nanos: z.number(),
secs: z.number(),
}),
stats: Stats, stats: Stats,
}), }),
}) })
const Result = z.union([Begin, Match, End, Summary]) const Result = Schema.Union([Begin, Match, End, Summary])
const decodeResult = Schema.decodeUnknownEffect(Schema.fromJsonString(Result))
export type Result = z.infer<typeof Result> export type Result = Schema.Schema.Type<typeof Result>
export type Match = z.infer<typeof Match> export type Match = Schema.Schema.Type<typeof Match>
export type Item = Match["data"] export type Item = Match["data"]
export type Begin = z.infer<typeof Begin> export type Begin = Schema.Schema.Type<typeof Begin>
export type End = z.infer<typeof End> export type End = Schema.Schema.Type<typeof End>
export type Summary = z.infer<typeof Summary> export type Summary = Schema.Schema.Type<typeof Summary>
export type Row = Match["data"] export type Row = Match["data"]
export interface SearchResult { export interface SearchResult {
@@ -187,10 +187,7 @@ function row(data: Row): Row {
} }
function parse(line: string) { function parse(line: string) {
return Effect.try({ return decodeResult(line).pipe(Effect.mapError((cause) => new Error("invalid ripgrep output", { cause })))
try: () => Result.parse(JSON.parse(line)),
catch: (cause) => new Error("invalid ripgrep output", { cause }),
})
} }
function fail(queue: Queue.Queue<string, PlatformError | Error | Cause.Done>, err: PlatformError | Error) { function fail(queue: Queue.Queue<string, PlatformError | Error | Cause.Done>, err: PlatformError | Error) {
@@ -21,7 +21,7 @@ export const FileRoutes = lazy(() =>
description: "Matches", description: "Matches",
content: { content: {
"application/json": { "application/json": {
schema: resolver(Ripgrep.Match.shape.data.array()), schema: resolver(Ripgrep.SearchMatch.zod.array()),
}, },
}, },
}, },