Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Improve regex for fileContextPath (#939)

Mish Ushakovc40eeaac383b
diff --git a/.changeset/metal-horses-peel.md b/.changeset/metal-horses-peel.md
new file mode 100644
index 000000000..e9b14ff01
--- /dev/null
+++ b/.changeset/metal-horses-peel.md
@@ -0,0 +1,5 @@
+---
+'e2b': patch
+---
+
+regex path for fileContextPath
diff --git a/packages/js-sdk/src/template/utils.ts b/packages/js-sdk/src/template/utils.ts
index 8ab4dc96a..7c7d7a316 100644
--- a/packages/js-sdk/src/template/utils.ts
+++ b/packages/js-sdk/src/template/utils.ts
@@ -111,6 +111,19 @@ export function getCallerFrame(depth: number): string | undefined {
return lines.slice(depth).join('\n')
}
+/**
+ * Matches paths like "at <anonymous> (/path/to/file.js:1:1)" or "at /path/to/file.js:1:1"
+ * @param line - The line to match
+ * @returns The directory of the file
+ */
+export function matchFileDir(line: string): string | undefined {
+ const match = line.match(/\/[^:]+/)
+ if (match) {
+ const filePath = match[0]
+ return path.dirname(filePath)
+ }
+}
+
/**
* Get the caller directory
* @returns The caller directory
@@ -125,15 +138,9 @@ export function getCallerDirectory(depth: number): string | undefined {
if (lines.length === 0) {
return
}
- const firstLine = lines[0]
-
- const match = firstLine.match(/at ([^:]+):\d+:\d+/)
- if (match) {
- const filePath = match[1]
- return path.dirname(filePath)
- }
- return
+ const firstLine = lines[0]
+ return matchFileDir(firstLine)
}
export function padOctal(mode: number): string {
diff --git a/packages/js-sdk/tests/template/utils/getCallerDirectory.test.ts b/packages/js-sdk/tests/template/utils/getCallerDirectory.test.ts
new file mode 100644
index 000000000..143c13cff
--- /dev/null
+++ b/packages/js-sdk/tests/template/utils/getCallerDirectory.test.ts
@@ -0,0 +1,8 @@
+import { expect, test } from 'vitest'
+import { getCallerDirectory } from '../../../src/template/utils'
+
+test('getCallerDirectory', () => {
+ // getCallerDirectory(1) should return the directory of the current file
+ // __dirname is the directory of the current file
+ expect(getCallerDirectory(1)).toBe(__dirname)
+})
diff --git a/packages/js-sdk/tests/template/utils/matchFileDir.test.ts b/packages/js-sdk/tests/template/utils/matchFileDir.test.ts
new file mode 100644
index 000000000..e11a9e2b7
--- /dev/null
+++ b/packages/js-sdk/tests/template/utils/matchFileDir.test.ts
@@ -0,0 +1,10 @@
+import { expect, test } from 'vitest'
+import { matchFileDir } from '../../../src/template/utils'
+
+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')
+})
diff --git a/packages/python-sdk/tests/async/template_async/utils/test_get_caller_directory.py b/packages/python-sdk/tests/async/template_async/utils/test_get_caller_directory.py
new file mode 100644
index 000000000..b42f8b26b
--- /dev/null
+++ b/packages/python-sdk/tests/async/template_async/utils/test_get_caller_directory.py
@@ -0,0 +1,8 @@
+import pytest
+from e2b.template.utils import get_caller_directory
+import os
+
+
+async def test_get_caller_directory():
+ assert get_caller_directory(1) == os.path.dirname(__file__)
diff --git a/packages/python-sdk/tests/sync/template_sync/utils/test_get_caller_directory.py b/packages/python-sdk/tests/sync/template_sync/utils/test_get_caller_directory.py
new file mode 100644
index 000000000..ba3141393
--- /dev/null
+++ b/packages/python-sdk/tests/sync/template_sync/utils/test_get_caller_directory.py
@@ -0,0 +1,6 @@
+import os
+from e2b.template.utils import get_caller_directory
+
+
+def test_get_caller_directory():
+ assert get_caller_directory(1) == os.path.dirname(__file__)