Extending the UI

Slots, commands and themes, and how a reviewed Git commit becomes part of the build.

intermediate9 min read

Open a new chat, click Starters beside Runtime, and pick a prompt. It appears in the message box, ready for whatever you want to add. That small button came from a module outside App.svelte. Its path into the composer shows where Pipal makes room for your own interface code.

In this chapter

  • Choose between a theme, a workspace contribution, and a replacement client.
  • Follow a bundled command and component to the draft they edit.
  • Trace a reviewed Git commit into the web build and see what a browser switch does.
  • Identify the trust you place in an installed extension.

Pick the part of the workspace you want to change

Three ways to change the workspace
FeatureTheme UI extension Custom client
You want toChange colors, typography or layoutAdd a command, control or search resultOwn the navigation, transcript or composer
You provideCSS for light or darkA module and any Svelte componentsA web app that calls the Pipal API
Pipal providesThe Appearance picker and workspace markupSlot components, slash commands, quick switcher and draft contextHTTP routes and the conversation stream

Choose up to two columns

Choose up to two columns

FeatureTheme UI extension Custom client
You want toChange colors, typography or layoutAdd a command, control or search resultOwn the navigation, transcript or composer
You provideCSS for light or darkA module and any Svelte componentsA web app that calls the Pipal API
Pipal providesThe Appearance picker and workspace markupSlot components, slash commands, quick switcher and draft contextHTTP routes and the conversation stream
Start with the change you want to make. Each option gives you control over a different part of the browser experience.

If the workspace already does what you want, a theme can change how it looks. For a new action, a UI extension can register a command or put a component in a UI slotA named place in the workspace where Pipal can render a Svelte component supplied by an extension., a named place in the existing screen. The extension interface adds to the workspace at these hooks. To replace the composer or transcript, edit the workspace or build a client against the server’s HTTP routes. The server supplies callable tools such as read_url, which Developer can use when the operator enables pipal.public-web, as Tools and extensions shows. ui-extensions.ts public-web.ts

The slots follow the parts of the screen you already know. sidebar goes above the navigation footer, headerActions goes before Appearance, and newConversation appears below the welcome heading. App.svelte

appearance puts content inside the Appearance picker, and composerActions sits in the toolbar beside Runtime. AppearancePicker.svelte App.svelte

A starter is an edit you can review before sending

The prompt-starters module
export default {
  id: 'prompt-starters',
  composerActions: Starters,
  commands: [{
    command: '/starter',
    label: 'Insert a prompt starter',
    available: context =>
      context.canEditDraft &&
      !context.conversationId,
    run(context) {
      const [, prompt] =
        starters(context.profile)[0];
      insertStarter(context, prompt);
    }
  }]
} satisfies UiExtension;
packages/web/src/extensions/prompt-starters/index.ts
  1. Registration ID

    The loader expects the exported ID to equal the name under which it found the module.

  2. Toolbar component

    App mounts Starters beside Runtime. The component uses the selected profile to choose which prompts to offer.

  3. New chat only

    The command is offered when the draft is editable and there is no selected conversation.

  4. Fill the draft

    The command takes the first starter for the current profile. The toolbar can choose among all the starters. Both go through insertStarter.

export default {
  ,
  ,
  commands: [{
    command: '/starter',
    label: 'Insert a prompt starter',
    

,
    




  }]
} satisfies UiExtension;
packages/web/src/extensions/prompt-starters/index.ts

1 Registration ID

The loader expects the exported ID to equal the name under which it found the module.

The bundled index.ts declares both entry points. Select a marked line to see what the workspace does with it.

The bundled prompt-starters module registers the toolbar button and the /starter command. In a new chat, the button lets you pick a prompt for the current profile. Profiles without their own list get Chat’s prompts. The command inserts the first prompt from the same list. Both call insertStarter(), which replaces a starter prefix if there already is one and keeps the text after it. That lets you try another starter without retyping your own words. index.ts prompts.ts

A command has a slash name, a label you can search for, and a run(context) function. Add an available(context) check when the action only makes sense in certain chats. An extension can also provide search(query, context). The quick switcher calls it for a nonempty query that begins with something other than a slash. Results can supply their own labels and actions. ui-extensions.ts CommandSwitcher.svelte

Deep diveA rejected module leaves its command names available

loadUiExtensions() sorts the enabled IDs and starts the imports together. It walks the settled results in that sorted order. The default export’s ID has to match its registered name. IDs start with a lowercase letter and can continue with lowercase letters, digits, dots, underscores, or hyphens. Slash names use the same initial letter after the slash, with lowercase letters, digits, underscores, or hyphens to follow. The loader also checks that slot fields and search, when present, are functions. load-ui-extensions.ts

