forked from lobehub/lobe-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
💄 style: add
SYSTEM_AGENT
env (lobehub#2694)
- Loading branch information
Showing
6 changed files
with
147 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { parseSystemAgent } from './parseSystemAgent'; | ||
|
||
describe('parseSystemAgent', () => { | ||
it('should parse a valid environment variable string correctly', () => { | ||
const envValue = 'topic=openai/gpt-3.5-turbo,translation=anthropic/claude-1'; | ||
const expected = { | ||
topic: { provider: 'openai', model: 'gpt-3.5-turbo' }, | ||
translation: { provider: 'anthropic', model: 'claude-1' }, | ||
}; | ||
|
||
expect(parseSystemAgent(envValue)).toEqual(expected); | ||
}); | ||
|
||
it('should handle empty environment variable string', () => { | ||
const envValue = ''; | ||
const expected = {}; | ||
|
||
expect(parseSystemAgent(envValue)).toEqual(expected); | ||
}); | ||
|
||
it('should ignore unknown keys in environment variable string', () => { | ||
const envValue = 'topic=openai/gpt-3.5-turbo,unknown=test/model'; | ||
const expected = { | ||
topic: { provider: 'openai', model: 'gpt-3.5-turbo' }, | ||
}; | ||
|
||
expect(parseSystemAgent(envValue)).toEqual(expected); | ||
}); | ||
|
||
it('should throw an error for missing model or provider values', () => { | ||
const envValue1 = 'topic=openai,translation=/claude-1'; | ||
const envValue2 = 'topic=/gpt-3.5-turbo,translation=anthropic/'; | ||
|
||
expect(() => parseSystemAgent(envValue1)).toThrowError(/Missing model or provider/); | ||
expect(() => parseSystemAgent(envValue2)).toThrowError(/Missing model or provider/); | ||
}); | ||
|
||
it('should throw an error for invalid environment variable format', () => { | ||
const envValue = 'topic-openai/gpt-3.5-turbo'; | ||
|
||
expect(() => parseSystemAgent(envValue)).toThrowError(/Invalid environment variable format/); | ||
}); | ||
|
||
it('should handle provider or model names with special characters', () => { | ||
const envValue = 'topic=openrouter/mistralai/mistral-7b-instruct:free'; | ||
const expected = { | ||
topic: { provider: 'openrouter', model: 'mistralai/mistral-7b-instruct:free' }, | ||
}; | ||
|
||
expect(parseSystemAgent(envValue)).toEqual(expected); | ||
}); | ||
|
||
it('should handle extra whitespace in environment variable string', () => { | ||
const envValue = ' topic = openai/gpt-3.5-turbo , translation = anthropic/claude-1 '; | ||
const expected = { | ||
topic: { provider: 'openai', model: 'gpt-3.5-turbo' }, | ||
translation: { provider: 'anthropic', model: 'claude-1' }, | ||
}; | ||
|
||
expect(parseSystemAgent(envValue)).toEqual(expected); | ||
}); | ||
|
||
it('should handle full-width comma in environment variable string', () => { | ||
const envValue = 'topic=openai/gpt-3.5-turbo,translation=anthropic/claude-1'; | ||
const expected = { | ||
topic: { provider: 'openai', model: 'gpt-3.5-turbo' }, | ||
translation: { provider: 'anthropic', model: 'claude-1' }, | ||
}; | ||
|
||
expect(parseSystemAgent(envValue)).toEqual(expected); | ||
}); | ||
|
||
it('should handle extra whitespace around provider and model names', () => { | ||
const envValue = 'topic= openai / gpt-3.5-turbo ,translation= anthropic / claude-1 '; | ||
const expected = { | ||
topic: { provider: 'openai', model: 'gpt-3.5-turbo' }, | ||
translation: { provider: 'anthropic', model: 'claude-1' }, | ||
}; | ||
|
||
expect(parseSystemAgent(envValue)).toEqual(expected); | ||
}); | ||
|
||
it('should handle an excessively long environment variable string', () => { | ||
const longProviderName = 'a'.repeat(100); | ||
const longModelName = 'b'.repeat(100); | ||
const envValue = `topic=${longProviderName}/${longModelName}`; | ||
const expected = { | ||
topic: { provider: longProviderName, model: longModelName }, | ||
}; | ||
|
||
expect(parseSystemAgent(envValue)).toEqual(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { DEFAULT_SYSTEM_AGENT_CONFIG } from '@/const/settings'; | ||
import { UserSystemAgentConfig } from '@/types/user/settings'; | ||
|
||
const protectedKeys = Object.keys(DEFAULT_SYSTEM_AGENT_CONFIG); | ||
|
||
export const parseSystemAgent = (envString: string = ''): Partial<UserSystemAgentConfig> => { | ||
if (!envString) return {}; | ||
|
||
const config: Partial<UserSystemAgentConfig> = {}; | ||
|
||
// 处理全角逗号和多余空格 | ||
let envValue = envString.replaceAll(',', ',').trim(); | ||
|
||
const pairs = envValue.split(','); | ||
|
||
for (const pair of pairs) { | ||
const [key, value] = pair.split('=').map((s) => s.trim()); | ||
|
||
if (key && value) { | ||
const [provider, ...modelParts] = value.split('/'); | ||
const model = modelParts.join('/'); | ||
|
||
if (!provider || !model) { | ||
throw new Error('Missing model or provider value'); | ||
} | ||
|
||
if (protectedKeys.includes(key)) { | ||
config[key as keyof UserSystemAgentConfig] = { | ||
model: model.trim(), | ||
provider: provider.trim(), | ||
}; | ||
} | ||
} else { | ||
throw new Error('Invalid environment variable format'); | ||
} | ||
} | ||
|
||
return config; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters