Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

13.1 KB
Raw Blame
// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.29.0
// source: identity.sql

package store

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const acquireAdvisorySessionLock = `-- name: AcquireAdvisorySessionLock :exec
SELECT pg_advisory_lock(hashtextextended($1, 0))
`

func (q *Queries) AcquireAdvisorySessionLock(ctx context.Context, lockKey string) error {
	_, err := q.db.Exec(ctx, acquireAdvisorySessionLock, lockKey)
	return err
}

const acquireAdvisoryTransactionLock = `-- name: AcquireAdvisoryTransactionLock :exec
SELECT pg_advisory_xact_lock(hashtextextended($1, 0))
`

func (q *Queries) AcquireAdvisoryTransactionLock(ctx context.Context, lockKey string) error {
	_, err := q.db.Exec(ctx, acquireAdvisoryTransactionLock, lockKey)
	return err
}

const acquireAdvisoryTransactionSharedLock = `-- name: AcquireAdvisoryTransactionSharedLock :exec
SELECT pg_advisory_xact_lock_shared(hashtextextended($1, 0))
`

func (q *Queries) AcquireAdvisoryTransactionSharedLock(ctx context.Context, lockKey string) error {
	_, err := q.db.Exec(ctx, acquireAdvisoryTransactionSharedLock, lockKey)
	return err
}

const createSession = `-- name: CreateSession :one
INSERT INTO auth_sessions(id, account_id, token_hash, expires_at)
VALUES ($1, $2, $3, LEAST($4::timestamptz, now() + $5::interval))
RETURNING id, account_id, created_at, expires_at
`

type CreateSessionParams struct {
	ID          string             `json:"id"`
	AccountID   string             `json:"account_id"`
	TokenHash   []byte             `json:"token_hash"`
	ExpiresAt   pgtype.Timestamptz `json:"expires_at"`
	MaxLifetime pgtype.Interval    `json:"max_lifetime"`
}

type CreateSessionRow struct {
	ID        string             `json:"id"`
	AccountID string             `json:"account_id"`
	CreatedAt pgtype.Timestamptz `json:"created_at"`
	ExpiresAt pgtype.Timestamptz `json:"expires_at"`
}

func (q *Queries) CreateSession(ctx context.Context, arg CreateSessionParams) (CreateSessionRow, error) {
	row := q.db.QueryRow(ctx, createSession,
		arg.ID,
		arg.AccountID,
		arg.TokenHash,
		arg.ExpiresAt,
		arg.MaxLifetime,
	)
	var i CreateSessionRow
	err := row.Scan(
		&i.ID,
		&i.AccountID,
		&i.CreatedAt,
		&i.ExpiresAt,
	)
	return i, err
}

const getAccountByEmail = `-- name: GetAccountByEmail :one
SELECT id, email, password_hash, created_at
FROM accounts
WHERE lower(email) = lower($1)
`

type GetAccountByEmailRow struct {
	ID           string             `json:"id"`
	Email        string             `json:"email"`
	PasswordHash string             `json:"password_hash"`
	CreatedAt    pgtype.Timestamptz `json:"created_at"`
}

// Login reads the Account without a lock and hashes outside any transaction.
func (q *Queries) GetAccountByEmail(ctx context.Context, email string) (GetAccountByEmailRow, error) {
	row := q.db.QueryRow(ctx, getAccountByEmail, email)
	var i GetAccountByEmailRow
	err := row.Scan(
		&i.ID,
		&i.Email,
		&i.PasswordHash,
		&i.CreatedAt,
	)
	return i, err
}

const getActiveSession = `-- name: GetActiveSession :one
SELECT s.id, s.account_id, s.created_at, s.expires_at,
       a.email, a.created_at AS account_created_at,
       human.id AS human_actor_id, human.tenant_id AS human_tenant_id,
       human.display_name AS human_display_name
FROM auth_sessions s
JOIN accounts a ON a.id = s.account_id
JOIN actors human ON human.account_id = a.id AND human.kind = 'human'
WHERE s.token_hash = $1 AND s.revoked_at IS NULL AND s.expires_at > now()
`

type GetActiveSessionRow struct {
	ID               string             `json:"id"`
	AccountID        string             `json:"account_id"`
	CreatedAt        pgtype.Timestamptz `json:"created_at"`
	ExpiresAt        pgtype.Timestamptz `json:"expires_at"`
	Email            string             `json:"email"`
	AccountCreatedAt pgtype.Timestamptz `json:"account_created_at"`
	HumanActorID     string             `json:"human_actor_id"`
	HumanTenantID    string             `json:"human_tenant_id"`
	HumanDisplayName string             `json:"human_display_name"`
}

// A session resolves its Account's single Human Actor once; that Actor's
// tenant_id is the deployment's Space, and every handler reads both from here.
func (q *Queries) GetActiveSession(ctx context.Context, tokenHash []byte) (GetActiveSessionRow, error) {
	row := q.db.QueryRow(ctx, getActiveSession, tokenHash)
	var i GetActiveSessionRow
	err := row.Scan(
		&i.ID,
		&i.AccountID,
		&i.CreatedAt,
		&i.ExpiresAt,
		&i.Email,
		&i.AccountCreatedAt,
		&i.HumanActorID,
		&i.HumanTenantID,
		&i.HumanDisplayName,
	)
	return i, err
}