Suppose two modules both claim /checklist. The first valid one in ID order gets the name. The second module fails as a unit, including any valid slot it also offered. Names are committed only after every command in the module passes validation, so a broken module claims none. Built-in slash names are reserved before any imports register. An import or validation failure appears under Browser extension errors in Settings → Extensions. The registry test makes this concrete. One module lists an available name and then collides with a built-in name. A later valid module still gets the first name. App.svelte load-ui-extensions.ts Settings.svelte load-ui-extensions.test.ts

Once a command is registered, a throwing availability check hides that command and logs the error. If a search hook fails, App logs the error and omits that hook’s results. App’s setDraft() also checks that the chat has stayed the same and the draft is still editable. If an asynchronous command finishes after you switch chats, its old context can’t use that function to write into the new draft. ui-extensions.ts App.svelte

The build follows the commit you select

From reviewed commit to browser
  • Git objects
  • Installer
  • Vite
  • Hono app.ts
  • Browser
  1. Installer → Git objects · Verify ID

    The installer resolves the full commit ID

    The supplied revision must be the full hexadecimal ID of a commit in the local repository. A branch or abbreviated ID is rejected.

  2. Git objects → Installer · Read blobs

    Git supplies the committed file bytes

    The installer copies every committed regular file into a staging directory, including assets and any host files. It rejects symlinks and submodules.

  3. Installer → Installer · Publish

    The installer places the revision in the web source tree

    The destination is packages/web/src/.pipal-ui/<id>-<commit>/. Older snapshots stay there after a newer commit goes active.

  4. Installer → Installer · Activate

    One file selects the active revision

    .pipal-ui-active/<id>.js records where the revision came from and re-exports its entry module.

  5. Vite → Vite · Find

    The build follows active pointers

    The Vite glob finds the active external modules. The theme plugin reads the pointer records to find their palettes. The bundled starter module is added separately.

  6. Browser → Hono · Reload

    The browser requests the rebuilt page

    A user reloads after Vite has written the new build to disk.

  7. Hono → Browser · Built files

    Hono serves the new build

    Hono serves the generated page and assets from the build directory.

  8. Browser → Browser · Filter

    The page restores its browser preferences

    The head script removes disabled extensions’ palette entries and stylesheet links. App uses the same list to skip module imports.

  9. Browser → Browser · Register

    The registry accepts the module’s contributions

    The browser starts enabled imports together. Once they settle, the loader validates their contributions in ID order.

Git objects
Installer
Vite
Hono app.ts
Browser

Installer → Git objects

The installer resolves the full commit ID

The supplied revision must be the full hexadecimal ID of a commit in the local repository. A branch or abbreviated ID is rejected.

Trace an extension from its Git objects to the browser. The pointer selects the revision at build time, and the browser applies its own switches at page load.

Start with a local Git checkout for the extension, with one index.ts or index.js at its root. From Pipal’s checkout, pass the ID the module exports, the extension repository root, and the full commit ID you reviewed. For example, with a reviewed checklist extension next door, the two commands are as follows. Replace the placeholder with the actual commit ID. ui-extension.mjs

Terminal window
pnpm ui-extension enable checklist ../pipal-checklist '<full-commit>'
pnpm build

The build command above uses the default mount path. For a deployment already at /ai/, use pnpm --filter @pipal/web build --base=/ai/ to keep that path. package.json vite.config.ts

The installer copies every regular file in the Git tree, including files the browser entry point never imports, into packages/web/src/.pipal-ui/<id>-<commit>/. The pointer at packages/web/src/.pipal-ui-active/<id>.js selects that snapshot. Editing the original checkout or moving a branch leaves the pointer and its copied files in place. Reload after the build finishes to see the extension. ui-extension.mjs

Deep diveHow the installer publishes files and rolls back to an earlier revision

The installer accepts a 40- or 64-digit hexadecimal ID and verifies that it names the exact commit. Git runs with replacement objects and lazy fetching disabled and with an empty allowed-protocol list. The recursive ls-tree lists the whole tree. The installer accepts regular-file entries with Git mode 100644 or 100755 and creates each destination file with requested mode 0444, so the executable bit is dropped. cat-file blob streams the exact bytes. A symlink, submodule, unsupported path, or missing or ambiguous root entry point fails the export before activation. ui-extension.mjs

The tree goes into a staging directory first. The installer publishes it at <id>-<commit> and reuses the directory if that snapshot already exists. It then renames the pointer into place. The pointer exports the source and commit as provenance and re-exports the snapshot module’s default export. The installedModules() glob discovers these external modules for lazy import. pipal-watch-extension-approvals registers the pointer directory as an input to watch when Vite transforms the loader. Updating or rolling back means enabling the chosen full commit, rebuilding, and reloading. ui-extension.mjs load-ui-extensions.ts vite.config.ts

Your browser can skip an installed extension

In Settings → Extensions, uncheck an ID under Browser extensions and choose Reload to apply. Turning off an extension writes 1 to the local storage key pipal.disabledExtension:<base>:<id>. Turning it back on removes the key. The base path lets two deployments on the same origin have separate switches. The next page load applies the preference to imports and palette choices. Settings.svelte extension-preferences.ts

