|
| 1 | +import { NodeExecution, NodeType } from "@dafthunk/types"; |
| 2 | + |
| 3 | +import { ExecutableNode } from "../types"; |
| 4 | +import { NodeContext } from "../types"; |
| 5 | + |
| 6 | +/** |
| 7 | + * RAG Search node implementation |
| 8 | + * This node provides a dataset selector widget and performs search |
| 9 | + * using Cloudflare's AutoRAG search() method with multi-tenant folder filtering. |
| 10 | + * Returns search results without AI-generated response. |
| 11 | + */ |
| 12 | +export class RagSearchNode extends ExecutableNode { |
| 13 | + public static readonly nodeType: NodeType = { |
| 14 | + id: "rag-search", |
| 15 | + name: "RAG Search", |
| 16 | + type: "rag-search", |
| 17 | + description: "Search through datasets and return relevant results", |
| 18 | + category: "AI", |
| 19 | + icon: "search", |
| 20 | + inputs: [ |
| 21 | + { |
| 22 | + name: "query", |
| 23 | + type: "string", |
| 24 | + description: "The search query to find relevant content", |
| 25 | + required: true, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "datasetId", |
| 29 | + type: "string", |
| 30 | + description: "Selected dataset ID for the search", |
| 31 | + hidden: true, |
| 32 | + required: true, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "rewriteQuery", |
| 36 | + type: "boolean", |
| 37 | + description: "Rewrite query for better search optimization", |
| 38 | + hidden: true, |
| 39 | + value: true, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "maxResults", |
| 43 | + type: "number", |
| 44 | + description: "Maximum number of results to return", |
| 45 | + hidden: true, |
| 46 | + value: 10, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "scoreThreshold", |
| 50 | + type: "number", |
| 51 | + description: "Minimum match score for results", |
| 52 | + hidden: true, |
| 53 | + value: 0.3, |
| 54 | + }, |
| 55 | + ], |
| 56 | + outputs: [ |
| 57 | + { |
| 58 | + name: "searchResults", |
| 59 | + type: "json", |
| 60 | + description: "Search results from the dataset", |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "searchQuery", |
| 64 | + type: "string", |
| 65 | + description: "The processed search query that was used", |
| 66 | + hidden: true, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "hasMore", |
| 70 | + type: "boolean", |
| 71 | + description: "Whether there are more results available", |
| 72 | + hidden: true, |
| 73 | + }, |
| 74 | + ], |
| 75 | + }; |
| 76 | + |
| 77 | + async execute(context: NodeContext): Promise<NodeExecution> { |
| 78 | + try { |
| 79 | + const { query, datasetId, rewriteQuery, maxResults, scoreThreshold } = |
| 80 | + context.inputs; |
| 81 | + |
| 82 | + const { organizationId } = context; |
| 83 | + |
| 84 | + // Validate required inputs |
| 85 | + if (!query || typeof query !== "string") { |
| 86 | + return this.createErrorResult("Query is required and must be a string"); |
| 87 | + } |
| 88 | + |
| 89 | + if (!datasetId || typeof datasetId !== "string") { |
| 90 | + return this.createErrorResult("Dataset ID is required"); |
| 91 | + } |
| 92 | + |
| 93 | + if (!organizationId || typeof organizationId !== "string") { |
| 94 | + return this.createErrorResult("Organization ID is required"); |
| 95 | + } |
| 96 | + |
| 97 | + if (!context.env.AI) { |
| 98 | + return this.createErrorResult("AI binding is not available"); |
| 99 | + } |
| 100 | + |
| 101 | + // Create multi-tenant folder filter |
| 102 | + const folderPrefix = `${organizationId}/${datasetId}/`; |
| 103 | + |
| 104 | + // Prepare AutoRAG search parameters |
| 105 | + const searchParams = { |
| 106 | + query: query.trim(), |
| 107 | + rewrite_query: Boolean(rewriteQuery), |
| 108 | + max_num_results: Math.min(Math.max(Number(maxResults) || 10, 1), 50), |
| 109 | + ranking_options: { |
| 110 | + score_threshold: Math.min( |
| 111 | + Math.max(Number(scoreThreshold) || 0.3, 0), |
| 112 | + 1 |
| 113 | + ), |
| 114 | + }, |
| 115 | + filters: { |
| 116 | + type: "eq" as const, |
| 117 | + key: "folder", |
| 118 | + value: folderPrefix, |
| 119 | + }, |
| 120 | + }; |
| 121 | + |
| 122 | + // Execute AutoRAG search (search only, no generation) |
| 123 | + const autoragInstance = context.env.AI.autorag( |
| 124 | + context.env.DATASETS_AUTORAG |
| 125 | + ); |
| 126 | + const result = await autoragInstance.search(searchParams); |
| 127 | + |
| 128 | + // Handle the response |
| 129 | + let searchResults = null; |
| 130 | + let searchQuery = ""; |
| 131 | + let hasMore = false; |
| 132 | + |
| 133 | + if (result && typeof result === "object") { |
| 134 | + searchResults = (result as any).data || []; |
| 135 | + searchQuery = (result as any).search_query || query; |
| 136 | + hasMore = (result as any).has_more || false; |
| 137 | + } |
| 138 | + |
| 139 | + return this.createSuccessResult({ |
| 140 | + searchResults, |
| 141 | + searchQuery, |
| 142 | + hasMore, |
| 143 | + }); |
| 144 | + } catch (error) { |
| 145 | + return this.createErrorResult( |
| 146 | + error instanceof Error |
| 147 | + ? error.message |
| 148 | + : "Unknown error during RAG search" |
| 149 | + ); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments