Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ollama Ollama Readline History

From Leeroopedia
Knowledge Sources
Domains CLI, Terminal UI
Last Updated 2025-02-15 00:00 GMT

Overview

Manages command history for the readline interactive input system, with persistent storage, navigation, and automatic compaction.

Description

History uses an arraylist.List[string] as a ring buffer with a configurable limit (default 100 entries). Init loads history from ~/.ollama/history, reading line-by-line. Add appends entries, calls Compact to trim excess entries from the front, and auto-saves to disk. Prev/Next navigate the history position for up/down arrow key support. Save writes atomically using a temp file and rename pattern to prevent data loss. History can be disabled per-session via the Enabled flag.

Usage

Provides persistent prompt history for ollama run, allowing users to recall previous prompts with up/down arrow keys across sessions.

Code Reference

Source Location

  • Repository: Ollama
  • File: readline/history.go
  • Lines: 1-151

Signature

type History struct {
    Buf      *arraylist.List[string]
    Autosave bool
    Pos      int
    Limit    int
    Filename string
    Enabled  bool
}

func NewHistory() (*History, error)
func (h *History) Init() error
func (h *History) Add(s string)
func (h *History) Compact()
func (h *History) Clear()
func (h *History) Prev() string
func (h *History) Next() string
func (h *History) Size() int
func (h *History) Save() error

Import

import "github.com/ollama/ollama/readline"

I/O Contract

Inputs

Name Type Required Description
s string Yes History entry to add

Outputs

Name Type Description
line string Previous or next history entry
error error File I/O errors during init or save

Usage Examples

history, err := readline.NewHistory()
if err != nil {
    log.Fatal(err)
}

// Add a new entry (auto-saves to ~/.ollama/history)
history.Add("What is the meaning of life?")

// Navigate history
prev := history.Prev() // "What is the meaning of life?"
next := history.Next() // ""

// Check size
fmt.Println(history.Size()) // 1

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment