Something went wrong. Try again.
the coolest token pot ever
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382/** * Tiny `/api` client. Stays mechanical: all data flow is client-first via * Dexie + this client. Don't add caching here. */
export type APIError = { code: string; message: string;};
export class HTTPError extends Error { status: number; code: string; constructor(status: number, code: string, message: string) { super(message); this.status = status; this.code = code; }}
async function request<T>( method: string, path: string, body?: unknown,): Promise<T> { const res = await fetch(path, { method, headers: body ? { "content-type": "application/json" } : undefined, body: body ? JSON.stringify(body) : undefined, credentials: "include", }); if (!res.ok) { let code = "http_error", message = res.statusText; try { const j = (await res.json()) as { error?: APIError }; if (j.error) { code = j.error.code; message = j.error.message; } } catch { /* ignore */ } throw new HTTPError(res.status, code, message); } // Empty body? Caller should pick a void return. if (res.status === 204) return undefined as T; return (await res.json()) as T;}
export const api = { get: <T>(path: string) => request<T>("GET", path), post: <T>(path: string, body?: unknown) => request<T>("POST", path, body), patch: <T>(path: string, body?: unknown) => request<T>("PATCH", path, body), del: <T>(path: string) => request<T>("DELETE", path),};
// Typed helpers ------------------------------------------------------------
export type User = { id: string; email: string; display_name: string; slack_id: { String: string; Valid: boolean } | null; created_at: number; last_seen_at: number | null; is_admin: number; status: string;};
export type Conversation = { id: string; user_id: string; title: string; created_at: number; updated_at: number; archived_at: number | null;};
export type Message = { id: string; conversation_id: string; client_id: string | null; role: "user" | "assistant" | "system" | "tool"; content: string; model: string | null; created_at: number;};
export const me = () => api.get<User>("/api/me");export const updateMe = (display_name: string) => api.patch<User>("/api/me", { display_name });export const balance = () => api.get<{ balance_micros: number; balance_usd: string }>("/api/balance");
export type MemoryRow = { key: string; value: string };export const getMemory = () => api.get<{ rows: MemoryRow[] }>("/api/memory");export const updateMemory = (rows: MemoryRow[]) => api.patch<{ ok: boolean }>("/api/memory", { rows });export const listConversations = () => api.get<Conversation[]>("/api/conversations");export const createConversation = (title: string) => api.post<Conversation>("/api/conversations", { title });export const listMessages = (id: string) => api.get<Message[]>(`/api/conversations/${id}/messages`);export const deleteConversation = (id: string) => api.del<void>(`/api/conversations/${id}`);
export type PoolStats = { balance_micros: number; balance_usd: string; spent_today_micros: number; spent_today_usd: string; contributors: number; users: number; active_keys: number; input_tokens: number; output_tokens: number; total_tokens: number; as_of: number;};
export const poolStats = () => api.get<PoolStats>("/api/stats");
export type AllocationUser = { user_id: string; display_name: string; email: string; key_count: number; shared_contribution_micros: number; private_reservation_micros: number; shared_allowance_today_micros: number; // smart-alloc breakdown: floor (guaranteed) + bonus (historical redistribution) shared_allowance_floor_micros: number; shared_allowance_bonus_micros: number; predicted_total_today_micros: number; history_days_used: number; is_donating: boolean; shared_spent_today_micros: number; private_spent_today_micros: number; shared_remaining_today_micros: number; share_fraction: number;};
export type Allocations = { pool: { total_shared_micros: number; spent_today_shared_micros: number; remaining_pool_today_micros: number; active_key_count: number; active_team_count: number; redistribution_surplus_micros: number; }; users: AllocationUser[]; last_recompute: { at: number; by_user_id: string; by_display_name: string; } | null;};
export const getAllocations = () => api.get<Allocations>("/api/allocations");
export type APIKey = { id: string; name: string; word: string; last4: string; masked: string; spent_micros: number; created_at: number; last_used_at: number; revoked_at: number; revoked: boolean;};
export const listKeys = () => api.get<APIKey[]>("/api/keys");export const createKey = (name: string) => api.post<APIKey & { plaintext: string }>("/api/keys", { name });export const revokeKey = (id: string) => api.del<void>(`/api/keys/${id}`);
export type Session = { id: string; created_at: number; last_used_at: number; expires_at: number; current: boolean; ip: string; user_agent: string; location: string;};
export type Model = { id: string; label: string; description: string; context_window: number; max_output_tokens: number; input_per_mil: number; output_per_mil: number | null; license: string; tier: string; thinking: boolean; image_input: boolean; structured_outputs: boolean; stats: { request_count: number; total_input_tokens: number; total_output_tokens: number; avg_tps: number | null; } | null;};
export type UsageDay = { day: number; amount_micros: number; input_tokens: number; output_tokens: number;};
export type UsageByModel = { day: number; model: string; amount_micros: number; input_tokens: number; output_tokens: number;};
export type UsageData = { daily: UsageDay[]; by_model: UsageByModel[];};
export const getUsage = () => api.get<UsageData>("/api/usage");
export const listModels = () => api.get<{ models: Model[]; refreshed_at: number }>("/api/models");
export const listSessions = () => api.get<Session[]>("/api/sessions");export const revokeSession = (id: string) => api.del<void>(`/api/sessions/${id}`);
export type PoolKey = { id: string; user_id: string; label: string; active: boolean; max_micros: number; shared_micros: number; private_micros: number; today_micros: number; total_micros: number; request_count: number; pioneer_health: number; // 0=unknown 1=healthy 2=unauthorized pioneer_team_id: string; pioneer_payment_plan: string; pioneer_credit_limit_micros: number; pioneer_remaining_micros: number; pending_validation: boolean; provider_id: string; revoked: boolean; created_at: number; last_used_at: number; last_billing_sync_at: number; owner_name: string; owner_email: string; mine: boolean;};
export const listPoolKeys = () => api.get<PoolKey[]>("/api/pool-keys");
export interface Provider { id: string; type: string; name: string; base_url: string; active: boolean; is_free: boolean;}export const listProviders = () => api.get<Provider[]>("/api/providers");export const createProvider = ( id: string, type: string, name: string, baseUrl: string, isFree: boolean,) => api.post<Provider>("/api/providers", { id, type, name, base_url: baseUrl, is_free: isFree, });export const updateProvider = ( id: string, updates: { type?: string; name?: string; base_url?: string; active?: boolean; is_free?: boolean; },) => api.patch<void>(`/api/providers/${id}`, updates);export const deleteProvider = (id: string) => api.del<void>(`/api/providers/${id}`);
export interface PoolKeyProbe { provider_id: string; payment_plan: string; credit_limit_micros: number; remaining_micros: number; today_micros: number;}export const probePoolKey = (apiKey: string, providerId?: string) => api.post<PoolKeyProbe>("/api/pool-keys/probe", { api_key: apiKey, ...(providerId ? { provider_id: providerId } : {}), });
export const addPoolKey = ( label: string, apiKey: string, sharedMicros?: number, providerId?: string,) => api.post<PoolKey & { pending_validation?: boolean; pending_reason?: string }>( "/api/pool-keys", { label, api_key: apiKey, ...(sharedMicros != null ? { shared_micros: sharedMicros } : {}), ...(providerId ? { provider_id: providerId } : {}), }, );export const setPoolKeyActive = (id: string, active: boolean) => api.patch<void>(`/api/pool-keys/${id}/active`, { active });export const updatePoolKeyLabel = (id: string, label: string) => api.patch<void>(`/api/pool-keys/${id}/label`, { label });export const updatePoolKeyLimits = ( id: string, maxMicros: number, sharedMicros: number,) => api.patch<void>(`/api/pool-keys/${id}/limits`, { max_micros: maxMicros, shared_micros: sharedMicros, });export const syncPoolKey = (id: string) => api.post<{ today_micros: number }>(`/api/pool-keys/${id}/sync`);export const deletePoolKey = (id: string) => api.del<void>(`/api/pool-keys/${id}`);export const recomputeAllocations = () => api.post<Allocations>("/api/allocations/recompute");
export async function logout(): Promise<void> { await fetch("/auth/logout", { method: "POST", credentials: "include" });}
// Admin helpers ------------------------------------------------------------
export type AdminUser = { id: string; email: string; display_name: string; slack_id: { String: string; Valid: boolean } | null; status: "active" | "waitlisted" | "banned"; is_admin: boolean; created_at: number; last_seen_at: number;};
export const adminListUsers = () => api.get<AdminUser[]>("/api/admin/users");export const adminSetUserStatus = ( id: string, status: "active" | "waitlisted" | "banned",) => api.patch<void>(`/api/admin/users/${id}/status`, { status });export const adminSetUserAdmin = (id: string, is_admin: boolean) => api.patch<void>(`/api/admin/users/${id}/admin`, { is_admin });export const adminDeleteUser = (id: string) => api.del<void>(`/api/admin/users/${id}`);