import { AppUiBuildConfig } from '@agentproto/app'; /** * Build an installed app's UI bundle on demand, per APP.md's declared * `ui.build` (`@agentproto/app`'s `AppUiBuildConfig`) — the piece that * lets a repo declare "how to build the UI" instead of committing the * generated `.agentproto/ui/index.html`. * * `ensureAppUiBuilt` is the one entry point, called from every path that's * about to serve or install an app's UI (`app_install` in app-tools.ts, * `GET /apps/:appId/ui` in http-server.ts, the MCP panel cache in * app-ui-apps.ts, and the CLI's `agentproto app serve`): if the declared * `uiPath` is missing, or older than the newest file matching `ui.build`'s * `sources` globs (default `["src/**"]`, resolved against `ui.build.cwd` — * default the app dir), it runs `ui.build.command` once, captures output to * `~/.agentproto/logs/app-ui-build/` (see `appUiBuildLogPath`), and returns a result the caller turns * into either a served page or a readable error. Concurrent callers for the * same `uiPath` share one in-flight build (single-flight, keyed on the * absolute `uiPath` — an app's bundle lives at one path regardless of which * route is asking). * * Staleness is two-tier. The mtime comparison above is only the fast path: * when it says "stale", the sources' CONTENT is hashed and compared with the * hash recorded after the last successful build (`appUiBuildStampPath`, under * the daemon state dir). A matching hash means nothing that feeds the bundle * actually changed — a `git checkout`/`stash`/hook that rewrote files with * identical bytes only bumped their mtimes — so no build runs. Test files * (`DEFAULT_SOURCE_EXCLUDES`) never count as sources, and a `!pattern` entry * in `sources` excludes more. * * Page renders go through `resolveAppUiBuildState`, which never makes a user * wait on a REbuild: when a bundle already exists it is served as-is while * the rebuild runs in the background (stale-while-revalidate), and a failed * background rebuild keeps serving it, without retrying until the sources * change again. Only a MISSING bundle shows the "building" placeholder. * * An app with no `ui.build` declared keeps today's behavior exactly: the * bundle must already exist on disk, and a missing one is a clear error * naming the path rather than a bare 404. */ /** Never sources, whatever `ui.build.sources` says: tests don't feed a UI * bundle, so editing one must not trigger a multi-second rebuild. */ declare const DEFAULT_SOURCE_EXCLUDES: readonly string[]; /** * Every file matched by `patterns` (relative to `cwd`), minus * {@link DEFAULT_SOURCE_EXCLUDES} and any `!pattern` entry in `patterns`. * Returned sorted by relative path, deduped (two patterns can match the same * file), as `{ abs, rel }` with `rel` always `/`-separated. */ declare function collectSourceFiles(cwd: string, patterns: readonly string[]): Promise<{ readonly abs: string; readonly rel: string; }[]>; /** Newest mtime (ms) among every source file `patterns` selects (relative * to `cwd`, see {@link collectSourceFiles}), or `undefined` if none. */ declare function newestSourceMtime(cwd: string, patterns: readonly string[]): Promise; /** * Content hash of everything that decides what `build` produces: the * selected source files (relative path + bytes, in sorted order) and the * build command itself. Identical inputs hash identically whatever their * mtimes — the point of the second staleness tier. */ declare function hashAppUiSources(uiPath: string, cwd: string, build: AppUiBuildConfig): Promise; /** Where the build stamp for `uiPath` lives — the daemon state dir, never * the app dir (same reasoning as {@link appUiBuildLogPath}); `` is * over the absolute `uiPath`, the same key single-flight uses. */ declare function appUiBuildStampPath(uiPath: string): string; /** Where a `ui.build` run's captured output lands — read this on a build * failure for the full log; the returned error carries only a tail. * Lives under the daemon state dir * (`$AGENTPROTO_HOME|~/.agentproto/logs/app-ui-build/-.log`), * never inside the app dir: an installed app's dir is replaceable code * (swapped on reinstall, packed into `.agentapp`s), not a place for * runtime output. `` is over the absolute app dir, so two apps with * the same folder name never share a log. */ declare function appUiBuildLogPath(appDir: string): string; interface EnsureAppUiBuiltInput { /** Absolute app directory — anchors a relative `build.cwd` and the build * log's location. */ readonly dir: string; /** Absolute path to the ui bundle's entry file (APP.md's `ui.path`, * already resolved absolute). Also the single-flight key. */ readonly uiPath: string; readonly build?: AppUiBuildConfig; /** Hard timeout for the build command. Defaults to 120s. */ readonly timeoutMs?: number; /** Return the previous build's failure, without re-running the command, * when the sources hash to what that failed build saw. Set by page * renders (`resolveAppUiBuildState`), which would otherwise re-run a * broken build on every panel open; left off for explicit callers * (`app_install`, `app serve`), where re-running IS the retry. */ readonly reuseFailure?: boolean; } type EnsureAppUiBuiltResult = { readonly ok: true; readonly built: boolean; } | { readonly ok: false; readonly error: string; }; /** Peek whether `uiPath` has a build running right now without starting one * — a non-blocking caller (`resolveAppUiBuildState`) uses this to join an * ALREADY-showing build instead of racing a fresh `ensureAppUiBuilt` call * (which would itself re-do the cheap stat/freshness check every time). */ declare function peekInFlightBuild(uiPath: string): Promise | undefined; /** Paired with {@link peekInFlightBuild}: when that returns a promise, this * is the wall-clock time (`Date.now()`) its build command actually started * — `undefined` if the in-flight promise hasn't reached `runShellCommand` * yet (a handful of stat calls out), in which case "now" is as good a * start time as any. */ declare function peekBuildStartedAt(uiPath: string): number | undefined; /** * Ensure `uiPath` exists and is at least as new as its declared sources, * building it first if not. Single-flight per `uiPath`: a second call for * the same path while a build is running awaits the same result instead of * starting a second build. */ declare function ensureAppUiBuilt(input: EnsureAppUiBuiltInput): Promise; type AppUiBuildState = /** Serve `uiPath`. `stale` is set when the bundle on disk is NOT the * current sources' build — `"rebuilding"` (a background build is running; * the next render picks up its output) or `"build-failed"` (the last * rebuild failed; see the build log) — but is still the best page there * is. */ { readonly kind: "ready"; readonly stale?: "rebuilding" | "build-failed"; } | { readonly kind: "building"; readonly startedAt: number; readonly logTail?: string; } | { readonly kind: "error"; readonly message: string; readonly logPath: string; readonly logTail?: string; }; /** * Non-blocking status check for `ensureAppUiBuilt`'s outcome — the piece * that lets a PAGE render (the `ui://app_ui_/view` MCP resource in * app-ui-apps.ts, `GET /apps/:appId/ui` in http-server.ts) never stall on a * multi-second `ui.build` run. Joins an already-running build via * {@link peekInFlightBuild} (or starts one — still single-flight, since * `ensureAppUiBuilt` itself owns `inFlight`) and waits only up to * `RESOLVE_FAST_PATH_MS`: long enough that the common "already built and * fresh" case (a handful of `stat` calls) resolves as `"ready"` without ever * showing a placeholder, short enough that a real build never blocks the * caller — it keeps running in the background regardless, and the NEXT call * (after the page's own reload) picks up wherever that build landed. * * Stale-while-revalidate: when a bundle already exists, a REbuild never * shows the "building" placeholder or a failure page — the existing bundle * is served (`ready` + `stale`) while the build runs, and after it fails. * A failed rebuild isn't re-run on each render until its sources change * (`reuseFailure`). Only a missing bundle yields `building`/`error`, and that * case always retries, since there's nothing else to serve. */ declare function resolveAppUiBuildState(input: EnsureAppUiBuiltInput): Promise; export { type AppUiBuildState, DEFAULT_SOURCE_EXCLUDES, type EnsureAppUiBuiltInput, type EnsureAppUiBuiltResult, appUiBuildLogPath, appUiBuildStampPath, collectSourceFiles, ensureAppUiBuilt, hashAppUiSources, newestSourceMtime, peekBuildStartedAt, peekInFlightBuild, resolveAppUiBuildState };