shahondin1624 f1ceeb4363 Refactor pass: shared utils, non-blocking discovery, safer monkey-patches
Audit produced five concrete improvements:

1) New shared/ module (zero-dep pure utilities)
   - shared/ansi.ts: hexToRgb (throws on malformed input instead of
     silently producing NaN), fgFromHex, stripAnsi, visibleWidth,
     ANSI_RESET_FG / ANSI_RESET_ALL constants.
   - shared/format.ts: formatTokens, formatElapsed.
   - shared/ctx.ts: safely() and safelyAsync() helpers for dealing with
     pi's "stale after session replacement or reload" ExtensionRunner
     semantics.

   Removes duplicate helpers from mechanicus-footer, markdown-body-color,
   dark-mechanicus-indicator.

2) ai-server: non-blocking startup + short-race timeout
   - Factory registers STATIC_MODELS immediately so pi startup isn't
     blocked on the HTTPS round-trip.
   - Races discoverModels() against a 300ms timeout. On LAN (~40ms) the
     live list wins and pi --list-models sees the real models. Past the
     timeout, fallback remains and background discovery updates the
     provider later.
   - listModelsCached() with 5s TTL for tab completions (was firing a
     round-trip on every keystroke).
   - loadModel/unloadModel invalidate the cache.

3) dark-mechanicus-indicator: stale-ctx guard
   - Wrap the setInterval ticker body in safely() so a race between
     session_shutdown and the ticker can't crash node. Same pattern as
     the earlier footer fix.

4) Safer monkey-patches in markdown-body-color and mechanicus-thinking-label
   - Feature-detect Markdown/Editor/AssistantMessageComponent's target
     method before patching. Warn-and-skip rather than silently create
     a broken prototype if a pi-tui upgrade renames the internal method.

5) Minor
   - Replaced five `as any` casts with typed Record<string, unknown>
     access in the monkey-patch sites.
   - ai-server debug log only fires when actual discovery succeeds.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-24 00:05:18 +02:00
2026-04-23 23:45:47 +02:00

pi-extensions

Personal collection of extensions, themes, and scripts for pi — Mario Zechner's CLI coding agent.

The anchor of the repo is ai-server, a multi-file pi extension that lets pi talk to a self-hosted llama.cpp router behind mTLS. Everything else (theme, banner, custom footer, working indicator, session-name generator, etc.) is Warhammer 40k "Dark Mechanicum" flavoring on top of pi's interactive TUI.

Installed extensions

Extension What it does
ai-server/ Remote llama.cpp provider over mTLS. Dynamic model discovery. Admin slash commands (load / unload / ctx / preset / restart / refresh). Custom SSE stream implementation with tool calls, reasoning, cache token reporting. See ai-server/README.md for the full setup.
local-llama.ts Tiny provider registration for a local llama-server (e.g. 127.0.0.1:8088).
dark-mechanicus-indicator.ts Replaces pi's default working-indicator with ⚙ <quote> · <elapsed>. ⚙ pulses gold-dim → normal → bright → normal (1s cycle); one of 45 Dark-Mechanicum quotes rotates every 10s; elapsed time ticks every second.
mechanicus-footer.ts Replaces pi's default footer. Context shown as 45k/131k (34%) instead of 34%/131k. Adds compaction counter (C2) and last-turn tok/s.
mechanicus-status-line.ts Third footer line: ⚙ HERETEK FORGE · ACTIVE / SCRAPCODE CIRCUITRY · OPERATIONAL / etc. Rotates each turn.
mechanicus-banner.ts Custom TUI header — cog-and-skull art (LEX HERETEK, SCRAPCODE TRIUMPHS, BLOOD-FORGE ETERNAL) with text block on the left and ASCII cog on the right.
mechanicus-session-names.ts Auto-names new sessions with a random <adjective>-<noun> · <NNN> (e.g. Heretek-Blood-Forge · 042). Mirrors the name into the terminal tab title.
mechanicus-thinking-label.ts Swaps pi's Thinking... fold-label for Cogitating....
markdown-body-color.ts Monkey-patches pi-tui's Markdown + Editor components so unstyled paragraph text and editor typing text don't inherit the terminal profile's default foreground (often green). Forces a lavender inkPurple body color.

