Agent Cursors
Agents editing a document like teammates in a multiplayer editor: named pointers, suggestions typed letter by letter, and a cost on every label. Switch between a doc and a table.
"use client"
import * as React from "react"
import { Check, FileText, Plus } from "lucide-react"
import {
AiKitStyles,
AiStyle,
BrandIcon,
cx,
Face,
Roll,
type AiShape,
type AiTone,
} from "@/lib/ai-kit"
export interface CursorAgent {
id: string
name: string
tone: AiTone
shape?: AiShape
/** Brand mark of the model behind the agent. */
vendor?: string
/** What this agent has cost so far, in dollars. */
cost: number
/** What it is doing, shown under its name on the pointer. */
task: string
/** `data-anchor` of the element its pointer sits on. No anchor hides the pointer. */
anchor?: string
/** 0 to 1, drawn as the ring round its face in the header. */
progress: number
}
/** A run of text, or a reference to a suggested edit by id. */
export type CursorSeg = string | { edit: string }
export interface CursorEdit {
/** Text the agent is replacing (struck through until accepted). */
from: string
/** The replacement, typed in letter by letter. */
to: string
/** How many letters of `to` are typed so far. */
typed: number
by: string
}
export type CursorBlock =
| {
id: string
kind: "h1" | "h2" | "p" | "li"
segs: CursorSeg[]
by?: string
dim?: boolean
}
| { id: string; kind: "code"; lines: CursorSeg[][]; by?: string }
| {
id: string
kind: "media"
width: number
height: number
text: CursorSeg[]
by?: string
}
export interface CursorTable {
title: string
columns: readonly string[]
rows: ReadonlyArray<{ id: string; cells: ReadonlyArray<CursorSeg[]> }>
/** Index of the column rendered as status pills. */
statusColumn?: number
}
export interface CursorComment {
by: string
text: string
anchor: string
reply?: string
}
export interface AgentCursorsProps {
path: string
view: "doc" | "table"
onViewChange?: (view: "doc" | "table") => void
agents: readonly CursorAgent[]
doc: readonly CursorBlock[]
table: CursorTable
edits: Readonly<Record<string, CursorEdit>>
/** Once accepted, edits show only their new text. */
accepted?: boolean
onAcceptAll?: () => void
comment?: CursorComment
onReply?: (text: string) => void
className?: string
}
const CSS = `
.aik-cu{width:min(100%,1000px);display:grid;grid-template-columns:minmax(0,1fr);gap:14px;container-type:inline-size}
.aik-cu-win{min-width:0}
.aik-cu-tabs{display:flex;gap:8px}
.aik-cu-tabs button{height:40px;padding:0 18px;border-radius:12px;font-weight:500;font-size:15px;color:var(--aik-text-3);background:var(--aik-raised);transition:background var(--aik-d-state),color var(--aik-d-state),box-shadow var(--aik-d-state),transform var(--aik-d-press)}
.aik-cu-tabs button:active{transform:scale(.96)}
.aik-cu-tabs button[aria-selected="true"]{color:var(--aik-text);background:transparent;box-shadow:inset 0 0 0 1px var(--aik-line-2)}
.aik-cu-win{--aik-r-shell:26px;--aik-rim:5px}
.aik-cu-bar{display:flex;align-items:center;gap:12px;padding:10px 12px 12px 16px;min-width:0}
.aik-cu-path{display:flex;align-items:center;gap:10px;font-size:15.5px;color:var(--aik-text-2);white-space:nowrap;min-width:0;flex:1 1 auto}
.aik-cu-path > span{overflow:hidden;text-overflow:ellipsis;min-width:0}
.aik-cu-crew{flex:none}
.aik-cu-path svg{color:var(--aik-text-3)}
.aik-cu-crew{margin-left:auto;display:flex;align-items:center;gap:4px}
.aik-cu-ring{position:relative;display:grid;place-items:center;width:40px;height:40px;border-radius:50%;background:conic-gradient(var(--c) calc(var(--p)*1turn),var(--aik-line-2) 0);transition:--p 600ms}
.aik-cu-ring::before{content:"";position:absolute;inset:2px;border-radius:50%;background:var(--aik-sunk)}
.aik-cu-ring > *{position:relative}
.aik-cu-ring[data-idle]{opacity:.5}
.aik-cu-sum{display:flex;align-items:center;gap:12px;margin-left:8px;font-size:15px;color:var(--aik-text-3);white-space:nowrap;font-variant-numeric:tabular-nums}
.aik-cu-sum b{color:var(--aik-text);font-weight:600}
.aik-cu-accept{height:40px;padding:0 16px;white-space:nowrap;flex:none;border-radius:12px;font-weight:600;font-size:15px;background:var(--aik-pri);color:var(--aik-pri-ink);transition:transform var(--aik-d-press),opacity var(--aik-d-state)}
.aik-cu-accept:active{transform:scale(.96)}
.aik-cu-accept:disabled{opacity:.4;cursor:default}
.aik-cu-page{position:relative;min-height:500px;padding:40px 48px 44px 76px;overflow:hidden}
.aik-cu-doc{display:grid;grid-template-columns:minmax(0,1fr);gap:18px;max-width:720px}
.aik-cu-blk{min-width:0;overflow-wrap:anywhere}
.aik-cu-blk{position:relative;transition:opacity var(--aik-d-panel)}
.aik-cu-blk[data-dim]{opacity:.38}
.aik-cu-who{position:absolute;left:-54px;top:0;display:grid;place-items:center;width:38px;height:38px;border-radius:50%;background:var(--aik-card);box-shadow:var(--aik-sh-pop);animation:aik-cu-pop 420ms var(--aik-ease-soft) both}
.aik-cu-blk[data-kind="h1"] h3{font-size:34px;line-height:40px;font-weight:650;letter-spacing:-.025em}
.aik-cu-blk[data-kind="h2"] h4{font-size:22px;line-height:28px;font-weight:600;letter-spacing:-.01em}
.aik-cu-blk[data-kind="p"] p,.aik-cu-blk[data-kind="li"] p{font-size:17px;line-height:28px;color:var(--aik-text-2)}
.aik-cu-blk[data-kind="li"] p{padding-left:20px;position:relative}
.aik-cu-blk[data-kind="li"] p::before{content:"";position:absolute;left:4px;top:12px;width:5px;height:5px;border-radius:50%;background:var(--aik-text-3)}
.aik-cu-media{display:grid;grid-template-columns:auto 1fr;gap:28px;align-items:start}
.aik-cu-shot{position:relative;height:var(--h);width:var(--w);border-radius:16px;background:linear-gradient(135deg,color-mix(in oklab,var(--aik-f-violet) 22%,var(--aik-raised)),var(--aik-raised) 55%,color-mix(in oklab,var(--aik-f-blue) 18%,var(--aik-raised)));box-shadow:inset 0 0 0 1px var(--aik-line-2)}
.aik-cu-shot::before{content:"";position:absolute;left:18px;top:18px;right:40%;height:9px;border-radius:5px;background:var(--aik-line-3);box-shadow:0 18px 0 -1px var(--aik-line-2),0 34px 0 -1px var(--aik-line-2)}
.aik-cu-shot[data-sel]{outline:2px solid var(--c);outline-offset:3px}
.aik-cu-handle{position:absolute;width:12px;height:12px;border-radius:4px;background:var(--aik-card);box-shadow:0 0 0 2px var(--c);opacity:0;transition:opacity var(--aik-d-state)}
.aik-cu-shot[data-sel] .aik-cu-handle{opacity:1}
.aik-cu-size{position:absolute;left:50%;bottom:14px;translate:-50% 0;padding:3px 9px;border-radius:7px;font:600 13px var(--aik-mono);color:var(--aik-pri-ink);background:var(--aik-pri);opacity:0;transition:opacity var(--aik-d-state);font-variant-numeric:tabular-nums}
.aik-cu-shot[data-sel] .aik-cu-size{opacity:.92}
.aik-cu-code{padding:18px 22px;border-radius:16px;background:var(--aik-sunk);box-shadow:inset 0 0 0 1px var(--aik-line);font:15px/26px var(--aik-mono);color:var(--aik-text-2);white-space:pre;overflow-x:auto}
.aik-cu-old{color:var(--aik-text-3);text-decoration:line-through;text-decoration-color:var(--aik-text-3);text-decoration-thickness:1.5px}
.aik-cu-new{position:relative;color:var(--aik-text);text-decoration:underline;text-decoration-color:var(--c);text-decoration-thickness:2px;text-underline-offset:4px;background:color-mix(in oklab,var(--c) 12%,transparent);border-radius:4px}
.aik-cu-new[data-typing]::after{content:"";display:inline-block;width:2px;height:1em;margin-left:1px;vertical-align:-.12em;background:var(--c);animation:aik-cu-blink .9s steps(1) infinite}
.aik-cu-done{color:inherit}
.aik-cu-tbl-wrap{display:grid;gap:20px}
.aik-cu-tbl-wrap h3{font-size:30px;line-height:36px;font-weight:650;letter-spacing:-.02em}
.aik-cu-tbl{width:100%;border-collapse:separate;border-spacing:0;border-radius:16px;box-shadow:inset 0 0 0 1px var(--aik-line-2);font-size:16px;overflow:hidden}
.aik-cu-tbl th{height:48px;padding:0 18px;text-align:left;font-weight:500;color:var(--aik-text-3);border-bottom:1px solid var(--aik-line)}
.aik-cu-tbl td{height:56px;padding:0 18px;border-bottom:1px solid var(--aik-line);white-space:nowrap}
.aik-cu-tbl td + td,.aik-cu-tbl th + th{border-left:1px solid var(--aik-line)}
.aik-cu-add{display:flex;align-items:center;gap:10px;height:52px;padding:0 18px;color:var(--aik-text-3);font-size:15.5px}
.aik-cu-pill{display:inline-flex;align-items:center;height:30px;padding:0 12px;border-radius:999px;font-size:14.5px;font-weight:500;color:var(--aik-text-2);background:var(--aik-raised)}
.aik-cu-pill[data-v="Doing"]{color:var(--aik-blue);background:color-mix(in oklab,var(--aik-blue) 15%,transparent)}
.aik-cu-pill[data-v="Done"]{color:var(--aik-green);background:color-mix(in oklab,var(--aik-green) 15%,transparent)}
.aik-cu-layer{position:absolute;inset:0;pointer-events:none;z-index:4}
.aik-cu-ptr{position:absolute;left:0;top:0;transition:transform 720ms var(--aik-ease-move),opacity var(--aik-d-panel);will-change:transform}
.aik-cu-ptr[data-hidden]{opacity:0}
.aik-cu-arrow{display:block;filter:drop-shadow(0 2px 3px rgb(0 0 0 / 30%))}
.aik-cu-tag{position:absolute;left:18px;top:24px;display:grid;gap:2px;min-width:188px;padding:10px 14px 11px;border-radius:14px;background:#fff;color:#16161a;box-shadow:0 0 0 1px rgb(12 12 24 / 8%),0 12px 28px -10px rgb(0 0 0 / 40%),0 8px 18px -8px var(--c);white-space:nowrap}
.aik-cu-tag::after{content:"";position:absolute;left:12px;right:12px;bottom:-1px;height:2px;border-radius:2px;background:linear-gradient(90deg,var(--c),color-mix(in oklab,var(--c) 30%,#ff9ad5))}
.aik-cu-tag b{display:flex;align-items:center;gap:12px;font-size:15px;font-weight:600}
.aik-cu-tag b em{margin-left:auto;padding:1px 7px;border-radius:7px;font:600 13px var(--aik-mono);font-style:normal;background:#ececef;font-variant-numeric:tabular-nums}
.aik-cu-tag span{font-size:14px;color:#5a5a64}
.aik-cu-note{position:absolute;z-index:5;width:340px;padding:16px;border-radius:20px;background:#f6f6f7;color:#16161a;box-shadow:0 0 0 1px rgb(12 12 24 / 8%),0 22px 50px -14px rgb(0 0 0 / 45%);animation:aik-cu-pop 420ms var(--aik-ease-soft) both;display:grid;gap:10px}
.aik-cu-note header{display:flex;align-items:center;gap:10px;font-weight:600;font-size:15px}
.aik-cu-note header em{margin-left:auto;font-style:normal;font-weight:400;color:#8a8a95;font-size:14px}
.aik-cu-note p{font-size:15.5px;line-height:22px;min-height:22px}
.aik-cu-note form{display:flex;gap:8px}
.aik-cu-note input{flex:1;height:40px;padding:0 14px;border:0;border-radius:12px;background:#e6e6ea;color:#16161a;outline:none;font-size:15px}
.aik-cu-note button{display:grid;place-items:center;width:40px;height:40px;border-radius:12px;background:#16161a;color:#fff}
@keyframes aik-cu-pop{from{opacity:0;transform:scale(.9) translateY(6px)}}
@keyframes aik-cu-blink{50%{opacity:0}}
@container (max-width:640px){
.aik-cu-page{padding:24px 18px 28px}
.aik-cu-who{display:none}
.aik-cu-sum,.aik-cu-crew .aik-cu-ring:nth-child(n+3){display:none}
.aik-cu-bar{gap:8px;padding-left:12px}.aik-cu-path{font-size:14px;min-width:0}.aik-cu-accept{padding:0 12px;font-size:14px}
.aik-cu-blk[data-kind="h1"] h3{font-size:26px;line-height:32px}
.aik-cu-media{grid-template-columns:1fr}
.aik-cu-shot{width:100%!important}
.aik-cu-tag{min-width:0}.aik-cu-tag span{display:none}
.aik-cu-tbl th:nth-child(2),.aik-cu-tbl td:nth-child(2){display:none}
.aik-cu-note{width:270px}
}
@media (prefers-reduced-motion: reduce){.aik-cu-ptr,.aik-cu-shot{transition:none}.aik-cu-new::after{animation:none}}
`
function money(n: number) {
return `$${n.toFixed(2)}`
}
/**
* Agents working in a document like teammates in a multiplayer editor: a
* named pointer per agent with its cost and task, suggestions typed in letter
* by letter over the struck old text, comments beside the page, and Accept all.
* Switch between a doc and a table.
*/
export function AgentCursors({
path,
view,
onViewChange,
agents,
doc,
table,
edits,
accepted,
onAcceptAll,
comment,
onReply,
className,
}: AgentCursorsProps) {
const page = React.useRef<HTMLDivElement>(null)
const [spots, setSpots] = React.useState<Record<string, [number, number]>>({})
const [notePos, setNotePos] = React.useState<[number, number] | null>(null)
const [reply, setReply] = React.useState("")
const byId = new Map(agents.map((a) => [a.id, a]))
// Pointers sit on the end of their anchor; re-read after every render.
React.useLayoutEffect(() => {
const root = page.current
if (!root) return
const box = root.getBoundingClientRect()
// Keep the last spot of a pointer that lost its anchor, so it fades in place.
const next: Record<string, [number, number]> = { ...spots }
for (const agent of agents) {
if (!agent.anchor) continue
const el = root.querySelector<HTMLElement>(
`[data-anchor="${agent.anchor}"]`
)
if (!el) continue
const rects = el.getClientRects()
const r = rects[rects.length - 1] ?? el.getBoundingClientRect()
next[agent.id] = [
r.right - box.left - 2,
r.bottom - box.top - r.height * 0.35,
]
}
let note: [number, number] | null = null
if (comment) {
const el = root.querySelector<HTMLElement>(
`[data-anchor="${comment.anchor}"]`
)
if (el) {
const r = el.getBoundingClientRect()
note = [Math.max(12, r.left - box.left - 40), r.top - box.top - 70]
}
}
const same =
JSON.stringify(next) === JSON.stringify(spots) &&
JSON.stringify(note) === JSON.stringify(notePos)
if (!same) {
setSpots(next)
setNotePos(note)
}
})
const segs = (list: readonly CursorSeg[], key: string) =>
list.map((seg, i) => {
if (typeof seg === "string")
return <React.Fragment key={`${key}-${i}`}>{seg}</React.Fragment>
const edit = edits[seg.edit]
if (!edit) return null
const agent = byId.get(edit.by)
const c = agent ? `var(--aik-f-${agent.tone})` : "var(--aik-blue)"
if (accepted && edit.typed > 0) {
return (
<span
key={`${key}-${i}`}
className="aik-cu-done"
data-anchor={`edit:${seg.edit}`}
>
{edit.to}
</span>
)
}
const typing = edit.typed > 0 && edit.typed < edit.to.length
return (
<React.Fragment key={`${key}-${i}`}>
<span
className={edit.typed > 0 ? "aik-cu-old" : undefined}
data-anchor={edit.typed > 0 ? undefined : `edit:${seg.edit}`}
>
{edit.from}
</span>
{edit.typed > 0 ? (
<span
className="aik-cu-new"
data-anchor={`edit:${seg.edit}`}
data-typing={
typing || agent?.anchor === `edit:${seg.edit}` ? "" : undefined
}
style={{ "--c": c } as React.CSSProperties}
>
{edit.to.slice(0, edit.typed)}
</span>
) : null}
</React.Fragment>
)
})
const pending = Object.values(edits).filter((e) => e.typed > 0).length
const total = agents.reduce((sum, a) => sum + a.cost, 0)
const gutter = (by?: string) => {
const agent = by ? byId.get(by) : undefined
return agent ? (
<span className="aik-cu-who" key={agent.id}>
<Face mood="happy" shape={agent.shape} tone={agent.tone} size={26} />
</span>
) : null
}
return (
<div className={cx("aik aik-cu", className)}>
<AiKitStyles />
<AiStyle id="agent-cursors" css={CSS} />
<div className="aik-cu-tabs" role="tablist" aria-label="View">
{(["doc", "table"] as const).map((v) => (
<button
key={v}
type="button"
role="tab"
aria-selected={view === v}
onClick={() => onViewChange?.(v)}
>
{v === "doc" ? "Doc" : "Table"}
</button>
))}
</div>
<div className="aik-cu-win aik-shell">
<div className="aik-cu-bar">
<span className="aik-cu-path">
<FileText size={18} />
<span>{path}</span>
</span>
<span className="aik-cu-crew">
{agents.map((agent) => (
<span
key={agent.id}
className="aik-cu-ring"
title={agent.name}
data-idle={agent.anchor ? undefined : ""}
style={
{
"--c": `var(--aik-f-${agent.tone})`,
"--p": agent.progress,
} as React.CSSProperties
}
>
<Face
mood={
agent.anchor
? "work"
: agent.progress >= 1
? "happy"
: "idle"
}
shape={agent.shape}
tone={agent.tone}
size={30}
/>
</span>
))}
</span>
<span className="aik-cu-sum">
<span>
<Roll>{`${accepted ? 0 : pending} suggestions`}</Roll>
</span>
<b>
<Roll>{money(total)}</Roll>
</b>
</span>
<button
type="button"
className="aik-cu-accept"
disabled={accepted || pending === 0}
onClick={onAcceptAll}
>
{accepted ? "Accepted" : "Accept all"}
</button>
</div>
<div className="aik-surface aik-cu-page" ref={page}>
{view === "doc" ? (
<div className="aik-cu-doc">
{doc.map((block) => (
<div
key={block.id}
className="aik-cu-blk"
data-kind={block.kind}
data-dim={"dim" in block && block.dim ? "" : undefined}
>
{gutter(block.by)}
{block.kind === "h1" ? (
<h3>{segs(block.segs, block.id)}</h3>
) : block.kind === "h2" ? (
<h4>{segs(block.segs, block.id)}</h4>
) : block.kind === "code" ? (
<pre className="aik-cu-code">
{block.lines.map((line, i) => (
<React.Fragment key={i}>
{segs(line, `${block.id}-${i}`)}
{i < block.lines.length - 1 ? "\n" : null}
</React.Fragment>
))}
</pre>
) : block.kind === "media" ? (
<div className="aik-cu-media">
<MediaShot block={block} agents={agents} />
<p
style={{
fontSize: 17,
lineHeight: "28px",
color: "var(--aik-text-2)",
}}
>
{segs(block.text, block.id)}
</p>
</div>
) : (
<p>{segs(block.segs, block.id)}</p>
)}
</div>
))}
</div>
) : (
<div className="aik-cu-tbl-wrap">
<h3>{table.title}</h3>
<div>
<table className="aik-cu-tbl">
<thead>
<tr>
{table.columns.map((c) => (
<th key={c}>{c}</th>
))}
</tr>
</thead>
<tbody>
{table.rows.map((row) => (
<tr key={row.id}>
{row.cells.map((cell, ci) => (
<td key={ci} data-anchor={`cell:${row.id}:${ci}`}>
{ci === table.statusColumn ? (
<span
className="aik-cu-pill"
data-v={cellText(cell, edits, accepted)}
>
{segs(cell, `${row.id}-${ci}`)}
</span>
) : (
segs(cell, `${row.id}-${ci}`)
)}
</td>
))}
</tr>
))}
</tbody>
</table>
<div className="aik-cu-add">
<Plus size={18} /> Add row
</div>
</div>
</div>
)}
<div className="aik-cu-layer" aria-hidden="true">
{agents.map((agent) => {
const at = spots[agent.id]
// Mount a pointer only once it has a spot, so it never glides in from the corner.
if (!at) return null
return (
<span
key={agent.id}
className="aik-cu-ptr"
data-aik-float=""
data-hidden={agent.anchor ? undefined : ""}
style={
{
"--c": `var(--aik-f-${agent.tone})`,
transform: `translate(${at[0]}px, ${at[1]}px)`,
} as React.CSSProperties
}
>
<svg
className="aik-cu-arrow"
width="26"
height="28"
viewBox="0 0 26 28"
>
<path
d="M3 2.5 22 12.2l-8.3 2.1-3.9 8.2Z"
fill="#fff"
stroke="rgb(0 0 0 / 25%)"
strokeWidth="1.2"
strokeLinejoin="round"
/>
</svg>
<span className="aik-cu-tag">
<b>
{agent.vendor ? (
<BrandIcon name={agent.vendor} tint="brand" size={14} />
) : null}
{agent.name}
<em>{money(agent.cost)}</em>
</b>
<span>{agent.task}</span>
</span>
</span>
)
})}
</div>
{comment && notePos ? (
<div
className="aik-cu-note"
data-aik-float=""
style={{ left: notePos[0], top: notePos[1] }}
key={comment.anchor}
>
<header>
{(() => {
const a = byId.get(comment.by)
return a ? (
<Face mood="talk" shape={a.shape} tone={a.tone} size={24} />
) : null
})()}
{byId.get(comment.by)?.name ?? comment.by}
<em>now</em>
</header>
<p>{comment.text}</p>
<form
onSubmit={(event) => {
event.preventDefault()
onReply?.(reply)
setReply("")
}}
>
<input
value={comment.reply ?? reply}
onChange={(e) => setReply(e.target.value)}
placeholder="Reply"
aria-label="Reply"
/>
<button type="submit" aria-label="Send reply">
<Check size={18} />
</button>
</form>
</div>
) : null}
</div>
</div>
</div>
)
}
function cellText(
cell: readonly CursorSeg[],
edits: Readonly<Record<string, CursorEdit>>,
accepted?: boolean
) {
return cell
.map((seg) => {
if (typeof seg === "string") return seg
const e = edits[seg.edit]
if (!e) return ""
return e.typed >= e.to.length || (accepted && e.typed > 0) ? e.to : e.from
})
.join("")
}
function MediaShot({
block,
agents,
}: {
block: Extract<CursorBlock, { kind: "media" }>
agents: readonly CursorAgent[]
}) {
const anchor = `media:${block.id}`
const by = agents.find((a) => a.anchor === anchor)
return (
<div
className="aik-cu-shot"
data-sel={by ? "" : undefined}
style={
{
"--w": `${block.width}px`,
"--h": `${block.height}px`,
"--c": by ? `var(--aik-f-${by.tone})` : "var(--aik-blue)",
} as React.CSSProperties
}
>
{[
{ left: -6, top: -6 },
{ right: -6, top: -6 },
{ left: -6, bottom: -6 },
].map((pos, i) => (
<span key={i} className="aik-cu-handle" style={pos} />
))}
<span
className="aik-cu-handle"
style={{ right: -6, bottom: -6 }}
data-anchor={anchor}
/>
<span className="aik-cu-size">
{Math.round(block.width)} × {block.height}
</span>
</div>
)
}
Installation
One command adds agent-cursors and the shared AI Kit layer it is built on (tokens, faces, marks and brand icons). No animation library, light and dark built in.
$npx shadcn@latest add https://tweenui.dev/r/agent-cursors.json