nyk commited on
Commit
c630ac6
·
unverified ·
1 Parent(s): 04b82f8

feat: aggregate token usage from db with stable agent grouping

Browse files
src/app/api/tokens/route.ts CHANGED
@@ -5,6 +5,7 @@ import { config, ensureDirExists } from '@/lib/config'
5
  import { requireRole } from '@/lib/auth'
6
  import { getAllGatewaySessions } from '@/lib/sessions'
7
  import { logger } from '@/lib/logger'
 
8
 
9
  const DATA_PATH = config.tokensPath
10
 
@@ -12,6 +13,7 @@ interface TokenUsageRecord {
12
  id: string
13
  model: string
14
  sessionId: string
 
15
  timestamp: number
16
  inputTokens: number
17
  outputTokens: number
@@ -53,6 +55,13 @@ const MODEL_PRICING: Record<string, number> = {
53
  'ollama/qwen2.5-coder:14b': 0.0,
54
  }
55
 
 
 
 
 
 
 
 
56
  function getModelCost(modelName: string): number {
57
  if (MODEL_PRICING[modelName] !== undefined) return MODEL_PRICING[modelName]
58
  for (const [model, cost] of Object.entries(MODEL_PRICING)) {
@@ -61,24 +70,119 @@ function getModelCost(modelName: string): number {
61
  return 1.0
62
  }
63
 
64
- /**
65
- * Load token data from persistent file, falling back to deriving from session stores.
66
- */
67
- async function loadTokenData(): Promise<TokenUsageRecord[]> {
68
- // First try loading from persistent token file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  try {
70
  ensureDirExists(dirname(DATA_PATH))
71
  await access(DATA_PATH)
72
  const data = await readFile(DATA_PATH, 'utf-8')
73
- const records = JSON.parse(data)
74
- if (Array.isArray(records) && records.length > 0) {
75
- return records
76
- }
 
 
77
  } catch {
78
- // File doesn't exist or is empty — derive from sessions
 
 
 
 
 
 
 
 
 
 
 
 
79
  }
80
 
81
- // Derive token usage from session stores
82
  return deriveFromSessions()
83
  }
84
 
@@ -103,6 +207,7 @@ function deriveFromSessions(): TokenUsageRecord[] {
103
  id: `session-${session.agent}-${session.key}`,
104
  model: session.model || 'unknown',
105
  sessionId: `${session.agent}:${session.chatType}`,
 
106
  timestamp: session.updatedAt,
107
  inputTokens,
108
  outputTokens,
@@ -218,7 +323,7 @@ export async function GET(request: NextRequest) {
218
 
219
  // Agent aggregation: extract agent name from sessionId (format: "agentName:chatType")
220
  const agentGroups = filteredData.reduce((acc, record) => {
221
- const agent = record.sessionId.split(':')[0] || 'unknown'
222
  if (!acc[agent]) acc[agent] = []
223
  acc[agent].push(record)
224
  return acc
@@ -241,7 +346,7 @@ export async function GET(request: NextRequest) {
241
 
242
  if (action === 'agent-costs') {
243
  const agentGroups = filteredData.reduce((acc, record) => {
244
- const agent = record.sessionId.split(':')[0] || 'unknown'
245
  if (!acc[agent]) acc[agent] = []
246
  acc[agent].push(record)
247
  return acc
@@ -327,12 +432,13 @@ export async function GET(request: NextRequest) {
327
  }
328
 
329
  if (format === 'csv') {
330
- const headers = ['timestamp', 'model', 'sessionId', 'operation', 'inputTokens', 'outputTokens', 'totalTokens', 'cost', 'duration']
331
  const csvRows = [headers.join(',')]
332
 
333
  filteredData.forEach(record => {
334
  csvRows.push([
335
  new Date(record.timestamp).toISOString(),
 
336
  record.model,
337
  record.sessionId,
338
  record.operation,
@@ -411,6 +517,7 @@ export async function POST(request: NextRequest) {
411
  id: `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
412
  model,
413
  sessionId,
 
414
  timestamp: Date.now(),
415
  inputTokens,
416
  outputTokens,
@@ -420,7 +527,8 @@ export async function POST(request: NextRequest) {
420
  duration,
421
  }
422
 
423
- const existingData = await loadTokenData()
 
424
  existingData.unshift(record)
425
 
426
  if (existingData.length > 10000) {
 
5
  import { requireRole } from '@/lib/auth'
6
  import { getAllGatewaySessions } from '@/lib/sessions'
7
  import { logger } from '@/lib/logger'
8
+ import { getDatabase } from '@/lib/db'
9
 
10
  const DATA_PATH = config.tokensPath
11
 
 
13
  id: string
14
  model: string
15
  sessionId: string
16
+ agentName: string
17
  timestamp: number
18
  inputTokens: number
19
  outputTokens: number
 
55
  'ollama/qwen2.5-coder:14b': 0.0,
56
  }
57
 
58
+ function extractAgentName(sessionId: string): string {
59
+ const trimmed = sessionId.trim()
60
+ if (!trimmed) return 'unknown'
61
+ const [agent] = trimmed.split(':')
62
+ return agent?.trim() || 'unknown'
63
+ }
64
+
65
  function getModelCost(modelName: string): number {
66
  if (MODEL_PRICING[modelName] !== undefined) return MODEL_PRICING[modelName]
67
  for (const [model, cost] of Object.entries(MODEL_PRICING)) {
 
70
  return 1.0
71
  }
72
 
73
+ interface DbTokenUsageRow {
74
+ id: number
75
+ model: string
76
+ session_id: string
77
+ input_tokens: number
78
+ output_tokens: number
79
+ created_at: number
80
+ }
81
+
82
+ function loadTokenDataFromDb(): TokenUsageRecord[] {
83
+ try {
84
+ const db = getDatabase()
85
+ const rows = db.prepare(`
86
+ SELECT id, model, session_id, input_tokens, output_tokens, created_at
87
+ FROM token_usage
88
+ ORDER BY created_at DESC, id DESC
89
+ LIMIT 10000
90
+ `).all() as DbTokenUsageRow[]
91
+
92
+ return rows.map((row) => {
93
+ const totalTokens = row.input_tokens + row.output_tokens
94
+ const costPer1k = getModelCost(row.model)
95
+ return {
96
+ id: `db-${row.id}`,
97
+ model: row.model,
98
+ sessionId: row.session_id,
99
+ agentName: extractAgentName(row.session_id),
100
+ timestamp: row.created_at * 1000,
101
+ inputTokens: row.input_tokens,
102
+ outputTokens: row.output_tokens,
103
+ totalTokens,
104
+ cost: (totalTokens / 1000) * costPer1k,
105
+ operation: 'heartbeat',
106
+ }
107
+ })
108
+ } catch (error) {
109
+ logger.warn({ err: error }, 'Failed to load token usage from database')
110
+ return []
111
+ }
112
+ }
113
+
114
+ function normalizeTokenRecord(record: Partial<TokenUsageRecord>): TokenUsageRecord | null {
115
+ if (!record.model || !record.sessionId) return null
116
+ const inputTokens = Number(record.inputTokens ?? 0)
117
+ const outputTokens = Number(record.outputTokens ?? 0)
118
+ const totalTokens = Number(record.totalTokens ?? inputTokens + outputTokens)
119
+ const model = String(record.model)
120
+ return {
121
+ id: String(record.id ?? `${Date.now()}-${Math.random().toString(36).slice(2, 11)}`),
122
+ model,
123
+ sessionId: String(record.sessionId),
124
+ agentName: String(record.agentName ?? extractAgentName(String(record.sessionId))),
125
+ timestamp: Number(record.timestamp ?? Date.now()),
126
+ inputTokens,
127
+ outputTokens,
128
+ totalTokens,
129
+ cost: Number(record.cost ?? (totalTokens / 1000) * getModelCost(model)),
130
+ operation: String(record.operation ?? 'chat_completion'),
131
+ duration: record.duration,
132
+ }
133
+ }
134
+
135
+ function dedupeTokenRecords(records: TokenUsageRecord[]): TokenUsageRecord[] {
136
+ const seen = new Set<string>()
137
+ const deduped: TokenUsageRecord[] = []
138
+
139
+ for (const record of records) {
140
+ const key = [
141
+ record.sessionId,
142
+ record.model,
143
+ record.timestamp,
144
+ record.inputTokens,
145
+ record.outputTokens,
146
+ record.totalTokens,
147
+ record.operation,
148
+ record.duration ?? '',
149
+ ].join('|')
150
+ if (seen.has(key)) continue
151
+ seen.add(key)
152
+ deduped.push(record)
153
+ }
154
+
155
+ return deduped
156
+ }
157
+
158
+ async function loadTokenDataFromFile(): Promise<TokenUsageRecord[]> {
159
  try {
160
  ensureDirExists(dirname(DATA_PATH))
161
  await access(DATA_PATH)
162
  const data = await readFile(DATA_PATH, 'utf-8')
163
+ const parsed = JSON.parse(data)
164
+ if (!Array.isArray(parsed)) return []
165
+
166
+ return parsed
167
+ .map((record: Partial<TokenUsageRecord>) => normalizeTokenRecord(record))
168
+ .filter((record): record is TokenUsageRecord => record !== null)
169
  } catch {
170
+ return []
171
+ }
172
+ }
173
+
174
+ /**
175
+ * Load token data from persistent file, falling back to deriving from session stores.
176
+ */
177
+ async function loadTokenData(): Promise<TokenUsageRecord[]> {
178
+ const dbRecords = loadTokenDataFromDb()
179
+ const fileRecords = await loadTokenDataFromFile()
180
+ const combined = dedupeTokenRecords([...dbRecords, ...fileRecords]).sort((a, b) => b.timestamp - a.timestamp)
181
+ if (combined.length > 0) {
182
+ return combined
183
  }
184
 
185
+ // Final fallback: derive from in-memory sessions
186
  return deriveFromSessions()
187
  }
188
 
 
207
  id: `session-${session.agent}-${session.key}`,
208
  model: session.model || 'unknown',
209
  sessionId: `${session.agent}:${session.chatType}`,
210
+ agentName: session.agent || 'unknown',
211
  timestamp: session.updatedAt,
212
  inputTokens,
213
  outputTokens,
 
323
 
324
  // Agent aggregation: extract agent name from sessionId (format: "agentName:chatType")
325
  const agentGroups = filteredData.reduce((acc, record) => {
326
+ const agent = record.agentName || extractAgentName(record.sessionId)
327
  if (!acc[agent]) acc[agent] = []
328
  acc[agent].push(record)
329
  return acc
 
346
 
347
  if (action === 'agent-costs') {
348
  const agentGroups = filteredData.reduce((acc, record) => {
349
+ const agent = record.agentName || extractAgentName(record.sessionId)
350
  if (!acc[agent]) acc[agent] = []
351
  acc[agent].push(record)
352
  return acc
 
432
  }
433
 
434
  if (format === 'csv') {
435
+ const headers = ['timestamp', 'agentName', 'model', 'sessionId', 'operation', 'inputTokens', 'outputTokens', 'totalTokens', 'cost', 'duration']
436
  const csvRows = [headers.join(',')]
437
 
438
  filteredData.forEach(record => {
439
  csvRows.push([
440
  new Date(record.timestamp).toISOString(),
441
+ record.agentName,
442
  record.model,
443
  record.sessionId,
444
  record.operation,
 
517
  id: `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
518
  model,
519
  sessionId,
520
+ agentName: extractAgentName(sessionId),
521
  timestamp: Date.now(),
522
  inputTokens,
523
  outputTokens,
 
527
  duration,
528
  }
529
 
530
+ // Persist only manually posted usage records in the JSON file.
531
+ const existingData = await loadTokenDataFromFile()
532
  existingData.unshift(record)
533
 
534
  if (existingData.length > 10000) {
tests/direct-cli.spec.ts CHANGED
@@ -108,6 +108,14 @@ test.describe('Direct CLI Integration', () => {
108
  const hbBody = await hbRes.json()
109
  expect(hbBody.token_recorded).toBe(true)
110
  expect(hbBody.agent).toBe(agentName)
 
 
 
 
 
 
 
 
111
  })
112
 
113
  test('DELETE /api/connect disconnects and sets agent offline', async ({ request }) => {
 
108
  const hbBody = await hbRes.json()
109
  expect(hbBody.token_recorded).toBe(true)
110
  expect(hbBody.agent).toBe(agentName)
111
+
112
+ const costsRes = await request.get('/api/tokens?action=agent-costs&timeframe=hour', {
113
+ headers: API_KEY_HEADER,
114
+ })
115
+ expect(costsRes.status()).toBe(200)
116
+ const costsBody = await costsRes.json()
117
+ expect(costsBody.agents).toHaveProperty(agentName)
118
+ expect(costsBody.agents[agentName].stats.totalTokens).toBeGreaterThanOrEqual(1500)
119
  })
120
 
121
  test('DELETE /api/connect disconnects and sets agent offline', async ({ request }) => {