| import { tr } from '../lang/i18n-lite'; |
|
|
| |
| export type GenerationEndDisplayKind = 'eos' | 'length_limit' | 'stopped' | 'error'; |
|
|
| export function generationEndReasonLabel(kind: GenerationEndDisplayKind): string { |
| switch (kind) { |
| case 'eos': |
| return tr('EOS reached'); |
| case 'length_limit': |
| return tr('Maximum length reached'); |
| case 'stopped': |
| return tr('Stopped'); |
| case 'error': |
| return tr('Error'); |
| default: { |
| const _exhaustive: never = kind; |
| return _exhaustive; |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| export const COMPLETION_FINISH_REASONS = ['stop', 'length', 'abort', 'error'] as const; |
|
|
| export type CompletionFinishReason = (typeof COMPLETION_FINISH_REASONS)[number]; |
|
|
| |
| const LEGACY_FINISH_TO_CANONICAL: Record<string, CompletionFinishReason> = { |
| eos: 'stop', |
| max_tokens: 'length', |
| aborted: 'abort', |
| }; |
|
|
| const FINISH_REASON_TO_DISPLAY: Record<CompletionFinishReason, GenerationEndDisplayKind> = { |
| stop: 'eos', |
| length: 'length_limit', |
| abort: 'stopped', |
| error: 'error', |
| }; |
|
|
| export function isCompletionFinishReason(s: string): s is CompletionFinishReason { |
| return (COMPLETION_FINISH_REASONS as readonly string[]).includes(s); |
| } |
|
|
| |
| export function isKnownPersistedCompletionReason(s: string): boolean { |
| return isCompletionFinishReason(s) || s in LEGACY_FINISH_TO_CANONICAL; |
| } |
|
|
| |
| export function canonicalizeCompletionFinishReason(raw: string): string { |
| const r = raw.trim(); |
| if (!r) return r; |
| if (isCompletionFinishReason(r)) return r; |
| return LEGACY_FINISH_TO_CANONICAL[r] ?? r; |
| } |
|
|
| |
| |
| |
| |
| export function completionFinishReasonLabel(finishReason: string | null | undefined): string { |
| const r = (finishReason ?? '').trim(); |
| if (!r) return ''; |
| const c = canonicalizeCompletionFinishReason(r); |
| if (isCompletionFinishReason(c)) { |
| return generationEndReasonLabel(FINISH_REASON_TO_DISPLAY[c]); |
| } |
| return r; |
| } |
|
|