-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrecorder.go
More file actions
147 lines (126 loc) · 4.26 KB
/
recorder.go
File metadata and controls
147 lines (126 loc) · 4.26 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
package main
import (
"context"
"database/sql"
"encoding/json"
"cdr.dev/slog/v3"
"github.com/coder/aibridge"
"github.com/google/uuid"
)
// SQLiteRecorder implements aibridge.Recorder and persists usage data to SQLite.
type SQLiteRecorder struct {
db *sql.DB
logger slog.Logger
stmtInsertInterception *sql.Stmt
stmtUpdateInterception *sql.Stmt
stmtInsertTokenUsage *sql.Stmt
stmtInsertPromptUsage *sql.Stmt
stmtInsertToolUsage *sql.Stmt
}
func NewSQLiteRecorder(db *sql.DB, logger slog.Logger) (*SQLiteRecorder, error) {
r := &SQLiteRecorder{db: db, logger: logger}
var err error
r.stmtInsertInterception, err = db.Prepare(`
INSERT INTO aibridge_interceptions (id, initiator_id, provider, model, started_at, metadata)
VALUES (?, ?, ?, ?, ?, ?)`)
if err != nil {
return nil, err
}
r.stmtUpdateInterception, err = db.Prepare(`
UPDATE aibridge_interceptions SET ended_at = ? WHERE id = ?`)
if err != nil {
return nil, err
}
r.stmtInsertTokenUsage, err = db.Prepare(`
INSERT INTO aibridge_token_usages (id, interception_id, provider_response_id, input_tokens, output_tokens, metadata, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?)`)
if err != nil {
return nil, err
}
r.stmtInsertPromptUsage, err = db.Prepare(`
INSERT INTO aibridge_user_prompts (id, interception_id, provider_response_id, prompt, metadata, created_at)
VALUES (?, ?, ?, ?, ?, ?)`)
if err != nil {
return nil, err
}
r.stmtInsertToolUsage, err = db.Prepare(`
INSERT INTO aibridge_tool_usages (id, interception_id, provider_response_id, server_url, tool, input, injected, invocation_error, metadata, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
if err != nil {
return nil, err
}
return r, nil
}
func (r *SQLiteRecorder) Close() error {
r.stmtInsertInterception.Close()
r.stmtUpdateInterception.Close()
r.stmtInsertTokenUsage.Close()
r.stmtInsertPromptUsage.Close()
r.stmtInsertToolUsage.Close()
return nil
}
func (r *SQLiteRecorder) RecordInterception(ctx context.Context, req *aibridge.InterceptionRecord) error {
metadata, _ := json.Marshal(req.Metadata)
_, err := r.stmtInsertInterception.ExecContext(ctx,
req.ID, req.InitiatorID, req.Provider, req.Model, req.StartedAt, string(metadata),
)
if err != nil {
r.logger.Warn(ctx, "failed to record interception", slog.Error(err))
}
return err
}
func (r *SQLiteRecorder) RecordInterceptionEnded(ctx context.Context, req *aibridge.InterceptionRecordEnded) error {
_, err := r.stmtUpdateInterception.ExecContext(ctx, req.EndedAt, req.ID)
if err != nil {
r.logger.Warn(ctx, "failed to record interception end", slog.Error(err))
}
return err
}
func (r *SQLiteRecorder) RecordTokenUsage(ctx context.Context, req *aibridge.TokenUsageRecord) error {
// Build metadata, merging extra token types.
merged := make(map[string]any)
for k, v := range req.Metadata {
merged[k] = v
}
for k, v := range req.ExtraTokenTypes {
merged[k] = v
}
metadata, _ := json.Marshal(merged)
_, err := r.stmtInsertTokenUsage.ExecContext(ctx,
uuid.NewString(), req.InterceptionID, req.MsgID, req.Input, req.Output, string(metadata), req.CreatedAt,
)
if err != nil {
r.logger.Warn(ctx, "failed to record token usage", slog.Error(err))
}
return err
}
func (r *SQLiteRecorder) RecordPromptUsage(ctx context.Context, req *aibridge.PromptUsageRecord) error {
metadata, _ := json.Marshal(req.Metadata)
_, err := r.stmtInsertPromptUsage.ExecContext(ctx,
uuid.NewString(), req.InterceptionID, req.MsgID, req.Prompt, string(metadata), req.CreatedAt,
)
if err != nil {
r.logger.Warn(ctx, "failed to record prompt usage", slog.Error(err))
}
return err
}
func (r *SQLiteRecorder) RecordToolUsage(ctx context.Context, req *aibridge.ToolUsageRecord) error {
metadata, _ := json.Marshal(req.Metadata)
input, _ := json.Marshal(req.Args)
var serverURL *string
if req.ServerURL != nil {
serverURL = req.ServerURL
}
var invocationError *string
if req.InvocationError != nil {
errStr := req.InvocationError.Error()
invocationError = &errStr
}
_, err := r.stmtInsertToolUsage.ExecContext(ctx,
uuid.NewString(), req.InterceptionID, req.MsgID, serverURL, req.Tool, string(input), req.Injected, invocationError, string(metadata), req.CreatedAt,
)
if err != nil {
r.logger.Warn(ctx, "failed to record tool usage", slog.Error(err))
}
return err
}