store

package
v0.0.17 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2026 License: MIT Imports: 11 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

Index

Constants

View Source
const BeeAgentID = "bee"

BeeAgentID is the agent_id value used for bee brain session tracking.

Variables

This section is empty.

Functions

func InitDB

func InitDB(dbPath string) (*sql.DB, error)

Types

type BatchMsg

type BatchMsg struct {
	ID            string
	SessionKey    string
	Platform      string
	Content       string
	Raw           string
	PlatformMsgID string
	MessageTime   int64
	Status        string // "received" or "merged"
	MergedInto    string // non-empty only when Status == "merged"
}

BatchMsg is a single row for a bulk insert via CreateBatch.

type ClaimedMessage

type ClaimedMessage struct {
	ID         string
	SessionKey string
	Platform   string
	Content    string
	RetryCount int
}

CreateBatch inserts multiple message rows in a single transaction using ClaimedMessage is a bee_platform_messages row claimed by the Feeder.

type ExecutionStore

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

func NewExecutionStore

func NewExecutionStore(db *sql.DB, logsDir string) *ExecutionStore

func (*ExecutionStore) CountSessions added in v0.0.10

func (s *ExecutionStore) CountSessions() (int, error)

CountSessions returns the total number of distinct sessions.

func (*ExecutionStore) CountSessionsByWorkerID added in v0.0.10

func (s *ExecutionStore) CountSessionsByWorkerID(workerID string) (int, error)

CountSessionsByWorkerID returns the total number of distinct sessions for a worker.

func (*ExecutionStore) Create

func (s *ExecutionStore) Create(workerID, triggerInput, sessionID string) (model.WorkerExecution, error)

func (*ExecutionStore) CreateBeeExecution

func (s *ExecutionStore) CreateBeeExecution(sessionID, triggerInput string) (model.WorkerExecution, error)

func (*ExecutionStore) GetByID

func (s *ExecutionStore) GetByID(id string) (model.WorkerExecution, error)

func (*ExecutionStore) GetBySessionID

func (s *ExecutionStore) GetBySessionID(sessionID string) (model.WorkerExecution, error)

func (*ExecutionStore) GetRunningByWorkerID added in v0.0.13

func (s *ExecutionStore) GetRunningByWorkerID(workerID string) (*model.WorkerExecution, error)

GetRunningByWorkerID returns the currently running execution for a worker, or nil if none.

func (*ExecutionStore) List

func (s *ExecutionStore) List() ([]model.WorkerExecution, error)

func (*ExecutionStore) ListBeeExecutions

func (s *ExecutionStore) ListBeeExecutions(limit int) ([]model.WorkerExecution, error)

ListBeeExecutions returns the bee's own execution history (worker_id IS NULL).

func (*ExecutionStore) ListBySessionID

func (s *ExecutionStore) ListBySessionID(sessionID string) ([]model.WorkerExecution, error)

func (*ExecutionStore) ListByWorkerID

func (s *ExecutionStore) ListByWorkerID(workerID string) ([]model.WorkerExecution, error)

func (*ExecutionStore) ListPaginated added in v0.0.10

func (s *ExecutionStore) ListPaginated(limit, offset int) ([]model.WorkerExecution, error)

ListPaginated returns executions grouped by session with pagination at the session level.

func (*ExecutionStore) ListPaginatedByWorkerID added in v0.0.10

func (s *ExecutionStore) ListPaginatedByWorkerID(workerID string, limit, offset int) ([]model.WorkerExecution, error)

ListPaginatedByWorkerID returns executions for a worker grouped by session with pagination at the session level.

func (*ExecutionStore) ListRecent

func (s *ExecutionStore) ListRecent(limit int) ([]model.WorkerExecution, error)

ListRecent returns the most recent executions (all types).

func (*ExecutionStore) PrepareLogPath added in v0.0.13

func (s *ExecutionStore) PrepareLogPath(id string, startedAt *int64) (string, error)

PrepareLogPath creates the date-partitioned log directory, records the log path in the DB, and returns the path. Must be called before launching the process so that the invoker can redirect stdout/stderr to the file. startedAt is used for date partitioning; falls back to time.Now() if nil.

func (*ExecutionStore) ReadLog added in v0.0.5

func (s *ExecutionStore) ReadLog(id string) (string, error)

