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.

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

package store

import (
	"context"
)

const addAgentUsageTotals = `-- name: AddAgentUsageTotals :exec
INSERT INTO agent_usage_totals(provider,model,input_tokens,cache_read_tokens,cache_write_tokens,output_tokens,reasoning_tokens,compaction_count)
VALUES ($1,$2,$3,$4,$5,$6,$7,$8)
ON CONFLICT (provider,model) DO UPDATE SET
 input_tokens=agent_usage_totals.input_tokens+EXCLUDED.input_tokens,
 cache_read_tokens=agent_usage_totals.cache_read_tokens+EXCLUDED.cache_read_tokens,
 cache_write_tokens=agent_usage_totals.cache_write_tokens+EXCLUDED.cache_write_tokens,
 output_tokens=agent_usage_totals.output_tokens+EXCLUDED.output_tokens,
 reasoning_tokens=agent_usage_totals.reasoning_tokens+EXCLUDED.reasoning_tokens,
 compaction_count=agent_usage_totals.compaction_count+EXCLUDED.compaction_count
`

type AddAgentUsageTotalsParams struct {
	Provider         string `json:"provider"`
	Model            string `json:"model"`
	InputTokens      int64  `json:"input_tokens"`
	CacheReadTokens  int64  `json:"cache_read_tokens"`
	CacheWriteTokens int64  `json:"cache_write_tokens"`
	OutputTokens     int64  `json:"output_tokens"`
	ReasoningTokens  int64  `json:"reasoning_tokens"`
	CompactionCount  int64  `json:"compaction_count"`
}

func (q *Queries) AddAgentUsageTotals(ctx context.Context, arg AddAgentUsageTotalsParams) error {
	_, err := q.db.Exec(ctx, addAgentUsageTotals,
		arg.Provider,
		arg.Model,
		arg.InputTokens,
		arg.CacheReadTokens,
		arg.CacheWriteTokens,
		arg.OutputTokens,
		arg.ReasoningTokens,
		arg.CompactionCount,
	)
	return err
}

const recordAgentLocalRunOperation = `-- name: RecordAgentLocalRunOperation :exec
UPDATE agent_local_runs
SET usage=coalesce(usage,'{}'::jsonb)||jsonb_build_object($1::text,'[]'::jsonb),
    updated_at=clock_timestamp()
WHERE run_id=$2::text
  AND NOT coalesce(usage,'{}'::jsonb) ? $1::text
`

type RecordAgentLocalRunOperationParams struct {
	OperationID string `json:"operation_id"`
	RunID       string `json:"run_id"`
}

// A Run keeps one entry per Runner Operation, written when the Operation
// starts with no usage yet; Message activities resolve native records by it.
func (q *Queries) RecordAgentLocalRunOperation(ctx context.Context, arg RecordAgentLocalRunOperationParams) error {
	_, err := q.db.Exec(ctx, recordAgentLocalRunOperation, arg.OperationID, arg.RunID)
	return err
}

const recordAgentLocalRunTurn = `-- name: RecordAgentLocalRunTurn :exec
UPDATE agent_local_runs
SET turn_ids=array_append(turn_ids,$1::text),
    updated_at=clock_timestamp()
WHERE run_id=$2::text
  AND NOT $1::text=ANY(turn_ids)
  AND cardinality(turn_ids)<64
`

type RecordAgentLocalRunTurnParams struct {
	TurnID string `json:"turn_id"`
	RunID  string `json:"run_id"`
}

// A Run keeps each Runtime turn its Operations started once; a Run runs a
// handful of Operations, so the cap only bounds a misbehaving Runtime.
func (q *Queries) RecordAgentLocalRunTurn(ctx context.Context, arg RecordAgentLocalRunTurnParams) error {
	_, err := q.db.Exec(ctx, recordAgentLocalRunTurn, arg.TurnID, arg.RunID)
	return err
}

const recordAgentLocalRunUsage = `-- name: RecordAgentLocalRunUsage :execrows
UPDATE agent_local_runs
SET usage=coalesce(usage,'{}'::jsonb)||jsonb_build_object($1::text,$2::jsonb),
    updated_at=clock_timestamp()
WHERE run_id=$3::text
  AND coalesce(usage->$1::text,'[]'::jsonb)='[]'::jsonb
`

type RecordAgentLocalRunUsageParams struct {
	OperationID string `json:"operation_id"`
	Usage       []byte `json:"usage"`
	RunID       string `json:"run_id"`
}

// One Runner Operation's usage lands on its Run once; a repeated terminal of
// the same Operation changes nothing.
func (q *Queries) RecordAgentLocalRunUsage(ctx context.Context, arg RecordAgentLocalRunUsageParams) (int64, error) {
	result, err := q.db.Exec(ctx, recordAgentLocalRunUsage, arg.OperationID, arg.Usage, arg.RunID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}