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 * as Predicate from "effect/Predicate";
import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer";
import * as NodeSocket from "@effect/platform-node/NodeSocket";
import {
AuthAccessTokenType,
AuthEnvironmentBootstrapTokenType,
AuthTokenExchangeGrantType,
OrchestrationThreadDetailSnapshot,
WsRpcGroup,
} from "@akeru/contracts";
import { assert } from "@effect/vitest";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Queue from "effect/Queue";
import * as Schema from "effect/Schema";
import {
FetchHttpClient,
HttpClient,
HttpClientRequest,
HttpClientResponse,
HttpServer,
} from "effect/unstable/http";
import { RpcClient, RpcSerialization } from "effect/unstable/rpc";
import * as Socket from "effect/unstable/socket/Socket";
import { SqlitePersistenceMemory } from "./persistence/Layers/Sqlite.ts";
import * as ServerEnvironment from "./environment/ServerEnvironment.ts";
import * as ServerSecretStore from "./auth/ServerSecretStore.ts";
import * as EnvironmentAuth from "./auth/EnvironmentAuth.ts";
import * as Data from "effect/Data";
import { defaultDesktopBootstrapToken, testEnvironmentDescriptor } from "./serverTestFixtures.ts";
export const decodeTransferThreadSnapshot = Schema.decodeUnknownEffect(
Schema.fromJsonString(OrchestrationThreadDetailSnapshot),
);
export const collectQueueUntil = Effect.fn("TransferBudget.collectQueueUntil")(function* <A>(
queue: Queue.Queue<A>,
predicate: (value: A) => boolean,
waitDescription: string,
) {
return yield* Effect.gen(function* () {
const values: A[] = [];
while (true) {
const value = yield* Queue.take(queue);
values.push(value);
if (predicate(value)) return values;
}
}).pipe(
Effect.timeoutOrElse({
duration: "10 seconds",
orElse: () => Effect.die(new Error(`Timed out waiting for ${waitDescription}`)),
}),
);
});
export const authTestLayer = () =>
EnvironmentAuth.layer.pipe(
Layer.provide(SqlitePersistenceMemory),
Layer.provide(ServerSecretStore.layer),
Layer.provide(
Layer.mock(ServerEnvironment.ServerEnvironmentIdentity)({
getEnvironmentId: Effect.succeed(testEnvironmentDescriptor.environmentId),
}),
),
);
export const parseSessionCookieFromWsUrl = (wsUrl: string): ParseSessionCookieFromWsUrlResult => {
const next = new URL(wsUrl);
const cookie = next.hash.startsWith("#cookie=")
? decodeURIComponent(next.hash.slice("#cookie=".length))
: null;
next.hash = "";
return {
cookie,
url: next.toString(),
};
};
export const wsRpcProtocolLayer = (wsUrl: string) => {
const { cookie, url } = parseSessionCookieFromWsUrl(wsUrl);
const webSocketConstructorLayer = Layer.succeed(
Socket.WebSocketConstructor,
(socketUrl, protocols) => {
const socket: Pick<globalThis.WebSocket, "close" | "readyState"> =
new NodeSocket.NodeWS.WebSocket(
socketUrl,
protocols,
cookie ? { headers: { cookie } } : undefined,
);
// SAFETY: ws supports the send and event listener APIs consumed by Effect; DOM dispatchEvent and URL are unused.
return socket as globalThis.WebSocket;
},
);
return RpcClient.layerProtocolSocket().pipe(
Layer.provide(Socket.layerWebSocket(url).pipe(Layer.provide(webSocketConstructorLayer))),
Layer.provide(RpcSerialization.layerJson),
);
};
export const createWsRpcClient = RpcClient.make(WsRpcGroup);
export type WsRpcClient = Effect.Success<typeof createWsRpcClient>;
export const withWsRpcClient = <A, E, R>(
wsUrl: string,
f: (client: WsRpcClient) => Effect.Effect<A, E, R>,
) => createWsRpcClient.pipe(Effect.flatMap(f), Effect.provide(wsRpcProtocolLayer(wsUrl)));
export const withFirstWsAckHeld = (
wsUrl: string,
held: Deferred.Deferred<void>,
release: Deferred.Deferred<void>,
) => {
let holdNextAck = true;
return Layer.effect(RpcClient.Protocol)(
Effect.map(RpcClient.Protocol, (protocol) =>
RpcClient.Protocol.of({
...protocol,
send: (clientId, request, transferables) => {
const send = protocol.send(clientId, request, transferables);
if (!Predicate.isTagged(request, "Ack") || !holdNextAck) {
return send;
}
holdNextAck = false;
return Deferred.succeed(held, undefined).pipe(
Effect.andThen(Deferred.await(release)),
Effect.andThen(send),
);
},
}),
),
).pipe(Layer.provide(wsRpcProtocolLayer(wsUrl)));
};
export const appendSessionCookieToWsUrl = (url: string, sessionCookieHeader: string) => {
const isAbsoluteUrl = /^[a-zA-Z][a-zA-Z\d+.-]*:/.test(url);
const next = new URL(url, "http://localhost");
next.hash = `cookie=${encodeURIComponent(sessionCookieHeader)}`;
return isAbsoluteUrl ? next.toString() : `${next.pathname}${next.search}${next.hash}`;
};
export const getHttpServerUrl = (pathname = "") =>
Effect.gen(function* () {
const server = yield* HttpServer.HttpServer;
const address = server.address;
if (!Predicate.isTagged(address, "TcpAddress"))
return yield* Effect.die("Test server must listen on TCP");
return `http://127.0.0.1:${address.port}${pathname}`;
});
export const bootstrapBrowserSession = (
credential = defaultDesktopBootstrapToken,
options?: {
readonly headers?: Record<string, string>;
},
) =>
Effect.gen(function* () {
const bootstrapUrl = yield* getHttpServerUrl("/api/auth/browser-session");
const response = yield* fetchEffect(bootstrapUrl, {
method: "POST",
headers: {
"content-type": "application/json",
...options?.headers,
},
body: jsonRequestBody({
credential,
}),
});
const body = yield* responseJsonEffect<{
readonly authenticated: boolean;
readonly sessionMethod: string;
readonly expiresAt: string;
}>(response);
return {
response,
body,
cookie: response.headers["set-cookie"],
};
});
export const exchangeAccessToken = (
credential = defaultDesktopBootstrapToken,
options?: {
readonly headers?: Record<string, string>;
readonly scope?: string;
readonly clientMetadata?: {
readonly label?: string;
readonly deviceType?: string;
readonly os?: string;
};
},
) =>
Effect.gen(function* () {
const tokenUrl = yield* getHttpServerUrl("/oauth/token");
const response = yield* fetchEffect(tokenUrl, {
method: "POST",
headers: {
"content-type": "application/x-www-form-urlencoded",
...options?.headers,
},
body: new URLSearchParams({
grant_type: AuthTokenExchangeGrantType,
subject_token: credential,
subject_token_type: AuthEnvironmentBootstrapTokenType,
requested_token_type: AuthAccessTokenType,
scope:
options?.scope ?? "orchestration:read orchestration:operate access:read access:write",
...(options?.clientMetadata?.label ? { client_label: options.clientMetadata.label } : {}),
...(options?.clientMetadata?.deviceType
? { client_device_type: options.clientMetadata.deviceType }
: {}),
...(options?.clientMetadata?.os ? { client_os: options.clientMetadata.os } : {}),
}).toString(),
});
const body = yield* responseJsonEffect<{
readonly access_token?: string;
readonly issued_token_type?: string;
readonly token_type?: string;
readonly expires_in?: number;
readonly scope?: string;
readonly _tag?: string;
readonly code?: string;
readonly reason?: string;
readonly traceId?: string;
}>(response);
return {
response,
body,
};
});
export class AuthenticationGetterError extends Data.TaggedError("AuthenticationGetterError")<{
readonly message: string;
}> {}
export class TestHttpRequestError extends Data.TaggedError("TestHttpRequestError")<{
readonly cause: unknown;
}> {}
export const testRequestUrl = (input: Parameters<typeof fetch>[0]): string => {
const value = input.toString();
if (!/^https?:\/\//i.test(value)) {
return value;
}
const url = new URL(value);
return `${url.pathname}${url.search}`;
};
const decodeHttpMethod = Schema.decodeUnknownSync(
Schema.Literals(["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS", "TRACE"]),
);
export const fetchEffect = (input: Parameters<typeof fetch>[0], init?: RequestInit) => {
const method = decodeHttpMethod(init?.method ?? "GET");
const headers = Object.fromEntries(new Headers(init?.headers));
const request = HttpClientRequest.make(method)(testRequestUrl(input), {
headers,
}).pipe(
Predicate.isString(init?.body)
? HttpClientRequest.bodyText(init.body, headers["content-type"] ?? "application/json")
: (request) => request,
);
const effect = HttpClient.execute(request);
return (
init?.redirect === "manual"
? effect.pipe(Effect.provideService(FetchHttpClient.RequestInit, { redirect: "manual" }))
: effect
).pipe(Effect.mapError((cause) => new TestHttpRequestError({ cause })));
};
export const jsonRequestBody = <Value>(value: Value): string => {
return JSON.stringify(value);
};
// SAFETY: The test selects A to match the fixture served by this request; production RPC uses schema decoders.
export const responseJsonEffect = <A>(response: HttpClientResponse.HttpClientResponse) =>
response.json.pipe(
Effect.map((json) => json as A),
Effect.mapError((cause) => new TestHttpRequestError({ cause })),
);
export const responseOk = (response: HttpClientResponse.HttpClientResponse) =>
response.status >= 200 && response.status < 300;
export const getAuthenticatedSessionCookieHeader = (credential = defaultDesktopBootstrapToken) =>
Effect.gen(function* () {
const { response, cookie } = yield* bootstrapBrowserSession(credential);
if (!responseOk(response)) {
return yield* new AuthenticationGetterError({
message: `Expected bootstrap session response to succeed, got ${response.status}`,
});
}
if (!cookie) {
return yield* new AuthenticationGetterError({
message: "Expected bootstrap session response to set a cookie.",
});
}
return cookie.split(";")[0] ?? cookie;
});
export const getAuthenticatedBearerSessionToken = (credential = defaultDesktopBootstrapToken) =>
Effect.gen(function* () {
const { response, body } = yield* exchangeAccessToken(credential);
if (!responseOk(response)) {
return yield* new AuthenticationGetterError({
message: `Expected bearer bootstrap response to succeed, got ${response.status}`,
});
}
if (!body.access_token) {
return yield* new AuthenticationGetterError({
message: "Expected token exchange response to include an access token.",
});
}
return body.access_token;
});
export const extractSessionTokenFromSetCookie = (cookieHeader: string): string => {
const [nameValue] = cookieHeader.split(";", 1);
const token = nameValue?.split("=", 2)[1];
if (!token) {
throw new Error("Expected session cookie header to contain a token value.");
}
return token;
};
export const splitHeaderTokens = (value: string | null | undefined) =>
(value ?? "")
.split(",")
.map((token) => token.trim())
.filter((token) => token.length > 0)
.toSorted();
export const assertBrowserApiCorsResponseHeaders = (
headers: Readonly<Record<string, string | undefined>>,
options?: {
readonly origin?: string;
readonly credentials?: boolean;
},
) => {
assert.equal(headers["access-control-allow-origin"], options?.origin ?? "*");
assert.equal(
headers["access-control-allow-credentials"],
options?.credentials ? "true" : undefined,
);
};
export const assertBrowserApiCorsPreflightHeaders = (
headers: Readonly<Record<string, string | undefined>>,
options?: {
readonly origin?: string;
readonly credentials?: boolean;
},
) => {
assertBrowserApiCorsResponseHeaders(headers, options);
assert.deepEqual(splitHeaderTokens(headers["access-control-allow-methods"] ?? null), [
"GET",
"OPTIONS",
"POST",
]);
assert.deepEqual(splitHeaderTokens(headers["access-control-allow-headers"]), [
"authorization",
"b3",
"content-type",
"traceparent",
]);
};
export const crossOriginClientOrigin = "http://remote-client.test:3773";
export const getWsServerUrl = (
pathname = "",
options?: { authenticated?: boolean; credential?: string },
) =>
Effect.gen(function* () {
const server = yield* HttpServer.HttpServer;
const address = server.address;
if (!Predicate.isTagged(address, "TcpAddress"))
return yield* Effect.die("Test server must listen on TCP");
const baseUrl = `ws://127.0.0.1:${address.port}${pathname}`;
if (options?.authenticated === false) {
return baseUrl;
}
return appendSessionCookieToWsUrl(
baseUrl,
yield* getAuthenticatedSessionCookieHeader(options?.credential),
);
});
// Mirrors NodeHttpServer.layerTest, which does not expose server options,
// with the production `websocket: { perMessageDeflate: true }` setting.
export const NodeHttpServerTestWithWsDeflate = HttpServer.layerTestClient.pipe(
Layer.provide(
Layer.fresh(FetchHttpClient.layer).pipe(
Layer.provide(Layer.succeed(FetchHttpClient.RequestInit)({ keepalive: false })),
),
),
Layer.provideMerge(
Layer.unwrap(
Effect.map(
Effect.promise(() => import("node:http")),
(NodeHttp) =>
NodeHttpServer.layer(NodeHttp.createServer, {
port: 0,
websocket: { perMessageDeflate: true },
}),
),
),
),
);
type ParseSessionCookieFromWsUrlResult = { readonly cookie: string | null; readonly url: string };