Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

629 B
Raw Blame
import { useRef, useMemo } from 'react';
import { getDefaultStore } from 'jotai';
import type { UsageHandlers } from './handlers';
import { createUsageCounters, createUsageHandlers } from './handlers';

export type { UsageHandlers };

/**
 * Stable writers for the per-conversation token usage atoms, driven by SSE events. All state
 * lives in the counters ref and the atoms, so the returned handlers never re-render the caller.
 */
export function useUsageHandler(): UsageHandlers {
  const countersRef = useRef(createUsageCounters());
  return useMemo(() => createUsageHandlers(getDefaultStore(), countersRef.current), []);
}