Skip to content

Commit 134ee81

Browse files
committed
refactor(api): update AutoRAG search parameter handling to only include defined values in RagAiSearchNode and RagSearchNode
1 parent a3c63ac commit 134ee81

5 files changed

Lines changed: 25 additions & 7 deletions

File tree

apps/api/src/nodes/rag/rag-ai-search-node.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { NodeExecution, NodeType } from "@dafthunk/types";
22

33
import { ExecutableNode } from "../types";
44
import { NodeContext } from "../types";
5+
import { createDatabase, getDataset } from "../../db";
56

67
/**
78
* RAG AI Search node implementation
@@ -110,8 +111,15 @@ export class RagAiSearchNode extends ExecutableNode {
110111
return this.createErrorResult("AI binding is not available");
111112
}
112113

114+
// Verify dataset exists and belongs to organization
115+
const db = createDatabase(context.env.DB);
116+
const dataset = await getDataset(db, datasetId, organizationId);
117+
if (!dataset) {
118+
return this.createErrorResult("Dataset not found or access denied");
119+
}
120+
113121
// Create multi-tenant folder filter
114-
const folderPrefix = `${organizationId}/${datasetId}/`;
122+
const folderPrefix = `${datasetId}/`;
115123

116124
// Prepare AutoRAG search parameters - only include defined values
117125
const searchParams: any = {
@@ -150,7 +158,7 @@ export class RagAiSearchNode extends ExecutableNode {
150158
searchParams.filters = {
151159
type: "eq" as const,
152160
key: "folder",
153-
value: "01975f74-76b4-7778-b50d-1d079cee26ce/01975fd5-5e25-728a-afd3-a8c40e6571b7/",
161+
value: folderPrefix,
154162
};
155163
}
156164

apps/api/src/nodes/rag/rag-search-node.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { NodeExecution, NodeType } from "@dafthunk/types";
22

33
import { ExecutableNode } from "../types";
44
import { NodeContext } from "../types";
5+
import { createDatabase, getDataset } from "../../db";
56

67
/**
78
* RAG Search node implementation
@@ -98,8 +99,15 @@ export class RagSearchNode extends ExecutableNode {
9899
return this.createErrorResult("AI binding is not available");
99100
}
100101

102+
// Verify dataset exists and belongs to organization
103+
const db = createDatabase(context.env.DB);
104+
const dataset = await getDataset(db, datasetId, organizationId);
105+
if (!dataset) {
106+
return this.createErrorResult("Dataset not found or access denied");
107+
}
108+
101109
// Create multi-tenant folder filter
102-
const folderPrefix = `${organizationId}/${datasetId}/`;
110+
const folderPrefix = `${datasetId}/`;
103111

104112
// Prepare AutoRAG search parameters - only include defined values
105113
const searchParams: any = {

apps/api/src/nodes/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export interface NodeContext {
105105
httpRequest?: HttpRequest;
106106
emailMessage?: EmailMessage;
107107
env: {
108+
DB: D1Database;
108109
AI: Ai;
109110
AI_OPTIONS: AiOptions;
110111
RESSOURCES: R2Bucket;

apps/api/src/routes/datasets.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ datasetRoutes.post("/:id/upload", jwtMiddleware, async (c) => {
203203
}
204204

205205
// Create the R2 path following the multitenant pattern
206-
const r2Path = `${organizationId}/${id}/${file.name}`;
206+
const r2Path = `${id}/${file.name}`;
207207

208208
// Upload to R2
209209
await c.env.DATASETS.put(r2Path, await file.arrayBuffer(), {
@@ -243,7 +243,7 @@ datasetRoutes.get("/:id/files", jwtMiddleware, async (c) => {
243243

244244
try {
245245
// List objects in the dataset's directory
246-
const prefix = `${organizationId}/${id}/`;
246+
const prefix = `${id}/`;
247247
const files = await c.env.DATASETS.list({ prefix });
248248

249249
const response: ListDatasetFilesResponse = {
@@ -277,7 +277,7 @@ datasetRoutes.delete("/:id/files/:filename", jwtMiddleware, async (c) => {
277277
}
278278

279279
try {
280-
const r2Path = `${organizationId}/${id}/${filename}`;
280+
const r2Path = `${id}/${filename}`;
281281
await c.env.DATASETS.delete(r2Path);
282282

283283
const response: DeleteDatasetFileResponse = {
@@ -308,7 +308,7 @@ datasetRoutes.get("/:id/files/:filename", jwtMiddleware, async (c) => {
308308
}
309309

310310
try {
311-
const r2Path = `${organizationId}/${id}/${filename}`;
311+
const r2Path = `${id}/${filename}`;
312312
const object = await c.env.DATASETS.get(r2Path);
313313

314314
if (!object) {

apps/api/src/runtime/runtime.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ export class Runtime extends WorkflowEntrypoint<Bindings, RuntimeParams> {
318318
emailMessage,
319319
onProgress: () => {},
320320
env: {
321+
DB: this.env.DB,
321322
AI: this.env.AI,
322323
AI_OPTIONS: aiOptions,
323324
RESSOURCES: this.env.RESSOURCES,

0 commit comments

Comments
 (0)