Skip to content

trading_agent.terminal.prompt_history

The prompts typed in the terminal program, recalled with Up and Down and searched with Ctrl+R.

Each prompt is one JSON string per line, so prompts with several lines are kept whole. The newest 1000 are kept.

Typical usage example:

history = PromptHistory(Path('~/.local/state/trading-agent/history').expanduser()) history.add('/status') history.previous('') # '/status'

PromptHistory

PromptHistory(file)

The typed prompts, oldest first, with a cursor for Up and Down.

Reads the saved prompts.

Parameters:

Name Type Description Default
file Path | None

Where prompts are kept, or None to keep them only in memory.

required
Source code in src/trading_agent/terminal/prompt_history.py
def __init__(self, file: Path | None):
    """Reads the saved prompts.

    Args:
        file (Path | None): Where prompts are kept, or None to keep them only in memory.
    """
    self._file = file
    self._prompts: list[str] = []
    self._position: int | None = None
    self._draft = ''
    if file is None:
        return
    try:
        lines = file.read_text(encoding='utf-8').splitlines()
    except OSError:
        return
    for line in lines:
        try:
            prompt = json.loads(line)
        except ValueError:
            continue
        if isinstance(prompt, str) and prompt:
            self._prompts.append(prompt)
    self._prompts = self._prompts[-_MOST_KEPT:]

add

add(prompt)

Records a sent prompt, unless it repeats the newest one.

Parameters:

Name Type Description Default
prompt str

The prompt.

required
Source code in src/trading_agent/terminal/prompt_history.py
def add(self, prompt: str) -> None:
    """Records a sent prompt, unless it repeats the newest one.

    Args:
        prompt (str): The prompt.
    """
    self._position = None
    self._draft = ''
    if not prompt.strip():
        return
    if self._prompts and self._prompts[-1] == prompt:
        return
    self._prompts.append(prompt)
    self._prompts = self._prompts[-_MOST_KEPT:]
    self._save()

previous

previous(current)

Moves one prompt back.

Parameters:

Name Type Description Default
current str

What is in the input box now; kept as the draft when leaving it.

required

Returns:

Type Description
str | None

The older prompt, or None at the oldest.

Source code in src/trading_agent/terminal/prompt_history.py
def previous(self, current: str) -> str | None:
    """Moves one prompt back.

    Args:
        current (str): What is in the input box now; kept as the draft when leaving it.

    Returns:
        str | None: The older prompt, or None at the oldest.
    """
    if not self._prompts:
        return None
    if self._position is None:
        self._draft = current
        self._position = len(self._prompts)
    if self._position == 0:
        return None
    self._position -= 1
    return self._prompts[self._position]

next

next()

Moves one prompt forward.

Returns:

Type Description
str | None

The newer prompt, the draft after the newest, or None when not browsing.

Source code in src/trading_agent/terminal/prompt_history.py
def next(self) -> str | None:
    """Moves one prompt forward.

    Returns:
        str | None: The newer prompt, the draft after the newest, or None when not browsing.
    """
    if self._position is None:
        return None
    self._position += 1
    if self._position >= len(self._prompts):
        self._position = None
        return self._draft
    return self._prompts[self._position]

search

search(words)

The prompts that contain some text, newest first, each once.

Parameters:

Name Type Description Default
words str

The text to look for, ignoring case.

required

Returns:

Type Description
list[str]

The matching prompts.

Source code in src/trading_agent/terminal/prompt_history.py
def search(self, words: str) -> list[str]:
    """The prompts that contain some text, newest first, each once.

    Args:
        words (str): The text to look for, ignoring case.

    Returns:
        list[str]: The matching prompts.
    """
    wanted = words.lower()
    found = []
    for prompt in reversed(self._prompts):
        if wanted in prompt.lower() and prompt not in found:
            found.append(prompt)
    return found