Skip to content

Conversation

hezhijie0327
Copy link
Contributor

@hezhijie0327 hezhijie0327 commented Jun 16, 2025

💻 变更类型 | Change Type

  • ✨ feat
  • 🐛 fix
  • ♻️ refactor
  • 💄 style
  • 👷 build
  • ⚡️ perf
  • 📝 docs
  • 🔨 chore

🔀 变更说明 | Description of Change

引入自 https://github.com/lobehub/lobe-chat/pull/8060/files#diff-14d8a61991816c736e782edc145026da43f50f04fcfc4eabc1536515b54a5c3d


  1. 还原原有逻辑及预留值参数 {{text}},并进行内容替换处理
  2. 测试代码
今天是:{{date}}
用户输入:{{text}}
理解意图并显示分析

Screenshot
image
image

📝 补充信息 | Additional Information

close #8187

Summary by Sourcery

Fix the inputTemplate behavior in AI chat by applying the user message template replacement, removing the old parser variable, and updating related locale placeholders.

Bug Fixes:

  • Fix inputTemplate preprocessing to correctly replace '{{text}}' in user messages before adding systemRole

Enhancements:

  • Update locale placeholders to use '{{text}}' instead of '{{input_template}}'
  • Remove deprecated 'input_template' entry from VARIABLE_GENERATORS

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jun 16, 2025
Copy link
Contributor

sourcery-ai bot commented Jun 16, 2025

Reviewer's Guide

This PR fixes the inputTemplate behavior by introducing a preprocessing step that applies the user message template replacement before adding the system role, removes the deprecated parser variable, and updates locale placeholders to use the new '{{text}}' syntax.

Sequence Diagram: Updated Message Preprocessing in generateAIChat

sequenceDiagram
    participant C as Caller
    participant GAC as generateAIChat
    participant CC as chatConfig
    participant AC as agentConfig

    C->>GAC: generateAIChat(messages, chatConfig, agentConfig)
    GAC-->>GAC: Initial preprocessMsgs from messages
    GAC-->>CC: Get chatConfig.inputTemplate
    alt chatConfig.inputTemplate exists
        GAC-->>GAC: Loop through preprocessMsgs
        opt message.role is 'user'
            GAC-->>GAC: Apply inputTemplate to user message content (replace '{{text}}')
        end
    end
    GAC-->>AC: Get agentConfig.systemRole
    opt agentConfig.systemRole exists
        GAC-->>GAC: Prepend systemRole to preprocessMsgs
    end
    GAC-->>CC: Get chatConfig.enableMaxTokens
    GAC-->>AC: Potentially update agentConfig.params.max_tokens
    GAC-->>CC: Get chatConfig.enableReasoningEffort
    GAC-->>AC: Potentially update agentConfig.params.reasoning_effort
Loading

Class Diagram: Update to VARIABLE_GENERATORS Object Structure

classDiagram
  class VARIABLE_GENERATORS {
    <<Object>>
    %% The 'input_template' property has been removed.
    %% The following are existing properties after the update.
    + date() : string
    + time() : string
    + timezone() : string
    + language() : string
    + platform() : string
    + user_agent() : string
    + current_year() : string
    + current_month() : string
    + current_day() : string
    + current_hour() : string
    + current_minute() : string
    + current_second() : string
    + uuid_v4() : string
    + user_display_name() : string
    + user_name() : string
    + user_id() : string
    + browser() : string
    + os() : string
    + device() : string
    + device_type() : string
  }
Loading

File-Level Changes

Change Details Files
Introduce inputTemplate preprocessing for user messages
  • Insert mapping over preprocessMsgs to replace '{{text}}' in user message content
  • Wrap replacement logic in try/catch to handle errors gracefully
  • Position template replacement before injecting the systemRole
src/store/chat/slices/aiChat/actions/generateAIChat.ts
Remove deprecated parser placeholder variable
  • Delete the 'input_template' entry from VARIABLE_GENERATORS
  • Remove unused imports related to agentChatConfigSelectors and getAgentStoreState
src/utils/client/parserPlaceholder.ts
Update locale placeholders to use '{{text}}'
  • Change placeholder string in default setting to '{{text}}'
  • Synchronize all translated locale files to use '{{text}}' instead of '{{input_template}}'
