Skip to content

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

12.4 KB
Raw Blame
import type { KeyboardEvent } from 'react';
import { Icon } from '../../icons/Icon.js';
import { useSlot } from '../../theme/SlotsProvider.js';
import { cn } from '../lib/cn.js';
import { Button } from '../primitives/Button.js';
import { IconButton } from '../primitives/IconButton.js';

export type Question = {
  id: string;
  question: string;
  options: string[];
};

export type AnsweredQuestion = Question & {
  answer: string;
  isCustom: boolean;
};

export type AskUserAnswerDraft = {
  radioValue: string;
  custom: string;
};

export const ASK_USER_CUSTOM_OPTION = '__tfy_ask_user_question_custom__';

/** A, B, … Z, then AA, AB, … for option indices. */
export function askUserOptionLetter(index: number): string {
  let n = index;
  let label = '';
  do {
    label = String.fromCharCode(65 + (n % 26)) + label;
    n = Math.floor(n / 26) - 1;
  } while (n >= 0);
  return label;
}

export type AskUserPromptProps = {
  questions: Question[];
  answeredQuestions?: AnsweredQuestion[];
  onSubmit: () => void;
  currentQuestion?: Question;
  currentQuestionIndex?: number;
  currentAnswer?: AskUserAnswerDraft;
  totalQuestions?: number;
  isMultiQuestion?: boolean;
  isLastQuestion?: boolean;
  allQuestionsAnswered?: boolean;
  isSubmitDisabled?: boolean;
  isSubmitAllDisabled?: boolean;
  onCurrentQuestionIndexChange?: (index: number) => void;
  onCurrentAnswerChange?: (questionId: string, update: Partial<AskUserAnswerDraft>) => void;
  readOnly?: boolean;
  dataTestPrefix?: string;
  className?: string;
};

function OptionLetterBadge({ letter, selected, className }: { letter: string; selected: boolean; className?: string }) {
  return (
    <span
      aria-hidden
      className={cn(
        'inline-flex size-5 shrink-0 items-center justify-center rounded border font-sans text-[0.6875rem] font-semibold leading-none',
        selected
          ? 'border-primary-button-bg/50 bg-primary-button-bg/15 text-primary-button-bg'
          : 'border-border bg-transparent text-text-secondary',
        className,
      )}
    >
      {letter}
    </span>
  );
}

