diff --git a/.github/workflows/status.yml b/.github/workflows/status.yml new file mode 100644 index 00000000..8b8467dd --- /dev/null +++ b/.github/workflows/status.yml @@ -0,0 +1,39 @@ +# Status page CI: typecheck, unit tests and a production build. No secrets needed. The build reads the live +# status API once (ISR); if it cannot, the page still builds and says so. +name: status + +on: + pull_request: + paths: ['apps/status/**', '.github/workflows/status.yml'] + push: + branches: [main] + paths: ['apps/status/**', '.github/workflows/status.yml'] + +concurrency: + group: status-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: read + +jobs: + build: + runs-on: ubuntu-latest + timeout-minutes: 15 + defaults: + run: + working-directory: apps/status + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 24.22.1 + - uses: oven-sh/setup-bun@v2 + with: + bun-version: 1.3.9 + - run: bun install --frozen-lockfile + - run: bun run typecheck + - run: bun run test + - run: bun run build + env: + NEXT_TELEMETRY_DISABLED: '1' diff --git a/CLAUDE.md b/CLAUDE.md index ea5a3d28..2ce82901 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -42,6 +42,7 @@ HelloNear is an AI agent persona platform on NEAR, built for NEAR HACKATHON (1 |---|---|---| | `apps/fe` | Web app (Next.js App Router, NEAR Connect) | Ancung | | `apps/landing` | Landing page | Nabil | +| `apps/status` | Status page, status.hellonear.xyz (Vercel): whether HelloNear and the services it runs on answer, read from the backend's `GET /v1/status` | Axel | | `apps/be` | Backend API, Postgres + pgvector, NEP-413 login, OutLayer + Composio | James | | `engine` | AI engine (Mastra + NEAR AI Cloud) | Axel | | `sc` | Mind registry contract (Rust `near-sdk`) | Axel | @@ -71,6 +72,7 @@ Each folder has its own workflow in `.github/workflows/`. It runs on pull reques | `fe` | `apps/fe` | lint, typecheck, `next build` | | `sc` | `sc` | unit tests, sandbox tests (incl. upgrades from the fixtures), clippy | | `landing` | `apps/landing` | typecheck, `next build` | +| `status` | `apps/status` | typecheck, unit tests, `next build` | CI does not replace the local integration and load tests required by the rules below. Nothing deploys from CI itself. A merged `main` is deployed to the hosted demo after its CI passes, by a poller on James's always-on LAN machine that comments the result on the merged PR (rule 3, `deploy/README.md`). diff --git a/README.md b/README.md index bc075e3c..eea33710 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ HelloNear gives a person a **Mind**: an AI agent with a persona, memory, skills | **Web app** (no account needed: *Continue as guest*) | **[app.hellonear.xyz](https://app.hellonear.xyz)** | | **Landing page** | **[hellonear.xyz](https://hellonear.xyz)** | | **API** (NEAR testnet) | **[api.hellonear.xyz/v1/health](https://api.hellonear.xyz/v1/health)** | +| **Status** (HelloNear and every provider it uses, checked every 5 minutes) | **[status.hellonear.xyz](https://status.hellonear.xyz)** · raw: [api.hellonear.xyz/v1/status](https://api.hellonear.xyz/v1/status) | | **Mind registry contract** | **[`hellonear-registry.testnet`](https://testnet.nearblocks.io/address/hellonear-registry.testnet)** (v0.3.0, NEP-330, [source verified](https://testnet.nearblocks.io/address/hellonear-registry.testnet?tab=contract)) | | **Swarm auction contract** | **[`auction.hellonear-swarm.testnet`](https://testnet.nearblocks.io/address/auction.hellonear-swarm.testnet?tab=contract)** (NEP-330, [source verified](https://testnet.nearblocks.io/address/auction.hellonear-swarm.testnet?tab=contract), [a real round](#the-swarm-auction-one-real-round-on-testnet)) | | **API reference** | **[OpenAPI spec](docs/openapi.json)** · [README section](#api-reference) | @@ -652,7 +653,7 @@ Every route and event is in [`docs/api-contract.md`](docs/api-contract.md). ## API reference -The web app is a client of a public HTTP API, and everything it does can be done with `curl`. The full spec is **[`docs/openapi.json`](docs/openapi.json)** (OpenAPI 3.1, 62 operations): open it in [Swagger Editor](https://editor.swagger.io/?url=https://raw.githubusercontent.com/Hellonear-AI/hellonear/main/docs/openapi.json) or any OpenAPI viewer to read every route and try it against the hosted API. Every server-sent event type is described in [`docs/api-contract.md`](docs/api-contract.md) §3. +The web app is a client of a public HTTP API, and everything it does can be done with `curl`. The full spec is **[`docs/openapi.json`](docs/openapi.json)** (OpenAPI 3.1, 63 operations): open it in [Swagger Editor](https://editor.swagger.io/?url=https://raw.githubusercontent.com/Hellonear-AI/hellonear/main/docs/openapi.json) or any OpenAPI viewer to read every route and try it against the hosted API. Every server-sent event type is described in [`docs/api-contract.md`](docs/api-contract.md) §3. - **Base URL:** `https://api.hellonear.xyz` (NEAR testnet), or `http://localhost:4000`. - **Auth:** a session cookie, `hn_session`. Get one with `POST /v1/auth/demo` (guest, no wallet) or with a NEAR wallet: `POST /v1/auth/challenge`, sign the message (NEP-413), then `POST /v1/auth/login`. `curl` works as is (no `Origin` header); a browser page on another site gets `403` for any call that changes something (CSRF protection). @@ -736,6 +737,7 @@ The web app is a client of a public HTTP API, and everything it does can be done | `GET` | `/v1/minds/{mindId}/wallet` | Balance, spent today, frozen, and the policy | 200 | yes | | `GET` | `/v1/minds/{mindId}/wallet/payments` | Payment receipts, refused ones included | 200 | yes | | `GET` | `/v1/minds/{mindId}/payments.csv` | Every payment as a CSV an accountant can open (date UTC, status in words, amount, counterparty, purpose, who approved, tx hash, NearBlocks link) | 200 `text/csv` | yes | +| `GET` | `/v1/status` | Public status of HelloNear and its providers (NEAR AI chat and embeddings, 1Click quotes, OutLayer, NEAR RPC, Agent Market, TEE Explorer): status, latency, uptime, 48 half-hour buckets and incidents of the last 7 days. CORS `*`, cached 30 s | 200 | | | `POST` | `/v1/relay` | Relay a NEP-366 signed delegate action to the registry (only `unequip_skill` / `set_wallet`, exactly 1 yoctoNEAR); the relayer pays the gas, never a deposit. Not for try-out users | 200, `503 relay_not_configured` until the relayer is set | | | `POST` | `/v1/minds/{mindId}/wallet/policy` | Build a store_wallet_policy transaction for the owner to sign | 200 | | | `POST` | `/v1/minds/{mindId}/wallet/freeze` | Build a freeze_wallet transaction for the owner to sign | 200 | | diff --git a/apps/be/.env.example b/apps/be/.env.example index fb2de79c..c676ae89 100644 --- a/apps/be/.env.example +++ b/apps/be/.env.example @@ -70,3 +70,7 @@ OUTLAYER_HANDOFF_URL= OUTLAYER_SUB_SKILL_PAYMENTS= # Optional, default https://proof.t16z.com: Phala's TEE Attestation Explorer, where POST /v1/runs/{id}/proof/tee-explorer uploads a run's Intel quote (#277; tests point it at a fake) TEE_EXPLORER_URL= +# Optional, status page checks (src/status.ts, GET /v1/status); defaults are the real services, tests point them at fakes +STATUS_ONECLICK_URL= +STATUS_MARKET_URL= +STATUS_NEAR_RPC_URL= diff --git a/apps/be/CLAUDE.md b/apps/be/CLAUDE.md index 08149376..9bcaef6b 100644 --- a/apps/be/CLAUDE.md +++ b/apps/be/CLAUDE.md @@ -25,6 +25,7 @@ Backend API: Postgres + pgvector, NEP-413 login, run/event storage + SSE, encryp - **Thread ids are issued by the backend** (`thr_…`). A `threadId` from the browser is only accepted if a run of that same Mind already used it: engine memory is keyed by thread, so a foreign id would read another Mind's history. - The chat relay (`src/runs.ts`) reads the engine stream to the end even when the browser leaves, so `run_events` is complete. Store every event before forwarding it; deltas are batched. A resumed run continues the paused run's `seq`; if the engine restarted (its counter is in memory) the backend shifts the new events after the stored ones. - **Verification proof (#254):** the engine puts the proof behind the badge on `run.verified.verification.proof` (~12 KB, mostly the Intel quote; hashes and keys' addresses only, no conversation text). It is stored whole in `run_events`, but `withoutProof` drops it from every streamed event and `NO_PROOF` from `GET /v1/runs/{runId}` and the Mind Graph's `…/events`; only `GET /v1/runs/{runId}/proof` (owner only, `runProofs`) returns it, one entry per `run.verified` that has one, `404 no_proof` when none. Any new route that returns stored events must strip it too. +- **Status page data (`src/status.ts`, `src/statusMath.ts`, `GET /v1/status`, status.hellonear.xyz):** every 5 minutes (`startStatusProbes`, first round 20 s after start, not in tests) the backend checks 9 services in parallel, each with its own timeout and degraded threshold (`SERVICES`), and stores one row each in `service_checks` (migration 016, kept 8 days). `hellonear-api` (Postgres), `hellonear-engine` (`/v1/health`), `nearai-chat` / `nearai-embeddings` (the engine's `GET /internal/v1/probes`, since only the engine has the NEAR AI key; nothing stored for them when the engine does not answer), `near-intents-quote` (a dry 1Click quote, 10 USDC NEAR → Base), `outlayer` (`/health`), `near-rpc` (FastNear `status`), `agent-market`, `tee-explorer`. `GET /v1/status` is handled before the app's CORS: public, `access-control-allow-origin: *`, no cookies, `cache-control: public, max-age=30`, answer cached 30 s and dropped after each round. The numbers (latest status, `unknown` after 15 minutes, uptime 24 h / 7 d with degraded as up, 48 half-hour buckets with `checks` / `bad` / `detail`, incidents as runs of non-operational checks ended by one good check) are pure functions in `statusMath.ts`. Upstreams can be pointed at fakes with `STATUS_ONECLICK_URL`, `STATUS_MARKET_URL`, `STATUS_NEAR_RPC_URL`, `TEE_EXPLORER_URL`, `OUTLAYER_URL`. Details are short and never carry keys or URLs. Contract and thresholds: `docs/api-contract.md` §7. Tests: `test/status-math.test.ts` (unit), `test/status.test.ts` (local Postgres, fake upstreams). - **TEE Explorer link (#277, `src/teeExplorer.ts`):** `POST /v1/runs/{runId}/proof/tee-explorer` (owner only, `getRun`) uploads each Intel quote in the run's stored proofs (`quotesOf`, one per attestation) to Phala's TEE Attestation Explorer through its own upload, `POST {TEE_EXPLORER_URL}/api/upload` with multipart `file` = the quote's bytes (default `https://proof.t16z.com`; read live 12 Nov from its page script: there is no URL that prefills a quote, and the API sends no CORS headers, so the FE cannot call it). It answers `{ runId, reports: [{ attestation, kind, url, verified }] }`, `url` = `/reports/`, a page that already reads VERIFIED (a local run's real gateway quote came back `verified: true`). The checksum must be 64 lowercase hex before it goes into a link. One upload per quote and process (in-memory `reports`, keyed by the quote's SHA-256). Only the quote leaves (NEAR AI's hardware report and nonce, no conversation text); it then shows in the Explorer's public "Recent attestations". `404 no_proof` without one, `503 tee_explorer_unreachable` / `tee_explorer_error` when the Explorer fails (never 502), `422 not_a_quote` for anything but hex. Tests: `test/fake-explorer.ts` and the two `TEE Explorer link (#277)` tests in `test/api.test.ts`. - **Cancel (#121):** each relayed run gets an `AbortController` (passed to `engineFetch` as `signal`, kept in the in-memory `live` map in `src/runs.ts` once its `runId` is known). `POST /v1/runs/{id}/cancel` aborts it; the relay then stores `run.finished` `{ status: "canceled" }` (builder: `workflow.finished`) as the last event, sets `runs.status = 'canceled'` and ends the stream, and the route answers once that is stored. A browser leaving still does **not** cancel. Not live (finished, paused, or after a restart): `409 not_running`; a paused run is declined, not canceled. Single process only: with more than one backend process the map would have to move to Postgres. - **Stuck runs (#218):** `sweepStuckRuns` (`src/runs.ts`, at start and every 5 minutes) ends a run left `running` by a backend that died mid-stream: not in `live`, and no event for `STUCK_AFTER_MS` (5 minutes; the engine gives up after 2). It must look stuck on two sweeps in a row at the same last `seq`, because an approval claims the run (`running`) just before its resumed stream starts. It then gets status `error` and a `run.error` event `run_interrupted`, so it can't block an approval or the chat's delete. @@ -64,4 +65,4 @@ Backend API: Postgres + pgvector, NEP-413 login, run/event storage + SSE, encryp ## Env -See `.env.example`: `DATABASE_URL`, `ENGINE_URL`, `ENGINE_API_KEY`, `SECRETS_MASTER_KEY` (32 bytes base64; losing it loses every stored secret), optional `PORT`, `FE_ORIGIN`, `AUTH_RECIPIENT`, `NEAR_RPC_URL`, `COOKIE_SECURE`, `OUTLAYER_URL`, `OUTLAYER_CONTRACT_ID`, `DEMO_MODE`, `DEMO_OUTLAYER_API_KEY`, `COMPOSIO_API_KEY`, `COMPOSIO_URL`, `AGENTMAIL_API_KEY`, `AGENTMAIL_API_KEY_2`, `AGENTMAIL_URL`, `DEMO_AGENTMAIL_INBOX`, `TEE_EXPLORER_URL`, `SHOP_PAY_TO`, `SHOP_SELLERS`, `RELAYER_ACCOUNT_ID`, `RELAYER_PRIVATE_KEY`, `REGISTRY_CONTRACT_ID`. `.env` is gitignored; never commit it. +See `.env.example`: `DATABASE_URL`, `ENGINE_URL`, `ENGINE_API_KEY`, `SECRETS_MASTER_KEY` (32 bytes base64; losing it loses every stored secret), optional `PORT`, `FE_ORIGIN`, `AUTH_RECIPIENT`, `NEAR_RPC_URL`, `COOKIE_SECURE`, `OUTLAYER_URL`, `OUTLAYER_CONTRACT_ID`, `DEMO_MODE`, `DEMO_OUTLAYER_API_KEY`, `COMPOSIO_API_KEY`, `COMPOSIO_URL`, `AGENTMAIL_API_KEY`, `AGENTMAIL_API_KEY_2`, `AGENTMAIL_URL`, `DEMO_AGENTMAIL_INBOX`, `TEE_EXPLORER_URL`, `SHOP_PAY_TO`, `SHOP_SELLERS`, `RELAYER_ACCOUNT_ID`, `RELAYER_PRIVATE_KEY`, `REGISTRY_CONTRACT_ID`, `STATUS_ONECLICK_URL`, `STATUS_MARKET_URL`, `STATUS_NEAR_RPC_URL`. `.env` is gitignored; never commit it. diff --git a/apps/be/migrations/016_service_checks.sql b/apps/be/migrations/016_service_checks.sql new file mode 100644 index 00000000..341c1e58 --- /dev/null +++ b/apps/be/migrations/016_service_checks.sql @@ -0,0 +1,12 @@ +-- Status page (status.hellonear.xyz): one row per check of HelloNear or a service it depends on, every 5 minutes, +-- kept 8 days (src/status.ts). `GET /v1/status` reads the last 7 days from here. +create table service_checks ( + id bigserial primary key, + service text not null, + checked_at timestamptz not null default now(), + status text not null check (status in ('operational', 'degraded', 'down')), + latency_ms integer, + detail text +); +create index service_checks_service_at on service_checks (service, checked_at desc); +create index service_checks_at on service_checks (checked_at); diff --git a/apps/be/package.json b/apps/be/package.json index ca4c2bdf..13406c32 100644 --- a/apps/be/package.json +++ b/apps/be/package.json @@ -9,7 +9,7 @@ "dev": "node --env-file=.env --import tsx src/server.ts", "migrate": "node --env-file=.env --import tsx src/migrate.ts", "test": "node --env-file-if-exists=.env --import tsx --test --test-concurrency=1 test/*.test.ts", - "test:unit": "node --import tsx --test test/crypto.test.ts test/sse.test.ts test/telegram-format.test.ts test/approvals.test.ts test/relay.test.ts", + "test:unit": "node --import tsx --test test/crypto.test.ts test/sse.test.ts test/telegram-format.test.ts test/approvals.test.ts test/relay.test.ts test/status-math.test.ts", "load": "node --env-file-if-exists=.env --import tsx load/api.ts", "load:shop": "node --env-file-if-exists=.env --import tsx load/shop.ts", "typecheck": "tsc --noEmit" diff --git a/apps/be/src/server.ts b/apps/be/src/server.ts index d008631e..d7be21a1 100644 --- a/apps/be/src/server.ts +++ b/apps/be/src/server.ts @@ -10,6 +10,7 @@ import { HttpError, assertSameOrigin, cors, logServerError, readBody, sendJson, import { TOOLKITS, connect, disconnect, listConnections, toolkitSchema } from './connections.ts' import { createMind, createMindSchema, deleteMind, listMinds, mindConfig, playgroundAttackOf, playgroundMind, requireMind, updateMind, updateMindSchema, type Mind } from './minds.ts' import { cancelRun, getRun, isAwaiting, listRuns, mindEvents, pendingApprovals, relayRun, runEvents, runProofs, sweepStuckRuns } from './runs.ts' +import { serveStatus, startStatusProbes } from './status.ts' import { explorerReport, quotesOf } from './teeExplorer.ts' import { relay } from './relay.ts' import { deleteSecret, listSecrets, putSecret, putSecretSchema, secretNameSchema, secretsForRun } from './secrets.ts' @@ -211,6 +212,8 @@ export const telegramRunner: MindRunner = { chat: runTelegramChat, answer: answe export const server = createServer(async (req, res) => { try { + // The public status page's data (status.hellonear.xyz): open to any origin, without cookies, so before the app's CORS. + if ((req.url ?? '').split('?')[0] === '/v1/status') return await serveStatus(req, res) cors(req, res) if (req.method === 'OPTIONS') return res.writeHead(204).end() assertSameOrigin(req) @@ -605,6 +608,8 @@ if (import.meta.url === `file://${process.argv[1]}`) { const applied = await migrate() if (applied.length) console.log('migrations applied', applied.join(', ')) server.listen(config.port, () => console.log(`backend listening on :${config.port}`)) + // Status page checks (src/status.ts): every 5 minutes, the first one shortly after start. + startStatusProbes() // Judge try-out users are deleted after the retention: at start and once a day. const sweep = () => cleanupDemoUsers(async (mindId) => { await deleteAllWorkflows(mindId); await disconnectTelegram(mindId).catch(() => {}) }).then((n) => { if (n) console.log('demo users removed', n) }, (e) => console.error('demo cleanup failed', e instanceof Error ? e.message : e)) void sweep() diff --git a/apps/be/src/status.ts b/apps/be/src/status.ts new file mode 100644 index 00000000..b712ef8e --- /dev/null +++ b/apps/be/src/status.ts @@ -0,0 +1,161 @@ +// The public status page (status.hellonear.xyz): every 5 minutes the backend checks HelloNear itself and each outside +// service it depends on, stores one row per check (`service_checks`, kept 8 days), and `GET /v1/status` answers from +// those rows. On 13 Nov NEAR AI's embeddings endpoint hung and 1Click's quote API timed out for hours, and the team only +// found out with curl; the page shows it, with what each outage means for a HelloNear user. +// The NEAR AI checks run in the engine (`GET /internal/v1/probes`), since only the engine holds the NEAR AI key. +// Upstream URLs are read per call, so tests can point them at fakes. Details are short and never carry keys or URLs. +import type { IncomingMessage, ServerResponse } from 'node:http' +import { config } from './config.ts' +import { query } from './db.ts' +import { engineFetch } from './engine.ts' +import { fetchFailure } from './http.ts' +import { report, verdict, type Check, type CheckStatus, type ServiceMeta, type StatusReport } from './statusMath.ts' + +export const PROBE_EVERY_MS = 5 * 60_000 +const KEEP_DAYS = 8 +const CACHE_MS = 30_000 + +const oneClick = () => process.env.STATUS_ONECLICK_URL ?? 'https://1click.chaindefuser.com' +const market = () => process.env.STATUS_MARKET_URL ?? 'https://market.near.ai' +const teeExplorer = () => process.env.TEE_EXPLORER_URL ?? 'https://proof.t16z.com' +// rpc.testnet.near.org is deprecated (12 Nov), so the check uses FastNear unless told otherwise. +const nearRpc = () => process.env.STATUS_NEAR_RPC_URL ?? 'https://test.rpc.fastnear.com' + +// USDC on NEAR to USDC on Base, as listed by 1Click's GET /v0/tokens on 13 Nov. A dry quote moves nothing. +const QUOTE_FROM = 'nep141:17208628f84f5d6ad33f0da3bbbeb27ffcb398eac501a31bd6ad2011e36133a1' +const QUOTE_TO = 'nep141:base-0x833589fcd6edb6e08f4c7c32d4f71b54bda02913.omft.near' + +export const SERVICES: (ServiceMeta & { degradedMs: number })[] = [ + { id: 'hellonear-api', name: 'HelloNear API', group: 'HelloNear', degradedMs: 1_000, impact: 'The web app, sign-in and every Mind stop working.' }, + { id: 'hellonear-engine', name: 'HelloNear engine', group: 'HelloNear', degradedMs: 2_000, impact: 'Minds cannot reply, run skills or run Autopilot jobs.' }, + { id: 'nearai-chat', name: 'NEAR AI Cloud: chat', group: 'NEAR AI', degradedMs: 10_000, impact: 'Minds cannot reply; nothing is paid or sent without a reply.' }, + { id: 'nearai-embeddings', name: 'NEAR AI Cloud: embeddings', group: 'NEAR AI', degradedMs: 5_000, impact: 'Chats and Autopilot run without searching past chats until it answers.' }, + { id: 'near-intents-quote', name: 'NEAR Intents (1Click quotes)', group: 'NEAR', degradedMs: 15_000, impact: 'Cross-chain stablecoin quotes can\'t be fetched; nothing is ever sent either way.' }, + { id: 'outlayer', name: 'OutLayer wallets', group: 'NEAR', degradedMs: 3_000, impact: 'Minds cannot read balances or pay; no payment goes out unchecked.' }, + { id: 'near-rpc', name: 'NEAR testnet RPC', group: 'NEAR', degradedMs: 3_000, impact: 'Account checks before a payment and invoice payment checks fail.' }, + { id: 'agent-market', name: 'NEAR AI Agent Market', group: 'Partners', degradedMs: 5_000, impact: 'Minds cannot search the Agent Market or get an agent\'s price.' }, + { id: 'tee-explorer', name: 'TEE Attestation Explorer', group: 'Partners', degradedMs: 5_000, impact: 'Check it in TEE Explorer cannot open a report; replies are still verified.' }, +] + +type Outcome = { ok: boolean; detail?: string; degraded?: boolean } +type Result = { service: string; status: CheckStatus; latencyMs: number | null; detail: string | null } + +const clip = (text: string, max = 160) => (text.length > max ? `${text.slice(0, max - 1)}…` : text) + +// Times one probe and turns how it went into a stored check. A timeout or error is down, never thrown. +async function timed(service: string, timeoutMs: number, run: (signal: AbortSignal) => Promise): Promise { + const meta = SERVICES.find((s) => s.id === service)! + const started = Date.now() + try { + const out = await run(AbortSignal.timeout(timeoutMs)) + const latencyMs = Date.now() - started + const status = out.ok && out.degraded ? 'degraded' : verdict(out.ok, latencyMs, meta.degradedMs) + return { service, status, latencyMs, detail: out.detail ? clip(out.detail) : status === 'degraded' && out.ok ? `Slow: answered in ${(latencyMs / 1000).toFixed(1)} s.` : null } + } catch (e) { + const reason = fetchFailure(e) + return { service, status: 'down', latencyMs: null, detail: reason === 'timeout' ? `No answer in ${timeoutMs / 1000} s.` : `Could not reach it (${reason}).` } + } +} + +const getOk = (url: string, signal: AbortSignal, init: RequestInit = {}) => + fetch(url, { redirect: 'manual', ...init, signal }).then((r) => ({ ok: r.ok, detail: r.ok ? undefined : `Answered HTTP ${r.status}.` })) + +const probes: Record Promise> = { + 'hellonear-api': () => timed('hellonear-api', 5_000, async () => { + await query('select 1') + return { ok: true } + }), + 'hellonear-engine': () => timed('hellonear-engine', 10_000, async (signal) => { + const res = await engineFetch('/v1/health', { signal }) + return { ok: res.ok, detail: res.ok ? undefined : `Answered HTTP ${res.status}.` } + }), + 'near-intents-quote': () => timed('near-intents-quote', 30_000, async (signal) => { + const res = await fetch(`${oneClick()}/v0/quote`, { + method: 'POST', signal, redirect: 'error', headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ + dry: true, swapType: 'EXACT_INPUT', slippageTolerance: 100, originAsset: QUOTE_FROM, destinationAsset: QUOTE_TO, amount: '10000000', + depositType: 'ORIGIN_CHAIN', recipient: '0x8ba1f109551bD432803012645Ac136ddd64DBA72', recipientType: 'DESTINATION_CHAIN', + refundTo: 'hellonear.near', refundType: 'ORIGIN_CHAIN', deadline: new Date(Date.now() + 10 * 60_000).toISOString(), + }), + }) + const body = (await res.json().catch(() => ({}))) as { quote?: unknown; message?: string } + if (res.ok && body.quote) return { ok: true } + return { ok: false, detail: `1Click answered HTTP ${res.status}${body.message ? `: ${body.message}` : '.'}` } + }), + 'outlayer': () => timed('outlayer', 10_000, (signal) => getOk(`${config.outlayerUrl}/health`, signal)), + 'near-rpc': () => timed('near-rpc', 10_000, async (signal) => { + const res = await fetch(nearRpc(), { method: 'POST', signal, headers: { 'content-type': 'application/json' }, body: JSON.stringify({ jsonrpc: '2.0', id: 'status', method: 'status', params: [] }) }) + const body = (await res.json().catch(() => ({}))) as { result?: { sync_info?: { syncing?: boolean } } } + if (!res.ok || !body.result) return { ok: false, detail: `Answered HTTP ${res.status} without a status.` } + return body.result.sync_info?.syncing ? { ok: true, degraded: true, detail: 'The node is still syncing.' } : { ok: true } + }), + 'agent-market': () => timed('agent-market', 15_000, async (signal) => { + const res = await fetch(`${market()}/v1/agents?limit=1`, { signal, redirect: 'error' }) + const body = (await res.json().catch(() => ({}))) as { agents?: unknown[] } + if (res.ok && Array.isArray(body.agents)) return { ok: true } + return { ok: false, detail: `Answered HTTP ${res.status}${res.ok ? ' without a catalog.' : '.'}` } + }), + 'tee-explorer': () => timed('tee-explorer', 15_000, (signal) => getOk(teeExplorer(), signal, { method: 'HEAD' })), +} + +type EngineProbe = { id: string; status: CheckStatus; latencyMs: number | null; detail: string | null } + +// NEAR AI chat and embeddings, checked by the engine in one call. Without an answer from the engine nothing is known +// about NEAR AI, so nothing is stored for them this round (the page then shows "none" for that half hour). +async function nearAiProbes(): Promise { + try { + const res = await engineFetch('/internal/v1/probes', { signal: AbortSignal.timeout(45_000) }) + if (!res.ok) return [] + const body = (await res.json()) as { probes?: EngineProbe[] } + return (body.probes ?? []) + .filter((p) => (p.id === 'nearai-chat' || p.id === 'nearai-embeddings') && ['operational', 'degraded', 'down'].includes(p.status)) + .map((p) => ({ service: p.id, status: p.status, latencyMs: typeof p.latencyMs === 'number' ? Math.round(p.latencyMs) : null, detail: p.detail ? clip(String(p.detail)) : null })) + } catch { + return [] + } +} + +// One round: every probe at once (one failing never stops the others), one row each, old rows dropped. +export async function runProbes(): Promise { + const settled = await Promise.allSettled([...Object.values(probes).map((p) => p()), nearAiProbes()]) + const results = settled.flatMap((s) => (s.status === 'fulfilled' ? s.value : [])) + for (const r of results) { + await query('insert into service_checks (service, status, latency_ms, detail) values ($1, $2, $3, $4)', [r.service, r.status, r.latencyMs, r.detail]) + } + await query(`delete from service_checks where checked_at < now() - make_interval(days => $1)`, [KEEP_DAYS]) + cached = undefined + return results +} + +let cached: { at: number; report: StatusReport } | undefined +export const resetStatusCache = () => { cached = undefined } + +export async function statusReport(now = new Date()): Promise { + if (cached && now.getTime() - cached.at < CACHE_MS) return cached.report + const rows = await query<{ service: string; checked_at: Date; status: CheckStatus; latency_ms: number | null; detail: string | null }>( + `select service, checked_at, status, latency_ms, detail from service_checks where checked_at > now() - interval '7 days'`, + ) + const checks: Check[] = rows.map((r) => ({ service: r.service, checkedAt: new Date(r.checked_at), status: r.status, latencyMs: r.latency_ms, detail: r.detail })) + const meta = SERVICES.map(({ id, name, group, impact }) => ({ id, name, group, impact })) + const out = report(meta, checks, now) + cached = { at: now.getTime(), report: out } + return out +} + +// Public and cross-origin on purpose: the status page lives on Vercel, reads it without cookies, and it holds nothing private. +const PUBLIC_HEADERS = { 'access-control-allow-origin': '*', 'access-control-allow-methods': 'GET, OPTIONS', 'access-control-allow-headers': 'content-type' } + +export async function serveStatus(req: IncomingMessage, res: ServerResponse): Promise { + if (req.method === 'OPTIONS') return void res.writeHead(204, { ...PUBLIC_HEADERS, 'access-control-max-age': '86400' }).end() + if (req.method !== 'GET' && req.method !== 'HEAD') return void res.writeHead(405, { ...PUBLIC_HEADERS, 'content-type': 'application/json' }).end(JSON.stringify({ error: 'method_not_allowed' })) + const body = JSON.stringify(await statusReport()) + res.writeHead(200, { ...PUBLIC_HEADERS, 'content-type': 'application/json', 'cache-control': 'public, max-age=30' }) + res.end(req.method === 'HEAD' ? undefined : body) +} + +export function startStatusProbes(): void { + const round = () => runProbes().catch((e) => console.error('status probes failed', e instanceof Error ? e.message : e)) + // A short pause first, so the engine (started alongside) is up for the first round. + setTimeout(round, 20_000).unref() + setInterval(round, PROBE_EVERY_MS).unref() +} diff --git a/apps/be/src/statusMath.ts b/apps/be/src/statusMath.ts new file mode 100644 index 00000000..90644b9b --- /dev/null +++ b/apps/be/src/statusMath.ts @@ -0,0 +1,123 @@ +// The public status page's numbers (status.hellonear.xyz, `GET /v1/status`), worked out from stored checks. Pure, so it +// is unit-tested without Postgres or the network. `src/status.ts` runs the probes and stores the checks. + +export type CheckStatus = 'operational' | 'degraded' | 'down' +export type ServiceStatus = CheckStatus | 'unknown' +export type BucketStatus = CheckStatus | 'none' +export type Group = 'HelloNear' | 'NEAR AI' | 'NEAR' | 'Partners' + +export type ServiceMeta = { id: string; name: string; group: Group; impact: string } +export type Check = { service: string; checkedAt: Date; status: CheckStatus; latencyMs: number | null; detail: string | null } + +export type ServiceReport = ServiceMeta & { + status: ServiceStatus + latencyMs: number | null + checkedAt: string | null + detail: string | null + uptime24h: number | null + uptime7d: number | null + history: Bucket[] +} +// One bar of the 24-hour strip. `checks`, `bad` and `detail` feed the hover text (how many probes ran in that half +// hour, how many were down or degraded, and what the last bad one said). +export type Bucket = { start: string; status: BucketStatus; checks: number; bad: number; detail: string | null } +// A run of non-operational checks of one service: a single good check ends it. Down if any check in it was down. +export type Incident = { service: string; name: string; status: 'down' | 'degraded'; start: string; end: string | null; minutes: number; detail: string | null } +export type StatusReport = { updatedAt: string; overall: CheckStatus; services: ServiceReport[]; incidents: Incident[] } + +export const BUCKET_MS = 30 * 60_000 +export const BUCKETS = 48 +// Probes run every 5 minutes; with no check for three rounds the last one is too old to show as current. +export const STALE_MS = 15 * 60_000 +export const MAX_INCIDENTS = 100 + +const RANK: Record = { none: 0, operational: 1, degraded: 2, down: 3 } +const worse = (a: BucketStatus, b: BucketStatus): BucketStatus => (RANK[b] > RANK[a] ? b : a) + +// Share of checks that answered (degraded counts as up), as a percent with 2 decimals; null without checks. +export function uptime(checks: Check[]): number | null { + if (checks.length === 0) return null + const up = checks.filter((c) => c.status !== 'down').length + return Math.round((up / checks.length) * 10_000) / 100 +} + +const byTime = (a: Check, b: Check) => a.checkedAt.getTime() - b.checkedAt.getTime() + +// 48 half-hour buckets ending with the one that holds `now`, oldest first; each takes the worst check inside it. +export function buckets(checks: Check[], now: Date): Bucket[] { + const end = Math.floor(now.getTime() / BUCKET_MS) * BUCKET_MS + BUCKET_MS + const first = end - BUCKETS * BUCKET_MS + const out: Bucket[] = Array.from({ length: BUCKETS }, (_, i) => ({ start: new Date(first + i * BUCKET_MS).toISOString(), status: 'none', checks: 0, bad: 0, detail: null })) + for (const c of [...checks].sort(byTime)) { + const i = Math.floor((c.checkedAt.getTime() - first) / BUCKET_MS) + if (i < 0 || i >= BUCKETS) continue + const b = out[i] + b.status = worse(b.status, c.status) + b.checks += 1 + if (c.status !== 'operational') { + b.bad += 1 + b.detail = c.detail + } + } + return out +} + +// Incidents from one list of checks (any services, any order): newest first, at most MAX_INCIDENTS. +export function incidents(meta: ServiceMeta[], checks: Check[], now: Date): Incident[] { + const out: Incident[] = [] + for (const m of meta) { + let run: Incident | undefined + for (const c of checks.filter((x) => x.service === m.id).sort(byTime)) { + if (c.status === 'operational') { + if (run) { + run.end = c.checkedAt.toISOString() + out.push(run) + run = undefined + } + continue + } + run ??= { service: m.id, name: m.name, status: 'degraded', start: c.checkedAt.toISOString(), end: null, minutes: 0, detail: null } + if (c.status === 'down') run.status = 'down' + run.detail = c.detail + } + if (run) out.push(run) + } + for (const i of out) i.minutes = Math.round(((i.end ? Date.parse(i.end) : now.getTime()) - Date.parse(i.start)) / 60_000) + return out.sort((a, b) => Date.parse(b.start) - Date.parse(a.start)).slice(0, MAX_INCIDENTS) +} + +// HelloNear's own API or engine down means the app is down; anything else down or slow means it is degraded. +export function overall(services: { id: string; group: Group; status: ServiceStatus }[]): CheckStatus { + if (services.some((s) => s.group === 'HelloNear' && s.status === 'down')) return 'down' + if (services.some((s) => s.status === 'down' || s.status === 'degraded')) return 'degraded' + return 'operational' +} + +// One report from every stored check of the last 7 days (any order), in the order of `meta`. +export function report(meta: ServiceMeta[], checks: Check[], now: Date): StatusReport { + const day = now.getTime() - 24 * 3600_000 + const week = now.getTime() - 7 * 24 * 3600_000 + const services = meta.map((m): ServiceReport => { + const mine = checks.filter((c) => c.service === m.id && c.checkedAt.getTime() > week).sort(byTime) + const last = mine.at(-1) + const fresh = last !== undefined && now.getTime() - last.checkedAt.getTime() <= STALE_MS + return { + ...m, + status: fresh ? last.status : 'unknown', + latencyMs: last?.latencyMs ?? null, + checkedAt: last ? last.checkedAt.toISOString() : null, + detail: last && !fresh ? 'No check in the last 15 minutes.' : (last?.detail ?? null), + uptime24h: uptime(mine.filter((c) => c.checkedAt.getTime() > day)), + uptime7d: uptime(mine), + history: buckets(mine, now), + } + }) + const recent = checks.filter((c) => c.checkedAt.getTime() > week) + return { updatedAt: now.toISOString(), overall: overall(services), services, incidents: incidents(meta, recent, now) } +} + +// A probe's verdict from how it went: an error or timeout is down, an answer slower than the threshold is degraded. +export function verdict(ok: boolean, latencyMs: number, degradedMs: number): CheckStatus { + if (!ok) return 'down' + return latencyMs > degradedMs ? 'degraded' : 'operational' +} diff --git a/apps/be/test/status-math.test.ts b/apps/be/test/status-math.test.ts new file mode 100644 index 00000000..91d4068f --- /dev/null +++ b/apps/be/test/status-math.test.ts @@ -0,0 +1,98 @@ +// The status page's numbers, without Postgres or the network. +import { test } from 'node:test' +import assert from 'node:assert/strict' +import { BUCKETS, BUCKET_MS, buckets, incidents, overall, report, uptime, verdict, type Check } from '../src/statusMath.ts' + +const now = new Date('2026-11-13T10:47:00Z') +const at = (minutesAgo: number) => new Date(now.getTime() - minutesAgo * 60_000) +const c = (service: string, minutesAgo: number, status: Check['status'], extra: Partial = {}): Check => ({ service, checkedAt: at(minutesAgo), status, latencyMs: 100, detail: null, ...extra }) + +test('uptime counts degraded as up, rounds to 2 decimals, and is null without checks', () => { + assert.equal(uptime([]), null) + assert.equal(uptime([c('a', 1, 'operational'), c('a', 2, 'degraded'), c('a', 3, 'down')]), 66.67) + assert.equal(uptime([c('a', 1, 'down')]), 0) + assert.equal(uptime([c('a', 1, 'operational')]), 100) +}) + +test('48 half-hour buckets, oldest first, ending with the one holding now; each the worst check inside', () => { + const b = buckets([c('a', 2, 'operational'), c('a', 5, 'down'), c('a', 20, 'degraded'), c('a', 50, 'operational'), c('a', 25 * 60, 'down')], now) + assert.equal(b.length, BUCKETS) + assert.equal(b.at(-1)!.start, '2026-11-13T10:30:00.000Z', 'the last bucket holds now') + assert.equal(new Date(b[1].start).getTime() - new Date(b[0].start).getTime(), BUCKET_MS) + assert.equal(b.at(-1)!.status, 'down', '10:42 down beats 10:45 operational') + assert.equal(b.at(-2)!.status, 'degraded', '10:27 degraded') + assert.equal(b.at(-3)!.status, 'operational', '09:57') + assert.equal(b[0].status, 'none', 'a check older than 24 h is outside') + assert.equal(b.filter((x) => x.status === 'none').length, BUCKETS - 3) +}) + +test('each bucket counts its checks and bad ones, with the last bad check\'s detail for the hover text', () => { + const b = buckets([ + c('a', 8, 'operational'), c('a', 6, 'down', { detail: 'No answer in 30 s.' }), c('a', 4, 'degraded', { detail: 'Slow: 4.0 s.' }), c('a', 2, 'operational'), + c('a', 35, 'operational'), c('a', 40, 'operational'), + ], now) + assert.deepEqual(b.at(-1), { start: '2026-11-13T10:30:00.000Z', status: 'down', checks: 4, bad: 2, detail: 'Slow: 4.0 s.' }) + assert.deepEqual(b.at(-2), { start: '2026-11-13T10:00:00.000Z', status: 'operational', checks: 2, bad: 0, detail: null }) + assert.deepEqual(b[0], { start: b[0].start, status: 'none', checks: 0, bad: 0, detail: null }) +}) + +test('incidents: one ongoing, one resolved, and two runs split by a single good check; newest first', () => { + const meta = [{ id: 'q', name: 'Quotes', group: 'NEAR' as const, impact: 'x' }, { id: 'e', name: 'Embeddings', group: 'NEAR AI' as const, impact: 'y' }] + const list = incidents(meta, [ + // q: degraded 10:02-10:07, then a good check, then down 10:17 to now (ongoing) + c('q', 45, 'degraded', { detail: 'Slow.' }), c('q', 40, 'degraded', { detail: 'Slower.' }), c('q', 35, 'operational'), + c('q', 30, 'down', { detail: 'HTTP 500.' }), c('q', 25, 'degraded', { detail: 'Slow again.' }), + // e: down 08:47-09:17 (resolved at 09:17), the last bad detail kept + c('e', 120, 'operational'), c('e', 115, 'down', { detail: 'No answer.' }), c('e', 100, 'down', { detail: 'Still nothing.' }), c('e', 90, 'operational'), c('e', 5, 'operational'), + ], now) + assert.deepEqual(list, [ + { service: 'q', name: 'Quotes', status: 'down', start: at(30).toISOString(), end: null, minutes: 30, detail: 'Slow again.' }, + { service: 'q', name: 'Quotes', status: 'degraded', start: at(45).toISOString(), end: at(35).toISOString(), minutes: 10, detail: 'Slower.' }, + { service: 'e', name: 'Embeddings', status: 'down', start: at(115).toISOString(), end: at(90).toISOString(), minutes: 25, detail: 'Still nothing.' }, + ]) +}) + +test('incidents are capped at 100, keeping the newest', () => { + const meta = [{ id: 'f', name: 'Flappy', group: 'Partners' as const, impact: 'z' }] + const checks: Check[] = [] + for (let i = 0; i < 150; i++) checks.push(c('f', 2 * i + 2, 'operational'), c('f', 2 * i + 1, 'down')) + const list = incidents(meta, checks, now) + assert.equal(list.length, 100) + assert.equal(list[0].start, at(1).toISOString()) +}) + +test('overall: HelloNear down is down; any other trouble is degraded; unknown changes nothing', () => { + assert.equal(overall([{ id: 'hellonear-api', group: 'HelloNear', status: 'down' }]), 'down') + assert.equal(overall([{ id: 'hellonear-api', group: 'HelloNear', status: 'operational' }, { id: 'near-intents-quote', group: 'NEAR', status: 'down' }]), 'degraded') + assert.equal(overall([{ id: 'x', group: 'Partners', status: 'degraded' }]), 'degraded') + assert.equal(overall([{ id: 'x', group: 'Partners', status: 'unknown' }, { id: 'y', group: 'NEAR', status: 'operational' }]), 'operational') +}) + +test('verdict: error is down, slower than the threshold is degraded', () => { + assert.equal(verdict(false, 10, 1000), 'down') + assert.equal(verdict(true, 1500, 1000), 'degraded') + assert.equal(verdict(true, 1000, 1000), 'operational') +}) + +test('report: services in the given order, latest check is current, a stale one is unknown, uptimes per window', () => { + const meta = [ + { id: 'b', name: 'B', group: 'NEAR' as const, impact: 'B breaks.' }, + { id: 'a', name: 'A', group: 'HelloNear' as const, impact: 'A breaks.' }, + { id: 'z', name: 'Z', group: 'Partners' as const, impact: 'Z breaks.' }, + ] + const r = report(meta, [ + c('a', 1, 'operational', { latencyMs: 42 }), c('a', 6, 'down'), + c('b', 30, 'degraded', { detail: 'Slow.' }), c('b', 3 * 24 * 60, 'down'), + c('ignored', 1, 'down'), + ], now) + assert.deepEqual(r.services.map((s) => s.id), ['b', 'a', 'z']) + const [b, a, z] = r.services + assert.deepEqual([a.status, a.latencyMs, a.checkedAt, a.uptime24h], ['operational', 42, at(1).toISOString(), 50]) + assert.equal(b.status, 'unknown', 'last check 30 min ago is stale') + assert.equal(b.detail, 'No check in the last 15 minutes.') + assert.deepEqual([b.uptime24h, b.uptime7d], [100, 50]) + assert.deepEqual([z.status, z.checkedAt, z.uptime7d, z.history.every((h) => h.status === 'none')], ['unknown', null, null, true]) + assert.equal(r.overall, 'operational', 'a is up now; b and z are unknown') + assert.equal(r.updatedAt, now.toISOString()) + assert.deepEqual(r.incidents.map((i) => [i.service, i.status, i.end === null]), [['a', 'down', false], ['b', 'down', true]], 'a was resolved by its next check; b went from down to degraded with no good check, so it is still ongoing') +}) diff --git a/apps/be/test/status.test.ts b/apps/be/test/status.test.ts new file mode 100644 index 00000000..ef655656 --- /dev/null +++ b/apps/be/test/status.test.ts @@ -0,0 +1,191 @@ +// The status page's data, end to end: real probes against fake upstreams (some up, one slow, one failing), stored in +// the local Postgres, and served by GET /v1/status. Run: bun run test (needs `docker compose up -d postgres`). +import { after, before, test } from 'node:test' +import assert from 'node:assert/strict' +import { createServer, type IncomingMessage, type Server } from 'node:http' +import type { AddressInfo } from 'node:net' +import { randomBytes } from 'node:crypto' + +const ENGINE_KEY = 'status-test-engine-key' +let engineUp = true +const seenEngineKeys: string[] = [] + +// One fake per upstream, each a tiny server; `slow` answers after 3.2 s (OutLayer's degraded threshold is 3 s). +function fake(handle: (req: IncomingMessage, body: string) => { status: number; body?: unknown; delayMs?: number }): Server { + return createServer((req, res) => { + let raw = '' + req.on('data', (c) => (raw += c)) + req.on('end', () => { + const out = handle(req, raw) + setTimeout(() => res.writeHead(out.status, { 'content-type': 'application/json' }).end(out.body === undefined ? '' : JSON.stringify(out.body)), out.delayMs ?? 0) + }) + }) +} + +const engine = fake((req) => { + seenEngineKeys.push(String(req.headers['x-engine-key'] ?? '')) + if (!engineUp) return { status: 503, body: { error: 'down' } } + if (req.url === '/v1/health') return { status: 200, body: { ok: true } } + if (req.url === '/internal/v1/probes') { + return { status: 200, body: { probes: [ + { id: 'nearai-chat', status: 'operational', latencyMs: 812, detail: null }, + { id: 'nearai-embeddings', status: 'down', latencyMs: null, detail: 'No answer in 15 s.' }, + { id: 'something-else', status: 'down', latencyMs: 1, detail: 'ignored' }, + ] } } + } + return { status: 404 } +}) +let quoteBody: any +const oneClick = fake((req, body) => { + quoteBody = JSON.parse(body || '{}') + return req.url === '/v0/quote' ? { status: 200, body: { quote: { amountOut: '9990000' } } } : { status: 404 } +}) +const outlayer = fake((req) => (req.url === '/health' ? { status: 200, body: { ok: true }, delayMs: 3200 } : { status: 404 })) +const rpc = fake(() => ({ status: 200, body: { jsonrpc: '2.0', id: 'status', result: { sync_info: { syncing: false } } } })) +const market = fake(() => ({ status: 500, body: { error: 'boom' } })) +const tee = fake((req) => ({ status: req.method === 'HEAD' ? 200 : 405 })) + +const servers = [engine, oneClick, outlayer, rpc, market, tee] +for (const s of servers) await new Promise((r) => s.listen(0, '127.0.0.1', r)) +const url = (s: Server) => `http://127.0.0.1:${(s.address() as AddressInfo).port}` + +process.env.ENGINE_URL = url(engine) +process.env.ENGINE_API_KEY = ENGINE_KEY +process.env.OUTLAYER_URL = url(outlayer) +process.env.STATUS_ONECLICK_URL = url(oneClick) +process.env.STATUS_NEAR_RPC_URL = url(rpc) +process.env.STATUS_MARKET_URL = url(market) +process.env.TEE_EXPLORER_URL = url(tee) +process.env.DATABASE_URL ??= 'postgresql://hellonear:hellonear-dev-only@localhost:55432/hellonear' +process.env.SECRETS_MASTER_KEY = randomBytes(32).toString('base64') + +const { server } = await import('../src/server.ts') +const { migrate, pool, query } = await import('../src/db.ts') +const { runProbes, resetStatusCache, SERVICES } = await import('../src/status.ts') + +let base = '' +before(async () => { + await migrate() + await query('delete from service_checks') + await new Promise((r) => server.listen(0, '127.0.0.1', r)) + base = url(server) +}) +after(async () => { + for (const s of [server, ...servers]) { s.closeAllConnections(); s.close() } + // The fake upstreams' rows must not stay in a dev database a local backend reads (seen 13 Nov: 4 ms 1Click "answers"). + await query('delete from service_checks') + await pool.end() +}) + +const ORDER = ['hellonear-api', 'hellonear-engine', 'nearai-chat', 'nearai-embeddings', 'near-intents-quote', 'outlayer', 'near-rpc', 'agent-market', 'tee-explorer'] + +test('one round: every service checked at once, one failing never stops the others, statuses as expected', async () => { + const results = await runProbes() + const by = Object.fromEntries(results.map((r) => [r.service, r])) + assert.deepEqual(Object.keys(by).sort(), [...ORDER].sort(), 'one result per service, the engine\'s extra probe dropped') + assert.equal(by['hellonear-api'].status, 'operational') + assert.equal(by['hellonear-engine'].status, 'operational') + assert.equal(by['nearai-chat'].status, 'operational') + assert.deepEqual([by['nearai-embeddings'].status, by['nearai-embeddings'].detail], ['down', 'No answer in 15 s.']) + assert.equal(by['near-intents-quote'].status, 'operational') + assert.equal(by['outlayer'].status, 'degraded', 'answered after 3.2 s, over its 3 s threshold') + assert.match(String(by['outlayer'].detail), /^Slow: answered in 3\.\d s\.$/) + assert.equal(by['near-rpc'].status, 'operational') + assert.deepEqual([by['agent-market'].status, by['agent-market'].detail], ['down', 'Answered HTTP 500.']) + assert.equal(by['tee-explorer'].status, 'operational') + assert.ok(seenEngineKeys.every((k) => k === ENGINE_KEY), 'the engine is called with its key') + assert.equal(quoteBody.dry, true, 'the 1Click check is a dry quote: nothing can move') + const rows = await query<{ n: string }>('select count(*) as n from service_checks') + assert.equal(Number(rows[0].n), ORDER.length) +}) + +test('GET /v1/status: the exact shape, public CORS, cache header, order and the stored statuses', async () => { + resetStatusCache() + const res = await fetch(`${base}/v1/status`, { headers: { origin: 'https://status.hellonear.xyz' } }) + assert.equal(res.status, 200) + assert.equal(res.headers.get('access-control-allow-origin'), '*') + assert.equal(res.headers.get('access-control-allow-credentials'), null, 'no credentials on a public route') + assert.equal(res.headers.get('cache-control'), 'public, max-age=30') + assert.match(String(res.headers.get('content-type')), /^application\/json/) + const body = await res.json() + assert.deepEqual(Object.keys(body).sort(), ['incidents', 'overall', 'services', 'updatedAt']) + assert.ok(!Number.isNaN(Date.parse(body.updatedAt))) + assert.equal(body.overall, 'degraded', 'partners and NEAR AI trouble, but HelloNear itself is up') + assert.deepEqual(body.services.map((s: { id: string }) => s.id), ORDER) + for (const s of body.services) { + assert.deepEqual(Object.keys(s).sort(), ['checkedAt', 'detail', 'group', 'history', 'id', 'impact', 'latencyMs', 'name', 'status', 'uptime24h', 'uptime7d']) + assert.ok(['HelloNear', 'NEAR AI', 'NEAR', 'Partners'].includes(s.group)) + assert.ok(s.impact.length > 10 && s.name.length > 2) + assert.equal(s.history.length, 48) + for (const h of s.history) { + assert.deepEqual(Object.keys(h).sort(), ['bad', 'checks', 'detail', 'start', 'status']) + assert.ok(['operational', 'degraded', 'down', 'none'].includes(h.status) && !Number.isNaN(Date.parse(h.start))) + assert.ok(h.bad <= h.checks && (h.status !== 'none' || h.checks === 0)) + } + assert.notEqual(s.history.at(-1).status, 'none', 'the current half hour holds the check just made') + } + const by = Object.fromEntries(body.services.map((s: { id: string }) => [s.id, s])) + assert.deepEqual([by['agent-market'].status, by['agent-market'].uptime24h, by['agent-market'].history.at(-1).status], ['down', 0, 'down']) + assert.deepEqual(by['agent-market'].history.at(-1), { start: by['agent-market'].history.at(-1).start, status: 'down', checks: 1, bad: 1, detail: 'Answered HTTP 500.' }) + assert.deepEqual(by['tee-explorer'].history.at(-1), { start: by['tee-explorer'].history.at(-1).start, status: 'operational', checks: 1, bad: 0, detail: null }) + // Every non-operational service is one ongoing incident; operational ones have none. + assert.deepEqual(body.incidents.map((i: { service: string }) => i.service).sort(), ['agent-market', 'nearai-embeddings', 'outlayer']) + const market = body.incidents.find((i: { service: string }) => i.service === 'agent-market') + assert.deepEqual(Object.keys(market).sort(), ['detail', 'end', 'minutes', 'name', 'service', 'start', 'status']) + assert.deepEqual([market.name, market.status, market.end, market.minutes, market.detail], ['NEAR AI Agent Market', 'down', null, 0, 'Answered HTTP 500.']) + assert.equal(body.incidents.find((i: { service: string }) => i.service === 'outlayer').status, 'degraded') + assert.deepEqual([by['outlayer'].status, by['outlayer'].uptime24h], ['degraded', 100]) + assert.deepEqual([by['nearai-chat'].status, by['nearai-chat'].latencyMs], ['operational', 812]) + assert.equal(by['hellonear-api'].group, 'HelloNear') + assert.deepEqual(SERVICES.map((s) => s.id), ORDER) + const text = JSON.stringify(body) + assert.ok(!text.includes(ENGINE_KEY) && !text.includes('127.0.0.1'), 'no key and no upstream URL in the answer') +}) + +test('OPTIONS is answered for any origin; other methods are refused; the app routes keep their own CORS', async () => { + const pre = await fetch(`${base}/v1/status`, { method: 'OPTIONS', headers: { origin: 'https://status.hellonear.xyz', 'access-control-request-method': 'GET' } }) + assert.equal(pre.status, 204) + assert.equal(pre.headers.get('access-control-allow-origin'), '*') + const post = await fetch(`${base}/v1/status`, { method: 'POST', headers: { origin: 'https://status.hellonear.xyz' } }) + assert.equal(post.status, 405) + const health = await fetch(`${base}/v1/health`, { headers: { origin: 'https://status.hellonear.xyz' } }) + assert.equal(health.headers.get('access-control-allow-origin'), null, 'other routes still allow the web app only') +}) + +test('engine down: the engine check is down, NEAR AI is not guessed (nothing stored for it this round)', async () => { + engineUp = false + try { + await query('delete from service_checks') + const results = await runProbes() + const by = Object.fromEntries(results.map((r) => [r.service, r])) + assert.equal(by['hellonear-engine'].status, 'down') + assert.equal(by['nearai-chat'], undefined) + assert.equal(by['nearai-embeddings'], undefined) + assert.equal(by['near-intents-quote'].status, 'operational', 'the rest still ran') + resetStatusCache() + const body = await (await fetch(`${base}/v1/status`)).json() + const s = Object.fromEntries(body.services.map((x: { id: string }) => [x.id, x])) + assert.equal(body.overall, 'down', 'HelloNear\'s own engine is down') + assert.deepEqual([s['nearai-chat'].status, s['nearai-chat'].checkedAt], ['unknown', null]) + } finally { + engineUp = true + } +}) + +test('the answer is cached about 30 s, and a new round clears the cache', async () => { + resetStatusCache() + const first = await (await fetch(`${base}/v1/status`)).json() + await query(`insert into service_checks (service, status, latency_ms, detail) values ('tee-explorer', 'down', null, 'test row')`) + const cachedBody = await (await fetch(`${base}/v1/status`)).json() + assert.equal(cachedBody.updatedAt, first.updatedAt, 'served from the cache') + await runProbes() + const fresh = await (await fetch(`${base}/v1/status`)).json() + assert.notEqual(fresh.updatedAt, first.updatedAt, 'a probe round drops the cached answer') +}) + +test('rows older than 8 days are deleted by a round', async () => { + await query(`insert into service_checks (service, checked_at, status) values ('outlayer', now() - interval '9 days', 'down')`) + await runProbes() + const old = await query<{ n: string }>(`select count(*) as n from service_checks where checked_at < now() - interval '8 days'`) + assert.equal(Number(old[0].n), 0) +}) diff --git a/apps/status/.gitignore b/apps/status/.gitignore new file mode 100644 index 00000000..cc1b7f16 --- /dev/null +++ b/apps/status/.gitignore @@ -0,0 +1 @@ +tsconfig.tsbuildinfo diff --git a/apps/status/AGENTS.md b/apps/status/AGENTS.md new file mode 100644 index 00000000..af3b7c84 --- /dev/null +++ b/apps/status/AGENTS.md @@ -0,0 +1,9 @@ + + +## This is NOT the Next.js you know + +This version has breaking changes — APIs, conventions, and file structure may all differ from your training data. Read the relevant guide in `node_modules/next/dist/docs/` (resolved from this file's directory; in monorepos the `next` package may not be visible from the repo root) before writing any code. Heed deprecation notices. + +This block is written and re-added by `next dev` — verify at `node_modules/next/dist/server/lib/generate-agent-files.js`. Removing it from a diff only re-creates the uncommitted change; committing it with your work keeps the tree clean. + + diff --git a/apps/status/CLAUDE.md b/apps/status/CLAUDE.md new file mode 100644 index 00000000..6db2acd1 --- /dev/null +++ b/apps/status/CLAUDE.md @@ -0,0 +1,35 @@ +# CLAUDE.md — apps/status + +The public status page, **status.hellonear.xyz**: whether HelloNear and the services it runs on answer (NEAR AI Cloud chat and embeddings, NEAR Intents 1Click quotes, OutLayer, NEAR RPC, NEAR AI Agent Market, Phala TEE Explorer). Built 13 Nov 2026 after two outages from outside our code in one night (NEAR AI embeddings hung, 1Click quotes timed out). Owner: Axel. Root rules in `/CLAUDE.md` also apply. + +**Update this file in the same PR whenever you change setup, commands, env vars, the data contract or where it is deployed.** + +## How it works + +- The **backend** probes every service every 5 minutes, keeps 8 days of results and serves them at `GET https://api.hellonear.xyz/v1/status` (public, CORS `*`, cached 30 s; contract in `docs/api-contract.md` and `lib/status.ts`). This app only reads that. +- One server-rendered page (`app/page.tsx`), revalidated every 30 s (`revalidate`), so it works without JavaScript. A small client component (`AutoRefresh.tsx`) refreshes it every minute while the tab is visible. +- If the status API can't be read, the page says so ("Can't reach the status API"); it never shows everything as down. +- 48 bars of 30 minutes (the last 24 h), each the worst check in it; uptime counts slow answers as up. A down service shows its `impact` line (what users see); a slow one says it still works. +- **Hover a bar** for its half hour: time range, state, about how long it was bad (`bad` checks × 5 min), the last problem's detail and how many checks ran (optional bucket fields `checks`, `bad`, `detail`). CSS only; hidden on touch screens. Times are UTC everywhere, said so on the page, since it is rendered on the server. +- **Past incidents:** the backend's `incidents` (runs of failed checks of one service, last 7 days) listed by UTC day like status.claude.com, Ongoing or Resolved with the duration; a quiet day reads "No incidents reported." +- Styles: plain CSS (`app/globals.css`) with the web app's Stone & Lime tokens, light and dark by `prefers-color-scheme`. Icons are the landing's. + +## Commands + +```sh +bun install +bun run dev # http://localhost:3200, reads STATUS_API_URL +STATUS_FIXTURE=1 bun run dev # sample data (the 13 Nov outages), no backend needed +bun run typecheck +bun run test # node:test, lib/ only +bun run build +``` + +## Env + +- `STATUS_API_URL`: where `/v1/status` lives. Default `https://api.hellonear.xyz/v1/status`; for a local backend `http://localhost:4000/v1/status`. +- `STATUS_FIXTURE=1`: use `lib/fixture.ts` instead of the API (local work only). + +## Deploy + +Not on the Proxmox box: on **Vercel**, deployed by Axel with the Vercel CLI from this folder (`vercel` then `vercel --prod`), root directory `apps/status`. Then the subdomain: a `CNAME status → cname.vercel-dns.com` in the `hellonear.xyz` Cloudflare zone (DNS only, not proxied) and the domain added to the Vercel project. That DNS change is on the box owner's side (James), with Axel's approval. diff --git a/apps/status/app/AutoRefresh.tsx b/apps/status/app/AutoRefresh.tsx new file mode 100644 index 00000000..1483acde --- /dev/null +++ b/apps/status/app/AutoRefresh.tsx @@ -0,0 +1,22 @@ +"use client"; + +import { useRouter } from "next/navigation"; +import { useEffect } from "react"; + +// The backend probes every 5 minutes and caches /v1/status for 30 s, so a refresh a minute is plenty. Only while +// the tab is visible, so a background tab doesn't keep asking. +export function AutoRefresh({ everyMs = 60_000 }: { everyMs?: number }) { + const router = useRouter(); + useEffect(() => { + const tick = () => { + if (document.visibilityState === "visible") router.refresh(); + }; + const id = window.setInterval(tick, everyMs); + document.addEventListener("visibilitychange", tick); + return () => { + window.clearInterval(id); + document.removeEventListener("visibilitychange", tick); + }; + }, [router, everyMs]); + return null; +} diff --git a/apps/status/app/apple-icon.png b/apps/status/app/apple-icon.png new file mode 100644 index 00000000..7df2cef0 Binary files /dev/null and b/apps/status/app/apple-icon.png differ diff --git a/apps/status/app/favicon.ico b/apps/status/app/favicon.ico new file mode 100644 index 00000000..ebdc91c3 Binary files /dev/null and b/apps/status/app/favicon.ico differ diff --git a/apps/status/app/globals.css b/apps/status/app/globals.css new file mode 100644 index 00000000..44d7aea9 --- /dev/null +++ b/apps/status/app/globals.css @@ -0,0 +1,143 @@ +/* The web app's Stone & Lime tokens (apps/fe/src/styles/globals.css), light and dark. */ +:root { + --bg: #F3F2EE; + --fg: #161614; + --muted: #5C5A54; + --card: #FFFFFF; + --border: #E0DED6; + --bar-none: #E0DED6; + --brand: #D8F04A; + --ok: #1A7340; + --ok-bar: #3FA76A; + --ok-soft: #E3F2E8; + --warn: #945800; + --warn-bar: #E3A12B; + --warn-soft: #FBEFD9; + --bad: #BF3220; + --bad-bar: #D9503C; + --bad-soft: #FBE6E2; + color-scheme: light; +} +@media (prefers-color-scheme: dark) { + :root { + --bg: #11110F; + --fg: #F2F1EC; + --muted: #A6A39B; + --card: #1A1A18; + --border: #2C2C29; + --bar-none: #2C2C29; + --ok: #46C27A; + --ok-bar: #46C27A; + --ok-soft: #12261A; + --warn: #F0B43C; + --warn-bar: #F0B43C; + --warn-soft: #2B2210; + --bad: #FF6A55; + --bad-bar: #FF6A55; + --bad-soft: #2E1512; + color-scheme: dark; + } +} + +* { box-sizing: border-box; } +html, body { margin: 0; } +body { + background: var(--bg); + color: var(--fg); + font-family: var(--font-geist), ui-sans-serif, system-ui, sans-serif; + font-size: 15px; + line-height: 1.5; + -webkit-font-smoothing: antialiased; +} +a { color: inherit; } + +.wrap { max-width: 760px; margin: 0 auto; padding: 32px 16px 64px; } +.muted { color: var(--muted); font-weight: 400; } + +.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 32px; } +.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; font-size: 22px; font-weight: 600; letter-spacing: -0.3px; } +.ghost { border: 1px solid var(--border); border-radius: 999px; padding: 8px 14px; text-decoration: none; font-size: 14px; background: var(--card); white-space: nowrap; } +.ghost:hover { border-color: var(--muted); } + +.banner { border-radius: 16px; padding: 20px 22px; margin-bottom: 32px; border: 1px solid transparent; } +.banner h1 { margin: 0; font-size: 20px; letter-spacing: -0.2px; } +.banner .sub { margin: 4px 0 0; color: var(--muted); font-size: 13px; } +.banner.operational { background: var(--ok-soft); border-color: color-mix(in oklab, var(--ok) 30%, transparent); } +.banner.operational h1 { color: var(--ok); } +.banner.degraded { background: var(--warn-soft); border-color: color-mix(in oklab, var(--warn) 30%, transparent); } +.banner.degraded h1 { color: var(--warn); } +.banner.down { background: var(--bad-soft); border-color: color-mix(in oklab, var(--bad) 30%, transparent); } +.banner.down h1 { color: var(--bad); } +.banner p { margin: 8px 0 0; } +.incidents { margin: 12px 0 0; padding-left: 18px; } +.incidents li { margin: 6px 0; } + +.group { margin-bottom: 28px; } +.group h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.6px; color: var(--muted); margin: 0 0 8px; font-weight: 600; } +.card { background: var(--card); border: 1px solid var(--border); border-radius: 16px; } +.row { padding: 16px 18px; border-top: 1px solid var(--border); } +.row:first-child { border-top: 0; } +.rowtop { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; } +.name { font-weight: 600; } +.state { font-size: 14px; font-weight: 500; white-space: nowrap; } +.state.operational { color: var(--ok); } +.state.degraded { color: var(--warn); } +.state.down { color: var(--bad); } +.state.unknown { color: var(--muted); } + +.bars { display: flex; gap: 2px; height: 32px; margin: 12px 0 6px; } +.bar { position: relative; flex: 1 1 0; min-width: 0; border-radius: 2px; background: var(--bar-none); } +.bar:hover { filter: brightness(0.85); } + +/* Hover a bar to see its half hour (CSS only, no JavaScript). */ +.tip { + position: absolute; top: calc(100% + 10px); left: 50%; transform: translateX(-50%) translateY(-4px); + z-index: 10; width: max-content; max-width: 260px; padding: 12px 14px; border-radius: 12px; + background: var(--card); border: 1px solid var(--border); box-shadow: 0 8px 24px rgb(0 0 0 / 0.18); + display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--fg); + opacity: 0; visibility: hidden; pointer-events: none; transition: opacity 120ms ease-out, transform 120ms ease-out; +} +.tip.left { left: 0; transform: translateY(-4px); } +.tip.right { left: auto; right: 0; transform: translateY(-4px); } +.bar:hover .tip { opacity: 1; visibility: visible; transform: translateX(-50%) translateY(0); } +.bar:hover .tip.left, .bar:hover .tip.right { transform: translateY(0); } +.tipwhen { font-weight: 600; } +.tipstate { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 8px; background: color-mix(in oklab, var(--fg) 6%, transparent); font-weight: 600; } +.tipstate .dot { width: 8px; height: 8px; border-radius: 999px; background: var(--bar-none); flex: none; } +.tipstate.ok .dot { background: var(--ok-bar); } +.tipstate.partial .dot { background: var(--warn-bar); } +.tipstate.major .dot { background: var(--bad-bar); } +.tipmin { margin-left: auto; padding-left: 12px; font-weight: 500; color: var(--muted); } +.tipdetail { color: var(--fg); } +.tipok { color: var(--fg); } +.tipmeta { color: var(--muted); font-size: 12px; } +@media (prefers-reduced-motion: reduce) { .tip { transition: none; } } +@media (hover: none) { .tip { display: none; } } +.bar.ok { background: var(--ok-bar); } +.bar.partial { background: var(--warn-bar); } +.bar.major { background: var(--bad-bar); } +.axis { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 12px; white-space: nowrap; } +.axis .line { flex: 1; height: 1px; background: var(--border); } +.impact { margin: 10px 0 0; font-size: 14px; } +.meta { margin: 6px 0 0; color: var(--muted); font-size: 12px; } + +.past { margin-top: 40px; } +.pasttitle { font-size: 22px; letter-spacing: -0.3px; margin: 0; } +.pastnote { color: var(--muted); font-size: 13px; margin: 4px 0 16px; } +.day h3 { font-size: 16px; margin: 20px 0 0; padding-bottom: 8px; border-bottom: 1px solid var(--border); } +.quiet { color: var(--muted); margin: 10px 0 0; } +.incident { margin-top: 12px; } +.incident p { margin: 2px 0; } +.ititle { font-weight: 600; font-size: 16px; } +.ititle.down { color: var(--bad); } +.ititle.degraded { color: var(--warn); } +.foot { margin-top: 40px; color: var(--muted); font-size: 13px; } +.foot p { margin: 4px 0; } + +@media (max-width: 480px) { + .wrap { padding-top: 20px; } + .brand { font-size: 19px; } + .bars { gap: 1px; height: 28px; } + .axis .line { display: none; } + .axis { justify-content: space-between; } +} diff --git a/apps/status/app/icon.svg b/apps/status/app/icon.svg new file mode 100644 index 00000000..0f11bfa2 --- /dev/null +++ b/apps/status/app/icon.svg @@ -0,0 +1 @@ + diff --git a/apps/status/app/layout.tsx b/apps/status/app/layout.tsx new file mode 100644 index 00000000..57ff4efb --- /dev/null +++ b/apps/status/app/layout.tsx @@ -0,0 +1,22 @@ +import type { Metadata } from "next"; +import { Geist } from "next/font/google"; +import "./globals.css"; + +const geist = Geist({ subsets: ["latin"], variable: "--font-geist" }); + +const DESCRIPTION = "Live status of HelloNear and the services it runs on: NEAR AI Cloud, NEAR Intents, OutLayer, NEAR RPC and more."; + +export const metadata: Metadata = { + metadataBase: new URL("https://status.hellonear.xyz"), + title: "HelloNear Status", + description: DESCRIPTION, + openGraph: { type: "website", siteName: "HelloNear", url: "/", title: "HelloNear Status", description: DESCRIPTION }, +}; + +export default function RootLayout({ children }: { children: React.ReactNode }) { + return ( + + {children} + + ); +} diff --git a/apps/status/app/page.tsx b/apps/status/app/page.tsx new file mode 100644 index 00000000..0123e5eb --- /dev/null +++ b/apps/status/app/page.tsx @@ -0,0 +1,167 @@ +import { AutoRefresh } from "./AutoRefresh"; +import { GROUPS, HEADLINE, LABEL, ago, bucketLines, duration, incidentDays, incidents, loadStatus, pct, type Service, type Status } from "@/lib/status"; + +// Rendered on the server and revalidated with the data (lib/status.ts), so the page works without JavaScript. +export const revalidate = 30; + +export default async function Page() { + const status = await loadStatus(); + return ( +
+ +
+ + {/* eslint-disable-next-line @next/next/no-img-element */} + + + HelloNear Status + + + + Open the app + +
+ {status ? : } +
+

