session

package
v1.10.1 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package session saves chats, so they can be resumed: one JSONL file per chat under ~/.docsgpt/sessions/<cwd slug>-<cwd hash>/, a header line and then the messages as they complete (pi's format, without its tree: a fork is a new file starting with a copy of the messages it keeps).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dir

func Dir(cwd string) string

Dir returns the directory of cwd's sessions: the end of the path made readable, and a hash of it, as paths can read the same (/x/a-b, /x/a/b).

Types

type Entry

type Entry struct {
	Type string    `json:"type"`
	Time time.Time `json:"time"`

	ID     string `json:"id,omitempty"`     // header
	Cwd    string `json:"cwd,omitempty"`    // header
	Parent string `json:"parent,omitempty"` // header: the file this chat was forked from
	Name   string `json:"name,omitempty"`   // name

	Server         string `json:"server,omitempty"`          // header, state
	Key            string `json:"key,omitempty"`             // header, state
	ConversationID string `json:"conversation_id,omitempty"` // header, state

	Message *docsgpt.Message `json:"message,omitempty"`
	Text    string           `json:"text,omitempty"` // what the user typed; Message adds the context
	// Attachments are the files a user message carried: their paths and
	// hashes, not their bytes, which the server keeps.
	Attachments []attach.File    `json:"attachments,omitempty"`
	Sources     []docsgpt.Source `json:"sources,omitempty"`
	Usage       *docsgpt.Usage   `json:"usage,omitempty"` // an exchange's last message: the tokens it took
	// contains filtered or unexported fields
}

Entry is one line of a session file: the "session" header first, then "message" lines, "state" lines (the server, key or server conversation changed) and "name" lines (the chat was named, or the name cleared).

type Session

type Session struct {
	Path           string
	Cwd            string
	Name           string // given with /name; "" for none
	Parent         string // the file this chat was forked from
	Server         string // the latest
	Key            string // the latest
	ConversationID string // the latest
	Created        time.Time
	Updated        time.Time
	Messages       []Entry // the "message" entries, in order
	// contains filtered or unexported fields
}

Session is one saved chat.

func List

func List(cwd string) ([]*Session, error)

List returns cwd's sessions that hold messages, latest first.

func Load

func Load(path string) (*Session, error)

Load reads a session file.

func New

func New(cwd, server, key string) *Session

New starts a session in memory; its file is written with the first messages.

func (*Session) Delete

func (s *Session) Delete() error

Delete removes the session's file.

func (*Session) Fork

func (s *Session) Fork(turn int) *Session

Fork starts a new session holding a copy of the exchanges before turn (counted from 0), so the chat can go on from there while this one stays as it is. Its file is written with its first record.

func (*Session) Record

func (s *Session) Record(server, key, conversationID string, msgs ...Entry) error

Record appends messages, noting first whether the server, the key or the server conversation changed. The first call writes the header (and what a fork copies).

func (*Session) Saved

func (s *Session) Saved() bool

Saved reports whether the session has a file yet.

func (*Session) SetName

func (s *Session) SetName(name string) error

SetName names the chat, "" clearing the name. A chat not saved yet writes it with its first record.

func (*Session) Title

func (s *Session) Title() string

Title is the chat's name, else the first line of its first question.

func (*Session) Turns

func (s *Session) Turns() []Turn

Turns groups the messages into exchanges.

func (*Session) Usage

func (s *Session) Usage() (last, total docsgpt.Usage)

Usage returns the tokens of the last exchange that reported them, and of all of them.

type Turn

type Turn struct {
	Question string
	Files    []attach.File // attached to the question
	Answer   string        // the assistant's texts of the exchange, joined
	Sources  []docsgpt.Source
	Usage    *docsgpt.Usage // nil when the server reported none

	// Where the exchange took place: the server, the key and the server
	// conversation ("" when the server named none), and the exchange's
	// index in that conversation.
	Server, Key, ConversationID string
	Index                       int
}

Turn is one exchange: what the user typed and the answer.

Jump to

Keyboard shortcuts

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