Files
wehub-resource-sync 26f897c1ec
release / release-please (push) Failing after 1m49s
docs / build (push) Failing after 6m34s
release / build-and-upload (arm64, linux) (push) Has been cancelled
release / build-and-upload (arm64, windows) (push) Has been cancelled
release / build-darwin (amd64, darwin) (push) Has been cancelled
release / checksums (push) Has been cancelled
release / finalize (push) Has been cancelled
release / build-darwin (arm64, darwin) (push) Has been cancelled
release / build-and-upload (amd64, linux) (push) Has been cancelled
release / build-and-upload (amd64, windows) (push) Has been cancelled
docs / deploy (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:34:16 +08:00

58 lines
1.6 KiB
Go

package db
import (
"database/sql"
"fmt"
"time"
)
// IntentCacheEntry is a cached summarization for a known agent session.
type IntentCacheEntry struct {
CacheKey string
Summary string
AgentName string
SessionID string
CreatedAt int64
}
// GetIntentCache returns the cached summary for a key, or nil if absent.
func (d *DB) GetIntentCache(key string) (*IntentCacheEntry, error) {
e := &IntentCacheEntry{}
err := d.sql.QueryRow(
`SELECT cache_key, summary, agent_name, session_id, created_at FROM intent_cache WHERE cache_key = ?`, key,
).Scan(&e.CacheKey, &e.Summary, &e.AgentName, &e.SessionID, &e.CreatedAt)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("get intent cache: %w", err)
}
return e, nil
}
// PutIntentCache inserts or replaces an intent cache entry.
func (d *DB) PutIntentCache(e IntentCacheEntry) error {
if e.CreatedAt == 0 {
e.CreatedAt = now()
}
_, err := d.sql.Exec(
`INSERT OR REPLACE INTO intent_cache (cache_key, summary, agent_name, session_id, created_at) VALUES (?, ?, ?, ?, ?)`,
e.CacheKey, e.Summary, e.AgentName, e.SessionID, e.CreatedAt,
)
if err != nil {
return fmt.Errorf("put intent cache: %w", err)
}
return nil
}
// CleanupOldIntentCache deletes entries older than maxAge. Returns rows deleted.
func (d *DB) CleanupOldIntentCache(maxAge time.Duration) (int64, error) {
cutoff := time.Now().Add(-maxAge).Unix()
result, err := d.sql.Exec(`DELETE FROM intent_cache WHERE created_at < ?`, cutoff)
if err != nil {
return 0, fmt.Errorf("cleanup intent cache: %w", err)
}
n, _ := result.RowsAffected()
return n, nil
}