/**
 * Token Usage Storage Service
 *
 * Tracks token usage per agent session with in-memory storage and
 * optional periodic flush to ~/.crewly/token-usage.json.
 *
 * @module services/monitoring/token-usage.service
 */
/**
 * A single token usage event recorded after an agent message
 */
export interface TokenUsageEvent {
    /** Timestamp of the usage event */
    timestamp: string;
    /** Agent identifier */
    agentId: string;
    /** Number of input tokens consumed */
    input: number;
    /** Number of output tokens generated */
    output: number;
    /** AI model used for this event */
    model: string;
    /** Optional task ID for per-task tracking */
    taskId?: string;
}
/**
 * Per-session token usage record containing all events and aggregates
 */
export interface SessionUsageRecord {
    /** Session name */
    sessionName: string;
    /** Agent identifier associated with this session */
    agentId: string;
    /** Total input tokens across all events */
    totalInput: number;
    /** Total output tokens across all events */
    totalOutput: number;
    /** Number of usage events recorded */
    eventCount: number;
    /** Individual usage events */
    events: TokenUsageEvent[];
}
/**
 * Aggregated usage summary for a single agent
 */
export interface AgentUsageSummary {
    /** Total input tokens consumed by this agent */
    totalInput: number;
    /** Total output tokens generated by this agent */
    totalOutput: number;
    /** Number of usage events */
    eventCount: number;
}
/**
 * Per-session summary for dashboard display
 */
export interface SessionUsageSummary {
    /** Session name */
    sessionName: string;
    /** Agent identifier */
    agentId: string;
    /** Total input tokens */
    totalInput: number;
    /** Total output tokens */
    totalOutput: number;
    /** Number of events */
    eventCount: number;
    /** Computed cost in USD based on model pricing */
    cost: number;
    /** Per-model breakdown of token usage */
    modelBreakdown: ModelUsageBreakdown[];
}
/**
 * Token usage breakdown for a specific model
 */
export interface ModelUsageBreakdown {
    /** Model identifier */
    model: string;
    /** Input tokens for this model */
    inputTokens: number;
    /** Output tokens for this model */
    outputTokens: number;
    /** Computed cost for this model in USD */
    cost: number;
}
/**
 * Per-model token cost rates in USD per token.
 * Used for server-side cost calculation.
 */
export declare const TOKEN_COSTS: Record<string, {
    input: number;
    output: number;
}>;
/**
 * Calculate the cost for a given number of tokens and model.
 *
 * @param inputTokens - Number of input tokens (excluding cache tokens)
 * @param outputTokens - Number of output tokens
 * @param model - Model identifier
 * @returns Cost in USD
 */
export declare function calculateCost(inputTokens: number, outputTokens: number, model: string): number;
/**
 * Set a cost override for a session. When present, getSummary() uses this
 * instead of recalculating from events.
 */
export declare function setSessionCostOverride(sessionName: string, cost: number): void;
/**
 * Get a cost override if one exists.
 */
export declare function getSessionCostOverride(sessionName: string): number | undefined;
/**
 * Service for tracking and aggregating token usage across agent sessions.
 *
 * Uses an in-memory Map keyed by sessionName, with optional periodic
 * flush to ~/.crewly/token-usage.json for persistence across restarts.
 *
 * @example
 * ```typescript
 * const service = TokenUsageService.getInstance();
 * service.recordUsage('session-1', 'agent-a', 500, 200, 'claude-3-opus');
 * const summary = service.getUsageByAgent('agent-a');
 * ```
 */
