2026-10-08 16:12:20 +08:00
|
|
|
// Code generated by sqlc. DO NOT EDIT.
|
|
|
|
|
// versions:
|
|
|
|
|
// sqlc v1.30.0
|
|
|
|
|
// source: query.sql
|
|
|
|
|
|
|
|
|
|
package sqlc
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2026-10-29 12:08:00 +08:00
|
|
|
const calibrateTurnCounts = `-- name: CalibrateTurnCounts :one
|
|
|
|
|
SELECT session_calibrate_turn_counts(100)::integer
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) CalibrateTurnCounts(ctx context.Context) (int32, error) {
|
|
|
|
|
row := q.db.QueryRow(ctx, calibrateTurnCounts)
|
|
|
|
|
var column_1 int32
|
|
|
|
|
err := row.Scan(&column_1)
|
|
|
|
|
return column_1, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-08 16:12:20 +08:00
|
|
|
const claimSession = `-- name: ClaimSession :exec
|
|
|
|
|
UPDATE sessions SET
|
|
|
|
|
parent_session_id=COALESCE(parent_session_id,NULLIF($1::text,'')::uuid),
|
|
|
|
|
group_id=COALESCE(group_id,NULLIF($2::text,'')::uuid),
|
|
|
|
|
device_id=COALESCE(device_id,NULLIF($3::text,'')),updated_at=now()
|
|
|
|
|
WHERE id=$4::uuid
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
type ClaimSessionParams struct {
|
|
|
|
|
ParentID string
|
|
|
|
|
GroupID string
|
|
|
|
|
MachineID string
|
|
|
|
|
ID string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (q *Queries) ClaimSession(ctx context.Context, arg ClaimSessionParams) error {
|
|
|
|
|
_, err := q.db.Exec(ctx, claimSession,
|
|
|
|
|
arg.ParentID,
|
|
|
|
|
arg.GroupID,
|
|
|
|
|
arg.MachineID,
|
|
|
|
|
arg.ID,
|
|
|
|
|
)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-29 12:08:00 +08:00
|
|
|
const copyTurnBatch = `-- name: CopyTurnBatch :one
|
|
|
|
|
SELECT session_copy_turn_batch(1000)::integer
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) CopyTurnBatch(ctx context.Context) (int32, error) {
|
|
|
|
|
row := q.db.QueryRow(ctx, copyTurnBatch)
|
|
|
|
|
var column_1 int32
|
|
|
|
|
err := row.Scan(&column_1)
|
|
|
|
|
return column_1, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-08 16:12:20 +08:00
|
|
|
const countOpenPlaceholders = `-- name: CountOpenPlaceholders :one
|
|
|
|
|
SELECT count(*) FROM sessions
|
|
|
|
|
WHERE owner_user_id = $1::uuid AND placeholder = true
|
|
|
|
|
AND deleted_at IS NULL AND purged_at IS NULL
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) CountOpenPlaceholders(ctx context.Context, dollar_1 string) (int64, error) {
|
|
|
|
|
row := q.db.QueryRow(ctx, countOpenPlaceholders, dollar_1)
|
|
|
|
|
var count int64
|
|
|
|
|
err := row.Scan(&count)
|
|
|
|
|
return count, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 17:32:21 +08:00
|
|
|
const countSessionIndex = `-- name: CountSessionIndex :one
|
|
|
|
|
SELECT count(*) FROM sessions WHERE owner_user_id=$1 AND device_id IS NOT NULL AND host_verified AND NOT placeholder
|
|
|
|
|
AND deleted_at IS NULL AND client_deleted_at IS NULL AND purged_at IS NULL
|
|
|
|
|
AND ($2::text='' OR device_id=$2::text)
|
|
|
|
|
AND ($3::text='' OR id=NULLIF($3::text,'')::uuid)
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
type CountSessionIndexParams struct {
|
|
|
|
|
OwnerUserID string
|
|
|
|
|
MachineID string
|
|
|
|
|
SessionID string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (q *Queries) CountSessionIndex(ctx context.Context, arg CountSessionIndexParams) (int64, error) {
|
|
|
|
|
row := q.db.QueryRow(ctx, countSessionIndex, arg.OwnerUserID, arg.MachineID, arg.SessionID)
|
|
|
|
|
var count int64
|
|
|
|
|
err := row.Scan(&count)
|
|
|
|
|
return count, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-08 16:12:20 +08:00
|
|
|
const currentSessionGroup = `-- name: CurrentSessionGroup :one
|
|
|
|
|
SELECT COALESCE(
|
|
|
|
|
(SELECT s.group_id::text FROM sessions s
|
|
|
|
|
WHERE s.id = NULLIF($1::text, '')::uuid
|
|
|
|
|
AND s.owner_user_id = $2::uuid),
|
|
|
|
|
(SELECT a.group_id::text FROM credit_accounts a
|
|
|
|
|
WHERE a.user_id = $2::uuid
|
|
|
|
|
AND a.period_start_at <= now() AND a.period_end_at > now()
|
|
|
|
|
AND a.group_id IS NOT NULL
|
|
|
|
|
ORDER BY a.period_start_at DESC LIMIT 1),
|
|
|
|
|
(SELECT CASE WHEN count(*) = 1 THEN min(g.id::text) END
|
|
|
|
|
FROM group_users gu JOIN groups g ON g.id = gu.group_id AND g.deleted_at IS NULL
|
|
|
|
|
WHERE gu.user_id = $2::uuid AND gu.removed_at IS NULL),
|
|
|
|
|
''
|
|
|
|
|
)::text AS group_id
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
type CurrentSessionGroupParams struct {
|
|
|
|
|
ParentID string
|
|
|
|
|
UserID string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (q *Queries) CurrentSessionGroup(ctx context.Context, arg CurrentSessionGroupParams) (string, error) {
|
|
|
|
|
row := q.db.QueryRow(ctx, currentSessionGroup, arg.ParentID, arg.UserID)
|
|
|
|
|
var group_id string
|
|
|
|
|
err := row.Scan(&group_id)
|
|
|
|
|
return group_id, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-29 12:08:00 +08:00
|
|
|
const cutoverTurns = `-- name: CutoverTurns :one
|
|
|
|
|
SELECT session_cutover_turns()::boolean
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) CutoverTurns(ctx context.Context) (bool, error) {
|
|
|
|
|
row := q.db.QueryRow(ctx, cutoverTurns)
|
|
|
|
|
var column_1 bool
|
|
|
|
|
err := row.Scan(&column_1)
|
|
|
|
|
return column_1, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-08 16:12:20 +08:00
|
|
|
const deleteImageCalls = `-- name: DeleteImageCalls :exec
|
|
|
|
|
DELETE FROM image_calls WHERE session_id=ANY($1::uuid[])
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) DeleteImageCalls(ctx context.Context, dollar_1 []string) error {
|
|
|
|
|
_, err := q.db.Exec(ctx, deleteImageCalls, dollar_1)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const deleteMCPCalls = `-- name: DeleteMCPCalls :exec
|
|
|
|
|
DELETE FROM mcp_tool_calls WHERE session_id=ANY($1::uuid[])
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) DeleteMCPCalls(ctx context.Context, dollar_1 []string) error {
|
|
|
|
|
_, err := q.db.Exec(ctx, deleteMCPCalls, dollar_1)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const deleteModelCalls = `-- name: DeleteModelCalls :exec
|
|
|
|
|
DELETE FROM model_calls WHERE session_id=ANY($1::uuid[])
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) DeleteModelCalls(ctx context.Context, dollar_1 []string) error {
|
|
|
|
|
_, err := q.db.Exec(ctx, deleteModelCalls, dollar_1)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const deleteSkillEvents = `-- name: DeleteSkillEvents :exec
|
|
|
|
|
DELETE FROM session_skill_events WHERE session_id=ANY($1::uuid[])
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) DeleteSkillEvents(ctx context.Context, dollar_1 []string) error {
|
|
|
|
|
_, err := q.db.Exec(ctx, deleteSkillEvents, dollar_1)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const deleteSnapshotItems = `-- name: DeleteSnapshotItems :exec
|
|
|
|
|
DELETE FROM session_resource_snapshot_items WHERE session_id=ANY($1::uuid[])
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) DeleteSnapshotItems(ctx context.Context, dollar_1 []string) error {
|
|
|
|
|
_, err := q.db.Exec(ctx, deleteSnapshotItems, dollar_1)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const deleteSnapshots = `-- name: DeleteSnapshots :exec
|
|
|
|
|
DELETE FROM session_resource_snapshots WHERE session_id=ANY($1::uuid[])
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) DeleteSnapshots(ctx context.Context, dollar_1 []string) error {
|
|
|
|
|
_, err := q.db.Exec(ctx, deleteSnapshots, dollar_1)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const deleteTurnTools = `-- name: DeleteTurnTools :exec
|
|
|
|
|
DELETE FROM session_turn_tools WHERE session_id=ANY($1::uuid[])
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) DeleteTurnTools(ctx context.Context, dollar_1 []string) error {
|
|
|
|
|
_, err := q.db.Exec(ctx, deleteTurnTools, dollar_1)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const deleteTurns = `-- name: DeleteTurns :exec
|
2026-10-29 12:08:00 +08:00
|
|
|
DELETE FROM session_turns t USING unnest($1::uuid[]) sid, LATERAL session_turn_times(sid) AS k(turn_index,started_at)
|
|
|
|
|
WHERE t.session_id=sid AND t.turn_index=k.turn_index AND t.started_at=k.started_at
|
2026-10-08 16:12:20 +08:00
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) DeleteTurns(ctx context.Context, dollar_1 []string) error {
|
|
|
|
|
_, err := q.db.Exec(ctx, deleteTurns, dollar_1)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-29 12:08:00 +08:00
|
|
|
const ensureTurnMonths = `-- name: EnsureTurnMonths :one
|
|
|
|
|
SELECT session_ensure_turn_months($1::timestamptz)::boolean
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) EnsureTurnMonths(ctx context.Context, atTime time.Time) (bool, error) {
|
|
|
|
|
row := q.db.QueryRow(ctx, ensureTurnMonths, atTime)
|
|
|
|
|
var column_1 bool
|
|
|
|
|
err := row.Scan(&column_1)
|
|
|
|
|
return column_1, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-08 16:12:20 +08:00
|
|
|
const getParent = `-- name: GetParent :one
|
|
|
|
|
SELECT jsonb_build_object('owner',owner_user_id,'parent',parent_session_id,'purged',purged_at,'deleted',deleted_at) AS data
|
|
|
|
|
FROM sessions WHERE id = $1::uuid
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) GetParent(ctx context.Context, dollar_1 string) ([]byte, error) {
|
|
|
|
|
row := q.db.QueryRow(ctx, getParent, dollar_1)
|
|
|
|
|
var data []byte
|
|
|
|
|
err := row.Scan(&data)
|
|
|
|
|
return data, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getSession = `-- name: GetSession :one
|
|
|
|
|
SELECT jsonb_build_object(
|
|
|
|
|
'id',id,'owner_user_id',owner_user_id,'group_id',group_id,'parent_session_id',parent_session_id,
|
2026-10-09 17:19:02 +08:00
|
|
|
'model_id',model_id,'device_id',device_id,'title',title,'title_source',title_source,'session_type',session_type,
|
2026-10-08 16:12:20 +08:00
|
|
|
'client_type',client_type,'client_name',client_name,'started_at',started_at,
|
|
|
|
|
'last_active_at',last_active_at,'ended_at',ended_at,'deleted_at',deleted_at,'purged_at',purged_at,
|
|
|
|
|
'placeholder',placeholder,'started_provis',started_at_provisional,'clock_suspect',clock_suspect,
|
|
|
|
|
'state_seq',state_seq,'state_hash',encode(state_hash,'base64'),'state_received_at',state_received_at,
|
|
|
|
|
'resources_id',resources_snapshot_id,'acked_turn',acked_turn,'facts_version',facts_version,
|
|
|
|
|
'last_stop_reason',last_stop_reason,'active_seconds',active_seconds,
|
2026-10-19 17:32:21 +08:00
|
|
|
'client_deleted_at',client_deleted_at,'reporting_enabled_at',reporting_enabled_at,'host_verified',host_verified
|
2026-10-08 16:12:20 +08:00
|
|
|
) AS data FROM sessions WHERE id = $1::uuid
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) GetSession(ctx context.Context, dollar_1 string) ([]byte, error) {
|
|
|
|
|
row := q.db.QueryRow(ctx, getSession, dollar_1)
|
|
|
|
|
var data []byte
|
|
|
|
|
err := row.Scan(&data)
|
|
|
|
|
return data, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getSnapshotItems = `-- name: GetSnapshotItems :one
|
|
|
|
|
SELECT items FROM session_resource_snapshots WHERE session_id=$1::uuid AND snapshot_id=$2
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
type GetSnapshotItemsParams struct {
|
|
|
|
|
Column1 string
|
|
|
|
|
SnapshotID string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (q *Queries) GetSnapshotItems(ctx context.Context, arg GetSnapshotItemsParams) ([]byte, error) {
|
|
|
|
|
row := q.db.QueryRow(ctx, getSnapshotItems, arg.Column1, arg.SnapshotID)
|
|
|
|
|
var items []byte
|
|
|
|
|
err := row.Scan(&items)
|
|
|
|
|
return items, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getTurnHash = `-- name: GetTurnHash :one
|
2026-10-29 12:08:00 +08:00
|
|
|
SELECT report_hash FROM session_turns WHERE session_id=$1::uuid AND turn_index=$2 AND started_at=session_turn_time($1::uuid,$2) FOR UPDATE
|
2026-10-08 16:12:20 +08:00
|
|
|
`
|
|
|
|
|
|
|
|
|
|
type GetTurnHashParams struct {
|
|
|
|
|
Column1 string
|
|
|
|
|
TurnIndex int32
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (q *Queries) GetTurnHash(ctx context.Context, arg GetTurnHashParams) ([]byte, error) {
|
|
|
|
|
row := q.db.QueryRow(ctx, getTurnHash, arg.Column1, arg.TurnIndex)
|
|
|
|
|
var report_hash []byte
|
|
|
|
|
err := row.Scan(&report_hash)
|
|
|
|
|
return report_hash, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const hasSnapshot = `-- name: HasSnapshot :one
|
|
|
|
|
SELECT EXISTS(SELECT 1 FROM session_resource_snapshots WHERE session_id=$1::uuid AND snapshot_id=$2)
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
type HasSnapshotParams struct {
|
|
|
|
|
Column1 string
|
|
|
|
|
SnapshotID string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (q *Queries) HasSnapshot(ctx context.Context, arg HasSnapshotParams) (bool, error) {
|
|
|
|
|
row := q.db.QueryRow(ctx, hasSnapshot, arg.Column1, arg.SnapshotID)
|
|
|
|
|
var exists bool
|
|
|
|
|
err := row.Scan(&exists)
|
|
|
|
|
return exists, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const insertPlaceholder = `-- name: InsertPlaceholder :exec
|
|
|
|
|
INSERT INTO sessions (
|
|
|
|
|
id,owner_user_id,group_id,parent_session_id,title,session_type,client_type,client_name,
|
|
|
|
|
device_id,started_at,last_active_at,placeholder,started_at_provisional,reporting_enabled_at
|
|
|
|
|
) VALUES (
|
|
|
|
|
$1::uuid,$2::uuid,NULLIF($3::text,'')::uuid,
|
|
|
|
|
NULLIF($4::text,'')::uuid,'','conversation','unknown','',
|
|
|
|
|
NULLIF($5::text,''),$6::timestamptz,NULL,true,true,now()
|
|
|
|
|
) ON CONFLICT (id) DO NOTHING
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
type InsertPlaceholderParams struct {
|
|
|
|
|
ID string
|
|
|
|
|
OwnerUserID string
|
|
|
|
|
GroupID string
|
|
|
|
|
ParentSessionID string
|
|
|
|
|
MachineID string
|
|
|
|
|
StartedAt time.Time
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (q *Queries) InsertPlaceholder(ctx context.Context, arg InsertPlaceholderParams) error {
|
|
|
|
|
_, err := q.db.Exec(ctx, insertPlaceholder,
|
|
|
|
|
arg.ID,
|
|
|
|
|
arg.OwnerUserID,
|
|
|
|
|
arg.GroupID,
|
|
|
|
|
arg.ParentSessionID,
|
|
|
|
|
arg.MachineID,
|
|
|
|
|
arg.StartedAt,
|
|
|
|
|
)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const listTurnFacts = `-- name: ListTurnFacts :many
|
|
|
|
|
SELECT jsonb_build_object(
|
2026-10-29 12:08:00 +08:00
|
|
|
'turn_index',t.turn_index,'started_at',t.started_at,'ended_at',ended_at,
|
2026-10-08 16:12:20 +08:00
|
|
|
'stop_reason',stop_reason,'facts_version',facts_version,
|
|
|
|
|
'resources_snapshot_id',resources_snapshot_id,'error_code',error_code
|
2026-10-29 12:08:00 +08:00
|
|
|
) AS data FROM session_turn_times($1::uuid) AS k(turn_index,started_at) CROSS JOIN LATERAL (SELECT t.session_id, t.turn_index, t.facts_version, t.report_hash, t.input_seq, t.started_at, t.ended_at, t.received_at, t.stop_reason, t.error_code, t.recovered, t.model_id, t.thinking_enabled, t.thinking_effort, t.resources_snapshot_id, t.client_version, t.engine_version, t.input_tokens, t.output_tokens, t.cache_creation_input_tokens, t.cache_read_input_tokens, t.subagent_input_tokens, t.subagent_output_tokens, t.subagent_cache_creation_input_tokens, t.subagent_cache_read_input_tokens, t.context_used, t.context_window, t.input_kind, t.input_client_type, t.input_machine_id, t.command_skill_id, t.attachments, t.canvas_nodes, t.steers, t.files_created, t.files_updated, t.files_deleted, t.compactions, t.permissions_asked, t.permissions_allowed, t.permissions_denied, t.truncated, t.input_text, t.input_text_truncated, t.title, t.title_source FROM session_turns t WHERE t.session_id=$1::uuid AND t.turn_index=k.turn_index AND t.started_at=k.started_at OFFSET 0) t ORDER BY t.turn_index
|
2026-10-08 16:12:20 +08:00
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) ListTurnFacts(ctx context.Context, dollar_1 string) ([][]byte, error) {
|
|
|
|
|
rows, err := q.db.Query(ctx, listTurnFacts, dollar_1)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
items := [][]byte{}
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
var data []byte
|
|
|
|
|
if err := rows.Scan(&data); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
items = append(items, data)
|
|
|
|
|
}
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return items, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const listTurnIndexes = `-- name: ListTurnIndexes :many
|
2026-10-29 12:08:00 +08:00
|
|
|
SELECT k.turn_index::integer AS turn_index FROM session_turn_times($1::uuid) AS k(turn_index,started_at) ORDER BY k.turn_index
|
2026-10-08 16:12:20 +08:00
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) ListTurnIndexes(ctx context.Context, dollar_1 string) ([]int32, error) {
|
|
|
|
|
rows, err := q.db.Query(ctx, listTurnIndexes, dollar_1)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
items := []int32{}
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
var turn_index int32
|
|
|
|
|
if err := rows.Scan(&turn_index); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
items = append(items, turn_index)
|
|
|
|
|
}
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return items, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const lockFamily = `-- name: LockFamily :many
|
|
|
|
|
SELECT id::text FROM sessions WHERE id=$1::uuid OR parent_session_id=$1::uuid ORDER BY id FOR UPDATE
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) LockFamily(ctx context.Context, dollar_1 string) ([]string, error) {
|
|
|
|
|
rows, err := q.db.Query(ctx, lockFamily, dollar_1)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
items := []string{}
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
var id string
|
|
|
|
|
if err := rows.Scan(&id); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
items = append(items, id)
|
|
|
|
|
}
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return items, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const lockReportingUser = `-- name: LockReportingUser :one
|
2026-10-28 11:10:49 +08:00
|
|
|
SELECT id::text FROM users WHERE id = $1::uuid FOR NO KEY UPDATE
|
2026-10-08 16:12:20 +08:00
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) LockReportingUser(ctx context.Context, dollar_1 string) (string, error) {
|
|
|
|
|
row := q.db.QueryRow(ctx, lockReportingUser, dollar_1)
|
|
|
|
|
var id string
|
|
|
|
|
err := row.Scan(&id)
|
|
|
|
|
return id, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const lockSession = `-- name: LockSession :one
|
|
|
|
|
SELECT jsonb_build_object(
|
|
|
|
|
'id',id,'owner_user_id',owner_user_id,'group_id',group_id,'parent_session_id',parent_session_id,
|
2026-10-09 17:19:02 +08:00
|
|
|
'model_id',model_id,'device_id',device_id,'title',title,'title_source',title_source,'session_type',session_type,
|
2026-10-08 16:12:20 +08:00
|
|
|
'client_type',client_type,'client_name',client_name,'started_at',started_at,
|
|
|
|
|
'last_active_at',last_active_at,'ended_at',ended_at,'deleted_at',deleted_at,'purged_at',purged_at,
|
|
|
|
|
'placeholder',placeholder,'started_provis',started_at_provisional,'clock_suspect',clock_suspect,
|
|
|
|
|
'state_seq',state_seq,'state_hash',encode(state_hash,'base64'),'state_received_at',state_received_at,
|
|
|
|
|
'resources_id',resources_snapshot_id,'acked_turn',acked_turn,'facts_version',facts_version,
|
|
|
|
|
'last_stop_reason',last_stop_reason,'active_seconds',active_seconds,
|
2026-10-19 17:32:21 +08:00
|
|
|
'client_deleted_at',client_deleted_at,'reporting_enabled_at',reporting_enabled_at,'host_verified',host_verified
|
2026-10-28 11:10:49 +08:00
|
|
|
) AS data FROM sessions WHERE id = $1::uuid FOR NO KEY UPDATE
|
2026-10-08 16:12:20 +08:00
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) LockSession(ctx context.Context, dollar_1 string) ([]byte, error) {
|
|
|
|
|
row := q.db.QueryRow(ctx, lockSession, dollar_1)
|
|
|
|
|
var data []byte
|
|
|
|
|
err := row.Scan(&data)
|
|
|
|
|
return data, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const purgeSessions = `-- name: PurgeSessions :exec
|
|
|
|
|
UPDATE sessions SET deleted_at=COALESCE(deleted_at,now()),client_deleted_at=COALESCE(client_deleted_at,now()),
|
2026-10-09 17:19:02 +08:00
|
|
|
title='',title_source=NULL,purged_at=COALESCE(purged_at,now()),updated_at=now() WHERE id=ANY($1::uuid[])
|
2026-10-08 16:12:20 +08:00
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) PurgeSessions(ctx context.Context, dollar_1 []string) error {
|
|
|
|
|
_, err := q.db.Exec(ctx, purgeSessions, dollar_1)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const recomputeSession = `-- name: RecomputeSession :exec
|
|
|
|
|
UPDATE sessions SET
|
2026-10-29 12:08:00 +08:00
|
|
|
turn_count=($1::jsonb->>'turn_count')::integer,turn_count_verified=true,
|
2026-10-08 16:12:20 +08:00
|
|
|
acked_turn=($1::jsonb->>'acked_turn')::integer,
|
|
|
|
|
active_seconds=($1::jsonb->>'active_seconds')::bigint,
|
|
|
|
|
started_at=CASE WHEN started_at_provisional AND
|
|
|
|
|
($1::jsonb->>'first_started')::timestamptz < started_at
|
|
|
|
|
THEN ($1::jsonb->>'first_started')::timestamptz ELSE started_at END,
|
|
|
|
|
last_active_at=($1::jsonb->>'last_active_at')::timestamptz,
|
|
|
|
|
ended_at=($1::jsonb->>'ended_at')::timestamptz,
|
|
|
|
|
facts_version=NULLIF(($1::jsonb->>'facts_version')::integer,0),
|
|
|
|
|
failure_code=$1::jsonb->>'failure_code',
|
|
|
|
|
last_stop_reason=NULLIF($1::jsonb->>'last_stop_reason',''),
|
|
|
|
|
resources_snapshot_id=NULLIF($1::jsonb->>'resources_snapshot_id',''),
|
|
|
|
|
reporting_enabled_at=COALESCE(reporting_enabled_at,($1::jsonb->>'received_at')::timestamptz),
|
|
|
|
|
updated_at=($1::jsonb->>'received_at')::timestamptz
|
|
|
|
|
WHERE id=($1::jsonb->>'id')::uuid
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) RecomputeSession(ctx context.Context, payload []byte) error {
|
|
|
|
|
_, err := q.db.Exec(ctx, recomputeSession, payload)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const saveSession = `-- name: SaveSession :exec
|
|
|
|
|
UPDATE sessions SET
|
2026-10-09 17:19:02 +08:00
|
|
|
title=COALESCE($1::jsonb->>'title',title),
|
|
|
|
|
title_source=COALESCE($1::jsonb->>'title_source',title_source),
|
2026-10-08 16:12:20 +08:00
|
|
|
session_type=$1::jsonb->>'session_type',
|
|
|
|
|
client_type=$1::jsonb->>'client_type',
|
|
|
|
|
client_name=$1::jsonb->>'client_type',
|
|
|
|
|
client_version=$1::jsonb->>'client_version',
|
|
|
|
|
engine_version=$1::jsonb->>'engine_version',
|
|
|
|
|
runtime_version=$1::jsonb->>'runtime_version',
|
|
|
|
|
expert_id=NULLIF($1::jsonb->>'expert_id','')::uuid,
|
|
|
|
|
model_id=NULLIF($1::jsonb->>'model_id','')::uuid,
|
|
|
|
|
mode=$1::jsonb->>'mode',
|
|
|
|
|
workspace_kind=$1::jsonb->>'workspace_kind',
|
|
|
|
|
started_at=($1::jsonb->>'started_at')::timestamptz,
|
|
|
|
|
client_deleted_at=($1::jsonb->>'client_deleted_at')::timestamptz,
|
|
|
|
|
placeholder=false,started_at_provisional=false,
|
|
|
|
|
reporting_enabled_at=COALESCE(reporting_enabled_at,($1::jsonb->>'received_at')::timestamptz),
|
|
|
|
|
updated_at=($1::jsonb->>'received_at')::timestamptz
|
|
|
|
|
WHERE id=($1::jsonb->>'id')::uuid
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) SaveSession(ctx context.Context, payload []byte) error {
|
|
|
|
|
_, err := q.db.Exec(ctx, saveSession, payload)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const saveSkillEvent = `-- name: SaveSkillEvent :exec
|
|
|
|
|
INSERT INTO session_skill_events
|
|
|
|
|
SELECT (jsonb_populate_record(NULL::session_skill_events,$1::jsonb)).*
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) SaveSkillEvent(ctx context.Context, payload []byte) error {
|
|
|
|
|
_, err := q.db.Exec(ctx, saveSkillEvent, payload)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const saveSnapshot = `-- name: SaveSnapshot :exec
|
|
|
|
|
INSERT INTO session_resource_snapshots(session_id,snapshot_id,revision,state_version,items)
|
|
|
|
|
VALUES (($1::jsonb->>'session_id')::uuid,$1::jsonb->>'snapshot_id',
|
|
|
|
|
($1::jsonb->>'revision')::bigint,($1::jsonb->>'state_version')::bigint,
|
|
|
|
|
$1::jsonb->'items')
|
|
|
|
|
ON CONFLICT (session_id,snapshot_id) DO NOTHING
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) SaveSnapshot(ctx context.Context, payload []byte) error {
|
|
|
|
|
_, err := q.db.Exec(ctx, saveSnapshot, payload)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const saveSnapshotItem = `-- name: SaveSnapshotItem :exec
|
|
|
|
|
INSERT INTO session_resource_snapshot_items
|
|
|
|
|
SELECT (jsonb_populate_record(NULL::session_resource_snapshot_items,$1::jsonb)).*
|
|
|
|
|
ON CONFLICT (session_id,snapshot_id,resource_id) DO NOTHING
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) SaveSnapshotItem(ctx context.Context, payload []byte) error {
|
|
|
|
|
_, err := q.db.Exec(ctx, saveSnapshotItem, payload)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const saveTool = `-- name: SaveTool :exec
|
|
|
|
|
INSERT INTO session_turn_tools
|
|
|
|
|
SELECT (jsonb_populate_record(NULL::session_turn_tools,$1::jsonb)).*
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) SaveTool(ctx context.Context, payload []byte) error {
|
|
|
|
|
_, err := q.db.Exec(ctx, saveTool, payload)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const saveTurn = `-- name: SaveTurn :exec
|
|
|
|
|
INSERT INTO session_turns
|
|
|
|
|
SELECT (jsonb_populate_record(NULL::session_turns,$1::jsonb)).*
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) SaveTurn(ctx context.Context, payload []byte) error {
|
|
|
|
|
_, err := q.db.Exec(ctx, saveTurn, payload)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 17:32:21 +08:00
|
|
|
const sessionIndex = `-- name: SessionIndex :many
|
|
|
|
|
SELECT id,device_id,title,title_source,session_type,parent_session_id,state_seq,host_revision,
|
|
|
|
|
started_at,last_active_at,state_received_at,last_live_report_at,live_boot_id,live_lease_epoch
|
|
|
|
|
FROM sessions WHERE owner_user_id=$1 AND device_id IS NOT NULL AND host_verified AND NOT placeholder
|
|
|
|
|
AND deleted_at IS NULL AND client_deleted_at IS NULL AND purged_at IS NULL
|
|
|
|
|
AND ($2::text='' OR device_id=$2::text)
|
|
|
|
|
AND ($3::text='' OR id=NULLIF($3::text,'')::uuid)
|
|
|
|
|
ORDER BY started_at DESC,id DESC LIMIT $5::integer OFFSET $4::integer
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
type SessionIndexParams struct {
|
|
|
|
|
OwnerUserID string
|
|
|
|
|
MachineID string
|
|
|
|
|
SessionID string
|
|
|
|
|
PageOffset int32
|
|
|
|
|
PageSize int32
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SessionIndexRow struct {
|
|
|
|
|
ID string
|
|
|
|
|
DeviceID *string
|
|
|
|
|
Title string
|
|
|
|
|
TitleSource *string
|
|
|
|
|
SessionType string
|
|
|
|
|
ParentSessionID *string
|
|
|
|
|
StateSeq int64
|
|
|
|
|
HostRevision int64
|
|
|
|
|
StartedAt time.Time
|
|
|
|
|
LastActiveAt *time.Time
|
|
|
|
|
StateReceivedAt *time.Time
|
|
|
|
|
LastLiveReportAt *time.Time
|
|
|
|
|
LiveBootID *string
|
|
|
|
|
LiveLeaseEpoch *int64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (q *Queries) SessionIndex(ctx context.Context, arg SessionIndexParams) ([]SessionIndexRow, error) {
|
|
|
|
|
rows, err := q.db.Query(ctx, sessionIndex,
|
|
|
|
|
arg.OwnerUserID,
|
|
|
|
|
arg.MachineID,
|
|
|
|
|
arg.SessionID,
|
|
|
|
|
arg.PageOffset,
|
|
|
|
|
arg.PageSize,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
items := []SessionIndexRow{}
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
var i SessionIndexRow
|
|
|
|
|
if err := rows.Scan(
|
|
|
|
|
&i.ID,
|
|
|
|
|
&i.DeviceID,
|
|
|
|
|
&i.Title,
|
|
|
|
|
&i.TitleSource,
|
|
|
|
|
&i.SessionType,
|
|
|
|
|
&i.ParentSessionID,
|
|
|
|
|
&i.StateSeq,
|
|
|
|
|
&i.HostRevision,
|
|
|
|
|
&i.StartedAt,
|
|
|
|
|
&i.LastActiveAt,
|
|
|
|
|
&i.StateReceivedAt,
|
|
|
|
|
&i.LastLiveReportAt,
|
|
|
|
|
&i.LiveBootID,
|
|
|
|
|
&i.LiveLeaseEpoch,
|
|
|
|
|
); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
items = append(items, i)
|
|
|
|
|
}
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return items, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-08 16:12:20 +08:00
|
|
|
const sessionStats = `-- name: SessionStats :one
|
|
|
|
|
SELECT jsonb_build_object(
|
2026-10-29 12:08:00 +08:00
|
|
|
'turns',(SELECT count(*) FROM session_turn_times($1::uuid)),
|
2026-10-08 16:12:20 +08:00
|
|
|
'tools',(SELECT count(*) FROM session_turn_tools WHERE session_id=$1::uuid),
|
|
|
|
|
'skill_events',(SELECT count(*) FROM session_skill_events WHERE session_id=$1::uuid),
|
|
|
|
|
'resource_snapshots',(SELECT count(*) FROM session_resource_snapshots WHERE session_id=$1::uuid),
|
|
|
|
|
'model_calls',(SELECT count(*) FROM model_calls WHERE session_id=$1::uuid),
|
|
|
|
|
'mcp_tool_calls',(SELECT count(*) FROM mcp_tool_calls WHERE session_id=$1::uuid),
|
|
|
|
|
'image_calls',(SELECT count(*) FROM image_calls WHERE session_id=$1::uuid)
|
|
|
|
|
) AS data
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) SessionStats(ctx context.Context, dollar_1 string) ([]byte, error) {
|
|
|
|
|
row := q.db.QueryRow(ctx, sessionStats, dollar_1)
|
|
|
|
|
var data []byte
|
|
|
|
|
err := row.Scan(&data)
|
|
|
|
|
return data, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 17:32:21 +08:00
|
|
|
const setLiveSummary = `-- name: SetLiveSummary :exec
|
|
|
|
|
UPDATE sessions SET last_live_report_at=$2,live_boot_id=$3,live_lease_epoch=$4 WHERE id=$1
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
type SetLiveSummaryParams struct {
|
|
|
|
|
ID string
|
|
|
|
|
LastLiveReportAt *time.Time
|
|
|
|
|
LiveBootID *string
|
|
|
|
|
LiveLeaseEpoch *int64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (q *Queries) SetLiveSummary(ctx context.Context, arg SetLiveSummaryParams) error {
|
|
|
|
|
_, err := q.db.Exec(ctx, setLiveSummary,
|
|
|
|
|
arg.ID,
|
|
|
|
|
arg.LastLiveReportAt,
|
|
|
|
|
arg.LiveBootID,
|
|
|
|
|
arg.LiveLeaseEpoch,
|
|
|
|
|
)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-08 16:12:20 +08:00
|
|
|
const setState = `-- name: SetState :exec
|
|
|
|
|
UPDATE sessions SET state_seq=($1::jsonb->>'state_seq')::bigint,
|
|
|
|
|
state_hash=decode($1::jsonb->>'state_hash','hex'),
|
|
|
|
|
state_received_at=($1::jsonb->>'received_at')::timestamptz,
|
|
|
|
|
updated_at=($1::jsonb->>'received_at')::timestamptz
|
|
|
|
|
WHERE id=($1::jsonb->>'id')::uuid
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) SetState(ctx context.Context, payload []byte) error {
|
|
|
|
|
_, err := q.db.Exec(ctx, setState, payload)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const takeReportingRateLimit = `-- name: TakeReportingRateLimit :one
|
|
|
|
|
INSERT INTO session_reporting_rate_limits (user_id, kind, bucket_start, request_count)
|
|
|
|
|
VALUES ($1::uuid, $2::text, $3::timestamptz, 1)
|
|
|
|
|
ON CONFLICT (user_id, kind) DO UPDATE SET
|
|
|
|
|
request_count = CASE
|
|
|
|
|
WHEN session_reporting_rate_limits.bucket_start = EXCLUDED.bucket_start
|
|
|
|
|
THEN session_reporting_rate_limits.request_count + 1
|
|
|
|
|
ELSE 1
|
|
|
|
|
END,
|
|
|
|
|
bucket_start = EXCLUDED.bucket_start
|
|
|
|
|
RETURNING request_count
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
type TakeReportingRateLimitParams struct {
|
|
|
|
|
UserID string
|
|
|
|
|
Kind string
|
|
|
|
|
BucketStart time.Time
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (q *Queries) TakeReportingRateLimit(ctx context.Context, arg TakeReportingRateLimitParams) (int32, error) {
|
|
|
|
|
row := q.db.QueryRow(ctx, takeReportingRateLimit, arg.UserID, arg.Kind, arg.BucketStart)
|
|
|
|
|
var request_count int32
|
|
|
|
|
err := row.Scan(&request_count)
|
|
|
|
|
return request_count, err
|
|
|
|
|
}
|
2026-10-19 17:32:21 +08:00
|
|
|
|
|
|
|
|
const verifySessionHost = `-- name: VerifySessionHost :exec
|
|
|
|
|
UPDATE sessions SET host_verified = true, host_revision = host_revision + 1
|
|
|
|
|
WHERE id = $1::uuid AND host_verified = false
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) VerifySessionHost(ctx context.Context, dollar_1 string) error {
|
|
|
|
|
_, err := q.db.Exec(ctx, verifySessionHost, dollar_1)
|
|
|
|
|
return err
|
|
|
|
|
}
|