-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsession_stat.go
More file actions
168 lines (130 loc) · 4.32 KB
/
session_stat.go
File metadata and controls
168 lines (130 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package codexsdk
import (
"context"
"fmt"
"os"
"time"
"github.com/google/uuid"
"github.com/ethpandaops/codex-agent-sdk-go/internal/session"
)
func resolveCodexHome(options *CodexAgentOptions) (string, error) {
codexHome := options.CodexHome
if codexHome != "" {
return codexHome, nil
}
return session.DefaultCodexHome()
}
func buildSessionStat(row *session.ThreadRow) *SessionStat {
return &SessionStat{
SessionID: row.ID,
CreatedAt: row.CreatedAt,
UpdatedAt: row.UpdatedAt,
Title: row.Title,
Source: row.Source,
ModelProvider: row.ModelProvider,
Cwd: row.Cwd,
SandboxPolicy: row.SandboxPolicy,
ApprovalMode: row.ApprovalMode,
TokensUsed: row.TokensUsed,
Archived: row.Archived,
ArchivedAt: row.ArchivedAt,
RolloutPath: row.RolloutPath,
GitSHA: row.GitSHA,
GitBranch: row.GitBranch,
GitOriginURL: row.GitOriginURL,
CLIVersion: row.CLIVersion,
FirstUserMessage: row.FirstUserMessage,
AgentNickname: row.AgentNickname,
AgentRole: row.AgentRole,
MemoryMode: row.MemoryMode,
}
}
func applyRolloutFileStat(stat *SessionStat) {
fi, err := os.Stat(stat.RolloutPath)
if err == nil {
stat.SizeBytes = fi.Size()
stat.LastModified = fi.ModTime()
}
}
// SessionStat contains metadata about a Codex CLI session read from the
// local SQLite database (~/.codex/state_5.sqlite).
type SessionStat struct {
// SessionID is the UUID of the session thread.
SessionID string
// SizeBytes is the size of the rollout JSONL file in bytes (0 if missing).
SizeBytes int64
// LastModified is the modification time of the rollout JSONL file.
LastModified time.Time
// CreatedAt is when the session was created (from the database).
CreatedAt time.Time
// UpdatedAt is when the session was last updated (from the database).
UpdatedAt time.Time
// Title is the session title.
Title string
// Source indicates how the session was created ("cli", "vscode", etc.).
Source string
// ModelProvider is the model provider used for the session.
ModelProvider string
// Cwd is the working directory for the session.
Cwd string
// SandboxPolicy is the sandbox policy applied to the session.
SandboxPolicy string
// ApprovalMode is the approval mode for the session.
ApprovalMode string
// TokensUsed is the total number of tokens consumed by the session.
TokensUsed int64
// Archived indicates whether the session has been archived.
Archived bool
// ArchivedAt is when the session was archived (nil if not archived).
ArchivedAt *time.Time
// RolloutPath is the path to the session's rollout JSONL file.
RolloutPath string
// GitSHA is the git commit SHA at session creation (nil if unavailable).
GitSHA *string
// GitBranch is the git branch at session creation (nil if unavailable).
GitBranch *string
// GitOriginURL is the git remote origin URL (nil if unavailable).
GitOriginURL *string
// CLIVersion is the version of the Codex CLI used for the session.
CLIVersion string
// FirstUserMessage is the first message sent by the user.
FirstUserMessage string
// AgentNickname is the agent's display name (nil if not set).
AgentNickname *string
// AgentRole is the agent's role designation (nil if not set).
AgentRole *string
// MemoryMode is the memory mode setting for the session.
MemoryMode string
}
// StatSession reads session metadata from the Codex CLI's local SQLite
// database. The sessionID must be a valid UUID. Use WithCwd to filter by
// project directory and WithCodexHome to override the default ~/.codex
// location.
//
// Returns ErrSessionNotFound when the session does not exist or the
// database file is missing.
func StatSession(
ctx context.Context,
sessionID string,
opts ...Option,
) (*SessionStat, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
if _, err := uuid.Parse(sessionID); err != nil {
return nil, fmt.Errorf("invalid session ID %q: %w", sessionID, err)
}
o := applyAgentOptions(opts)
codexHome, err := resolveCodexHome(o)
if err != nil {
return nil, fmt.Errorf("resolving codex home: %w", err)
}
dbPath := session.DatabasePath(codexHome)
row, err := session.LookupThread(ctx, dbPath, sessionID, o.Cwd)
if err != nil {
return nil, err
}
stat := buildSessionStat(row)
applyRolloutFileStat(stat)
return stat, nil
}