Something went wrong. Try again.
Open source agent hosting on the AT Protocol
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293// `/admin`: the installation owner's page for the people they invited. Each owner's model budget// (what their agents' calls on the service's model keys may cost, in US dollars), with forms to add// credit, set the budget, or lift it; and a form for a new invite. Plain forms, no script.//// Only the installation owner ever sees it: the server answers everyone else with the not-found page.
import { esc, page, type Page } from './web.ts'
export interface AdminOwner { did: string handle: string createdAt: string budgetUsd?: number spentUsd: number agents: { name: string; handle: string }[]}
export interface AdminView { owners: AdminOwner[] openInvites: number notice?: { text: string; error?: boolean } /** A new invite's sign-up link, shown once, right after it's made. */ invite?: { url: string; expiresAt: string; budgetUsd?: number }}
const CSS = '<style>' + '.owners{width:100%;border-collapse:collapse;margin:1rem 0;font-size:.92rem}' + '.owners th{text-align:left;font-weight:500;font-size:.8rem;color:var(--muted);padding:.4rem .5rem;border-bottom:1px solid var(--line)}' + '.owners td{padding:.6rem .5rem;border-bottom:1px solid var(--line);vertical-align:top}' + '.owners .num{font-variant-numeric:tabular-nums;white-space:nowrap}' + '.owners .out{color:var(--fg);font-weight:500}' + '.owners form{display:flex;gap:.4rem;align-items:center;margin:0 0 .4rem}.owners td.change{min-width:15.5rem}' + '.owners input[type=text]{flex:none;width:5rem;padding:.25rem .65rem}' + 'button.small{padding:.25rem .8rem;font-size:.85rem;white-space:nowrap}' + '.muted{color:var(--muted)}.did{font-size:.75rem;word-break:break-all}' + '.notice{border-left:3px solid var(--green);padding:.25rem .75rem;margin:1rem 0}.notice.error{border-color:#dc2626}' + '.invite label{display:block;margin:.6rem 0 .25rem;font-size:.9rem}.invite input{max-width:12rem}' + '.link{word-break:break-all;user-select:all}' + '@media (max-width:700px){.owners thead{display:none}.owners tr,.owners td{display:block}.owners td{border:0;padding:.25rem 0}.owners tr{border-bottom:1px solid var(--line);padding:.5rem 0}}' + '</style>'
const money = (n: number) => `$${n.toFixed(2)}`const hidden = (name: string, value: string) => `<input type="hidden" name="${esc(name)}" value="${esc(value)}">`
function row(o: AdminOwner): string { const left = o.budgetUsd === undefined ? undefined : o.budgetUsd - o.spentUsd const budget = o.budgetUsd === undefined ? `<span class="muted">unlimited</span>` : `${money(o.budgetUsd)}<br><span class="${left! <= 0 ? 'out' : 'muted'}">${left! <= 0 ? 'used up' : `${money(left!)} left`}</span>` const agents = o.agents.length ? o.agents.map((a) => `@${esc(a.handle)}`).join('<br>') : '<span class="muted">none</span>' const form = (action: string, body: string) => `<form method="post" action="/admin">${hidden('action', action)}${hidden('owner', o.did)}${body}</form>` const forms = form('add', `<input type="text" name="usd" inputmode="decimal" placeholder="5.00" aria-label="Dollars to add for ${esc(o.handle)}" required><button class="small">Add credit</button>`) + form('set', `<input type="text" name="usd" inputmode="decimal" placeholder="${o.budgetUsd ?? '20'}" aria-label="New budget for ${esc(o.handle)}" required><button class="small">Set budget</button>`) + (o.budgetUsd === undefined ? '' : form('unlimited', `<button class="small">Make unlimited</button>`)) return ( `<tr><td>${esc(o.handle)}<br><span class="muted did">${esc(o.did)}</span></td>` + `<td>${agents}</td><td class="num">${money(o.spentUsd)}</td><td class="num">${budget}</td><td class="change">${forms}</td></tr>` )}
export function adminPage(v: AdminView): Page { const notice = v.notice ? `<p class="notice${v.notice.error ? ' error' : ''}" role="${v.notice.error ? 'alert' : 'status'}">${esc(v.notice.text)}</p>` : '' const invite = v.invite ? `<section class="notice" role="status"><p><b>New invite</b> (${v.invite.budgetUsd === undefined ? 'unlimited' : `${money(v.invite.budgetUsd)} budget`}, ` + `expires ${esc(v.invite.expiresAt.slice(0, 10))}). It's shown only now, and works once:</p><p class="link"><code>${esc(v.invite.url)}</code></p></section>` : '' const table = v.owners.length ? `<table class="owners"><thead><tr><th>Owner</th><th>Agents</th><th>Spent</th><th>Budget</th><th>Change</th></tr></thead><tbody>${v.owners.map(row).join('')}</tbody></table>` : `<p class="muted">Nobody has signed up with an invite yet.</p>` return page( 200, 'Admin · Languid', CSS + `<h1>Admin</h1>` + `<p class="muted">The people you invited, and what their agents' model calls may cost on the service's model keys. ` + `Add credit raises a budget by that much; an unlimited owner gets a budget of what they've spent plus it. ` + `Calls on their own ChatGPT connection don't count.</p>` + notice + invite + table + `<h2>Invite someone</h2>` + `<form method="post" action="/admin" class="invite">${hidden('action', 'invite')}` + `<label for="invite-usd">Budget in dollars (empty for unlimited)</label><input id="invite-usd" type="text" name="usd" inputmode="decimal" placeholder="10.00">` + `<label for="invite-days">Link good for (days)</label><input id="invite-days" type="text" name="days" inputmode="numeric" value="7">` + `<p><button>Make invite link</button></p></form>` + `<p class="muted">${v.openInvites} unused invite${v.openInvites === 1 ? '' : 's'} open.</p>`, { path: '/admin', app: true }, )}