export declare class TokenUsageService {
    private static instance;
    /** In-memory storage keyed by session name */
    private readonly sessions;
    /** Active task context per session (sessionName → { taskId, runId }) */
    private readonly taskContexts;
    /** Timer handle for periodic flush */
    private flushTimer;
    /** Directory for persisting usage data */
    private readonly storageDir;
    /**
     * Create a new TokenUsageService instance
     *
     * @param storageDir - Directory for persistence file (defaults to ~/.crewly)
     */
    constructor(storageDir?: string);
    /**
     * Get the singleton TokenUsageService instance
     *
     * @returns The shared TokenUsageService instance
     */
    static getInstance(): TokenUsageService;
    /**
     * Reset the singleton instance (for testing)
     */
    static resetInstance(): void;
    /**
     * Record a single token usage event for a session.
     *
     * Creates the session record if it doesn't exist, then appends the event
     * and updates running totals.
     *
     * @param sessionName - The session name (e.g. 'crewly-orc')
     * @param agentId - The agent identifier
     * @param input - Number of input tokens consumed
     * @param output - Number of output tokens generated
     * @param model - AI model identifier used
     */
    recordUsage(sessionName: string, agentId: string, input: number, output: number, model: string, taskId?: string): void;
    /**
     * Set a cache-aware cost override for a session.
     * Used when the caller has pre-calculated cost with proper cache pricing.
     */
    overrideSessionCost(sessionName: string, cost: number): void;
    /**
     * Get aggregated token usage for a specific agent across all sessions.
     *
     * @param agentId - The agent identifier to query
     * @returns Aggregated usage summary for the agent
     */
    getUsageByAgent(agentId: string): AgentUsageSummary;
    /**
     * Get per-session usage summaries for the dashboard.
     *
     * @returns Array of session usage summaries
     */
    getUsageBySessions(taskId?: string): SessionUsageSummary[];
    /**
     * Set the current task context for a session.
     * All subsequent recordUsage calls for this session will be tagged with the taskId.
     *
     * @param sessionName - Session name
     * @param taskId - Task ID to associate, or null to clear
     * @param runId - Optional run ID for precise run-level attribution
     */
    setTaskContext(sessionName: string, taskId: string | null, runId?: string): void;
    /**
     * Get the current task context for a session (taskId only, backward compatible).
     *
     * @param sessionName - Session name
     * @returns Current task ID or null
     */
    getTaskContext(sessionName: string): string | null;
    /**
     * Get the full task+run context for a session.
     *
     * @param sessionName - Session name
     * @returns Object with taskId and optional runId, or null if no context
     */
    getRunContext(sessionName: string): {
        taskId: string;
        runId?: string;
    } | null;
    /**
     * Get aggregated token usage grouped by task ID.
     *
     * @returns Map of taskId to aggregated token totals
     */
    getUsageByTask(): Map<string, {
        taskId: string;
        totalInput: number;
        totalOutput: number;
        eventCount: number;
        cost: number;
    }>;
    /**
     * Get token usage events for a specific session recorded after a given timestamp.
     * Used to compute the token delta for a single Request when the orchestrator
     * handles the work directly (no WorkItem delegation).
     *
     * @param sessionName - The session to query (e.g. 'crewly-orc')
     * @param since - Only return events recorded at or after this time
     * @param until - Only return events before this time (defaults to now)
     * @returns Aggregated input tokens, output tokens, and cost in the window
     */
    getSessionUsageSince(sessionName: string, since: Date, until?: Date): {
        inputTokens: number;
        outputTokens: number;
        cost: number;
    };
    /**
     * Clear all tracked token usage data.
     */
    resetUsage(): void;
    /**
     * Start periodic flush of usage data to disk.
     *
     * Writes the current in-memory state to ~/.crewly/token-usage.json
     * at a fixed interval.
     */
    startPeriodicFlush(): void;
    /**
     * Stop the periodic flush timer.
     */
    stopPeriodicFlush(): void;
    /**
     * Flush current usage data to disk.
     *
     * Writes sessions as a JSON array to the storage directory.
     */
    flushToDisk(): Promise<void>;
    /**
     * Load previously persisted usage data from disk.
     *
     * Merges loaded data into the current in-memory state.
     */
    loadFromDisk(): Promise<void>;
    /**
     * Get the number of tracked sessions (for testing).
     *
     * @returns Number of sessions in the store
     */
    getSessionCount(): number;
}
//# sourceMappingURL=token-usage.service.d.ts.map