refactor: extract OpenAI message converter from SessionManager#140
Open
Lellansin wants to merge 2 commits into
Open
refactor: extract OpenAI message converter from SessionManager#140Lellansin wants to merge 2 commits into
Lellansin wants to merge 2 commits into
Conversation
Move buildOpenAIMessages, sessionMessageToOpenAIMessage, pairToolMessages, getTrailingPendingToolCallMessage, and related helpers (~240 lines) into a dedicated OpenAIMessageConverter class under src/common/. SessionManager now delegates to the converter while retaining a deprecated buildOpenAIMessages pass-through for test compatibility. Net reduction: 2884 → 2644 lines in session.ts
20 tests covering buildMessages (content handling, tool-call pairing, interrupted backfill, compaction filtering), getTrailingPendingToolCallMessage, and findToolFunction. No dependency on SessionManager — pure data-in / data-out.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
变更内容
src/common/openai-message-converter.ts(277 行),将 OpenAI 消息转换逻辑从SessionManager中拆分为独立模块OpenAIMessageConverter提供 3 个公开 API:buildMessages(messages, thinkingEnabled, model)— 构建 OpenAI 消息数组getTrailingPendingToolCallMessage(messages)— 获取尾部待执行 tool callsfindToolFunction(toolCalls, toolCallId)— 供appendToolMessages查找 tool 函数session.ts从 2884 行缩减至 2644 行(净减少 240 行),仅保留编排逻辑src/tests/openai-message-converter.test.ts(20 个独立单测),覆盖内容处理、工具调用配对、compaction 过滤等场景SessionManager,纯数据进数据出背景
SessionManager承载了过多职责——消息格式转换(工具配对、thinking mode 注入、多模态过滤)与 LLM 编排、session 生命周期混在一起,导致:(manager as any).buildOpenAIMessages()访问)SessionManager本次抽取将 14 个 private 转换方法收敛到
OpenAIMessageConverter,SessionManager通过构造函数注入renderInitPrompt回调解耦/init模板渲染,其余纯逻辑无外部依赖。