Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Use "callsites" instead of regex for current directory matching (#1015)
Mish Ushakov72b5c73aa01e
diff --git a/.changeset/tasty-walls-move.md b/.changeset/tasty-walls-move.mdnew file mode 100644index 000000000..ffcb9227b--- /dev/null+++ b/.changeset/tasty-walls-move.md@@ -0,0 +1,5 @@+---+'e2b': patch+---++fixes default context directory for windows pathsdiff --git a/packages/js-sdk/src/template/utils.ts b/packages/js-sdk/src/template/utils.tsindex 6bcf5ccd9..bc3273c44 100644--- a/packages/js-sdk/src/template/utils.ts+++ b/packages/js-sdk/src/template/utils.ts@@ -176,35 +176,21 @@ export function getCallerFrame(depth: number): string | undefined {return lines.slice(depth).join('\n')}-/**- * Extract the directory path from a stack trace line.- *- * Matches patterns like:- * - "at <anonymous> (/path/to/file.js:1:1)"- * - "at /path/to/file.js:1:1"- * - "at <anonymous> (file:///C:/path/to/file.js:1:1)"- * - "at (file:///C:/path/to/file.js:1:1)"- * @param line A line from a stack trace- * @returns The directory of the file, or undefined if not found- */-export function matchFileDir(line: string): string | undefined {- const match = line.match(- /(?:file:\/\/\/)?([A-Za-z]:)?([/\\][^:]+)(?::\d+:\d+)?\)?/- )- if (match) {- // Extract the full matched path- let filePath = match[0]-- // Remove file:/// protocol prefix if present- filePath = filePath.replace(/^file:\/\/\//, '')-- // Remove trailing closing parenthesis if present- filePath = filePath.replace(/\)$/, '')-- // Remove :line:column suffix if present- filePath = filePath.replace(/:\d+:\d+$/, '')+// adopted from https://github.com/sindresorhus/callsites+export function callsites(depth: number): NodeJS.CallSite[] {+ const _originalPrepareStackTrace = Error.prepareStackTrace+ try {+ let result: NodeJS.CallSite[] = []+ Error.prepareStackTrace = (_, callSites) => {+ const callSitesWithoutCurrent = callSites.slice(depth)+ result = callSitesWithoutCurrent+ return callSitesWithoutCurrent+ }- return path.dirname(filePath)+ new Error().stack+ return result+ } finally {+ Error.prepareStackTrace = _originalPrepareStackTrace}}@@ -215,18 +201,18 @@ export function matchFileDir(line: string): string | undefined {* @returns The caller's directory path, or undefined if not available*/export function getCallerDirectory(depth: number): string | undefined {- const caller = getCallerFrame(depth + 1) // +1 depth to skip this function (getCallerDirectory)- if (!caller) {- return+ // +1 depth to skip this function (getCallerDirectory)+ const callSites = callsites(depth + 1)+ if (callSites.length === 0) {+ return undefined}- const lines = caller.split('\n')- if (lines.length === 0) {- return+ const fileName = callSites[0].getFileName()+ if (!fileName) {+ return undefined}- const firstLine = lines[0]- return matchFileDir(firstLine)+ return path.dirname(fileName)}/**diff --git a/packages/js-sdk/tests/template/utils/matchFileDir.test.ts b/packages/js-sdk/tests/template/utils/matchFileDir.test.tsdeleted file mode 100644index b410da911..000000000--- a/packages/js-sdk/tests/template/utils/matchFileDir.test.ts+++ /dev/null@@ -1,78 +0,0 @@-import { expect, test } from 'vitest'-import { matchFileDir } from '../../../src/template/utils'--// Basic Unix/Linux paths-test('matchFileDir', () => {- expect(matchFileDir('at /path/to/file.js:1:1')).toBe('/path/to')-})--test('matchFileDir (anonymous)', () => {- expect(matchFileDir('at <anonymous> (/path/to/file.js:1:1)')).toBe('/path/to')-})--test('matchFileDir Unix path with spaces', () => {- expect(matchFileDir('at /home/user/my project/file.js:1:1')).toBe(- '/home/user/my project'- )-})--test('matchFileDir Unix path with special characters', () => {- expect(matchFileDir('at /path/to-my_dir.test/file.js:1:1')).toBe(- '/path/to-my_dir.test'- )-})--test('matchFileDir Unix root directory', () => {- expect(matchFileDir('at /file.js:1:1')).toBe('/')-})--test('matchFileDir Unix path with @symbol (scoped packages)', () => {- expect(matchFileDir('at /node_modules/@scope/package/index.js:1:1')).toBe(- '/node_modules/@scope/package'- )-})--test('matchFileDir Unix path with brackets (Next.js dynamic routes)', () => {- expect(matchFileDir('at /app/routes/[id]/page.js:1:1')).toBe(- '/app/routes/[id]'- )-})--// Basic Windows paths (forward slash)-test('matchFileDir Windows path', () => {- expect(matchFileDir('at C:/path/to/file.js:1:1')).toBe('C:/path/to')-})--test('matchFileDir Windows path (anonymous)', () => {- expect(matchFileDir('at <anonymous> (C:/path/to/file.js:1:1)')).toBe(- 'C:/path/to'- )-})--test('matchFileDir Windows path with spaces', () => {- expect(matchFileDir('at C:/Program Files/App/index.js:1:1')).toBe(- 'C:/Program Files/App'- )-})--// File protocol paths-test('matchFileDir file:// protocol Windows', () => {- expect(matchFileDir('at <anonymous> (file:///C:/path/to/file.js:1:1)')).toBe(- 'C:/path/to'- )-})--test('matchFileDir file:// protocol Windows (no anonymous)', () => {- expect(matchFileDir('at (file:///C:/path/to/file.js:1:1)')).toBe('C:/path/to')-})--// Paths with parentheses-test('matchFileDir with parentheses in path', () => {- expect(matchFileDir('at /path/to(1)/file.js:1:1')).toBe('/path/to(1)')-})--test('matchFileDir with parentheses in path (anonymous)', () => {- expect(matchFileDir('at <anonymous> (/path/to(1)/file.js:1:1)')).toBe(- '/path/to(1)'- )-})