Files
db284f5f96 refactor(ai): one platform-LLM path, one eval auth helper (#676)
Two duplicated blocks, both now living once:

- intent-router and offer-engine each carried their own copy of the
  platform provider call (Groq-first, OpenRouter fallback, json_object,
  failure logging). Both now go through callPlatformJson in
  platform-llm.ts — the SSOT the other four structured Cat features
  already use — so provider choice and the json-mode 400-retry apply
  everywhere, once. form-prefill-service stays separate on purpose: it
  accepts a user-supplied API key/model, so it is not a pure platform
  call.

- signIn/buildAuthCookie (GoTrue REST + forged @supabase/ssr cookie) was
  copied across eval-cat, eval-voice-routing and eval-wallet-routing.
  Extracted to scripts/eval-auth.mjs; deploy-selfhost.sh ships it next
  to eval-cat.mjs so the nightly timer on the box keeps resolving the
  import.

Verified: tsc clean, lint clean, 2153 unit tests green, and
eval:voice 13/13 against production through the new auth path.

Co-authored-by: Georgy Butaev <41178744+g-but@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-11 16:28:38 +00:00

43 lines
1.7 KiB
JavaScript

/**
* Shared auth for eval scripts that probe the deployed app as a signed-in user
* (eval-cat, eval-voice-routing, eval-wallet-routing). Extracted once three
* copies existed.
*
* The trick both helpers encode: the app authenticates via the @supabase/ssr
* cookie, so a plain fetch can act as a user by signing in against GoTrue REST
* and forging that cookie — name `sb-<host-label>-auth-token`, value
* `base64-<base64url(session JSON)>`, chunked into `.0`,`.1`,… pieces at 3180
* chars (MAX_CHUNK_SIZE in @supabase/ssr).
*
* Plain params, no env reads: each eval script owns its own env conventions
* (CAT_EVAL_*, VOICE_EVAL_*, …) and passes the resolved values in.
*/
/** Sign in via GoTrue REST. Returns the session JSON ({ access_token, user, … }). */
export async function signInWithPassword({ supabaseUrl, anonKey, email, password }) {
const res = await fetch(`${supabaseUrl}/auth/v1/token?grant_type=password`, {
method: 'POST',
headers: { apikey: anonKey, 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password }),
});
if (!res.ok) {
throw new Error(`GoTrue sign-in failed: ${res.status} ${await res.text()}`);
}
return res.json();
}
/** Forge the @supabase/ssr auth cookie for a GoTrue session. */
export function buildAuthCookie(supabaseUrl, session) {
const name = `sb-${new URL(supabaseUrl).hostname.split('.')[0]}-auth-token`;
const value = `base64-${Buffer.from(JSON.stringify(session)).toString('base64url')}`;
const MAX = 3180;
if (value.length <= MAX) {
return `${name}=${value}`;
}
const parts = [];
for (let i = 0; i * MAX < value.length; i++) {
parts.push(`${name}.${i}=${value.slice(i * MAX, (i + 1) * MAX)}`);
}
return parts.join('; ');
}