Skip to content

Commit 0bcf734

Browse files
authored
migrate Snapshot schemas to Effect Schema (#23747)
1 parent b1c3095 commit 0bcf734

5 files changed

Lines changed: 31 additions & 29 deletions

File tree

packages/opencode/src/server/routes/instance/session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ export const SessionRoutes = lazy(() =>
471471
description: "Successfully retrieved diff",
472472
content: {
473473
"application/json": {
474-
schema: resolver(Snapshot.FileDiff.array()),
474+
schema: resolver(Snapshot.FileDiff.zod.array()),
475475
},
476476
},
477477
},

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ export const User = Base.extend({
366366
.object({
367367
title: z.string().optional(),
368368
body: z.string().optional(),
369-
diffs: Snapshot.FileDiff.array(),
369+
diffs: Snapshot.FileDiff.zod.array(),
370370
})
371371
.optional(),
372372
agent: z.string(),

packages/opencode/src/session/session.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export const Info = z
127127
additions: z.number(),
128128
deletions: z.number(),
129129
files: z.number(),
130-
diffs: Snapshot.FileDiff.array().optional(),
130+
diffs: Snapshot.FileDiff.zod.array().optional(),
131131
})
132132
.optional(),
133133
share: z
@@ -239,7 +239,7 @@ export const Event = {
239239
"session.diff",
240240
z.object({
241241
sessionID: SessionID.zod,
242-
diff: Snapshot.FileDiff.array(),
242+
diff: Snapshot.FileDiff.zod.array(),
243243
}),
244244
),
245245
Error: BusEvent.define(

packages/opencode/src/snapshot/index.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Cause, Duration, Effect, Layer, Schedule, Semaphore, Context, Stream } from "effect"
1+
import { Cause, Duration, Effect, Layer, Schedule, Schema, Semaphore, Context, Stream } from "effect"
22
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
33
import { formatPatch, structuredPatch } from "diff"
44
import path from "path"
@@ -10,25 +10,25 @@ import { Hash } from "@opencode-ai/shared/util/hash"
1010
import { Config } from "../config"
1111
import { Global } from "../global"
1212
import { Log } from "../util"
13-
14-
export const Patch = z.object({
15-
hash: z.string(),
16-
files: z.string().array(),
13+
import { withStatics } from "@/util/schema"
14+
import { zod } from "@/util/effect-zod"
15+
16+
export const Patch = Schema.Struct({
17+
hash: Schema.String,
18+
files: Schema.mutable(Schema.Array(Schema.String)),
19+
}).pipe(withStatics((s) => ({ zod: zod(s) })))
20+
export type Patch = typeof Patch.Type
21+
22+
export const FileDiff = Schema.Struct({
23+
file: Schema.String,
24+
patch: Schema.String,
25+
additions: Schema.Number,
26+
deletions: Schema.Number,
27+
status: Schema.optional(Schema.Literals(["added", "deleted", "modified"])),
1728
})
18-
export type Patch = z.infer<typeof Patch>
19-
20-
export const FileDiff = z
21-
.object({
22-
file: z.string(),
23-
patch: z.string(),
24-
additions: z.number(),
25-
deletions: z.number(),
26-
status: z.enum(["added", "deleted", "modified"]).optional(),
27-
})
28-
.meta({
29-
ref: "SnapshotFileDiff",
30-
})
31-
export type FileDiff = z.infer<typeof FileDiff>
29+
.annotate({ identifier: "SnapshotFileDiff" })
30+
.pipe(withStatics((s) => ({ zod: zod(s) })))
31+
export type FileDiff = typeof FileDiff.Type
3232

3333
const log = Log.create({ service: "snapshot" })
3434
const prune = "7.days"

packages/opencode/src/tool/edit.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,17 @@ export const EditTool = Tool.define(
153153
}).pipe(Effect.orDie),
154154
)
155155

156+
let additions = 0
157+
let deletions = 0
158+
for (const change of diffLines(contentOld, contentNew)) {
159+
if (change.added) additions += change.count || 0
160+
if (change.removed) deletions += change.count || 0
161+
}
156162
const filediff: Snapshot.FileDiff = {
157163
file: filePath,
158164
patch: diff,
159-
additions: 0,
160-
deletions: 0,
161-
}
162-
for (const change of diffLines(contentOld, contentNew)) {
163-
if (change.added) filediff.additions += change.count || 0
164-
if (change.removed) filediff.deletions += change.count || 0
165+
additions,
166+
deletions,
165167
}
166168

167169
yield* ctx.metadata({

0 commit comments

Comments
 (0)