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.

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

package store

import (
	"context"

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

const cancelVoiceLiveTask = `-- name: CancelVoiceLiveTask :execrows
UPDATE voice_live_tasks SET canceled_at=clock_timestamp()
WHERE work_item_id=$1::text AND canceled_at IS NULL
`

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

const countVoiceLiveRoomHumans = `-- name: CountVoiceLiveRoomHumans :one
SELECT count(*) FROM active_room_members member JOIN actors actor ON actor.id=member.actor_id
WHERE member.room_id=$1::text AND actor.kind='human'
`

func (q *Queries) CountVoiceLiveRoomHumans(ctx context.Context, roomID string) (int64, error) {
	row := q.db.QueryRow(ctx, countVoiceLiveRoomHumans, roomID)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const countVoiceLiveTurnConfirmations = `-- name: CountVoiceLiveTurnConfirmations :one
SELECT count(*) FROM voice_live_confirmations
WHERE voice_session_id=$1::text AND turn_id=$2::text
  AND tool=$3::text AND used_at IS NULL
`

type CountVoiceLiveTurnConfirmationsParams struct {
	VoiceSessionID string `json:"voice_session_id"`
	TurnID         string `json:"turn_id"`
	Tool           string `json:"tool"`
}

// Questions this turn asked about one tool that nobody answered yet.
func (q *Queries) CountVoiceLiveTurnConfirmations(ctx context.Context, arg CountVoiceLiveTurnConfirmationsParams) (int64, error) {
	row := q.db.QueryRow(ctx, countVoiceLiveTurnConfirmations, arg.VoiceSessionID, arg.TurnID, arg.Tool)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const createUsedVoiceLiveConfirmation = `-- name: CreateUsedVoiceLiveConfirmation :exec
INSERT INTO voice_live_confirmations(id,voice_session_id,turn_id,tool,arguments_hash,goal,surface,used_at,used_turn_id)
VALUES($1::text,$2::text,$3::text,$4::text,
  $5::text,'','',clock_timestamp(),$3::text)
`

type CreateUsedVoiceLiveConfirmationParams struct {
	ID             string `json:"id"`
	VoiceSessionID string `json:"voice_session_id"`
	TurnID         string `json:"turn_id"`
	Tool           string `json:"tool"`
	ArgumentsHash  string `json:"arguments_hash"`
}

// A yes the user gave in their own turn without a question first, used at
// once, so a retry within that turn is admitted like any used yes.
func (q *Queries) CreateUsedVoiceLiveConfirmation(ctx context.Context, arg CreateUsedVoiceLiveConfirmationParams) error {
	_, err := q.db.Exec(ctx, createUsedVoiceLiveConfirmation,
		arg.ID,
		arg.VoiceSessionID,
		arg.TurnID,
		arg.Tool,
		arg.ArgumentsHash,
	)
	return err
}

const createVoiceLiveConfirmation = `-- name: CreateVoiceLiveConfirmation :exec
INSERT INTO voice_live_confirmations(id,voice_session_id,turn_id,tool,arguments_hash,goal,surface)
VALUES($1::text,$2::text,$3::text,$4::text,
  $5::text,$6::text,$7::text)
`

type CreateVoiceLiveConfirmationParams struct {
	ID             string `json:"id"`
	VoiceSessionID string `json:"voice_session_id"`
	TurnID         string `json:"turn_id"`
	Tool           string `json:"tool"`
	ArgumentsHash  string `json:"arguments_hash"`
	Goal           string `json:"goal"`
	Surface        string `json:"surface"`
}

func (q *Queries) CreateVoiceLiveConfirmation(ctx context.Context, arg CreateVoiceLiveConfirmationParams) error {
	_, err := q.db.Exec(ctx, createVoiceLiveConfirmation,
		arg.ID,
		arg.VoiceSessionID,
		arg.TurnID,
		arg.Tool,
		arg.ArgumentsHash,
		arg.Goal,
		arg.Surface,
	)
	return err
}

const createVoiceLiveTask = `-- name: CreateVoiceLiveTask :one
INSERT INTO voice_live_tasks(work_item_id,room_id,owner_actor_id,home_room_id,ordinal,voice_session_id,goal,surface)
SELECT $1::text,$2::text,$3::text,$4::text,
  coalesce((SELECT max(ordinal) FROM voice_live_tasks
    WHERE room_id=$2::text AND owner_actor_id=$3::text),0)+1,
  $5::text,$6::text,$7::text
RETURNING ordinal
`

type CreateVoiceLiveTaskParams struct {
	WorkItemID     string `json:"work_item_id"`
	RoomID         string `json:"room_id"`
	OwnerActorID   string `json:"owner_actor_id"`
	HomeRoomID     string `json:"home_room_id"`
	VoiceSessionID string `json:"voice_session_id"`
	Goal           string `json:"goal"`
	Surface        string `json:"surface"`
}

// Numbered per Room and user; the caller holds the per-user task lock.
func (q *Queries) CreateVoiceLiveTask(ctx context.Context, arg CreateVoiceLiveTaskParams) (int32, error) {
	row := q.db.QueryRow(ctx, createVoiceLiveTask,
		arg.WorkItemID,
		arg.RoomID,
		arg.OwnerActorID,
		arg.HomeRoomID,
		arg.VoiceSessionID,
		arg.Goal,
		arg.Surface,
	)
	var ordinal int32
	err := row.Scan(&ordinal)
	return ordinal, err
}

const findVoiceLiveRepeatedTask = `-- name: FindVoiceLiveRepeatedTask :one
SELECT task.ordinal FROM voice_live_tasks task
WHERE task.room_id=$1::text AND task.owner_actor_id=$2::text
  AND task.goal=$3::text AND task.canceled_at IS NULL
  AND task.created_at>$4::timestamptz
  AND NOT EXISTS(SELECT 1 FROM task_meta_callback_messages message WHERE message.task_id=task.work_item_id)
ORDER BY task.created_at DESC LIMIT 1
`

type FindVoiceLiveRepeatedTaskParams struct {
	RoomID       string             `json:"room_id"`
	OwnerActorID string             `json:"owner_actor_id"`
	Goal         string             `json:"goal"`
	Since        pgtype.Timestamptz `json:"since"`
}

// The same goal handed off again moments ago, before any reply.
func (q *Queries) FindVoiceLiveRepeatedTask(ctx context.Context, arg FindVoiceLiveRepeatedTaskParams) (int32, error) {
	row := q.db.QueryRow(ctx, findVoiceLiveRepeatedTask,
		arg.RoomID,
		arg.OwnerActorID,
		arg.Goal,
		arg.Since,
	)
	var ordinal int32
	err := row.Scan(&ordinal)
	return ordinal, err
}

const getVoiceLiveAgentComputer = `-- name: GetVoiceLiveAgentComputer :one
SELECT coalesce(computer.status,'')::text AS computer_status,
  coalesce(registration.agent_management_id,'')::text AS agent_management_id
FROM agent_registrations registration
LEFT JOIN local_computers computer ON computer.computer_id=registration.computer_id
WHERE registration.actor_id=$1::text AND registration.tenant_id=$2::text
`

type GetVoiceLiveAgentComputerParams struct {
	ActorID  string `json:"actor_id"`
	TenantID string `json:"tenant_id"`
}

type GetVoiceLiveAgentComputerRow struct {
	ComputerStatus    string `json:"computer_status"`
	AgentManagementID string `json:"agent_management_id"`
}

// Where the Agent answering a live session runs, whether its computer is
// online, and the Agent's upstream identity for its Tasks.
func (q *Queries) GetVoiceLiveAgentComputer(ctx context.Context, arg GetVoiceLiveAgentComputerParams) (GetVoiceLiveAgentComputerRow, error) {
	row := q.db.QueryRow(ctx, getVoiceLiveAgentComputer, arg.ActorID, arg.TenantID)
	var i GetVoiceLiveAgentComputerRow
	err := row.Scan(&i.ComputerStatus, &i.AgentManagementID)
	return i, err
}

const getVoiceLiveConfirmation = `-- name: GetVoiceLiveConfirmation :one
SELECT goal,surface FROM voice_live_confirmations
WHERE id=$1::text AND voice_session_id=$2::text AND tool=$3::text
  AND turn_id<>$4::text AND used_at IS NULL AND created_at>$5::timestamptz
`

type GetVoiceLiveConfirmationParams struct {
	ID             string             `json:"id"`
	VoiceSessionID string             `json:"voice_session_id"`
	Tool           string             `json:"tool"`
	TurnID         string             `json:"turn_id"`
	Since          pgtype.Timestamptz `json:"since"`
}

type GetVoiceLiveConfirmationRow struct {
	Goal    string `json:"goal"`
	Surface string `json:"surface"`
}

// A confirmation still usable from this turn, without using it.
func (q *Queries) GetVoiceLiveConfirmation(ctx context.Context, arg GetVoiceLiveConfirmationParams) (GetVoiceLiveConfirmationRow, error) {
	row := q.db.QueryRow(ctx, getVoiceLiveConfirmation,
		arg.ID,
		arg.VoiceSessionID,
		arg.Tool,
		arg.TurnID,
		arg.Since,
	)
	var i GetVoiceLiveConfirmationRow
	err := row.Scan(&i.Goal, &i.Surface)
	return i, err
}

const getVoiceLiveTask = `-- name: GetVoiceLiveTask :one
SELECT work_item_id, room_id, owner_actor_id, home_room_id, ordinal, voice_session_id, goal, surface, canceled_at, created_at FROM voice_live_tasks
WHERE room_id=$1::text AND owner_actor_id=$2::text AND ordinal=$3::int
`

type GetVoiceLiveTaskParams struct {
	RoomID       string `json:"room_id"`
	OwnerActorID string `json:"owner_actor_id"`
	Ordinal      int32  `json:"ordinal"`
}

func (q *Queries) GetVoiceLiveTask(ctx context.Context, arg GetVoiceLiveTaskParams) (VoiceLiveTask, error) {
	row := q.db.QueryRow(ctx, getVoiceLiveTask, arg.RoomID, arg.OwnerActorID, arg.Ordinal)
	var i VoiceLiveTask
	err := row.Scan(
		&i.WorkItemID,
		&i.RoomID,
		&i.OwnerActorID,
		&i.HomeRoomID,
		&i.Ordinal,
		&i.VoiceSessionID,
		&i.Goal,
		&i.Surface,
		&i.CanceledAt,
		&i.CreatedAt,
	)
	return i, err
}

const getVoiceLiveTaskByWorkItem = `-- name: GetVoiceLiveTaskByWorkItem :one
SELECT work_item_id, room_id, owner_actor_id, home_room_id, ordinal, voice_session_id, goal, surface, canceled_at, created_at FROM voice_live_tasks
WHERE room_id=$1::text AND owner_actor_id=$2::text
  AND work_item_id=$3::text
`

type GetVoiceLiveTaskByWorkItemParams struct {
	RoomID       string `json:"room_id"`
	OwnerActorID string `json:"owner_actor_id"`
	WorkItemID   string `json:"work_item_id"`
}

// A call names a Task's output folder by its Work Item ID.
func (q *Queries) GetVoiceLiveTaskByWorkItem(ctx context.Context, arg GetVoiceLiveTaskByWorkItemParams) (VoiceLiveTask, error) {
	row := q.db.QueryRow(ctx, getVoiceLiveTaskByWorkItem, arg.RoomID, arg.OwnerActorID, arg.WorkItemID)
	var i VoiceLiveTask
	err := row.Scan(
		&i.WorkItemID,
		&i.RoomID,
		&i.OwnerActorID,
		&i.HomeRoomID,
		&i.Ordinal,
		&i.VoiceSessionID,
		&i.Goal,
		&i.Surface,
		&i.CanceledAt,
		&i.CreatedAt,
	)
	return i, err
}

const listVoiceLivePendingApprovals = `-- name: ListVoiceLivePendingApprovals :many
SELECT decision.id,coalesce(decision.alter_ego_task_id,'')::text AS task_id,decision.kind,decision.view,
  card.version AS card_version
FROM decisions decision
JOIN message_cards card ON card.message_id=decision.message_id
WHERE decision.alter_ego_task_id=ANY($1::text[])
  AND decision.decider_actor_id=$2::text
  AND decision.status='pending'
  AND (decision.expires_at IS NULL OR decision.expires_at>clock_timestamp())
  AND decision.kind IN ('connector_write','runtime_permission','agent_change')
ORDER BY decision.created_at,decision.id
`

type ListVoiceLivePendingApprovalsParams struct {
	TaskIds        []string `json:"task_ids"`
	DeciderActorID string   `json:"decider_actor_id"`
}

type ListVoiceLivePendingApprovalsRow struct {
	ID          string `json:"id"`
	TaskID      string `json:"task_id"`
	Kind        string `json:"kind"`
	View        []byte `json:"view"`
	CardVersion int64  `json:"card_version"`
}

// The approval cards the user's live tasks wait on and the user decides:
// connector writes, Runtime permissions and Agent changes, with the card
// version an answer names.
func (q *Queries) ListVoiceLivePendingApprovals(ctx context.Context, arg ListVoiceLivePendingApprovalsParams) ([]ListVoiceLivePendingApprovalsRow, error) {
	rows, err := q.db.Query(ctx, listVoiceLivePendingApprovals, arg.TaskIds, arg.DeciderActorID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListVoiceLivePendingApprovalsRow{}
	for rows.Next() {
		var i ListVoiceLivePendingApprovalsRow
		if err := rows.Scan(
			&i.ID,
			&i.TaskID,
			&i.Kind,
			&i.View,
			&i.CardVersion,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listVoiceLiveRoomPendingWakes = `-- name: ListVoiceLiveRoomPendingWakes :many
SELECT id,source,body,connection_grant,agent_actor_id,tenant_id FROM proactive_pending_wakes
WHERE room_id=$1::text AND delivered_at IS NULL
ORDER BY created_at
LIMIT 20
`

type ListVoiceLiveRoomPendingWakesRow struct {
	ID              string `json:"id"`
	Source          string `json:"source"`
	Body            string `json:"body"`
	ConnectionGrant []byte `json:"connection_grant"`
	AgentActorID    string `json:"agent_actor_id"`
	TenantID        string `json:"tenant_id"`
}

// Proactive wakes still waiting for the call in this Room to end, oldest first.
func (q *Queries) ListVoiceLiveRoomPendingWakes(ctx context.Context, roomID string) ([]ListVoiceLiveRoomPendingWakesRow, error) {
	rows, err := q.db.Query(ctx, listVoiceLiveRoomPendingWakes, roomID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListVoiceLiveRoomPendingWakesRow{}
	for rows.Next() {
		var i ListVoiceLiveRoomPendingWakesRow
		if err := rows.Scan(
			&i.ID,
			&i.Source,
			&i.Body,
			&i.ConnectionGrant,
			&i.AgentActorID,
			&i.TenantID,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listVoiceLiveTaskReports = `-- name: ListVoiceLiveTaskReports :many
SELECT body,created_at FROM task_meta_callback_messages
WHERE task_id=$1::text AND tenant_id=$2::text
ORDER BY meta_seq DESC
LIMIT $4::int OFFSET $3::int
`

type ListVoiceLiveTaskReportsParams struct {
	TaskID    string `json:"task_id"`
	TenantID  string `json:"tenant_id"`
	RowOffset int32  `json:"row_offset"`
	RowLimit  int32  `json:"row_limit"`
}

type ListVoiceLiveTaskReportsRow struct {
	Body      string             `json:"body"`
	CreatedAt pgtype.Timestamptz `json:"created_at"`
}

// What a voice task sent back to its coordinator, newest first.
func (q *Queries) ListVoiceLiveTaskReports(ctx context.Context, arg ListVoiceLiveTaskReportsParams) ([]ListVoiceLiveTaskReportsRow, error) {
	rows, err := q.db.Query(ctx, listVoiceLiveTaskReports,
		arg.TaskID,
		arg.TenantID,
		arg.RowOffset,
		arg.RowLimit,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListVoiceLiveTaskReportsRow{}
	for rows.Next() {
		var i ListVoiceLiveTaskReportsRow
		if err := rows.Scan(&i.Body, &i.CreatedAt); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listVoiceLiveTasks = `-- name: ListVoiceLiveTasks :many
SELECT task.ordinal,task.goal,task.surface,task.work_item_id,task.canceled_at,item.title,
  (task.voice_session_id IS NOT DISTINCT FROM $1::text)::boolean AS started_here,
  coalesce(item.task_execution_status,'')::text AS execution_status,
  item.task_execution_updated_at AS execution_updated_at,
  coalesce(report.id,'')::text AS report_id,coalesce(report.body,'')::text AS report_body,
  coalesce(report.told,false)::boolean AS report_delivered,
  coalesce(run.run_id,'')::text AS run_id,run.created_at AS run_created_at
FROM voice_live_tasks task
JOIN work_items item ON item.id=task.work_item_id
LEFT JOIN LATERAL (
  SELECT current.run_id,current.created_at FROM agent_local_runs current
  WHERE current.context_key=$2::text||task.work_item_id||'"]'
    AND current.finished_at IS NULL
  ORDER BY current.created_at DESC LIMIT 1
) run ON true
LEFT JOIN LATERAL (
  -- The report its coordinator gets when a turn ends.
  SELECT message.id,message.body,
    (EXISTS(SELECT 1 FROM voice_live_report_deliveries delivery WHERE delivery.report_id=message.id)
      OR EXISTS(SELECT 1 FROM voice_sessions later
        WHERE later.room_id=task.room_id AND later.created_by_actor_id=task.owner_actor_id
          AND later.purpose='chat_live' AND later.id<>$1::text
          AND later.created_at>message.created_at)) AS told
  FROM task_meta_callback_messages message
  WHERE message.task_id=task.work_item_id AND message.generation=item.task_execution_generation
  ORDER BY message.meta_seq DESC LIMIT 1
) report ON true
WHERE task.room_id=$3::text AND task.owner_actor_id=$4::text
  AND (task.voice_session_id=$1::text
    OR task.canceled_at IS NULL AND item.task_execution_status IN ('running','waiting_for_approval')
    OR item.task_execution_updated_at>$5::timestamptz
 OR report.id IS NOT NULL AND NOT report.told)
ORDER BY task.ordinal DESC
LIMIT 20
`

type ListVoiceLiveTasksParams struct {
	VoiceSessionID   string             `json:"voice_session_id"`
	ContextKeyPrefix string             `json:"context_key_prefix"`
	RoomID           string             `json:"room_id"`
	OwnerActorID     string             `json:"owner_actor_id"`
	Since            pgtype.Timestamptz `json:"since"`
}

type ListVoiceLiveTasksRow struct {
	Ordinal            int32              `json:"ordinal"`
	Goal               string             `json:"goal"`
	Surface            string             `json:"surface"`
	WorkItemID         string             `json:"work_item_id"`
	CanceledAt         pgtype.Timestamptz `json:"canceled_at"`
	Title              string             `json:"title"`
	StartedHere        bool               `json:"started_here"`
	ExecutionStatus    string             `json:"execution_status"`
	ExecutionUpdatedAt pgtype.Timestamptz `json:"execution_updated_at"`
	ReportID           string             `json:"report_id"`
	ReportBody         string             `json:"report_body"`
	ReportDelivered    bool               `json:"report_delivered"`
	RunID              string             `json:"run_id"`
	RunCreatedAt       pgtype.Timestamptz `json:"run_created_at"`
}

// The user's tasks in this Room a call talks about: those it started, those
// still running or waiting, and those that changed since a cutoff, with
// their Task's execution state, latest report and the Run working on it now.
// A report counts as delivered once published, or once another call of the
// user's started after it: that call told it, heard in full or cut off, so
// later calls do not tell it again.
func (q *Queries) ListVoiceLiveTasks(ctx context.Context, arg ListVoiceLiveTasksParams) ([]ListVoiceLiveTasksRow, error) {
	rows, err := q.db.Query(ctx, listVoiceLiveTasks,
		arg.VoiceSessionID,
		arg.ContextKeyPrefix,
		arg.RoomID,
		arg.OwnerActorID,
		arg.Since,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListVoiceLiveTasksRow{}
	for rows.Next() {
		var i ListVoiceLiveTasksRow
		if err := rows.Scan(
			&i.Ordinal,
			&i.Goal,
			&i.Surface,
			&i.WorkItemID,
			&i.CanceledAt,
			&i.Title,
			&i.StartedHere,
			&i.ExecutionStatus,
			&i.ExecutionUpdatedAt,
			&i.ReportID,
			&i.ReportBody,
			&i.ReportDelivered,
			&i.RunID,
			&i.RunCreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const notifyVoiceLiveRoomAlert = `-- name: NotifyVoiceLiveRoomAlert :exec
SELECT pg_notify('teamhub_realtime', json_build_object(
  'topic','voicetasks:'||$1::text,'kind','voice.tasks.changed','resource_id',$2::text,
  'sequence',0,'revision',0)::text)
`

type NotifyVoiceLiveRoomAlertParams struct {
	RoomID string `json:"room_id"`
	WakeID string `json:"wake_id"`
}

// Tells live calls in a Room that an app event waits to be told.
func (q *Queries) NotifyVoiceLiveRoomAlert(ctx context.Context, arg NotifyVoiceLiveRoomAlertParams) error {
	_, err := q.db.Exec(ctx, notifyVoiceLiveRoomAlert, arg.RoomID, arg.WakeID)
	return err
}

const notifyVoiceLiveTaskChanged = `-- name: NotifyVoiceLiveTaskChanged :exec
SELECT pg_notify('teamhub_realtime', json_build_object(
  'topic','voicetasks:'||task.room_id,'kind','voice.tasks.changed','resource_id',task.work_item_id,
  'sequence',0,'revision',0)::text)
FROM voice_live_tasks task WHERE task.work_item_id=$1::text
`

// Tells live calls in a voice task's Room that it changed; nothing for other
// Tasks. Delivered on commit, to every TeamHub process.
func (q *Queries) NotifyVoiceLiveTaskChanged(ctx context.Context, workItemID string) error {
	_, err := q.db.Exec(ctx, notifyVoiceLiveTaskChanged, workItemID)
	return err
}

const useVoiceLiveConfirmation = `-- name: UseVoiceLiveConfirmation :one
UPDATE voice_live_confirmations SET used_at=clock_timestamp(),used_turn_id=$1::text
WHERE id=$2::text AND voice_session_id=$3::text AND tool=$4::text
  AND turn_id<>$1::text AND used_at IS NULL AND created_at>$5::timestamptz
RETURNING goal,surface
`

type UseVoiceLiveConfirmationParams struct {
	TurnID         string             `json:"turn_id"`
	ID             string             `json:"id"`
	VoiceSessionID string             `json:"voice_session_id"`
	Tool           string             `json:"tool"`
	Since          pgtype.Timestamptz `json:"since"`
}

type UseVoiceLiveConfirmationRow struct {
	Goal    string `json:"goal"`
	Surface string `json:"surface"`
}

// A confirmation counts once, from a later turn of the same call, while fresh.
func (q *Queries) UseVoiceLiveConfirmation(ctx context.Context, arg UseVoiceLiveConfirmationParams) (UseVoiceLiveConfirmationRow, error) {
	row := q.db.QueryRow(ctx, useVoiceLiveConfirmation,
		arg.TurnID,
		arg.ID,
		arg.VoiceSessionID,
		arg.Tool,
		arg.Since,
	)
	var i UseVoiceLiveConfirmationRow
	err := row.Scan(&i.Goal, &i.Surface)
	return i, err
}

const useVoiceLiveConfirmationFor = `-- name: UseVoiceLiveConfirmationFor :execrows
UPDATE voice_live_confirmations SET used_at=clock_timestamp(),used_turn_id=$1::text
WHERE id=(SELECT pending.id FROM voice_live_confirmations pending
  WHERE pending.voice_session_id=$2::text AND pending.tool=$3::text
    AND pending.arguments_hash=$4::text AND pending.turn_id<>$1::text
    AND pending.used_at IS NULL AND pending.created_at>$5::timestamptz
  ORDER BY pending.created_at DESC LIMIT 1 FOR UPDATE SKIP LOCKED)
  OR id=(SELECT used.id FROM voice_live_confirmations used
  WHERE used.voice_session_id=$2::text AND used.tool=$3::text
    AND used.arguments_hash=$4::text AND used.used_turn_id=$1::text
  LIMIT 1)
`

type UseVoiceLiveConfirmationForParams struct {
	TurnID         string             `json:"turn_id"`
	VoiceSessionID string             `json:"voice_session_id"`
	Tool           string             `json:"tool"`
	ArgumentsHash  string             `json:"arguments_hash"`
	Since          pgtype.Timestamptz `json:"since"`
}

// The latest confirmation asked for exactly these arguments, used once; a
// retry within the turn that used it is admitted again.
func (q *Queries) UseVoiceLiveConfirmationFor(ctx context.Context, arg UseVoiceLiveConfirmationForParams) (int64, error) {
	result, err := q.db.Exec(ctx, useVoiceLiveConfirmationFor,
		arg.TurnID,
		arg.VoiceSessionID,
		arg.Tool,
		arg.ArgumentsHash,
		arg.Since,
	)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}