@lobehub/chat 0.162.4 → 0.162.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,31 @@
2
2
 
3
3
  # Changelog
4
4
 
5
+ ### [Version 0.162.5](https://github.com/lobehub/lobe-chat/compare/v0.162.4...v0.162.5)
6
+
7
+ <sup>Released on **2024-05-28**</sup>
8
+
9
+ #### 💄 Styles
10
+
11
+ - **misc**: Add `SYSTEM_AGENT` env.
12
+
13
+ <br/>
14
+
15
+ <details>
16
+ <summary><kbd>Improvements and Fixes</kbd></summary>
17
+
18
+ #### Styles
19
+
20
+ - **misc**: Add `SYSTEM_AGENT` env, closes [#2694](https://github.com/lobehub/lobe-chat/issues/2694) ([0dfcf8d](https://github.com/lobehub/lobe-chat/commit/0dfcf8d))
21
+
22
+ </details>
23
+
24
+ <div align="right">
25
+
26
+ [![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)
27
+
28
+ </div>
29
+
5
30
  ### [Version 0.162.4](https://github.com/lobehub/lobe-chat/compare/v0.162.3...v0.162.4)
6
31
 
7
32
  <sup>Released on **2024-05-28**</sup>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/chat",
3
- "version": "0.162.4",
3
+ "version": "0.162.5",
4
4
  "description": "Lobe Chat - an open-source, high-performance chatbot framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.",
5
5
  "keywords": [
6
6
  "framework",
package/src/config/app.ts CHANGED
@@ -25,6 +25,7 @@ export const getAppConfig = () => {
25
25
  AGENTS_INDEX_URL: z.string().url(),
26
26
 
27
27
  DEFAULT_AGENT_CONFIG: z.string(),
28
+ SYSTEM_AGENT: z.string().optional(),
28
29
 
29
30
  PLUGINS_INDEX_URL: z.string().url(),
30
31
  PLUGIN_SETTINGS: z.string().optional(),
@@ -44,6 +45,7 @@ export const getAppConfig = () => {
44
45
  : 'https://chat-agents.lobehub.com',
45
46
 
46
47
  DEFAULT_AGENT_CONFIG: process.env.DEFAULT_AGENT_CONFIG || '',
48
+ SYSTEM_AGENT: process.env.SYSTEM_AGENT,
47
49
 
48
50
  PLUGINS_INDEX_URL: !!process.env.PLUGINS_INDEX_URL
49
51
  ? process.env.PLUGINS_INDEX_URL
@@ -1,4 +1,4 @@
1
- import { getAppConfig } from '@/config/app';
1
+ import { appEnv, getAppConfig } from '@/config/app';
2
2
  import { fileEnv } from '@/config/file';
3
3
  import { langfuseEnv } from '@/config/langfuse';
4
4
  import { getLLMConfig } from '@/config/llm';
@@ -9,6 +9,7 @@ import {
9
9
  TogetherAIProviderCard,
10
10
  } from '@/config/modelProviders';
11
11
  import { enableNextAuth } from '@/const/auth';
12
+ import { parseSystemAgent } from '@/server/globalConfig/parseSystemAgent';
12
13
  import { GlobalServerConfig } from '@/types/serverConfig';
13
14
  import { extractEnabledModels, transformToChatModelCards } from '@/utils/parseModels';
14
15
 
@@ -51,7 +52,6 @@ export const getServerGlobalConfig = () => {
51
52
  defaultAgent: {
52
53
  config: parseAgentConfig(DEFAULT_AGENT_CONFIG),
53
54
  },
54
-
55
55
  enableUploadFileToServer: !!fileEnv.S3_SECRET_ACCESS_KEY,
56
56
  enabledAccessCode: ACCESS_CODES?.length > 0,
57
57
  enabledOAuthSSO: enableNextAuth,
@@ -114,6 +114,7 @@ export const getServerGlobalConfig = () => {
114
114
  zeroone: { enabled: ENABLED_ZEROONE },
115
115
  zhipu: { enabled: ENABLED_ZHIPU },
116
116
  },
117
+ systemAgent: parseSystemAgent(appEnv.SYSTEM_AGENT),
117
118
  telemetry: {
118
119
  langfuse: langfuseEnv.ENABLE_LANGFUSE,
119
120
  },
@@ -0,0 +1,95 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import { parseSystemAgent } from './parseSystemAgent';
4
+
5
+ describe('parseSystemAgent', () => {
6
+ it('should parse a valid environment variable string correctly', () => {
7
+ const envValue = 'topic=openai/gpt-3.5-turbo,translation=anthropic/claude-1';
8
+ const expected = {
9
+ topic: { provider: 'openai', model: 'gpt-3.5-turbo' },
10
+ translation: { provider: 'anthropic', model: 'claude-1' },
11
+ };
12
+
13
+ expect(parseSystemAgent(envValue)).toEqual(expected);
14
+ });
15
+
16
+ it('should handle empty environment variable string', () => {
17
+ const envValue = '';
18
+ const expected = {};
19
+
20
+ expect(parseSystemAgent(envValue)).toEqual(expected);
21
+ });
22
+
23
+ it('should ignore unknown keys in environment variable string', () => {
24
+ const envValue = 'topic=openai/gpt-3.5-turbo,unknown=test/model';
25
+ const expected = {
26
+ topic: { provider: 'openai', model: 'gpt-3.5-turbo' },
27
+ };
28
+
29
+ expect(parseSystemAgent(envValue)).toEqual(expected);
30
+ });
31
+
32
+ it('should throw an error for missing model or provider values', () => {
33
+ const envValue1 = 'topic=openai,translation=/claude-1';
34
+ const envValue2 = 'topic=/gpt-3.5-turbo,translation=anthropic/';
35
+
36
+ expect(() => parseSystemAgent(envValue1)).toThrowError(/Missing model or provider/);
37
+ expect(() => parseSystemAgent(envValue2)).toThrowError(/Missing model or provider/);
38
+ });
39
+
40
+ it('should throw an error for invalid environment variable format', () => {
41
+ const envValue = 'topic-openai/gpt-3.5-turbo';
42
+
43
+ expect(() => parseSystemAgent(envValue)).toThrowError(/Invalid environment variable format/);
44
+ });
45
+
46
+ it('should handle provider or model names with special characters', () => {
47
+ const envValue = 'topic=openrouter/mistralai/mistral-7b-instruct:free';
48
+ const expected = {
49
+ topic: { provider: 'openrouter', model: 'mistralai/mistral-7b-instruct:free' },
50
+ };
51
+
52
+ expect(parseSystemAgent(envValue)).toEqual(expected);
53
+ });
54
+
55
+ it('should handle extra whitespace in environment variable string', () => {
56
+ const envValue = ' topic = openai/gpt-3.5-turbo , translation = anthropic/claude-1 ';
57
+ const expected = {
58
+ topic: { provider: 'openai', model: 'gpt-3.5-turbo' },
59
+ translation: { provider: 'anthropic', model: 'claude-1' },
60
+ };
61
+
62
+ expect(parseSystemAgent(envValue)).toEqual(expected);
63
+ });
64
+
65
+ it('should handle full-width comma in environment variable string', () => {
66
+ const envValue = 'topic=openai/gpt-3.5-turbo,translation=anthropic/claude-1';
67
+ const expected = {
68
+ topic: { provider: 'openai', model: 'gpt-3.5-turbo' },
69
+ translation: { provider: 'anthropic', model: 'claude-1' },
70
+ };
71
+
72
+ expect(parseSystemAgent(envValue)).toEqual(expected);
73
+ });
74
+
75
+ it('should handle extra whitespace around provider and model names', () => {
76
+ const envValue = 'topic= openai / gpt-3.5-turbo ,translation= anthropic / claude-1 ';
77
+ const expected = {
78
+ topic: { provider: 'openai', model: 'gpt-3.5-turbo' },
79
+ translation: { provider: 'anthropic', model: 'claude-1' },
80
+ };
81
+
82
+ expect(parseSystemAgent(envValue)).toEqual(expected);
83
+ });
84
+
85
+ it('should handle an excessively long environment variable string', () => {
86
+ const longProviderName = 'a'.repeat(100);
87
+ const longModelName = 'b'.repeat(100);
88
+ const envValue = `topic=${longProviderName}/${longModelName}`;
89
+ const expected = {
90
+ topic: { provider: longProviderName, model: longModelName },
91
+ };
92
+
93
+ expect(parseSystemAgent(envValue)).toEqual(expected);
94
+ });
95
+ });
@@ -0,0 +1,39 @@
1
+ import { DEFAULT_SYSTEM_AGENT_CONFIG } from '@/const/settings';
2
+ import { UserSystemAgentConfig } from '@/types/user/settings';
3
+
4
+ const protectedKeys = Object.keys(DEFAULT_SYSTEM_AGENT_CONFIG);
5
+
6
+ export const parseSystemAgent = (envString: string = ''): Partial<UserSystemAgentConfig> => {
7
+ if (!envString) return {};
8
+
9
+ const config: Partial<UserSystemAgentConfig> = {};
10
+
11
+ // 处理全角逗号和多余空格
12
+ let envValue = envString.replaceAll(',', ',').trim();
13
+
14
+ const pairs = envValue.split(',');
15
+
16
+ for (const pair of pairs) {
17
+ const [key, value] = pair.split('=').map((s) => s.trim());
18
+
19
+ if (key && value) {
20
+ const [provider, ...modelParts] = value.split('/');
21
+ const model = modelParts.join('/');
22
+
23
+ if (!provider || !model) {
24
+ throw new Error('Missing model or provider value');
25
+ }
26
+
27
+ if (protectedKeys.includes(key)) {
28
+ config[key as keyof UserSystemAgentConfig] = {
29
+ model: model.trim(),
30
+ provider: provider.trim(),
31
+ };
32
+ }
33
+ } else {
34
+ throw new Error('Invalid environment variable format');
35
+ }
36
+ }
37
+
38
+ return config;
39
+ };
@@ -81,7 +81,9 @@ export const createCommonSlice: StateCreator<
81
81
  const serverSettings: DeepPartial<UserSettings> = {
82
82
  defaultAgent: serverConfig.defaultAgent,
83
83
  languageModel: serverConfig.languageModel,
84
+ systemAgent: serverConfig.systemAgent,
84
85
  };
86
+
85
87
  const defaultSettings = merge(get().defaultSettings, serverSettings);
86
88
 
87
89
  // merge preference
@@ -1,7 +1,11 @@
1
1
  import { DeepPartial } from 'utility-types';
2
2
 
3
3
  import { ChatModelCard } from '@/types/llm';
4
- import { UserDefaultAgent, GlobalLLMProviderKey } from '@/types/user/settings';
4
+ import {
5
+ GlobalLLMProviderKey,
6
+ UserDefaultAgent,
7
+ UserSystemAgentConfig,
8
+ } from '@/types/user/settings';
5
9
 
6
10
  export interface ServerModelProviderConfig {
7
11
  enabled?: boolean;
@@ -21,6 +25,7 @@ export interface GlobalServerConfig {
21
25
  enabledAccessCode?: boolean;
22
26
  enabledOAuthSSO?: boolean;
23
27
  languageModel?: ServerLanguageModel;
28
+ systemAgent?: DeepPartial<UserSystemAgentConfig>;
24
29
  telemetry: {
25
30
  langfuse?: boolean;
26
31
  };