Skip to main content

TypeScript Agent CLI

A TypeScript-first interactive command-line interface that connects to the Perplexity Agent API with streaming responses, runtime model selection, and integrated web search.

Features

  • Interactive REPL with streaming token output
  • Runtime model selection from a curated list
  • Web search integration via the web_search tool
  • TypeScript-specific patterns: type narrowing, const assertions, typed error classes
  • Conversation history for multi-turn interactions
  • Graceful error handling and clean shutdown

Installation

Usage

The CLI prompts you to select a model, then enters an interactive loop:
Commands: /search (enable web search), /nosearch (disable), /clear (reset history), /quit (exit).

Full Code

Save as src/cli.ts:

Example Session

Key TypeScript Patterns

Const Assertions for Tool Types

Use as const to narrow tool type literals:

Streaming Event Type Narrowing

Check chunk.type before accessing event-specific fields:

Typed Error Handling

Conversation history is preserved across turns, so the model can reference earlier messages. Use /clear to start a fresh conversation without restarting the CLI.

Limitations

  • The CLI uses Node.js readline, which does not support arrow-key history navigation. For a richer experience, consider inquirer or prompts.
  • Conversation history is in-memory only and lost when the process exits.
  • Streaming events may vary by model provider. The response.output_text.delta event is consistent across all models.