@retrivora-ai/rag-engine 2.4.8 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/server.mjs CHANGED
@@ -326,7 +326,7 @@ var init_package = __esm({
326
326
  "package.json"() {
327
327
  package_default = {
328
328
  name: "@retrivora-ai/rag-engine",
329
- version: "2.4.8",
329
+ version: "2.5.0",
330
330
  description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
331
331
  author: "Abhinav Alkuchi",
332
332
  license: "UNLICENSED",
@@ -1543,6 +1543,7 @@ var init_MongoDBProvider = __esm({
1543
1543
  MongoDBProvider = class extends BaseVectorProvider {
1544
1544
  constructor(config) {
1545
1545
  super(config);
1546
+ this.initialized = false;
1546
1547
  const opts = config.options;
1547
1548
  if (!opts.uri || !opts.database || !opts.collection) {
1548
1549
  throw new Error("[MongoDBProvider] options uri, database, and collection are required.");
@@ -1620,9 +1621,11 @@ var init_MongoDBProvider = __esm({
1620
1621
  };
1621
1622
  }
1622
1623
  async initialize() {
1624
+ if (this.initialized) return;
1623
1625
  await this.client.connect();
1624
1626
  this.db = this.client.db(this.dbName);
1625
1627
  this.collection = this.db.collection(this.collectionName);
1628
+ this.initialized = true;
1626
1629
  }
1627
1630
  async upsert(doc, namespace) {
1628
1631
  const document = __spreadValues({
@@ -1714,7 +1717,6 @@ var init_MongoDBProvider = __esm({
1714
1717
  const sanitized = super.sanitizeFilter(filter);
1715
1718
  const mongoFilter = {};
1716
1719
  for (const [key, value] of Object.entries(sanitized)) {
1717
- if (key.startsWith("__") || key === "keywords" || key === "queryText") continue;
1718
1720
  if (key === "namespace") {
1719
1721
  mongoFilter[key] = value;
1720
1722
  } else {
@@ -1733,6 +1735,7 @@ var init_MongoDBProvider = __esm({
1733
1735
  }
1734
1736
  async disconnect() {
1735
1737
  await this.client.close();
1738
+ this.initialized = false;
1736
1739
  }
1737
1740
  };
1738
1741
  }
@@ -1777,15 +1780,19 @@ var init_MilvusProvider = __esm({
1777
1780
  await this.http.post("/v1/vector/upsert", payload);
1778
1781
  }
1779
1782
  async batchUpsert(docs, namespace) {
1780
- const payload = {
1781
- collectionName: this.indexName,
1782
- data: docs.map((doc) => __spreadValues({
1783
- vector: doc.vector,
1784
- content: doc.content,
1785
- metadata: doc.metadata || {}
1786
- }, namespace ? { namespace } : {}))
1787
- };
1788
- await this.http.post("/v1/vector/upsert", payload);
1783
+ const batchSize = 100;
1784
+ for (let i = 0; i < docs.length; i += batchSize) {
1785
+ const batch = docs.slice(i, i + batchSize);
1786
+ const payload = {
1787
+ collectionName: this.indexName,
1788
+ data: batch.map((doc) => __spreadValues({
1789
+ vector: doc.vector,
1790
+ content: doc.content,
1791
+ metadata: doc.metadata || {}
1792
+ }, namespace ? { namespace } : {}))
1793
+ };
1794
+ await this.http.post("/v1/vector/upsert", payload);
1795
+ }
1789
1796
  }
1790
1797
  async query(vector, topK, namespace, _filter) {
1791
1798
  const sanitizedFilter = this.sanitizeFilter(_filter);
@@ -1796,7 +1803,8 @@ var init_MilvusProvider = __esm({
1796
1803
  for (const [key, value] of Object.entries(sanitizedFilter)) {
1797
1804
  if (key === "queryText" || key === "keywords") continue;
1798
1805
  if (typeof value === "string") {
1799
- filterParts.push(`metadata["${key}"] == "${value.replace(/"/g, '\\"')}"`);
1806
+ const escaped = value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
1807
+ filterParts.push(`metadata["${key}"] == "${escaped}"`);
1800
1808
  } else if (typeof value === "number") {
1801
1809
  filterParts.push(`metadata["${key}"] == ${value}`);
1802
1810
  }
@@ -1820,7 +1828,7 @@ var init_MilvusProvider = __esm({
1820
1828
  metadata: res["metadata"] || {}
1821
1829
  }));
1822
1830
  }
1823
- async delete(id, _namespace) {
1831
+ async delete(id, namespace) {
1824
1832
  await this.http.post("/v1/vector/delete", {
1825
1833
  collectionName: this.indexName,
1826
1834
  id
@@ -1892,7 +1900,6 @@ var init_QdrantProvider = __esm({
1892
1900
  return this.ensureIndex(fieldName, schemaType || "keyword");
1893
1901
  })
1894
1902
  );
1895
- } else {
1896
1903
  }
1897
1904
  this.schemaDiscovered = true;
1898
1905
  }
@@ -2138,7 +2145,7 @@ var init_ChromaDBProvider = __esm({
2138
2145
  documents: docs.map((d) => d.content),
2139
2146
  metadatas: docs.map((d) => __spreadValues(__spreadValues({}, d.metadata || {}), namespace ? { namespace } : {}))
2140
2147
  };
2141
- await this.http.post(`/api/v1/collections/${this.collectionId}/add`, payload);
2148
+ await this.http.post(`/api/v1/collections/${this.collectionId}/upsert`, payload);
2142
2149
  }
2143
2150
  async query(vector, topK, namespace, _filter) {
2144
2151
  const sanitizedFilter = this.sanitizeFilter(_filter);
@@ -2222,17 +2229,16 @@ var init_RedisProvider = __esm({
2222
2229
  await this.ping();
2223
2230
  }
2224
2231
  async upsert(doc, namespace) {
2225
- const key = namespace ? `${namespace}:${doc.id}` : String(doc.id);
2226
- await this.http.post("/", ["JSON.SET", key, "$", JSON.stringify({
2227
- vector: doc.vector,
2228
- content: doc.content,
2229
- metadata: doc.metadata || {}
2230
- })]);
2232
+ await this.batchUpsert([doc], namespace);
2231
2233
  }
2232
2234
  async batchUpsert(docs, namespace) {
2233
- for (const doc of docs) {
2234
- await this.upsert(doc, namespace);
2235
- }
2235
+ const payload = docs.map((doc) => __spreadValues({
2236
+ id: String(doc.id),
2237
+ vector: doc.vector,
2238
+ data: doc.content,
2239
+ metadata: doc.metadata || {}
2240
+ }, namespace ? { namespace } : {}));
2241
+ await this.http.post("/upsert", payload);
2236
2242
  }
2237
2243
  async query(vector, topK, namespace, _filter) {
2238
2244
  const payload = {
@@ -2250,19 +2256,22 @@ var init_RedisProvider = __esm({
2250
2256
  }));
2251
2257
  }
2252
2258
  async delete(id, namespace) {
2253
- const key = namespace ? `${namespace}:${id}` : id;
2254
- await this.http.post("/", ["DEL", key]);
2259
+ await this.http.post("/delete", __spreadValues({
2260
+ ids: [id]
2261
+ }, namespace ? { namespace } : {}));
2255
2262
  }
2256
2263
  async deleteNamespace(namespace) {
2257
- console.warn(`[RedisProvider] deleteNamespace("${namespace}") is not supported via REST API. Use Redis CLI: SCAN + DEL`);
2264
+ if (!namespace) {
2265
+ throw new Error("[RedisProvider] namespace is required for deleteNamespace");
2266
+ }
2267
+ await this.http.post("/reset", { namespace });
2258
2268
  }
2259
2269
  /**
2260
- * Check reachability via a PING command.
2261
- * Returns false on connection failure so health checks surface real problems.
2270
+ * Check reachability of the Upstash Vector API.
2262
2271
  */
2263
2272
  async ping() {
2264
2273
  try {
2265
- await this.http.post("/", ["PING"]);
2274
+ await this.http.get("/");
2266
2275
  return true;
2267
2276
  } catch (e) {
2268
2277
  return false;
@@ -2293,11 +2302,12 @@ var init_WeaviateProvider = __esm({
2293
2302
  if (!baseUrl) throw new Error("[WeaviateProvider] options.baseUrl is required");
2294
2303
  this.http = axios7.create({
2295
2304
  baseURL: baseUrl,
2296
- headers: __spreadValues({
2305
+ headers: __spreadValues(__spreadValues({
2297
2306
  "Content-Type": "application/json"
2298
2307
  }, opts.apiKey ? {
2299
- "X-OpenAI-Api-Key": opts.apiKey,
2300
2308
  "Authorization": `Bearer ${opts.apiKey}`
2309
+ } : {}), opts.openaiApiKey ? {
2310
+ "X-OpenAI-Api-Key": opts.openaiApiKey
2301
2311
  } : {})
2302
2312
  });
2303
2313
  }
@@ -2374,10 +2384,12 @@ var init_WeaviateProvider = __esm({
2374
2384
  await this.http.delete(`/v1/objects/${this.indexName}/${id}`);
2375
2385
  }
2376
2386
  async deleteNamespace(namespace) {
2377
- await this.http.post("/v1/batch/objects", {
2378
- match: {
2379
- class: this.indexName,
2380
- where: { path: ["namespace"], operator: "Equal", valueString: namespace }
2387
+ await this.http.delete("/v1/batch/objects", {
2388
+ data: {
2389
+ match: {
2390
+ class: this.indexName,
2391
+ where: { path: ["namespace"], operator: "Equal", valueString: namespace }
2392
+ }
2381
2393
  }
2382
2394
  });
2383
2395
  }
@@ -2608,6 +2620,9 @@ var init_SimpleGraphProvider = __esm({
2608
2620
  this.edges = [];
2609
2621
  }
2610
2622
  async initialize() {
2623
+ console.warn(
2624
+ "[SimpleGraphProvider] Warning: SimpleGraphProvider is an in-memory database meant for demonstration and local development. Graph data is NOT persisted across restarts."
2625
+ );
2611
2626
  }
2612
2627
  async addNodes(nodes) {
2613
2628
  for (const node of nodes) {
@@ -2629,15 +2644,21 @@ var init_SimpleGraphProvider = __esm({
2629
2644
  for (const edge of matchedEdges) {
2630
2645
  if (!matchedNodeIds.has(edge.source)) {
2631
2646
  const source = this.nodes.get(edge.source);
2632
- if (source) matchedNodes.push(source);
2647
+ if (source) {
2648
+ matchedNodes.push(source);
2649
+ matchedNodeIds.add(edge.source);
2650
+ }
2633
2651
  }
2634
2652
  if (!matchedNodeIds.has(edge.target)) {
2635
2653
  const target = this.nodes.get(edge.target);
2636
- if (target) matchedNodes.push(target);
2654
+ if (target) {
2655
+ matchedNodes.push(target);
2656
+ matchedNodeIds.add(edge.target);
2657
+ }
2637
2658
  }
2638
2659
  }
2639
2660
  return {
2640
- nodes: Array.from(new Set(matchedNodes)),
2661
+ nodes: matchedNodes,
2641
2662
  edges: matchedEdges
2642
2663
  };
2643
2664
  }
@@ -5813,27 +5834,42 @@ var MCPClient = class {
5813
5834
  var MCPRegistry = class {
5814
5835
  constructor(servers = []) {
5815
5836
  this.clients = [];
5837
+ /** Cache populated by getAllTools(); maps tool name → the client that owns it. */
5838
+ this.toolClientCache = null;
5816
5839
  this.clients = servers.map((cfg) => new MCPClient(cfg));
5817
5840
  }
5818
5841
  async getAllTools() {
5819
5842
  const list = [];
5843
+ const cache = /* @__PURE__ */ new Map();
5820
5844
  await Promise.all(
5821
5845
  this.clients.map(async (client) => {
5822
5846
  try {
5823
5847
  const tools = await client.listTools();
5824
- tools.forEach((t) => list.push({ serverName: client["config"].name, tool: t }));
5848
+ tools.forEach((t) => {
5849
+ list.push({ serverName: client["config"].name, tool: t });
5850
+ cache.set(t.name, client);
5851
+ });
5825
5852
  } catch (e) {
5826
5853
  console.warn(`[MCPRegistry] Failed to fetch tools from server "${client["config"].name}":`, e);
5827
5854
  }
5828
5855
  })
5829
5856
  );
5857
+ this.toolClientCache = cache;
5830
5858
  return list;
5831
5859
  }
5832
5860
  async callTool(toolName, args = {}) {
5861
+ var _a2;
5862
+ const cachedClient = (_a2 = this.toolClientCache) == null ? void 0 : _a2.get(toolName);
5863
+ if (cachedClient) {
5864
+ return cachedClient.callTool(toolName, args);
5865
+ }
5866
+ console.warn(`[MCPRegistry] Cache miss for tool "${toolName}", falling back to full scan.`);
5833
5867
  for (const client of this.clients) {
5834
5868
  try {
5835
5869
  const tools = await client.listTools();
5836
5870
  if (tools.some((t) => t.name === toolName)) {
5871
+ if (!this.toolClientCache) this.toolClientCache = /* @__PURE__ */ new Map();
5872
+ this.toolClientCache.set(toolName, client);
5837
5873
  return await client.callTool(toolName, args);
5838
5874
  }
5839
5875
  } catch (e) {
@@ -5841,18 +5877,44 @@ var MCPRegistry = class {
5841
5877
  }
5842
5878
  throw new Error(`[MCPRegistry] Tool "${toolName}" not found on any registered MCP server.`);
5843
5879
  }
5880
+ /** Invalidate the tool cache and disconnect all MCP clients. */
5844
5881
  disconnectAll() {
5882
+ this.toolClientCache = null;
5845
5883
  this.clients.forEach((c) => c.disconnect());
5846
5884
  }
5847
5885
  };
5848
5886
 
5849
5887
  // src/core/MultiAgentCoordinator.ts
5888
+ function parseToolCall(text) {
5889
+ const match = text.match(/TOOL_CALL:\s*([\w-]+)\s*\(\s*([\s\S]*?)\s*\)\s*[.,;]?\s*$/m);
5890
+ if (!match) return null;
5891
+ const name = match[1].trim();
5892
+ let argsRaw = match[2].trim();
5893
+ argsRaw = argsRaw.replace(/'/g, '"');
5894
+ if (argsRaw && !argsRaw.startsWith("{")) {
5895
+ argsRaw = `{${argsRaw}}`;
5896
+ }
5897
+ let args = {};
5898
+ if (argsRaw) {
5899
+ try {
5900
+ args = JSON.parse(argsRaw);
5901
+ } catch (e) {
5902
+ const pairs = [...argsRaw.matchAll(/"?([\w-]+)"?\s*:\s*"([^"]*)"/g)];
5903
+ if (pairs.length === 0) {
5904
+ console.warn("[parseToolCall] Could not parse tool args:", argsRaw);
5905
+ return null;
5906
+ }
5907
+ for (const [, k, v] of pairs) args[k] = v;
5908
+ }
5909
+ }
5910
+ return { name, args };
5911
+ }
5850
5912
  var MultiAgentCoordinator = class {
5851
5913
  constructor(options) {
5852
5914
  var _a2;
5853
5915
  this.llmProvider = options.llmProvider;
5854
5916
  this.mcpRegistry = options.mcpRegistry;
5855
- this.documentSearch = options.documentSearch;
5917
+ this.retriever = options.retriever;
5856
5918
  this.maxIterations = (_a2 = options.maxIterations) != null ? _a2 : 5;
5857
5919
  }
5858
5920
  /**
@@ -5886,18 +5948,17 @@ var MultiAgentCoordinator = class {
5886
5948
  throw error[0];
5887
5949
  }
5888
5950
  }
5889
- return {
5890
- reply: finalReply,
5891
- sources
5892
- };
5951
+ return { reply: finalReply, sources };
5893
5952
  }
5894
5953
  /**
5895
- * Run the multi-agent coordination loop as an async stream, yielding intermediate thought blocks and tool execution events.
5954
+ * Run the multi-agent coordination loop as an async stream,
5955
+ * yielding intermediate thought blocks and tool execution events.
5896
5956
  */
5897
5957
  runStream(_0) {
5898
5958
  return __asyncGenerator(this, arguments, function* (question, history = []) {
5959
+ var _a2;
5899
5960
  const mcpTools = yield new __await(this.mcpRegistry.getAllTools());
5900
- let systemPrompt = `You are a goal-driven supervisor agent coordinating a RAG search engine and external MCP servers to solve the user's task.
5961
+ const systemPrompt = `You are a goal-driven supervisor agent coordinating a RAG search engine and external MCP servers to solve the user's task.
5901
5962
  You must think step-by-step before acting. Write your internal thinking process inside a \`<think>...</think>\` block first, then act or answer.
5902
5963
 
5903
5964
  Available Tools:
@@ -5905,8 +5966,8 @@ Available Tools:
5905
5966
 
5906
5967
  ${mcpTools.length > 0 ? "MCP Server Tools:" : ""}
5907
5968
  ${mcpTools.map((mt) => {
5908
- var _a2;
5909
- return `- ${mt.tool.name}(${JSON.stringify(((_a2 = mt.tool.inputSchema) == null ? void 0 : _a2.properties) || {})}): ${mt.tool.description || "No description provided."}`;
5969
+ var _a3;
5970
+ return `- ${mt.tool.name}(${JSON.stringify(((_a3 = mt.tool.inputSchema) == null ? void 0 : _a3.properties) || {})}): ${mt.tool.description || "No description provided."}`;
5910
5971
  }).join("\n")}
5911
5972
 
5912
5973
  Tool Calling Protocol:
@@ -5914,7 +5975,7 @@ If you need to call a tool, write:
5914
5975
  TOOL_CALL: <toolName>({ "argName": "value" })
5915
5976
  And then STOP generating immediately.
5916
5977
 
5917
- Once you have gathered enough information to fully satisfy the user's query, write your final response directly after the closing \`</think>\` block.
5978
+ Once you have gathered enough information to fully satisfy the user query, write your final response directly after the closing \`</think>\` block.
5918
5979
  `;
5919
5980
  const supervisorHistory = [
5920
5981
  { role: "system", content: systemPrompt },
@@ -5922,7 +5983,6 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5922
5983
  { role: "user", content: question }
5923
5984
  ];
5924
5985
  let iteration = 0;
5925
- const allSources = [];
5926
5986
  while (iteration < this.maxIterations) {
5927
5987
  iteration++;
5928
5988
  const context = "";
@@ -5935,24 +5995,14 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5935
5995
  fullModelResponse = reply;
5936
5996
  const thinkIndex = reply.indexOf("<think>");
5937
5997
  const endThinkIndex = reply.indexOf("</think>");
5938
- let thinkingText = "";
5939
5998
  let answerText = reply;
5940
5999
  if (thinkIndex !== -1 && endThinkIndex !== -1) {
5941
- thinkingText = reply.substring(thinkIndex + 7, endThinkIndex);
6000
+ const thinkingText = reply.substring(thinkIndex + 7, endThinkIndex);
5942
6001
  answerText = reply.substring(endThinkIndex + 8);
5943
6002
  yield { type: "thinking", text: thinkingText };
5944
6003
  }
5945
- const toolMatch = answerText.match(/TOOL_CALL:\s*(\w+)\s*\(([\s\S]*)\)/);
5946
- if (toolMatch) {
5947
- const toolName = toolMatch[1];
5948
- const toolArgsStr = toolMatch[2];
5949
- try {
5950
- const toolArgs = JSON.parse(toolArgsStr.trim());
5951
- toolCallDetected = { name: toolName, args: toolArgs };
5952
- } catch (e) {
5953
- console.error("[MultiAgentCoordinator] Failed to parse tool args:", toolArgsStr, e);
5954
- }
5955
- } else {
6004
+ toolCallDetected = parseToolCall(answerText);
6005
+ if (!toolCallDetected) {
5956
6006
  yield answerText;
5957
6007
  }
5958
6008
  } else {
@@ -5981,9 +6031,7 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5981
6031
  if (!inThinking && textBuffer.includes("TOOL_CALL:")) {
5982
6032
  const idx = textBuffer.indexOf("TOOL_CALL:");
5983
6033
  const precedingText = textBuffer.slice(0, idx);
5984
- if (precedingText.trim()) {
5985
- yield precedingText;
5986
- }
6034
+ if (precedingText.trim()) yield precedingText;
5987
6035
  textBuffer = textBuffer.slice(idx);
5988
6036
  }
5989
6037
  if (!inThinking && !textBuffer.includes("TOOL_CALL:") && textBuffer.length > 0) {
@@ -6002,17 +6050,8 @@ Once you have gathered enough information to fully satisfy the user's query, wri
6002
6050
  }
6003
6051
  }
6004
6052
  if (textBuffer.trim()) {
6005
- const toolMatch = textBuffer.match(/TOOL_CALL:\s*(\w+)\s*\(([\s\S]*)\)/);
6006
- if (toolMatch) {
6007
- const toolName = toolMatch[1];
6008
- const toolArgsStr = toolMatch[2];
6009
- try {
6010
- const toolArgs = JSON.parse(toolArgsStr.trim());
6011
- toolCallDetected = { name: toolName, args: toolArgs };
6012
- } catch (e) {
6013
- console.error("[MultiAgentCoordinator] Failed to parse tool args:", toolArgsStr, e);
6014
- }
6015
- } else {
6053
+ toolCallDetected = parseToolCall(textBuffer);
6054
+ if (!toolCallDetected) {
6016
6055
  yield textBuffer;
6017
6056
  }
6018
6057
  }
@@ -6029,14 +6068,16 @@ Once you have gathered enough information to fully satisfy the user's query, wri
6029
6068
  let toolResultText = "";
6030
6069
  try {
6031
6070
  if (toolName === "document_search") {
6032
- const searchRes = yield new __await(this.documentSearch(toolArgs.query || ""));
6033
- toolResultText = `document_search returned:
6034
- ${searchRes.reply}
6035
-
6036
- Sources count: ${searchRes.sources.length}`;
6037
- if (searchRes.sources && searchRes.sources.length > 0) {
6038
- allSources.push(...searchRes.sources);
6039
- yield { reply: "", sources: searchRes.sources };
6071
+ const query = String((_a2 = toolArgs.query) != null ? _a2 : "");
6072
+ const { sources } = yield new __await(this.retriever.retrieve(query));
6073
+ const snippets = sources.slice(0, 5).map((s, i) => {
6074
+ var _a3;
6075
+ return `[${i + 1}] ${String((_a3 = s.content) != null ? _a3 : "").slice(0, 400)}`;
6076
+ }).join("\n\n");
6077
+ toolResultText = `document_search returned ${sources.length} source(s):
6078
+ ${snippets}`;
6079
+ if (sources.length > 0) {
6080
+ yield { reply: "", sources };
6040
6081
  }
6041
6082
  } else {
6042
6083
  const mcpRes = yield new __await(this.mcpRegistry.callTool(toolName, toolArgs));
@@ -8948,8 +8989,30 @@ var Pipeline = class {
8948
8989
  getLLMProvider() {
8949
8990
  return this.initialised ? this.llmProvider : void 0;
8950
8991
  }
8992
+ /**
8993
+ * True when multi-agent / MCP mode is active:
8994
+ * architecture === 'multi-agent' OR mcpServers array is non-empty.
8995
+ * Uses MultiAgentCoordinator (custom supervisor loop).
8996
+ */
8997
+ get isMultiAgentMode() {
8998
+ var _a2, _b, _c;
8999
+ return ((_a2 = this.config.rag) == null ? void 0 : _a2.architecture) === "multi-agent" || ((_c = (_b = this.config.mcpServers) == null ? void 0 : _b.length) != null ? _c : 0) > 0;
9000
+ }
9001
+ /**
9002
+ * True when the LangChain ReAct agent mode is selected:
9003
+ * architecture === 'agentic'.
9004
+ * Uses LangChainAgent (LangGraph-backed ReAct, optional peer dep).
9005
+ */
9006
+ get isLangChainAgentMode() {
9007
+ var _a2;
9008
+ return ((_a2 = this.config.rag) == null ? void 0 : _a2.architecture) === "agentic";
9009
+ }
9010
+ /** Union of both agentic execution paths — used to gate the normal RAG pipeline. */
9011
+ get isAgenticMode() {
9012
+ return this.isMultiAgentMode || this.isLangChainAgentMode;
9013
+ }
8951
9014
  async initialize() {
8952
- var _a2, _b, _c, _d;
9015
+ var _a2, _b, _c;
8953
9016
  if (this.initialised) return;
8954
9017
  const chartInstruction = `You are a helpful assistant. Use the provided context to answer questions accurately.
8955
9018
 
@@ -8992,19 +9055,22 @@ var Pipeline = class {
8992
9055
  this.entityExtractor = new EntityExtractor(this.llmProvider);
8993
9056
  }
8994
9057
  await this.vectorDB.initialize();
8995
- if (((_a2 = this.config.rag) == null ? void 0 : _a2.architecture) === "agentic" || ((_b = this.config.rag) == null ? void 0 : _b.architecture) === "multi-agent" || this.config.mcpServers && this.config.mcpServers.length > 0) {
9058
+ if (this.isMultiAgentMode) {
9059
+ console.log("[Pipeline] Initializing MultiAgentCoordinator (multi-agent / MCP mode).");
8996
9060
  this.mcpRegistry = new MCPRegistry(this.config.mcpServers || []);
8997
9061
  this.multiAgentCoordinator = new MultiAgentCoordinator({
8998
9062
  llmProvider: this.llmProvider,
8999
9063
  mcpRegistry: this.mcpRegistry,
9000
- documentSearch: async (query) => {
9001
- return this.runNormalQuery(query);
9002
- }
9064
+ // Pass Pipeline itself as the IRetriever — avoids closure over internal methods.
9065
+ retriever: this,
9066
+ maxIterations: (_a2 = this.config.rag) == null ? void 0 : _a2.agentMaxIterations
9003
9067
  });
9068
+ } else if (this.isLangChainAgentMode) {
9069
+ console.log("[Pipeline] Initializing LangChainAgent (agentic / ReAct mode).");
9004
9070
  this.agent = new LangChainAgent(this, this.config);
9005
9071
  await this.agent.initialize(this.llmProvider);
9006
9072
  }
9007
- if ((_d = (_c = this.config.rag) == null ? void 0 : _c.cag) == null ? void 0 : _d.enabled) {
9073
+ if ((_c = (_b = this.config.rag) == null ? void 0 : _b.cag) == null ? void 0 : _c.enabled) {
9008
9074
  await this.loadColdContext(this.config.projectId);
9009
9075
  }
9010
9076
  this.initialised = true;
@@ -9167,11 +9233,14 @@ ${m.content}`).join("\n\n---\n\n");
9167
9233
  return { reply, sources };
9168
9234
  }
9169
9235
  async ask(question, history = [], namespace) {
9170
- var _a2, _b;
9171
9236
  await this.initialize();
9172
- if ((((_a2 = this.config.rag) == null ? void 0 : _a2.architecture) === "agentic" || ((_b = this.config.rag) == null ? void 0 : _b.architecture) === "multi-agent" || this.config.mcpServers && this.config.mcpServers.length > 0) && this.multiAgentCoordinator) {
9237
+ if (this.isMultiAgentMode && this.multiAgentCoordinator) {
9173
9238
  return await this.multiAgentCoordinator.run(question, history);
9174
9239
  }
9240
+ if (this.isLangChainAgentMode && this.agent) {
9241
+ const reply2 = await this.agent.run(question, history);
9242
+ return { reply: reply2, sources: [] };
9243
+ }
9175
9244
  const stream = this.askStream(question, history, namespace);
9176
9245
  let reply = "";
9177
9246
  let sources = [];
@@ -9204,9 +9273,8 @@ ${m.content}`).join("\n\n---\n\n");
9204
9273
  }
9205
9274
  askStream(_0) {
9206
9275
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9207
- var _a2, _b;
9208
9276
  yield new __await(this.initialize());
9209
- if ((((_a2 = this.config.rag) == null ? void 0 : _a2.architecture) === "agentic" || ((_b = this.config.rag) == null ? void 0 : _b.architecture) === "multi-agent" || this.config.mcpServers && this.config.mcpServers.length > 0) && this.multiAgentCoordinator) {
9277
+ if (this.isMultiAgentMode && this.multiAgentCoordinator) {
9210
9278
  const stream2 = this.multiAgentCoordinator.runStream(question, history);
9211
9279
  try {
9212
9280
  for (var iter = __forAwait(stream2), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
@@ -9225,6 +9293,11 @@ ${m.content}`).join("\n\n---\n\n");
9225
9293
  }
9226
9294
  return;
9227
9295
  }
9296
+ if (this.isLangChainAgentMode && this.agent) {
9297
+ const reply = yield new __await(this.agent.run(question, history));
9298
+ yield reply;
9299
+ return;
9300
+ }
9228
9301
  const stream = this.askStreamInternal(question, history, namespace);
9229
9302
  try {
9230
9303
  for (var iter2 = __forAwait(stream), more2, temp2, error2; more2 = !(temp2 = yield new __await(iter2.next())).done; more2 = false) {
@@ -9255,7 +9328,7 @@ ${m.content}`).join("\n\n---\n\n");
9255
9328
  */
9256
9329
  askStreamInternal(_0) {
9257
9330
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9258
- var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B;
9331
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A;
9259
9332
  yield new __await(this.initialize());
9260
9333
  const ns = formatNamespace(namespace != null ? namespace : this.config.projectId);
9261
9334
  const topK = (_b = (_a2 = this.config.rag) == null ? void 0 : _a2.topK) != null ? _b : 5;
@@ -9300,10 +9373,11 @@ ${m.content}`).join("\n\n---\n\n");
9300
9373
  const hasNumericPredicates = Array.isArray(filter.__numericPredicates) && filter.__numericPredicates.length > 0;
9301
9374
  const wantsExhaustiveList = true;
9302
9375
  const retrievalLimit = Math.max(topK * 50, 1e3);
9376
+ const retrieveStart = performance.now();
9303
9377
  const rawSources = ((strategyResult === "vector" || strategyResult === "both") && queryVector && queryVector.length > 0 ? yield new __await(this.vectorDB.query(queryVector, retrievalLimit, ns, filter)) : []).filter((s) => Boolean(s && typeof s === "object"));
9304
9378
  const retrieveEnd = performance.now();
9305
- const embedMs = retrieveEnd - embedStart;
9306
- const retrieveMs = retrieveEnd - embedStart;
9379
+ const embedMs = retrieveStart - embedStart;
9380
+ const retrieveMs = retrieveEnd - retrieveStart;
9307
9381
  const rerankStart = performance.now();
9308
9382
  const structuredSources = this.applyStructuredFilters(rawSources, filter).filter((s) => Boolean(s && typeof s === "object"));
9309
9383
  let fullSources = hasNumericPredicates ? structuredSources : structuredSources.filter((m) => {
@@ -9526,7 +9600,7 @@ ${context}`;
9526
9600
  const latency = {
9527
9601
  embedMs: Math.round(embedMs),
9528
9602
  retrieveMs: Math.round(retrieveMs),
9529
- rerankMs: arch !== "naive" && (arch === "retrieve-and-rerank" || ((_x = this.config.rag) == null ? void 0 : _x.useReranking)) ? Math.round(rerankMs) : void 0,
9603
+ rerankMs: useReranking ? Math.round(rerankMs) : void 0,
9530
9604
  generateMs: Math.round(generateMs),
9531
9605
  totalMs: Math.round(totalMs)
9532
9606
  };
@@ -9539,7 +9613,7 @@ ${context}`;
9539
9613
  totalTokens: promptTokens + completionTokens,
9540
9614
  estimatedCostUsd: estimateCostUsd(promptTokens, completionTokens, this.config.llm.model)
9541
9615
  };
9542
- const awaitHallucination = ((_y = this.config.llm.options) == null ? void 0 : _y.awaitHallucination) === true;
9616
+ const awaitHallucination = ((_x = this.config.llm.options) == null ? void 0 : _x.awaitHallucination) === true;
9543
9617
  const [uiTransformation, hallucinationResult] = yield new __await(Promise.all([
9544
9618
  uiTransformationPromise,
9545
9619
  awaitHallucination ? hallucinationScoringPromise : Promise.resolve(void 0)
@@ -9568,10 +9642,10 @@ ${context}`;
9568
9642
  hallucinationReason: hScore.reason
9569
9643
  } : {});
9570
9644
  const trace = buildTrace(hallucinationResult);
9571
- const isTelemetryActive = (_A = (_z = this.config.telemetry) == null ? void 0 : _z.enabled) != null ? _A : Boolean(this.config.licenseKey);
9645
+ const isTelemetryActive = (_z = (_y = this.config.telemetry) == null ? void 0 : _y.enabled) != null ? _z : Boolean(this.config.licenseKey);
9572
9646
  if (isTelemetryActive) {
9573
9647
  const defaultUrl = "https://www.retrivora.com/api/telemetry";
9574
- const telemetryUrl = ((_B = this.config.telemetry) == null ? void 0 : _B.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
9648
+ const telemetryUrl = ((_A = this.config.telemetry) == null ? void 0 : _A.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
9575
9649
  const absoluteUrl = telemetryUrl.startsWith("http") ? telemetryUrl : "https://www.retrivora.com" + (telemetryUrl.startsWith("/") ? telemetryUrl : "/" + telemetryUrl);
9576
9650
  (async () => {
9577
9651
  var _a3, _b2, _c2, _d2, _e2;
@@ -9745,7 +9819,7 @@ ${context}`;
9745
9819
  return Number.isFinite(numeric) ? numeric : null;
9746
9820
  }
9747
9821
  async retrieve(query, options) {
9748
- var _a2, _b, _c, _d, _e, _f, _g2;
9822
+ var _a2, _b, _c, _d, _e, _f;
9749
9823
  const ns = formatNamespace((_a2 = options.namespace) != null ? _a2 : this.config.projectId);
9750
9824
  const topK = (_b = options.topK) != null ? _b : 5;
9751
9825
  const cacheKey = `${ns}::${query}`;
@@ -9776,16 +9850,7 @@ ${context}`;
9776
9850
  await this.vectorDB.query(queryVector, retrievalLimit, ns, baseFilter),
9777
9851
  baseFilter
9778
9852
  ) : [];
9779
- const resolvedSources = [];
9780
- for (const source of sources) {
9781
- const parentId = (_g2 = source.metadata) == null ? void 0 : _g2.parent_id;
9782
- if (parentId) {
9783
- console.log(`[Pipeline] Multi-Vector: Found child chunk. Parent ID: ${parentId}`);
9784
- resolvedSources.push(source);
9785
- } else {
9786
- resolvedSources.push(source);
9787
- }
9788
- }
9853
+ const resolvedSources = sources;
9789
9854
  console.log("[Pipeline] retrieve() response:", {
9790
9855
  query,
9791
9856
  namespace: ns,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@retrivora-ai/rag-engine",
3
- "version": "2.4.8",
3
+ "version": "2.5.0",
4
4
  "description": "Retrivora AI is a plug-and-play AI engine for RAG chat experiences — generic vector DB + LLM provider, embeddable or standalone.",
5
5
  "author": "Abhinav Alkuchi",
6
6
  "license": "UNLICENSED",