make @file references in custom slash commands more robust (#2203)

Co-authored-by: Adam Spiers <opencode@adamspiers.org>
Co-authored-by: rekram1-node <aidenpcline@gmail.com>
This commit is contained in:
Adam Spiers
2025-09-02 03:14:27 +01:00
committed by GitHub
parent a9875c5531
commit 47d4c87bdd
3 changed files with 153 additions and 19 deletions

View File

@@ -0,0 +1,48 @@
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,5 +1,4 @@
import path from "path"
import os from "os"
import { spawn } from "child_process"
import { Decimal } from "decimal.js"
import { z, ZodSchema } from "zod"
@@ -51,6 +50,7 @@ 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,7 +1229,6 @@ export namespace Session {
})
export type CommandInput = z.infer<typeof CommandInput>
const bashRegex = /!`([^`]+)`/g
const fileRegex = /@([^\s]+)/g
export async function command(input: CommandInput) {
log.info("command", input)
@@ -1238,10 +1237,6 @@ export namespace Session {
let template = command.template.replace("$ARGUMENTS", input.arguments)
// intentionally doing match regex doing bash regex replacements
// this is because bash commands can output "@" references
const fileMatches = template.matchAll(fileRegex)
const bash = Array.from(template.matchAll(bashRegex))
if (bash.length > 0) {
const results = await Promise.all(
@@ -1264,19 +1259,8 @@ export namespace Session {
},
] as ChatInput["parts"]
for (const match of fileMatches) {
const filename = match[1]
const filepath = filename.startsWith("~/")
? path.join(os.homedir(), filename.slice(2))
: path.join(Instance.worktree, filename)
parts.push({
type: "file",
url: `file://${filepath}`,
filename,
mime: "text/plain",
})
}
const fileReferenceParts = processFileReferences(template, Instance.worktree)
parts.push(...fileReferenceParts)
return prompt({
sessionID: input.sessionID,