Skip to content

fix: preserve base URL path prefix in direct requests - #60559

Open
fabiovincenzi wants to merge 5 commits into
anomalyco:devfrom
fabiovincenzi:preserve-path-prefix
Open

fabiovincenzi wants to merge 5 commits into
anomalyco:devfrom
fabiovincenzi:preserve-path-prefix

Conversation

@fabiovincenzi

Copy link
Copy Markdown

Issue for this PR

Closes #60542

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

#50428 fixed the generated client, so --server http://host/prefix now passes the connection check. A few places still build request URLs by hand with new URL("/api/...", serverUrl). The leading slash makes the path absolute, so the prefix is dropped.

With a prefixed --server this breaks opencode api (the request and the /openapi.json lookup) and opencode session import.

The fix uses the same approach as #50428: add a trailing slash to the base and join the path without its leading slash. The three CLI call sites share a small ServerConnection.requestUrl helper.

The service health probe (client/src/service-probe.ts) and the attachment upload (app/src/composer/attachments/destination.ts) use the same pattern. They can't receive a prefixed URL today, but I fixed them too so they don't break once they can.

How did you verify your code works?

  • packages/cli/test/api-prefix.test.ts runs opencode api against a server mounted under a prefix. Without the fix, the two prefixed cases fail.
  • packages/client/test/service-probe.test.ts covers the probe, and server-connection.test.ts covers requestUrl.
  • Manually, with opencode serve behind a reverse proxy on /proxy: opencode api --server http://127.0.0.1:4999/proxy GET /api/session gets a 404 from the proxy on dev, and the session list with this change.

Visual

N/A

Screenshots / recordings

N/A, not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks good to me. I ran the #60542 reproduction from source: on the base, opencode api (both the raw path and the /openapi.json lookup) and opencode session import sent their requests to the host root. With this change they all go to /proxy/..., and query strings survive. The new tests pass. With plain tsc, the changed CLI and client files have no type errors. The packages' typecheck scripts fail the same way on the base in this environment, and I didn't type-check the app package.

Public API: unchanged. ServerConnection.requestUrl is internal to the CLI.

One small inline note on the new CLI test. Nothing else blocking.

Comment thread packages/cli/test/api-prefix.test.ts Outdated
import { OPENCODE_VERSION } from "../src/version"
import { isolatedEnv } from "./fixture/environment"

test.each(["", "/proxy", "/proxy/"])(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This test starts the CLI as a subprocess six times (~1.6s each), and server-connection.test.ts already covers the joining cases for requestUrl. Could you cut it down to a single prefixed case (e.g. /proxy) that runs both the raw and the operation form? The "" case doesn't add anything here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The api prefix test now runs once against /proxy and still covers both forms. That addresses my earlier point. It passes with the change and fails on the base. The source is unchanged since my last review, and I have nothing new to raise. Public API: unchanged.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The new commit only merges dev, and none of this PR's files changed. The CLI prefix and requestUrl tests still pass after the merge. I have nothing new to raise. Public API: unchanged.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The new commit merges dev. The conflict in the attachment upload is resolved correctly: it applies the prefix-preserving join to dev's new sdk.http.url. The rest of the change is unchanged, and the CLI and client tests still pass after the merge. I have nothing new to raise. Public API: unchanged.

@monolith-james

Copy link
Copy Markdown

This unblocks #61624, which we'd use to run OpenCode inside marimohub behind its authenticated proxy under a per-session path prefix. A review would be much appreciated.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The new commit only merges dev, and this PR's changes are unchanged. The CLI and client tests still pass after the merge. I have nothing new to raise. Public API: unchanged.

This branch has not been deployed

No deployments
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.

opencode api and session import drop the --server path prefix

2 participants