+ Checked every 5 minutes from the HelloNear backend. Each bar is 30 minutes, the last 24 hours (hover one for details); uptime counts slow answers as up. Times are UTC. +

+

+ During the hackathon everything runs on NEAR testnet.{" "} + Source and README +

+
+
+ ); +} + +function Unreachable() { + return ( +
+

Can't reach the status API

+

This page couldn't read api.hellonear.xyz just now. That alone doesn't mean the app is down; try again in a minute.

+
+ ); +} + +function Board({ status }: { status: Status }) { + const open = incidents(status); + return ( + <> +
+

{HEADLINE[status.overall]}

+

Updated {ago(status.updatedAt)}

+ {open.length > 0 && ( +
    + {open.map((s) => ( +
  • + {/* The impact line says what breaks when a service is down; a slow one still works. */} + {s.name} {s.status === "down" ? "is down" : "is answering slowly"} + {s.detail ? ` (${s.detail})` : ""}.{s.status === "down" ? ` ${s.impact}` : " It still works."} +
  • + ))} +
+ )} +
+ {GROUPS.map((g) => { + const services = status.services.filter((s) => s.group === g); + if (services.length === 0) return null; + return ( +
+

{g}

+
+ {services.map((s) => ( + + ))} +
+
+ ); + })} + {status.incidents ? : null} + + ); +} + +const clock = (iso: string) => new Date(iso).toLocaleTimeString("en-GB", { hour: "2-digit", minute: "2-digit", timeZone: "UTC" }); + +function PastIncidents({ status }: { status: Status }) { + return ( +
+

Past incidents

+

Worked out from the checks: failed checks in a row of one service make one incident. Times in UTC.

+ {incidentDays(status.incidents ?? []).map(({ day, items }) => ( +
+

{new Date(`${day}T00:00:00Z`).toLocaleDateString("en-GB", { day: "numeric", month: "short", year: "numeric", timeZone: "UTC" })}

+ {items.length === 0 ? ( +

No incidents reported.

+ ) : ( + items.map((x) => ( +
+

+ {x.name} {x.status === "down" ? "down" : "slow"} +

+

+ {x.end ? "Resolved" : "Ongoing"} + {" - "} + {x.end ? `From ${clock(x.start)} to ${clock(x.end)}` : `Since ${clock(x.start)}`} ({duration(x.minutes)}). + {x.detail ? ` ${x.detail}.` : ""} +

+
+ )) + )} +
+ ))} +
+ ); +} + +function Row({ s }: { s: Service }) { + return ( +
+
+ {s.name} + + {LABEL[s.status]} + {s.latencyMs !== null && s.status !== "down" ? · {s.latencyMs} ms : null} + +
+
+ {s.history.map((b, i) => { + const l = bucketLines(b, "en-GB"); + // Tooltips near the edges open inward, so they never run off the card. + const side = i < 8 ? "left" : i > s.history.length - 9 ? "right" : "mid"; + return ( + + + {l.when} + {l.level === "ok" ? ( + No downtime recorded in this half hour. + ) : ( + + + )} + {l.detail ? {l.detail} : null} + {b.checks ? {b.checks} {b.checks === 1 ? "check" : "checks"}{b.bad ? `, ${b.bad} with a problem` : ", all fine"} : null} + + + ); + })} +
+
+ 24 h ago + + {pct(s.uptime24h)} + + Now +
+ {s.status === "down" ?

{s.impact}

: null} +

+ Checked {ago(s.checkedAt)} + {s.uptime7d !== null ? ` · 7 days: ${s.uptime7d.toFixed(2)}%` : ""} +

+
+ ); +} diff --git a/apps/status/bun.lock b/apps/status/bun.lock new file mode 100644 index 00000000..4141bc7a --- /dev/null +++ b/apps/status/bun.lock @@ -0,0 +1,200 @@ +{ + "lockfileVersion": 1, + "configVersion": 1, + "workspaces": { + "": { + "name": "@hellonear/status", + "dependencies": { + "next": "16.4.4", + "react": "19.3.1", + "react-dom": "19.3.1", + }, + "devDependencies": { + "@types/node": "26.6.6", + "@types/react": "19.3.0", + "@types/react-dom": "19.3.0", + "tsx": "4.23.15", + "typescript": "6.0.3", + }, + }, + }, + "packages": { + "@emnapi/runtime": ["@emnapi/runtime@1.11.3", "", { "dependencies": { "tslib": "^2.4.0" } }, "sha512-Xz4Tpyki7XyrpbUK1jR1AhdAdaXyhhY4lZ3neLodmhpuWfy2PAQN5B46sAiU4liOXGLkHypn/qU+jvfWSCYYLA=="], + + "@esbuild/aix-ppc64": ["@esbuild/aix-ppc64@0.28.2", "", { "os": "aix", "cpu": "ppc64" }, "sha512-XExcO+dvLKvVtNTibSTBej1NCAbaGhWn9Ww1ZPx80qsahhPFe/8jgWP0IchNe0F3HwkU7n8ejhH8bjonqht8mQ=="], + + "@esbuild/android-arm": ["@esbuild/android-arm@0.28.2", "", { "os": "android", "cpu": "arm" }, "sha512-kXXoiPVVGQcnIYGOeaovwOURpniDBpSq4A03qkQ+BMQqtGG6HYap3xne9C1O1yo4TR3qxlCX5IqqmX6fFo2Lqg=="], + + "@esbuild/android-arm64": ["@esbuild/android-arm64@0.28.2", "", { "os": "android", "cpu": "arm64" }, "sha512-5YfKeeI8qWfBZIX+u2xZC3Zlb3Os/gLS2sbEKM+I4ZOcsWmHS2WLysCcQZDAFRslDUU5Oiq44gf6PYN1vGwG5A=="], + + "@esbuild/android-x64": ["@esbuild/android-x64@0.28.2", "", { "os": "android", "cpu": "x64" }, "sha512-O387ite7SzUyCcy3JQX4P4bLtEA7bLLkx+esve5JHnyYfNTxcVpXZo9jhdB0lTKN44gztELTdU7nS8Nr16Fs1Q=="], + + "@esbuild/darwin-arm64": ["@esbuild/darwin-arm64@0.28.2", "", { "os": "darwin", "cpu": "arm64" }, "sha512-n4KqkOQrraxHJcgjM1RvwbigfQKIKJVpM7xp+KsxiyUSrRdIXnt73VhrPAx0fV44hgfmIVKjxMN9J1t5jySVkw=="], + + "@esbuild/darwin-x64": ["@esbuild/darwin-x64@0.28.2", "", { "os": "darwin", "cpu": "x64" }, "sha512-uq6suIWYP37qzGddBKPw5QEQPi6HiLGsO7UmkpfyaYNQ3D+rN6w6WfwH+nuqcGXWvawGwxOEroO4YGnFh95azw=="], + + "@esbuild/freebsd-arm64": ["@esbuild/freebsd-arm64@0.28.2", "", { "os": "freebsd", "cpu": "arm64" }, "sha512-n+I0BTSRIoy+d6RPKnEVwql5UwBJolytvY4mAOIEJorKlqgPII8ix6slVVrfZ5Tnj7glIZvloylbB/EJPMWEXw=="], + + "@esbuild/freebsd-x64": ["@esbuild/freebsd-x64@0.28.2", "", { "os": "freebsd", "cpu": "x64" }, "sha512-78XJTJkvPs0kz2w61301PJjXl4g7q3JqiYMZ/M/yVI73EHBrCRTgkhu9oqG7vPqq+a/yadEW8aD+agKlk5xrmg=="], + + "@esbuild/linux-arm": ["@esbuild/linux-arm@0.28.2", "", { "os": "linux", "cpu": "arm" }, "sha512-XlDnu2q5yoqems+xay6wSAcg9DDD7K9RLKZEBOMZm3ckNpJBvOX20tSfby8KfrrhINDyv9V2YVZKY/SpoGJI8w=="], + + "@esbuild/linux-arm64": ["@esbuild/linux-arm64@0.28.2", "", { "os": "linux", "cpu": "arm64" }, "sha512-pW4AC0P3it8c7do9MVM4p51FzHzdM/TZrerurgRcHJ2WTa1VQ1CIq18xncfpBJw4ojkiZZrKW2yIBWBP92j6Ug=="], + + "@esbuild/linux-ia32": ["@esbuild/linux-ia32@0.28.2", "", { "os": "linux", "cpu": "ia32" }, "sha512-CYbnj78HsIeA+DhgUKgFCfvNsTHFhMMrinUrMZpDXJXKN8T3XViTZ/+wtHeVxEWY8ewSzTFN+nRmSwO2tZaLUQ=="], + + "@esbuild/linux-loong64": ["@esbuild/linux-loong64@0.28.2", "", { "os": "linux", "cpu": "none" }, "sha512-buwkd8nsph4R+ajRvw0qM5Hja/TXQow3ptzWO2EbG/cqcIkHloRrdlBtQlshyYGTNFvfkfJ5tpPLVkY4DtsPfQ=="], + + "@esbuild/linux-mips64el": ["@esbuild/linux-mips64el@0.28.2", "", { "os": "linux", "cpu": "none" }, "sha512-ZVykbDyk7519VwiNb9Lcj9m8XM6v5V9uKPvrEMkkEedVewf+0itkhahp4HDpgERXhwLRpWFypsGbG/J8s0QjJA=="], + + "@esbuild/linux-ppc64": ["@esbuild/linux-ppc64@0.28.2", "", { "os": "linux", "cpu": "ppc64" }, "sha512-CAXl+Dtd9UUuJd8pKKdwh6MLm3MUMiqMPmhZ3tTSXPqfyQ3vDl6R5hZdZ/kYojK4ofXtdfSv1tFq8XzWx3heNQ=="], + + "@esbuild/linux-riscv64": ["@esbuild/linux-riscv64@0.28.2", "", { "os": "linux", "cpu": "none" }, "sha512-GeXCej4IQtU1B+QlDV8W/RRvbzI3O/Stss+/bCXv4lZls5WGRtu2a+3JkA3i4qIUlMXpcHebWpF8AkJhATowuA=="], + + "@esbuild/linux-s390x": ["@esbuild/linux-s390x@0.28.2", "", { "os": "linux", "cpu": "s390x" }, "sha512-3H1weTYZPxt/WOhByszQZybS9w5lKzUn1FDMsgEChbHWQwHYQQRfBxgCcZvPhjHfKyJjIievvMmEUawJrdY9Dg=="], + + "@esbuild/linux-x64": ["@esbuild/linux-x64@0.28.2", "", { "os": "linux", "cpu": "x64" }, "sha512-4xTZr1FUmSoQW4XIWmit3tzQrUTZM+N3P0XV8xROKYF50XfI7xeO90+1bZvNwxIufQ9hDQVRJH5YhgPVF8A/HQ=="], + + "@esbuild/netbsd-arm64": ["@esbuild/netbsd-arm64@0.28.2", "", { "os": "none", "cpu": "arm64" }, "sha512-sSATRjPeDBg3pdgHoQfoYBob11Kk1FGa9lui5RIHZCoCkJa9QKlvl3/vKz2usCmYYjs7ymJR/2Nnsqe+Hjt5nw=="], + + "@esbuild/netbsd-x64": ["@esbuild/netbsd-x64@0.28.2", "", { "os": "none", "cpu": "x64" }, "sha512-lqnzCV+mM0gIADaKihiCg6ifgfU2L3h5E33rNQBN1Y4MaVGnzryzmvvf7UHxprpQdE8hpqLolJ9Rl+SkIRDpyw=="], + + "@esbuild/openbsd-arm64": ["@esbuild/openbsd-arm64@0.28.2", "", { "os": "openbsd", "cpu": "arm64" }, "sha512-AL2qJILH7lNjrDmCQDvdxMfAUIv8KMNZOvrwAQ8i8//ntL9FflhOyMJ8OZSMBb8/AWXe3/5v5S20y3zCoZWKoQ=="], + + "@esbuild/openbsd-x64": ["@esbuild/openbsd-x64@0.28.2", "", { "os": "openbsd", "cpu": "x64" }, "sha512-QtiuPytchRyC4rwUKhexJdQKvDuZ6hWloi3igqPQNUJCS1/v9EiO3UTOXR6A3FoMo4fnAKbWJdqaIwhOzh8qEw=="], + + "@esbuild/openharmony-arm64": ["@esbuild/openharmony-arm64@0.28.2", "", { "os": "none", "cpu": "arm64" }, "sha512-WkhYDmpTjLvGlScA1rwjRUmhl4k8oXR3cIbtqWmELgU/dFeHHlEllxDvdWcNJV9rbzCexB5vz8gtNewWLgCT7Q=="], + + "@esbuild/sunos-x64": ["@esbuild/sunos-x64@0.28.2", "", { "os": "sunos", "cpu": "x64" }, "sha512-GPMSkTOtMnv2U2F8gxe4Io6qmVs+YKyp832Etqqxr0hFngmXQ3rzwytelm3GIn7T4VviRUlf3sOgBOiTdvaf7g=="], + + "@esbuild/win32-arm64": ["@esbuild/win32-arm64@0.28.2", "", { "os": "win32", "cpu": "arm64" }, "sha512-PIhhEkE9uPBleRBrQEJpUn7MBnibZzbGzYWPmY3x+YoVg/95zbjB4CxPPOQ8l5tYYM4mMaCthF8/1DIfBQQyWQ=="], + + "@esbuild/win32-ia32": ["@esbuild/win32-ia32@0.28.2", "", { "os": "win32", "cpu": "ia32" }, "sha512-YmJbfTlvU7Sdn9BB+4PRES4oB6pxgS37MAONj+hBr/cpXS1aBPKXxNnDbu+QCWPj0o9dgyxeq79g6c5P8KeuYA=="], + + "@esbuild/win32-x64": ["@esbuild/win32-x64@0.28.2", "", { "os": "win32", "cpu": "x64" }, "sha512-5ebpxr3nWMzrL/rnUI755Jkuee0bHL/Gq0WTF9lvcpv73wAp5eu8MfBUgWK9bhWvZjj7yX8etf/8tI8Ney695g=="], + + "@img/colour": ["@img/colour@1.1.0", "", {}, "sha512-Td76q7j57o/tLVdgS746cYARfSyxk8iEfRxewL9h4OMzYhbW4TAcppl0mT4eyqXddh6L/jwoM75mo7ixa/pCeQ=="], + + "@img/sharp-darwin-arm64": ["@img/sharp-darwin-arm64@0.35.5", "", { "optionalDependencies": { "@img/sharp-libvips-darwin-arm64": "1.3.4" }, "os": "darwin", "cpu": "arm64" }, "sha512-QRUlFQ0WxvdWyqqG/WtI3iupfD5rBzmCHXSdPsY91sAtVtTo7Q4cb6zOccZ3gqEqkr0f1As1ehLqmEpDsRf+lg=="], + + "@img/sharp-darwin-x64": ["@img/sharp-darwin-x64@0.35.5", "", { "optionalDependencies": { "@img/sharp-libvips-darwin-x64": "1.3.4" }, "os": "darwin", "cpu": "x64" }, "sha512-+BR255RhDlpygUpOc/Jdt1nT6DQ3XG/ERo5wbcdOf5Q320dKtPCKPLR1LJs9VGXRaMa8l1uUa0tkCNOXiAxZUw=="], + + "@img/sharp-freebsd-wasm32": ["@img/sharp-freebsd-wasm32@0.35.5", "", { "dependencies": { "@img/sharp-wasm32": "0.35.5" }, "os": "freebsd" }, "sha512-Y/z91nEZ4uIBX5X3nfTovjU9lHNKFYbL2lpHCLVNmXQK03VIZvXBBt0KxbPGp2SdGSF+2mQU4e+hQaWOt86iAw=="], + + "@img/sharp-libvips-darwin-arm64": ["@img/sharp-libvips-darwin-arm64@1.3.4", "", { "os": "darwin", "cpu": "arm64" }, "sha512-5R89nBYiRdUlSWJxPhO+GVtaXzXSxKnRu/xqMn3KTA3L9EB9Oy/P+Nn2f2vlhPuUdy/Zusb2DarbyTpGCfEDuw=="], + + "@img/sharp-libvips-darwin-x64": ["@img/sharp-libvips-darwin-x64@1.3.4", "", { "os": "darwin", "cpu": "x64" }, "sha512-iR2OKH80yi0U+dUplyh3/xdpFvps6YkCwsXenIJxqxR1v9o+xtKTGbS9H7cps+2Vxjc8B1j96p75NmTGjIhtpQ=="], + + "@img/sharp-libvips-linux-arm": ["@img/sharp-libvips-linux-arm@1.3.4", "", { "os": "linux", "cpu": "arm" }, "sha512-LmRtTsOHuvM2+wlO2Db37dx5MiZhB0FvSunciw48YjdOkZz9KAiRbm8ujeMOA1INqmei5NapFxYEK1D1ZSidmw=="], + + "@img/sharp-libvips-linux-arm64": ["@img/sharp-libvips-linux-arm64@1.3.4", "", { "os": "linux", "cpu": "arm64" }, "sha512-Y3dgX/6lE2QhQb+Gxy0WZxfg9MEm/JBjamZpS2IklP7xIQoKN4hzAm7KcMVGtaVDt3neE9OKBC7vAfonA/Lr1A=="], + + "@img/sharp-libvips-linux-ppc64": ["@img/sharp-libvips-linux-ppc64@1.3.4", "", { "os": "linux", "cpu": "ppc64" }, "sha512-Le6boB8Tai0Nis+gIxIpKx68UDVVIqdR8Tin5Yf1z2LJJQLDJvCDRqRu+jC2qCoD+eIomonmOwB4smBRxfVpYQ=="], + + "@img/sharp-libvips-linux-riscv64": ["@img/sharp-libvips-linux-riscv64@1.3.4", "", { "os": "linux", "cpu": "none" }, "sha512-aHkkIEHPRdQEegJN20MLmGtxYD9R2wQr3Cwpddnu5+YKMt6Uzax7S9h5gpZTo8wyrGuZSlfQ63OevL5mTyOC7Q=="], + + "@img/sharp-libvips-linux-s390x": ["@img/sharp-libvips-linux-s390x@1.3.4", "", { "os": "linux", "cpu": "s390x" }, "sha512-ra/mB6MikESDUO7Yg+Mi95bFBb9GsObURuhnOv3OqknjGe9sZrG8tCe9q0xSIGrtLgvgw0gKnFWcK4blSgQOuQ=="], + + "@img/sharp-libvips-linux-x64": ["@img/sharp-libvips-linux-x64@1.3.4", "", { "os": "linux", "cpu": "x64" }, "sha512-GJ//SSXbnwSDes02umB3nDJLFcQzw8a18V8fyhqr6tV515tOEMdImjjxj1AoafMRz56F3PHgftnj1QEKSU1zkw=="], + + "@img/sharp-libvips-linuxmusl-arm64": ["@img/sharp-libvips-linuxmusl-arm64@1.3.4", "", { "os": "linux", "cpu": "arm64" }, "sha512-hvulFwtjUcagsis6BBxHwGFwWoNZjgYmULGVrZcyfNbjA8hKILbRxGg15/7w5HDyXHXUos/j6baAWqnCyQ2DWA=="], + + "@img/sharp-libvips-linuxmusl-x64": ["@img/sharp-libvips-linuxmusl-x64@1.3.4", "", { "os": "linux", "cpu": "x64" }, "sha512-6zXKeE/p39I1AmA3cJG35eyBGNqNddLnUXjhwBnsGjFPWqf5VKkDBEqaEkPDoTEtkxwi2vv8Tcr2mDyP4So7Fg=="], + + "@img/sharp-linux-arm": ["@img/sharp-linux-arm@0.35.5", "", { "optionalDependencies": { "@img/sharp-libvips-linux-arm": "1.3.4" }, "os": "linux", "cpu": "arm" }, "sha512-LEaXK2WdXVK5ykcw0buWyPMsmLLL2vpHLD6yrNSW+JGEL3BZPA4tpKN6iaMc4AxTTAoaX/sU1rOL51lcIz48ZQ=="], + + "@img/sharp-linux-arm64": ["@img/sharp-linux-arm64@0.35.5", "", { "optionalDependencies": { "@img/sharp-libvips-linux-arm64": "1.3.4" }, "os": "linux", "cpu": "arm64" }, "sha512-LYVx5JTsOM2CBzmxreh+nl64/3H6Xb09iSLknqH47z2T2DFFxDeFLP5y4dJwe6H7uGQlHPyEEtIqyo3DYsRwdQ=="], + + "@img/sharp-linux-ppc64": ["@img/sharp-linux-ppc64@0.35.5", "", { "optionalDependencies": { "@img/sharp-libvips-linux-ppc64": "1.3.4" }, "os": "linux", "cpu": "ppc64" }, "sha512-QVxAAq8evVRI9ia2vqgwrmWucn5Dfv+JdWzj75pD8omHLPSP7f8p20O8jxzjCcuCEQEOtYOZUmX1hkiZ0kdevA=="], + + "@img/sharp-linux-riscv64": ["@img/sharp-linux-riscv64@0.35.5", "", { "optionalDependencies": { "@img/sharp-libvips-linux-riscv64": "1.3.4" }, "os": "linux", "cpu": "none" }, "sha512-LtdreXguaavKODPIfzJ4kffx7UNt1omwtK0rch4EBbbSTXPnxWmYSayXdLJw0fJzQ97kHt1gL/yh4tvU+nCyRQ=="], + + "@img/sharp-linux-s390x": ["@img/sharp-linux-s390x@0.35.5", "", { "optionalDependencies": { "@img/sharp-libvips-linux-s390x": "1.3.4" }, "os": "linux", "cpu": "s390x" }, "sha512-UZasTOFiYzotTsGOCu42BfUzP6Tu6Do/947iRm1RsLKvlllxwGcn4RN27LibGWceix4Y+Pmw3jsnTcCQIgWjqA=="], + + "@img/sharp-linux-x64": ["@img/sharp-linux-x64@0.35.5", "", { "optionalDependencies": { "@img/sharp-libvips-linux-x64": "1.3.4" }, "os": "linux", "cpu": "x64" }, "sha512-SxFtLTeJInhAA9Q836kux2vZNeOBQEx658qvbboZScr0wIARym3IcGmW7KpVD5sbVg0Ojy+udFQdayYIZyoNog=="], + + "@img/sharp-linuxmusl-arm64": ["@img/sharp-linuxmusl-arm64@0.35.5", "", { "optionalDependencies": { "@img/sharp-libvips-linuxmusl-arm64": "1.3.4" }, "os": "linux", "cpu": "arm64" }, "sha512-9HbMclmI1zlNkFRs3z9/eBtDjfD0sGlrX1z6b1qwmiFY5ElDLh4BC0LPBdVp7z1DXFiKlIcznf+ZlsuZzLxQqg=="], + + "@img/sharp-linuxmusl-x64": ["@img/sharp-linuxmusl-x64@0.35.5", "", { "optionalDependencies": { "@img/sharp-libvips-linuxmusl-x64": "1.3.4" }, "os": "linux", "cpu": "x64" }, "sha512-4KOphqB035HrVdqLZfCgMzzERrQkkzOwRhl4OAkRO1YCldbaFjySXMaK534Mo0V+LndnlJk+sbUyLeU0ULyD1A=="], + + "@img/sharp-wasm32": ["@img/sharp-wasm32@0.35.5", "", { "dependencies": { "@emnapi/runtime": "^1.11.3" } }, "sha512-Ptsga1su4tQx+LLF1ECS9U6nz5kmrXKo6XVbtR48Ke3ZRxxgaWBu7IDtEe1quo8hiupwm6WFqxVlXaSf7IINGQ=="], + + "@img/sharp-webcontainers-wasm32": ["@img/sharp-webcontainers-wasm32@0.35.5", "", { "dependencies": { "@img/sharp-wasm32": "0.35.5" }, "cpu": "none" }, "sha512-hfhF/FmoQyTUkA0bIKFOtw536BQSeBMe6BF6QyWlrPxT754+TFLaZ7sKKTfvvM0yJgKgaYTwnFCIZ/GuDw5SUA=="], + + "@img/sharp-win32-arm64": ["@img/sharp-win32-arm64@0.35.5", "", { "os": "win32", "cpu": "arm64" }, "sha512-X4t7g+7ZA5DKblCBEXGjUqqemj4vczING/5viFwAL8h4N3qYeyjwdCvRLHi4EdOUI+2Z7UFlp1VM+p/AuEtm6Q=="], + + "@img/sharp-win32-ia32": ["@img/sharp-win32-ia32@0.35.5", "", { "os": "win32", "cpu": "ia32" }, "sha512-5Zm82LoBc43nhwNybZlG7Y1KO//Zhsn306fQl29ZOuStHLGTo3BWL83q3cznX0poxSAMuYL1On/BHBxkBeKr6A=="], + + "@img/sharp-win32-x64": ["@img/sharp-win32-x64@0.35.5", "", { "os": "win32", "cpu": "x64" }, "sha512-x76eH0vEiHlcMQu8Y8IenntaACtddpT6W0wmXtWrnKcnKI7ME5DdgqhAD6SEWOEl1v2zDvkZDhFA9KnURwpfqg=="], + + "@next/env": ["@next/env@16.4.4", "", {}, "sha512-6/3ZuELR0tPLgNigvv8d/LAmfLIo1IpNfvrI/AHCAl9w9D0DNVGQGvo+OLPzAWR+WMW3vVyO7HaW0kfL97huBg=="], + + "@next/swc-darwin-arm64": ["@next/swc-darwin-arm64@16.4.4", "", { "os": "darwin", "cpu": "arm64" }, "sha512-1KlZ5IJ7zcLBZvqG1q7nuRWm2LAAN4C3iBqqoBCRRl1UEv0A4k6Rek+x3j23UiWf8Gz+VsN+M/LylAWkxpm2mg=="], + + "@next/swc-darwin-x64": ["@next/swc-darwin-x64@16.4.4", "", { "os": "darwin", "cpu": "x64" }, "sha512-Tb7efFdhbS/ph74RS7wob84gXGM58qxoO/WrkiMsTmy/JZktKOKBeiM45W0E/b8JfBzZJdexQqSmvedBwDK0PA=="], + + "@next/swc-linux-arm64-gnu": ["@next/swc-linux-arm64-gnu@16.4.4", "", { "os": "linux", "cpu": "arm64" }, "sha512-wojSfLnH8RRImhtbcn3Dlx9JICh6aQHgDA33XDr+EPua2tt7Q/yjDn0FTAbqfNlLrBmOASAGhLh6NvBk9L3gzw=="], + + "@next/swc-linux-arm64-musl": ["@next/swc-linux-arm64-musl@16.4.4", "", { "os": "linux", "cpu": "arm64" }, "sha512-zgxpGOZaR+2O6dOk5ZAUkKgH92ZtqNg3ohzyg2HmWc2N+AZH4dEpTwdpB0e5MR9YRVdVDUkEM5g6c3TWWn8g1g=="], + + "@next/swc-linux-x64-gnu": ["@next/swc-linux-x64-gnu@16.4.4", "", { "os": "linux", "cpu": "x64" }, "sha512-fg3Wr030Y3BDkCXi6HGsPz0BUPTGZJS8TZSrCO0tS3O/KBsCyx4eP6aeaizCpGmcUKhtb4l7JoMc88QO2qBl2g=="], + + "@next/swc-linux-x64-musl": ["@next/swc-linux-x64-musl@16.4.4", "", { "os": "linux", "cpu": "x64" }, "sha512-40cr71PSdCV9zU2g0B1XG27af10tYU+9g8F+FtF3qSguQg0jSZDoxn0NrAvgFQ0cy9s0ZBA/00P9g/XL1O43xg=="], + + "@next/swc-win32-arm64-msvc": ["@next/swc-win32-arm64-msvc@16.4.4", "", { "os": "win32", "cpu": "arm64" }, "sha512-vFMiEbRZ+LYNeswslf4LuGuoPHE8JbtqV0GnmzbpnGNMs0uWaR2q98hu8M/UWGycNJERBXIUss+BGd3PIJ8VZg=="], + + "@next/swc-win32-x64-msvc": ["@next/swc-win32-x64-msvc@16.4.4", "", { "os": "win32", "cpu": "x64" }, "sha512-jy2P4V8BDZ+CFCzYABHYRZ1CmyFhfmYe22i8KA4uEafdYPrUjJ3Ot+lx265mLWLHiZRIfBzCn3V1F17m0vzl9Q=="], + + "@swc/helpers": ["@swc/helpers@0.5.23", "", { "dependencies": { "tslib": "^2.8.0" } }, "sha512-5lSsMOTXURePglDfvuAQUqkGek9Hg2kksOYay2m0+XR++b2NWYL/4sWyuvVBIs8oKnJaxkdi9whaL/sqN13afw=="], + + "@types/node": ["@types/node@26.6.6", "", { "dependencies": { "undici-types": "~8.9.0" } }, "sha512-MzQj+8Kx3XI40OmUAE6Qoh58ZMNstNNMwn2nfd3qWsfe7nDNSqJNzes4njhz6AHwrSHOffSLwXU6yb474Q4aog=="], + + "@types/react": ["@types/react@19.3.0", "", { "dependencies": { "csstype": "^3.2.2" } }, "sha512-N0rFCuH9YoxG9/m61l9MfpJKfmLOVU0em7ipIz6TRgSSkvReLB9vL85GB+yr8Bs5leqpvg96JSwF4ZS1s4viQg=="], + + "@types/react-dom": ["@types/react-dom@19.3.0", "", { "peerDependencies": { "@types/react": "^19.3.0" } }, "sha512-ZI7bU42mZXXKHn/qNLEw2IrbiINU7X5+vfgdixBHkCNpYWXjKgfQ/P+uyGb5CjOLB9UcnTeg3rylQtV2hym44Q=="], + + "baseline-browser-mapping": ["baseline-browser-mapping@2.11.36", "", { "bin": { "baseline-browser-mapping": "dist/cli.cjs" } }, "sha512-AbPFGS4K9S3meMRCLTM0Zas+e9X7b5ygUBOboDT5zevCZt9ByKSbMtKhwyVqW+oL8WemPVsbP7X3rdZcIyOa0w=="], + + "caniuse-lite": ["caniuse-lite@1.0.30001826", "", {}, "sha512-wTB+9tIbLro/BCXwLuNV1meHE4L9dS6dArMDwgYfNBPMYScqClHyGunhkCSDtHhe/69Xl0sHlKgBVZ+fNsYtWA=="], + + "client-only": ["client-only@0.0.1", "", {}, "sha512-IV3Ou0jSMzZrd3pZ48nLkT9DA7Ag1pnPzaiQhpW7c3RbcqqzvzzVu+L8gfqMp/8IM2MQtSiqaCxrrcfu8I8rMA=="], + + "csstype": ["csstype@3.2.3", "", {}, "sha512-z1HGKcYy2xA8AGQfwrn0PAy+PB7X/GSj3UVJW9qKyn43xWa+gl5nXmU4qqLMRzWVLFC8KusUX8T/0kCiOYpAIQ=="], + + "detect-libc": ["detect-libc@2.1.2", "", {}, "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ=="], + + "esbuild": ["esbuild@0.28.2", "", { "optionalDependencies": { "@esbuild/aix-ppc64": "0.28.2", "@esbuild/android-arm": "0.28.2", "@esbuild/android-arm64": "0.28.2", "@esbuild/android-x64": "0.28.2", "@esbuild/darwin-arm64": "0.28.2", "@esbuild/darwin-x64": "0.28.2", "@esbuild/freebsd-arm64": "0.28.2", "@esbuild/freebsd-x64": "0.28.2", "@esbuild/linux-arm": "0.28.2", "@esbuild/linux-arm64": "0.28.2", "@esbuild/linux-ia32": "0.28.2", "@esbuild/linux-loong64": "0.28.2", "@esbuild/linux-mips64el": "0.28.2", "@esbuild/linux-ppc64": "0.28.2", "@esbuild/linux-riscv64": "0.28.2", "@esbuild/linux-s390x": "0.28.2", "@esbuild/linux-x64": "0.28.2", "@esbuild/netbsd-arm64": "0.28.2", "@esbuild/netbsd-x64": "0.28.2", "@esbuild/openbsd-arm64": "0.28.2", "@esbuild/openbsd-x64": "0.28.2", "@esbuild/openharmony-arm64": "0.28.2", "@esbuild/sunos-x64": "0.28.2", "@esbuild/win32-arm64": "0.28.2", "@esbuild/win32-ia32": "0.28.2", "@esbuild/win32-x64": "0.28.2" }, "bin": { "esbuild": "bin/esbuild" } }, "sha512-HKVLS8dvII+xoKW9kmqxbRKrnWEXfJJr/FZhhJmiqIB0e053QNYFqOBouTMO/k5sID4MvCiUCvv8b9M4h32wIA=="], + + "fsevents": ["fsevents@2.3.3", "", { "os": "darwin" }, "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw=="], + + "nanoid": ["nanoid@3.3.20", "", { "bin": { "nanoid": "bin/nanoid.cjs" } }, "sha512-uKdg2G3GNCKQn9byYOpxbGqrT2fGO5KRt5J/8b3pok8rT6qxGWF6hxMyJiEYtAf+FVyYuD9hRaDqX5uPFYJ4ZQ=="], + + "next": ["next@16.4.4", "", { "dependencies": { "@next/env": "16.4.4", "@swc/helpers": "0.5.23", "baseline-browser-mapping": "^2.9.19", "caniuse-lite": "^1.0.30001579", "postcss": "8.5.23", "styled-jsx": "5.1.6" }, "optionalDependencies": { "@next/swc-darwin-arm64": "16.4.4", "@next/swc-darwin-x64": "16.4.4", "@next/swc-linux-arm64-gnu": "16.4.4", "@next/swc-linux-arm64-musl": "16.4.4", "@next/swc-linux-x64-gnu": "16.4.4", "@next/swc-linux-x64-musl": "16.4.4", "@next/swc-win32-arm64-msvc": "16.4.4", "@next/swc-win32-x64-msvc": "16.4.4", "sharp": "^0.35.5" }, "peerDependencies": { "@opentelemetry/api": "^1.1.0", "@playwright/test": "^1.51.1", "babel-plugin-react-compiler": "*", "react": "^18.2.0 || 19.0.0-rc-de68d2f4-20241204 || ^19.0.0", "react-dom": "^18.2.0 || 19.0.0-rc-de68d2f4-20241204 || ^19.0.0", "sass": "^1.3.0" }, "optionalPeers": ["@opentelemetry/api", "@playwright/test", "babel-plugin-react-compiler", "sass"], "bin": { "next": "dist/bin/next" } }, "sha512-Hu4pOw2x0RQ0TQabNWbpsNg/3+SZf+rLQwvfhBLnptv98GS4dcIVMlfEyn9jb2/ZJDSahhnUKEs0YrfarjGX3A=="], + + "picocolors": ["picocolors@1.1.1", "", {}, "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA=="], + + "postcss": ["postcss@8.5.23", "", { "dependencies": { "nanoid": "^3.3.16", "picocolors": "^1.1.1", "source-map-js": "^1.2.1" } }, "sha512-g50586zr4bZmwFiTlflMu8E0bDTb5I5gertgwAKmsdUlTQIhZtunzUlD1WSzwcVWPoAVpsrA6vlfCD7oXvRwgg=="], + + "react": ["react@19.3.1", "", {}, "sha512-QNhWdt7nrgVWWQG58QuGsAR3jv3Hbe4mb4vz7w2csnHavciZJgda1CPNaldF6eVxv90ud3jUXpKlHL69gO38yw=="], + + "react-dom": ["react-dom@19.3.1", "", { "dependencies": { "scheduler": "^0.28.0" }, "peerDependencies": { "react": "^19.3.1" } }, "sha512-Y0bARz53lhEC1NMCASs0/j+faB/zqt3R8hpvg1e8+61ZVJ4UL0UdC4cBbXfZMcIuhrfgeILTLe+ZeBCavQPtKQ=="], + + "scheduler": ["scheduler@0.28.0", "", {}, "sha512-juorfCmIkIw8tT+p5BXSm6PJjQF/ycEYmKyzURCIt/RaZIhL+PulbQ9Yu2z1HdOJDdqDTlxA1+xKBmHXJsczAw=="], + + "semver": ["semver@7.8.6", "", { "bin": { "semver": "bin/semver.js" } }, "sha512-9lpSSU4Mly3mrtruwLs1eLK6+gzbzqeBFwRNjIjE+9LuDpWdYctrQlQ3ej1Ilgi7gknpsTRnPoLP8DpCHqHIJw=="], + + "sharp": ["sharp@0.35.5", "", { "dependencies": { "@img/colour": "^1.1.0", "detect-libc": "^2.1.2", "semver": "^7.8.5" }, "optionalDependencies": { "@img/sharp-darwin-arm64": "0.35.5", "@img/sharp-darwin-x64": "0.35.5", "@img/sharp-freebsd-wasm32": "0.35.5", "@img/sharp-libvips-darwin-arm64": "1.3.4", "@img/sharp-libvips-darwin-x64": "1.3.4", "@img/sharp-libvips-linux-arm": "1.3.4", "@img/sharp-libvips-linux-arm64": "1.3.4", "@img/sharp-libvips-linux-ppc64": "1.3.4", "@img/sharp-libvips-linux-riscv64": "1.3.4", "@img/sharp-libvips-linux-s390x": "1.3.4", "@img/sharp-libvips-linux-x64": "1.3.4", "@img/sharp-libvips-linuxmusl-arm64": "1.3.4", "@img/sharp-libvips-linuxmusl-x64": "1.3.4", "@img/sharp-linux-arm": "0.35.5", "@img/sharp-linux-arm64": "0.35.5", "@img/sharp-linux-ppc64": "0.35.5", "@img/sharp-linux-riscv64": "0.35.5", "@img/sharp-linux-s390x": "0.35.5", "@img/sharp-linux-x64": "0.35.5", "@img/sharp-linuxmusl-arm64": "0.35.5", "@img/sharp-linuxmusl-x64": "0.35.5", "@img/sharp-webcontainers-wasm32": "0.35.5", "@img/sharp-win32-arm64": "0.35.5", "@img/sharp-win32-ia32": "0.35.5", "@img/sharp-win32-x64": "0.35.5" } }, "sha512-Ywn4OnzGukp7CDMrp08RQ50YKmuwG47brZgIVPTvBaaAfQlRlygrRqSrxdCiL9M+LlzLBiJ68IR1QqvzHyjC7g=="], + + "source-map-js": ["source-map-js@1.2.2", "", {}, "sha512-KGj/8Y43x35aZVDtt+J4mK1hoLGHULMYfSkODJNQjNDC3oW1PqPoxMwo0pLUsWM/UEGzON/NxeHywEfNXNP3Vw=="], + + "styled-jsx": ["styled-jsx@5.1.6", "", { "dependencies": { "client-only": "0.0.1" }, "peerDependencies": { "react": ">= 16.8.0 || 17.x.x || ^18.0.0-0 || ^19.0.0-0" } }, "sha512-qSVyDTeMotdvQYoHWLNGwRFJHC+i+ZvdBRYosOFgC+Wg1vx4frN2/RG/NA7SYqqvKNLf39P2LSRA2pu6n0XYZA=="], + + "tslib": ["tslib@2.9.1", "", {}, "sha512-a4Ux9VKuIMHsOS7ENbEq6hK2ZGYIT27Rtl0E4eD9KqwxhKiQeMZE/9xb3IdYwWaSOyIBQLExhQgXDTYoiCxnWA=="], + + "tsx": ["tsx@4.23.15", "", { "dependencies": { "esbuild": "~0.28.0" }, "optionalDependencies": { "fsevents": "~2.3.3" }, "bin": { "tsx": "dist/cli.mjs" } }, "sha512-Yiex1Ovn8z2xPpOWckIiysV1SSyRMY9BkLF++q0yKiDxCqRhosKfMg3janKkiLBwZ5c/YryloKwGZcrEmtwxKw=="], + + "typescript": ["typescript@6.0.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-y2TvuxSZPDyQakkFRPZHKFm+KKVqIisdg9/CZwm9ftvKXLP8NRWj38/ODjNbr43SsoXqNuAisEf1GdCxqWcdBw=="], + + "undici-types": ["undici-types@8.9.0", "", {}, "sha512-KTDyRTYX8sWmKXAikPHHSyc63CRPETMctyjKFupcC6OBLXT3xsN0e9aF7m+mIXutFWpUXuedtowG7iLOzp0kQg=="], + } +} diff --git a/apps/status/lib/fixture.ts b/apps/status/lib/fixture.ts new file mode 100644 index 00000000..311e4403 --- /dev/null +++ b/apps/status/lib/fixture.ts @@ -0,0 +1,45 @@ +// Sample data for working on the page before the backend's /v1/status is live (STATUS_FIXTURE=1 bun run dev). +// It mirrors 13 Nov 2026: NEAR AI's embeddings hung and 1Click's quote API timed out, everything else answered. +import type { BucketState, Service, Status } from "./status" + +const BUCKETS = 48 +const HALF_HOUR = 30 * 60 * 1000 + +function history(now: number, badFrom: number, bad: BucketState): Service["history"] { + return Array.from({ length: BUCKETS }, (_, i) => ({ + start: new Date(now - (BUCKETS - i) * HALF_HOUR).toISOString(), + status: i < 4 ? "none" : i >= badFrom ? bad : "operational", + checks: i < 4 ? 0 : 6, + bad: i >= badFrom && bad !== "operational" ? (i === badFrom ? 3 : 6) : 0, + detail: i >= badFrom && bad === "down" ? "No answer in time" : null, + })) +} + +export function fixture(): Status { + const now = Date.now() + const at = new Date(now - 90_000).toISOString() + const ok = (id: string, name: string, group: Service["group"], latencyMs: number, impact: string): Service => ({ + id, name, group, status: "operational", latencyMs, checkedAt: at, detail: null, impact, uptime24h: 100, uptime7d: 99.95, history: history(now, BUCKETS, "operational"), + }) + const iso = (msAgo: number) => new Date(now - msAgo).toISOString() + return { + updatedAt: at, + overall: "down", + incidents: [ + { service: "near-intents-quote", name: "NEAR Intents: 1Click quotes", status: "down", start: iso(2 * 3_600_000), end: null, minutes: 120, detail: "1Click answered 500 after 25 s" }, + { service: "nearai-embeddings", name: "NEAR AI Cloud: embeddings", status: "down", start: iso(9 * 3_600_000), end: null, minutes: 540, detail: "No answer in 8 s" }, + { service: "agent-market", name: "NEAR AI Agent Market", status: "degraded", start: iso(30 * 3_600_000), end: iso(29.5 * 3_600_000), minutes: 30, detail: "Answered in 3.4 s" }, + ], + services: [ + ok("hellonear-api", "HelloNear API", "HelloNear", 42, "The web app can't sign in, chat or pay."), + ok("hellonear-engine", "Mind engine", "HelloNear", 18, "Minds can't answer; nothing is paid."), + ok("nearai-chat", "NEAR AI Cloud: chat models", "NEAR AI", 1450, "Minds can't answer until it is back."), + { ...ok("nearai-embeddings", "NEAR AI Cloud: embeddings", "NEAR AI", 0, "Chats and Autopilot run without searching past chats until it answers."), status: "down", latencyMs: null, detail: "No answer in 8 s", uptime24h: 62.5, uptime7d: 94.6, history: history(now, 30, "down") }, + { ...ok("near-intents-quote", "NEAR Intents: 1Click quotes", "NEAR", 0, "Cross-chain stablecoin quotes can't be fetched; nothing is ever sent either way."), status: "down", latencyMs: null, detail: "1Click answered 500 after 25 s", uptime24h: 91.67, uptime7d: 98.8, history: history(now, 44, "down") }, + ok("outlayer", "OutLayer wallets (testnet)", "NEAR", 310, "Payments, limits and Freeze wait until it is back; nothing is sent."), + ok("near-rpc", "NEAR RPC (FastNear testnet)", "NEAR", 220, "Account lookups and payment checks fail; Minds say so and don't pay."), + { ...ok("agent-market", "NEAR AI Agent Market", "Partners", 2900, "Agent searches and quotes can't be fetched."), status: "degraded", detail: "Answered in 2.9 s", uptime24h: 100, uptime7d: 100 }, + ok("tee-explorer", "Phala TEE Attestation Explorer", "Partners", 640, "The Verified badge still works; opening the report in TEE Explorer doesn't."), + ], + } +} diff --git a/apps/status/lib/status.ts b/apps/status/lib/status.ts new file mode 100644 index 00000000..db4ea381 --- /dev/null +++ b/apps/status/lib/status.ts @@ -0,0 +1,118 @@ +// The status page reads the backend's public GET /v1/status (apps/be, docs/api-contract.md). The shape below +// is that contract; keep the two in step. + +export type State = "operational" | "degraded" | "down" | "unknown" +export type BucketState = "operational" | "degraded" | "down" | "none" + +export type Service = { + id: string + name: string + group: "HelloNear" | "NEAR AI" | "NEAR" | "Partners" + status: State + latencyMs: number | null + checkedAt: string | null + detail: string | null + impact: string + uptime24h: number | null + uptime7d: number | null + // checks / bad / detail are optional: probes in the bucket, how many were down or slow, and the last problem's detail. + history: { start: string; status: BucketState; checks?: number; bad?: number; detail?: string | null }[] +} + +// A run of failed checks of one service, worked out by the backend from the last 7 days of probes. +export type Incident = { service: string; name: string; status: "down" | "degraded"; start: string; end: string | null; minutes: number; detail: string | null } + +export type Status = { updatedAt: string; overall: "operational" | "degraded" | "down"; services: Service[]; incidents?: Incident[] } + +export const GROUPS: Service["group"][] = ["HelloNear", "NEAR AI", "NEAR", "Partners"] + +// Read when the page is rendered, so a deploy can point it elsewhere (STATUS_API_URL) without a rebuild of the code. +export const statusUrl = () => process.env.STATUS_API_URL ?? "https://api.hellonear.xyz/v1/status" + +// A plain fetch, revalidated every 30 s by Next (the backend caches its answer for 30 s too). Any failure +// becomes null, and the page says it can't reach the status API rather than showing everything as down. +export async function loadStatus(): Promise { + if (process.env.STATUS_FIXTURE === "1") return (await import("./fixture")).fixture() + try { + const res = await fetch(statusUrl(), { next: { revalidate: 30 }, signal: AbortSignal.timeout(10_000) }) + if (!res.ok) return null + const body = (await res.json()) as Status + return Array.isArray(body?.services) ? body : null + } catch { + return null + } +} + +export const LABEL: Record = { operational: "Operational", degraded: "Degraded", down: "Down", unknown: "No data yet" } + +export const HEADLINE: Record = { + operational: "All systems operational", + // The backend says "degraded" when a provider is down or slow but HelloNear itself answers, and "down" only + // when HelloNear's own API or engine is down. + degraded: "Partial outage: some services are having problems", + down: "Major outage: HelloNear is not answering", +} + +// The services that are not operational, worst first, for the banner at the top. +export function incidents(s: Status): Service[] { + const rank: Record = { down: 0, degraded: 1, unknown: 2, operational: 3 } + return s.services.filter((x) => x.status === "down" || x.status === "degraded").sort((a, b) => rank[a.status] - rank[b.status]) +} + +// "3 min ago" style, from the page's own clock. +export function ago(iso: string | null, now = Date.now()): string { + if (!iso) return "never" + const s = Math.max(0, Math.round((now - Date.parse(iso)) / 1000)) + if (s < 60) return `${s} s ago` + const m = Math.round(s / 60) + if (m < 60) return `${m} min ago` + const h = Math.round(m / 60) + return h < 48 ? `${h} h ago` : `${Math.round(h / 24)} d ago` +} + +// The tooltip's lines for one 30-minute bar. Probes run every 5 minutes, so each bad probe stands for about 5 minutes. +export const PROBE_MINUTES = 5 +// The colour of one bar, like status.claude.com: green when every check was fine, yellow when only some failed +// (or it was only slow), red when every check failed. Without the optional counts it falls back to the bucket status. +export type Level = "ok" | "partial" | "major" | "none" +export function levelOf(b: Service["history"][number]): Level { + if (b.status === "none") return "none" + if (b.status === "operational") return "ok" + if (b.status === "degraded") return "partial" + if (typeof b.checks === "number" && typeof b.bad === "number" && b.checks > 0) return b.bad >= b.checks ? "major" : "partial" + return "major" +} +export const LEVEL_LABEL: Record = { ok: "Operational", partial: "Partial outage", major: "Major outage", none: "No checks" } + +export function bucketLines(b: Service["history"][number], locale?: string): { when: string; state: string; level: Level; minutes: number | null; detail: string | null } { + const start = new Date(b.start) + const end = new Date(start.getTime() + 30 * 60 * 1000) + // UTC, said so: the page is rendered on the server, whose clock zone is not the reader's. + const t = (d: Date) => d.toLocaleTimeString(locale, { hour: "2-digit", minute: "2-digit", timeZone: "UTC" }) + const when = `${start.toLocaleDateString(locale, { day: "numeric", month: "short", timeZone: "UTC" })}, ${t(start)} to ${t(end)} UTC` + const level = levelOf(b) + const state = b.status === "degraded" ? "Slow" : LEVEL_LABEL[level] + const minutes = b.bad && b.bad > 0 ? b.bad * PROBE_MINUTES : null + return { when, state, level, minutes, detail: b.detail ?? null } +} + +// The last `days` calendar days (UTC), newest first, each with its incidents (by start time), so a quiet day +// still shows as "No incidents reported". +export function incidentDays(list: Incident[], days = 7, now = Date.now()): { day: string; items: Incident[] }[] { + const out: { day: string; items: Incident[] }[] = [] + for (let i = 0; i < days; i++) { + const d = new Date(now - i * 86_400_000).toISOString().slice(0, 10) + out.push({ day: d, items: list.filter((x) => x.start.slice(0, 10) === d).sort((a, b) => b.start.localeCompare(a.start)) }) + } + return out +} + +// "2 h 40 min", "35 min". +export function duration(minutes: number): string { + const m = Math.max(0, Math.round(minutes)) + if (m < 60) return `${m} min` + const h = Math.floor(m / 60) + return m % 60 ? `${h} h ${m % 60} min` : `${h} h` +} + +export const pct = (n: number | null) => (n === null ? "No data" : `${n.toFixed(2)}% uptime`) diff --git a/apps/status/next-env.d.ts b/apps/status/next-env.d.ts new file mode 100644 index 00000000..a419cbe4 --- /dev/null +++ b/apps/status/next-env.d.ts @@ -0,0 +1,7 @@ +/// +/// +import "./.next/dev/types/routes.d.ts"; +import "./.next/dev/types/root-params.d.ts"; + +// NOTE: This file should not be edited +// see https://nextjs.org/docs/app/api-reference/config/typescript for more information. diff --git a/apps/status/next.config.ts b/apps/status/next.config.ts new file mode 100644 index 00000000..cb651cdc --- /dev/null +++ b/apps/status/next.config.ts @@ -0,0 +1,5 @@ +import type { NextConfig } from "next"; + +const nextConfig: NextConfig = {}; + +export default nextConfig; diff --git a/apps/status/package.json b/apps/status/package.json new file mode 100644 index 00000000..3454754f --- /dev/null +++ b/apps/status/package.json @@ -0,0 +1,27 @@ +{ + "name": "@hellonear/status", + "version": "0.1.0", + "private": true, + "engines": { + "node": "^22.22.2 || >=24.0.0" + }, + "scripts": { + "dev": "next dev --port 3200", + "build": "next build", + "start": "next start --port 3200", + "typecheck": "tsc --noEmit", + "test": "node --import tsx --test tests/*.test.ts" + }, + "dependencies": { + "next": "16.4.4", + "react": "19.3.1", + "react-dom": "19.3.1" + }, + "devDependencies": { + "@types/node": "26.6.6", + "@types/react": "19.3.0", + "@types/react-dom": "19.3.0", + "tsx": "4.23.15", + "typescript": "6.0.3" + } +} diff --git a/apps/status/tests/status.test.ts b/apps/status/tests/status.test.ts new file mode 100644 index 00000000..e60dd830 --- /dev/null +++ b/apps/status/tests/status.test.ts @@ -0,0 +1,53 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { ago, duration, incidentDays, incidents, levelOf, pct } from "../lib/status"; +import { fixture } from "../lib/fixture"; + +test("incidents lists down before slow, and leaves operational services out", () => { + const ids = incidents(fixture()).map((s) => [s.id, s.status]); + assert.deepEqual(ids, [["nearai-embeddings", "down"], ["near-intents-quote", "down"], ["agent-market", "degraded"]]); +}); + +test("ago and pct read like the page means", () => { + const now = Date.parse("2026-11-13T00:00:00Z"); + assert.equal(ago("2026-11-12T23:59:30Z", now), "30 s ago"); + assert.equal(ago("2026-11-12T23:57:00Z", now), "3 min ago"); + assert.equal(ago("2026-11-12T21:00:00Z", now), "3 h ago"); + assert.equal(ago(null, now), "never"); + assert.equal(pct(99.954), "99.95% uptime"); + assert.equal(pct(null), "No data"); +}); + +test("every fixture service has 48 half-hour buckets, oldest first", () => { + for (const s of fixture().services) { + assert.equal(s.history.length, 48, s.id); + assert.ok(Date.parse(s.history[0].start) < Date.parse(s.history[47].start), s.id); + } +}); + +test("past incidents: 7 UTC days newest first, quiet days empty, durations readable", () => { + const now = Date.parse("2026-11-13T03:00:00Z"); + const list = [ + { service: "a", name: "A", status: "down" as const, start: "2026-11-13T01:00:00Z", end: null, minutes: 120, detail: null }, + { service: "b", name: "B", status: "degraded" as const, start: "2026-11-11T10:00:00Z", end: "2026-11-11T10:30:00Z", minutes: 30, detail: "slow" }, + ]; + const days = incidentDays(list, 7, now); + assert.equal(days.length, 7); + assert.equal(days[0].day, "2026-11-13"); + assert.equal(days[0].items.length, 1); + assert.equal(days[1].items.length, 0); + assert.equal(days[2].items[0].service, "b"); + assert.equal(duration(35), "35 min"); + assert.equal(duration(160), "2 h 40 min"); + assert.equal(duration(120), "2 h"); +}); + +test("bar colours: green all fine, yellow some failed or slow, red all failed", () => { + const b = (status: "operational" | "degraded" | "down" | "none", checks?: number, bad?: number) => ({ start: "2026-11-13T00:00:00Z", status, checks, bad }); + assert.equal(levelOf(b("operational", 6, 0)), "ok"); + assert.equal(levelOf(b("down", 6, 3)), "partial"); + assert.equal(levelOf(b("down", 6, 6)), "major"); + assert.equal(levelOf(b("degraded", 6, 6)), "partial"); + assert.equal(levelOf(b("down")), "major", "no counts: the bucket status decides"); + assert.equal(levelOf(b("none", 0, 0)), "none"); +}); diff --git a/apps/status/tsconfig.json b/apps/status/tsconfig.json new file mode 100644 index 00000000..1a0ca4a3 --- /dev/null +++ b/apps/status/tsconfig.json @@ -0,0 +1,41 @@ +{ + "compilerOptions": { + "target": "ES2022", + "lib": [ + "dom", + "dom.iterable", + "esnext" + ], + "allowJs": false, + "skipLibCheck": true, + "strict": true, + "noEmit": true, + "esModuleInterop": true, + "module": "esnext", + "moduleResolution": "bundler", + "resolveJsonModule": true, + "isolatedModules": true, + "jsx": "react-jsx", + "incremental": true, + "plugins": [ + { + "name": "next" + } + ], + "paths": { + "@/*": [ + "./*" + ] + } + }, + "include": [ + "next-env.d.ts", + "**/*.ts", + "**/*.tsx", + ".next/types/**/*.ts", + ".next/dev/types/**/*.ts" + ], + "exclude": [ + "node_modules" + ] +} diff --git a/docs/api-contract.md b/docs/api-contract.md index 82de4eb3..1cdc0cec 100644 --- a/docs/api-contract.md +++ b/docs/api-contract.md @@ -188,6 +188,10 @@ Format (`@mastra/memory` 1.40 `docs-memory-working-memory.md`, read 11 Nov): the `{ "ok": true, "mastra": "1.81.0", "attestation": { "": { "ok": true, "checkedAt": "…", "ms": 17558 } } }`: attestation is warmed at startup and every 50 minutes. +### `GET /internal/v1/probes` (status page) + +`X-Engine-Key` like every route but health. Runs the two NEAR AI checks for the status page with the engine's key (plain requests, not the verified transport): `{ "probes": [ { "id": "nearai-chat" | "nearai-embeddings", "status": "operational" | "degraded" | "down", "latencyMs": number | null, "detail": string | null } ] }`. Chat: a completion of `z-ai/glm-5.3-flash` (`max_tokens` 32), degraded over 10 s, down after 30 s or on an error. Embeddings: one `Qwen/Qwen3-Embedding-0.6B` embedding, degraded over 5 s, down after 15 s, an error or an empty vector. The backend calls it every 5 minutes **[tested: engine `test/probes.test.ts` with a fake NEAR AI; 401 without the key in `test/engine.test.ts`; live 13 Nov: both operational, ~1.9 s and ~1.7 s]**. + ### Visual Builder workflows (#20) A workflow belongs to one Mind. The canvas is saved by the engine (Mastra stores it in Postgres, schema `mastra`), so the backend only proxies these calls; ids: `workflowId` is lowercase words joined by single dashes (max 48), `mindId` letters/digits/`_` joined by single dashes. @@ -378,6 +382,21 @@ Implemented in `apps/be` (#8). Base URL `http://localhost:4000` locally. JSON bo `GET /v1/health` (public): `{ ok: true, engine: boolean, revision: string | null }`. `engine` is whether the engine's own health answered; `revision` is the git commit the hosted release was deployed from (the `REVISION` file `deploy/deploy.sh` writes), `null` when the backend runs from a checkout. So anyone can see what is live without SSH. +`GET /v1/status` (public, any origin, no cookies; `access-control-allow-origin: *`, `cache-control: public, max-age=30`, `OPTIONS` answered): the status page's data (status.hellonear.xyz). Every 5 minutes the backend checks 9 services in parallel and stores one row each in `service_checks` (kept 8 days); the answer is computed from the last 7 days and cached 30 s. +``` +{ "updatedAt": ISO, "overall": "operational" | "degraded" | "down", + "services": [{ "id", "name", "group": "HelloNear" | "NEAR AI" | "NEAR" | "Partners", + "status": "operational" | "degraded" | "down" | "unknown", "latencyMs": number | null, "checkedAt": ISO | null, + "detail": string | null, "impact": string, "uptime24h": number | null, "uptime7d": number | null, + "history": [{ "start": ISO, "status": "operational" | "degraded" | "down" | "none", "checks": number, "bad": number, "detail": string | null }] }], + "incidents": [{ "service", "name", "status": "down" | "degraded", "start": ISO, "end": ISO | null, "minutes": number, "detail": string | null }] } +``` +- Services, in this order: `hellonear-api` (Postgres `select 1`), `hellonear-engine` (engine `/v1/health`), `nearai-chat` and `nearai-embeddings` (from the engine's `GET /internal/v1/probes`), `near-intents-quote` (1Click `POST /v0/quote`, `dry: true`, 10 USDC NEAR → Base), `outlayer` (`GET {OUTLAYER_URL}/health`), `near-rpc` (FastNear testnet `status`), `agent-market` (`GET market.near.ai/v1/agents?limit=1`), `tee-explorer` (`HEAD proof.t16z.com`). +- Degraded over (ms): api 1,000, engine 2,000, NEAR AI chat 10,000, embeddings 5,000, 1Click quote 15,000, OutLayer 3,000, RPC 3,000 (or a node still syncing), Agent Market 5,000, TEE Explorer 5,000. Down: an error, a non-2xx answer, an answer without the expected field, or no answer within the timeout (api 5 s, engine 10 s, chat 30 s, embeddings 15 s, quote 30 s, OutLayer and RPC 10 s, Agent Market and TEE Explorer 15 s). +- `status` is the latest check, or `unknown` when it is older than 15 minutes (or there is none). If the engine does not answer, nothing is stored for NEAR AI that round (it is not guessed). `overall` is `down` when a HelloNear service is down, `degraded` when anything else is down or degraded. Uptime counts degraded as up. +- `history` is 48 half-hour buckets ending with the current one, oldest first: the worst check in each, `checks` and `bad` (down or degraded) counted, `detail` the last bad check's. `incidents` (newest first, at most 100): consecutive non-operational checks of one service; one good check ends it (`end`), `null` while ongoing; `down` if any check in it was down. +- Details are short and never carry keys or URLs **[tested: `test/status-math.test.ts` (unit), `test/status.test.ts` (local Postgres, fake upstreams: up, slow, failing, engine down); live 13 Nov: 1Click's quote answered 500 after 25 s, the rest operational]**. + ### Auth (NEP-413) | Method and path | Body | Response | diff --git a/docs/openapi.json b/docs/openapi.json index ddadd226..2aaac9b6 100644 --- a/docs/openapi.json +++ b/docs/openapi.json @@ -44,6 +44,217 @@ "security": [] } }, + "/v1/status": { + "get": { + "tags": [ + "System" + ], + "summary": "Status page data: HelloNear and every service it depends on, checked every 5 minutes", + "description": "Public, any origin, no cookies (`access-control-allow-origin: *`, `cache-control: public, max-age=30`). Read by status.hellonear.xyz. See docs/api-contract.md \u00a77.", + "security": [], + "responses": { + "200": { + "description": "Current status, 24-hour history and incidents of the last 7 days", + "content": { + "application/json": { + "schema": { + "type": "object", + "required": [ + "updatedAt", + "overall", + "services", + "incidents" + ], + "properties": { + "updatedAt": { + "type": "string", + "format": "date-time" + }, + "overall": { + "type": "string", + "enum": [ + "operational", + "degraded", + "down" + ] + }, + "services": { + "type": "array", + "items": { + "type": "object", + "required": [ + "id", + "name", + "group", + "status", + "latencyMs", + "checkedAt", + "detail", + "impact", + "uptime24h", + "uptime7d", + "history" + ], + "properties": { + "id": { + "type": "string" + }, + "name": { + "type": "string" + }, + "group": { + "type": "string", + "enum": [ + "HelloNear", + "NEAR AI", + "NEAR", + "Partners" + ] + }, + "status": { + "type": "string", + "enum": [ + "operational", + "degraded", + "down", + "unknown" + ] + }, + "latencyMs": { + "type": [ + "number", + "null" + ] + }, + "checkedAt": { + "type": [ + "string", + "null" + ], + "format": "date-time" + }, + "detail": { + "type": [ + "string", + "null" + ] + }, + "impact": { + "type": "string" + }, + "uptime24h": { + "type": [ + "number", + "null" + ] + }, + "uptime7d": { + "type": [ + "number", + "null" + ] + }, + "history": { + "type": "array", + "items": { + "type": "object", + "required": [ + "start", + "status", + "checks", + "bad", + "detail" + ], + "properties": { + "start": { + "type": "string", + "format": "date-time" + }, + "status": { + "type": "string", + "enum": [ + "operational", + "degraded", + "down", + "none" + ] + }, + "checks": { + "type": "integer" + }, + "bad": { + "type": "integer" + }, + "detail": { + "type": [ + "string", + "null" + ] + } + } + } + } + } + } + }, + "incidents": { + "type": "array", + "items": { + "type": "object", + "required": [ + "service", + "name", + "status", + "start", + "end", + "minutes", + "detail" + ], + "properties": { + "service": { + "type": "string" + }, + "name": { + "type": "string" + }, + "status": { + "type": "string", + "enum": [ + "down", + "degraded" + ] + }, + "start": { + "type": "string", + "format": "date-time" + }, + "end": { + "type": [ + "string", + "null" + ], + "format": "date-time" + }, + "minutes": { + "type": "integer" + }, + "detail": { + "type": [ + "string", + "null" + ] + } + } + } + } + } + } + } + } + } + } + } + }, "/v1/auth/challenge": { "post": { "tags": [ @@ -2123,4 +2334,4 @@ } } } -} \ No newline at end of file +} diff --git a/engine/CLAUDE.md b/engine/CLAUDE.md index 978b1c35..7279a2bb 100644 --- a/engine/CLAUDE.md +++ b/engine/CLAUDE.md @@ -64,6 +64,7 @@ AI engine: a Mastra server that runs each Mind (soul, skills, tools, memory, sch - **Deleting a chat (#207, `src/threads.ts`, `DELETE /v1/minds/{mindId}/threads/{threadId}` with `{ userId }`):** `forgetThread` checks the thread's resource is the caller's (`ownsResource`: `userId:mindId` or `:v{n}`, else `404`), then `Memory.deleteThread` (from `mindAgent.getMemory()`: a bare `Memory` has no storage until the agent hands it Mastra's) and `await memory.settled()`. `PostgresStore.deleteThread` removes the messages, the `memory_messages*` vector rows (today `mastra.memory_messages_1024`) and the thread in one transaction, so a deleted chat no longer comes back through resource-scoped semantic recall. A missing thread is `204` (the backend retries until `204`). Working memory is per resource and stays. `test/threads.test.ts` writes rows and fake vectors directly (no NEAR AI); its last test embeds through NEAR AI and checks real recall. - **HelloNear guide and the Mind's own skills (#114):** `skills/hellonear-guide` has `builtin: true`: every Mind loads it (`mindSkills` in `src/skills.ts`), it is left out of `GET /v1/skills`, and it can't be equipped. It holds only what the app really has (screens, buttons, skills, try-out rules, "HelloNear support" messages are phishing); keep it in step with the FE when screens change. Each SKILL.md also has `title` (the Skills screen name) and `blurb` (one line on what it can and cannot do): `skillsLine` in `src/mind.ts` puts the Mind's skills and the ones it could add into its prompt. Without blurbs it once told an owner to add Email helper to send email, which Email helper cannot do. - **Memory in Settings (#209, `src/memory.ts`, `GET`/`PUT`/`DELETE /v1/minds/{mindId}/memory`):** the owner reads, replaces and clears the working memory of one resource, `resourceId(userId, mindId, personaVersion)`, the key chat runs use (GET takes `userId` and `personaVersion` as query parameters; PUT `{ userId, personaVersion?, markdown }` (at most 20,000 characters) answers `{ memory: { markdown, updatedAt } }`; DELETE `{ userId, personaVersion? }` answers `204`). Working memory is **free Markdown**: `mastra.ts` sets no `template` or `schema`, so the Mind is only shown Mastra's default template ("# User Information" with bullet fields) and each `updateWorkingMemory` replaces the whole document (docs-memory-working-memory.md in `@mastra/memory`). `updatedAt` is the row's (`mastra.mastra_resources`), one time for the whole document; nothing stamps sections. Clearing writes `''`, which `Memory.getWorkingMemory` reads as none; recall embeddings of past chats stay (deleting a chat drops those, #207). **Switches per run:** `mind.memory: { enabled?, recall? }` (absent = on); `memoryOptions` turns `false` into Mastra per-call `memory.options` (`workingMemory: { enabled: false }`: no document in the prompt, no `updateWorkingMemory` tool; `semanticRecall: false`: no vector search and the run's messages are not embedded, so chats held with recall off are not found later either). Workflow and Autopilot agent steps take no per-call options and keep the defaults. **When the embedder does not answer (13 Nov, `src/embedderHealth.ts`):** NEAR AI's embeddings endpoint hung for over 60 s while chat answered, and every chat run failed in Mastra's prepare-memory-step ("Failed to determine the embedder's output dimension"), the hosted demo too. A probe (one embedding, 8 s timeout, every 30 s, started at boot with `startEmbedderProbe(embedder)`) keeps `embedderReady()`; while it is false, `memoryOptions(mind, false)` adds `semanticRecall: false`, so chat runs still answer, without search of past chats (working memory and the thread's last messages stay). Workflow and Autopilot agent steps take no per-call memory options, so the agent's `memory` is a function too (`memoryFor()` in `src/mastra.ts`): while `embedderReady()` is false it returns `memoryWithoutRecall`, the same settings with `semanticRecall: false` and no vector or embedder; the agent hands it Mastra's storage, so threads, messages and working memory are shared. Turning recall off only in the call options (`defaultOptions`) did not help there: the workflow agent step still failed with the same error, so Mastra reads it from the Memory. Before the fix `test/workflows.test.ts` failed 2 of 9 with the embedder down (13 Nov); after it, 9 of 9. Tests: `test/embedder-health.test.ts`, `test/mind.test.ts` (unit) and `test/memory.test.ts` (Postgres; the switch tests run the real Memory on Mastra's mock model and check its prompt and tools; one test embeds through NEAR AI). +- **Status page probes (`src/probes.ts`, `GET /internal/v1/probes`, `X-Engine-Key`):** NEAR AI chat (`z-ai/glm-5.3-flash`, `max_tokens` 32, degraded over 10 s, down after 30 s) and embeddings (`Qwen/Qwen3-Embedding-0.6B`, degraded over 5 s, down after 15 s or an empty vector), as plain requests with the engine's key (not the verified transport: no run or badge depends on them). The backend calls it every 5 minutes for status.hellonear.xyz (`apps/be/src/status.ts`). Tests: `test/probes.test.ts` (fake NEAR AI), the 401 in `test/engine.test.ts`. - **Stop (#121, `abortOnClose` in `src/server.ts`):** chat and approve/decline runs pass an `abortSignal` to Mastra that fires when the backend drops the request (its cancel route aborts the fetch). Measured 9 Nov: without it the engine kept writing ~25 s (301 text deltas) after the cut, so the model kept spending tokens; with it, 0. The wallet and mail tools call the backend without that signal, so a payment already sent to OutLayer is never cut halfway. Builder workflow runs are not covered yet. - **#124 (live Meet test):** `meetLinkOf` takes the first `https://meet.google.com/` URL anywhere in Composio's answer (live, it was not at `response_data.meetingUri`); without one the error lists the field names it saw, never values. With a run `timeZone`, the prompt states the owner's zone and offset (`timeZoneLine`), and the calendar create tools refuse a time whose offset is not the owner's at that moment (`checkOwnerOffset`): "tomorrow 2pm" once became 14:00+00:00. Reply text goes through `plainDashes` (em and en dash → hyphen), because the prompt rule alone still let them through from real calendars; display only, verification covers the model's bytes. - **Composio tool versions (#124):** the tool router runs each tool's **latest** version, not the default schema `GET /api/v3/tools/{slug}` shows. Always read `?version=latest` before changing tool arguments. On 9 Nov `GOOGLECALENDAR_CREATE_EVENT` latest (`20261103_00`) takes `start_datetime` / `end_datetime` with an offset (end wins over the duration fields) and `send_updates: 'all' | 'externalOnly' | 'none'`; a boolean failed live. `create_meeting_room` now defaults to true there, so it is always sent `false`. diff --git a/engine/package.json b/engine/package.json index 439b6285..3cd12f46 100644 --- a/engine/package.json +++ b/engine/package.json @@ -9,7 +9,7 @@ "dev": "node --env-file=.env --import tsx src/server.ts", "test": "node --env-file=.env --import tsx --test test/*.test.ts", "typecheck": "tsc --noEmit", - "test:unit": "node --import tsx --test test/skills.test.ts test/templates.test.ts test/graph.test.ts test/secrets.test.ts test/verify.test.ts test/tools.test.ts test/builder.test.ts test/wallet-tools.test.ts test/email.test.ts test/composio.test.ts test/calendar.test.ts test/apps.test.ts test/titles.test.ts test/mail-tools.test.ts test/mind.test.ts test/attacks.test.ts test/auto-approve-rules.test.ts test/zoom.test.ts test/counted.test.ts test/crosschain.test.ts test/market.test.ts test/embedder-health.test.ts" + "test:unit": "node --import tsx --test test/skills.test.ts test/templates.test.ts test/graph.test.ts test/secrets.test.ts test/verify.test.ts test/tools.test.ts test/builder.test.ts test/wallet-tools.test.ts test/email.test.ts test/composio.test.ts test/calendar.test.ts test/apps.test.ts test/titles.test.ts test/mail-tools.test.ts test/mind.test.ts test/attacks.test.ts test/auto-approve-rules.test.ts test/zoom.test.ts test/counted.test.ts test/crosschain.test.ts test/market.test.ts test/embedder-health.test.ts test/probes.test.ts" }, "dependencies": { "@ai-sdk/openai-compatible": "3.0.82", diff --git a/engine/src/probes.ts b/engine/src/probes.ts new file mode 100644 index 00000000..500c82b0 --- /dev/null +++ b/engine/src/probes.ts @@ -0,0 +1,47 @@ +// NEAR AI checks for the status page (status.hellonear.xyz). The backend asks for them every 5 minutes through +// `GET /internal/v1/probes`, since only the engine holds the NEAR AI key. Plain requests, not the verified chat +// transport: they only ask whether NEAR AI answers, and no run or badge depends on them. The key never leaves here. +export type ProbeStatus = 'operational' | 'degraded' | 'down' +export type Probe = { id: 'nearai-chat' | 'nearai-embeddings'; status: ProbeStatus; latencyMs: number | null; detail: string | null } + +type Target = { baseUrl: string; apiKey: string; chatModel: string; embeddingModel: string } + +// Answers slower than this count as degraded; no answer within the timeout counts as down. +export const LIMITS = { chat: { degradedMs: 10_000, timeoutMs: 30_000 }, embeddings: { degradedMs: 5_000, timeoutMs: 15_000 } } + +async function one(id: Probe['id'], limits: { degradedMs: number; timeoutMs: number }, call: (signal: AbortSignal) => Promise): Promise { + const started = Date.now() + try { + const ok = await call(AbortSignal.timeout(limits.timeoutMs)) + const latencyMs = Date.now() - started + if (!ok) return { id, status: 'down', latencyMs, detail: 'Answered without a result.' } + return latencyMs > limits.degradedMs ? { id, status: 'degraded', latencyMs, detail: `Slow: answered in ${(latencyMs / 1000).toFixed(1)} s.` } : { id, status: 'operational', latencyMs, detail: null } + } catch (e) { + const name = (e as { name?: string })?.name + if (name === 'TimeoutError') return { id, status: 'down', latencyMs: null, detail: `No answer in ${limits.timeoutMs / 1000} s.` } + return { id, status: 'down', latencyMs: null, detail: e instanceof HttpStatus ? `Answered HTTP ${e.status}.` : 'Could not reach it.' } + } +} + +class HttpStatus extends Error { + constructor(readonly status: number) { super(`HTTP ${status}`) } +} + +async function post(t: Target, path: string, body: unknown, signal: AbortSignal): Promise { + const res = await fetch(`${t.baseUrl}${path}`, { method: 'POST', signal, headers: { authorization: `Bearer ${t.apiKey}`, 'content-type': 'application/json' }, body: JSON.stringify(body) }) + if (!res.ok) throw new HttpStatus(res.status) + return res.json() +} + +export async function nearAiProbes(t: Target): Promise { + return Promise.all([ + one('nearai-chat', LIMITS.chat, async (signal) => { + const out = await post(t, '/chat/completions', { model: t.chatModel, messages: [{ role: 'user', content: 'Reply with OK.' }], max_tokens: 32, reasoning_effort: 'low' }, signal) + return Array.isArray(out?.choices) && out.choices.length > 0 + }), + one('nearai-embeddings', LIMITS.embeddings, async (signal) => { + const out = await post(t, '/embeddings', { model: t.embeddingModel, input: 'ping' }, signal) + return (out?.data?.[0]?.embedding?.length ?? 0) > 0 + }), + ]) +} diff --git a/engine/src/server.ts b/engine/src/server.ts index 34bb5ead..7247dd0a 100644 --- a/engine/src/server.ts +++ b/engine/src/server.ts @@ -25,6 +25,7 @@ import { secretSchema } from './secrets.ts' import { deleteWorkflow, listWorkflows, loadWorkflow, saveWorkflow } from './workflows.ts' import { forgetThread } from './threads.ts' import { embedderReady, startEmbedderProbe } from './embedderHealth.ts' +import { nearAiProbes } from './probes.ts' import { ruleFor } from './autoApprove.ts' import { clearWorkingMemory, readWorkingMemory, writeWorkingMemory } from './memory.ts' import { autopilotSchema, deleteAutopilot, getAutopilot, listRuns, setAutopilot } from './autopilot.ts' @@ -209,6 +210,10 @@ export const server = createServer(async (req, res) => { if (req.method === 'GET' && url.pathname === '/v1/health') return sendJson(res, 200, { ok: true, mastra: '1.81.0', attestation: attestationStatus() }) if (!authorized(req)) return sendJson(res, 401, { error: 'unauthorized' }) + // Status page (apps/be/src/status.ts): NEAR AI chat and embeddings, checked with the engine's key. + if (req.method === 'GET' && url.pathname === '/internal/v1/probes') { + return sendJson(res, 200, { probes: await nearAiProbes({ baseUrl: config.nearaiBaseUrl, apiKey: config.nearaiApiKey, chatModel: config.defaultModel, embeddingModel: config.embeddingModel }) }) + } if (req.method === 'GET' && url.pathname === '/v1/templates') return sendJson(res, 200, { templates: templateSummaries() }) if (req.method === 'GET' && url.pathname === '/v1/skills') return sendJson(res, 200, { skills: skillCatalogSummaries() }) // Skill ids are [a-z0-9-] (skills.ts), so anything else falls through to 404 without decoding. diff --git a/engine/test/engine.test.ts b/engine/test/engine.test.ts index 06ac6828..fee38e18 100644 --- a/engine/test/engine.test.ts +++ b/engine/test/engine.test.ts @@ -41,6 +41,7 @@ const mind = (name: string, extra: Record = {}) => ({ describe('engine HTTP API', () => { test('health is public, everything else needs X-Engine-Key, bad bodies are rejected', async () => { assert.equal((await fetch(base + '/v1/health')).status, 200) + assert.equal((await fetch(base + '/internal/v1/probes')).status, 401, 'the status probes need X-Engine-Key') assert.equal((await post('/v1/minds/m1/chat', {}, {})).status, 401) assert.equal((await post('/v1/minds/m1/chat', {}, { 'x-engine-key': 'wrong' })).status, 401) assert.equal((await post('/v1/minds/m1/chat', { userId })).status, 400) diff --git a/engine/test/probes.test.ts b/engine/test/probes.test.ts new file mode 100644 index 00000000..3e14c225 --- /dev/null +++ b/engine/test/probes.test.ts @@ -0,0 +1,71 @@ +// NEAR AI checks for the status page against a fake NEAR AI: no network, key or model needed. +import { after, before, test } from 'node:test' +import assert from 'node:assert/strict' +import { createServer, type Server } from 'node:http' +import type { AddressInfo } from 'node:net' +import { LIMITS, nearAiProbes } from '../src/probes.ts' + +let mode: 'ok' | 'chat500' | 'embedEmpty' | 'hang' = 'ok' +const seen: { path: string; auth?: string; body: any }[] = [] +let server: Server +let baseUrl = '' + +before(async () => { + server = createServer((req, res) => { + let raw = '' + req.on('data', (c) => (raw += c)) + req.on('end', () => { + seen.push({ path: req.url ?? '', auth: req.headers.authorization, body: JSON.parse(raw || '{}') }) + if (mode === 'hang') return // never answers + if (req.url === '/chat/completions') { + if (mode === 'chat500') return void res.writeHead(500).end('{}') + return void res.writeHead(200, { 'content-type': 'application/json' }).end(JSON.stringify({ choices: [{ message: { content: 'OK' } }] })) + } + const embedding = mode === 'embedEmpty' ? [] : [0.1, 0.2] + res.writeHead(200, { 'content-type': 'application/json' }).end(JSON.stringify({ data: [{ embedding }] })) + }) + }) + await new Promise((r) => server.listen(0, r)) + baseUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}` +}) +after(() => { server.closeAllConnections(); server.close() }) + +const target = () => ({ baseUrl, apiKey: 'test-key', chatModel: 'z-ai/glm-5.3-flash', embeddingModel: 'Qwen/Qwen3-Embedding-0.6B' }) +const byId = (ps: Awaited>) => Object.fromEntries(ps.map((p) => [p.id, p])) + +test('both answer: operational, with the key sent only to NEAR AI and the right models', async () => { + mode = 'ok'; seen.length = 0 + const p = byId(await nearAiProbes(target())) + assert.equal(p['nearai-chat'].status, 'operational') + assert.equal(p['nearai-embeddings'].status, 'operational') + assert.equal(p['nearai-chat'].detail, null) + assert.ok(seen.every((s) => s.auth === 'Bearer test-key')) + assert.equal(seen.find((s) => s.path === '/chat/completions')!.body.model, 'z-ai/glm-5.3-flash') + assert.equal(seen.find((s) => s.path === '/embeddings')!.body.model, 'Qwen/Qwen3-Embedding-0.6B') +}) + +test('a 500 or an empty embedding is down, and one failing never hides the other', async () => { + mode = 'chat500' + let p = byId(await nearAiProbes(target())) + assert.deepEqual([p['nearai-chat'].status, p['nearai-chat'].detail], ['down', 'Answered HTTP 500.']) + assert.equal(p['nearai-embeddings'].status, 'operational') + mode = 'embedEmpty' + p = byId(await nearAiProbes(target())) + assert.equal(p['nearai-embeddings'].status, 'down') + assert.equal(p['nearai-chat'].status, 'operational') +}) + +test('no answer within the timeout is down, never a hang', async () => { + mode = 'hang' + const saved = structuredClone(LIMITS) + LIMITS.chat.timeoutMs = 200; LIMITS.embeddings.timeoutMs = 200 + try { + const started = Date.now() + const p = byId(await nearAiProbes(target())) + assert.ok(Date.now() - started < 3000) + assert.deepEqual([p['nearai-chat'].status, p['nearai-chat'].detail], ['down', 'No answer in 0.2 s.']) + assert.equal(p['nearai-embeddings'].status, 'down') + } finally { + Object.assign(LIMITS.chat, saved.chat); Object.assign(LIMITS.embeddings, saved.embeddings) + } +})