-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathagents.ts
More file actions
389 lines (350 loc) · 11.8 KB
/
agents.ts
File metadata and controls
389 lines (350 loc) · 11.8 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
/**
* Agent Sessions - Programmatic API for AI agents
*
* Track agent runs with automatic file watching, git tracking,
* AI cost logging, and budget enforcement.
*
* @example
* ```typescript
* import { AgentSession } from 'codesession-cli/agents';
*
* const session = new AgentSession('Refactor auth module', {
* budget: 5.00,
* directory: './src',
* git: true,
* });
*
* session.start();
* // ... agent does work ...
* const summary = session.end();
* ```
*/
import {
createSession,
endSession as dbEndSession,
getSession,
addAIUsage,
getFileChanges,
getCommits,
getAIUsage,
} from './db';
import { initGit, startGitPolling, stopGitPolling, checkForNewCommits, cleanupGit } from './git';
import { startWatcher, stopWatcher, cleanupWatcher } from './watcher';
export interface AgentSessionConfig {
/** Hard budget cap in dollars. Session auto-ends if exceeded. */
budget?: number;
/** Directory to watch for file changes. Defaults to cwd. */
directory?: string;
/** Enable git commit tracking. Defaults to true. */
git?: boolean;
/** Interval (ms) to check for new git commits. Defaults to 5000. */
gitPollInterval?: number;
/** Callback when budget is exceeded. Called before session ends. */
onBudgetExceeded?: (spent: number, budget: number) => void;
/** Callback when AI usage is logged. */
onAIUsage?: (cost: number, totalCost: number, model: string) => void;
/** Callback on each file change. */
onFileChange?: (filePath: string, changeType: string) => void;
/** Optional metadata to attach to the session. */
metadata?: Record<string, any>;
}
export interface AgentSessionSummary {
sessionId: number;
name: string;
duration: number;
filesChanged: number;
commits: number;
aiCost: number;
aiTokens: number;
budgetRemaining: number | null;
files: { path: string; type: string; timestamp: string }[];
commitList: { hash: string; message: string; timestamp: string }[];
aiUsageBreakdown: { provider: string; model: string; tokens: number; cost: number; timestamp: string }[];
metadata?: Record<string, any>;
}
export class AgentSession {
private sessionId: number | null = null;
private name: string;
private config: AgentSessionConfig;
private totalCost = 0;
private totalTokens = 0;
private started = false;
private ended = false;
constructor(name: string, config: AgentSessionConfig = {}) {
this.name = name;
this.config = {
git: true,
gitPollInterval: 5000,
...config,
};
}
/**
* Start the agent session. Begins file watching and git tracking.
* @returns The session ID
*/
start(): number {
if (this.started) {
throw new Error(`Session "${this.name}" is already started.`);
}
if (this.ended) {
throw new Error(`Session "${this.name}" has already ended. Create a new AgentSession.`);
}
const cwd = this.config.directory || process.cwd();
this.sessionId = createSession({
name: this.name,
startTime: new Date().toISOString(),
workingDirectory: cwd,
filesChanged: 0,
commits: 0,
aiCost: 0,
aiTokens: 0,
status: 'active',
});
// Start file watcher
startWatcher(this.sessionId, cwd);
// Start git polling
if (this.config.git) {
initGit(this.sessionId, cwd);
startGitPolling(this.sessionId, this.config.gitPollInterval);
}
this.started = true;
return this.sessionId;
}
/**
* Log AI usage for this session. Automatically checks budget.
* Cost is optional — auto-calculated from built-in pricing if omitted (requires known model).
* @returns The remaining budget (null if no budget set)
* @throws BudgetExceededError if budget is exceeded
*/
logAI(provider: string, model: string, tokens: number, cost: number, options?: { promptTokens?: number; completionTokens?: number; agentName?: string }): number | null {
this.assertStarted();
// Check budget BEFORE writing to database
const newTotalCost = this.totalCost + cost;
if (this.config.budget !== undefined && newTotalCost > this.config.budget) {
if (this.config.onBudgetExceeded) {
this.config.onBudgetExceeded(newTotalCost, this.config.budget);
}
// Don't write this usage to DB - budget already exceeded
throw new BudgetExceededError(newTotalCost, this.config.budget);
}
// Budget check passed - safe to write to DB
addAIUsage({
sessionId: this.sessionId!,
provider,
model,
tokens,
promptTokens: options?.promptTokens,
completionTokens: options?.completionTokens,
cost,
agentName: options?.agentName,
timestamp: new Date().toISOString(),
});
this.totalCost = newTotalCost;
this.totalTokens += tokens;
// Notify callback
if (this.config.onAIUsage) {
this.config.onAIUsage(cost, this.totalCost, model);
}
// Auto-end session if budget exactly met or exceeded
if (this.config.budget !== undefined && this.totalCost >= this.config.budget) {
this.end(`Budget reached: $${this.totalCost.toFixed(2)} / $${this.config.budget.toFixed(2)}`);
}
return this.config.budget !== undefined
? Math.max(0, this.config.budget - this.totalCost)
: null;
}
/**
* Check how much budget remains.
* @returns Remaining budget in dollars, or null if no budget set
*/
get budgetRemaining(): number | null {
if (this.config.budget === undefined) return null;
return Math.max(0, this.config.budget - this.totalCost);
}
/** Current total cost spent in this session */
get spent(): number {
return this.totalCost;
}
/** Current total tokens used in this session */
get tokens(): number {
return this.totalTokens;
}
/** Whether this session is currently active */
get isActive(): boolean {
return this.started && !this.ended;
}
/** The database session ID (null if not started) */
get id(): number | null {
return this.sessionId;
}
/**
* End the agent session and return a full summary.
*/
end(notes?: string): AgentSessionSummary {
this.assertStarted();
// Stop tracking
void stopWatcher(this.sessionId!);
stopGitPolling(this.sessionId!);
cleanupGit(this.sessionId!);
const endTime = new Date().toISOString();
dbEndSession(this.sessionId!, endTime, notes);
this.ended = true;
// Build summary
const session = getSession(this.sessionId!)!;
const files = getFileChanges(this.sessionId!);
const commits = getCommits(this.sessionId!);
const aiUsage = getAIUsage(this.sessionId!);
return {
sessionId: this.sessionId!,
name: this.name,
duration: session.duration || 0,
filesChanged: session.filesChanged,
commits: session.commits,
aiCost: session.aiCost,
aiTokens: session.aiTokens,
budgetRemaining: this.config.budget !== undefined
? Math.max(0, this.config.budget - session.aiCost)
: null,
files: files.map((f) => ({
path: f.filePath,
type: f.changeType,
timestamp: f.timestamp,
})),
commitList: commits.map((c) => ({
hash: c.hash,
message: c.message,
timestamp: c.timestamp,
})),
aiUsageBreakdown: aiUsage.map((a) => ({
provider: a.provider,
model: a.model,
tokens: a.tokens,
cost: a.cost,
timestamp: a.timestamp,
})),
metadata: this.config.metadata,
};
}
/**
* Check if an AI call would exceed the budget.
* Useful for pre-flight checks before making expensive API calls.
* @returns true if the call would stay within budget
*/
canAfford(estimatedCost: number): boolean {
if (this.config.budget === undefined) return true;
return this.totalCost + estimatedCost <= this.config.budget;
}
private assertStarted(): void {
if (!this.started) {
throw new Error(`Session "${this.name}" has not been started. Call .start() first.`);
}
if (this.ended) {
throw new Error(`Session "${this.name}" has already ended.`);
}
}
}
/**
* Thrown when an agent session exceeds its budget.
* Catch this to handle budget enforcement gracefully.
*
* @example
* ```typescript
* try {
* session.logAI('openai', 'gpt-4o', 5000, 0.10);
* } catch (e) {
* if (e instanceof BudgetExceededError) {
* console.log(`Over budget: spent $${e.spent}, limit $${e.budget}`);
* }
* }
* ```
*/
export class BudgetExceededError extends Error {
public spent: number;
public budget: number;
constructor(spent: number, budget: number) {
super(`Budget exceeded: spent $${spent.toFixed(2)} of $${budget.toFixed(2)} limit`);
this.name = 'BudgetExceededError';
this.spent = spent;
this.budget = budget;
}
}
/**
* Quick helper to run an agent function within a tracked session.
* Handles start/end/error automatically.
*
* @example
* ```typescript
* const summary = await runAgentSession('Fix all linting errors', {
* budget: 3.00,
* directory: './src',
* }, async (session) => {
* // Your agent logic here
* const response = await openai.chat.completions.create({ ... });
* session.logAI('openai', 'gpt-4o', response.usage.total_tokens, 0.05);
* });
* ```
*/
export async function runAgentSession(
name: string,
config: AgentSessionConfig,
agentFn: (session: AgentSession) => Promise<void>
): Promise<AgentSessionSummary> {
const session = new AgentSession(name, config);
session.start();
try {
await agentFn(session);
} catch (error) {
if (error instanceof BudgetExceededError) {
// Budget exceeded — session may still be active (throw happens before DB write).
// End it cleanly so watcher/git resources are released.
if (session.isActive) {
session.end(`Budget exceeded: $${(error as BudgetExceededError).spent.toFixed(2)} / $${(error as BudgetExceededError).budget.toFixed(2)}`);
}
const dbSession = getSession(session.id!)!;
const files = getFileChanges(session.id!);
const commits = getCommits(session.id!);
const aiUsage = getAIUsage(session.id!);
return {
sessionId: session.id!,
name,
duration: dbSession.duration || 0,
filesChanged: dbSession.filesChanged,
commits: dbSession.commits,
aiCost: dbSession.aiCost,
aiTokens: dbSession.aiTokens,
budgetRemaining: 0,
files: files.map((f) => ({ path: f.filePath, type: f.changeType, timestamp: f.timestamp })),
commitList: commits.map((c) => ({ hash: c.hash, message: c.message, timestamp: c.timestamp })),
aiUsageBreakdown: aiUsage.map((a) => ({ provider: a.provider, model: a.model, tokens: a.tokens, cost: a.cost, timestamp: a.timestamp })),
metadata: config.metadata,
};
}
// For non-budget errors, end session with error note and re-throw
if (session.isActive) {
session.end(`Error: ${(error as Error).message}`);
}
throw error;
}
if (session.isActive) {
return session.end();
}
// Session was ended during agentFn (e.g. explicit end call)
const dbSession = getSession(session.id!)!;
const files = getFileChanges(session.id!);
const commitsList = getCommits(session.id!);
const aiUsage = getAIUsage(session.id!);
return {
sessionId: session.id!,
name,
duration: dbSession.duration || 0,
filesChanged: dbSession.filesChanged,
commits: dbSession.commits,
aiCost: dbSession.aiCost,
aiTokens: dbSession.aiTokens,
budgetRemaining: config.budget !== undefined ? Math.max(0, config.budget - dbSession.aiCost) : null,
files: files.map((f) => ({ path: f.filePath, type: f.changeType, timestamp: f.timestamp })),
commitList: commitsList.map((c) => ({ hash: c.hash, message: c.message, timestamp: c.timestamp })),
aiUsageBreakdown: aiUsage.map((a) => ({ provider: a.provider, model: a.model, tokens: a.tokens, cost: a.cost, timestamp: a.timestamp })),
metadata: config.metadata,
};
}