UNPKG

axiom

Version:

Axiom AI SDK provides - an API to wrap your AI calls with observability instrumentation. - offline evals - online evals

409 lines (401 loc) 15 kB
import { AxiomCLIError } from "./chunk-ISSDOC43.js"; // src/cli/auth/config.ts import { promises as fs } from "fs"; import path from "path"; import os from "os"; var CONFIG_FILENAME = "config.json"; var CONFIG_DIR_NAME = "axiom"; function getConfigDir() { const platform = process.platform; const homeDir = os.homedir(); const xdgConfigHome = process.env.XDG_CONFIG_HOME; if (xdgConfigHome) { return path.join(xdgConfigHome, CONFIG_DIR_NAME); } if (platform === "win32") { const appData = process.env.APPDATA; if (appData) { return path.join(appData, CONFIG_DIR_NAME); } return path.join(homeDir, "AppData", "Roaming", CONFIG_DIR_NAME); } return path.join(homeDir, ".config", CONFIG_DIR_NAME); } function getGlobalConfigPath() { return path.join(getConfigDir(), CONFIG_FILENAME); } async function loadGlobalConfig() { const configPath = getGlobalConfigPath(); try { const content = await fs.readFile(configPath, "utf-8"); return JSON.parse(content); } catch (error) { if (error.code === "ENOENT") { return { profiles: {} }; } throw error; } } async function saveGlobalConfig(config) { const configPath = getGlobalConfigPath(); const configDir = path.dirname(configPath); const content = JSON.stringify(config, null, 2); await fs.mkdir(configDir, { recursive: true, mode: 448 }); await fs.writeFile(configPath, content, "utf-8"); await fs.chmod(configPath, 384); } function getActiveProfile(config) { const profileName = config.active_profile; if (!profileName) return null; const profile = config.profiles[profileName]; if (!profile) return null; return profile; } // src/cli/auth/global-auth.ts var authContext = null; function getAuthContext() { return authContext; } async function setupGlobalAuth() { const config = await loadGlobalConfig(); const profile = getActiveProfile(config); if (profile) { authContext = { token: profile.token, url: profile.url, orgId: profile.org_id }; } return authContext; } // src/config/index.ts import "zod"; // src/cli/auth/oauth.ts import { randomBytes, createHash } from "crypto"; var OAUTH_CLIENT_ID = "264d906a404efc209b027f6595e6b616"; var OAUTH_AUTH_PATH = "/oauth/authorize"; var OAUTH_TOKEN_PATH = "/oauth/token"; var OAuth = class { constructor(oauthBaseUrl) { this.oauthBaseUrl = oauthBaseUrl; } static generateCodeVerifier() { return randomBytes(32).toString("base64url"); } static generateCodeChallenge(verifier) { return createHash("sha256").update(verifier).digest("base64url"); } static generateState() { return randomBytes(16).toString("hex"); } buildAuthUrl(params) { const url = new URL(OAUTH_AUTH_PATH, this.oauthBaseUrl); url.searchParams.set("client_id", OAUTH_CLIENT_ID); url.searchParams.set("redirect_uri", params.redirectUri); url.searchParams.set("response_type", "code"); url.searchParams.set("state", params.state); url.searchParams.set("code_challenge", params.codeChallenge); url.searchParams.set("code_challenge_method", "S256"); url.searchParams.set("scope", "*"); return url.toString(); } async exchangeCodeForToken(params) { const tokenUrl = new URL(OAUTH_TOKEN_PATH, this.oauthBaseUrl); const body = new URLSearchParams({ grant_type: "authorization_code", client_id: OAUTH_CLIENT_ID, code: params.code, redirect_uri: params.redirectUri, code_verifier: params.codeVerifier }); const response = await fetch(tokenUrl.toString(), { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: body.toString() }); if (!response.ok) { const errorText = await response.text(); throw new Error(`Token exchange failed: ${response.status} ${errorText}`); } const data = await response.json(); return data.access_token; } }; // src/cli/auth/api.ts async function fetchOrganizations(token, apiBaseUrl) { const response = await fetch(`${apiBaseUrl}/v2/orgs`, { headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" } }); if (!response.ok) { throw new AxiomCLIError( `Failed to fetch organizations: ${response.status} ${response.statusText}` ); } const data = await response.json(); return data; } async function verifyToken(token, orgId, apiBaseUrl) { const response = await fetch(`${apiBaseUrl}/v2/user`, { headers: { Authorization: `Bearer ${token}`, "X-Axiom-Org-Id": orgId, "Content-Type": "application/json" } }); return response.ok; } // src/cli/auth/callback-server.ts import http from "http"; function escapeHtml(text) { const map = { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#039;", "`": "&#96;" }; return text.replace(/[&<>"']/g, (m) => map[m] || m); } var SVG_LOGO = `<svg width="124" height="24" viewBox="0 0 124 24" fill="none" xmlns="http://www.w3.org/2000/svg" class="logo"> <path d="M42.9919 16.8116H36.3696L35.5537 19.1572C35.4209 19.539 34.9714 19.8513 34.5548 19.8513H30.9779C30.5614 19.8513 30.3459 19.5449 30.499 19.1703L36.9816 3.31644C37.1346 2.9419 37.6009 2.63546 38.0174 2.63546H41.3718C41.7883 2.63546 42.2541 2.94207 42.4067 3.3168L48.8634 19.17C49.016 19.5447 48.8 19.8513 48.3835 19.8513H44.8067C44.3901 19.8513 43.9406 19.539 43.8078 19.1572L42.9919 16.8116ZM41.8232 13.4223L39.6807 7.18148L37.5383 13.4223H41.8232ZM64.1105 19.8513C63.694 19.8513 63.1767 19.5694 62.9611 19.2247L59.9029 14.3369L56.8447 19.2247C56.6291 19.5694 56.1119 19.8513 55.6953 19.8513H51.3392C50.9227 19.8513 50.7754 19.5801 51.0119 19.2486L56.8978 11.0013L51.5315 3.24414C51.2999 2.90937 51.4513 2.63546 51.8679 2.63546H55.89C56.3066 2.63546 56.8268 2.91563 57.0461 3.25807L59.9029 7.71959L62.7343 3.25955C62.9522 2.9163 63.4713 2.63546 63.8879 2.63546H67.938C68.3545 2.63546 68.5048 2.90868 68.272 3.24261L62.8801 10.9743L68.7935 19.2489C69.0303 19.5802 68.8832 19.8513 68.4666 19.8513H64.1105ZM76.525 19.119C76.525 19.5218 76.1841 19.8513 75.7675 19.8513H72.5522C72.1356 19.8513 71.7947 19.5218 71.7947 19.119V3.36771C71.7947 2.96498 72.1356 2.63546 72.5522 2.63546H75.7675C76.1841 2.63546 76.525 2.96498 76.525 3.36771V19.119ZM79.3736 11.1896C79.3736 6.18625 83.2688 2.15134 89.2511 2.15134C95.2334 2.15134 99.1289 6.18625 99.1289 11.1896C99.1289 16.2199 95.2334 20.2548 89.2511 20.2548C83.2688 20.2548 79.3736 16.2199 79.3736 11.1896ZM94.2873 11.1896C94.2873 8.58038 92.3953 6.21307 89.2511 6.21307C86.1349 6.21307 84.2149 8.58038 84.2149 11.1896C84.2149 13.7989 86.1349 16.2199 89.2511 16.2199C92.3675 16.2199 94.2873 13.7989 94.2873 11.1896ZM119.318 19.8513C118.902 19.8513 118.517 19.5245 118.463 19.1251L117.225 9.89847L113.65 19.1652C113.505 19.5426 113.045 19.8513 112.628 19.8513H111.055C110.638 19.8513 110.179 19.5423 110.034 19.1645L106.457 9.81766L105.218 19.125C105.165 19.5245 104.781 19.8513 104.364 19.8513H101.344C100.927 19.8513 100.633 19.5249 100.69 19.1259L102.932 3.3609C102.988 2.96191 103.376 2.63546 103.792 2.63546H106.59C107.007 2.63546 107.469 2.94351 107.617 3.32002L111.827 14.041L116.064 3.31949C116.213 2.94327 116.676 2.63546 117.092 2.63546H119.89C120.307 2.63546 120.694 2.96191 120.751 3.3609L122.993 19.1259C123.05 19.5249 122.755 19.8513 122.339 19.8513H119.318ZM23.9616 15.6531L18.8054 6.97021C18.5689 6.57115 17.9863 6.24465 17.5106 6.24465H14.2915C13.5433 6.24465 13.2365 5.73171 13.6097 5.1048L15.375 2.13986C15.5151 1.90455 15.5148 1.61487 15.3743 1.37981C15.2337 1.14476 14.9741 1 14.6933 1H10.2025C9.72681 1 9.14291 1.32577 8.90491 1.72395L0.17865 16.3217C-0.0594434 16.7199 -0.0595348 17.3715 0.178285 17.7698L2.42362 21.5297C2.79777 22.1561 3.41129 22.1569 3.78699 21.5313L5.54143 18.6103C5.91722 17.9847 6.53065 17.9854 6.9048 18.6119L8.49538 21.2754C8.7332 21.6737 9.317 21.9995 9.79273 21.9995H20.1698C20.6455 21.9995 21.2293 21.6737 21.4672 21.2754L23.959 17.1028C24.1968 16.7045 24.198 16.0521 23.9616 15.6531ZM16.9981 15.2352C17.3699 15.8629 17.0619 16.3765 16.3136 16.3765H8.24192C7.49372 16.3765 7.1876 15.864 7.56175 15.2375L11.6007 8.47417C11.9748 7.84772 12.5869 7.84774 12.9611 8.47421L16.9981 15.2352Z" fill="#121224"/> </svg>`; function renderCallbackPage(error) { const errorClass = error ? ' class="error"' : ""; const errorMessage = error ? escapeHtml(error) : ""; return `<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Axiom</title> <link rel="icon" href="https://app.axiom.co/static/favicon.ico"> <meta name="description" content="Axiom CLI"> <meta name="viewport" content="width=device-width,initial-scale=1"> <style> html, body, .root { width: 100%; height: 100%; text-rendering: optimizeLegibility; -webkit-font-smoothing: antialiased; } body { color: #334155; font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji; font-size: 14px; font-weight: 500; font-variant: tabular-nums; line-height: 1.5; background-color: #fff; font-feature-settings: "tnum"; margin: 0; } h1, h2, h3, h4, h5, h6 { margin-top: 0; margin-bottom: .5em; font-weight: 500; } p { margin-top: 0; margin-bottom: 1em; } h2 { font-size: 16px; font-weight: 600; } .root { display: flex; align-items: center; justify-content: center; } .logo { width: 92px; float: left; position: absolute; top: 16px; left: 16px; } .center p { padding: 8px 0; } .error .center { color: #bf0e08; } </style> </head> <body${errorClass}> <div class="root"> <a class="" target="_blank" rel="noopener noreferrer" href="https://axiom.co"> ${SVG_LOGO} </svg> </a> <div class="center"> ${error ? `<h2 id="msg">Login failed</h2> <p id="details">${errorMessage}</p>` : `<h2 id="msg">Login successful</h2> <p id="details">You can close this page and return to your CLI.</p>`} </div> </div> <script> window.history.replaceState({}, '', \`\${window.location.pathname}\`); </script> </body> </html>`; } async function startCallbackServer() { return new Promise((resolve) => { const server = http.createServer(); server.listen(0, "127.0.0.1", () => { const address = server.address(); resolve({ server, port: address.port, url: `http://127.0.0.1:${address.port}` }); }); }); } async function waitForCallback(server, expectedState) { return new Promise((resolve, reject) => { const timeout = setTimeout( () => { server.close(); reject(new Error("Authentication timeout after 5 minutes")); }, 5 * 60 * 1e3 ); server.on("request", (req, res) => { const url = new URL(req.url || "", `http://${req.headers.host}`); const code = url.searchParams.get("code"); const state = url.searchParams.get("state"); const error = url.searchParams.get("error"); const errorDescription = url.searchParams.get("error_description"); if (error) { const errorMsg = errorDescription || error; res.writeHead(400, { "Content-Type": "text/html" }); res.end(renderCallbackPage(errorMsg)); clearTimeout(timeout); server.close(); reject(new Error(`OAuth error: ${errorMsg}`)); return; } if (!code || !state) { res.writeHead(400, { "Content-Type": "text/html" }); res.end(renderCallbackPage("Missing code or state parameter")); return; } if (state !== expectedState) { res.writeHead(400, { "Content-Type": "text/html" }); res.end(renderCallbackPage("Invalid state parameter (CSRF protection)")); clearTimeout(timeout); server.close(); reject(new Error("Invalid state parameter")); return; } res.writeHead(200, { "Content-Type": "text/html" }); res.end(renderCallbackPage()); clearTimeout(timeout); server.close(); resolve({ code }); }); }); } // src/config/index.ts var DEFAULT_EVAL_INCLUDE = ["**/*.eval.{ts,js,mts,mjs,cts,cjs}"]; function defineConfig(config) { return config; } function createPartialDefaults() { let token; let url; let orgId; try { const authContext2 = getAuthContext(); if (authContext2) { token = authContext2.token; url = authContext2.url; orgId = authContext2.orgId; } } catch { } token = token || process.env.AXIOM_TOKEN; url = url || process.env.AXIOM_URL; orgId = orgId || process.env.AXIOM_ORG_ID; const edgeUrl = process.env.AXIOM_EDGE_URL; return { eval: { url: url || "https://api.axiom.co", edgeUrl, orgId, token, dataset: process.env.AXIOM_DATASET, flagSchema: void 0, instrumentation: null, include: [...DEFAULT_EVAL_INCLUDE], exclude: [], timeoutMs: 6e4 } }; } function validateConfig(config) { const errors = []; const isDebug = process.env.AXIOM_DEBUG === "true"; if (!isDebug) { if (!config.eval?.token) { errors.push( "eval.token is required (set in axiom.config.ts or AXIOM_TOKEN environment variable)" ); } if (!config.eval?.dataset) { errors.push( "eval.dataset is required (set in axiom.config.ts or AXIOM_DATASET environment variable)" ); } if (!config.eval?.url) { console.log( "eval.url was not specified. Defaulting to `https://api.axiom.co`. Please set it in axiom.config.ts or AXIOM_URL environment variable if you want to use a different endpoint." ); } } const instrumentation = config.eval?.instrumentation; if (instrumentation !== null && instrumentation !== void 0 && typeof instrumentation !== "function") { errors.push( "eval.instrumentation must be a function returning OTEL setup information or null." ); } if (errors.length > 0) { throw new AxiomCLIError(`Invalid Axiom configuration: - ${errors.join("\n - ")}`); } return config; } export { getGlobalConfigPath, loadGlobalConfig, saveGlobalConfig, getActiveProfile, OAuth, fetchOrganizations, verifyToken, startCallbackServer, waitForCallback, getAuthContext, setupGlobalAuth, defineConfig, createPartialDefaults, validateConfig }; //# sourceMappingURL=chunk-3VKWOZAQ.js.map