chat

package
v0.0.0-...-ae24e80 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2025 License: MPL-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultCachePath = cmp.Or(os.Getenv("HOME"), os.Getenv("USERPROFILE")) + "/.openai-cli-chat-pebble-storage-cache"

DefaultCachePath defines the default location for the chat session cache, which is used to store conversation history as a pebble-backed database.

On Unix-like systems, it is set to ~/.openai-cli-chat-cache, and on Windows, it is set to %USERPROFILE%/.openai-cli-chat-cache, which are the common locations for user-specific configuration files.

Functions

func ChunkString

func ChunkString(s string, maxTokens int64) ([]string, error)

ChunkString takes a given string (and number of tokens it contains), and splits it into smaller strings that are within the given max token limit. This is useful for embeddings which require smaller context windows than their chat counterparts.

Types

type Command

type Command struct {
	// Name of the command.
	//
	// If Matches is nil, the command is executed when the input matches the name.
	Name string

	// Description of the command.
	Description string

	// Matches is a function that checks if the command matches the input.
	//
	// If Matches is nil, the command is executed when the input matches the name.
	// If Matches is not nil, the command is executed when Matches returns true.
	Matches func(input string) bool

	// Run is the function that executes the command.
	Run CommandFunc
}

Command represents an abstract command with a name, a matching function, and an execution function.

type CommandFunc

type CommandFunc func(ctx context.Context, session *Session, input string)

CommandFunc defines the function signature for executing a command.

type ReqRespPair

type ReqRespPair struct {
	Model      string                       `json:"model,omitzero"`
	Req        openai.ChatCompletionMessage `json:"req,omitzero"`
	ReqTokens  int64                        `json:"req_tokens,omitzero"`
	Resp       openai.ChatCompletionMessage `json:"resp,omitzero"`
	RespTokens int64                        `json:"resp_tokens,omitzero"`
}

ReqRespPair represents a request-response pair in the chat session, used for storing conversation history in the backend.

type Session

type Session struct {
	Client                     *openai.Client
	ChatModel                  string
	StorageBackend             storage.Backend[string, ReqRespPair]
	Messages                   []openai.ChatCompletionMessage
	CurrentTokensUsed          int64
	SummarizeContextWindowSize int64

	Terminal   *term.Terminal
	OutWriter  *bufio.Writer
	TermWidth  int
	TermHeight int
	Commands   []Command
}

Session encapsulates the state and behavior of a CLI chat session. It manages terminal I/O, conversation history, caching, and command processing.

func NewSession

func NewSession(ctx context.Context, client *openai.Client, chatModel string, r io.Reader, w io.Writer, b storage.Backend[string, ReqRespPair]) (*Session, func(), error)

NewSession creates and initializes a new chat session.

It sets the terminal to raw mode, loads any existing chat history, and registers the default commands.

A restoration function is returned to restore the terminal state on exit.

func (*Session) Run

func (cs *Session) Run(ctx context.Context)

Run starts the main loop of the chat session.

func (*Session) RunOnce

func (cs *Session) RunOnce(ctx context.Context) (bool, error)

func (*Session) ShowHelp

func (cs *Session) ShowHelp()

Directories

Path Synopsis
Package storage provides a pluggable storage layer for the chat application.
Package storage provides a pluggable storage layer for the chat application.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL