fix: more durable @ references for commands (#2386)

This commit is contained in:
Aiden Cline
2025-09-02 21:24:56 -05:00
committed by GitHub
parent 751b81af34
commit f740663ded
3 changed files with 69 additions and 93 deletions

View File

@@ -1,48 +0,0 @@
import os from "os"
import path from "path"
/**
* Regular expression to match @ file references in text
* Matches @ followed by file paths, excluding commas, periods at end of sentences, and backticks
* Does not match when preceded by word characters or backticks (to avoid email addresses and quoted references)
*/
export const fileRegex = /(?<![\w`])@(\.?[^\s`,.]*(?:\.[^\s`,.]+)*)/g
/**
* File part type for chat input
*/
export type FilePart = {
type: "file"
url: string
filename: string
mime: string
}
/**
* Processes file references in a template string and returns file parts
* @param template - The template string containing @file references
* @param basePath - The base path to resolve relative file paths against
* @returns Array of file parts for the chat input
*/
export function processFileReferences(template: string, basePath: string): FilePart[] {
// intentionally doing match regex doing bash regex replacements
// this is because bash commands can output "@" references
const matches = template.matchAll(fileRegex)
const parts: FilePart[] = []
for (const match of matches) {
const filename = match[1]
const filepath = filename.startsWith("~/")
? path.join(os.homedir(), filename.slice(2))
: path.resolve(basePath, filename)
parts.push({
type: "file",
url: `file://${filepath}`,
filename,
mime: "text/plain",
})
}
return parts
}

View File

@@ -1,4 +1,6 @@
import os from "os"
import path from "path"
import fs from "fs/promises"
import { spawn } from "child_process"
import { Decimal } from "decimal.js"
import { z, ZodSchema } from "zod"
@@ -50,7 +52,6 @@ import { ulid } from "ulid"
import { defer } from "../util/defer"
import { Command } from "../command"
import { $ } from "bun"
import { processFileReferences } from "./file-reference"
export namespace Session {
const log = Log.create({ service: "session" })
@@ -1229,6 +1230,12 @@ export namespace Session {
})
export type CommandInput = z.infer<typeof CommandInput>
const bashRegex = /!`([^`]+)`/g
/**
* Regular expression to match @ file references in text
* Matches @ followed by file paths, excluding commas, periods at end of sentences, and backticks
* Does not match when preceded by word characters or backticks (to avoid email addresses and quoted references)
*/
export const fileRegex = /(?<![\w`])@(\.?[^\s`,.]*(?:\.[^\s`,.]+)*)/g
export async function command(input: CommandInput) {
log.info("command", input)
@@ -1259,8 +1266,36 @@ export namespace Session {
},
] as ChatInput["parts"]
const fileReferenceParts = processFileReferences(template, Instance.worktree)
parts.push(...fileReferenceParts)
const matches = Array.from(template.matchAll(fileRegex))
await Promise.all(
matches.map(async (match) => {
const name = match[1]
const filepath = name.startsWith("~/")
? path.join(os.homedir(), name.slice(2))
: path.resolve(Instance.worktree, name)
const stats = await fs.stat(filepath).catch(() => undefined)
if (!stats) {
const agent = await Agent.get(name)
if (agent) {
parts.push({
type: "agent",
name: agent.name,
})
}
return
}
if (stats.isDirectory()) return
parts.push({
type: "file",
url: `file://${filepath}`,
filename: name,
mime: "text/plain",
})
}),
)
return prompt({
sessionID: input.sessionID,