Skip to content

The agent that grows with you

9.7 KB
Raw Blame
import type { ToolCallMessagePart } from '@assistant-ui/react'

import { isPreviewableTarget, toolPreviewOutcome } from '@/components/assistant-ui/tool/fallback-model'
import { leaveIntroOnLayout } from '@/components/onboarding/first-run/intro'
import { toolCallOwnerMessageId } from '@/lib/chat-messages'
import { connectorText, recordOf } from '@/lib/connector-tools'
import { invalidateSlashCompletions } from '@/lib/slash-completion-cache'
import { refreshBackgroundProcesses } from '@/store/composer-status'
import { flashPetActivity, setPetActivity } from '@/store/pet'
import { recordPreviewArtifact, reofferPreviewArtifact } from '@/store/preview-status'
import { $sessionStates, storedSessionIdForRuntimeId } from '@/store/session-states'
import { setupBeatResult } from '@/store/setup-beat'
import { isTerminalSubagentCompletion, pruneDelegateFallbackSubagents, upsertSubagent } from '@/store/subagents'
import { restoreSessionTodosFromSnapshot } from '@/store/todos'
import { setSessionDraftingTool } from '@/store/tool-drafting'
import { clearSessionToolProgress, clearToolProgress, setToolProgress } from '@/store/tool-progress'
import { notifyWorkspaceChanged, toolChangedPath, toolMayMutateFiles } from '@/store/workspace-events'

import { SUBAGENT_EVENT_TYPES, toTodoPayload } from '../utils'

import type { GatewayEventContext } from './types'

function recordToolPreview(ctx: GatewayEventContext, sessionId: string, cwd: string, pendingProduction: boolean): void {
  const { event, payload, occurredAt } = ctx

  if (!payload?.name || payload.result === undefined || event.seq === undefined) {
    return
  }

  const { previewTarget, status } = toolPreviewOutcome({
    type: 'tool-call',
    toolName: payload.name,
    args: payload.args ?? {},
    result: payload.result,
    toolResultMetadata: payload,
    completedAt: occurredAt
  })

  if (status === 'success' && previewTarget && isPreviewableTarget(previewTarget)) {
    const record = pendingProduction ? reofferPreviewArtifact : recordPreviewArtifact
    record(sessionId, previewTarget, cwd, storedSessionIdForRuntimeId(sessionId) ?? sessionId)
  }
}

/** What a completed tool leaves behind outside its row: a preview target or a committed layout. */
function noteToolOutcome(ctx: GatewayEventContext, sessionId: string, cwd: string, pendingProduction: boolean): void {
  if (!ctx.deps.sessionInterrupted(sessionId)) {
    recordToolPreview(ctx, sessionId, cwd, pendingProduction)
  }

  if (!ctx.event.replayed && appliedLayout(ctx.payload?.name, ctx.payload?.result)) {
    leaveIntroOnLayout()
  }
}

/** The layout a completed tool applied: the setup_beat layout answer, or a set_theme layout (applied or picked). */
function appliedLayout(name: string | undefined, result: ToolCallMessagePart['result']): string | undefined {
  if (name === 'setup_beat') {
    return setupBeatResult(result).answer?.layout
  }

  if (name === 'set_theme') {
    const row = recordOf(result)

    return connectorText(recordOf(row.applied ?? row.picked).layout)
  }

  return undefined
}

/** tool.progress (live MCP progress for a running call): a row annotation, never a transcript
 *  mutation. Retired on tool.complete (which then falls through to handleToolEvent); the dispatcher
 *  also sweeps a session's leftovers when its turn ends (index.ts). */
export function handleToolProgressEvent({ deps, event, payload, sessionId }: GatewayEventContext): boolean {
  const toolId = typeof payload?.tool_id === 'string' ? payload.tool_id : ''

  if (!sessionId) {
    return event.type === 'tool.progress'
  }

  if (event.type === 'tool.complete') {
    clearToolProgress(sessionId, toolId)

    return false
  }

  if (event.type !== 'tool.progress') {
    return false
  }

  if (!deps.sessionInterrupted(sessionId)) {
    setToolProgress(sessionId, toolId, {
      message: typeof payload?.message === 'string' && payload.message ? payload.message : null,
      percent: typeof payload?.percent === 'number' ? payload.percent : null
    })
  }

  return true
}

/** A finished turn retires its live tool progress even if a tool.complete went missing. Runs for every
 *  event (the dispatcher's finally), because message.complete is consumed by an earlier handler. */
export function retireTurnToolProgress(eventType: string, sessionId: string): void {
  if (eventType === 'message.complete' || eventType === 'error') {
    clearSessionToolProgress(sessionId)
  }
}

