Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

2.7 KB
Raw Blame
'use client';

import { useTrueForgeAgentSpec } from '@truefoundry/trueforge-assistant-ui-runtime';

import { useAuiState } from '../assistant-ui.js';
import { shellIsCreateAgent, useOptionalShellMode } from '../server/ShellModeContext.js';
import { isNewChatView } from '../utils/isNewChatView.js';

export type NamedAgentHeaderState = {
  name: string;
  isEditing: boolean;
  /** When true, prefer a non-empty thread/session title over `name`. */
  allowThreadTitle: boolean;
};

// Canonical named-agent / draft header state, including mutable edit mode.
export function useNamedAgentHeaderState(): NamedAgentHeaderState | null {
  const shell = useOptionalShellMode();
  if (shell == null || shell.mode.status !== 'active') return null;

  const identity = shell.mode.agentName ?? shell.mode.agentId;
  if (identity != null && identity.length > 0) {
    return {
      name: identity,
      isEditing: shell.mode.isMutable && shell.mode.agentId != null,
      allowThreadTitle: false,
    };
  }

  if (!shell.mode.isMutable) return null;

  const isCreateAgent = shell.mode.isCreateAgent;
  return {
    name: isCreateAgent ? 'New Agent' : 'New Chat',
    isEditing: false,
    allowThreadTitle: true,
  };
}

export function useNamedAgentHeaderVisible(): boolean {
  const state = useNamedAgentHeaderState();
  return state !== null;
}

// Mutable New Agent / Edit with a selected model — drives Save Agent + header chrome.
export function useSaveAgentVisible(): boolean {
  const shell = useOptionalShellMode();
  const { agentSpec } = useTrueForgeAgentSpec();
  if (shell == null || shell.mode.status !== 'active' || !shell.mode.isMutable || !shell.mode.isCreateAgent) {
    return false;
  }
  return Boolean(agentSpec?.model?.name?.trim());
}

// Clear chat: any active session (Try Agent, New Chat, New Agent, Edit) that has
// something to clear. A fresh thread has nothing, so the control stays hidden.
export function useChatChromeActionsVisible(): boolean {
  const shell = useOptionalShellMode();
  const isFresh = useAuiState(isNewChatView);
  return shell != null && shell.mode.status === 'active' && !isFresh;
}

export function useResetAgentBuilderVisible(): boolean {
  const shell = useOptionalShellMode();
  return shell != null && shellIsCreateAgent(shell.mode);
}

// True when the thread header has anything to show (title, Save, and/or Clear).
// Clear alone matters for orphaned immutable history (deleted agent, no name).
export function useChatHeaderContentVisible(): boolean {
  const namedVisible = useNamedAgentHeaderVisible();
  const saveVisible = useSaveAgentVisible();
  const clearVisible = useChatChromeActionsVisible();
  const resetVisible = useResetAgentBuilderVisible();
  return namedVisible || saveVisible || clearVisible || resetVisible;
}