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.

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

package store

import (
	"context"

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

const allocateDeletingRoomSeq = `-- name: AllocateDeletingRoomSeq :one
UPDATE rooms
SET next_room_seq=next_room_seq+1
WHERE id=$1 AND deletion_started_at IS NOT NULL AND deleted_at IS NULL
RETURNING next_room_seq
`

func (q *Queries) AllocateDeletingRoomSeq(ctx context.Context, id string) (int64, error) {
	row := q.db.QueryRow(ctx, allocateDeletingRoomSeq, id)
	var next_room_seq int64
	err := row.Scan(&next_room_seq)
	return next_room_seq, err
}

const allocateRoomLifecycleSeq = `-- name: AllocateRoomLifecycleSeq :one
UPDATE rooms
SET next_room_seq=next_room_seq+1
WHERE id=$1 AND deleted_at IS NULL
RETURNING next_room_seq
`

func (q *Queries) AllocateRoomLifecycleSeq(ctx context.Context, id string) (int64, error) {
	row := q.db.QueryRow(ctx, allocateRoomLifecycleSeq, id)
	var next_room_seq int64
	err := row.Scan(&next_room_seq)
	return next_room_seq, err
}

const allocateRoomSeq = `-- name: AllocateRoomSeq :one
UPDATE rooms
SET next_room_seq=next_room_seq+1
WHERE id=$1 AND deletion_started_at IS NULL AND deleted_at IS NULL
RETURNING next_room_seq
`

func (q *Queries) AllocateRoomSeq(ctx context.Context, id string) (int64, error) {
	row := q.db.QueryRow(ctx, allocateRoomSeq, id)
	var next_room_seq int64
	err := row.Scan(&next_room_seq)
	return next_room_seq, err
}

const createMessageCreatedRoomEvent = `-- name: CreateMessageCreatedRoomEvent :one
WITH event AS (
    INSERT INTO room_events(event_id, tenant_id, room_id, room_seq, event_type, actor_id, resource_id, payload)
    VALUES ($1, $2, $3, $4, 'message.created',
            $5, $6, $7)
    RETURNING room_seq
), published AS (
    UPDATE messages
    SET room_seq=$4::bigint, mentioned_actor_ids=$8::text[]
    WHERE id=$6::text AND tenant_id=$2::text
      AND room_id=$3::text AND room_seq IS NULL
    RETURNING id
)
SELECT event.room_seq, (SELECT count(*) FROM published)::bigint AS published_messages
FROM event
`

type CreateMessageCreatedRoomEventParams struct {
	EventID           string   `json:"event_id"`
	TenantID          string   `json:"tenant_id"`
	RoomID            string   `json:"room_id"`
	RoomSeq           int64    `json:"room_seq"`
	ActorID           string   `json:"actor_id"`
	MessageID         string   `json:"message_id"`
	Payload           []byte   `json:"payload"`
	MentionedActorIds []string `json:"mentioned_actor_ids"`
}

type CreateMessageCreatedRoomEventRow struct {
	RoomSeq           int64 `json:"room_seq"`
	PublishedMessages int64 `json:"published_messages"`
}

// message.created stamps its unpublished Message with the Room sequence and
// the Actors it mentions in the same statement.
func (q *Queries) CreateMessageCreatedRoomEvent(ctx context.Context, arg CreateMessageCreatedRoomEventParams) (CreateMessageCreatedRoomEventRow, error) {
	row := q.db.QueryRow(ctx, createMessageCreatedRoomEvent,
		arg.EventID,
		arg.TenantID,
		arg.RoomID,
		arg.RoomSeq,
		arg.ActorID,
		arg.MessageID,
		arg.Payload,
		arg.MentionedActorIds,
	)
	var i CreateMessageCreatedRoomEventRow
	err := row.Scan(&i.RoomSeq, &i.PublishedMessages)
	return i, err
}

const createRoomEvent = `-- name: CreateRoomEvent :one
INSERT INTO room_events(event_id, tenant_id, room_id, room_seq, event_type, actor_id, resource_id, payload)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING event_id, tenant_id, room_id, room_seq, event_type, actor_id, resource_id, payload, occurred_at
`

type CreateRoomEventParams struct {
	EventID    string `json:"event_id"`
	TenantID   string `json:"tenant_id"`
	RoomID     string `json:"room_id"`
	RoomSeq    int64  `json:"room_seq"`
	EventType  string `json:"event_type"`
	ActorID    string `json:"actor_id"`
	ResourceID string `json:"resource_id"`
	Payload    []byte `json:"payload"`
}

func (q *Queries) CreateRoomEvent(ctx context.Context, arg CreateRoomEventParams) (RoomEvent, error) {
	row := q.db.QueryRow(ctx, createRoomEvent,
		arg.EventID,
		arg.TenantID,
		arg.RoomID,
		arg.RoomSeq,
		arg.EventType,
		arg.ActorID,
		arg.ResourceID,
		arg.Payload,
	)
	var i RoomEvent
	err := row.Scan(
		&i.EventID,
		&i.TenantID,
		&i.RoomID,
		&i.RoomSeq,
		&i.EventType,
		&i.ActorID,
		&i.ResourceID,
		&i.Payload,
		&i.OccurredAt,
	)
	return i, err
}

const getRoomEventWatermarkForAccount = `-- name: GetRoomEventWatermarkForAccount :one
SELECT coalesce(max(event.room_seq),0)::bigint AS next_room_seq
FROM rooms r
JOIN actors caller ON caller.tenant_id=r.tenant_id AND caller.account_id=$2 AND caller.kind='human'
JOIN active_room_members membership
  ON membership.room_id=collaboration_membership_room_id(r.id)
 AND membership.actor_id=caller.id
LEFT JOIN room_events event ON event.room_id=r.id
WHERE r.id=$1 AND room_actor_participating(r.id,caller.id)
GROUP BY r.id
`

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

// Every allocated room_seq is listed, so the watermark matches the Room
// snapshot sequence; hidden Messages appear as message.hidden placeholders.
func (q *Queries) GetRoomEventWatermarkForAccount(ctx context.Context, arg GetRoomEventWatermarkForAccountParams) (int64, error) {
	row := q.db.QueryRow(ctx, getRoomEventWatermarkForAccount, arg.ID, arg.AccountID)
	var next_room_seq int64
	err := row.Scan(&next_room_seq)
	return next_room_seq, err
}

const listRoomEventWatermarksForAccount = `-- name: ListRoomEventWatermarksForAccount :many
SELECT r.id AS room_id,
       coalesce((SELECT max(event.room_seq) FROM room_events event WHERE event.room_id=r.id),0)::bigint AS latest_room_seq
FROM rooms r
WHERE r.id = ANY($1::text[])
  AND EXISTS (
    SELECT 1 FROM actors caller
    JOIN active_room_members membership
      ON membership.actor_id=caller.id
     AND membership.room_id=collaboration_membership_room_id(r.id)
    WHERE caller.tenant_id=r.tenant_id AND caller.account_id=$2::text AND caller.kind='human'
      AND room_actor_participating(r.id,caller.id)
  )
ORDER BY r.id
`

type ListRoomEventWatermarksForAccountParams struct {
	RoomIds   []string `json:"room_ids"`
	AccountID string   `json:"account_id"`
}

type ListRoomEventWatermarksForAccountRow struct {
	RoomID        string `json:"room_id"`
	LatestRoomSeq int64  `json:"latest_room_seq"`
}

// One read answers a burst of Room wakes: the listed Rooms the Account's Human
// currently participates in, with their committed room_seq. room_seq is
// allocated under the Room row lock held until commit, so the committed
// events of a Room always form a prefix and this watermark never skips a
// later commit.
func (q *Queries) ListRoomEventWatermarksForAccount(ctx context.Context, arg ListRoomEventWatermarksForAccountParams) ([]ListRoomEventWatermarksForAccountRow, error) {
	rows, err := q.db.Query(ctx, listRoomEventWatermarksForAccount, arg.RoomIds, arg.AccountID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListRoomEventWatermarksForAccountRow{}
	for rows.Next() {
		var i ListRoomEventWatermarksForAccountRow
		if err := rows.Scan(&i.RoomID, &i.LatestRoomSeq); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listRoomEventsForAccount = `-- name: ListRoomEventsForAccount :many
SELECT e.event_id, e.tenant_id, e.room_id, e.room_seq,
       (CASE WHEN hidden_message.id IS NULL THEN e.event_type ELSE 'message.hidden' END)::text AS event_type,
       e.actor_id, e.resource_id,
       (CASE WHEN hidden_message.id IS NULL THEN e.payload ELSE '{"kind":"message.hidden"}'::jsonb END)::jsonb AS payload,
       e.occurred_at,
       actor.kind AS actor_kind, actor.display_name AS actor_display_name
FROM room_events e
JOIN rooms r ON r.id=e.room_id AND r.tenant_id=e.tenant_id
JOIN actors actor ON actor.id=e.actor_id AND actor.tenant_id=e.tenant_id
LEFT JOIN messages hidden_message
  ON e.event_type='message.created'
 AND hidden_message.id=e.resource_id
 AND hidden_message.room_id=e.room_id
 AND hidden_message.message_kind='task_terminal_notice'
WHERE e.room_id=$1
  AND EXISTS (
    SELECT 1 FROM actors caller
    JOIN active_room_members membership
      ON membership.actor_id=caller.id
     AND membership.room_id=collaboration_membership_room_id(r.id)
    WHERE caller.tenant_id=r.tenant_id AND caller.account_id=$2 AND caller.kind='human'
      AND room_actor_participating(r.id,caller.id)
  )
  AND e.room_seq > $3 AND e.room_seq <= $4
ORDER BY e.room_seq
LIMIT $5
`

type ListRoomEventsForAccountParams struct {
	RoomID    string      `json:"room_id"`
	AccountID pgtype.Text `json:"account_id"`
	RoomSeq   int64       `json:"room_seq"`
	RoomSeq_2 int64       `json:"room_seq_2"`
	Limit     int32       `json:"limit"`
}

type ListRoomEventsForAccountRow struct {
	EventID          string             `json:"event_id"`
	TenantID         string             `json:"tenant_id"`
	RoomID           string             `json:"room_id"`
	RoomSeq          int64              `json:"room_seq"`
	EventType        string             `json:"event_type"`
	ActorID          string             `json:"actor_id"`
	ResourceID       string             `json:"resource_id"`
	Payload          []byte             `json:"payload"`
	OccurredAt       pgtype.Timestamptz `json:"occurred_at"`
	ActorKind        string             `json:"actor_kind"`
	ActorDisplayName string             `json:"actor_display_name"`
}

// Task terminal notices are Agent-only history. Humans receive a payload-free
// message.hidden placeholder so their room_seq cursor stays contiguous.
func (q *Queries) ListRoomEventsForAccount(ctx context.Context, arg ListRoomEventsForAccountParams) ([]ListRoomEventsForAccountRow, error) {
	rows, err := q.db.Query(ctx, listRoomEventsForAccount,
		arg.RoomID,
		arg.AccountID,
		arg.RoomSeq,
		arg.RoomSeq_2,
		arg.Limit,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListRoomEventsForAccountRow{}
	for rows.Next() {
		var i ListRoomEventsForAccountRow
		if err := rows.Scan(
			&i.EventID,
			&i.TenantID,
			&i.RoomID,
			&i.RoomSeq,
			&i.EventType,
			&i.ActorID,
			&i.ResourceID,
			&i.Payload,
			&i.OccurredAt,
			&i.ActorKind,
			&i.ActorDisplayName,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listRoomTenants = `-- name: ListRoomTenants :many
SELECT id, tenant_id FROM rooms WHERE id = ANY($1::text[])
`

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

func (q *Queries) ListRoomTenants(ctx context.Context, roomIds []string) ([]ListRoomTenantsRow, error) {
	rows, err := q.db.Query(ctx, listRoomTenants, roomIds)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListRoomTenantsRow{}
	for rows.Next() {
		var i ListRoomTenantsRow
		if err := rows.Scan(&i.ID, &i.TenantID); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const publishInvalidation = `-- name: PublishInvalidation :exec
SELECT publish_teamhub_realtime_wake(
    $1::text, $2::text, $3::text,
    $4::bigint, 0
)
`

type PublishInvalidationParams struct {
	Topic      string `json:"topic"`
	Kind       string `json:"kind"`
	ResourceID string `json:"resource_id"`
	Sequence   int64  `json:"sequence"`
}

// A browser invalidation hint. NOTIFY inside the caller's
// transaction is delivered only after commit.
func (q *Queries) PublishInvalidation(ctx context.Context, arg PublishInvalidationParams) error {
	_, err := q.db.Exec(ctx, publishInvalidation,
		arg.Topic,
		arg.Kind,
		arg.ResourceID,
		arg.Sequence,
	)
	return err
}