Skip to content

[P2] Lazy load heavy optional dependencies / 重型可选依赖延迟加载 #3225

@doudouOUC

Description

@doudouOUC

Priority: P2
Category: performance, core
Parent: #3011
Depends on: Lazy tool registration sub-issue (establishes lazy loading pattern)

Problem / 问题

Heavy dependencies loaded eagerly at startup but used much later or conditionally:

  • OpenTelemetry SDK (~500KB): only when telemetry enabled
  • highlight.js/lowlight (~300KB): only when rendering code
  • web-tree-sitter + WASM (~100KB): only for shell parsing
  • React + Ink: only for interactive mode
  • Channel packages (telegram/weixin/dingtalk): only in channel mode

重型依赖启动时全量加载,但使用时机远晚于启动或仅在特定条件下使用。

Proposed Solution / 方案

Convert static imports to dynamic import():

Dependency Current Target
OpenTelemetry SDK Static via barrel import() when telemetry enabled
highlight.js Static in CodeColorizer import() on first code render
web-tree-sitter Static import wrapper import() on first parse call
React + Ink Static in gemini.tsx import() only in interactive mode
Channel packages In dependency graph import() only in channel mode

Reference: Claude Code defers OpenTelemetry (~400KB + protobuf), gRPC exporters (~700KB), React components to first use.

Acceptance Criteria / 验收标准

  • OpenTelemetry not loaded when telemetry disabled
  • highlight.js not loaded until first code block rendered
  • React/Ink not loaded in non-interactive mode (-p, --stream-json)
  • Bundle size of main chunk reduced measurably

Metadata

Metadata

Assignees

Labels

category/coreCore engine and logiccategory/performancePerformance and optimizationpriority/P2Medium - Moderately impactful, noticeable problem

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions