aiellie

Search

Find a component, template or page

GitHub
Components

Plugin Selector

A small button that shows which plugins a chat can use, with their marks and a count, and opens a menu to turn them on and off. Its rows also come alone, for a submenu of your own, and so do chips, one per plugin turned on, whose mark turns into a remove button on hover.

Open full screen
fig. 01 · open it full screen from the corner

Installation

npx shadcn@latest add "@aiellie/plugin-selector"

shadcn needs the @aiellie registry in components.json first. npx aiellie add plugin-selector adds it on its first run, then installs the same way.

Plugin selector turns on the tools a reply may use, like GitHub or Slack. Several can be on at once: the trigger shows up to three of their marks and a count, and the menu lists them all with a tick each. It comes in three parts, so the same list can go in a menu of its own or show as removable chips in a composer.

Getting started

  1. List the plugins

    Each PluginOption needs an id, a name and a Hugeicons glyph. value holds the ids turned on.

    components/composer-plugins.tsx
    "use client"
    
    import * as React from "react"
    import { Github01Icon, SlackIcon } from "@hugeicons/core-free-icons"
    
    import {
      PluginSelector,
      type PluginOption,
    } from "@/components/aiellie/plugin-selector"
    
    const PLUGINS: PluginOption[] = [
      { id: "github", name: "GitHub", icon: Github01Icon },
      { id: "slack", name: "Slack", icon: SlackIcon },
    ]
    
    export function ComposerPlugins() {
      const [plugins, setPlugins] = React.useState<string[]>([])
    
      return (
        <PluginSelector
          plugins={PLUGINS}
          value={plugins}
          onValueChange={setPlugins}
        />
      )
    }
  2. Give the model those tools

    Send the ids with the message and pick the matching tools on the server.

    app/api/chat/route.ts
    const { messages, plugins } = await req.json()
    
    const result = streamText({
      model,
      messages: await convertToModelMessages(messages),
      tools: Object.fromEntries(
        Object.entries(TOOLS).filter(([id]) => plugins.includes(id))
      ),
    })

Anatomy

<button data-slot="plugin-selector"> {/* render, an extra-small outline Button by default */}
  <svg /> {/* up to three marks of plugins turned on, or a puzzle piece */}
  <span class="sr-only">{label}: GitHub, Slack</span>
  <span aria-hidden>2 plugins</span> {/* the one name, a count, or the label */}
  <svg /> {/* a chevron */}
</button>

<div data-slot="menu-content">
  <div data-slot="menu-group">
    <div data-slot="menu-group-label" /> {/* Plugins */}
    <div data-slot="menu-checkbox-item" /> {/* PluginSelectorItems, one per plugin */}
  </div>
  <div data-slot="menu-item" /> {/* Turn off all plugins, once any are on */}
</div>

<div data-slot="plugin-chips">
  <span data-slot="plugin-chip" /> {/* mark, which becomes a remove button on hover, and name */}
</div>

value always comes back in the order of plugins, whatever order they were ticked in. The menu stays open while ticking, so several can be turned on in one go. A chip's mark turns into a cross on hover or keyboard focus, so the chip removes itself without a second button.

Examples

Chips in the composer

PluginChips shows what's on, each with its own remove button, beside or under the message.

GitHubGoogle Drive

In a submenu

PluginSelectorItems is just the rows, for a menu of your own, like a Plugins submenu of the Add menu.

API reference

PluginSelector

PropTypeDefaultDescription
pluginsPluginOption[]requiredEvery plugin, in the order to list them.
valuestring[]requiredThe ids of the plugins turned on.
onValueChange(value: string[]) => voidrequiredCalled with the new ids, in the order of plugins.
labelstring"Plugins"What is being chosen, read out ahead of the names, and shown while none are on.
renderReactElement<Button variant="outline" size="xs" />The trigger's element.
side / alignMenuContent placementWhere the menu opens.

PluginSelectorItems and PluginChips

Both take plugins, value and onValueChange, the same as PluginSelector.

ExportRendersNotes
PluginSelectorItemsMenuCheckboxItemsThe rows alone, for a menu of your own.
PluginChipsdivOne chip per plugin turned on, or nothing when none are. Takes className.

PluginOption

FieldTypeDescription
idstringWhat value holds while it is on.
namestringShown in its row, its chip and the trigger.
iconIconSvgElementA Hugeicons glyph.