Skip to content

fix(extension-variable): migrate lunar variables to before-chat and renderer providers#807

Open
Juvwxyz wants to merge 1 commit intoChatLunaLab:v1-devfrom
Juvwxyz:v1-dev
Open

fix(extension-variable): migrate lunar variables to before-chat and renderer providers#807
Juvwxyz wants to merge 1 commit intoChatLunaLab:v1-devfrom
Juvwxyz:v1-dev

Conversation

@Juvwxyz
Copy link
Copy Markdown

@Juvwxyz Juvwxyz commented Mar 31, 2026

Summary

This PR fixes variable injection in koishi-plugin-chatluna-variable-extension so lunar/holiday variables work in both the main ChatLuna
flow and chatluna-character preset rendering.

Changes

  • migrate lunar variable injection from the obsolete chatluna/chat hook to chatluna/before-chat
  • extract shared lunar/holiday variable generation into a single helper
  • register the same lunar/holiday variables through ctx.chatluna.promptRenderer.registerVariableProvider(...)
    • this makes the variables available to chatluna-character without changing its rendering flow
  • wrap latest_message function registration with ctx.effect(...)
    • ensures provider lifecycle is tied to the plugin scope and avoids stale registrations on reload

Behavior

After this change:

  • ChatLuna main chat requests still receive:
    • lunar_date
    • lunar_year
    • lunar_month
    • lunar_day
    • lunar_zodiac
    • lunar_year_ganzhi
    • current_holiday
  • chatluna-character templates can also resolve the same global lunar/holiday variables via the shared prompt renderer
  • latest_message() behavior is unchanged

Verification

  • built koishi-plugin-chatluna-variable-extension successfully:
    • yarn workspace koishi-plugin-chatluna-variable-extension build

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the registration of function and variable providers by wrapping them in ctx.effect to ensure proper lifecycle management. In lunar.ts, the event listener was updated to chatluna/before-chat, and lunar variable generation was extracted into a dedicated function. Feedback was provided regarding the latest_message provider to include a safety check for the session object and to validate that messageCount is a positive integer to prevent runtime errors.

Comment on lines +23 to +31
async (args, _, configurable) => {
const session = configurable.session as Session
const messageCount = parseInt(args[0]) || 4
const messages = collector.getMessages(
session.guildId || session.userId,
session.userId
)
return messages.slice(-messageCount).join('\n\n')
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The latest_message function provider lacks a safety check for the session object within the configurable parameter. If the renderer is invoked without a session (e.g., in some automated or template-testing contexts), this will cause a runtime error when accessing session.guildId. Additionally, it's safer to ensure messageCount is a positive integer to avoid unexpected behavior with slice when negative values are provided.

            async (args, _, configurable) => {
                const session = configurable?.session as Session
                if (!session) {
                    return ''
                }
                const messageCount = Math.max(1, parseInt(args[0]) || 4)
                const messages = collector.getMessages(
                    session.guildId || session.userId,
                    session.userId
                )
                return messages.slice(-messageCount).join('\n\n')
            }

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 31, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: d838eaa6-cb20-4dcb-bf4a-25720209708f

📥 Commits

Reviewing files that changed from the base of the PR and between 086a1f7 and f0f4849.

📒 Files selected for processing (2)
  • packages/extension-variable/src/plugins/group.ts
  • packages/extension-variable/src/plugins/lunar.ts

概览

修改了extension-variable插件中的提示词渲染器和变量提供者的注册机制。group.ts中将latest_message提供者的注册移至ctx.effect()内部,lunar.ts中重构了农历和节假日变量的生成与注册方式,并引入了buildLunarVariables()函数与DEFAULT_HOLIDAY_REGIONS常量以集中管理变量逻辑。

变化

内聚组 / 文件 摘要
提示词渲染器注册重构
packages/extension-variable/src/plugins/group.ts
latest_message prompt renderer函数提供者的注册从直接方法调用改为通过ctx.effect()包装,改变了提供者的生命周期管理方式,但提供者的回调逻辑(会话提取、消息计数解析默认为4、消息检索和连接)保持不变。
农历变量提供者重构
packages/extension-variable/src/plugins/lunar.ts
引入buildLunarVariables()函数来集中处理农历日期和节假日计算,新增DEFAULT_HOLIDAY_REGIONS常量替代内联默认值,将农历变量的生成机制从ctx.before('chatluna/chat', ...)钩子改为ctx.on('chatluna/before-chat', ...)事件处理器,并新增了显式的ctx.effect()变量提供者注册。

代码审查工作量估算

🎯 3 (中等) | ⏱️ ~20 分钟

可能相关的 PR

推荐审查者

  • dingyi222666

诗歌

🐰 Effect的魔法翻动页,

农历函数齐聚一堂,

常量不再四处游荡,

变量如约而至,

提示词焕然新生!✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed PR标题准确概括了主要变更,清晰指出了从旧hook迁移到新hook和renderer提供程序的核心改动。
Description check ✅ Passed PR描述详细阐述了变更的目的、具体改动、预期行为和验证方式,与代码改动内容高度相关。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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