Skip to content
history logo

History

history

Imported skill history from langchain

SKILL.md

Full skill instructions

"""Command history manager for input persistence."""

from future import annotations

import json from pathlib import Path # noqa: TC003 - used at runtime in type hints

class HistoryManager: """Manages command history with file persistence.

Uses append-only writes for concurrent safety. Multiple agents can
safely write to the same history file without corruption.
"""

def __init__(self, history_file: Path, max_entries: int = 100) -> None:
    """Initialize the history manager.

    Args:
        history_file: Path to the JSON-lines history file
        max_entries: Maximum number of entries to keep
    """
    self.history_file = history_file
    self.max_entries = max_entries
    self._entries: list[str] = []
    self._current_index: int = -1
    self._temp_input: str = ""
    self._load_history()

def _load_history(self) -> None:
    """Load history from file."""
    if not self.history_file.exists():
        return

    try:
        with self.history_file.open("r", encoding="utf-8") as f:
            entries = []
            for raw_line in f:
                line = raw_line.rstrip("\n\r")
                if not line:
                    continue
                try:
                    entry = json.loads(line)
                except json.JSONDecodeError:
                    entry = line
                entries.append(entry if isinstance(entry, str) else str(entry))
            self._entries = entries[-self.max_entries :]
    except (OSError, UnicodeDecodeError):
        self._entries = []

def _append_to_file(self, text: str) -> None:
    """Append a single entry to history file (concurrent-safe)."""
    try:
        self.history_file.parent.mkdir(parents=True, exist_ok=True)
        with self.history_file.open("a", encoding="utf-8") as f:
            f.write(json.dumps(text) + "\n")
    except OSError:
        pass

def _compact_history(self) -> None:
    """Rewrite history file to remove old entries.

    Only called when entries exceed 2x max_entries to minimize rewrites.
    """
    try:
        self.history_file.parent.mkdir(parents=True, exist_ok=True)
        with self.history_file.open("w", encoding="utf-8") as f:
            for entry in self._entries:
                f.write(json.dumps(entry) + "\n")
    except OSError:
        pass

def add(self, text: str) -> None:
    """Add a command to history.

    Args:
        text: The command text to add
    """
    text = text.strip()
    # Skip empty or slash commands
    if not text or text.startswith("/"):
        return

    # Skip duplicates of the last entry
    if self._entries and self._entries[-1] == text:
        return

    self._entries.append(text)

    # Append to file (fast, concurrent-safe)
    self._append_to_file(text)

    # Compact only when we have 2x max entries (rare operation)
    if len(self._entries) > self.max_entries * 2:
        self._entries = self._entries[-self.max_entries :]
        self._compact_history()

    self.reset_navigation()

def get_previous(self, current_input: str, prefix: str = "") -> str | None:
    """Get the previous history entry.

    Args:
        current_input: Current input text (saved on first navigation)
        prefix: Optional prefix to filter entries

    Returns:
        Previous matching entry or None
    """
    if not self._entries:
        return None

    # Save current input on first navigation
    if self._current_index == -1:
        self._temp_input = current_input
        self._current_index = len(self._entries)

    # Search backwards for matching entry
    for i in range(self._current_index - 1, -1, -1):
        if self._entries[i].startswith(prefix):
            self._current_index = i
            return self._entries[i]

    return None

def get_next(self, prefix: str = "") -> str | None:
    """Get the next history entry.

    Args:
        prefix: Optional prefix to filter entries

    Returns:
        Next matching entry, original input at end, or None
    """
    if self._current_index == -1:
        return None

    # Search forwards for matching entry
    for i in range(self._current_index + 1, len(self._entries)):
        if self._entries[i].startswith(prefix):
            self._current_index = i
            return self._entries[i]

    # Return to original input at the end
    result = self._temp_input
    self.reset_navigation()
    return result

def reset_navigation(self) -> None:
    """Reset navigation state."""
    self._current_index = -1
    self._temp_input = ""

More skills from majiayu000

xiaohongshu logo
majiayu000/claude-arsenal

xiaohongshu

xiaohongshu

286 148
View
agent-task-conductor logo
majiayu000/claude-skill-registry

agent-task-conductor

Conduct multi-agent task orchestration and workflow coordination.

663 1
View
conductor-setup logo
majiayu000/claude-skill-registry

conductor-setup

Initialize project with Conductor artifacts (product definition,

663 1
View
animation-designer logo
majiayu000/claude-skill-registry

animation-designer

Expert in web animations, transitions, and motion design using Framer Motion and CSS

663 1
View
diagramming logo
majiayu000/claude-skill-registry

diagramming

Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...

663 1
View
h3-pg logo
majiayu000/claude-skill-registry-data

h3-pg

PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.

23 1
View
conductor-development logo
majiayu000/claude-skill-registry

conductor-development

Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...

663 1
View
conductor-status logo
majiayu000/claude-skill-registry

conductor-status

Display project status, active tracks, and next actions

663 1
View
dockerization logo
majiayu000/claude-skill-registry

dockerization

Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.

663 1
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View