Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import { remoteHttpClientLayer } from "@akeru/client-runtime/rpc";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/unstable/http";
import { readDesktopPrimaryBearerToken } from "./desktopAuth";
import { resolvePrimaryEnvironmentHttpUrl } from "./target";
function isSameOriginBrowserPrimary(): boolean {
if (
typeof window === "undefined" ||
window.desktopBridge !== undefined ||
!window.location.origin.startsWith("http")
) {
return false;
}
return new URL(resolvePrimaryEnvironmentHttpUrl("/")).origin === window.location.origin;
}
function withPrimaryBearerToken(client: HttpClient.HttpClient): HttpClient.HttpClient {
return client.pipe(
HttpClient.mapRequestEffect((request) =>
Effect.promise(readDesktopPrimaryBearerToken).pipe(
Effect.map((bearerToken) =>
bearerToken ? HttpClientRequest.bearerToken(request, bearerToken) : request,
),
),
),
);
}
export function makePrimaryEnvironmentHttpLayer() {
return Layer.unwrap(
Effect.sync(() => {
const baseLayer = remoteHttpClientLayer(globalThis.fetch);
if (isSameOriginBrowserPrimary()) {
return Layer.merge(
baseLayer,
Layer.succeed(FetchHttpClient.RequestInit, { credentials: "include" }),
);
}
const bearerClientLayer = Layer.effect(
HttpClient.HttpClient,
Effect.map(HttpClient.HttpClient, withPrimaryBearerToken),
).pipe(Layer.provide(baseLayer));
return Layer.merge(
bearerClientLayer,
Layer.succeed(FetchHttpClient.RequestInit, { credentials: "omit" }),
);
}),
);
}
export const primaryEnvironmentHttpLayer = makePrimaryEnvironmentHttpLayer();