or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

advanced-selection.mdbase-prompt.mdconfirmation.mdindex.mdselection.mdtext-input.mdutilities.md
tile.json

base-prompt.mddocs/

Base Prompt System

Core prompt functionality providing the foundation for all prompt types. Includes event handling, state management, and the base prompt lifecycle.

Capabilities

Prompt Base Class

The foundation class that provides common functionality for all prompt types including state management, event handling, and the core prompt lifecycle.

/**
 * Base class for all prompts providing common functionality
 * @template T - The type of value the prompt will return
 */
class Prompt<T = any> {
  constructor(options: PromptOptions<Prompt>, trackValue?: boolean);
  
  /** Current state of the prompt */
  state: State;
  /** Current error message if any */
  error: string;
  /** Current value of the prompt */
  value: T;
  
  /** Start the prompt and return user input */
  prompt(): Promise<string | symbol>;
  /** Subscribe to prompt events */
  on<K extends keyof ClackEvents>(event: K, cb: ClackEvents[K]): void;
  /** Subscribe to prompt events once */
  once<K extends keyof ClackEvents>(event: K, cb: ClackEvents[K]): void;
  /** Emit prompt events */
  emit<K extends keyof ClackEvents>(event: K, ...data: Parameters<ClackEvents[K]>): void;
}

Usage Examples:

import { Prompt } from '@clack/core';

// Create a custom prompt by extending Prompt
class CustomPrompt extends Prompt<string> {
  constructor(options: PromptOptions<CustomPrompt>) {
    super(options);
  }
}

// Use with custom render function
const prompt = new CustomPrompt({
  render() {
    return `Custom prompt: ${this.value || '(empty)'}`;
  },
  validate(value) {
    if (!value) return 'Value is required';
  }
});

const result = await prompt.prompt();

Prompt Options Interface

Configuration interface for all prompt types defining render function, validation, and behavior options.

/**
 * Base configuration options for all prompt types
 * @template Self - The prompt type being configured
 */
interface PromptOptions<Self> {
  /** Required render function that defines prompt appearance */
  render(this: Omit<Self, 'prompt'>): string | undefined;
  /** Optional placeholder text */
  placeholder?: string;
  /** Optional initial value */
  initialValue?: any;
  /** Optional validation function */
  validate?: (value: any) => string | Error | undefined;
  /** Optional input stream (defaults to stdin) */
  input?: Readable;
  /** Optional output stream (defaults to stdout) */
  output?: Writable;
  /** Optional debug mode */
  debug?: boolean;
  /** Optional abort signal for cancellation */
  signal?: AbortSignal;
}

State Type

Enumeration of all possible prompt states during the prompt lifecycle.

/**
 * Possible states for a prompt during its lifecycle
 */
type State = 'initial' | 'active' | 'cancel' | 'submit' | 'error';

Event System

Event interface defining all events that prompts can emit during their lifecycle.

/**
 * Events that prompts can emit during their lifecycle
 */
interface ClackEvents {
  /** Emitted when prompt is initialized */
  initial: (value?: any) => void;
  /** Emitted when prompt becomes active */
  active: (value?: any) => void;
  /** Emitted when prompt is cancelled */
  cancel: (value?: any) => void;
  /** Emitted when prompt is submitted */
  submit: (value?: any) => void;
  /** Emitted when prompt encounters an error */
  error: (value?: any) => void;
  /** Emitted when cursor position changes */
  cursor: (key?: Action) => void;
  /** Emitted when any key is pressed */
  key: (key?: string) => void;
  /** Emitted when prompt value changes */
  value: (value?: string) => void;
  /** Emitted when confirmation value changes */
  confirm: (value?: boolean) => void;
  /** Emitted when prompt is finalized */
  finalize: () => void;
}

Usage Examples:

import { TextPrompt } from '@clack/core';

const prompt = new TextPrompt({
  render() {
    return `Enter text: ${this.valueWithCursor}`;
  }
});

// Listen to events
prompt.on('value', (value) => {
  console.log('Value changed:', value);
});

prompt.on('submit', () => {
  console.log('Prompt submitted');
});

prompt.on('cancel', () => {
  console.log('Prompt cancelled');
});

const result = await prompt.prompt();