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.
/**
* PKCE utilities using the Web Crypto API.
* Ported from Mastra Code (mastra-ai/mastra, `mastracode/sdk/src/auth/pkce.ts`), Apache-2.0.
*/
export function base64urlEncode(bytes: Uint8Array): string {
let binary = "";
for (const byte of bytes) {
binary += String.fromCharCode(byte);
}
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
}
/** Generate a PKCE code verifier and its S256 challenge. */
export async function generatePKCE(): Promise<{
verifier: string;
challenge: string;
}> {
const verifierBytes = new Uint8Array(32);
crypto.getRandomValues(verifierBytes);
const verifier = base64urlEncode(verifierBytes);
const data = new TextEncoder().encode(verifier);
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
const challenge = base64urlEncode(new Uint8Array(hashBuffer));
return { verifier, challenge };
}