// Error reports from a computer (its runner, its tray, and `languid computer setup`) to the ops log // of each service it runs tools for, through ai.languid.runner.report with the runner's own token. // A report holds the error's class, message and stack frames (scrubbed: home directories and // anything that looks like a secret replaced), the OS and the languid version. Never tool calls, // arguments or output. Best effort: a report that can't be sent is dropped. import { languidVersion, scrub } from './telemetry.ts' import { loadState, registrations, type State } from './state.ts' import { installedClient } from './client-update.ts' export const REPORT_SOURCES = ['runner', 'tray', 'setup'] as const export type ReportSource = (typeof REPORT_SOURCES)[number] export interface ComputerReport { source: ReportSource level: 'error' | 'warn' message: string name?: string stack?: string /** process.platform/process.arch, like linux/x64. */ platform: string version?: string } export function computerReport(source: ReportSource, error: unknown, level: 'error' | 'warn' = 'error'): ComputerReport { const e = error as any const report: ComputerReport = { source, level, message: scrub(typeof e?.message === 'string' ? e.message : String(error), 1000), platform: `${process.platform}/${process.arch}`, } if (typeof e?.name === 'string') report.name = scrub(e.name, 120) if (typeof e?.stack === 'string') { const frames = e.stack.split('\n').filter((l: string) => /^\s+at /.test(l)).join('\n') if (frames) report.stack = scrub(frames, 4000) } const v = languidVersion() if (v) report.version = v return report } /** * An error from a script that reports through `languid computer report` (the Windows tray): * its message, type, and PowerShell's ScriptStackTrace ("at Fn, C:\...\Languid.ps1: line 12"), * indented so its lines count as frames. */ export function scriptError(message: string, name?: string, stack?: string): Error { const e = new Error(message || 'unknown error') if (name) e.name = name const frames = (stack ?? '').split(/\r?\n/).map((l) => l.trim()).filter((l) => l.startsWith('at ')) e.stack = [`${e.name}: ${e.message}`, ...frames.map((l) => ` ${l}`)].join('\n') return e } /** * Send a report to every service this computer runs tools for (once per service). Before setup has * a runner token (a first setup that fails), it goes to the installing service as a problem report * instead (ai.languid.ops.report), and the report's id comes back. Never throws. */ export async function reportComputerError( source: ReportSource, error: unknown, opts: { state?: State; targets?: { serviceUrl: string; token: string }[]; level?: 'error' | 'warn'; timeoutMs?: number; serviceUrl?: string } = {}, ): Promise { try { if (process.env.LANGUID_NO_REPORTS) return const report = computerReport(source, error, opts.level) const seen = new Set() const sends = (opts.targets ?? registrations(opts.state ?? loadState())).filter((r) => !seen.has(r.serviceUrl) && seen.add(r.serviceUrl)) if (!sends.length) { const service = opts.serviceUrl ?? process.env.LANGUID_SERVER ?? installedClient()?.service if (!service) return const { source: step, ...rest } = report return (await sendProblemReport(service, { ...rest, step }, { timeoutMs: opts.timeoutMs }))?.id } await Promise.all(sends.map((r) => fetch(new URL('/xrpc/ai.languid.runner.report', r.serviceUrl), { method: 'POST', headers: { authorization: `Bearer ${r.token}`, 'content-type': 'application/json' }, body: JSON.stringify(report), signal: AbortSignal.timeout(opts.timeoutMs ?? 5000), }).catch(() => {}), )) } catch {} } /** What ai.languid.ops.report takes (src/ops.ts reportEvent). `log` and `note` only with the person's yes. */ export interface ProblemReport { step: string message?: string level?: 'error' | 'warn' | 'info' name?: string stack?: string platform?: string version?: string note?: string log?: string /** The id of an earlier report this adds to. */ id?: string } /** Send a problem report to the service's ops log. Undefined if it couldn't be sent. Never throws. */ export async function sendProblemReport(serviceUrl: string, report: ProblemReport, opts: { token?: string; timeoutMs?: number } = {}): Promise<{ id: string } | undefined> { try { const res = await fetch(new URL('/xrpc/ai.languid.ops.report', serviceUrl), { method: 'POST', headers: { 'content-type': 'application/json', ...(opts.token ? { authorization: `Bearer ${opts.token}` } : {}) }, body: JSON.stringify(report), signal: AbortSignal.timeout(opts.timeoutMs ?? 8000), }) if (!res.ok) return const body = (await res.json()) as { id?: unknown } return typeof body.id === 'string' ? { id: body.id } : undefined } catch { return undefined } }