const getActor = `-- name: GetActor :one
SELECT id, tenant_id, kind, display_name, account_id, handle, created_at
FROM actors
WHERE id=$1 AND tenant_id=$2
`

type GetActorParams struct {
	ID       string `json:"id"`
	TenantID string `json:"tenant_id"`
}

func (q *Queries) GetActor(ctx context.Context, arg GetActorParams) (Actor, error) {
	row := q.db.QueryRow(ctx, getActor, arg.ID, arg.TenantID)
	var i Actor
	err := row.Scan(
		&i.ID,
		&i.TenantID,
		&i.Kind,
		&i.DisplayName,
		&i.AccountID,
		&i.Handle,
		&i.CreatedAt,
	)
	return i, err
}

const getActorForTenantAccount = `-- name: GetActorForTenantAccount :one
SELECT target.id, target.tenant_id, target.kind, target.display_name,
       target.account_id, target.handle, target.created_at
FROM actors target
JOIN actors caller
  ON caller.tenant_id=target.tenant_id AND caller.account_id=$3 AND caller.kind='human'
WHERE target.tenant_id=$1 AND target.id=$2
  AND (
    target.kind<>'agent' OR EXISTS (
      SELECT 1 FROM agent_registrations registration
      WHERE registration.actor_id=target.id
        AND registration.tenant_id=target.tenant_id
        AND registration.status='active'
        AND (
          registration.visibility_scope='workspace' OR
          registration.created_by_actor_id=caller.id OR
          EXISTS (
            SELECT 1
            FROM active_room_members caller_membership
            JOIN active_room_members agent_membership
              ON agent_membership.room_id=caller_membership.room_id
             AND agent_membership.actor_id=registration.actor_id
            WHERE caller_membership.actor_id=caller.id
          )
        )
    )
  )
`

type GetActorForTenantAccountParams struct {
	TenantID  string      `json:"tenant_id"`
	ID        string      `json:"id"`
	AccountID pgtype.Text `json:"account_id"`
}

func (q *Queries) GetActorForTenantAccount(ctx context.Context, arg GetActorForTenantAccountParams) (Actor, error) {
	row := q.db.QueryRow(ctx, getActorForTenantAccount, arg.TenantID, arg.ID, arg.AccountID)
	var i Actor
	err := row.Scan(
		&i.ID,
		&i.TenantID,
		&i.Kind,
		&i.DisplayName,
		&i.AccountID,
		&i.Handle,
		&i.CreatedAt,
	)
	return i, err
}

const getHumanActor = `-- name: GetHumanActor :one
SELECT actors.id,actors.tenant_id,actors.kind,actors.display_name,
       actors.account_id,actors.handle,actors.created_at
FROM actors
WHERE actors.tenant_id=$1 AND actors.account_id=$2 AND actors.kind='human'
`

type GetHumanActorParams struct {
	TenantID  string      `json:"tenant_id"`
	AccountID pgtype.Text `json:"account_id"`
}

// The one lookup of an Account's Human Actor in a Tenant.
func (q *Queries) GetHumanActor(ctx context.Context, arg GetHumanActorParams) (Actor, error) {
	row := q.db.QueryRow(ctx, getHumanActor, arg.TenantID, arg.AccountID)
	var i Actor
	err := row.Scan(
		&i.ID,
		&i.TenantID,
		&i.Kind,
		&i.DisplayName,
		&i.AccountID,
		&i.Handle,
		&i.CreatedAt,
	)
	return i, err
}

const listActorsForAccount = `-- name: ListActorsForAccount :many
SELECT actors.id,actors.tenant_id,actors.kind,actors.display_name,
       actors.account_id,actors.handle,actors.created_at
FROM actors
WHERE actors.account_id = $1
ORDER BY actors.tenant_id,actors.id
`

