The open source coding agent.
TypeScript
fix: more durable @ references for commands (#2386)
Aiden Clinef740663dedc5
diff --git a/packages/opencode/src/session/file-reference.ts b/packages/opencode/src/session/file-reference.tsdeleted file mode 100644index 0d81fbf3c48..00000000000--- a/packages/opencode/src/session/file-reference.ts+++ /dev/null@@ -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-}diff --git a/packages/opencode/src/session/index.ts b/packages/opencode/src/session/index.tsindex c16ce929169..10e06acde9f 100644--- a/packages/opencode/src/session/index.ts+++ b/packages/opencode/src/session/index.ts@@ -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`,.]+)*)/gexport 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,diff --git a/packages/opencode/test/session/fileRegex.test.ts b/packages/opencode/test/session/fileRegex.test.tsindex 0d630c08dbb..201a877fc1c 100644--- a/packages/opencode/test/session/fileRegex.test.ts+++ b/packages/opencode/test/session/fileRegex.test.ts@@ -1,12 +1,8 @@-import { describe, expect, test, beforeAll, mock } from "bun:test"+import { describe, expect, test } from "bun:test"+import { Session } from "../../src/session/index"-describe("processFileReferences", () => {- let result: any-- beforeAll(async () => {- mock.module("os", () => ({ default: { homedir: () => "/home/fake-user" } }))- const { processFileReferences } = await import("../../src/session/file-reference")- const template = `This is a @valid/path/to/a/file and it should also match at+describe("fileRegex", () => {+ const template = `This is a @valid/path/to/a/file and it should also match atthe beginning of a line:@another-valid/path/to/a/file@@ -26,77 +22,70 @@ Also shouldn't forget @/absolute/paths.txt with and @/without/extensions,as well as @~/home-files and @~/paths/under/home.txt.If the reference is \`@quoted/in/backticks\` then it shouldn't match at all.`- result = processFileReferences(template, "/base")- })- test("should extract exactly 12 file references", () => {- expect(result.length).toBe(12)- })+ const matches = Array.from(template.matchAll(Session.fileRegex))- test("all files should have correct type and mime", () => {- result.forEach((file: any) => {- expect(file.type).toBe("file")- expect(file.mime).toBe("text/plain")- })+ test("should extract exactly 12 file references", () => {+ expect(matches.length).toBe(12)})test("should extract valid/path/to/a/file", () => {- expect(result[0].filename).toBe("valid/path/to/a/file")- expect(result[0].url).toBe("file:///base/valid/path/to/a/file")+ expect(matches[0][1]).toBe("valid/path/to/a/file")})test("should extract another-valid/path/to/a/file", () => {- expect(result[1].filename).toBe("another-valid/path/to/a/file")- expect(result[1].url).toBe("file:///base/another-valid/path/to/a/file")+ expect(matches[1][1]).toBe("another-valid/path/to/a/file")})test("should extract paths ignoring comma after", () => {- expect(result[2].filename).toBe("commas")- expect(result[2].url).toBe("file:///base/commas")+ expect(matches[2][1]).toBe("commas")})test("should extract a path with a file extension and comma after", () => {- expect(result[3].filename).toBe("file-extensions.md")- expect(result[3].url).toBe("file:///base/file-extensions.md")+ expect(matches[3][1]).toBe("file-extensions.md")})test("should extract a path with multiple dots and comma after", () => {- expect(result[4].filename).toBe("multiple.extensions.bak")- expect(result[4].url).toBe("file:///base/multiple.extensions.bak")+ expect(matches[4][1]).toBe("multiple.extensions.bak")})test("should extract hidden directory", () => {- expect(result[5].filename).toBe(".config/")- expect(result[5].url).toBe("file:///base/.config")+ expect(matches[5][1]).toBe(".config/")})test("should extract hidden file", () => {- expect(result[6].filename).toBe(".bashrc")- expect(result[6].url).toBe("file:///base/.bashrc")+ expect(matches[6][1]).toBe(".bashrc")})test("should extract a file ignoring period at end of sentence", () => {- expect(result[7].filename).toBe("foo.md")- expect(result[7].url).toBe("file:///base/foo.md")+ expect(matches[7][1]).toBe("foo.md")})test("should extract an absolute path with an extension", () => {- expect(result[8].filename).toBe("/absolute/paths.txt")- expect(result[8].url).toBe("file:///absolute/paths.txt")+ expect(matches[8][1]).toBe("/absolute/paths.txt")})test("should extract an absolute path without an extension", () => {- expect(result[9].filename).toBe("/without/extensions")- expect(result[9].url).toBe("file:///without/extensions")+ expect(matches[9][1]).toBe("/without/extensions")})test("should extract an absolute path in home directory", () => {- expect(result[10].filename).toBe("~/home-files")- expect(result[10].url).toBe("file:///home/fake-user/home-files")+ expect(matches[10][1]).toBe("~/home-files")})test("should extract an absolute path under home directory", () => {- expect(result[11].filename).toBe("~/paths/under/home.txt")- expect(result[11].url).toBe("file:///home/fake-user/paths/under/home.txt")+ expect(matches[11][1]).toBe("~/paths/under/home.txt")+ })++ test("should not match when preceded by backtick", () => {+ const backtickTest = "This `@should/not/match` should be ignored"+ const backtickMatches = Array.from(backtickTest.matchAll(Session.fileRegex))+ expect(backtickMatches.length).toBe(0)+ })++ test("should not match email addresses", () => {+ const emailTest = "Contact [email protected] for help"+ const emailMatches = Array.from(emailTest.matchAll(Session.fileRegex))+ expect(emailMatches.length).toBe(0)})})