/** tool.generating / tool.start / tool.complete / subagent.*. */
export function handleToolEvent(ctx: GatewayEventContext): boolean {
  const { deps, event, payload, sessionId, isActiveEvent, occurredAt } = ctx
  const { flushQueuedDeltas, nativeSubagentSessionsRef, sessionInterrupted, updateSessionState, upsertToolCall } = deps

  if (event.type === 'todo.updated') {
    if (sessionId && !sessionInterrupted(sessionId)) {
      restoreSessionTodosFromSnapshot(sessionId, payload, true)
    }

    return true
  }

  if (event.type === 'tool.generating') {
    // Announced while the model is still emitting the call's JSON, so it
    // carries a name and nothing else — no id, no args. Materializing a row
    // from it strands an argless placeholder whenever the bubble is sealed
    // before the real `tool.start` arrives, because the two can no longer be
    // reconciled across the boundary. It's a status, so say it as one.
    // A stopped turn can still emit a frame or two before the backend
    // notices, and naming a tool we will never run leaves the label up
    // until something else retires it. `mutateStream` drops late tool rows
    // on the same condition; the status line has to agree with it.
    if (!sessionId || sessionInterrupted(sessionId)) {
      return true
    }

    setSessionDraftingTool(sessionId, typeof payload?.name === 'string' ? payload.name : '')

    if (isActiveEvent) {
      setPetActivity({ reasoning: false, toolRunning: true })
    }

    return true
  }

  if (event.type === 'tool.start') {
    if (!sessionId) {
      return true
    }

    flushQueuedDeltas(sessionId)
    upsertToolCall(sessionId, toTodoPayload(payload) ?? payload, 'running', event.type, occurredAt)

    if (isActiveEvent) {
      setPetActivity({ reasoning: false, toolRunning: true })
    }

    return true
  }

  if (event.type === 'tool.complete') {
    if (sessionId) {
      flushQueuedDeltas(sessionId)

      const state = $sessionStates.get()[sessionId]
      // Read before the upsert seals the part: only a completion that resolves
      // a still-pending call is a fresh production; a duplicate or replayed
      // completion may never re-offer a dismissed target.
      const pendingProduction = !event.replayed && Boolean(toolCallOwnerMessageId(state?.messages ?? [], payload))

      upsertToolCall(sessionId, toTodoPayload(payload) ?? payload, 'complete', event.type, occurredAt)

      noteToolOutcome(ctx, sessionId, state?.cwd ?? '', pendingProduction)

      if (isActiveEvent) {
        setPetActivity({ toolRunning: false })

        // A tool can fail without ending the turn when the agent recovers
        // and continues. Surface that failure as a short pet beat too;
        // otherwise only turn-level errors ever reach the failed state.
        if (payload?.error) {
          flashPetActivity({ error: true })
        }
      }

      // A pending clarify blocks the turn, so the first tool.complete after
      // one is the clarify resolving — drop the "needs input" flag here so
      // the sidebar indicator clears as soon as it's answered, not only at
      // message.complete.
      updateSessionState(sessionId, state => (state.needsInput ? { ...state, needsInput: false } : state))

      // terminal/process tool calls are the only things that spawn or reap
      // background processes — sync the composer status stack right after,
      // even on an interrupted turn (idempotent re-sync; the 5s poll may be
      // unarmed when no running row is on screen yet, #81114).
      if (payload?.name === 'terminal' || payload?.name === 'process') {
        void refreshBackgroundProcesses(sessionId)
      }
    }

    // The agent just created/deleted/renamed a skill, which adds or removes
    // its `/name` command. Drop the composer's cached `/` list so the new
    // skill is offerable now rather than after the hour-long TTL.
    if (payload?.name === 'skill_manage') {
      invalidateSlashCompletions()
    }

    // A file-mutating tool just finished — nudge the git-mirroring surfaces
    // (coding rail, review pane, file tree) to refresh. Event-driven, not
    // polled: fires exactly when the agent touches the tree.
    if (payload && toolMayMutateFiles(payload)) {
      notifyWorkspaceChanged(toolChangedPath(payload))
    }

    return true
  }

  if (SUBAGENT_EVENT_TYPES.has(event.type)) {
    // A Stop interrupts the parent TURN, not the children: a delegation can
    // still be finishing in the background, and its `subagent.complete` is the
    // only thing that terminalizes the row. Dropping it leaves a permanently
    // 'running' spinner (#75505). Terminal completions are accepted past the
    // interrupt; only live progress keeps the guard, so a stopped turn's
    // late mid-flight frames still can't repaint its stream. upsertSubagent's
    // retired-id and terminal-status guards make a stale completion a no-op.
    const acceptWhileInterrupted = isTerminalSubagentCompletion(event.type, payload)

    if (sessionId && payload && (!sessionInterrupted(sessionId) || acceptWhileInterrupted)) {
      if (!nativeSubagentSessionsRef.current.has(sessionId)) {
        pruneDelegateFallbackSubagents(sessionId)
      }

      nativeSubagentSessionsRef.current.add(sessionId)
      upsertSubagent(
        sessionId,
        payload as Record<string, unknown>,
        event.type === 'subagent.spawn_requested' || event.type === 'subagent.start',
        event.type
      )
    }

    return true
  }

  return false
}