Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: should inject the user Locals Language into systemRole (#13911)

LiJian2824c826bddc
diff --git a/src/services/chat/mecha/agentConfigResolver.test.ts b/src/services/chat/mecha/agentConfigResolver.test.ts
index b93de6289d1..ac7bef7680d 100644
--- a/src/services/chat/mecha/agentConfigResolver.test.ts
+++ b/src/services/chat/mecha/agentConfigResolver.test.ts
@@ -9,6 +9,7 @@ import * as agentStore from '@/store/agent';
import * as agentSelectors from '@/store/agent/selectors';
import * as agentGroupStore from '@/store/agentGroup';
import * as agentGroupSelectors from '@/store/agentGroup/selectors';
+import * as userSelectors from '@/store/user/selectors';
import { resolveAgentConfig } from './agentConfigResolver';
@@ -105,7 +106,10 @@ describe('resolveAgentConfig', () => {
it('should return agent config and chat config correctly', () => {
const result = resolveAgentConfig({ agentId: 'test-agent' });
- expect(result.agentConfig).toEqual(mockAgentConfig);
+ // systemRole should have locale appended (currentResponseLanguage falls back to browser locale)
+ expect(result.agentConfig.systemRole).toContain('You are a helpful assistant');
+ expect(result.agentConfig.model).toBe(mockAgentConfig.model);
+ expect(result.agentConfig.plugins).toEqual(mockAgentConfig.plugins);
expect(result.chatConfig).toEqual(mockChatConfig);
});
@@ -561,8 +565,9 @@ describe('resolveAgentConfig', () => {
expect(result.agentConfig.systemRole).toContain('You are a helpful assistant');
expect(result.agentConfig.systemRole).toContain('Page agent system prompt');
+ // Locale instruction is injected between custom role and page-agent role
expect(result.agentConfig.systemRole).toMatch(
- /You are a helpful assistant\n\nPage agent system prompt/,
+ /You are a helpful assistant[\s\S]*Page agent system prompt/,
);
});
@@ -580,7 +585,7 @@ describe('resolveAgentConfig', () => {
scope: 'page',
});
- expect(result.agentConfig.systemRole).toBe(
+ expect(result.agentConfig.systemRole).toContain(
'Page agent system prompt with XML instructions...',
);
});
@@ -676,9 +681,9 @@ describe('resolveAgentConfig', () => {
scope: 'page',
});
- // Should still inject PageAgentIdentifier but with empty systemRole
+ // Should still inject PageAgentIdentifier but with no page-agent systemRole
expect(result.plugins).toContain(PageAgentIdentifier);
- expect(result.agentConfig.systemRole.trim()).toBe('You are a helpful assistant');
+ expect(result.agentConfig.systemRole).toContain('You are a helpful assistant');
expect(result.chatConfig.enableHistoryCount).toBe(false);
});
@@ -694,7 +699,7 @@ describe('resolveAgentConfig', () => {
});
expect(result.plugins).toContain(PageAgentIdentifier);
- expect(result.agentConfig.systemRole.trim()).toBe('You are a helpful assistant');
+ expect(result.agentConfig.systemRole).toContain('You are a helpful assistant');
expect(result.chatConfig.enableHistoryCount).toBe(false);
});
@@ -1169,9 +1174,61 @@ describe('resolveAgentConfig', () => {
// Only plugins should be empty, other config should be preserved
expect(result.plugins).toEqual([]);
- expect(result.agentConfig).toEqual(mockAgentConfig);
+ expect(result.agentConfig.systemRole).toContain('You are a helpful assistant');
+ expect(result.agentConfig.model).toBe(mockAgentConfig.model);
+ expect(result.agentConfig.plugins).toEqual(mockAgentConfig.plugins);
expect(result.chatConfig).toEqual(mockChatConfig);
expect(result.isBuiltinAgent).toBe(false);
});
});
+
+ describe('response language injection for regular agents', () => {
+ beforeEach(() => {
+ vi.spyOn(agentSelectors.agentSelectors, 'getAgentSlugById').mockReturnValue(() => undefined);
+ });
+
+ it('should append response language to systemRole when userLocale is set', () => {
+ vi.spyOn(
+ userSelectors.userGeneralSettingsSelectors,
+ 'currentResponseLanguage',
+ ).mockReturnValue('zh-CN');
+
+ const result = resolveAgentConfig({ agentId: 'test-agent' });
+
+ expect(result.agentConfig.systemRole).toBe(
+ 'You are a helpful assistant\n\nPreferred reply language: zh-CN. Use this language unless the user explicitly asks to switch.',
+ );
+ });
+
+ it('should use locale instruction as systemRole when agent has no systemRole', () => {
+ vi.spyOn(
+ userSelectors.userGeneralSettingsSelectors,
+ 'currentResponseLanguage',
+ ).mockReturnValue('ja-JP');
+ vi.spyOn(agentSelectors.agentSelectors, 'getAgentConfigById').mockReturnValue(
+ () =>
+ ({
+ ...mockAgentConfig,
+ systemRole: '',
+ }) as any,
+ );
+
+ const result = resolveAgentConfig({ agentId: 'test-agent' });
+
+ expect(result.agentConfig.systemRole).toBe(
+ 'Preferred reply language: ja-JP. Use this language unless the user explicitly asks to switch.',
+ );
+ });
+
+ it('should not modify systemRole when userLocale is falsy', () => {
+ vi.spyOn(
+ userSelectors.userGeneralSettingsSelectors,
+ 'currentResponseLanguage',
+ ).mockReturnValue(undefined as any);
+
+ const result = resolveAgentConfig({ agentId: 'test-agent' });
+
+ expect(result.agentConfig.systemRole).toBe('You are a helpful assistant');
+ });
+ });
});
diff --git a/src/services/chat/mecha/agentConfigResolver.ts b/src/services/chat/mecha/agentConfigResolver.ts
index 979e63b237c..a7ecc938122 100644
--- a/src/services/chat/mecha/agentConfigResolver.ts
+++ b/src/services/chat/mecha/agentConfigResolver.ts
@@ -228,8 +228,24 @@ export const resolveAgentConfig = (ctx: AgentConfigResolverContext): ResolvedAge
// Regular agent - use provided plugins if available, fallback to agent's plugins
const finalPlugins = plugins && plugins.length > 0 ? plugins : basePlugins;
+ // Inject response language preference into system role for regular agents
+ const userLocale = userGeneralSettingsSelectors.currentResponseLanguage(
+ useUserStore.getState(),
+ );
+ const localeInstruction = userLocale
+ ? `Preferred reply language: ${userLocale}. Use this language unless the user explicitly asks to switch.`
+ : '';
+ const systemRoleWithLocale = localeInstruction
+ ? agentConfig.systemRole
+ ? `${agentConfig.systemRole}\n\n${localeInstruction}`
+ : localeInstruction
+ : agentConfig.systemRole;
+
// Apply params adjustments based on chatConfig
- let finalAgentConfig = applyParamsFromChatConfig(agentConfig, chatConfig);
+ let finalAgentConfig = applyParamsFromChatConfig(
+ { ...agentConfig, systemRole: systemRoleWithLocale },
+ chatConfig,
+ );
let finalChatConfig = chatConfig;
// === Page Editor Auto-Injection ===
@@ -245,13 +261,13 @@ export const resolveAgentConfig = (ctx: AgentConfigResolverContext): ResolvedAge
const pageAgentRuntime = getAgentRuntimeConfig(BUILTIN_AGENT_SLUGS.pageAgent, {});
const pageAgentSystemRole = pageAgentRuntime?.systemRole || '';
- // 3. Merge system roles: custom agent's role + page-agent role
+ // 3. Merge system roles: custom agent's role (with locale) + page-agent role
// Only append page-agent role if it exists
const mergedSystemRole = pageAgentSystemRole
- ? agentConfig.systemRole
- ? `${agentConfig.systemRole}\n\n${pageAgentSystemRole}`
+ ? systemRoleWithLocale
+ ? `${systemRoleWithLocale}\n\n${pageAgentSystemRole}`
: pageAgentSystemRole
- : agentConfig.systemRole || '';
+ : systemRoleWithLocale || '';
finalAgentConfig = {
...finalAgentConfig,