Compare commits

...
5 changed files with 182 additions and 56 deletions

No files matched your search

+2 -23
View File
@@ -47,6 +47,7 @@ import { derivePendingApprovals, derivePendingUserInputs } from "../session-logi
import { gitRemoveWorktreeMutationOptions, gitStatusQueryOptions } from "../lib/gitReactQuery";
import { serverConfigQueryOptions } from "../lib/serverReactQuery";
import { readNativeApi } from "../nativeApi";
import { resolveServerHttpOrigin } from "../serverConnection";
import { type DraftThreadEnvMode, useComposerDraftStore } from "../composerDraftStore";
import { selectThreadTerminalState, useTerminalStateStore } from "../terminalStateStore";
import { toastManager } from "./ui/toast";
@@ -178,29 +179,7 @@ function T3Wordmark() {
);
}
/**
* Derives the server's HTTP origin (scheme + host + port) from the same
* sources WsTransport uses, converting ws(s) to http(s).
*/
function getServerHttpOrigin(): string {
const bridgeUrl = window.desktopBridge?.getWsUrl();
const envUrl = import.meta.env.VITE_WS_URL as string | undefined;
const wsUrl =
bridgeUrl && bridgeUrl.length > 0
? bridgeUrl
: envUrl && envUrl.length > 0
? envUrl
: `${window.location.protocol === "https:" ? "wss" : "ws"}://${window.location.hostname}:${window.location.port}`;
// Parse to extract just the origin, dropping path/query (e.g. ?token=…)
const httpUrl = wsUrl.replace(/^wss:/, "https:").replace(/^ws:/, "http:");
try {
return new URL(httpUrl).origin;
} catch {
return httpUrl;
}
}
const serverHttpOrigin = getServerHttpOrigin();
const serverHttpOrigin = resolveServerHttpOrigin();
function ProjectFavicon({ cwd }: { cwd: string }) {
const [status, setStatus] = useState<"loading" | "loaded" | "error">("loading");
+68
View File
@@ -0,0 +1,68 @@
import { describe, expect, it } from "vitest";
import {
resolveServerHttpOriginFromInput,
resolveServerWsUrlFromInput,
} from "./serverConnection";
const localhostLocation = {
protocol: "http:",
hostname: "localhost",
port: "5733",
origin: "http://localhost:5733",
} as const;
describe("serverConnection", () => {
it("prefers the desktop bridge websocket URL", () => {
expect(
resolveServerWsUrlFromInput({
bridgeWsUrl: "ws://127.0.0.1:4444?token=abc",
envWsUrl: "ws://localhost:3773",
isDev: true,
location: localhostLocation,
}),
).toBe("ws://127.0.0.1:4444?token=abc");
});
it("uses the configured env websocket URL when available", () => {
expect(
resolveServerWsUrlFromInput({
envWsUrl: "ws://localhost:3773",
isDev: true,
location: localhostLocation,
}),
).toBe("ws://localhost:3773");
});
it("infers the paired dev server websocket port when env wiring is missing", () => {
expect(
resolveServerWsUrlFromInput({
isDev: true,
location: localhostLocation,
}),
).toBe("ws://localhost:3773");
});
it("falls back to same-origin websocket outside paired localhost dev mode", () => {
expect(
resolveServerWsUrlFromInput({
isDev: false,
location: {
protocol: "https:",
hostname: "example.com",
port: "443",
origin: "https://example.com",
},
}),
).toBe("wss://example.com:443");
});
it("converts the resolved websocket URL into the matching http origin", () => {
expect(
resolveServerHttpOriginFromInput({
isDev: true,
location: localhostLocation,
}),
).toBe("http://localhost:3773");
});
});
+103
View File
@@ -0,0 +1,103 @@
const DEFAULT_DEV_SERVER_PORT = 3773;
const DEFAULT_DEV_WEB_PORT = 5733;
const DEV_SERVER_PORT_GAP = DEFAULT_DEV_WEB_PORT - DEFAULT_DEV_SERVER_PORT;
const LOOPBACK_HOSTS = new Set(["localhost", "127.0.0.1", "::1", "[::1]"]);
interface BrowserLocationLike {
readonly protocol: string;
readonly hostname: string;
readonly port: string;
readonly origin?: string;
}
interface ResolveServerConnectionInput {
readonly bridgeWsUrl?: string;
readonly envWsUrl?: string;
readonly isDev: boolean;
readonly location: BrowserLocationLike;
}
function trimToUndefined(value: string | undefined): string | undefined {
const trimmed = value?.trim();
return trimmed && trimmed.length > 0 ? trimmed : undefined;
}
function inferLoopbackDevServerWsUrl(input: ResolveServerConnectionInput): string | null {
if (!input.isDev) return null;
if (!LOOPBACK_HOSTS.has(input.location.hostname)) return null;
const currentPort = Number.parseInt(input.location.port, 10);
if (!Number.isInteger(currentPort) || currentPort < DEFAULT_DEV_WEB_PORT) {
return null;
}
const inferredServerPort = currentPort - DEV_SERVER_PORT_GAP;
if (inferredServerPort < DEFAULT_DEV_SERVER_PORT) {
return null;
}
const protocol = input.location.protocol === "https:" ? "wss:" : "ws:";
return `${protocol}//${input.location.hostname}:${String(inferredServerPort)}`;
}
export function resolveServerWsUrlFromInput(input: ResolveServerConnectionInput): string {
const bridgeWsUrl = trimToUndefined(input.bridgeWsUrl);
if (bridgeWsUrl) {
return bridgeWsUrl;
}
const envWsUrl = trimToUndefined(input.envWsUrl);
if (envWsUrl) {
return envWsUrl;
}
const inferredDevServerWsUrl = inferLoopbackDevServerWsUrl(input);
if (inferredDevServerWsUrl) {
return inferredDevServerWsUrl;
}
const protocol = input.location.protocol === "https:" ? "wss:" : "ws:";
const port = input.location.port.length > 0 ? `:${input.location.port}` : "";
return `${protocol}//${input.location.hostname}${port}`;
}
export function resolveServerHttpOriginFromInput(input: ResolveServerConnectionInput): string {
const fallbackOrigin =
input.location.origin ??
`${input.location.protocol}//${input.location.hostname}${input.location.port ? `:${input.location.port}` : ""}`;
try {
const wsUrl = new URL(resolveServerWsUrlFromInput(input));
const protocol =
wsUrl.protocol === "wss:" ? "https:" : wsUrl.protocol === "ws:" ? "http:" : wsUrl.protocol;
return `${protocol}//${wsUrl.host}`;
} catch {
return fallbackOrigin;
}
}
function getCurrentInput(): ResolveServerConnectionInput | null {
if (typeof window === "undefined") {
return null;
}
const bridgeWsUrl = window.desktopBridge?.getWsUrl?.() ?? undefined;
const envWsUrl = (import.meta.env.VITE_WS_URL as string | undefined) ?? undefined;
return {
...(bridgeWsUrl ? { bridgeWsUrl } : {}),
...(envWsUrl ? { envWsUrl } : {}),
isDev: import.meta.env.DEV,
location: window.location,
};
}
export function resolveServerWsUrl(): string {
const input = getCurrentInput();
return input ? resolveServerWsUrlFromInput(input) : "";
}
export function resolveServerHttpOrigin(): string {
const input = getCurrentInput();
return input ? resolveServerHttpOriginFromInput(input) : "";
}
+3 -22
View File
@@ -16,6 +16,8 @@ import { create } from "zustand";
import { type ChatMessage, type Project, type Thread } from "./types";
import { Debouncer } from "@tanstack/react-pacer";
import { resolveServerHttpOrigin } from "./serverConnection";
// ── State ────────────────────────────────────────────────────────────
export interface AppState {
@@ -208,30 +210,9 @@ function inferProviderForThreadModel(input: {
return "codex";
}
function resolveWsHttpOrigin(): string {
if (typeof window === "undefined") return "";
const bridgeWsUrl = window.desktopBridge?.getWsUrl?.();
const envWsUrl = import.meta.env.VITE_WS_URL as string | undefined;
const wsCandidate =
typeof bridgeWsUrl === "string" && bridgeWsUrl.length > 0
? bridgeWsUrl
: typeof envWsUrl === "string" && envWsUrl.length > 0
? envWsUrl
: null;
if (!wsCandidate) return window.location.origin;
try {
const wsUrl = new URL(wsCandidate);
const protocol =
wsUrl.protocol === "wss:" ? "https:" : wsUrl.protocol === "ws:" ? "http:" : wsUrl.protocol;
return `${protocol}//${wsUrl.host}`;
} catch {
return window.location.origin;
}
}
function toAttachmentPreviewUrl(rawUrl: string): string {
if (rawUrl.startsWith("/")) {
return `${resolveWsHttpOrigin()}${rawUrl}`;
return `${resolveServerHttpOrigin()}${rawUrl}`;
}
return rawUrl;
}
+6 -11
View File
@@ -1,6 +1,8 @@
import { WebSocketResponse, WsPush, WsResponse } from "@t3tools/contracts";
import { Cause, Schema } from "effect";
import { resolveServerWsUrl } from "./serverConnection";
type PushListener = (data: unknown) => void;
interface PendingRequest {
@@ -34,17 +36,10 @@ export class WsTransport {
private readonly url: string;
constructor(url?: string) {
const bridgeUrl = window.desktopBridge?.getWsUrl();
// In dev mode, VITE_WS_URL points to the server's WebSocket endpoint.
// In production, the page is served by the WS server on the same host:port.
const envUrl = import.meta.env.VITE_WS_URL as string | undefined;
this.url =
url ??
(bridgeUrl && bridgeUrl.length > 0
? bridgeUrl
: envUrl && envUrl.length > 0
? envUrl
: `${window.location.protocol === "https:" ? "wss" : "ws"}://${window.location.hostname}:${window.location.port}`);
this.url = url ?? resolveServerWsUrl();
if (!this.url) {
throw new Error("WebSocket URL is required");
}
this.connect();
}