src/locales/default/setting.ts
locales/*/setting.json

Possibly linked issues

  • #0: PR fixes the user input preprocessing template not applying by correctly replacing {{text}} in user messages.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

@lobehubbot
Copy link
Member

👍 @hezhijie0327

Thank you for raising your pull request and contributing to our Community
Please make sure you have followed our contributing guidelines. We will review it as soon as possible.
If you encounter any problems, please feel free to connect with us.
非常感谢您提出拉取请求并为我们的社区做出贡献,请确保您已经遵循了我们的贡献指南,我们会尽快审查它。
如果您遇到任何问题,请随时与我们联系。

Copy link
Contributor

gru-agent bot commented Jun 16, 2025

TestGru Assignment

Summary

Link CommitId Status Reason
Detail 5ee4ed4 ✅ Finished

History Assignment

Files

File Pull Request
src/store/chat/slices/aiChat/actions/generateAIChat.ts ❌ Failed (I failed to setup the environment.)
src/utils/client/parserPlaceholder.ts ❌ Failed (I failed to setup the environment.)

Tip

You can @gru-agent and leave your feedback. TestGru will make adjustments based on your input

Copy link

vercel bot commented Jun 16, 2025

@hezhijie0327 is attempting to deploy a commit to the LobeHub Community Team on Vercel.

A member of the Team first needs to authorize it.

@dosubot dosubot bot added the 🐛 Bug Something isn't working | 缺陷 label Jun 16, 2025
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @hezhijie0327 - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `src/store/chat/slices/aiChat/actions/generateAIChat.ts:532` </location>
<code_context>
+          try {
+            return { ...m, content: chatConfig.inputTemplate.replaceAll('{{text}}', m.content) };
+          } catch (error) {
+            console.error(error);
+
+            return m;
</code_context>

<issue_to_address>
Provide more context when logging template errors

Add a descriptive message or use structured logging to indicate which template failed and the reason for the error.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
            console.error(error);

            return m;
=======
            console.error(
              "Error applying inputTemplate in aiChat: Failed to replace '{{text}}' in template.",
              {
                template: chatConfig.inputTemplate,
                messageContent: m.content,
                error,
              }
            );

            return m;
>>>>>>> REPLACE

</suggested_fix>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +532 to +539
console.error(error);

return m;
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Provide more context when logging template errors

Add a descriptive message or use structured logging to indicate which template failed and the reason for the error.

Suggested change
console.error(error);
return m;
console.error(
"Error applying inputTemplate in aiChat: Failed to replace '{{text}}' in template.",
{
template: chatConfig.inputTemplate,
messageContent: m.content,
error,
}
);
return m;

Comment on lines 525 to 544
preprocessMsgs = !chatConfig.inputTemplate
? preprocessMsgs
: preprocessMsgs.map((m) => {
if (m.role === 'user') {
try {
return { ...m, content: chatConfig.inputTemplate.replaceAll('{{text}}', m.content) };
} catch (error) {
console.error(error);

return m;
}
}

return m;
});
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (code-quality): Invert ternary operator to remove negation (invert-ternary)

Suggested change
preprocessMsgs = !chatConfig.inputTemplate
? preprocessMsgs
: preprocessMsgs.map((m) => {
if (m.role === 'user') {
try {
return { ...m, content: chatConfig.inputTemplate.replaceAll('{{text}}', m.content) };
} catch (error) {
console.error(error);
return m;
}
}
return m;
});
preprocessMsgs = chatConfig.inputTemplate ? preprocessMsgs.map((m) => {
if (m.role === 'user') {
try {
return { ...m, content: chatConfig.inputTemplate.replaceAll('{{text}}', m.content) };
} catch (error) {
console.error(error);
return m;
}
}
return m;
}) : preprocessMsgs;


ExplanationNegated conditions are more difficult to read than positive ones, so it is best
to avoid them where we can. By inverting the ternary condition and swapping the
expressions we can simplify the code.

Copy link

codecov bot commented Jun 16, 2025

Codecov Report

Attention: Patch coverage is 31.25000% with 11 lines in your changes missing coverage. Please review.

Project coverage is 87.83%. Comparing base (9c4e2c0) to head (b16e63e).
Report is 31 commits behind head on main.

Files with missing lines Patch % Lines
...store/chat/slices/aiChat/actions/generateAIChat.ts 31.25% 11 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8204      +/-   ##
==========================================
- Coverage   87.84%   87.83%   -0.02%     
==========================================
  Files         836      836              
  Lines       62181    62196      +15     
  Branches     4134     3938     -196     
==========================================
+ Hits        54624    54628       +4     
- Misses       7557     7568      +11     
Flag Coverage Δ
app 87.83% <31.25%> (-0.02%) ⬇️
server 95.67% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Jun 16, 2025
@sxjeru
Copy link
Contributor

sxjeru commented Jun 16, 2025

如果之前的用户消息也会处理占位符的话,这个配置项的描述可能要改一下,比如:用户发送的消息均会遵循此模板处理。而不只是最新的一条消息。

@lobehubbot
Copy link
Member

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


If the previous user messages also process placeholders, the description of this configuration item may need to be changed, for example: all messages sent by the user will be filled with this template. And not just the latest news.

@hezhijie0327
Copy link
Contributor Author

如果之前的用户消息也会处理占位符的话,这个配置项的描述可能要改一下,比如:用户发送的消息均会填充到此模板。而不只是最新的一条消息。

lobehub/lobe-chat-pglite:1.91.3 做了测试,符合原始逻辑,现有描述 用户最新的一条消息会填充到此模板 揣摩下似乎没啥问题?
image

@lobehubbot
Copy link
Member

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


If the previous user messages also process placeholders, the description of this configuration item may need to be changed, for example: all messages sent by the user will be filled with this template. And not just the latest news.

I tested it with lobehub/lobe-chat-pglite:1.91.3, which conforms to the original logic. The existing description The latest message from the user will be filled with this template. It seems that there is no problem?
image

@sxjeru
Copy link
Contributor

sxjeru commented Jun 16, 2025

现在对 {{text}} 的替换处理是包含过去用户的历史消息的,这样确实更符合直觉,就是好像没有在描述里体现出来。

单看描述好像只对最新的用户消息处理,以前的则保持原样,不经预处理模板。

比如图中的“你好呀”是历史用户消息,仍然被处理为了“将 你好呀 翻译成英文”。

@lobehubbot
Copy link
Member

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


The replacement process for {{text}} now contains historical messages from past users, which is indeed more intuitional, that is, it seems that it is not reflected in the description.

It seems that the description is only processed for the latest user messages, while the previous ones remain the same without preprocessing the template.

For example, the "Hello" in the picture is a historical user message, but it is still processed to "translate Hello to English".

Copy link

vercel bot commented Jun 20, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lobe-chat-preview ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 20, 2025 3:39am

@arvinxx arvinxx merged commit 61c2c3c into lobehub:main Jun 20, 2025
16 of 18 checks passed
@lobehubbot
Copy link
Member

❤️ Great PR @hezhijie0327 ❤️

The growth of project is inseparable from user feedback and contribution, thanks for your contribution! If you are interesting with the lobehub developer community, please join our discord and then dm @arvinxx or @canisminor1990. They will invite you to our private developer channel. We are talking about the lobe-chat development or sharing ai newsletter around the world.
项目的成长离不开用户反馈和贡献,感谢您的贡献! 如果您对 LobeHub 开发者社区感兴趣,请加入我们的 discord,然后私信 @arvinxx@canisminor1990。他们会邀请您加入我们的私密开发者频道。我们将会讨论关于 Lobe Chat 的开发,分享和讨论全球范围内的 AI 消息。

github-actions bot pushed a commit that referenced this pull request Jun 20, 2025
## [Version&nbsp;1.96.0](v1.95.0...v1.96.0)
<sup>Released on **2025-06-20**</sup>

#### ✨ Features

- **misc**: Add v0 (Vercel) provider support.

#### 🐛 Bug Fixes

- **misc**: Fix inputTemplate behavior.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### What's improved

* **misc**: Add v0 (Vercel) provider support, closes [#8235](#8235) ([5842a18](5842a18))

#### What's fixed

* **misc**: Fix inputTemplate behavior, closes [#8204](#8204) ([61c2c3c](61c2c3c))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>
@lobehubbot
Copy link
Member

🎉 This PR is included in version 1.96.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@hezhijie0327 hezhijie0327 deleted the input branch June 20, 2025 22:02
github-actions bot pushed a commit to jaworldwideorg/OneJA-Bot that referenced this pull request Jun 25, 2025
## [Version&nbsp;1.95.0](v1.94.5...v1.95.0)
<sup>Released on **2025-06-25**</sup>

#### ✨ Features

- **misc**: Add Brave & Google PSE & Kagi as build-in Search Provider, add v0 (Vercel) provider support.

#### 🐛 Bug Fixes

- **misc**: Fix `MiniMax-M1` reasoning tag missing, fix inputTemplate behavior, Google Gemini tools declarations, Remove unsupported parameters of Hunyuan.

#### 💄 Styles

- **openrouter**: Add stable versions of Gemini 2.5 models.
- **misc**: Add `blockAds` & `stealth` params for Browserless, Optimized Gemini thinkingBudget configuration, update i18n, update i18n.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### What's improved

* **misc**: Add Brave & Google PSE & Kagi as build-in Search Provider, closes [lobehub#8172](https://github.com/jaworldwideorg/OneJA-Bot/issues/8172) ([16ae521](16ae521))
* **misc**: Add v0 (Vercel) provider support, closes [lobehub#8235](https://github.com/jaworldwideorg/OneJA-Bot/issues/8235) ([5842a18](5842a18))

#### What's fixed

* **misc**: Fix `MiniMax-M1` reasoning tag missing, closes [lobehub#8240](https://github.com/jaworldwideorg/OneJA-Bot/issues/8240) ([ea76c11](ea76c11))
* **misc**: Fix inputTemplate behavior, closes [lobehub#8204](https://github.com/jaworldwideorg/OneJA-Bot/issues/8204) ([61c2c3c](61c2c3c))
* **misc**: Google Gemini tools declarations, closes [lobehub#8256](https://github.com/jaworldwideorg/OneJA-Bot/issues/8256) ([08f5d73](08f5d73))
* **misc**: Remove unsupported parameters of Hunyuan, closes [lobehub#8247](https://github.com/jaworldwideorg/OneJA-Bot/issues/8247) ([826d724](826d724))

#### Styles

* **openrouter**: Add stable versions of Gemini 2.5 models, closes [lobehub#8239](https://github.com/jaworldwideorg/OneJA-Bot/issues/8239) ([d34ecab](d34ecab))
* **misc**: Add `blockAds` & `stealth` params for Browserless, closes [lobehub#8255](https://github.com/jaworldwideorg/OneJA-Bot/issues/8255) ([2ff3efa](2ff3efa))
* **misc**: Optimized Gemini thinkingBudget configuration, closes [lobehub#8224](https://github.com/jaworldwideorg/OneJA-Bot/issues/8224) ([03625e8](03625e8))
* **misc**: Update i18n, closes [lobehub#8253](https://github.com/jaworldwideorg/OneJA-Bot/issues/8253) ([b86dc9b](b86dc9b))
* **misc**: Update i18n, closes [lobehub#8242](https://github.com/jaworldwideorg/OneJA-Bot/issues/8242) ([2d1babc](2d1babc))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>
AirboZH pushed a commit to yuanze-dev/lobe-chat that referenced this pull request Jul 9, 2025
AirboZH pushed a commit to yuanze-dev/lobe-chat that referenced this pull request Jul 9, 2025
## [Version&nbsp;1.96.0](lobehub/lobe-chat@v1.95.0...v1.96.0)
<sup>Released on **2025-06-20**</sup>

#### ✨ Features

- **misc**: Add v0 (Vercel) provider support.

#### 🐛 Bug Fixes

- **misc**: Fix inputTemplate behavior.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### What's improved

* **misc**: Add v0 (Vercel) provider support, closes [lobehub#8235](lobehub#8235) ([5842a18](lobehub@5842a18))

#### What's fixed

* **misc**: Fix inputTemplate behavior, closes [lobehub#8204](lobehub#8204) ([61c2c3c](lobehub@61c2c3c))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>
bbbugg pushed a commit to bbbugg/lobe-chat that referenced this pull request Aug 14, 2025
bbbugg pushed a commit to bbbugg/lobe-chat that referenced this pull request Aug 14, 2025
## [Version&nbsp;1.96.0](lobehub/lobe-chat@v1.95.0...v1.96.0)
<sup>Released on **2025-06-20**</sup>

#### ✨ Features

- **misc**: Add v0 (Vercel) provider support.

#### 🐛 Bug Fixes

- **misc**: Fix inputTemplate behavior.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### What's improved

* **misc**: Add v0 (Vercel) provider support, closes [lobehub#8235](lobehub#8235) ([d00e2f5](lobehub@d00e2f5))

#### What's fixed

* **misc**: Fix inputTemplate behavior, closes [lobehub#8204](lobehub#8204) ([411607f](lobehub@411607f))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Bug Something isn't working | 缺陷 released size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

用户输入预处理未生效
4 participants