contextUsage.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233
  1. import type { ChatUsage, JsonValue } from '@shared/types'
  2. import { record } from './normalize'
  3. /** Match mobile: context is the latest prompt occupancy, never cumulative usage
  4. * or the full (possibly compacted/paginated) visible transcript. */
  5. export function normalizeChatUsage(...sources: unknown[]): ChatUsage | undefined {
  6. const fields: [Exclude<keyof ChatUsage, 'raw'>, string[]][] = [
  7. ['inputTokens', ['input_tokens', 'inputTokens', 'input', 'prompt']],
  8. ['outputTokens', ['output_tokens', 'outputTokens', 'output', 'completion']],
  9. ['totalTokens', ['total_tokens', 'totalTokens', 'total']],
  10. ['contextTokens', ['context_used', 'contextUsed', 'context_tokens', 'contextTokens']],
  11. ['contextLimit', ['context_max', 'contextMax', 'context_limit', 'contextLimit', 'context_length', 'contextLength', 'effective_context_length']],
  12. ['percentUsed', ['context_percent', 'contextPercent', 'percent_used', 'percentUsed']],
  13. ]
  14. const usage: ChatUsage = {}
  15. for (const source of sources) {
  16. const root = record(source)
  17. for (const value of [record(root.usage), record(record(root.info).usage), root, record(root.info)]) {
  18. for (const [field, aliases] of fields) {
  19. if (usage[field] !== undefined) continue
  20. for (const alias of aliases) {
  21. const raw = value[alias]
  22. if (typeof raw !== 'number' && (typeof raw !== 'string' || !raw.trim())) continue
  23. const number = Number(raw)
  24. if (!Number.isFinite(number) || number < 0 || (field === 'contextLimit' && number === 0)) continue
  25. usage[field] = number
  26. break
  27. }
  28. }
  29. }
  30. }
  31. return Object.keys(usage).length ? { ...usage, raw: sources[0] as JsonValue } : undefined
  32. }