Skip to content

fix: improve support for running agentapi under a subpath #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 31, 2025

Conversation

johnstcn
Copy link
Member

@johnstcn johnstcn commented Jul 30, 2025

Relates to coder/coder#18779

Previously, we had been defaulting agentAPIURL to window.location.origin which assumes the application owns its subdomain. This is not necessarily the case if agentapi is running under a subpath.

A 'good enough' fix seems to be defaulting to "two levels up" from /chat/embed.
Additionally, when redirecting to /chat/embed from /, we also need to take chatBasePath into account.

Screenshot 2025-07-30 at 17 36 47

@johnstcn johnstcn self-assigned this Jul 30, 2025
@johnstcn johnstcn requested a review from Copilot July 30, 2025 16:37
Copy link
Contributor

@Copilot 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 improves support for running agentapi under a subpath by fixing URL handling when the application doesn't own its subdomain. The changes ensure proper redirection and API URL construction when the chat interface is served from a non-root path.

Key changes:

  • Updates the default agent API URL calculation in the chat client to use relative path navigation instead of assuming root domain ownership
  • Modifies the server redirect logic to respect the configured chat base path
  • Adds comprehensive test coverage for the redirect functionality

Reviewed Changes

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

File Description
chat/src/components/chat-provider.tsx Updates default agent API URL to use relative path navigation ("../../") instead of window.location.origin
lib/httpapi/server.go Refactors server to store chatBasePath as instance variable and use it in redirect logic; adds Handler() method for testing
lib/httpapi/server_test.go Adds comprehensive test coverage for redirect functionality with different base path configurations

@johnstcn johnstcn marked this pull request as ready for review July 30, 2025 16:48
// `window.location.origin` but this assumes that the application owns the
// entire origin.
// See: https://github.com/coder/coder/issues/18779#issuecomment-3133290494 for more context.
const defaultAgentAPIURL = new URL("../../", window.location.href).toString();
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm afraid this won't always work. Chat is hosted both at /chat and /chat/embed - the /chat/embed route has a simplified UI.

I created #42 to suggest a workaround.

Copy link
Member Author

Choose a reason for hiding this comment

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

oof, TIL

Co-authored-by: Cian Johnston <cian@coder.com>
@johnstcn johnstcn requested a review from hugodutka July 31, 2025 11:15
@hugodutka hugodutka merged commit 23530e8 into main Jul 31, 2025
2 checks passed
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.

4 participants