ReadLog returns the log content for an execution. Returns empty string (no error) when no log path is set or the file does not yet exist.

func (*ExecutionStore) UpdatePID

func (s *ExecutionStore) UpdatePID(id string, pid int) error

func (*ExecutionStore) UpdateResult

func (s *ExecutionStore) UpdateResult(id string, result string, status model.ExecutionStatus) error

func (*ExecutionStore) UpdateStatus

func (s *ExecutionStore) UpdateStatus(id string, status model.ExecutionStatus) error

type InboundMessage

type InboundMessage struct {
	ID         string
	Content    string
	ReceivedAt int64
}

InboundMessage is a non-merged bee_platform_messages row for display in chat history.

type LocalReply

type LocalReply struct {
	ID         string `json:"id"`
	SessionKey string `json:"session_key"`
	Content    string `json:"content"`
	CreatedAt  int64  `json:"created_at"`
}

LocalReply is a row from the local_replies table.

type LocalReplyStore

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

LocalReplyStore manages the local_replies table.

func NewLocalReplyStore

func NewLocalReplyStore(db *sql.DB) *LocalReplyStore

NewLocalReplyStore constructs a LocalReplyStore.

func (*LocalReplyStore) Create

func (s *LocalReplyStore) Create(ctx context.Context, id, sessionKey, content string) error

Create inserts a new reply row.

func (*LocalReplyStore) DeleteBySession

func (s *LocalReplyStore) DeleteBySession(ctx context.Context, sessionKey string) error

DeleteBySession removes all replies for the given session key.

func (*LocalReplyStore) ListBySession

func (s *LocalReplyStore) ListBySession(ctx context.Context, sessionKey string) ([]LocalReply, error)

ListBySession returns all replies for a session key ordered by created_at.

type LocalSession

type LocalSession struct {
	ID        string `json:"id"`
	Name      string `json:"name"`
	CreatedAt int64  `json:"created_at"`
	UpdatedAt int64  `json:"updated_at"`
}

LocalSession is a row from the local_sessions table.

type LocalSessionStore

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

LocalSessionStore manages the local_sessions table.

func NewLocalSessionStore

func NewLocalSessionStore(db *sql.DB) *LocalSessionStore

NewLocalSessionStore constructs a LocalSessionStore.

func (*LocalSessionStore) Create

func (s *LocalSessionStore) Create(ctx context.Context, id, name string) error

Create inserts a new local session.

func (*LocalSessionStore) Delete

func (s *LocalSessionStore) Delete(ctx context.Context, id string) error

Delete removes a session by ID.

func (*LocalSessionStore) List

List returns all sessions ordered by updated_at descending.

func (*LocalSessionStore) TouchUpdatedAt

func (s *LocalSessionStore) TouchUpdatedAt(ctx context.Context, id string) error

TouchUpdatedAt bumps updated_at to now for the given session. Called by the HTTP handler on every message send so the session list stays sorted by activity.

type Memory

type Memory struct {
	Key       string `json:"key"`
	Value     string `json:"value"`
	UpdatedAt int64  `json:"updated_at"`
}

type MemoryStore

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

func NewMemoryStore

func NewMemoryStore(db *sql.DB) *MemoryStore

func (*MemoryStore) Delete

func (s *MemoryStore) Delete(scope, key string) error

func (*MemoryStore) Get

func (s *MemoryStore) Get(scope, key string) (*Memory, error)

func (*MemoryStore) ListByScope

func (s *MemoryStore) ListByScope(scope string, limit int) ([]Memory, error)

func (*MemoryStore) Save

func (s *MemoryStore) Save(scope, key, value string) error

type MessageStore

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

MessageStore persists platform messages to the bee_platform_messages table.

func NewMessageStore

func NewMessageStore(db *sql.DB) *MessageStore

NewMessageStore constructs a MessageStore.

func (*MessageStore) ClaimBatch

func (s *MessageStore) ClaimBatch(ctx context.Context, batchSize int) ([]ClaimedMessage, error)

ClaimBatch atomically selects up to batchSize 'received' messages — at most one per session_key — skipping any session that already has a message in 'feeding' status. Within each session, the message with the earliest received_at is selected (FIFO).

func (*MessageStore) CountReceived

