fix: better file/content return

This commit is contained in:
Adam
2025-09-04 12:39:34 -05:00
parent d440ba32ab
commit f171250033
4 changed files with 61 additions and 18 deletions

View File

@@ -1,7 +1,7 @@
import { z } from "zod"
import { Bus } from "../bus"
import { $ } from "bun"
import { createPatch } from "diff"
import { formatPatch, structuredPatch } from "diff"
import path from "path"
import fs from "fs"
import ignore from "ignore"
@@ -28,6 +28,7 @@ export namespace File {
.object({
name: z.string(),
path: z.string(),
absolute: z.string(),
type: z.enum(["file", "directory"]),
ignored: z.boolean(),
})
@@ -36,6 +37,34 @@ export namespace File {
})
export type Node = z.infer<typeof Node>
export const Content = z
.object({
content: z.string(),
diff: z.string().optional(),
patch: z
.object({
oldFileName: z.string(),
newFileName: z.string(),
oldHeader: z.string().optional(),
newHeader: z.string().optional(),
hunks: z.array(
z.object({
oldStart: z.number(),
oldLines: z.number(),
newStart: z.number(),
newLines: z.number(),
lines: z.array(z.string()),
}),
),
index: z.string().optional(),
})
.optional(),
})
.openapi({
ref: "FileContent",
})
export type Content = z.infer<typeof Content>
export const Event = {
Edited: Bus.event(
"file.edited",
@@ -127,13 +156,14 @@ export namespace File {
const diff = await $`git diff ${file}`.cwd(Instance.directory).quiet().nothrow().text()
if (diff.trim()) {
const original = await $`git show HEAD:${file}`.cwd(Instance.directory).quiet().nothrow().text()
const patch = createPatch(file, original, content, "old", "new", {
const diff = structuredPatch(file, file, original, content, "old", "new", {
context: Infinity,
})
return { type: "patch", content: patch }
const patch = formatPatch(diff)
return { content, patch, diff }
}
}
return { type: "raw", content }
return { content }
}
export async function list(dir?: string) {
@@ -157,6 +187,7 @@ export namespace File {
nodes.push({
name: entry.name,
path: relativePath,
absolute: fullPath,
type,
ignored: ignored(type === "directory" ? relativePath + "/" : relativePath),
})

View File

@@ -974,12 +974,7 @@ export namespace Server {
description: "File content",
content: {
"application/json": {
schema: resolver(
z.object({
type: z.enum(["raw", "patch"]),
content: z.string(),
}),
),
schema: resolver(File.Content),
},
},
},

View File

@@ -527,10 +527,10 @@ export namespace Session {
break
}
}
offset = Math.max(start - 2, 0)
if (end) {
limit = end - offset + 2
}
}
offset = Math.max(start - 1, 0)
if (end) {
limit = end - offset
}
}
const args = { filePath, offset, limit }

View File

@@ -1117,10 +1117,30 @@ export type Symbol = {
export type FileNode = {
name: string
path: string
absolute: string
type: "file" | "directory"
ignored: boolean
}
export type FileContent = {
content: string
diff?: string
patch?: {
oldFileName: string
newFileName: string
oldHeader?: string
newHeader?: string
hunks: Array<{
oldStart: number
oldLines: number
newStart: number
newLines: number
lines: Array<string>
}>
index?: string
}
}
export type File = {
path: string
added: number
@@ -1893,10 +1913,7 @@ export type FileReadResponses = {
/**
* File content
*/
200: {
type: "raw" | "patch"
content: string
}
200: FileContent
}
export type FileReadResponse = FileReadResponses[keyof FileReadResponses]