mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 05:01:03 +03:00
fix(core): preserve a single blank line in paged reads (#63809)
This commit is contained in:
committed by
GitHub
Unverified
parent
b3c1936a97
commit
2396f7b972
@@ -348,7 +348,8 @@ export const toModelContent = (path: string, offset: number | undefined, output:
|
||||
const start = output.type === "text-page" ? output.offset : 1
|
||||
// Pages already join selected lines; a trailing newline represents a selected blank line.
|
||||
const text = output.type === "file" ? output.content.replace(/\n$/, "") : output.content
|
||||
const lines = output.content === "" ? [] : text.split("\n")
|
||||
const empty = output.type === "text-page" ? output.lineCount === 0 : output.content === ""
|
||||
const lines = empty ? [] : text.split("\n")
|
||||
// A page that starts before the requested offset is the file's tail, returned for an offset past the end.
|
||||
const total = start + lines.length - 1
|
||||
const pastEnd = output.type === "text-page" && offset !== undefined && start < offset
|
||||
|
||||
@@ -82,6 +82,7 @@ export type FileContent = typeof FileContent.Type
|
||||
export class TextPage extends Schema.Class<TextPage>("ReadTool.TextPage")({
|
||||
type: Schema.Literal("text-page"),
|
||||
content: Schema.String,
|
||||
lineCount: NonNegativeInt,
|
||||
mime: Schema.String,
|
||||
offset: PositiveInt,
|
||||
truncated: Schema.Boolean,
|
||||
@@ -332,6 +333,7 @@ const textPage = Effect.fnUntraced(function* (
|
||||
return new TextPage({
|
||||
type: "text-page",
|
||||
content: entries.join("\n"),
|
||||
lineCount: entries.length,
|
||||
mime: mimeType(input),
|
||||
offset: from,
|
||||
truncated: next !== undefined,
|
||||
|
||||
@@ -90,9 +90,30 @@ describe("ReadTool text serialization", () => {
|
||||
name: "preserves empty-file page output",
|
||||
content: "",
|
||||
page: { limit: 2 },
|
||||
output: { type: "text-page", content: "", offset: 1, truncated: false },
|
||||
output: { type: "text-page", content: "", lineCount: 0, offset: 1, truncated: false },
|
||||
model: "Read file lines.txt, 0 lines",
|
||||
},
|
||||
{
|
||||
name: "numbers the only blank line in a newline-only file",
|
||||
content: "\n",
|
||||
page: { limit: 1 },
|
||||
output: { type: "text-page", content: "", lineCount: 1, offset: 1, truncated: false },
|
||||
model: "Read file lines.txt, lines 1-1\n1: ",
|
||||
},
|
||||
{
|
||||
name: "numbers a single blank line before continuation",
|
||||
content: "alpha\n\nomega\n",
|
||||
page: { offset: 2, limit: 1 },
|
||||
output: { type: "text-page", content: "", lineCount: 1, offset: 2, truncated: true, next: 3 },
|
||||
model: "Read file lines.txt, lines 2-2\n2: \n[Output truncated. Continue reading with offset: 3]",
|
||||
},
|
||||
{
|
||||
name: "counts the last blank line when an offset is past EOF",
|
||||
content: "alpha\n\n",
|
||||
page: { offset: 9, limit: 1 },
|
||||
output: { type: "text-page", content: "", lineCount: 1, offset: 2, truncated: false },
|
||||
model: "Read file lines.txt, lines 2-2 (offset 9 is past the end; the file has 2 lines)\n2: ",
|
||||
},
|
||||
]
|
||||
|
||||
cases.forEach((input) => {
|
||||
@@ -105,7 +126,7 @@ describe("ReadTool text serialization", () => {
|
||||
const result = yield* ReadToolFileSystem.read(current.environment, file, "lines.txt", input.page)
|
||||
|
||||
expect(result).toMatchObject(input.output)
|
||||
expect(ReadTool.toModelContent("lines.txt", undefined, result)).toBe(input.model)
|
||||
expect(ReadTool.toModelContent("lines.txt", input.page.offset, result)).toBe(input.model)
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
@@ -1130,6 +1130,7 @@ describe("ReadTool", () => {
|
||||
readResult = new ReadToolFileSystem.TextPage({
|
||||
type: "text-page",
|
||||
content: "hello",
|
||||
lineCount: 1,
|
||||
mime: "text/plain",
|
||||
offset: 2,
|
||||
truncated: true,
|
||||
@@ -1170,6 +1171,7 @@ describe("ReadTool", () => {
|
||||
readResult = new ReadToolFileSystem.TextPage({
|
||||
type: "text-page",
|
||||
content: "nine\nten",
|
||||
lineCount: 2,
|
||||
mime: "text/plain",
|
||||
offset: 9,
|
||||
truncated: true,
|
||||
|
||||
Reference in New Issue
Block a user