func (s *MessageStore) CountReceived(ctx context.Context) (int, error)

CountReceived returns the number of messages with status 'received'.

func (*MessageStore) Create

func (s *MessageStore) Create(ctx context.Context, id, sessionKey, platform, content, raw, platformMsgID string, messageTime int64) (bool, error)

Create inserts a new message record with status "received". Returns inserted=false (no error) when platform_msg_id is non-empty and already exists. If platform_msg_id is empty, the insert always proceeds (no dedup). messageTime is stored as received_at; pass 0 to use server time.

func (*MessageStore) CreateBatch

func (s *MessageStore) CreateBatch(ctx context.Context, msgs []BatchMsg) (int64, error)

INSERT OR IGNORE. Returns the number of rows actually inserted. MessageTime is used as received_at; falls back to time.Now().UnixMilli() if zero.

func (*MessageStore) DeleteBySessionKey

func (s *MessageStore) DeleteBySessionKey(ctx context.Context, sessionKey string) error

DeleteBySessionKey removes all bee_platform_messages for the given session key.

func (*MessageStore) FetchMergedContent added in v0.0.6

func (s *MessageStore) FetchMergedContent(ctx context.Context, primaryID string) ([]string, error)

FetchMergedContent returns the content of all messages merged into the given primary ID, ordered by received_at ASC (chronological arrival order).

func (*MessageStore) GetByID

func (s *MessageStore) GetByID(ctx context.Context, id string) (StoredMessage, error)

GetByID fetches the platform, session_key, and raw fields for a single message.

func (*MessageStore) ListBySessionKey

func (s *MessageStore) ListBySessionKey(ctx context.Context, sessionKey string) ([]InboundMessage, error)

ListBySessionKey returns all non-merged messages for a session, ordered by received_at ASC.

func (*MessageStore) MarkBeeProcessed

func (s *MessageStore) MarkBeeProcessed(ctx context.Context, ids []string) error

MarkBeeProcessed sets status to 'bee_processed' for the given message IDs.

func (*MessageStore) MarkMerged

func (s *MessageStore) MarkMerged(ctx context.Context, primaryID string, mergedIDs []string) error

MarkMerged sets primaryID status to "merged" and records merged_into on all mergedIDs.

func (*MessageStore) ResetFeedingBatch

func (s *MessageStore) ResetFeedingBatch(ctx context.Context, ids []string) error

ResetFeedingBatch restores 'feeding' messages back to 'received'.

func (*MessageStore) ResetFeedingToReceived

func (s *MessageStore) ResetFeedingToReceived(ctx context.Context) ([]string, error)

ResetFeedingToReceived resets all messages stuck in 'feeding' back to 'received'. Returns the IDs of affected rows so the caller can delete orphaned pending tasks.

func (*MessageStore) RollbackWithRetry added in v0.0.2

func (s *MessageStore) RollbackWithRetry(ctx context.Context, ids []string, maxRetries int) error

RollbackWithRetry increments retry_count for each message and resets status to 'received' for messages below maxRetries, or 'failed' for those that have reached the limit. Callers determine which IDs are permanently failed from their in-memory ClaimedMessage.RetryCount values (see Feeder.rollback).

func (*MessageStore) SetStatus

func (s *MessageStore) SetStatus(ctx context.Context, id, status string) error

SetStatus updates the status of a single message.

func (*MessageStore) UpdateStatusBatch

func (s *MessageStore) UpdateStatusBatch(ctx context.Context, ids []string, status string) error

UpdateStatusBatch sets the same status on all provided message IDs.

type SessionAgent added in v0.0.2

type SessionAgent struct {
	AgentID   string
	AgentType string // "bee" or "worker"
	Name      string // worker name, "bee", or "(deleted)"
	UpdatedAt int64
}

SessionAgent represents one agent's session context entry, enriched with a human-readable name.

type SessionStore

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

SessionStore persists session context to the session_contexts table.

func NewSessionStore

func NewSessionStore(db *sql.DB) *SessionStore

NewSessionStore constructs a SessionStore.

func (*SessionStore) ClearSessionContexts

func (s *SessionStore) ClearSessionContexts(ctx context.Context, sessionKey string) error

ClearSessionContexts deletes all session_contexts rows for sessionKey, resetting session state for bee and all workers under that key.

