@compilr-dev/agents 0.0.1
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 +1277 -0
- package/dist/agent.d.ts +1272 -0
- package/dist/agent.js +1912 -0
- package/dist/anchors/builtin.d.ts +24 -0
- package/dist/anchors/builtin.js +61 -0
- package/dist/anchors/index.d.ts +6 -0
- package/dist/anchors/index.js +5 -0
- package/dist/anchors/manager.d.ts +115 -0
- package/dist/anchors/manager.js +412 -0
- package/dist/anchors/types.d.ts +168 -0
- package/dist/anchors/types.js +10 -0
- package/dist/context/index.d.ts +12 -0
- package/dist/context/index.js +10 -0
- package/dist/context/manager.d.ts +224 -0
- package/dist/context/manager.js +770 -0
- package/dist/context/types.d.ts +377 -0
- package/dist/context/types.js +7 -0
- package/dist/costs/index.d.ts +8 -0
- package/dist/costs/index.js +7 -0
- package/dist/costs/tracker.d.ts +121 -0
- package/dist/costs/tracker.js +295 -0
- package/dist/costs/types.d.ts +157 -0
- package/dist/costs/types.js +8 -0
- package/dist/errors.d.ts +178 -0
- package/dist/errors.js +249 -0
- package/dist/guardrails/builtin.d.ts +27 -0
- package/dist/guardrails/builtin.js +223 -0
- package/dist/guardrails/index.d.ts +6 -0
- package/dist/guardrails/index.js +5 -0
- package/dist/guardrails/manager.d.ts +117 -0
- package/dist/guardrails/manager.js +288 -0
- package/dist/guardrails/types.d.ts +159 -0
- package/dist/guardrails/types.js +7 -0
- package/dist/hooks/index.d.ts +31 -0
- package/dist/hooks/index.js +29 -0
- package/dist/hooks/manager.d.ts +147 -0
- package/dist/hooks/manager.js +600 -0
- package/dist/hooks/types.d.ts +368 -0
- package/dist/hooks/types.js +12 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.js +73 -0
- package/dist/mcp/client.d.ts +93 -0
- package/dist/mcp/client.js +287 -0
- package/dist/mcp/errors.d.ts +60 -0
- package/dist/mcp/errors.js +78 -0
- package/dist/mcp/index.d.ts +43 -0
- package/dist/mcp/index.js +45 -0
- package/dist/mcp/manager.d.ts +120 -0
- package/dist/mcp/manager.js +276 -0
- package/dist/mcp/tools.d.ts +54 -0
- package/dist/mcp/tools.js +99 -0
- package/dist/mcp/types.d.ts +150 -0
- package/dist/mcp/types.js +40 -0
- package/dist/memory/index.d.ts +8 -0
- package/dist/memory/index.js +7 -0
- package/dist/memory/loader.d.ts +114 -0
- package/dist/memory/loader.js +463 -0
- package/dist/memory/types.d.ts +182 -0
- package/dist/memory/types.js +8 -0
- package/dist/messages/index.d.ts +82 -0
- package/dist/messages/index.js +155 -0
- package/dist/permissions/index.d.ts +5 -0
- package/dist/permissions/index.js +4 -0
- package/dist/permissions/manager.d.ts +125 -0
- package/dist/permissions/manager.js +379 -0
- package/dist/permissions/types.d.ts +162 -0
- package/dist/permissions/types.js +7 -0
- package/dist/providers/claude.d.ts +90 -0
- package/dist/providers/claude.js +348 -0
- package/dist/providers/index.d.ts +8 -0
- package/dist/providers/index.js +11 -0
- package/dist/providers/mock.d.ts +133 -0
- package/dist/providers/mock.js +204 -0
- package/dist/providers/types.d.ts +168 -0
- package/dist/providers/types.js +4 -0
- package/dist/rate-limit/index.d.ts +45 -0
- package/dist/rate-limit/index.js +47 -0
- package/dist/rate-limit/limiter.d.ts +104 -0
- package/dist/rate-limit/limiter.js +326 -0
- package/dist/rate-limit/provider-wrapper.d.ts +112 -0
- package/dist/rate-limit/provider-wrapper.js +201 -0
- package/dist/rate-limit/retry.d.ts +108 -0
- package/dist/rate-limit/retry.js +287 -0
- package/dist/rate-limit/types.d.ts +181 -0
- package/dist/rate-limit/types.js +22 -0
- package/dist/rehearsal/file-analyzer.d.ts +22 -0
- package/dist/rehearsal/file-analyzer.js +351 -0
- package/dist/rehearsal/git-analyzer.d.ts +22 -0
- package/dist/rehearsal/git-analyzer.js +472 -0
- package/dist/rehearsal/index.d.ts +35 -0
- package/dist/rehearsal/index.js +36 -0
- package/dist/rehearsal/manager.d.ts +100 -0
- package/dist/rehearsal/manager.js +290 -0
- package/dist/rehearsal/types.d.ts +235 -0
- package/dist/rehearsal/types.js +8 -0
- package/dist/skills/index.d.ts +160 -0
- package/dist/skills/index.js +282 -0
- package/dist/state/agent-state.d.ts +41 -0
- package/dist/state/agent-state.js +88 -0
- package/dist/state/checkpointer.d.ts +110 -0
- package/dist/state/checkpointer.js +362 -0
- package/dist/state/errors.d.ts +66 -0
- package/dist/state/errors.js +88 -0
- package/dist/state/index.d.ts +35 -0
- package/dist/state/index.js +37 -0
- package/dist/state/serializer.d.ts +55 -0
- package/dist/state/serializer.js +172 -0
- package/dist/state/types.d.ts +312 -0
- package/dist/state/types.js +14 -0
- package/dist/tools/builtin/bash-output.d.ts +61 -0
- package/dist/tools/builtin/bash-output.js +90 -0
- package/dist/tools/builtin/bash.d.ts +150 -0
- package/dist/tools/builtin/bash.js +354 -0
- package/dist/tools/builtin/edit.d.ts +50 -0
- package/dist/tools/builtin/edit.js +215 -0
- package/dist/tools/builtin/glob.d.ts +62 -0
- package/dist/tools/builtin/glob.js +244 -0
- package/dist/tools/builtin/grep.d.ts +74 -0
- package/dist/tools/builtin/grep.js +363 -0
- package/dist/tools/builtin/index.d.ts +44 -0
- package/dist/tools/builtin/index.js +69 -0
- package/dist/tools/builtin/kill-shell.d.ts +44 -0
- package/dist/tools/builtin/kill-shell.js +80 -0
- package/dist/tools/builtin/read-file.d.ts +57 -0
- package/dist/tools/builtin/read-file.js +184 -0
- package/dist/tools/builtin/shell-manager.d.ts +176 -0
- package/dist/tools/builtin/shell-manager.js +337 -0
- package/dist/tools/builtin/task.d.ts +202 -0
- package/dist/tools/builtin/task.js +350 -0
- package/dist/tools/builtin/todo.d.ts +207 -0
- package/dist/tools/builtin/todo.js +453 -0
- package/dist/tools/builtin/utils.d.ts +27 -0
- package/dist/tools/builtin/utils.js +70 -0
- package/dist/tools/builtin/web-fetch.d.ts +96 -0
- package/dist/tools/builtin/web-fetch.js +290 -0
- package/dist/tools/builtin/write-file.d.ts +54 -0
- package/dist/tools/builtin/write-file.js +147 -0
- package/dist/tools/define.d.ts +60 -0
- package/dist/tools/define.js +65 -0
- package/dist/tools/index.d.ts +10 -0
- package/dist/tools/index.js +37 -0
- package/dist/tools/registry.d.ts +79 -0
- package/dist/tools/registry.js +151 -0
- package/dist/tools/types.d.ts +59 -0
- package/dist/tools/types.js +4 -0
- package/dist/tracing/hooks.d.ts +58 -0
- package/dist/tracing/hooks.js +377 -0
- package/dist/tracing/index.d.ts +51 -0
- package/dist/tracing/index.js +55 -0
- package/dist/tracing/logging.d.ts +78 -0
- package/dist/tracing/logging.js +310 -0
- package/dist/tracing/manager.d.ts +160 -0
- package/dist/tracing/manager.js +468 -0
- package/dist/tracing/otel.d.ts +102 -0
- package/dist/tracing/otel.js +246 -0
- package/dist/tracing/types.d.ts +346 -0
- package/dist/tracing/types.js +38 -0
- package/dist/utils/index.d.ts +23 -0
- package/dist/utils/index.js +44 -0
- package/package.json +79 -0
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context Management Types
|
|
3
|
+
*
|
|
4
|
+
* Types for managing agent context windows, including token tracking,
|
|
5
|
+
* compaction, summarization, and filtering.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Context categories for budget allocation
|
|
9
|
+
*
|
|
10
|
+
* Each category has its own budget, preventing one type of content
|
|
11
|
+
* from consuming space meant for another.
|
|
12
|
+
*/
|
|
13
|
+
export type ContextCategory = 'system' | 'recentMessages' | 'toolResults' | 'history';
|
|
14
|
+
/**
|
|
15
|
+
* Budget allocation percentages for each category
|
|
16
|
+
* Must sum to 1.0 (100%)
|
|
17
|
+
*/
|
|
18
|
+
export interface BudgetAllocation {
|
|
19
|
+
/**
|
|
20
|
+
* System prompt allocation
|
|
21
|
+
* @default 0.05 (5%)
|
|
22
|
+
*/
|
|
23
|
+
system: number;
|
|
24
|
+
/**
|
|
25
|
+
* Recent messages (last N turns) allocation
|
|
26
|
+
* @default 0.40 (40%)
|
|
27
|
+
*/
|
|
28
|
+
recentMessages: number;
|
|
29
|
+
/**
|
|
30
|
+
* Tool results allocation
|
|
31
|
+
* @default 0.25 (25%)
|
|
32
|
+
*/
|
|
33
|
+
toolResults: number;
|
|
34
|
+
/**
|
|
35
|
+
* Older conversation history allocation
|
|
36
|
+
* @default 0.30 (30%)
|
|
37
|
+
*/
|
|
38
|
+
history: number;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Budget information for a single category
|
|
42
|
+
*/
|
|
43
|
+
export interface CategoryBudgetInfo {
|
|
44
|
+
/**
|
|
45
|
+
* Allocation fraction (0.0 - 1.0)
|
|
46
|
+
*/
|
|
47
|
+
allocated: number;
|
|
48
|
+
/**
|
|
49
|
+
* Allocated tokens based on maxContextTokens
|
|
50
|
+
*/
|
|
51
|
+
allocatedTokens: number;
|
|
52
|
+
/**
|
|
53
|
+
* Currently used tokens in this category
|
|
54
|
+
*/
|
|
55
|
+
used: number;
|
|
56
|
+
/**
|
|
57
|
+
* Remaining tokens available
|
|
58
|
+
*/
|
|
59
|
+
remaining: number;
|
|
60
|
+
/**
|
|
61
|
+
* Utilization of this category (0.0 - 1.0)
|
|
62
|
+
*/
|
|
63
|
+
utilization: number;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Result of a pre-flight context check
|
|
67
|
+
*
|
|
68
|
+
* Call canAddContent() before adding content to check if action is needed.
|
|
69
|
+
*/
|
|
70
|
+
export interface PreflightResult {
|
|
71
|
+
/**
|
|
72
|
+
* Whether the content can be added without issues
|
|
73
|
+
*/
|
|
74
|
+
allowed: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Whether an action is required before adding
|
|
77
|
+
*/
|
|
78
|
+
requiresAction: boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Recommended action to take
|
|
81
|
+
*/
|
|
82
|
+
action?: 'compact' | 'summarize' | 'reject';
|
|
83
|
+
/**
|
|
84
|
+
* Which category needs action (for compact)
|
|
85
|
+
*/
|
|
86
|
+
category?: ContextCategory;
|
|
87
|
+
/**
|
|
88
|
+
* Estimated tokens for the content to add
|
|
89
|
+
*/
|
|
90
|
+
estimatedTokens: number;
|
|
91
|
+
/**
|
|
92
|
+
* Tokens remaining in the target category budget
|
|
93
|
+
*/
|
|
94
|
+
budgetRemaining: number;
|
|
95
|
+
/**
|
|
96
|
+
* Human-readable recommendation
|
|
97
|
+
*/
|
|
98
|
+
recommendation?: string;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Verbosity levels for graceful degradation
|
|
102
|
+
*
|
|
103
|
+
* Tools should adapt their output based on context pressure.
|
|
104
|
+
*/
|
|
105
|
+
export type VerbosityLevel = 'full' | 'normal' | 'abbreviated' | 'minimal';
|
|
106
|
+
/**
|
|
107
|
+
* Verbosity threshold configuration
|
|
108
|
+
*/
|
|
109
|
+
export interface VerbosityConfig {
|
|
110
|
+
/**
|
|
111
|
+
* Utilization threshold for 'full' verbosity (below this)
|
|
112
|
+
* @default 0.50 (50%)
|
|
113
|
+
*/
|
|
114
|
+
fullThreshold: number;
|
|
115
|
+
/**
|
|
116
|
+
* Utilization threshold for 'normal' verbosity (below this)
|
|
117
|
+
* @default 0.80 (80%)
|
|
118
|
+
*/
|
|
119
|
+
normalThreshold: number;
|
|
120
|
+
/**
|
|
121
|
+
* Utilization threshold for 'abbreviated' verbosity (below this)
|
|
122
|
+
* @default 0.95 (95%)
|
|
123
|
+
*/
|
|
124
|
+
abbreviatedThreshold: number;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Context management configuration
|
|
128
|
+
*/
|
|
129
|
+
export interface ContextConfig {
|
|
130
|
+
/**
|
|
131
|
+
* Maximum tokens for the context window
|
|
132
|
+
* @default 200000 (Claude's limit)
|
|
133
|
+
*/
|
|
134
|
+
maxContextTokens: number;
|
|
135
|
+
/**
|
|
136
|
+
* Budget allocation for each category
|
|
137
|
+
* Enables category-specific compaction
|
|
138
|
+
*/
|
|
139
|
+
budget: BudgetAllocation;
|
|
140
|
+
/**
|
|
141
|
+
* Verbosity level thresholds
|
|
142
|
+
* Controls graceful degradation
|
|
143
|
+
*/
|
|
144
|
+
verbosity: VerbosityConfig;
|
|
145
|
+
/**
|
|
146
|
+
* Filtering configuration
|
|
147
|
+
*/
|
|
148
|
+
filtering: FilteringConfig;
|
|
149
|
+
/**
|
|
150
|
+
* Compaction configuration
|
|
151
|
+
*/
|
|
152
|
+
compaction: CompactionConfig;
|
|
153
|
+
/**
|
|
154
|
+
* Summarization configuration
|
|
155
|
+
*/
|
|
156
|
+
summarization: SummarizationConfig;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Filtering configuration - applied before adding to context
|
|
160
|
+
*/
|
|
161
|
+
export interface FilteringConfig {
|
|
162
|
+
/**
|
|
163
|
+
* Maximum tokens for a single tool result
|
|
164
|
+
* Results larger than this will be truncated
|
|
165
|
+
* @default 80000
|
|
166
|
+
*/
|
|
167
|
+
maxToolResultTokens: number;
|
|
168
|
+
/**
|
|
169
|
+
* Maximum lines for file content
|
|
170
|
+
* @default 500
|
|
171
|
+
*/
|
|
172
|
+
maxFileLines: number;
|
|
173
|
+
/**
|
|
174
|
+
* Maximum lines for error traces
|
|
175
|
+
* @default 50
|
|
176
|
+
*/
|
|
177
|
+
maxErrorLines: number;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Compaction configuration - replaces old content with file references
|
|
181
|
+
*/
|
|
182
|
+
export interface CompactionConfig {
|
|
183
|
+
/**
|
|
184
|
+
* Trigger compaction every N turns
|
|
185
|
+
* @default 20
|
|
186
|
+
*/
|
|
187
|
+
triggerInterval: number;
|
|
188
|
+
/**
|
|
189
|
+
* Trigger compaction when context utilization exceeds this threshold
|
|
190
|
+
* @default 0.5 (50%)
|
|
191
|
+
*/
|
|
192
|
+
triggerThreshold: number;
|
|
193
|
+
/**
|
|
194
|
+
* Number of recent turns to preserve (not compact)
|
|
195
|
+
* @default 10
|
|
196
|
+
*/
|
|
197
|
+
preserveRecentTurns: number;
|
|
198
|
+
/**
|
|
199
|
+
* Minimum tokens in a message to consider for compaction
|
|
200
|
+
* @default 1000
|
|
201
|
+
*/
|
|
202
|
+
minTokensToCompact: number;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Summarization configuration - compresses entire history
|
|
206
|
+
*/
|
|
207
|
+
export interface SummarizationConfig {
|
|
208
|
+
/**
|
|
209
|
+
* Emit warning when context utilization exceeds this threshold
|
|
210
|
+
* @default 0.80 (80%)
|
|
211
|
+
*/
|
|
212
|
+
warningThreshold: number;
|
|
213
|
+
/**
|
|
214
|
+
* Trigger normal summarization when context utilization exceeds this threshold
|
|
215
|
+
* @default 0.90 (90%)
|
|
216
|
+
*/
|
|
217
|
+
triggerThreshold: number;
|
|
218
|
+
/**
|
|
219
|
+
* Trigger emergency summarization (fewer preserved messages)
|
|
220
|
+
* @default 0.95 (95%)
|
|
221
|
+
*/
|
|
222
|
+
emergencyThreshold: number;
|
|
223
|
+
/**
|
|
224
|
+
* Number of recent messages to preserve in normal mode
|
|
225
|
+
* @default 6
|
|
226
|
+
*/
|
|
227
|
+
preserveRecentMessages: number;
|
|
228
|
+
/**
|
|
229
|
+
* Number of recent messages to preserve in emergency mode
|
|
230
|
+
* @default 4
|
|
231
|
+
*/
|
|
232
|
+
emergencyPreserveMessages: number;
|
|
233
|
+
/**
|
|
234
|
+
* Maximum tokens for the summary
|
|
235
|
+
* @default 2000
|
|
236
|
+
*/
|
|
237
|
+
summaryMaxTokens: number;
|
|
238
|
+
/**
|
|
239
|
+
* Maximum summarization rounds before throwing error
|
|
240
|
+
* @default 3
|
|
241
|
+
*/
|
|
242
|
+
maxRounds: number;
|
|
243
|
+
/**
|
|
244
|
+
* Target utilization after summarization
|
|
245
|
+
* @default 0.70 (70%)
|
|
246
|
+
*/
|
|
247
|
+
targetUtilization: number;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Result of a compaction operation
|
|
251
|
+
*/
|
|
252
|
+
export interface CompactionResult {
|
|
253
|
+
/**
|
|
254
|
+
* Number of messages before compaction
|
|
255
|
+
*/
|
|
256
|
+
messagesBefore: number;
|
|
257
|
+
/**
|
|
258
|
+
* Number of messages after compaction
|
|
259
|
+
*/
|
|
260
|
+
messagesAfter: number;
|
|
261
|
+
/**
|
|
262
|
+
* Total tokens before compaction
|
|
263
|
+
*/
|
|
264
|
+
tokensBefore: number;
|
|
265
|
+
/**
|
|
266
|
+
* Total tokens after compaction
|
|
267
|
+
*/
|
|
268
|
+
tokensAfter: number;
|
|
269
|
+
/**
|
|
270
|
+
* Files created during compaction (for reversibility)
|
|
271
|
+
*/
|
|
272
|
+
filesCreated: string[];
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Result of a summarization operation
|
|
276
|
+
*/
|
|
277
|
+
export interface SummarizationResult {
|
|
278
|
+
/**
|
|
279
|
+
* Tokens in original conversation
|
|
280
|
+
*/
|
|
281
|
+
originalTokens: number;
|
|
282
|
+
/**
|
|
283
|
+
* Tokens in summary
|
|
284
|
+
*/
|
|
285
|
+
summaryTokens: number;
|
|
286
|
+
/**
|
|
287
|
+
* Number of messages preserved (not summarized)
|
|
288
|
+
*/
|
|
289
|
+
messagesPreserved: number;
|
|
290
|
+
/**
|
|
291
|
+
* The generated summary text
|
|
292
|
+
*/
|
|
293
|
+
summary: string;
|
|
294
|
+
/**
|
|
295
|
+
* Number of summarization rounds performed
|
|
296
|
+
*/
|
|
297
|
+
rounds: number;
|
|
298
|
+
/**
|
|
299
|
+
* Whether emergency mode was used
|
|
300
|
+
*/
|
|
301
|
+
emergency: boolean;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Result of a filtering operation
|
|
305
|
+
*/
|
|
306
|
+
export interface FilteringResult {
|
|
307
|
+
/**
|
|
308
|
+
* Whether the content was filtered
|
|
309
|
+
*/
|
|
310
|
+
filtered: boolean;
|
|
311
|
+
/**
|
|
312
|
+
* Original token count
|
|
313
|
+
*/
|
|
314
|
+
originalTokens: number;
|
|
315
|
+
/**
|
|
316
|
+
* Token count after filtering
|
|
317
|
+
*/
|
|
318
|
+
filteredTokens: number;
|
|
319
|
+
/**
|
|
320
|
+
* Path to file where full content was saved (if filtered)
|
|
321
|
+
*/
|
|
322
|
+
savedToFile?: string;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Context-related events
|
|
326
|
+
*/
|
|
327
|
+
export type ContextEvent = {
|
|
328
|
+
type: 'context_warning';
|
|
329
|
+
utilization: number;
|
|
330
|
+
threshold: number;
|
|
331
|
+
} | {
|
|
332
|
+
type: 'context_compacted';
|
|
333
|
+
result: CompactionResult;
|
|
334
|
+
} | {
|
|
335
|
+
type: 'context_summarized';
|
|
336
|
+
result: SummarizationResult;
|
|
337
|
+
} | {
|
|
338
|
+
type: 'content_filtered';
|
|
339
|
+
result: FilteringResult;
|
|
340
|
+
};
|
|
341
|
+
/**
|
|
342
|
+
* Context event handler
|
|
343
|
+
*/
|
|
344
|
+
export type ContextEventHandler = (event: ContextEvent) => void;
|
|
345
|
+
/**
|
|
346
|
+
* Context statistics
|
|
347
|
+
*/
|
|
348
|
+
export interface ContextStats {
|
|
349
|
+
/**
|
|
350
|
+
* Current token count
|
|
351
|
+
*/
|
|
352
|
+
currentTokens: number;
|
|
353
|
+
/**
|
|
354
|
+
* Maximum tokens allowed
|
|
355
|
+
*/
|
|
356
|
+
maxTokens: number;
|
|
357
|
+
/**
|
|
358
|
+
* Current utilization (0.0 - 1.0)
|
|
359
|
+
*/
|
|
360
|
+
utilization: number;
|
|
361
|
+
/**
|
|
362
|
+
* Number of messages in history
|
|
363
|
+
*/
|
|
364
|
+
messageCount: number;
|
|
365
|
+
/**
|
|
366
|
+
* Number of turns (user message + assistant response pairs)
|
|
367
|
+
*/
|
|
368
|
+
turnCount: number;
|
|
369
|
+
/**
|
|
370
|
+
* Number of compactions performed
|
|
371
|
+
*/
|
|
372
|
+
compactionCount: number;
|
|
373
|
+
/**
|
|
374
|
+
* Number of summarizations performed
|
|
375
|
+
*/
|
|
376
|
+
summarizationCount: number;
|
|
377
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Usage module - Token usage tracking
|
|
3
|
+
*
|
|
4
|
+
* Tracks token usage across LLM calls with optional budget limits.
|
|
5
|
+
* Note: We track tokens only, not dollar costs.
|
|
6
|
+
*/
|
|
7
|
+
export { UsageTracker, createUsageTracker } from './tracker.js';
|
|
8
|
+
export type { TokenUsage, UsageRecord, UsageStats, TokenBudgetConfig, BudgetStatus, UsageEventType, UsageEvent, UsageEventHandler, UsageTrackerOptions, RecordUsageInput, } from './types.js';
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UsageTracker - Tracks token usage across LLM calls
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* const tracker = new UsageTracker({
|
|
7
|
+
* budget: {
|
|
8
|
+
* maxTotalTokens: 100000,
|
|
9
|
+
* warningThreshold: 0.8,
|
|
10
|
+
* },
|
|
11
|
+
* });
|
|
12
|
+
*
|
|
13
|
+
* tracker.onEvent((event) => {
|
|
14
|
+
* if (event.type === 'usage:budget_warning') {
|
|
15
|
+
* console.log('Budget warning!', event.status);
|
|
16
|
+
* }
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* // Record usage after each LLM call
|
|
20
|
+
* tracker.record({
|
|
21
|
+
* model: 'claude-sonnet-4-20250514',
|
|
22
|
+
* provider: 'anthropic',
|
|
23
|
+
* tokens: { inputTokens: 1000, outputTokens: 500, totalTokens: 1500 },
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* // Get stats
|
|
27
|
+
* console.log(tracker.getStats());
|
|
28
|
+
* console.log(tracker.formatTokens(tracker.getTotalTokens()));
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
import type { UsageRecord, UsageStats, BudgetStatus, UsageEventHandler, UsageTrackerOptions, RecordUsageInput } from './types.js';
|
|
32
|
+
/**
|
|
33
|
+
* UsageTracker tracks token usage across LLM calls
|
|
34
|
+
*/
|
|
35
|
+
export declare class UsageTracker {
|
|
36
|
+
private readonly enabled;
|
|
37
|
+
private readonly budget?;
|
|
38
|
+
private readonly sessionId?;
|
|
39
|
+
private readonly eventHandlers;
|
|
40
|
+
/** All usage records */
|
|
41
|
+
private readonly records;
|
|
42
|
+
/** Cached stats (invalidated on new records) */
|
|
43
|
+
private cachedStats?;
|
|
44
|
+
constructor(options?: UsageTrackerOptions);
|
|
45
|
+
/**
|
|
46
|
+
* Record token usage from an LLM call
|
|
47
|
+
*/
|
|
48
|
+
record(input: RecordUsageInput): UsageRecord | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Get aggregated usage statistics
|
|
51
|
+
*/
|
|
52
|
+
getStats(): UsageStats;
|
|
53
|
+
/**
|
|
54
|
+
* Get stats for a specific session
|
|
55
|
+
*/
|
|
56
|
+
getSessionStats(sessionId: string): UsageStats;
|
|
57
|
+
/**
|
|
58
|
+
* Get total tokens used
|
|
59
|
+
*/
|
|
60
|
+
getTotalTokens(): number;
|
|
61
|
+
/**
|
|
62
|
+
* Get total input tokens
|
|
63
|
+
*/
|
|
64
|
+
getTotalInputTokens(): number;
|
|
65
|
+
/**
|
|
66
|
+
* Get total output tokens
|
|
67
|
+
*/
|
|
68
|
+
getTotalOutputTokens(): number;
|
|
69
|
+
/**
|
|
70
|
+
* Get budget status
|
|
71
|
+
*/
|
|
72
|
+
getBudgetStatus(): BudgetStatus;
|
|
73
|
+
/**
|
|
74
|
+
* Check if budget is exceeded
|
|
75
|
+
*/
|
|
76
|
+
isBudgetExceeded(): boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Check budget and emit events
|
|
79
|
+
*/
|
|
80
|
+
private checkBudget;
|
|
81
|
+
/**
|
|
82
|
+
* Reset all tracking data
|
|
83
|
+
*/
|
|
84
|
+
reset(): void;
|
|
85
|
+
/**
|
|
86
|
+
* Get all records
|
|
87
|
+
*/
|
|
88
|
+
getRecords(): readonly UsageRecord[];
|
|
89
|
+
/**
|
|
90
|
+
* Get records for a specific session
|
|
91
|
+
*/
|
|
92
|
+
getSessionRecords(sessionId: string): UsageRecord[];
|
|
93
|
+
/**
|
|
94
|
+
* Format tokens with commas for display
|
|
95
|
+
*/
|
|
96
|
+
formatTokens(tokens: number): string;
|
|
97
|
+
/**
|
|
98
|
+
* Get a human-readable summary
|
|
99
|
+
*/
|
|
100
|
+
getSummary(): string;
|
|
101
|
+
/**
|
|
102
|
+
* Register an event handler
|
|
103
|
+
*/
|
|
104
|
+
onEvent(handler: UsageEventHandler): () => void;
|
|
105
|
+
/**
|
|
106
|
+
* Emit an event
|
|
107
|
+
*/
|
|
108
|
+
private emit;
|
|
109
|
+
/**
|
|
110
|
+
* Check if tracking is enabled
|
|
111
|
+
*/
|
|
112
|
+
get isEnabled(): boolean;
|
|
113
|
+
/**
|
|
114
|
+
* Get the number of records
|
|
115
|
+
*/
|
|
116
|
+
get size(): number;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Create a UsageTracker instance
|
|
120
|
+
*/
|
|
121
|
+
export declare function createUsageTracker(options?: UsageTrackerOptions): UsageTracker;
|