Skip to content

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

fix(agent): allow shared datasets across template blocks (#20295)

Sbaaoui Idrissc939c43388c6
diff --git a/web/src/components/knowledge-base-item.tsx b/web/src/components/knowledge-base-item.tsx
index 735661b509..f0f3b8b4ef 100644
--- a/web/src/components/knowledge-base-item.tsx
+++ b/web/src/components/knowledge-base-item.tsx
@@ -149,11 +149,13 @@ export function useDisableDifferenceEmbeddingDataset(
export function KnowledgeBaseFormField({
showVariable = false,
name = 'dataset_ids',
+ label,
required = false,
ownerTenantId,
}: {
showVariable?: boolean;
name?: string;
+ label?: ReactNode;
required?: boolean;
ownerTenantId?: string;
}) {
@@ -215,7 +217,7 @@ export function KnowledgeBaseFormField({
name={name}
tooltip={t('chat.knowledgeBasesTip')}
required={required}
- label={t('chat.knowledgeBases')}
+ label={label ?? t('chat.knowledgeBases')}
>
{(field) => (
<MultiSelect
diff --git a/web/src/hooks/use-knowledge-request.ts b/web/src/hooks/use-knowledge-request.ts
index 0b830111f0..8faa7e08a2 100644
--- a/web/src/hooks/use-knowledge-request.ts
+++ b/web/src/hooks/use-knowledge-request.ts
@@ -1159,7 +1159,10 @@ export const useFetchDatasetsByIds = (
ids: string[],
ownerTenantId?: string,
) => {
- const sortedIds = useMemo(() => ids.filter(isDatasetId).sort(), [ids]);
+ const sortedIds = useMemo(
+ () => Array.from(new Set(ids.filter(isDatasetId))).sort(),
+ [ids],
+ );
const { data, isFetching: loading } = useQuery<IDataset[]>({
queryKey: KnowledgeListKeys.byIds(sortedIds),
enabled: sortedIds.length > 0,
diff --git a/web/src/pages/agents/agent-templates.tsx b/web/src/pages/agents/agent-templates.tsx
index 61eceff6e1..df6206ae84 100644
--- a/web/src/pages/agents/agent-templates.tsx
+++ b/web/src/pages/agents/agent-templates.tsx
@@ -8,7 +8,10 @@ import { IFlowTemplate } from '@/interfaces/database/agent';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { CreateAgentDialog } from './create-agent-dialog';
import { TemplateCard } from './template-card';
-import { bindUnboundRetrieval } from './template-retrieval-binding';
+import {
+ bindUnboundRetrieval,
+ collectUnboundRetrievalBindings,
+} from './template-retrieval-binding';
import { MenuItemKey, SideBar } from './template-sidebar';
export default function AgentTemplates() {
@@ -45,11 +48,21 @@ export default function AgentTemplates() {
async (payload: any) => {
const dsl = template?.dsl;
const canvasCategory = template?.canvas_category;
- const datasetIds: string[] = payload?.dataset_ids ?? [];
const memoryIds: string[] = payload?.memory_ids ?? [];
+ const datasetBlocks = collectUnboundRetrievalBindings(dsl).datasetBlocks;
+ const datasetBindings = Object.fromEntries(
+ datasetBlocks.map((block, index) => [
+ block.blockId,
+ datasetBlocks.length === 1
+ ? (payload?.dataset_ids ?? [])
+ : (payload?.dataset_bindings?.[index] ?? []),
+ ]),
+ );
const boundDsl =
- dsl && (datasetIds.length > 0 || memoryIds.length > 0)
- ? bindUnboundRetrieval(dsl, datasetIds, memoryIds)
+ dsl &&
+ (Object.values(datasetBindings).some((ids) => ids.length > 0) ||
+ memoryIds.length > 0)
+ ? bindUnboundRetrieval(dsl, datasetBindings, memoryIds)
: dsl;
const ret = await setAgent({
diff --git a/web/src/pages/agents/create-agent-dialog.tsx b/web/src/pages/agents/create-agent-dialog.tsx
index 57dd6cd0ba..13c97c2ca4 100644
--- a/web/src/pages/agents/create-agent-dialog.tsx
+++ b/web/src/pages/agents/create-agent-dialog.tsx
@@ -10,7 +10,7 @@ import { BrainCircuit, Route } from 'lucide-react';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { CreateAgentForm, CreateAgentFormProps } from './create-agent-form';
-import { countUnboundRetrieval } from './template-retrieval-binding';
+import { collectUnboundRetrievalBindings } from './template-retrieval-binding';
type CreateAgentDialogProps = CreateAgentFormProps & {
canvasCategory?: AgentCategory;
@@ -27,7 +27,8 @@ export function CreateAgentDialog({
const { t } = useTranslation();
const retrievalBindings = useMemo(
- () => (template ? countUnboundRetrieval(template.dsl) : undefined),
+ () =>
+ template ? collectUnboundRetrievalBindings(template.dsl) : undefined,
[template],
);
diff --git a/web/src/pages/agents/create-agent-form.tsx b/web/src/pages/agents/create-agent-form.tsx
index 184e830b02..42f1851cb8 100644
--- a/web/src/pages/agents/create-agent-form.tsx
+++ b/web/src/pages/agents/create-agent-form.tsx
@@ -22,7 +22,7 @@ import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router';
import { FlowType, FlowTypeConfig } from './constant';
import { NameFormField, NameFormSchema } from './name-form-field';
-import { RetrievalBindingCount } from './template-retrieval-binding';
+import { RetrievalBindings } from './template-retrieval-binding';
export type CreateAgentFormProps = IModalProps<any> & {
loading?: boolean;
@@ -30,7 +30,7 @@ export type CreateAgentFormProps = IModalProps<any> & {
// Templates may ship retrieval steps without any dataset/memory bound. When
// set, the form asks the user to bind them before creating the agent, so
// the canvas never ends up with a retrieval that fails at runtime.
- retrievalBindings?: RetrievalBindingCount;
+ retrievalBindings?: RetrievalBindings;
};
type FlowTypeCardProps = {
@@ -89,6 +89,7 @@ export const FormSchema = z.object({
description: z.string().trim().optional(),
type: z.nativeEnum(FlowType).optional(),
dataset_ids: z.array(z.string()).optional(),
+ dataset_bindings: z.array(z.array(z.string())).optional(),
memory_ids: z.array(z.string()).optional(),
});
@@ -119,16 +120,26 @@ export function CreateAgentForm({
}, [navigate]);
async function onSubmit(data: FormSchemaType) {
- if (
- (retrievalBindings?.datasetCount ?? 0) > 0 &&
- isEmpty(data.dataset_ids)
- ) {
+ const datasetBlocks = retrievalBindings?.datasetBlocks ?? [];
+ if (datasetBlocks.length === 1 && isEmpty(data.dataset_ids)) {
form.setError('dataset_ids', {
type: 'manual',
message: t('flow.retrievalDatasetRequired'),
});
return;
}
+ if (datasetBlocks.length > 1) {
+ const missingIndex = datasetBlocks.findIndex((_, index) =>
+ isEmpty(data.dataset_bindings?.[index]),
+ );
+ if (missingIndex >= 0) {
+ form.setError(`dataset_bindings.${missingIndex}`, {
+ type: 'manual',
+ message: t('flow.retrievalDatasetRequired'),
+ });
+ return;
+ }
+ }
if ((retrievalBindings?.memoryCount ?? 0) > 0 && isEmpty(data.memory_ids)) {
form.setError('memory_ids', {
type: 'manual',
@@ -142,11 +153,13 @@ export function CreateAgentForm({
}
}
- const datasetHint = retrievalBindings?.datasetCount
- ? t('flow.retrievalTemplateDatasetHint', {
- num: retrievalBindings.datasetCount,
- })
- : undefined;
+ const datasetBlocks = retrievalBindings?.datasetBlocks ?? [];
+ const datasetHint =
+ datasetBlocks.length === 1
+ ? t('flow.retrievalTemplateDatasetHint', {
+ num: 1,
+ })
+ : undefined;
const memoryHint = retrievalBindings?.memoryCount
? t('flow.retrievalTemplateMemoryHint', {
num: retrievalBindings.memoryCount,
@@ -170,12 +183,25 @@ export function CreateAgentForm({
</RAGFlowFormItem>
)}
{!isCompiler && <NameFormField></NameFormField>}
- {!isCompiler && datasetHint && (
+ {!isCompiler && datasetBlocks.length === 1 && (
<section className="space-y-4">
<p className="text-sm text-text-secondary">{datasetHint}</p>
<KnowledgeBaseFormField required showVariable={false} />
</section>
)}
+ {!isCompiler && datasetBlocks.length > 1 && (
+ <section className="space-y-4">
+ {datasetBlocks.map((block, index) => (
+ <KnowledgeBaseFormField
+ key={block.blockId}
+ required
+ showVariable={false}
+ name={`dataset_bindings.${index}`}
+ label={block.displayName}
+ />
+ ))}
+ </section>
+ )}
{!isCompiler && memoryHint && (
<section className="space-y-4">
<p className="text-sm text-text-secondary">{memoryHint}</p>
diff --git a/web/src/pages/agents/template-retrieval-binding.test.ts b/web/src/pages/agents/template-retrieval-binding.test.ts
index ed0f91a396..a7b0259003 100644
--- a/web/src/pages/agents/template-retrieval-binding.test.ts
+++ b/web/src/pages/agents/template-retrieval-binding.test.ts
@@ -1,6 +1,6 @@
import {
bindUnboundRetrieval,
- countUnboundRetrieval,
+ collectUnboundRetrievalBindings,
} from './template-retrieval-binding';
// Mirrors the "Your starter dataset chatbot" template shape: the Retrieval
@@ -16,6 +16,8 @@ const starterLikeDsl = {
llm_id: '',
tools: [
{
+ id: 'Retrieval:WarmWeeksRead',
+ name: 'Retrieval',
component_name: 'Retrieval',
params: {
retrieval_from: 'dataset',
@@ -37,6 +39,8 @@ const starterLikeDsl = {
llm_id: '',
tools: [
{
+ id: 'Retrieval:WarmOwlsCough',
+ name: 'Retrieval',
component_name: 'Retrieval',
params: {
retrieval_from: 'dataset',
@@ -51,10 +55,15 @@ const starterLikeDsl = {
},
};
-describe('countUnboundRetrieval', () => {
- it('counts every unbound dataset retrieval in both DSL views', () => {
- expect(countUnboundRetrieval(starterLikeDsl as any)).toEqual({
- datasetCount: 2,
+describe('collectUnboundRetrievalBindings', () => {
+ it('counts a retrieval mirrored across both DSL views once', () => {
+ expect(collectUnboundRetrievalBindings(starterLikeDsl as any)).toEqual({
+ datasetBlocks: [
+ {
+ blockId: 'Retrieval:WarmWeeksRead',
+ displayName: 'Retrieval',
+ },
+ ],
memoryCount: 0,
});
});
@@ -62,18 +71,31 @@ describe('countUnboundRetrieval', () => {
it('ignores already-bound steps and legacy-free DSLs', () => {
const bound = bindUnboundRetrieval(
starterLikeDsl as any,
- ['kb-1'],
+ { 'Retrieval:WarmWeeksRead': ['kb-1'] },
[],
) as any;
- expect(countUnboundRetrieval(bound)).toEqual({
- datasetCount: 0,
+ expect(collectUnboundRetrievalBindings(bound)).toEqual({
+ datasetBlocks: [],
memoryCount: 0,
});
});
it('returns zeroes for a missing DSL', () => {
- expect(countUnboundRetrieval(undefined)).toEqual({
- datasetCount: 0,
+ expect(collectUnboundRetrievalBindings(undefined)).toEqual({
+ datasetBlocks: [],
+ memoryCount: 0,
+ });
+ });
+
+ it('returns stable block ids and graph display names', () => {
+ const dsl = standaloneRetrievalDsl(['Schema', 'Examples', 'Description']);
+
+ expect(collectUnboundRetrievalBindings(dsl as any)).toEqual({
+ datasetBlocks: [
+ { blockId: 'retrieval-0', displayName: 'Schema' },
+ { blockId: 'retrieval-1', displayName: 'Examples' },
+ { blockId: 'retrieval-2', displayName: 'Description' },
+ ],
memoryCount: 0,
});
});
@@ -83,7 +105,7 @@ describe('bindUnboundRetrieval', () => {
it('binds the selected dataset to both views and drops legacy kb_ids', () => {
const bound = bindUnboundRetrieval(
starterLikeDsl as any,
- ['kb-1'],
+ { 'Retrieval:WarmWeeksRead': ['kb-1'] },
[],
) as any;
@@ -100,7 +122,11 @@ describe('bindUnboundRetrieval', () => {
});
it('does not mutate the input DSL', () => {
- bindUnboundRetrieval(starterLikeDsl as any, ['kb-1'], []);
+ bindUnboundRetrieval(
+ starterLikeDsl as any,
+ { 'Retrieval:WarmWeeksRead': ['kb-1'] },
+ [],
+ );
const params = starterLikeDsl.graph.nodes[0].data.form.tools[0]
.params as Record<string, any>;
expect(params.dataset_ids).toBeUndefined();
@@ -120,9 +146,115 @@ describe('bindUnboundRetrieval', () => {
],
},
};
- const bound = bindUnboundRetrieval(dsl as any, ['kb-1'], []) as any;
+ const bound = bindUnboundRetrieval(dsl as any, { r1: ['kb-1'] }, []) as any;
expect(
(bound.graph.nodes[0].data.form as Record<string, any>).dataset_ids,
).toEqual(['kb-x']);
});
+
+ it.each([
+ ['different datasets', [['kb-1'], ['kb-2']]],
+ ['the same dataset in two blocks', [['kb-1'], ['kb-1']]],
+ ['the same dataset in three blocks', [['kb-1'], ['kb-1'], ['kb-1']]],
+ ])('preserves %s', (_name, selections) => {
+ const dsl = standaloneRetrievalDsl(
+ selections.map((_, index) => `Block ${index + 1}`),
+ );
+ const bindings = Object.fromEntries(
+ selections.map((datasetIds, index) => [`retrieval-${index}`, datasetIds]),
+ );
+
+ const bound = bindUnboundRetrieval(dsl as any, bindings, []) as any;
+
+ expect(
+ bound.graph.nodes.map((node: any) => node.data.form.dataset_ids),
+ ).toEqual(selections);
+ expect(
+ Object.values(bound.components).map(
+ (component: any) => component.obj.params.dataset_ids,
+ ),
+ ).toEqual(selections);
+ });
+
+ it('pairs embedded retrieval mirrors by owner and tool position', () => {
+ const dsl = {
+ graph: {
+ nodes: [
+ {
+ id: 'agent-1',
+ data: {
+ label: 'Agent',
+ name: 'Research agent',
+ form: {
+ tools: [
+ {
+ id: 'graph-retrieval-id',
+ name: 'Research sources',
+ component_name: 'Retrieval',
+ params: { retrieval_from: 'dataset', dataset_ids: [] },
+ },
+ ],
+ },
+ },
+ },
+ ],
+ },
+ components: {
+ 'agent-1': {
+ obj: {
+ component_name: 'Agent',
+ params: {
+ tools: [
+ {
+ id: 'component-retrieval-id',
+ name: 'Retrieval',
+ component_name: 'Retrieval',
+ params: { retrieval_from: 'dataset', dataset_ids: [] },
+ },
+ ],
+ },
+ },
+ },
+ },
+ };
+
+ const bound = bindUnboundRetrieval(
+ dsl as any,
+ { 'graph-retrieval-id': ['kb-1'] },
+ [],
+ ) as any;
+
+ expect(bound.graph.nodes[0].data.form.tools[0].params.dataset_ids).toEqual([
+ 'kb-1',
+ ]);
+ expect(
+ bound.components['agent-1'].obj.params.tools[0].params.dataset_ids,
+ ).toEqual(['kb-1']);
+ });
});
+
+function standaloneRetrievalDsl(names: string[]) {
+ return {
+ graph: {
+ nodes: names.map((name, index) => ({
+ id: `retrieval-${index}`,
+ data: {
+ label: 'Retrieval',
+ name,
+ form: { retrieval_from: 'dataset', dataset_ids: [] },
+ },
+ })),
+ },
+ components: Object.fromEntries(
+ names.map((_, index) => [
+ `retrieval-${index}`,
+ {
+ obj: {
+ component_name: 'Retrieval',
+ params: { retrieval_from: 'dataset', dataset_ids: [] },
+ },
+ },
+ ]),
+ ),
+ };
+}
diff --git a/web/src/pages/agents/template-retrieval-binding.ts b/web/src/pages/agents/template-retrieval-binding.ts
index 872d06bb74..7f0240c190 100644
--- a/web/src/pages/agents/template-retrieval-binding.ts
+++ b/web/src/pages/agents/template-retrieval-binding.ts
@@ -1,16 +1,7 @@
-import { cloneDeep } from 'lodash';
import { DSL } from '@/interfaces/database/agent';
import { Operator, RetrievalFrom } from '@/pages/agent/constant';
+import { cloneDeep } from 'lodash';
-/**
- * Retrieval params live in two views of a DSL: the canvas graph
- * (`graph.nodes[].data.form` / `...tools[].params`) and the components the
- * runtime executes (`components[].obj.params` / `...tools[].params`). Both are
- * written on save, so binding helpers must keep the two views in sync. Counting
- * helpers, however, follow the components view only — the topology the engine
- * runs — so a single retrieval step mirrored in both views is never counted
- * twice.
- */
interface RetrievalParams {
retrieval_from?: string;
dataset_ids?: string[];
@@ -18,157 +9,226 @@ interface RetrievalParams {
kb_ids?: string[];
}
-function pushRetrievalParams(
- locations: Array<{ params: RetrievalParams }>,
- params: unknown,
-) {
- if (
- params &&
- typeof params === 'object' &&
- typeof (params as RetrievalParams).retrieval_from === 'string'
- ) {
- locations.push({ params: params as RetrievalParams });
+interface RetrievalTool {
+ component_name?: string;
+ id?: string;
+ name?: string;
+ params?: RetrievalParams & { tools?: RetrievalTool[] };
+}
+
+type AgentParams = RetrievalParams & { tools?: RetrievalTool[] };
+
+interface RetrievalBlockLocation {
+ blockId: string;
+ displayName: string;
+ params: RetrievalParams[];
+}
+
+export interface DatasetBlockBinding {
+ blockId: string;
+ displayName: string;
+}
+
+export interface RetrievalBindings {
+ datasetBlocks: DatasetBlockBinding[];
+ memoryCount: number;
+}
+
+function findAgentParams(
+ components: Record<string, any>,
+ agentId: string,
+): AgentParams | undefined {
+ const root = components[agentId]?.obj;
+ if (root?.component_name === Operator.Agent) return root.params;
+
+ const findNested = (tools: RetrievalTool[] = []): AgentParams | undefined => {
+ for (const tool of tools) {
+ if (tool.component_name !== Operator.Agent) continue;
+ if (tool.id === agentId) return tool.params;
+ const nested = findNested(tool.params?.tools);
+ if (nested) return nested;
+ }
+ };
+
+ for (const component of Object.values(components)) {
+ const obj = (component as Record<string, any>)?.obj;
+ if (obj?.component_name === Operator.Agent) {
+ const nested = findNested(obj.params?.tools);
+ if (nested) return nested;
+ }
}
}
-/** Retrieval steps reachable from the canvas graph view. */
-function walkGraphRetrievalParams(
- dsl: DSL | Record<string, any>,
-): Array<{ params: RetrievalParams }> {
- const locations: Array<{ params: RetrievalParams }> = [];
- const graph = (dsl as Record<string, any>)?.graph;
- if (Array.isArray(graph?.nodes)) {
- for (const node of graph.nodes) {
- const label = node?.data?.label;
- const form = node?.data?.form;
- if (label === Operator.Retrieval) {
- pushRetrievalParams(locations, form);
- } else if (label === Operator.Agent) {
- for (const tool of form?.tools ?? []) {
- if (tool?.component_name === Operator.Retrieval) {
- pushRetrievalParams(locations, tool?.params);
- }
- }
+function collectComponentOnlyBlocks(
+ components: Record<string, any>,
+): RetrievalBlockLocation[] {
+ const blocks: RetrievalBlockLocation[] = [];
+ const collectTools = (tools: RetrievalTool[] = [], ownerId: string) => {
+ tools.forEach((tool, index) => {
+ if (tool.component_name === Operator.Retrieval && tool.params) {
+ blocks.push({
+ blockId: tool.id || `${ownerId}:tool:${index}`,
+ displayName:
+ tool.name?.trim() || `${Operator.Retrieval} ${index + 1}`,
+ params: [tool.params],
+ });
+ } else if (tool.component_name === Operator.Agent) {
+ collectTools(
+ tool.params?.tools,
+ tool.id || `${ownerId}:agent:${index}`,
+ );
}
+ });
+ };
+
+ for (const [componentId, component] of Object.entries(components)) {
+ const obj = (component as Record<string, any>)?.obj;
+ if (obj?.component_name === Operator.Retrieval && obj.params) {
+ blocks.push({
+ blockId: componentId,
+ displayName: obj.name?.trim() || Operator.Retrieval,
+ params: [obj.params],
+ });
+ } else if (obj?.component_name === Operator.Agent) {
+ collectTools(obj.params?.tools, componentId);
}
}
- return locations;
+ return blocks;
}
-/** Retrieval steps the runtime actually executes (topology of record). */
-function walkComponentsRetrievalParams(
- dsl: DSL | Record<string, any>,
-): Array<{ params: RetrievalParams }> {
- const locations: Array<{ params: RetrievalParams }> = [];
- const components = (dsl as Record<string, any>)?.components;
- if (components && typeof components === 'object') {
- for (const component of Object.values(components)) {
- const obj = (component as Record<string, any>)?.obj;
- if (obj?.component_name === Operator.Retrieval) {
- pushRetrievalParams(locations, obj?.params);
- } else if (obj?.component_name === Operator.Agent) {
- for (const tool of obj?.params?.tools ?? []) {
- if (tool?.component_name === Operator.Retrieval) {
- pushRetrievalParams(locations, tool?.params);
- }
+function collectRetrievalBlocks(
+ dsl: DSL | Record<string, any> | undefined,
+): RetrievalBlockLocation[] {
+ if (!dsl) return [];
+
+ const dslObject = dsl as Record<string, any>;
+ const graphNodes = dslObject.graph?.nodes;
+ const components =
+ dslObject.components && typeof dslObject.components === 'object'
+ ? dslObject.components
+ : {};
+ if (!Array.isArray(graphNodes)) {
+ return collectComponentOnlyBlocks(components);
+ }
+
+ const blocks: RetrievalBlockLocation[] = [];
+ for (const [nodeIndex, node] of graphNodes.entries()) {
+ const form = node?.data?.form;
+ if (node?.data?.label === Operator.Retrieval && form) {
+ const params = [form as RetrievalParams];
+ const component = components[node.id]?.obj;
+ if (
+ component?.component_name === Operator.Retrieval &&
+ component.params
+ ) {
+ params.push(component.params);
+ }
+ blocks.push({
+ blockId: node.id,
+ displayName:
+ node.data.name?.trim() || `${Operator.Retrieval} ${nodeIndex + 1}`,
+ params,
+ });
+ } else if (node?.data?.label === Operator.Agent) {
+ const componentAgentParams = findAgentParams(components, node.id);
+ for (const [toolIndex, tool] of (form?.tools ?? []).entries()) {
+ if (
+ tool?.component_name !== Operator.Retrieval ||
+ !tool.params ||
+ typeof tool.params.retrieval_from !== 'string'
+ ) {
+ continue;
}
+ const params = [tool.params as RetrievalParams];
+ const componentTool = componentAgentParams?.tools?.[toolIndex];
+ if (
+ componentTool?.component_name === Operator.Retrieval &&
+ componentTool.params
+ ) {
+ params.push(componentTool.params);
+ }
+ blocks.push({
+ blockId: tool.id || `${node.id}:tool:${toolIndex}`,
+ displayName:
+ tool.name?.trim() ||
+ node.data.name?.trim() ||
+ `${Operator.Retrieval} ${toolIndex + 1}`,
+ params,
+ });
}
}
}
- return locations;
-}
-
-/**
- * Every retrieval location across both views of the DSL. Binding must write
- * both the canvas graph and the executed components so a later save (which
- * rebuilds components from the graph) cannot silently unbind a step.
- */
-function walkRetrievalParams(
- dsl: DSL | Record<string, any>,
-): Array<{ params: RetrievalParams }> {
- return [
- ...walkGraphRetrievalParams(dsl),
- ...walkComponentsRetrievalParams(dsl),
- ];
+ return blocks;
}
-export interface RetrievalBindingCount {
- datasetCount: number;
- memoryCount: number;
+function isUnbound(
+ block: RetrievalBlockLocation,
+ field: keyof RetrievalParams,
+) {
+ return block.params.every((params) => {
+ const ids = params[field];
+ return !Array.isArray(ids) || ids.length === 0;
+ });
}
-/**
- * Counts retrieval steps that explicitly source from a dataset/memory but do
- * not carry any binding yet. Such steps fail at runtime with a
- * `dataset_ids`/`memory_ids is required` error. The count follows the
- * `components` view — the topology the runtime executes — and only falls back
- * to the graph view when the DSL has no components block, so a retrieval step
- * mirrored in both views is reported once.
- */
-export function countUnboundRetrieval(
+export function collectUnboundRetrievalBindings(
dsl: DSL | Record<string, any> | undefined,
-): RetrievalBindingCount {
- let datasetCount = 0;
+): RetrievalBindings {
+ const datasetBlocks: DatasetBlockBinding[] = [];
let memoryCount = 0;
- if (!dsl) {
- return { datasetCount, memoryCount };
- }
- const dslObject = dsl as Record<string, any>;
- const hasComponents =
- !!dslObject.components && typeof dslObject.components === 'object';
- const locations = hasComponents
- ? walkComponentsRetrievalParams(dslObject)
- : walkGraphRetrievalParams(dslObject);
- for (const { params } of locations) {
- if (params.retrieval_from === RetrievalFrom.Dataset) {
- const ids = params.dataset_ids ?? params.kb_ids;
- if (!Array.isArray(ids) || ids.length === 0) {
- datasetCount++;
- }
- } else if (params.retrieval_from === RetrievalFrom.Memory) {
- const ids = params.memory_ids;
- if (!Array.isArray(ids) || ids.length === 0) {
- memoryCount++;
- }
+
+ for (const block of collectRetrievalBlocks(dsl)) {
+ const source = block.params[0]?.retrieval_from;
+ if (
+ source === RetrievalFrom.Dataset &&
+ block.params.every((params) => {
+ const ids = params.dataset_ids ?? params.kb_ids;
+ return !Array.isArray(ids) || ids.length === 0;
+ })
+ ) {
+ datasetBlocks.push({
+ blockId: block.blockId,
+ displayName: block.displayName,
+ });
+ } else if (
+ source === RetrievalFrom.Memory &&
+ isUnbound(block, 'memory_ids')
+ ) {
+ memoryCount++;
}
}
- return { datasetCount, memoryCount };
+ return { datasetBlocks, memoryCount };
}
-/**
- * Applies the selected dataset/memory ids to every unbound retrieval step in
- * the template DSL (both the graph and the components views). Steps that
- * already carry ids are left untouched so users can fine-tune each retrieval
- * in the canvas later.
- */
export function bindUnboundRetrieval(
dsl: DSL | Record<string, any> | undefined,
- datasetIds: string[],
+ datasetBindings: Record<string, string[]>,
memoryIds: string[],
): DSL | undefined {
- if (!dsl) {
- return undefined;
- }
+ if (!dsl) return undefined;
+
const next = cloneDeep(dsl) as Record<string, any>;
- for (const { params } of walkRetrievalParams(next)) {
+ for (const block of collectRetrievalBlocks(next)) {
+ const source = block.params[0]?.retrieval_from;
+ const datasetIds = datasetBindings[block.blockId] ?? [];
if (
- params.retrieval_from === RetrievalFrom.Dataset &&
- datasetIds.length > 0
+ source === RetrievalFrom.Dataset &&
+ datasetIds.length > 0 &&
+ block.params.every((params) => {
+ const ids = params.dataset_ids ?? params.kb_ids;
+ return !Array.isArray(ids) || ids.length === 0;
+ })
) {
- const ids = params.dataset_ids ?? params.kb_ids;
- if (!Array.isArray(ids) || ids.length === 0) {
- params.dataset_ids = datasetIds;
+ for (const params of block.params) {
+ params.dataset_ids = [...datasetIds];
delete params.kb_ids;
}
- }
- if (
- params.retrieval_from === RetrievalFrom.Memory &&
- memoryIds.length > 0
+ } else if (
+ source === RetrievalFrom.Memory &&
+ memoryIds.length > 0 &&
+ isUnbound(block, 'memory_ids')
) {
- const ids = params.memory_ids;
- if (!Array.isArray(ids) || ids.length === 0) {
- params.memory_ids = memoryIds;
- }
+ for (const params of block.params) params.memory_ids = [...memoryIds];
}
}
return next as DSL;