Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/opencode/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,20 @@ export namespace Server {
},
)
.use(async (c, next) => {
const directory = c.req.query("directory") || c.req.header("x-opencode-directory") || process.cwd()
const rawDirectory = c.req.query("directory") || c.req.header("x-opencode-directory") || process.cwd()
const directory = (() => {
const prefix = "opencode-uri:"
Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The prefix constant is defined inline in the server code but as a constant in the SDK files. This creates a maintenance risk where the prefix strings could become out of sync. Consider extracting this to a shared constant or at minimum using a named constant here as well for consistency and easier maintenance.

Copilot uses AI. Check for mistakes.
Comment on lines +250 to +251
Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The IIFE pattern here defines the prefix constant inside the function scope. Since this middleware runs on every request and the prefix is a constant value, consider defining it at the module level alongside OPENCODE_DIRECTORY_HEADER_PREFIX in the SDK files for better performance and consistency.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback

if (!rawDirectory.startsWith(prefix)) return rawDirectory
try {
return decodeURIComponent(rawDirectory.slice(prefix.length))
} catch (error) {
log.warn("Failed to decode opencode-uri directory, falling back to process.cwd()", {
rawDirectory,
error,
})
return process.cwd()
}
})()
return Instance.provide({
directory,
init: InstanceBootstrap,
Expand Down
21 changes: 18 additions & 3 deletions packages/sdk/js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { type Config } from "./gen/client/types.gen.js"
import { OpencodeClient } from "./gen/sdk.gen.js"
export { type Config as OpencodeClientConfig, OpencodeClient }

const OPENCODE_DIRECTORY_HEADER_PREFIX = "opencode-uri:"

export function createOpencodeClient(config?: Config & { directory?: string }) {
if (!config?.fetch) {
const customFetch: any = (req: any) => {
Expand All @@ -19,9 +21,22 @@ export function createOpencodeClient(config?: Config & { directory?: string }) {
}

if (config?.directory) {
config.headers = {
...config.headers,
"x-opencode-directory": config.directory,
const directoryHeader = OPENCODE_DIRECTORY_HEADER_PREFIX + encodeURIComponent(config.directory)
if (config.headers instanceof Headers || Array.isArray(config.headers)) {
const headers = new Headers(config.headers)
headers.set("x-opencode-directory", directoryHeader)
config = {
...config,
headers,
}
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style guide suggestion: The else statement here could potentially be avoided using early return pattern. For example, handling the object case first and returning early would eliminate the need for else. However, this is a minor suggestion - the current implementation is clear and readable, so feel free to keep it as-is if you prefer.

config = {
...config,
headers: {
...(config.headers ?? {}),
"x-opencode-directory": directoryHeader,
},
}
}
}

Expand Down
21 changes: 18 additions & 3 deletions packages/sdk/js/src/v2/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { type Config } from "./gen/client/types.gen.js"
import { OpencodeClient } from "./gen/sdk.gen.js"
export { type Config as OpencodeClientConfig, OpencodeClient }

const OPENCODE_DIRECTORY_HEADER_PREFIX = "opencode-uri:"

export function createOpencodeClient(config?: Config & { directory?: string }) {
if (!config?.fetch) {
const customFetch: any = (req: any) => {
Expand All @@ -19,9 +21,22 @@ export function createOpencodeClient(config?: Config & { directory?: string }) {
}

if (config?.directory) {
config.headers = {
...config.headers,
"x-opencode-directory": config.directory,
const directoryHeader = OPENCODE_DIRECTORY_HEADER_PREFIX + encodeURIComponent(config.directory)
if (config.headers instanceof Headers || Array.isArray(config.headers)) {
const headers = new Headers(config.headers)
headers.set("x-opencode-directory", directoryHeader)
config = {
...config,
headers,
}
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style guide suggestion: The else statement here could potentially be avoided using early return pattern. For example, handling the object case first and returning early would eliminate the need for else. However, this is a minor suggestion - the current implementation is clear and readable, so feel free to keep it as-is if you prefer.

config = {
...config,
headers: {
...(config.headers ?? {}),
"x-opencode-directory": directoryHeader,
},
}
}
}

Expand Down