func (q *Queries) ListActorsForAccount(ctx context.Context, accountID pgtype.Text) ([]Actor, error) {
	rows, err := q.db.Query(ctx, listActorsForAccount, accountID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Actor{}
	for rows.Next() {
		var i Actor
		if err := rows.Scan(
			&i.ID,
			&i.TenantID,
			&i.Kind,
			&i.DisplayName,
			&i.AccountID,
			&i.Handle,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listActorsForTenantAccount = `-- name: ListActorsForTenantAccount :many
SELECT target.id, target.tenant_id, target.kind, target.display_name,
       target.account_id, target.handle, target.created_at,
       target_account.email AS account_email
FROM actors target
LEFT JOIN accounts target_account ON target_account.id=target.account_id
JOIN actors caller
  ON caller.tenant_id=target.tenant_id
 AND caller.account_id=$1::text
 AND caller.kind='human'
WHERE target.tenant_id = $2::text
  AND (
    target.kind<>'agent' OR EXISTS (
      SELECT 1 FROM agent_registrations registration
      WHERE registration.actor_id=target.id
        AND registration.tenant_id=target.tenant_id
        AND registration.status='active'
        AND (
          registration.visibility_scope='workspace' OR
          registration.created_by_actor_id=caller.id OR
          EXISTS (
            SELECT 1
            FROM active_room_members caller_membership
            JOIN active_room_members agent_membership
              ON agent_membership.room_id=caller_membership.room_id
             AND agent_membership.actor_id=registration.actor_id
            WHERE caller_membership.actor_id=caller.id
          )
        )
    )
  )
  AND ($3::text = '' OR (target.created_at, target.id) > (
      SELECT created_at, id FROM actors WHERE id = $3::text AND tenant_id = $2::text
  ))
ORDER BY target.created_at, target.id
LIMIT $4::int
`

type ListActorsForTenantAccountParams struct {
	AccountID string `json:"account_id"`
	TenantID  string `json:"tenant_id"`
	Cursor    string `json:"cursor"`
	PageLimit int32  `json:"page_limit"`
}

type ListActorsForTenantAccountRow struct {
	ID           string             `json:"id"`
	TenantID     string             `json:"tenant_id"`
	Kind         string             `json:"kind"`
	DisplayName  string             `json:"display_name"`
	AccountID    pgtype.Text        `json:"account_id"`
	Handle       pgtype.Text        `json:"handle"`
	CreatedAt    pgtype.Timestamptz `json:"created_at"`
	AccountEmail pgtype.Text        `json:"account_email"`
}

func (q *Queries) ListActorsForTenantAccount(ctx context.Context, arg ListActorsForTenantAccountParams) ([]ListActorsForTenantAccountRow, error) {
	rows, err := q.db.Query(ctx, listActorsForTenantAccount,
		arg.AccountID,
		arg.TenantID,
		arg.Cursor,
		arg.PageLimit,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListActorsForTenantAccountRow{}
	for rows.Next() {
		var i ListActorsForTenantAccountRow
		if err := rows.Scan(
			&i.ID,
			&i.TenantID,
			&i.Kind,
			&i.DisplayName,
			&i.AccountID,
			&i.Handle,
			&i.CreatedAt,
			&i.AccountEmail,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const lockAccountPasswordHash = `-- name: LockAccountPasswordHash :one
SELECT password_hash FROM accounts WHERE id = $1 FOR UPDATE
`

// Login rechecks the verified hash under the Account row lock that password
// changes and set-password take, so an old password never signs in after a change.
func (q *Queries) LockAccountPasswordHash(ctx context.Context, id string) (string, error) {
	row := q.db.QueryRow(ctx, lockAccountPasswordHash, id)
	var password_hash string
	err := row.Scan(&password_hash)
	return password_hash, err
}

const releaseAdvisorySessionLock = `-- name: ReleaseAdvisorySessionLock :one
SELECT pg_advisory_unlock(hashtextextended($1, 0))
`

func (q *Queries) ReleaseAdvisorySessionLock(ctx context.Context, lockKey string) (bool, error) {
	row := q.db.QueryRow(ctx, releaseAdvisorySessionLock, lockKey)
	var pg_advisory_unlock bool
	err := row.Scan(&pg_advisory_unlock)
	return pg_advisory_unlock, err
}

const renewSession = `-- name: RenewSession :one
UPDATE auth_sessions
SET expires_at = LEAST(clock_timestamp() + $1::interval, created_at + $2::interval)
WHERE token_hash = $3
  AND revoked_at IS NULL AND expires_at > clock_timestamp()
  AND created_at + $2::interval > clock_timestamp()
  AND expires_at < created_at + $2::interval
  AND expires_at <= clock_timestamp() + $1::interval - $4::interval
RETURNING expires_at
`

type RenewSessionParams struct {
	IdleTtl         pgtype.Interval `json:"idle_ttl"`
	MaxLifetime     pgtype.Interval `json:"max_lifetime"`
	TokenHash       []byte          `json:"token_hash"`
	RenewalInterval pgtype.Interval `json:"renewal_interval"`
}

func (q *Queries) RenewSession(ctx context.Context, arg RenewSessionParams) (pgtype.Timestamptz, error) {
	row := q.db.QueryRow(ctx, renewSession,
		arg.IdleTtl,
		arg.MaxLifetime,
		arg.TokenHash,
		arg.RenewalInterval,
	)
	var expires_at pgtype.Timestamptz
	err := row.Scan(&expires_at)
	return expires_at, err
}

const revokeSession = `-- name: RevokeSession :exec
UPDATE auth_sessions
SET revoked_at = coalesce(revoked_at, now()),
    revoked_reason = CASE WHEN revoked_at IS NULL THEN 'logout' ELSE revoked_reason END
WHERE token_hash = $1
`

func (q *Queries) RevokeSession(ctx context.Context, tokenHash []byte) error {
	_, err := q.db.Exec(ctx, revokeSession, tokenHash)
	return err
}