@a5c-ai/agent-runtime 5.0.1-staging.016f0b0e8119
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/README.md +23 -0
- package/dist/apiResult.d.ts +19 -0
- package/dist/apiResult.d.ts.map +1 -0
- package/dist/apiResult.js +16 -0
- package/dist/background/state.d.ts +14 -0
- package/dist/background/state.d.ts.map +1 -0
- package/dist/background/state.js +25 -0
- package/dist/backgroundProcessRegistry.d.ts +66 -0
- package/dist/backgroundProcessRegistry.d.ts.map +1 -0
- package/dist/backgroundProcessRegistry.js +202 -0
- package/dist/cost/claudeCodeParser.d.ts +81 -0
- package/dist/cost/claudeCodeParser.d.ts.map +1 -0
- package/dist/cost/claudeCodeParser.js +232 -0
- package/dist/cost/collector.d.ts +42 -0
- package/dist/cost/collector.d.ts.map +1 -0
- package/dist/cost/collector.js +105 -0
- package/dist/cost/effectCost.d.ts +23 -0
- package/dist/cost/effectCost.d.ts.map +1 -0
- package/dist/cost/effectCost.js +26 -0
- package/dist/cost/index.d.ts +19 -0
- package/dist/cost/index.d.ts.map +1 -0
- package/dist/cost/index.js +39 -0
- package/dist/cost/journal.d.ts +40 -0
- package/dist/cost/journal.d.ts.map +1 -0
- package/dist/cost/journal.js +137 -0
- package/dist/cost/types.d.ts +164 -0
- package/dist/cost/types.d.ts.map +1 -0
- package/dist/cost/types.js +228 -0
- package/dist/daemon/automationExecutor.d.ts +16 -0
- package/dist/daemon/automationExecutor.d.ts.map +1 -0
- package/dist/daemon/automationExecutor.js +222 -0
- package/dist/daemon/config.d.ts +8 -0
- package/dist/daemon/config.d.ts.map +1 -0
- package/dist/daemon/config.js +209 -0
- package/dist/daemon/daemonLog.d.ts +13 -0
- package/dist/daemon/daemonLog.d.ts.map +1 -0
- package/dist/daemon/daemonLog.js +64 -0
- package/dist/daemon/fileWatcher.d.ts +9 -0
- package/dist/daemon/fileWatcher.d.ts.map +1 -0
- package/dist/daemon/fileWatcher.js +141 -0
- package/dist/daemon/index.d.ts +13 -0
- package/dist/daemon/index.d.ts.map +1 -0
- package/dist/daemon/index.js +22 -0
- package/dist/daemon/lifecycle.d.ts +12 -0
- package/dist/daemon/lifecycle.d.ts.map +1 -0
- package/dist/daemon/lifecycle.js +257 -0
- package/dist/daemon/loop.d.ts +21 -0
- package/dist/daemon/loop.d.ts.map +1 -0
- package/dist/daemon/loop.js +196 -0
- package/dist/daemon/timerScheduler.d.ts +13 -0
- package/dist/daemon/timerScheduler.d.ts.map +1 -0
- package/dist/daemon/timerScheduler.js +122 -0
- package/dist/daemon/types.d.ts +93 -0
- package/dist/daemon/types.d.ts.map +1 -0
- package/dist/daemon/types.js +25 -0
- package/dist/daemon/webhookListener.d.ts +6 -0
- package/dist/daemon/webhookListener.d.ts.map +1 -0
- package/dist/daemon/webhookListener.js +110 -0
- package/dist/execution/index.d.ts +8 -0
- package/dist/execution/index.d.ts.map +1 -0
- package/dist/execution/index.js +12 -0
- package/dist/execution/modes/docker.d.ts +21 -0
- package/dist/execution/modes/docker.d.ts.map +1 -0
- package/dist/execution/modes/docker.js +125 -0
- package/dist/execution/modes/index.d.ts +10 -0
- package/dist/execution/modes/index.d.ts.map +1 -0
- package/dist/execution/modes/index.js +14 -0
- package/dist/execution/modes/kubernetes.d.ts +29 -0
- package/dist/execution/modes/kubernetes.d.ts.map +1 -0
- package/dist/execution/modes/kubernetes.js +121 -0
- package/dist/execution/modes/local.d.ts +23 -0
- package/dist/execution/modes/local.d.ts.map +1 -0
- package/dist/execution/modes/local.js +102 -0
- package/dist/execution/modes/ssh.d.ts +23 -0
- package/dist/execution/modes/ssh.d.ts.map +1 -0
- package/dist/execution/modes/ssh.js +134 -0
- package/dist/execution/provider.d.ts +32 -0
- package/dist/execution/provider.d.ts.map +1 -0
- package/dist/execution/provider.js +90 -0
- package/dist/execution/types.d.ts +105 -0
- package/dist/execution/types.d.ts.map +1 -0
- package/dist/execution/types.js +9 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +42 -0
- package/dist/observability/health.d.ts +19 -0
- package/dist/observability/health.d.ts.map +1 -0
- package/dist/observability/health.js +129 -0
- package/dist/observability/index.d.ts +6 -0
- package/dist/observability/index.d.ts.map +1 -0
- package/dist/observability/index.js +20 -0
- package/dist/observability/runStatus.d.ts +44 -0
- package/dist/observability/runStatus.d.ts.map +1 -0
- package/dist/observability/runStatus.js +169 -0
- package/dist/observability/timeline.d.ts +11 -0
- package/dist/observability/timeline.d.ts.map +1 -0
- package/dist/observability/timeline.js +176 -0
- package/dist/observability/types.d.ts +62 -0
- package/dist/observability/types.d.ts.map +1 -0
- package/dist/observability/types.js +8 -0
- package/dist/observability/webhooks.d.ts +68 -0
- package/dist/observability/webhooks.d.ts.map +1 -0
- package/dist/observability/webhooks.js +132 -0
- package/dist/resources/budget-tracker.d.ts +56 -0
- package/dist/resources/budget-tracker.d.ts.map +1 -0
- package/dist/resources/budget-tracker.js +131 -0
- package/dist/resources/concurrency-guard.d.ts +55 -0
- package/dist/resources/concurrency-guard.d.ts.map +1 -0
- package/dist/resources/concurrency-guard.js +132 -0
- package/dist/resources/index.d.ts +12 -0
- package/dist/resources/index.d.ts.map +1 -0
- package/dist/resources/index.js +20 -0
- package/dist/resources/manager.d.ts +49 -0
- package/dist/resources/manager.d.ts.map +1 -0
- package/dist/resources/manager.js +111 -0
- package/dist/resources/timeout-cascade.d.ts +56 -0
- package/dist/resources/timeout-cascade.d.ts.map +1 -0
- package/dist/resources/timeout-cascade.js +145 -0
- package/dist/resources/types.d.ts +108 -0
- package/dist/resources/types.d.ts.map +1 -0
- package/dist/resources/types.js +9 -0
- package/dist/session/context.d.ts +22 -0
- package/dist/session/context.d.ts.map +1 -0
- package/dist/session/context.js +113 -0
- package/dist/session/continuityState.d.ts +39 -0
- package/dist/session/continuityState.d.ts.map +1 -0
- package/dist/session/continuityState.js +164 -0
- package/dist/session/cost.d.ts +63 -0
- package/dist/session/cost.d.ts.map +1 -0
- package/dist/session/cost.js +194 -0
- package/dist/session/discovery.d.ts +22 -0
- package/dist/session/discovery.d.ts.map +1 -0
- package/dist/session/discovery.js +35 -0
- package/dist/session/history.d.ts +30 -0
- package/dist/session/history.d.ts.map +1 -0
- package/dist/session/history.js +143 -0
- package/dist/session/index.d.ts +20 -0
- package/dist/session/index.d.ts.map +1 -0
- package/dist/session/index.js +78 -0
- package/dist/session/memoryExtraction.d.ts +65 -0
- package/dist/session/memoryExtraction.d.ts.map +1 -0
- package/dist/session/memoryExtraction.js +201 -0
- package/dist/session/parse.d.ts +45 -0
- package/dist/session/parse.d.ts.map +1 -0
- package/dist/session/parse.js +170 -0
- package/dist/session/persistence.d.ts +46 -0
- package/dist/session/persistence.d.ts.map +1 -0
- package/dist/session/persistence.js +180 -0
- package/dist/session/types.d.ts +267 -0
- package/dist/session/types.d.ts.map +1 -0
- package/dist/session/types.js +45 -0
- package/dist/session/write.d.ts +61 -0
- package/dist/session/write.d.ts.map +1 -0
- package/dist/session/write.js +213 -0
- package/dist/telemetry/audit-log.d.ts +56 -0
- package/dist/telemetry/audit-log.d.ts.map +1 -0
- package/dist/telemetry/audit-log.js +59 -0
- package/dist/telemetry/index.d.ts +9 -0
- package/dist/telemetry/index.d.ts.map +1 -0
- package/dist/telemetry/index.js +15 -0
- package/dist/telemetry/provider.d.ts +39 -0
- package/dist/telemetry/provider.d.ts.map +1 -0
- package/dist/telemetry/provider.js +91 -0
- package/dist/telemetry/span-tree.d.ts +46 -0
- package/dist/telemetry/span-tree.d.ts.map +1 -0
- package/dist/telemetry/span-tree.js +93 -0
- package/dist/telemetry/types.d.ts +85 -0
- package/dist/telemetry/types.d.ts.map +1 -0
- package/dist/telemetry/types.js +21 -0
- package/package.json +90 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Claude Code JSONL session file parser for cost data collection.
|
|
4
|
+
*
|
|
5
|
+
* Parses Claude Code session JSONL files (main session and subagent files)
|
|
6
|
+
* to extract token usage data and compute costs. Each assistant message with
|
|
7
|
+
* a `usage` block becomes a {@link CostEventData} entry.
|
|
8
|
+
*
|
|
9
|
+
* JSONL format (one JSON object per line):
|
|
10
|
+
* ```json
|
|
11
|
+
* {
|
|
12
|
+
* "type": "assistant",
|
|
13
|
+
* "message": {
|
|
14
|
+
* "model": "claude-opus-4-6",
|
|
15
|
+
* "usage": {
|
|
16
|
+
* "input_tokens": 3,
|
|
17
|
+
* "cache_creation_input_tokens": 26928,
|
|
18
|
+
* "cache_read_input_tokens": 0,
|
|
19
|
+
* "cache_creation": { "ephemeral_5m_input_tokens": 0, "ephemeral_1h_input_tokens": 26928 },
|
|
20
|
+
* "output_tokens": 28,
|
|
21
|
+
* "service_tier": "standard"
|
|
22
|
+
* }
|
|
23
|
+
* },
|
|
24
|
+
* "timestamp": "2026-04-05T..."
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
29
|
+
if (k2 === undefined) k2 = k;
|
|
30
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
31
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
32
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
33
|
+
}
|
|
34
|
+
Object.defineProperty(o, k2, desc);
|
|
35
|
+
}) : (function(o, m, k, k2) {
|
|
36
|
+
if (k2 === undefined) k2 = k;
|
|
37
|
+
o[k2] = m[k];
|
|
38
|
+
}));
|
|
39
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
40
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
41
|
+
}) : function(o, v) {
|
|
42
|
+
o["default"] = v;
|
|
43
|
+
});
|
|
44
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
45
|
+
var ownKeys = function(o) {
|
|
46
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
47
|
+
var ar = [];
|
|
48
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
49
|
+
return ar;
|
|
50
|
+
};
|
|
51
|
+
return ownKeys(o);
|
|
52
|
+
};
|
|
53
|
+
return function (mod) {
|
|
54
|
+
if (mod && mod.__esModule) return mod;
|
|
55
|
+
var result = {};
|
|
56
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
57
|
+
__setModuleDefault(result, mod);
|
|
58
|
+
return result;
|
|
59
|
+
};
|
|
60
|
+
})();
|
|
61
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
62
|
+
exports.parseClaudeCodeSession = parseClaudeCodeSession;
|
|
63
|
+
exports.parseClaudeCodeSessionWithSubagents = parseClaudeCodeSessionWithSubagents;
|
|
64
|
+
exports.aggregateUsageData = aggregateUsageData;
|
|
65
|
+
const node_fs_1 = require("node:fs");
|
|
66
|
+
const path = __importStar(require("node:path"));
|
|
67
|
+
const types_1 = require("./types");
|
|
68
|
+
// ============================================================================
|
|
69
|
+
// Single-file parser
|
|
70
|
+
// ============================================================================
|
|
71
|
+
/**
|
|
72
|
+
* Parse a single Claude Code session JSONL file into cost events.
|
|
73
|
+
*
|
|
74
|
+
* Reads the file, splits by newline, and for each line that represents an
|
|
75
|
+
* assistant message with usage data, extracts token counts and computes
|
|
76
|
+
* the USD cost using the extended pricing model.
|
|
77
|
+
*
|
|
78
|
+
* @param sessionJsonlPath - Absolute path to the `.jsonl` file.
|
|
79
|
+
* @returns Array of cost events extracted from assistant messages.
|
|
80
|
+
*/
|
|
81
|
+
async function parseClaudeCodeSession(sessionJsonlPath) {
|
|
82
|
+
let content;
|
|
83
|
+
try {
|
|
84
|
+
content = await node_fs_1.promises.readFile(sessionJsonlPath, "utf-8");
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
const e = err;
|
|
88
|
+
if (e.code === "ENOENT")
|
|
89
|
+
return [];
|
|
90
|
+
throw err;
|
|
91
|
+
}
|
|
92
|
+
const lines = content.split("\n");
|
|
93
|
+
const events = [];
|
|
94
|
+
for (const line of lines) {
|
|
95
|
+
const trimmed = line.trim();
|
|
96
|
+
if (!trimmed)
|
|
97
|
+
continue;
|
|
98
|
+
let entry;
|
|
99
|
+
try {
|
|
100
|
+
entry = JSON.parse(trimmed);
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
// Malformed line — skip silently.
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (entry.type !== "assistant")
|
|
107
|
+
continue;
|
|
108
|
+
const usage = entry.message?.usage;
|
|
109
|
+
if (!usage)
|
|
110
|
+
continue;
|
|
111
|
+
const model = entry.message?.model ?? "unknown";
|
|
112
|
+
const inputTokens = usage.input_tokens ?? 0;
|
|
113
|
+
const outputTokens = usage.output_tokens ?? 0;
|
|
114
|
+
const cacheCreationTokens = usage.cache_creation_input_tokens ?? 0;
|
|
115
|
+
const cacheReadTokens = usage.cache_read_input_tokens ?? 0;
|
|
116
|
+
const cacheCreation5mTokens = usage.cache_creation?.ephemeral_5m_input_tokens ?? 0;
|
|
117
|
+
const cacheCreation1hTokens = usage.cache_creation?.ephemeral_1h_input_tokens ?? 0;
|
|
118
|
+
const costUsd = (0, types_1.calculateCostUsdExtended)(model, inputTokens, outputTokens, cacheCreation5mTokens, cacheCreation1hTokens, cacheReadTokens);
|
|
119
|
+
events.push({
|
|
120
|
+
model,
|
|
121
|
+
inputTokens,
|
|
122
|
+
outputTokens,
|
|
123
|
+
cacheCreationTokens,
|
|
124
|
+
cacheReadTokens,
|
|
125
|
+
cacheCreation5mTokens,
|
|
126
|
+
cacheCreation1hTokens,
|
|
127
|
+
serviceTier: usage.service_tier,
|
|
128
|
+
costUsd: costUsd ?? undefined,
|
|
129
|
+
timestamp: entry.timestamp,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
return events;
|
|
133
|
+
}
|
|
134
|
+
// ============================================================================
|
|
135
|
+
// Session + subagent parser
|
|
136
|
+
// ============================================================================
|
|
137
|
+
/**
|
|
138
|
+
* Parse a Claude Code session JSONL file and all associated subagent files.
|
|
139
|
+
*
|
|
140
|
+
* Subagent files live at `<sessionDir>/subagents/agent-*.jsonl` where
|
|
141
|
+
* `<sessionDir>` is derived by stripping the `.jsonl` extension from the
|
|
142
|
+
* main session file path.
|
|
143
|
+
*
|
|
144
|
+
* @param sessionJsonlPath - Absolute path to the main session `.jsonl` file.
|
|
145
|
+
* @returns Merged array of cost events from the main session and all subagents.
|
|
146
|
+
*/
|
|
147
|
+
async function parseClaudeCodeSessionWithSubagents(sessionJsonlPath) {
|
|
148
|
+
// Parse main session file.
|
|
149
|
+
const mainEvents = await parseClaudeCodeSession(sessionJsonlPath);
|
|
150
|
+
// Derive the session directory (same name without .jsonl extension).
|
|
151
|
+
const sessionDir = sessionJsonlPath.replace(/\.jsonl$/, "");
|
|
152
|
+
const subagentsDir = path.join(sessionDir, "subagents");
|
|
153
|
+
let subagentFiles;
|
|
154
|
+
try {
|
|
155
|
+
const entries = await node_fs_1.promises.readdir(subagentsDir);
|
|
156
|
+
subagentFiles = entries
|
|
157
|
+
.filter((f) => f.startsWith("agent-") && f.endsWith(".jsonl"))
|
|
158
|
+
.map((f) => path.join(subagentsDir, f));
|
|
159
|
+
}
|
|
160
|
+
catch (err) {
|
|
161
|
+
const e = err;
|
|
162
|
+
if (e.code === "ENOENT")
|
|
163
|
+
return mainEvents;
|
|
164
|
+
throw err;
|
|
165
|
+
}
|
|
166
|
+
// Parse all subagent files and merge.
|
|
167
|
+
const subagentResults = await Promise.all(subagentFiles.map((f) => parseClaudeCodeSession(f)));
|
|
168
|
+
const allEvents = [...mainEvents];
|
|
169
|
+
for (const sub of subagentResults) {
|
|
170
|
+
allEvents.push(...sub);
|
|
171
|
+
}
|
|
172
|
+
return allEvents;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Aggregate usage data across an array of cost events.
|
|
176
|
+
*
|
|
177
|
+
* Sums token counts and costs, producing both overall totals and a
|
|
178
|
+
* per-model breakdown.
|
|
179
|
+
*
|
|
180
|
+
* @param events - Array of cost events (e.g. from {@link parseClaudeCodeSessionWithSubagents}).
|
|
181
|
+
* @returns Aggregated usage summary with per-model breakdown.
|
|
182
|
+
*/
|
|
183
|
+
function aggregateUsageData(events) {
|
|
184
|
+
const result = {
|
|
185
|
+
eventCount: 0,
|
|
186
|
+
totalInputTokens: 0,
|
|
187
|
+
totalOutputTokens: 0,
|
|
188
|
+
totalCacheCreationTokens: 0,
|
|
189
|
+
totalCacheReadTokens: 0,
|
|
190
|
+
totalCacheCreation5mTokens: 0,
|
|
191
|
+
totalCacheCreation1hTokens: 0,
|
|
192
|
+
totalCostUsd: 0,
|
|
193
|
+
byModel: {},
|
|
194
|
+
};
|
|
195
|
+
for (const event of events) {
|
|
196
|
+
result.eventCount += 1;
|
|
197
|
+
result.totalInputTokens += event.inputTokens;
|
|
198
|
+
result.totalOutputTokens += event.outputTokens;
|
|
199
|
+
result.totalCacheCreationTokens += event.cacheCreationTokens;
|
|
200
|
+
result.totalCacheReadTokens += event.cacheReadTokens;
|
|
201
|
+
result.totalCacheCreation5mTokens += event.cacheCreation5mTokens;
|
|
202
|
+
result.totalCacheCreation1hTokens += event.cacheCreation1hTokens;
|
|
203
|
+
result.totalCostUsd += event.costUsd ?? 0;
|
|
204
|
+
// Per-model breakdown.
|
|
205
|
+
const model = event.model ?? "unknown";
|
|
206
|
+
if (!result.byModel[model]) {
|
|
207
|
+
result.byModel[model] = {
|
|
208
|
+
model,
|
|
209
|
+
eventCount: 0,
|
|
210
|
+
inputTokens: 0,
|
|
211
|
+
outputTokens: 0,
|
|
212
|
+
cacheCreationTokens: 0,
|
|
213
|
+
cacheReadTokens: 0,
|
|
214
|
+
costUsd: 0,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
const ms = result.byModel[model];
|
|
218
|
+
ms.eventCount += 1;
|
|
219
|
+
ms.inputTokens += event.inputTokens;
|
|
220
|
+
ms.outputTokens += event.outputTokens;
|
|
221
|
+
ms.cacheCreationTokens += event.cacheCreationTokens;
|
|
222
|
+
ms.cacheReadTokens += event.cacheReadTokens;
|
|
223
|
+
ms.costUsd += event.costUsd ?? 0;
|
|
224
|
+
}
|
|
225
|
+
// Round totals to 6 decimal places to avoid floating-point drift.
|
|
226
|
+
result.totalCostUsd =
|
|
227
|
+
Math.round(result.totalCostUsd * 1_000_000) / 1_000_000;
|
|
228
|
+
for (const ms of Object.values(result.byModel)) {
|
|
229
|
+
ms.costUsd = Math.round(ms.costUsd * 1_000_000) / 1_000_000;
|
|
230
|
+
}
|
|
231
|
+
return result;
|
|
232
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cost data collector for Babysitter runs.
|
|
3
|
+
*
|
|
4
|
+
* Bridges the Claude Code JSONL parser with the run journal: parses a
|
|
5
|
+
* session file (including subagent files), then writes COST_TRACKED events
|
|
6
|
+
* to the run's journal for later aggregation and reporting.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Parse a Claude Code session (main + subagents) and write COST_TRACKED
|
|
10
|
+
* events into the run journal.
|
|
11
|
+
*
|
|
12
|
+
* Each assistant message with usage data becomes a separate journal event,
|
|
13
|
+
* preserving the full token breakdown for fine-grained cost analysis.
|
|
14
|
+
*
|
|
15
|
+
* @param runDir - Absolute path to the run directory (e.g. `.a5c/runs/<runId>`).
|
|
16
|
+
* @param sessionJsonlPath - Absolute path to the main session `.jsonl` file.
|
|
17
|
+
* @returns The number of COST_TRACKED events written to the journal.
|
|
18
|
+
*/
|
|
19
|
+
export declare function collectCostDataForRun(runDir: string, sessionJsonlPath: string): Promise<number>;
|
|
20
|
+
/**
|
|
21
|
+
* Resolve the Claude Code projects directory path for a given session ID.
|
|
22
|
+
*
|
|
23
|
+
* Claude Code stores session JSONL files at:
|
|
24
|
+
* ```
|
|
25
|
+
* ~/.claude/projects/<project-slug>/<sessionId>.jsonl
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* Since we don't always know the project slug, this function returns the
|
|
29
|
+
* base Claude Code projects directory. The caller can then search for the
|
|
30
|
+
* session file within the project subdirectories.
|
|
31
|
+
*
|
|
32
|
+
* If a workspace path is provided, the project slug is derived using Claude
|
|
33
|
+
* Code's path-encoding convention (forward slashes and colons replaced with
|
|
34
|
+
* dashes, prefixed with the drive letter on Windows).
|
|
35
|
+
*
|
|
36
|
+
* @param sessionId - The Claude Code session UUID.
|
|
37
|
+
* @param workspacePath - Optional absolute workspace path to derive the project slug.
|
|
38
|
+
* @returns Absolute path to the session `.jsonl` file (when workspace is
|
|
39
|
+
* provided) or the Claude Code projects directory (when workspace is omitted).
|
|
40
|
+
*/
|
|
41
|
+
export declare function resolveClaudeCodeSessionDir(sessionId: string, workspacePath?: string): string;
|
|
42
|
+
//# sourceMappingURL=collector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collector.d.ts","sourceRoot":"","sources":["../../src/cost/collector.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAQH;;;;;;;;;;GAUG;AACH,wBAAsB,qBAAqB,CACzC,MAAM,EAAE,MAAM,EACd,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,MAAM,CAAC,CAWjB;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,2BAA2B,CACzC,SAAS,EAAE,MAAM,EACjB,aAAa,CAAC,EAAE,MAAM,GACrB,MAAM,CAkBR"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Cost data collector for Babysitter runs.
|
|
4
|
+
*
|
|
5
|
+
* Bridges the Claude Code JSONL parser with the run journal: parses a
|
|
6
|
+
* session file (including subagent files), then writes COST_TRACKED events
|
|
7
|
+
* to the run's journal for later aggregation and reporting.
|
|
8
|
+
*/
|
|
9
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
12
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
13
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
14
|
+
}
|
|
15
|
+
Object.defineProperty(o, k2, desc);
|
|
16
|
+
}) : (function(o, m, k, k2) {
|
|
17
|
+
if (k2 === undefined) k2 = k;
|
|
18
|
+
o[k2] = m[k];
|
|
19
|
+
}));
|
|
20
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
21
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
22
|
+
}) : function(o, v) {
|
|
23
|
+
o["default"] = v;
|
|
24
|
+
});
|
|
25
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
26
|
+
var ownKeys = function(o) {
|
|
27
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
28
|
+
var ar = [];
|
|
29
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
30
|
+
return ar;
|
|
31
|
+
};
|
|
32
|
+
return ownKeys(o);
|
|
33
|
+
};
|
|
34
|
+
return function (mod) {
|
|
35
|
+
if (mod && mod.__esModule) return mod;
|
|
36
|
+
var result = {};
|
|
37
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
38
|
+
__setModuleDefault(result, mod);
|
|
39
|
+
return result;
|
|
40
|
+
};
|
|
41
|
+
})();
|
|
42
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
43
|
+
exports.collectCostDataForRun = collectCostDataForRun;
|
|
44
|
+
exports.resolveClaudeCodeSessionDir = resolveClaudeCodeSessionDir;
|
|
45
|
+
const path = __importStar(require("node:path"));
|
|
46
|
+
const os = __importStar(require("node:os"));
|
|
47
|
+
const claudeCodeParser_1 = require("./claudeCodeParser");
|
|
48
|
+
const journal_1 = require("./journal");
|
|
49
|
+
/**
|
|
50
|
+
* Parse a Claude Code session (main + subagents) and write COST_TRACKED
|
|
51
|
+
* events into the run journal.
|
|
52
|
+
*
|
|
53
|
+
* Each assistant message with usage data becomes a separate journal event,
|
|
54
|
+
* preserving the full token breakdown for fine-grained cost analysis.
|
|
55
|
+
*
|
|
56
|
+
* @param runDir - Absolute path to the run directory (e.g. `.a5c/runs/<runId>`).
|
|
57
|
+
* @param sessionJsonlPath - Absolute path to the main session `.jsonl` file.
|
|
58
|
+
* @returns The number of COST_TRACKED events written to the journal.
|
|
59
|
+
*/
|
|
60
|
+
async function collectCostDataForRun(runDir, sessionJsonlPath) {
|
|
61
|
+
const events = await (0, claudeCodeParser_1.parseClaudeCodeSessionWithSubagents)(sessionJsonlPath);
|
|
62
|
+
let written = 0;
|
|
63
|
+
for (const event of events) {
|
|
64
|
+
await (0, journal_1.appendCostEvent)(runDir, event);
|
|
65
|
+
written += 1;
|
|
66
|
+
}
|
|
67
|
+
return written;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Resolve the Claude Code projects directory path for a given session ID.
|
|
71
|
+
*
|
|
72
|
+
* Claude Code stores session JSONL files at:
|
|
73
|
+
* ```
|
|
74
|
+
* ~/.claude/projects/<project-slug>/<sessionId>.jsonl
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* Since we don't always know the project slug, this function returns the
|
|
78
|
+
* base Claude Code projects directory. The caller can then search for the
|
|
79
|
+
* session file within the project subdirectories.
|
|
80
|
+
*
|
|
81
|
+
* If a workspace path is provided, the project slug is derived using Claude
|
|
82
|
+
* Code's path-encoding convention (forward slashes and colons replaced with
|
|
83
|
+
* dashes, prefixed with the drive letter on Windows).
|
|
84
|
+
*
|
|
85
|
+
* @param sessionId - The Claude Code session UUID.
|
|
86
|
+
* @param workspacePath - Optional absolute workspace path to derive the project slug.
|
|
87
|
+
* @returns Absolute path to the session `.jsonl` file (when workspace is
|
|
88
|
+
* provided) or the Claude Code projects directory (when workspace is omitted).
|
|
89
|
+
*/
|
|
90
|
+
function resolveClaudeCodeSessionDir(sessionId, workspacePath) {
|
|
91
|
+
const homeDir = os.homedir();
|
|
92
|
+
const projectsDir = path.join(homeDir, ".claude", "projects");
|
|
93
|
+
if (!workspacePath) {
|
|
94
|
+
return projectsDir;
|
|
95
|
+
}
|
|
96
|
+
// Claude Code encodes the workspace path as the project directory name.
|
|
97
|
+
// On Windows: C:\Users\foo\project -> C--Users-foo-project
|
|
98
|
+
// The colon becomes a dash, backslashes become dashes (so :\ -> --)
|
|
99
|
+
// On Unix: /home/foo/project -> -home-foo-project
|
|
100
|
+
const projectSlug = workspacePath
|
|
101
|
+
.replace(/\\/g, "/") // Normalize to forward slashes.
|
|
102
|
+
.replace(/\//g, "-") // Replace path separators with dashes.
|
|
103
|
+
.replace(/:/g, "-"); // Replace colons with dashes (Windows drive).
|
|
104
|
+
return path.join(projectsDir, projectSlug, `${sessionId}.jsonl`);
|
|
105
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Status: NOT INTEGRATED YET
|
|
3
|
+
* Moved from @a5c-ai/babysitter-sdk.
|
|
4
|
+
* Per-effect cost aggregation (GAP-SUBOBS-003).
|
|
5
|
+
*/
|
|
6
|
+
import type { EffectIndex } from "@a5c-ai/babysitter-sdk";
|
|
7
|
+
export interface EffectCostSummary {
|
|
8
|
+
effectId: string;
|
|
9
|
+
taskId: string;
|
|
10
|
+
kind: string;
|
|
11
|
+
inputTokens: number;
|
|
12
|
+
outputTokens: number;
|
|
13
|
+
cacheCreationInputTokens: number;
|
|
14
|
+
cacheReadInputTokens: number;
|
|
15
|
+
costUsd: number;
|
|
16
|
+
model?: string;
|
|
17
|
+
}
|
|
18
|
+
export interface EffectCostResult {
|
|
19
|
+
effects: EffectCostSummary[];
|
|
20
|
+
totalCostUsd: number;
|
|
21
|
+
}
|
|
22
|
+
export declare function computeEffectCosts(index: EffectIndex): EffectCostResult;
|
|
23
|
+
//# sourceMappingURL=effectCost.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"effectCost.d.ts","sourceRoot":"","sources":["../../src/cost/effectCost.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAE1D,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,wBAAwB,EAAE,MAAM,CAAC;IACjC,oBAAoB,EAAE,MAAM,CAAC;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,iBAAiB,EAAE,CAAC;IAC7B,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,WAAW,GAAG,gBAAgB,CAwBvE"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.computeEffectCosts = computeEffectCosts;
|
|
4
|
+
function computeEffectCosts(index) {
|
|
5
|
+
const effects = [];
|
|
6
|
+
let totalCostUsd = 0;
|
|
7
|
+
for (const record of index.listEffects()) {
|
|
8
|
+
if (record.costUsd === undefined && record.inputTokens === undefined && record.outputTokens === undefined) {
|
|
9
|
+
continue; // No cost data for this effect
|
|
10
|
+
}
|
|
11
|
+
const summary = {
|
|
12
|
+
effectId: record.effectId,
|
|
13
|
+
taskId: record.taskId,
|
|
14
|
+
kind: record.kind ?? "unknown",
|
|
15
|
+
inputTokens: record.inputTokens ?? 0,
|
|
16
|
+
outputTokens: record.outputTokens ?? 0,
|
|
17
|
+
cacheCreationInputTokens: record.cacheCreationInputTokens ?? 0,
|
|
18
|
+
cacheReadInputTokens: record.cacheReadInputTokens ?? 0,
|
|
19
|
+
costUsd: record.costUsd ?? 0,
|
|
20
|
+
model: record.costModel,
|
|
21
|
+
};
|
|
22
|
+
effects.push(summary);
|
|
23
|
+
totalCostUsd += summary.costUsd;
|
|
24
|
+
}
|
|
25
|
+
return { effects, totalCostUsd };
|
|
26
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cost tracking module for Babysitter SDK.
|
|
3
|
+
*
|
|
4
|
+
* Provides type definitions, pricing constants, and cost calculation utilities
|
|
5
|
+
* for Anthropic Claude API token usage.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { calculateCostUsd, MODEL_PRICING } from '../cost';
|
|
10
|
+
*
|
|
11
|
+
* const cost = calculateCostUsd('claude-opus-4-6', 1000, 500, 200, 100);
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export { type CostEventData, type ModelCostStats, type KindCostStats, type RunCostStats, type AggregateCostStats, type ModelPricing, type ModelPricingExtended, MODEL_PRICING, MODEL_PRICING_EXTENDED, calculateCostUsd, calculateCostUsdExtended, type CostStatsOptions, } from "./types";
|
|
15
|
+
export { COST_TRACKED_EVENT_TYPE, appendCostEvent, extractCostEvents, computeRunCostStats, } from "./journal";
|
|
16
|
+
export { parseClaudeCodeSession, parseClaudeCodeSessionWithSubagents, aggregateUsageData, type AggregatedUsage, } from "./claudeCodeParser";
|
|
17
|
+
export { collectCostDataForRun, resolveClaudeCodeSessionDir, } from "./collector";
|
|
18
|
+
export { computeEffectCosts, type EffectCostSummary, type EffectCostResult, } from "./effectCost";
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cost/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EAEL,KAAK,aAAa,EAGlB,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,kBAAkB,EAGvB,KAAK,YAAY,EACjB,KAAK,oBAAoB,EACzB,aAAa,EACb,sBAAsB,EAGtB,gBAAgB,EAChB,wBAAwB,EAGxB,KAAK,gBAAgB,GACtB,MAAM,SAAS,CAAC;AAEjB,OAAO,EAEL,uBAAuB,EACvB,eAAe,EACf,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,WAAW,CAAC;AAEnB,OAAO,EAEL,sBAAsB,EACtB,mCAAmC,EACnC,kBAAkB,EAClB,KAAK,eAAe,GACrB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAEL,qBAAqB,EACrB,2BAA2B,GAC5B,MAAM,aAAa,CAAC;AAErB,OAAO,EACL,kBAAkB,EAClB,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,GACtB,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.computeEffectCosts = exports.resolveClaudeCodeSessionDir = exports.collectCostDataForRun = exports.aggregateUsageData = exports.parseClaudeCodeSessionWithSubagents = exports.parseClaudeCodeSession = exports.computeRunCostStats = exports.extractCostEvents = exports.appendCostEvent = exports.COST_TRACKED_EVENT_TYPE = exports.calculateCostUsdExtended = exports.calculateCostUsd = exports.MODEL_PRICING_EXTENDED = exports.MODEL_PRICING = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Cost tracking module for Babysitter SDK.
|
|
6
|
+
*
|
|
7
|
+
* Provides type definitions, pricing constants, and cost calculation utilities
|
|
8
|
+
* for Anthropic Claude API token usage.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { calculateCostUsd, MODEL_PRICING } from '../cost';
|
|
13
|
+
*
|
|
14
|
+
* const cost = calculateCostUsd('claude-opus-4-6', 1000, 500, 200, 100);
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
var types_1 = require("./types");
|
|
18
|
+
Object.defineProperty(exports, "MODEL_PRICING", { enumerable: true, get: function () { return types_1.MODEL_PRICING; } });
|
|
19
|
+
Object.defineProperty(exports, "MODEL_PRICING_EXTENDED", { enumerable: true, get: function () { return types_1.MODEL_PRICING_EXTENDED; } });
|
|
20
|
+
// Cost calculation
|
|
21
|
+
Object.defineProperty(exports, "calculateCostUsd", { enumerable: true, get: function () { return types_1.calculateCostUsd; } });
|
|
22
|
+
Object.defineProperty(exports, "calculateCostUsdExtended", { enumerable: true, get: function () { return types_1.calculateCostUsdExtended; } });
|
|
23
|
+
var journal_1 = require("./journal");
|
|
24
|
+
// Journal helpers
|
|
25
|
+
Object.defineProperty(exports, "COST_TRACKED_EVENT_TYPE", { enumerable: true, get: function () { return journal_1.COST_TRACKED_EVENT_TYPE; } });
|
|
26
|
+
Object.defineProperty(exports, "appendCostEvent", { enumerable: true, get: function () { return journal_1.appendCostEvent; } });
|
|
27
|
+
Object.defineProperty(exports, "extractCostEvents", { enumerable: true, get: function () { return journal_1.extractCostEvents; } });
|
|
28
|
+
Object.defineProperty(exports, "computeRunCostStats", { enumerable: true, get: function () { return journal_1.computeRunCostStats; } });
|
|
29
|
+
var claudeCodeParser_1 = require("./claudeCodeParser");
|
|
30
|
+
// Claude Code JSONL parser
|
|
31
|
+
Object.defineProperty(exports, "parseClaudeCodeSession", { enumerable: true, get: function () { return claudeCodeParser_1.parseClaudeCodeSession; } });
|
|
32
|
+
Object.defineProperty(exports, "parseClaudeCodeSessionWithSubagents", { enumerable: true, get: function () { return claudeCodeParser_1.parseClaudeCodeSessionWithSubagents; } });
|
|
33
|
+
Object.defineProperty(exports, "aggregateUsageData", { enumerable: true, get: function () { return claudeCodeParser_1.aggregateUsageData; } });
|
|
34
|
+
var collector_1 = require("./collector");
|
|
35
|
+
// Cost data collector
|
|
36
|
+
Object.defineProperty(exports, "collectCostDataForRun", { enumerable: true, get: function () { return collector_1.collectCostDataForRun; } });
|
|
37
|
+
Object.defineProperty(exports, "resolveClaudeCodeSessionDir", { enumerable: true, get: function () { return collector_1.resolveClaudeCodeSessionDir; } });
|
|
38
|
+
var effectCost_1 = require("./effectCost");
|
|
39
|
+
Object.defineProperty(exports, "computeEffectCosts", { enumerable: true, get: function () { return effectCost_1.computeEffectCosts; } });
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cost tracking journal helpers for Babysitter SDK.
|
|
3
|
+
*
|
|
4
|
+
* Provides functions to append COST_TRACKED events to the run journal,
|
|
5
|
+
* extract cost events from a journal, and compute aggregated cost statistics.
|
|
6
|
+
*/
|
|
7
|
+
import { type AppendEventResult, type JournalEvent } from "@a5c-ai/babysitter-sdk";
|
|
8
|
+
import type { CostEventData, RunCostStats } from "./types";
|
|
9
|
+
/** Journal event type constant for cost tracking events. */
|
|
10
|
+
export declare const COST_TRACKED_EVENT_TYPE = "COST_TRACKED";
|
|
11
|
+
/**
|
|
12
|
+
* Append a COST_TRACKED event to a run's journal.
|
|
13
|
+
*
|
|
14
|
+
* Computes the USD cost (if not already set) before persisting so that the
|
|
15
|
+
* journal record is self-contained.
|
|
16
|
+
*
|
|
17
|
+
* @param runDir - Absolute path to the run directory.
|
|
18
|
+
* @param costData - Token usage and metadata for the cost event.
|
|
19
|
+
* @returns The append result with seq, ulid, filename, checksum, path, and recordedAt.
|
|
20
|
+
*/
|
|
21
|
+
export declare function appendCostEvent(runDir: string, costData: CostEventData): Promise<AppendEventResult>;
|
|
22
|
+
/**
|
|
23
|
+
* Filter journal events to only COST_TRACKED entries.
|
|
24
|
+
*
|
|
25
|
+
* @param events - Full journal event list (as returned by `loadJournal`).
|
|
26
|
+
* @returns Subset of events whose type is COST_TRACKED.
|
|
27
|
+
*/
|
|
28
|
+
export declare function extractCostEvents(events: JournalEvent[]): JournalEvent[];
|
|
29
|
+
/**
|
|
30
|
+
* Compute aggregated cost statistics for a run from its journal events.
|
|
31
|
+
*
|
|
32
|
+
* Extracts COST_TRACKED events, then accumulates totals with per-model and
|
|
33
|
+
* per-task-kind breakdowns.
|
|
34
|
+
*
|
|
35
|
+
* @param runId - The run identifier (included in the returned stats object).
|
|
36
|
+
* @param events - Full journal event list for the run.
|
|
37
|
+
* @returns Aggregated cost stats for the run.
|
|
38
|
+
*/
|
|
39
|
+
export declare function computeRunCostStats(runId: string, events: JournalEvent[]): RunCostStats;
|
|
40
|
+
//# sourceMappingURL=journal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"journal.d.ts","sourceRoot":"","sources":["../../src/cost/journal.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAe,KAAK,iBAAiB,EAAE,KAAK,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAChG,OAAO,KAAK,EACV,aAAa,EACb,YAAY,EAGb,MAAM,SAAS,CAAC;AAGjB,4DAA4D;AAC5D,eAAO,MAAM,uBAAuB,iBAAiB,CAAC;AAEtD;;;;;;;;;GASG;AACH,wBAAsB,eAAe,CACnC,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,aAAa,GACtB,OAAO,CAAC,iBAAiB,CAAC,CA0B5B;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,YAAY,EAAE,GAAG,YAAY,EAAE,CAExE;AAED;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,YAAY,EAAE,GACrB,YAAY,CAwFd"}
|