You've already forked agentic-coding-workflow
bugfix
This commit is contained in:
@@ -30,7 +30,7 @@ func (q *Queries) DeleteAgentKindConfigFile(ctx context.Context, arg DeleteAgent
|
||||
}
|
||||
|
||||
const listAgentKindConfigFiles = `-- name: ListAgentKindConfigFiles :many
|
||||
SELECT id, agent_kind_id, rel_path, encrypted_content, updated_by, created_at, updated_at
|
||||
SELECT id, agent_kind_id, rel_path, encrypted_content, updated_by, created_at, updated_at, key_version
|
||||
FROM acp_agent_kind_config_files
|
||||
WHERE agent_kind_id = $1
|
||||
ORDER BY rel_path ASC
|
||||
@@ -53,6 +53,7 @@ func (q *Queries) ListAgentKindConfigFiles(ctx context.Context, agentKindID pgty
|
||||
&i.UpdatedBy,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.KeyVersion,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -72,7 +73,7 @@ ON CONFLICT (agent_kind_id, rel_path) DO UPDATE
|
||||
SET encrypted_content = EXCLUDED.encrypted_content,
|
||||
updated_by = EXCLUDED.updated_by,
|
||||
updated_at = now()
|
||||
RETURNING id, agent_kind_id, rel_path, encrypted_content, updated_by, created_at, updated_at
|
||||
RETURNING id, agent_kind_id, rel_path, encrypted_content, updated_by, created_at, updated_at, key_version
|
||||
`
|
||||
|
||||
type UpsertAgentKindConfigFileParams struct {
|
||||
@@ -100,6 +101,7 @@ func (q *Queries) UpsertAgentKindConfigFile(ctx context.Context, arg UpsertAgent
|
||||
&i.UpdatedBy,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.KeyVersion,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
@@ -24,25 +24,31 @@ func (q *Queries) CountAgentKindUsage(ctx context.Context, agentKindID pgtype.UU
|
||||
|
||||
const createAgentKind = `-- name: CreateAgentKind :one
|
||||
INSERT INTO acp_agent_kinds (
|
||||
id, name, display_name, description, binary_path, args, encrypted_env, enabled, created_by, tool_allowlist, client_type, encrypted_mcp_servers
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
|
||||
id, name, display_name, description, binary_path, args, encrypted_env, enabled, created_by, tool_allowlist, client_type, encrypted_mcp_servers,
|
||||
model_id, max_cost_usd, max_tokens, max_wall_clock_seconds
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)
|
||||
RETURNING id, name, display_name, description, binary_path, args, encrypted_env,
|
||||
enabled, created_by, created_at, updated_at, tool_allowlist, client_type, encrypted_mcp_servers
|
||||
enabled, created_by, created_at, updated_at, tool_allowlist, client_type, encrypted_mcp_servers,
|
||||
model_id, max_cost_usd, max_tokens, max_wall_clock_seconds, key_version
|
||||
`
|
||||
|
||||
type CreateAgentKindParams struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Description string `json:"description"`
|
||||
BinaryPath string `json:"binary_path"`
|
||||
Args []string `json:"args"`
|
||||
EncryptedEnv []byte `json:"encrypted_env"`
|
||||
Enabled bool `json:"enabled"`
|
||||
CreatedBy pgtype.UUID `json:"created_by"`
|
||||
ToolAllowlist []string `json:"tool_allowlist"`
|
||||
ClientType string `json:"client_type"`
|
||||
EncryptedMcpServers []byte `json:"encrypted_mcp_servers"`
|
||||
ID pgtype.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Description string `json:"description"`
|
||||
BinaryPath string `json:"binary_path"`
|
||||
Args []string `json:"args"`
|
||||
EncryptedEnv []byte `json:"encrypted_env"`
|
||||
Enabled bool `json:"enabled"`
|
||||
CreatedBy pgtype.UUID `json:"created_by"`
|
||||
ToolAllowlist []string `json:"tool_allowlist"`
|
||||
ClientType string `json:"client_type"`
|
||||
EncryptedMcpServers []byte `json:"encrypted_mcp_servers"`
|
||||
ModelID pgtype.UUID `json:"model_id"`
|
||||
MaxCostUsd pgtype.Numeric `json:"max_cost_usd"`
|
||||
MaxTokens *int64 `json:"max_tokens"`
|
||||
MaxWallClockSeconds *int32 `json:"max_wall_clock_seconds"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateAgentKind(ctx context.Context, arg CreateAgentKindParams) (AcpAgentKind, error) {
|
||||
@@ -59,6 +65,10 @@ func (q *Queries) CreateAgentKind(ctx context.Context, arg CreateAgentKindParams
|
||||
arg.ToolAllowlist,
|
||||
arg.ClientType,
|
||||
arg.EncryptedMcpServers,
|
||||
arg.ModelID,
|
||||
arg.MaxCostUsd,
|
||||
arg.MaxTokens,
|
||||
arg.MaxWallClockSeconds,
|
||||
)
|
||||
var i AcpAgentKind
|
||||
err := row.Scan(
|
||||
@@ -76,6 +86,11 @@ func (q *Queries) CreateAgentKind(ctx context.Context, arg CreateAgentKindParams
|
||||
&i.ToolAllowlist,
|
||||
&i.ClientType,
|
||||
&i.EncryptedMcpServers,
|
||||
&i.ModelID,
|
||||
&i.MaxCostUsd,
|
||||
&i.MaxTokens,
|
||||
&i.MaxWallClockSeconds,
|
||||
&i.KeyVersion,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -91,7 +106,8 @@ func (q *Queries) DeleteAgentKind(ctx context.Context, id pgtype.UUID) error {
|
||||
|
||||
const getAgentKindByID = `-- name: GetAgentKindByID :one
|
||||
SELECT id, name, display_name, description, binary_path, args, encrypted_env,
|
||||
enabled, created_by, created_at, updated_at, tool_allowlist, client_type, encrypted_mcp_servers
|
||||
enabled, created_by, created_at, updated_at, tool_allowlist, client_type, encrypted_mcp_servers,
|
||||
model_id, max_cost_usd, max_tokens, max_wall_clock_seconds, key_version
|
||||
FROM acp_agent_kinds
|
||||
WHERE id = $1
|
||||
`
|
||||
@@ -114,13 +130,19 @@ func (q *Queries) GetAgentKindByID(ctx context.Context, id pgtype.UUID) (AcpAgen
|
||||
&i.ToolAllowlist,
|
||||
&i.ClientType,
|
||||
&i.EncryptedMcpServers,
|
||||
&i.ModelID,
|
||||
&i.MaxCostUsd,
|
||||
&i.MaxTokens,
|
||||
&i.MaxWallClockSeconds,
|
||||
&i.KeyVersion,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getAgentKindByName = `-- name: GetAgentKindByName :one
|
||||
SELECT id, name, display_name, description, binary_path, args, encrypted_env,
|
||||
enabled, created_by, created_at, updated_at, tool_allowlist, client_type, encrypted_mcp_servers
|
||||
enabled, created_by, created_at, updated_at, tool_allowlist, client_type, encrypted_mcp_servers,
|
||||
model_id, max_cost_usd, max_tokens, max_wall_clock_seconds, key_version
|
||||
FROM acp_agent_kinds
|
||||
WHERE name = $1
|
||||
`
|
||||
@@ -143,13 +165,19 @@ func (q *Queries) GetAgentKindByName(ctx context.Context, name string) (AcpAgent
|
||||
&i.ToolAllowlist,
|
||||
&i.ClientType,
|
||||
&i.EncryptedMcpServers,
|
||||
&i.ModelID,
|
||||
&i.MaxCostUsd,
|
||||
&i.MaxTokens,
|
||||
&i.MaxWallClockSeconds,
|
||||
&i.KeyVersion,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listAgentKinds = `-- name: ListAgentKinds :many
|
||||
SELECT id, name, display_name, description, binary_path, args, encrypted_env,
|
||||
enabled, created_by, created_at, updated_at, tool_allowlist, client_type, encrypted_mcp_servers
|
||||
enabled, created_by, created_at, updated_at, tool_allowlist, client_type, encrypted_mcp_servers,
|
||||
model_id, max_cost_usd, max_tokens, max_wall_clock_seconds, key_version
|
||||
FROM acp_agent_kinds
|
||||
ORDER BY created_at DESC
|
||||
`
|
||||
@@ -178,6 +206,11 @@ func (q *Queries) ListAgentKinds(ctx context.Context) ([]AcpAgentKind, error) {
|
||||
&i.ToolAllowlist,
|
||||
&i.ClientType,
|
||||
&i.EncryptedMcpServers,
|
||||
&i.ModelID,
|
||||
&i.MaxCostUsd,
|
||||
&i.MaxTokens,
|
||||
&i.MaxWallClockSeconds,
|
||||
&i.KeyVersion,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -191,7 +224,8 @@ func (q *Queries) ListAgentKinds(ctx context.Context) ([]AcpAgentKind, error) {
|
||||
|
||||
const listEnabledAgentKinds = `-- name: ListEnabledAgentKinds :many
|
||||
SELECT id, name, display_name, description, binary_path, args, encrypted_env,
|
||||
enabled, created_by, created_at, updated_at, tool_allowlist, client_type, encrypted_mcp_servers
|
||||
enabled, created_by, created_at, updated_at, tool_allowlist, client_type, encrypted_mcp_servers,
|
||||
model_id, max_cost_usd, max_tokens, max_wall_clock_seconds, key_version
|
||||
FROM acp_agent_kinds
|
||||
WHERE enabled = TRUE
|
||||
ORDER BY name ASC
|
||||
@@ -221,6 +255,11 @@ func (q *Queries) ListEnabledAgentKinds(ctx context.Context) ([]AcpAgentKind, er
|
||||
&i.ToolAllowlist,
|
||||
&i.ClientType,
|
||||
&i.EncryptedMcpServers,
|
||||
&i.ModelID,
|
||||
&i.MaxCostUsd,
|
||||
&i.MaxTokens,
|
||||
&i.MaxWallClockSeconds,
|
||||
&i.KeyVersion,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -234,32 +273,41 @@ func (q *Queries) ListEnabledAgentKinds(ctx context.Context) ([]AcpAgentKind, er
|
||||
|
||||
const updateAgentKind = `-- name: UpdateAgentKind :one
|
||||
UPDATE acp_agent_kinds
|
||||
SET display_name = $2,
|
||||
description = $3,
|
||||
binary_path = $4,
|
||||
args = $5,
|
||||
encrypted_env = $6,
|
||||
enabled = $7,
|
||||
tool_allowlist = $8,
|
||||
client_type = $9,
|
||||
encrypted_mcp_servers = $10,
|
||||
updated_at = now()
|
||||
SET display_name = $2,
|
||||
description = $3,
|
||||
binary_path = $4,
|
||||
args = $5,
|
||||
encrypted_env = $6,
|
||||
enabled = $7,
|
||||
tool_allowlist = $8,
|
||||
client_type = $9,
|
||||
encrypted_mcp_servers = $10,
|
||||
model_id = $11,
|
||||
max_cost_usd = $12,
|
||||
max_tokens = $13,
|
||||
max_wall_clock_seconds = $14,
|
||||
updated_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING id, name, display_name, description, binary_path, args, encrypted_env,
|
||||
enabled, created_by, created_at, updated_at, tool_allowlist, client_type, encrypted_mcp_servers
|
||||
enabled, created_by, created_at, updated_at, tool_allowlist, client_type, encrypted_mcp_servers,
|
||||
model_id, max_cost_usd, max_tokens, max_wall_clock_seconds, key_version
|
||||
`
|
||||
|
||||
type UpdateAgentKindParams struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Description string `json:"description"`
|
||||
BinaryPath string `json:"binary_path"`
|
||||
Args []string `json:"args"`
|
||||
EncryptedEnv []byte `json:"encrypted_env"`
|
||||
Enabled bool `json:"enabled"`
|
||||
ToolAllowlist []string `json:"tool_allowlist"`
|
||||
ClientType string `json:"client_type"`
|
||||
EncryptedMcpServers []byte `json:"encrypted_mcp_servers"`
|
||||
ID pgtype.UUID `json:"id"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Description string `json:"description"`
|
||||
BinaryPath string `json:"binary_path"`
|
||||
Args []string `json:"args"`
|
||||
EncryptedEnv []byte `json:"encrypted_env"`
|
||||
Enabled bool `json:"enabled"`
|
||||
ToolAllowlist []string `json:"tool_allowlist"`
|
||||
ClientType string `json:"client_type"`
|
||||
EncryptedMcpServers []byte `json:"encrypted_mcp_servers"`
|
||||
ModelID pgtype.UUID `json:"model_id"`
|
||||
MaxCostUsd pgtype.Numeric `json:"max_cost_usd"`
|
||||
MaxTokens *int64 `json:"max_tokens"`
|
||||
MaxWallClockSeconds *int32 `json:"max_wall_clock_seconds"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateAgentKind(ctx context.Context, arg UpdateAgentKindParams) (AcpAgentKind, error) {
|
||||
@@ -274,6 +322,10 @@ func (q *Queries) UpdateAgentKind(ctx context.Context, arg UpdateAgentKindParams
|
||||
arg.ToolAllowlist,
|
||||
arg.ClientType,
|
||||
arg.EncryptedMcpServers,
|
||||
arg.ModelID,
|
||||
arg.MaxCostUsd,
|
||||
arg.MaxTokens,
|
||||
arg.MaxWallClockSeconds,
|
||||
)
|
||||
var i AcpAgentKind
|
||||
err := row.Scan(
|
||||
@@ -291,6 +343,11 @@ func (q *Queries) UpdateAgentKind(ctx context.Context, arg UpdateAgentKindParams
|
||||
&i.ToolAllowlist,
|
||||
&i.ClientType,
|
||||
&i.EncryptedMcpServers,
|
||||
&i.ModelID,
|
||||
&i.MaxCostUsd,
|
||||
&i.MaxTokens,
|
||||
&i.MaxWallClockSeconds,
|
||||
&i.KeyVersion,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,201 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: dashboard.sql
|
||||
|
||||
package acpsqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const sessionRollup = `-- name: SessionRollup :one
|
||||
SELECT
|
||||
COUNT(*)::bigint AS total,
|
||||
COUNT(*) FILTER (WHERE status = 'exited')::bigint AS succeeded,
|
||||
COUNT(*) FILTER (WHERE status = 'crashed')::bigint AS crashed,
|
||||
COUNT(*) FILTER (WHERE status IN ('starting','running'))::bigint AS active,
|
||||
COALESCE(SUM(cost_usd), 0)::numeric AS total_cost_usd,
|
||||
COALESCE(SUM(tokens_total), 0)::bigint AS total_tokens,
|
||||
COALESCE(
|
||||
AVG(EXTRACT(EPOCH FROM (ended_at - started_at)))
|
||||
FILTER (WHERE ended_at IS NOT NULL),
|
||||
0)::float8 AS avg_duration_seconds
|
||||
FROM acp_sessions
|
||||
WHERE ($1::uuid IS NULL OR user_id = $1)
|
||||
AND ($2::uuid IS NULL OR project_id = $2)
|
||||
AND ($3::uuid IS NULL OR requirement_id = $3)
|
||||
AND started_at >= $4
|
||||
AND started_at < $5
|
||||
`
|
||||
|
||||
type SessionRollupParams struct {
|
||||
Column1 pgtype.UUID `json:"column_1"`
|
||||
Column2 pgtype.UUID `json:"column_2"`
|
||||
Column3 pgtype.UUID `json:"column_3"`
|
||||
StartedAt pgtype.Timestamptz `json:"started_at"`
|
||||
StartedAt_2 pgtype.Timestamptz `json:"started_at_2"`
|
||||
}
|
||||
|
||||
type SessionRollupRow struct {
|
||||
Total int64 `json:"total"`
|
||||
Succeeded int64 `json:"succeeded"`
|
||||
Crashed int64 `json:"crashed"`
|
||||
Active int64 `json:"active"`
|
||||
TotalCostUsd pgtype.Numeric `json:"total_cost_usd"`
|
||||
TotalTokens int64 `json:"total_tokens"`
|
||||
AvgDurationSeconds float64 `json:"avg_duration_seconds"`
|
||||
}
|
||||
|
||||
// owner/admin scope + 可选 project/requirement + 时间窗内的会话汇总。
|
||||
// success = status='exited';avg_duration_seconds 仅统计已结束(ended_at 非空)会话。
|
||||
func (q *Queries) SessionRollup(ctx context.Context, arg SessionRollupParams) (SessionRollupRow, error) {
|
||||
row := q.db.QueryRow(ctx, sessionRollup,
|
||||
arg.Column1,
|
||||
arg.Column2,
|
||||
arg.Column3,
|
||||
arg.StartedAt,
|
||||
arg.StartedAt_2,
|
||||
)
|
||||
var i SessionRollupRow
|
||||
err := row.Scan(
|
||||
&i.Total,
|
||||
&i.Succeeded,
|
||||
&i.Crashed,
|
||||
&i.Active,
|
||||
&i.TotalCostUsd,
|
||||
&i.TotalTokens,
|
||||
&i.AvgDurationSeconds,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const sessionTimeline = `-- name: SessionTimeline :many
|
||||
|
||||
SELECT direction,
|
||||
rpc_kind,
|
||||
COALESCE(method, '') AS method,
|
||||
COUNT(*)::bigint AS event_count,
|
||||
MIN(created_at)::timestamptz AS first_at,
|
||||
MAX(created_at)::timestamptz AS last_at
|
||||
FROM acp_events
|
||||
WHERE session_id = $1
|
||||
GROUP BY direction, rpc_kind, COALESCE(method, '')
|
||||
ORDER BY first_at ASC
|
||||
`
|
||||
|
||||
type SessionTimelineRow struct {
|
||||
Direction string `json:"direction"`
|
||||
RpcKind string `json:"rpc_kind"`
|
||||
Method string `json:"method"`
|
||||
EventCount int64 `json:"event_count"`
|
||||
FirstAt pgtype.Timestamptz `json:"first_at"`
|
||||
LastAt pgtype.Timestamptz `json:"last_at"`
|
||||
}
|
||||
|
||||
// dashboard.sql: run-history dashboard 读模型(只读聚合)。
|
||||
// - SessionTimeline: 单会话的 RPC 事件按 (direction, rpc_kind, method) 分桶,
|
||||
// 给出条数 + 首/末时间戳,用于会话时间线概览。
|
||||
// - SessionRollup: 在 owner/admin scope + 可选 project/requirement 过滤下,
|
||||
// 统计会话总数、成功率(exited 视为成功)、总成本、平均时长。
|
||||
// - SessionsByDay: 按天的 总数 / 成功 / 失败 / 成本 时间序列。
|
||||
//
|
||||
// scope 约定(与 ListSessionsByUser/ListAllSessions 一致):
|
||||
//
|
||||
// $1 user_id 为 NULL 时不按用户过滤(admin 全量),非 NULL 时仅该用户。
|
||||
//
|
||||
// 单会话事件时间线:按 method/kind 分桶聚合(条数 + 首末时间)。
|
||||
func (q *Queries) SessionTimeline(ctx context.Context, sessionID pgtype.UUID) ([]SessionTimelineRow, error) {
|
||||
rows, err := q.db.Query(ctx, sessionTimeline, sessionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []SessionTimelineRow
|
||||
for rows.Next() {
|
||||
var i SessionTimelineRow
|
||||
if err := rows.Scan(
|
||||
&i.Direction,
|
||||
&i.RpcKind,
|
||||
&i.Method,
|
||||
&i.EventCount,
|
||||
&i.FirstAt,
|
||||
&i.LastAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const sessionsByDay = `-- name: SessionsByDay :many
|
||||
SELECT
|
||||
date_trunc('day', started_at)::timestamptz AS day,
|
||||
COUNT(*)::bigint AS total,
|
||||
COUNT(*) FILTER (WHERE status = 'exited')::bigint AS succeeded,
|
||||
COUNT(*) FILTER (WHERE status = 'crashed')::bigint AS crashed,
|
||||
COALESCE(SUM(cost_usd), 0)::numeric AS total_cost_usd
|
||||
FROM acp_sessions
|
||||
WHERE ($1::uuid IS NULL OR user_id = $1)
|
||||
AND ($2::uuid IS NULL OR project_id = $2)
|
||||
AND ($3::uuid IS NULL OR requirement_id = $3)
|
||||
AND started_at >= $4
|
||||
AND started_at < $5
|
||||
GROUP BY day
|
||||
ORDER BY day ASC
|
||||
`
|
||||
|
||||
type SessionsByDayParams struct {
|
||||
Column1 pgtype.UUID `json:"column_1"`
|
||||
Column2 pgtype.UUID `json:"column_2"`
|
||||
Column3 pgtype.UUID `json:"column_3"`
|
||||
StartedAt pgtype.Timestamptz `json:"started_at"`
|
||||
StartedAt_2 pgtype.Timestamptz `json:"started_at_2"`
|
||||
}
|
||||
|
||||
type SessionsByDayRow struct {
|
||||
Day pgtype.Timestamptz `json:"day"`
|
||||
Total int64 `json:"total"`
|
||||
Succeeded int64 `json:"succeeded"`
|
||||
Crashed int64 `json:"crashed"`
|
||||
TotalCostUsd pgtype.Numeric `json:"total_cost_usd"`
|
||||
}
|
||||
|
||||
// 按天的会话时间序列(总数 / 成功 / 失败 / 成本),同 SessionRollup 的 scope。
|
||||
func (q *Queries) SessionsByDay(ctx context.Context, arg SessionsByDayParams) ([]SessionsByDayRow, error) {
|
||||
rows, err := q.db.Query(ctx, sessionsByDay,
|
||||
arg.Column1,
|
||||
arg.Column2,
|
||||
arg.Column3,
|
||||
arg.StartedAt,
|
||||
arg.StartedAt_2,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []SessionsByDayRow
|
||||
for rows.Next() {
|
||||
var i SessionsByDayRow
|
||||
if err := rows.Scan(
|
||||
&i.Day,
|
||||
&i.Total,
|
||||
&i.Succeeded,
|
||||
&i.Crashed,
|
||||
&i.TotalCostUsd,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
+240
-17
@@ -8,6 +8,7 @@ import (
|
||||
"net/netip"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/pgvector/pgvector-go"
|
||||
)
|
||||
|
||||
type AcpAgentKind struct {
|
||||
@@ -25,6 +26,11 @@ type AcpAgentKind struct {
|
||||
ToolAllowlist []string `json:"tool_allowlist"`
|
||||
ClientType string `json:"client_type"`
|
||||
EncryptedMcpServers []byte `json:"encrypted_mcp_servers"`
|
||||
ModelID pgtype.UUID `json:"model_id"`
|
||||
MaxCostUsd pgtype.Numeric `json:"max_cost_usd"`
|
||||
MaxTokens *int64 `json:"max_tokens"`
|
||||
MaxWallClockSeconds *int32 `json:"max_wall_clock_seconds"`
|
||||
KeyVersion int16 `json:"key_version"`
|
||||
}
|
||||
|
||||
type AcpAgentKindConfigFile struct {
|
||||
@@ -35,6 +41,7 @@ type AcpAgentKindConfigFile struct {
|
||||
UpdatedBy pgtype.UUID `json:"updated_by"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
KeyVersion int16 `json:"key_version"`
|
||||
}
|
||||
|
||||
type AcpEvent struct {
|
||||
@@ -64,23 +71,64 @@ type AcpPermissionRequest struct {
|
||||
}
|
||||
|
||||
type AcpSession struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
ProjectID pgtype.UUID `json:"project_id"`
|
||||
AgentKindID pgtype.UUID `json:"agent_kind_id"`
|
||||
UserID pgtype.UUID `json:"user_id"`
|
||||
IssueID pgtype.UUID `json:"issue_id"`
|
||||
RequirementID pgtype.UUID `json:"requirement_id"`
|
||||
AgentSessionID *string `json:"agent_session_id"`
|
||||
Branch string `json:"branch"`
|
||||
CwdPath string `json:"cwd_path"`
|
||||
IsMainWorktree bool `json:"is_main_worktree"`
|
||||
Status string `json:"status"`
|
||||
Pid *int32 `json:"pid"`
|
||||
ExitCode *int32 `json:"exit_code"`
|
||||
LastError *string `json:"last_error"`
|
||||
StartedAt pgtype.Timestamptz `json:"started_at"`
|
||||
EndedAt pgtype.Timestamptz `json:"ended_at"`
|
||||
ID pgtype.UUID `json:"id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
ProjectID pgtype.UUID `json:"project_id"`
|
||||
AgentKindID pgtype.UUID `json:"agent_kind_id"`
|
||||
UserID pgtype.UUID `json:"user_id"`
|
||||
IssueID pgtype.UUID `json:"issue_id"`
|
||||
RequirementID pgtype.UUID `json:"requirement_id"`
|
||||
AgentSessionID *string `json:"agent_session_id"`
|
||||
Branch string `json:"branch"`
|
||||
CwdPath string `json:"cwd_path"`
|
||||
IsMainWorktree bool `json:"is_main_worktree"`
|
||||
Status string `json:"status"`
|
||||
Pid *int32 `json:"pid"`
|
||||
ExitCode *int32 `json:"exit_code"`
|
||||
LastError *string `json:"last_error"`
|
||||
StartedAt pgtype.Timestamptz `json:"started_at"`
|
||||
EndedAt pgtype.Timestamptz `json:"ended_at"`
|
||||
LastStopReason *string `json:"last_stop_reason"`
|
||||
OrchestratorStepID pgtype.UUID `json:"orchestrator_step_id"`
|
||||
PromptTokens int64 `json:"prompt_tokens"`
|
||||
CompletionTokens int64 `json:"completion_tokens"`
|
||||
ThinkingTokens int64 `json:"thinking_tokens"`
|
||||
TotalCostUsd pgtype.Numeric `json:"total_cost_usd"`
|
||||
LastActivityAt pgtype.Timestamptz `json:"last_activity_at"`
|
||||
BudgetMaxCostUsd pgtype.Numeric `json:"budget_max_cost_usd"`
|
||||
BudgetMaxTokens *int64 `json:"budget_max_tokens"`
|
||||
BudgetMaxWallClockSeconds *int32 `json:"budget_max_wall_clock_seconds"`
|
||||
TerminatedReason *string `json:"terminated_reason"`
|
||||
SandboxMode *string `json:"sandbox_mode"`
|
||||
CostUsd pgtype.Numeric `json:"cost_usd"`
|
||||
TokensTotal int64 `json:"tokens_total"`
|
||||
}
|
||||
|
||||
type AcpSessionUsage struct {
|
||||
ID int64 `json:"id"`
|
||||
SessionID pgtype.UUID `json:"session_id"`
|
||||
UserID pgtype.UUID `json:"user_id"`
|
||||
ProjectID pgtype.UUID `json:"project_id"`
|
||||
AgentKindID pgtype.UUID `json:"agent_kind_id"`
|
||||
ModelID pgtype.UUID `json:"model_id"`
|
||||
PromptTokens int64 `json:"prompt_tokens"`
|
||||
CompletionTokens int64 `json:"completion_tokens"`
|
||||
ThinkingTokens int64 `json:"thinking_tokens"`
|
||||
CostUsd pgtype.Numeric `json:"cost_usd"`
|
||||
SourceEventID *int64 `json:"source_event_id"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
type AcpTurn struct {
|
||||
ID int64 `json:"id"`
|
||||
SessionID pgtype.UUID `json:"session_id"`
|
||||
TurnIndex int32 `json:"turn_index"`
|
||||
PromptRequestID *string `json:"prompt_request_id"`
|
||||
Status string `json:"status"`
|
||||
StopReason *string `json:"stop_reason"`
|
||||
UpdateCount int32 `json:"update_count"`
|
||||
StartedAt pgtype.Timestamptz `json:"started_at"`
|
||||
CompletedAt pgtype.Timestamptz `json:"completed_at"`
|
||||
}
|
||||
|
||||
type AuditLog struct {
|
||||
@@ -94,6 +142,81 @@ type AuditLog struct {
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
type ChangeRequest struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
ProjectID pgtype.UUID `json:"project_id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
RequirementID pgtype.UUID `json:"requirement_id"`
|
||||
IssueID pgtype.UUID `json:"issue_id"`
|
||||
Number int32 `json:"number"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
SourceBranch string `json:"source_branch"`
|
||||
TargetBranch string `json:"target_branch"`
|
||||
State string `json:"state"`
|
||||
ReviewVerdict string `json:"review_verdict"`
|
||||
CiState string `json:"ci_state"`
|
||||
Provider string `json:"provider"`
|
||||
ExternalID *int64 `json:"external_id"`
|
||||
ExternalUrl string `json:"external_url"`
|
||||
MergeCommitSha string `json:"merge_commit_sha"`
|
||||
ReviewedBy pgtype.UUID `json:"reviewed_by"`
|
||||
ReviewedAt pgtype.Timestamptz `json:"reviewed_at"`
|
||||
CreatedBy pgtype.UUID `json:"created_by"`
|
||||
MergedAt pgtype.Timestamptz `json:"merged_at"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
}
|
||||
|
||||
type CodeChunk struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
RunID pgtype.UUID `json:"run_id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
CommitSha string `json:"commit_sha"`
|
||||
FilePath string `json:"file_path"`
|
||||
Lang *string `json:"lang"`
|
||||
StartLine int32 `json:"start_line"`
|
||||
EndLine int32 `json:"end_line"`
|
||||
Content string `json:"content"`
|
||||
ContentSha []byte `json:"content_sha"`
|
||||
TokenCount *int32 `json:"token_count"`
|
||||
Embedding *pgvector.Vector `json:"embedding"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
type CodeIndexRun struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
WorktreeID pgtype.UUID `json:"worktree_id"`
|
||||
Branch string `json:"branch"`
|
||||
CommitSha string `json:"commit_sha"`
|
||||
Status string `json:"status"`
|
||||
EmbeddingEndpointID pgtype.UUID `json:"embedding_endpoint_id"`
|
||||
EmbeddingModel string `json:"embedding_model"`
|
||||
Dims int32 `json:"dims"`
|
||||
FilesIndexed int32 `json:"files_indexed"`
|
||||
ChunksIndexed int32 `json:"chunks_indexed"`
|
||||
Error *string `json:"error"`
|
||||
StartedAt pgtype.Timestamptz `json:"started_at"`
|
||||
FinishedAt pgtype.Timestamptz `json:"finished_at"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
type CommitSummary struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
WorktreeID pgtype.UUID `json:"worktree_id"`
|
||||
Branch string `json:"branch"`
|
||||
CommitSha string `json:"commit_sha"`
|
||||
Kind string `json:"kind"`
|
||||
Title *string `json:"title"`
|
||||
BodyMd string `json:"body_md"`
|
||||
Model *string `json:"model"`
|
||||
Status string `json:"status"`
|
||||
Error *string `json:"error"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
type Conversation struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
UserID pgtype.UUID `json:"user_id"`
|
||||
@@ -110,6 +233,14 @@ type Conversation struct {
|
||||
RequirementID pgtype.UUID `json:"requirement_id"`
|
||||
}
|
||||
|
||||
type CryptoKey struct {
|
||||
Version int32 `json:"version"`
|
||||
Provider string `json:"provider"`
|
||||
KeyRef *string `json:"key_ref"`
|
||||
Status string `json:"status"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
type GitCredential struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
@@ -119,6 +250,7 @@ type GitCredential struct {
|
||||
Fingerprint *string `json:"fingerprint"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
KeyVersion int16 `json:"key_version"`
|
||||
}
|
||||
|
||||
type Issue struct {
|
||||
@@ -135,6 +267,17 @@ type Issue struct {
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
ParentID pgtype.UUID `json:"parent_id"`
|
||||
Priority int32 `json:"priority"`
|
||||
}
|
||||
|
||||
type IssueDependency struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
ProjectID pgtype.UUID `json:"project_id"`
|
||||
BlockedID pgtype.UUID `json:"blocked_id"`
|
||||
BlockerID pgtype.UUID `json:"blocker_id"`
|
||||
CreatedBy pgtype.UUID `json:"created_by"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
type Job struct {
|
||||
@@ -162,6 +305,7 @@ type LlmEndpoint struct {
|
||||
CreatedBy pgtype.UUID `json:"created_by"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
KeyVersion int16 `json:"key_version"`
|
||||
}
|
||||
|
||||
type LlmModel struct {
|
||||
@@ -252,6 +396,50 @@ type Notification struct {
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
type NotificationPref struct {
|
||||
UserID pgtype.UUID `json:"user_id"`
|
||||
EmailEnabled bool `json:"email_enabled"`
|
||||
ImEnabled bool `json:"im_enabled"`
|
||||
ImWebhookUrlEnc []byte `json:"im_webhook_url_enc"`
|
||||
MinSeverity string `json:"min_severity"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
}
|
||||
|
||||
type OrchestratorRun struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
ProjectID pgtype.UUID `json:"project_id"`
|
||||
RequirementID pgtype.UUID `json:"requirement_id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
OwnerID pgtype.UUID `json:"owner_id"`
|
||||
Status string `json:"status"`
|
||||
CurrentPhase string `json:"current_phase"`
|
||||
Config []byte `json:"config"`
|
||||
LastError *string `json:"last_error"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
EndedAt pgtype.Timestamptz `json:"ended_at"`
|
||||
}
|
||||
|
||||
type OrchestratorStep struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
RunID pgtype.UUID `json:"run_id"`
|
||||
Phase string `json:"phase"`
|
||||
Attempt int32 `json:"attempt"`
|
||||
ParentStepID pgtype.UUID `json:"parent_step_id"`
|
||||
Depth int32 `json:"depth"`
|
||||
Status string `json:"status"`
|
||||
AgentKindID pgtype.UUID `json:"agent_kind_id"`
|
||||
AcpSessionID pgtype.UUID `json:"acp_session_id"`
|
||||
Prompt string `json:"prompt"`
|
||||
StopReason *string `json:"stop_reason"`
|
||||
GateResult []byte `json:"gate_result"`
|
||||
LastError *string `json:"last_error"`
|
||||
JobID pgtype.UUID `json:"job_id"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
EndedAt pgtype.Timestamptz `json:"ended_at"`
|
||||
}
|
||||
|
||||
type Project struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
Slug string `json:"slug"`
|
||||
@@ -264,6 +452,30 @@ type Project struct {
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
}
|
||||
|
||||
type ProjectMember struct {
|
||||
ProjectID pgtype.UUID `json:"project_id"`
|
||||
UserID pgtype.UUID `json:"user_id"`
|
||||
Role string `json:"role"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
AddedBy pgtype.UUID `json:"added_by"`
|
||||
}
|
||||
|
||||
type ProjectMemory struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
ProjectID pgtype.UUID `json:"project_id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
Kind string `json:"kind"`
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
Tags []string `json:"tags"`
|
||||
Source string `json:"source"`
|
||||
Embedding *pgvector.Vector `json:"embedding"`
|
||||
EmbeddingModel *string `json:"embedding_model"`
|
||||
CreatedBy pgtype.UUID `json:"created_by"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
}
|
||||
|
||||
type PromptTemplate struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
@@ -299,6 +511,16 @@ type RequirementArtifact struct {
|
||||
SourceMessageID *int64 `json:"source_message_id"`
|
||||
CreatedBy pgtype.UUID `json:"created_by"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
Verdict string `json:"verdict"`
|
||||
}
|
||||
|
||||
type RequirementPhasePointer struct {
|
||||
RequirementID pgtype.UUID `json:"requirement_id"`
|
||||
Phase string `json:"phase"`
|
||||
ApprovedArtifactID pgtype.UUID `json:"approved_artifact_id"`
|
||||
ApprovedBy pgtype.UUID `json:"approved_by"`
|
||||
ApprovedAt pgtype.Timestamptz `json:"approved_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
@@ -354,6 +576,7 @@ type WorkspaceRunProfile struct {
|
||||
CreatedBy pgtype.UUID `json:"created_by"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
KeyVersion int16 `json:"key_version"`
|
||||
}
|
||||
|
||||
type WorkspaceWorktree struct {
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: session_usage.sql
|
||||
|
||||
package acpsqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const insertSessionUsage = `-- name: InsertSessionUsage :one
|
||||
INSERT INTO acp_session_usage (
|
||||
session_id, user_id, project_id, agent_kind_id, model_id,
|
||||
prompt_tokens, completion_tokens, thinking_tokens, cost_usd, source_event_id
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
ON CONFLICT (source_event_id) WHERE source_event_id IS NOT NULL DO NOTHING
|
||||
RETURNING id
|
||||
`
|
||||
|
||||
type InsertSessionUsageParams struct {
|
||||
SessionID pgtype.UUID `json:"session_id"`
|
||||
UserID pgtype.UUID `json:"user_id"`
|
||||
ProjectID pgtype.UUID `json:"project_id"`
|
||||
AgentKindID pgtype.UUID `json:"agent_kind_id"`
|
||||
ModelID pgtype.UUID `json:"model_id"`
|
||||
PromptTokens int64 `json:"prompt_tokens"`
|
||||
CompletionTokens int64 `json:"completion_tokens"`
|
||||
ThinkingTokens int64 `json:"thinking_tokens"`
|
||||
CostUsd pgtype.Numeric `json:"cost_usd"`
|
||||
SourceEventID *int64 `json:"source_event_id"`
|
||||
}
|
||||
|
||||
// 写一条 per-turn 用量账目。source_event_id 非空时按唯一索引去重(同一事件
|
||||
// 重复 Observe 不会重复入账);返回受影响行数判定是否实际插入。
|
||||
func (q *Queries) InsertSessionUsage(ctx context.Context, arg InsertSessionUsageParams) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, insertSessionUsage,
|
||||
arg.SessionID,
|
||||
arg.UserID,
|
||||
arg.ProjectID,
|
||||
arg.AgentKindID,
|
||||
arg.ModelID,
|
||||
arg.PromptTokens,
|
||||
arg.CompletionTokens,
|
||||
arg.ThinkingTokens,
|
||||
arg.CostUsd,
|
||||
arg.SourceEventID,
|
||||
)
|
||||
var id int64
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
@@ -29,6 +29,56 @@ func (q *Queries) AcquireMainWorktree(ctx context.Context, arg AcquireMainWorktr
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const addSessionUsageTotals = `-- name: AddSessionUsageTotals :one
|
||||
UPDATE acp_sessions
|
||||
SET prompt_tokens = prompt_tokens + $2,
|
||||
completion_tokens = completion_tokens + $3,
|
||||
thinking_tokens = thinking_tokens + $4,
|
||||
total_cost_usd = total_cost_usd + $5,
|
||||
cost_usd = cost_usd + $5,
|
||||
tokens_total = tokens_total + $2 + $3 + $4,
|
||||
last_activity_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING prompt_tokens, completion_tokens, thinking_tokens, total_cost_usd
|
||||
`
|
||||
|
||||
type AddSessionUsageTotalsParams struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
PromptTokens int64 `json:"prompt_tokens"`
|
||||
CompletionTokens int64 `json:"completion_tokens"`
|
||||
ThinkingTokens int64 `json:"thinking_tokens"`
|
||||
TotalCostUsd pgtype.Numeric `json:"total_cost_usd"`
|
||||
}
|
||||
|
||||
type AddSessionUsageTotalsRow struct {
|
||||
PromptTokens int64 `json:"prompt_tokens"`
|
||||
CompletionTokens int64 `json:"completion_tokens"`
|
||||
ThinkingTokens int64 `json:"thinking_tokens"`
|
||||
TotalCostUsd pgtype.Numeric `json:"total_cost_usd"`
|
||||
}
|
||||
|
||||
// 单 round-trip 累加 session 运行时 token/cost 总量并刷新 last_activity_at,
|
||||
// 返回累加后的权威总量供预算判定。
|
||||
// 同时把 dashboard rollup 列(cost_usd / tokens_total)与运行时累加器对齐,
|
||||
// 使 run-history dashboard 在会话进行中及退出后都能读到 agent 上报的成本。
|
||||
func (q *Queries) AddSessionUsageTotals(ctx context.Context, arg AddSessionUsageTotalsParams) (AddSessionUsageTotalsRow, error) {
|
||||
row := q.db.QueryRow(ctx, addSessionUsageTotals,
|
||||
arg.ID,
|
||||
arg.PromptTokens,
|
||||
arg.CompletionTokens,
|
||||
arg.ThinkingTokens,
|
||||
arg.TotalCostUsd,
|
||||
)
|
||||
var i AddSessionUsageTotalsRow
|
||||
err := row.Scan(
|
||||
&i.PromptTokens,
|
||||
&i.CompletionTokens,
|
||||
&i.ThinkingTokens,
|
||||
&i.TotalCostUsd,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const countActiveSessions = `-- name: CountActiveSessions :one
|
||||
SELECT COUNT(*) FROM acp_sessions WHERE status IN ('starting', 'running')
|
||||
`
|
||||
@@ -52,11 +102,73 @@ func (q *Queries) CountActiveSessionsByUser(ctx context.Context, userID pgtype.U
|
||||
return count, err
|
||||
}
|
||||
|
||||
const getCrashedSessionForResume = `-- name: GetCrashedSessionForResume :one
|
||||
SELECT id, workspace_id, project_id, agent_kind_id, user_id,
|
||||
issue_id, requirement_id, agent_session_id,
|
||||
branch, cwd_path, is_main_worktree, status,
|
||||
pid, exit_code, last_error, started_at, ended_at, last_stop_reason,
|
||||
orchestrator_step_id,
|
||||
prompt_tokens, completion_tokens, thinking_tokens, total_cost_usd,
|
||||
last_activity_at, budget_max_cost_usd, budget_max_tokens,
|
||||
budget_max_wall_clock_seconds, terminated_reason, sandbox_mode,
|
||||
cost_usd, tokens_total
|
||||
FROM acp_sessions
|
||||
WHERE orchestrator_step_id = $1
|
||||
AND status IN ('crashed','exited')
|
||||
ORDER BY started_at DESC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
// 返回某 step 最近一次处于可恢复终态(crashed/exited)的 session。
|
||||
func (q *Queries) GetCrashedSessionForResume(ctx context.Context, orchestratorStepID pgtype.UUID) (AcpSession, error) {
|
||||
row := q.db.QueryRow(ctx, getCrashedSessionForResume, orchestratorStepID)
|
||||
var i AcpSession
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.WorkspaceID,
|
||||
&i.ProjectID,
|
||||
&i.AgentKindID,
|
||||
&i.UserID,
|
||||
&i.IssueID,
|
||||
&i.RequirementID,
|
||||
&i.AgentSessionID,
|
||||
&i.Branch,
|
||||
&i.CwdPath,
|
||||
&i.IsMainWorktree,
|
||||
&i.Status,
|
||||
&i.Pid,
|
||||
&i.ExitCode,
|
||||
&i.LastError,
|
||||
&i.StartedAt,
|
||||
&i.EndedAt,
|
||||
&i.LastStopReason,
|
||||
&i.OrchestratorStepID,
|
||||
&i.PromptTokens,
|
||||
&i.CompletionTokens,
|
||||
&i.ThinkingTokens,
|
||||
&i.TotalCostUsd,
|
||||
&i.LastActivityAt,
|
||||
&i.BudgetMaxCostUsd,
|
||||
&i.BudgetMaxTokens,
|
||||
&i.BudgetMaxWallClockSeconds,
|
||||
&i.TerminatedReason,
|
||||
&i.SandboxMode,
|
||||
&i.CostUsd,
|
||||
&i.TokensTotal,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getSessionByID = `-- name: GetSessionByID :one
|
||||
SELECT id, workspace_id, project_id, agent_kind_id, user_id,
|
||||
issue_id, requirement_id, agent_session_id,
|
||||
branch, cwd_path, is_main_worktree, status,
|
||||
pid, exit_code, last_error, started_at, ended_at
|
||||
pid, exit_code, last_error, started_at, ended_at, last_stop_reason,
|
||||
orchestrator_step_id,
|
||||
prompt_tokens, completion_tokens, thinking_tokens, total_cost_usd,
|
||||
last_activity_at, budget_max_cost_usd, budget_max_tokens,
|
||||
budget_max_wall_clock_seconds, terminated_reason, sandbox_mode,
|
||||
cost_usd, tokens_total
|
||||
FROM acp_sessions
|
||||
WHERE id = $1
|
||||
`
|
||||
@@ -82,6 +194,75 @@ func (q *Queries) GetSessionByID(ctx context.Context, id pgtype.UUID) (AcpSessio
|
||||
&i.LastError,
|
||||
&i.StartedAt,
|
||||
&i.EndedAt,
|
||||
&i.LastStopReason,
|
||||
&i.OrchestratorStepID,
|
||||
&i.PromptTokens,
|
||||
&i.CompletionTokens,
|
||||
&i.ThinkingTokens,
|
||||
&i.TotalCostUsd,
|
||||
&i.LastActivityAt,
|
||||
&i.BudgetMaxCostUsd,
|
||||
&i.BudgetMaxTokens,
|
||||
&i.BudgetMaxWallClockSeconds,
|
||||
&i.TerminatedReason,
|
||||
&i.SandboxMode,
|
||||
&i.CostUsd,
|
||||
&i.TokensTotal,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getSessionByStepID = `-- name: GetSessionByStepID :one
|
||||
SELECT id, workspace_id, project_id, agent_kind_id, user_id,
|
||||
issue_id, requirement_id, agent_session_id,
|
||||
branch, cwd_path, is_main_worktree, status,
|
||||
pid, exit_code, last_error, started_at, ended_at, last_stop_reason,
|
||||
orchestrator_step_id,
|
||||
prompt_tokens, completion_tokens, thinking_tokens, total_cost_usd,
|
||||
last_activity_at, budget_max_cost_usd, budget_max_tokens,
|
||||
budget_max_wall_clock_seconds, terminated_reason, sandbox_mode,
|
||||
cost_usd, tokens_total
|
||||
FROM acp_sessions
|
||||
WHERE orchestrator_step_id = $1
|
||||
ORDER BY started_at DESC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetSessionByStepID(ctx context.Context, orchestratorStepID pgtype.UUID) (AcpSession, error) {
|
||||
row := q.db.QueryRow(ctx, getSessionByStepID, orchestratorStepID)
|
||||
var i AcpSession
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.WorkspaceID,
|
||||
&i.ProjectID,
|
||||
&i.AgentKindID,
|
||||
&i.UserID,
|
||||
&i.IssueID,
|
||||
&i.RequirementID,
|
||||
&i.AgentSessionID,
|
||||
&i.Branch,
|
||||
&i.CwdPath,
|
||||
&i.IsMainWorktree,
|
||||
&i.Status,
|
||||
&i.Pid,
|
||||
&i.ExitCode,
|
||||
&i.LastError,
|
||||
&i.StartedAt,
|
||||
&i.EndedAt,
|
||||
&i.LastStopReason,
|
||||
&i.OrchestratorStepID,
|
||||
&i.PromptTokens,
|
||||
&i.CompletionTokens,
|
||||
&i.ThinkingTokens,
|
||||
&i.TotalCostUsd,
|
||||
&i.LastActivityAt,
|
||||
&i.BudgetMaxCostUsd,
|
||||
&i.BudgetMaxTokens,
|
||||
&i.BudgetMaxWallClockSeconds,
|
||||
&i.TerminatedReason,
|
||||
&i.SandboxMode,
|
||||
&i.CostUsd,
|
||||
&i.TokensTotal,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -89,26 +270,37 @@ func (q *Queries) GetSessionByID(ctx context.Context, id pgtype.UUID) (AcpSessio
|
||||
const insertSession = `-- name: InsertSession :one
|
||||
INSERT INTO acp_sessions (
|
||||
id, workspace_id, project_id, agent_kind_id, user_id,
|
||||
issue_id, requirement_id, branch, cwd_path, is_main_worktree, status
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
||||
issue_id, requirement_id, branch, cwd_path, is_main_worktree, status,
|
||||
orchestrator_step_id,
|
||||
budget_max_cost_usd, budget_max_tokens, budget_max_wall_clock_seconds
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)
|
||||
RETURNING id, workspace_id, project_id, agent_kind_id, user_id,
|
||||
issue_id, requirement_id, agent_session_id,
|
||||
branch, cwd_path, is_main_worktree, status,
|
||||
pid, exit_code, last_error, started_at, ended_at
|
||||
pid, exit_code, last_error, started_at, ended_at, last_stop_reason,
|
||||
orchestrator_step_id,
|
||||
prompt_tokens, completion_tokens, thinking_tokens, total_cost_usd,
|
||||
last_activity_at, budget_max_cost_usd, budget_max_tokens,
|
||||
budget_max_wall_clock_seconds, terminated_reason, sandbox_mode,
|
||||
cost_usd, tokens_total
|
||||
`
|
||||
|
||||
type InsertSessionParams struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
ProjectID pgtype.UUID `json:"project_id"`
|
||||
AgentKindID pgtype.UUID `json:"agent_kind_id"`
|
||||
UserID pgtype.UUID `json:"user_id"`
|
||||
IssueID pgtype.UUID `json:"issue_id"`
|
||||
RequirementID pgtype.UUID `json:"requirement_id"`
|
||||
Branch string `json:"branch"`
|
||||
CwdPath string `json:"cwd_path"`
|
||||
IsMainWorktree bool `json:"is_main_worktree"`
|
||||
Status string `json:"status"`
|
||||
ID pgtype.UUID `json:"id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
ProjectID pgtype.UUID `json:"project_id"`
|
||||
AgentKindID pgtype.UUID `json:"agent_kind_id"`
|
||||
UserID pgtype.UUID `json:"user_id"`
|
||||
IssueID pgtype.UUID `json:"issue_id"`
|
||||
RequirementID pgtype.UUID `json:"requirement_id"`
|
||||
Branch string `json:"branch"`
|
||||
CwdPath string `json:"cwd_path"`
|
||||
IsMainWorktree bool `json:"is_main_worktree"`
|
||||
Status string `json:"status"`
|
||||
OrchestratorStepID pgtype.UUID `json:"orchestrator_step_id"`
|
||||
BudgetMaxCostUsd pgtype.Numeric `json:"budget_max_cost_usd"`
|
||||
BudgetMaxTokens *int64 `json:"budget_max_tokens"`
|
||||
BudgetMaxWallClockSeconds *int32 `json:"budget_max_wall_clock_seconds"`
|
||||
}
|
||||
|
||||
func (q *Queries) InsertSession(ctx context.Context, arg InsertSessionParams) (AcpSession, error) {
|
||||
@@ -124,6 +316,10 @@ func (q *Queries) InsertSession(ctx context.Context, arg InsertSessionParams) (A
|
||||
arg.CwdPath,
|
||||
arg.IsMainWorktree,
|
||||
arg.Status,
|
||||
arg.OrchestratorStepID,
|
||||
arg.BudgetMaxCostUsd,
|
||||
arg.BudgetMaxTokens,
|
||||
arg.BudgetMaxWallClockSeconds,
|
||||
)
|
||||
var i AcpSession
|
||||
err := row.Scan(
|
||||
@@ -144,6 +340,20 @@ func (q *Queries) InsertSession(ctx context.Context, arg InsertSessionParams) (A
|
||||
&i.LastError,
|
||||
&i.StartedAt,
|
||||
&i.EndedAt,
|
||||
&i.LastStopReason,
|
||||
&i.OrchestratorStepID,
|
||||
&i.PromptTokens,
|
||||
&i.CompletionTokens,
|
||||
&i.ThinkingTokens,
|
||||
&i.TotalCostUsd,
|
||||
&i.LastActivityAt,
|
||||
&i.BudgetMaxCostUsd,
|
||||
&i.BudgetMaxTokens,
|
||||
&i.BudgetMaxWallClockSeconds,
|
||||
&i.TerminatedReason,
|
||||
&i.SandboxMode,
|
||||
&i.CostUsd,
|
||||
&i.TokensTotal,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -152,7 +362,12 @@ const listAllSessions = `-- name: ListAllSessions :many
|
||||
SELECT id, workspace_id, project_id, agent_kind_id, user_id,
|
||||
issue_id, requirement_id, agent_session_id,
|
||||
branch, cwd_path, is_main_worktree, status,
|
||||
pid, exit_code, last_error, started_at, ended_at
|
||||
pid, exit_code, last_error, started_at, ended_at, last_stop_reason,
|
||||
orchestrator_step_id,
|
||||
prompt_tokens, completion_tokens, thinking_tokens, total_cost_usd,
|
||||
last_activity_at, budget_max_cost_usd, budget_max_tokens,
|
||||
budget_max_wall_clock_seconds, terminated_reason, sandbox_mode,
|
||||
cost_usd, tokens_total
|
||||
FROM acp_sessions
|
||||
WHERE ($1::uuid IS NULL OR requirement_id = $1)
|
||||
ORDER BY started_at DESC
|
||||
@@ -185,6 +400,68 @@ func (q *Queries) ListAllSessions(ctx context.Context, dollar_1 pgtype.UUID) ([]
|
||||
&i.LastError,
|
||||
&i.StartedAt,
|
||||
&i.EndedAt,
|
||||
&i.LastStopReason,
|
||||
&i.OrchestratorStepID,
|
||||
&i.PromptTokens,
|
||||
&i.CompletionTokens,
|
||||
&i.ThinkingTokens,
|
||||
&i.TotalCostUsd,
|
||||
&i.LastActivityAt,
|
||||
&i.BudgetMaxCostUsd,
|
||||
&i.BudgetMaxTokens,
|
||||
&i.BudgetMaxWallClockSeconds,
|
||||
&i.TerminatedReason,
|
||||
&i.SandboxMode,
|
||||
&i.CostUsd,
|
||||
&i.TokensTotal,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listSessionUsageBySession = `-- name: ListSessionUsageBySession :many
|
||||
SELECT id, session_id, user_id, project_id, agent_kind_id, model_id,
|
||||
prompt_tokens, completion_tokens, thinking_tokens, cost_usd,
|
||||
source_event_id, created_at
|
||||
FROM acp_session_usage
|
||||
WHERE session_id = $1
|
||||
ORDER BY created_at DESC
|
||||
LIMIT $2
|
||||
`
|
||||
|
||||
type ListSessionUsageBySessionParams struct {
|
||||
SessionID pgtype.UUID `json:"session_id"`
|
||||
Limit int32 `json:"limit"`
|
||||
}
|
||||
|
||||
func (q *Queries) ListSessionUsageBySession(ctx context.Context, arg ListSessionUsageBySessionParams) ([]AcpSessionUsage, error) {
|
||||
rows, err := q.db.Query(ctx, listSessionUsageBySession, arg.SessionID, arg.Limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []AcpSessionUsage
|
||||
for rows.Next() {
|
||||
var i AcpSessionUsage
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.SessionID,
|
||||
&i.UserID,
|
||||
&i.ProjectID,
|
||||
&i.AgentKindID,
|
||||
&i.ModelID,
|
||||
&i.PromptTokens,
|
||||
&i.CompletionTokens,
|
||||
&i.ThinkingTokens,
|
||||
&i.CostUsd,
|
||||
&i.SourceEventID,
|
||||
&i.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -200,7 +477,12 @@ const listSessionsByUser = `-- name: ListSessionsByUser :many
|
||||
SELECT id, workspace_id, project_id, agent_kind_id, user_id,
|
||||
issue_id, requirement_id, agent_session_id,
|
||||
branch, cwd_path, is_main_worktree, status,
|
||||
pid, exit_code, last_error, started_at, ended_at
|
||||
pid, exit_code, last_error, started_at, ended_at, last_stop_reason,
|
||||
orchestrator_step_id,
|
||||
prompt_tokens, completion_tokens, thinking_tokens, total_cost_usd,
|
||||
last_activity_at, budget_max_cost_usd, budget_max_tokens,
|
||||
budget_max_wall_clock_seconds, terminated_reason, sandbox_mode,
|
||||
cost_usd, tokens_total
|
||||
FROM acp_sessions
|
||||
WHERE user_id = $1
|
||||
AND ($2::uuid IS NULL OR requirement_id = $2)
|
||||
@@ -239,6 +521,76 @@ func (q *Queries) ListSessionsByUser(ctx context.Context, arg ListSessionsByUser
|
||||
&i.LastError,
|
||||
&i.StartedAt,
|
||||
&i.EndedAt,
|
||||
&i.LastStopReason,
|
||||
&i.OrchestratorStepID,
|
||||
&i.PromptTokens,
|
||||
&i.CompletionTokens,
|
||||
&i.ThinkingTokens,
|
||||
&i.TotalCostUsd,
|
||||
&i.LastActivityAt,
|
||||
&i.BudgetMaxCostUsd,
|
||||
&i.BudgetMaxTokens,
|
||||
&i.BudgetMaxWallClockSeconds,
|
||||
&i.TerminatedReason,
|
||||
&i.SandboxMode,
|
||||
&i.CostUsd,
|
||||
&i.TokensTotal,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listSessionsForReaper = `-- name: ListSessionsForReaper :many
|
||||
SELECT id, user_id, started_at, last_activity_at, budget_max_wall_clock_seconds
|
||||
FROM acp_sessions
|
||||
WHERE status IN ('starting','running')
|
||||
AND (
|
||||
(budget_max_wall_clock_seconds IS NOT NULL
|
||||
AND started_at < now() - make_interval(secs => budget_max_wall_clock_seconds))
|
||||
OR ($1::timestamptz IS NOT NULL AND started_at < $1::timestamptz)
|
||||
OR last_activity_at < $2::timestamptz
|
||||
)
|
||||
`
|
||||
|
||||
type ListSessionsForReaperParams struct {
|
||||
Column1 pgtype.Timestamptz `json:"column_1"`
|
||||
Column2 pgtype.Timestamptz `json:"column_2"`
|
||||
}
|
||||
|
||||
type ListSessionsForReaperRow struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
UserID pgtype.UUID `json:"user_id"`
|
||||
StartedAt pgtype.Timestamptz `json:"started_at"`
|
||||
LastActivityAt pgtype.Timestamptz `json:"last_activity_at"`
|
||||
BudgetMaxWallClockSeconds *int32 `json:"budget_max_wall_clock_seconds"`
|
||||
}
|
||||
|
||||
// 返回应被回收的活跃 session:
|
||||
//
|
||||
// 超过自身 wall-clock 上限(budget_max_wall_clock_seconds 非空且 started_at 过早),或
|
||||
// 超过全局 wall-clock 上限($1::timestamptz = now - 全局上限),或
|
||||
// 空闲超时(last_activity_at < $2::timestamptz)。
|
||||
func (q *Queries) ListSessionsForReaper(ctx context.Context, arg ListSessionsForReaperParams) ([]ListSessionsForReaperRow, error) {
|
||||
rows, err := q.db.Query(ctx, listSessionsForReaper, arg.Column1, arg.Column2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ListSessionsForReaperRow
|
||||
for rows.Next() {
|
||||
var i ListSessionsForReaperRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.UserID,
|
||||
&i.StartedAt,
|
||||
&i.LastActivityAt,
|
||||
&i.BudgetMaxWallClockSeconds,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -269,6 +621,23 @@ func (q *Queries) MarkSessionFailedIfActive(ctx context.Context, arg MarkSession
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const markSessionTerminatedReason = `-- name: MarkSessionTerminatedReason :exec
|
||||
UPDATE acp_sessions
|
||||
SET terminated_reason = $2
|
||||
WHERE id = $1 AND terminated_reason IS NULL
|
||||
`
|
||||
|
||||
type MarkSessionTerminatedReasonParams struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
TerminatedReason *string `json:"terminated_reason"`
|
||||
}
|
||||
|
||||
// 写入终止原因;已有非空 terminated_reason 时不覆盖(reaper / budget 互斥保护)。
|
||||
func (q *Queries) MarkSessionTerminatedReason(ctx context.Context, arg MarkSessionTerminatedReasonParams) error {
|
||||
_, err := q.db.Exec(ctx, markSessionTerminatedReason, arg.ID, arg.TerminatedReason)
|
||||
return err
|
||||
}
|
||||
|
||||
const releaseMainWorktree = `-- name: ReleaseMainWorktree :execrows
|
||||
UPDATE workspaces SET active_main_session_id = NULL
|
||||
WHERE id = $1 AND active_main_session_id = $2
|
||||
@@ -287,6 +656,26 @@ func (q *Queries) ReleaseMainWorktree(ctx context.Context, arg ReleaseMainWorktr
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const resetSessionForResume = `-- name: ResetSessionForResume :exec
|
||||
UPDATE acp_sessions
|
||||
SET status = 'starting',
|
||||
agent_session_id = NULL,
|
||||
pid = NULL,
|
||||
exit_code = NULL,
|
||||
last_error = NULL,
|
||||
ended_at = NULL,
|
||||
terminated_reason = NULL,
|
||||
last_activity_at = now()
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
// 把崩溃/退出的 session 复用为新一轮:回到 starting、清空上次运行时字段。
|
||||
// 编排器 resume 在重新 Spawn 前调用,使同一行 session 在同 cwd/worktree 上复用。
|
||||
func (q *Queries) ResetSessionForResume(ctx context.Context, id pgtype.UUID) error {
|
||||
_, err := q.db.Exec(ctx, resetSessionForResume, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const resetStuckSessionsOnRestart = `-- name: ResetStuckSessionsOnRestart :many
|
||||
UPDATE acp_sessions
|
||||
SET status = 'crashed',
|
||||
@@ -332,6 +721,178 @@ func (q *Queries) ResetStuckSessionsOnRestart(ctx context.Context) ([]ResetStuck
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const sumProjectCostUSD = `-- name: SumProjectCostUSD :one
|
||||
SELECT COALESCE(SUM(cost_usd), 0)::numeric AS cost_usd
|
||||
FROM acp_session_usage
|
||||
WHERE project_id = $1 AND created_at >= $2
|
||||
`
|
||||
|
||||
type SumProjectCostUSDParams struct {
|
||||
ProjectID pgtype.UUID `json:"project_id"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
// 某 project 自 since 起的 ACP 累计花费(per-project 软预算)。
|
||||
func (q *Queries) SumProjectCostUSD(ctx context.Context, arg SumProjectCostUSDParams) (pgtype.Numeric, error) {
|
||||
row := q.db.QueryRow(ctx, sumProjectCostUSD, arg.ProjectID, arg.CreatedAt)
|
||||
var cost_usd pgtype.Numeric
|
||||
err := row.Scan(&cost_usd)
|
||||
return cost_usd, err
|
||||
}
|
||||
|
||||
const summarizeAcpUsageByDay = `-- name: SummarizeAcpUsageByDay :many
|
||||
SELECT date_trunc('day', created_at)::timestamptz AS day,
|
||||
SUM(prompt_tokens)::bigint AS prompt_tokens,
|
||||
SUM(completion_tokens)::bigint AS completion_tokens,
|
||||
SUM(thinking_tokens)::bigint AS thinking_tokens,
|
||||
SUM(cost_usd)::numeric AS cost_usd
|
||||
FROM acp_session_usage
|
||||
WHERE created_at >= $1 AND created_at < $2
|
||||
GROUP BY day
|
||||
ORDER BY day
|
||||
`
|
||||
|
||||
type SummarizeAcpUsageByDayParams struct {
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
CreatedAt_2 pgtype.Timestamptz `json:"created_at_2"`
|
||||
}
|
||||
|
||||
type SummarizeAcpUsageByDayRow struct {
|
||||
Day pgtype.Timestamptz `json:"day"`
|
||||
PromptTokens int64 `json:"prompt_tokens"`
|
||||
CompletionTokens int64 `json:"completion_tokens"`
|
||||
ThinkingTokens int64 `json:"thinking_tokens"`
|
||||
CostUsd pgtype.Numeric `json:"cost_usd"`
|
||||
}
|
||||
|
||||
func (q *Queries) SummarizeAcpUsageByDay(ctx context.Context, arg SummarizeAcpUsageByDayParams) ([]SummarizeAcpUsageByDayRow, error) {
|
||||
rows, err := q.db.Query(ctx, summarizeAcpUsageByDay, arg.CreatedAt, arg.CreatedAt_2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []SummarizeAcpUsageByDayRow
|
||||
for rows.Next() {
|
||||
var i SummarizeAcpUsageByDayRow
|
||||
if err := rows.Scan(
|
||||
&i.Day,
|
||||
&i.PromptTokens,
|
||||
&i.CompletionTokens,
|
||||
&i.ThinkingTokens,
|
||||
&i.CostUsd,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const summarizeAcpUsageByModel = `-- name: SummarizeAcpUsageByModel :many
|
||||
SELECT model_id,
|
||||
SUM(prompt_tokens)::bigint AS prompt_tokens,
|
||||
SUM(completion_tokens)::bigint AS completion_tokens,
|
||||
SUM(thinking_tokens)::bigint AS thinking_tokens,
|
||||
SUM(cost_usd)::numeric AS cost_usd
|
||||
FROM acp_session_usage
|
||||
WHERE created_at >= $1 AND created_at < $2
|
||||
GROUP BY model_id
|
||||
ORDER BY cost_usd DESC
|
||||
`
|
||||
|
||||
type SummarizeAcpUsageByModelParams struct {
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
CreatedAt_2 pgtype.Timestamptz `json:"created_at_2"`
|
||||
}
|
||||
|
||||
type SummarizeAcpUsageByModelRow struct {
|
||||
ModelID pgtype.UUID `json:"model_id"`
|
||||
PromptTokens int64 `json:"prompt_tokens"`
|
||||
CompletionTokens int64 `json:"completion_tokens"`
|
||||
ThinkingTokens int64 `json:"thinking_tokens"`
|
||||
CostUsd pgtype.Numeric `json:"cost_usd"`
|
||||
}
|
||||
|
||||
func (q *Queries) SummarizeAcpUsageByModel(ctx context.Context, arg SummarizeAcpUsageByModelParams) ([]SummarizeAcpUsageByModelRow, error) {
|
||||
rows, err := q.db.Query(ctx, summarizeAcpUsageByModel, arg.CreatedAt, arg.CreatedAt_2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []SummarizeAcpUsageByModelRow
|
||||
for rows.Next() {
|
||||
var i SummarizeAcpUsageByModelRow
|
||||
if err := rows.Scan(
|
||||
&i.ModelID,
|
||||
&i.PromptTokens,
|
||||
&i.CompletionTokens,
|
||||
&i.ThinkingTokens,
|
||||
&i.CostUsd,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const summarizeAcpUsageByUser = `-- name: SummarizeAcpUsageByUser :many
|
||||
SELECT user_id,
|
||||
SUM(prompt_tokens)::bigint AS prompt_tokens,
|
||||
SUM(completion_tokens)::bigint AS completion_tokens,
|
||||
SUM(thinking_tokens)::bigint AS thinking_tokens,
|
||||
SUM(cost_usd)::numeric AS cost_usd
|
||||
FROM acp_session_usage
|
||||
WHERE created_at >= $1 AND created_at < $2
|
||||
GROUP BY user_id
|
||||
ORDER BY cost_usd DESC
|
||||
`
|
||||
|
||||
type SummarizeAcpUsageByUserParams struct {
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
CreatedAt_2 pgtype.Timestamptz `json:"created_at_2"`
|
||||
}
|
||||
|
||||
type SummarizeAcpUsageByUserRow struct {
|
||||
UserID pgtype.UUID `json:"user_id"`
|
||||
PromptTokens int64 `json:"prompt_tokens"`
|
||||
CompletionTokens int64 `json:"completion_tokens"`
|
||||
ThinkingTokens int64 `json:"thinking_tokens"`
|
||||
CostUsd pgtype.Numeric `json:"cost_usd"`
|
||||
}
|
||||
|
||||
func (q *Queries) SummarizeAcpUsageByUser(ctx context.Context, arg SummarizeAcpUsageByUserParams) ([]SummarizeAcpUsageByUserRow, error) {
|
||||
rows, err := q.db.Query(ctx, summarizeAcpUsageByUser, arg.CreatedAt, arg.CreatedAt_2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []SummarizeAcpUsageByUserRow
|
||||
for rows.Next() {
|
||||
var i SummarizeAcpUsageByUserRow
|
||||
if err := rows.Scan(
|
||||
&i.UserID,
|
||||
&i.PromptTokens,
|
||||
&i.CompletionTokens,
|
||||
&i.ThinkingTokens,
|
||||
&i.CostUsd,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const updateSessionFinished = `-- name: UpdateSessionFinished :exec
|
||||
UPDATE acp_sessions
|
||||
SET status = $2, exit_code = $3, last_error = $4, ended_at = now()
|
||||
@@ -355,6 +916,22 @@ func (q *Queries) UpdateSessionFinished(ctx context.Context, arg UpdateSessionFi
|
||||
return err
|
||||
}
|
||||
|
||||
const updateSessionLastStopReason = `-- name: UpdateSessionLastStopReason :exec
|
||||
UPDATE acp_sessions
|
||||
SET last_stop_reason = $2
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
type UpdateSessionLastStopReasonParams struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
LastStopReason *string `json:"last_stop_reason"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateSessionLastStopReason(ctx context.Context, arg UpdateSessionLastStopReasonParams) error {
|
||||
_, err := q.db.Exec(ctx, updateSessionLastStopReason, arg.ID, arg.LastStopReason)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateSessionRunning = `-- name: UpdateSessionRunning :exec
|
||||
UPDATE acp_sessions
|
||||
SET status = 'running', agent_session_id = $2, pid = $3
|
||||
@@ -371,3 +948,20 @@ func (q *Queries) UpdateSessionRunning(ctx context.Context, arg UpdateSessionRun
|
||||
_, err := q.db.Exec(ctx, updateSessionRunning, arg.ID, arg.AgentSessionID, arg.Pid)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateSessionSandboxMode = `-- name: UpdateSessionSandboxMode :exec
|
||||
UPDATE acp_sessions
|
||||
SET sandbox_mode = $2
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
type UpdateSessionSandboxModeParams struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
SandboxMode *string `json:"sandbox_mode"`
|
||||
}
|
||||
|
||||
// 记录本 session 运行所用的沙箱模式(审计/取证)。
|
||||
func (q *Queries) UpdateSessionSandboxMode(ctx context.Context, arg UpdateSessionSandboxModeParams) error {
|
||||
_, err := q.db.Exec(ctx, updateSessionSandboxMode, arg.ID, arg.SandboxMode)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: turns.sql
|
||||
|
||||
package acpsqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const getLatestTurnBySession = `-- name: GetLatestTurnBySession :one
|
||||
SELECT id, session_id, turn_index, prompt_request_id, status,
|
||||
stop_reason, update_count, started_at, completed_at
|
||||
FROM acp_turns
|
||||
WHERE session_id = $1
|
||||
ORDER BY turn_index DESC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetLatestTurnBySession(ctx context.Context, sessionID pgtype.UUID) (AcpTurn, error) {
|
||||
row := q.db.QueryRow(ctx, getLatestTurnBySession, sessionID)
|
||||
var i AcpTurn
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.SessionID,
|
||||
&i.TurnIndex,
|
||||
&i.PromptRequestID,
|
||||
&i.Status,
|
||||
&i.StopReason,
|
||||
&i.UpdateCount,
|
||||
&i.StartedAt,
|
||||
&i.CompletedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const incrementTurnUpdateCount = `-- name: IncrementTurnUpdateCount :exec
|
||||
UPDATE acp_turns
|
||||
SET update_count = update_count + 1
|
||||
WHERE session_id = $1 AND turn_index = $2 AND status = 'in_progress'
|
||||
`
|
||||
|
||||
type IncrementTurnUpdateCountParams struct {
|
||||
SessionID pgtype.UUID `json:"session_id"`
|
||||
TurnIndex int32 `json:"turn_index"`
|
||||
}
|
||||
|
||||
func (q *Queries) IncrementTurnUpdateCount(ctx context.Context, arg IncrementTurnUpdateCountParams) error {
|
||||
_, err := q.db.Exec(ctx, incrementTurnUpdateCount, arg.SessionID, arg.TurnIndex)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertTurn = `-- name: InsertTurn :one
|
||||
INSERT INTO acp_turns (
|
||||
session_id, turn_index, prompt_request_id, status
|
||||
) VALUES ($1, $2, $3, $4)
|
||||
RETURNING id, session_id, turn_index, prompt_request_id, status,
|
||||
stop_reason, update_count, started_at, completed_at
|
||||
`
|
||||
|
||||
type InsertTurnParams struct {
|
||||
SessionID pgtype.UUID `json:"session_id"`
|
||||
TurnIndex int32 `json:"turn_index"`
|
||||
PromptRequestID *string `json:"prompt_request_id"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
func (q *Queries) InsertTurn(ctx context.Context, arg InsertTurnParams) (AcpTurn, error) {
|
||||
row := q.db.QueryRow(ctx, insertTurn,
|
||||
arg.SessionID,
|
||||
arg.TurnIndex,
|
||||
arg.PromptRequestID,
|
||||
arg.Status,
|
||||
)
|
||||
var i AcpTurn
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.SessionID,
|
||||
&i.TurnIndex,
|
||||
&i.PromptRequestID,
|
||||
&i.Status,
|
||||
&i.StopReason,
|
||||
&i.UpdateCount,
|
||||
&i.StartedAt,
|
||||
&i.CompletedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listTurnsBySession = `-- name: ListTurnsBySession :many
|
||||
SELECT id, session_id, turn_index, prompt_request_id, status,
|
||||
stop_reason, update_count, started_at, completed_at
|
||||
FROM acp_turns
|
||||
WHERE session_id = $1
|
||||
ORDER BY turn_index ASC
|
||||
`
|
||||
|
||||
func (q *Queries) ListTurnsBySession(ctx context.Context, sessionID pgtype.UUID) ([]AcpTurn, error) {
|
||||
rows, err := q.db.Query(ctx, listTurnsBySession, sessionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []AcpTurn
|
||||
for rows.Next() {
|
||||
var i AcpTurn
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.SessionID,
|
||||
&i.TurnIndex,
|
||||
&i.PromptRequestID,
|
||||
&i.Status,
|
||||
&i.StopReason,
|
||||
&i.UpdateCount,
|
||||
&i.StartedAt,
|
||||
&i.CompletedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const markTurnAborted = `-- name: MarkTurnAborted :exec
|
||||
UPDATE acp_turns
|
||||
SET status = 'aborted', completed_at = now()
|
||||
WHERE session_id = $1 AND turn_index = $2 AND status = 'in_progress'
|
||||
`
|
||||
|
||||
type MarkTurnAbortedParams struct {
|
||||
SessionID pgtype.UUID `json:"session_id"`
|
||||
TurnIndex int32 `json:"turn_index"`
|
||||
}
|
||||
|
||||
func (q *Queries) MarkTurnAborted(ctx context.Context, arg MarkTurnAbortedParams) error {
|
||||
_, err := q.db.Exec(ctx, markTurnAborted, arg.SessionID, arg.TurnIndex)
|
||||
return err
|
||||
}
|
||||
|
||||
const markTurnCompleted = `-- name: MarkTurnCompleted :exec
|
||||
UPDATE acp_turns
|
||||
SET status = 'completed', stop_reason = $3, update_count = $4, completed_at = now()
|
||||
WHERE session_id = $1 AND turn_index = $2 AND status = 'in_progress'
|
||||
`
|
||||
|
||||
type MarkTurnCompletedParams struct {
|
||||
SessionID pgtype.UUID `json:"session_id"`
|
||||
TurnIndex int32 `json:"turn_index"`
|
||||
StopReason *string `json:"stop_reason"`
|
||||
UpdateCount int32 `json:"update_count"`
|
||||
}
|
||||
|
||||
func (q *Queries) MarkTurnCompleted(ctx context.Context, arg MarkTurnCompletedParams) error {
|
||||
_, err := q.db.Exec(ctx, markTurnCompleted,
|
||||
arg.SessionID,
|
||||
arg.TurnIndex,
|
||||
arg.StopReason,
|
||||
arg.UpdateCount,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const purgeTurnsBefore = `-- name: PurgeTurnsBefore :execrows
|
||||
DELETE FROM acp_turns WHERE started_at < $1
|
||||
`
|
||||
|
||||
func (q *Queries) PurgeTurnsBefore(ctx context.Context, startedAt pgtype.Timestamptz) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, purgeTurnsBefore, startedAt)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user