Skip to content

Conversation

@Eric-Guo
Copy link
Contributor

@Eric-Guo Eric-Guo commented Jan 6, 2026

Why it crashed

mergeConfigs() calls mergeHeaders(), which ends up doing Headers.set(...). In Tauri/WebView, Headers.set() throws if the header key/value contains characters outside ISO‑8859‑1. We were putting a raw filesystem path into x-opencode-directory, and paths can contain Unicode.

Fix (implemented)

SDK: URL-encode the directory header as opencode-uri: + encodeURIComponent(directory) (done for both v1 + v2 clients).
Server: If the incoming directory value starts with opencode-uri:, decode it before using it.

企业微信截图_17676675582211

Copilot AI review requested due to automatic review settings January 6, 2026 04:19
@github-actions
Copy link
Contributor

github-actions bot commented Jan 6, 2026

The following comment was made by an LLM, it may be inaccurate:

Result

No duplicate PRs found.

All search queries returned only the current PR (#7031), indicating this is a unique issue that hasn't been addressed in other open PRs. The searches covered:

  • Specific error message and encoding issues
  • Header-related Unicode/path problems
  • Tauri/WebView compatibility issues
  • The proposed solution (encodeURIComponent with opencode-uri prefix)

This appears to be a novel fix for handling Unicode characters in filesystem paths when they're passed through HTTP headers in a Tauri/WebView environment.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a TypeError that occurs in Tauri/WebView environments when filesystem paths containing Unicode characters are passed via HTTP headers. The Headers.set() API in WebView only accepts ISO-8859-1 characters, causing crashes when raw filesystem paths with Unicode are used.

Key changes:

  • SDK clients (v1 and v2) now URL-encode the directory path with an opencode-uri: prefix before setting the header
  • Server-side middleware detects and decodes the prefixed header value to restore the original path
  • Headers handling logic updated to support different header types (Headers object, array, plain object)

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
packages/sdk/js/src/v2/client.ts Adds encoding logic for directory header with opencode-uri prefix to prevent Unicode-related crashes in WebView
packages/sdk/js/src/client.ts Identical encoding logic applied to v1 client for consistency
packages/opencode/src/server/server.ts Adds decoding middleware to handle the encoded directory header from clients

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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
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 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

@rekram1-node rekram1-node requested review from adamdotdevin and removed request for adamdotdevin January 6, 2026 04:42
@rekram1-node
Copy link
Collaborator

/review

...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,
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.

@adamdotdevin
Copy link
Contributor

adamdotdevin commented Jan 7, 2026

closing in favor of #7145

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants