database

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2026 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Overview

Package database provides shared database services for agent-go consumers:

  • ADK session.Service (lazy initialized, separate adk_sessions.db)
  • GORM-backed main database for common tables + business tables
  • MessageMetadataStore / SessionExtStore (GORM-persisted)
  • DB() accessor for business-layer queries and migrations

Database layout:

<appDataDir>/data/
  ├── <dbFileName>        ← main DB (GORM): message_metadata, session_ext + business tables
  └── adk_sessions.db     ← ADK internal: sessions, events, app_states, user_states

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Checkpoint

func Checkpoint(db *sql.DB) error

Checkpoint performs a WAL checkpoint (TRUNCATE mode) on a SQLite database.

func CommonModels

func CommonModels() []any

CommonModels returns all framework-level GORM models. Business layers should append their own models when calling AutoMigrate.

func MigrateCommon

func MigrateCommon(db *gorm.DB) error

MigrateCommon runs AutoMigrate for framework-level tables. Business layers should call this with their own db, then add business tables.

func Vacuum

func Vacuum(db *sql.DB) error

Vacuum reclaims unused space in a SQLite database.

Types

type DBService

type DBService struct {
	SessionExt *SessionExtStore
	// contains filtered or unexported fields
}

DBService provides ADK session.Service lazy initialization and a shared GORM database for common tables and business-layer extensions.

func NewDBService

func NewDBService(appDataDir, dbFileName string) *DBService

NewDBService creates a new DBService. The main database is opened immediately and common tables are migrated.

func (*DBService) DB

func (s *DBService) DB() *gorm.DB

DB returns the shared GORM database handle. Business layers use this for custom queries and AutoMigrate of business tables.

func (*DBService) DeleteADKEvents

func (s *DBService) DeleteADKEvents(sessionID string) error

DeleteADKEvents deletes all ADK events for a given session from adk_sessions.db.

func (*DBService) GetADKMaintenanceDB

func (s *DBService) GetADKMaintenanceDB() (*sql.DB, error)

GetADKMaintenanceDB opens a temporary read-write connection to adk_sessions.db for maintenance operations (WAL checkpoint, VACUUM). Caller must close the returned *sql.DB.

func (*DBService) GetADKSessionEvents

func (s *DBService) GetADKSessionEvents(sessionID string) ([]Message, error)

GetADKSessionEvents returns messages/events for a session from the ADK database.

func (*DBService) GetDBPath

func (s *DBService) GetDBPath() string

GetDBPath returns the database directory path.

func (*DBService) GetSessionService

func (s *DBService) GetSessionService() (session.Service, error)

GetSessionService returns the ADK session.Service (lazy initialized).

func (*DBService) ListADKSessions

func (s *DBService) ListADKSessions(appName string) ([]Session, error)

ListADKSessions returns sessions filtered by app name from the ADK database.

type Message

type Message struct {
	ID          string          `json:"id"`
	SessionID   string          `json:"session_id"`
	Seq         int64           `json:"seq"`
	TurnID      string          `json:"turn_id"`
	ClientMsgID string          `json:"client_msg_id,omitempty"`
	Role        string          `json:"role"`
	Content     string          `json:"content"`
	Reasoning   string          `json:"reasoning,omitempty"`
	ToolCall    json.RawMessage `json:"tool_call,omitempty"`
	Metadata    json.RawMessage `json:"metadata,omitempty"`
	CreatedAt   time.Time       `json:"created_at"`
}

Message is the generic message struct returned by ListHistory. This replaces msgdb.Message after the msgdb removal.

type Session

type Session struct {
	ID               string    `json:"id"`
	SessionID        string    `json:"session_id"`
	Title            string    `json:"title"`
	ApprovalMode     string    `json:"approval_mode"`
	WorkspaceID      string    `json:"workspace_id"`
	TokenUsage       string    `json:"token_usage"`
	LLMLastSentMsgID string    `json:"llm_last_sent_msg_id"`
	CreatedAt        time.Time `json:"created_at"`
	UpdatedAt        time.Time `json:"updated_at"`
}

Session is the generic session struct returned by ListSessions. This replaces msgdb.Session after the msgdb removal.

type SessionExtModel

type SessionExtModel struct {
	SessionID    string `gorm:"primaryKey;size:64"`
	ApprovalMode string `gorm:"size:16;default:default"`
	Title        string `gorm:"size:256;default:''"`
	TokenUsage   string `gorm:"type:text;default:'{}'"`
	SystemPrompt string `gorm:"type:text;default:''"`
}

SessionExtModel stores extended session data (approval_mode, title, token_usage, system_prompt) that ADK session.Service does not natively support.

func (SessionExtModel) TableName

func (SessionExtModel) TableName() string

type SessionExtStore

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

SessionExtStore provides GORM-persisted extended session data (title, token_usage, approval_mode, system_prompt) that ADK session.Service does not natively support.

func NewSessionExtStore

func NewSessionExtStore(db *gorm.DB) *SessionExtStore

NewSessionExtStore creates a GORM-backed session extension store.

func (*SessionExtStore) Delete

func (s *SessionExtStore) Delete(sessionID string) error

Delete removes the session extension record for a session.

func (*SessionExtStore) GetApprovalMode

func (s *SessionExtStore) GetApprovalMode(sessionID string) string

GetApprovalMode returns the approval mode for a session. Returns "default" if not set.

func (*SessionExtStore) GetSessionExt

func (s *SessionExtStore) GetSessionExt(sessionID string) *SessionExtModel

GetSessionExt returns the session extension record, creating a default one if not exists.

func (*SessionExtStore) GetSessionExtBatch

func (s *SessionExtStore) GetSessionExtBatch(sessionIDs []string) map[string]*SessionExtModel

GetSessionExtBatch returns multiple session extension records in a single query.

func (*SessionExtStore) SaveSessionExt

func (s *SessionExtStore) SaveSessionExt(m *SessionExtModel) error

SaveSessionExt persists the session extension record (create or update).

func (*SessionExtStore) SetApprovalMode

func (s *SessionExtStore) SetApprovalMode(sessionID, mode string) error

SetApprovalMode sets the approval mode for a session.

Jump to

Keyboard shortcuts

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