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 { z } from "zod"
import { Bus } from "../bus" import { Bus } from "../bus"
import { $ } from "bun" import { $ } from "bun"
import { createPatch } from "diff" import { formatPatch, structuredPatch } from "diff"
import path from "path" import path from "path"
import fs from "fs" import fs from "fs"
import ignore from "ignore" import ignore from "ignore"
@@ -28,6 +28,7 @@ export namespace File {
.object({ .object({
name: z.string(), name: z.string(),
path: z.string(), path: z.string(),
absolute: z.string(),
type: z.enum(["file", "directory"]), type: z.enum(["file", "directory"]),
ignored: z.boolean(), ignored: z.boolean(),
}) })
@@ -36,6 +37,34 @@ export namespace File {
}) })
export type Node = z.infer<typeof Node> 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 = { export const Event = {
Edited: Bus.event( Edited: Bus.event(
"file.edited", "file.edited",
@@ -127,13 +156,14 @@ export namespace File {
const diff = await $`git diff ${file}`.cwd(Instance.directory).quiet().nothrow().text() const diff = await $`git diff ${file}`.cwd(Instance.directory).quiet().nothrow().text()
if (diff.trim()) { if (diff.trim()) {
const original = await $`git show HEAD:${file}`.cwd(Instance.directory).quiet().nothrow().text() 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, 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) { export async function list(dir?: string) {
@@ -157,6 +187,7 @@ export namespace File {
nodes.push({ nodes.push({
name: entry.name, name: entry.name,
path: relativePath, path: relativePath,
absolute: fullPath,
type, type,
ignored: ignored(type === "directory" ? relativePath + "/" : relativePath), ignored: ignored(type === "directory" ? relativePath + "/" : relativePath),
}) })

View File

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

View File

@@ -527,10 +527,10 @@ export namespace Session {
break 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 } const args = { filePath, offset, limit }

View File

@@ -1117,10 +1117,30 @@ export type Symbol = {
export type FileNode = { export type FileNode = {
name: string name: string
path: string path: string
absolute: string
type: "file" | "directory" type: "file" | "directory"
ignored: boolean 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 = { export type File = {
path: string path: string
added: number added: number
@@ -1893,10 +1913,7 @@ export type FileReadResponses = {
/** /**
* File content * File content
*/ */
200: { 200: FileContent
type: "raw" | "patch"
content: string
}
} }
export type FileReadResponse = FileReadResponses[keyof FileReadResponses] export type FileReadResponse = FileReadResponses[keyof FileReadResponses]