Skip to content

Commit 5435f7f

Browse files
authored
helper to convert file:// to path (#1399)
* helper to convert file:// to path * ♻️ Refactor path utility creation in testhost.ts Replaced inline createNodePath implementation with module import.
1 parent 218d047 commit 5435f7f

7 files changed

Lines changed: 24 additions & 47 deletions

File tree

packages/cli/src/nodehost.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { glob } from "glob"
99
import { debug as debug_, error, info, warn } from "./log"
1010
import { execa } from "execa"
1111
import { join } from "node:path"
12-
import { createNodePath } from "./nodepath"
12+
import { createNodePath } from "../../core/src/path"
1313
import { DockerManager } from "./docker"
1414
import { createWorkspaceFileSystem } from "../../core/src/workspace"
1515
import { filterGitIgnore } from "../../core/src/gitignore"
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
isAbsolute,
1010
} from "node:path"
1111
import { changeext } from "../../core/src/fs"
12+
import { fileURLToPath } from "node:url"
1213

1314
/**
1415
* Creates a frozen object that provides various path manipulation functions.
@@ -30,5 +31,6 @@ export function createNodePath(): Path {
3031
resolve, // Resolve a sequence of paths to an absolute path
3132
isAbsolute, // Check if a path is absolute
3233
changeext,
33-
})
34+
resolveFileURL: fileURLToPath,
35+
} satisfies Path)
3436
}

packages/core/src/testhost.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
UTF8Encoder,
1212
setRuntimeHost,
1313
RuntimeHost,
14-
AzureTokenResolver,
1514
ModelConfigurations,
1615
ModelConfiguration,
1716
} from "./host"
@@ -37,22 +36,7 @@ import {
3736
import { defaultModelConfigurations } from "./llms"
3837
import { CancellationToken } from "./cancellation"
3938
import { changeext } from "./fs"
40-
41-
// Function to create a frozen object representing Node.js path methods
42-
// This object provides utility methods for path manipulations
43-
export function createNodePath(): Path {
44-
return Object.freeze({
45-
dirname,
46-
extname,
47-
basename,
48-
join,
49-
normalize,
50-
relative,
51-
resolve,
52-
isAbsolute,
53-
changeext,
54-
})
55-
}
39+
import { createNodePath } from "./path"
5640

5741
// Class representing a test host for runtime, implementing the RuntimeHost interface
5842
export class TestHost implements RuntimeHost {

packages/core/src/types/prompt_template.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,6 +1827,12 @@ interface Path {
18271827
* @param ext
18281828
*/
18291829
changeext(path: string, ext: string): string
1830+
1831+
/**
1832+
* Converts a file://... to a path
1833+
* @param fileUrl
1834+
*/
1835+
resolveFileURL(fileUrl: string): string
18301836
}
18311837

18321838
interface Fenced {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
script({ model: "echo" })
2+
3+
const __filename = path.resolveFileURL(import.meta.url)
4+
const __dirname = path.dirname(__filename)
5+
6+
console.log("Current file path:", __filename)
7+
console.log("Current directory:", __dirname)
8+
const langgraph = await workspace.readText(path.join(__dirname, "langgraph.md"))
9+
10+
console.log(`langgraph.md content: ${langgraph.content.length}`)
11+
if (!langgraph) throw new Error("Failed to read langgraph.md")

packages/vscode/src/vshost.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import * as vscode from "vscode"
2-
import { createVSPath } from "./vspath"
2+
import { createNodePath } from "../../core/src/path"
33
import { TerminalServerManager } from "./servermanager"
44
import { Uri } from "vscode"
55
import { ExtensionState } from "./state"
66
import { Utils } from "vscode-uri"
7-
import { checkFileExists, readFileText } from "./fs"
87
import { filterGitIgnore } from "../../core/src/gitignore"
98
import { setHost, Host } from "../../core/src/host"
109
import { TraceOptions } from "../../core/src/trace"
@@ -18,7 +17,7 @@ import {
1817

1918
export class VSCodeHost extends EventTarget implements Host {
2019
userState: any = {}
21-
readonly path = createVSPath()
20+
readonly path = createNodePath()
2221
readonly server: TerminalServerManager
2322
constructor(readonly state: ExtensionState) {
2423
super()

packages/vscode/src/vspath.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)