Before you design a change, read Contributing and philosophy: the six principles every feature follows.
| Path | What |
|---|---|
App/ |
The Mac app (SwiftUI and Swift Charts, no dependencies). Files that need no AppKit or SwiftUI are shared with Linux. |
Linux/ |
The Linux service's own Swift files, the KDE app and widget (Linux/kde), the installer and the Flatpak. See Linux/README.md. |
agents/ |
Coding-agent reporters. See agents/README.md. |
Android/ |
The Android app: the web dashboard in a web view, with pairing and finding a machine that answers. Android/build.sh builds it with the Android SDK's own tools, and with Go adds its built-in Tailscale node (Android/tailnet, on tsnet). See Android/README.md. |
web/ |
The read-only web dashboard every service serves at /view/ (plain HTML, CSS and JavaScript, no build step). scripts/embed-web.sh writes it into Linux/WebSources.swift, with the KDE app's Machine Room painters (Linux/kde/ui/CampusPainter.js, InsidePainter.js) wrapped for the browser; run it after changing any of them, or the build check fails. The home-screen icons under web/icons/ are drawn from icon.svg by scripts/web-icons.sh. |
ThirdParty/CSQLite/ |
SQLite's source as one file (the amalgamation, unchanged), compiled into the service on Linux and Windows. The Mac uses the system's SQLite. Its README says which version and how to replace it. |
ThirdParty/CZlib/ |
The part of zlib that compresses in memory (unchanged), compiled into the service on Linux and Windows for its gzip answers. The Mac uses the system's zlib. Its README says which version and how to replace it. |
Tests/ |
Checks for parsers and rules |
icon/ |
The app icon |
scripts/ |
Packaging and test scripts |
Needs Xcode 26 or later.
./build-library-app.sh # builds Tokenmeter.app
./build-library-app.sh --fast # for checking a change: not optimised, only changed files compiled again
scripts/package.sh # makes dist/Tokenmeter-<version>.dmg
pkill -x Tokenmeter # quit a running copy before opening a new buildPackage.swift also builds on a Mac: there it is Tokenmeter's background service, the same tokenmeter serve as on Linux (its local server and routes in Linux/, built with TOKENMETER_SERVICE, which leaves out the views the shared files keep for the app). It answers what the KDE app's pages ask, on the app's status endpoint port (7733 unless changed), while no window is open. Run it by hand to try it, with Tokenmeter.app quit:
swift build -c release # .build/release/tokenmeter
.build/release/tokenmeter serveThe service does the work and the app only shows it. work.lock in Tokenmeter's folder (WorkLock) keeps two services from counting the same requests or opening the same ports. build-library-app.sh puts a copy of the service inside Tokenmeter.app (Contents/MacOS/tokenmeter-service); at each launch the app installs it in ~/Library/Application Support/Tokenmeter/service when none is there or the installed one is older (BackgroundService in App/ClientMode.swift, the places in App/MacService.swift), loads its launch agent into launchd, restarts it when the file changed, and shows it (ClientMode). While it doesn't answer, a bar above the pages says why and offers to start it.
scripts/package-service.sh packs the service alone for the machine it runs on: dist/tokenmeter-service-<version>-<os>-<arch>.tar.gz (linux-x86_64, linux-aarch64, macos-arm64) with its .sha256. Inside are the tokenmeter program, install.sh (Linux/install.sh, which installs only the service when no app is next to it, or Mac/install-service.sh), a version file and manifest.json with the program's checksum (ServicePackage in App/ServicePackage.swift). scripts/package-linux.sh and scripts/package.sh make it from the same build as the app's download, so the file is the same everywhere. tokenmeter version says a service's version and kind of machine.
Shown from the service, the app keeps no state of its own: each store the pages read says what it holds (MirroredStore in App/ServiceMirror.swift), the service answers GET /mirror/<name> with it, and the app holds one GET /feed request open for every part and adopts the ones that changed. Long histories (finished requests, agent replies) send only their last stretch. What the app changes (an Engine Proxy, a saved engine, a machine, the status endpoint's sharing) goes to the service's own routes, the ones the KDE app uses. Every page is shown this way, the Machine Room and its replay too; the app still reads other machines itself.
A service started by hand only prints its alerts; the copy inside Tokenmeter.app posts them to Notification Center.
To share the app without Gatekeeper warnings, sign and notarize it with a Developer ID Application certificate:
SIGN_IDENTITY="Developer ID Application: Your Name (TEAMID)" NOTARY_PROFILE=kapitsa-notary scripts/package.shThe header of scripts/package.sh explains the one-time notary setup.
Package.swift builds the tokenmeter service from the shared files in App/ plus Linux/*.swift. A file in App/ joins the Linux build when it is listed in Package.swift.
swift build -c release # .build/release/tokenmeter
scripts/package-linux.sh # dist/Tokenmeter-<version>-linux-<arch>.tar.gz
scripts/package-flatpak.sh # then dist/Tokenmeter-<version>-linux-<arch>.flatpak (needs flatpak-builder)No Swift on your machine? Build in Docker:
docker run --rm -v "$PWD":/src -w /src swift:6.2-noble swift build -c releaseTo work on the QML pages without a running service, use the sample answers in Linux/kde/mock:
cd Linux/kde
QML_XHR_ALLOW_FILE_READ=1 qml6 app/main.qml -- --mock "file://$PWD/mock" --page usagescripts/test.sh # on a Mac: builds the parsers and rules with the checks in Tests/ and runs them
scripts/test-linux.sh # the same on Linux, with the service's files
scripts/check.sh # everything the build workflow checks on Linux, in a containerscripts/check.sh runs on your own machine what the build workflow runs on GitHub's, so a pull request can be checked while GitHub Actions is off. It works in a container (Docker or Podman, on Linux or a Mac), so it needs no Swift here, uses none of this machine's ports and leaves a Tokenmeter that runs here alone. The first run builds the container's image, a few minutes; if Docker Hub refuses, name a mirror: CHECK_BASE=mirror.gcr.io/library/swift:6.2-noble scripts/check.sh. The service is built into a volume named tokenmeter-check-build, so later runs compile only what changed; docker volume rm tokenmeter-check-build starts it over.
scripts/check.sh # everything below but the slow extras
scripts/check.sh --quick # syntax, embedded files, Tests/, the build and four service tests
scripts/check.sh --only wire,mesh # only these service tests (the build still runs)
scripts/check.sh --all # also the idle budget (wants a quiet machine) and the KDE app's drills and Gizmo
scripts/check.sh --list # the names --only takesIn order: every Swift file is parsed, the Mac-only ones too, which catches a missing bracket in a view before it reaches a Mac; the embedded files (agent reporters, web dashboard, drills) are compared with their sources; the agents' own tests; the checks in Tests/; the service built as a release ships it; every script in Linux/tests against that build; and the KDE app's pages standing still. It ends with one line per step and writes the same to .build-check/report.txt, which goes into the pull request. Each step's output is in .build-check/logs/.
It does not compile the Mac app (that needs a Mac: ./build-library-app.sh --fast), build the Flatpak or the Android app, or walk the web dashboard in a browser.
./build-library-app.sh compiles every file of App/ together, optimised, which is what a release needs and takes minutes. ./build-library-app.sh --fast is for seeing whether a change compiles and how it looks: nothing is optimised, each file is compiled by itself on every core, and the objects are kept in build/fast, so the next --fast build compiles only the files that changed and the ones that use what changed in them. The service inside is the package's debug build, which is kept the same way. The app it makes runs but is slower than the real one, so build without --fast for the app you use and for anything you measure.
CI (.github/workflows/build.yml) compiles the Mac app, runs the tests, builds the Linux download and Flatpak, and runs the service against a stand-in llama.cpp server (Linux/tests/fake_llama.py). Linux/tests/gpu.sh checks the GPU readings against made-up computers (including a DGX Spark's GB10, whose memory is shared), and Linux/tests/fake_spark_engines.py stands in for SGLang and TensorRT-LLM. Linux/tests/machines.sh checks sharing the endpoint and watching another machine, with a second service standing in for it; it runs by hand against a service on port 7733 too. Linux/tests/history-store.sh starts a service on a month of history as versions before 0.9.0 kept it and checks the move into the database, the same answers after a restart, and that a later version's database is refused. Linux/tests/wire.sh checks the local server itself: answers compressed for readers that take gzip, many questions on one connection, a quiet connection closed after 20 s, the dashboard's files kept by their tag, and the service still answering with more connections open than it keeps. Linux/tests/storage.sh checks Settings' Storage card and tokenmeter storage the same way: every kind with its size and days, the days kept changed, the exports, and the whole history copied and taken by a second, empty home. Linux/tests/idle-frames.sh opens the KDE app's pages with sample data on a display of its own (Xvfb) and fails when a page that stands still keeps drawing frames. Linux/tests/history-line.sh fills two weeks of agent reports and fails when /status waits a second while the Sessions answers are worked out (since 0.8.102 they are worked out on a line of their own, HistoryWork, from what the main line hands over; anything they must ask the main line goes through HistoryWork.onMain). Linux/tests/idle-budget.sh runs a service with nothing to watch for a minute and fails when it goes over its budget of processor time, wake-ups, open files or memory, so a leak fails there before a release.
scripts/package-linux-arm64.sh builds Tokenmeter-<version>-linux-aarch64.tar.gz and the aarch64 service package (for a DGX Spark or another Arm machine) on an ordinary x86_64 Linux PC. It runs scripts/package-linux.sh in Swift's arm64 Docker image, which QEMU's user-mode emulation runs on the PC: no virtual machine to set up, but 5 to 10 times slower than on Arm (most of an hour the first time; the build is kept in .build-arm64/, so later runs compile only what changed). The files land in dist/ beside the x86_64 ones, where scripts/release.sh finds them.
It compiles the files side by side on every core (-Xswiftc -no-whole-module-optimization, passed to the package scripts in SWIFT_BUILD_FLAGS). The service is one Swift module, and the usual release build optimizes it in one pass that runs mostly on one core: natively on 4 cores that was 444 s at 1.3 cores against 215 s at 3.5 for the parallel build, and under QEMU 96 min against 53. The parallel build gives up some optimizations across files. --whole-module builds the usual way, and --jobs N caps the compiles running at once.
It needs Docker and QEMU's arm64 emulation, and says which is missing. On Arch Linux, once:
sudo pacman -S --needed docker qemu-user-static qemu-user-static-binfmt
sudo systemctl restart systemd-binfmt
sudo systemctl enable --now docker
sudo usermod -aG docker "$USER" # then log out and in
scripts/package-linux-arm64.sh --check # checks Docker and QEMU, builds nothing
scripts/package-linux-arm64.sh # or --service-only for just the service packageThe version is set in scripts/package.sh, scripts/package-linux.sh, scripts/package-service.sh and scripts/package-flatpak.sh, or by APP_VERSION when you run them.
By hand, from packages you built (uses no GitHub Actions minutes). Build the packages on the machines that make them, each into its dist folder:
scripts/package.sh # on a Mac: the DMG and the Mac service package
scripts/package-linux.sh # on Linux: the tarball and the Linux service package
scripts/package-linux-arm64.sh # on an x86_64 Linux PC: the same two for Arm (aarch64, DGX Spark), under QEMU
scripts/package-flatpak.sh # on Linux: the Flatpak
Android/build.sh # with the Android SDK: the Android app, signed with your kept keyThen scripts/release.sh makes the tag and the release on GitHub and uploads the files. It builds nothing:
scripts/release.sh --dry-run dist # what it would do
scripts/release.sh dist # tag v<version>, publish, upload
scripts/release.sh ~/mac-dist ~/linux-dist # packages gathered from several machinesIt takes the version from the file names, checks each service package against its .sha256 (and makes the checksum file where one is missing), lists what a whole release would also carry (the Android app among them, Tokenmeter-<version>-android.apk), and asks before it publishes. Run it again from another machine to add that machine's packages to the same release: files the release already has are left alone (--replace uploads them again). --draft leaves the release for you to look at first; a draft is offered to nobody. The tag points at the commit checked out where you run it (--commit names another), which must be pushed. It needs GitHub's gh tool, signed in once with gh auth login. scripts/release.sh --help lists the rest.
By a tag, when GitHub Actions is available: pushing a tag such as v0.7.0 runs the release workflow, which tests, builds that version, and publishes a GitHub release with an ad-hoc signed DMG, the Linux tarballs (x86_64, and aarch64 built under QEMU) and the Flatpak. It leaves out the Android app, which would be signed with a key made for that run and so couldn't update the app on a phone: add it with scripts/release.sh dist after Android/build.sh. One or the other for a version: a release made by release.sh makes the tag too, which starts this workflow where Actions runs; it then stops at its publish step because the release is already there, and changes nothing.
The release also carries the service package for each platform, tokenmeter-service-<version>-<platform>.tar.gz, with a .sha256 beside it: that pair is what the machines of a mesh look for when they update. To try updates without a release, build a package with a later APP_VERSION (APP_VERSION=0.8.3 scripts/package-service.sh, after setting the same version in App/StatusJSON.swift) and put it in ~/.tokenmeter/updates of one machine. TOKENMETER_RELEASES_URL points a service at another address than GitHub's for the latest release (a file served locally, in GitHub's format).
The images in docs/images are shown in the README and this wiki. Mac screenshots are always taken in the dark theme. The Linux ones are the KDE app rendered from the sample answers in Linux/kde/mock with QT_QUICK_CONTROLS_STYLE=RoomStyle (see Linux/README.md); the window always wears the Machine Room's colours, so no dark colour scheme is needed.
Engines plug in through one protocol in App/Engine.swift:
@MainActor
protocol InferenceEngine: AnyObject {
var id: String { get } // "llamacpp"
var displayName: String { get } // "llama.cpp"
func poll() async // refresh; called every two seconds
var snapshots: [ServerSnapshot] { get }
}ServerSnapshot is the engine-neutral picture of one server (status, port, model, live and total tokens, requests, slots, memory, context, cache). Fill in what the engine reports and leave the rest nil.
Most engines speak HTTP and only need to subclass HTTPEngine (App/HTTPEngine.swift), which handles finding servers, saved servers, polling, rates and Usage, and remembers which default ports can't be its server now (PortMemory: a port another engine's server answers on is asked again after a minute, one nothing listens on every half minute); the subclass reads the server's endpoints into an EngineReading. App/LlamaCppEngine.swift and App/SplashEngine.swift are short examples. Then:
- Add a case to
EngineKind(App/EngineConfig.swift) so it can be added under Engines. - Register the engine in
EngineMonitor(App/EngineMonitor.swift). - For Linux, list the new file in
Package.swift. - An engine that does not subclass
HTTPEnginecallsRequestLogStore.shared.addnext toUsageStore.shared.record, so its finished requests reach the Performance page.