export function AskUserPrompt({
  questions,
  answeredQuestions = [],
  onSubmit,
  currentQuestion,
  currentQuestionIndex = 0,
  currentAnswer = { radioValue: '', custom: '' },
  totalQuestions = questions.length,
  isMultiQuestion: isMultiQuestionProp,
  isLastQuestion: isLastQuestionProp,
  allQuestionsAnswered = false,
  isSubmitDisabled = true,
  isSubmitAllDisabled = true,
  onCurrentQuestionIndexChange,
  onCurrentAnswerChange,
  readOnly = false,
  dataTestPrefix,
  className,
}: AskUserPromptProps) {
  const Markdown = useSlot('Markdown');
  const isMultiQuestion = isMultiQuestionProp ?? totalQuestions > 1;
  const isLastQuestion = isLastQuestionProp ?? currentQuestionIndex === totalQuestions - 1;

  if (questions.length === 0) {
    if (answeredQuestions.length === 0) return null;
    return (
      <div className={cn('aui-ask-user-prompt mt-2 flex flex-col gap-3', className)}>
        {answeredQuestions.map(q => (
          <div key={q.id} data-testid={dataTestPrefix ? `${dataTestPrefix}-answered-${q.id}` : undefined}>
            <div className="flex items-center justify-between rounded-t-lg border border-primary-button-bg/30 bg-primary-button-bg/10 px-4 py-2">
              <div className="min-w-0 font-sans text-xs font-medium text-primary-button-bg [&_.markdown-body]:text-inherit!">
                <Markdown content={q.question} />
              </div>
              <div className="flex h-fit shrink-0 items-center gap-1 rounded border border-primary-button-bg/40 bg-primary-bg px-2 py-0.5 text-[0.6875rem] font-medium text-text-secondary">
                <span className="leading-tight">Answered</span>
              </div>
            </div>
            <div className="flex items-center gap-2 rounded-b-lg border border-t-0 border-border px-4 py-3">
              <Icon name="circle-check" size="0.875em" className="shrink-0 text-primary-button-bg" />
              <span className="font-sans text-[0.8125rem] font-medium text-text-primary">
                {q.isCustom ? `Other: ${q.answer}` : q.answer}
              </span>
            </div>
          </div>
        ))}
      </div>
    );
  }

  if (!currentQuestion) return null;

  const isCustomSelected = currentAnswer.radioValue === ASK_USER_CUSTOM_OPTION;
  const hasOptions = currentQuestion.options.length > 0;
  const customOptionLetter = askUserOptionLetter(currentQuestion.options.length);

  const handleOptionSelect = (value: string) => {
    if (readOnly) return;
    onCurrentAnswerChange?.(currentQuestion.id, { radioValue: value });
  };

  const goToNextQuestion = () => onCurrentQuestionIndexChange?.(currentQuestionIndex + 1);
  const isNextStep = isMultiQuestion && !isLastQuestion;

  const handleAnswerKeyDown = (event: KeyboardEvent) => {
    if (event.key !== 'Enter' || event.shiftKey || readOnly) return;
    event.preventDefault();
    if (isNextStep) {
      if (!isSubmitDisabled) goToNextQuestion();
      return;
    }
    if (!isSubmitAllDisabled) onSubmit();
  };

  const answerInput = (
    <textarea
      rows={1}
      value={currentAnswer.custom}
      disabled={readOnly || !currentQuestion.id}
      onFocus={() =>
        onCurrentAnswerChange?.(currentQuestion.id, {
          radioValue: ASK_USER_CUSTOM_OPTION,
        })
      }
      onChange={e => {
        onCurrentAnswerChange?.(currentQuestion.id, {
          custom: e.target.value,
          radioValue: ASK_USER_CUSTOM_OPTION,
        });
      }}
      placeholder={hasOptions ? 'Other' : 'Type your answer'}
      aria-label={hasOptions ? 'Other (custom answer)' : 'Your answer'}
      className={cn(
        'field-sizing-content min-h-8 max-h-[3lh] w-full resize-none overflow-y-auto rounded-md border border-input-border bg-primary-bg px-2.5 py-1 text-sm leading-5 text-text-primary',
        'placeholder:text-sm focus:outline-none focus:ring-1 focus:ring-focus-ring',
      )}
    />
  );

  return (
    <div
      className={cn(
        // Inset vs composer so stacked pause chrome matches the design (~1rem each side).
        'aui-ask-user-prompt mx-auto w-[calc(100%-2rem)] min-w-0 overflow-hidden rounded-lg',
        className,
      )}
      data-testid={dataTestPrefix ? `${dataTestPrefix}-question-card` : undefined}
    >
      <div className="flex items-center justify-between border rounded-t-lg border-primary-button-bg/30 bg-primary-button-bg/10 px-4 py-2.5">
        {totalQuestions > 1 ? (
          <div className="min-w-0 font-sans text-sm font-medium text-primary-button-bg">Questions</div>
        ) : (
          <div className="min-w-0 font-sans text-sm font-medium text-primary-button-bg [&_.markdown-body]:text-inherit!">
            <Markdown content={currentQuestion.question} />
          </div>
        )}
        {readOnly ? (
          <div className="flex h-fit shrink-0 items-center gap-1 rounded border border-primary-button-bg/40 bg-primary-bg px-2 py-0.5 text-[0.6875rem] font-medium text-text-secondary">
            <span className="leading-tight">Unanswered</span>
          </div>
        ) : null}
        {!readOnly && isMultiQuestion && (
          <div className="flex shrink-0 items-center gap-1">
            <IconButton
              aria-label="Previous question"
              variant="ghost"
              onClick={() => onCurrentQuestionIndexChange?.(Math.max(0, currentQuestionIndex - 1))}
              disabled={currentQuestionIndex === 0}
              className="text-text-primary"
            >
              <Icon name="chevron-left" size="0.75em" />
            </IconButton>
            <IconButton
              aria-label="Next question"
              variant="ghost"
              onClick={() => onCurrentQuestionIndexChange?.(Math.min(totalQuestions - 1, currentQuestionIndex + 1))}
              disabled={currentQuestionIndex === totalQuestions - 1}
              className="text-text-primary"
            >
              <Icon name="chevron-right" size="0.75em" />
            </IconButton>
            <div className="ml-1 rounded bg-primary-button-bg/20 px-2 py-1 font-sans text-xs font-medium text-text-primary">
              <span> {currentQuestionIndex + 1} </span> <span> of </span> <span> {totalQuestions} </span>
            </div>
          </div>
        )}
      </div>

      <div className="flex flex-col gap-2 bg-primary-bg px-4 py-3 border border-t-0 border-border">
        <div className="flex min-w-0 flex-col gap-3">
          {isMultiQuestion && (
            <div className="font-sans text-xs font-medium text-text-primary">
              <Markdown content={currentQuestion.question || 'What would you like to do?'} />
            </div>
          )}
          <div
            className="grid min-w-0 grid-cols-[auto_minmax(0,1fr)] gap-x-2 gap-y-3 overflow-visible"
            role={hasOptions ? 'radiogroup' : undefined}
            aria-label={hasOptions ? currentQuestion.question || 'Answer options' : undefined}
            onKeyDown={handleAnswerKeyDown}
          >
            {currentQuestion.options.map((opt, index) => {
              const isSelected = currentAnswer.radioValue === opt;
              const letter = askUserOptionLetter(index);
              return (
                <label
                  key={`${index}:${opt}`}
                  className={cn(
                    'col-span-2 grid grid-cols-subgrid items-center overflow-visible',
                    readOnly ? 'cursor-default' : 'cursor-pointer',
                  )}
                >
                  <input
                    type="radio"
                    value={opt}
                    disabled={readOnly || !currentQuestion.id}
                    name={`ask-user-question-option-${currentQuestion.id}`}
                    checked={isSelected}
                    onChange={() => handleOptionSelect(opt)}
                    className="sr-only"
                  />
                  <OptionLetterBadge letter={letter} selected={isSelected} />
                  <span className="min-w-0 font-sans text-sm font-medium text-text-primary">{opt}</span>
                </label>
              );
            })}

            {hasOptions ? (
              <label
                className={cn(
                  // Top-align so the letter stays on the first line as the textarea grows.
                  'col-span-2 grid grid-cols-subgrid items-start overflow-visible',
                  readOnly ? 'cursor-default' : 'cursor-pointer',
                )}
              >
                <input
                  type="radio"
                  disabled={readOnly || !currentQuestion.id}
                  value={ASK_USER_CUSTOM_OPTION}
                  name={`ask-user-question-option-${currentQuestion.id}`}
                  checked={isCustomSelected}
                  onChange={() => handleOptionSelect(ASK_USER_CUSTOM_OPTION)}
                  className="sr-only"
                />
                <OptionLetterBadge letter={customOptionLetter} selected={isCustomSelected} className="mt-1.5" />
                <div className="min-w-0">{answerInput}</div>
              </label>
            ) : (
              <div className="col-span-2 min-w-0">{answerInput}</div>
            )}
          </div>
        </div>

        {!readOnly && (
          <div
            className={cn('flex items-center', {
              'justify-between': isMultiQuestion,
              'justify-end': !isMultiQuestion,
            })}
          >
            {isMultiQuestion && isLastQuestion && !allQuestionsAnswered ? (
              <span className="text-xs font-medium text-failure-bg">Answer all questions to submit</span>
            ) : (
              <span />
            )}
            {isNextStep ? (
              <Button.Primary
                type="button"
                size="small"
                onClick={goToNextQuestion}
                disabled={isSubmitDisabled}
                className="bg-gray-850 text-white hover:bg-gray-850 dark:text-black"
              >
                Next
              </Button.Primary>
            ) : (
              <Button.Primary
                type="button"
                size="small"
                onClick={onSubmit}
                disabled={isSubmitAllDisabled}
                className="bg-gray-850 text-white hover:bg-gray-850 dark:text-black"
              >
                Continue
              </Button.Primary>
            )}
          </div>
        )}
      </div>
    </div>
  );
}

declare module '../../theme/SlotsProvider.js' {
  interface AtomSlots {
    AskUserPrompt: typeof AskUserPrompt;
  }
}