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.

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

package store

import (
	"context"

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

const cancelAgentPresentationSequence = `-- name: CancelAgentPresentationSequence :execrows
UPDATE agent_presentation_sequences
SET status='canceled',updated_at=clock_timestamp()
WHERE id=$1::text AND status IN ('pending','delivering')
`

func (q *Queries) CancelAgentPresentationSequence(ctx context.Context, sequenceID string) (int64, error) {
	result, err := q.db.Exec(ctx, cancelAgentPresentationSequence, sequenceID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const completeAgentPresentationSequenceIfDelivered = `-- name: CompleteAgentPresentationSequenceIfDelivered :execrows
UPDATE agent_presentation_sequences sequence
SET status='completed',updated_at=clock_timestamp()
WHERE sequence.id=$1::text
  AND sequence.status IN ('pending','delivering')
  AND NOT EXISTS (
    SELECT 1 FROM agent_presentation_sequence_items item
    WHERE item.sequence_id=sequence.id AND item.delivered_at IS NULL
  )
`

func (q *Queries) CompleteAgentPresentationSequenceIfDelivered(ctx context.Context, sequenceID string) (int64, error) {
	result, err := q.db.Exec(ctx, completeAgentPresentationSequenceIfDelivered, sequenceID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const createAgentPresentationSequence = `-- name: CreateAgentPresentationSequence :one
INSERT INTO agent_presentation_sequences(id,tenant_id,room_id,agent_actor_id,local_run_id,reply_to_message_id,status)
VALUES($1::text,$2::text,$3::text,$4::text,$5::text,$6::text,'pending')
RETURNING id, tenant_id, room_id, agent_actor_id, status, created_at, updated_at, local_run_id, reply_to_message_id
`

type CreateAgentPresentationSequenceParams struct {
	ID               string      `json:"id"`
	TenantID         string      `json:"tenant_id"`
	RoomID           string      `json:"room_id"`
	AgentActorID     string      `json:"agent_actor_id"`
	LocalRunID       pgtype.Text `json:"local_run_id"`
	ReplyToMessageID pgtype.Text `json:"reply_to_message_id"`
}

func (q *Queries) CreateAgentPresentationSequence(ctx context.Context, arg CreateAgentPresentationSequenceParams) (AgentPresentationSequence, error) {
	row := q.db.QueryRow(ctx, createAgentPresentationSequence,
		arg.ID,
		arg.TenantID,
		arg.RoomID,
		arg.AgentActorID,
		arg.LocalRunID,
		arg.ReplyToMessageID,
	)
	var i AgentPresentationSequence
	err := row.Scan(
		&i.ID,
		&i.TenantID,
		&i.RoomID,
		&i.AgentActorID,
		&i.Status,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.LocalRunID,
		&i.ReplyToMessageID,
	)
	return i, err
}

const createAgentPresentationSequenceItem = `-- name: CreateAgentPresentationSequenceItem :one
INSERT INTO agent_presentation_sequence_items(sequence_id,position,content,pause_before,not_before)
VALUES($1::text,$2::integer,$3::text,$4::text,$5::timestamptz)
RETURNING sequence_id, position, content, pause_before, not_before, delivered_message_id, delivered_at
`

type CreateAgentPresentationSequenceItemParams struct {
	SequenceID  string             `json:"sequence_id"`
	Position    int32              `json:"position"`
	Content     string             `json:"content"`
	PauseBefore string             `json:"pause_before"`
	NotBefore   pgtype.Timestamptz `json:"not_before"`
}

func (q *Queries) CreateAgentPresentationSequenceItem(ctx context.Context, arg CreateAgentPresentationSequenceItemParams) (AgentPresentationSequenceItem, error) {
	row := q.db.QueryRow(ctx, createAgentPresentationSequenceItem,
		arg.SequenceID,
		arg.Position,
		arg.Content,
		arg.PauseBefore,
		arg.NotBefore,
	)
	var i AgentPresentationSequenceItem
	err := row.Scan(
		&i.SequenceID,
		&i.Position,
		&i.Content,
		&i.PauseBefore,
		&i.NotBefore,
		&i.DeliveredMessageID,
		&i.DeliveredAt,
	)
	return i, err
}

const getAgentPresentationSequence = `-- name: GetAgentPresentationSequence :one
SELECT id, tenant_id, room_id, agent_actor_id, status, created_at, updated_at, local_run_id, reply_to_message_id FROM agent_presentation_sequences WHERE id=$1::text
`

func (q *Queries) GetAgentPresentationSequence(ctx context.Context, id string) (AgentPresentationSequence, error) {
	row := q.db.QueryRow(ctx, getAgentPresentationSequence, id)
	var i AgentPresentationSequence
	err := row.Scan(
		&i.ID,
		&i.TenantID,
		&i.RoomID,
		&i.AgentActorID,
		&i.Status,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.LocalRunID,
		&i.ReplyToMessageID,
	)
	return i, err
}

const lockDueAgentPresentationSequenceItems = `-- name: LockDueAgentPresentationSequenceItems :many
SELECT item.sequence_id,item.position,item.content,item.pause_before,item.not_before,
       sequence.tenant_id,sequence.room_id,sequence.agent_actor_id,sequence.reply_to_message_id,
       sequence.local_run_id
FROM agent_presentation_sequence_items item
JOIN agent_presentation_sequences sequence ON sequence.id=item.sequence_id
WHERE sequence.status IN ('pending','delivering')
  AND item.delivered_at IS NULL
  AND item.not_before<=clock_timestamp()
  AND (
    item.position=1
    OR EXISTS (
      SELECT 1 FROM agent_presentation_sequence_items previous
      WHERE previous.sequence_id=item.sequence_id
        AND previous.position=item.position-1
        AND previous.delivered_at IS NOT NULL
        AND previous.delivered_at + CASE item.pause_before
          WHEN 'brief' THEN interval '1 second'
          WHEN 'natural' THEN interval '1500 milliseconds'
          ELSE interval '0 seconds'
        END<=clock_timestamp()
    )
  )
ORDER BY item.not_before,item.sequence_id,item.position
LIMIT $1::integer
FOR UPDATE OF item SKIP LOCKED
`

type LockDueAgentPresentationSequenceItemsRow struct {
	SequenceID       string             `json:"sequence_id"`
	Position         int32              `json:"position"`
	Content          string             `json:"content"`
	PauseBefore      string             `json:"pause_before"`
	NotBefore        pgtype.Timestamptz `json:"not_before"`
	TenantID         string             `json:"tenant_id"`
	RoomID           string             `json:"room_id"`
	AgentActorID     string             `json:"agent_actor_id"`
	ReplyToMessageID pgtype.Text        `json:"reply_to_message_id"`
	LocalRunID       pgtype.Text        `json:"local_run_id"`
}

func (q *Queries) LockDueAgentPresentationSequenceItems(ctx context.Context, rowLimit int32) ([]LockDueAgentPresentationSequenceItemsRow, error) {
	rows, err := q.db.Query(ctx, lockDueAgentPresentationSequenceItems, rowLimit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []LockDueAgentPresentationSequenceItemsRow{}
	for rows.Next() {
		var i LockDueAgentPresentationSequenceItemsRow
		if err := rows.Scan(
			&i.SequenceID,
			&i.Position,
			&i.Content,
			&i.PauseBefore,
			&i.NotBefore,
			&i.TenantID,
			&i.RoomID,
			&i.AgentActorID,
			&i.ReplyToMessageID,
			&i.LocalRunID,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const markAgentPresentationSequenceDelivering = `-- name: MarkAgentPresentationSequenceDelivering :execrows
UPDATE agent_presentation_sequences
SET status='delivering',updated_at=clock_timestamp()
WHERE id=$1::text AND status='pending'
`

func (q *Queries) MarkAgentPresentationSequenceDelivering(ctx context.Context, sequenceID string) (int64, error) {
	result, err := q.db.Exec(ctx, markAgentPresentationSequenceDelivering, sequenceID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const markAgentPresentationSequenceItemDelivered = `-- name: MarkAgentPresentationSequenceItemDelivered :one
UPDATE agent_presentation_sequence_items
SET delivered_message_id=$1::text,delivered_at=clock_timestamp()
WHERE sequence_id=$2::text
  AND position=$3::integer
  AND delivered_at IS NULL
RETURNING sequence_id, position, content, pause_before, not_before, delivered_message_id, delivered_at
`

type MarkAgentPresentationSequenceItemDeliveredParams struct {
	MessageID  string `json:"message_id"`
	SequenceID string `json:"sequence_id"`
	Position   int32  `json:"position"`
}

func (q *Queries) MarkAgentPresentationSequenceItemDelivered(ctx context.Context, arg MarkAgentPresentationSequenceItemDeliveredParams) (AgentPresentationSequenceItem, error) {
	row := q.db.QueryRow(ctx, markAgentPresentationSequenceItemDelivered, arg.MessageID, arg.SequenceID, arg.Position)
	var i AgentPresentationSequenceItem
	err := row.Scan(
		&i.SequenceID,
		&i.Position,
		&i.Content,
		&i.PauseBefore,
		&i.NotBefore,
		&i.DeliveredMessageID,
		&i.DeliveredAt,
	)
	return i, err
}

const nextAgentPresentationSequenceItemWaitMilliseconds = `-- name: NextAgentPresentationSequenceItemWaitMilliseconds :one
SELECT CEIL(EXTRACT(EPOCH FROM (
  GREATEST(item.not_before, COALESCE(previous.delivered_at + CASE item.pause_before
    WHEN 'brief' THEN interval '1 second'
    WHEN 'natural' THEN interval '1500 milliseconds'
    ELSE interval '0 seconds'
  END, item.not_before)) - clock_timestamp()
)) * 1000)::bigint AS wait_milliseconds
FROM agent_presentation_sequence_items item
JOIN agent_presentation_sequences sequence ON sequence.id=item.sequence_id
LEFT JOIN agent_presentation_sequence_items previous
  ON previous.sequence_id=item.sequence_id AND previous.position=item.position-1
WHERE sequence.status IN ('pending','delivering')
  AND item.delivered_at IS NULL
  AND (item.position=1 OR previous.delivered_at IS NOT NULL)
ORDER BY wait_milliseconds
LIMIT 1
`

// The wait is measured on the database clock that LockDueAgentPresentationSequenceItems uses.
func (q *Queries) NextAgentPresentationSequenceItemWaitMilliseconds(ctx context.Context) (int64, error) {
	row := q.db.QueryRow(ctx, nextAgentPresentationSequenceItemWaitMilliseconds)
	var wait_milliseconds int64
	err := row.Scan(&wait_milliseconds)
	return wait_milliseconds, err
}