Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/UsageTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export interface ContextUsage {
readonly percent: number;
}

export interface LastAssistantInfo {
readonly uuid: string;
}

interface AuditUsage {
readonly input_tokens: number;
readonly cache_creation_input_tokens: number;
Expand Down Expand Up @@ -49,6 +53,7 @@ export class UsageTracker {
private lastAssistantUsage: AuditUsage | undefined;
private lastContextWindow = 0;
private cumulativeCost = 0;
private _lastAssistantUuid: string | undefined;

/** Load context usage from the tail of the audit file (sync, fast, 256KB). */
public loadContextFromAudit(auditFile: string, sessionId: string): void {
Expand Down Expand Up @@ -76,6 +81,9 @@ export class UsageTracker {
if (message?.usage) {
this.lastAssistantUsage = message.usage;
}
if (!this._lastAssistantUuid && typeof entry.uuid === 'string') {
this._lastAssistantUuid = entry.uuid;
}
}

// Once we have both, no need to continue
Expand Down Expand Up @@ -123,6 +131,7 @@ export class UsageTracker {
if (msg.type !== 'assistant') {
return;
}
this._lastAssistantUuid = msg.uuid;
const { id, usage } = msg.message;
if (!usage || this.processedMessageIds.has(id)) {
return;
Expand All @@ -147,6 +156,13 @@ export class UsageTracker {
return this.cumulativeCost;
}

public get lastAssistant(): LastAssistantInfo | undefined {
if (!this._lastAssistantUuid) {
return undefined;
}
return { uuid: this._lastAssistantUuid };
}

public get context(): ContextUsage | undefined {
if (!this.lastAssistantUsage) {
return undefined;
Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ async function submit(override?: string): Promise<void> {
}
break;
case 'assistant': {
const ctx = usage.context;
const pctSuffix = ctx ? ` (${ctx.percent.toFixed(1)}%)` : '';
logEvent(`\x1b[2mmessageId: ${msg.uuid}${pctSuffix}\x1b[0m`);
for (const block of msg.message.content) {
if (block.type === 'text') {
logEvent(`assistant: ${block.text}`);
Expand Down Expand Up @@ -363,6 +366,10 @@ async function start(): Promise<void> {
session.setSessionId(savedSession);
term.info(`Resuming session: ${savedSession}`);
usage.loadContextFromAudit(paths.auditFile, savedSession);
const lastAssistant = usage.lastAssistant;
if (lastAssistant) {
term.info(`\x1b[2mlast messageId: ${lastAssistant.uuid}\x1b[0m`);
}
const ctx = usage.context;
if (ctx) {
term.info(formatContext(ctx));
Expand Down
2 changes: 1 addition & 1 deletion src/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Terminal {
}

public logLine(message: string, ...args: any[]): void {
process.stdout.write('[');
process.stdout.write('\x1b[0m[');
process.stdout.write(this.timestamp());
process.stdout.write('] ');
process.stdout.write(message);
Expand Down
Loading