func (*SessionStore) DeleteWorkerSessionContext added in v0.0.2

func (s *SessionStore) DeleteWorkerSessionContext(ctx context.Context, sessionKey, workerID string) error

DeleteWorkerSessionContext removes the session context row for one worker. Deleting a non-existent row is not an error.

func (*SessionStore) GetSessionContext

func (s *SessionStore) GetSessionContext(ctx context.Context, sessionKey, agentID string) (string, error)

GetSessionContext returns the session_id for (sessionKey, agentID). Returns ("", nil) when no row exists — this is normal for the first message, not a database error. Returns non-nil error only on database failure.

func (*SessionStore) ListSessionContexts added in v0.0.2

func (s *SessionStore) ListSessionContexts(ctx context.Context, sessionKey string) ([]SessionAgent, error)

ListSessionContexts returns all agents with session contexts for sessionKey, ordered by updated_at DESC. Worker names are resolved via LEFT JOIN; deleted workers appear as "(deleted)". AgentType is derived in Go from AgentID.

func (*SessionStore) UpsertSessionContext

func (s *SessionStore) UpsertSessionContext(ctx context.Context, sessionKey, agentID, sessionID string) error

UpsertSessionContext writes or overwrites the session_id for (sessionKey, agentID).

type StoredMessage

type StoredMessage struct {
	Platform   string
	SessionKey string
	Raw        string
}

StoredMessage is the subset of bee_platform_messages fields needed by platform senders.

type TaskFilter added in v0.0.2

type TaskFilter struct {
	MessageID  string
	SessionKey string
	WorkerID   string
	Status     string // comma-separated, e.g. "pending,running"
	Type       string // comma-separated, e.g. "immediate,countdown"
	Limit      int    // 0 means no limit
	Offset     int
}

TaskFilter specifies filtering criteria for List and CountTasks. message_id and session_key are mutually exclusive.

type TaskStore

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

TaskStore handles persistence for bee tasks.

func NewTaskStore

func NewTaskStore(db *sql.DB) *TaskStore

NewTaskStore creates a TaskStore backed by db.

func (*TaskStore) CancelBySessionKey

func (s *TaskStore) CancelBySessionKey(ctx context.Context, sessionKey string) (int64, error)

CancelBySessionKey cancels all pending/running tasks for a given session. Returns the number of tasks cancelled.

func (*TaskStore) CancelByWorkerID

func (s *TaskStore) CancelByWorkerID(ctx context.Context, workerID string) error

CancelByWorkerID cancels all pending/running tasks for a given worker.

func (*TaskStore) CancelTask

func (s *TaskStore) CancelTask(ctx context.Context, taskID string) error

CancelTask sets a task status to cancelled.

func (*TaskStore) ClaimDueTasks

func (s *TaskStore) ClaimDueTasks(ctx context.Context, nowMS int64, scheduledNextRuns map[string]int64) ([]model.ClaimedTask, error)

ClaimDueTasks atomically selects all pending tasks that are due at or before nowMS, marks immediate/countdown tasks as running, and sets scheduled tasks' next_run_at to the pre-computed value from scheduledNextRuns (keyed by task ID). scheduledNextRuns may be nil if there are no due scheduled tasks.

func (*TaskStore) CompleteScheduledTask

func (s *TaskStore) CompleteScheduledTask(ctx context.Context, taskID string) (bool, error)

CompleteScheduledTask resets a scheduled task back to pending so it can be picked up again by the next cron cycle. If the task has been cancelled, the status is preserved and the method returns false.

func (*TaskStore) CompleteTask added in v0.0.16

func (s *TaskStore) CompleteTask(ctx context.Context, taskID string) error

CompleteTask marks a task as completed on successful worker exit. For scheduled tasks with a cron expression, it resets to pending instead so the task is picked up again on the next scheduled run.

func (*TaskStore) CountAllByStatus

func (s *TaskStore) CountAllByStatus(ctx context.Context) (map[string]int, error)

CountAllByStatus returns a map of task status to count across all tasks.

func (*TaskStore) CountPendingByWorkerID

func (s *TaskStore) CountPendingByWorkerID(ctx context.Context, workerID string) (int, error)

CountPendingByWorkerID returns the number of pending tasks for a given worker.

func (*TaskStore) CountScheduledActive

