File size: 15,012 Bytes
63522a5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | import {
ServerClient,
SettingsClient,
} from "@openhands/typescript-client/clients";
import type { ServerInfo as BaseServerInfo } from "@openhands/typescript-client";
import { getAgentServerClientOptions } from "#/api/agent-server-client-options";
import { isAuthRequired } from "#/api/agent-server-config";
import {
getActiveBackend,
getEffectiveLocalBackend,
isNoBackend,
} from "#/api/backend-registry/active-store";
import type { Backend } from "#/api/backend-registry/types";
import defaults from "../../config/defaults.json";
const AGENT_SERVER_INFO_TIMEOUT_MS = 5000;
const UNKNOWN_AGENT_SERVER_VERSION = "unknown";
export const MINIMUM_COMPATIBLE_AGENT_SERVER_VERSION =
defaults.compatibility.minimumAgentServer;
export const AGENT_SERVER_UNSUPPORTED_VERSION_ERROR_CODE =
"AGENT_SERVER_UNSUPPORTED_VERSION";
export const AGENT_SERVER_UNKNOWN_VERSION_ERROR_CODE =
"AGENT_SERVER_UNKNOWN_VERSION";
/**
* Sentinel string thrown (as an Error message) when a local backend
* rejects the configured session API key with HTTP 401.
* Shared between the backend health probe ({@link validateLocalBackend})
* and any consumer that needs to detect this specific failure.
*/
export const INVALID_BACKEND_API_KEY_ERROR = "Invalid API key";
export interface AgentServerInfo extends BaseServerInfo {
sdk_version?: string;
usable_tools?: string[] | null;
runtime_services?: unknown;
}
let cachedAgentServerInfo: AgentServerInfo | null = null;
let cachedAgentServerInfoHost: string | null = null;
const getAdvertisedTools = (serverInfo: AgentServerInfo | null) => {
if (Array.isArray(serverInfo?.usable_tools)) {
return serverInfo.usable_tools;
}
return null;
};
export class AgentServerUnavailableError extends Error {
readonly details: string | null;
readonly noBackendConfigured: boolean;
constructor(
details?: string | null,
options?: { noBackendConfigured?: boolean },
) {
const noBackendConfigured = options?.noBackendConfigured ?? false;
super(
noBackendConfigured
? "No agent server backend is configured yet. Add a backend to get started."
: "Could not connect to the configured agent server. Make sure it is running and reachable, then reload the page.",
);
this.name = "AgentServerUnavailableError";
this.details = details ?? null;
this.noBackendConfigured = noBackendConfigured;
}
}
export const isAgentServerUnavailableError = (
error: unknown,
): error is AgentServerUnavailableError =>
error instanceof AgentServerUnavailableError ||
(typeof error === "object" &&
error !== null &&
"name" in error &&
(error.name === "AgentServerUnavailableError" ||
error.name === "AgentServerUnsupportedVersionError" ||
error.name === "AgentServerUnknownVersionError"));
export class AgentServerUnsupportedVersionError extends AgentServerUnavailableError {
readonly code = AGENT_SERVER_UNSUPPORTED_VERSION_ERROR_CODE;
readonly actualVersion: string;
readonly requiredVersion = MINIMUM_COMPATIBLE_AGENT_SERVER_VERSION;
constructor(actualVersion: string) {
const message = `Agent Canvas requires agent-server ${MINIMUM_COMPATIBLE_AGENT_SERVER_VERSION} or newer; this backend is running ${actualVersion}. Please upgrade the agent-server backend.`;
super(message);
this.name = "AgentServerUnsupportedVersionError";
this.message = message;
this.actualVersion = actualVersion;
}
}
export class AgentServerUnknownVersionError extends AgentServerUnavailableError {
readonly code = AGENT_SERVER_UNKNOWN_VERSION_ERROR_CODE;
readonly actualVersion: string | null;
readonly requiredVersion = MINIMUM_COMPATIBLE_AGENT_SERVER_VERSION;
constructor(actualVersion: string | null) {
const reported = actualVersion ? ` It reported "${actualVersion}".` : "";
const message =
`Could not determine this backend's agent-server version.${reported} ` +
`Agent Canvas requires agent-server ${MINIMUM_COMPATIBLE_AGENT_SERVER_VERSION} ` +
"or newer, but this backend did not return a valid version from " +
"/server_info. Restart or rebuild the agent-server backend, then try again.";
super(message);
this.name = "AgentServerUnknownVersionError";
this.message = message;
this.actualVersion = actualVersion;
}
}
export const isAgentServerUnsupportedVersionError = (
error: unknown,
): error is AgentServerUnsupportedVersionError =>
error instanceof AgentServerUnsupportedVersionError ||
(typeof error === "object" &&
error !== null &&
"code" in error &&
error.code === AGENT_SERVER_UNSUPPORTED_VERSION_ERROR_CODE);
export const isAgentServerUnknownVersionError = (
error: unknown,
): error is AgentServerUnknownVersionError =>
error instanceof AgentServerUnknownVersionError ||
(typeof error === "object" &&
error !== null &&
"code" in error &&
error.code === AGENT_SERVER_UNKNOWN_VERSION_ERROR_CODE);
/**
* Returns true when the agent-server probe failed with HTTP 401.
* In public mode this means the stored key is stale (server restarted
* with a different `LOCAL_BACKEND_API_KEY`). Only meaningful when
* auth is required — a 401 in local mode is a misconfiguration, not a
* key-rotation event. Uses {@link isAuthRequired} so both the build-time
* `VITE_AUTH_REQUIRED` flag and the runtime `window.__AGENT_CANVAS_AUTH_REQUIRED__`
* injection (used by pre-built static binaries) are honoured.
*/
export const isAgentServerAuthError = (error: unknown): boolean =>
isAuthRequired() && isSdkHttpStatusError(error, 401);
export function clearCachedAgentServerInfo() {
cachedAgentServerInfo = null;
cachedAgentServerInfoHost = null;
}
export function getCachedAgentServerInfo(options?: {
host?: string | null;
}): AgentServerInfo | null {
if (options?.host && options.host !== cachedAgentServerInfoHost) {
return null;
}
return cachedAgentServerInfo;
}
export function isAgentServerToolAvailable(toolName: string) {
const availableTools = getAdvertisedTools(cachedAgentServerInfo);
if (!Array.isArray(availableTools)) {
return true;
}
return availableTools.includes(toolName);
}
export function isSdkHttpError(error: unknown) {
return (
error instanceof Error &&
error.name === "HttpError" &&
"status" in error &&
typeof error.status === "number"
);
}
/**
* Narrows an SDK HTTP error to a specific status code.
* Use instead of manually casting `(err as { status: number }).status`.
*/
export function isSdkHttpStatusError(error: unknown, status: number): boolean {
return (
isSdkHttpError(error) && (error as { status: number }).status === status
);
}
function normalizeAgentServerInfoVersion(version: unknown): string | null {
if (typeof version !== "string") return null;
const trimmed = version.trim();
return trimmed || null;
}
function getRawAgentServerSdkVersion(
serverInfo: AgentServerInfo,
): string | null {
return normalizeAgentServerInfoVersion(serverInfo.sdk_version);
}
function getRawAgentServerVersion(serverInfo: AgentServerInfo): string | null {
return (
normalizeAgentServerInfoVersion(serverInfo.version) ??
getRawAgentServerSdkVersion(serverInfo)
);
}
function getComparableAgentServerVersion(
serverInfo: AgentServerInfo,
): string | null {
const version = getRawAgentServerVersion(serverInfo);
if (!version || version.toLowerCase() === UNKNOWN_AGENT_SERVER_VERSION) {
return null;
}
return version;
}
export function getDisplayAgentServerVersion(
serverInfo: AgentServerInfo,
): string | null {
const version = getComparableAgentServerVersion(serverInfo);
if (!version || !parseAgentServerVersion(version)) {
return null;
}
return version;
}
export function getDisplayAgentServerSdkVersion(
serverInfo: AgentServerInfo,
): string | null {
const version =
getRawAgentServerSdkVersion(serverInfo) ??
getComparableAgentServerVersion(serverInfo);
if (!version || version.toLowerCase() === UNKNOWN_AGENT_SERVER_VERSION) {
return null;
}
return version;
}
export function getCachedAgentServerSdkVersion(
host?: string | null,
): string | null {
if (!cachedAgentServerInfo) return null;
if (host && getEffectiveLocalBackend()?.host !== host) return null;
return getDisplayAgentServerSdkVersion(cachedAgentServerInfo);
}
export function getCachedAgentServerVersion(
host?: string | null,
): string | null {
if (!cachedAgentServerInfo) return null;
if (host && getEffectiveLocalBackend()?.host !== host) return null;
return getDisplayAgentServerVersion(cachedAgentServerInfo);
}
export function compareAgentServerVersions(actual: string, required: string) {
const parsedActual = parseAgentServerVersion(actual);
const parsedRequired = parseAgentServerVersion(required);
if (!parsedActual || !parsedRequired) {
return null;
}
for (const key of ["major", "minor", "patch"] as const) {
if (parsedActual[key] > parsedRequired[key]) {
return 1;
}
if (parsedActual[key] < parsedRequired[key]) {
return -1;
}
}
if (parsedActual.prerelease && !parsedRequired.prerelease) {
return -1;
}
if (!parsedActual.prerelease && parsedRequired.prerelease) {
return 1;
}
if (parsedActual.prerelease && parsedRequired.prerelease) {
return parsedActual.prerelease.localeCompare(parsedRequired.prerelease);
}
return 0;
}
function parseAgentServerVersion(version: string) {
const trimmed = version.trim().replace(/^v/, "");
const [withoutBuild] = trimmed.split("+");
const [core, prerelease] = withoutBuild.split("-", 2);
const parts = core.split(".");
if (parts.length !== 3) {
return null;
}
const [major, minor, patch] = parts.map((part) => Number(part));
if (
![major, minor, patch].every((part) => Number.isInteger(part) && part >= 0)
) {
return null;
}
return { major, minor, patch, prerelease };
}
export function assertAgentServerVersionIsSupported(
serverInfo: AgentServerInfo,
) {
const actualVersion = getComparableAgentServerVersion(serverInfo);
if (!actualVersion) {
clearCachedAgentServerInfo();
throw new AgentServerUnknownVersionError(
getRawAgentServerVersion(serverInfo),
);
}
const comparison = compareAgentServerVersions(
actualVersion,
MINIMUM_COMPATIBLE_AGENT_SERVER_VERSION,
);
if (comparison === null) {
clearCachedAgentServerInfo();
throw new AgentServerUnknownVersionError(actualVersion);
}
if (comparison < 0) {
clearCachedAgentServerInfo();
throw new AgentServerUnsupportedVersionError(actualVersion);
}
}
/**
* Validates a local agent-server backend with a two-step probe:
* 1. GET /api/settings — authenticates the configured session API key;
* a 401 throws an Error with message {@link INVALID_BACKEND_API_KEY_ERROR}.
* 2. GET /server_info — asserts the server meets the minimum version floor.
*
* Returns the display version string reported by the server, or `null` when
* the server does not report a parseable version. Throws on any failure.
*
* Used by both the backend health poller and the backend-form connection test
* so that auth-check semantics (status codes, error messages) stay in one place.
*/
export async function validateLocalBackend(
backend: Pick<Backend, "host" | "apiKey">,
timeout: number,
): Promise<string | null> {
const clientOptions = getAgentServerClientOptions({
host: backend.host,
sessionApiKey: backend.apiKey || null,
timeout,
});
try {
await new SettingsClient(clientOptions).getSettings();
const serverInfo = await new ServerClient(clientOptions).getServerInfo();
assertAgentServerVersionIsSupported(serverInfo);
return getDisplayAgentServerVersion(serverInfo);
} catch (error) {
if (isSdkHttpStatusError(error, 401)) {
throw new Error(INVALID_BACKEND_API_KEY_ERROR);
}
throw error;
}
}
export async function loadAgentServerInfo() {
// The probe is a *local* agent-server concern — it verifies the runtime
// hosting the GUI is reachable. It must NEVER run against the active
// backend when that backend is cloud, because cloud hosts don't
// expose /api/server_info and would fail with a CORS error besides.
const local = getEffectiveLocalBackend();
if (!local) {
clearCachedAgentServerInfo();
// Empty registry (NO_BACKEND sentinel) — the user has no backend
// configured at all. Throw so root.tsx shows the manage-backends
// modal instead of silently rendering a broken home page.
if (isNoBackend(getActiveBackend().backend)) {
throw new AgentServerUnavailableError("No backend configured", {
noBackendConfigured: true,
});
}
// Active backend is cloud — no local probe needed.
return null;
}
const clientOptions = getAgentServerClientOptions({
host: local.host,
sessionApiKey: local.apiKey || null,
timeout: AGENT_SERVER_INFO_TIMEOUT_MS,
});
let serverInfo: AgentServerInfo;
try {
serverInfo = (await new ServerClient(
clientOptions,
).getServerInfo()) as AgentServerInfo;
} catch (error) {
clearCachedAgentServerInfo();
// Preserve 401 so root.tsx can show the auth screen (public mode).
// All other HTTP errors (502, 503, etc.) mean the server is unreachable
// or misconfigured — treat them as unavailable.
if (isSdkHttpStatusError(error, 401)) {
throw error;
}
const details = error instanceof Error ? error.message : null;
throw new AgentServerUnavailableError(details);
}
assertAgentServerVersionIsSupported(serverInfo);
// /server_info is unprotected, so a stale session key still gets 200.
// In public mode, validate the key against a protected endpoint so a
// server restart with a new LOCAL_BACKEND_API_KEY surfaces immediately
// instead of letting the app load and fail on every subsequent call.
if (isAuthRequired()) {
try {
await new SettingsClient(clientOptions).getSettings();
} catch (error) {
// Only rethrow 401 — that means the stored key is invalid /
// rotated. Other HTTP errors (403, 5xx) and non-HTTP errors
// (network, timeout) are swallowed: the server *is* up (we just
// reached /server_info), so let the app proceed with an
// unvalidated key rather than blocking the UI.
// NOTE: If the connection drops between the /server_info and
// getSettings() probes, the app loads with an unvalidated key and
// subsequent 401s won't trigger the auth screen (they come from
// React Query hooks, not this bootstrap path). Acceptable for now
// since the window is narrow and a page refresh recovers.
if (isSdkHttpStatusError(error, 401)) {
throw error;
}
console.warn(
"[agent-server] getSettings() probe failed (non-401):",
error,
);
}
}
cachedAgentServerInfo = serverInfo;
cachedAgentServerInfoHost = clientOptions.host;
return serverInfo;
}
|