mirror of
https://gitcode.com/JianFeeeee/ModelRouter.git
synced 2026-09-20 08:57:57 +00:00
singleChat / singleChatAuto / streamChat / streamChatAuto shared ~260 near- identical lines (diff after stripping comments was empty). Extract three shared bodies and shrink all four entry points to thin dispatchers: - failChat: error → record + writeError, shared ChainErr extraction (errors.As returns false for direct-path errors, so the two paths stay equivalent without a branch) - recordChatUsage: exact upstream numbers win, byte estimates fill gaps - writeChatCompletion: unified ChatCompletion rendering (model name is the only direct/auto difference, passed in) - pumpStream: the full SSE pump (preamble, delta loop, terminal finish + usage chunk, [DONE]) — shared by both streaming entries Behavior change (pinned by TestDirectStreamFailoverAuditSource): direct streams now pin rec.Source to the source that actually served the stream after a failover, instead of discarding it (_, usedModel). The audit row previously recorded the first candidate, which was wrong on failover. chat.go: 982 → 896 lines (−86).