Ver código fonte

fix: code-based extension (#1477)

zxhlyh 1 ano atrás
pai
commit
7b26c9e2ef

+ 2 - 2
web/app/components/app/configuration/toolbox/moderation/form-generation.tsx

@@ -60,8 +60,8 @@ const FormGeneration: FC<FormGenerationProps> = ({
                   defaultValue={value?.[form.variable]}
                   items={form.options.map((option) => {
                     return {
-                      value: option,
-                      name: option,
+                      name: option.label[locale === 'zh-Hans' ? 'zh-Hans' : 'en-US'],
+                      value: option.value,
                     }
                   })}
                   onSelect={item => handleFormChange(form.variable, item.value as string)}

+ 11 - 2
web/app/components/app/configuration/toolbox/moderation/moderation-setting-modal.tsx

@@ -91,10 +91,19 @@ const ModerationSettingModal: FC<ModerationSettingModalProps> = ({
   const currentProvider = providers.find(provider => provider.key === localeData.type)
 
   const handleDataTypeChange = (type: string) => {
+    let config: undefined | Record<string, any>
+    const currProvider = providers.find(provider => provider.key === type)
+
+    if (systemTypes.findIndex(t => t === type) < 0 && currProvider?.form_schema) {
+      config = currProvider?.form_schema.reduce((prev, next) => {
+        prev[next.variable] = next.default
+        return prev
+      }, {} as Record<string, any>)
+    }
     setLocaleData({
       ...localeData,
       type,
-      config: undefined,
+      config,
     })
   }
 
@@ -198,7 +207,7 @@ const ModerationSettingModal: FC<ModerationSettingModalProps> = ({
 
     if (systemTypes.findIndex(t => t === localeData.type) < 0 && currentProvider?.form_schema) {
       for (let i = 0; i < currentProvider.form_schema.length; i++) {
-        if (!localeData.config?.[currentProvider.form_schema[i].variable]) {
+        if (!localeData.config?.[currentProvider.form_schema[i].variable] && currentProvider.form_schema[i].required) {
           notify({
             type: 'error',
             message: t('appDebug.errorMessage.valueOfVarRequired', { key: locale === 'en' ? currentProvider.form_schema[i].label['en-US'] : currentProvider.form_schema[i].label['zh-Hans'] }),

+ 11 - 2
web/app/components/app/configuration/tools/external-data-tool-modal.tsx

@@ -67,10 +67,19 @@ const ExternalDataToolModal: FC<ExternalDataToolModalProps> = ({
   const currentProvider = providers.find(provider => provider.key === localeData.type)
 
   const handleDataTypeChange = (type: string) => {
+    let config: undefined | Record<string, any>
+    const currProvider = providers.find(provider => provider.key === type)
+
+    if (systemTypes.findIndex(t => t === type) < 0 && currProvider?.form_schema) {
+      config = currProvider?.form_schema.reduce((prev, next) => {
+        prev[next.variable] = next.default
+        return prev
+      }, {} as Record<string, any>)
+    }
     setLocaleData({
       ...localeData,
       type,
-      config: undefined,
+      config,
     })
   }
 
@@ -152,7 +161,7 @@ const ExternalDataToolModal: FC<ExternalDataToolModalProps> = ({
 
     if (systemTypes.findIndex(t => t === localeData.type) < 0 && currentProvider?.form_schema) {
       for (let i = 0; i < currentProvider.form_schema.length; i++) {
-        if (!localeData.config?.[currentProvider.form_schema[i].variable]) {
+        if (!localeData.config?.[currentProvider.form_schema[i].variable] && currentProvider.form_schema[i].required) {
           notify({
             type: 'error',
             message: t('appDebug.errorMessage.valueOfVarRequired', { key: locale === 'en' ? currentProvider.form_schema[i].label['en-US'] : currentProvider.form_schema[i].label['zh-Hans'] }),

+ 1 - 1
web/i18n/lang/app-debug.en.ts

@@ -134,7 +134,7 @@ const translation = {
     },
     moderation: {
       title: 'Content moderation',
-      description: 'Content moderation',
+      description: 'Secure model output by using moderation API or maintaining a sensitive word list.',
       allEnabled: 'INPUT/OUTPUT Content Enabled',
       inputEnabled: 'INPUT Content Enabled',
       outputEnabled: 'OUTPUT Content Enabled',

+ 10 - 10
web/i18n/lang/app-debug.zh.ts

@@ -133,13 +133,13 @@ const translation = {
       title: '工具箱',
     },
     moderation: {
-      title: '内容审',
-      description: '内容审核',
-      allEnabled: '审核输入/审核输出 内容已启用',
-      inputEnabled: '审输入内容已启用',
-      outputEnabled: '审输出内容已启用',
+      title: '内容审',
+      description: '您可以调用审查 API 或者维护敏感词库来使模型更安全地输出。',
+      allEnabled: '审查输入/审查输出 内容已启用',
+      inputEnabled: '审输入内容已启用',
+      outputEnabled: '审输出内容已启用',
       modal: {
-        title: '内容审设置',
+        title: '内容审设置',
         provider: {
           title: '类别',
           openai: 'OpenAI Moderation',
@@ -155,17 +155,17 @@ const translation = {
           line: '行',
         },
         content: {
-          input: '审输入内容',
-          output: '审输出内容',
+          input: '审输入内容',
+          output: '审输出内容',
           preset: '预设回复',
           placeholder: '这里预设回复内容',
-          condition: '审核输入内容和审核输出内容至少启用一项',
+          condition: '审查输入内容和审查输出内容至少启用一项',
           fromApi: '预设回复通过 API 返回',
           errorMessage: '预设回复不能为空',
           supportMarkdown: '支持 Markdown',
         },
         openaiNotConfig: {
-          before: 'OpenAI 内容审需要在',
+          before: 'OpenAI 内容审需要在',
           after: '中配置 OpenAI API 密钥。',
         },
       },

+ 2 - 1
web/models/common.ts

@@ -214,9 +214,10 @@ export type CodeBasedExtensionForm = {
   label: I18nText
   variable: string
   required: boolean
-  options: string[]
+  options: { label: I18nText; value: string }[]
   default: string
   placeholder: string
+  max_length?: number
 }
 
 export type CodeBasedExtensionItem = {