Spaces:
Running
Running
File size: 9,961 Bytes
31b3c47 | 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 | import { createHash } from "node:crypto";
import { getRequest } from "@tanstack/react-start/server";
import {
assertSameSiteRequest,
CrossSiteRequestError,
} from "../auth/isolation.server.ts";
import { assertAppDataServerOnly } from "./server-only.ts";
import {
CONNECTOR_TOKEN_HEADER,
ConnectorType,
type CallToolOptions,
type CallToolResult,
type ToolArgs,
} from "./types.ts";
assertAppDataServerOnly("app-data/client.server");
export const CONNECTORS_HOST_STAGING = "connectors.app-builder-testing.com";
export const CONNECTORS_HOST_PROD = "connectors.grok.me";
function env(key: string): string | undefined {
const v = process.env[key]?.trim();
return v || undefined;
}
function isLoopbackHost(host: string): boolean {
return host === "localhost" || host === "127.0.0.1" || host === "[::1]";
}
type InboundContext = {
token: string | null;
publicHost: string | null;
connectorsBase: string | null;
};
function connectorsBaseFor(publicHost: string | null): string | null {
const explicit = env("GROK_CONNECTORS_URL");
if (explicit) return explicit.replace(/\/+$/, "");
const host = publicHost?.toLowerCase();
if (!host || isLoopbackHost(host)) return null;
if (
host === "app-builder-testing.com" ||
host.endsWith(".app-builder-testing.com")
) {
return `https://${CONNECTORS_HOST_STAGING}`;
}
if (host === "grok.me" || host.endsWith(".grok.me")) {
return `https://${CONNECTORS_HOST_PROD}`;
}
return null;
}
function tryGetRequest(): Request | null {
try {
return getRequest() ?? null;
} catch {
return null;
}
}
function inboundContext(): InboundContext {
const req = tryGetRequest();
const xf = req?.headers.get("x-forwarded-host")?.split(",")[0]?.trim();
const publicHost =
(xf || req?.headers.get("host") || "").split(":")[0]?.trim() || null;
const headerToken = req?.headers.get(CONNECTOR_TOKEN_HEADER)?.trim() || null;
const envToken =
process.env.NODE_ENV === "production"
? null
: (env("GROK_CONNECTOR_ACCESS_TOKEN") ?? null);
return {
token: headerToken ?? envToken,
publicHost,
connectorsBase: connectorsBaseFor(publicHost),
};
}
export function resolveGateAppDataBase(): string | null {
return inboundContext().connectorsBase;
}
export function getConnectorAccessToken(): string | null {
return inboundContext().token;
}
type GateJson = {
ok?: boolean;
data?: unknown;
errorMessage?: string;
loginUrl?: string;
};
async function gatePost(
ctx: InboundContext,
body: Record<string, unknown>,
token: string,
): Promise<{ status: number; json: GateJson }> {
const base = ctx.connectorsBase;
if (!base) {
throw new Error(
"cannot resolve gate host (missing x-forwarded-host/host on the server request); " +
"open the app through the gated public URL so the gate can proxy and inject credentials",
);
}
if (!/^https?:\/\//i.test(base)) {
throw new Error(
`gate base must be absolute http(s) URL (got ${base}); refusing relative fetch`,
);
}
const headers: Record<string, string> = {
"content-type": "application/json",
accept: "application/json",
authorization: `Bearer ${token}`,
};
if (ctx.publicHost) {
headers["x-forwarded-host"] = ctx.publicHost;
}
const res = await fetch(`${base}/call-tool`, {
method: "POST",
headers,
body: JSON.stringify(body),
redirect: "manual",
});
let json: GateJson = {};
const text = await res.text();
if (text) {
try {
json = JSON.parse(text) as GateJson;
} catch {
json = {
ok: false,
errorMessage: `gate non-JSON response (HTTP ${res.status}): ${text.slice(0, 200)}`,
};
}
}
return { status: res.status, json };
}
function gateSigninUrl(ctx: InboundContext): string | undefined {
const base = ctx.connectorsBase;
if (!base) return undefined;
try {
const connectorsHost = new URL(base).host.toLowerCase();
const gateHost = connectorsHost.replace(/^connectors\./, "gate.");
if (gateHost === connectorsHost) return undefined;
const publicHost = ctx.publicHost?.toLowerCase();
const gated =
publicHost && !isLoopbackHost(publicHost)
? `https://${publicHost}`
: undefined;
const signin = `https://${gateHost}/__gate/signin`;
return gated
? `${signin}?return_to=${encodeURIComponent(gated)}`
: signin;
} catch {
return undefined;
}
}
function missingAuthResult(ctx: InboundContext): CallToolResult {
const loginUrl = gateSigninUrl(ctx);
return {
ok: false,
data: null,
loginRequired: true,
errorMessage:
"missing_connector_token: open this app through the edge gate " +
"(the server must receive x-connector-access-token on the inbound request)",
...(loginUrl ? { loginUrl } : {}),
};
}
function crossSiteBlockedResult(): CallToolResult | null {
try {
assertSameSiteRequest();
return null;
} catch (e) {
if (e instanceof CrossSiteRequestError) {
return { ok: false, data: null, errorMessage: e.message };
}
return null;
}
}
const FAILURE_MEMO_TTL_MS = 5_000;
const failureMemo = new Map<string, { at: number; result: CallToolResult }>();
function tokenIdentityKey(token: string): string {
const payload = token.split(".")[1];
if (payload) {
try {
const claims: unknown = JSON.parse(
Buffer.from(payload, "base64url").toString("utf8"),
);
if (claims && typeof claims === "object" && !Array.isArray(claims)) {
const { sub, team_id: teamId } = claims as {
sub?: unknown;
team_id?: unknown;
};
if (typeof sub === "string" && sub) {
return createHash("sha256")
.update(
JSON.stringify([sub, typeof teamId === "string" ? teamId : null]),
)
.digest("base64url");
}
}
} catch {
// Not a decodable JWT payload: fall through to hashing the raw token.
}
}
return createHash("sha256").update(token).digest("base64url");
}
function memoizedFailure(key: string | null): CallToolResult | null {
if (!key) return null;
const hit = failureMemo.get(key);
if (!hit) return null;
if (Date.now() - hit.at > FAILURE_MEMO_TTL_MS) {
failureMemo.delete(key);
return null;
}
return hit.result;
}
function memoizeFailure(
key: string | null,
result: CallToolResult,
): CallToolResult {
if (!key) return result;
const now = Date.now();
for (const [staleKey, entry] of failureMemo) {
if (now - entry.at > FAILURE_MEMO_TTL_MS) failureMemo.delete(staleKey);
}
failureMemo.set(key, { at: now, result });
return result;
}
export function failureMemoSize(): number {
return failureMemo.size;
}
function safeMemoKey(parts: unknown[]): string | null {
try {
return JSON.stringify(parts);
} catch {
return null;
}
}
function nonPostBlockedResult(): CallToolResult | null {
const req = tryGetRequest();
if (!req || req.method === "POST") return null;
return {
ok: false,
data: null,
errorMessage:
`blocked ${req.method} inbound request: connector calls must run inside ` +
'a createServerFn({ method: "POST" }) handler',
};
}
export async function callTool(
toolName: string,
args: ToolArgs,
options: CallToolOptions,
): Promise<CallToolResult> {
const blocked = crossSiteBlockedResult() ?? nonPostBlockedResult();
if (blocked) return blocked;
const ctx = inboundContext();
const token = options.token ?? ctx.token;
if (!token) {
return missingAuthResult(ctx);
}
const connectorType = options.connectorType;
if (!connectorType) {
return {
ok: false,
data: null,
errorMessage:
"connectorType is required: pass the connector type granted to this app " +
"(e.g. { connectorType: ConnectorType.GoogleDrive })",
};
}
const memoKey = safeMemoKey([
toolName,
args,
connectorType,
options?.connectorCatalogId ?? null,
tokenIdentityKey(token),
]);
const memoized = memoizedFailure(memoKey);
if (memoized) {
return memoized;
}
const fail = (errorMessage: string): CallToolResult =>
memoizeFailure(memoKey, { ok: false, data: null, errorMessage });
if (connectorType === ConnectorType.Mcp && !options?.connectorCatalogId) {
return {
ok: false,
data: null,
errorMessage: "connectorCatalogId is required when connectorType is Mcp",
};
}
try {
const { status, json } = await gatePost(
ctx,
{
host: ctx.publicHost ?? undefined,
connector_type: connectorType,
tool_name: toolName,
arguments: args,
connector_catalog_id: options.connectorCatalogId,
},
token,
);
if (status === 401) {
const loginUrl =
gateSigninUrl(ctx) ??
(typeof json.loginUrl === "string" && json.loginUrl
? json.loginUrl
: undefined);
return {
ok: false,
data: null,
loginRequired: true,
errorMessage: json.errorMessage ?? "login required",
...(loginUrl ? { loginUrl } : {}),
};
}
if (status === 403) {
return fail(json.errorMessage ?? "access_denied");
}
if (json.errorMessage && json.ok === false) {
return fail(json.errorMessage);
}
if (status >= 400 && json.ok !== true) {
return fail(json.errorMessage ?? `HTTP ${status}`);
}
if (json.ok === false) {
return fail(json.errorMessage ?? "tool error");
}
return { ok: true, data: json.data ?? null };
} catch (e) {
return fail(e instanceof Error ? e.message : String(e));
}
}
export {
ConnectorType,
GoogleDriveTools,
CONNECTOR_TOKEN_HEADER,
} from "./types.ts";
export type {
CallToolResult,
CallToolOptions,
ToolArgs,
ConnectorTypeName,
} from "./types.ts";
export { isLoginRequired, redirectToLoginIfRequired } from "./login.ts";
|