-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.ts
More file actions
494 lines (464 loc) · 14 KB
/
Copy pathcache.ts
File metadata and controls
494 lines (464 loc) · 14 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
import { mkdirSync } from "node:fs";
import { dirname } from "node:path";
import {
type BatchManifest,
type IngestionEvent,
manifestPrefix,
type ObjectStore,
otelResourceSpansToEvents,
} from "@agentpond/core";
import { type DuckDBConnection, DuckDBInstance } from "@duckdb/node-api";
export type SyncResult = {
manifestsProcessed: number;
objectsProcessed: number;
eventsProcessed: number;
};
export class AgentPondDuckDb {
private instance?: DuckDBInstance;
private connection?: DuckDBConnection;
constructor(readonly dbPath: string) {
mkdirSync(dirname(dbPath), { recursive: true });
}
async init(): Promise<void> {
await this.exec(`
CREATE TABLE IF NOT EXISTS processed_manifests (
key TEXT PRIMARY KEY,
processed_at TIMESTAMP DEFAULT current_timestamp
);
CREATE TABLE IF NOT EXISTS processed_objects (
key TEXT PRIMARY KEY,
manifest_key TEXT,
processed_at TIMESTAMP DEFAULT current_timestamp
);
CREATE TABLE IF NOT EXISTS events_raw (
event_id TEXT PRIMARY KEY,
project_id TEXT,
manifest_key TEXT,
object_key TEXT,
event_type TEXT,
event_timestamp TIMESTAMP,
entity_id TEXT,
body_json TEXT,
event_json TEXT
);
CREATE TABLE IF NOT EXISTS traces (
id TEXT PRIMARY KEY,
project_id TEXT,
name TEXT,
user_id TEXT,
session_id TEXT,
start_time TIMESTAMP,
end_time TIMESTAMP,
metadata_json TEXT,
input_json TEXT,
output_json TEXT,
total_cost DOUBLE,
updated_at TIMESTAMP
);
CREATE TABLE IF NOT EXISTS observations (
id TEXT PRIMARY KEY,
project_id TEXT,
trace_id TEXT,
parent_observation_id TEXT,
type TEXT,
name TEXT,
start_time TIMESTAMP,
end_time TIMESTAMP,
metadata_json TEXT,
input_json TEXT,
output_json TEXT,
usage_details_json TEXT,
cost_details_json TEXT,
total_cost DOUBLE,
updated_at TIMESTAMP
);
CREATE TABLE IF NOT EXISTS scores (
id TEXT PRIMARY KEY,
project_id TEXT,
trace_id TEXT,
observation_id TEXT,
session_id TEXT,
name TEXT,
value DOUBLE,
string_value TEXT,
data_type TEXT,
source TEXT,
comment TEXT,
metadata_json TEXT,
timestamp TIMESTAMP,
updated_at TIMESTAMP
);
CREATE OR REPLACE VIEW sessions AS
SELECT
session_id AS id,
project_id,
min(start_time) AS first_seen_at,
max(coalesce(end_time, start_time)) AS last_seen_at,
count(*) AS trace_count
FROM traces
WHERE session_id IS NOT NULL AND session_id <> ''
GROUP BY session_id, project_id;
`);
await this.exec(`
ALTER TABLE traces ADD COLUMN IF NOT EXISTS total_cost DOUBLE;
ALTER TABLE observations ADD COLUMN IF NOT EXISTS usage_details_json TEXT;
ALTER TABLE observations ADD COLUMN IF NOT EXISTS cost_details_json TEXT;
ALTER TABLE observations ADD COLUMN IF NOT EXISTS total_cost DOUBLE;
`);
}
async syncFromStore(params: {
store: ObjectStore;
projectId: string;
prefix: string;
}): Promise<SyncResult> {
await this.init();
const result: SyncResult = {
manifestsProcessed: 0,
objectsProcessed: 0,
eventsProcessed: 0,
};
const manifestKeys = await params.store.listKeys(
manifestPrefix(params.prefix, params.projectId),
);
for (const manifestKey of manifestKeys) {
if (await this.exists("processed_manifests", manifestKey)) continue;
const manifest = await params.store.getJson<BatchManifest>(manifestKey);
for (const object of manifest.objects) {
if (await this.exists("processed_objects", object.key)) continue;
const events =
object.entityType === "otel"
? otelResourceSpansToEvents(
await params.store.getJson<unknown[]>(object.key),
)
: await params.store.getJson<IngestionEvent[]>(object.key);
for (const event of events) {
const entityId =
object.entityType === "otel"
? (stringValue((event.body as Record<string, unknown>).id) ??
object.entityId)
: object.entityId;
await this.upsertRawEvent({
projectId: manifest.projectId,
manifestKey,
objectKey: object.key,
entityId,
event,
});
await this.projectEvent(manifest.projectId, event);
result.eventsProcessed += 1;
}
await this.insertKey("processed_objects", object.key, manifestKey);
result.objectsProcessed += 1;
}
await this.insertKey("processed_manifests", manifestKey);
result.manifestsProcessed += 1;
}
return result;
}
async query<T = Record<string, unknown>>(sql: string): Promise<T[]> {
await this.init();
return this.all<T>(sql);
}
async close(): Promise<void> {
if (this.connection) {
const connection = this.connection;
this.connection = undefined;
connection.closeSync();
}
if (this.instance) {
const instance = this.instance;
this.instance = undefined;
instance.closeSync();
}
}
private async projectEvent(
projectId: string,
event: IngestionEvent,
): Promise<void> {
const body = event.body as Record<string, unknown>;
if (event.type === "trace-create") {
const id = stringValue(body.id ?? body.traceId);
if (!id) return;
await this.exec(`DELETE FROM traces WHERE id = ${sql(id)}`);
await this.exec(`
INSERT INTO traces (
id,
project_id,
name,
user_id,
session_id,
start_time,
end_time,
metadata_json,
input_json,
output_json,
total_cost,
updated_at
) VALUES (
${sql(id)},
${sql(projectId)},
${sql(stringValue(body.name))},
${sql(stringValue(body.userId))},
${sql(stringValue(body.sessionId))},
${sql(timestampValue(body.startTime ?? body.createdAt ?? event.timestamp))},
${sql(timestampValue(body.endTime))},
${sql(jsonString(body.metadata))},
${sql(jsonString(body.input))},
${sql(jsonString(body.output))},
NULL,
${sql(timestampValue(event.timestamp))}
)
`);
await this.refreshTraceTotalCost(id);
return;
}
if (event.type === "score-create") {
const id = stringValue(body.id) ?? event.id;
const {
numberValue,
stringValue: scoreStringValue,
dataType,
} = scoreValue(body.value, stringValue(body.dataType));
await this.exec(`DELETE FROM scores WHERE id = ${sql(id)}`);
await this.exec(`
INSERT INTO scores VALUES (
${sql(id)},
${sql(projectId)},
${sql(stringValue(body.traceId))},
${sql(stringValue(body.observationId))},
${sql(stringValue(body.sessionId))},
${sql(stringValue(body.name))},
${numberValue === null ? "NULL" : String(numberValue)},
${sql(scoreStringValue)},
${sql(dataType)},
${sql(scoreSource(body))},
${sql(stringValue(body.comment))},
${sql(jsonString(body.metadata))},
${sql(timestampValue(body.createdAt ?? event.timestamp))},
${sql(timestampValue(event.timestamp))}
)
`);
return;
}
const id = stringValue(body.id) ?? event.id;
const traceId = stringValue(body.traceId);
const costDetails = objectValue(body.costDetails);
const totalCost =
numericValue(body.totalCost) ?? costDetailsTotal(costDetails);
await this.exec(`DELETE FROM observations WHERE id = ${sql(id)}`);
await this.exec(`
INSERT INTO observations (
id,
project_id,
trace_id,
parent_observation_id,
type,
name,
start_time,
end_time,
metadata_json,
input_json,
output_json,
usage_details_json,
cost_details_json,
total_cost,
updated_at
) VALUES (
${sql(id)},
${sql(projectId)},
${sql(traceId)},
${sql(stringValue(body.parentObservationId))},
${sql(event.type)},
${sql(stringValue(body.name))},
${sql(timestampValue(body.startTime ?? body.createdAt ?? event.timestamp))},
${sql(timestampValue(body.endTime))},
${sql(jsonString(body.metadata))},
${sql(jsonString(body.input))},
${sql(jsonString(body.output))},
${sql(jsonString(body.usageDetails))},
${sql(jsonString(costDetails))},
${totalCost === undefined ? "NULL" : String(totalCost)},
${sql(timestampValue(event.timestamp))}
)
`);
if (traceId) await this.refreshTraceTotalCost(traceId);
}
private async refreshTraceTotalCost(traceId: string): Promise<void> {
await this.exec(`
UPDATE traces
SET total_cost = (
SELECT CASE WHEN count(total_cost) = 0 THEN NULL ELSE sum(total_cost) END
FROM observations
WHERE trace_id = ${sql(traceId)}
)
WHERE id = ${sql(traceId)}
`);
}
private async upsertRawEvent(params: {
projectId: string;
manifestKey: string;
objectKey: string;
entityId: string;
event: IngestionEvent;
}): Promise<void> {
await this.exec(
`DELETE FROM events_raw WHERE event_id = ${sql(params.event.id)}`,
);
await this.exec(`
INSERT INTO events_raw VALUES (
${sql(params.event.id)},
${sql(params.projectId)},
${sql(params.manifestKey)},
${sql(params.objectKey)},
${sql(params.event.type)},
${sql(timestampValue(params.event.timestamp))},
${sql(params.entityId)},
${sql(JSON.stringify(params.event.body))},
${sql(JSON.stringify(params.event))}
)
`);
}
private async exists(
table: "processed_manifests" | "processed_objects",
key: string,
): Promise<boolean> {
const rows = await this.all(
`SELECT key FROM ${table} WHERE key = ${sql(key)} LIMIT 1`,
);
return rows.length > 0;
}
private async insertKey(
table: "processed_manifests" | "processed_objects",
key: string,
manifestKey?: string,
): Promise<void> {
if (table === "processed_objects") {
await this.exec(
`INSERT INTO processed_objects (key, manifest_key) VALUES (${sql(key)}, ${sql(manifestKey)})`,
);
return;
}
await this.exec(
`INSERT INTO processed_manifests (key) VALUES (${sql(key)})`,
);
}
private async getConnection(): Promise<DuckDBConnection> {
if (!this.connection) {
this.instance = await DuckDBInstance.create(this.dbPath);
this.connection = await this.instance.connect();
}
return this.connection;
}
private async exec(sqlText: string): Promise<void> {
await (await this.getConnection()).run(sqlText);
}
private async all<T = Record<string, unknown>>(
sqlText: string,
): Promise<T[]> {
const reader = await (await this.getConnection()).runAndReadAll(sqlText);
return reader.getRowObjectsJS() as T[];
}
}
function sql(value: unknown): string {
if (value === null || value === undefined) return "NULL";
return `'${String(value).replaceAll("'", "''")}'`;
}
function stringValue(value: unknown): string | undefined {
if (typeof value === "string") return value;
if (typeof value === "number" || typeof value === "boolean")
return String(value);
return undefined;
}
function timestampValue(value: unknown): string | undefined {
const raw = stringValue(value);
if (!raw) return undefined;
const date = new Date(raw);
if (Number.isNaN(date.getTime())) return undefined;
return date.toISOString();
}
function jsonString(value: unknown): string | undefined {
if (value === null || value === undefined) return undefined;
return JSON.stringify(value);
}
function objectValue(value: unknown): Record<string, unknown> | undefined {
if (!value || typeof value !== "object" || Array.isArray(value))
return undefined;
return value as Record<string, unknown>;
}
function numericValue(value: unknown): number | undefined {
if (typeof value === "number" && Number.isFinite(value)) return value;
if (typeof value !== "string") return undefined;
const parsed = Number(value);
return Number.isFinite(parsed) ? parsed : undefined;
}
function costDetailsTotal(
costDetails: Record<string, unknown> | undefined,
): number | undefined {
if (!costDetails) return undefined;
const explicitTotal = numericValue(costDetails.total);
if (explicitTotal !== undefined) return explicitTotal;
let total = 0;
let hasCost = false;
for (const [key, value] of Object.entries(costDetails)) {
if (key === "total") continue;
const numeric = numericValue(value);
if (numeric === undefined) continue;
total += numeric;
hasCost = true;
}
return hasCost ? total : undefined;
}
function scoreSource(body: Record<string, unknown>): string {
const explicit = stringValue(body.source);
if (isScoreSource(explicit)) return explicit;
const metadata = body.metadata;
if (metadata && typeof metadata === "object" && !Array.isArray(metadata)) {
const metadataSource = stringValue(
(metadata as Record<string, unknown>).source,
);
if (isScoreSource(metadataSource)) return metadataSource;
}
return "API";
}
function isScoreSource(
value: string | undefined,
): value is "API" | "EVAL" | "ANNOTATION" {
return value === "API" || value === "EVAL" || value === "ANNOTATION";
}
function scoreValue(
value: unknown,
declaredDataType: string | undefined,
): {
numberValue: number | null;
stringValue: string | null;
dataType: string;
} {
if (typeof value === "number")
return {
numberValue: value,
stringValue: null,
dataType: declaredDataType ?? "NUMERIC",
};
if (typeof value === "boolean")
return {
numberValue: value ? 1 : 0,
stringValue: String(value),
dataType: declaredDataType ?? "BOOLEAN",
};
if (typeof value === "string") {
const numeric = Number(value);
if ((declaredDataType ?? "") === "NUMERIC" && !Number.isNaN(numeric)) {
return { numberValue: numeric, stringValue: value, dataType: "NUMERIC" };
}
return {
numberValue: null,
stringValue: value,
dataType: declaredDataType ?? "CATEGORICAL",
};
}
return {
numberValue: null,
stringValue: null,
dataType: declaredDataType ?? "NUMERIC",
};
}