{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "agent-review-02",
  "title": "Code review with agent replies",
  "description": "A file diff where a comment on a line gets a reply with a suggested change: Apply suggestion becomes Applied in a commit, the new lines light up in the file and the thread folds once resolved.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/agent-run.json",
    "https://ui.ndekocode.dev/r/avatar.json",
    "https://ui.ndekocode.dev/r/brand-tile.json",
    "https://ui.ndekocode.dev/r/button.json",
    "https://ui.ndekocode.dev/r/chat-motion.json",
    "https://ui.ndekocode.dev/r/demonstration.json",
    "https://ui.ndekocode.dev/r/drawn-check.json",
    "https://ui.ndekocode.dev/r/icons.json",
    "https://ui.ndekocode.dev/r/live-duration.json",
    "https://ui.ndekocode.dev/r/live-region.json",
    "https://ui.ndekocode.dev/r/mesh-tile.json",
    "https://ui.ndekocode.dev/r/morph-button.json",
    "https://ui.ndekocode.dev/r/motion-number.json",
    "https://ui.ndekocode.dev/r/motion-presets.json",
    "https://ui.ndekocode.dev/r/replay-button.json",
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/use-prefers-reduced-motion.json",
    "https://ui.ndekocode.dev/r/use-session.json",
    "https://ui.ndekocode.dev/r/use-timeouts.json",
    "https://ui.ndekocode.dev/r/utils.json"
  ],
  "files": [
    {
      "path": "registry/ndeko/blocks/agent-review/agent-review-02/index.tsx",
      "content": "'use client';\n\nimport { AnimatePresence, motion } from 'motion/react';\n\nimport { MotionNumber } from '@/components/core/motion-number';\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\nimport { useSession } from '@/registry/ndeko/hooks/use-session';\nimport { useTimeouts } from '@/registry/ndeko/hooks/use-timeouts';\nimport {\n  pressedSolidOf,\n  spokenUnless,\n} from '@/registry/ndeko/lib/demonstration';\nimport { FileIcon, PlusIcon } from '@/registry/ndeko/lib/icons';\nimport { rowPresenceOf } from '@/registry/ndeko/lib/motion-presets';\nimport { cn } from '@/registry/ndeko/lib/utils';\nimport { BrandTile } from '@/registry/ndeko/ui/brand-tile';\nimport { Button } from '@/registry/ndeko/ui/button';\nimport { ClockGate } from '@/registry/ndeko/ui/live-duration';\nimport { LiveRegion } from '@/registry/ndeko/ui/live-region';\nimport { ReplayButton } from '@/registry/ndeko/ui/replay-button';\n\nimport {\n  countOfKind,\n  fileLinesOf,\n  GUARD_KEYS,\n  numbered,\n  PULL_REQUEST,\n  type LineKind,\n  type NumberedLine,\n} from './data';\nimport {\n  APPLY_MS,\n  hasGuardOf,\n  INITIAL,\n  LOOP_AFTER_MS,\n  reducer,\n  REPLY_MS,\n  RESTING,\n  SCRIPT,\n  type ReviewState,\n} from './session';\nimport { ReviewThread } from './thread';\n\nconst SESSION = {\n  reducer,\n  initial: INITIAL,\n  resting: RESTING,\n  script: SCRIPT,\n  loopAfter: LOOP_AFTER_MS,\n};\n\nconst SIGN: Record<LineKind, string> = { same: ' ', added: '+', removed: '-' };\n\nconst LINE_TINT: Record<LineKind, string> = {\n  same: '',\n  added: 'bg-success/10',\n  removed: 'bg-destructive/10',\n};\n\nconst LINE_WORD: Record<LineKind, string> = {\n  same: '',\n  added: 'Added: ',\n  removed: 'Removed: ',\n};\n\nfunction openCountOf(state: ReviewState) {\n  const open = state.threads.filter((thread) => !thread.isResolved);\n  return open.length;\n}\n\nfunction conversationsOf(count: number) {\n  if (count === 0) {\n    return 'Every conversation is resolved';\n  }\n  if (count === 1) {\n    return '1 open conversation';\n  }\n  return `${count} open conversations`;\n}\n\nfunction plusClassOf(isPressed: boolean) {\n  if (isPressed) {\n    return 'scale-90 opacity-100';\n  }\n  return 'opacity-0';\n}\n\n/** The lines a suggestion just added light for a moment, then keep the tint of an added line. */\nfunction freshClassOf(isFresh: boolean) {\n  if (isFresh) {\n    return 'transition-colors duration-1000 ease-out starting:bg-primary/30';\n  }\n  return undefined;\n}\n\nfunction lineNumberOf(line: NumberedLine) {\n  return line.newNumber ?? line.oldNumber;\n}\n\ntype DiffRowProps = {\n  line: NumberedLine;\n  canComment: boolean;\n  isPlusPressed: boolean;\n  onOpen: (lineKey: string) => void;\n};\n\nfunction DiffRow({ line, canComment, isPlusPressed, onOpen }: DiffRowProps) {\n  const isFresh = GUARD_KEYS.has(line.key);\n  const number = lineNumberOf(line);\n\n  return (\n    <div\n      className={cn(\n        'group/row flex items-start font-mono text-xs leading-6',\n        LINE_TINT[line.kind],\n        freshClassOf(isFresh)\n      )}\n    >\n      <span className='flex h-6 w-7 shrink-0 items-center justify-center'>\n        {canComment && (\n          <button\n            type='button'\n            aria-label={`Comment on line ${number}`}\n            onClick={() => onOpen(line.key)}\n            className={cn(\n              'bg-primary text-primary-foreground flex size-5 cursor-pointer items-center justify-center rounded-md transition-[opacity,scale] duration-150 outline-none group-hover/row:opacity-100 focus-visible:opacity-100 active:scale-[0.97] [@media(hover:none)]:opacity-60',\n              plusClassOf(isPlusPressed)\n            )}\n          >\n            <PlusIcon className='size-3.5' />\n          </button>\n        )}\n      </span>\n      <span className='text-muted-foreground/70 hidden w-8 shrink-0 text-end tabular-nums select-none @md:block'>\n        {line.oldNumber}\n      </span>\n      <span className='text-muted-foreground/70 w-8 shrink-0 text-end tabular-nums select-none'>\n        {line.newNumber}\n      </span>\n      <span\n        aria-hidden='true'\n        className='text-muted-foreground w-6 shrink-0 text-center select-none'\n      >\n        {SIGN[line.kind]}\n      </span>\n      <span className='sr-only'>{LINE_WORD[line.kind]}</span>\n      <code className='min-w-0 flex-1 pe-3 break-all whitespace-pre-wrap'>\n        {line.text}\n      </code>\n    </div>\n  );\n}\n\ntype CommentFieldProps = {\n  draft: string;\n  shouldFocus: boolean;\n  isPostPressed: boolean;\n  onChange: (text: string) => void;\n  onPost: () => void;\n  onCancel: () => void;\n};\n\nfunction CommentField({\n  draft,\n  shouldFocus,\n  isPostPressed,\n  onChange,\n  onPost,\n  onCancel,\n}: CommentFieldProps) {\n  const canPost = draft.trim() !== '';\n\n  function handleKeys(event: React.KeyboardEvent<HTMLTextAreaElement>) {\n    if (event.key === 'Escape') {\n      onCancel();\n      return;\n    }\n    const hasModifier = event.metaKey || event.ctrlKey;\n    const isPostKey = event.key === 'Enter' && hasModifier;\n    if (isPostKey && canPost) {\n      onPost();\n    }\n  }\n\n  return (\n    <div className='bg-card hairline-ring ring-foreground/12 has-[textarea:focus-visible]:ring-ring/25 dark:bg-foreground/[0.04] flex flex-col gap-2 rounded-lg p-2 font-sans shadow-[0_1px_2px_rgb(0_0_0/0.05)] transition-[box-shadow] duration-150 has-[textarea:focus-visible]:ring-[3px]'>\n      <textarea\n        value={draft}\n        rows={2}\n        autoFocus={shouldFocus}\n        aria-label='Comment on this line'\n        placeholder='Leave a comment for Coder'\n        onChange={(event) => onChange(event.target.value)}\n        onKeyDown={handleKeys}\n        className='placeholder:text-muted-foreground/75 resize-none bg-transparent px-1.5 py-1 text-base leading-normal outline-none md:text-[13px]'\n      />\n      <div className='flex items-center justify-end gap-2'>\n        <Button type='button' variant='ghost' size='sm' onClick={onCancel}>\n          Cancel\n        </Button>\n        <Button\n          type='button'\n          size='sm'\n          disabled={!canPost}\n          onClick={onPost}\n          className={pressedSolidOf(isPostPressed)}\n        >\n          Post comment\n        </Button>\n      </div>\n    </div>\n  );\n}\n\nexport default function AgentReview02() {\n  const {\n    ref,\n    isInView,\n    isAutoplaying,\n    state,\n    act,\n    pressed,\n    isFinished,\n    restart,\n  } = useSession(SESSION);\n  const { schedule } = useTimeouts();\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const rowPresence = rowPresenceOf(shouldReduceMotion);\n  const hasGuard = hasGuardOf(state);\n  const fileLines = fileLinesOf(hasGuard);\n  const lines = numbered(fileLines);\n  const additions = countOfKind(fileLines, 'added');\n  const deletions = countOfKind(fileLines, 'removed');\n  const openCount = openCountOf(state);\n  const conversations = conversationsOf(openCount);\n  const message = spokenUnless(isAutoplaying, conversations);\n  const canReplay = isFinished && !isAutoplaying;\n\n  function post() {\n    const lineKey = state.openLine;\n    act({ type: 'posted' });\n    if (lineKey !== null) {\n      schedule(() => act({ type: 'replied', lineKey }), REPLY_MS);\n    }\n  }\n\n  function apply(lineKey: string) {\n    act({ type: 'applying', lineKey });\n    schedule(() => act({ type: 'applied', lineKey }), APPLY_MS);\n  }\n\n  return (\n    <ClockGate isActive={isInView}>\n      <div\n        ref={ref}\n        className='bg-background text-foreground @container min-h-dvh w-full p-3 @3xl:p-8'\n      >\n        <div className='mx-auto flex max-w-3xl flex-col gap-4'>\n          <header className='flex flex-wrap items-center gap-x-3 gap-y-2'>\n            <BrandTile brand='github' size='lg' title='GitHub' />\n            <div className='flex min-w-40 flex-1 flex-col leading-tight'>\n              <h1 className='text-sm font-medium'>{PULL_REQUEST.title}</h1>\n              <span className='text-muted-foreground text-xs'>\n                {conversations}\n              </span>\n            </div>\n            <ReplayButton\n              isShown={canReplay}\n              onReplay={restart}\n              label='Replay the review'\n            />\n          </header>\n\n          <section\n            aria-label={`Changes in ${PULL_REQUEST.file}`}\n            className='bg-card hairline-ring ring-border dark:bg-foreground/[0.03] overflow-hidden rounded-xl shadow-[0_1px_2px_rgb(0_0_0/0.04)]'\n          >\n            <div className='flex h-10 items-center gap-2 px-3'>\n              <FileIcon className='text-muted-foreground size-4 shrink-0' />\n              <span className='min-w-0 flex-1 truncate font-mono text-xs'>\n                {PULL_REQUEST.file}\n              </span>\n              <span className='flex items-center gap-2 text-xs font-medium'>\n                <MotionNumber\n                  value={additions}\n                  prefix='+'\n                  className='text-success-foreground'\n                />\n                <MotionNumber\n                  value={deletions}\n                  prefix='-'\n                  className='text-destructive-ink'\n                />\n              </span>\n            </div>\n            <ol className='pb-2'>\n              {lines.map((line) => {\n                const thread = state.threads.find(\n                  (candidate) => candidate.lineKey === line.key\n                );\n                const isFieldOpen = state.openLine === line.key;\n                const canComment = thread === undefined;\n                return (\n                  <li key={line.key}>\n                    <DiffRow\n                      line={line}\n                      canComment={canComment}\n                      isPlusPressed={pressed === `plus-${line.key}`}\n                      onOpen={(lineKey) => act({ type: 'opened', lineKey })}\n                    />\n                    <AnimatePresence initial={false}>\n                      {isFieldOpen && (\n                        <motion.div\n                          key='field'\n                          className='overflow-hidden'\n                          {...rowPresence}\n                        >\n                          <div className='p-2 @md:ps-9'>\n                            <CommentField\n                              draft={state.draft}\n                              shouldFocus={!isAutoplaying}\n                              isPostPressed={pressed === 'post'}\n                              onChange={(text) => act({ type: 'typed', text })}\n                              onPost={post}\n                              onCancel={() => act({ type: 'closed' })}\n                            />\n                          </div>\n                        </motion.div>\n                      )}\n                      {thread && (\n                        <motion.div\n                          key='thread'\n                          className='overflow-hidden'\n                          {...rowPresence}\n                        >\n                          <div className='p-2 @md:ps-9'>\n                            <ReviewThread\n                              thread={thread}\n                              pressed={pressed}\n                              onApply={apply}\n                              onResolve={(lineKey) =>\n                                act({ type: 'resolved', lineKey })\n                              }\n                              onToggleFold={(lineKey) =>\n                                act({ type: 'folded', lineKey })\n                              }\n                            />\n                          </div>\n                        </motion.div>\n                      )}\n                    </AnimatePresence>\n                  </li>\n                );\n              })}\n            </ol>\n          </section>\n        </div>\n        <LiveRegion message={message} />\n      </div>\n    </ClockGate>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/blocks/agent-review-02/index.tsx"
    },
    {
      "path": "registry/ndeko/blocks/agent-review/agent-review-02/thread.tsx",
      "content": "'use client';\n\nimport { seriesColorOf } from '@/registry/ndeko/lib/agent-run';\nimport { pressedSolidOf } from '@/registry/ndeko/lib/demonstration';\nimport { ChevronDownIcon, CircleCheckIcon } from '@/registry/ndeko/lib/icons';\nimport { cn } from '@/registry/ndeko/lib/utils';\nimport { Avatar } from '@/registry/ndeko/ui/avatar';\nimport { Button } from '@/registry/ndeko/ui/button';\nimport { Fold, Swap } from '@/registry/ndeko/ui/chat-motion';\nimport { DrawnCheck } from '@/registry/ndeko/ui/drawn-check';\nimport { MeshTile } from '@/registry/ndeko/ui/mesh-tile';\nimport { MorphButton, type MorphState } from '@/registry/ndeko/ui/morph-button';\n\nimport {\n  CODER,\n  NOTE_REPLY,\n  OLD_REPLY,\n  OLD_THREAD_LINE,\n  PULL_REQUEST,\n  REVIEWER,\n  SUGGESTION,\n  SUGGESTION_REPLY,\n  type LineKind,\n} from './data';\nimport type { ApplyStatus, Thread } from './session';\n\nconst APPLY_LABELS: Record<MorphState, string> = {\n  idle: 'Apply suggestion',\n  busy: 'Applying',\n  done: 'Applied',\n};\n\nconst SIGN: Record<LineKind, string> = { same: ' ', added: '+', removed: '-' };\n\nconst LINE_TINT: Record<LineKind, string> = {\n  same: '',\n  added: 'bg-success/10',\n  removed: 'bg-destructive/10',\n};\n\nfunction CoderTile() {\n  const color = seriesColorOf(CODER.series);\n\n  return (\n    <MeshTile seed={CODER.name} color={color} className='size-6 rounded-md' />\n  );\n}\n\nfunction TypingDots() {\n  return (\n    <span className='flex h-6 items-center gap-1' role='status'>\n      <span className='sr-only'>Coder is writing a reply</span>\n      <span className='bg-muted-foreground size-1.5 animate-pulse rounded-full' />\n      <span className='bg-muted-foreground size-1.5 animate-pulse rounded-full [animation-delay:160ms]' />\n      <span className='bg-muted-foreground size-1.5 animate-pulse rounded-full [animation-delay:320ms]' />\n    </span>\n  );\n}\n\nfunction morphStateOf(apply: ApplyStatus): MorphState {\n  if (apply === 'busy') {\n    return 'busy';\n  }\n  return 'idle';\n}\n\nfunction pressedQuietOf(isPressed: boolean) {\n  if (isPressed) {\n    return 'scale-[0.94] bg-foreground/[0.07]';\n  }\n  return undefined;\n}\n\ntype SuggestionBlockProps = {\n  apply: ApplyStatus;\n  isPressed: boolean;\n  onApply: () => void;\n};\n\nfunction SuggestionBlock({ apply, isPressed, onApply }: SuggestionBlockProps) {\n  const isApplied = apply === 'done';\n\n  return (\n    <div className='bg-foreground/[0.04] flex flex-col gap-2 rounded-lg p-2'>\n      <span className='text-muted-foreground px-1 text-xs'>\n        Suggested change\n      </span>\n      <ol className='bg-card dark:bg-foreground/[0.04] overflow-hidden rounded-md py-1 font-mono text-xs leading-5'>\n        {SUGGESTION.map((line) => (\n          <li\n            key={line.key}\n            className={cn('flex gap-2 px-2', LINE_TINT[line.kind])}\n          >\n            <span\n              aria-hidden='true'\n              className='text-muted-foreground w-3 shrink-0 select-none'\n            >\n              {SIGN[line.kind]}\n            </span>\n            <code className='break-all whitespace-pre-wrap'>{line.text}</code>\n          </li>\n        ))}\n      </ol>\n      <div className='flex min-h-7 items-center px-1'>\n        <Swap swapKey={String(isApplied)}>\n          {!isApplied && (\n            <MorphButton\n              type='button'\n              size='sm'\n              state={morphStateOf(apply)}\n              labels={APPLY_LABELS}\n              onClick={onApply}\n              className={pressedSolidOf(isPressed)}\n            />\n          )}\n          {isApplied && (\n            <span className='text-success-foreground flex items-center gap-1.5 text-xs font-medium'>\n              <DrawnCheck tone='success' entrance='draw' />\n              Applied in\n              <code className='font-mono'>{PULL_REQUEST.commit}</code>\n            </span>\n          )}\n        </Swap>\n      </div>\n    </div>\n  );\n}\n\nfunction replyTextOf(thread: Thread) {\n  if (thread.lineKey === OLD_THREAD_LINE) {\n    return OLD_REPLY;\n  }\n  if (thread.reply === 'suggestion') {\n    return SUGGESTION_REPLY;\n  }\n  return NOTE_REPLY;\n}\n\nfunction chevronOf(isFolded: boolean) {\n  if (isFolded) {\n    return 'rotate-0';\n  }\n  return 'rotate-180';\n}\n\ntype ReviewThreadProps = {\n  thread: Thread;\n  pressed: string | null;\n  onApply: (lineKey: string) => void;\n  onResolve: (lineKey: string) => void;\n  onToggleFold: (lineKey: string) => void;\n};\n\nexport function ReviewThread({\n  thread,\n  pressed,\n  onApply,\n  onResolve,\n  onToggleFold,\n}: ReviewThreadProps) {\n  const isTyping = thread.reply === 'typing';\n  const hasSuggestion = thread.reply === 'suggestion';\n  const isOpen = !thread.isFolded;\n\n  return (\n    <div className='bg-card hairline-ring ring-border dark:bg-foreground/[0.04] rounded-lg font-sans shadow-[0_1px_2px_rgb(0_0_0/0.04)]'>\n      {thread.isResolved && (\n        <button\n          type='button'\n          aria-expanded={isOpen}\n          onClick={() => onToggleFold(thread.lineKey)}\n          className='hover:bg-foreground/[0.03] focus-visible:ring-ring/25 flex h-9 w-full cursor-pointer items-center gap-2 rounded-lg px-3 text-start text-xs outline-none focus-visible:ring-[3px]'\n        >\n          <CircleCheckIcon className='text-success-foreground size-4 shrink-0' />\n          <span className='font-medium'>Resolved conversation</span>\n          <span className='text-muted-foreground'>2 comments</span>\n          <ChevronDownIcon\n            className={cn(\n              'text-muted-foreground ms-auto size-4 transition-transform duration-200 ease-(--ease-out-strong)',\n              chevronOf(thread.isFolded)\n            )}\n          />\n        </button>\n      )}\n      <Fold open={isOpen}>\n        <div className='flex flex-col gap-3 p-3'>\n          <div className='flex gap-2.5'>\n            <Avatar className='size-6' seed={REVIEWER.name} />\n            <div className='flex min-w-0 flex-col'>\n              <span className='text-xs leading-6 font-medium'>\n                {REVIEWER.name}\n              </span>\n              <p className='text-[13px] leading-relaxed'>{thread.comment}</p>\n            </div>\n          </div>\n          <div className='flex gap-2.5'>\n            <CoderTile />\n            <div className='flex min-w-0 flex-1 flex-col gap-2'>\n              <span className='text-xs leading-6 font-medium'>\n                {CODER.name}\n              </span>\n              {isTyping && <TypingDots />}\n              {!isTyping && (\n                <p className='text-[13px] leading-relaxed transition-opacity duration-300 starting:opacity-0'>\n                  {replyTextOf(thread)}\n                </p>\n              )}\n              {hasSuggestion && (\n                <SuggestionBlock\n                  apply={thread.apply}\n                  isPressed={pressed === 'apply'}\n                  onApply={() => onApply(thread.lineKey)}\n                />\n              )}\n            </div>\n          </div>\n          {!thread.isResolved && (\n            <div className='flex justify-end'>\n              <Button\n                type='button'\n                variant='outline'\n                size='sm'\n                disabled={isTyping}\n                onClick={() => onResolve(thread.lineKey)}\n                className={pressedQuietOf(pressed === 'resolve')}\n              >\n                Resolve conversation\n              </Button>\n            </div>\n          )}\n        </div>\n      </Fold>\n    </div>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/blocks/agent-review-02/thread.tsx"
    },
    {
      "path": "registry/ndeko/blocks/agent-review/agent-review-02/session.ts",
      "content": "import type { SessionStep } from '@/registry/ndeko/hooks/use-session';\n\nimport {\n  DEMO_COMMENT,\n  OLD_COMMENT,\n  OLD_THREAD_LINE,\n  TARGET_LINE,\n} from './data';\n\nexport type ReplyStatus = 'typing' | 'suggestion' | 'note';\n\nexport type ApplyStatus = 'idle' | 'busy' | 'done';\n\nexport type Thread = {\n  lineKey: string;\n  comment: string;\n  reply: ReplyStatus;\n  apply: ApplyStatus;\n  isResolved: boolean;\n  isFolded: boolean;\n};\n\nexport type ReviewState = {\n  /** The line whose comment field is open. */\n  openLine: string | null;\n  draft: string;\n  threads: Thread[];\n};\n\nexport type ReviewAction =\n  | { type: 'opened'; lineKey: string }\n  | { type: 'closed' }\n  | { type: 'typed'; text: string }\n  | { type: 'posted' }\n  | { type: 'replied'; lineKey: string }\n  | { type: 'applying'; lineKey: string }\n  | { type: 'applied'; lineKey: string }\n  | { type: 'resolved'; lineKey: string }\n  | { type: 'folded'; lineKey: string };\n\ntype Step = SessionStep<ReviewState, ReviewAction>;\n\nconst OLD_THREAD: Thread = {\n  lineKey: OLD_THREAD_LINE,\n  comment: OLD_COMMENT,\n  reply: 'note',\n  apply: 'idle',\n  isResolved: true,\n  isFolded: true,\n};\n\nexport const INITIAL: ReviewState = {\n  openLine: null,\n  draft: '',\n  threads: [OLD_THREAD],\n};\n\nexport function threadOf(state: ReviewState, lineKey: string) {\n  return state.threads.find((thread) => thread.lineKey === lineKey);\n}\n\nexport function hasGuardOf(state: ReviewState) {\n  const target = threadOf(state, TARGET_LINE);\n  return target?.apply === 'done';\n}\n\nfunction patched(\n  state: ReviewState,\n  lineKey: string,\n  change: (thread: Thread) => Thread\n): ReviewState {\n  const threads = state.threads.map((thread) => {\n    if (thread.lineKey === lineKey) {\n      return change(thread);\n    }\n    return thread;\n  });\n  return { ...state, threads };\n}\n\nfunction commentOf(state: ReviewState) {\n  const trimmed = state.draft.trim();\n  if (trimmed === '') {\n    return DEMO_COMMENT;\n  }\n  return trimmed;\n}\n\nfunction posted(state: ReviewState): ReviewState {\n  const lineKey = state.openLine;\n  if (lineKey === null) {\n    return state;\n  }\n  const thread: Thread = {\n    lineKey,\n    comment: commentOf(state),\n    reply: 'typing',\n    apply: 'idle',\n    isResolved: false,\n    isFolded: false,\n  };\n  return {\n    openLine: null,\n    draft: '',\n    threads: [...state.threads, thread],\n  };\n}\n\nfunction replyFor(lineKey: string): ReplyStatus {\n  if (lineKey === TARGET_LINE) {\n    return 'suggestion';\n  }\n  return 'note';\n}\n\nfunction replied(thread: Thread): Thread {\n  if (thread.reply !== 'typing') {\n    return thread;\n  }\n  return { ...thread, reply: replyFor(thread.lineKey) };\n}\n\nfunction applying(thread: Thread): Thread {\n  const canApply = thread.reply === 'suggestion' && thread.apply === 'idle';\n  if (!canApply) {\n    return thread;\n  }\n  return { ...thread, apply: 'busy' };\n}\n\nfunction applied(thread: Thread): Thread {\n  if (thread.apply !== 'busy') {\n    return thread;\n  }\n  return { ...thread, apply: 'done' };\n}\n\nfunction resolved(thread: Thread): Thread {\n  if (thread.reply === 'typing') {\n    return thread;\n  }\n  return { ...thread, isResolved: true, isFolded: true };\n}\n\nfunction folded(thread: Thread): Thread {\n  return { ...thread, isFolded: !thread.isFolded };\n}\n\nfunction opened(state: ReviewState, lineKey: string): ReviewState {\n  const hasThread = threadOf(state, lineKey) !== undefined;\n  if (hasThread) {\n    return state;\n  }\n  return { ...state, openLine: lineKey, draft: '' };\n}\n\nexport function reducer(state: ReviewState, action: ReviewAction): ReviewState {\n  switch (action.type) {\n    case 'opened':\n      return opened(state, action.lineKey);\n    case 'closed':\n      return { ...state, openLine: null, draft: '' };\n    case 'typed':\n      return { ...state, draft: action.text };\n    case 'posted':\n      return posted(state);\n    case 'replied':\n      return patched(state, action.lineKey, replied);\n    case 'applying':\n      return patched(state, action.lineKey, applying);\n    case 'applied':\n      return patched(state, action.lineKey, applied);\n    case 'resolved':\n      return patched(state, action.lineKey, resolved);\n    case 'folded':\n      return patched(state, action.lineKey, folded);\n  }\n}\n\nconst CHUNK = 2;\n\nfunction delayOfChunk(index: number) {\n  if (index === 0) {\n    return 700;\n  }\n  return 60;\n}\n\nfunction typingSteps(): Step[] {\n  const steps: Step[] = [];\n  const last = DEMO_COMMENT.length + CHUNK;\n  for (let end = CHUNK; end < last; end += CHUNK) {\n    const text = DEMO_COMMENT.slice(0, end);\n    const after = delayOfChunk(steps.length);\n    steps.push({ after, by: 'demo', action: { type: 'typed', text } });\n  }\n  return steps;\n}\n\nfunction hasPosted(state: ReviewState) {\n  return threadOf(state, TARGET_LINE) !== undefined;\n}\n\nfunction hasPressedApply(state: ReviewState) {\n  const target = threadOf(state, TARGET_LINE);\n  const apply = target?.apply ?? 'idle';\n  return apply !== 'idle';\n}\n\nexport const REPLY_MS = 1700;\n\nexport const APPLY_MS = 900;\n\n/** A request that waits on a person is answered by the demonstration after this long. */\nconst DECIDE_AFTER_MS = 2500;\n\nexport const SCRIPT: Step[] = [\n  {\n    after: 1200,\n    by: 'demo',\n    press: `plus-${TARGET_LINE}`,\n    action: { type: 'opened', lineKey: TARGET_LINE },\n  },\n  ...typingSteps(),\n  {\n    after: 500,\n    by: 'person',\n    press: 'post',\n    action: { type: 'posted' },\n    isAnswered: hasPosted,\n  },\n  { after: REPLY_MS, action: { type: 'replied', lineKey: TARGET_LINE } },\n  {\n    after: DECIDE_AFTER_MS,\n    by: 'person',\n    press: 'apply',\n    action: { type: 'applying', lineKey: TARGET_LINE },\n    isAnswered: hasPressedApply,\n  },\n  { after: APPLY_MS, action: { type: 'applied', lineKey: TARGET_LINE } },\n  {\n    after: 2200,\n    by: 'demo',\n    press: 'resolve',\n    action: { type: 'resolved', lineKey: TARGET_LINE },\n  },\n];\n\nfunction restingOf() {\n  let state = INITIAL;\n  for (const step of SCRIPT) {\n    state = reducer(state, step.action);\n  }\n  const unfolded = reducer(state, { type: 'folded', lineKey: TARGET_LINE });\n  return unfolded;\n}\n\n/** Under reduced motion the thread rests open, so the applied suggestion can be read. */\nexport const RESTING = restingOf();\n\nexport const LOOP_AFTER_MS = 3200;\n",
      "type": "registry:component",
      "target": "components/blocks/agent-review-02/session.ts"
    },
    {
      "path": "registry/ndeko/blocks/agent-review/agent-review-02/data.ts",
      "content": "import type { SeriesSlot } from '@/registry/ndeko/lib/agent-run';\n\nexport const PULL_REQUEST = {\n  title: 'Compute the average order value',\n  file: 'lib/orders/average.ts',\n  commit: 'a1b2c3d',\n};\n\nexport const CODER = { name: 'Coder', series: 5 as SeriesSlot };\n\nexport const REVIEWER = {\n  name: 'Lena Brandt',\n};\n\nexport type LineKind = 'same' | 'added' | 'removed';\n\nexport type DiffLine = {\n  key: string;\n  kind: LineKind;\n  text: string;\n};\n\n/** The line the demonstration comments on, and the one the suggestion rewrites. */\nexport const TARGET_LINE = 'divide';\n\nexport const OLD_THREAD_LINE = 'loop';\n\nconst BEFORE_TARGET: DiffLine[] = [\n  {\n    key: 'signature',\n    kind: 'same',\n    text: 'export function averageOrderValue(orders: Order[]) {',\n  },\n  { key: 'old-return', kind: 'removed', text: '  return totalOf(orders);' },\n  { key: 'total', kind: 'added', text: '  const total = totalOf(orders);' },\n];\n\nconst TARGET: DiffLine = {\n  key: TARGET_LINE,\n  kind: 'added',\n  text: '  return total / orders.length;',\n};\n\nconst AFTER_TARGET: DiffLine[] = [\n  { key: 'close', kind: 'same', text: '}' },\n  { key: 'blank', kind: 'same', text: '' },\n  {\n    key: 'total-signature',\n    kind: 'same',\n    text: 'export function totalOf(orders: Order[]) {',\n  },\n  { key: 'let', kind: 'same', text: '  let total = 0;' },\n  {\n    key: OLD_THREAD_LINE,\n    kind: 'same',\n    text: '  for (const order of orders) {',\n  },\n  { key: 'add', kind: 'same', text: '    total += order.amountCents;' },\n  { key: 'loop-close', kind: 'same', text: '  }' },\n  { key: 'return-total', kind: 'same', text: '  return total;' },\n  { key: 'total-close', kind: 'same', text: '}' },\n];\n\nexport const GUARD_LINES: DiffLine[] = [\n  { key: 'guard', kind: 'added', text: '  if (orders.length === 0) {' },\n  { key: 'guard-return', kind: 'added', text: '    return 0;' },\n  { key: 'guard-close', kind: 'added', text: '  }' },\n];\n\nexport const GUARD_KEYS = new Set(GUARD_LINES.map((line) => line.key));\n\nexport function fileLinesOf(hasGuard: boolean): DiffLine[] {\n  if (hasGuard) {\n    return [...BEFORE_TARGET, ...GUARD_LINES, TARGET, ...AFTER_TARGET];\n  }\n  return [...BEFORE_TARGET, TARGET, ...AFTER_TARGET];\n}\n\n/** The change the agent proposes, as its own small diff. */\nexport const SUGGESTION: DiffLine[] = [\n  ...GUARD_LINES,\n  { ...TARGET, kind: 'same' },\n];\n\nexport const DEMO_COMMENT = 'This should handle an empty list';\n\nexport const SUGGESTION_REPLY =\n  'Right: with no orders this divides by zero and returns NaN. Returning 0 keeps the dashboard readable.';\n\nexport const NOTE_REPLY =\n  'Noted. I have no change to suggest on this line, tell me what you would like instead.';\n\nexport const OLD_COMMENT = 'Could this be a reduce?';\n\nexport const OLD_REPLY =\n  'A loop reads plainer here and matches the rest of the file, so I kept it.';\n\nexport type NumberedLine = DiffLine & {\n  oldNumber: number | null;\n  newNumber: number | null;\n};\n\nexport function numbered(lines: DiffLine[]): NumberedLine[] {\n  const result: NumberedLine[] = [];\n  let oldNumber = 11;\n  let newNumber = 11;\n  for (const line of lines) {\n    if (line.kind === 'removed') {\n      oldNumber += 1;\n      result.push({ ...line, oldNumber, newNumber: null });\n      continue;\n    }\n    if (line.kind === 'added') {\n      newNumber += 1;\n      result.push({ ...line, oldNumber: null, newNumber });\n      continue;\n    }\n    oldNumber += 1;\n    newNumber += 1;\n    result.push({ ...line, oldNumber, newNumber });\n  }\n  return result;\n}\n\nexport function countOfKind(lines: DiffLine[], kind: LineKind) {\n  const matching = lines.filter((line) => line.kind === kind);\n  return matching.length;\n}\n",
      "type": "registry:component",
      "target": "components/blocks/agent-review-02/data.ts"
    }
  ],
  "categories": [
    "blocks",
    "agent-review"
  ],
  "type": "registry:block"
}