Skip to content

Commit f171250

Browse files
committed
fix: better file/content return
1 parent d440ba3 commit f171250

4 files changed

Lines changed: 61 additions & 18 deletions

File tree

packages/opencode/src/file/index.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { z } from "zod"
22
import { Bus } from "../bus"
33
import { $ } from "bun"
4-
import { createPatch } from "diff"
4+
import { formatPatch, structuredPatch } from "diff"
55
import path from "path"
66
import fs from "fs"
77
import ignore from "ignore"
@@ -28,6 +28,7 @@ export namespace File {
2828
.object({
2929
name: z.string(),
3030
path: z.string(),
31+
absolute: z.string(),
3132
type: z.enum(["file", "directory"]),
3233
ignored: z.boolean(),
3334
})
@@ -36,6 +37,34 @@ export namespace File {
3637
})
3738
export type Node = z.infer<typeof Node>
3839

40+
export const Content = z
41+
.object({
42+
content: z.string(),
43+
diff: z.string().optional(),
44+
patch: z
45+
.object({
46+
oldFileName: z.string(),
47+
newFileName: z.string(),
48+
oldHeader: z.string().optional(),
49+
newHeader: z.string().optional(),
50+
hunks: z.array(
51+
z.object({
52+
oldStart: z.number(),
53+
oldLines: z.number(),
54+
newStart: z.number(),
55+
newLines: z.number(),
56+
lines: z.array(z.string()),
57+
}),
58+
),
59+
index: z.string().optional(),
60+
})
61+
.optional(),
62+
})
63+
.openapi({
64+
ref: "FileContent",
65+
})
66+
export type Content = z.infer<typeof Content>
67+
3968
export const Event = {
4069
Edited: Bus.event(
4170
"file.edited",
@@ -127,13 +156,14 @@ export namespace File {
127156
const diff = await $`git diff ${file}`.cwd(Instance.directory).quiet().nothrow().text()
128157
if (diff.trim()) {
129158
const original = await $`git show HEAD:${file}`.cwd(Instance.directory).quiet().nothrow().text()
130-
const patch = createPatch(file, original, content, "old", "new", {
159+
const diff = structuredPatch(file, file, original, content, "old", "new", {
131160
context: Infinity,
132161
})
133-
return { type: "patch", content: patch }
162+
const patch = formatPatch(diff)
163+
return { content, patch, diff }
134164
}
135165
}
136-
return { type: "raw", content }
166+
return { content }
137167
}
138168

139169
export async function list(dir?: string) {
@@ -157,6 +187,7 @@ export namespace File {
157187
nodes.push({
158188
name: entry.name,
159189
path: relativePath,
190+
absolute: fullPath,
160191
type,
161192
ignored: ignored(type === "directory" ? relativePath + "/" : relativePath),
162193
})

packages/opencode/src/server/server.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -974,12 +974,7 @@ export namespace Server {
974974
description: "File content",
975975
content: {
976976
"application/json": {
977-
schema: resolver(
978-
z.object({
979-
type: z.enum(["raw", "patch"]),
980-
content: z.string(),
981-
}),
982-
),
977+
schema: resolver(File.Content),
983978
},
984979
},
985980
},

packages/opencode/src/session/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,10 @@ export namespace Session {
527527
break
528528
}
529529
}
530-
offset = Math.max(start - 2, 0)
531-
if (end) {
532-
limit = end - offset + 2
533-
}
530+
}
531+
offset = Math.max(start - 1, 0)
532+
if (end) {
533+
limit = end - offset
534534
}
535535
}
536536
const args = { filePath, offset, limit }

packages/sdk/js/src/gen/types.gen.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,10 +1117,30 @@ export type Symbol = {
11171117
export type FileNode = {
11181118
name: string
11191119
path: string
1120+
absolute: string
11201121
type: "file" | "directory"
11211122
ignored: boolean
11221123
}
11231124

1125+
export type FileContent = {
1126+
content: string
1127+
diff?: string
1128+
patch?: {
1129+
oldFileName: string
1130+
newFileName: string
1131+
oldHeader?: string
1132+
newHeader?: string
1133+
hunks: Array<{
1134+
oldStart: number
1135+
oldLines: number
1136+
newStart: number
1137+
newLines: number
1138+
lines: Array<string>
1139+
}>
1140+
index?: string
1141+
}
1142+
}
1143+
11241144
export type File = {
11251145
path: string
11261146
added: number
@@ -1893,10 +1913,7 @@ export type FileReadResponses = {
18931913
/**
18941914
* File content
18951915
*/
1896-
200: {
1897-
type: "raw" | "patch"
1898-
content: string
1899-
}
1916+
200: FileContent
19001917
}
19011918

19021919
export type FileReadResponse = FileReadResponses[keyof FileReadResponses]

0 commit comments

Comments
 (0)