CandyCodeWeb
Browser UI for candy-code's server mode
candy-code-web is the browser face of candy-code's WebSocket server mode. It is a Vite, Vue 3, TypeScript and Pinia app that is built once and committed as dist/, plus a one-class PHP shim, SugarCraft\CodeWeb\Assets, that tells candy-code serve where the bundle lives. PHP users get the UI from composer require alone and never need Node to run it. Node is needed only to change the UI.
Install
composer require sugarcraft/candy-code-web:@dev
Requires PHP 8.3 or newer. The package has no release tag yet, and Packagist serves dev-master only. A project on the default minimum-stability: stable must add the :@dev suffix, or the install will not resolve.
Install it in the PHP project that hosts candy-code, in the same vendor tree that candy-code serve resolves at startup. A project-local install and composer global require both qualify, as long as the two packages land in one autoloader. candy-code lists this package under suggest, not require, so headless installs stay lean.
The candy-code phar cannot host this UI. A packaged phar freezes its own vendor as the only autoload root, and this package is only a suggestion, so a phar install never finds it. The browser UI is a Composer-channel feature.
Quickstart
Start the server and open the sign-in URL it prints. The browser takes over from there, and users never write a line of PHP.
candy-code serve
The server binds to 127.0.0.1 by default, so the URL works on the same machine. Exposing the server is a separate decision, covered under Configuration below.
The locator class exists so the server can find the bundle, and so a distributor can verify a build's identity:
use SugarCraft\CodeWeb\Assets;
$root = Assets::distPath(); // absolute, realpath()-resolved dist/ directory
$manifest = Assets::manifest(); // Vite manifest: 'index.html' => ['file' => 'assets/index-….js', …]
echo Assets::version(), PHP_EOL; // bundle version stamped into dist/build-info.json
Concepts
The package has two halves that never mix at runtime. The PHP half is a single class that answers one question: where is the bundle, and is it intact. The TypeScript half is the UI itself, which speaks the candy-code.v1 protocol over one WebSocket. Between them sits a committed build, so a PHP install never runs a Node toolchain.
The wire, not the UI, is the contract
The page trades a one-time code for an HttpOnly session cookie, then holds one WebSocket that speaks candy-code.v1. The protocol and the UI are documented in candy-code's docs/SERVER.md. Nothing in this package defines a second protocol. The generated protocol mirror and the string catalogue are regenerated from candy-code's sources, so the UI cannot drift from the server silently.
Committed bundle, checked build
The built bundle is part of the package, in the same way as the committed demo GIFs. Content-hashed file names and the absence of timestamps keep a rebuild stable. CI runs npm ci, lint, typecheck, the unit tests and a build, then fails if the committed dist/ differs from what the source produces. A UI change is therefore one commit that carries the source and the bundle together.
The PHP shim is for location, not rendering
Assets finds dist/, parses the Vite manifest and the build stamp, and reports on them. It never serves a file, never reads a request, and never renders HTML. The server does the serving, behind its own security headers and path jail.
Tasks
Open a session from the browser
Open the sign-in URL. Each session opens as a tab, and the sessions sidebar lists every session on the server, newest first, with a filter and a status for each. A badge marks a session that is waiting on a question. Rows for another workspace carry a Close action that asks first, and retries with force when the server refuses because a turn is running.
Start a session in another directory
Start the server with --allow-dir-browse. New session then opens a folder picker over the server's browse root, with a breadcrumb, child directories with a project badge, a show-hidden toggle and a path box. Without the flag, New session starts on the server's own root.
Answer a permission card
A permission card offers once, always for this session, always and save, reject, and reject and stop. Press y, a or n on a focused card. Always (save) is the only answer that writes a durable grant to the config, so it takes two steps. The first click spells out what it saves, and the second commits. A card whose answer never settles re-arms its buttons after ten seconds.
Watch several sessions at once
Open sessions stay as tabs, and the grid tiles up to nine of them, three by three. Each tile shows its status, step, spend and the last few events. The focused tile streams in full, and the others are narrated by the server with a tail every two seconds. A screen reader hears only the focused tile. The layout is remembered per browser.
Share a read-only snapshot
On a write-scoped connection, a session row offers Share page. It publishes a snapshot and shows the full link, its expiry, and a copy button. A share is the only read-only door to a session, so the viewer of the link cannot prompt, answer or steer it. Any browser of the same server can revoke a share, because every signed-in browser is the same operator.
Answer from the approvals drawer
The warning count in the top bar opens the approvals drawer. It lists every open question in every session of the server, oldest first, grouped by session, and answerable in place. A question appears the moment it is put, whether or not the page follows its session. Opt-in desktop notifications also say when a turn finishes out of sight.
Turn on device notifications
Settings has a toggle, Notify me on this device. It subscribes the browser through the service worker at /sw.js, so a question or a finished turn still raises a notification when the page is closed. The toggle needs a secure context, which means HTTPS or localhost. Switching it off unsubscribes both the browser and the server.
Dictate a prompt
The mic button on the composer dictates through one of two engines, and its title says which one is live. The server engine is used when the server advertises transcribe, and it sends the audio to the server's own voice endpoint. The browser engine hands audio to the browser vendor's speech service, and Firefox has no such engine. Both need a secure context, so on a plain-HTTP LAN address the mic button is absent.
API reference
The whole PHP surface is SugarCraft\CodeWeb\Assets, a final class with static methods and no instances. Every method reads from the package's dist/ directory. distPath(), manifest() and version() throw when the bundle is broken, while tryDistPath() returns null instead of throwing. diagnose() never throws, which makes it the one to call from a health check.
Assets::distPath(); // absolute, realpath()-resolved dist/ — throws RuntimeException
// when dist/index.html is missing (a broken install)
Assets::tryDistPath(); // soft probe: null instead of throwing
Assets::manifest(); // decoded Vite manifest: entry => {file, css, …}
Assets::version(); // bundle version, stamped into dist/build-info.json
Assets::diagnose($packageRoot); // {dist, manifest, version, error}, never an exception
| Class | Method | Description |
|---|---|---|
| Assets | static distPath(): string | Absolute, realpath()-resolved path of the committed bundle. Throws RuntimeException when dist/index.html is missing, which is a broken install. |
| Assets | static tryDistPath(): ?string | The soft probe of the same location. It returns null instead of throwing, for callers that degrade. A server with no UI keeps the API and the WebSocket up. |
| Assets | static manifest(): array | The decoded Vite manifest from dist/manifest.json. Each source entry maps to its file, css and isEntry. |
| Assets | static version(): string | The bundle's release version from dist/build-info.json. The package.json file is not shipped to Packagist, so the version travels inside dist/. |
| Assets | static diagnose(string $packageRoot): array | A structured {dist, manifest, version, error} report for any package root, which is the directory that holds src/ and dist/. It is the failure surface of the three methods above, made observable. |
The class holds two constants, MANIFEST = 'manifest.json' and BUILD_INFO = 'build-info.json'. Both are public, so a distributor can read the same files without going through the class.
Only methods declared in candy-code-web/src, or inherited from a parent declared there, are listed. A type that extends a class outside that tree, such as \RuntimeException, does not show the methods of that class.
SugarCraft\CodeWeb
Assets final class
Locates the pre-built candy-code-web bundle on disk.
| Method | Summary |
|---|---|
static tryDistPath(): ?string | Absolute path of the committed bundle (no trailing slash), or null when the install is incomplete. |
static distPath(): string | Absolute path of the committed bundle (no trailing slash). |
static manifest(): array | The decoded Vite manifest: source entry => {file, css?, isEntry?, …}. |
static version(): string | The bundle's release version, as package.json declared it at build time. |
static diagnose(string $packageRoot): array | Structured health report for any package root — the whole failure surface of distPath(), manifest() and version() made observable without a thrown exception. Written for tests and a future serve doctor; the package's own root is just one argument. |
Configuration
The package reads no configuration file, and it has no options of its own. Two settings decide what the server does with the bundle, and both belong to candy-code.
| Setting | Type | Default | Effect |
|---|---|---|---|
--web-root <dir> (candy-code serve) | path | the packaged dist/ | Serves a custom build of the bundle. It is the only documented way to serve your own dist/. |
CANDY_CODE_SERVER_WEB_ROOT | path | unset | The environment form of --web-root. |
--allow-dir-browse (serve) | flag | off | Lets New session open a folder picker over the server's browse root. |
--allow-remote, --tls-cert, --tls-key, --allowed-ips | flags | loopback only | Exposure. The server refuses a non-loopback host without --allow-remote. The TLS flags terminate TLS in the server, and --allowed-ips bounds who may connect. |
The Settings form in the UI is generated from the server's own schema. Each value shows where it came from, whether an environment variable or a flag locks it, and how it applies. A diff of the file is previewed before anything is saved.
Deployment and the front proxy
Decide exposure before headers. The UI is as public as the server behind it. The Dockerfile's shipped serve --host 0.0.0.0 --allow-remote posture makes that decision for you, so read it before you use it.
The server ships dist/assets uncompressed, at about 690 KB cold. Compression is the front proxy's job, so turn gzip or brotli on for /assets/. The file names are content-hashed and served as immutable, so proxy-compressed copies stay valid across deploys.
candy-code serve already sends a Content-Security-Policy, along with nosniff, Referrer-Policy: no-referrer, frame denial and same-origin COOP and CORP headers. The policy is applied to every response, including the bundle and the share pages, except the WebSocket handshake and the /canvas/{token} pages. Those carry their own sandbox policy, with connect-src 'none' and frame-ancestors 'self', and candy-code's SERVER.md documents it. The default policy is:
default-src 'self'; connect-src 'self'; img-src 'self' data: blob:; media-src 'self' blob:; style-src 'self' 'unsafe-inline'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'
Browser storage
Everything the UI keeps in the browser lives in localStorage under the candy-code-web: prefix. Sign-out clears the credential, the transcript caches and the tab-restore keys. Device preferences such as the theme survive it. The keys that matter most are these:
| Key | Contents | Bound | Cleared by |
|---|---|---|---|
login-secret | The sign-in secret. It is a credential. | one | Sign-out, or any 401 from the server. |
promptHistory | Recently sent prompts, newest first. | 50 entries of 16 Ki characters | Sign-out in every tab, or the palette's Clear prompt history. |
drafts | Unsent composer text, per session. | 50 sessions of 16 Ki characters | Sign-out, a session leaving the index, or first-in first-out eviction. |
theme, notify, diffStyle | UI preferences. | enum values | Kept across sign-out. |
This table covers the keys that matter most. The full inventory is the Browser storage table in the package README, which is the one the test suite checks: a spec derives every persisted key from the sources and fails when a key ships without a row there, or a row outlives its key.
The sign-in secret stays in storage while you are signed in, so anything that can run script on this origin can read it. Keep the host page clean, and treat browser storage as holding a credential.
Keys and shortcuts
The UI is keyboard first. Press ? for the help overlay, which lists the live chords. It opens only when the composer is empty or focus sits outside an editable field. Three actions can be rebound and are kept per browser: the command palette, the help overlay and the tab chord. The defaults in this table are the shipped ones.
| Key | Action |
|---|---|
? | Open the help overlay, which lists every live chord. |
Ctrl+K / ⌘K | Open the command palette: sessions, slash commands, panels, a new session and the theme. |
Alt+1 to Alt+9 | Pick an open tab. This is the tab chord, and the tab strip cycles it. |
Enter | Send the prompt. While a turn runs, the server's queue mode decides whether it steers, queues or interrupts. |
Esc Esc | Stop the running turn. |
Ctrl+↑ / Ctrl+↓ | Walk the prompt history back and forth. |
Ctrl+G | Search the prompt history. Enter fills the draft, and Esc restores yours. |
y, a, n | Answer a focused permission card. |
/ and @ | Complete a server command, or a mention such as a file, an MCP resource or a sub-agent. |
↑ / ↓, Enter / →, Backspace / ←, Home / End | In the folder picker: move, open, go up, and jump to the first or last entry. |
The chords quoted elsewhere in this page are defaults, not law. A rebinding saved in this browser wins.
Theme and accessibility
The theme is a per-browser preference, stored under the theme key. The command palette has a theme action that changes it. The UI reads prefers-reduced-motion, and when the user asks for reduced motion it turns off all animations and transitions globally.
Keyboard focus rings are set globally on buttons, links, selects, inputs and text areas. They show on keyboard focus and not on a click. Every dialog runs through one shared focus spine: focus enters the panel on open, Tab stays inside it, Esc dismisses it, and the trigger regains focus on close. A test fails if a dialog bypasses the spine.
Screen readers hear a newly arrived question through a polite live region, with where it came from and what it offers. A completed reply is announced once at the tail of the transcript. Token deltas are not announced, because that would flood the reader. Diff rows never rely on colour alone: each row has a plus or minus glyph and a kind class, and its old and new line numbers are read out.
The tab title carries a (N) count of pending questions, so a waiting ask is visible without reading the page.
Developing the UI
The TypeScript lives in src-web/, which keeps it out of the PSR-4 src/ tree. Node is pinned in .nvmrc. Every command below runs in the package directory.
cd candy-code-web
npm ci
npm run dev # Vite dev server; proxies /ws and /api to 127.0.0.1:7420
npm run typecheck # vue-tsc
npm run lint # eslint
npm test # vitest (happy-dom): protocol client, reducer, stores, components
npm run build # writes dist/ — commit it with the source change
npm run check:dist # CI law: dist/ must be byte-identical to a clean build of the source
The dev server forwards /ws and /api to a running candy-code serve on 127.0.0.1:7420, so start the server first. Two generators keep the UI in step with the server. npm run gen:protocol regenerates the protocol types from the wire schema, and npm run gen:strings regenerates the string mirror from candy-code's catalogue. Neither file is edited by hand.
Adding a user-facing string
Every sentence in the UI is an i18n string. The English copy lives in candy-code, and this package keeps a committed mirror of it. A new string follows one direction. Add the key to candy-code's catalogue and its locale twins, all or none. The sibling regenerates the JSON that the mirror reads from, and then npm run gen:strings rewrites the mirror, which is committed with the change. Call sites use t('web.<area>.<name>'), and the key must carry the web. prefix. The server sends its own strings at runtime, so the mirror is only the offline fallback. Never edit the mirror by hand.
Examples
The package ships two PHP examples in examples/. They read the committed bundle, and they need only composer install in this checkout. Neither one starts a server.
| Example | What it does | Exit code |
|---|---|---|
examples/diagnose.php | Prints the Assets::diagnose() report for this checkout, or for a package root passed as the first argument. | 0 when the bundle is intact, 1 when the report carries an error. |
examples/locate.php | Shows the soft probe that candy-code serve uses. It prints the web root, the index entry and the version, or says that no bundle is present. | 0 in both cases. |
composer install
php examples/diagnose.php
php examples/locate.php
php examples/diagnose.php /path/to/another/package-root
A healthy checkout prints a dist: line with the absolute bundle path, a version: line, and an entries: count. The values below are from a real run, and the path is shown as a placeholder.
dist: /home/you/candy-code-web/dist
version: 0.1.0
entries: 3
Testing
The PHP side has one suite, and the UI has four checks. Run them in this order.
composer install && vendor/bin/phpunit # tests/AssetsTest.php
npm ci && npm test # vitest, happy-dom
npm run typecheck && npm run lint
npm run e2e # Playwright (Chromium) against a real candy-code serve
tests/AssetsTest.php checks the committed bundle itself. The manifest's files exist and are content-hashed. index.html loads the entry by a relative URL. The version matches package.json. No source map or .env file has leaked into dist/.
CI runs the web leg in .github/workflows/web.yml. It runs npm ci, the audit, lint, typecheck, the unit tests, the build, and the dist check, and then the end-to-end suite.
Troubleshooting
candy-code serve runs, but the browser shows no UI
The bundle is missing or incomplete, and the server is serving the API and the WebSocket without a page. Assets::distPath() throws a RuntimeException that reads dist/index.html is missing. Run php examples/diagnose.php to see the verdict, then reinstall the package with composer require sugarcraft/candy-code-web:@dev so the dist/ directory is restored.
The report names a broken manifest or build stamp
diagnose() reports dist/manifest.json or dist/build-info.json as missing or not valid JSON, and it names the file. A broken stamp means a damaged download or a hand-edited bundle. Reinstall the package rather than repairing the file by hand.
The mic button or the device-notification toggle is missing
Both need a secure context, which means HTTPS or localhost. On a plain-HTTP address that is not localhost, the browser refuses the microphone and the push APIs, so the controls are hidden. Serve the UI over TLS with --tls-cert and --tls-key, or through a TLS front proxy.
The folder picker is missing from New session
The picker needs the server to run with --allow-dir-browse. Without it, New session starts on the server's own root, which is the intended behaviour.
The page says the server is gone
The reconnect ladder ran out. The client retries six times, with the delay doubling from half a second to fifteen seconds, plus or minus thirty per cent jitter. Click Try again to spend a fresh ladder. If the server is still down, start candy-code serve again.
The browser asks you to sign in again after a reload
Sign-out clears the stored secret, and so does any 401 from the server. Sign in again from the URL the server printed.
npm run dev cannot reach the API
The dev server proxies /ws and /api to 127.0.0.1:7420. Start candy-code serve on that port before npm run dev, or the proxy has nothing to forward to.
CI fails with "dist/ is stale"
A source change was committed without its bundle. Run npm ci && npm run build in candy-code-web/, then commit dist/ with the source change in the same commit. npm run check:dist reproduces the CI verdict locally before you push.
Upstream mapping
There is no upstream to mirror. The UI is first-party, and the PHP shim is too. The nearest analogues are the opencode web UI and the OpenClaw Control UI, which ship the same way: a static bundle served by the agent's own server. Its protocol is candy-code's own candy-code.v1, documented in candy-code's docs/SERVER.md.
| Concern | Where it lives |
|---|---|
| Bundle location and identity | src/Assets.php, the whole PHP surface |
| UI source | src-web/, Vue 3, Pinia and Vite |
| Committed build | dist/, checked by npm run check:dist in CI |
| Wire protocol | candy-code's candy-code.v1, mirrored by npm run gen:protocol |
| Strings | candy-code's catalogue, mirrored by npm run gen:strings |
Architecture
The package has three layers that meet at two files. The PHP layer is src/Assets.php. It resolves the path of dist/, checks that index.html is there, and reads the manifest and the build stamp. The UI layer is src-web/, a Vue 3 application with Pinia stores and a single WebSocket client. The build layer is vite.config.ts, which writes the manifest, stamps the build with its version, and produces the content-hashed files in dist/.
The Composer archive excludes src-web/, tests/, phpunit.xml, node_modules/, the Node configuration, the build scripts and the Playwright files. What remains is the built bundle, the PHP shim, the examples/ directory, and the top-level README, LICENSE and CALIBER_LEARNINGS.md. A Composer install therefore carries the bundle, the one-class locator and the examples, and no build tooling.
Path safety
Assets resolves dist/ through realpath(). A server that confines requests to that directory can then compare the request path against the same canonical prefix the kernel reports, even when vendor/ is a symlink farm, as path repositories make it.
Changelog and versioning
No release is tagged yet. The version is the build stamp in dist/build-info.json, currently 0.1.0, and Assets::version() reads it. The stamp travels inside dist/ because package.json is excluded from the Packagist archive. Until a tag exists, install with :@dev and check the stamp to know which build you have.
Related
- candy-code: the server this UI is served by, and the owner of the protocol and the strings.
- candy-code SERVER.md: the protocol, the server flags and the trust model.