AI Tool Call
Tool calls made visible: an inline chip that unfolds into a result peek, a wire a packet travels between request and response, and a hub with lines drawing out to real services.
"use client"
import * as React from "react"
import { Check, RotateCcw, X } from "lucide-react"
import {
AiKitStyles,
AiStyle,
BrandIcon,
cx,
Face,
Pill,
Roll,
type AiMood,
type AiShape,
type AiStatus,
type AiTone,
} from "@/lib/ai-kit"
const CSS = `
.aik-tc-chip{position:relative;display:inline-block;vertical-align:baseline;font-size:inherit;line-height:inherit}
.aik-tc-chipb{display:inline-flex;align-items:center;gap:6px;height:26px;padding:0 8px 0 7px;border-radius:8px;vertical-align:-6px;background:var(--aik-raised);box-shadow:inset 0 0 0 1px var(--aik-line);font:12.5px var(--aik-mono);color:var(--aik-text);white-space:nowrap;cursor:default;transition:background var(--aik-d-state),box-shadow var(--aik-d-state),transform var(--aik-d-press) var(--aik-ease-out)}
.aik-tc-chip[data-peek] .aik-tc-chipb{cursor:pointer}
.aik-tc-chip[data-peek] .aik-tc-chipb:active{transform:scale(.97)}
.aik-tc-chipb > svg{color:var(--aik-text-3)}
.aik-tc-chipb em{font-style:normal;color:var(--aik-text-2);max-width:150px;overflow:hidden;text-overflow:ellipsis}
.aik-tc-chip[data-s="run"] .aik-tc-chipb{background:color-mix(in oklab,var(--aik-blue) 10%,transparent);box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--aik-blue) 38%,transparent)}
.aik-tc-chip[data-s="fail"] .aik-tc-chipb{background:color-mix(in oklab,var(--aik-red) 10%,transparent);box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--aik-red) 38%,transparent)}
.aik-tc-chip[data-open] .aik-tc-chipb{box-shadow:inset 0 0 0 1px var(--aik-line-3)}
.aik-tc-dot{position:relative;display:grid;place-items:center;width:14px;height:14px;flex:none;border-radius:50%;color:var(--aik-on-fill)}
.aik-tc-dot[data-s="run"]{border:1.75px solid color-mix(in oklab,var(--aik-blue) 22%,transparent);border-top-color:var(--aik-blue);animation:aik-spin .8s linear infinite}
.aik-tc-dot[data-s="done"]{background:var(--aik-green);animation:aik-swap var(--aik-d-panel) var(--aik-ease-out)}
.aik-tc-dot[data-s="fail"]{background:var(--aik-red);animation:aik-swap var(--aik-d-panel) var(--aik-ease-out)}
.aik-tc-dot[data-s="todo"]{border:1.5px dashed var(--aik-line-3)}
.aik-tc-peek{position:absolute;left:0;top:calc(100% + 8px);z-index:5;display:grid;gap:6px;width:max-content;max-width:min(340px,80cqw);padding:12px 14px;border-radius:14px;background:var(--aik-card);box-shadow:var(--aik-sh-pop);font-family:var(--aik-sans);font-size:13px;line-height:19px;color:var(--aik-text-2);text-align:left;white-space:normal;
clip-path:inset(0 0 100% 0 round 14px);opacity:0;visibility:hidden;transition:clip-path 420ms var(--aik-ease-move),opacity var(--aik-d-state) var(--aik-ease-out),visibility 0s 420ms}
.aik-tc-chip[data-open] .aik-tc-peek{clip-path:inset(-40px -40px -60px -40px round 14px);opacity:1;visibility:visible;transition:clip-path 420ms var(--aik-ease-move),opacity var(--aik-d-state) var(--aik-ease-out)}
.aik-tc-peek > b{font-weight:600;font-size:12px;letter-spacing:.04em;text-transform:uppercase;color:var(--aik-text-3)}
.aik-tc-peek code{display:block;font:12.5px/19px var(--aik-mono);color:var(--aik-text);white-space:pre;overflow:hidden;text-overflow:ellipsis}
@media (prefers-reduced-motion: reduce){.aik-tc-peek{transition:none!important}.aik-tc-dot{animation:none!important}}
.aik-tc-wire{--aik-r-shell:26px;--aik-rim:5px;width:min(100%,560px);container-type:inline-size}
.aik-tc-wcard{overflow:hidden}
.aik-tc-whead{display:flex;align-items:center;gap:10px;height:52px;padding:0 10px 0 18px;border-bottom:1px solid var(--aik-line)}
.aik-tc-whead b{flex:1;min-width:0;font:500 14px var(--aik-mono);overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
.aik-tc-lat{font:12.5px var(--aik-mono);color:var(--aik-text-3);font-variant-numeric:tabular-nums;min-width:52px;text-align:right}
.aik-tc-ib{display:grid;place-items:center;width:34px;height:34px;border-radius:10px;color:var(--aik-text-2);transition:background var(--aik-d-state),color var(--aik-d-state),transform var(--aik-d-press) var(--aik-ease-out)}
.aik-tc-ib:hover{background:var(--aik-hover);color:var(--aik-text)}
.aik-tc-ib:active{transform:scale(.94)}
.aik-tc-lane{display:grid;grid-template-columns:auto minmax(0,1fr) auto;align-items:center;gap:14px;padding:26px 30px 22px}
.aik-tc-end{display:grid;justify-items:center;gap:8px;font-size:12.5px;color:var(--aik-text-2)}
.aik-tc-tile{position:relative;display:grid;place-items:center;width:60px;height:60px;border-radius:18px;background:var(--aik-raised);box-shadow:inset 0 0 0 1px var(--aik-line);color:var(--aik-text);transition:box-shadow var(--aik-d-panel) var(--aik-ease-out)}
.aik-tc-wire[data-p="done"] .aik-tc-tile.svc{box-shadow:inset 0 0 0 1.5px var(--aik-green)}
.aik-tc-wire[data-p="fail"] .aik-tc-tile.svc{box-shadow:inset 0 0 0 1.5px var(--aik-red)}
.aik-tc-w{position:relative;height:60px;align-self:start;container-type:inline-size}
.aik-tc-w::before,.aik-tc-w > i{content:"";position:absolute;left:0;right:0;top:29px;height:2px;border-radius:1px;background:var(--aik-line-2)}
.aik-tc-w > i{background:var(--aik-blue);transform-origin:0 50%;transform:scaleX(var(--fill,0));transition:transform 640ms var(--aik-ease-move),background var(--aik-d-panel)}
.aik-tc-wire[data-p="receiving"] .aik-tc-w > i,.aik-tc-wire[data-p="done"] .aik-tc-w > i{background:var(--aik-green)}
.aik-tc-wire[data-p="fail"] .aik-tc-w > i{background:var(--aik-red)}
.aik-tc-pkt{position:absolute;top:22px;left:0;width:16px;height:16px;border-radius:6px;background:var(--aik-blue);box-shadow:0 0 0 4px color-mix(in oklab,var(--aik-blue) 18%,transparent),0 0 14px 2px color-mix(in oklab,var(--aik-blue) 50%,transparent);transform:translateX(calc(var(--at,0)*(100cqw - 16px)));opacity:var(--show,0);transition:transform 640ms var(--aik-ease-move),opacity var(--aik-d-state),background var(--aik-d-panel),box-shadow var(--aik-d-panel)}
.aik-tc-wire[data-p="receiving"] .aik-tc-pkt{background:var(--aik-green);box-shadow:0 0 0 4px color-mix(in oklab,var(--aik-green) 18%,transparent),0 0 14px 2px color-mix(in oklab,var(--aik-green) 50%,transparent)}
.aik-tc-w > span{position:absolute;left:0;right:0;top:2px;text-align:center;font:11.5px var(--aik-mono);color:var(--aik-text-3)}
.aik-tc-panes{display:grid;grid-template-columns:minmax(0,1fr) minmax(0,1fr);border-top:1px solid var(--aik-line)}
.aik-tc-pane{display:grid;align-content:start;gap:8px;min-height:132px;padding:14px 18px 16px;min-width:0}
.aik-tc-pane + .aik-tc-pane{border-left:1px solid var(--aik-line);background:var(--aik-panel)}
.aik-tc-pane pre{margin:0;font:12.5px/19px var(--aik-mono);color:var(--aik-text);white-space:pre-wrap;overflow-wrap:anywhere}
.aik-tc-pane code{display:block;font:12.5px/19px var(--aik-mono);color:var(--aik-text-2);overflow-wrap:anywhere;animation:aik-in var(--aik-d-enter) var(--aik-ease-out) both}
.aik-tc-pane code:last-of-type{color:var(--aik-text)}
@container (max-width:430px){.aik-tc-lane{padding:16px 16px 12px;gap:10px}.aik-tc-end{font-size:0;gap:0}.aik-tc-pane{padding:10px 14px 12px}.aik-tc-tile{width:48px;height:48px;border-radius:14px}.aik-tc-w{height:48px}.aik-tc-w::before,.aik-tc-w > i{top:23px}.aik-tc-pkt{top:16px}.aik-tc-w > span{display:none}.aik-tc-panes{grid-template-columns:minmax(0,1fr)}.aik-tc-pane + .aik-tc-pane{border-left:0;border-top:1px solid var(--aik-line)}.aik-tc-pane{min-height:0}.aik-tc-lat{display:none}}
@media (prefers-reduced-motion: reduce){.aik-tc-pkt,.aik-tc-w > i{transition:none}}
.aik-tc-hubw{--aik-r-shell:26px;--aik-rim:5px;width:min(100%,520px);container-type:inline-size}
.aik-tc-hcard{display:grid;gap:4px;padding:16px 18px 18px}
.aik-tc-hhead{display:flex;align-items:baseline;justify-content:space-between;gap:10px;font-size:15px;font-weight:600}
.aik-tc-hhead .aik-roll{font:500 13px var(--aik-mono);color:var(--aik-text-2);font-variant-numeric:tabular-nums}
.aik-tc-hub{--r:min(124px,30cqw);position:relative;height:calc(var(--r)*2 + 96px)}
.aik-tc-core{position:absolute;left:50%;top:calc(50% - 8px);display:grid;place-items:center;width:92px;height:92px;margin:-46px 0 0 -46px;border-radius:50%;background:var(--aik-card);box-shadow:inset 0 0 0 1px var(--aik-line-2);z-index:2}
.aik-tc-spoke{position:absolute;left:50%;top:calc(50% - 8px);width:var(--r);height:2px;margin-top:-1px;border-radius:1px;background:var(--aik-line-2);transform-origin:0 50%;transform:rotate(var(--a))}
.aik-tc-spoke::before{content:"";position:absolute;inset:0;border-radius:1px;background:var(--aik-blue);transform-origin:0 50%;transform:scaleX(0);transition:transform 520ms var(--aik-ease-move),background var(--aik-d-panel)}
.aik-tc-spoke[data-s]::before{transform:scaleX(1)}
.aik-tc-spoke[data-s="done"]::before{background:var(--aik-green)}
.aik-tc-spoke[data-s="fail"]::before{background:var(--aik-red)}
.aik-tc-spoke::after{content:"";position:absolute;left:0;top:-4px;width:10px;height:10px;border-radius:50%;background:var(--aik-blue);box-shadow:0 0 10px 2px color-mix(in oklab,var(--aik-blue) 55%,transparent);opacity:0}
.aik-tc-spoke[data-s="run"]::after{animation:aik-tc-pulse 1s var(--aik-ease-move) infinite}
@keyframes aik-tc-pulse{0%{opacity:0;transform:translateX(40px)}15%{opacity:1}85%{opacity:1}100%{opacity:0;transform:translateX(calc(var(--r) - 34px))}}
.aik-tc-svc{position:absolute;left:50%;top:calc(50% - 8px);z-index:3;display:flex;flex-direction:column;align-items:center;width:0;height:0;transform:rotate(var(--a)) translateX(var(--r)) rotate(calc(var(--a)*-1))}
.aik-tc-svc button{display:grid;place-items:center;width:52px;height:52px;margin-top:-26px;flex:none;border-radius:16px;background:var(--aik-raised);box-shadow:inset 0 0 0 1px var(--aik-line);color:var(--aik-text);transition:box-shadow var(--aik-d-panel) var(--aik-ease-out),transform var(--aik-d-press) var(--aik-ease-out),background var(--aik-d-state)}
.aik-tc-svc button:hover{background:var(--aik-sunk)}
.aik-tc-svc button:active{transform:scale(.94)}
.aik-tc-svc[data-s="run"] button{box-shadow:inset 0 0 0 1.5px var(--aik-blue),0 0 0 4px color-mix(in oklab,var(--aik-blue) 14%,transparent)}
.aik-tc-svc[data-s="done"] button{box-shadow:inset 0 0 0 1.5px var(--aik-green)}
.aik-tc-svc[data-s="fail"] button{box-shadow:inset 0 0 0 1.5px var(--aik-red)}
.aik-tc-svc > span{margin-top:6px;padding:0 6px;border-radius:6px;background:var(--aik-card);font-size:12px;line-height:20px;color:var(--aik-text-3);white-space:nowrap}
.aik-tc-res{display:inline-flex;align-items:center;gap:5px;height:22px;margin-top:6px;padding:0 8px 0 5px;border-radius:7px;background:var(--aik-card);box-shadow:var(--aik-sh-pop);font-size:12px;font-weight:500;white-space:nowrap;animation:aik-pop var(--aik-d-enter) var(--aik-ease-out) both}
@media (prefers-reduced-motion: reduce){.aik-tc-spoke::after{animation:none!important}.aik-tc-spoke::before{transition:none}}
`
interface AgentLook {
/** Face colour: a named tone or any CSS colour. */
tone?: AiTone | (string & {})
shape?: AiShape
mood?: AiMood
}
export interface AiToolChipProps extends Omit<
React.HTMLAttributes<HTMLSpanElement>,
"children"
> {
/** The tool's name, shown in mono. */
name: string
/** The call's main argument, such as a file name or a query. */
arg?: string
status?: AiStatus
icon?: React.ReactNode
/** A small result preview that unfolds under the chip once the call is done. */
peek?: React.ReactNode
/** A label at the top of the peek. */
peekTitle?: string
/** Controls the peek. Leave undefined to open it on hover, focus and click. */
open?: boolean
onOpenChange?: (open: boolean) => void
}
/**
* A tool call inline in a sentence. It spins while running and, once done,
* unfolds in place into a small peek at the result.
*/
export function AiToolChip({
name,
arg,
status = "done",
icon,
peek,
peekTitle,
open,
onOpenChange,
className,
...rest
}: AiToolChipProps) {
const [own, setOwn] = React.useState(false)
const id = React.useId()
const canPeek = Boolean(peek) && status === "done"
const shown = canPeek && (open ?? own)
const set = (next: boolean) => {
if (open === undefined) setOwn(next)
onOpenChange?.(next)
}
return (
<span
{...rest}
className={cx("aik aik-tc-chip", className)}
data-s={status}
data-peek={canPeek ? "" : undefined}
data-open={shown ? "" : undefined}
onPointerEnter={() => canPeek && set(true)}
onPointerLeave={() => canPeek && set(false)}
onKeyDown={(event) => {
if (event.key === "Escape" && shown) set(false)
}}
>
<AiKitStyles />
<AiStyle id="ai-tool" css={CSS} />
<button
type="button"
className="aik-tc-chipb"
aria-expanded={canPeek ? shown : undefined}
aria-controls={canPeek ? id : undefined}
onClick={() => canPeek && set(!shown)}
onFocus={() => canPeek && set(true)}
onBlur={() => canPeek && set(false)}
>
{icon}
{name}
{arg ? <em>{arg}</em> : null}
<i className="aik-tc-dot" data-s={status}>
{status === "done" ? <Check size={10} strokeWidth={3} /> : null}
{status === "fail" ? <X size={10} strokeWidth={3} /> : null}
</i>
</button>
{peek ? (
<span
className="aik-tc-peek"
id={id}
role="group"
aria-label={peekTitle}
>
{peekTitle ? <b>{peekTitle}</b> : null}
{peek}
</span>
) : null}
</span>
)
}
export interface AiToolService {
label: string
/** A Simple Icons slug the kit ships, such as "github". */
brand?: string
/** A custom mark in place of the brand icon. */
icon?: React.ReactNode
}
export type AiWirePhase =
| "idle"
| "sending"
| "waiting"
| "receiving"
| "done"
| "fail"
export interface AiToolWireProps
extends AgentLook, React.HTMLAttributes<HTMLDivElement> {
/** The tool's name, shown in mono in the header. */
name: string
/** The agent's name under its tile. */
agent?: string
service: AiToolService
/** Where the call is. The packet travels out on `sending` and back on `receiving`. */
phase: AiWirePhase
/** The request body so far. */
request: string
/** Response lines so far. */
response: string[]
/** Elapsed time, already formatted. */
latency?: string
/** Shows a retry button in the header. */
onRetry?: () => void
}
const PHASE_STATUS: Record<AiWirePhase, AiStatus> = {
idle: "todo",
sending: "run",
waiting: "run",
receiving: "run",
done: "done",
fail: "fail",
}
const PHASE_LABEL: Record<AiWirePhase, string> = {
idle: "Ready",
sending: "Sending",
waiting: "Waiting",
receiving: "Receiving",
done: "Done",
fail: "Failed",
}
/**
* A tool call as a wire between the agent and a service. A packet carries the
* request out and the response back, with both bodies shown underneath.
*/
export function AiToolWire({
name,
agent = "Agent",
service,
phase,
request,
response,
latency,
onRetry,
tone = "blue",
shape = "orb",
mood,
className,
...rest
}: AiToolWireProps) {
const out = phase === "sending" || phase === "waiting"
const live = phase === "sending" || phase === "receiving"
const status = PHASE_STATUS[phase]
return (
<div
{...rest}
className={cx("aik aik-tc-wire", className)}
data-p={phase}
style={
{
"--at": out ? 1 : 0,
"--show": live ? 1 : 0,
"--fill": phase === "idle" ? 0 : 1,
...rest.style,
} as React.CSSProperties
}
>
<AiKitStyles />
<AiStyle id="ai-tool" css={CSS} />
<div className="aik-shell">
<div className="aik-surface aik-tc-wcard">
<div className="aik-tc-whead">
<b>{name}</b>
<Pill status={status}>
<Roll>{PHASE_LABEL[phase]}</Roll>
</Pill>
<span className="aik-tc-lat">{latency}</span>
{onRetry ? (
<button
type="button"
className="aik-tc-ib"
aria-label="Run again"
onClick={onRetry}
>
<RotateCcw size={16} />
</button>
) : null}
</div>
<div className="aik-tc-lane">
<div className="aik-tc-end">
<span className="aik-tc-tile">
<Face
size={36}
tone={tone}
shape={shape}
mood={
mood ??
(phase === "done"
? "happy"
: phase === "fail"
? "oops"
: phase === "idle"
? "idle"
: phase === "waiting"
? "think"
: "work")
}
/>
</span>
{agent}
</div>
<div className="aik-tc-w" aria-hidden="true">
<span>
<Roll>
{phase === "receiving" || phase === "done"
? "← response"
: phase === "fail"
? "no answer"
: "request →"}
</Roll>
</span>
<i />
<b className="aik-tc-pkt" />
</div>
<div className="aik-tc-end">
<span className="aik-tc-tile svc">
{service.icon ??
(service.brand ? (
<BrandIcon name={service.brand} size={26} />
) : null)}
<i
className="aik-beam"
data-on={phase === "waiting" ? "" : undefined}
/>
</span>
{service.label}
</div>
</div>
<div className="aik-tc-panes">
<div className="aik-tc-pane">
<span className="aik-label">Request</span>
<pre>
{request}
{phase === "idle" || phase === "sending" ? (
<i className="aik-caret" />
) : null}
</pre>
</div>
<div className="aik-tc-pane" aria-live="polite">
<span className="aik-label">Response</span>
{response.map((line, index) => (
<code key={`${index}-${line}`}>{line}</code>
))}
</div>
</div>
</div>
</div>
</div>
)
}
export interface AiHubService extends AiToolService {
id: string
}
export interface AiHubCall {
id: string
/** The `id` of the service being called. */
service: string
status: "run" | "done" | "fail"
/** A short result shown beside the service once the call lands. */
label?: string
}
export interface AiToolHubProps
extends AgentLook, React.HTMLAttributes<HTMLDivElement> {
services: AiHubService[]
/** Calls in flight or finished. The latest call per service is drawn. */
calls: AiHubCall[]
/** The heading above the hub. */
label?: string
/** Called with a service's id when its tile is pressed. */
onCall?: (service: string) => void
}
/**
* The agent at the centre of its tools. Each call draws a line out to a
* service with a pulse travelling along it; several can run at once.
*/
export function AiToolHub({
services,
calls,
label = "Tools",
onCall,
tone = "blue",
shape = "blob",
mood,
className,
...rest
}: AiToolHubProps) {
const latest = new Map<string, AiHubCall>()
for (const call of calls) latest.set(call.service, call)
const running = calls.some((call) => call.status === "run")
const total = calls.length
return (
<div {...rest} className={cx("aik aik-tc-hubw", className)}>
<AiKitStyles />
<AiStyle id="ai-tool" css={CSS} />
<div className="aik-shell">
<div className="aik-surface aik-tc-hcard">
<div className="aik-tc-hhead">
{label}
<Roll>{`${total} ${total === 1 ? "call" : "calls"}`}</Roll>
</div>
<div className="aik-tc-hub">
{services.map((service, index) => {
const angle = -90 + (index * 360) / services.length
const call = latest.get(service.id)
return (
<i
key={service.id}
className="aik-tc-spoke"
data-s={call?.status}
style={{ "--a": `${angle}deg` } as React.CSSProperties}
/>
)
})}
<span className="aik-tc-core">
<Face
size={60}
tone={tone}
shape={shape}
mood={mood ?? (running ? "work" : total > 0 ? "happy" : "idle")}
/>
</span>
{services.map((service, index) => {
const angle = -90 + (index * 360) / services.length
const call = latest.get(service.id)
return (
<div
key={service.id}
className="aik-tc-svc"
data-s={call?.status}
style={{ "--a": `${angle}deg` } as React.CSSProperties}
>
<button
type="button"
aria-label={`Call ${service.label}`}
onClick={() => onCall?.(service.id)}
>
{service.icon ??
(service.brand ? (
<BrandIcon name={service.brand} size={22} />
) : null)}
</button>
{call && call.status !== "run" && call.label ? (
<span className="aik-tc-res" key={call.id} role="status">
<i className="aik-tc-dot" data-s={call.status}>
{call.status === "done" ? (
<Check size={10} strokeWidth={3} />
) : (
<X size={10} strokeWidth={3} />
)}
</i>
{call.label}
</span>
) : (
<span>{service.label}</span>
)}
</div>
)
})}
</div>
</div>
</div>
</div>
)
}
Installation
One command adds ai-tool and the shared AI Kit layer it is built on (tokens, faces, marks and brand icons). No animation library, light and dark built in.
"use client"
import * as React from "react"
import { FileText, Search, Terminal } from "lucide-react"
import { AiDemo } from "@/lib/ai-demo"
import {
AiStyle,
Face,
typeInto,
useAiLoop,
useIdleHands,
type AiStatus,
} from "@/lib/ai-kit"
import {
AiToolChip,
AiToolHub,
AiToolWire,
type AiHubCall,
type AiHubService,
type AiWirePhase,
} from "@/registry/tween/ai-tool"
const CSS = `
.aik-tcd-turn{display:grid;grid-template-columns:32px minmax(0,1fr);gap:14px;align-content:start;width:min(100%,540px);min-height:330px}
.aik-tcd-turn > div > b{display:block;padding:4px 0 6px;font-size:15px;font-weight:600}
.aik-tcd-turn p{font-size:16px;line-height:34px;color:var(--aik-text)}
@container (max-width:420px){.aik-tcd-turn{grid-template-columns:minmax(0,1fr)}.aik-tcd-turn > .aik-face{display:none}.aik-tcd-turn p{font-size:15px;line-height:32px}}
`
const realSleep = (ms: number) =>
new Promise<void>((resolve) => window.setTimeout(resolve, ms))
function Chips() {
const root = React.useRef<HTMLDivElement>(null)
const [hands, touch] = useIdleHands()
const [state, setState] = React.useState<AiStatus[]>(["done", "done", "run"])
const [open, setOpen] = React.useState(-1)
useAiLoop(
root,
async ({ sleep }) => {
setOpen(-1)
setState(["todo", "todo", "todo"])
await sleep(600)
for (let index = 0; index < 3; index++) {
setState((list) =>
list.map((item, at) => (at === index ? "run" : item))
)
await sleep(1100)
setState((list) =>
list.map((item, at) => (at === index ? "done" : item))
)
await sleep(350)
}
for (const index of [0, 1, 2]) {
setOpen(index)
await sleep(2100)
setOpen(-1)
await sleep(500)
}
},
{
gap: 600,
enabled: !hands,
still: () => {
setState(["done", "done", "done"])
setOpen(0)
},
}
)
const controlled = (index: number) => (hands ? undefined : open === index)
return (
<div
ref={root}
className="aik aik-tcd-turn"
onPointerDown={touch}
onKeyDown={touch}
onPointerMove={hands ? touch : undefined}
>
<Face
size={32}
shape="orb"
mood={state.includes("run") ? "work" : "talk"}
/>
<div>
<b>Mira</b>
<p>
I opened{" "}
<AiToolChip
name="read_file"
arg="hero.tsx"
icon={<FileText size={13} />}
status={state[0]}
open={controlled(0)}
peekTitle="hero.tsx · lines 12 to 14"
peek={
<>
<code>{'<h1 className="hero-title">'}</code>
<code>{" Motion, considered."}</code>
<code>{"</h1>"}</code>
</>
}
/>{" "}
to see how the headline is built, searched{" "}
<AiToolChip
name="search_registry"
arg={'"skew"'}
icon={<Search size={13} />}
status={state[1]}
open={controlled(1)}
peekTitle="3 matches"
peek={
<>
<code>text-skew-reveal · registry/tween</code>
<code>skew-scroll-gallery · registry/tween</code>
<code>text-skew-reveal-demo · example</code>
</>
}
/>{" "}
for a reveal that already exists, then ran{" "}
<AiToolChip
name="run_command"
arg="pnpm typecheck"
icon={<Terminal size={13} />}
status={state[2]}
open={controlled(2)}
peekTitle="Exit 0 · 2.6s"
peek={
<>
<code>$ tsc --noEmit</code>
<code>checked 214 files</code>
<code>0 errors</code>
</>
}
/>{" "}
to make sure nothing broke.
</p>
</div>
</div>
)
}
const REQUEST = `{
"repo": "tween-www",
"title": "Add pricing section",
"base": "main"
}`
const RESPONSE = [
"201 Created",
"number: 482",
"files: 3 (+126 −8)",
"url: github.com/tween/www/pull/482",
]
function Wire() {
const root = React.useRef<HTMLDivElement>(null)
const [hands, touch] = useIdleHands()
const [phase, setPhase] = React.useState<AiWirePhase>("waiting")
const [request, setRequest] = React.useState(REQUEST)
const [lines, setLines] = React.useState(0)
const [ms, setMs] = React.useState(420)
const run = React.useRef(0)
const play = React.useCallback(
async (
sleep: (ms: number) => Promise<void>,
mine: number,
type: boolean
) => {
const alive = () => run.current === mine
setPhase("idle")
setLines(0)
setMs(0)
if (type) {
setRequest("")
await sleep(400)
await typeInto(
(value) => alive() && setRequest(value),
REQUEST,
sleep,
14
)
} else setRequest(REQUEST)
await sleep(500)
if (!alive()) return
setPhase("sending")
for (let tick = 0; tick < 4; tick++) {
await sleep(170)
setMs((value) => value + 170)
}
if (!alive()) return
setPhase("waiting")
for (let tick = 0; tick < 7; tick++) {
await sleep(170)
setMs((value) => value + 170)
}
if (!alive()) return
setPhase("receiving")
for (let line = 1; line <= RESPONSE.length; line++) {
await sleep(240)
if (!alive()) return
setMs((value) => value + 60)
setLines(line)
}
setPhase("done")
},
[]
)
useAiLoop(
root,
async ({ sleep }) => {
const mine = ++run.current
await play(sleep, mine, true)
await sleep(2600)
},
{
gap: 500,
enabled: !hands,
still: () => {
setPhase("done")
setLines(RESPONSE.length)
setMs(1840)
},
}
)
return (
<div
ref={root}
style={{ display: "grid", width: "min(100%, 560px)" }}
onPointerDown={touch}
onKeyDown={touch}
>
<AiToolWire
name="github.create_pull_request"
agent="Mira"
service={{ label: "GitHub", brand: "github" }}
phase={phase}
request={request}
response={RESPONSE.slice(0, lines)}
latency={phase === "idle" ? "" : `${(ms / 1000).toFixed(2)}s`}
onRetry={() => {
const mine = ++run.current
void play(realSleep, mine, false)
}}
/>
</div>
)
}
const SERVICES: AiHubService[] = [
{ id: "github", label: "GitHub", brand: "github" },
{ id: "linear", label: "Linear", brand: "linear" },
{ id: "figma", label: "Figma", brand: "figma" },
{ id: "vercel", label: "Vercel", brand: "vercel" },
{ id: "notion", label: "Notion", brand: "notion" },
]
const RESULT: Record<string, string> = {
github: "PR #482 opened",
linear: "TW-91 moved",
figma: "3 frames read",
vercel: "Preview ready",
notion: "Notes saved",
}
function Hub() {
const root = React.useRef<HTMLDivElement>(null)
const [hands, touch] = useIdleHands()
const [calls, setCalls] = React.useState<AiHubCall[]>([
{ id: "seed-1", service: "github", status: "done", label: RESULT.github },
{ id: "seed-2", service: "linear", status: "run" },
])
const serial = React.useRef(0)
const start = (service: string) => {
const id = `call-${++serial.current}`
setCalls((list) => [...list, { id, service, status: "run" }])
return id
}
const finish = (id: string) =>
setCalls((list) =>
list.map((call) =>
call.id === id
? { ...call, status: "done", label: RESULT[call.service] }
: call
)
)
useAiLoop(
root,
async ({ sleep }) => {
setCalls([])
await sleep(900)
const github = start("github")
await sleep(500)
const linear = start("linear")
await sleep(1300)
finish(github)
await sleep(600)
const figma = start("figma")
await sleep(700)
finish(linear)
await sleep(900)
finish(figma)
await sleep(500)
const vercel = start("vercel")
const notion = start("notion")
await sleep(1500)
finish(vercel)
await sleep(500)
finish(notion)
await sleep(2400)
},
{
gap: 400,
enabled: !hands,
still: () =>
setCalls([
{ id: "a", service: "github", status: "done", label: RESULT.github },
{ id: "b", service: "figma", status: "done", label: RESULT.figma },
]),
}
)
return (
<div
ref={root}
style={{ display: "grid", width: "min(100%, 520px)" }}
onPointerDown={touch}
onKeyDown={touch}
>
<AiToolHub
label="Mira's tools"
services={SERVICES}
calls={calls}
onCall={(service) => {
const id = start(service)
window.setTimeout(() => finish(id), 1500)
}}
/>
</div>
)
}
export function AiToolDemo({ only }: { only?: string }) {
return (
<>
<AiStyle id="ai-tool-demo" css={CSS} />
<AiDemo
only={only}
tabs={[
{ name: "Unfolding chip", node: <Chips /> },
{ name: "Wire", node: <Wire /> },
{ name: "Hub", node: <Hub /> },
]}
/>
</>
)
}
Installation
Usage
import { AiToolChip, AiToolHub, AiToolWire } from "@/components/ui/ai-tool"<p>
I opened{" "}
<AiToolChip
name="read_file"
arg="hero.tsx"
status="done"
peekTitle="hero.tsx · lines 12 to 14"
peek={<code>{'<h1 className="hero-title">'}</code>}
/>{" "}
to see how the headline is built.
</p><AiToolWire
name="github.create_pull_request"
agent="Mira"
service={{ label: "GitHub", brand: "github" }}
phase={phase}
request={request}
response={lines}
latency="1.84s"
onRetry={retry}
/>Brand marks come from the kit's BrandIcon set. Pass icon instead of brand to use your own.
Versions
AiToolChipsits inside a sentence. It is tinted and spinning while the call runs, then settles to neutral with a check. Once done it unfolds a small peek at the result on hover, focus or click, and Escape folds it back.AiToolWiredraws the call as a wire between the agent and a service. A packet carries the request out onsending, the service tile takes a travelling edge light onwaiting, and the packet returns onreceivingwhile the response arrives line by line.AiToolHubputs the agent's face at the centre of its services. Each call draws a line out with a pulse travelling along it, the tile lights, and a result chip lands beside it. Several calls can run at once.
Props
AiToolChip
| Prop | Type | Default | Description |
|---|---|---|---|
name | string | The tool's name, in mono. | |
arg | string | The main argument, such as a file name. | |
status | "todo" | "run" | "ask" | "done" | "fail" | "done" | Drives the tint and the mark. |
icon | ReactNode | A small icon before the name. | |
peek | ReactNode | The result preview. Shown only once the call is done. | |
peekTitle | string | A label at the top of the peek. | |
open | boolean | Controls the peek. Omit to let hover and focus do it. | |
onOpenChange | (open: boolean) => void | Called when the peek opens or closes. |
AiToolWire
| Prop | Type | Default | Description |
|---|---|---|---|
name | string | The tool's name in the header. | |
service | { label, brand?, icon? } | The far end of the wire. | |
phase | "idle" | "sending" | "waiting" | "receiving" | "done" | "fail" | Where the call is. | |
request | string | The request body so far. | |
response | string[] | Response lines so far. | |
latency | string | Elapsed time, already formatted. | |
agent | string | "Agent" | The name under the agent's tile. |
onRetry | () => void | Shows a retry button in the header. |
AiToolHub
| Prop | Type | Default | Description |
|---|---|---|---|
services | { id, label, brand?, icon? }[] | Tiles placed evenly around the face. | |
calls | { id, service, status, label? }[] | status is run, done or fail. | |
label | string | "Tools" | The heading above the hub. |
onCall | (service: string) => void | Called when a service tile is pressed. |
AiToolWire and AiToolHub also take tone, shape and mood for the face.