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.
✨ feat: support plugin settings env (lobehub#821)
* ✨ feat: support plugin settings env * 📝 docs: add plugin settings document
- Loading branch information
Showing
7 changed files
with
197 additions
and
6 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
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 |
---|---|---|
@@ -1,7 +1,13 @@ | ||
import { createGatewayOnEdgeRuntime } from '@lobehub/chat-plugins-gateway'; | ||
|
||
import { parserPluginSettings } from '@/app/api/plugin/gateway/settings'; | ||
import { getServerConfig } from '@/config/server'; | ||
|
||
const pluginsIndexUrl = getServerConfig().PLUGINS_INDEX_URL; | ||
const { PLUGINS_INDEX_URL: pluginsIndexUrl, PLUGIN_SETTINGS } = getServerConfig(); | ||
|
||
export const POST = createGatewayOnEdgeRuntime({ pluginsIndexUrl }); | ||
const defaultPluginSettings = parserPluginSettings(PLUGIN_SETTINGS); | ||
|
||
export const POST = createGatewayOnEdgeRuntime({ | ||
defaultPluginSettings, | ||
pluginsIndexUrl, | ||
}); |
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,105 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { parserPluginSettings } from './settings'; | ||
|
||
describe('parserPluginSettings', () => { | ||
it('should return an empty object when input is undefined', () => { | ||
expect(parserPluginSettings()).toEqual({}); | ||
}); | ||
|
||
it('should return an empty object when input is an empty string', () => { | ||
expect(parserPluginSettings('')).toEqual({}); | ||
}); | ||
|
||
it('should parse plugin settings from a well-formed string', () => { | ||
const input = 'plugin1:key1=value1;key2=value2,plugin2:key3=value3'; | ||
const expected = { | ||
plugin1: { key1: 'value1', key2: 'value2' }, | ||
plugin2: { key3: 'value3' }, | ||
}; | ||
expect(parserPluginSettings(input)).toEqual(expected); | ||
}); | ||
|
||
it('should handle strings with Chinese commas', () => { | ||
const input = 'plugin1:key1=value1;key2=value2,plugin2:key3=value3'; | ||
const expected = { | ||
plugin1: { key1: 'value1', key2: 'value2' }, | ||
plugin2: { key3: 'value3' }, | ||
}; | ||
expect(parserPluginSettings(input)).toEqual(expected); | ||
}); | ||
|
||
it('should ignore empty segments', () => { | ||
const input = 'plugin1:key1=value1;key2=value2,,,plugin2:key3=value3'; | ||
const expected = { | ||
plugin1: { key1: 'value1', key2: 'value2' }, | ||
plugin2: { key3: 'value3' }, | ||
}; | ||
expect(parserPluginSettings(input)).toEqual(expected); | ||
}); | ||
|
||
it('should merge settings for the same pluginId', () => { | ||
const input = 'plugin1:key1=value1;key2=value2,plugin1:key3=value3;key4=value4'; | ||
const expected = { | ||
plugin1: { key1: 'value1', key2: 'value2', key3: 'value3', key4: 'value4' }, | ||
}; | ||
expect(parserPluginSettings(input)).toEqual(expected); | ||
}); | ||
|
||
it('should override previous values if the same key appears again for the same pluginId', () => { | ||
const input = 'plugin1:key1=value1;key2=value2,plugin1:key2=newValue2;key3=value3'; | ||
const expected = { | ||
plugin1: { key1: 'value1', key2: 'newValue2', key3: 'value3' }, | ||
}; | ||
expect(parserPluginSettings(input)).toEqual(expected); | ||
}); | ||
|
||
describe('error senses', () => { | ||
it('should ignore settings with incorrect key-value format', () => { | ||
const input = 'plugin1:key1=value1;incorrectFormat,plugin2:key2=value2'; | ||
const expected = { | ||
plugin1: { key1: 'value1' }, | ||
plugin2: { key2: 'value2' }, | ||
}; | ||
expect(parserPluginSettings(input)).toEqual(expected); | ||
}); | ||
|
||
it('should handle extra separators gracefully', () => { | ||
const input = 'plugin1:key1=value1==value1.1;key2=value2;,plugin2:key3=value3'; | ||
const expected = { | ||
plugin1: { key1: 'value1', key2: 'value2' }, | ||
plugin2: { key3: 'value3' }, | ||
}; | ||
expect(parserPluginSettings(input)).toEqual(expected); | ||
}); | ||
|
||
it('should ignore settings with empty keys or values', () => { | ||
const input = 'plugin1:=value1;key2=,plugin2:key3=value3'; | ||
const expected = { | ||
plugin2: { key3: 'value3' }, | ||
}; | ||
|
||
expect(parserPluginSettings(input)).toEqual(expected); | ||
}); | ||
|
||
it('should ignore leading and trailing whitespace in keys and values', () => { | ||
const input = ' plugin1 : key1 = value1 ; key2 = value2 , plugin2 : key3=value3 '; | ||
const expected = { | ||
plugin1: { key1: 'value1', key2: 'value2' }, | ||
plugin2: { key3: 'value3' }, | ||
}; | ||
expect(parserPluginSettings(input)).toEqual(expected); | ||
}); | ||
|
||
it('should handle special characters in keys and values', () => { | ||
const input = 'plugin1:key1=value1+special;key2=value2#special,plugin2:key3=value3/special'; | ||
const expected = { | ||
plugin1: { key1: 'value1+special', key2: 'value2#special' }, | ||
plugin2: { key3: 'value3/special' }, | ||
}; | ||
expect(parserPluginSettings(input)).toEqual(expected); | ||
}); | ||
|
||
// ... (additional test cases as needed) | ||
}); | ||
}); |
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,29 @@ | ||
export const parserPluginSettings = ( | ||
settingsStr?: string, | ||
): Record<string, Record<string, string>> => { | ||
if (!settingsStr) return {}; | ||
|
||
const settings = new Map(); | ||
|
||
const array = settingsStr.split(/[,,]/).filter(Boolean); | ||
|
||
for (const item of array) { | ||
const [id, pluginSettingsStr] = item.split(':'); | ||
if (!id) continue; | ||
|
||
const pluginSettingItems = pluginSettingsStr.split(';'); | ||
|
||
const cleanId = id.trim(); | ||
|
||
for (const item of pluginSettingItems) { | ||
const [key, value] = item.split('='); | ||
if (!key || !value) continue; | ||
const cleanKey = key.trim(); | ||
const cleanValue = value.trim(); | ||
|
||
settings.set(cleanId, { ...settings.get(cleanId), [cleanKey]: cleanValue }); | ||
} | ||
} | ||
|
||
return Object.fromEntries(settings.entries()); | ||
}; |
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