Skip to content

Commit 1a5500f

Browse files
hugodutkajohnstcn
andauthored
fix: suggest changes to cj/fix/chat-base-path (#42)
Co-authored-by: Cian Johnston <[email protected]>
1 parent 01d97da commit 1a5500f

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ BASE_PATH ?= /magic-base-path-placeholder
66

77
$(CHAT_SOURCES_STAMP): $(CHAT_SOURCES)
88
@echo "Chat sources changed. Running build steps..."
9-
cd chat && BASE_PATH=${BASE_PATH} bun run build
9+
cd chat && NEXT_PUBLIC_BASE_PATH="${BASE_PATH}" bun run build
1010
rm -rf lib/httpapi/chat && mkdir -p lib/httpapi/chat && touch lib/httpapi/chat/marker
1111
cp -r chat/out/. lib/httpapi/chat/
1212
touch $@

chat/next.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type { NextConfig } from "next";
2-
const basePath = process.env.BASE_PATH ?? "/chat";
2+
let basePath = process.env.NEXT_PUBLIC_BASE_PATH ?? "/chat";
3+
if (basePath.endsWith("/")) {
4+
basePath = basePath.slice(0, -1);
5+
}
36

47
const nextConfig: NextConfig = {
58
// Enable static exports

chat/src/components/chat-provider.tsx

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,20 @@ interface ChatContextValue {
5151

5252
const ChatContext = createContext<ChatContextValue | undefined>(undefined);
5353

54-
export function ChatProvider({ children }: PropsWithChildren) {
55-
const [messages, setMessages] = useState<(Message | DraftMessage)[]>([]);
56-
const [loading, setLoading] = useState<boolean>(false);
57-
const [serverStatus, setServerStatus] = useState<ServerStatus>("unknown");
58-
const eventSourceRef = useRef<EventSource | null>(null);
54+
const useAgentAPIUrl = (): string => {
5955
const searchParams = useSearchParams();
60-
// NOTE(cian): We use '../../' here to construct the agent API URL relative
61-
// to the current window location. Let's say the app is hosted on a subpath
56+
const paramsUrl = searchParams.get("url");
57+
if (paramsUrl) {
58+
return paramsUrl;
59+
}
60+
const basePath = process.env.NEXT_PUBLIC_BASE_PATH;
61+
if (!basePath) {
62+
throw new Error(
63+
"agentAPIUrl is not set. Please set the url query parameter to the URL of the AgentAPI or the NEXT_PUBLIC_BASE_PATH environment variable."
64+
);
65+
}
66+
// NOTE(cian): We use '../' here to construct the agent API URL relative
67+
// to the chat's location. Let's say the app is hosted on a subpath
6268
// `/@admin/workspace.agent/apps/ccw/`. When you visit this URL you get
6369
// redirected to `/@admin/workspace.agent/apps/ccw/chat/embed`. This serves
6470
// this React application, but it needs to know where the agent API is hosted.
@@ -67,8 +73,25 @@ export function ChatProvider({ children }: PropsWithChildren) {
6773
// `window.location.origin` but this assumes that the application owns the
6874
// entire origin.
6975
// See: https://github.com/coder/coder/issues/18779#issuecomment-3133290494 for more context.
70-
const defaultAgentAPIURL = new URL("../../", window.location.href).toString();
71-
const agentAPIUrl = searchParams.get("url") || defaultAgentAPIURL;
76+
let chatURL: string = new URL(basePath, window.location.origin).toString();
77+
// NOTE: trailing slashes and relative URLs are tricky.
78+
// https://developer.mozilla.org/en-US/docs/Web/API/URL_API/Resolving_relative_references#current_directory_relative
79+
if (!chatURL.endsWith("/")) {
80+
chatURL += "/";
81+
}
82+
const agentAPIURL = new URL("..", chatURL).toString();
83+
if (agentAPIURL.endsWith("/")) {
84+
return agentAPIURL.slice(0, -1);
85+
}
86+
return agentAPIURL;
87+
};
88+
89+
export function ChatProvider({ children }: PropsWithChildren) {
90+
const [messages, setMessages] = useState<(Message | DraftMessage)[]>([]);
91+
const [loading, setLoading] = useState<boolean>(false);
92+
const [serverStatus, setServerStatus] = useState<ServerStatus>("unknown");
93+
const eventSourceRef = useRef<EventSource | null>(null);
94+
const agentAPIUrl = useAgentAPIUrl();
7295

7396
// Set up SSE connection to the events endpoint
7497
useEffect(() => {

lib/httpapi/server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"log/slog"
88
"net/http"
99
"net/url"
10+
"strings"
1011
"sync"
1112
"time"
1213

@@ -97,7 +98,7 @@ func NewServer(ctx context.Context, agentType mf.AgentType, process *termexec.Pr
9798
agentio: process,
9899
agentType: agentType,
99100
emitter: emitter,
100-
chatBasePath: chatBasePath,
101+
chatBasePath: strings.TrimSuffix(chatBasePath, "/"),
101102
}
102103

103104
// Register API routes

0 commit comments

Comments
 (0)