Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257<script lang="ts"> import { cancelLogin, mcpServers, readLogin, signInToMcp, signOutOfMcp, type McpAuth, type McpServer, } from './api'; import { safeHref } from './markdown';
// The HTTP MCP servers that sign in with OAuth, with where each sign-in // stands and the action it needs. A sign-in opens the authorization page // in a new tab; the daemon's loopback callback finishes it, and this // section polls until it has. let servers = $state<McpServer[] | null>(null); let failure = $state(''); let signing = $state<{ server: string; id: string; url: string | null; progress: string; } | null>(null);
const shown = $derived( (servers ?? []).filter( (server) => server.transport === 'http' && server.auth.state !== 'none', ), );
async function load() { try { servers = await mcpServers(); } catch (error) { failure = `could not read MCP servers: ${(error as Error).message}`; servers = []; } } $effect(() => { load(); });
function describe(auth: McpAuth): string { switch (auth.state) { case 'signed_in': { const who = auth.account ? `signed in as ${auth.account}` : 'signed in'; const until = auth.expires_at ? ` · token renews ${new Date(auth.expires_at).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}` : ''; return who + until; } case 'signed_out': return 'sign in to use its tools again'; default: return auth.reason ? `needs sign-in · ${auth.reason}` : 'needs sign-in'; } }
async function signIn(server: string) { failure = ''; signing = { server, id: '', url: null, progress: 'starting the sign-in' }; try { const login = await signInToMcp(server); // The authorization page comes from the server's own metadata: only a web address opens. const url = login.url ? safeHref(login.url) : null; signing = { server, id: login.id, url, progress: 'waiting for the browser', }; if (url) window.open(url, '_blank', 'noopener'); await follow(login.id); } catch (error) { failure = (error as Error).message; signing = null; } }
async function follow(id: string) { while (signing?.id === id) { await new Promise((done) => setTimeout(done, 1000)); const login = await readLogin(id); if (signing?.id !== id) return; if (login.state === 'complete') { signing = null; await load(); return; } if (login.state !== 'waiting' && login.state !== 'exchanging') { failure = `sign-in ${login.state}${login.failure ? `: ${login.failure.detail}` : ''}`; signing = null; return; } signing = { ...signing, progress: login.progress || signing.progress }; } }
async function cancel() { const id = signing?.id; signing = null; if (id) await cancelLogin(id).catch(() => {}); }
async function signOut(server: string) { failure = ''; try { await signOutOfMcp(server); await load(); } catch (error) { failure = (error as Error).message; } }</script>
{#if servers === null} <p class="note">reading MCP servers…</p>{:else if shown.length === 0 && !failure} <p class="note"> no MCP server asks for a sign-in. one that answers with a sign-in request shows here. </p>{:else} <ul class="servers"> {#each shown as server (server.name)} {@const active = signing?.server === server.name} <li> <div class="row"> <span class="name">{server.name}</span> <span class="state" data-state={active ? 'signing' : server.auth.state} >{active ? 'signing in' : server.auth.state.replaceAll('_', ' ')}</span > </div> <p class="note url">{server.url}</p> {#if active && signing} <p class="note" role="status">{signing.progress}</p> <div class="actions"> {#if signing.url}<a class="button" href={signing.url} target="_blank" rel="noopener noreferrer">open sign-in page</a >{/if} <button onclick={cancel}>cancel</button> </div> {:else} <p class="note">{describe(server.auth)}</p> {#if server.auth.step_up}<p class="note"> needs wider access: {server.auth.step_up} </p>{/if} <div class="actions"> <button onclick={() => signIn(server.name)} disabled={signing !== null} > {server.auth.state === 'signed_in' ? server.auth.step_up ? 'grant access' : 'sign in again' : 'sign in'} </button> {#if server.auth.state === 'signed_in'}<button onclick={() => signOut(server.name)} disabled={signing !== null}>sign out</button >{/if} </div> {/if} </li> {/each} </ul>{/if}{#if failure}<p class="note failure" role="alert">{failure}</p>{/if}
<style> /* Settings' own note, button and action styles, which do not reach a child component. */ .note { display: block; margin: 6px 0 0; color: var(--muted); font-size: calc(12px * var(--ui-scale)); line-height: 1.4; } .actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; } button, .button { display: inline-flex; align-items: center; height: 28px; padding: 0 10px; color: var(--text); font: inherit; font-size: calc(12.5px * var(--ui-scale)); text-decoration: none; background: var(--field); border: 0; border-radius: 10px; cursor: pointer; transition: background-color 120ms ease, transform 100ms ease-out; } button:hover:not(:disabled), .button:hover { background: var(--selected); } button:active:not(:disabled) { transform: scale(0.97); } button:disabled { color: var(--faint); cursor: default; } .servers { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; } .row { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; } .name { font-size: calc(13px * var(--ui-scale)); font-weight: 560; } .state { color: var(--muted); font-size: calc(12px * var(--ui-scale)); } .state[data-state='signed_in'] { color: var(--added); } .state[data-state='needs_sign_in'] { color: var(--danger); } .url { overflow-wrap: anywhere; } .failure { color: var(--danger); }</style>