@ramarivera/coding-agent-langfuse 0.1.12 → 0.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/backfill.js +26 -15
- package/package.json +1 -1
package/dist/backfill.js
CHANGED
|
@@ -5,7 +5,7 @@ import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSy
|
|
|
5
5
|
import { homedir } from "node:os";
|
|
6
6
|
import { dirname, join } from "node:path";
|
|
7
7
|
const allAgents = ["claude", "codex", "grok", "opencode", "pi"];
|
|
8
|
-
const importIdentityVersion = "
|
|
8
|
+
const importIdentityVersion = "v7-host-independent-session-traces";
|
|
9
9
|
const defaultEndpoint = "https://langfuse.ai.roxasroot.net/otel/v1/traces";
|
|
10
10
|
const deadRemoteEndpoint = "http://langfuse.ai.roxasroot.net:14318/v1/traces";
|
|
11
11
|
const defaultStatePath = join(homedir(), ".local/state/coding-agent-langfuse/backfill-v6.json");
|
|
@@ -311,21 +311,23 @@ function pricingForModel(model) {
|
|
|
311
311
|
function costDetails(usage, model) {
|
|
312
312
|
if (!usage)
|
|
313
313
|
return undefined;
|
|
314
|
+
const rates = pricingForModel(model);
|
|
315
|
+
if (rates) {
|
|
316
|
+
const input = ((usage.input ?? 0) * rates.input) / 1_000_000;
|
|
317
|
+
const output = (((usage.output ?? 0) + (usage.reasoning ?? 0)) * rates.output) /
|
|
318
|
+
1_000_000;
|
|
319
|
+
const cache_read = ((usage.cacheRead ?? 0) * rates.cacheRead) / 1_000_000;
|
|
320
|
+
const cache_write = ((usage.cacheWrite ?? 0) * rates.cacheWrite) /
|
|
321
|
+
1_000_000;
|
|
322
|
+
const total = input + output + cache_read + cache_write;
|
|
323
|
+
if (total > 0) {
|
|
324
|
+
return { input, output, cache_read, cache_write, total, source: "estimated" };
|
|
325
|
+
}
|
|
326
|
+
}
|
|
314
327
|
if (usage.cost !== undefined && usage.cost > 0) {
|
|
315
328
|
return { total: usage.cost, source: "recorded" };
|
|
316
329
|
}
|
|
317
|
-
|
|
318
|
-
if (!rates)
|
|
319
|
-
return undefined;
|
|
320
|
-
const input = ((usage.input ?? 0) * rates.input) / 1_000_000;
|
|
321
|
-
const output = (((usage.output ?? 0) + (usage.reasoning ?? 0)) * rates.output) /
|
|
322
|
-
1_000_000;
|
|
323
|
-
const cache_read = ((usage.cacheRead ?? 0) * rates.cacheRead) / 1_000_000;
|
|
324
|
-
const cache_write = ((usage.cacheWrite ?? 0) * rates.cacheWrite) / 1_000_000;
|
|
325
|
-
const total = input + output + cache_read + cache_write;
|
|
326
|
-
if (total <= 0)
|
|
327
|
-
return undefined;
|
|
328
|
-
return { input, output, cache_read, cache_write, total, source: "estimated" };
|
|
330
|
+
return undefined;
|
|
329
331
|
}
|
|
330
332
|
function isGenerationEvent(event) {
|
|
331
333
|
return event.usage !== undefined && event.role !== "user";
|
|
@@ -343,6 +345,7 @@ function codexEvents(homeDir) {
|
|
|
343
345
|
asString(getPath(payload, ["model"]));
|
|
344
346
|
let currentModel = model;
|
|
345
347
|
let currentCwd = cwd;
|
|
348
|
+
const seenTokenCounts = new Set();
|
|
346
349
|
const events = [
|
|
347
350
|
{
|
|
348
351
|
agent: "codex",
|
|
@@ -418,6 +421,14 @@ function codexEvents(homeDir) {
|
|
|
418
421
|
const info = asRecord(rowPayload.info);
|
|
419
422
|
const usage = normalizeUsage(asRecord(info.last_token_usage)) ??
|
|
420
423
|
normalizeUsage(asRecord(info.total_token_usage));
|
|
424
|
+
const tokenKey = JSON.stringify({
|
|
425
|
+
timestamp,
|
|
426
|
+
model: asString(info.model) ?? currentModel,
|
|
427
|
+
usage,
|
|
428
|
+
});
|
|
429
|
+
if (seenTokenCounts.has(tokenKey))
|
|
430
|
+
continue;
|
|
431
|
+
seenTokenCounts.add(tokenKey);
|
|
421
432
|
events.push({
|
|
422
433
|
agent: "codex",
|
|
423
434
|
sourcePath: path,
|
|
@@ -738,10 +749,10 @@ function stableId(input) {
|
|
|
738
749
|
return createHash("sha256").update(input).digest("hex").slice(0, 32);
|
|
739
750
|
}
|
|
740
751
|
function fingerprint(event) {
|
|
741
|
-
return `${importIdentityVersion}:${event.agent}:${event.
|
|
752
|
+
return `${importIdentityVersion}:${event.agent}:${event.sessionId}:${event.recordId}`;
|
|
742
753
|
}
|
|
743
754
|
function traceFingerprint(event) {
|
|
744
|
-
return `${importIdentityVersion}:${event.agent}:${event.
|
|
755
|
+
return `${importIdentityVersion}:${event.agent}:${event.sessionId}`;
|
|
745
756
|
}
|
|
746
757
|
function traceId(event) {
|
|
747
758
|
return stableId(traceFingerprint(event));
|