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.

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

package store

import (
	"context"

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

const authorizeTenantAgentTemplate = `-- name: AuthorizeTenantAgentTemplate :one
SELECT actor.id, actor.display_name, actor.kind
FROM actors actor
WHERE actor.tenant_id=$1 AND actor.account_id=$2 AND actor.kind='human'
`

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

type AuthorizeTenantAgentTemplateRow struct {
	ID          string `json:"id"`
	DisplayName string `json:"display_name"`
	Kind        string `json:"kind"`
}

func (q *Queries) AuthorizeTenantAgentTemplate(ctx context.Context, arg AuthorizeTenantAgentTemplateParams) (AuthorizeTenantAgentTemplateRow, error) {
	row := q.db.QueryRow(ctx, authorizeTenantAgentTemplate, arg.TenantID, arg.AccountID)
	var i AuthorizeTenantAgentTemplateRow
	err := row.Scan(&i.ID, &i.DisplayName, &i.Kind)
	return i, err
}

const completeAgentTemplateInstallation = `-- name: CompleteAgentTemplateInstallation :exec
UPDATE agent_registrations SET template_installed_at=clock_timestamp()
WHERE actor_id=$1 AND template_id IS NOT NULL AND template_installed_at IS NULL
`

func (q *Queries) CompleteAgentTemplateInstallation(ctx context.Context, actorID string) error {
	_, err := q.db.Exec(ctx, completeAgentTemplateInstallation, actorID)
	return err
}

const deleteBuiltinAgentTemplatesOutside = `-- name: DeleteBuiltinAgentTemplatesOutside :exec
DELETE FROM agent_templates
WHERE origin='builtin' AND NOT (template_id=ANY($1::text[]))
`

func (q *Queries) DeleteBuiltinAgentTemplatesOutside(ctx context.Context, keptTemplateIds []string) error {
	_, err := q.db.Exec(ctx, deleteBuiltinAgentTemplatesOutside, keptTemplateIds)
	return err
}

const deletePublishedAgentTemplate = `-- name: DeletePublishedAgentTemplate :execrows
DELETE FROM agent_templates
WHERE template_id=$1 AND origin='published'
`

// Removal is authorized before this runs: the Space's Human removes any
// template published from this Space.
func (q *Queries) DeletePublishedAgentTemplate(ctx context.Context, templateID string) (int64, error) {
	result, err := q.db.Exec(ctx, deletePublishedAgentTemplate, templateID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const findOwnedSkillCopy = `-- name: FindOwnedSkillCopy :one
SELECT skill.skill_id,manifest.manifest_version,skill.name
FROM skills skill
JOIN skill_manifests manifest
  ON manifest.skill_id=skill.skill_id
 AND manifest.content_hash=$1
 AND manifest.package_state='sealed'
WHERE skill.owner_kind='tenant' AND skill.tenant_id=$2::text
  AND skill.access_scope='owner' AND skill.owner_actor_id=$3::text
  AND skill.name=$4
  AND manifest.state='active'
ORDER BY manifest.created_at DESC,skill.skill_id COLLATE "C"
LIMIT 1
`

type FindOwnedSkillCopyParams struct {
	ContentHash  []byte `json:"content_hash"`
	TenantID     string `json:"tenant_id"`
	OwnerActorID string `json:"owner_actor_id"`
	Name         string `json:"name"`
}

type FindOwnedSkillCopyRow struct {
	SkillID         string `json:"skill_id"`
	ManifestVersion string `json:"manifest_version"`
	Name            string `json:"name"`
}

func (q *Queries) FindOwnedSkillCopy(ctx context.Context, arg FindOwnedSkillCopyParams) (FindOwnedSkillCopyRow, error) {
	row := q.db.QueryRow(ctx, findOwnedSkillCopy,
		arg.ContentHash,
		arg.TenantID,
		arg.OwnerActorID,
		arg.Name,
	)
	var i FindOwnedSkillCopyRow
	err := row.Scan(&i.SkillID, &i.ManifestVersion, &i.Name)
	return i, err
}

const getAgentTemplate = `-- name: GetAgentTemplate :one
SELECT template_id, origin, tenant_id, name, description, instructions, avatar_key, skill_bindings, published_by_actor_id, created_at, updated_at, connector_bindings, routines FROM agent_templates WHERE template_id=$1
`

func (q *Queries) GetAgentTemplate(ctx context.Context, templateID string) (AgentTemplate, error) {
	row := q.db.QueryRow(ctx, getAgentTemplate, templateID)
	var i AgentTemplate
	err := row.Scan(
		&i.TemplateID,
		&i.Origin,
		&i.TenantID,
		&i.Name,
		&i.Description,
		&i.Instructions,
		&i.AvatarKey,
		&i.SkillBindings,
		&i.PublishedByActorID,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.ConnectorBindings,
		&i.Routines,
	)
	return i, err
}

const getAgentTemplateInstallation = `-- name: GetAgentTemplateInstallation :one
SELECT template_id::text AS template_id,template_routines::jsonb AS routines,
       template_installed_at AS completed_at
FROM agent_registrations WHERE actor_id=$1 AND template_id IS NOT NULL
`

type GetAgentTemplateInstallationRow struct {
	TemplateID  string             `json:"template_id"`
	Routines    []byte             `json:"routines"`
	CompletedAt pgtype.Timestamptz `json:"completed_at"`
}

func (q *Queries) GetAgentTemplateInstallation(ctx context.Context, actorID string) (GetAgentTemplateInstallationRow, error) {
	row := q.db.QueryRow(ctx, getAgentTemplateInstallation, actorID)
	var i GetAgentTemplateInstallationRow
	err := row.Scan(&i.TemplateID, &i.Routines, &i.CompletedAt)
	return i, err
}

const getAgentTemplateOwner = `-- name: GetAgentTemplateOwner :one
SELECT origin, published_by_actor_id, tenant_id FROM agent_templates WHERE template_id=$1
`

type GetAgentTemplateOwnerRow struct {
	Origin             string      `json:"origin"`
	PublishedByActorID pgtype.Text `json:"published_by_actor_id"`
	TenantID           pgtype.Text `json:"tenant_id"`
}

func (q *Queries) GetAgentTemplateOwner(ctx context.Context, templateID string) (GetAgentTemplateOwnerRow, error) {
	row := q.db.QueryRow(ctx, getAgentTemplateOwner, templateID)
	var i GetAgentTemplateOwnerRow
	err := row.Scan(&i.Origin, &i.PublishedByActorID, &i.TenantID)
	return i, err
}

const getAgentTemplateSkillSource = `-- name: GetAgentTemplateSkillSource :one
SELECT manifest.manifest_id,skill.skill_id,manifest.manifest_version,skill.name,
       manifest.description,manifest.config_schema,manifest.frontmatter_extra,
       manifest.content_hash,manifest.file_count,manifest.size_bytes
FROM skills skill
JOIN skill_manifests manifest
  ON manifest.skill_id=skill.skill_id
 AND manifest.manifest_version=$1::text
 AND manifest.package_state='sealed'
WHERE skill.skill_id=$2::text AND skill.owner_kind='tenant'
  AND skill.tenant_id=$3::text
  AND manifest.state='active'
`

type GetAgentTemplateSkillSourceParams struct {
	ManifestVersion string `json:"manifest_version"`
	SkillID         string `json:"skill_id"`
	TenantID        string `json:"tenant_id"`
}

type GetAgentTemplateSkillSourceRow struct {
	ManifestID       string `json:"manifest_id"`
	SkillID          string `json:"skill_id"`
	ManifestVersion  string `json:"manifest_version"`
	Name             string `json:"name"`
	Description      string `json:"description"`
	ConfigSchema     []byte `json:"config_schema"`
	FrontmatterExtra []byte `json:"frontmatter_extra"`
	ContentHash      []byte `json:"content_hash"`
	FileCount        int32  `json:"file_count"`
	SizeBytes        int64  `json:"size_bytes"`
}

// Publishing a template shares its Skill versions, so the source is read from
// the publishing Tenant without the caller visibility filter.
func (q *Queries) GetAgentTemplateSkillSource(ctx context.Context, arg GetAgentTemplateSkillSourceParams) (GetAgentTemplateSkillSourceRow, error) {
	row := q.db.QueryRow(ctx, getAgentTemplateSkillSource, arg.ManifestVersion, arg.SkillID, arg.TenantID)
	var i GetAgentTemplateSkillSourceRow
	err := row.Scan(
		&i.ManifestID,
		&i.SkillID,
		&i.ManifestVersion,
		&i.Name,
		&i.Description,
		&i.ConfigSchema,
		&i.FrontmatterExtra,
		&i.ContentHash,
		&i.FileCount,
		&i.SizeBytes,
	)
	return i, err
}

const getSkillIdentity = `-- name: GetSkillIdentity :one
SELECT owner_kind,COALESCE(tenant_id,'')::text AS tenant_id,
       access_scope,COALESCE(owner_actor_id,'')::text AS owner_actor_id
FROM skills
WHERE skill_id=$1::text
`

type GetSkillIdentityRow struct {
	OwnerKind    string `json:"owner_kind"`
	TenantID     string `json:"tenant_id"`
	AccessScope  string `json:"access_scope"`
	OwnerActorID string `json:"owner_actor_id"`
}

func (q *Queries) GetSkillIdentity(ctx context.Context, skillID string) (GetSkillIdentityRow, error) {
	row := q.db.QueryRow(ctx, getSkillIdentity, skillID)
	var i GetSkillIdentityRow
	err := row.Scan(
		&i.OwnerKind,
		&i.TenantID,
		&i.AccessScope,
		&i.OwnerActorID,
	)
	return i, err
}

const insertPublishedAgentTemplate = `-- name: InsertPublishedAgentTemplate :one
INSERT INTO agent_templates (
    template_id, origin, tenant_id, name, description, instructions, avatar_key,
    skill_bindings, connector_bindings, published_by_actor_id, routines
) VALUES ($1,'published',$2,$3,$4,$5,$6,$7,$8,$9,$10)
RETURNING template_id, origin, tenant_id, name, description, instructions, avatar_key, skill_bindings, published_by_actor_id, created_at, updated_at, connector_bindings, routines
`

type InsertPublishedAgentTemplateParams struct {
	TemplateID         string      `json:"template_id"`
	TenantID           pgtype.Text `json:"tenant_id"`
	Name               string      `json:"name"`
	Description        string      `json:"description"`
	Instructions       string      `json:"instructions"`
	AvatarKey          pgtype.Text `json:"avatar_key"`
	SkillBindings      []byte      `json:"skill_bindings"`
	ConnectorBindings  []byte      `json:"connector_bindings"`
	PublishedByActorID pgtype.Text `json:"published_by_actor_id"`
	Routines           []byte      `json:"routines"`
}

func (q *Queries) InsertPublishedAgentTemplate(ctx context.Context, arg InsertPublishedAgentTemplateParams) (AgentTemplate, error) {
	row := q.db.QueryRow(ctx, insertPublishedAgentTemplate,
		arg.TemplateID,
		arg.TenantID,
		arg.Name,
		arg.Description,
		arg.Instructions,
		arg.AvatarKey,
		arg.SkillBindings,
		arg.ConnectorBindings,
		arg.PublishedByActorID,
		arg.Routines,
	)
	var i AgentTemplate
	err := row.Scan(
		&i.TemplateID,
		&i.Origin,
		&i.TenantID,
		&i.Name,
		&i.Description,
		&i.Instructions,
		&i.AvatarKey,
		&i.SkillBindings,
		&i.PublishedByActorID,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.ConnectorBindings,
		&i.Routines,
	)
	return i, err
}

const listAgentTemplates = `-- name: ListAgentTemplates :many
SELECT template.template_id, template.origin, template.tenant_id, template.name, template.description, template.instructions, template.avatar_key, template.skill_bindings, template.published_by_actor_id, template.created_at, template.updated_at, template.connector_bindings, template.routines,
    publisher.display_name AS published_by_display_name,
    publisher.tenant_id AS published_by_tenant_id
FROM agent_templates template
LEFT JOIN actors publisher ON publisher.id=template.published_by_actor_id
ORDER BY template.origin COLLATE "C", template.created_at DESC, template.template_id COLLATE "C"
`

type ListAgentTemplatesRow struct {
	TemplateID             string             `json:"template_id"`
	Origin                 string             `json:"origin"`
	TenantID               pgtype.Text        `json:"tenant_id"`
	Name                   string             `json:"name"`
	Description            string             `json:"description"`
	Instructions           string             `json:"instructions"`
	AvatarKey              pgtype.Text        `json:"avatar_key"`
	SkillBindings          []byte             `json:"skill_bindings"`
	PublishedByActorID     pgtype.Text        `json:"published_by_actor_id"`
	CreatedAt              pgtype.Timestamptz `json:"created_at"`
	UpdatedAt              pgtype.Timestamptz `json:"updated_at"`
	ConnectorBindings      []byte             `json:"connector_bindings"`
	Routines               []byte             `json:"routines"`
	PublishedByDisplayName pgtype.Text        `json:"published_by_display_name"`
	PublishedByTenantID    pgtype.Text        `json:"published_by_tenant_id"`
}

func (q *Queries) ListAgentTemplates(ctx context.Context) ([]ListAgentTemplatesRow, error) {
	rows, err := q.db.Query(ctx, listAgentTemplates)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListAgentTemplatesRow{}
	for rows.Next() {
		var i ListAgentTemplatesRow
		if err := rows.Scan(
			&i.TemplateID,
			&i.Origin,
			&i.TenantID,
			&i.Name,
			&i.Description,
			&i.Instructions,
			&i.AvatarKey,
			&i.SkillBindings,
			&i.PublishedByActorID,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.ConnectorBindings,
			&i.Routines,
			&i.PublishedByDisplayName,
			&i.PublishedByTenantID,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const pauseAgentTemplateRoutines = `-- name: PauseAgentTemplateRoutines :many
UPDATE room_reminders
SET enabled=false, next_run_at=NULL, revision=revision+1, updated_at=clock_timestamp()
WHERE template_agent_actor_id=$1
RETURNING id, tenant_id, room_id, created_by_actor_id, instruction, mention_actor_id, cadence_kind, timezone, hour, minute, enabled, next_run_at, revision, created_at, updated_at, name, weekdays, template_agent_actor_id, template_routine_key, trigger, run_at, tracking_item_id, tracking_purpose, interval_minutes
`

func (q *Queries) PauseAgentTemplateRoutines(ctx context.Context, templateAgentActorID pgtype.Text) ([]RoomReminder, error) {
	rows, err := q.db.Query(ctx, pauseAgentTemplateRoutines, templateAgentActorID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []RoomReminder{}
	for rows.Next() {
		var i RoomReminder
		if err := rows.Scan(
			&i.ID,
			&i.TenantID,
			&i.RoomID,
			&i.CreatedByActorID,
			&i.Instruction,
			&i.MentionActorID,
			&i.CadenceKind,
			&i.Timezone,
			&i.Hour,
			&i.Minute,
			&i.Enabled,
			&i.NextRunAt,
			&i.Revision,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.Name,
			&i.Weekdays,
			&i.TemplateAgentActorID,
			&i.TemplateRoutineKey,
			&i.Trigger,
			&i.RunAt,
			&i.TrackingItemID,
			&i.TrackingPurpose,
			&i.IntervalMinutes,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const reserveAgentTemplateInstallation = `-- name: ReserveAgentTemplateInstallation :execrows
UPDATE agent_registrations
SET template_id=$1::text,template_routines=$2::jsonb
WHERE actor_id=$3::text AND template_id IS NULL
`

type ReserveAgentTemplateInstallationParams struct {
	TemplateID   string `json:"template_id"`
	Routines     []byte `json:"routines"`
	AgentActorID string `json:"agent_actor_id"`
}

func (q *Queries) ReserveAgentTemplateInstallation(ctx context.Context, arg ReserveAgentTemplateInstallationParams) (int64, error) {
	result, err := q.db.Exec(ctx, reserveAgentTemplateInstallation, arg.TemplateID, arg.Routines, arg.AgentActorID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const upsertBuiltinAgentTemplate = `-- name: UpsertBuiltinAgentTemplate :exec
INSERT INTO agent_templates (
    template_id, origin, name, description, instructions, avatar_key,
    skill_bindings, connector_bindings, routines
) VALUES ($1,'builtin',$2,$3,$4,$5,$6,$7,$8)
ON CONFLICT (template_id) DO UPDATE SET
    name=EXCLUDED.name,
    description=EXCLUDED.description,
    instructions=EXCLUDED.instructions,
    avatar_key=EXCLUDED.avatar_key,
    skill_bindings=EXCLUDED.skill_bindings,
    connector_bindings=EXCLUDED.connector_bindings,
    routines=EXCLUDED.routines,
    updated_at=now()
WHERE agent_templates.origin='builtin'
`

type UpsertBuiltinAgentTemplateParams struct {
	TemplateID        string      `json:"template_id"`
	Name              string      `json:"name"`
	Description       string      `json:"description"`
	Instructions      string      `json:"instructions"`
	AvatarKey         pgtype.Text `json:"avatar_key"`
	SkillBindings     []byte      `json:"skill_bindings"`
	ConnectorBindings []byte      `json:"connector_bindings"`
	Routines          []byte      `json:"routines"`
}

func (q *Queries) UpsertBuiltinAgentTemplate(ctx context.Context, arg UpsertBuiltinAgentTemplateParams) error {
	_, err := q.db.Exec(ctx, upsertBuiltinAgentTemplate,
		arg.TemplateID,
		arg.Name,
		arg.Description,
		arg.Instructions,
		arg.AvatarKey,
		arg.SkillBindings,
		arg.ConnectorBindings,
		arg.Routines,
	)
	return err
}