Collaboration Stream
What agents say to each other as a quiet chat: who each message is from and for, hand-offs and questions for you tagged, files as chips and a composer with @.
"use client"
import * as React from "react"
import {
ArrowUp,
AtSign,
Code2,
FileText,
Forward,
MessageCircleQuestion,
} from "lucide-react"
import {
AgentGlyph,
AiKitStyles,
AiStyle,
BrandIcon,
cx,
Face,
type AiShape,
type AiTone,
} from "@/lib/ai-kit"
export interface StreamMember {
id: string
name: string
tone: AiTone
/** People get a face; agents get a pixel mark. */
kind: "person" | "agent"
shape?: AiShape
}
/** A run of text, or a brand mark with its name, inside a message. */
export type StreamPart = string | { brand: string; label: string }
export interface StreamMessage {
id: string
kind: "system" | "message"
from: string
/** Who it is addressed to, when it is a direct hand-off or question. */
to?: string
/** The request it answers, quoted above the body. */
quote?: string
parts: readonly StreamPart[]
chips?: ReadonlyArray<{ kind: "tool" | "file"; label: string }>
tag?: "question" | "handoff"
time?: string
}
export interface CollaborationStreamProps {
members: readonly StreamMember[]
messages: readonly StreamMessage[]
/** A member id, or "all". */
filter: string
onFilter: (id: string) => void
draft: string
onDraft: (value: string) => void
onSend: (value: string) => void
live?: boolean
className?: string
}
const CSS = `
.aik-cs{--aik-r-shell:26px;--aik-rim:5px;width:min(100%,780px);container-type:inline-size}
.aik-cs-card{display:grid;grid-template-rows:auto 1fr auto;grid-template-columns:minmax(0,1fr)}
.aik-cs-tabs{display:flex;align-items:center;gap:4px;padding:12px 14px;border-bottom:1px solid var(--aik-line);overflow-x:auto;scrollbar-width:none}
.aik-cs-tabs button{display:inline-flex;align-items:center;gap:8px;height:34px;padding:0 12px;border-radius:10px;font-size:14.5px;color:var(--aik-text-3);white-space:nowrap;transition:background var(--aik-d-state),color var(--aik-d-state),transform var(--aik-d-press)}
.aik-cs-tabs button:hover{color:var(--aik-text)}
.aik-cs-tabs button:active{transform:scale(.96)}
.aik-cs-tabs button[aria-selected="true"]{background:var(--aik-raised);color:var(--aik-text)}
.aik-cs-live{margin-left:auto;display:inline-flex;align-items:center;gap:7px;padding-left:10px;font-size:14px;color:var(--aik-text-2)}
.aik-cs-live i{position:relative;width:7px;height:7px;border-radius:50%;background:var(--aik-blue)}
.aik-cs-live i::after{content:"";position:absolute;inset:0;border-radius:inherit;background:var(--aik-blue);animation:aik-cs-ping 1.6s var(--aik-ease-out) infinite}
@keyframes aik-cs-ping{from{opacity:.55;transform:scale(1)}to{opacity:0;transform:scale(3)}}
.aik-cs-feed{position:relative;height:470px;overflow:hidden;-webkit-mask:linear-gradient(transparent,#000 64px);mask:linear-gradient(transparent,#000 64px)}
.aik-cs-list{position:absolute;inset:auto 0 0 0;display:grid;gap:14px;padding:18px 18px 16px}
.aik-cs-sys{display:flex;align-items:center;gap:8px;padding-left:46px;font-size:14px;color:var(--aik-text-3);animation:aik-cs-in 420ms var(--aik-ease-soft) both}
.aik-cs-sys b{font-weight:500;color:var(--aik-text-2)}
.aik-cs-msg{display:grid;grid-template-columns:34px minmax(0,1fr);gap:12px;align-items:start;animation:aik-cs-in 520ms var(--aik-ease-soft) both}
.aik-cs-msg > span:first-child{display:grid;place-items:center;width:34px;height:34px;margin-top:6px;border-radius:10px;background:color-mix(in oklab,var(--c) 12%,var(--aik-card));box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--c) 24%,transparent)}
.aik-cs-bub{justify-self:start;display:grid;gap:10px;max-width:min(100%,580px);padding:13px 15px;border-radius:16px;background:var(--aik-card);box-shadow:var(--aik-sh-card)}
.aik-cs-msg[data-q] .aik-cs-bub{box-shadow:0 0 0 1px color-mix(in oklab,var(--aik-amber) 45%,transparent),0 10px 26px -14px color-mix(in oklab,var(--aik-amber) 60%,transparent)}
.aik-cs-msg[data-you]{grid-template-columns:minmax(0,1fr) 34px}
.aik-cs-msg[data-you] > span:first-child{grid-column:2;grid-row:1}
.aik-cs-msg[data-you] .aik-cs-bub{grid-column:1;grid-row:1;justify-self:end;background:var(--aik-pri);color:var(--aik-pri-ink);box-shadow:none}
.aik-cs-msg[data-you] .aik-cs-who time{color:color-mix(in oklab,var(--aik-pri-ink) 60%,transparent)}
.aik-cs-who{display:flex;align-items:center;gap:8px;font-size:15px}
.aik-cs-who b{font-weight:600}
.aik-cs-who time{margin-left:auto;padding-left:16px;font:13px var(--aik-mono);color:var(--aik-text-3)}
.aik-cs-tag{display:inline-flex;white-space:nowrap;flex:none;align-items:center;gap:5px;height:22px;padding:0 8px;border-radius:7px;font-size:12px;font-weight:500;color:var(--aik-amber);background:color-mix(in oklab,var(--aik-amber) 15%,transparent)}
.aik-cs-tag[data-k="handoff"]{color:var(--aik-blue);background:color-mix(in oklab,var(--aik-blue) 13%,transparent)}
.aik-cs-quote{display:grid;gap:3px;padding:9px 12px;border-radius:11px;background:var(--aik-sunk);box-shadow:inset 0 0 0 1px var(--aik-line);font-size:14px;color:var(--aik-text-2)}
.aik-cs-quote > span{display:flex;align-items:center;gap:6px;font-size:13px;color:var(--aik-text-3)}
.aik-cs-quote b{font-weight:500;color:var(--aik-text)}
.aik-cs-text{font-size:15px;line-height:23px}
.aik-cs-text .aik-cs-brand{display:inline-flex;align-items:center;gap:5px;vertical-align:-3px}
.aik-cs-text .aik-cs-brand > span{display:inline-grid;place-items:center;width:21px;height:21px;border-radius:6px;background:var(--aik-sunk);box-shadow:inset 0 0 0 1px var(--aik-line)}
.aik-cs-chips{display:flex;flex-wrap:wrap;gap:6px}
.aik-cs-chips span{display:inline-flex;align-items:center;gap:6px;height:30px;padding:0 11px;border-radius:9px;background:var(--aik-sunk);box-shadow:inset 0 0 0 1px var(--aik-line);font-size:13.5px;color:var(--aik-text-2)}
.aik-cs-mention{color:var(--aik-blue);font-weight:500}
.aik-cs-msg[data-you] .aik-cs-mention{color:inherit;text-decoration:underline;text-underline-offset:3px}
@keyframes aik-cs-in{from{opacity:0;transform:translateY(14px)}}
.aik-cs-comp{position:relative;padding:12px 14px 14px;border-top:1px solid var(--aik-line)}
.aik-cs-field{position:relative;display:flex;align-items:center;gap:8px;height:50px;padding:0 6px 0 14px;border-radius:15px;background:var(--aik-sunk);box-shadow:inset 0 0 0 1px var(--aik-line);transition:box-shadow var(--aik-d-state)}
.aik-cs-field:focus-within{box-shadow:inset 0 0 0 1.5px var(--aik-blue)}
.aik-cs-field > svg{color:var(--aik-text-3)}
.aik-cs-input{position:relative;flex:1;min-width:0;height:100%}
.aik-cs-input input,.aik-cs-input div{position:absolute;inset:0;display:flex;align-items:center;font-size:15px;white-space:pre;border:0;padding:0;margin:0;background:none;outline:none;letter-spacing:0}
.aik-cs-input input{color:transparent;caret-color:var(--aik-text);width:100%}
.aik-cs-input input::placeholder{color:var(--aik-text-3)}
.aik-cs-input div{pointer-events:none;overflow:hidden}
.aik-cs-send{display:grid;place-items:center;width:38px;height:38px;border-radius:11px;background:var(--aik-raised);color:var(--aik-text-3);transition:background var(--aik-d-state),color var(--aik-d-state),transform var(--aik-d-press)}
.aik-cs-send[data-on]{background:var(--aik-pri);color:var(--aik-pri-ink)}
.aik-cs-send:active{transform:scale(.92)}
.aik-cs-pop{position:absolute;left:14px;bottom:calc(100% - 4px);width:min(280px,calc(100% - 28px));padding:6px;border-radius:14px;background:var(--aik-card);box-shadow:var(--aik-sh-pop);transform-origin:bottom left;animation:aik-cs-pop 240ms var(--aik-ease-out);z-index:2}
.aik-cs-pop p{padding:6px 8px 4px;font-size:12px;color:var(--aik-text-3)}
.aik-cs-pop button{display:flex;align-items:center;gap:10px;width:100%;height:38px;padding:0 9px;border-radius:9px;font-size:14.5px;text-align:left}
.aik-cs-pop button[data-active]{background:var(--aik-hover)}
@keyframes aik-cs-pop{from{opacity:0;transform:translateY(6px) scale(.97)}}
@container (max-width:520px){
.aik-cs-feed{height:430px}
.aik-cs-list{padding:14px 10px 12px}
.aik-cs-msg{grid-template-columns:28px minmax(0,1fr);gap:9px}
.aik-cs-msg[data-you]{grid-template-columns:minmax(0,1fr) 28px}
.aik-cs-msg > span:first-child{width:28px;height:28px}
.aik-cs-sys{padding-left:37px;font-size:13px}
.aik-cs-text{font-size:14px;line-height:21px}
.aik-cs-live span{display:none}
.aik-cs-tabs button{padding:0 10px}
.aik-cs-tabs button:not([aria-selected="true"]) .aik-cs-tn{display:none}
.aik-cs-who time{display:none}
}
@media (prefers-reduced-motion: reduce){.aik-cs-msg,.aik-cs-sys,.aik-cs-pop,.aik-cs-live i::after{animation:none}}
`
/** Splits text into plain runs and @mentions of known members. */
function withMentions(text: string, names: readonly string[]) {
if (!names.length) return [text]
const escaped = names.map((n) => n.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
const pattern = new RegExp(`(@(?:${escaped.join("|")}))`, "g")
return text.split(pattern).map((part, i) =>
part.startsWith("@") && names.includes(part.slice(1)) ? (
<span key={i} className="aik-cs-mention">
{part}
</span>
) : (
part
)
)
}
/**
* What agents say to each other, as a quiet chat: who each message is from
* and for, hand-offs and questions for you tagged, tool calls and files as
* chips, a filter per member, and a composer that completes @mentions.
*/
export function CollaborationStream({
members,
messages,
filter,
onFilter,
draft,
onDraft,
onSend,
live = true,
className,
}: CollaborationStreamProps) {
const byId = new Map(members.map((m) => [m.id, m]))
const names = members.map((m) => m.name)
const [active, setActive] = React.useState(0)
// An "@word" at the end of the draft opens the mention menu.
const query = draft.match(/(^|\s)@(\w*)$/)?.[2]
const matches =
query === undefined
? []
: members.filter((m) =>
m.name.toLowerCase().startsWith(query.toLowerCase())
)
const pick = (member: StreamMember) => {
onDraft(draft.replace(/@(\w*)$/, `@${member.name} `))
setActive(0)
}
const shown = messages.filter(
(m) => filter === "all" || m.from === filter || m.to === filter
)
const avatar = (member: StreamMember | undefined, size: number) =>
member?.kind === "person" ? (
<Face
shape={member.shape ?? "squircle"}
tone={member.tone}
size={size + 6}
mood="idle"
/>
) : (
<AgentGlyph
seed={member?.id ?? "x"}
tone={member?.tone ?? "ink"}
size={size}
/>
)
return (
<div className={cx("aik aik-cs aik-shell", className)}>
<AiKitStyles />
<AiStyle id="collaboration-stream" css={CSS} />
<div className="aik-surface aik-cs-card">
<div
className="aik-cs-tabs"
role="tablist"
aria-label="Filter messages"
>
<button
type="button"
role="tab"
aria-selected={filter === "all"}
onClick={() => onFilter("all")}
>
Everyone
</button>
{members.map((member) => (
<button
key={member.id}
type="button"
role="tab"
aria-selected={filter === member.id}
aria-label={member.name}
onClick={() => onFilter(member.id)}
>
<AgentGlyph seed={member.id} tone={member.tone} size={15} />
<span className="aik-cs-tn">{member.name}</span>
</button>
))}
{live ? (
<span className="aik-cs-live">
<i />
<span>Live</span>
</span>
) : null}
</div>
<div className="aik-cs-feed" aria-live="polite">
<ol className="aik-cs-list">
{shown.slice(-8).map((message) => {
const from = byId.get(message.from)
const to = message.to ? byId.get(message.to) : undefined
if (message.kind === "system") {
return (
<li key={message.id} className="aik-cs-sys">
{message.parts.map((part, i) =>
typeof part === "string" ? (
<React.Fragment key={i}>{part}</React.Fragment>
) : (
<b key={i}>{part.label}</b>
)
)}
</li>
)
}
const you = from?.kind === "person"
return (
<li
key={message.id}
className="aik-cs-msg"
data-you={you ? "" : undefined}
data-q={message.tag === "question" ? "" : undefined}
style={
{
"--c": `var(--aik-f-${from?.tone ?? "ink"})`,
} as React.CSSProperties
}
>
<span>{avatar(from, 16)}</span>
<div className="aik-cs-bub">
<div className="aik-cs-who">
<b>{from?.name}</b>
{message.tag ? (
<span className="aik-cs-tag" data-k={message.tag}>
{message.tag === "question" ? (
<MessageCircleQuestion size={13} />
) : (
<Forward size={13} />
)}
{message.tag === "question"
? "Question for you"
: "Handoff"}
</span>
) : null}
{message.time ? <time>{message.time}</time> : null}
</div>
{message.quote ? (
<div className="aik-cs-quote">
<span>
to{" "}
<AgentGlyph
seed={to?.id ?? "x"}
tone={to?.tone ?? "ink"}
size={12}
/>
<b>{to?.name}</b>
</span>
{message.quote}
</div>
) : null}
<p className="aik-cs-text">
{message.parts.map((part, i) =>
typeof part === "string" ? (
<React.Fragment key={i}>
{withMentions(part, names)}
</React.Fragment>
) : (
<span key={i} className="aik-cs-brand">
<span>
<BrandIcon
name={part.brand}
tint="brand"
size={13}
/>
</span>
{part.label}
</span>
)
)}
</p>
{message.chips?.length ? (
<div className="aik-cs-chips">
{message.chips.map((chip) => (
<span key={chip.label}>
{chip.kind === "tool" ? (
<Code2 size={14} />
) : (
<FileText size={14} />
)}
{chip.label}
</span>
))}
</div>
) : null}
</div>
</li>
)
})}
</ol>
</div>
<form
className="aik-cs-comp"
onSubmit={(event) => {
event.preventDefault()
if (draft.trim()) onSend(draft.trim())
}}
>
{matches.length ? (
<div className="aik-cs-pop" role="listbox" aria-label="Mention">
<p>Mention</p>
{matches.map((member, i) => (
<button
key={member.id}
type="button"
role="option"
aria-selected={i === active}
data-active={i === active ? "" : undefined}
onMouseEnter={() => setActive(i)}
onClick={() => pick(member)}
>
{avatar(member, 15)}
{member.name}
</button>
))}
</div>
) : null}
<div className="aik-cs-field">
<AtSign size={17} aria-hidden="true" />
<div className="aik-cs-input">
<div aria-hidden="true">{withMentions(draft, names)}</div>
<input
value={draft}
placeholder="Message the team, @ to mention"
aria-label="Message"
onChange={(event) => onDraft(event.target.value)}
onKeyDown={(event) => {
if (!matches.length) return
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
event.preventDefault()
setActive(
(i) =>
(i +
(event.key === "ArrowDown"
? 1
: matches.length - 1)) %
matches.length
)
} else if (event.key === "Enter" || event.key === "Tab") {
event.preventDefault()
pick(matches[active] ?? matches[0])
}
}}
/>
</div>
<button
type="submit"
className="aik-cs-send"
data-on={draft.trim() ? "" : undefined}
aria-label="Send"
>
<ArrowUp size={18} />
</button>
</div>
</form>
</div>
</div>
)
}
Installation
One command adds collaboration-stream 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/collaboration-stream.json