func (s *TaskStore) CountScheduledActive(ctx context.Context) (int, error)

CountScheduledActive returns the number of pending scheduled tasks.

func (*TaskStore) CountTasks added in v0.0.14

func (s *TaskStore) CountTasks(ctx context.Context, f TaskFilter) (int, error)

CountTasks returns the number of tasks matching the given filter (ignores Limit/Offset).

func (*TaskStore) Create

func (s *TaskStore) Create(ctx context.Context, t model.Task) (string, error)

Create inserts a new task and returns its generated ID.

func (*TaskStore) DeletePendingByMessageIDs

func (s *TaskStore) DeletePendingByMessageIDs(ctx context.Context, messageIDs []string) error

DeletePendingByMessageIDs removes pending tasks belonging to the given message IDs.

func (*TaskStore) FailTask

func (s *TaskStore) FailTask(ctx context.Context, taskID string) error

FailTask marks a task as failed. For scheduled tasks with a cron expression, it resets to pending instead so the task retries on the next scheduled run. Called by the dispatcher when a worker process exits abnormally.

func (*TaskStore) GetByID

func (s *TaskStore) GetByID(ctx context.Context, id string) (model.Task, error)

GetByID fetches a single task by ID.

func (*TaskStore) GetTaskByExecutionID

func (s *TaskStore) GetTaskByExecutionID(ctx context.Context, executionID string) (*model.Task, error)

GetTaskByExecutionID returns the task with the given execution_id, or nil if not found.

func (*TaskStore) List added in v0.0.2

func (s *TaskStore) List(ctx context.Context, f TaskFilter) ([]model.Task, error)

List returns tasks matching the given filter. If session_key is set, tasks are joined with bee_platform_messages to resolve the session. Results are ordered by created_at DESC.

func (*TaskStore) ListByMessageID

func (s *TaskStore) ListByMessageID(ctx context.Context, messageID, status, taskType string) ([]model.Task, error)

ListByMessageID returns tasks for a given message, optionally filtered by status and/or type.

func (*TaskStore) ListBySessionKey

func (s *TaskStore) ListBySessionKey(ctx context.Context, sessionKey, status, taskType string) ([]model.Task, error)

ListBySessionKey returns tasks whose originating message belongs to the given session. status and taskType support comma-separated values (e.g., "pending,running"); empty means all.

func (*TaskStore) PeekDueScheduledTasks added in v0.0.13

func (s *TaskStore) PeekDueScheduledTasks(ctx context.Context, nowMS int64) ([]model.Task, error)

PeekDueScheduledTasks returns all pending scheduled tasks whose next_run_at is at or before nowMS (or NULL). Read-only — no updates, no locking. Used by Scheduler.poll to compute real next_run_at values before claiming.

func (*TaskStore) ResetRunningToPending

func (s *TaskStore) ResetRunningToPending(ctx context.Context) (int64, error)

ResetRunningToPending resets all running tasks back to pending.

func (*TaskStore) SetExecution

func (s *TaskStore) SetExecution(ctx context.Context, taskID, executionID, status string) error

SetExecution writes execution_id and status back to a task.

func (*TaskStore) UpdateStatus

func (s *TaskStore) UpdateStatus(ctx context.Context, taskID, status string) error

UpdateStatus sets only the status of a task. Unlike SetExecution, it does not touch execution_id.

type WorkerStore

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

func NewWorkerStore

func NewWorkerStore(db *sql.DB) *WorkerStore

func (*WorkerStore) CountByStatus

func (s *WorkerStore) CountByStatus() (map[string]int, error)

CountByStatus returns a map of worker status to count.

func (*WorkerStore) Create

func (s *WorkerStore) Create(w model.Worker) (model.Worker, error)

func (*WorkerStore) Delete

func (s *WorkerStore) Delete(id string) error

func (*WorkerStore) GetByID

func (s *WorkerStore) GetByID(id string) (model.Worker, error)

func (*WorkerStore) List

func (s *WorkerStore) List() ([]model.Worker, error)

func (*WorkerStore) Update

func (s *WorkerStore) Update(w model.Worker) (model.Worker, error)

func (*WorkerStore) UpdateStatus

func (s *WorkerStore) UpdateStatus(id string, status model.WorkerStatus) error

Jump to

Keyboard shortcuts

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