Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Fix: Validate the SofyaSearch API key in the agent tool. (#20945)

balibabucf25b92eef28
diff --git a/web/src/locales/en.ts b/web/src/locales/en.ts
index 1796d8bf9e..6cfc36c667 100644
--- a/web/src/locales/en.ts
+++ b/web/src/locales/en.ts
@@ -3565,6 +3565,8 @@ This process aggregates variables from multiple branches into a single variable
tokenizer: 'Indexer',
tokenizerRequired: 'Please add the Indexer node first',
nodeFormInvalid: 'Invalid settings, please fix them first',
+ apiKeyRequired: 'Please enter the API Key',
+ apiKeyMissing: 'No API key configured, please enter one first',
agentModelMissing: 'No model selected, please choose one first',
extractorModelMissing: 'No model selected, please choose one first',
extractorKeywordsTopNMissing:
diff --git a/web/src/locales/zh.ts b/web/src/locales/zh.ts
index 7e859ea13d..5642a21f3a 100644
--- a/web/src/locales/zh.ts
+++ b/web/src/locales/zh.ts
@@ -3087,6 +3087,8 @@ NER:使用 spaCy NER 和基于规则的关键词提取来抽取 Entities 和 R
tokenizer: '索引器',
tokenizerRequired: '请先添加索引器节点',
nodeFormInvalid: '配置有误,请先修正',
+ apiKeyRequired: '请输入 API Key',
+ apiKeyMissing: '未配置 API Key,请先填写',
agentModelMissing: '未选择模型,请先选择',
extractorModelMissing: '未选择模型,请先选择',
extractorKeywordsTopNMissing:
diff --git a/web/src/pages/agent/form/components/api-key-field.tsx b/web/src/pages/agent/form/components/api-key-field.tsx
index f9debfc4f3..caba6be34e 100644
--- a/web/src/pages/agent/form/components/api-key-field.tsx
+++ b/web/src/pages/agent/form/components/api-key-field.tsx
@@ -1,32 +1,19 @@
-import {
- FormControl,
- FormField,
- FormItem,
- FormLabel,
- FormMessage,
-} from '@/components/ui/form';
+import { RAGFlowFormItem } from '@/components/ragflow-form';
import { Input } from '@/components/ui/input';
import { t } from 'i18next';
-import { useFormContext } from 'react-hook-form';
interface IApiKeyFieldProps {
placeholder?: string;
+ required?: boolean;
}
-export function ApiKeyField({ placeholder }: IApiKeyFieldProps) {
- const form = useFormContext();
+export function ApiKeyField({ placeholder, required }: IApiKeyFieldProps) {
return (
- <FormField
- control={form.control}
+ <RAGFlowFormItem
name="api_key"
- render={({ field }) => (
- <FormItem>
- <FormLabel>{t('flow.apiKey')}</FormLabel>
- <FormControl>
- <Input type="password" {...field} placeholder={placeholder}></Input>
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
+ label={t('flow.apiKey')}
+ required={required}
+ >
+ <Input type="password" placeholder={placeholder}></Input>
+ </RAGFlowFormItem>
);
}
diff --git a/web/src/pages/agent/form/search1api-form/index.tsx b/web/src/pages/agent/form/search1api-form/index.tsx
index 4cb87d8636..a0b07f644d 100644
--- a/web/src/pages/agent/form/search1api-form/index.tsx
+++ b/web/src/pages/agent/form/search1api-form/index.tsx
@@ -1,15 +1,10 @@
import { FormContainer } from '@/components/form-container';
+import { RAGFlowFormItem } from '@/components/ragflow-form';
import { TopNFormField } from '@/components/top-n-item';
-import {
- Form,
- FormControl,
- FormField,
- FormItem,
- FormLabel,
- FormMessage,
-} from '@/components/ui/form';
+import { Form } from '@/components/ui/form';
import { SelectWithSearch } from '@/components/originui/select-with-search';
import { useTranslate } from '@/hooks/common-hooks';
+import i18n from '@/locales/config';
import { zodResolver } from '@hookform/resolvers/zod';
import { memo, useEffect, useMemo } from 'react';
import { useForm, useFormContext, useWatch } from 'react-hook-form';
@@ -58,7 +53,7 @@ const Search1APIServiceLabels: Record<string, string> = {
};
export const Search1APIFormPartialSchema = {
- api_key: z.string().optional(),
+ api_key: z.string().min(1, i18n.t('flow.apiKeyRequired')),
channel: z.string().optional(),
search_service: z.string().optional(),
top_n: z.coerce.number(),
@@ -106,37 +101,24 @@ export function Search1APIWidgets() {
return (
<>
- <ApiKeyField placeholder={t('search1APIApiKeyTip')}></ApiKeyField>
- <FormField
- control={form.control}
- name={'channel'}
- render={({ field }) => (
- <FormItem>
- <FormLabel tooltip={t('search1APIChannelTip')}>
- {t('channel')}
- </FormLabel>
- <FormControl>
- <SelectWithSearch {...field} options={channelOptions} />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name={'search_service'}
- render={({ field }) => (
- <FormItem>
- <FormLabel tooltip={t('search1APIServiceTip')}>
- {t('search1APIService')}
- </FormLabel>
- <FormControl>
- <SelectWithSearch {...field} options={serviceOptions} />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
+ <ApiKeyField
+ placeholder={t('search1APIApiKeyTip')}
+ required
+ ></ApiKeyField>
+ <RAGFlowFormItem
+ name="channel"
+ label={t('channel')}
+ tooltip={t('search1APIChannelTip')}
+ >
+ <SelectWithSearch options={channelOptions} />
+ </RAGFlowFormItem>
+ <RAGFlowFormItem
+ name="search_service"
+ label={t('search1APIService')}
+ tooltip={t('search1APIServiceTip')}
+ >
+ <SelectWithSearch options={serviceOptions} />
+ </RAGFlowFormItem>
<TopNFormField max={50}></TopNFormField>
</>
);
@@ -152,6 +134,7 @@ function Search1APIForm({ node }: INextOperatorForm) {
const form = useForm<z.infer<typeof FormSchema>>({
defaultValues,
resolver: zodResolver(FormSchema),
+ mode: 'onChange',
});
useWatchFormChange(node?.id, form);
diff --git a/web/src/pages/agent/form/sofya-form/index.tsx b/web/src/pages/agent/form/sofya-form/index.tsx
index 421dc00bf5..7b28174946 100644
--- a/web/src/pages/agent/form/sofya-form/index.tsx
+++ b/web/src/pages/agent/form/sofya-form/index.tsx
@@ -1,18 +1,13 @@
import { FormContainer } from '@/components/form-container';
+import { RAGFlowFormItem } from '@/components/ragflow-form';
import { TopNFormField } from '@/components/top-n-item';
-import {
- Form,
- FormControl,
- FormField,
- FormItem,
- FormLabel,
- FormMessage,
-} from '@/components/ui/form';
+import { Form } from '@/components/ui/form';
import { SelectWithSearch } from '@/components/originui/select-with-search';
import { useTranslate } from '@/hooks/common-hooks';
+import i18n from '@/locales/config';
import { zodResolver } from '@hookform/resolvers/zod';
import { memo, useMemo } from 'react';
-import { useForm, useFormContext } from 'react-hook-form';
+import { useForm } from 'react-hook-form';
import { z } from 'zod';
import { SofyaSearchDepth, initialSofyaValues } from '../../constant';
import { useFormValues } from '../../hooks/use-form-values';
@@ -32,7 +27,7 @@ const SofyaSearchDepthLabelKeys: Record<SofyaSearchDepth, string> = {
};
export const SofyaFormPartialSchema = {
- api_key: z.string().optional(),
+ api_key: z.string().min(1, i18n.t('flow.apiKeyRequired')),
search_depth: z.string().optional(),
top_n: z.coerce.number(),
};
@@ -44,7 +39,6 @@ const FormSchema = z.object({
export function SofyaWidgets() {
const { t } = useTranslate('flow');
- const form = useFormContext();
const searchDepthOptions = useMemo(
() =>
@@ -57,22 +51,14 @@ export function SofyaWidgets() {
return (
<>
- <ApiKeyField placeholder={t('sofyaApiKeyTip')}></ApiKeyField>
- <FormField
- control={form.control}
- name={'search_depth'}
- render={({ field }) => (
- <FormItem>
- <FormLabel tooltip={t('sofyaSearchDepthTip')}>
- {t('sofyaSearchDepth')}
- </FormLabel>
- <FormControl>
- <SelectWithSearch {...field} options={searchDepthOptions} />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
+ <ApiKeyField placeholder={t('sofyaApiKeyTip')} required></ApiKeyField>
+ <RAGFlowFormItem
+ name="search_depth"
+ label={t('sofyaSearchDepth')}
+ tooltip={t('sofyaSearchDepthTip')}
+ >
+ <SelectWithSearch options={searchDepthOptions} />
+ </RAGFlowFormItem>
<TopNFormField></TopNFormField>
</>
);
@@ -86,6 +72,7 @@ function SofyaForm({ node }: INextOperatorForm) {
const form = useForm<z.infer<typeof FormSchema>>({
defaultValues,
resolver: zodResolver(FormSchema),
+ mode: 'onChange',
});
useWatchFormChange(node?.id, form);
diff --git a/web/src/pages/agent/form/tool-form/search1api-form/index.tsx b/web/src/pages/agent/form/tool-form/search1api-form/index.tsx
index 8d00479809..ad36d82631 100644
--- a/web/src/pages/agent/form/tool-form/search1api-form/index.tsx
+++ b/web/src/pages/agent/form/tool-form/search1api-form/index.tsx
@@ -20,6 +20,7 @@ function Search1APIForm() {
const form = useForm<z.infer<typeof FormSchema>>({
defaultValues: values,
resolver: zodResolver(FormSchema),
+ mode: 'onChange',
});
useWatchFormChange(form);
diff --git a/web/src/pages/agent/form/tool-form/sofya-form/index.tsx b/web/src/pages/agent/form/tool-form/sofya-form/index.tsx
index 27d80ddb8e..aa9a251cfd 100644
--- a/web/src/pages/agent/form/tool-form/sofya-form/index.tsx
+++ b/web/src/pages/agent/form/tool-form/sofya-form/index.tsx
@@ -17,6 +17,7 @@ function SofyaForm() {
const form = useForm<z.infer<typeof FormSchema>>({
defaultValues: values,
resolver: zodResolver(FormSchema),
+ mode: 'onChange',
});
useWatchFormChange(form);
diff --git a/web/src/pages/agent/utils/__tests__/canvas-checklist.test.ts b/web/src/pages/agent/utils/__tests__/canvas-checklist.test.ts
index 4fda8baadf..808ed8518a 100644
--- a/web/src/pages/agent/utils/__tests__/canvas-checklist.test.ts
+++ b/web/src/pages/agent/utils/__tests__/canvas-checklist.test.ts
@@ -817,6 +817,112 @@ describe('collectCanvasIssues: stale resources', () => {
});
});
+describe('collectCanvasIssues: required tool API key', () => {
+ it.each([
+ ['SofyaSearch', 'SofyaSearch:s1'],
+ ['Search1APISearch', 'Search1API:s2'],
+ ])('flags a standalone %s node without an API key', (label, id) => {
+ const issues = collect({
+ nodes: [makeNode(id, label, { api_key: '' })],
+ });
+
+ expect(issues).toContainEqual(
+ expect.objectContaining({
+ nodeId: id,
+ type: CanvasIssueType.MissingRequired,
+ messageKey: 'flow.apiKeyMissing',
+ }),
+ );
+ });
+
+ it.each([['SofyaSearch'], ['Search1APISearch']])(
+ 'passes a standalone %s node with an API key',
+ (label) => {
+ const issues = collect({
+ nodes: [makeNode(`${label}:s1`, label, { api_key: 'secret' })],
+ });
+
+ expect(
+ issues.some(
+ (x) =>
+ x.type === CanvasIssueType.MissingRequired &&
+ x.messageKey === 'flow.apiKeyMissing',
+ ),
+ ).toBe(false);
+ },
+ );
+
+ it('flags an agent-embedded tool without an API key on the Tool node', () => {
+ const agent = makeNode('Agent:a1', 'Agent', {
+ llm_id: 'model-1',
+ tools: [
+ {
+ component_name: 'SofyaSearch',
+ id: 'tool-1',
+ name: 'Sofya',
+ params: { api_key: '' },
+ },
+ ],
+ });
+
+ const issues = collect({
+ nodes: [agent, makeNode('Tool:t1', 'Tool')],
+ edges: [makeEdge('Agent:a1', 'Tool:t1', 'tool')],
+ });
+
+ expect(issues).toContainEqual(
+ expect.objectContaining({
+ nodeId: 'Tool:t1',
+ toolId: 'tool-1',
+ nodeName: 'Agent:a1 / Sofya',
+ operatorLabel: 'SofyaSearch',
+ type: CanvasIssueType.MissingRequired,
+ messageKey: 'flow.apiKeyMissing',
+ }),
+ );
+ });
+
+ it('flags an agent-embedded tool with no params object at all', () => {
+ const agent = makeNode('Agent:a1', 'Agent', {
+ llm_id: 'model-1',
+ tools: [{ component_name: 'Search1APISearch', id: 'tool-2' }],
+ });
+
+ const issues = collect({ nodes: [agent] });
+
+ expect(issues).toContainEqual(
+ expect.objectContaining({
+ nodeId: 'Agent:a1',
+ toolId: undefined,
+ nodeName: 'Agent:a1 / Search1APISearch',
+ type: CanvasIssueType.MissingRequired,
+ messageKey: 'flow.apiKeyMissing',
+ }),
+ );
+ });
+
+ it('passes an agent-embedded tool with an API key and skips other tools', () => {
+ const agent = makeNode('Agent:a1', 'Agent', {
+ llm_id: 'model-1',
+ tools: [
+ {
+ component_name: 'SofyaSearch',
+ id: 'tool-1',
+ name: 'Sofya',
+ params: { api_key: 'secret' },
+ },
+ { component_name: 'ExeSQL', id: 'tool-2', params: {} },
+ ],
+ });
+
+ const issues = collect({ nodes: [agent] });
+
+ expect(
+ issues.filter((x) => x.messageKey === 'flow.apiKeyMissing'),
+ ).toHaveLength(0);
+ });
+});
+
describe('collectCanvasIssues: agent-embedded retrieval tools', () => {
const agentWithTool = () =>
makeNode('Agent:a1', 'Agent', {
diff --git a/web/src/pages/agent/utils/canvas-checklist.ts b/web/src/pages/agent/utils/canvas-checklist.ts
index dbf0f7f22e..1994f14f4e 100644
--- a/web/src/pages/agent/utils/canvas-checklist.ts
+++ b/web/src/pages/agent/utils/canvas-checklist.ts
@@ -95,6 +95,13 @@ const OrphanExemptOperators: string[] = [
Operator.ExitLoop,
];
+// Tools whose API key is required by the service, on the canvas and inside an
+// agent's tool list alike — an empty key always fails at run time.
+const ApiKeyRequiredOperators: string[] = [
+ Operator.SofyaSearch,
+ Operator.Search1APISearch,
+];
+
function collectStrings(value: unknown): string[] {
if (typeof value === 'string') {
return [value];
@@ -504,6 +511,14 @@ export function collectCanvasIssues({
});
}
+ if (ApiKeyRequiredOperators.includes(label) && !form?.api_key) {
+ issues.push({
+ ...target,
+ type: CanvasIssueType.MissingRequired,
+ messageKey: 'flow.apiKeyMissing',
+ });
+ }
+
if (label === Operator.Retrieval) {
issues.push(
...collectRetrievalBindingIssues(form, target, {
@@ -526,7 +541,11 @@ export function collectCanvasIssues({
if (Array.isArray(form?.tools)) {
const toolNode = findToolCanvasNode(edges, nodeMap, node.id);
for (const tool of form.tools) {
- if (tool?.component_name !== Operator.Retrieval) {
+ const isRetrieval = tool?.component_name === Operator.Retrieval;
+ const needsApiKey = ApiKeyRequiredOperators.includes(
+ tool?.component_name,
+ );
+ if (!isRetrieval && !needsApiKey) {
continue;
}
// Point at the concrete tool under the concrete agent — the Tool
@@ -541,12 +560,21 @@ export function collectCanvasIssues({
}`,
operatorLabel: tool.component_name ?? Operator.Tool,
};
- issues.push(
- ...collectRetrievalBindingIssues(tool?.params, toolTarget, {
- memoryIds,
- staleDatasetIds,
- }),
- );
+ if (isRetrieval) {
+ issues.push(
+ ...collectRetrievalBindingIssues(tool?.params, toolTarget, {
+ memoryIds,
+ staleDatasetIds,
+ }),
+ );
+ }
+ if (needsApiKey && !tool?.params?.api_key) {
+ issues.push({
+ ...toolTarget,
+ type: CanvasIssueType.MissingRequired,
+ messageKey: 'flow.apiKeyMissing',
+ });
+ }
}
}
}