The page picks its palette before Svelte starts

Open Appearance to choose a paletteA named stylesheet the workspace offers as a choice for its light or dark color scheme. for light or dark whenever more than one is available for that scheme. AppearancePicker.svelte

The built-in files live in packages/web/src/themes/light/ and packages/web/src/themes/dark/. The build looks for the same folders under an active extension’s themes/ directory. The filename supplies the palette ID. Write selectors that match data-scheme and data-palette on the page root, as the built-in palettes do. The plugin leaves selector scoping to the stylesheet. vite.config.ts pipal.css

The pipal-themes Vite plugin discovers the CSS when you build and writes the available palettes into the page as metadata. It also adds appearance-init.js as a script from the workspace’s own origin. The blocking script in the head restores your choice and sets the root attributes before Svelte starts. Loading it from the workspace’s origin satisfies the page’s Content Security Policy for scripts. That lets a saved dark palette appear on the opening screen. vite.config.ts index.html

Deep diveA single page-load decision applies to CSS and JavaScript

The plugin generates a stylesheet per owner with palettes, whether Pipal itself or an extension. It requires lowercase kebab-case filenames and rejects duplicate IDs within a scheme at build time, even when a browser would disable one of the owners. It uses an opening Theme: comment for the display label when present. A /* Preferred theme */ line marks a default candidate. appearance-init.js takes the first preferred palette still in the catalog for a scheme and otherwise falls back to pipal when the saved choice is missing or invalid. vite.config.ts appearance-init.js

The generated head contains preload links and initially disabled stylesheets for extension palettes. The preload can fetch the CSS even if the extension is disabled in this browser. The initializer reads the stored switches, removes palette metadata and stylesheet tags for disabled IDs, enables the other extension stylesheets, and records the disabled IDs on the document root. App.svelte reads that list before it calls the module loader. Palette filtering depends on the saved switches, so a module that fails JavaScript registration can still have a palette in the picker. The picker reads the surviving metadata through palettesFor() and listens for appearance updates through the store in appearance.ts. vite.config.ts appearance-init.js App.svelte load-ui-extensions.ts appearance.ts

A replacement client keeps the server API

If you want a different transcript or navigation model, you can edit the stock workspace or build a client with your own rendering. Hono can serve a replacement index.html and assets from packages/web/build/. Include pipal-base-path.txt with the mount path, including the trailing slash. The server reads and validates it at startup. Keep your source outside this generated directory. main.ts app.ts

For a first round trip, sign in and create or select a conversation through the HTTP API. Serve the client on the configured origin so requests carry the session cookie and writes have the origin Hono expects. app.ts

Open an EventSource at /api/v1/conversations/:id/events with no query parameters and listen for snapshot. Read the event’s JSON snapshot field for the current conversation view. The envelope also carries version: 1. Close the stream when you switch chats. Post {text, requestId} to /api/v1/conversations/:id/messages to send a message, then render its reply from the stream. Give a new submission a fresh request ID and reuse it with the same payload on retry. If the deployment has a base path, add it to both URLs. app.ts

A button can fill the draft before you ever send it. Once you do send it, where do the chosen profile’s instructions and default model come from? Profiles and models opens the files that make those choices.

Recap

  • A UI extension contributes browser actions, search, and components to places the workspace already provides.
  • The installer pins a commit, the build follows active pointers, and browser switches apply at reload.
  • Themes change styles before Svelte starts. A replacement client can own the screen and still use Pipal’s HTTP API and event stream.
Check yourself
  1. You enable a reviewed commit, then edit the extension’s local checkout. What does a rebuild use?
    • Any files currently on disk in the extension checkout
    • The newest commit on its default branch
    • The copied Git files selected by the active pointer
    Answer and explanation

    The copied Git files selected by the active pointer

    Enable copies the reviewed commit and writes a pointer to its snapshot. Choosing a different revision requires changing that pointer through the installer.

  2. You switch off an installed extension in Settings and reload. Another browser still offers its command. Why?
    • The switch is local to your browser and the extension still ships in the build
    • The command must be deleted separately in Settings
    • Commands are unaffected by browser switches
    Answer and explanation

    The switch is local to your browser and the extension still ships in the build

    On reload, your browser skips the disabled module before import. Other browsers keep their own preferences.

  3. An extension imports successfully and passes the registry checks. What access does it have?
    • Only the fields passed in UiContext
    • The workspace page and the API access of the signed-in browser
    • Direct operating-system privileges of the Pipal server account
    Answer and explanation

    The workspace page and the API access of the signed-in browser

    Registration validates contributions. The module starts running at import and shares the workspace page’s access. The browser sends the session cookie on matching requests.

  1. You enable a reviewed commit, then edit the extension’s local checkout. What does a rebuild use?

  2. You switch off an installed extension in Settings and reload. Another browser still offers its command. Why?

  3. An extension imports successfully and passes the registry checks. What access does it have?

In the source

Book contents

Search the book