Skip to content

refactor: extract OpenAI message converter from SessionManager#140

Open
Lellansin wants to merge 2 commits into
lessweb:mainfrom
Lellansin:refactor/openai-message-converter
Open

refactor: extract OpenAI message converter from SessionManager#140
Lellansin wants to merge 2 commits into
lessweb:mainfrom
Lellansin:refactor/openai-message-converter

Conversation

@Lellansin
Copy link
Copy Markdown
Contributor

变更内容

  • 创建 src/common/openai-message-converter.ts(277 行),将 OpenAI 消息转换逻辑从 SessionManager 中拆分为独立模块
  • OpenAIMessageConverter 提供 3 个公开 API:
    • buildMessages(messages, thinkingEnabled, model) — 构建 OpenAI 消息数组
    • getTrailingPendingToolCallMessage(messages) — 获取尾部待执行 tool calls
    • findToolFunction(toolCalls, toolCallId) — 供 appendToolMessages 查找 tool 函数
  • session.ts 从 2884 行缩减至 2644 行(净减少 240 行),仅保留编排逻辑
  • 新增 src/tests/openai-message-converter.test.ts(20 个独立单测),覆盖内容处理、工具调用配对、compaction 过滤等场景
  • 测试无依赖 SessionManager,纯数据进数据出

背景

SessionManager 承载了过多职责——消息格式转换(工具配对、thinking mode 注入、多模态过滤)与 LLM 编排、session 生命周期混在一起,导致:

  • 类体积膨胀(2884 行)
  • 转换逻辑无法独立测试(现有测试通过 (manager as any).buildOpenAIMessages() 访问)
  • 后期切换模型提供商或优化转换策略需改动 SessionManager

本次抽取将 14 个 private 转换方法收敛到 OpenAIMessageConverterSessionManager 通过构造函数注入 renderInitPrompt 回调解耦 /init 模板渲染,其余纯逻辑无外部依赖。

Lellansin added 2 commits May 30, 2026 17:17
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant