// languid service: the one place that reads and writes agent records. // // Clients (pi-languid, future UIs, the hosted harness) never touch the PDS or // hold agent credentials. They call these XRPC methods with a bearer token // that identifies an owner (a session from `languid login`, which signs in // with the owner's atproto account, or the installation's own token), and see // only that owner's agents, computers and runs. Browsers get a landing page at // `/` and can sign in there too (src/web.ts); a browser's session cookie opens // the service's pages and the chat at /agents/, never its XRPC methods. // The service: // - compiles each request's context from the agent's records, // - records turns, assigning computer tool calls to exactly one computer, // - executes tools (block edits here, computer tools on runners, which // long-poll this service for jobs: see src/hub.ts and src/runner.ts), // - serializes every write per agent. The Spaces alpha has no // compare-and-swap, so a single writer is what keeps block edits from // overwriting each other. import crypto from 'node:crypto' import http from 'node:http' import { getSupportedThinkingLevels, type AssistantMessage, type Model, type Models, type ToolResultMessage, type UserMessage } from '@earendil-works/pi-ai' import { DEFAULT_REASONING, Harness } from './harness.ts' import { IDLE_LIMITS, idleCompactionDue, idleSettings } from './compaction.ts' import { agentSession, checkHandle, createAccount, createAgent, DELETE_GRACE_DAYS, deactivateAgent, handleDomain, installationAuthority, knownAccounts, moveHandle, opsAuthority, purgeDeletedAgents, resolveHandle } from './manager.ts' import { casePage, evalsPage, type EvalRunRow } from './eval-page.ts' import type { Actor } from './atproto/space.ts' import { XrpcError, type PdsCall } from './atproto/xrpc.ts' import { createModels } from './models.ts' import { suggestTitle } from './auto-title.ts' import { ClaudeConnections } from './anthropic.ts' import { KeyError, OpenRouterConnections } from './model-keys.ts' import { ChatGPTConnections, CODEX, withOwnerChatGPT, XAI } from './chatgpt.ts' import { type Connection, pushRecent } from './model-groups.ts' import { isVia, now, NSID, onboardingMemory, ownerProfileMemory, parseOwnerProfile, OWNER_WORK, type OwnerProfile, type OwnerWork, type ApprovalRequest, type ComputerNode, type Message, type MessagePart, type Schedule, type Wakeup, type Machine } from './records.ts' import { DEFAULT_SERVICE, displayNameOf, getAgent, home, loadState, saveState, type State } from './state.ts' import { moveRules } from './approvals.ts' import { bashCommand, parseCommands, RULE_TOOLS, suggestCommand, type ComputerRules } from './permissions.ts' import { hashToken, RunnerHub } from './hub.ts' import { ACCOUNTS_COOKIE, atprotoProtocol, cookie, LoginError, Logins, oauthOrigin, SESSION_COOKIE, type OAuthProtocol, type Page } from './oauth.ts' import { computersPage, type ComputerRow } from './computers.ts' import { agentSettingsPage, SETTINGS_LIMITS, tabOf } from './agent-settings.ts' import { correctionPrompt, MEMORY_JS, memoryPage } from './memory-page.ts' import { UNITS, validZone, zonedToIso } from './schedules-page.ts' import { machinePage, machinesPage } from './machines-page.ts' import { backlinksOf, collectionPage, navHref, recordPage, recordsIndexPage, refTarget, repoOf, repoPage, uriHref, type Listing, type Members, type RecordRow, type RepoRef, type Where } from './records-page.ts' import { SpaceRepo, type SpaceRecord } from './atproto/space.ts' import { DELETE_PUBLIC_PROFILES, deletePublicProfile, moveOwnerProfile, readOwnerProfile, writeOwnerProfile } from './owner-space.ts' import { atprotoSigningKey, guardedFetch, resolveRepo, xrpcUrl } from './atproto/identity.ts' import { ServiceAuth, ServiceAuthError, didDocument as serviceDidDocument, looksLikeJwt, serviceDid } from './service-auth.ts' import { DEFAULT_POLICY, isWriteRule, parsePolicy, writeRule } from './policy.ts' import { SITE_HEADERS, SiteHost, sitesHostname } from './sites.ts' import { DiscordBots, UserError } from './discord/bots.ts' import { discordBotPage, discordPage } from './discord/page.ts' import { adminFeedbackPage, adminLogsPage, adminMessagesPage, adminPage, inviteQr, type AdminView } from './admin.ts' import { OpsMessages } from './messages.ts' import { usagePage, USAGE_RANGES } from './usage-page.ts' import { inboxPage, type PendingApproval } from './inbox-page.ts' import { LATEST_NEWS, NEWS_COOKIE, NEWS_PATH, newsPage, newsUnread } from './news-page.ts' import { summarizeUsage } from './usage.ts' import { applyStripeEvent, COMP, CREDIT_MAX, CREDIT_MIN, createCreditCheckout, creditAmount, createPortalSession, createSubscriptionCheckout, customerOf, isComp, grantCredit, grantsOf, isActive, isPlan, PLANS, setCancelAtPeriodEnd, stripeConfig, subscriptionEventOwner, verifyStripeSignature, type BillingEntry } from './billing.ts' import { billingPage } from './billing-page.ts' import { INCLUDED_HOURS, ModalSandboxes, modalConfigured, SANDBOX_COMPUTER, type SandboxProvider } from './sandboxes.ts' import { IMAGE_TYPES, MAX_IMAGE_BYTES, MAX_IMAGES } from './images.ts' import { autoRefusal, chargeCheck, opsMessage as autoOpsMessage, review as autoApprove, type AutoVerdict } from './auto-approve.ts' import { audioType, canTranscribe, chargeTranscription, MAX_AUDIO_BYTES, MAX_SECONDS, transcribe, transcribeRefusal } from './transcribe.ts' import { aboutYouCard, aboutYouPage, accountPage, APP_NAV, NAV_NEST, appNav, page, callChange, CHAT_JS, chatPage, DOCS_JS, esc, NAV_JS, FAVICON, FONT_FILES, installCommand, installScript, type AgentState, landingPage, notFoundPage, signupDonePage, signupGonePage, sentence, signupPage, simplePage, type ChatItem } from './web.ts' import { budgetRefusal, BudgetError, onLanguidKeys, MANAGED_MARKUP, MAX_INVITE_USES, openInviteUses, returnOwnerInvite, takeOwnerInvite, guestDefaultModel, houseModels, findOwnerDid, findOwnerInvite, parseDollars, HANDLE_LABEL, meteredModels, MIN_PASSWORD, newOwnerInvite, ownerSummary } from './owners.ts' import { hostOrTailnet, RateLimiter, type LimitOptions } from './limits.ts' import { AgentStore, needsApproval, transcriptKey, type Sender, type SessionOptions } from './store.ts' import { ACCESS_TTL_MS, answerRpc, AuthCodes, authorizationServerMetadata, authRedirect, int, MAX_MCP_CLIENTS, MCP_SCOPE, mcpConsentPage, mcpErrorPage, mcpSignInPage, MCP_JS, MCP_JS_PATH, McpToolError, OAuthError, parseAuthRequest, protectedResourceMetadata, REFRESH_TTL_MS, registerClient, str, type McpTool, } from './mcp.ts' import { specByName, takesLock } from './tools.ts' import { pendingWakeups, performWakeup, reconcileWakeups, ref } from './wakeups.ts' import { DEFAULT_DAILY_RUNS, JOB_REVIEW_GOAL, migrateLegacy, parked, performMachine, permissionsFrom, runsToday, validateMachine } from './machines.ts' import { machineUsage, usageTag } from './usage.ts' import { tid } from './atproto/tid.ts' import { Funnel, funnelRows, type FunnelStep } from './funnel.ts' import { computerEvent, errorEvent, KINDS, LEVELS, OpsLog, OpsSpace } from './ops.ts' import { AgentTelemetry, clientReport, errorReport, feedbackRecord, setTelemetryRecipient, TELEMETRY_RECIPIENT, TelemetryError, type ErrorReport, type Feedback, type Source } from './telemetry.ts' /** pi's thinking levels, weakest first: what a reasoning level may be. */ const REASONING_LEVELS = ['off', 'minimal', 'low', 'medium', 'high', 'xhigh', 'max'] /** Where OpenRouter's sign-in sends the owner back, with a code. */ const OPENROUTER_CALLBACK = '/settings/models/openrouter' const OPENROUTER_STATE_COOKIE = 'languid_openrouter_state' export class XrpcHttpError extends Error { status: number error: string constructor(status: number, error: string, message: string) { super(message) this.status = status this.error = error } } const invalid = (message: string) => new XrpcHttpError(400, 'InvalidRequest', message) const notWaiting = (why: string) => new XrpcHttpError(409, 'NotWaiting', `that call is not waiting for an answer: ${why}`) /** A thread title as it is stored: one line, trimmed, at most 200 characters. */ function threadTitle(title: unknown): string { const clean = typeof title === 'string' ? title.replace(/\s+/g, ' ').trim() : '' if (!clean) throw invalid('a title is required') if (clean.length > 200) throw invalid('at most 200 characters') return clean } /** Body size limits: unauthenticated registration, runners and owner calls (both may carry images: pi's read attaches them). */ const MAX_BODY = { register: 16 * 1024, runner: 16 * 1024 * 1024, owner: 16 * 1024 * 1024, chat: Math.ceil((MAX_IMAGES * MAX_IMAGE_BYTES * 4) / 3) + 64 * 1024 } /** Read a request body, refusing anything over `max` bytes before buffering it. */ async function readText(req: http.IncomingMessage, max: number): Promise { const tooLarge = () => new XrpcHttpError(413, 'PayloadTooLarge', `request body is over ${max} bytes`) if (Number(req.headers['content-length'] ?? 0) > max) throw tooLarge() const chunks: Buffer[] = [] let size = 0 for await (const chunk of req) { size += chunk.length if (size > max) throw tooLarge() chunks.push(chunk) } return Buffer.concat(chunks).toString('utf8') } /** A 429 with Retry-After, as XRPC errors are shaped. */ function tooMany(res: http.ServerResponse, retry: number, message = `too many requests; try again in ${retry}s`) { if (res.headersSent || res.destroyed) return res.writeHead(429, { 'content-type': 'application/json', 'x-content-type-options': 'nosniff', 'retry-after': String(retry) }) res.end(JSON.stringify({ error: 'RateLimitExceeded', message })) } const withRetry = (p: Page, retry: number): Page => ({ ...p, headers: { ...p.headers, 'retry-after': String(retry) } }) async function readJson(req: http.IncomingMessage, max: number): Promise { const body = await readText(req, max) try { return body ? JSON.parse(body) : {} } catch { throw invalid('body must be JSON') } } /** Who is calling: an owner DID, from a login session or the installation's token. */ interface Caller { did: string /** app: an app calling through the owner's PDS with a service token (service-auth.ts); it can call only APP_METHODS. */ via: 'session' | 'token' | 'app' handle?: string /** For sessions: the state key, so the session can be ended. */ session?: string } type Handler = (input: any, caller: Caller) => Promise const SESSION_TTL_MS = 30 * 24 * 60 * 60_000 /** How many other accounts a browser keeps signed in beside the one in use. */ const MAX_OTHER_ACCOUNTS = 5 /** Agents a signed-in owner may have. Each one is an account on the PDS and a DID on the PLC directory, which is permanent. */ const MAX_AGENTS_PER_OWNER = Number(process.env.LANGUID_MAX_AGENTS_PER_OWNER ?? 5) export interface ServeOptions { host: string port: number /** The installation's own token: it acts as the installation owner (state.owner). */ token: string /** The address clients use. Sign-in needs it to be HTTPS on port 443, or loopback. */ url?: string /** The sign-in protocol; defaults to atproto OAuth when `url` allows it. Tests pass a fake. */ oauth?: OAuthProtocol log?: (s: string) => void /** State source; reloaded on each store load so new agents/runners are picked up. */ state?: () => State /** Models for service-side runs. Defaults to pi's ModelRuntime (auth.json + env keys). */ models?: () => Promise /** Owner-scoped subscription storage/login; injectable for offline tests. */ chatgpt?: ChatGPTConnections claude?: ClaudeConnections openrouter?: OpenRouterConnections /** * Rate limits (src/limits.ts); false turns them off. The client-address header defaults to * LANGUID_CLIENT_IP_HEADER, and LANGUID_RATE_LIMITS=off turns them off. */ limits?: LimitOptions | false /** Wakeup polling interval; false disables the automatic worker (manual tick still works). */ wakeups?: { intervalMs?: number } | false /** false: record no error reports (src/telemetry.ts). */ telemetry?: false /** Write the service's ops events to the ops space (src/ops.ts). `languid serve` turns it on; tests leave it off. */ ops?: boolean /** Connect the Discord bots owners added on /discord (on by default; tests turn it off). */ discord?: boolean /** Cloud sandboxes (sandboxes.ts). Defaults to Modal when MODAL_TOKEN_ID and MODAL_TOKEN_SECRET are set; tests pass a fake. */ sandboxes?: SandboxProvider /** How long a run waits for the owner's answer (default APPROVAL_TIMEOUT_MS; tests shorten it). */ approvalTimeoutMs?: number } export type RunEvent = | { seq: number; type: 'text'; text: string } /** The reply just shown is a record now, so the page can offer 👍/👎 on it. */ | { seq: number; type: 'saved'; message: string } // `machine`: the same as `computer`, for clients from before the rename (see LEGACY_METHODS). | { seq: number; type: 'tool_call'; id: string; tool: string; arguments: string; computer?: string; machine?: string } | { seq: number; type: 'tool_result'; id: string; tool: string; output: string; isError: boolean } /** * A run that asks (web chat, Discord) is waiting for the owner to approve or deny this call. * `always` is the rule an "Always allow" would save, when one fits: a bash command by its leading * words, or (without `command`) the whole tool on that computer. */ /** `request`: the ai.languid.approval.request record behind it, to answer with ai.languid.approval.answer. */ | { seq: number; type: 'approval'; id: string; tool: string; arguments: string; computer?: string; machine?: string; always?: AlwaysRule; request?: string } | { seq: number; type: 'decision'; id: string; approved: boolean; reason?: string } interface Run { id: string agent: string owner: string thread: string status: 'running' | 'done' | 'error' error?: string events: RunEvent[] /** The assistant text streamed so far in the current turn, until the turn ends and it becomes a text event. */ partial?: string /** The owner stopped it. */ stopped?: boolean startedAt: string finishedAt?: string } /** * What a run does with computer tool calls that change things (bash, edit, write): deny them, allow * them, ask the owner and wait (the web chat, where someone is watching), or let a classifier model * approve the reversible ones the owner asked for and ask about the rest (src/auto-approve.ts). */ export type ApprovalMode = 'deny' | 'allow' | 'ask' | 'auto' const APPROVAL_MODES: readonly string[] = ['deny', 'allow', 'ask', 'auto'] /** A rule "Always allow" saves: a bash command by its leading words, or (no command) the tool. */ export interface AlwaysRule { command?: string } /** The rule an "Always allow" on this call would save, if any: bash gets a narrow command rule or none. */ export function alwaysRuleFor(tool: string, argumentsJson: string, computer?: string): AlwaysRule | undefined { if (!computer) return undefined if (tool !== 'bash') return {} const command = suggestCommand(bashCommand(argumentsJson) ?? '') return command ? { command } : undefined } /** How long a run waits for the owner to answer an approval before denying the call. */ const APPROVAL_TIMEOUT_MS = 5 * 60_000 /** Why a run that denies changes refused one: no one was asked. */ const NO_ONE_ASKED = 'no one was asked: this run was started with changes denied (approval "deny"); start it with approval "ask" or "allow"' /** What a call the owner never answered gets as its result. */ const NO_ANSWER = 'the owner was asked, and there was no answer in time' type NewRunEvent = RunEvent extends infer E ? (E extends RunEvent ? Omit : never) : never const MAX_RUNS_KEPT = 200 /** How long a run that ends before its new thread's name is written waits for it (src/auto-title.ts). */ const TITLE_WAIT_MS = 5_000 /** ai.languid.computer.* methods by their names before "machine" became "computer" (Nov 15). */ export const LEGACY_METHODS = { list: 'ai.languid.machine.list', invite: 'ai.languid.machine.invite', attach: 'ai.languid.machine.attach', remove: 'ai.languid.machine.remove', register: 'ai.languid.machine.register', } as const /** * The address clients on the service's own computer should use. A public URL goes through a proxy, * which the installation token isn't accepted from, so they get a direct one instead: loopback when * the service listens on every interface, else the address it listens on. */ export function localClientUrl(url: string, host: string, port: number, tailnetName?: string) { const o = oauthOrigin(url) if (!('origin' in o) || o.loopback) return url if (host === '0.0.0.0' || host === '::') return `http://127.0.0.1:${port}` if (tailnetName) return `http://${tailnetName}:${port}` return `http://${host.includes(':') ? `[${host}]` : host}:${port}` } export function createService(opts: ServeOptions) { const readState = opts.state ?? loadState const log = opts.log ?? (() => {}) /** The ops space (src/ops.ts): the service's own log and the sign-up funnel, on the ops account. */ const opsSpace = new OpsSpace({ account: () => opsAuthority(readState), readers: () => { const owner = readState().owner?.did return owner ? [owner] : [] }, legacy: async () => (readState().owner ? installationAuthority(readState()) : undefined), log, }) const ops = new OpsLog(() => opsSpace.open(), log) /** Messages from Languid in owners' Inboxes (src/messages.ts), kept in the ops space. */ const opsMessages = new OpsMessages(() => opsSpace.open(), log) const approvalTimeout = opts.approvalTimeoutMs ?? APPROVAL_TIMEOUT_MS const getModels = opts.models ?? createModels const chatgpt = opts.chatgpt ?? new ChatGPTConnections() const claude = opts.claude ?? new ClaudeConnections() const openrouter = opts.openrouter ?? new OpenRouterConnections() /** Owners' own API keys, by provider (model-keys.ts), and their SuperGrok / X plan once signed in. */ const keyConnections = [claude, openrouter] const ownKeys = (did: string) => [...keyConnections.filter((k) => k.has(did)).map((k) => k.provider), ...(chatgpt.has(did, XAI) ? [XAI] : [])] /** Calls the owner pays for themselves (their ChatGPT plan, their own API keys): no Languid budget. */ const ownerFunded = (did: string, model: { provider: string }) => model.provider === CODEX || ownKeys(did).includes(model.provider) /** Which of the owner's connections pays for a call to this model, for the pickers. */ const connectionOf = (did: string, model: { provider: string }): Connection => model.provider === CODEX ? 'chatgpt' : model.provider === XAI && chatgpt.has(did, XAI) ? 'grok' : ownKeys(did).includes(model.provider) ? (`${model.provider}-key` as Connection) : 'languid' /** Remembers a model the owner picked, newest first, for the pickers' Recent list. */ const noteRecentModel = (did: string, value: string) => { const state = readState() const prefs = ((state.preferences ??= {})[did] ??= {}) prefs.recentModels = pushRecent(prefs.recentModels, value) saveState(state) } const ownerModels = async (did: string): Promise => { const host = await getModels() const owner = await chatgpt.runtime(did) for (const k of keyConnections) await k.apply(did, owner) // Preserve pi credentials for the installation owner, never for invited owners. const install = () => did === readState().owner?.did const models = withOwnerChatGPT(host, owner, () => install() && !owner.hasConfiguredAuth(CODEX), () => ownKeys(did)) // Everyone else is offered only the models they may run (LANGUID_GUEST_MODELS, or their own connections). return new Proxy(models, { get(target, prop) { const v = Reflect.get(target, prop, target) if (prop !== 'getAvailable' || typeof v !== 'function') return v return async (...args: any[]) => { const list = await v.apply(target, args) // Languid's keys serve only Languid's picks, for everyone; the rest need the owner's own connection. return list.filter((m: any) => ownerFunded(did, m) || onLanguidKeys(m)) } }, }) } const stores = new Map>() /** The stores that have loaded, for what must be read without waiting (the sidebar's Inbox count). */ const loadedStores = new Map() /** Each agent's error reports (src/telemetry.ts), with its own login, apart from its store. */ const telemetries = new Map>() /** * Where feedback and error reports go: the ops account (ops., src/ops.ts), made * if it isn't there yet. Sharing a telemetry space adds it as a reader. Undefined when * LANGUID_TELEMETRY_RECIPIENT names someone else. */ async function feedbackDestination(): Promise { if (process.env.LANGUID_TELEMETRY_RECIPIENT) return undefined if (!readState().ops) await opsAuthority(readState) const account = readState().ops if (account) setTelemetryRecipient(account.handle) return account?.did } if (readState().ops) setTelemetryRecipient(readState().ops!.handle) /** Spaces owners shared before the ops account was the destination now share with it (AgentTelemetry.follow). */ async function followFeedbackDestination() { if (!(await feedbackDestination())) return for (const name of Object.keys(readState().agents)) { try { if (await (await telemetryOf(name)).follow(readState().pds)) log(`${name}'s shared error reports and feedback now go to ${TELEMETRY_RECIPIENT()}`) } catch (e: any) { log(`${name}'s error reports couldn't move to ${TELEMETRY_RECIPIENT()}: ${e?.message ?? e}`) } } } /** Who the installation owner reads shared feedback as: themselves (old shares) and the ops account. */ const maintainerDids = () => [installOwner(readState()), readState().ops?.did].filter((d): d is string => !!d) /** The feedback inbox: every shared report or feedback, newest first, as text for an agent. */ async function feedbackInboxText({ kind, limit }: { kind?: string; limit?: number }): Promise { const n = Math.min(200, Math.max(1, limit || 30)) const agents = await sharedWith(maintainerDids(), n) const when = (s: string) => `${s.replace('T', ' ').slice(0, 16)} UTC` if (kind === 'errors') { const rows = agents.flatMap((a) => a.reports.map((r) => ({ ...r, handle: a.handle }))).sort((a, b) => (a.createdAt < b.createdAt ? 1 : -1)).slice(0, n) if (!rows.length) return `No error reports shared (${agents.length} agent(s) share).` return rows.map((r) => `- ${when(r.createdAt)} ${r.handle} [${r.source}] ${r.name}: ${r.message}${r.method ? ` (${r.method})` : ''}${r.version ? ` @${r.version.slice(0, 7)}` : ''}`).join('\n') } const rows = agents .flatMap((a) => a.feedback.map((f) => ({ ...f, handle: a.handle }))) .filter((f) => !kind || f.kind === kind) .sort((a, b) => (a.createdAt < b.createdAt ? 1 : -1)) .slice(0, n) if (!rows.length) return `No feedback${kind ? ` of kind ${kind}` : ''} yet, from ${agents.length} agent(s) that share.` return rows.map((f) => `## ${f.kind} from ${f.handle}, ${when(f.createdAt)}${f.model ? ` (${f.provider}/${f.model})` : ''}${f.version ? ` @${f.version.slice(0, 7)}` : ''}\n${f.text}`).join('\n\n') } function telemetryOf(agent: string): Promise { let t = telemetries.get(agent) if (!t) { t = (async () => { const state = readState() const entry = getAgent(state, agent) return new AgentTelemetry(await agentSession(state, entry), entry.owner ?? installOwner(state)) })() telemetries.set(agent, t) t.catch(() => telemetries.delete(agent)) } return t } /** Record an error for an agent, in the background. A report that can't be written is only logged. */ function reportError(agent: unknown, source: Source, error: unknown, extra: { method?: string; provider?: string; model?: string } = {}) { // Languid's own bugs always reach the ops log (no content, just the agent's name and the error). if (source === 'service') ops.event(errorEvent('error', 'bug', error, { subject: typeof agent === 'string' ? agent : undefined, method: extra.method })) if (opts.telemetry === false || typeof agent !== 'string' || !readState().agents[agent]) return void writeReport(agent, errorReport(source, error, extra)) } async function writeReport(agent: string, report: ErrorReport) { try { const rkey = await (await telemetryOf(agent)).record(report) if (rkey) log(`error report ${rkey} for ${agent} (${report.source})`) return rkey } catch (e: any) { log(`error report for ${agent} not written: ${e?.message ?? e}`) return undefined } } /** The space each store was opened on. */ const storeSpaces = new Map() const locks = new Map>() const runs = new Map() /** When this process started: a call made before then with no result was cut off by the restart. */ const bootedAt = Date.now() /** runKey(agent, thread) -> id of the thread's active run. One run per thread; an agent's threads run at the same time. */ const activeRuns = new Map() const runKey = (agent: string, thread: string) => `${agent}/${thread}` /** Client message keys being written right now, so two sends of one key can't both start turns. */ const sending = new Set() /** Live controls of active runs: their open approvals, and stopping them. Not part of what getRun returns. */ const runControls = new Map void>; stop: () => void; setMode?: (m: ApprovalMode) => void }>() /** Approval request URI -> the run and call waiting on it. The records are the truth; this wakes the waiter. */ const approvalWaiters = new Map() /** Requests being answered right now, so two answers can't both be written. */ const answering = new Set() const hub = new RunnerHub(() => readState().runners) const wakeupAbort = new AbortController() let wakeupTimer: ReturnType | undefined let ticking: Promise | undefined const wakeupRetry = new Map() // One bounded item per agent per sweep. The wakeup's thread is the claim: it can't have another // run while this one goes, and the agent's other threads carry on. The agent lock covers only the // scan (it writes wakeup records), not the model call. A crash loses the claim; only tool-free // inference can be retried. /** When the last sweep for deleted agents past their grace period ran. */ let lastPurge = 0 /** Machine runs going on in the background (src/machines.ts). */ const machineRuns = new Set>() async function tickWakeups() { if (ticking) return ticking ticking = (async () => { if (Date.now() - lastPurge > 3600000) { lastPurge = Date.now() try { for (const name of await purgeDeletedAgents(readState())) { log(`deleted agent ${name}'s account for good: its grace period is over`) ops.event({ level: 'info', kind: 'purge', message: `deleted agent ${name}'s account for good`, subject: name }) } } catch (e: any) { log(`purging deleted agents failed: ${e?.message ?? e}`) ops.event(errorEvent('warn', 'purge', e, { message: 'purging deleted agents failed' })) } } for (const name of Object.keys(readState().agents)) { if (wakeupAbort.signal.aborted) return await idleCompaction(name) if ((wakeupRetry.get(name)?.after ?? 0) > Date.now()) continue try { const claimed = await withLock(name, async () => { const h = await store(name, null) // After a restart, calls still waiting on the owner's answer get a run that waits with them. adoptAllParked(h) const pending = await reconcileWakeups(h) // The first one that can go: not running already, and not waiting on the owner's answer. const w = pending.find((p) => !activeRuns.has(runKey(name, p.value.thread)) && !(p.value.kind === 'machine' && parked(h, p.value.thread))) // Reserve only a real step, with no await between the check and the reservation. if (!w) return undefined const id = `wakeup:${crypto.randomUUID()}` activeRuns.set(runKey(name, w.value.thread), id) runs.set(id, { id, agent: name, owner: h.entry.owner, thread: w.value.thread, status: 'running', events: [], startedAt: new Date().toISOString() }) return { h, w, id } }) if (!claimed) continue const { h, w, id } = claimed const failed = (e: any) => { const failures = (wakeupRetry.get(name)?.failures ?? 0) + 1 wakeupRetry.set(name, { failures, after: Date.now() + Math.min(3600000, 30000 * 2 ** Math.min(failures, 7)) }) log(`wakeup for ${name} remains pending: ${e?.message ?? e}`) if (failures === 1) reportError(name, 'wakeup', e) } const work = async () => { try { if (limit('model', h.entry.owner)) throw new Error('owner model rate limit reached') const machine = w.value.kind === 'machine' ? h.recordsOf(NSID.machine).find((m) => m.uri === w.value.source.uri) : undefined if (w.value.kind === 'machine') { // As a chat turn: the model must have credentials, and Languid's credit pays only for its picks. const model = h.model(w.value.thread) if (!(await h.models.getAuth(model.provider as any).catch(() => undefined))) throw new Error(`no credentials for ${model.provider}`) const refusal = budgetRefusal(readState(), h.entry.owner, model, ownerFunded(h.entry.owner, model)) if (refusal) throw new Error(refusal) } const end = limiter ? limiter.startStream(h.entry.owner) : () => {} if (!end) throw new Error('owner model concurrency limit reached') let outcome = 'done' try { if (w.value.kind === 'machine') outcome = await usageTag.run({ machine: w.value.source.uri.slice(w.value.source.uri.lastIndexOf('/') + 1) }, () => performMachine(h, w, machine, wakeupAbort.signal)) else await performWakeup(h, w, wakeupAbort.signal) } finally { end() } wakeupRetry.delete(name) log(outcome === 'parked' ? `machine run ${w.rkey} for ${name} waits on the owner` : `wakeup ${w.rkey} completed for ${name}`) } catch (e: any) { failed(e) } finally { runs.delete(id) activeRuns.delete(runKey(name, w.value.thread)) } } // A Machine run can take minutes: it goes on in the background, so other agents' wakeups // don't wait for it. Its thread stays claimed (activeRuns) until it ends. if (w.value.kind === 'machine') { const p = work() machineRuns.add(p) void p.finally(() => machineRuns.delete(p)) } else await work() } catch (e: any) { const failures = (wakeupRetry.get(name)?.failures ?? 0) + 1 wakeupRetry.set(name, { failures, after: Date.now() + Math.min(3600000, 30000 * 2 ** Math.min(failures, 7)) }) log(`wakeup for ${name} remains pending: ${e?.message ?? e}`) if (failures === 1) reportError(name, 'wakeup', e) } } // Ticked by hand (tests): finish this sweep's Machine runs and idle compactions before returning. if (opts.wakeups === false) await Promise.all([...machineRuns, ...idleRuns]) })().finally(() => { ticking = undefined }) return ticking } // Idle compaction (src/compaction.ts): a long thread that has gone quiet is summarized while the // provider still has it cached. At most one per agent at a time, in the background; one try per // quiet stretch (the latest message it was tried after), so a failure or a thread with nothing // old enough isn't asked again until someone writes in it. const idleTried = new Map() const idleRetry = new Map() const idleBusy = new Set() const idleRuns = new Set>() /** Whether a thread's model runs on Languid's keys and credit (not the owner's own connection). */ const hostedModel = (owner: string, model: { provider: string; id: string }) => !ownerFunded(owner, model) && onLanguidKeys(model) async function idleCompaction(name: string) { if (idleBusy.has(name) || (idleRetry.get(name)?.after ?? 0) > Date.now()) return let found: { h: Harness; thread: string } | undefined try { found = await withLock(name, async () => { const h = await store(name, null) const limits = h.compactionSettings() // Each thread's latest message, in one pass (liveMessages per thread would sort them all each time). const lastOf = new Map() for (const m of h.messages) { if (m.value.stopReason === 'error' || m.value.stopReason === 'aborted') continue const prev = lastOf.get(m.value.thread) if (!prev || transcriptKey(m) > transcriptKey(prev)) lastOf.set(m.value.thread, m) } for (const t of h.threads) { const thread = t.rkey let model: Model try { model = h.model(thread) } catch { continue } const settings = idleSettings(h.config, hostedModel(h.entry.owner, model), limits?.threshold) if (!settings.enabled) continue const last = lastOf.get(thread) const candidate = { ephemeral: t.value.ephemeral, idleCompaction: t.value.idleCompaction, automated: !!h.wakeupOf(thread), running: activeRuns.has(runKey(name, thread)) || h.turnRunning(thread), parked: h.parkedCalls(thread).length > 0, ...(last ? { last: { rkey: last.rkey, role: last.value.role, stopReason: last.value.stopReason, createdAt: last.value.createdAt } } : {}), summarizedAt: h.latestSummary(thread)?.value.createdAt, contextTokens: 0, } // Cheap checks first; counting the context compiles the whole thread. if (!idleCompactionDue({ ...candidate, contextTokens: Infinity }, settings, Date.now(), idleTried.get(runKey(name, thread)))) continue // Too short counts as tried too: the thread doesn't grow until someone writes in it. idleTried.set(runKey(name, thread), last!.rkey) if (!idleCompactionDue({ ...candidate, contextTokens: h.contextTokens(thread) }, settings)) continue return { h, thread } } return undefined }) } catch (e: any) { log(`idle compaction scan for ${name} failed: ${e?.message ?? e}`) return } if (!found) return const { h, thread } = found const owner = h.entry.owner idleBusy.add(name) const p = (async () => { const model = h.model(thread) // No credit (or no credentials): skip quietly; the next turn compacts at the threshold as usual. if (budgetRefusal(readState(), owner, model, ownerFunded(owner, model))) return if (!(await h.models.getAuth(model.provider as any).catch(() => undefined))) return const end = limiter ? limiter.startStream(owner) : () => {} // Busy right now: try again next sweep. if (!end) return void idleTried.delete(runKey(name, thread)) try { const r = await h.compact(thread, { trigger: 'idle', signal: wakeupAbort.signal }) idleRetry.delete(name) log(r.compacted ? `idle compaction of ${name}'s thread ${thread}: ${r.tokensBefore} -> ${r.tokensAfter} tokens (${r.via})` : `idle compaction of ${name}'s thread ${thread} skipped: ${r.reason}`) } catch (e: any) { if (e instanceof BudgetError || wakeupAbort.signal.aborted) return const failures = (idleRetry.get(name)?.failures ?? 0) + 1 idleRetry.set(name, { failures, after: Date.now() + Math.min(3600000, 30000 * 2 ** Math.min(failures, 7)) }) log(`idle compaction of ${name}'s thread ${thread} failed: ${e?.message ?? e}`) if (failures === 1) ops.event(errorEvent('warn', 'other', e, { subject: name, message: 'idle compaction failed' })) } finally { end() } })().finally(() => { idleBusy.delete(name) idleRuns.delete(p) }) idleRuns.add(p) } /** The header a proxy in front of the service puts the client's address in, if there is one. */ const clientIpHeader = ((opts.limits && opts.limits.clientIpHeader) || process.env.LANGUID_CLIENT_IP_HEADER || '').toLowerCase() || undefined const limiter = opts.limits === false || (opts.limits === undefined && process.env.LANGUID_RATE_LIMITS === 'off') ? undefined : new RateLimiter({ ...opts.limits, clientIpHeader }) /** Seconds until `key` may make a request of this kind, or 0 (and the request is counted). */ const limit = (name: Parameters[0], key: string) => limiter?.take(name, key) ?? 0 /** * A page or form from a browser: a signed-in one counts against its owner, anyone else against * their address. A room on one Wi-Fi address shares that address, so its signed-in people mustn't. */ const pageLimit = (ip: string, who: { did: string } | undefined) => (who ? limit('owner', who.did) : limit('pages', ip)) /** Run fn with exclusive write access to one agent. */ function withLock(agent: string, fn: () => Promise): Promise { const prev = locks.get(agent) ?? Promise.resolve() const next = prev.then(fn, fn) const settled = next.catch(() => {}) locks.set(agent, settled) void settled.finally(() => { if (locks.get(agent) === settled) locks.delete(agent) }) return next } /** The owner of things recorded before sign-in existed. */ const installOwner = (state: State) => state.owner?.did ?? '' /** Set when an owner skips Home's What brings you to Languid? step. */ const ABOUT_SKIP_COOKIE = 'languid_about_skip' const agentsOf = (state: State, did: string) => Object.values(state.agents).filter((a) => a.owner === did).map((a) => a.name) /** Whose a runner is, by its key in state.runners. */ const runnerOwner = (state: State, key: string) => state.runners[key]?.owner ?? installOwner(state) /** The key of an owner's computer by name. Names are unique per owner; another owner can use the same one. */ const runnerKey = (state: State, owner: string, name: string) => Object.entries(state.runners).find(([key, r]) => r.name === name && runnerOwner(state, key) === owner)?.[0] /** * An agent's store. `owner` is the caller's DID: someone else's agent is reported as not found, * the same as one that doesn't exist. Pass null only for the service's own work. */ /** * Always-allow rules this service kept in state.json before they were computer permissions * records (Nov 14), or that a local pi saved there when it shares the service's state: moved into * the agent's space, for computers it still has, then dropped from state. */ async function importSavedRules(st: Harness) { const allow = async (computer: string, tool: string) => (st.computers.has(computer) ? (await st.allowAlways(computer, tool), 'saved' as const) : 'dropped' as const) const saved = await moveRules(st.entry.name, allow, { load: readState, save: saveState }) if (saved) log(`moved ${saved} saved approvals for ${st.entry.name} into its computer permissions records`) } function store(agent: unknown, owner: string | null): Promise { if (typeof agent !== 'string' || !agent) throw invalid('agent is required') const agents = readState().agents const known = Object.hasOwn(agents, agent) ? agents[agent] : undefined if (owner !== null && (!known || known.owner !== owner)) throw new XrpcHttpError(404, 'AgentNotFound', `no agent named "${agent}"`) let s = stores.get(agent) // The agent's space can move (admin migrate-authority): drop a store opened on the old one. if (s && known && storeSpaces.get(agent) !== known.space) { stores.delete(agent) loadedStores.delete(agent) s = undefined } if (!s) { if (known) storeSpaces.set(agent, known.space) s = (async () => { const state = readState() let entry try { entry = getAgent(state, agent) } catch { throw new XrpcHttpError(404, 'AgentNotFound', `no agent named "${agent}"`) } // Model calls are metered against the agent's owner (their budget, if they have one). const models = meteredModels(await ownerModels(entry.owner ?? installOwner(state)), entry.owner ?? installOwner(state), readState, saveState, log, (m) => ownerFunded(entry.owner ?? installOwner(state), m), entry.name) const st = new Harness(state, entry, await agentSession(state, entry), models) st.messenger = { send: (agent, thread, text, to) => discord.deliver(agent, thread, text, to), destinations: (agent) => discord.destinations(agent), reach: (agent, thread) => discord.reach(agent, thread), } if (opts.telemetry !== false) { st.feedback = { submit: async (agent, kind, text, model) => { const t = await telemetryOf(agent) const rkey = await t.recordFeedback(feedbackRecord(kind, text, model)) if (rkey) log(`feedback ${rkey} from ${agent}`) return { saved: !!rkey, ...(await t.isShared()) } }, // Runs only once the owner approved the call (feedback_share always asks). share: async (agent) => { await feedbackDestination() const owner = readState().agents[agent]?.owner ?? installOwner(readState()) const sharing = await (await telemetryOf(agent)).setSharing(true, owner, readState().pds) log(`${agent}'s owner approved sharing its error reports and feedback`) return `Shared: ${sharing.recipientHandle ?? sharing.recipient}, the Languid maintainer, can now read your error reports and feedback. Your owner can stop this on your settings page.` }, // The maintainer's own agents read what everyone shared. ...((entry.owner ?? installOwner(state)) === installOwner(state) && installOwner(state) ? { inbox: feedbackInboxText } : {}), } } st.runners = hub.forOwner(entry.owner ?? installOwner(state), (r) => r.owner ?? installOwner(readState())) // The agent's own evals: trials run on scratch copies the owner makes, in the background. st.evalHost = { authority: () => ownerActor(entry.owner ?? installOwner(state)), models, background: (task) => void task().catch((e) => log(`eval run for ${agent} failed: ${e?.message ?? e}`)), } await st.load() await importSavedRules(st) return st })() stores.set(agent, s) s.then((st) => (stores.get(agent) === s ? loadedStores.set(agent, st) : undefined), () => stores.delete(agent)) } return s } /** Computers can be added or removed by other requests; re-read them before placing calls. */ async function refreshRunners(s: Harness) { await s.refreshComputers() } /** A reasoning level from a request: pi's thinking levels; '' or absent is none given. */ const reasoningOf = (v: unknown): string | undefined => { if (v === undefined || v === '') return undefined if (typeof v !== 'string' || !REASONING_LEVELS.includes(v)) throw invalid(`reasoning is one of ${REASONING_LEVELS.join(', ')}`) return v } const requireThread = (s: Harness, thread: unknown) => { if (typeof thread !== 'string' || !s.threads.some((t) => t.rkey === thread)) throw invalid(`unknown thread ${thread}`) return thread } const requireComputer = (s: Harness, computer: unknown) => { if (computer === undefined || computer === '') return undefined try { return s.requireComputer(String(computer)) } catch (e: any) { throw new XrpcHttpError(400, 'UnknownComputer', e.message) } } /** A client's session: its computer, and its working directory there (only meaningful with a computer). */ const sessionOf = (s: Harness, input: { computer?: unknown; cwd?: unknown; contextWindow?: unknown }): SessionOptions => { const computer = requireComputer(s, input.computer) const cwd = computer && typeof input.cwd === 'string' && input.cwd.startsWith('/') ? input.cwd : undefined const contextWindow = windowOf(input.contextWindow) return { computer, ...(cwd ? { cwd } : {}), ...(contextWindow ? { contextWindow } : {}) } } /** A client's model window (local inference), from a query string or a body. */ const windowOf = (v: unknown): number | undefined => { const n = typeof v === 'string' ? Number(v) : v return typeof n === 'number' && Number.isInteger(n) && n > 0 ? n : undefined } /** Who sent a user turn: always the authenticated caller. `via` is the client's own label. */ const senderOf = (caller: { did: string }, via: unknown): Sender => { if (via === undefined || via === null || via === '') return { did: caller.did, via: 'api' } if (!isVia(via)) throw invalid('via must be a short lowercase label (letters, digits, ".", "_" or "-"), such as "discord"') return { did: caller.did, via } } const busyThread = (s: Harness, thread: string) => { const id = activeRuns.get(runKey(s.entry.name, thread)) if (id) { throw new XrpcHttpError(409, 'ThreadBusy', `a service-side run (${id}) is active on thread ${thread}`) } } /** The OAuth client, when sign-in is on. Also how the service acts as a signed-in owner (creating their agents' spaces). */ let protocol: OAuthProtocol | undefined /** Where new owners stop (src/funnel.ts): kept in the ops space. */ const funnel = new Funnel(() => opsSpace.open(), log) /** Record that an invited owner reached a step. Only owners who signed up with an invite are counted. */ function funnelStep(step: FunnelStep, did: string | undefined, extra: { detail?: string } = {}) { const state = readState() const o = did ? state.owners?.[did] : undefined if (!o || !state.owner) return void funnel.record(step, did!, { handle: o.handle, ...extra }) } /** An invited owner by handle (as typed at sign-in) or DID. */ const ownerByHandle = (handle: string) => { const h = handle.trim().replace(/^@/, '').toLowerCase() if (!h) return undefined const owners = readState().owners ?? {} return owners[h] ? h : Object.keys(owners).find((did) => owners[did].handle.toLowerCase() === h) } /** * An owner, to write as: the installation owner with the password this installation holds, anyone * else through their OAuth session. Evals are written this way, as is the authority over scratch spaces. */ async function ownerActor(did: string): Promise { const state = readState() if (state.owner && did === state.owner.did) return installationAuthority(state) if (!protocol) throw new XrpcHttpError(403, 'OwnerUnavailable', "this service can't act for that owner without sign-in") return { did, call: await protocol.act(did) } } /** * Send a message into a thread and see it answered inside the service: the agent works on its own * (model calls, tools, records) while the caller polls getRun. The message is a record before this * returns. Each thread has one run at a time, and an agent's threads run at the same time: if the * thread already has a run, that run answers the message too, and the caller follows it from * `since` (accepted: "queued"). A message sent again with the same clientMessageId isn't recorded * twice (accepted: "duplicate"). `approval` says what happens to computer tool calls that change * things in a new run; a queued message goes by the run it joins. See ApprovalMode. */ function validateImages(images: unknown) { if (!Array.isArray(images)) throw invalid('images must be a list') if (images.length > MAX_IMAGES) throw invalid(`at most ${MAX_IMAGES} images a message`) for (const i of images) { if (!i || typeof i.data !== 'string' || typeof i.mimeType !== 'string') throw invalid('each image is { data, mimeType }') if (!IMAGE_TYPES.has(i.mimeType)) throw invalid(`images must be PNG, JPEG, GIF or WebP, not ${i.mimeType}`) if (Buffer.byteLength(i.data, 'base64') > MAX_IMAGE_BYTES) throw invalid(`an image is at most ${MAX_IMAGE_BYTES / 1024 / 1024} MB`) } } async function startRun( caller: Caller, input: { agent: unknown; prompt: unknown; thread?: unknown; continue?: unknown; computer?: unknown; cwd?: unknown; via?: unknown; clientMessageId?: unknown; images?: { data: string; mimeType: string }[]; model?: { provider: string; id: string; reasoning?: string } }, approval: ApprovalMode, ): Promise<{ run: string; thread: string; accepted: 'started' | 'queued' | 'duplicate'; since: number; message: string }> { const { agent, prompt, thread, continue: latest, computer, cwd, via, clientMessageId, images = [], model: picked } = input validateImages(images) const h = await store(agent, caller.did) const name = h.entry.name if (typeof prompt !== 'string' || (!prompt.trim() && !images.length)) throw invalid('prompt is required') if (thread !== undefined && latest) throw invalid('pass thread or continue, not both') if (clientMessageId !== undefined && (typeof clientMessageId !== 'string' || !clientMessageId || clientMessageId.length > 128)) { throw invalid('clientMessageId must be a string of 1 to 128 characters') } const text = prompt.trim() await refreshRunners(h) const runSession = { ...sessionOf(h, { computer, cwd }), from: senderOf(caller, via) } let th: string | undefined = latest ? h.latestThread()?.rkey : thread === undefined ? undefined : requireThread(h, thread) // The same message again (a retry, a redelivered event): point at it rather than record it twice. const keyed = clientMessageId ? `${name}\u0000${caller.did}\u0000${clientMessageId}` : undefined if (keyed) { const seen = h.findClientMessage(caller.did, clientMessageId as string) if (seen) { const same = seen.value.parts.flatMap((p) => (p.type === 'text' ? [p.text] : [])).join('\n') === text && (th === undefined || seen.value.thread === th) if (!same) throw new XrpcHttpError(409, 'ClientMessageConflict', `clientMessageId ${clientMessageId} was already used for a different message`) const active = runs.get(activeRuns.get(runKey(name, seen.value.thread)) ?? '') return { run: active?.id ?? '', thread: seen.value.thread, accepted: 'duplicate', since: active?.events.length ?? 0, message: seen.uri } } if (sending.has(keyed)) throw new XrpcHttpError(409, 'ClientMessageConflict', `a message with clientMessageId ${clientMessageId} is being sent`) } let model try { model = picked ? h.models.getModel(picked.provider as any, picked.id) ?? h.model(th) : h.model(th) } catch (e: any) { throw new XrpcHttpError(400, 'ModelUnavailable', e.message) } const auth = await h.models.getAuth(model.provider as any).catch(() => undefined) if (!auth) throw new XrpcHttpError(400, 'NoCredentials', `no credentials for ${model.provider} on the service host`) const refusal = budgetRefusal(readState(), caller.did, model, ownerFunded(caller.did, model)) if (refusal) throw new XrpcHttpError(402, 'BudgetExhausted', refusal) // A new run holds one of the owner's model streams for as long as it goes. Reserved before the // message is written, so a refusal leaves nothing behind. let end: (() => void) | false | undefined if (!th || !activeRuns.has(runKey(name, th))) { end = limiter ? limiter.startStream(caller.did) : () => {} if (!end) throw new XrpcHttpError(429, 'TooManyRuns', 'too many runs going at once for this owner; wait for one to finish') } let sent if (keyed) sending.add(keyed) try { th ??= await withLock(name, async () => (await h.newThread(`${via === 'web' ? 'chat' : 'run'} ${new Date().toISOString().slice(0, 16)}`)).rkey) // A model picked for this thread in the chat before (or while) sending. const had = h.threads.find((t) => t.rkey === th)?.value.model if (picked && (had?.id !== picked.id || had?.provider !== picked.provider || had?.reasoning !== picked.reasoning)) { await withLock(name, () => h.setThreadModel(th!, picked)) } const content = images.length ? [{ type: 'text' as const, text }, ...images.map((i) => ({ type: 'image' as const, data: i.data, mimeType: i.mimeType }))] : text sent = await h.persistUser(th, { role: 'user', content, timestamp: Date.now() }, runSession.from, clientMessageId as string | undefined) } catch (e) { if (end) end() throw e } finally { if (keyed) sending.delete(keyed) } // From here to activeRuns.set there's no await, so a run that's finishing either sees this // message (it checks for one, then gives up the thread, in the same step) or has already gone. const key = runKey(name, th) const joined = runs.get(activeRuns.get(key) ?? '') if (joined) { if (end) end() log(`run ${joined.id} on ${name}/${th} takes a queued message`) funnelStep('first-chat', caller.did, { detail: typeof via === 'string' ? via : 'cli' }) return { run: joined.id, thread: th, accepted: 'queued', since: joined.events.length, message: sent.uri } } if (!end) { // The run we saw ended while the message was written: start one after all. end = limiter ? limiter.startStream(caller.did) : () => {} if (!end) { log(`message ${sent.uri} waits: owner at the concurrent run limit`) throw new XrpcHttpError(429, 'TooManyRuns', 'too many runs going at once for this owner; the message is saved and will be answered by the next run on its thread') } } const title = autoTitle(h, caller.did, th, text) const run = launchRun(h, caller, th, { model, auth, approval, via, runSession, release: end, ...(title ? { title } : {}) }) funnelStep('first-chat', caller.did, { detail: typeof via === 'string' ? via : 'cli' }) return { run: run.id, thread: th, accepted: 'started', since: 0, message: sent.uri } } /** * Name a new thread while its first message is answered (src/auto-title.ts): undefined unless the * thread has no name, this is its first message, and the owner hasn't turned auto-titling off. * The name is written only if the thread still has none by then, so it never replaces one the * owner or the agent gave it. The promise never rejects. */ function autoTitle(h: Harness, owner: string, th: string, text: string): Promise | undefined { const thread = h.threads.find((t) => t.rkey === th) if (!thread || thread.value.named || thread.value.ephemeral || !text) return undefined if (readState().preferences?.[owner]?.autoTitle === false) return undefined if (h.messages.filter((m) => m.value.thread === th && m.value.role === 'user').length !== 1) return undefined const name = h.entry.name return suggestTitle(h.models, text) .then(async (title) => { if (!title) return const rec = await withLock(name, () => h.exclusive(() => h.autoTitleThread(th, title))) if (rec) log(`auto-titled ${name}/${th}`) }) .catch((e: any) => log(`auto-titling ${name}/${th} failed: ${e?.message ?? e}`)) } /** * Start a run on a thread that has none: the agent answers it on its own while clients poll getRun. * The caller has reserved the owner's model stream (`release` gives it back) and made sure no run * is active on the thread. With `resume`, the run goes on from a reply whose calls all have * results, as after a restart cut the last run off. */ function launchRun( h: Harness, caller: Caller, th: string, o: { model: ReturnType; auth: { source?: string }; approval: ApprovalMode; via: unknown; runSession: SessionOptions; release: () => void; resume?: boolean; run?: Run; title?: Promise }, ): Run { const { model, auth, via, runSession, release } = o let approval = o.approval const name = h.entry.name const key = runKey(name, th) // A parked run (adoptParked) goes on as itself, so whoever follows it sees the rest. const run: Run = o.run ?? { id: crypto.randomUUID(), agent: name, owner: caller.did, thread: th, status: 'running', events: [], startedAt: new Date().toISOString() } run.status = 'running' activeRuns.set(key, run.id) runs.set(run.id, run) for (const old of [...runs.keys()].slice(0, Math.max(0, runs.size - MAX_RUNS_KEPT))) runs.delete(old) const push = (e: NewRunEvent) => run.events.push({ seq: run.events.length, ...e } as RunEvent) const turn = h.turn(th) const approvals = new Map void>() let stopped = false runControls.set(run.id, { approvals, stop: () => { stopped = true run.stopped = true for (const answer of [...approvals.values()]) answer(false, 'the run was stopped') turn.abort() }, // The chat's permission control, changed while the run goes: the calls still to come follow it. setMode: (m: ApprovalMode) => { approval = m }, }) /** * Ask the owner about one call, and wait for their answer (or the timeout). The question is an * ai.languid.approval.request record; the answer comes as an ai.languid.tool.approval * (answerApproval), and this waiter is only how the run hears of it. */ const ask = (call: { callId: string; tool: string; argumentsJson: string; assignedComputer?: string }) => new Promise((resolve) => { if (stopped) return resolve(false) let request: string | undefined const answer = (approved: boolean, reason?: string) => { if (!approvals.delete(call.callId)) return clearTimeout(timer) if (request) approvalWaiters.delete(request) push({ type: 'decision', id: call.callId, approved, ...(reason ? { reason } : {}) }) resolve(approved ? true : reason === 'no answer in time' ? NO_ANSWER : false) } // Unref'd: a process on its way out (a restart) doesn't wait on a question; its record outlives it. const timer = setTimeout(() => answer(false, 'no answer in time'), approvalTimeout) timer.unref?.() approvals.set(call.callId, answer) const always = alwaysRuleFor(call.tool, call.argumentsJson, call.assignedComputer) void writeRequest(h, th, call, always, Date.now() + approvalTimeout) .catch((e: any) => { log(`run ${run.id}: approval request for ${call.callId} not written: ${e?.message ?? e}`) return undefined }) .then((rec) => { if (!approvals.has(call.callId)) return if (rec) { request = rec.uri approvalWaiters.set(rec.uri, { run: run.id, callId: call.callId }) } push({ type: 'approval', id: call.callId, tool: call.tool, arguments: call.argumentsJson, computer: call.assignedComputer, machine: call.assignedComputer, ...(always ? { always } : {}), ...(request ? { request } : {}) }) }) }) /** * Auto mode (src/auto-approve.ts): true when the classifier approved the call, false when the * owner should be asked. Bills the check to the owner and logs the verdict to ops. */ const autoReview = async (call: { callId: string; tool: string; argumentsJson: string }): Promise => { const owner = run.owner let v: AutoVerdict if (autoRefusal(readState(), owner)) v = { decision: 'inbox', reason: 'no_credit', latencyMs: 0 } else { const latest = h.messages.findLast((m) => m.value.thread === th && m.value.role === 'user' && m.value.author === owner) const request = latest ? latest.value.parts.flatMap((p) => (p.type === 'text' ? [p.text] : [])).join('\n') : '' v = await autoApprove({ tool: call.tool, argumentsJson: call.argumentsJson, request }) } let charged = 0 if (v.usage && (v.usage.input || v.usage.output)) { try { const state = readState() charged = chargeCheck(state, owner, name, v.usage).charged saveState(state) } catch (e: any) { log(`run ${run.id}: auto check not billed: ${e?.message ?? e}`) } } ops.event({ level: v.reason === 'error' ? 'warn' : 'info', kind: 'auto_approval', subject: h.entry.agent.did, message: autoOpsMessage(call.tool, v, charged) }) log(`run ${run.id}: auto ${v.decision} ${call.tool} (${v.reason}${v.rule ? `:${v.rule}` : ''}${v.error ? `: ${v.error}` : ''}, ${v.latencyMs}ms)`) if (v.decision !== 'approved') return false push({ type: 'decision', id: call.callId, approved: true, reason: 'approved by Auto' }) return true } turn.hooks = { onTextDelta: (d: string) => (run.partial = (run.partial ?? '') + d), onAssistantDone: () => { const text = (run.partial ?? '').trim() if (text) push({ type: 'text', text }) run.partial = undefined }, // Only the web chat offers 👍/👎; other clients see the events they always have. onAssistantSaved: (rkey: string, hasText: boolean) => via === 'web' && hasText && push({ type: 'saved', message: rkey }), onToolCall: (call: any) => push({ type: 'tool_call', id: call.callId, tool: call.tool, arguments: call.argumentsJson, computer: call.assignedComputer, machine: call.assignedComputer }), onToolResult: (r: any) => push({ type: 'tool_result', id: r.callId, tool: r.tool, output: r.output, isError: r.isError }), onRetry: (attempt: number, error: string, waitMs: number) => log(`run ${run.id}: model call failed (${error}); try ${attempt + 1} in ${Math.round(waitMs / 1000)}s`), // The owner's rules for the computer decide first, even in a run with no one to ask: a deny // rule refuses the call whatever the run's mode, an allow rule runs it. The rest go by the // mode: asked about, all allowed (the chat's "allow all"), or denied. approve: async (call: { callId: string; tool: string; argumentsJson: string; assignedComputer?: string }) => { // Sharing is only ever the owner's own yes to this question: never "allow all" or a rule. if (call.tool === 'feedback_share') return approval === 'ask' || approval === 'auto' ? ask(call) : 'only your owner can agree to share, and no one is here to ask; they can share from your settings page' const rule = h.decideCall(call) if (rule.effect === 'deny') return `not allowed: ${rule.reason} (the owner's rules)` if (rule.effect === 'allow' || approval === 'allow') return true if (approval === 'auto') return (await autoReview(call)) ? true : ask(call) // Nobody said no: the client started the run with nobody to ask. Say so, rather than "the owner denied it". return approval === 'ask' ? ask(call) : NO_ONE_ASKED }, } log(`run ${run.id} started for ${name} on thread ${th} (${model.provider}/${model.id}, ${auth.source}, writes: ${approval})`) void (async () => { try { // turn.run answers the thread until it's no longer pending. A message can still land as it // returns, so check once more; the check and giving up the thread happen in one step. let resume = !!o.resume let title = o.title for (;;) { const answered = await turn.run(runSession, { resume }) resume = false if (stopped || !answered) break if (h.pending(th)) continue // A new thread's name, being written alongside: give it up to 5 s, so the run's last // status carries it. Then look for a message once more, as above. if (!title) break await Promise.race([title, new Promise((r) => setTimeout(r, TITLE_WAIT_MS).unref?.())]) title = undefined if (!h.pending(th)) break } run.status = 'done' } catch (e: any) { run.status = stopped ? 'done' : 'error' if (!stopped) { run.error = e?.message ?? String(e) reportError(name, 'run', e, { method: 'ai.languid.agent.run', provider: model.provider, model: model.id }) } } finally { activeRuns.delete(key) release() run.finishedAt = new Date().toISOString() run.partial = undefined runControls.delete(run.id) turn.hooks = {} log(`run ${run.id} ${stopped ? 'stopped' : run.status}${run.error ? `: ${run.error}` : ''}`) } })() return run } /** * Pick a thread up where a restart cut its run off: each call that never got a result gets one * saying it was interrupted, and a new run goes on from there, so the agent tries again (and asks * again, for a call that needs the owner's yes). Refused while the thread has a run, or when it * has nothing to pick up. */ async function resumeRun(caller: Caller, agent: unknown, thread: unknown, approval: ApprovalMode, via: string, computer?: unknown) { const h = await store(agent, caller.did) const name = h.entry.name const th = requireThread(h, thread) const key = runKey(name, th) const active = runs.get(activeRuns.get(key) ?? '') if (active) return { run: active.id, thread: th, accepted: 'queued' as const, since: active.events.length } if (!h.interruptedCalls(th, bootedAt).length) throw new XrpcHttpError(409, 'NothingToResume', 'this thread has nothing to pick up') const { model, auth, end } = await reserveModel(caller, h, th) try { const closed = await withLock(name, () => h.closeInterrupted(th, bootedAt)) if (closed) log(`${name}/${th}: ${closed} interrupted call(s) closed to pick the thread up`) } catch (e) { end() throw e } // Another resume (a second tab) may have started while the calls were closed. const raced = runs.get(activeRuns.get(key) ?? '') if (raced) { end() return { run: raced.id, thread: th, accepted: 'queued' as const, since: raced.events.length } } const runSession = { ...sessionOf(h, { computer }), from: senderOf(caller, via) } const run = launchRun(h, caller, th, { model, auth, approval, via, runSession, release: end, resume: true }) return { run: run.id, thread: th, accepted: 'started' as const, since: 0 } } /** The thread's model and its credentials, and one of the owner's model streams (`end` gives it back). */ async function reserveModel(caller: Caller, h: Harness, th: string) { await refreshRunners(h) let model try { model = h.model(th) } catch (e: any) { throw new XrpcHttpError(400, 'ModelUnavailable', e.message) } const auth = await h.models.getAuth(model.provider as any).catch(() => undefined) if (!auth) throw new XrpcHttpError(400, 'NoCredentials', `no credentials for ${model.provider} on the service host`) const refusal = budgetRefusal(readState(), caller.did, model, ownerFunded(caller.did, model)) if (refusal) throw new XrpcHttpError(402, 'BudgetExhausted', refusal) const end = limiter ? limiter.startStream(caller.did) : () => {} if (!end) throw new XrpcHttpError(429, 'TooManyRuns', 'too many runs going at once for this owner; wait for one to finish') return { model, auth, end } } /** * Write the ai.languid.approval.request for a call that asks. A script's method call comes as * `.`: its request points at the script's own tool.call, with `n` and `method`. */ async function writeRequest(h: Harness, thread: string, call: { callId: string; tool: string; argumentsJson: string; assignedComputer?: string }, always: AlwaysRule | undefined, expires: number) { let rec = h.getCall(call.callId) let n: number | undefined const dot = call.callId.lastIndexOf('.') if (!rec && dot > 0) { rec = h.getCall(call.callId.slice(0, dot)) n = Number(call.callId.slice(dot + 1)) } if (!rec) return undefined let intention: unknown try { intention = JSON.parse(call.argumentsJson)?.call_intention } catch {} const value: ApprovalRequest = { thread, call: { uri: rec.uri, cid: rec.cid }, ...(n !== undefined && Number.isInteger(n) ? { n, method: call.tool } : {}), tool: call.tool, arguments: call.argumentsJson, ...(typeof intention === 'string' ? { intention } : {}), ...(call.assignedComputer ? { computer: call.assignedComputer } : {}), ...(always ? { always } : {}), expiresAt: new Date(expires).toISOString(), createdAt: now(), } return h.writeApprovalRequest(value) } /** * The owner answers an approval request (the web chat, Discord, ai.languid.approval.answer): write * their ai.languid.tool.approval, then wake the run waiting on it. Refused (409 NotWaiting) when the * request is unknown, already answered, past its expiry, or nothing waits on it any more. `always` * also saves the request's always-allow rule, which can answer the run's other waiting calls too. */ async function answerApproval(h: Harness, requestUri: unknown, approved: boolean, o: { always?: boolean; by: string; via: string }) { if (typeof requestUri !== 'string' || !requestUri) throw invalid('request is required') adoptAllParked(h) const rkey = requestUri.slice(requestUri.lastIndexOf('/') + 1) const req = h.approvalRequest(rkey) if (!req || req.uri !== requestUri) throw notWaiting(`no approval request ${requestUri}`) if (h.approvalOf(rkey) || answering.has(requestUri)) throw notWaiting('it was already answered') if (Date.parse(req.value.expiresAt) <= Date.now()) throw notWaiting('it expired with no answer') if (req.value.machine) { // A Machine run parked on it: the answer is the record, and the next sweep goes on from it. if (o.always) throw invalid("there's no rule to save for this call") answering.add(requestUri) try { await h.writeApproval(req, { request: { uri: req.uri, cid: req.cid }, call: req.value.call, approved, by: o.by, via: o.via, createdAt: now() }) } finally { answering.delete(requestUri) } wakeupRetry.delete(h.entry.name) setTimeout(() => void tickWakeups(), 0).unref?.() return {} } const waiter = approvalWaiters.get(requestUri) const r = waiter && runs.get(waiter.run) const wake = waiter && runControls.get(waiter.run)?.approvals.get(waiter.callId) if (!waiter || !r || !wake) throw notWaiting('nothing waits on it any more (its run ended, or a restart stopped the script that asked)') if (o.always && !approved) throw invalid('always goes with approved: true') if (o.always && !(req.value.computer && req.value.always)) throw invalid("there's no rule to save for this call") answering.add(requestUri) try { await h.writeApproval(req, { request: { uri: req.uri, cid: req.cid }, call: req.value.call, approved, ...(o.always ? { always: true } : {}), by: o.by, via: o.via, createdAt: now() }) } finally { answering.delete(requestUri) } if (o.always) return { rule: await saveAlways(h, r, waiter.callId, o) } wake(approved) return {} } /** * Answer a run's waiting call by its call id (the web chat, Discord): through its request record * (answerApproval), or, for an ask whose request couldn't be written, in memory as before. */ async function answerCall(h: Harness, r: Run, id: unknown, approved: boolean, always: boolean, by: { by: string; via: string }) { const waiting = runControls.get(r.id)?.approvals const wake = typeof id === 'string' ? waiting?.get(id) : undefined if (!wake) throw new XrpcHttpError(409, 'NotWaiting', 'that call is not waiting for an answer (it was answered, timed out, or the run ended)') const asked = r.events.findLast((e) => e.type === 'approval' && e.id === id) as Extract | undefined if (asked?.request) return answerApproval(h, asked.request, approved, { always, ...by }) if (always) { if (!approved) throw invalid('always goes with approved: true') return { rule: await saveAlways(h, r, id as string, by) } } wake(approved) return {} } /** Threads of the agent with calls parked on an open request and no run: give each a parked run. */ function adoptAllParked(h: Harness) { // A Machine run's asks aren't a chat run's: its worker picks the thread up once they're answered. const threads = new Set(h.approvalRequests().filter((r) => r.value.n === undefined && !r.value.machine && h.requestOpen(r)).map((r) => r.value.thread)) for (const th of threads) adoptParked(h, th) } /** * A thread whose latest reply still waits on the owner's answer to calls that asked before a * restart: give it a run holding those questions, as a live run would, so the chat, Discord and * ai.languid.approval.answer can answer them. Once every one is answered (or expires), the * allowed calls run, the rest get their denial, and the run goes on from there. */ function adoptParked(h: Harness, th: string): Run | undefined { const name = h.entry.name const key = runKey(name, th) if (activeRuns.has(key)) return undefined const parked = h.parkedCalls(th) if (!parked.length) return undefined const run: Run = { id: crypto.randomUUID(), agent: name, owner: h.entry.owner, thread: th, status: 'running', events: [], startedAt: new Date().toISOString() } activeRuns.set(key, run.id) runs.set(run.id, run) const push = (e: NewRunEvent) => run.events.push({ seq: run.events.length, ...e } as RunEvent) const approvals = new Map void>() const outcomes = new Map() const timers: ReturnType[] = [] let stopped = false for (const c of parked) { const req = h.requestFor(c.rkey)! const callId = c.value.callId approvals.set(callId, (approved, reason) => { if (!approvals.delete(callId)) return approvalWaiters.delete(req.uri) push({ type: 'decision', id: callId, approved, ...(reason ? { reason } : {}) }) outcomes.set(callId, approved ? true : reason === 'no answer in time' ? NO_ANSWER : reason === 'the run was stopped' ? reason : 'the owner denied this tool call') if (approvals.size) return for (const t of timers) clearTimeout(t) void continueParked(h, run, outcomes, stopped) }) approvalWaiters.set(req.uri, { run: run.id, callId }) const timer = setTimeout(() => approvals.get(callId)?.(false, 'no answer in time'), Math.max(0, Date.parse(req.value.expiresAt) - Date.now())) timer.unref?.() timers.push(timer) push({ type: 'tool_call', id: callId, tool: c.value.tool, arguments: c.value.argumentsJson, computer: c.value.assignedComputer, machine: c.value.assignedComputer }) push({ type: 'approval', id: callId, tool: c.value.tool, arguments: c.value.argumentsJson, computer: c.value.assignedComputer, machine: c.value.assignedComputer, ...(req.value.always ? { always: req.value.always } : {}), request: req.uri }) } runControls.set(run.id, { approvals, stop: () => { stopped = true run.stopped = true for (const answer of [...approvals.values()]) answer(false, 'the run was stopped') }, }) log(`run ${run.id}: ${name}/${th} waits on the owner for ${parked.length} call(s) asked before a restart`) return run } /** Settle a parked run's calls by the owner's answers, then go on as the same run (unless stopped). */ async function continueParked(h: Harness, run: Run, outcomes: Map, stopped: boolean) { const name = h.entry.name const th = run.thread const key = runKey(name, th) const push = (e: NewRunEvent) => run.events.push({ seq: run.events.length, ...e } as RunEvent) let launched = false try { for (const [callId, outcome] of outcomes) { const call = h.getCall(callId) if (!call) continue let output: string let isError = true const rule = outcome === true && call.value.tool !== 'feedback_share' ? h.decideCall(call.value) : undefined if (outcome !== true) output = outcome else if (rule?.effect === 'deny') output = `not allowed: ${rule.reason} (the owner's rules)` else { try { let args: unknown = {} try { args = JSON.parse(call.value.argumentsJson) } catch {} const r = await h.executeTool(call.value.tool, callId, args) output = r.text isError = r.isError } catch (e: any) { output = e?.message ?? String(e) } } push({ type: 'tool_result', id: callId, tool: call.value.tool, output, isError }) await withLock(name, () => h.persistToolResult(th, { role: 'toolResult', toolCallId: callId, toolName: call.value.tool, content: [{ type: 'text', text: output }], isError, timestamp: Date.now() })) } if (stopped) return const caller: Caller = { did: run.owner, via: 'session' } const { model, auth, end } = await reserveModel(caller, h, th) try { // Its other calls (none, usually: a reply's calls run side by side) were cut off by the restart. await withLock(name, () => h.closeInterrupted(th)) } catch (e) { end() throw e } activeRuns.delete(key) runControls.delete(run.id) const runSession = { ...sessionOf(h, {}), from: senderOf(caller, 'web') } launchRun(h, caller, th, { model, auth, approval: 'ask', via: 'web', runSession, release: end, resume: true, run }) launched = true } catch (e: any) { run.status = 'error' run.error = e?.message ?? String(e) log(`run ${run.id} (parked) failed: ${run.error}`) } finally { if (!launched) { if (run.status === 'running') run.status = 'done' run.finishedAt = new Date().toISOString() if (activeRuns.get(key) === run.id) activeRuns.delete(key) runControls.delete(run.id) } } } /** The agent's open requests something still waits on (in one thread, if given), newest first. */ function pendingRequests(h: Harness, thread?: string) { adoptAllParked(h) return h .approvalRequests() // A Machine's asks wait with no run: its thread is the run, parked until they're answered. .filter((r) => (thread === undefined || r.value.thread === thread) && h.requestOpen(r) && (approvalWaiters.has(r.uri) || !!r.value.machine)) .reverse() } /** A run of the caller's, by id; someone else's is not found. */ function ownRun(id: unknown, caller: { did: string }) { const r = typeof id === 'string' ? runs.get(id) : undefined if (!r || r.owner !== caller.did) throw new XrpcHttpError(404, 'RunNotFound', `no run ${id}`) return r } /** The events of a run from `since` on, and where to continue. */ function runStatus(r: Run, since: unknown) { const from = Math.max(0, Number(since ?? 0) || 0) const { events, ...rest } = r return { ...rest, run: r.id, events: events.slice(from), next: events.length } } /** * Save the rule an approval's "Always allow" offers, then allow that call and every other call in * the run waiting on something the rules now allow. Returns the rule saved. */ async function saveAlways(h: Harness, r: Run, id: string, by: { by: string; via: string }): Promise<{ computer: string; tool: string; command?: string }> { const asked = r.events.find((e) => e.type === 'approval' && e.id === id) as Extract | undefined if (!asked?.computer || !asked.always) throw invalid("there's no rule to save for this call") const rule = { computer: asked.computer, tool: asked.tool, ...(asked.always.command ? { command: asked.always.command } : {}) } if (rule.command) await h.allowCommand(rule.computer, rule.command) else await h.allowAlways(rule.computer, rule.tool) const reason = rule.command ? `from now on, \`${rule.command}\` runs on ${rule.computer} without asking` : `from now on, ${rule.tool} runs on ${rule.computer} without asking` const waiting = runControls.get(r.id)?.approvals waiting?.get(id)?.(true, reason) for (const e of [...r.events]) { if (e.type !== 'approval' || !waiting?.has(e.id)) continue if (h.decideCall({ tool: e.tool, argumentsJson: e.arguments, assignedComputer: e.computer }).effect !== 'allow') continue // The rule answers this one too: record that, as the owner's yes. const req = e.request ? h.approvalRequest(e.request.slice(e.request.lastIndexOf('/') + 1)) : undefined if (req && h.requestOpen(req) && !answering.has(req.uri)) { await h.writeApproval(req, { request: { uri: req.uri, cid: req.cid }, call: req.value.call, approved: true, by: by.by, via: by.via, createdAt: now() }).catch((err: any) => log(`approval for ${req.uri} not written: ${err?.message ?? err}`)) } waiting.get(e.id)?.(true, reason) } return rule } /** What the owner's rules say about a recorded call: allow, deny or ask. */ function ruleForCall(s: Harness, callId: string) { const call = s.getCall(callId)?.value return call ? s.decideCall(call) : { effect: 'ask' as const, reason: 'unknown call' } } const queries: Record = { // The agent's always-allow rules: which tools run on which of its computers without asking (pi's /approvals). 'ai.languid.computer.listPermissions': async ({ agent }, caller) => ({ rules: (await store(agent, caller.did)).alwaysAllowed() }), // Every permission the owner set for the agent, for the CLI: each computer's rules (its own, // and any left for a name it no longer has), and its script rules. 'ai.languid.agent.getPermissions': async ({ agent }, caller) => { const s = await store(agent, caller.did) await refreshRunners(s) const names = [...new Set([...s.computers.keys(), ...s.allRules().map((r) => r.computer)])].sort() return { computers: names.map((computer) => ({ computer, attached: s.computers.has(computer), rules: s.rulesFor(computer) })), policy: s.policy(), defaultPolicy: s.config.policy === undefined, } }, 'ai.languid.run.list': async ({ agent }, caller) => { const h = await store(agent, caller.did) const machines = await withLock(h.entry.name, () => migrated(h)) const [wakeups, messages] = await Promise.all([h.repo.list(NSID.wakeup), h.repo.list(NSID.message)]) return { schedules: machines.filter((m) => m.value.trigger.type === 'schedule').map(asSchedule), machines, wakeups, pending: pendingWakeups(wakeups, messages).map(ref), wakeOnJobEnd: machines.some((m) => m.value.trigger.type === 'jobEnd' && m.value.enabled), } }, // Not ai.languid.machine.list: that's the old name of ai.languid.computer.list (LEGACY_METHODS). 'ai.languid.machine.listMachines': async ({ agent }, caller) => { const machines = await machinesOf(await store(agent, caller.did)) return { machines: machines.map(({ runs, last, ...m }) => ({ ...m, ...(last ? { lastRun: { uri: last.wakeup.uri, thread: last.wakeup.value.thread, createdAt: last.wakeup.value.createdAt, status: last.status } } : {}) })), } }, 'ai.languid.agent.describe': async ({ agent }, caller) => { const s = await store(agent, caller.did) await refreshRunners(s) return { name: s.entry.name, did: s.entry.agent.did, handle: s.entry.agent.handle, space: s.entry.space, model: { provider: s.config.provider, id: s.config.model }, blocks: [...s.blocks.values()].map((b) => b.value), computers: [...s.computers.keys()], machines: [...s.computers.keys()], // for clients from before the rename threads: s.threads.map((t) => ({ rkey: t.rkey, title: t.value.title })), } }, // The agent's threads, most recently active first (pi's /threads). 'ai.languid.thread.list': async ({ agent }, caller) => ({ threads: (await store(agent, caller.did)).threadList() }), // Models this service can call (it has credentials for their provider). Clients offer these // for an agent's model; calls themselves go through ai.languid.model.stream. // Registered computers and whether their runners are connected. 'ai.languid.computer.list': async (_input, caller) => { const state = readState() const computers = Object.entries(state.runners).filter(([key]) => runnerOwner(state, key) === caller.did).map(([key, r]) => ({ name: r.name, online: hub.online(key), lastSeenAt: hub.lastSeen(key), registeredAt: r.registeredAt, legacy: !r.tokenHash || undefined, })) return { computers, machines: computers } // `machines` for clients from before the rename }, 'ai.languid.model.list': async (_input, caller) => { const models = await (await ownerModels(caller.did)).getAvailable() return { models: models.map((m) => ({ provider: m.provider, id: m.id, name: m.name, reasoning: m.reasoning, input: m.input, cost: m.cost, contextWindow: m.contextWindow, maxTokens: m.maxTokens, })), } }, // Status and events of a service-side run, from event `since` on. 'ai.languid.agent.getRun': async ({ run, since }, caller) => runStatus(ownRun(run, caller), since), // Calls waiting for the owner's yes: open requests something still waits on, newest first. 'ai.languid.approval.list': async ({ agent, thread }, caller) => { const h = await store(agent, caller.did) if (thread !== undefined && typeof thread !== 'string') throw invalid('thread must be a thread record key') const approvals = pendingRequests(h, thread as string | undefined).map((r) => ({ uri: r.uri, cid: r.cid, thread: r.value.thread, call: r.value.call, ...(r.value.n !== undefined ? { n: r.value.n, method: r.value.method } : {}), tool: r.value.tool, arguments: r.value.arguments, ...(r.value.intention ? { intention: r.value.intention } : {}), ...(r.value.computer ? { computer: r.value.computer } : {}), ...(r.value.always ? { always: r.value.always } : {}), ...(r.value.machine ? { machine: r.value.machine } : {}), expiresAt: r.value.expiresAt, createdAt: r.value.createdAt, ...(approvalWaiters.has(r.uri) ? { run: approvalWaiters.get(r.uri)!.run } : {}), })) return { approvals } }, // The full transcript for the next model request: system message (prompt, blocks, tools), then the thread. 'ai.languid.agent.getContext': async ({ agent, thread, ...session }, caller) => { const s = await store(agent, caller.did) await refreshRunners(s) const th = requireThread(s, thread) return { messages: s.compileTranscript(th, undefined, sessionOf(s, session)) } }, } // Who the caller is, and ending a login session. queries['ai.languid.session.get'] = async (_input, caller) => { const state = readState() const o = state.owners?.[caller.did] const budget = o?.budgetUsd !== undefined ? { budgetUsd: o.budgetUsd, spentUsd: Number((o.spentUsd ?? 0).toFixed(4)) } : {} return { did: caller.did, handle: caller.handle, via: caller.via, agents: agentsOf(state, caller.did), ...budget } } // Inviting owners and setting their budgets: the installation owner only. const requireInstallOwner = (caller: Caller) => { if (caller.did !== installOwner(readState())) throw new XrpcHttpError(403, 'Forbidden', 'only the installation owner can do this') } const usd = (v: unknown, what: string) => { if (v === undefined || v === null) return undefined if (typeof v !== 'number' || !Number.isFinite(v) || v < 0 || v > 100_000) throw invalid(`${what} must be a number of US dollars`) return v } // Error reports (src/telemetry.ts). The owner sees and decides about their agents' reports; the // recipient sees the reports owners shared with them, and only those. const ownAgent = (agent: unknown, caller: Caller) => { if (typeof agent !== 'string' || !agent) throw invalid('agent is required') if (readState().agents[agent]?.owner !== caller.did) throw new XrpcHttpError(404, 'AgentNotFound', `no agent named "${agent}"`) return agent } queries['ai.languid.telemetry.getStatus'] = async ({ agent }, caller) => (await telemetryOf(ownAgent(agent, caller))).status() /** The installation owner also reads what's shared with the ops account, the feedback destination. */ const readsAs = (did: string) => (did === installOwner(readState()) ? maintainerDids() : [did]) queries['ai.languid.telemetry.listShared'] = async ({ limit }, caller) => ({ agents: await sharedWith(readsAs(caller.did), Math.min(500, Math.max(1, Number(limit) || 100))) }) /** The reports owners have shared with any of `dids`: only agents whose owner said yes and which have that recipient as a member. */ async function sharedWith(dids: string[], n: number) { const state = readState() const agents: { agent: string; handle: string; reports: (ErrorReport & { rkey: string })[]; feedback: (Feedback & { rkey: string })[] }[] = [] for (const [name, entry] of Object.entries(state.agents)) { try { const t = await telemetryOf(name) const sharing = await t.existingSharing() if (!sharing?.share || !dids.includes(sharing.recipient)) continue if (!(await t.members()).some((m) => m.did === sharing.recipient)) continue agents.push({ agent: name, handle: entry.agent.handle, reports: (await t.list(n)).map((r) => ({ ...r.value, rkey: r.rkey })), feedback: (await t.listFeedback(n)).map((r) => ({ ...r.value, rkey: r.rkey })), }) } catch (e: any) { log(`shared reports: ${name}'s unreadable: ${e?.message ?? e}`) } } return agents } /** The feedback inbox, for the installation owner's own clients (a local CLI run's feedback_inbox). */ queries['ai.languid.telemetry.feedbackInbox'] = async ({ kind, limit }, caller) => { requireInstallOwner(caller) return { text: await feedbackInboxText({ kind: typeof kind === 'string' && kind ? kind : undefined, limit: Number(limit) || undefined }) } } queries['ai.languid.admin.listOwners'] = async (_input, caller) => { requireInstallOwner(caller) const state = readState() const owners = Object.entries(state.owners ?? {}).map(([did, o]) => ({ ...ownerSummary(did, o), agents: agentsOf(state, did) })) const invites = Object.values(state.ownerInvites ?? {}).filter((v) => Date.parse(v.expiresAt) > Date.now()) return { owners, openInvites: invites.length } } const procedures: Record = { // A client's own error (the CLI's crash), recorded for one of the caller's agents. 'ai.languid.telemetry.report': async ({ agent, error }, caller) => { const name = ownAgent(agent, caller) if (typeof error !== 'object' || !error) throw invalid('error is required') const t = await telemetryOf(name) const rkey = await writeReport(name, clientReport(error)) const sharing = await t.existingSharing() return { ...(rkey ? { rkey } : {}), decided: !!sharing, shared: !!sharing?.share, recipient: TELEMETRY_RECIPIENT() } }, 'ai.languid.telemetry.setSharing': async ({ agent, share }, caller) => { const name = ownAgent(agent, caller) if (typeof share !== 'boolean') throw invalid('share must be true or false') let sharing try { if (share) await feedbackDestination() sharing = await (await telemetryOf(name)).setSharing(share, caller.did, readState().pds) } catch (e: any) { if (e instanceof TelemetryError) throw invalid(e.message) throw e } log(`${caller.handle} ${share ? 'shares' : 'stopped sharing'} ${name}'s error reports${share ? ` with ${sharing.recipientHandle ?? sharing.recipient}` : ''}`) return { sharing } }, // Feedback from one of the caller's agents run outside the service (the CLI's feedback tool), saved like the service's own. 'ai.languid.telemetry.sendFeedback': async ({ agent, kind, text, provider, model }, caller) => { const name = ownAgent(agent, caller) if (opts.telemetry === false) throw invalid('feedback is off on this service') const t = await telemetryOf(name) let rkey try { rkey = await t.recordFeedback(feedbackRecord(kind, text, { ...(typeof provider === 'string' ? { provider } : {}), ...(typeof model === 'string' ? { model } : {}) })) } catch (e: any) { if (e instanceof TelemetryError) throw invalid(e.message) throw e } if (rkey) log(`feedback ${rkey} from ${name} (via ${caller.via})`) return { saved: !!rkey, ...(await t.isShared()) } }, 'ai.languid.telemetry.clear': async ({ agent }, caller) => ({ deleted: await (await telemetryOf(ownAgent(agent, caller))).clear() }), // Always allow a tool on one of the agent's computers (pi's "Yes, and always allow"). Only tools // that otherwise ask, and only computers the agent has: a rule for a name it doesn't have yet // would quietly cover whichever computer later takes that name. 'ai.languid.computer.allowTool': async ({ agent, computer, tool }, caller) => { if (typeof computer !== 'string' || !computer || typeof tool !== 'string') throw invalid('computer and tool are required') if (!needsApproval(tool)) throw invalid(`${tool} doesn't ask, so there's nothing to always allow`) const s = await store(agent, caller.did) await refreshRunners(s) if (!s.computers.has(computer)) throw new XrpcHttpError(400, 'UnknownComputer', `${s.entry.name} has no computer named "${computer}"`) await s.allowAlways(computer, tool) log(`${caller.handle ?? caller.did} always allowed ${tool} on ${computer} for ${s.entry.name}`) return { rules: s.alwaysAllowed() } }, // Ask before a tool on a computer again; without a tool, before every tool there. 'ai.languid.computer.revokeTool': async ({ agent, computer, tool }, caller) => { if (typeof computer !== 'string' || !computer || (tool !== undefined && typeof tool !== 'string')) throw invalid('computer is required, and tool must be a string') const s = await store(agent, caller.did) await s.askAgain(computer, tool) return { rules: s.alwaysAllowed() } }, // Replace a computer's rules (as the settings page's form does). Only computers the agent has, // except that a computer's rules can always be cleared. 'ai.languid.computer.setRules': async ({ agent, computer, rules }, caller) => { if (typeof computer !== 'string' || !computer) throw invalid('computer is required') if (!rules || typeof rules !== 'object') throw invalid('rules is required') const list = (v: unknown, field: string): string[] => { if (v === undefined) return [] if (!Array.isArray(v) || v.some((x) => typeof x !== 'string')) throw invalid(`rules.${field} must be a list of strings`) return v } const allow = list(rules.allow, 'allow') const deny = list(rules.deny, 'deny') for (const t of [...allow, ...deny]) if (!(RULE_TOOLS as readonly string[]).includes(t)) throw invalid(`rules only cover ${RULE_TOOLS.join(', ')}, not ${t}`) if (allow.some((t) => deny.includes(t))) throw invalid('a tool can be allowed or denied, not both') let next: ComputerRules try { next = { allow: [...new Set(allow)], deny: [...new Set(deny)], allowCommands: parseCommands(list(rules.allowCommands, 'allowCommands').join('\n'), 'commands that run without asking'), denyCommands: parseCommands(list(rules.denyCommands, 'denyCommands').join('\n'), 'commands it may never run'), } } catch (e: any) { throw invalid(e.message) } const s = await store(agent, caller.did) await refreshRunners(s) const empty = !next.allow.length && !next.deny.length && !next.allowCommands.length && !next.denyCommands.length if (!empty && !s.computers.has(computer)) throw new XrpcHttpError(400, 'UnknownComputer', `${s.entry.name} has no computer named "${computer}"`) await s.setRules(computer, next) log(`${caller.handle ?? caller.did} set ${s.entry.name}'s rules on ${computer}: ${JSON.stringify(next)}`) return { computer, rules: s.rulesFor(computer) } }, // Replace the agent's script rules; null (or the default's own rules) goes back to the default. 'ai.languid.agent.setPolicy': async ({ agent, policy }, caller) => { let next: ReturnType | undefined if (policy !== null) { try { next = parsePolicy(policy) } catch (e: any) { throw invalid(e.message) } if (next.rules.length > SETTINGS_LIMITS.policyRules) throw invalid(`at most ${SETTINGS_LIMITS.policyRules} rules`) if (JSON.stringify(next) === JSON.stringify(parsePolicy(DEFAULT_POLICY))) next = undefined } const s = await store(agent, caller.did) await withLock(s.entry.name, () => s.updateConfig({ policy: next })) log(`${caller.handle ?? caller.did} set ${s.entry.name}'s script policy (${next ? `${next.rules.length} rules` : 'default'})`) return { policy: s.policy(), defaultPolicy: next === undefined } }, 'ai.languid.admin.inviteOwner': async ({ ttlDays, budgetUsd, model, uses }, caller) => { requireInstallOwner(caller) if (uses !== undefined && (!Number.isInteger(uses) || uses < 1 || uses > MAX_INVITE_USES)) throw invalid(`uses must be a whole number from 1 to ${MAX_INVITE_USES}`) const days = ttlDays === undefined ? 7 : Number(ttlDays) if (!Number.isFinite(days) || days <= 0 || days > 90) throw invalid('ttlDays must be between 0 and 90') if (model !== undefined && (typeof model?.provider !== 'string' || typeof model?.model !== 'string')) throw invalid('model must be { provider, model }') if (model && !(await getModels()).getModel(model.provider as any, model.model)) throw invalid(`unknown model ${model.provider}/${model.model}`) const state = readState() const { code, invite } = newOwnerInvite(state, { ttlDays: days, budgetUsd: usd(budgetUsd, 'budgetUsd'), model, uses }) saveState(state) log(`owner invite made (expires ${invite.expiresAt}${invite.budgetUsd !== undefined ? `, budget $${invite.budgetUsd}` : ''})`) const base = 'origin' in where ? where.origin : opts.url return { code, url: base ? `${base}/signup?code=${code}` : undefined, ...invite } }, 'ai.languid.admin.setBudget': async ({ owner, budgetUsd }, caller) => { requireInstallOwner(caller) const state = readState() const did = findOwnerDid(state, owner) if (!did) throw new XrpcHttpError(404, 'OwnerNotFound', `no invited owner ${owner}`) const entry = state.owners![did] const amount = usd(budgetUsd, 'budgetUsd') if (amount === undefined) entry.budgetUsd = undefined else entry.budgetUsd = amount saveState(state) log(`budget for ${entry.handle}: ${amount === undefined ? 'none' : `$${amount}`}`) return ownerSummary(did, entry) }, 'ai.languid.session.delete': async (_input, caller) => { if (!caller.session) throw invalid('the installation token is not a login session; it cannot be signed out') const state = readState() delete state.sessions?.[caller.session] saveState(state) // The last session of this owner: also give up the access they granted to their agent spaces. const now = Date.now() if (!Object.values(state.sessions ?? {}).some((v) => v.did === caller.did && Date.parse(v.expiresAt) > now)) await protocol?.forget(caller.did) return {} }, // Create an agent for the caller: its account on this PDS, its space under the caller's own DID. 'ai.languid.agent.create': async ({ name, model }, caller) => { if (typeof name !== 'string' || !name) throw invalid('name is required') if (model !== undefined && (typeof model?.provider !== 'string' || typeof model?.model !== 'string')) throw invalid('model must be { provider, model }') return makeAgent(caller, name, model, { skipRateLimit: true }) }, // Machines (docs/machines.md): owner-only, like ai.languid.run.*. Agent scripts can't reach these. 'ai.languid.machine.create': async ({ agent, ...input }, caller) => createMachine(await store(agent, caller.did), input, caller.did), 'ai.languid.machine.update': async ({ agent, rkey, ...input }, caller) => updateMachine(await store(agent, caller.did), rkey, input, caller.did), 'ai.languid.machine.delete': async ({ agent, rkey }, caller) => deleteMachine(await store(agent, caller.did), rkey), // The old schedule methods, as shortcuts over Machines (a schedule is a schedule Machine with no tools). 'ai.languid.run.createSchedule': async ({ agent, ...input }, caller) => createSchedule(await store(agent, caller.did), input, caller.did), 'ai.languid.run.setScheduleEnabled': async ({ agent, rkey, enabled }, caller) => { if (typeof rkey !== 'string' || typeof enabled !== 'boolean') throw invalid('rkey and enabled are required') await updateMachine(await store(agent, caller.did), rkey, { enabled }, caller.did, 'ScheduleNotFound') return {} }, 'ai.languid.run.deleteSchedule': async ({ agent, rkey }, caller) => deleteMachine(await store(agent, caller.did), rkey, 'ScheduleNotFound'), 'ai.languid.run.setJobWakeups': async ({ agent, enabled }, caller) => { const h = await store(agent, caller.did) if (typeof enabled !== 'boolean') throw invalid('enabled must be boolean') await setJobWakeups(h, enabled, caller.did) return {} }, 'ai.languid.thread.create': async ({ agent, title }, caller) => { const s = await store(agent, caller.did) return withLock(s.entry.name, async () => { const t = await s.newThread(typeof title === 'string' && title ? title : `thread ${new Date().toISOString().slice(0, 16)}`) return { thread: t.rkey } }) }, 'ai.languid.thread.rename': async ({ agent, thread, title }, caller) => { const s = await store(agent, caller.did) const th = requireThread(s, thread) const clean = threadTitle(title) return withLock(s.entry.name, async () => { const rec = await s.renameThread(th, clean) return { thread: rec.rkey, title: rec.value.title } }) }, // Record one finished message. For an assistant message, also records its tool calls and // returns where each will run and whether it needs the owner's approval. 'ai.languid.message.record': async ({ agent, thread, message, computer, cwd, via }, caller) => { const s = await store(agent, caller.did) const th = requireThread(s, thread) busyThread(s, th) if (!message || typeof message !== 'object') throw invalid('message is required') const sender = senderOf(caller, via) return withLock(s.entry.name, async () => { if (message.role === 'user') { if (typeof message.content !== 'string' && !Array.isArray(message.content)) throw invalid('message.content must be a string or list') if (Array.isArray(message.content)) { validateImages(message.content.filter((c: any) => c?.type === 'image')) } await s.persistUser(th, message as UserMessage, sender) } else if (message.role === 'toolResult') await s.persistToolResult(th, message as ToolResultMessage) else if (message.role === 'assistant') { await refreshRunners(s) await s.persistAssistant(th, message as AssistantMessage, sessionOf(s, { computer, cwd })) const calls = (message as AssistantMessage).content.filter((b) => b.type === 'toolCall') return { calls: calls.map((tc) => ({ callId: tc.id, tool: tc.name, computer: s.getCall(tc.id)?.value.assignedComputer, machine: s.getCall(tc.id)?.value.assignedComputer, // for clients from before the rename // A call the owner's rules allow doesn't need pi to ask; one they deny is blocked. needsApproval: needsApproval(tc.name) && ruleForCall(s, tc.id).effect === 'ask', blocked: s.callProblem(tc.id, tc.name) ?? (needsApproval(tc.name) && ruleForCall(s, tc.id).effect === 'deny' ? `not allowed: ${ruleForCall(s, tc.id).reason} (the owner's rules)` : undefined), })), } } else throw invalid(`unsupported message role ${message.role}`) return {} }) }, // Execute a recorded tool call. Computer tools that change things need approved: true. 'ai.languid.tool.execute': async ({ agent, callId, tool, args, approved }, caller) => { const s = await store(agent, caller.did) if (typeof callId !== 'string' || typeof tool !== 'string') throw invalid('callId and tool are required') if (!specByName.has(tool)) throw invalid(`unknown tool ${tool}`) const call = s.getCall(callId) if (!call) throw invalid(`tool call ${callId} was not recorded; record its assistant message first`) if (call.value.tool !== tool) throw invalid(`tool call ${callId} is ${call.value.tool}, not ${tool}`) const blocked = s.callProblem(callId, tool) if (blocked) return { output: blocked, isError: true } if (tool === 'feedback_share' && approved !== true) return { output: 'the owner did not agree to share', isError: true } if (needsApproval(tool)) { const rule = ruleForCall(s, callId) if (rule.effect === 'deny') return { output: `not allowed: ${rule.reason} (the owner's rules)`, isError: true } if (approved !== true && rule.effect !== 'allow') return { output: 'the owner did not approve this tool call', isError: true } } const run = async () => { try { const r = await s.executeTool(tool, callId, args ?? {}) return { output: r.text, isError: r.isError, computer: r.computer, machine: r.computer, content: r.content, details: r.details } } catch (e: any) { return { output: e.message, isError: true } } } // Block edits write records, so they take the lock; computer tools and xrpc scripts (which lock // per write) run concurrently. return takesLock(specByName.get(tool)!) ? withLock(s.entry.name, run) : run() }, // Start a run inside the service: the agent works through `prompt` on its own (model calls, // tools, records) while the caller polls agent.getRun. Computer tools that change things are // denied unless approveWrites is true, since there's no one to ask. // tools, records) while the caller polls agent.getRun. `approval` says what computer tool calls that // change things do: "ask" waits for ai.languid.approval.answer, "allow" runs them, "deny" (the // default) refuses them. Older clients pass approveWrites: true for "allow"; `approval` wins. 'ai.languid.agent.run': async ({ agent, prompt, thread, continue: latest, approveWrites, approval, computer, cwd, via, clientMessageId }, caller) => { if (approval !== undefined && !APPROVAL_MODES.includes(approval)) throw invalid('approval must be "ask", "allow", "auto" or "deny"') return startRun(caller, { agent, prompt, thread, continue: latest, computer, cwd, via, clientMessageId }, approval ?? (approveWrites === true ? 'allow' : 'deny')) }, // The owner answers a call waiting for their yes: writes their ai.languid.tool.approval and // wakes the run. 409 NotWaiting when it was answered, expired, or nothing waits on it. 'ai.languid.approval.answer': async ({ agent, request, approved, always, via }, caller) => { const h = await store(agent, caller.did) if (typeof approved !== 'boolean') throw invalid('approved must be true or false') if (always !== undefined && typeof always !== 'boolean') throw invalid('always must be true or false') if (via !== undefined && (typeof via !== 'string' || !via || via.length > 64)) throw invalid('via must be a string of 1 to 64 characters') const out = await answerApproval(h, request, approved, { always: always === true, by: caller.did, via: (via as string | undefined) ?? 'xrpc' }) log(`${h.entry.name}: ${approved ? 'approved' : 'denied'} ${request}${out.rule ? ' (always)' : ''} via ${via ?? 'xrpc'}`) return out.rule ? { rule: out.rule } : {} }, // A one-time code a new computer passes to ai.languid.computer.register (`languid computer join`). 'ai.languid.computer.invite': async ({ agents, ttlMinutes }, caller) => { const names: string[] = Array.isArray(agents) ? agents.map(String) : [] const inv = inviteComputer(caller.did, names, ttlMinutes === undefined ? 15 : Number(ttlMinutes)) // The service's own address, for the join command: the caller may reach it under a local one. return { ...inv, ...(opts.url ? { url: opts.url } : {}) } }, // Grant existing agents access without re-registering the runner or rotating its token. 'ai.languid.computer.attach': async ({ name, agents }, caller) => { if (typeof name !== 'string' || !name) throw invalid('name is required') if (!Array.isArray(agents) || !agents.length || agents.some((a) => typeof a !== 'string' || !a.trim())) { throw invalid('agents must be a non-empty array of agent names') } return attachComputer(caller.did, name, agents) }, // Take a computer away from some agents: its record and always-allow rules there. The runner stays. 'ai.languid.computer.detach': async ({ name, agents }, caller) => { if (typeof name !== 'string' || !name) throw invalid('name is required') if (!Array.isArray(agents) || !agents.length || agents.some((a) => typeof a !== 'string' || !a.trim())) { throw invalid('agents must be a non-empty array of agent names') } return detachComputer(caller.did, name, agents) }, // Forget a computer: its record in every agent, its runner token, and its queued calls. 'ai.languid.computer.remove': async ({ name }, caller) => { if (typeof name !== 'string' || !name) throw invalid('name is required') return removeComputer(caller.did, name) }, // Summarize a thread's older messages now (pi's /compact). Without force, only if over the threshold. 'ai.languid.thread.compact': async ({ agent, thread, instructions, force, trigger, contextWindow }, caller) => { const s = await store(agent, caller.did) const th = requireThread(s, thread) busyThread(s, th) let model try { model = s.model() } catch (e: any) { throw new XrpcHttpError(400, 'ModelUnavailable', e.message) } if (!(await s.models.getAuth(model.provider as any).catch(() => undefined))) { throw new XrpcHttpError(400, 'NoCredentials', `no credentials for ${model.provider} on the service host`) } const opts = { instructions: typeof instructions === 'string' && instructions.trim() ? instructions.trim() : undefined, trigger: trigger === 'overflow' || trigger === 'threshold' ? trigger : force === false ? 'threshold' : 'manual', // A client on a smaller local model measures against its own window. contextWindow: windowOf(contextWindow), } as const const result = force === false ? ((await s.compactIfNeeded(th, undefined, opts.contextWindow)) ?? { compacted: false, reason: 'below the threshold' }) : await s.compact(th, { ...opts, force: true }) if (result.compacted) log(`compacted ${s.entry.name} thread ${th} (${opts.trigger})`) return result }, 'ai.languid.agent.setModel': async ({ agent, provider, model }, caller) => { const s = await store(agent, caller.did) if (typeof provider !== 'string' || typeof model !== 'string') throw invalid('provider and model are required') if (!provider || !model || provider.length > 64 || model.length > 160 || !/^[A-Za-z0-9._-]+$/.test(provider) || !/^[A-Za-z0-9._:/@-]+$/.test(model)) { throw invalid('invalid provider or model') } const available = await (await ownerModels(caller.did)).getAvailable() if (!available.some((m) => m.provider === provider && m.id === model)) { throw invalid(`this service can't call ${provider}/${model}`) } return withLock(s.entry.name, async () => { await s.setModel(provider, model) return { model: { provider, id: model } } }) }, } // The computer methods' old names, still answered for runners and CLIs installed before the // rename (Nov 15). Drop them once those have updated. for (const [now, old] of Object.entries(LEGACY_METHODS)) { if (queries[`ai.languid.computer.${now}`]) queries[old] = queries[`ai.languid.computer.${now}`] if (procedures[`ai.languid.computer.${now}`]) procedures[old] = procedures[`ai.languid.computer.${now}`] } /** Give one of the owner's computers to some of their agents, without re-registering its runner. */ async function attachComputer(owner: string, name: string, agents: string[]) { const state = readState() const key = runnerKey(state, owner, name) const runner = key ? state.runners[key] : undefined if (!runner) throw new XrpcHttpError(404, 'ComputerNotFound', `no computer named "${name}"`) const mine = agentsOf(state, owner) const names = [...new Set(agents)] // Validate the whole request before granting any access. Don't reveal other owners' agents. for (const agent of names) if (!mine.includes(agent)) throw new XrpcHttpError(404, 'AgentNotFound', `no agent named "${agent}"`) let node: ComputerNode | undefined for (const agent of mine) { const s = await store(agent, owner) // The store's copy covers an old machine.node record it couldn't move yet. node = (await s.repo.get(NSID.computer, name))?.value ?? s.computers.get(name)?.value if (node) break } if (node) { const { $type: _t, ...value } = node as ComputerNode & { $type?: string } node = value } // A runner can have been registered before any agents existed. node ??= { name, hostname: name, os: 'unknown', capabilities: ['posix-shell'], createdAt: runner.registeredAt, lastSeenAt: runner.registeredAt } let attachedTo = 0 for (const agent of names) { const s = await store(agent, owner) await withLock(s.entry.name, async () => { if (!(await s.repo.get(NSID.computer, name))) { await s.repo.put(NSID.computer, name, { ...node! }) attachedTo++ } await s.refreshComputers() }) } return { name, agents: names, attachedTo } } /** Take a computer away from some of the owner's agents. Its permissions there go with it. */ async function detachComputer(owner: string, name: string, agents: string[]) { const mine = agentsOf(readState(), owner) const names = [...new Set(agents)] for (const agent of names) if (!mine.includes(agent)) throw new XrpcHttpError(404, 'AgentNotFound', `no agent named "${agent}"`) let detachedFrom = 0 for (const agent of names) { const s = await store(agent, owner) await withLock(s.entry.name, async () => { if (await s.removeComputer(name)) detachedFrom++ // So another computer given this name later starts with no rules, asking. await s.forgetRules(name) await s.refreshComputers() }) } return { name, agents: names, detachedFrom } } /** Forget one of the owner's computers: its record in every agent, its runner token, and its queued calls. */ async function removeComputer(owner: string, name: string) { const state = readState() // Only the owner's own computer by that name: another owner's is left alone. const key = runnerKey(state, owner, name) const { detachedFrom: removedFrom } = await detachComputer(owner, name, agentsOf(state, owner)) if (key) { const fresh = readState() delete fresh.runners[key] saveState(fresh) hub.forget(key) } return { removedFrom, hadRunner: !!key } } /** A one-time code a new computer passes to ai.languid.computer.register. No agents: all of the owner's. */ function inviteComputer(owner: string, agents: string[], minutes: number) { const state = readState() const mine = agentsOf(state, owner) for (const a of agents) if (!mine.includes(a)) throw new XrpcHttpError(404, 'AgentNotFound', `no agent named "${a}"`) if (!(minutes > 0 && minutes <= 24 * 60)) throw invalid('ttlMinutes is 1-1440') const code = crypto.randomBytes(12).toString('base64url') const expiresAt = new Date(Date.now() + minutes * 60_000).toISOString() const fresh = Object.fromEntries(Object.entries(state.invites ?? {}).filter(([, v]) => Date.parse(v.expiresAt) > Date.now())) state.invites = { ...fresh, [code]: { expiresAt, agents, owner } } saveState(state) return { code, expiresAt, agents: agents.length ? agents : mine } } /** * Register a computer that ran `languid computer join`. The one-time invite code proves the owner sent * it. Returns a runner token: the computer keeps it, this service keeps only its hash. */ async function registerComputer(input: any) { const { code, name: wanted, hostname, os: osName, capabilities } = input ?? {} const state = readState() const invite = typeof code === 'string' && state.invites && Object.hasOwn(state.invites, code) ? state.invites[code] : undefined if (!invite || typeof invite !== 'object' || !(Date.parse(invite.expiresAt) > Date.now()) || !Array.isArray(invite.agents)) { throw new XrpcHttpError(403, 'InvalidInvite', 'invite code is unknown, used, or expired') } const host = typeof hostname === 'string' ? hostname : '' const name = typeof wanted === 'string' && wanted ? wanted : host.toLowerCase().split('.')[0].replace(/[^a-z0-9-]/g, '-') if (!/^[a-z0-9][a-z0-9-]{0,39}$/.test(name)) throw invalid('computer name: lowercase letters, digits, dashes (max 40)') const owner = invite.owner ?? installOwner(state) // Names are unique per owner. Another owner's computer by this name keeps its key, and this one // gets the name and a suffix from the owner's DID. const key = runnerKey(state, owner, name) ?? (Object.hasOwn(state.runners, name) ? `${name}~${hashToken(owner).slice(0, 12)}` : name) const token = crypto.randomBytes(32).toString('base64url') delete state.invites![code] // Re-joining under an existing name replaces its token; the old runner is cut off. hub.forget(key) state.runners[key] = { name, tokenHash: hashToken(token), registeredAt: new Date().toISOString(), owner } saveState(state) const mine = agentsOf(state, owner) const agents = invite.agents.length ? invite.agents.filter((a) => mine.includes(a)) : mine const t = new Date().toISOString() for (const agent of agents) { const s = await store(agent, null) await withLock(s.entry.name, async () => { const prior = s.computers.get(name) await s.repo.put(NSID.computer, name, { name, hostname: host || name, os: typeof osName === 'string' ? osName : 'unknown', capabilities: Array.isArray(capabilities) ? capabilities.map(String) : ['posix-shell'], createdAt: prior?.value.createdAt ?? t, lastSeenAt: t, }) await s.refreshComputers() }) } return { name, token, agents } } const RUNNER_METHODS = new Set(['poll', 'result', 'jobDone', 'report'].map((m) => `/xrpc/ai.languid.runner.${m}`)) /** Runner endpoints (poll, result, jobDone, report) authenticate with the runner's own token, not the owner's. */ async function runnerRequest(nsid: string, req: http.IncomingMessage, res: http.ServerResponse, send: (status: number, body: unknown) => void, ip: string) { const token = (req.headers.authorization ?? '').replace(/^Bearer /, '') const key = token ? hub.authenticate(token) : undefined if (!key) { if (token) limit('authFailures', ip) return send(401, { error: 'AuthRequired', message: 'unknown runner token' }) } const retry = limit('runner', key) if (retry) return tooMany(res, retry) let input: any try { input = await readJson(req, MAX_BODY.runner) } catch (e: any) { return send(e.status ?? 400, { error: e.error ?? 'InvalidRequest', message: e.message }) } if (nsid === 'ai.languid.runner.poll') { const closed = new AbortController() res.on('close', () => closed.abort()) const wasOnline = hub.online(key) const running = Array.isArray(input.running) ? input.running.map(String) : [] const pending = hub.poll(key, String(input.session ?? ''), running, closed.signal) if (!wasOnline) log(`computer ${hub.label(key)} connected`) return send(200, await pending) } if (nsid === 'ai.languid.runner.jobDone') return send(...(await jobDone(key, input))) if (nsid === 'ai.languid.runner.report') { // A computer's runner, tray or setup hit an error: into the ops log (errors and metadata only). const state = readState() const ev = computerEvent(input, runnerOwner(state, key), state.runners[key]?.name) if (!ev) return send(400, { error: 'InvalidRequest', message: 'a report needs source and message' }) ops.event(ev) return send(200, {}) } const ok = hub.result(key, String(input.id ?? ''), input) return ok ? send(200, {}) : send(404, { error: 'JobNotFound', message: 'no such job (it may have timed out)' }) } /** A runner's background job ended. Only the agent whose call started it, on that computer, takes the report. */ async function jobDone(key: string, report: any): Promise<[number, unknown]> { const notFound: [number, unknown] = [404, { error: 'JobNotFound', message: 'no such background job' }] const agent = report?.origin?.agent if (typeof report?.id !== 'string' || typeof agent !== 'string') return notFound const state = readState() const computer = state.runners[key]?.name if (!computer || !Object.hasOwn(state.agents, agent) || (state.agents[agent].owner ?? installOwner(state)) !== runnerOwner(state, key)) return notFound const s = await store(agent, null) const ok = await s.finishJob(computer, report) if (ok) log(`job ${report.id} on ${computer} ${report.status}`) return ok ? [200, {}] : notFound } /** * One model call for an agent, streamed as NDJSON pi-ai events. The service compiles the context from * the agent's records and calls the model named in its config record, with the service's credentials: * clients (pi) never call a model provider themselves and need no model keys. * Text and thinking deltas omit `partial` (clients apply the delta to the previous partial), so a long * response doesn't resend the whole message for every token. */ async function streamModel(req: http.IncomingMessage, res: http.ServerResponse, send: (status: number, body: unknown) => void, caller: Caller) { let input: any try { input = await readJson(req, MAX_BODY.owner) } catch (e: any) { return send(e.status ?? 400, { error: e.error ?? 'InvalidRequest', message: e.message }) } const abort = new AbortController() res.on('close', () => abort.abort()) let s: Harness let model let messages let stream: ReturnType try { s = await store(input.agent, caller.did) await refreshRunners(s) const thread = requireThread(s, input.thread) busyThread(s, thread) try { model = s.model() } catch (e: any) { throw new XrpcHttpError(400, 'ModelUnavailable', e.message) } const auth = await s.models.getAuth(model.provider as any).catch(() => undefined) if (!auth) throw new XrpcHttpError(400, 'NoCredentials', `no credentials for ${model.provider} on the service host`) // Near the context window: summarize older messages first. A failed compaction doesn't block the call. try { const c = await s.compactIfNeeded(thread, abort.signal) if (c?.compacted) log(`compacted ${s.entry.name} thread ${thread}: ~${c.tokensBefore} -> ~${c.tokensAfter} tokens`) } catch (e: any) { log(`compaction of ${s.entry.name} thread ${thread} failed: ${e?.message ?? e}`) } messages = s.compileTranscript(thread, undefined, sessionOf(s, input)) stream = s.models.streamSimple(model, { messages } as any, { signal: abort.signal, reasoning: typeof input.reasoning === 'string' ? input.reasoning : undefined, identity: input.identity, } as any) } catch (e: any) { if (e instanceof BudgetError) return send(402, { error: 'BudgetExhausted', message: e.message }) if (e instanceof XrpcHttpError) return send(e.status, { error: e.error, message: e.message }) log(`ai.languid.model.stream failed: ${e?.stack ?? e}`) reportError(input?.agent, 'service', e, { method: 'ai.languid.model.stream' }) return send(500, { error: 'InternalServerError', message: e?.message ?? String(e) }) } const started = Date.now() res.writeHead(200, { 'content-type': 'application/x-ndjson', 'cache-control': 'no-store' }) let stop = '' for await (const event of stream) { if (res.destroyed) break const out: any = { ...event } if (event.type === 'text_delta' || event.type === 'thinking_delta') delete out.partial if (event.type === 'done' || event.type === 'error') stop = event.type === 'done' ? event.reason : `${event.reason}: ${event.error.errorMessage ?? ''}` if (event.type === 'error' && event.reason === 'error') { const err = Object.assign(new Error(event.error.errorMessage ?? 'the model call failed'), { name: 'ModelError', stack: undefined }) reportError(s.entry.name, 'model', err, { method: 'ai.languid.model.stream', provider: model.provider, model: model.id }) } res.write(JSON.stringify(out) + '\n') } res.end() log(`ai.languid.model.stream ${s.entry.name} ${model.provider}/${model.id} ${stop} ${Date.now() - started}ms`) } // Sign-in with an atproto account. Needs an address an authorization server accepts as a client. const where = opts.url ? oauthOrigin(opts.url) : { reason: 'the service has no URL configured' } /** Agents' sites: on their own host (LANGUID_SITES_HOST), and at /_sites/ when the service is only on loopback. */ const sitesHost = sitesHostname() const sitesOnPath = !opts.url || ('loopback' in where && where.loopback) const siteHost = new SiteHost({ pds: () => readState().pds, lookup: (who) => { const id = who.toLowerCase() for (const e of Object.values(readState().agents)) if (e.agent.did === who || e.agent.handle.toLowerCase() === id) return e.agent.did return undefined }, log, }) /** did:web of this service, which PDSes forward app calls to; none without a public HTTPS address. */ const appDid = 'origin' in where && !where.loopback ? serviceDid(where.origin) : undefined const signingKeys = new Map() const serviceAuth = appDid ? new ServiceAuth({ did: appDid, resolveKey: async (did, fresh) => { const hit = signingKeys.get(did) if (hit && !fresh && Date.now() - hit.at < 10 * 60_000) return hit.key const key = await atprotoSigningKey(did, readState().pds) if (signingKeys.size >= 10_000) signingKeys.clear() signingKeys.set(did, { key, at: Date.now() }) return key }, }) : undefined let logins: Logins | undefined if ('origin' in where) { const state = readState() protocol = opts.oauth ?? atprotoProtocol({ origin: where.origin, loopback: where.loopback, dir: `${home()}/oauth`, resolver: state.pds, plc: process.env.LANGUID_PLC, allowHttp: state.pds.startsWith('http:'), }) logins = new Logins({ protocol, origin: where.origin, // Invite-only: the installation owner, invited owners, owners of existing agents, and allowedDids. allowed: (did) => { const st = readState() return did === installOwner(st) || !!st.owners?.[did] || Object.values(st.agents).some((a) => a.owner === did) || (st.allowedDids ?? []).includes(did) }, mint: (did, handle, kind) => { const token = `lgs_${crypto.randomBytes(32).toString('base64url')}` const st = readState() const now = Date.now() const live = Object.fromEntries(Object.entries(st.sessions ?? {}).filter(([, v]) => Date.parse(v.expiresAt) > now)) const expiresAt = new Date(now + SESSION_TTL_MS).toISOString() st.sessions = { ...live, [hashToken(token)]: { did, handle, createdAt: new Date(now).toISOString(), expiresAt, ...(kind === 'web' ? { kind } : {}) } } saveState(st) log(`sign-in${kind === 'web' ? ' (web)' : ''}: ${handle} (${did})`) return { token, expiresAt } }, file: `${home()}/oauth/logins.json`, onEvent: (e) => { const did = e.did ?? ownerByHandle(e.handle) if (e.type === 'started') funnelStep('sign-in-started', did, { detail: e.kind }) else if (e.type === 'signed-in') funnelStep('signed-in', did, { detail: e.kind }) else { log(`sign-in failed${e.handle ? ` for ${e.handle}` : ''}: ${e.reason}`) funnelStep('sign-in-failed', did, { detail: e.reason }) } }, // Adding an account: the one signed in stays, behind it. A second sign-in as the same account replaces it. keep: (cookieHeader, did) => { const was = [webSession(cookieHeader), ...otherAccounts(cookieHeader)].filter((s) => !!s) const stale = was.filter((s) => s.did === did) if (stale.length) { const st = readState() for (const s of stale) delete st.sessions?.[s.key] saveState(st) } const kept = was.filter((s) => s.did !== did).slice(0, MAX_OTHER_ACCOUNTS) if (!kept.length && cookie(cookieHeader, ACCOUNTS_COOKIE) === undefined) return [] return [logins!.accountsCookie(kept.map((s) => s.token), SESSION_TTL_MS / 1000)] }, }) } else { log(`sign-in with an atproto account is off: ${where.reason}`) } const signIn: { origin: string } | { reason: string } = logins && 'origin' in where ? { origin: where.origin } : { reason: 'reason' in where ? where.reason : 'it is off' } const isHttps = ('origin' in signIn ? signIn.origin : opts.url ?? '').startsWith('https:') type SandboxBilling = NonNullable & { startedAt?: string } // Cloud sandboxes (sandboxes.ts): offered when Modal is configured and the service has a public URL. // The computer is registered when its owner subscribes, and woken by the hub when a call is sent to it. const sandboxes: SandboxProvider | undefined = opts.sandboxes ?? (modalConfigured() && 'origin' in signIn ? new ModalSandboxes(signIn.origin, process.env.FLY_IMAGE_REF ?? 'dev', log) : undefined) /** Owners whose sandbox is starting right now, so two calls start one sandbox. */ const sandboxStarting = new Map>() const sandboxRunning = new Set() const thisMonth = () => new Date().toISOString().slice(0, 7) const addSandboxSeconds = (sb: SandboxBilling, seconds: number) => { const month = thisMonth() if (sb.usedMonth !== month) { sb.usedMonth = month sb.usedSeconds = 0 } sb.usedSeconds = (sb.usedSeconds ?? 0) + Math.max(0, Math.round(seconds)) } const sandboxHours = (b?: BillingEntry) => { const sb = b?.sandbox as SandboxBilling | undefined if (!sb) return 0 const base = sb.usedMonth === thisMonth() ? (sb.usedSeconds ?? 0) : 0 const live = sb.startedAt ? Math.max(0, (Date.now() - Date.parse(sb.startedAt)) / 1000) : 0 return (base + (Number.isFinite(live) ? live : 0)) / 3600 } /** Reconcile any sandboxes left marked running from before a service restart so billing isn't lost. */ const reconcileSandboxUsage = () => { if (!sandboxes) return const state = readState() let changed = false const nowMs = Date.now() for (const [did, b] of Object.entries(state.billing ?? {})) { const sb = b.sandbox as SandboxBilling | undefined if (!sb?.startedAt || sandboxRunning.has(did)) continue const startedMs = Date.parse(sb.startedAt) if (Number.isFinite(startedMs) && startedMs <= nowMs) { addSandboxSeconds(sb, (nowMs - startedMs) / 1000) } delete sb.startedAt changed = true } if (changed) saveState(state) } const sandboxOwnerOf = (state: State, key: string) => { const r = state.runners[key] return r?.name === SANDBOX_COMPUTER ? (r.owner ?? installOwner(state)) : undefined } const sandboxUsable = (state: State, did: string) => isActive(state.billing?.[did]?.sandbox) && sandboxHours(state.billing?.[did]) < INCLUDED_HOURS if (sandboxes) { if (sandboxes instanceof ModalSandboxes) sandboxes.prewarm().catch(() => {}) hub.waker = { can: (key) => { const state = readState() const did = sandboxOwnerOf(state, key) return !!did && sandboxUsable(state, did) }, wake: (key) => { const state = readState() const did = sandboxOwnerOf(state, key) if (!did) throw new Error('not a cloud sandbox') const b = state.billing?.[did] if (!isActive(b?.sandbox)) throw new Error('no cloud sandbox subscription; see /billing') if (sandboxHours(b) >= INCLUDED_HOURS) throw new Error(`this month's ${INCLUDED_HOURS} sandbox hours are used up`) const already = sandboxStarting.get(did) if (already) return already const starting = (async () => { // A fresh runner token for each start: the sandbox gets it in its environment, and nothing keeps it. const token = crypto.randomBytes(32).toString('base64url') const fresh = readState() if (!fresh.runners[key]) throw new Error('the computer was removed') fresh.runners[key].tokenHash = hashToken(token) saveState(fresh) hub.forget(key) const run = await sandboxes.start({ did, serviceUrl: (signIn as { origin: string }).origin, token, volume: isActive(fresh.billing?.[did]?.volume) }) sandboxRunning.add(did) const startedAtIso = new Date(run.startedAt).toISOString() const startedState = readState() const startedSb = startedState.billing?.[did]?.sandbox as SandboxBilling | undefined if (startedSb) { startedSb.startedAt = startedAtIso saveState(startedState) } log(`cloud sandbox for ${did} started`) void run.done.then(() => { sandboxRunning.delete(did) const after = readState() const sb = after.billing?.[did]?.sandbox as SandboxBilling | undefined if (sb) { addSandboxSeconds(sb, (Date.now() - run.startedAt) / 1000) if (sb.startedAt === startedAtIso) delete sb.startedAt saveState(after) } log(`cloud sandbox for ${did} stopped`) }) })().finally(() => sandboxStarting.delete(did)) sandboxStarting.set(did, starting) return starting }, } } /** Bring an owner's sandbox in line with what they pay for: register its computer, or stop it. */ async function afterBilling(did: string) { const state = readState() const b = state.billing?.[did] if (isActive(b?.sandbox)) { if (runnerKey(state, did, SANDBOX_COMPUTER)) return const code = crypto.randomBytes(18).toString('base64url') state.invites = { ...state.invites, [code]: { expiresAt: new Date(Date.now() + 600_000).toISOString(), agents: [], owner: did } } saveState(state) await registerComputer({ code, name: SANDBOX_COMPUTER, hostname: 'Languid cloud', os: 'Linux (cloud sandbox)', capabilities: ['posix-shell', 'os:linux'] }) log(`cloud sandbox computer registered for ${did}`) } else if (sandboxes && sandboxRunning.has(did)) { await sandboxes.stop(did) } } /** Volumes whose subscription ended over 14 days ago are deleted. */ const VOLUME_KEEP_MS = 14 * 86_400_000 const volumeSweep = sandboxes ? setInterval(async () => { const state = readState() for (const [did, b] of Object.entries(state.billing ?? {})) { if (!b.volume?.endedAt || b.volumeDeletedAt || isActive(b.volume) || Date.now() - Date.parse(b.volume.endedAt) < VOLUME_KEEP_MS) continue try { await sandboxes.deleteVolume(did) const after = readState() after.billing![did].volumeDeletedAt = new Date().toISOString() saveState(after) log(`deleted the sandbox volume of ${did}`) } catch (e: any) { log(`deleting the sandbox volume of ${did} failed: ${e?.message ?? e}`) } } }, 3600_000) : undefined volumeSweep?.unref() /** What each agent is doing, for the shell beside it: waiting on its owner, running, or how its last run ended in the past hour. */ function agentStates(names: string[]): Record { const out: Record = {} const hourAgo = Date.now() - 3600_000 for (const name of names) { let state: AgentState = 'idle' let thread: string | undefined for (const [key, id] of activeRuns) { if (!key.startsWith(name + '/')) continue if ((runControls.get(id)?.approvals.size ?? 0) > 0) { state = 'waiting' thread = key.slice(name.length + 1) break } state = 'running' thread ??= key.slice(name.length + 1) } if (state === 'idle') { let last: Run | undefined for (const r of runs.values()) if (r.agent === name && (!last || r.startedAt > last.startedAt)) last = r const ended = last?.finishedAt ? Date.parse(last.finishedAt) : NaN if (last && ended > hourAgo) state = last.status === 'error' && !last.stopped ? 'failed' : last.status === 'done' ? 'done' : 'idle' } out[name] = { state, ...(thread ? { thread } : {}) } } return out } function writePage(req: http.IncomingMessage, res: http.ServerResponse, p: Page) { let body = p.body // An app page's sidebar lists the signed-in owner's agents; filled here so each page needn't look them up. if (body.includes(APP_NAV)) { const who = webSession(req.headers.cookie) // A page's own part of the sidebar (the chat's threads) goes under its agent there. const start = body.indexOf(NAV_NEST[0]), end = body.indexOf(NAV_NEST[1]) const nest = start >= 0 && end > start ? body.slice(start + NAV_NEST[0].length, end) : undefined if (nest !== undefined) body = body.slice(0, start) + body.slice(end + NAV_NEST[1].length) let nav = '' if (who) { const state = readState() const names = agentsOf(state, who.did) const states = agentStates(names) const agents = names.map((name) => ({ name, displayName: displayNameOf(state.agents[name]), handle: state.agents[name].agent.handle, state: states[name].state })) const admin = !!installOwner(state) && who.did === installOwner(state) const o = state.owners?.[who.did] const creditUsd = o?.budgetUsd === undefined ? undefined : Math.max(0, o.budgetUsd - (o.spentUsd ?? 0)) const accounts = otherAccounts(req.headers.cookie).map((s) => ({ did: s.did, handle: s.handle })) // The Inbox's count: calls of the owner's agents waiting on their yes right now. let inbox = 0 for (const [key, id] of activeRuns) if (names.includes(key.slice(0, key.indexOf('/')))) inbox += runControls.get(id)?.approvals.size ?? 0 // And Machine runs parked on a question, which wait in records with no run. for (const name of names) inbox += loadedStores.get(name)?.approvalRequests().filter((r) => r.value.machine && r.value.n === undefined && loadedStores.get(name)!.requestOpen(r)).length ?? 0 // And messages from Languid they haven't dismissed (only once the ops account exists). if (state.ops) inbox += opsMessages.count(who.did, state.owners?.[who.did]?.createdAt) const path = new URL(req.url ?? '/', 'http://x').pathname // What's new shows a dot until the owner opens it; what they've seen is a cookie in their browser. const news = path !== NEWS_PATH && newsUnread(cookie(req.headers.cookie, NEWS_COOKIE)) nav = appNav({ handle: who.handle, agents, path, admin, nest, creditUsd, accounts, inbox, news }) } body = body.replace(APP_NAV, () => nav) } const hdrs = isHttps && !('strict-transport-security' in p.headers) ? { ...p.headers, 'strict-transport-security': 'max-age=31536000; includeSubDomains' } : p.headers res.writeHead(p.status, hdrs) res.end(req.method === 'HEAD' ? undefined : body) } /** * Server-issued redirect notices, keyed by owner DID + path + kind + text. * Pages only display ?done=/?error=/?deleted= query params that were genuinely issued by this * server for this owner within the last minute, preventing attacker-crafted URLs from injecting * spoofed banners. */ const NOTICE_TTL_MS = 60_000 const MAX_NOTICES = 2_000 const issuedNotices = new Map() const noticeKey = (did: string, path: string, kind: 'done' | 'error' | 'deleted', text: string) => `${did}\0${path}\0${kind}\0${text}` function issueNotice(did: string, path: string, kind: 'done' | 'error' | 'deleted', text: string) { const now = Date.now() if (issuedNotices.size >= MAX_NOTICES) { for (const [k, exp] of issuedNotices) if (exp <= now) issuedNotices.delete(k) while (issuedNotices.size >= MAX_NOTICES) issuedNotices.delete(issuedNotices.keys().next().value!) } const key = noticeKey(did, path, kind, text) issuedNotices.delete(key) issuedNotices.set(key, now + NOTICE_TTL_MS) } function takeNotice(did: string, path: string, kind: 'done' | 'error' | 'deleted', text: string | null | undefined, consume = true): string | undefined { if (!text) return undefined const key = noticeKey(did, path, kind, text) const exp = issuedNotices.get(key) if (!exp || exp <= Date.now()) { if (exp) issuedNotices.delete(key) return undefined } if (consume) issuedNotices.delete(key) return text } /** The browser session a request's cookie stands for (web sign-in only), and its state key. */ function webSession(cookieHeader: string | undefined) { return browserSession(cookie(cookieHeader, SESSION_COOKIE)) } /** A live browser session for a token (web sign-in only), with its state key. */ function browserSession(token: string | undefined) { if (!token || !/^lgs_[A-Za-z0-9_-]{43}$/.test(token)) return undefined const key = hashToken(token) const session = readState().sessions?.[key] if (!session || session.kind !== 'web' || Date.parse(session.expiresAt) <= Date.now()) return undefined return { key, token, ...session } } /** * The browser's other signed-in accounts, besides the one its session cookie names: live ones only, * one per account. The cookie holds only session tokens, which are random and kept here as hashes, so * it says nothing to anyone who reads it but this service. */ function otherAccounts(cookieHeader: string | undefined) { const active = webSession(cookieHeader) const seen = new Set(active ? [active.did] : []) const out: NonNullable>[] = [] for (const token of (cookie(cookieHeader, ACCOUNTS_COOKIE) ?? '').split('.').slice(0, MAX_OTHER_ACCOUNTS * 2)) { const s = browserSession(token) if (!s || seen.has(s.did)) continue seen.add(s.did) out.push(s) } return out.slice(0, MAX_OTHER_ACCOUNTS) } /** The cookies that make `active` the browser's account, with `others` signed in behind it. */ function accountCookies(active: NonNullable> | undefined, others: { token: string }[]) { const maxAge = active ? Math.max(0, Math.floor((Date.parse(active.expiresAt) - Date.now()) / 1000)) : 0 return [logins!.sessionCookie(active?.token ?? '', maxAge), logins!.accountsCookie(others.map((s) => s.token), SESSION_TTL_MS / 1000)] } /** * A new agent's model when none was asked for: the owner's own default, else the first of Languid's * picks, else the guest default, else anything this owner can run; never one they can't. */ async function defaultModelFor(did: string, state: State): Promise<{ provider: string; model: string } | undefined> { const available = await (await ownerModels(did)).getAvailable() const callable = (m?: { provider: string; model: string }) => (m && available.some((a) => a.provider === m.provider && a.id === m.model) ? m : undefined) const ref = (v: string) => (v.indexOf('/') > 0 ? { provider: v.slice(0, v.indexOf('/')), model: v.slice(v.indexOf('/') + 1) } : undefined) return callable(state.owners?.[did]?.model) ?? houseModels().map((v) => callable(ref(v))).find(Boolean) ?? callable(guestDefaultModel()) ?? (available[0] ? { provider: available[0].provider, model: available[0].id } : undefined) } /** * An agent on a model its owner can't run (a key removed, a model gone from the catalog) moves to * the default a new agent would get, so no page ever offers a dead model. Returns the models it checked. */ async function keepRunnable(h: Harness, did: string) { const available = await (await ownerModels(did)).getAvailable() const runs = (provider: string, id: string) => available.some((m) => m.provider === provider && m.id === id) if (!runs(h.config.provider, h.config.model)) { const to = await defaultModelFor(did, readState()) if (to && runs(to.provider, to.model)) { log(`${h.entry.name}: ${h.config.provider}/${h.config.model} can't run for its owner; moved to ${to.provider}/${to.model}`) await withLock(h.entry.name, () => h.setModel(to.provider, to.model)) } } return available } /** * Each owner's ai.languid.owner.profile as last read from or written to their private owner space * (src/owner-space.ts): null when they have none. Derived from the PDS, which is the source of * truth; a restart reads it again. */ const ownerProfiles = new Map() /** Owners whose profile couldn't be read just now, and when to try again. */ const ownerProfileRetry = new Map() /** What the owner said they use Languid for: the profile, null when they haven't said, undefined when it can't be read now. */ async function ownerProfileOf(did: string): Promise { if (ownerProfiles.has(did)) return ownerProfiles.get(did) if ((ownerProfileRetry.get(did) ?? 0) > Date.now()) return undefined try { const owner = await ownerActor(did) const found = await readOwnerProfile(owner) ownerProfiles.set(did, found?.profile ?? null) // One still in their public repo moves into their space, if this sign-in has the permission. if (found?.where === 'public') { await moveOwnerProfile(owner, found).then( (moved) => log(`owner profile of ${did}: ${moved ? 'copied into their space' : "this sign-in can't write their space"}${DELETE_PUBLIC_PROFILES ? '; public copy deleted' : ''}`), (e) => log(`owner profile of ${did}: couldn't move it into their space: ${e?.message ?? e}`), ) } return found?.profile ?? null } catch (e: any) { log(`owner profile of ${did} unreadable: ${e?.message ?? e}`) ownerProfileRetry.set(did, Date.now() + 5 * 60_000) return undefined } } /** Save what the owner chose to ai.languid.owner.profile in their private owner space. Throws LoginError 403 when their sign-in lacks the permission. */ async function saveOwnerProfile(did: string, choice: { use: 'personal' | 'work'; work?: OwnerWork; workOther?: string }): Promise { const prev = await ownerProfileOf(did) const t = now() // Choosing Work again keeps the kind of work they already gave. const work = choice.use === 'work' ? (choice.work ?? (prev?.use === 'work' ? prev.work : undefined)) : undefined const workOther = work === 'other' ? (choice.work === 'other' ? choice.workOther : prev?.workOther) : undefined const record = { $type: NSID.ownerProfile, use: choice.use, ...(work ? { work } : {}), ...(workOther ? { workOther: workOther.trim().slice(0, 100) } : {}), createdAt: prev?.createdAt || t, updatedAt: t, } const owner = await ownerActor(did) try { await writeOwnerProfile(owner, record) if (DELETE_PUBLIC_PROFILES) await deletePublicProfile(owner).catch((e) => log(`owner profile of ${did}: public copy not deleted: ${e?.message ?? e}`)) } catch (e: any) { if (e instanceof XrpcError && (e.status === 401 || e.status === 403)) throw new LoginError(403, e.message) throw e } const saved = parseOwnerProfile(record)! ownerProfiles.set(did, saved) ownerProfileRetry.delete(did) log(`owner profile of ${did}: ${saved.use}${saved.work ? ` (${saved.work})` : ''}`) return saved } /** * Records that used to be public and aren't any more (FORMERLY_PUBLIC): delete each agent's * ai.languid.agent.profile, and move each owner's profile into their space (deleting the public * copy). Runs at start; repeating it finds nothing to do. */ async function privatizeFormerlyPublic() { const state = readState() for (const entry of Object.values(state.agents)) { try { const session = await agentSession(state, entry) const where = { repo: session.did, collection: NSID.agentProfile, rkey: 'self' } const had = await session.call('com.atproto.repo.getRecord', { params: where }).then(() => true, () => false) if (!had) continue await session.call('com.atproto.repo.deleteRecord', { body: where }) log(`deleted ${entry.name}'s public ${NSID.agentProfile}`) } catch (e: any) { log(`couldn't delete ${entry.name}'s public ${NSID.agentProfile}: ${e?.message ?? e}`) } } const owners = new Set([...(state.owner ? [state.owner.did] : []), ...Object.keys(state.owners ?? {}), ...Object.values(state.agents).map((a) => a.owner)]) for (const did of owners) { if (!did) continue ownerProfiles.delete(did) ownerProfileRetry.delete(did) await ownerProfileOf(did).catch(() => undefined) } } /** Home's and About you's form: save the choice, or (Home only) skip it, then go back. */ async function profileRequest(url: URL, req: http.IncomingMessage, res: http.ServerResponse, ip: string) { const who = logins ? webSession(req.headers.cookie) : undefined const retry = pageLimit(ip, who) if (retry) return tooMany(res, retry) if (!who || !logins) { res.writeHead(303, { location: '/', 'cache-control': 'no-store' }) return res.end() } const step = url.searchParams.get('about') === 'work' ? 'work' : 'use' if (req.method === 'GET' || req.method === 'HEAD') { const current = (await ownerProfileOf(who.did)) ?? undefined const saved = url.searchParams.has('saved') return writePage(req, res, aboutYouPage({ card: aboutYouCard({ step, next: '/profile', current }), current, ...(saved ? { notice: 'Saved.' } : {}) })) } if (req.method !== 'POST') return writePage(req, res, simplePage(405, 'Method not allowed', '

Use the form on this page.

')) if (!logins.sameOrigin(req.headers.origin)) return writePage(req, res, simplePage(403, 'Not from here', "

This request didn't come from this service's own page.

")) const form = new URLSearchParams(await readText(req, MAX_BODY.register)) const next = form.get('next') === '/profile' ? '/profile' : '/' const use = form.get('use') if (use === 'skip') { const secure = 'origin' in signIn && signIn.origin.startsWith('https:') funnelStep('onboarded', who.did, { detail: 'skipped' }) res.writeHead(303, { location: next === '/' ? await firstChat(who) : '/', 'set-cookie': `${ABOUT_SKIP_COOKIE}=1; Path=/; Max-Age=${365 * 86400}; HttpOnly; SameSite=Lax${secure ? '; Secure' : ''}`, 'cache-control': 'no-store' }) return res.end() } if (use !== 'personal' && use !== 'work') return writePage(req, res, simplePage(400, 'Pick one', '

Pick personal or work. Back

')) const workRaw = form.get('work') ?? undefined const work = workRaw && Object.hasOwn(OWNER_WORK, workRaw) ? (workRaw as OwnerWork) : undefined const workOther = work === 'other' ? (form.get('workOther') ?? '').trim().slice(0, 100) : undefined const back = (card: string) => (next === '/profile' ? writePage(req, res, aboutYouPage({ card, current: ownerProfiles.get(who.did) ?? undefined })) : writePage(req, res, simplePage(400, 'About you', card))) if (work === 'other' && !workOther) return back(aboutYouCard({ step: 'work', next, current: ownerProfiles.get(who.did) ?? undefined, error: 'Say what kind of work, or pick one of the others.' })) try { const saved = await saveOwnerProfile(who.did, { use, ...(work ? { work } : {}), ...(workOther ? { workOther } : {}) }) // Work without a kind yet: on to the second tap. const more = saved.use === 'work' && !work if (!more) funnelStep('onboarded', who.did, { detail: saved.use === 'work' ? `work: ${saved.work ?? ''}` : saved.use }) const location = more ? `${next}?about=work#about` : next === '/profile' ? '/profile?saved' : await firstChat(who) res.writeHead(303, { location, 'cache-control': 'no-store' }) return res.end() } catch (e: any) { const current = ownerProfiles.get(who.did) ?? undefined if (e instanceof LoginError) return back(aboutYouCard({ step: use === 'work' && work ? 'work' : 'use', next, current, signInAgain: who.handle })) log(`owner profile save for ${who.did} failed: ${e?.message ?? e}`) return back(aboutYouCard({ step: 'use', next, current, error: "Your PDS didn't save that. Try again in a minute." })) } } /** * Where onboarding goes next: an owner with no agent yet gets one, named after them, and lands in * its chat, so the first thing they see is their agent rather than an empty Home. Anyone with an * agent, or when making one fails, goes Home (with the reason, by the New agent form). */ async function firstChat(who: { did: string; handle: string }): Promise { if (agentsOf(readState(), who.did).length) return '/' const label = who.handle.split('.')[0].toLowerCase().replace(/[^a-z0-9-]/g, '').replace(/-+/g, '-') const base = (/^[a-z]/.test(label) ? label : `a${label}`).slice(0, 24).replace(/-+$/, '') let last: unknown for (const name of [`${base}-agent`, ...[2, 3, 4].map((n) => `${base}-agent-${n}`)]) { const state = readState() if (Object.hasOwn(state.agents, name) || (state.deletedAgents && Object.hasOwn(state.deletedAgents, name))) continue try { await makeAgent(who, name) return `/agents/${encodeURIComponent(name)}` } catch (e: any) { last = e // A name someone else's account holds: try the next one. Anything else won't be fixed by another name. if (!(e instanceof XrpcHttpError && (e.error === 'AgentNameTaken' || /already registered|already has a space/.test(e.message)))) break } } const message = String((last as any)?.message ?? last ?? 'Make your first agent below.') log(`first agent for ${who.handle} wasn't made: ${message}`) const shown = message.slice(0, 200) issueNotice(who.did, '/', 'error', shown) return `/?error=${encodeURIComponent(shown)}#new-agent` } /** Make an agent for an owner: its account on this PDS, its space under the owner's own DID. The CLI and the Home page both come here. */ function makeAgent(caller: { did: string; handle?: string }, name: string, model?: { provider: string; model: string }, makeOpts?: { skipRateLimit?: boolean }) { if (!makeOpts?.skipRateLimit) { const retry = limit('createAgent', caller.did) if (retry) { const err = new XrpcHttpError(429, 'RateLimitExceeded', `too many requests; try again in ${retry}s`) as XrpcHttpError & { retryAfter?: number } err.retryAfter = retry return Promise.reject(err) } } // One at a time per owner (their agent count) and per name; different owners' run side by side. // createAgent saves the new entry with updateState, so concurrent ones don't overwrite each other. return withLock(`agent.create:${caller.did}`, () => withLock(name, async () => { const state = readState() const own = caller.did === installOwner(state) const deletedCount = Object.values(state.deletedAgents ?? {}).filter((a) => a.owner === caller.did).length if (!own && agentsOf(state, caller.did).length + deletedCount >= MAX_AGENTS_PER_OWNER) { throw new XrpcHttpError(403, 'AgentLimit', `you have ${MAX_AGENTS_PER_OWNER} agents, the most this service allows`) } if (Object.hasOwn(state.agents, name) || (state.deletedAgents && Object.hasOwn(state.deletedAgents, name))) { throw new XrpcHttpError(409, 'AgentNameTaken', `the agent name "${name}" is taken on this service`) } let owner: { did: string; handle: string; call: PdsCall } | undefined if (!own) { if (!protocol) throw new XrpcHttpError(400, 'SignInOff', 'this service has sign-in off, so it cannot create a space as you') try { owner = { did: caller.did, handle: caller.handle ?? caller.did, call: await protocol.act(caller.did) } } catch (e: any) { if (e instanceof LoginError) throw new XrpcHttpError(401, 'OwnerSignInRequired', e.message) throw e } } let entry try { const profile = await ownerProfileOf(caller.did) // An owner's first agent walks them through setup (onboardingMemory), then unpins the block. const first = !own && 'origin' in signIn && agentsOf(state, caller.did).length === 0 const onboarding = first ? onboardingMemory({ origin: signIn.origin, install: installCommand(signIn.origin), sandboxes: !!sandboxes }) : undefined entry = await createAgent(state, name, { model: model ?? (await defaultModelFor(caller.did, state)), owner, ...(profile ? { ownerAbout: ownerProfileMemory(profile) } : {}), ...(onboarding ? { onboarding } : {}), }) } catch (e: any) { log(`agent.create ${name} for ${caller.did} failed: ${e?.message ?? e}`) throw new XrpcHttpError(400, 'AgentNotCreated', e?.message ?? String(e)) } log(`agent ${entry.name} (${entry.agent.did}) created for ${caller.did} in ${entry.space}`) funnelStep('first-agent', caller.did, { detail: entry.name }) return { name: entry.name, did: entry.agent.did, handle: entry.agent.handle, space: entry.space } })) } /** `/`: the landing page, or the signed-in page for a browser with a session. */ async function frontPage(req: http.IncomingMessage, res: http.ServerResponse) { // Sign-in is tied to one origin (the OAuth client and the Origin checks). A browser on another // name is sent there when that's safe: on loopback (localhost: to 127.0.0.1), and from // www.. Anywhere else (a tailnet address, or behind a proxy that rewrites Host, where // a redirect could loop) the page links to the origin instead of showing the form. if ('origin' in signIn && req.headers.host !== new URL(signIn.origin).host) { const originHost = new URL(signIn.origin).host const loopback = 'loopback' in where && where.loopback if (loopback || req.headers.host === `www.${originHost}`) { res.writeHead(loopback ? 302 : 301, { location: `${signIn.origin}/`, 'cache-control': 'no-store' }) return res.end() } return writePage(req, res, landingPage({ elsewhere: signIn.origin })) } const who = webSession(req.headers.cookie) // "Add another account" from the account menu: the sign-in form, while this one stays signed in. if (who && 'origin' in signIn && new URL(req.url ?? '/', 'http://x').searchParams.has('add')) return writePage(req, res, landingPage(signIn, { add: true })) if (who && 'origin' in signIn) { const state = readState() const o = state.owners?.[who.did] const budget = o?.budgetUsd !== undefined ? { budgetUsd: o.budgetUsd, spentUsd: o.spentUsd ?? 0 } : undefined const query = new URL(req.url ?? '/', 'http://x').searchParams const consume = req.method === 'GET' const deleted = takeNotice(who.did, '/', 'deleted', query.get('deleted')?.slice(0, 80), consume) const failed = takeNotice(who.did, '/', 'error', query.get('error')?.slice(0, 200), consume) const names = agentsOf(state, who.did) // Each agent's card: its model, its latest thread and when it last moved, and whether it has a computer. const cards = Object.fromEntries( await Promise.all( names.map(async (n) => { try { const h = await store(n, who.did) const t = h.threadList()[0] return [n, { model: h.config.model, computers: h.computers.size, ...(t ? { latest: { rkey: t.rkey, name: t.name, preview: t.preview, updatedAt: t.updatedAt } } : {}) }] as const } catch { return [n, {}] as const } }), ), ) const week = summarizeUsage(state, who.did, 7).total // The onboarding step: until they say what they use Languid for (or skip it), and whenever they come back to it. const asked = query.get('about') const profile = await ownerProfileOf(who.did) const skipped = cookie(req.headers.cookie, ABOUT_SKIP_COOKIE) !== undefined const step: 'use' | 'work' | undefined = asked === 'work' || asked === 'use' ? asked : skipped ? undefined : profile === null ? 'use' : profile?.use === 'work' && !profile.work ? 'work' : undefined return writePage(req, res, accountPage({ ...(step ? { about: aboutYouCard({ step, next: '/', current: profile ?? undefined }) } : {}), handle: who.handle, did: who.did, agents: names, origin: signIn.origin, budget, ...(deleted ? { notice: `Deleted ${deleted}. Its account can be restored for ${DELETE_GRACE_DAYS} days; ask the Languid maintainer.` } : {}), ...(failed ? { error: failed } : {}), handles: Object.fromEntries(names.map((n) => [n, state.agents[n].agent.handle])), displayNames: Object.fromEntries(names.map((n) => [n, displayNameOf(state.agents[n])])), states: agentStates(names), cards, week: { charged: week.charged, calls: week.calls }, })) } writePage(req, res, landingPage(signIn)) } /** Messages the chat page shows of a thread, at most. */ const CHAT_ITEMS_SHOWN = 200 /** A thread's messages as the chat page shows them, with each assistant turn's tool calls and results. */ function chatItems(h: Harness, thread: string, viewer: string): { items: ChatItem[]; hidden: number } { const all: ChatItem[] = [] for (const m of h.messages) { if (m.value.thread !== thread) continue const text = m.value.parts.flatMap((p) => (p.type === 'text' ? [p.text] : [])).join('\n').trim() if (m.value.role === 'user') { const from = m.value.author === viewer ? undefined : (h.handles.get(m.value.author) ?? m.value.author) const images = m.value.parts.flatMap((p) => { const id = p.type === 'image' ? AgentStore.imageId(p) : undefined return id ? [`/agents/${encodeURIComponent(h.entry.name)}/images/${id}`] : [] }) all.push({ role: 'user', text, at: m.value.createdAt, ...(from ? { from } : {}), ...(images.length ? { images } : {}) }) continue } const calls = h.calls .filter((c) => c.value.message === m.rkey) .sort((a, b) => a.value.index - b.value.index) .map((c) => { const r = h.results.find((r) => r.value.call === c.rkey) return { id: c.value.callId, tool: c.value.tool, arguments: c.value.argumentsJson, computer: r?.value.computer ?? c.value.assignedComputer, ...(r ? { output: r.value.output, isError: r.value.isError } : {}), } }) const error = m.value.stopReason === 'aborted' ? 'stopped' : m.value.stopReason === 'error' ? (m.value.errorMessage ?? 'the model call failed') : undefined all.push({ role: 'assistant', text, at: m.value.createdAt, calls, ...(text ? { rkey: m.rkey } : {}), ...(error ? { error } : {}) }) } const hidden = Math.max(0, all.length - CHAT_ITEMS_SHOWN) return { items: all.slice(hidden), hidden } } /** * The chat: `/agents/` (the page), and the calls its script makes, all authenticated by the * browser's session cookie, never a bearer token. Calls that change anything must come from this * service's own pages (the Origin header) and be JSON, which a cross-site form can't send; the * cookie is SameSite=Lax as well. Paths under /agents/: * POST send {text, thread?, computer?, approval?} * start a run (a new thread without one); approval is * "ask" (default) or "allow" * POST resume {thread, computer?, approval?} * pick up a thread a restart cut off (resumeRun) * POST threads/rename {thread, title} name a thread * GET runs/?since=n its events, as agent.getRun * POST runs//mode {approval} ask or allow for the rest of the run; allow answers yes to what waits * POST runs//approve {id, approved, always?} * answer an approval; always saves "always allow this * tool on this computer" for the agent * POST approvals/revoke {computer, tool} remove an always-allow rule * POST runs//stop stop it */ async function chatRequest(url: URL, req: http.IncomingMessage, res: http.ServerResponse, send: (status: number, body: unknown) => void, ip: string) { const parts = url.pathname.split('/').slice(2) let name: string try { name = decodeURIComponent(parts[0] ?? '') } catch { name = '' } const rest = parts.slice(1) const isPage = rest.length === 0 && (req.method === 'GET' || req.method === 'HEAD') const who = logins && 'origin' in signIn ? webSession(req.headers.cookie) : undefined // Without a session, limited per address; a signed-in browser's pages and calls (it polls a run // twice a second) per owner, like XRPC calls. if (!who) { const retry = limit('pages', ip) if (retry) return isPage ? writePage(req, res, withRetry(simplePage(429, 'Slow down', `

Too many requests from this address. Try again in ${retry} seconds.

`), retry)) : tooMany(res, retry) } if (!who) { if (isPage) { res.writeHead(303, { location: '/', 'cache-control': 'no-store' }) return res.end() } return send(401, { error: 'AuthRequired', message: 'sign in again: your browser session has ended' }) } const caller: Caller = { did: who.did, handle: who.handle, via: 'session' } const ownerRetry = limit('owner', caller.did) if (ownerRetry) return tooMany(res, ownerRetry) try { if (isPage) return writePage(req, res, await chatView(name, caller, url)) if (req.method === 'POST') { if (!logins!.sameOrigin(req.headers.origin)) throw new XrpcHttpError(403, 'Forbidden', "this request didn't come from this service's own page") if (!/^application\/json(;|$)/i.test(req.headers['content-type'] ?? '')) throw invalid('send JSON') } const own = () => { const known = readState().agents[name] if (!known || known.owner !== caller.did) throw new XrpcHttpError(404, 'AgentNotFound', `no agent named "${name}"`) } // An image sent in one of this agent's threads, by its blob CID (or an older image's sha256). if (req.method === 'GET' && rest.length === 2 && rest[0] === 'images') { own() const id = rest[1] const h = await store(name, caller.did) let part: Extract | undefined for (const m of h.messages) { part = m.value.parts.find((p): p is Extract => p.type === 'image' && AgentStore.imageId(p) === id) if (part) break } const data = part && (await h.imageData(part)) if (!part || !data) throw new XrpcHttpError(404, 'ImageNotFound', 'no such image') const bytes = Buffer.from(data, 'base64') res.writeHead(200, { 'content-type': IMAGE_TYPES.has(part.mimeType) ? part.mimeType : 'application/octet-stream', 'content-length': bytes.length, // The id is the content's hash, so the bytes never change; only this owner may see them. 'cache-control': 'private, max-age=31536000, immutable', 'x-content-type-options': 'nosniff', 'content-security-policy': "default-src 'none'; sandbox", }) return res.end(bytes) } // The mic: a recording in, its text out, for the owner to edit and send. Billed like a model call. if (req.method === 'POST' && rest.length === 1 && rest[0] === 'transcribe') { own() const modelRetry = limit('model', caller.did) if (modelRetry) return tooMany(res, modelRetry) if (!canTranscribe()) throw new XrpcHttpError(501, 'NotConfigured', 'voice messages are not set up on this service') const { audio, mimeType, seconds: heard } = await readJson(req, MAX_BODY.chat) const type = audioType(mimeType) if (!type) throw invalid('record WebM, Ogg, MP4, MP3 or WAV audio') if (typeof audio !== 'string' || !audio) throw invalid('audio is base64') const bytes = Buffer.from(audio, 'base64') if (bytes.length > MAX_AUDIO_BYTES) throw invalid(`a recording is at most ${MAX_AUDIO_BYTES / 1024 / 1024} MB`) const refusal = transcribeRefusal(readState(), caller.did) if (refusal) throw new XrpcHttpError(402, 'BudgetExhausted', refusal) const { text, seconds } = await transcribe(bytes, type).catch((e) => { log(`transcribe for ${caller.handle} failed: ${e?.message ?? e}`) throw new XrpcHttpError(502, 'TranscriptionFailed', "couldn't turn that recording into text; try again") }) const billed = Number.isFinite(seconds) ? seconds : Math.min(MAX_SECONDS, Math.max(1, Number(heard) || 1)) const state = readState() chargeTranscription(state, caller.did, name, Math.min(billed, MAX_SECONDS * 2)) saveState(state) return send(200, { text }) } if (req.method === 'POST' && rest.length === 1 && rest[0] === 'send') { own() const modelRetry = limit('model', caller.did) if (modelRetry) return tooMany(res, modelRetry) const { text, thread, computer, approval = 'ask', clientMessageId, images, model: pickedModel, reasoning: pickedReasoning } = await readJson(req, MAX_BODY.chat) // The page's permissions pill: ask before each change (the default), allow them all, as // `languid run --yes` does, or Auto (src/auto-approve.ts). if (approval !== 'ask' && approval !== 'allow' && approval !== 'auto') throw invalid('approval must be "ask", "allow" or "auto"') // The computer picked in the page: computer tools run there unless the agent names another. let model: { provider: string; id: string; reasoning?: string } | undefined if (pickedModel !== undefined && pickedModel !== '') { if (typeof pickedModel !== 'string' || pickedModel.indexOf('/') < 1) throw invalid('model is provider/id') const at = pickedModel.indexOf('/') const reasoning = reasoningOf(pickedReasoning) model = { provider: pickedModel.slice(0, at), id: pickedModel.slice(at + 1), ...(reasoning ? { reasoning } : {}) } const available = await (await ownerModels(caller.did)).getAvailable() if (!available.some((m) => m.provider === model!.provider && m.id === model!.id)) throw invalid(`this service can't call ${pickedModel}`) } const started = await startRun(caller, { agent: name, prompt: text, thread: thread || undefined, computer: computer || undefined, via: 'web', clientMessageId, model, ...(images === undefined ? {} : { images }) }, approval) log(`chat: ${who.handle} -> ${name} (run ${started.run})`) return send(200, started) } if (req.method === 'POST' && rest.length === 1 && rest[0] === 'resume') { own() const modelRetry = limit('model', caller.did) if (modelRetry) return tooMany(res, modelRetry) const { thread, computer, approval = 'ask' } = await readJson(req, MAX_BODY.chat) if (approval !== 'ask' && approval !== 'allow' && approval !== 'auto') throw invalid('approval must be "ask", "allow" or "auto"') const started = await resumeRun(caller, name, thread, approval, 'web', computer || undefined) log(`chat: ${who.handle} picks up ${name}/${started.thread} (run ${started.run})`) return send(200, started) } if (req.method === 'POST' && rest.length === 1 && rest[0] === 'model-favorite') { own() const { value, favorite } = await readJson(req, MAX_BODY.register) if (typeof value !== 'string' || !value.includes('/') || value.length > 512 || typeof favorite !== 'boolean') throw invalid('a model value and favorite boolean are required') if (favorite) { const available = await (await ownerModels(caller.did)).getAvailable() if (!available.some((m) => `${m.provider}/${m.id}` === value)) throw invalid(`this service can't call ${value}`) } // Re-read after awaiting the catalog: another browser may have saved a different favorite. const state = readState() const preferences = (state.preferences ??= {}) const prefs = (preferences[caller.did] ??= {}) const models = new Set(prefs.favoriteModels ?? []) if (favorite) models.add(value) else models.delete(value) if (models.size > 200) throw invalid('at most 200 favorite models') prefs.favoriteModels = [...models] saveState(state) return send(200, { favoriteModels: prefs.favoriteModels }) } if (req.method === 'POST' && rest.length === 1 && rest[0] === 'model') { // With a thread: that thread's model, saved on its thread record. Without: the agent's model, // saved to its config record (the default for threads that pick none; pi and languid run use it too). own() const { provider, model, thread, reasoning: level } = await readJson(req, MAX_BODY.register) if (typeof provider !== 'string' || typeof model !== 'string') throw invalid('provider and model are required') const reasoning = reasoningOf(level) const available = await (await ownerModels(caller.did)).getAvailable() if (!available.some((m) => m.provider === provider && m.id === model)) throw invalid(`this service can't call ${provider}/${model}`) const h = await store(name, caller.did) if (thread !== undefined && thread !== '') { const th = requireThread(h, thread) // A level not given keeps the thread's (or, picking its first model, the agent's). const keep = h.threads.find((t) => t.rkey === th)?.value.model?.reasoning ?? h.config.reasoning await withLock(h.entry.name, () => h.setThreadModel(th, { provider, id: model, ...((reasoning ?? keep) ? { reasoning: reasoning ?? keep } : {}) })) log(`chat: ${who.handle} set ${name}/${th}'s model to ${provider}/${model}`) } else { await withLock(h.entry.name, () => h.setModel(provider, model)) if (reasoning !== undefined) await withLock(h.entry.name, () => h.setReasoning(reasoning)) log(`chat: ${who.handle} set ${name}'s model to ${provider}/${model}`) } noteRecentModel(caller.did, `${provider}/${model}`) return send(200, { model: { provider, id: model } }) } if (req.method === 'POST' && rest.length === 2 && rest[0] === 'threads' && rest[1] === 'rename') { own() const { thread, title } = await readJson(req, MAX_BODY.register) if (typeof thread !== 'string') throw invalid('a thread is required') const clean = threadTitle(title) const h = await store(name, caller.did) const th = requireThread(h, thread) const rec = await withLock(h.entry.name, () => h.renameThread(th, clean)) log(`chat: ${who.handle} named ${name}'s thread ${th} "${rec.value.title}"`) return send(200, { thread: th, title: rec.value.title }) } if (req.method === 'POST' && rest.length === 2 && rest[0] === 'evals' && rest[1] === 'save') { // 👍/👎 under a reply: that moment, saved as a case in the owner's repo in the agent's space // (or the holdout space, which the agent can't read). own() const { thread, message, verdict, note, holdout } = await readJson(req, MAX_BODY.register * 4) if (verdict !== 'good' && verdict !== 'bad') throw invalid('verdict must be "good" or "bad"') if (note !== undefined && (typeof note !== 'string' || note.length > 2000)) throw invalid('the note is at most 2,000 characters') if (typeof message !== 'string') throw invalid('a message is required') const h = await store(name, caller.did) const th = requireThread(h, thread) const evals = await import('./evals.ts') const { SpaceRepo } = await import('./atproto/space.ts') const threadRec = h.threads.find((t) => t.rkey === th)! const blocks = new Map([...h.blocks.values()].map((b) => [b.rkey, b.cid])) let saved try { saved = evals.caseFromThread(threadRec, h.messages, blocks, { at: message, verdict, note: note || undefined, by: caller.did }) } catch (e: any) { throw invalid(e.message) } const owner = await ownerActor(caller.did) const space = holdout === true ? await evals.openHoldout(owner, h.entry) : h.entry.space await new SpaceRepo(owner, space).put(NSID.evalCase, saved.rkey, saved.value) log(`chat: ${who.handle} saved ${name}'s message ${message} as ${holdout === true ? 'a held-out ' : 'a '}case (${verdict})`) return send(200, { case: saved.rkey, holdout: holdout === true, changedBlocks: saved.changedBlocks }) } if (req.method === 'POST' && rest.length === 2 && rest[0] === 'approvals' && rest[1] === 'revoke') { own() const { computer, tool } = await readJson(req, MAX_BODY.register) if (typeof computer !== 'string' || typeof tool !== 'string') throw invalid('computer and tool are required') const h = await store(name, caller.did) await h.askAgain(computer, tool) log(`chat: ${who.handle} removed always-allow ${tool} on ${computer} for ${name}`) return send(200, { alwaysAllowed: h.alwaysAllowed() }) } if (rest[0] === 'runs' && rest.length >= 2) { own() const r = ownRun(rest[1], caller) if (r.agent !== name) throw new XrpcHttpError(404, 'RunNotFound', `no run ${rest[1]}`) if (req.method === 'GET' && rest.length === 2) { // The page draws calls that change a file as a diff, worked out here. const st = runStatus(r, url.searchParams.get('since')) const events = st.events.map((e) => { const change = e.type === 'tool_call' || e.type === 'approval' ? callChange(e.tool, e.arguments) : undefined return change ? { ...e, change } : e }) // The thread's name, once it has one, so a rename during the run (the agent's) shows at once. const thread = (await store(name, caller.did)).threads.find((t) => t.rkey === r.thread) return send(200, { ...st, events, ...(thread?.value.named ? { title: thread.value.title } : {}) }) } if (req.method === 'POST' && rest.length === 3 && rest[2] === 'approve') { const { id, approved, always } = await readJson(req, MAX_BODY.register) const { rule } = await answerCall(await store(name, caller.did), r, id, approved === true, always === true, { by: caller.did, via: 'web' }) if (rule) { log(`chat: ${who.handle} always allowed ${rule.command ? `\`${rule.command}\`` : rule.tool} on ${rule.computer} for ${name} (run ${r.id})`) return send(200, { rule }) } log(`chat: ${who.handle} ${approved === true ? 'approved' : 'denied'} ${id} in run ${r.id}`) return send(200, {}) } // "Allow all" from the chat, mid-run: the rest of this run's calls go without asking, and the // ones waiting now are answered yes (sharing excepted: that's only ever its own yes). if (req.method === 'POST' && rest.length === 3 && rest[2] === 'mode') { const { approval } = await readJson(req, MAX_BODY.register) if (approval !== 'ask' && approval !== 'allow' && approval !== 'auto') throw invalid('approval is ask, allow or auto') const control = runControls.get(r.id) control?.setMode?.(approval) let allowed = 0 if (approval === 'allow' && control) { const h = await store(name, caller.did) for (const id of [...control.approvals.keys()]) { const asked = r.events.findLast((e) => e.type === 'approval' && e.id === id) as Extract | undefined if (asked?.tool === 'feedback_share') continue try { await answerCall(h, r, id, true, false, { by: caller.did, via: 'web' }) allowed++ } catch (e: any) { log(`chat: allow all couldn't answer ${id} in run ${r.id}: ${e?.message ?? e}`) } } } log(`chat: ${who.handle} set run ${r.id} to ${approval}${allowed ? ` (allowed ${allowed} waiting)` : ''}`) return send(200, { allowed }) } if (req.method === 'POST' && rest.length === 3 && rest[2] === 'stop') { runControls.get(r.id)?.stop() return send(200, {}) } } return send(404, { error: 'NotFound', message: `${req.method} ${url.pathname}` }) } catch (e: any) { if (e instanceof XrpcHttpError) { if (isPage) return writePage(req, res, e.status === 404 ? notFoundPage(url.pathname) : simplePage(e.status, 'Chat unavailable', `

${esc(sentence(e.message))}

`)) return send(e.status, { error: e.error, message: e.message }) } log(`${url.pathname} failed: ${e?.stack ?? e}`) if (isPage) return writePage(req, res, simplePage(500, 'Chat unavailable', `

Something broke on the service. Try again shortly.

`)) return send(500, { error: 'InternalServerError', message: e?.message ?? String(e) }) } } /** * `/computers`: the owner's computers, which agents each works for, and adding or removing one. * Plain forms from this service's own page (the Origin header), each answered with a redirect * back, except a new join code, which is shown once on the page the POST returns. */ async function computersRequest(url: URL, req: http.IncomingMessage, res: http.ServerResponse, ip: string) { const who = logins ? webSession(req.headers.cookie) : undefined const retry = pageLimit(ip, who) if (retry) return tooMany(res, retry) if (!who) { res.writeHead(303, { location: '/', 'cache-control': 'no-store' }) return res.end() } const back = (notice?: { text: string; error?: boolean }) => { if (notice) issueNotice(who.did, '/computers', notice.error ? 'error' : 'done', notice.text) const q = notice ? `?${new URLSearchParams(notice.error ? { error: notice.text } : { done: notice.text })}` : '' res.writeHead(303, { location: `/computers${q}`, 'cache-control': 'no-store' }) res.end() } let invite: { code: string; expiresAt: string; url: string; agents: string[] } | undefined if (req.method === 'POST') { if (!logins!.sameOrigin(req.headers.origin)) return writePage(req, res, simplePage(403, 'Not from here', "

This request didn't come from this service's own page.

")) if (!/^application\/x-www-form-urlencoded(;|$)/i.test(req.headers['content-type'] ?? '')) return writePage(req, res, simplePage(400, 'Bad request', '

Send the form.

')) const form = new URLSearchParams(await readText(req, MAX_BODY.register)) const action = form.get('action') const name = form.get('name') ?? '' try { if (action === 'assign') { const wanted = new Set(form.getAll('agent')) const mine = agentsOf(readState(), who.did) const add = mine.filter((a) => wanted.has(a)) const drop = mine.filter((a) => !wanted.has(a)) const added = add.length ? (await attachComputer(who.did, name, add)).attachedTo : 0 const dropped = drop.length ? (await detachComputer(who.did, name, drop)).detachedFrom : 0 log(`${who.handle} assigned ${name} to ${add.join(', ') || 'no agents'}`) return back({ text: added || dropped ? `Saved: ${name} works for ${add.join(', ') || 'no agents'}.` : `No change to ${name}.` }) } if (action === 'remove') { const r = await removeComputer(who.did, name) log(`${who.handle} removed computer ${name}`) return back({ text: r.hadRunner || r.removedFrom ? `Removed ${name}.` : `There was no computer named ${name}.` }) } if (action === 'invite') { invite = { ...inviteComputer(who.did, [], 15), url: opts.url ?? (signIn as { origin: string }).origin } log(`${who.handle} made a computer join code from the web`) } else return back({ text: 'Unknown action.', error: true }) } catch (e: any) { if (!(e instanceof XrpcHttpError)) log(`/computers ${action} failed: ${e?.stack ?? e}`) return back({ text: e instanceof XrpcHttpError ? sentence(e.message) : 'That failed. Try again.', error: true }) } } else if (req.method !== 'GET' && req.method !== 'HEAD') return writePage(req, res, simplePage(405, 'Method not allowed', '

Use the forms on the page.

')) const state = readState() const agents = agentsOf(state, who.did) const rows = new Map() for (const [key, r] of Object.entries(state.runners)) { if (runnerOwner(state, key) !== who.did) continue rows.set(r.name, { name: r.name, online: hub.online(key), lastSeenAt: hub.lastSeen(key), registeredAt: r.registeredAt, registered: true, ...(r.tokenHash ? {} : { legacy: true }), agents: [], }) } for (const agent of agents) { const s = await store(agent, who.did) await s.refreshComputers() for (const m of s.computers.keys()) { const row = rows.get(m) ?? { name: m, online: false, registered: false, agents: [] } row.agents.push(agent) rows.set(m, row) } } const consume = req.method === 'GET' const done = takeNotice(who.did, '/computers', 'done', url.searchParams.get('done'), consume) const error = takeNotice(who.did, '/computers', 'error', url.searchParams.get('error'), consume) const p = computersPage({ computers: [...rows.values()].sort((a, b) => Number(b.online) - Number(a.online) || a.name.localeCompare(b.name)), agents, handles: Object.fromEntries(agents.map((n) => [n, state.agents[n].agent.handle])), ...(invite ? { invite } : {}), ...(error ? { notice: { text: error, error: true } } : done ? { notice: { text: done } } : {}), ...(sandboxes ? (() => { const cloudKey = runnerKey(state, who.did, SANDBOX_COMPUTER) const sbx = state.billing?.[who.did]?.sandbox const st = sandboxStarting.has(who.did) ? ('starting' as const) : cloudKey && hub.online(cloudKey) ? ('running' as const) : ('asleep' as const) return { cloud: { active: isActive(sbx), state: st, usd: PLANS.sandbox.usd, hours: INCLUDED_HOURS, used: sandboxHours(state.billing?.[who.did]) } } })() : {}), }) return writePage(req, res, p) } /** * Discord bots (see discord/bots.ts). A message from Discord is a run, as a web chat message is, * sent as the owner (the bot answers only them) with via "discord", in the channel's own thread. */ const discord = new DiscordBots({ load: readState, update: (change) => { const s = readState() change(s) saveState(s) }, send: async (bot, input, beforeRun) => { const caller: Caller = { did: bot.owner, via: 'session' } const retry = limit('model', bot.owner) if (retry) throw new Error(`Too many messages; try again in ${retry}s.`) const h = await store(bot.agent, bot.owner) let thread = input.thread && h.threads.some((t) => t.rkey === input.thread) ? input.thread : undefined thread ??= await withLock(bot.agent, async () => (await h.newThread(input.title)).rkey) beforeRun?.(thread) const started = await startRun(caller, { agent: bot.agent, prompt: input.text, thread, via: 'discord', clientMessageId: input.clientMessageId, images: input.images }, 'ask') log(`discord: ${bot.name} -> ${bot.agent} (run ${started.run || 'none'}, ${started.accepted})`) return started }, run: (id) => runs.get(id), answer: async (run, callId, approved, always) => { const r = runs.get(run) if (!r || !runControls.get(run)?.approvals.has(callId)) return false try { const { rule } = await answerCall(await store(r.agent, r.owner), r, callId, approved, !!always && approved, { by: r.owner, via: 'discord' }) if (rule) log(`discord: always allowed ${rule.command ? `\`${rule.command}\`` : rule.tool} on ${rule.computer} for ${r.agent} (run ${r.id})`) return true } catch (e: any) { if (e instanceof XrpcHttpError && e.status === 409) return false throw e } }, log, }) /** The owner's computers (joined, or named by one of their agents), and whether `agent` may use each. */ async function agentComputers(owner: string, agent: string) { const state = readState() const rows = new Map() for (const [key, r] of Object.entries(state.runners)) { if (runnerOwner(state, key) === owner && r.tokenHash) rows.set(r.name, { name: r.name, online: hub.online(key), allowed: false }) } const s = await store(agent, owner) await s.refreshComputers() for (const m of s.computers.keys()) rows.set(m, { ...(rows.get(m) ?? { name: m, online: false }), allowed: true }) return [...rows.values()].sort((a, b) => Number(b.online) - Number(a.online) || a.name.localeCompare(b.name)) } const handleOf = (agent: string) => readState().agents[agent]?.agent.handle ?? agent /** * An agent's settings page: GET shows it, POST saves one section (the form's `action`) and comes * back with what happened. Only the agent's owner gets either; anyone else gets "not found". */ async function agentSettingsRequest(name: string, url: URL, req: http.IncomingMessage, res: http.ServerResponse, ip: string) { const who = logins && 'origin' in signIn ? webSession(req.headers.cookie) : undefined const retry = pageLimit(ip, who) if (retry) return tooMany(res, retry) if (!who) { res.writeHead(303, { location: '/', 'cache-control': 'no-store' }) return res.end() } const known = readState().agents[name] if (!known || known.owner !== who.did) return writePage(req, res, notFoundPage(url.pathname)) const path = `/agents/${encodeURIComponent(name)}/settings` const back = (notice: { text: string; error?: boolean }, section: string) => { issueNotice(who.did, path, notice.error ? 'error' : 'done', notice.text) const q = new URLSearchParams(notice.error ? { error: notice.text } : { done: notice.text }) res.writeHead(303, { location: `${path}?tab=${tabOf(section)}&${q}#${section}`, 'cache-control': 'no-store' }) res.end() } const h = await store(name, who.did) if (req.method === 'POST') { if (!logins!.sameOrigin(req.headers.origin)) return writePage(req, res, simplePage(403, 'Not from here', "

This request didn't come from this service's own page.

")) if (!/^application\/x-www-form-urlencoded(;|$)/i.test(req.headers['content-type'] ?? '')) return writePage(req, res, simplePage(400, 'Bad request', '

Send the form.

')) // The system prompt can be long: up to 100k characters, which form-encoding can triple. const form = new URLSearchParams(await readText(req, 512 * 1024)) const action = form.get('action') ?? '' const L = SETTINGS_LIMITS try { if (action === 'name') { const wanted = (form.get('displayName') ?? '').replace(/\s+/g, ' ').trim() if ([...new Intl.Segmenter().segment(wanted)].length > L.displayName) return back({ text: `A name can be at most ${L.displayName} characters.`, error: true }, 'name') const before = readState().agents[name]?.displayName ?? '' if (wanted === before) return back({ text: 'No change to the name.' }, 'name') // Its public profile keeps whatever else is there (a description, an avatar); only displayName changes. const session = await agentSession(readState(), h.entry) const old = await session .call('com.atproto.repo.getRecord', { params: { repo: session.did, collection: 'app.bsky.actor.profile', rkey: 'self' } }) .then((r: any) => r.value as Record) .catch(() => ({}) as Record) const { displayName: _, ...rest } = old const record = { ...rest, $type: 'app.bsky.actor.profile', ...(wanted ? { displayName: wanted } : {}) } await session.call('com.atproto.repo.putRecord', { body: { repo: session.did, collection: 'app.bsky.actor.profile', rkey: 'self', record } }) const s = readState() if (s.agents[name]) { if (wanted) s.agents[name].displayName = wanted else delete s.agents[name].displayName saveState(s) } if (wanted) h.entry.displayName = wanted else delete h.entry.displayName log(`${who.handle} named ${name} "${wanted}" from settings`) return back({ text: wanted ? `Saved: it's called ${wanted} now.` : `Saved: it's called ${name} again.` }, 'name') } if (action === 'handle') { // A new label under the PDS's handle domain. The DID stays; the PDS updates its DID document. const domain = await handleDomain(readState().pds) let label = (form.get('handle') ?? '').trim().toLowerCase().replace(/^@/, '') if (label.endsWith(domain)) label = label.slice(0, -domain.length) if (!HANDLE_LABEL.test(label) || label.includes('--')) return back({ text: 'Handles are 3 to 20 characters: a–z, 0–9, and hyphens in the middle.', error: true }, 'handle') const handle = `${label}${domain}` if (handle === h.entry.agent.handle) return back({ text: 'No change to the handle.' }, 'handle') try { checkHandle(handle) } catch (e: any) { return back({ text: sentence(e.message), error: true }, 'handle') } return await withLock(`handle:${handle}`, async () => { const s = readState() // Agent names stay theirs too: a new agent "label" would want this handle. const ours = knownAccounts(s).some((a) => a.handle === handle) || (label !== name && !!s.agents[label]) || !!s.deletedAgents?.[label] if (ours || Object.values(s.owners ?? {}).some((o) => o.handle === handle) || (await resolveHandle(s.pds, handle))) { return back({ text: `${handle} is taken.`, error: true }, 'handle') } const entry = s.agents[name] if (!entry) return back({ text: 'That failed. Try again.', error: true }, 'handle') const before = entry.agent.handle await moveHandle(s.pds, entry.agent, handle) saveState(s) h.entry.agent.handle = handle h.handles.set(h.entry.agent.did, handle) log(`${who.handle} moved ${name} from ${before} to ${handle} from settings`) return back({ text: `Saved: its handle is ${handle} now.` }, 'handle') }) } if (action === 'model') { const value = form.get('model') ?? '' const at = value.indexOf('/') const provider = value.slice(0, at) const model = value.slice(at + 1) if (at < 1) return back({ text: 'Pick a model.', error: true }, 'model') const reasoning = reasoningOf(form.get('reasoning') ?? undefined) if (`${h.config.provider}/${h.config.model}` === value && (reasoning === undefined || reasoning === (h.config.reasoning ?? DEFAULT_REASONING))) return back({ text: 'No change to the model.' }, 'model') const available = await (await ownerModels(who.did)).getAvailable() const m = available.find((x) => x.provider === provider && x.id === model) if (!m) return back({ text: `This service can't call ${value}.`, error: true }, 'model') await withLock(h.entry.name, () => h.setModel(provider, model)) if (reasoning !== undefined) await withLock(h.entry.name, () => h.setReasoning(reasoning)) log(`${who.handle} set ${name}'s model to ${value} from settings`) noteRecentModel(who.did, value) return back({ text: `Saved: it now uses ${m.name}.` }, 'model') } if (action === 'prompt') { const prompt = (form.get('systemPrompt') ?? '').replace(/\r\n/g, '\n') if (!prompt.trim()) return back({ text: "The instructions can't be empty.", error: true }, 'instructions') if (prompt.length > L.systemPrompt) return back({ text: `The instructions are over ${L.systemPrompt.toLocaleString('en-US')} characters.`, error: true }, 'instructions') if (prompt === h.config.systemPrompt) return back({ text: 'No change to the instructions.' }, 'instructions') await withLock(h.entry.name, () => h.updateConfig({ systemPrompt: prompt })) log(`${who.handle} changed ${name}'s system prompt from settings`) return back({ text: 'Saved the instructions. They apply from its next turn.' }, 'instructions') } if (action === 'compaction') { const read = (field: string, label: string, bounds: { min: number; max: number }) => { const raw = (form.get(field) ?? '').replace(/[,_\s]/g, '') if (!raw) return undefined const v = Number(raw) if (!Number.isInteger(v) || v < bounds.min || v > bounds.max) throw invalid(`${label} must be a whole number from ${bounds.min.toLocaleString('en-US')} to ${bounds.max.toLocaleString('en-US')}`) return v } const reserveTokens = read('reserveTokens', 'Room to leave free', L.reserveTokens) const keepRecentTokens = read('keepRecentTokens', 'Recent history kept', L.keepRecentTokens) const idle = h.config.compaction?.idle const compaction = { ...(reserveTokens ? { reserveTokens } : {}), ...(keepRecentTokens ? { keepRecentTokens } : {}), ...(idle ? { idle } : {}) } await withLock(h.entry.name, () => h.updateConfig({ compaction: Object.keys(compaction).length ? compaction : undefined })) log(`${who.handle} set ${name}'s compaction to ${JSON.stringify(compaction)} from settings`) return back({ text: Object.keys(compaction).length ? 'Saved how it handles long threads.' : 'Saved: long threads use the defaults.' }, 'long-threads') } if (action === 'idle-compaction') { const raw = (form.get('idleMinutes') ?? '').replace(/[,_\s]/g, '') const minutes = raw ? Number(raw) : undefined const B = IDLE_LIMITS.minutes if (minutes !== undefined && (!Number.isInteger(minutes) || minutes < B.min || minutes > B.max)) throw invalid(`Minutes of quiet must be a whole number from ${B.min} to ${B.max.toLocaleString('en-US')}`) const on = form.get('idleEnabled') === 'on' const model = h.model() // Matching the default isn't saved, so the setting follows the model: on for Languid's credit, off for the owner's own connection. const fallback = hostedModel(h.entry.owner, model) const prev = h.config.compaction?.idle const idle = { ...(on !== fallback ? { enabled: on } : {}), ...(minutes ? { minutes } : {}), ...(prev?.minTokens ? { minTokens: prev.minTokens } : {}) } const { idle: _old, ...rest } = h.config.compaction ?? {} const compaction = { ...rest, ...(Object.keys(idle).length ? { idle } : {}) } await withLock(h.entry.name, () => h.updateConfig({ compaction: Object.keys(compaction).length ? compaction : undefined })) log(`${who.handle} set ${name}'s idle compaction to ${JSON.stringify(idle)} from settings`) return back({ text: on ? 'Saved: quiet conversations are compacted.' : "Saved: quiet conversations aren't compacted." }, 'long-threads') } if (action === 'auto-title') { const on = form.get('autoTitle') === 'on' const state = readState() const prefs = ((state.preferences ??= {})[who.did] ??= {}) if (on) delete prefs.autoTitle else prefs.autoTitle = false saveState(state) return back({ text: on ? 'Saved: new chats get a title.' : "Saved: new chats aren't titled." }, 'chats') } if (action === 'jobs') { const on = form.get('wakeOnJobEnd') === 'on' await setJobWakeups(h, on, who.did) return back({ text: on ? 'Saved: it looks over jobs when they finish.' : "Saved: it won't look over finished jobs." }, 'jobs') } if (action === 'computers') { const wanted = new Set(form.getAll('computer')) for (const c of await agentComputers(who.did, name)) { if (wanted.has(c.name) && !c.allowed) await attachComputer(who.did, c.name, [name]) if (!wanted.has(c.name) && c.allowed) await detachComputer(who.did, c.name, [name]) } const now = (await agentComputers(who.did, name)).filter((c) => c.allowed).map((c) => c.name) log(`${who.handle} set ${name}'s computers to ${now.join(', ') || 'none'} from settings`) return back({ text: now.length ? `Saved: it may use ${now.join(', ')}.` : 'Saved: it may use no computers.' }, 'computers') } if (action === 'revoke') { const computer = form.get('computer') ?? '' const tool = form.get('tool') ?? '' if (!computer || !tool) return back({ text: 'Which rule?', error: true }, 'computers') await h.askAgain(computer, tool) log(`${who.handle} removed always-allow ${tool} on ${computer} for ${name} from settings`) return back({ text: `It will ask before ${tool} on ${computer} again.` }, 'computers') } if (action === 'rules') { const computer = form.get('computer') ?? '' await refreshRunners(h) if (!h.computers.has(computer)) return back({ text: `It may not use a computer named ${computer}.`, error: true }, 'rules') const rules: ComputerRules = { allow: [], deny: [], allowCommands: [], denyCommands: [] } for (const tool of RULE_TOOLS) { const mode = form.get(`tool:${tool}`) ?? 'ask' if (mode === 'allow') rules.allow.push(tool) else if (mode === 'deny') rules.deny.push(tool) else if (mode !== 'ask') return back({ text: `Rules not saved: unknown choice for ${tool}.`, error: true }, 'rules') } try { rules.allowCommands = parseCommands(form.get('allowCommands') ?? '', 'Commands that run without asking') rules.denyCommands = parseCommands(form.get('denyCommands') ?? '', 'Commands it may never run') } catch (e: any) { return back({ text: `Rules not saved. ${e.message}.`, error: true }, 'rules') } await h.setRules(computer, rules) log(`${who.handle} set ${name}'s rules on ${computer} from settings: ${JSON.stringify(rules)}`) return back({ text: `Saved the rules for ${computer}. They apply to the next call.` }, 'rules') } if (action === 'policy') { let policy try { policy = parsePolicy(JSON.parse(form.get('policy') ?? '')) } catch (e: any) { return back({ text: `Script permissions not saved: ${e instanceof SyntaxError ? 'that is not valid JSON' : e.message}.`, error: true }, 'permissions') } if (policy.rules.length > L.policyRules) return back({ text: `Script permissions not saved: at most ${L.policyRules} rules.`, error: true }, 'permissions') const same = JSON.stringify(policy) === JSON.stringify(parsePolicy(DEFAULT_POLICY)) await withLock(h.entry.name, () => h.updateConfig({ policy: same ? undefined : policy })) log(`${who.handle} set ${name}'s script policy (${policy.rules.length} rules) from settings`) return back({ text: same ? 'Script permissions saved: they match the default.' : `Script permissions saved: ${policy.rules.length} rules.` }, 'permissions') } if (action === 'write-add' || action === 'write-remove') { /** Whether a rule is the one a Remove button names, as JSON, whatever its key order. */ const sameRule = (r: object, json: string | null) => { try { const o = JSON.parse(json ?? '') const keys = (x: any) => Object.keys(x).sort() return JSON.stringify(keys(r)) === JSON.stringify(keys(o)) && keys(r).every((k) => (r as any)[k] === o[k]) } catch { return false } } let message = '' try { await withLock(h.entry.name, async () => { const rules = [...h.policy().rules] if (action === 'write-add') { const rule = writeRule(form.get('collection') ?? '', form.get('rkey') ?? '', form.get('where') ?? '', form.get('effect') ?? '') const same = rules.findIndex((r) => isWriteRule(r) && r.collection === rule.collection && r.rkey === rule.rkey && r.repo === rule.repo) if (same >= 0) rules.splice(same, 1) // First, so it decides before any rule further down. rules.unshift(rule) if (rules.length > L.policyRules) throw new Error(`at most ${L.policyRules} rules`) message = `Saved: its scripts may write ${rule.collection}${rule.rkey ? `/${rule.rkey}` : ''}${rule.effect === 'approve' ? ', asking you first' : ''}.` } else { const i = rules.findIndex((r) => isWriteRule(r) && sameRule(r, form.get('rule'))) if (i < 0) throw new Error('that rule is already gone') const [gone] = rules.splice(i, 1) message = `Removed: its scripts may no longer write ${gone.collection}${gone.rkey ? `/${gone.rkey}` : ''}.` } const same = JSON.stringify({ rules }) === JSON.stringify(parsePolicy(DEFAULT_POLICY)) await h.updateConfig({ policy: same ? undefined : { rules } }) }) } catch (e: any) { return back({ text: `Not saved: ${e.message.replace(/^./, (c: string) => c.toLowerCase())}.`, error: true }, 'writes') } log(`${who.handle} changed ${name}'s record writes from settings: ${message}`) return back({ text: message }, 'writes') } if (action === 'telemetry-share' || action === 'telemetry-stop') { const share = action === 'telemetry-share' try { if (share) await feedbackDestination() const sharing = await (await telemetryOf(name)).setSharing(share, who.did, readState().pds) log(`${who.handle} ${share ? 'shares' : 'does not share'} ${name}'s error reports from settings`) return back({ text: share ? `Error reports are shared with ${sharing.recipientHandle ?? sharing.recipient}.` : 'Error reports are not shared.' }, 'errors') } catch (e: any) { if (!(e instanceof TelemetryError)) throw e return back({ text: sentence(e.message), error: true }, 'errors') } } if (action === 'telemetry-clear') { const deleted = await (await telemetryOf(name)).clear() return back({ text: `Deleted ${deleted} error report${deleted === 1 ? '' : 's'}.` }, 'errors') } if (action === 'delete') { const handle = h.entry.agent.handle const typed = (form.get('confirm') ?? '').trim().replace(/^@/, '').toLowerCase() if (typed !== handle.toLowerCase()) return back({ text: `Not deleted: type ${handle} to confirm.`, error: true }, 'delete') const purgeAfter = await withLock(name, async () => { // Stop its runs first, so nothing writes while its account goes. for (const r of runs.values()) if (r.agent === name && r.status === 'running') runControls.get(r.id)?.stop() const { purgeAfter } = await deactivateAgent(readState(), name) discord.forgetAgent(name) stores.delete(name) storeSpaces.delete(name) telemetries.delete(name) wakeupRetry.delete(name) return purgeAfter }) log(`${who.handle} deleted ${name} (${h.entry.agent.did}) from settings; its account is deactivated until ${purgeAfter.toISOString()}`) const deletedName = displayNameOf(h.entry).slice(0, 80) issueNotice(who.did, '/', 'deleted', deletedName) const q = new URLSearchParams({ deleted: deletedName }) res.writeHead(303, { location: `/?${q}`, 'cache-control': 'no-store' }) return res.end() } if (action === 'policy-default') { await withLock(h.entry.name, () => h.updateConfig({ policy: undefined })) log(`${who.handle} reset ${name}'s script policy from settings`) return back({ text: 'Script permissions saved: it uses the default rules.' }, 'permissions') } return back({ text: 'Unknown action.', error: true }, 'model') } catch (e: any) { if (!(e instanceof XrpcHttpError)) log(`settings ${action} for ${name} failed: ${e?.stack ?? e}`) return back({ text: e instanceof XrpcHttpError ? sentence(e.message) : 'That failed. Try again.', error: true }, 'model') } } if (req.method !== 'GET' && req.method !== 'HEAD') return writePage(req, res, simplePage(405, 'Method not allowed', '

Use the forms on the page.

')) await refreshRunners(h) const models = await keepRunnable(h, who.did) const computers = await agentComputers(who.did, name) const telemetry = await telemetryOf(name).then((t) => t.status()).catch((e: any) => { log(`${name}'s error reports unreadable: ${e?.message ?? e}`) return undefined }) const consume = req.method === 'GET' const done = takeNotice(who.did, path, 'done', url.searchParams.get('done'), consume) const error = takeNotice(who.did, path, 'error', url.searchParams.get('error'), consume) return writePage( req, res, agentSettingsPage({ agent: name, ...(readState().agents[name]?.displayName ? { displayName: readState().agents[name].displayName } : {}), handle: h.entry.agent.handle, handleDomain: await handleDomain(readState().pds).catch(() => h.entry.agent.handle.slice(h.entry.agent.handle.indexOf('.'))), did: h.entry.agent.did, ...(url.searchParams.get('tab') ? { tab: url.searchParams.get('tab')! } : {}), model: { provider: h.config.provider, id: h.config.model }, reasoning: h.config.reasoning ?? DEFAULT_REASONING, models: models.map((m) => ({ provider: m.provider, id: m.id, name: m.name, contextWindow: m.contextWindow, connection: connectionOf(who.did, m) })), recentModels: readState().preferences?.[who.did]?.recentModels ?? [], houseModels: houseModels(), systemPrompt: h.config.systemPrompt, compaction: h.config.compaction ?? {}, idleDefault: (() => { try { return hostedModel(h.entry.owner, h.model()) } catch { return false } })(), limits: h.compactionSettings(), wakeOnJobEnd: (await withLock(h.entry.name, () => migrated(h))).some((m) => m.value.trigger.type === 'jobEnd' && m.value.enabled), autoTitle: readState().preferences?.[who.did]?.autoTitle !== false, computers, rules: computers.filter((c) => c.allowed).map((c) => ({ computer: c.name, rules: h.rulesFor(c.name) })), blocks: [...h.blocks.values()].map((b) => b.value), ...(h.config.policy === undefined ? {} : { policy: JSON.stringify(h.config.policy, null, 2) }), defaultPolicy: JSON.stringify(DEFAULT_POLICY, null, 2), writeRules: h.policy().rules.filter(isWriteRule), ...(telemetry ? { telemetry } : {}), ...(error ? { notice: { text: error, error: true } } : done ? { notice: { text: done } } : {}), }), ) } // Machines (src/machines.ts), for XRPC, the old schedule methods and the Machines tab alike. type Store = Awaited> /** Old schedules and wakeOnJobEnd become Machines before anything reads or changes Machines. Under the agent lock. */ async function migrated(h: Store) { const schedules = await h.repo.list(NSID.schedule) if (schedules.length || h.config.wakeOnJobEnd !== undefined) await migrateLegacy(h, schedules) const machines = await h.repo.list(NSID.machine) h.remember(machines) return machines } /** A Machine's model must be one its owner can run, as in the chat's model picker. */ async function checkMachineModel(owner: string, model: Machine['model']) { if (!model) return const available = await (await ownerModels(owner)).getAvailable() if (!available.some((m) => m.provider === model.provider && m.id === model.id)) throw invalid(`${model.provider}/${model.id} isn't a model you can run here`) } async function createMachine(h: Store, input: any, owner: string) { let value: Machine try { value = validateMachine(input, owner) } catch (e: any) { throw invalid(e.message) } await checkMachineModel(owner, value.model) return withLock(h.entry.name, async () => { const key = tid() await h.repo.create(NSID.machine, { $type: NSID.machine, ...value }, key) const record = await h.repo.get(NSID.machine, key) if (record) h.remember([record]) return { machine: record } }) } async function updateMachine(h: Store, rkey: unknown, input: any, owner: string, notFound = 'MachineNotFound') { if (typeof rkey !== 'string' || !rkey) throw invalid('rkey is required') if (input?.model !== undefined && input.model !== null) await checkMachineModel(owner, input.model) return withLock(h.entry.name, async () => { await migrated(h) const m = await h.repo.get(NSID.machine, rkey) if (!m) throw new XrpcHttpError(404, notFound, 'no such Machine') let value: Machine try { value = validateMachine(input, owner, m.value) } catch (e: any) { throw invalid(e.message) } const ref = await h.repo.put(NSID.machine, rkey, { $type: NSID.machine, ...value }) const record = { ...m, ...ref, value } h.remember([record]) return { machine: record } }) } // Runs already made stay work: deleting stops only future ones. async function deleteMachine(h: Store, rkey: unknown, notFound = 'MachineNotFound') { if (typeof rkey !== 'string' || !rkey) throw invalid('rkey is required') return withLock(h.entry.name, async () => { await migrated(h) if (!(await h.repo.get(NSID.machine, rkey))) throw new XrpcHttpError(404, notFound, 'no such Machine') await h.repo.delete(NSID.machine, rkey) h.forget(NSID.machine, rkey) return {} }) } /** Each Machine with its runs today and its latest run; and every Machine's runs, newest first. */ async function machinesOf(h: Store) { const machines = await withLock(h.entry.name, () => migrated(h)) const [wakeups, messages] = await Promise.all([h.repo.list(NSID.wakeup), h.repo.list(NSID.message)]) h.remember([...wakeups]) const pending = new Set(pendingWakeups(wakeups, messages).map((w) => w.uri)) const runOf = (w: SpaceRecord) => ({ wakeup: w, status: (!pending.has(w.uri) ? 'done' : parked(h, w.value.thread) ? 'waiting' : 'pending') as 'done' | 'waiting' | 'pending' }) const time = Date.now() const runs = (m: SpaceRecord) => wakeups.filter((w) => w.value.source.uri === m.uri || (m.value.migratedFrom && w.value.source.uri === m.value.migratedFrom.uri)).sort((a, b) => b.value.createdAt.localeCompare(a.value.createdAt) || b.rkey.localeCompare(a.rkey)) return machines.map((m) => { const mine = runs(m) return { ...m, runsToday: runsToday(m, wakeups, time), ...(mine[0] ? { last: runOf(mine[0]) } : {}), runs: mine.slice(0, 20).map(runOf) } }) } /** A Machine as an old schedule record, for ai.languid.run.list. */ const asSchedule = (m: SpaceRecord) => { const t = m.value.trigger as Extract return { ...m, value: { prompt: m.value.goal, startsAt: t.startsAt, ...(t.intervalSeconds === undefined ? {} : { intervalSeconds: t.intervalSeconds }), enabled: m.value.enabled, authorizedBy: m.value.authorizedBy, createdAt: m.value.createdAt } } } /** ai.languid.run.createSchedule: a schedule Machine with no tools, as schedules were. */ async function createSchedule(h: Store, input: any, owner: string) { if (typeof input?.prompt !== 'string' || !input.prompt.trim() || Buffer.byteLength(input.prompt, 'utf8') > 8000) throw invalid('prompt must be 1-8000 UTF-8 bytes') const { machine } = await createMachine(h, { name: 'Schedule', goal: input.prompt, trigger: { type: 'schedule', startsAt: input.startsAt, intervalSeconds: input.intervalSeconds }, ...(input.enabled === undefined ? {} : { enabled: input.enabled }) }, owner) return { schedule: machine && asSchedule(machine), machine } } /** ai.languid.run.setJobWakeups: turn job review Machines on (making one if there's none) or off. */ async function setJobWakeups(h: Store, enabled: boolean, owner: string) { const jobMachines = (await withLock(h.entry.name, () => migrated(h))).filter((m) => m.value.trigger.type === 'jobEnd') if (enabled && !jobMachines.length) return createMachine(h, { name: 'Job review', goal: JOB_REVIEW_GOAL, trigger: { type: 'jobEnd' } }, owner) for (const m of jobMachines) if (m.value.enabled !== enabled) await updateMachine(h, m.rkey, { enabled }, owner) return {} } /** `/agents//machines` and `/agents//machines/`: the Machines tab, and one Machine. */ async function machinesRequest(name: string, rkey: string | undefined, url: URL, req: http.IncomingMessage, res: http.ServerResponse, ip: string) { const who = logins && 'origin' in signIn ? webSession(req.headers.cookie) : undefined const retry = pageLimit(ip, who) if (retry) return tooMany(res, retry) if (!who) { res.writeHead(303, { location: '/', 'cache-control': 'no-store' }) return res.end() } const known = readState().agents[name] if (!known || known.owner !== who.did) return writePage(req, res, notFoundPage(url.pathname)) const list = `/agents/${encodeURIComponent(name)}/machines` const path = rkey ? `${list}/${encodeURIComponent(rkey)}` : list const cookieZone = /(?:^|;\s*)languid_tz=([^;]+)/.exec(req.headers.cookie ?? '')?.[1] let zone = 'UTC' try { if (cookieZone && validZone(decodeURIComponent(cookieZone))) zone = decodeURIComponent(cookieZone) } catch {} const back = (notice: { text: string; error?: boolean }, to = path, anchor?: string, headers: Record = {}) => { issueNotice(who.did, to, notice.error ? 'error' : 'done', notice.text) const q = new URLSearchParams(notice.error ? { error: notice.text } : { done: notice.text }) res.writeHead(303, { location: `${to}?${q}${anchor ? `#${anchor}` : ''}`, 'cache-control': 'no-store', ...headers }) res.end() } const h = await store(name, who.did) if (req.method === 'POST') { if (!logins!.sameOrigin(req.headers.origin)) return writePage(req, res, simplePage(403, 'Not from here', "

This request didn't come from this service's own page.

")) if (!/^application\/x-www-form-urlencoded(;|$)/i.test(req.headers['content-type'] ?? '')) return writePage(req, res, simplePage(400, 'Bad request', '

Send the form.

')) const form = new URLSearchParams(await readText(req, 64 * 1024)) const action = form.get('action') ?? '' try { if (action === 'save') { const when = form.get('when') let trigger: any let tz: Record = {} if (when === 'quiet') trigger = { type: 'threadSettled', quietMinutes: Number(form.get('quiet')) } else if (when === 'job') trigger = { type: 'jobEnd' } else if (when === 'schedule') { const picked = form.get('zone') ?? '' if (!validZone(picked)) return back({ text: 'Pick a time zone.', error: true }, path, rkey ? 'edit' : 'new') const startsAt = zonedToIso(form.get('at') ?? '', picked) if (!startsAt) return back({ text: 'Pick a start date and time.', error: true }, path, rkey ? 'edit' : 'new') const repeat = form.get('repeat') === 'every' const unit = form.get('unit') ?? '' const k = Number(form.get('every')) if (repeat && (!Object.hasOwn(UNITS, unit) || !Number.isSafeInteger(k) || k < 1)) return back({ text: 'Repeat every whole number of minutes, hours, days or weeks.', error: true }, path, rkey ? 'edit' : 'new') trigger = { type: 'schedule', startsAt, ...(repeat ? { intervalSeconds: k * UNITS[unit as keyof typeof UNITS] } : {}) } tz = { 'set-cookie': `languid_tz=${encodeURIComponent(picked)}; Path=/; Max-Age=31536000; SameSite=Lax; HttpOnly${isHttps ? '; Secure' : ''}` } } else return back({ text: 'Pick when it runs.', error: true }, path, rkey ? 'edit' : 'new') const model = form.get('model') ?? '' const slash = model.indexOf('/') const input = { name: form.get('name') ?? '', goal: form.get('goal') ?? '', trigger, model: model && slash > 0 ? { provider: model.slice(0, slash), id: model.slice(slash + 1) } : null, permissions: permissionsFrom({ editMemory: form.has('editMemory'), askMemory: form.has('askMemory'), readRecords: form.has('readRecords') }) ?? null, dailyRuns: Number(form.get('dailyRuns') || DEFAULT_DAILY_RUNS), } if (rkey) { await updateMachine(h, rkey, input, who.did) log(`${who.handle} changed ${name}'s Machine ${rkey}`) return back({ text: 'Saved.' }, path, undefined, tz) } const made = await createMachine(h, input, who.did) log(`${who.handle} added a Machine for ${name}`) return back({ text: 'Added.' }, list, made.machine ? `m-${encodeURIComponent(made.machine.rkey)}` : 'list', tz) } if ((action === 'on' || action === 'off') && rkey) { await updateMachine(h, rkey, { enabled: action === 'on' }, who.did) log(`${who.handle} turned ${action} ${name}'s Machine ${rkey}`) return back({ text: action === 'on' ? 'On.' : 'Off.' }, form.get('back') === 'machine' ? path : list, form.get('back') === 'machine' ? undefined : `m-${encodeURIComponent(rkey)}`) } if (action === 'delete' && rkey) { await deleteMachine(h, rkey) log(`${who.handle} deleted ${name}'s Machine ${rkey}`) return back({ text: 'Deleted.' }, list, 'list') } return back({ text: 'Unknown action.', error: true }) } catch (e: any) { if (!(e instanceof XrpcHttpError)) log(`machines ${action} for ${name} failed: ${e?.stack ?? e}`) return back({ text: sentence(e?.message ?? 'That failed. Try again.'), error: true }, path, rkey ? (action === 'save' ? 'edit' : undefined) : 'new') } } if (req.method !== 'GET' && req.method !== 'HEAD') return writePage(req, res, simplePage(405, 'Method not allowed', '

Use the forms on the page.

')) const machines = await machinesOf(h) const spent = machineUsage(readState(), who.did, name, 30) const summary = (m: (typeof machines)[number]) => ({ ...m, ...(spent.has(m.rkey) ? { charged: spent.get(m.rkey)!.charged } : {}) }) const models = (await keepRunnable(h, who.did)).map((m) => ({ value: `${m.provider}/${m.id}`, label: m.name ?? `${m.provider}/${m.id}` })) const consume = req.method === 'GET' const done = takeNotice(who.did, url.pathname, 'done', url.searchParams.get('done'), consume) const error = takeNotice(who.did, url.pathname, 'error', url.searchParams.get('error'), consume) const notice = error ? { notice: { text: error, error: true } } : done ? { notice: { text: done } } : {} const common = { agent: name, handle: h.entry.agent.handle, agentModel: `${h.config.provider}/${h.config.model}`, models, zone, now: Date.now(), ...notice } if (rkey) { const m = machines.find((x) => x.rkey === rkey) if (!m) return writePage(req, res, notFoundPage(url.pathname)) return writePage(req, res, machinePage({ ...common, machine: summary(m), runs: m.runs, ...(url.searchParams.get('delete') ? { confirm: true } : {}) })) } return writePage(req, res, machinesPage({ ...common, machines: machines.map(summary), ...(url.searchParams.get('start') === 'blank' ? { blank: true } : {}) })) } /** Resolves with the run once it has ended (or is gone), checking once a second. */ const runEnded = (id: string) => new Promise((resolve) => { const check = () => { const r = runs.get(id) if (!r || r.status !== 'running') return resolve(r) setTimeout(check, 1000) } check() }) const runReply = (r: Run | undefined) => (r?.events ?? []) .flatMap((e) => (e.type === 'text' ? [e.text] : [])) .join('\n\n') .trim() /** `/agents//memory`: the agent's blocks, to read, to pin or unpin, and to correct. */ async function memoryRequest(name: string, url: URL, req: http.IncomingMessage, res: http.ServerResponse, ip: string) { const who = logins && 'origin' in signIn ? webSession(req.headers.cookie) : undefined const retry = pageLimit(ip, who) if (retry) return tooMany(res, retry) if (!who) { res.writeHead(303, { location: '/', 'cache-control': 'no-store' }) return res.end() } const known = readState().agents[name] if (!known || known.owner !== who.did) return writePage(req, res, notFoundPage(url.pathname)) const path = `/agents/${encodeURIComponent(name)}/memory` const back = (notice: { text: string; error?: boolean }, label?: string) => { issueNotice(who.did, path, notice.error ? 'error' : 'done', notice.text) const q = new URLSearchParams(notice.error ? { error: notice.text } : { done: notice.text }) res.writeHead(303, { location: `${path}?${q}${label ? `#block-${encodeURIComponent(label)}` : ''}`, 'cache-control': 'no-store' }) res.end() } const h = await store(name, who.did) if (req.method === 'POST') { if (!logins!.sameOrigin(req.headers.origin)) return writePage(req, res, simplePage(403, 'Not from here', "

This request didn't come from this service's own page.

")) if (!/^application\/x-www-form-urlencoded(;|$)/i.test(req.headers['content-type'] ?? '')) return writePage(req, res, simplePage(400, 'Bad request', '

Send the form.

')) const form = new URLSearchParams(await readText(req, 16 * 1024)) const action = form.get('action') ?? '' try { if (action === 'pin') { const label = form.get('label') ?? '' const pinned = form.get('pinned') === 'on' if (!h.blocks.has(label)) return back({ text: `There's no block named ${label}.`, error: true }) await withLock(h.entry.name, () => h.pinBlock(label, pinned)) log(`${who.handle} ${pinned ? 'pinned' : 'unpinned'} ${name}'s block ${label}`) return back({ text: pinned ? `Pinned ${label}: it sees it in full every turn.` : `Unpinned ${label}: it reads it when it needs it.` }, label) } if (action === 'correct') { // The owner's correction goes to the agent in a thread of its own that is never listed and // is deleted once the run ends. The page (memory.js) follows the run; without scripts, this // waits for it a while and then shows the block. const json = /application\/json/.test(req.headers.accept ?? '') const label = form.get('label') ?? '' const correction = (form.get('correction') ?? '').trim() const selection = (form.get('selection') ?? '').trim() const block = h.blocks.get(label)?.value const refuse = (text: string) => { if (!json) return back({ text, error: true }, label) res.writeHead(400, { 'content-type': 'application/json', 'x-content-type-options': 'nosniff', 'cache-control': 'no-store' }) res.end(JSON.stringify({ error: 'InvalidRequest', message: text })) } if (!block) return refuse(`There's no block named ${label}.`) if (block.readOnly) return refuse(`${name} can't change ${label}: it's read-only.`) if (!correction) return refuse('Say what should change.') if (correction.length > 4000 || selection.length > 8000) return refuse('That correction is too long.') const retry = limit('model', who.did) if (retry) return tooMany(res, retry) const th = await withLock(h.entry.name, () => h.newThread(`Memory correction: ${label}`, { ephemeral: true })) const drop = () => withLock(h.entry.name, () => h.deleteThread(th.rkey)).catch((e: any) => log(`deleting ${name}'s correction thread ${th.rkey} failed: ${e?.message ?? e}`)) let started try { started = await startRun( { did: who.did, handle: who.handle, via: 'session' }, { agent: name, prompt: correctionPrompt(label, selection, correction), thread: th.rkey, via: 'memory' }, 'deny', ) } catch (e) { await drop() throw e } log(`${who.handle} sent ${name} a correction to ${label} (run ${started.run})`) const ended = runEnded(started.run).then(async (r) => { await drop() return r }) if (json) { res.writeHead(200, { 'content-type': 'application/json', 'x-content-type-options': 'nosniff', 'cache-control': 'no-store' }) return res.end(JSON.stringify({ run: started.run, label })) } const r = await Promise.race([ended, new Promise<'waiting'>((ok) => setTimeout(() => ok('waiting'), 45_000))]) if (r === 'waiting') return back({ text: `${name} is still working on it. Reload in a minute to see ${label}.` }, label) if (r?.status === 'error') return back({ text: sentence(r.error ?? 'The correction failed.'), error: true }, label) return back({ text: runReply(r).slice(0, 600) || `${name} updated ${label}.` }, label) } return back({ text: 'Unknown action.', error: true }) } catch (e: any) { if (!(e instanceof XrpcHttpError)) log(`memory ${action} for ${name} failed: ${e?.stack ?? e}`) return back({ text: sentence(e?.message ?? 'That failed. Try again.'), error: true }) } } if (req.method !== 'GET' && req.method !== 'HEAD') return writePage(req, res, simplePage(405, 'Method not allowed', '

Use the forms on the page.

')) const consume = req.method === 'GET' const done = takeNotice(who.did, path, 'done', url.searchParams.get('done'), consume) const error = takeNotice(who.did, path, 'error', url.searchParams.get('error'), consume) return writePage( req, res, memoryPage({ agent: name, handle: h.entry.agent.handle, blocks: [...h.blocks.values()].map((b) => b.value), ...(error ? { notice: { text: error, error: true } } : done ? { notice: { text: done } } : {}), }), ) } /** Each member's records in an agent's space, listed whole and kept briefly: the navigator reads them a lot. */ const spaceListings = new Map }>() function spaceRows(name: string, actor: () => Promise, space: string, repo: string): Promise { const key = `${name}\n${repo}` const hit = spaceListings.get(key) if (hit && Date.now() - hit.at < 20_000) return hit.rows const rows = actor().then((a) => new SpaceRepo(a, space, repo).list()).then((rs) => rs.map((r) => ({ collection: r.collection, rkey: r.rkey, cid: r.cid, value: r.value }))) rows.catch(() => spaceListings.delete(key)) spaceListings.set(key, { at: Date.now(), rows }) if (spaceListings.size > 200) spaceListings.delete(spaceListings.keys().next().value!) return rows } /** * `/agents//records[//[/[/]]]`: a navigator over the * agent's records, in its space (each member's repo, read as that member) and in both public repos. */ async function recordsRequest(name: string, rest: string, url: URL, req: http.IncomingMessage, res: http.ServerResponse, ip: string) { const who = logins && 'origin' in signIn ? webSession(req.headers.cookie) : undefined const retry = pageLimit(ip, who) if (retry) return tooMany(res, retry) if (!who) { res.writeHead(303, { location: '/', 'cache-control': 'no-store' }) return res.end() } const known = readState().agents[name] if (!known || known.owner !== who.did) return writePage(req, res, notFoundPage(url.pathname)) if (req.method !== 'GET' && req.method !== 'HEAD') return writePage(req, res, simplePage(405, 'Method not allowed', '

This page only reads.

')) const h = await store(name, who.did) let owner: Promise | undefined const pds = readState().pds const m: Members = { agent: name, space: h.entry.space, agentRepo: { did: h.session.did, handle: h.entry.agent.handle }, ownerRepo: { did: who.did, handle: who.handle }, } const b = { agent: name, handle: h.entry.agent.handle } const actorOf = async (r: RepoRef): Promise => (r.role === 'agent' ? h.session : await (owner ??= ownerActor(who.did))) const rowsOf = (r: RepoRef) => spaceRows(name, () => actorOf(r), m.space, r.did) const members = (where: Where) => [repoOf(m, where, m.agentRepo.did)!, repoOf(m, where, m.ownerRepo.did)!] const listing = async (): Promise => Promise.all(members('space').map(async (repo) => ({ repo, rows: await rowsOf(repo).catch(() => []) }))) const publicCall = async (nsid: string, params: Record) => { const at = await resolveRepo(String(params.repo), pds) return guardedFetch(xrpcUrl(at.pds, nsid, { ...params, repo: at.did }), [new URL(pds).origin]) } const counts = (rows: RecordRow[]) => { const c = new Map() for (const r of rows) c.set(r.collection, (c.get(r.collection) ?? 0) + 1) return [...c].sort(([a], [z]) => a.localeCompare(z)).map(([nsid, count]) => ({ nsid, count })) } const collectionsOf = async (r: RepoRef): Promise<{ nsid: string; count?: number }[]> => r.where === 'space' ? counts(await rowsOf(r)) : ((await publicCall('com.atproto.repo.describeRepo', { repo: r.did })).collections ?? []).map((nsid: string) => ({ nsid })) const failed = (e: any) => sentence(e?.message ?? 'That failed.') try { if (!rest) { const jump = url.searchParams.get('uri') if (jump !== null) { const href = uriHref(m, jump) if (href) { res.writeHead(303, { location: href, 'cache-control': 'no-store' }) return res.end() } } const repos = await Promise.all( [...members('space'), ...members('repo')].map(async (repo) => { try { return { repo, collections: await collectionsOf(repo) } } catch (e: any) { return { repo, collections: [], error: failed(e) } } }), ) return writePage(req, res, recordsIndexPage({ ...b, repos, ...(jump !== null ? { notice: { text: `${jump.trim() || 'That'} isn't one of ${name}'s records.`, error: true } } : {}) })) } const parts = rest.split('/').map((p) => decodeURIComponent(p)) const [where, did, collection, rkey] = parts const repo = (where === 'space' || where === 'repo') && parts.length <= 4 ? repoOf(m, where, did) : undefined if (!repo) return writePage(req, res, notFoundPage(url.pathname)) if (!collection) return writePage(req, res, repoPage({ ...b, repo, collections: await collectionsOf(repo) })) const PAGE = 100 if (!rkey) { if (repo.where === 'space') { const all = (await rowsOf(repo)).filter((r) => r.collection === collection).reverse() const offset = Math.max(0, Math.floor(Number(url.searchParams.get('offset')) || 0)) const q = (o: number) => (o > 0 ? `?offset=${o}` : '') return writePage( req, res, collectionPage({ ...b, repo, collection, records: all.slice(offset, offset + PAGE), total: all.length, ...(offset + PAGE < all.length ? { next: q(offset + PAGE) } : {}), ...(offset > 0 ? { prev: q(Math.max(0, offset - PAGE)) } : {}), }), ) } const cursor = url.searchParams.get('cursor') ?? undefined const page = await publicCall('com.atproto.repo.listRecords', { repo: repo.did, collection, limit: PAGE, cursor }) const records: RecordRow[] = (page.records ?? []).map((r: any) => ({ collection, rkey: String(r.uri).split('/').pop()!, cid: r.cid, value: r.value })) return writePage( req, res, collectionPage({ ...b, repo, collection, records, ...(page.cursor && records.length === PAGE ? { next: `?cursor=${encodeURIComponent(page.cursor)}` } : {}), ...(cursor ? { prev: '' } : {}) }), ) } let record: (RecordRow & { uri: string }) | undefined let all: Listing | undefined if (repo.where === 'space') { all = await listing() const row = all.find((l) => l.repo.did === repo.did)?.rows.find((r) => r.collection === collection && r.rkey === rkey) if (row) record = { ...row, uri: `${m.space}/${repo.did}/${collection}/${rkey}` } } else { try { const r = await publicCall('com.atproto.repo.getRecord', { repo: repo.did, collection, rkey }) record = { collection, rkey, cid: r.cid, value: r.value, uri: r.uri ?? `at://${repo.did}/${collection}/${rkey}` } } catch (e: any) { if (e?.error !== 'RecordNotFound') throw e } } if (!record) return writePage(req, res, notFoundPage(url.pathname)) if (url.searchParams.get('format') === 'json') { res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store', 'x-content-type-options': 'nosniff' }) return res.end(req.method === 'HEAD' ? undefined : JSON.stringify({ uri: record.uri, cid: record.cid, value: record.value }, null, 2)) } const resolve = (s: string, key?: string): string | undefined => { if (s.startsWith('at://')) return uriHref(m, s) const member = repoOf(m, repo.where, s) if (member) return navHref(name, member) if (all && key) { const t = refTarget(all, repo, key, s) if (t) return navHref(name, t.repo, t.row.collection, t.row.rkey) } return undefined } return writePage(req, res, recordPage({ ...b, repo, record, resolve, ...(all ? { backlinks: backlinksOf(all, record) } : {}) })) } catch (e: any) { if (!(e instanceof XrpcHttpError)) log(`records ${rest} for ${name} failed: ${e?.stack ?? e}`) return writePage(req, res, simplePage(502, "Couldn't read that", `

${esc(failed(e))}

Back to the records

`)) } } /** * `/agents//evals`: the agent's cases and runs, one run trial by trial, its eval settings, * and starting a run or the gate. Runs go in the background, written as the owner. */ async function evalsRequest(name: string, url: URL, req: http.IncomingMessage, res: http.ServerResponse, ip: string, caseKey?: string) { const who = logins && 'origin' in signIn ? webSession(req.headers.cookie) : undefined const retry = pageLimit(ip, who) if (retry) return tooMany(res, retry) if (!who) { res.writeHead(303, { location: '/', 'cache-control': 'no-store' }) return res.end() } const known = readState().agents[name] if (!known || known.owner !== who.did) return writePage(req, res, notFoundPage(url.pathname)) const path = `/agents/${encodeURIComponent(name)}/evals` let backTo = path const back = (notice: { text: string; error?: boolean }, extra: Record = {}) => { issueNotice(who.did, backTo, notice.error ? 'error' : 'done', notice.text) const q = new URLSearchParams({ ...extra, ...(notice.error ? { error: notice.text } : { done: notice.text }) }) res.writeHead(303, { location: `${backTo}?${q}`, 'cache-control': 'no-store' }) res.end() } const evals = await import('./evals.ts') const h = await store(name, who.did) const owner = await ownerActor(who.did) const readers = { owner, agent: h.session } if (req.method === 'POST') { if (!logins!.sameOrigin(req.headers.origin)) return writePage(req, res, simplePage(403, 'Not from here', "

This request didn't come from this service's own page.

")) if (!/^application\/x-www-form-urlencoded(;|$)/i.test(req.headers['content-type'] ?? '')) return writePage(req, res, simplePage(400, 'Bad request', '

Send the form.

')) const form = new URLSearchParams(await readText(req, MAX_BODY.register)) const action = form.get('action') ?? '' // A run started from a case's page goes back there, with the run's results on it. if (form.get('back') === 'case' && form.get('case')) backTo = `${path}/cases/${encodeURIComponent(form.get('case')!)}` try { if (action === 'settings') { const judge = form.get('judge') ?? '' const daily = Number(form.get('dailyTrials') ?? evals.DEFAULT_DAILY_TRIALS) if (!Number.isInteger(daily) || daily < 0 || daily > 1000) return back({ text: 'Trials a day must be a whole number from 0 to 1,000.', error: true }) if (judge) { const available = await (await ownerModels(who.did)).getAvailable() if (!available.some((m) => `${m.provider}/${m.id}` === judge)) return back({ text: `This service can't call ${judge}.`, error: true }) } const next = { ...(judge ? { judge } : {}), ...(daily !== evals.DEFAULT_DAILY_TRIALS ? { dailyTrials: daily } : {}) } await withLock(h.entry.name, () => h.updateConfig({ evals: Object.keys(next).length ? next : undefined })) log(`${who.handle} set ${name}'s eval settings to ${JSON.stringify(next)}`) return back({ text: 'Saved the eval settings.' }) } if (action === 'run' || action === 'gate') { const trials = Number(form.get('trials') ?? 3) if (!Number.isInteger(trials) || trials < 1 || trials > 10) return back({ text: 'Trials per case must be 1 to 10.', error: true }) const only = form.get('case') const gate = action === 'gate' const cases = gate ? await evals.gateCases(readers, h.entry) : await evals.agentCases(readers, h.entry, { only: only ? [only] : [] }) if (!cases.length) return back({ text: gate ? 'There are no gated or held-out cases to run.' : 'There are no cases to run.', error: true }) let started!: (uri: string) => void let failed!: (e: unknown) => void const uri = new Promise((ok, no) => ((started = ok), (failed = no))) void evals .evalAgent(readState(), h.entry, cases, { authority: owner, agent: h.session, writer: owner }, { models: h.models, trials, gate, onStart: started }) .catch((e) => { failed(e) log(`eval run for ${name} failed: ${e?.message ?? e}`) }) const run = (await uri).split('/').pop()! log(`${who.handle} started ${gate ? 'the gate' : `a run of ${cases.length} case(s)`} for ${name} (${run})`) return back({ text: `Started: ${cases.length} case${cases.length === 1 ? '' : 's'}, ${trials} trial${trials === 1 ? '' : 's'} each. Reload to see results.` }, { run }) } return back({ text: 'Unknown action.', error: true }) } catch (e: any) { if (!(e instanceof XrpcHttpError)) log(`evals ${action} for ${name} failed: ${e?.stack ?? e}`) return back({ text: sentence(e?.message ?? 'That failed. Try again.'), error: true }) } } if (req.method !== 'GET' && req.method !== 'HEAD') return writePage(req, res, simplePage(405, 'Method not allowed', '

Use the forms on the page.

')) const cases = await evals.agentCases(readers, h.entry, { holdout: true }) const { runs: runRecs, results: allResults } = await evals.runsOf(readers, h.entry) const row = (r: { uri: string; rkey: string; value: any; by: 'owner' | 'agent' }): EvalRunRow => { const v = r.value const changes = v.changes ? [...(v.changes.systemPrompt !== undefined ? ['the system prompt'] : []), ...(v.changes.blocks ?? []).map((b: any) => `block ${b.label}`)] : undefined return { rkey: r.rkey, by: r.by, createdAt: v.createdAt, model: v.target?.model ?? '', trials: v.trials, status: v.status, ...(v.gate ? { gate: true } : {}), ...(changes?.length ? { changes } : {}), ...(v.summary ? { summary: v.summary } : {}), ...(v.holdout ? { holdout: v.holdout } : {}), ...(v.error ? { error: v.error } : {}), } } const caseByUri = new Map(cases.map((k) => [k.uri, k])) const checkDescription = (uri: string, id: string) => caseByUri.get(uri)?.value.steps.flatMap((st) => st.checks ?? []).find((k) => k.id === id)?.description const picked = url.searchParams.get('run') let run const pickedRec = picked ? runRecs.find((r) => r.rkey === picked) : undefined if (pickedRec) { const results = allResults.filter((x) => x.value.run === pickedRec.uri) run = { ...row(pickedRec), results: results .sort((a, b) => (a.rkey < b.rkey ? -1 : 1)) .map((x) => ({ case: x.value.case.uri.split('/').pop(), ...(caseByUri.get(x.value.case.uri) ? { caseName: caseByUri.get(x.value.case.uri)!.value.name, author: caseByUri.get(x.value.case.uri)!.author as 'owner' | 'agent' | 'holdout' } : {}), heldOut: evals.authorOfRef(x.value.case, h.entry) === 'holdout', trial: x.value.trial, status: x.value.status, passed: x.value.passed, ...(x.value.score !== undefined ? { score: x.value.score } : {}), durationMs: x.value.usage?.durationMs ?? 0, checks: (x.value.checks ?? []).map((k: any) => { const description = checkDescription(x.value.case.uri, k.id) return description ? { ...k, description } : k }), ...(x.value.error ? { error: x.value.error } : {}), })), } } const consume = req.method === 'GET' const done = takeNotice(who.did, url.pathname, 'done', url.searchParams.get('done'), consume) const error = takeNotice(who.did, url.pathname, 'error', url.searchParams.get('error'), consume) const runByUri = new Map(runRecs.map((r) => [r.uri, r])) // A case's trials, newest run first: results point at the case by URI, whatever its version. const trialsOf = (uri: string) => allResults .filter((x) => x.value.case?.uri === uri && runByUri.has(x.value.run)) .map((x) => ({ x, run: runByUri.get(x.value.run)! })) .sort((a, b) => (a.run.rkey === b.run.rkey ? a.x.value.trial - b.x.value.trial : a.run.rkey < b.run.rkey ? 1 : -1)) if (caseKey !== undefined) { const by = url.searchParams.get('by') const k = cases.find((c) => c.rkey === caseKey && c.author === (by === 'agent' || by === 'holdout' ? by : 'owner')) if (!k) return writePage(req, res, notFoundPage(url.pathname)) const c = k.value as any const seed: { collection: string; rkey: string; value: any }[] = c.seed ?? [] const counts = new Map() for (const r of seed) counts.set(r.collection, (counts.get(r.collection) ?? 0) + 1) const textOf = (m: any) => (m.parts ?? []).filter((p: any) => p.type === 'text').map((p: any) => p.text).join('\n') const long = (name: string, v: string) => ['code', 'rubric'].includes(name) || v.length > 120 || v.includes('\n') const show = (v: unknown) => (typeof v === 'string' ? v : JSON.stringify(v, null, 2)) return writePage( req, res, casePage({ agent: name, handle: h.entry.agent.handle, rkey: k.rkey, author: k.author as 'owner' | 'agent' | 'holdout', name: c.name, ...(c.description ? { description: c.description } : {}), tags: c.tags ?? [], gated: c.steps.some((st: any) => (st.checks ?? []).some((x: any) => !x.advisory)), ...(c.limits ? { limits: c.limits } : {}), seed: [...counts].map(([collection, count]) => ({ collection, count })), history: seed.filter((r) => r.collection === NSID.message).map((r) => ({ role: r.value.role, text: textOf(r.value) })), steps: c.steps.map((st: any) => ({ ...(st.thread ? { thread: st.thread } : {}), turns: st.turns ?? [], checks: (st.checks ?? []).map((x: any) => { const { $type, ...rest } = x.grader ?? {} return { id: x.id, ...(x.description ? { description: x.description } : {}), ...(x.advisory ? { advisory: true } : {}), ...(x.weight !== undefined ? { weight: x.weight } : {}), kind: String($type ?? '').split('#').pop() || 'check', fields: Object.entries(rest).map(([n, v]) => ({ name: n, value: show(v), long: long(n, show(v)) })), } }), })), ...(c.origin ? { origin: { ...(c.origin.thread?.uri ? { thread: c.origin.thread.uri.split('/').pop() } : {}), verdict: c.origin.verdict, ...(c.origin.note ? { note: c.origin.note } : {}), ...(c.origin.changedBlocks ? { changedBlocks: c.origin.changedBlocks } : {}) } } : {}), trials: trialsOf(k.uri).map(({ x, run }) => ({ run: run.rkey, createdAt: run.value.createdAt, model: run.value.target?.model ?? '', ...(run.value.gate ? { gate: true } : {}), trial: x.value.trial, status: x.value.status, passed: x.value.passed, ...(x.value.score !== undefined ? { score: x.value.score } : {}), durationMs: x.value.usage?.durationMs ?? 0, ...(x.value.error ? { error: x.value.error } : {}), checks: (x.value.checks ?? []).map((ck: any) => { const description = checkDescription(k.uri, ck.id) return description ? { ...ck, description } : ck }), transcript: x.value.transcript ?? [], })), ...(error ? { notice: { text: error, error: true } } : done ? { notice: { text: done } } : {}), }), ) } const lastOf = (uri: string) => { const all = trialsOf(uri) if (!all.length) return undefined const latest = all.filter((t) => t.run.rkey === all[0].run.rkey) return { passed: latest.filter((t) => t.x.value.passed).length, trials: latest.length, when: all[0].run.value.createdAt as string } } const models = await (await ownerModels(who.did)).getAvailable() const state = readState() return writePage( req, res, evalsPage({ agent: name, handle: h.entry.agent.handle, cases: cases.map((k) => ({ rkey: k.rkey, name: k.value.name, author: k.author as 'owner' | 'agent' | 'holdout', ...(k.value.description ? { description: k.value.description } : {}), gated: k.value.steps.some((st) => (st.checks ?? []).some((x) => !x.advisory)), ...(lastOf(k.uri) ? { last: lastOf(k.uri) } : {}), })), runs: runRecs.slice(0, 20).map(row), ...(run ? { run } : {}), ...(h.config.evals?.judge ? { judge: h.config.evals.judge } : {}), ...(evals.judgeFor(state, {}) ? { judgeDefault: evals.judgeFor(state, {}) } : {}), dailyTrials: h.config.evals?.dailyTrials ?? evals.DEFAULT_DAILY_TRIALS, usedToday: await evals.trialsToday(h.session, h.entry), models: models.map((m) => ({ value: `${m.provider}/${m.id}`, name: `${m.name} (${m.provider})` })), ...(error ? { notice: { text: error, error: true } } : done ? { notice: { text: done } } : {}), }), ) } async function discordRequest(url: URL, req: http.IncomingMessage, res: http.ServerResponse, ip: string) { const who = logins ? webSession(req.headers.cookie) : undefined const retry = pageLimit(ip, who) if (retry) return tooMany(res, retry) if (!who) { res.writeHead(303, { location: '/', 'cache-control': 'no-store' }) return res.end() } // /discord lists the bots; /discord/ is one bot's page. const app = /^\/discord\/(\d{5,25})$/.exec(url.pathname)?.[1] if (url.pathname !== '/discord' && !app) return writePage(req, res, notFoundPage(url.pathname)) // After an action, back to the page it came from, saying what happened. const back = (notice: { text: string; error?: boolean }, to = app ? `/discord/${app}` : '/discord') => { issueNotice(who.did, to, notice.error ? 'error' : 'done', notice.text) res.writeHead(303, { location: `${to}?${new URLSearchParams(notice.error ? { error: notice.text } : { done: notice.text })}`, 'cache-control': 'no-store' }) res.end() } const agents = () => agentsOf(readState(), who.did).map((name) => ({ name, handle: handleOf(name) })) if (req.method === 'POST') { if (!logins!.sameOrigin(req.headers.origin)) return writePage(req, res, simplePage(403, 'Not from here', "

This request didn't come from this service's own page.

")) if (!/^application\/x-www-form-urlencoded(;|$)/i.test(req.headers['content-type'] ?? '')) return writePage(req, res, simplePage(400, 'Bad request', '

Send the form.

')) const form = new URLSearchParams(await readText(req, MAX_BODY.register)) const action = form.get('action') const agent = form.get('agent') ?? '' const mine = agentsOf(readState(), who.did) try { if (!app) { if (action !== 'add') return back({ text: 'Unknown action.', error: true }) if (!mine.includes(agent)) return back({ text: `You have no agent named ${agent}.`, error: true }) const bot = await discord.add(who.did, form.get('token') ?? '', agent) log(`${who.handle} added Discord bot ${bot.name} for ${agent}`) return back({ text: `Added ${bot.name} as ${handleOf(agent)}. Next: Add to a server.` }, `/discord/${bot.appId}`) } const current = discord.list(who.did).find((b) => b.appId === app) if (!current) return back({ text: 'There is no such bot.', error: true }, '/discord') if (action === 'agent') { if (!mine.includes(agent)) return back({ text: `You have no agent named ${agent}.`, error: true }) const changed = discord.setAgent(who.did, app, agent) return back({ text: changed ? `Saved: it's ${handleOf(agent)} in new threads.` : 'No change.' }) } if (action === 'computers') { const wanted = new Set(form.getAll('computer')) const have = await agentComputers(who.did, current.agent) for (const c of have) { if (wanted.has(c.name) && !c.allowed) await attachComputer(who.did, c.name, [current.agent]) if (!wanted.has(c.name) && c.allowed) await detachComputer(who.did, c.name, [current.agent]) } const now = (await agentComputers(who.did, current.agent)).filter((c) => c.allowed).map((c) => c.name) log(`${who.handle} set ${current.agent}'s computers to ${now.join(', ') || 'none'} from /discord`) return back({ text: now.length ? `Saved: ${handleOf(current.agent)} may use ${now.join(', ')}.` : `Saved: ${handleOf(current.agent)} may use no computers.` }) } if (action === 'options') { discord.setOptions(who.did, app, { listen: form.get('listen') === 'on', threads: form.get('threads') !== 'off' }) return back({ text: 'Saved.' }) } if (action === 'token') { const bot = await discord.add(who.did, form.get('token') ?? '', current.agent) if (bot.appId !== app) return back({ text: `That token is for ${bot.name}, another bot; it's added as its own.`, error: true }, `/discord/${bot.appId}`) return back({ text: 'Saved the new token and reconnected.' }) } if (action === 'remove') { const gone = discord.remove(who.did, app) if (gone) log(`${who.handle} removed Discord bot ${gone.name}`) return back({ text: gone ? `Removed ${gone.name}.` : 'There was no such bot.' }, '/discord') } return back({ text: 'Unknown action.', error: true }) } catch (e: any) { if (!(e instanceof UserError)) log(`/discord ${action} failed: ${e?.stack ?? e}`) return back({ text: e instanceof UserError ? e.message : 'That failed. Try again.', error: true }) } } else if (req.method !== 'GET' && req.method !== 'HEAD') return writePage(req, res, simplePage(405, 'Method not allowed', '

Use the forms on the page.

')) const consume = req.method === 'GET' const done = takeNotice(who.did, url.pathname, 'done', url.searchParams.get('done'), consume) const error = takeNotice(who.did, url.pathname, 'error', url.searchParams.get('error'), consume) const notice = error ? { notice: { text: error, error: true } } : done ? { notice: { text: done } } : {} if (!app) return writePage(req, res, discordPage({ bots: discord.list(who.did), agents: agents(), ...notice })) const d = await discord.detail(who.did, app) if (!d) return writePage(req, res, notFoundPage(url.pathname)) return writePage(req, res, discordBotPage({ ...d, agents: agents(), computers: await agentComputers(who.did, d.bot.agent), ...notice })) } /** * `/admin`: owners' model budgets and new invites, for the installation owner only. Anyone else, * signed in or not, gets the not-found page, so the page doesn't say it exists. Changes are * same-origin form posts, like the other pages. */ /** /errors: the error reports owners shared with whoever is signed in. */ async function sharedErrorsRequest(url: URL, req: http.IncomingMessage, res: http.ServerResponse, ip: string) { const who = logins ? webSession(req.headers.cookie) : undefined const retry = pageLimit(ip, who) if (retry) return tooMany(res, retry) if (!who) { res.writeHead(303, { location: '/', 'cache-control': 'no-store' }) return res.end() } if (req.method !== 'GET' && req.method !== 'HEAD') return writePage(req, res, simplePage(405, 'Method not allowed', '

This page only shows reports.

')) const agents = await sharedWith(readsAs(who.did), 100) const all = agents.flatMap((a) => a.reports.map((r) => ({ ...r, handle: a.handle }))).sort((a, b) => (a.createdAt < b.createdAt ? 1 : -1)) const rows = all.length ? `
    ` + all .map( (r) => `
  • ${esc(r.name)} ${esc(r.source)} ${esc(r.message)}` + `${esc(r.handle)} · ${esc(r.createdAt.replace('T', ' ').slice(0, 16))} UTC${r.method ? ` · ${esc(r.method)}` : ''}${r.provider ? ` · ${esc(r.provider)}/${esc(r.model ?? '')}` : ''}${r.version ? ` · ${esc(r.version)}` : ''}${r.platform ? ` · ${esc(r.platform)}` : ''}` + (r.stack ? `
    Stack
    ${esc(r.stack)}
    ` : '') + `
  • `, ) .join('') + `
` : '

None yet.

' const css = '' const feedback = agents.flatMap((a) => a.feedback.map((f) => ({ ...f, handle: a.handle }))).sort((a, b) => (a.createdAt < b.createdAt ? 1 : -1)) const feedbackRows = feedback.length ? `

Feedback

    ` + feedback .map( (f) => `
  • ${esc(f.kind)} ${esc(f.text)}` + `${esc(f.handle)} · ${esc(f.createdAt.replace('T', ' ').slice(0, 16))} UTC${f.provider ? ` · ${esc(f.provider)}/${esc(f.model ?? '')}` : ''}${f.version ? ` · ${esc(f.version)}` : ''}
  • `, ) .join('') + `
` : '' const intro = `

${all.length} report${all.length === 1 ? '' : 's'} and ${feedback.length} feedback from ${agents.length} agent${agents.length === 1 ? '' : 's'}, shared with you.

` return writePage(req, res, page(200, 'Error reports · Languid', `${css}

Error reports

${intro}${feedbackRows}${feedback.length ? '

Errors

' : ''}${rows}`, { app: true, path: '/errors' })) } /** `/admin/logs`: the ops events, for the installation owner only (everyone else gets not-found). */ async function adminLogsRequest(url: URL, req: http.IncomingMessage, res: http.ServerResponse, ip: string) { const who = logins ? webSession(req.headers.cookie) : undefined const retry = pageLimit(ip, who) if (retry) return tooMany(res, retry) const install = installOwner(readState()) if (!who || !install || who.did !== install) return writePage(req, res, notFoundPage(url.pathname)) if (req.method !== 'GET' && req.method !== 'HEAD') return writePage(req, res, simplePage(405, 'Method not allowed', '

This page only shows logs.

')) const level = url.searchParams.get('level') ?? undefined const kind = url.searchParams.get('kind') ?? undefined const { events, fromMemory } = await ops.list() return writePage(req, res, adminLogsPage({ events, fromMemory, ...(level && (LEVELS as readonly string[]).includes(level) ? { level } : {}), ...(kind && (KINDS as readonly string[]).includes(kind) ? { kind } : {}) })) } /** `/admin/feedback`: what agents sent the maintainer, from every agent whose owner shares it. Installation owner only. */ async function adminFeedbackRequest(url: URL, req: http.IncomingMessage, res: http.ServerResponse, ip: string) { const who = logins ? webSession(req.headers.cookie) : undefined const retry = pageLimit(ip, who) if (retry) return tooMany(res, retry) const install = installOwner(readState()) if (!who || !install || who.did !== install) return writePage(req, res, notFoundPage(url.pathname)) if (req.method !== 'GET' && req.method !== 'HEAD') return writePage(req, res, simplePage(405, 'Method not allowed', '

This page only shows feedback.

')) const agents = await sharedWith(maintainerDids(), 200) const feedback = agents.flatMap((a) => a.feedback.map((f) => ({ ...f, handle: a.handle }))).sort((a, b) => (a.createdAt < b.createdAt ? 1 : -1)) const kind = url.searchParams.get('kind') ?? undefined const notSharing = Object.keys(readState().agents).length - agents.length return writePage(req, res, adminFeedbackPage({ feedback, sharing: agents.length, notSharing, destination: TELEMETRY_RECIPIENT(), ...(kind ? { kind } : {}) })) } /** `/admin/messages`: send a message to owners' Inboxes from the ops account, or delete one. Installation owner only. */ async function adminMessagesRequest(url: URL, req: http.IncomingMessage, res: http.ServerResponse, ip: string) { const who = logins ? webSession(req.headers.cookie) : undefined const retry = pageLimit(ip, who) if (retry) return tooMany(res, retry) const install = installOwner(readState()) if (!who || !install || who.did !== install) return writePage(req, res, notFoundPage(url.pathname)) const view = async (notice?: { text: string; error?: boolean }) => { const state = readState() const owners = Object.entries(state.owners ?? {}).map(([did, o]) => ({ did, handle: o.handle })) if (state.owner && !owners.some((o) => o.did === state.owner!.did)) owners.push({ did: state.owner.did, handle: state.owner.handle }) owners.sort((a, b) => a.handle.localeCompare(b.handle)) const messages = await opsMessages.all() return adminMessagesPage({ messages, dismissed: opsMessages.dismissals(), owners, from: readState().ops?.handle, ...(notice ? { notice } : {}) }) } if (req.method === 'POST') { if (!logins!.sameOrigin(req.headers.origin)) return writePage(req, res, simplePage(403, 'Not from here', "

This request didn't come from this service's own page.

")) if (!/^application\/x-www-form-urlencoded(;|$)/i.test(req.headers['content-type'] ?? '')) return writePage(req, res, simplePage(400, 'Bad request', '

Send the form.

')) const form = new URLSearchParams(await readText(req, 64 * 1024)) const action = form.get('action') try { if (action === 'send') { const to = form.get('to') || undefined const state = readState() if (to && !state.owners?.[to] && state.owner?.did !== to) return writePage(req, res, await view({ text: 'Pick someone who has signed up.', error: true })) const title = form.get('title') ?? '', body = form.get('body') ?? '' if (!title.trim() || !body.trim()) return writePage(req, res, await view({ text: 'Write a title and a message.', error: true })) if (!state.ops) await opsAuthority(readState) const m = await opsMessages.send({ title, body, ...(to ? { to } : {}) }) log(`messages: sent ${m.rkey} to ${to ?? 'everyone'}`) return writePage(req, res, await view({ text: to ? `Sent to ${state.owners?.[to]?.handle ?? state.owner?.handle ?? to}.` : 'Sent to everyone.' })) } if (action === 'delete') { await opsMessages.remove(form.get('message') ?? '') return writePage(req, res, await view({ text: 'Deleted.' })) } } catch (e: any) { log(`messages: ${action} failed: ${e?.stack ?? e}`) return writePage(req, res, await view({ text: sentence(e?.message ?? 'That failed. Try again.'), error: true })) } return writePage(req, res, await view({ text: 'Pick Send or Delete.', error: true })) } if (req.method !== 'GET' && req.method !== 'HEAD') return writePage(req, res, simplePage(405, 'Method not allowed', '

Use the form on the page.

')) return writePage(req, res, await view()) } async function adminRequest(url: URL, req: http.IncomingMessage, res: http.ServerResponse, ip: string) { const who = logins ? webSession(req.headers.cookie) : undefined const retry = pageLimit(ip, who) if (retry) return tooMany(res, retry) const install = installOwner(readState()) if (!who || !install || who.did !== install) return writePage(req, res, notFoundPage(url.pathname)) const view = async (extra: { notice?: { text: string; error?: boolean }; invite?: AdminView["invite"] } = {}) => { const events = await funnel.list().catch((e: any) => { log(`funnel: couldn't read: ${e?.message ?? e}`) return undefined }) const state = readState() const owners = Object.entries(state.owners ?? {}) .map(([did, o]) => { const b = state.billing?.[did] const kind = (sub?: { id: string; status: string }) => (!sub || !isActive(sub) ? undefined : isComp(sub) ? ('given' as const) : ('paid' as const)) return { ...ownerSummary(did, o), agents: agentsOf(state, did).map((name) => ({ name, handle: state.agents[name].agent.handle })), sandbox: kind(b?.sandbox), volume: kind(b?.volume) } }) .sort((a, b) => a.handle.localeCompare(b.handle)) const openInvites = openInviteUses(state) const funnelOwners = Object.entries(state.owners ?? {}).map(([did, o]) => ({ did, handle: o.handle, createdAt: o.createdAt, agents: agentsOf(state, did).length > 0 })) return adminPage({ owners, openInvites, sandboxes: !!sandboxes, ...(events ? { funnel: funnelRows(funnelOwners, events) } : {}), ...extra }) } const back = (text: string, error = false) => { issueNotice(who.did, '/admin', error ? 'error' : 'done', text) res.writeHead(303, { location: `/admin?${new URLSearchParams(error ? { error: text } : { done: text })}`, 'cache-control': 'no-store' }) res.end() } if (req.method === 'POST') { if (!logins!.sameOrigin(req.headers.origin)) return writePage(req, res, simplePage(403, 'Not from here', "

This request didn't come from this service's own page.

")) if (!/^application\/x-www-form-urlencoded(;|$)/i.test(req.headers['content-type'] ?? '')) return writePage(req, res, simplePage(400, 'Bad request', '

Send the form.

')) const form = new URLSearchParams(await readText(req, MAX_BODY.register)) const action = form.get('action') if (action === 'invite') { const typed = (form.get('usd') ?? '').trim() const budgetUsd = typed ? parseDollars(typed) : undefined if (typed && budgetUsd === undefined) return back('The budget must be a dollar amount, like 10 or 7.50.', true) const days = Number((form.get('days') ?? '').trim() || '7') if (!Number.isInteger(days) || days < 1 || days > 90) return back('The link can last 1 to 90 days.', true) const uses = Number((form.get('uses') ?? '').trim() || '1') if (!Number.isInteger(uses) || uses < 1 || uses > MAX_INVITE_USES) return back(`One link can sign up 1 to ${MAX_INVITE_USES} people.`, true) const state = readState() const { code, invite } = newOwnerInvite(state, { ttlDays: days, budgetUsd, uses }) saveState(state) log(`owner invite made from /admin (expires ${invite.expiresAt}${budgetUsd !== undefined ? `, budget $${budgetUsd}` : ''})`) // Shown in this response, never put in a redirect's URL: the code is the invite. const base = 'origin' in signIn ? signIn.origin : (opts.url ?? '') const link = `${base}/signup?code=${code}` const p = await view({ invite: { url: link, expiresAt: invite.expiresAt, budgetUsd, uses, qr: await inviteQr(link) } }) return writePage(req, res, { ...p, headers: { ...p.headers, 'cache-control': 'no-store' } }) } if (action === 'give' || action === 'take') { const plan = form.get('plan') const state = readState() const did = findOwnerDid(state, form.get('owner')) if (!did || !isPlan(plan)) return back('There is no such owner or plan.', true) const b = ((state.billing ??= {})[did] ??= {}) const live = b[plan] if (action === 'give') { if (isActive(live)) return back(`${state.owners![did].handle} already has one.`, true) if (plan === 'volume' && !isActive(b.sandbox)) return back('Give a sandbox first.', true) b[plan] = { id: COMP, status: 'active' } as any } else { if (!isComp(live)) return back('Only a plan you gave can be taken back here; paid ones are cancelled in Stripe.', true) b[plan] = { ...live!, status: 'canceled', endedAt: new Date().toISOString() } as any } saveState(state) await afterBilling(did) log(`${who.handle} ${action === 'give' ? 'gave' : 'took back'} a ${plan} for ${state.owners![did].handle}`) return back(`${state.owners![did].handle}: ${plan} ${action === 'give' ? 'given' : 'taken back'}.`) } if (action !== 'add' && action !== 'set' && action !== 'unlimited') return back('Unknown action.', true) const amount = action === 'unlimited' ? undefined : parseDollars(form.get('usd')) if (action !== 'unlimited' && amount === undefined) return back('Type a dollar amount, like 5 or 2.50.', true) const state = readState() const did = findOwnerDid(state, form.get('owner')) if (!did) return back('There is no such owner.', true) const o = state.owners![did] const spent = o.spentUsd ?? 0 if (action === 'add') { grantCredit(state, `admin:${crypto.randomUUID()}`, { did, usd: amount!, source: 'admin', at: new Date().toISOString() }) o.budgetUsd = Math.min(100_000, o.budgetUsd!) } else if (action === 'set') o.budgetUsd = amount else o.budgetUsd = undefined saveState(state) log(`${who.handle} set ${o.handle}'s budget to ${o.budgetUsd === undefined ? 'none' : `$${o.budgetUsd}`} from /admin`) return back(o.budgetUsd === undefined ? `${o.handle} is unlimited now.` : `${o.handle}'s budget is $${o.budgetUsd.toFixed(2)} now, with $${Math.max(0, o.budgetUsd - spent).toFixed(2)} left.`) } else if (req.method !== 'GET' && req.method !== 'HEAD') return writePage(req, res, simplePage(405, 'Method not allowed', '

Use the forms on the page.

')) const consume = req.method === 'GET' const done = takeNotice(who.did, '/admin', 'done', url.searchParams.get('done'), consume) const error = takeNotice(who.did, '/admin', 'error', url.searchParams.get('error'), consume) return writePage(req, res, await view(error ? { notice: { text: error, error: true } } : done ? { notice: { text: done } } : {})) } /** * `/billing`: model credit and the monthly plans (cloud sandbox, volume). Credit and subscriptions * change only when Stripe's webhook says so, so coming back from Stripe just says it's on its way. */ async function billingRequest(url: URL, req: http.IncomingMessage, res: http.ServerResponse, ip: string) { const who = logins ? webSession(req.headers.cookie) : undefined const retry = pageLimit(ip, who) if (retry) return tooMany(res, retry) if (!who) { res.writeHead(303, { location: '/', 'cache-control': 'no-store' }) return res.end() } const stripe = stripeConfig() const origin = 'origin' in signIn ? signIn.origin : (opts.url ?? '') const back = (text: string, error = false) => { issueNotice(who.did, '/billing', error ? 'error' : 'done', text) res.writeHead(303, { location: `/billing?${new URLSearchParams(error ? { error: text } : { done: text })}`, 'cache-control': 'no-store' }) res.end() } const checkout = (url: string) => { res.writeHead(303, { location: url, 'cache-control': 'no-store' }) res.end() } if (req.method === 'POST') { if (!logins!.sameOrigin(req.headers.origin)) return writePage(req, res, simplePage(403, 'Not from here', "

This request didn't come from this service's own page.

")) const form = new URLSearchParams(await readText(req, MAX_BODY.register)) const action = form.get('action') const state = readState() if (!stripe) return back('Buying isn\'t open yet.', true) try { if (action === 'credit') { const usd = creditAmount(form.get('usd')) const o = state.owners?.[who.did] if (!o) return back("Can't buy that.", true) if (usd === undefined) return back(`Pick a whole amount from $${CREDIT_MIN} to $${CREDIT_MAX}.`, true) const session = await createCreditCheckout(stripe, { did: who.did, handle: who.handle, usd, origin, customer: customerOf(state, who.did) }) log(`${who.handle} started a $${usd} credit checkout (${session.id})`) return checkout(session.url) } if (action === 'portal') { const customer = customerOf(state, who.did) if (!customer) return back('Nothing to manage yet: you haven\'t paid by card here.', true) return checkout(await createPortalSession(stripe, customer, `${origin}/billing`)) } const plan = form.get('plan') if (!isPlan(plan)) return back('Unknown plan.', true) const b = state.billing?.[who.did] const live = b?.[plan] if (action === 'subscribe') { if (!sandboxes) return back("Cloud sandboxes aren't offered here.", true) if (isActive(live)) return back(`You already have ${PLANS[plan].name.replace(/^Languid /, 'a ')}.`, true) if (plan === 'volume' && !isActive(b?.sandbox)) return back('A volume needs a cloud sandbox.', true) const session = await createSubscriptionCheckout(stripe, { did: who.did, handle: who.handle, plan, origin, customer: customerOf(state, who.did) }) log(`${who.handle} started a ${plan} subscription checkout (${session.id})`) return checkout(session.url) } if (action === 'cancel' || action === 'resume') { if (!live || !isActive(live)) return back('Nothing to change.', true) if (isComp(live)) return back('Languid gave you this; nothing to cancel.', true) const cancel = action === 'cancel' await setCancelAtPeriodEnd(stripe, live.id, cancel) const after = readState() const entry = after.billing?.[who.did]?.[plan] if (entry) entry.cancelAtPeriodEnd = cancel saveState(after) log(`${who.handle} ${cancel ? 'cancelled' : 'kept'} their ${plan} subscription`) return back(cancel ? `It stops ${live.periodEnd ? `on ${new Date(live.periodEnd).toLocaleDateString('en-US', { month: 'long', day: 'numeric', timeZone: 'UTC' })}` : 'at the end of this month'}.` : 'It renews as before.') } return back('Unknown action.', true) } catch (e: any) { log(`billing for ${who.handle} failed: ${e?.message ?? e}`) return back(`Stripe didn't take that: ${e?.message ?? e}`, true) } } else if (req.method !== 'GET' && req.method !== 'HEAD') return writePage(req, res, simplePage(405, 'Method not allowed', '

Use the forms on the page.

')) const state = readState() const o = state.owners?.[who.did] const b = state.billing?.[who.did] const key = runnerKey(state, who.did, SANDBOX_COMPUTER) const consume = req.method === 'GET' const done = takeNotice(who.did, '/billing', 'done', url.searchParams.get('done'), consume) const error = takeNotice(who.did, '/billing', 'error', url.searchParams.get('error'), consume) const subscribed = url.searchParams.get('subscribed') const notice = error ? { text: error, error: true } : done ? { text: done } : url.searchParams.get('paid') ? { text: 'Paid. Your credit shows here once Stripe confirms it, usually within a few seconds.' } : isPlan(subscribed) ? { text: `Subscribed. It shows here once Stripe confirms it, usually within a few seconds.` } : undefined return writePage( req, res, billingPage({ notice, stripe: !!stripe, tax: !!stripe?.tax, credit: o?.budgetUsd === undefined ? undefined : { left: Math.max(0, o.budgetUsd - (o.spentUsd ?? 0)), budget: o.budgetUsd, spent: o.spentUsd ?? 0 }, markupPct: Math.round(MANAGED_MARKUP * 100), grants: grantsOf(state, who.did), sandboxes: !!sandboxes, sandbox: b?.sandbox ? { ...b.sandbox, hoursUsed: sandboxHours(b), hoursIncluded: INCLUDED_HOURS, state: sandboxStarting.has(who.did) ? 'starting' : key && hub.online(key) ? 'running' : 'asleep' } : undefined, volume: b?.volume, portal: !!customerOf(state, who.did), }), ) } /** `/usage`: the signed-in owner's model calls over the last 7, 30 or 90 days. */ function usageRequest(url: URL, req: http.IncomingMessage, res: http.ServerResponse, ip: string) { const who = logins ? webSession(req.headers.cookie) : undefined const retry = pageLimit(ip, who) if (retry) return tooMany(res, retry) if (!who) { res.writeHead(303, { location: '/', 'cache-control': 'no-store' }) return res.end() } if (req.method !== 'GET' && req.method !== 'HEAD') return writePage(req, res, simplePage(405, 'Method not allowed', '

This page is only for reading.

')) const asked = Number(url.searchParams.get('days')) const range = (USAGE_RANGES as readonly number[]).includes(asked) ? asked : 30 const state = readState() const o = state.owners?.[who.did] const summary = summarizeUsage(state, who.did, range) const agentNames = Object.fromEntries([...new Set([...agentsOf(state, who.did), ...summary.byAgent.map((a) => a.name)])].filter((n) => state.agents[n]).map((n) => [n, displayNameOf(state.agents[n])])) const credit = o?.budgetUsd === undefined ? undefined : { left: Math.max(0, o.budgetUsd - (o.spentUsd ?? 0)), budget: o.budgetUsd } return writePage(req, res, usagePage({ range, summary, credit, agentNames, markupPct: Math.round(MANAGED_MARKUP * 100) })) } /** `/inbox`: every call the owner's agents wait on their yes for, with Allow and Deny. */ async function inboxRequest(url: URL, req: http.IncomingMessage, res: http.ServerResponse, ip: string) { const who = logins && 'origin' in signIn ? webSession(req.headers.cookie) : undefined const retry = pageLimit(ip, who) if (retry) return tooMany(res, retry) if (!who) { res.writeHead(303, { location: '/', 'cache-control': 'no-store' }) return res.end() } const back = (notice: { text: string; error?: boolean }, to?: string | null) => { const q = new URLSearchParams(notice.error ? { error: notice.text } : { done: notice.text }) const path = to && to.startsWith('/') && !to.startsWith('//') && !/[\r\n]/.test(to) ? to : '/inbox' const targetPath = new URL(path, 'http://x').pathname issueNotice(who.did, targetPath, notice.error ? 'error' : 'done', notice.text) res.writeHead(303, { location: `${path}${path.includes('?') ? '&' : '?'}${q}`, 'cache-control': 'no-store' }) res.end() } if (req.method === 'POST') { if (!logins!.sameOrigin(req.headers.origin)) return writePage(req, res, simplePage(403, 'Not from here', "

This request didn't come from this service's own page.

")) if (!/^application\/x-www-form-urlencoded(;|$)/i.test(req.headers['content-type'] ?? '')) return writePage(req, res, simplePage(400, 'Bad request', '

Send the form.

')) const form = new URLSearchParams(await readText(req, 16 * 1024)) const agent = form.get('agent') ?? '' const action = form.get('action') if (action === 'dismiss') { try { if (!(await opsMessages.dismiss(who.did, form.get('message') ?? ''))) return back({ text: 'No such message.', error: true }) return back({ text: 'Dismissed.' }) } catch (e: any) { log(`inbox: dismissing a message for ${who.handle} failed: ${e?.message ?? e}`) return back({ text: 'That failed. Try again.', error: true }) } } const agents = readState().agents const known = Object.hasOwn(agents, agent) ? agents[agent] : undefined if (!known || known.owner !== who.did) return back({ text: 'No such agent.', error: true }) if (action !== 'allow' && action !== 'deny') return back({ text: 'Pick Allow or Deny.', error: true }) try { await answerApproval(await store(agent, who.did), form.get('request'), action === 'allow', { by: who.did, via: 'web' }) log(`inbox: ${who.handle} ${action === 'allow' ? 'approved' : 'denied'} ${form.get('request')} for ${agent}`) return back({ text: action === 'allow' ? 'Allowed.' : 'Denied.' }, form.get('back')) } catch (e: any) { if (e instanceof XrpcHttpError && e.error === 'NotWaiting') return back({ text: 'That one already has an answer, or its time ran out.', error: true }, form.get('back')) if (!(e instanceof XrpcHttpError)) log(`inbox ${action} for ${agent} failed: ${e?.stack ?? e}`) return back({ text: sentence(e?.message ?? 'That failed. Try again.'), error: true }, form.get('back')) } } if (req.method !== 'GET' && req.method !== 'HEAD') return writePage(req, res, simplePage(405, 'Method not allowed', '

Use the buttons on the page.

')) const state = readState() const items: PendingApproval[] = [] for (const name of agentsOf(state, who.did)) { try { const h = await store(name, who.did) for (const r of pendingRequests(h)) { items.push({ uri: r.uri, agent: name, agentName: displayNameOf(state.agents[name]), thread: r.value.thread, threadTitle: h.threads.find((t) => t.rkey === r.value.thread)?.value.title, ...(r.value.machine ? { machine: { rkey: r.value.machine.uri.slice(r.value.machine.uri.lastIndexOf('/') + 1), name: h.recordsOf(NSID.machine).find((m) => m.uri === r.value.machine!.uri)?.value.name } } : {}), tool: r.value.method ?? r.value.tool, arguments: r.value.arguments, ...(r.value.computer ? { computer: r.value.computer } : {}), createdAt: r.value.createdAt, }) } } catch (e: any) { log(`inbox: ${name}'s approvals not read: ${e?.message ?? e}`) } } items.sort((a, b) => b.createdAt.localeCompare(a.createdAt)) const messages = state.ops ? await opsMessages.for(who.did, state.owners?.[who.did]?.createdAt) : [] const consume = req.method === 'GET' const done = takeNotice(who.did, '/inbox', 'done', url.searchParams.get('done'), consume) const error = takeNotice(who.did, '/inbox', 'error', url.searchParams.get('error'), consume) return writePage(req, res, inboxPage({ items, messages, now: Date.now(), notice: error ? { text: error, error: true } : done ? { text: done } : undefined })) } /** Stripe's webhook: verified by its signature over the raw body, then applied to the credit ledger. */ async function stripeWebhook(req: http.IncomingMessage, res: http.ServerResponse, ip: string) { const send = (status: number, body: unknown) => { if (res.headersSent || res.destroyed) return res.writeHead(status, { 'content-type': 'application/json', 'x-content-type-options': 'nosniff', 'cache-control': 'no-store' }) res.end(JSON.stringify(body)) } const retry = limit('oauth', ip) if (retry) return tooMany(res, retry) const stripe = stripeConfig() if (!stripe) return send(404, { error: 'NotFound' }) if (req.method !== 'POST') return send(405, { error: 'MethodNotAllowed' }) const raw = await readText(req, 512 * 1024) if (!verifyStripeSignature(raw, req.headers['stripe-signature'] as string | undefined, stripe.webhookSecret)) return send(400, { error: 'BadSignature' }) let event: any try { event = JSON.parse(raw) } catch { return send(400, { error: 'BadJson' }) } // One at a time, so two deliveries of one event can't both grant. const outcome = await withLock('credits', async () => { const state = readState() const what = applyStripeEvent(state, event) if (!what.startsWith('ignored') && !what.startsWith('nothing') && !what.startsWith('waiting')) saveState(state) return what }) log(`stripe ${event?.type} ${event?.id}: ${outcome}`) const subscriber = subscriptionEventOwner(event) if (subscriber) await afterBilling(subscriber.did).catch((e) => log(`after billing for ${subscriber.did}: ${e?.message ?? e}`)) return send(200, { received: true }) } /** * Model connections: the owner's own ChatGPT plan, SuperGrok / X plan, Claude (Anthropic API) key and OpenRouter account. * Website only: a web session and same-origin forms, plus OpenRouter's sign-in coming back to * OPENROUTER_CALLBACK. Pages never carry a credential, only a key's last four characters. */ async function modelConnectionsRequest(req: http.IncomingMessage, res: http.ServerResponse, ip: string, url: URL) { const who = logins ? webSession(req.headers.cookie) : undefined const retry = pageLimit(ip, who) if (retry) return tooMany(res, retry) if (!who) { res.writeHead(303, { location: '/', 'cache-control': 'no-store' }) return res.end() } const view = async (status = 200, claudeError?: string, openrouterError?: string, pasted = false) => { const connection = await chatgpt.status(who.did) const form = (action: string, label: string) => `
` let gpt = '

Use your ChatGPT plan.

' if (connection.status === 'connected') gpt += '

Connected.

' + form('disconnect', 'Disconnect ChatGPT') else if (connection.status === 'pending') gpt += (connection.code ? `

Enter this on OpenAI’s device page:

${esc(connection.code)}
` : '

Requesting a code from OpenAI…

') + '

Expires in 15 minutes. Check connection

' + form('disconnect', 'Cancel') else gpt += (connection.status === 'error' ? '

Couldn’t connect. The code may have expired, or device-code sign-in is off in ChatGPT’s security settings.

' : '') + form('connect', 'Connect ChatGPT') const grokConnection = await chatgpt.status(who.did, XAI) let grok = '

Use your SuperGrok or X Premium plan.

' if (grokConnection.status === 'connected') grok += '

Connected.

' + form('grok-disconnect', 'Disconnect Grok') else if (grokConnection.status === 'pending') grok += (grokConnection.code ? `

Approve on xAI’s sign-in page, and enter this code if it asks:

${esc(grokConnection.code)}
` : '

Requesting a code from xAI…

') + '

Expires in 15 minutes. Check connection

' + form('grok-disconnect', 'Cancel') else grok += (grokConnection.status === 'error' ? '

Couldn’t connect. The code may have expired, or the sign-in was declined.

' : '') + form('grok-connect', 'Connect Grok') const key = claude.status(who.did) let cl = '' if (claudeError) cl += `

${esc(claudeError)}

` if (key.status === 'connected') cl += `

Connected with the key ending ${esc(key.hint)}.

` + form('claude-remove', 'Remove Claude key') else cl += `

Create a key in the Claude Console and paste it here.

` + `
` + `` + `

` const ork = openrouter.status(who.did) let or = '' if (openrouterError) or += `

${esc(openrouterError)}

` if (ork.status === 'connected') or += `

Connected with the key ending ${esc(ork.hint)}.

` + form('openrouter-remove', 'Remove OpenRouter key') else or += form('openrouter-connect', 'Connect OpenRouter') + `Paste a key instead` + `

Create a key in OpenRouter’s settings.

` + `
` + `` + `

` return writePage(req, res, page(status, 'Model connections · Languid', `

Model connections

ChatGPT

${gpt}

Grok

${grok}

Claude

${cl}

OpenRouter

${or}`, { app: true, headers: connection.status === 'pending' || grokConnection.status === 'pending' ? { refresh: '3; url=/settings/models' } : {} })) } try { if (req.method === 'POST') { if (!logins!.sameOrigin(req.headers.origin)) throw new XrpcHttpError(403, 'Forbidden', 'This request must come from Languid.') if (!/^application\/x-www-form-urlencoded(;|$)/i.test(req.headers['content-type'] ?? '')) throw invalid('send a form') const form = new URLSearchParams(await readText(req, MAX_BODY.register)) const action = form.get('action') if (action === 'connect') { const retry = limit('signIn', who.did) if (retry) return tooMany(res, retry) await chatgpt.start(who.did) } else if (action === 'disconnect') await chatgpt.disconnect(who.did) else if (action === 'grok-connect') { const retry = limit('signIn', who.did) if (retry) return tooMany(res, retry) await chatgpt.start(who.did, XAI) } else if (action === 'grok-disconnect') await chatgpt.disconnect(who.did, XAI) else if (action === 'claude-save') { const retry = limit('signIn', who.did) if (retry) return tooMany(res, retry) try { await claude.save(who.did, form.get('key') ?? '', await chatgpt.runtime(who.did)) } catch (e) { if (e instanceof KeyError) return view(400, e.message) throw e } } else if (action === 'claude-remove') await claude.remove(who.did, await chatgpt.runtime(who.did)) else if (action === 'openrouter-save') { const retry = limit('signIn', who.did) if (retry) return tooMany(res, retry) try { await openrouter.save(who.did, form.get('key') ?? '', await chatgpt.runtime(who.did)) } catch (e) { if (e instanceof KeyError) return view(400, undefined, e.message, true) throw e } } else if (action === 'openrouter-connect') { const retry = limit('signIn', who.did) if (retry) return tooMany(res, retry) const origin = 'origin' in signIn ? signIn.origin : opts.url if (!origin) throw new XrpcHttpError(503, 'Unavailable', 'This service has no public URL for OpenRouter to return to.') const started = openrouter.begin(who.did, origin + OPENROUTER_CALLBACK) const stateCookie = `${OPENROUTER_STATE_COOKIE}=${started.state}; Path=${OPENROUTER_CALLBACK}; Max-Age=600; HttpOnly; SameSite=Lax${isHttps ? '; Secure' : ''}` res.writeHead(303, { location: started.url, 'set-cookie': stateCookie, 'cache-control': 'no-store' }) return res.end() } else if (action === 'openrouter-remove') await openrouter.remove(who.did, await chatgpt.runtime(who.did)) else throw invalid('unknown action') res.writeHead(303, { location: '/settings/models', 'cache-control': 'no-store' }) return res.end() } if (req.method !== 'GET' && req.method !== 'HEAD') return writePage(req, res, simplePage(405, 'Method not allowed', '

Use the connection form.

')) if (url.pathname === OPENROUTER_CALLBACK) { // Back from OpenRouter. Only a code with a matching state nonce and cookie turns into a key. const code = url.searchParams.get('code') const state = url.searchParams.get('state') const cookieState = cookie(req.headers.cookie, OPENROUTER_STATE_COOKIE) if (!code || code.length > 512) return view(400, undefined, 'OpenRouter didn’t connect. Try again.') try { await openrouter.finish(who.did, code, await chatgpt.runtime(who.did), state, cookieState) } catch (e) { if (e instanceof KeyError) return view(400, undefined, e.message) throw e } const clearState = `${OPENROUTER_STATE_COOKIE}=; Path=${OPENROUTER_CALLBACK}; Max-Age=0; HttpOnly; SameSite=Lax${isHttps ? '; Secure' : ''}` res.writeHead(303, { location: '/settings/models', 'set-cookie': clearState, 'cache-control': 'no-store' }) return res.end() } return await view() } catch (e: any) { // Deliberately do not expose OAuth provider responses or credentials. return writePage(req, res, simplePage(e instanceof XrpcHttpError ? e.status : 500, 'Connection unavailable', '

Could not update your model connection. Please try again.

Back

')) } } /** The chat page's contents for one of the caller's agents. */ async function chatView(name: string, caller: Caller, url: URL) { const h = await store(name, caller.did) await refreshRunners(h) // Questions a restart left open get a run again, so the page can show them and take the answer. adoptAllParked(h) const available = await keepRunnable(h, caller.did) const threads = h.threadList() const wanted = url.searchParams.get('thread') const thread = url.searchParams.has('new') ? undefined : wanted ? requireThread(h, wanted) : threads[0]?.rkey const r = thread ? runs.get(activeRuns.get(runKey(name, thread)) ?? '') : undefined const live = r && r.status === 'running' ? r : undefined const decided = new Set(live?.events.flatMap((e) => (e.type === 'decision' ? [e.id] : []))) const state = readState() const o = state.owners?.[caller.did] return chatPage({ agent: name, displayName: displayNameOf(state.agents[name] ?? h.entry), handle: h.entry.agent.handle, model: (() => { const own = thread ? h.threads.find((t) => t.rkey === thread)?.value.model : undefined return own && available.some((m) => m.provider === own.provider && m.id === own.id) ? `${own.provider}/${own.id}` : `${h.config.provider}/${h.config.model}` })(), reasoning: (() => { try { return h.reasoning(thread) } catch { return 'off' } })(), models: available.map((m) => ({ provider: m.provider, id: m.id, name: m.name, cost: m.cost, connection: connectionOf(caller.did, m), ...(m.input?.includes('image') ? { image: true } : {}), ...(m.reasoning ? { levels: getSupportedThinkingLevels(m) } : {}), })), favoriteModels: state.preferences?.[caller.did]?.favoriteModels ?? [], recentModels: state.preferences?.[caller.did]?.recentModels ?? [], houseModels: houseModels(), voice: canTranscribe(), autoApproval: autoRefusal(state, caller.did) ? 'no_credit' : 'on', alwaysAllowed: h.alwaysAllowed(), computers: [...h.computers.keys()].map((m) => ({ name: m, online: h.runners.online(m) })), threads, thread, ...(thread ? chatItems(h, thread, caller.did) : { items: [], hidden: 0 }), ...(thread && !live && h.interruptedCalls(thread, bootedAt).length ? { interrupted: true } : {}), ...(live ? { run: { id: live.id, since: live.events.length, approvals: live.events.flatMap((e) => (e.type === 'approval' && !decided.has(e.id) ? [{ id: e.id, tool: e.tool, arguments: e.arguments, computer: e.computer, ...(e.always ? { always: e.always } : {}) }] : [])), }, } : {}), ...(o?.budgetUsd !== undefined ? { budget: { budgetUsd: o.budgetUsd, spentUsd: o.spentUsd ?? 0 } } : {}), }) } let domainCache: string | undefined /** The handle suffix new owners get: the PDS's first handle domain. */ const ownerDomain = async () => (domainCache ??= await handleDomain(readState().pds)) /** * Sign-up with an owner invite: GET shows the form, POST makes the account on this service's PDS * and adds its DID to the owners who may sign in. The invite is used up only when that works. */ async function signupRequest(url: URL, req: http.IncomingMessage, res: http.ServerResponse, ip: string) { const page = (p: Page) => writePage(req, res, p) if (!logins || !('origin' in signIn)) return page(simplePage(404, 'No sign-up', `

This service doesn't offer sign-up: ${esc('reason' in signIn ? signIn.reason : 'sign-in is off')}.

`)) if (req.method === 'GET' || req.method === 'HEAD') { const retry = limit('pages', ip) if (retry) return tooMany(res, retry) const code = url.searchParams.get('code') ?? '' if (!findOwnerInvite(readState(), code)) return page(signupGonePage()) return page(signupPage(code, await ownerDomain())) } if (req.method !== 'POST') return page(notFoundPage(url.pathname)) const retry = limit('signIn', ip) if (retry) return page(withRetry(simplePage(429, 'Slow down', `

Too many sign-up attempts from this address. Try again in ${retry} seconds.

`), retry)) const form = new URLSearchParams(await readText(req, MAX_BODY.register)) const code = form.get('code') ?? '' const label = (form.get('handle') ?? '').trim().toLowerCase().replace(/^@/, '') const password = form.get('password') ?? '' if (!logins.sameOrigin(req.headers.origin)) return page(simplePage(403, 'Not from here', `

This sign-up didn't come from this service's own page.

`)) const domain = await ownerDomain() const again = (error: string, status = 400) => page(signupPage(code, domain, { error, handle: label, status })) if (!findOwnerInvite(readState(), code)) return page(signupGonePage()) if (!HANDLE_LABEL.test(label) || label.includes('--')) return again('Handles are 3 to 20 characters: a–z, 0–9, and hyphens in the middle.') if (password.length < MIN_PASSWORD) return again(`Passwords need at least ${MIN_PASSWORD} characters.`) const handle = `${label}${domain}` try { checkHandle(handle) } catch (e: any) { return again(sentence(e.message)) } // Per handle, not global: the invite is taken and put back with no await between reading and saving // the state, so sign-ups on one link can run side by side (a room arrives at once). return withLock(`signup:${handle}`, async () => { // Take the invite first, so two submissions of one link can't both make accounts; put it back if this fails. const state = readState() const found = findOwnerInvite(state, code) if (!found) return page(signupGonePage()) if (state.agents[label] || state.deletedAgents?.[label]) return again(`${handle} is taken.`, 409) takeOwnerInvite(state, found.key) saveState(state) let did: string try { did = (await createAccount(state.pds, handle, password)).did } catch (e: any) { const back = readState() returnOwnerInvite(back, found.key, found.invite) saveState(back) const message = String(e?.message ?? e) log(`sign-up as ${handle} failed: ${message}`) if (/handle/i.test(message) && /taken|exists|unavailable/i.test(message)) return again(`${handle} is taken.`, 409) if (/handle/i.test(message)) return again(sentence(message)) return again("The PDS didn't make the account. Try again in a minute.", 502) } const after = readState() after.owners = { ...after.owners, [did]: { handle, createdAt: new Date().toISOString(), ...(found.invite.budgetUsd !== undefined ? { budgetUsd: found.invite.budgetUsd, spentUsd: 0 } : {}), ...(found.invite.model ? { model: found.invite.model } : {}), }, } if (found.invite.budgetUsd !== undefined) after.creditGrants = { ...after.creditGrants, [`invite:${did}`]: { did, usd: found.invite.budgetUsd, source: 'invite', at: new Date().toISOString() } } saveState(after) log(`sign-up: ${handle} (${did})${found.invite.budgetUsd !== undefined ? `, budget $${found.invite.budgetUsd}` : ''}`) funnelStep('signed-up', did) // Straight on to signing in at the PDS, rather than a page with one more button to press. try { return page(await logins!.startWeb(handle, req.headers.origin)) } catch (e: any) { log(`sign-in after sign-up as ${handle} didn't start: ${e?.message ?? e}`) return page(signupDonePage(handle, (signIn as { origin: string }).origin)) } }) } // ---- MCP: other agents talk to yours (src/mcp.ts) --------------------------------------------- /** Longest a tool call waits for a reply. MCP clients give up on a call after 30 to 60 seconds. */ const MCP_MAX_WAIT_MS = 25_000 const MCP_MAX_REPLY = 32_000 const mcpCodes = new AuthCodes() type McpCtx = { caller: Caller; signal: AbortSignal } /** An agent the caller may message, by name, handle or DID. Only their own, for now. */ function mcpAgent(ref: unknown, caller: Caller) { if (typeof ref !== 'string' || !ref.trim()) throw new McpToolError('agent is required: a name, handle or DID from list_agents') const want = ref.trim().replace(/^@/, '').toLowerCase() const state = readState() const entry = Object.values(state.agents).find((a) => a.name === want || a.agent.did === ref.trim() || a.agent.handle.toLowerCase() === want) if (!entry || entry.owner !== caller.did) throw new McpToolError(`no agent "${ref}" that you can message; list_agents shows yours`) return entry } const textOf = (m: { value: { parts: { type: string; text?: string }[] } }) => m.value.parts.flatMap((p) => (p.type === 'text' && p.text ? [p.text] : [])).join('\n').trim() /** A message id as MCP callers see it: `/`. */ const mcpMessageId = (agent: string, rkey: string) => `${agent}/${rkey}` /** * The answer to one user message, from the thread's records: the assistant messages after it, up * to the one that ends the turn. A run in memory only says it is still going, and what it has * written so far; a restart loses nothing a caller needs. */ function mcpReply(h: Harness, rkey: string) { const name = h.entry.name const sent = h.messages.find((m) => m.rkey === rkey && m.value.role === 'user') if (!sent) throw new McpToolError(`no message ${mcpMessageId(name, rkey)}`) const thread = sent.value.thread const ordered = h.messages .filter((m) => m.value.thread === thread) .map((m) => ({ m, key: transcriptKey(m) })) .sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0)) .map((x) => x.m) const after = ordered.slice(ordered.findIndex((m) => m.rkey === rkey) + 1) const steps: typeof after = [] let end: (typeof after)[number] | undefined for (const m of after) { if (m.value.role === 'user') continue steps.push(m) if (m.value.stopReason !== 'toolUse') { end = m break } } const run = runs.get(activeRuns.get(runKey(name, thread)) ?? '') let reply = steps.map(textOf).filter(Boolean).join('\n\n') let status: 'done' | 'working' | 'waiting' | 'error' | 'stopped' | 'no_reply' let error: string | undefined if (end && end.value.stopReason !== 'error' && end.value.stopReason !== 'aborted') status = 'done' else if (run && run.status === 'running') { status = 'working' if (run.partial?.trim()) reply = [reply, run.partial.trim()].filter(Boolean).join('\n\n') } else if (end?.value.stopReason === 'aborted') status = 'stopped' else if (end?.value.stopReason === 'error') { status = 'error' error = end.value.errorMessage ?? 'the model call failed' } else if (h.pending(thread)) status = 'waiting' else status = 'no_reply' const truncated = reply.length > MCP_MAX_REPLY return { message_id: mcpMessageId(name, rkey), agent: name, thread, status, ...(reply ? { reply: truncated ? reply.slice(0, MCP_MAX_REPLY) : reply } : {}), ...(truncated ? { truncated: true } : {}), ...(error ? { error } : {}), ...(status === 'working' || status === 'waiting' ? { hint: 'not finished: call get_reply with this message_id (never send the message again)' } : {}), ...(status === 'waiting' ? { note: 'saved, and waiting for a run: you have the most runs going at once, so it is answered when one ends' } : {}), } } /** mcpReply, waiting up to `waitMs` for it to finish. A wait running out never cancels anything. */ async function mcpWaitReply(agent: string, rkey: string, caller: Caller, waitMs: number, signal: AbortSignal) { const deadline = Date.now() + waitMs for (;;) { const r = mcpReply(await store(agent, caller.did), rkey) if ((r.status !== 'working' && r.status !== 'waiting') || Date.now() >= deadline || signal.aborted) return r await new Promise((resolve) => setTimeout(resolve, Math.min(300, Math.max(0, deadline - Date.now())))) } } const agentSchema = { type: 'string', description: 'The agent: its name, handle or DID, from list_agents.' } const waitSchema = { type: 'integer', minimum: 0, maximum: MCP_MAX_WAIT_MS, description: `How long to wait for the reply, in milliseconds (default 20000, at most ${MCP_MAX_WAIT_MS}). Running out never cancels the send.` } const pageSchema = { limit: { type: 'integer', minimum: 1, maximum: 100, description: 'Items per page (default 25).' }, cursor: { type: 'string', description: 'next_cursor from the previous page; leave out for the first.' }, } const readOnly = { readOnlyHint: true, openWorldHint: false } const mcpTools: McpTool[] = [ { name: 'whoami', title: 'Who am I', description: 'Your account on Languid: your DID and handle.', inputSchema: { type: 'object', properties: {} }, annotations: readOnly, run: async (_args, { caller }) => ({ did: caller.did, handle: caller.handle ?? readState().owners?.[caller.did]?.handle }), }, { name: 'list_agents', title: 'List agents', description: 'The Languid agents you can message: your own.', inputSchema: { type: 'object', properties: {} }, annotations: readOnly, run: async (_args, { caller }) => { const state = readState() return { agents: agentsOf(state, caller.did).map((n) => { const e = state.agents[n] return { agent: n, name: displayNameOf(e), handle: e.agent.handle, did: e.agent.did } }), } }, }, { name: 'find_agent', title: 'Find an agent', description: 'Look up a Languid agent by handle or DID, and whether you can message it.', inputSchema: { type: 'object', properties: { agent: { type: 'string', description: 'A handle (scout.languid.ai) or DID.' } }, required: ['agent'] }, annotations: readOnly, run: async (args, { caller }) => { const ref = str(args, 'agent', { required: true, max: 300 })!.trim().replace(/^@/, '') const e = Object.values(readState().agents).find((a) => a.agent.did === ref || a.agent.handle.toLowerCase() === ref.toLowerCase() || a.name === ref.toLowerCase()) if (!e) return { found: false, reason: `${ref} isn't an agent on this Languid service` } const mine = e.owner === caller.did return { found: true, agent: mine ? e.name : undefined, name: displayNameOf(e), handle: e.agent.handle, did: e.agent.did, can_message: mine, ...(mine ? {} : { reason: "for now you can message only your own agents" }), } }, }, { name: 'start_thread', title: 'Start a thread', description: 'Start a new thread (conversation) with one of your agents. Each call makes a thread: don\'t retry one whose outcome you don\'t know. send_message can also start one.', inputSchema: { type: 'object', properties: { agent: agentSchema, title: { type: 'string', maxLength: 200, description: 'Optional title.' } }, required: ['agent'] }, run: async (args, { caller }) => { const entry = mcpAgent(args.agent, caller) const title = str(args, 'title', { max: 200 })?.replace(/\s+/g, ' ').trim() const h = await store(entry.name, caller.did) const th = await withLock(entry.name, () => h.newThread(title || `mcp ${new Date().toISOString().slice(0, 16)}`)) if (title) await withLock(entry.name, () => h.renameThread(th.rkey, title)) return { agent: entry.name, thread: th.rkey } }, }, { name: 'list_threads', title: 'List threads', description: "One agent's threads, most recently active first.", inputSchema: { type: 'object', properties: { agent: agentSchema, ...pageSchema }, required: ['agent'] }, annotations: readOnly, run: async (args, { caller }) => { const entry = mcpAgent(args.agent, caller) const limitN = int(args, 'limit', 1, 100, 25) const from = Number(str(args, 'cursor') ?? 0) if (!Number.isInteger(from) || from < 0) throw new McpToolError('cursor is not one this tool gave') const all = (await store(entry.name, caller.did)).threadList() return { threads: all.slice(from, from + limitN).map((t) => ({ thread: t.rkey, title: t.name, messages: t.messages, updated_at: t.updatedAt })), ...(from + limitN < all.length ? { next_cursor: String(from + limitN) } : {}), } }, }, { name: 'send_message', title: 'Send a message', description: 'Send a message to one of your agents and wait up to wait_ms for its reply. Leave out thread to start a new one. ' + 'If the reply isn\'t finished (status "working" or "waiting"), call get_reply with the returned message_id; never send the same text again to retry. ' + 'Reuse client_message_id only for an identical send. Tool calls that need your approval on a computer are refused.', inputSchema: { type: 'object', properties: { agent: agentSchema, text: { type: 'string', minLength: 1, maxLength: 16000, description: 'The message.' }, thread: { type: 'string', description: 'The thread, from start_thread, list_threads or an earlier send. Leave out to start a new one.' }, client_message_id: { type: 'string', minLength: 1, maxLength: 128, description: 'Your own key for this send: sending again with the same key and text returns the first one instead of a second message.' }, wait_ms: waitSchema, }, required: ['agent', 'text'], }, run: async (args, { caller, signal }) => { const entry = mcpAgent(args.agent, caller) const text = str(args, 'text', { required: true, max: 16000 })! const retry = limit('model', caller.did) if (retry) throw new McpToolError(`too many messages; try again in ${retry}s`) const sent = await startRun( caller, { agent: entry.name, prompt: text, thread: str(args, 'thread', { max: 64 }), via: 'mcp', clientMessageId: str(args, 'client_message_id', { max: 128 }) }, 'deny', ) const rkey = sent.message.split('/').pop()! const reply = await mcpWaitReply(entry.name, rkey, caller, int(args, 'wait_ms', 0, MCP_MAX_WAIT_MS, 20_000), signal) return { ...reply, accepted: sent.accepted } }, }, { name: 'get_reply', title: 'Get a reply', description: 'The status and reply for a message you sent, waiting up to wait_ms for it to finish. Waiting never sends anything again.', inputSchema: { type: 'object', properties: { message_id: { type: 'string', maxLength: 200, description: 'message_id from send_message or read_thread.' }, wait_ms: waitSchema }, required: ['message_id'], }, annotations: readOnly, run: async (args, { caller, signal }) => { const id = str(args, 'message_id', { required: true, max: 200 })! const at = id.lastIndexOf('/') if (at <= 0) throw new McpToolError('message_id must be one send_message or read_thread gave, like "scout/3m2abc…"') const entry = mcpAgent(id.slice(0, at), caller) return mcpWaitReply(entry.name, id.slice(at + 1), caller, int(args, 'wait_ms', 0, MCP_MAX_WAIT_MS, 20_000), signal) }, }, { name: 'read_thread', title: 'Read a thread', description: "A thread's messages, oldest first. The first page is the latest messages; next_cursor goes further back.", inputSchema: { type: 'object', properties: { agent: agentSchema, thread: { type: 'string', description: 'The thread.' }, ...pageSchema }, required: ['agent', 'thread'] }, annotations: readOnly, run: async (args, { caller }) => { const entry = mcpAgent(args.agent, caller) const h = await store(entry.name, caller.did) const thread = str(args, 'thread', { required: true, max: 64 })! if (!h.threads.some((t) => t.rkey === thread)) throw new McpToolError(`no thread ${thread}; list_threads shows them`) const limitN = int(args, 'limit', 1, 100, 25) const ordered = h.messages .filter((m) => m.value.thread === thread) .map((m) => ({ m, key: transcriptKey(m) })) .sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0)) .map((x) => x.m) const end = str(args, 'cursor') === undefined ? ordered.length : Number(str(args, 'cursor')) if (!Number.isInteger(end) || end < 0 || end > ordered.length) throw new McpToolError('cursor is not one this tool gave') const start = Math.max(0, end - limitN) const tools = (rkey: string) => h.calls.filter((c) => c.value.thread === thread && c.value.message === rkey).map((c) => c.value.tool) return { agent: entry.name, thread, messages: ordered.slice(start, end).map((m) => { const used = m.value.role === 'assistant' ? tools(m.rkey) : [] return { message_id: mcpMessageId(entry.name, m.rkey), role: m.value.role, ...(m.value.role === 'user' ? { from: m.value.author, via: m.value.via } : {}), text: textOf(m), ...(used.length ? { tool_calls: used } : {}), ...(m.value.stopReason === 'error' || m.value.stopReason === 'aborted' ? { stop_reason: m.value.stopReason } : {}), created_at: m.value.createdAt, } }), ...(start > 0 ? { next_cursor: String(start) } : {}), } }, }, ] /** CORS for the MCP endpoint and the OAuth metadata: tokens are bearer tokens, never cookies, so any origin may call. */ const MCP_CORS = { 'access-control-allow-origin': '*', 'access-control-allow-methods': 'GET, POST, DELETE, OPTIONS', 'access-control-allow-headers': 'authorization, content-type, mcp-protocol-version, mcp-session-id, last-event-id', 'access-control-expose-headers': 'www-authenticate, mcp-session-id', 'access-control-max-age': '86400', } const MCP_PATHS = new Set([ '/mcp', MCP_JS_PATH, '/mcp/register', '/mcp/token', '/mcp/revoke', '/mcp/authorize', '/.well-known/oauth-protected-resource', '/.well-known/oauth-protected-resource/mcp', '/.well-known/oauth-authorization-server', ]) /** Mint an MCP access token and its refresh token, for one app. */ function mintMcpTokens(did: string, handle: string, client: string) { const access = `lgs_${crypto.randomBytes(32).toString('base64url')}` const refresh = `lgs_${crypto.randomBytes(32).toString('base64url')}` const st = readState() const now = Date.now() const live = Object.fromEntries(Object.entries(st.sessions ?? {}).filter(([, v]) => Date.parse(v.expiresAt) > now)) const created = new Date(now).toISOString() st.sessions = { ...live, [hashToken(access)]: { did, handle, createdAt: created, expiresAt: new Date(now + ACCESS_TTL_MS).toISOString(), kind: 'mcp', client }, [hashToken(refresh)]: { did, handle, createdAt: created, expiresAt: new Date(now + REFRESH_TTL_MS).toISOString(), kind: 'mcp-refresh', client }, } saveState(st) return { access_token: access, token_type: 'Bearer', expires_in: Math.floor(ACCESS_TTL_MS / 1000), refresh_token: refresh, scope: MCP_SCOPE } } /** `/mcp` and the OAuth pieces MCP apps sign in with. */ async function mcpRequest(url: URL, req: http.IncomingMessage, res: http.ServerResponse, ip: string) { const json = (status: number, body: unknown, extra: Record = {}) => { if (res.headersSent || res.destroyed) return res.writeHead(status, { 'content-type': 'application/json', 'x-content-type-options': 'nosniff', 'cache-control': 'no-store', ...MCP_CORS, ...extra }) res.end(JSON.stringify(body)) } const origin = 'origin' in signIn ? signIn.origin : undefined const p = url.pathname if (req.method === 'OPTIONS' && p !== '/mcp/authorize') { res.writeHead(204, MCP_CORS) return res.end() } const oauthError = (e: OAuthError) => json(e.status, { error: e.code, error_description: e.message }) if (p === MCP_JS_PATH && (req.method === 'GET' || req.method === 'HEAD')) { res.writeHead(200, { 'content-type': 'text/javascript; charset=utf-8', 'cache-control': 'public, max-age=86400', 'x-content-type-options': 'nosniff' }) return res.end(req.method === 'HEAD' ? undefined : MCP_JS) } if (p.startsWith('/.well-known/')) { if (req.method !== 'GET') return json(405, { error: 'method_not_allowed' }) if (!origin) return json(404, { error: 'not_found', error_description: 'sign-in is off on this service; use a bearer token' }) return json(200, p === '/.well-known/oauth-authorization-server' ? authorizationServerMetadata(origin) : protectedResourceMetadata(origin)) } if (p === '/mcp') { const resourceMetadata = origin ? `${origin}/.well-known/oauth-protected-resource/mcp` : undefined const challenge = { 'www-authenticate': `Bearer${resourceMetadata ? ` resource_metadata="${resourceMetadata}"` : ''}` } if (req.method !== 'POST') { // Stateless: no server-sent stream to open (GET) and no session to end (DELETE). return json(405, { jsonrpc: '2.0', id: null, error: { code: -32000, message: 'this server answers POST only' } }, { allow: 'POST, OPTIONS' }) } const caller = authenticate(req.headers.authorization, req, 'mcp') if (!caller || caller === 'token-elsewhere') { if (req.headers.authorization) limit('authFailures', ip) return json(401, { error: 'invalid_token', error_description: caller === 'token-elsewhere' ? 'the installation token works only from the service host' : 'sign in, or send a bearer token from languid login' }, challenge) } const retry = limit('owner', caller.did) if (retry) return tooMany(res, retry) let body try { body = await readJson(req, 1024 * 1024) } catch (e: any) { return json(e?.status ?? 400, { jsonrpc: '2.0', id: null, error: { code: -32700, message: e?.message ?? 'parse error' } }) } const abort = new AbortController() res.on('close', () => abort.abort()) const answer = await answerRpc(body, mcpTools, { caller, signal: abort.signal }, log) if (answer === undefined) { res.writeHead(202, MCP_CORS) return res.end() } return json(200, answer) } // The rest is the authorization server, which needs sign-in. if (!origin) return json(404, { error: 'not_found', error_description: 'sign-in is off on this service; use a bearer token' }) const retry = limit(p === '/mcp/register' ? 'register' : 'oauth', ip) if (retry) return tooMany(res, retry) try { if (p === '/mcp/register') { if (req.method !== 'POST') return json(405, { error: 'method_not_allowed' }) const { id, client } = registerClient(await readJson(req, MAX_BODY.register)) const st = readState() const clients = Object.entries(st.mcpClients ?? {}) // Registration is open, so the list is capped: the oldest registrations go first. st.mcpClients = Object.fromEntries([...clients.slice(Math.max(0, clients.length - MAX_MCP_CLIENTS + 1)), [id, client]]) saveState(st) log(`mcp client registered: ${client.name} (${id})`) return json(201, { client_id: id, client_id_issued_at: Math.floor(Date.parse(client.createdAt) / 1000), client_name: client.name, redirect_uris: client.redirectUris, grant_types: ['authorization_code', 'refresh_token'], response_types: ['code'], token_endpoint_auth_method: 'none', }) } if (p === '/mcp/token') { if (req.method !== 'POST') return json(405, { error: 'method_not_allowed' }) const raw = await readText(req, MAX_BODY.register) const form: Record = (req.headers['content-type'] ?? '').includes('json') ? JSON.parse(raw || '{}') : Object.fromEntries(new URLSearchParams(raw)) if (form.grant_type === 'authorization_code') { const got = mcpCodes.redeem(form.code, form.client_id, form.redirect_uri, form.code_verifier) const mcpClients = readState().mcpClients if (!mcpClients || !Object.hasOwn(mcpClients, got.clientId)) throw new OAuthError(400, 'invalid_client', 'unknown client') log(`mcp sign-in: ${got.handle} (${got.did}) for ${mcpClients[got.clientId].name}`) return json(200, mintMcpTokens(got.did, got.handle, got.clientId)) } if (form.grant_type === 'refresh_token') { const key = typeof form.refresh_token === 'string' ? hashToken(form.refresh_token) : '' const st = readState() const s = st.sessions?.[key] if (!s || s.kind !== 'mcp-refresh' || Date.parse(s.expiresAt) <= Date.now() || (form.client_id && form.client_id !== s.client)) { throw new OAuthError(400, 'invalid_grant', 'the refresh token is unknown, expired or revoked') } // Refresh tokens are used once: each refresh replaces it. delete st.sessions![key] saveState(st) return json(200, mintMcpTokens(s.did, s.handle, s.client!)) } throw new OAuthError(400, 'unsupported_grant_type', 'grant_type must be authorization_code or refresh_token') } if (p === '/mcp/revoke') { if (req.method !== 'POST') return json(405, { error: 'method_not_allowed' }) const form = Object.fromEntries(new URLSearchParams(await readText(req, MAX_BODY.register))) const key = form.token ? hashToken(form.token) : '' const st = readState() const s = st.sessions?.[key] if (s && (s.kind === 'mcp' || s.kind === 'mcp-refresh')) { // Revoking either token ends the app's sign-in: both go. for (const [k, v] of Object.entries(st.sessions!)) if (v.client === s.client && v.did === s.did && (v.kind === 'mcp' || v.kind === 'mcp-refresh')) delete st.sessions![k] saveState(st) } return json(200, {}) } if (p === '/mcp/authorize') { const params = req.method === 'POST' ? new URLSearchParams(await readText(req, MAX_BODY.register)) : url.searchParams if (req.method !== 'GET' && req.method !== 'POST') return json(405, { error: 'method_not_allowed' }) const clients = readState().mcpClients const parsed = parseAuthRequest(params, clients) if ('page' in parsed) return writePage(req, res, mcpErrorPage(parsed.page)) if ('redirect' in parsed) { res.writeHead(303, { location: parsed.redirect, 'cache-control': 'no-store' }) return res.end() } const ar = parsed.ok const client = clients![ar.clientId] const who = webSession(req.headers.cookie) if (req.method === 'GET') { if (!who) return writePage(req, res, mcpSignInPage(client, `${p}${url.search}`, origin)) return writePage(req, res, mcpConsentPage(client, ar, who)) } // The decision: from this service's own page, by a signed-in browser. if (!logins?.sameOrigin(req.headers.origin)) return writePage(req, res, mcpErrorPage("This approval didn't come from Languid's own page.", 403)) if (!who) return writePage(req, res, mcpSignInPage(client, `${p}?${params}`, origin)) const to = params.get('decision') === 'allow' ? authRedirect(ar, origin, { code: mcpCodes.issue(ar, { did: who.did, handle: who.handle }) }) : authRedirect(ar, origin, { error: 'access_denied', description: 'the owner said no' }) if (params.get('decision') === 'allow') log(`mcp app approved: ${client.name} (${ar.clientId}) by ${who.handle}`) res.writeHead(303, { location: to, 'cache-control': 'no-store' }) return res.end() } return json(404, { error: 'not_found' }) } catch (e: any) { if (e instanceof OAuthError) return oauthError(e) if (e instanceof XrpcHttpError) return json(e.status, { error: 'invalid_request', error_description: e.message }) if (e instanceof SyntaxError) return json(400, { error: 'invalid_request', error_description: 'the body is not valid JSON' }) log(`${p} failed: ${e?.stack ?? e}`) return json(500, { error: 'server_error', error_description: 'the request failed on the service; see its log' }) } } /** The sign-in pages and the CLI's side of a login. None of it needs a token. */ async function oauthRequest(url: URL, req: http.IncomingMessage, res: http.ServerResponse, send: (status: number, body: unknown) => void, ip: string) { if (!logins) return send(404, { error: 'SignInUnavailable', message: `this service doesn't offer sign-in: ${'reason' in where ? where.reason : ''}` }) const page = (p: Page) => writePage(req, res, p) const browser = url.pathname === '/oauth/login' || url.pathname === '/oauth/callback' || url.pathname === '/oauth/login/decide' || url.pathname === '/oauth/login/handle' || url.pathname.startsWith('/oauth/web/') // Client metadata is fetched by authorization servers, often, for every sign-in: not limited. if (!(req.method === 'GET' && url.pathname === '/oauth/client-metadata.json')) { const starting = req.method === 'POST' && (url.pathname === '/oauth/login/start' || url.pathname === '/oauth/web/start' || url.pathname === '/oauth/login/handle') const retry = limit(starting ? 'signIn' : 'oauth', ip) if (retry) { const message = `too many sign-in attempts from this address; try again in ${retry}s` if (url.pathname === '/oauth/web/start') return page(withRetry(landingPage(signIn, { error: message, status: 429 }), retry)) if (browser) return page(withRetry(Logins.errorPage(new LoginError(429, message)), retry)) return tooMany(res, retry, message) } } try { if (req.method === 'POST' && url.pathname === '/oauth/web/start') { const form = new URLSearchParams(await readText(req, MAX_BODY.register)) const handle = form.get('handle') ?? '' try { return page(await logins.startWeb(handle, req.headers.origin, form.get('next') ?? undefined)) } catch (e) { // Back to the form, with the reason and what was typed. if (e instanceof LoginError) return page(landingPage(signIn, { error: e.message, handle, status: e.status })) throw e } } if (req.method === 'POST' && url.pathname === '/oauth/web/logout') { if (!logins.sameOrigin(req.headers.origin)) throw new LoginError(403, "this sign-out didn't come from this service's own page") const who = webSession(req.headers.cookie) const others = otherAccounts(req.headers.cookie) if (who) { const state = readState() delete state.sessions?.[who.key] saveState(state) log(`sign-out (web): ${who.handle} (${who.did})`) const now = Date.now() if (!Object.values(state.sessions ?? {}).some((v) => v.did === who.did && Date.parse(v.expiresAt) > now)) { await protocol?.forget(who.did) } } // Signs out the account in use; the browser's next signed-in account (if any) takes its place. const [next, ...rest] = others const setCookie = next || cookie(req.headers.cookie, ACCOUNTS_COOKIE) !== undefined ? accountCookies(next, rest) : logins.sessionCookie('', 0) res.writeHead(303, { location: '/', 'set-cookie': setCookie, 'cache-control': 'no-store' }) return res.end() } if (req.method === 'POST' && url.pathname === '/oauth/web/switch') { // To another account this browser is signed in as, from the account menu. if (!logins.sameOrigin(req.headers.origin)) throw new LoginError(403, "this switch didn't come from this service's own page") const did = new URLSearchParams(await readText(req, MAX_BODY.register)).get('did') const who = webSession(req.headers.cookie) const others = otherAccounts(req.headers.cookie) const to = others.find((s) => s.did === did) if (!to) { res.writeHead(303, { location: '/', 'cache-control': 'no-store' }) return res.end() } log(`switch account (web): ${who?.handle ?? 'none'} to ${to.handle} (${to.did})`) res.writeHead(303, { location: '/', 'set-cookie': accountCookies(to, [...(who ? [who] : []), ...others.filter((s) => s !== to)]), 'cache-control': 'no-store' }) return res.end() } if (req.method === 'GET' && url.pathname === '/oauth/client-metadata.json') return send(200, logins.metadata()) if (req.method === 'POST' && url.pathname === '/oauth/login/start') return send(200, logins.start((await readJson(req, MAX_BODY.register)).handle)) if (req.method === 'POST' && url.pathname === '/oauth/login/poll') { const { login, secret } = await readJson(req, MAX_BODY.register) return send(200, logins.poll(login, secret)) } if (req.method === 'GET' && url.pathname === '/oauth/login') { // A browser already signed in here can approve a terminal as itself, without going to its PDS again. const session = webSession(req.headers.cookie) return page(await logins.browse(url.searchParams.get('flow'), session && { did: session.did, handle: session.handle })) } if (req.method === 'POST' && url.pathname === '/oauth/login/handle') { return page(await logins.chooseHandle(new URLSearchParams(await readText(req, MAX_BODY.register)), req.headers.cookie, req.headers.origin)) } if (req.method === 'GET' && url.pathname === '/oauth/callback') return page(await logins.callback(url.searchParams, req.headers.cookie)) if (req.method === 'POST' && url.pathname === '/oauth/login/decide') { const form = new URLSearchParams(await readText(req, MAX_BODY.register)) return page(logins.decide(form, req.headers.cookie, req.headers.origin)) } return send(404, { error: 'NotFound', message: `${req.method} ${url.pathname}` }) } catch (e: any) { const err = e instanceof LoginError ? e : e instanceof XrpcHttpError ? new LoginError(e.status, e.message) : undefined if (!err) log(`${url.pathname} failed: ${e?.stack ?? e}`) const shown = err ?? new LoginError(500, 'sign-in failed on the service; see its log') return browser ? page(Logins.errorPage(shown)) : send(shown.status, { error: 'LoginFailed', message: shown.message }) } } const expected = Buffer.from(`Bearer ${opts.token}`) /** * On a service with a public HTTPS address, the installation token (which acts as the installation * owner, and can approve tool calls on their computers) is accepted only from this host or the * tailnet. Everyone else signs in. Decided by the socket's address, which a client can't choose; a * request carrying the proxy's client-address header came through the proxy, so it is never local, * even when the proxy runs on this host. */ const publicUrl = 'origin' in where && !where.loopback const tokenAllowed = (req: http.IncomingMessage) => !publicUrl || (hostOrTailnet(req.socket.remoteAddress) && !(clientIpHeader && req.headers[clientIpHeader] !== undefined)) /** The owner a bearer token stands for: a login session, or the installation's token. */ function authenticate(header: string | undefined, req: http.IncomingMessage, route: 'xrpc' | 'mcp' = 'xrpc'): Caller | 'token-elsewhere' | undefined { const auth = Buffer.from(header ?? '') if (auth.length === expected.length && crypto.timingSafeEqual(auth, expected)) { return tokenAllowed(req) ? { did: installOwner(readState()), via: 'token' } : 'token-elsewhere' } const token = (header ?? '').match(/^Bearer (lgs_[A-Za-z0-9_-]{43})$/)?.[1] if (!token) return undefined const key = hashToken(token) const session = readState().sessions?.[key] // A browser's session cookie opens pages only; it is never an XRPC bearer token. An MCP app's // access token works at /mcp only, and its refresh token only at /mcp/token. const usable = session && (session.kind === undefined || (route === 'mcp' && session.kind === 'mcp')) if (!session || !usable || Date.parse(session.expiresAt) <= Date.now()) return undefined return { did: session.did, handle: session.handle, via: 'session', session: key } } /** Model calls and runs spend the service's model credentials: a tighter limit, and a cap on concurrent streams. */ const MODEL_METHODS = new Set(['ai.languid.model.stream', 'ai.languid.agent.run']) const dispatch = async (req: http.IncomingMessage, res: http.ServerResponse, send: (status: number, body: unknown) => void) => { const url = new URL(req.url ?? '/', 'http://localhost') // Agents' static sites (src/sites.ts). The sites host answers nothing else; /_sites/ only on a // loopback service, since a site on the main origin could act as a signed-in owner. const onSitesHost = sitesHost !== undefined && (req.headers.host ?? '').toLowerCase().replace(/:\d+$/, '') === sitesHost if (onSitesHost || (sitesOnPath && (url.pathname === '/_sites' || url.pathname.startsWith('/_sites/')))) { const retry = limit('pages', limiter?.client(req) ?? '') if (retry) { res.writeHead(429, { ...SITE_HEADERS, 'retry-after': String(retry), 'content-type': 'text/plain; charset=utf-8' }) return res.end('Too many requests. Try again shortly.\n') } return onSitesHost ? siteHost.serve(req, res, url.pathname) : siteHost.serve(req, res, url.pathname.slice('/_sites'.length) || '/', '/_sites') } if (url.pathname === '/xrpc/_health') return send(200, { ok: true }) const ip = limiter?.client(req) ?? '' // An address that keeps presenting wrong tokens gets nothing until its bucket refills. const cooling = limiter?.wait('authFailures', ip) ?? 0 if (cooling) return tooMany(res, cooling, `too many requests with a wrong token from this address; try again in ${cooling}s`) // Files held in memory (the icon, the font, the page scripts) cost nothing to send and aren't limited: // every page loads them, and in a room on one address they'd use up its page budget. if (url.pathname === '/favicon.svg' && (req.method === 'GET' || req.method === 'HEAD')) { res.writeHead(200, { 'content-type': 'image/svg+xml', 'cache-control': 'public, max-age=86400', 'content-security-policy': "default-src 'none'; style-src 'unsafe-inline'", 'x-content-type-options': 'nosniff', }) return res.end(req.method === 'HEAD' ? undefined : FAVICON) } // The page font. Its URL carries a hash of the file, so it can be cached for good. const font = Object.hasOwn(FONT_FILES, url.pathname) ? FONT_FILES[url.pathname] : undefined if (font && (req.method === 'GET' || req.method === 'HEAD')) { res.writeHead(200, { 'content-type': font.type, 'cache-control': 'public, max-age=31536000, immutable', 'x-content-type-options': 'nosniff', }) return res.end(req.method === 'HEAD' ? undefined : font.data) } // Home's New agent form: make one for the signed-in owner, then open its chat. if (url.pathname === '/profile') return profileRequest(url, req, res, ip) if (url.pathname === '/agents/new' && req.method === 'POST' && logins) { const who = webSession(req.headers.cookie) const retry = pageLimit(ip, who) if (retry) return tooMany(res, retry) if (!who) { res.writeHead(303, { location: '/', 'cache-control': 'no-store' }) return res.end() } if (!logins.sameOrigin(req.headers.origin)) return writePage(req, res, simplePage(403, 'Not from here', "

This request didn't come from this service's own page.

")) const form = new URLSearchParams(await readText(req, MAX_BODY.register)) const name = (form.get('name') ?? '').trim() let location = `/agents/${encodeURIComponent(name)}` try { if (!name) throw new Error('Give it a name.') await makeAgent(who, name) } catch (e: any) { if (e instanceof XrpcHttpError && e.status === 429) return tooMany(res, (e as any).retryAfter ?? 60, e.message) const shown = String(e?.message ?? e).slice(0, 200) issueNotice(who.did, '/', 'error', shown) location = `/?error=${encodeURIComponent(shown)}#new-agent` } res.writeHead(303, { location, 'cache-control': 'no-store' }) return res.end() } if (url.pathname === '/' && (req.method === 'GET' || req.method === 'HEAD')) { const retry = pageLimit(ip, logins && 'origin' in signIn ? webSession(req.headers.cookie) : undefined) if (retry) return writePage(req, res, withRetry(simplePage(429, 'Slow down', `

Too many requests from this address. Try again in ${retry} seconds.

`), retry)) return frontPage(req, res) } // Computer registration is authenticated by a one-time invite code. if ((url.pathname === '/xrpc/ai.languid.computer.register' || url.pathname === `/xrpc/${LEGACY_METHODS.register}`) && req.method === 'POST') { const retry = limit('register', ip) if (retry) return tooMany(res, retry) try { const result = await registerComputer(await readJson(req, MAX_BODY.register)) log(`ai.languid.computer.register ${result.name} for ${result.agents.join(', ')}`) return send(200, result) } catch (e: any) { if (e instanceof XrpcHttpError) return send(e.status, { error: e.error, message: e.message }) log(`ai.languid.computer.register failed: ${e?.stack ?? e}`) return send(500, { error: 'InternalServerError', message: e?.message ?? String(e) }) } } if (RUNNER_METHODS.has(url.pathname) && req.method === 'POST') { return runnerRequest(url.pathname.slice(6), req, res, send, ip) } if (url.pathname.startsWith('/oauth/')) return oauthRequest(url, req, res, send, ip) // Where PDSes send app calls made with atproto-proxy: did:web:#languid. if (url.pathname === '/.well-known/did.json' && appDid && 'origin' in where && (req.method === 'GET' || req.method === 'HEAD')) { return send(200, serviceDidDocument(appDid, where.origin)) } if (MCP_PATHS.has(url.pathname)) return mcpRequest(url, req, res, ip) if ((url.pathname === '/docs' || url.pathname.startsWith('/docs/') || url.pathname === '/llms.txt') && (req.method === 'GET' || req.method === 'HEAD')) { const retry = limit('pages', ip) if (retry) return tooMany(res, retry) const origin = 'origin' in signIn ? signIn.origin : opts.url ?? DEFAULT_SERVICE // Loaded on the first docs request, so the CLI (which imports this file) never loads marked. const { docAt, docMarkdown, docsPage, llmsTxt } = await import('./docs.ts') const found = docAt(url.pathname) if (url.pathname === '/llms.txt' || found?.markdown) { res.writeHead(200, { 'content-type': 'text/markdown; charset=utf-8', 'cache-control': 'no-cache', 'x-content-type-options': 'nosniff' }) return res.end(req.method === 'HEAD' ? undefined : found ? docMarkdown(found.doc, origin) : llmsTxt(origin)) } if (found) return writePage(req, res, docsPage(found.doc, origin)) return writePage(req, res, notFoundPage(url.pathname)) } if (url.pathname === '/signup') { try { return await signupRequest(url, req, res, ip) } catch (e: any) { log(`/signup failed: ${e?.stack ?? e}`) return writePage(req, res, simplePage(e?.status ?? 500, 'Sign-up failed', `

Something broke on the service. Try again shortly.

`)) } } if ((url.pathname === '/install' || url.pathname === '/install.sh' || url.pathname === '/install.ps1') && (req.method === 'GET' || req.method === 'HEAD')) { const retry = limit('pages', ip) if (retry) return tooMany(res, retry) const script = installScript('origin' in signIn ? signIn.origin : opts.url ?? DEFAULT_SERVICE, url.pathname === '/install.ps1' ? 'ps1' : 'sh') if (!script) return send(404, { error: 'NotFound', message: 'this service has no install script' }) res.writeHead(200, { 'content-type': 'text/plain; charset=utf-8', 'cache-control': 'no-cache', 'x-content-type-options': 'nosniff' }) return res.end(req.method === 'HEAD' ? undefined : script) } if (url.pathname === '/chat.js' && (req.method === 'GET' || req.method === 'HEAD')) { res.writeHead(200, { 'content-type': 'text/javascript; charset=utf-8', 'cache-control': 'public, max-age=86400', 'x-content-type-options': 'nosniff', }) return res.end(req.method === 'HEAD' ? undefined : CHAT_JS) } if (url.pathname === '/nav.js' && (req.method === 'GET' || req.method === 'HEAD')) { res.writeHead(200, { 'content-type': 'text/javascript; charset=utf-8', 'cache-control': 'public, max-age=86400', 'x-content-type-options': 'nosniff', }) return res.end(req.method === 'HEAD' ? undefined : NAV_JS) } if (url.pathname === '/memory.js' && (req.method === 'GET' || req.method === 'HEAD')) { res.writeHead(200, { 'content-type': 'text/javascript; charset=utf-8', 'cache-control': 'public, max-age=86400', 'x-content-type-options': 'nosniff', }) return res.end(req.method === 'HEAD' ? undefined : MEMORY_JS) } if (url.pathname === '/docs.js' && (req.method === 'GET' || req.method === 'HEAD')) { res.writeHead(200, { 'content-type': 'text/javascript; charset=utf-8', 'cache-control': 'public, max-age=86400', 'x-content-type-options': 'nosniff', }) return res.end(req.method === 'HEAD' ? undefined : DOCS_JS) } if (url.pathname === '/settings/models' || url.pathname === OPENROUTER_CALLBACK) return modelConnectionsRequest(req, res, ip, url) if (url.pathname === '/settings/chatgpt') { // The page's old address, from when it was only ChatGPT. res.writeHead(301, { location: '/settings/models' + url.search }) return res.end() } if (url.pathname === '/computers') return computersRequest(url, req, res, ip) if (url.pathname === '/discord' || url.pathname.startsWith('/discord/')) return discordRequest(url, req, res, ip) if (url.pathname === '/admin') return adminRequest(url, req, res, ip) if (url.pathname === '/admin/logs') return adminLogsRequest(url, req, res, ip) if (url.pathname === '/admin/feedback') return adminFeedbackRequest(url, req, res, ip) if (url.pathname === '/admin/messages') return adminMessagesRequest(url, req, res, ip) if (url.pathname === '/settings/credits') { res.writeHead(303, { location: `/billing${url.search}`, 'cache-control': 'no-store' }) return res.end() } if (url.pathname === '/billing') return billingRequest(url, req, res, ip) if (url.pathname === '/usage') return usageRequest(url, req, res, ip) if (url.pathname === '/inbox') return inboxRequest(url, req, res, ip) if (url.pathname === NEWS_PATH) { const who = logins && 'origin' in signIn ? webSession(req.headers.cookie) : undefined const retry = pageLimit(ip, who) if (retry) return tooMany(res, retry) if (!who) { res.writeHead(303, { location: '/', 'cache-control': 'no-store' }) return res.end() } const p = newsPage() p.headers = { ...p.headers, 'set-cookie': `${NEWS_COOKIE}=${LATEST_NEWS}; Path=/; Max-Age=${365 * 86400}; HttpOnly; SameSite=Lax${isHttps ? '; Secure' : ''}` } return writePage(req, res, p) } if (url.pathname === '/stripe/webhook') return stripeWebhook(req, res, ip) if (url.pathname === '/errors') return sharedErrorsRequest(url, req, res, ip) const settingsOf = /^\/agents\/([^/]+)\/settings$/.exec(url.pathname)?.[1] if (settingsOf) { let name = '' try { name = decodeURIComponent(settingsOf) } catch {} return agentSettingsRequest(name, url, req, res, ip) } const memoryOf = /^\/agents\/([^/]+)\/memory$/.exec(url.pathname)?.[1] if (memoryOf) { let name = '' try { name = decodeURIComponent(memoryOf) } catch {} return memoryRequest(name, url, req, res, ip) } // The Schedules page became the Machines tab. const schedulesOf = /^\/agents\/([^/]+)\/schedules$/.exec(url.pathname)?.[1] if (schedulesOf) { res.writeHead(301, { location: `/agents/${schedulesOf}/machines`, 'cache-control': 'no-store' }) return res.end() } const machinesOfPath = /^\/agents\/([^/]+)\/machines(?:\/([^/]+))?$/.exec(url.pathname) if (machinesOfPath) { let name = '' let rkey: string | undefined try { name = decodeURIComponent(machinesOfPath[1]) rkey = machinesOfPath[2] === undefined ? undefined : decodeURIComponent(machinesOfPath[2]) } catch {} return machinesRequest(name, rkey, url, req, res, ip) } const recordsOf = /^\/agents\/([^/]+)\/records(?:\/(.*))?$/.exec(url.pathname) if (recordsOf) { let name = '' try { name = decodeURIComponent(recordsOf[1]) } catch {} return recordsRequest(name, (recordsOf[2] ?? '').replace(/\/+$/, ''), url, req, res, ip) } const evalsOf = /^\/agents\/([^/]+)\/evals(?:\/cases\/([^/]+))?$/.exec(url.pathname) if (evalsOf) { let name = '' let rkey: string | undefined try { name = decodeURIComponent(evalsOf[1]) rkey = evalsOf[2] === undefined ? undefined : decodeURIComponent(evalsOf[2]) } catch {} return evalsRequest(name, url, req, res, ip, rkey) } if (url.pathname.startsWith('/agents/')) return chatRequest(url, req, res, send, ip) // A browser asking for a page that doesn't exist gets the 404 page; API clients keep JSON errors. if ((req.method === 'GET' || req.method === 'HEAD') && !url.pathname.startsWith('/xrpc/') && (req.headers.accept ?? '').includes('text/html')) { const retry = limit('pages', ip) if (retry) return tooMany(res, retry) return writePage(req, res, notFoundPage(url.pathname)) } const nsid = url.pathname.startsWith('/xrpc/') ? url.pathname.slice(6) : '' let caller = authenticate(req.headers.authorization, req) // An app calling through the owner's PDS: a service token for one method (service-auth.ts). const bearer = (req.headers.authorization ?? '').match(/^Bearer (\S+)$/)?.[1] if (!caller && serviceAuth && bearer && looksLikeJwt(bearer)) { try { caller = { did: await serviceAuth.verify(bearer, nsid), via: 'app' } } catch (e: any) { // Not counted as a wrong token: every app call from one PDS shares its address. if (e instanceof ServiceAuthError) return send(401, { error: e.error, message: e.message }) log(`service auth failed: ${e?.stack ?? e}`) return send(500, { error: 'InternalServerError', message: 'checking the service token failed' }) } } if (!caller || caller === 'token-elsewhere') { // Only a token that was presented and refused counts; a request without one (a browser's /favicon.ico) doesn't. if (req.headers.authorization) limit('authFailures', ip) const message = caller === 'token-elsewhere' ? "the installation token works only from this service's host or its tailnet; sign in with: languid login" : 'missing, invalid or expired bearer token; sign in with: languid login' return send(401, { error: 'AuthRequired', message }) } const retry = limit('owner', caller.did) || (MODEL_METHODS.has(nsid) ? limit('model', caller.did) : 0) || (nsid === 'ai.languid.agent.create' ? limit('createAgent', caller.did) : 0) if (retry) return tooMany(res, retry) const started = Date.now() if (nsid === 'ai.languid.model.stream' && req.method === 'POST') { const end = limiter ? limiter.startStream(caller.did) : () => {} if (!end) return tooMany(res, 5, `too many model calls at once (at most ${limiter!.modelStreams}); try again shortly`) try { return await streamModel(req, res, send, caller) } finally { end() } } let input: any try { let result: unknown if (req.method === 'GET' && Object.hasOwn(queries, nsid)) { result = await queries[nsid]((input = Object.fromEntries(url.searchParams)), caller) } else if (req.method === 'POST' && Object.hasOwn(procedures, nsid)) { result = await procedures[nsid]((input = await readJson(req, MAX_BODY.owner)), caller) } else { return send(404, { error: 'MethodNotImplemented', message: `${req.method} ${nsid || url.pathname}` }) } log(`${nsid} ${Date.now() - started}ms`) send(200, result ?? {}) } catch (e: any) { if (e instanceof XrpcHttpError) return send(e.status, { error: e.error, message: e.message }) log(`${nsid} failed: ${e?.stack ?? e}`) // Only the caller's own agent gets the report; the ops log gets every unexpected failure. const agents = readState().agents if (typeof input?.agent === 'string' && Object.hasOwn(agents, input.agent) && agents[input.agent]?.owner === caller.did) reportError(input.agent, 'service', e, { method: nsid }) else ops.event(errorEvent('error', 'bug', e, { method: nsid })) send(500, { error: 'InternalServerError', message: e?.message ?? String(e) }) } } const server = http.createServer(async (req, res) => { const send = (status: number, body: unknown) => { if (res.headersSent || res.destroyed) return // After a refused body the rest of it is unread: close the connection rather than reuse it. res.writeHead(status, { 'content-type': 'application/json', 'x-content-type-options': 'nosniff', ...(isHttps ? { 'strict-transport-security': 'max-age=31536000; includeSubDomains' } : {}), ...(status === 413 ? { connection: 'close' } : {}), }) res.end(JSON.stringify(body)) } try { await dispatch(req, res, send) } catch (e: any) { if (res.headersSent || res.destroyed) return if (e?.code === 'ERR_INVALID_URL') { return send(400, { error: 'InvalidRequest', message: 'invalid request URL' }) } if (e instanceof XrpcHttpError) { return send(e.status, { error: e.error, message: e.message }) } log(`unhandled ${req.method} ${req.url}: ${e?.stack ?? e}`) const wantsHtml = !(req.url ?? '').startsWith('/xrpc/') && req.url !== '/stripe/webhook' && (req.headers.accept ?? '').includes('text/html') if (wantsHtml) { return writePage(req, res, simplePage(500, 'Something broke', '

Something broke on the service. Try again shortly.

')) } return send(500, { error: 'InternalServerError', message: 'internal server error' }) } }) return { server, tickWakeups, listen: () => new Promise((resolve) => server.listen(opts.port, opts.host, () => { reconcileSandboxUsage() if (opts.wakeups !== false) { wakeupTimer = setInterval(() => { void tickWakeups() }, Math.max(1000, opts.wakeups?.intervalMs ?? 30000)) wakeupTimer.unref() void tickWakeups() } // Seal any secrets state.json still holds in plain text, now that there's a key (see secrets.ts). if (process.env.LANGUID_SECRET_KEY && !opts.state) saveState(loadState()) if (opts.discord !== false) discord.startAll() if (opts.ops) { ops.start() ops.event({ level: 'info', kind: 'boot', message: `started${process.env.FLY_MACHINE_ID ? ` on ${process.env.FLY_MACHINE_ID}` : ''}` }) void ops.flush() } if (!opts.state) void privatizeFormerlyPublic().catch((e) => log(`privatizing old public records failed: ${e?.message ?? e}`)) if (opts.ops && opts.telemetry !== false) void followFeedbackDestination().catch((e) => log(`moving shared error reports to the feedback destination failed: ${e?.message ?? e}`)) resolve(server) })), /** * The service is stopping (a deploy, a restart): runs live in memory and end with it, so record * an error report for each one still going, saying why. Waits at most `ms` for the writes. */ interruptRuns: async (reason: string, ms = 4000) => { const going = [...runs.values()].filter((r) => r.status === 'running') ops.event({ level: 'info', kind: 'boot', message: `stopping: ${reason}${going.length ? ` (${going.length} run(s) cut off)` : ''}` }) const flushed = opts.ops ? ops.flush() : Promise.resolve() const writes = going.map((r) => { log(`run ${r.id} for ${r.agent} cut off: ${reason}`) const e = Object.assign(new Error(`a run on thread ${r.thread} was cut off: ${reason}`), { name: 'RunLost', stack: undefined }) return opts.telemetry === false || !readState().agents[r.agent] ? Promise.resolve(undefined) : writeReport(r.agent, errorReport('run', e, { method: r.id.startsWith('wakeup:') ? 'wakeup' : 'ai.languid.agent.run' })) }) await Promise.race([Promise.all([...writes, flushed]), new Promise((r) => setTimeout(r, ms))]) return going.length }, /** The ops log (src/ops.ts), for the process's own crash handlers. */ ops, close: async () => { ops.stop() siteHost.close() if (wakeupTimer) clearInterval(wakeupTimer) wakeupAbort.abort() discord.stopAll() await ticking // Machine runs stop at the abort; their records say where to go on after a restart. await Promise.all([...machineRuns]) await chatgpt.close() hub.close() if (volumeSweep) clearInterval(volumeSweep) limiter?.close() server.closeAllConnections() return new Promise((resolve) => server.close(() => resolve())) }, } }