Skip to content

feat: Refactor extra context system#9

Merged
SilverRainZ merged 8 commits intomasterfrom
refactor/extra-context
Apr 5, 2026
Merged

feat: Refactor extra context system#9
SilverRainZ merged 8 commits intomasterfrom
refactor/extra-context

Conversation

@SilverRainZ
Copy link
Copy Markdown
Member

@SilverRainZ SilverRainZ commented Apr 3, 2026

Summary

重构 extra context 系统,提供更扩展的 API:

  • 新增 ABC 层次结构:ExtraContextParsingPhaseExtraContextParsedPhaseExtraContextResolvingPhaseExtraContextGlobalExtraContext
  • 新增 @extra_context 装饰器用于注册自定义 extra context
  • Template 新增 extra 字段声明需要的 context
  • 模板通过 load('name') 显式加载 extra context

Breaking Changes

Extra context 访问方式变更:

  • 之前:{{ _doc.title }}
  • 之后:{% set _doc = load('doc') %}{{ _doc.title }}

Context 名称不再以 _ 前缀存储。

SilverRainZ and others added 7 commits April 4, 2026 00:48
- Add ABC hierarchy: ExtraContext, ParsingPhaseExtraContext,
  ParsedPhaseExtraContext, ResolvingPhaseExtraContext, GlobalExtraContext
- Add @extra_context decorator for registering custom extra context
- Template now has extra field to declare required context
- Templates use load('name') to access extra context
- Registry integration via REGISTRY.source
- Update documentation and examples

BREAKING CHANGE: Extra context access changed from {{ _name }} to
{% set _name = load('name') %}; context names no longer prefixed with '_'
- Remove REGISTRY.source property (decorators handle registration)
- Remove ExtraContextRegistry, ExtraContextGenerator, ExtraContext from __all__
- Change on_anytime to take env instead of app
- Simplify ExtraContextGenerator by extracting _generate method
- Fix 'Source' -> 'Extra Context' naming in comments and docs
When template requests an extra context that is not registered or has
wrong phase type, _generate now reports an error instead of silently
skipping.

Co-Authored-By: MiMo v2 Pro <mimo@xiaomi.com>
Co-Authored-By: MiMo v2 Pro <mimo@xiaomi.com>
- ExtraContext is now a type alias instead of ABC
- Each phase class is its own ABC
- ExtraContextGenerator validates requested contexts in __init__
- Uses todo set to track and generate only requested contexts

Co-Authored-By: MiMo v2 Pro <mimo@xiaomi.com>
Test custom extra context registration with @extra_context decorator,
load() function in templates, and Template.extra field.

Co-Authored-By: MiMo v2 Pro <mimo@xiaomi.com>
Co-Authored-By: MiMo v2 Pro <mimo@xiaomi.com>
@SilverRainZ SilverRainZ force-pushed the refactor/extra-context branch from a04c547 to be8d8d4 Compare April 5, 2026 07:36
@SilverRainZ SilverRainZ force-pushed the refactor/extra-context branch from ac6c687 to ba64f28 Compare April 5, 2026 11:25
@SilverRainZ SilverRainZ merged commit a0ccb78 into master Apr 5, 2026
6 checks passed
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