chatstore

package
v0.55.1 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package chatstore is the on-disk store behind `ppz chat`: per-identity chat history, the list of added pipes, and read markers — one JSON file per conversation window under $PPZ_HOME/chat/<handle>/. JetStream stays the source of truth for received messages; this store is authoritative only for the bits it can't hand back — your outbound sends and your read position.

Windows are loaded into memory on Open, mutated there (with in-memory dedup), and written back on Flush (atomic temp+rename) — so the launch replay burst is O(1) per message, not a file rewrite each time.

Index

Constants

View Source
const (
	KindAgent  = "agent"
	KindSource = "source" // message-kind source: a bare inbox (human/service)
	KindPipe   = "pipe"
)

Window kinds. A window is uniquely identified by (Kind, Name): an agent DM keyed by the counterparty handle, or an uncollared pipe keyed by its target.

View Source
const (
	DirIn  = "in"  // received
	DirOut = "out" // sent by me (never replayable from the wire)
)

Message directions.

Variables

This section is empty.

Functions

This section is empty.

Types

type Message

type Message struct {
	ID        string `json:"id"`
	Dir       string `json:"dir"`
	Sender    string `json:"sender"`
	Subject   string `json:"subject,omitempty"`
	Payload   string `json:"payload"`
	CreatedAt string `json:"created_at"`
}

Message is one stored envelope. CreatedAt is the daemon's RFC3339-UTC stamp, which sorts lexically.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is a per-identity chat store rooted at <home>/chat/<handle>/.

func Open

func Open(home, handle string) (*Store, error)

Open resolves (and creates) <home>/chat/<handle>/ and loads any existing window files into memory.

func (*Store) AddPipe

func (s *Store) AddPipe(name, label string) error

AddPipe creates an (initially empty) pipe window so it shows in the roster before any message arrives.

func (*Store) Flush

func (s *Store) Flush() error

Flush writes every dirty window to disk (atomic temp+rename per file).

func (*Store) Ingest

func (s *Store) Ingest(kind, name, label string, m Message) (bool, error)

Ingest records a message in the (kind, name) window, creating the window if needed. Idempotent by Message.ID. Returns whether it was newly added. The write is buffered in memory; call Flush to persist.

func (*Store) MarkRead

func (s *Store) MarkRead(kind, name string) error

MarkRead advances the window's read marker to its newest message (unread→0).

func (*Store) Messages

func (s *Store) Messages(kind, name string) ([]Message, error)

Messages returns a window's messages in chronological (CreatedAt) order.

func (*Store) Migrate added in v0.54.0

func (s *Store) Migrate(fromKind, toKind, name string) error

Migrate re-keys a window from fromKind to toKind (same name): a simple re-key when the target is absent, or a merge (dedup by id, keeping the later read marker) when it exists. The old file is deleted; the target is written on the next Flush. Used when chat learns a handle it first saw as an agent DM is actually a message source, so a restart can't rebuild two windows for one counterparty.

func (*Store) RemovePipe

func (s *Store) RemovePipe(name string) error

RemovePipe forgets a pipe window and deletes its file.

func (*Store) Unread

func (s *Store) Unread(kind, name string) (int, error)

Unread counts received messages newer than the window's read marker.

func (*Store) Windows

func (s *Store) Windows() ([]Window, error)

Windows lists every known conversation with its current unread count, deterministically ordered (agents before pipes, then by name).

type Window

type Window struct {
	Kind   string
	Name   string
	Label  string
	Unread int
}

Window is a roster entry as the store sees it.

Jump to

Keyboard shortcuts

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