Theme

File Palette
themes/dark-mechanicus.json Dark purple aubergine background (#16101e), AdMech blood-red accents (#a8232c), burnished brass borders (#b8803d), nurgle-green syntax strings (#5a6b2e), cognitor-pink syntax types (#c75a8a). All 51 color tokens defined.

Activate with /settings inside pi (or set "theme": "dark-mechanicus" in ~/.pi/agent/settings.json). Pi hot-reloads on edits to the active theme file.

Setup scripts

Script Runs on Purpose
scripts/install-client.sh New pi client Bootstraps a client: fetches certs from the Caddy host, rsyncs extensions + themes into ~/.pi/agent/, verifies the mTLS /health endpoint.
scripts/issue-client-cert.sh Caddy host Generates a new client identity (key + cert + modern + legacy p12) for a named device. Use for each new machine.
scripts/install-browser-certs.sh New client Imports client cert + root CA into Brave Flatpak NSS, system NSS, Firefox profiles, and optionally the system trust store. Mostly obsolete since the Caddy switch to Let's Encrypt but still useful for the mTLS client cert side.

Tests

Twenty-eight tests total, no external dependencies. Runs with Node 22+'s built-in test runner + --experimental-strip-types:

node --experimental-strip-types --test tests/*.test.ts
File Coverage
tests/messages.test.ts 13 unit tests over ai-server/messages.ts — pi Context → OpenAI payload conversion (system prompts, user/assistant/tool-result roles, tool calls, image-only messages).
tests/router-utils.test.ts 9 unit tests over ai-server/router-utils.tsextractCtxSize, isShardArtefact (the filter that hides GGUF multi-shard phantoms from the model picker).
tests/integration.test.ts 6 live-endpoint tests: /health, /models, model-entry shape, mTLS enforcement, publicly-trusted cert (Let's Encrypt contract), chat completion usage shape including prompt_tokens_details.cached_tokens. Auto-skip if the server is unreachable.

Stream-parsing edge cases (SSE framing, tool-call splits across chunks, reasoning deltas, abort mid-stream) remain deferred — they need a mock HTTPS server harness, not worth the complexity for a one-user setup.

Quick onboarding for another machine

# 1. Mint a new client identity (on the Caddy host)
ssh shahondin1624@192.168.2.2 '~/pi-extensions/scripts/issue-client-cert.sh laptop-alice'

# 2. On the new pi client
git clone https://git.shahondin1624.de/shahondin1624/pi-extensions.git ~/pi-extensions
cd ~/pi-extensions
scripts/install-client.sh

# 3. Activate the theme
#    /settings in pi, pick "dark-mechanicus"

Layout

pi-extensions/
├── ai-server/                  the core mTLS provider (multi-file extension)
│   ├── index.ts                entry — provider + admin commands
│   ├── config.ts               URLs, SSH host, cert loading, MODELS[]
│   ├── messages.ts             Context → OpenAI messages
│   ├── stream.ts               custom SSE stream, mTLS HTTPS, pi-ai events
│   ├── admin.ts                router HTTP client + SSH helpers
│   ├── router-utils.ts         pure helpers (test-friendly)
│   └── README.md               full mTLS + systemd + Caddy setup notes
├── dark-mechanicus-indicator.ts  working indicator (cog + quote + timer)
├── mechanicus-footer.ts        custom footer layout
├── mechanicus-status-line.ts   rotating flavor status line
├── mechanicus-banner.ts        TUI header art
├── mechanicus-session-names.ts auto-name generator + tab title
├── mechanicus-thinking-label.ts "Cogitating..." for folded thinking blocks
├── markdown-body-color.ts      forces body text color for chat + editor
├── local-llama.ts              local llama-server provider
├── themes/
│   └── dark-mechanicus.json    51-token theme
├── scripts/                    install helpers
├── tests/                      node:test suites
└── README.md                   this file

License

Personal use. No license declared; the repo is private.

S
Description
Personal pi (pi-coding-agent) extensions: ai-server (mTLS remote provider + admin commands) and local-llama.
Readme 490 KiB
Languages
TypeScript 79.3%
JavaScript 11.3%
Shell 9.4%