@retrivora-ai/rag-engine 2.4.8 → 2.4.9

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.js CHANGED
@@ -332,7 +332,7 @@ var init_package = __esm({
332
332
  "package.json"() {
333
333
  package_default = {
334
334
  name: "@retrivora-ai/rag-engine",
335
- version: "2.4.8",
335
+ version: "2.4.9",
336
336
  description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
337
337
  author: "Abhinav Alkuchi",
338
338
  license: "UNLICENSED",
@@ -1549,6 +1549,7 @@ var init_MongoDBProvider = __esm({
1549
1549
  MongoDBProvider = class extends BaseVectorProvider {
1550
1550
  constructor(config) {
1551
1551
  super(config);
1552
+ this.initialized = false;
1552
1553
  const opts = config.options;
1553
1554
  if (!opts.uri || !opts.database || !opts.collection) {
1554
1555
  throw new Error("[MongoDBProvider] options uri, database, and collection are required.");
@@ -1626,9 +1627,11 @@ var init_MongoDBProvider = __esm({
1626
1627
  };
1627
1628
  }
1628
1629
  async initialize() {
1630
+ if (this.initialized) return;
1629
1631
  await this.client.connect();
1630
1632
  this.db = this.client.db(this.dbName);
1631
1633
  this.collection = this.db.collection(this.collectionName);
1634
+ this.initialized = true;
1632
1635
  }
1633
1636
  async upsert(doc, namespace) {
1634
1637
  const document = __spreadValues({
@@ -1720,7 +1723,6 @@ var init_MongoDBProvider = __esm({
1720
1723
  const sanitized = super.sanitizeFilter(filter);
1721
1724
  const mongoFilter = {};
1722
1725
  for (const [key, value] of Object.entries(sanitized)) {
1723
- if (key.startsWith("__") || key === "keywords" || key === "queryText") continue;
1724
1726
  if (key === "namespace") {
1725
1727
  mongoFilter[key] = value;
1726
1728
  } else {
@@ -1739,6 +1741,7 @@ var init_MongoDBProvider = __esm({
1739
1741
  }
1740
1742
  async disconnect() {
1741
1743
  await this.client.close();
1744
+ this.initialized = false;
1742
1745
  }
1743
1746
  };
1744
1747
  }
@@ -1783,15 +1786,19 @@ var init_MilvusProvider = __esm({
1783
1786
  await this.http.post("/v1/vector/upsert", payload);
1784
1787
  }
1785
1788
  async batchUpsert(docs, namespace) {
1786
- const payload = {
1787
- collectionName: this.indexName,
1788
- data: docs.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);
1789
+ const batchSize = 100;
1790
+ for (let i = 0; i < docs.length; i += batchSize) {
1791
+ const batch = docs.slice(i, i + batchSize);
1792
+ const payload = {
1793
+ collectionName: this.indexName,
1794
+ data: batch.map((doc) => __spreadValues({
1795
+ vector: doc.vector,
1796
+ content: doc.content,
1797
+ metadata: doc.metadata || {}
1798
+ }, namespace ? { namespace } : {}))
1799
+ };
1800
+ await this.http.post("/v1/vector/upsert", payload);
1801
+ }
1795
1802
  }
1796
1803
  async query(vector, topK, namespace, _filter) {
1797
1804
  const sanitizedFilter = this.sanitizeFilter(_filter);
@@ -1802,7 +1809,8 @@ var init_MilvusProvider = __esm({
1802
1809
  for (const [key, value] of Object.entries(sanitizedFilter)) {
1803
1810
  if (key === "queryText" || key === "keywords") continue;
1804
1811
  if (typeof value === "string") {
1805
- filterParts.push(`metadata["${key}"] == "${value.replace(/"/g, '\\"')}"`);
1812
+ const escaped = value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
1813
+ filterParts.push(`metadata["${key}"] == "${escaped}"`);
1806
1814
  } else if (typeof value === "number") {
1807
1815
  filterParts.push(`metadata["${key}"] == ${value}`);
1808
1816
  }
@@ -1826,7 +1834,7 @@ var init_MilvusProvider = __esm({
1826
1834
  metadata: res["metadata"] || {}
1827
1835
  }));
1828
1836
  }
1829
- async delete(id, _namespace) {
1837
+ async delete(id, namespace) {
1830
1838
  await this.http.post("/v1/vector/delete", {
1831
1839
  collectionName: this.indexName,
1832
1840
  id
@@ -1898,7 +1906,6 @@ var init_QdrantProvider = __esm({
1898
1906
  return this.ensureIndex(fieldName, schemaType || "keyword");
1899
1907
  })
1900
1908
  );
1901
- } else {
1902
1909
  }
1903
1910
  this.schemaDiscovered = true;
1904
1911
  }
@@ -2144,7 +2151,7 @@ var init_ChromaDBProvider = __esm({
2144
2151
  documents: docs.map((d) => d.content),
2145
2152
  metadatas: docs.map((d) => __spreadValues(__spreadValues({}, d.metadata || {}), namespace ? { namespace } : {}))
2146
2153
  };
2147
- await this.http.post(`/api/v1/collections/${this.collectionId}/add`, payload);
2154
+ await this.http.post(`/api/v1/collections/${this.collectionId}/upsert`, payload);
2148
2155
  }
2149
2156
  async query(vector, topK, namespace, _filter) {
2150
2157
  const sanitizedFilter = this.sanitizeFilter(_filter);
@@ -2228,17 +2235,16 @@ var init_RedisProvider = __esm({
2228
2235
  await this.ping();
2229
2236
  }
2230
2237
  async upsert(doc, namespace) {
2231
- const key = namespace ? `${namespace}:${doc.id}` : String(doc.id);
2232
- await this.http.post("/", ["JSON.SET", key, "$", JSON.stringify({
2233
- vector: doc.vector,
2234
- content: doc.content,
2235
- metadata: doc.metadata || {}
2236
- })]);
2238
+ await this.batchUpsert([doc], namespace);
2237
2239
  }
2238
2240
  async batchUpsert(docs, namespace) {
2239
- for (const doc of docs) {
2240
- await this.upsert(doc, namespace);
2241
- }
2241
+ const payload = docs.map((doc) => __spreadValues({
2242
+ id: String(doc.id),
2243
+ vector: doc.vector,
2244
+ data: doc.content,
2245
+ metadata: doc.metadata || {}
2246
+ }, namespace ? { namespace } : {}));
2247
+ await this.http.post("/upsert", payload);
2242
2248
  }
2243
2249
  async query(vector, topK, namespace, _filter) {
2244
2250
  const payload = {
@@ -2256,19 +2262,22 @@ var init_RedisProvider = __esm({
2256
2262
  }));
2257
2263
  }
2258
2264
  async delete(id, namespace) {
2259
- const key = namespace ? `${namespace}:${id}` : id;
2260
- await this.http.post("/", ["DEL", key]);
2265
+ await this.http.post("/delete", __spreadValues({
2266
+ ids: [id]
2267
+ }, namespace ? { namespace } : {}));
2261
2268
  }
2262
2269
  async deleteNamespace(namespace) {
2263
- console.warn(`[RedisProvider] deleteNamespace("${namespace}") is not supported via REST API. Use Redis CLI: SCAN + DEL`);
2270
+ if (!namespace) {
2271
+ throw new Error("[RedisProvider] namespace is required for deleteNamespace");
2272
+ }
2273
+ await this.http.post("/reset", { namespace });
2264
2274
  }
2265
2275
  /**
2266
- * Check reachability via a PING command.
2267
- * Returns false on connection failure so health checks surface real problems.
2276
+ * Check reachability of the Upstash Vector API.
2268
2277
  */
2269
2278
  async ping() {
2270
2279
  try {
2271
- await this.http.post("/", ["PING"]);
2280
+ await this.http.get("/");
2272
2281
  return true;
2273
2282
  } catch (e) {
2274
2283
  return false;
@@ -2299,11 +2308,12 @@ var init_WeaviateProvider = __esm({
2299
2308
  if (!baseUrl) throw new Error("[WeaviateProvider] options.baseUrl is required");
2300
2309
  this.http = import_axios7.default.create({
2301
2310
  baseURL: baseUrl,
2302
- headers: __spreadValues({
2311
+ headers: __spreadValues(__spreadValues({
2303
2312
  "Content-Type": "application/json"
2304
2313
  }, opts.apiKey ? {
2305
- "X-OpenAI-Api-Key": opts.apiKey,
2306
2314
  "Authorization": `Bearer ${opts.apiKey}`
2315
+ } : {}), opts.openaiApiKey ? {
2316
+ "X-OpenAI-Api-Key": opts.openaiApiKey
2307
2317
  } : {})
2308
2318
  });
2309
2319
  }
@@ -2380,10 +2390,12 @@ var init_WeaviateProvider = __esm({
2380
2390
  await this.http.delete(`/v1/objects/${this.indexName}/${id}`);
2381
2391
  }
2382
2392
  async deleteNamespace(namespace) {
2383
- await this.http.post("/v1/batch/objects", {
2384
- match: {
2385
- class: this.indexName,
2386
- where: { path: ["namespace"], operator: "Equal", valueString: namespace }
2393
+ await this.http.delete("/v1/batch/objects", {
2394
+ data: {
2395
+ match: {
2396
+ class: this.indexName,
2397
+ where: { path: ["namespace"], operator: "Equal", valueString: namespace }
2398
+ }
2387
2399
  }
2388
2400
  });
2389
2401
  }
@@ -2614,6 +2626,9 @@ var init_SimpleGraphProvider = __esm({
2614
2626
  this.edges = [];
2615
2627
  }
2616
2628
  async initialize() {
2629
+ console.warn(
2630
+ "[SimpleGraphProvider] Warning: SimpleGraphProvider is an in-memory database meant for demonstration and local development. Graph data is NOT persisted across restarts."
2631
+ );
2617
2632
  }
2618
2633
  async addNodes(nodes) {
2619
2634
  for (const node of nodes) {
@@ -2635,15 +2650,21 @@ var init_SimpleGraphProvider = __esm({
2635
2650
  for (const edge of matchedEdges) {
2636
2651
  if (!matchedNodeIds.has(edge.source)) {
2637
2652
  const source = this.nodes.get(edge.source);
2638
- if (source) matchedNodes.push(source);
2653
+ if (source) {
2654
+ matchedNodes.push(source);
2655
+ matchedNodeIds.add(edge.source);
2656
+ }
2639
2657
  }
2640
2658
  if (!matchedNodeIds.has(edge.target)) {
2641
2659
  const target = this.nodes.get(edge.target);
2642
- if (target) matchedNodes.push(target);
2660
+ if (target) {
2661
+ matchedNodes.push(target);
2662
+ matchedNodeIds.add(edge.target);
2663
+ }
2643
2664
  }
2644
2665
  }
2645
2666
  return {
2646
- nodes: Array.from(new Set(matchedNodes)),
2667
+ nodes: matchedNodes,
2647
2668
  edges: matchedEdges
2648
2669
  };
2649
2670
  }
@@ -5918,27 +5939,42 @@ var MCPClient = class {
5918
5939
  var MCPRegistry = class {
5919
5940
  constructor(servers = []) {
5920
5941
  this.clients = [];
5942
+ /** Cache populated by getAllTools(); maps tool name → the client that owns it. */
5943
+ this.toolClientCache = null;
5921
5944
  this.clients = servers.map((cfg) => new MCPClient(cfg));
5922
5945
  }
5923
5946
  async getAllTools() {
5924
5947
  const list = [];
5948
+ const cache = /* @__PURE__ */ new Map();
5925
5949
  await Promise.all(
5926
5950
  this.clients.map(async (client) => {
5927
5951
  try {
5928
5952
  const tools = await client.listTools();
5929
- tools.forEach((t) => list.push({ serverName: client["config"].name, tool: t }));
5953
+ tools.forEach((t) => {
5954
+ list.push({ serverName: client["config"].name, tool: t });
5955
+ cache.set(t.name, client);
5956
+ });
5930
5957
  } catch (e) {
5931
5958
  console.warn(`[MCPRegistry] Failed to fetch tools from server "${client["config"].name}":`, e);
5932
5959
  }
5933
5960
  })
5934
5961
  );
5962
+ this.toolClientCache = cache;
5935
5963
  return list;
5936
5964
  }
5937
5965
  async callTool(toolName, args = {}) {
5966
+ var _a2;
5967
+ const cachedClient = (_a2 = this.toolClientCache) == null ? void 0 : _a2.get(toolName);
5968
+ if (cachedClient) {
5969
+ return cachedClient.callTool(toolName, args);
5970
+ }
5971
+ console.warn(`[MCPRegistry] Cache miss for tool "${toolName}", falling back to full scan.`);
5938
5972
  for (const client of this.clients) {
5939
5973
  try {
5940
5974
  const tools = await client.listTools();
5941
5975
  if (tools.some((t) => t.name === toolName)) {
5976
+ if (!this.toolClientCache) this.toolClientCache = /* @__PURE__ */ new Map();
5977
+ this.toolClientCache.set(toolName, client);
5942
5978
  return await client.callTool(toolName, args);
5943
5979
  }
5944
5980
  } catch (e) {
@@ -5946,18 +5982,44 @@ var MCPRegistry = class {
5946
5982
  }
5947
5983
  throw new Error(`[MCPRegistry] Tool "${toolName}" not found on any registered MCP server.`);
5948
5984
  }
5985
+ /** Invalidate the tool cache and disconnect all MCP clients. */
5949
5986
  disconnectAll() {
5987
+ this.toolClientCache = null;
5950
5988
  this.clients.forEach((c) => c.disconnect());
5951
5989
  }
5952
5990
  };
5953
5991
 
5954
5992
  // src/core/MultiAgentCoordinator.ts
5993
+ function parseToolCall(text) {
5994
+ const match = text.match(/TOOL_CALL:\s*([\w-]+)\s*\(\s*([\s\S]*?)\s*\)\s*[.,;]?\s*$/m);
5995
+ if (!match) return null;
5996
+ const name = match[1].trim();
5997
+ let argsRaw = match[2].trim();
5998
+ argsRaw = argsRaw.replace(/'/g, '"');
5999
+ if (argsRaw && !argsRaw.startsWith("{")) {
6000
+ argsRaw = `{${argsRaw}}`;
6001
+ }
6002
+ let args = {};
6003
+ if (argsRaw) {
6004
+ try {
6005
+ args = JSON.parse(argsRaw);
6006
+ } catch (e) {
6007
+ const pairs = [...argsRaw.matchAll(/"?([\w-]+)"?\s*:\s*"([^"]*)"/g)];
6008
+ if (pairs.length === 0) {
6009
+ console.warn("[parseToolCall] Could not parse tool args:", argsRaw);
6010
+ return null;
6011
+ }
6012
+ for (const [, k, v] of pairs) args[k] = v;
6013
+ }
6014
+ }
6015
+ return { name, args };
6016
+ }
5955
6017
  var MultiAgentCoordinator = class {
5956
6018
  constructor(options) {
5957
6019
  var _a2;
5958
6020
  this.llmProvider = options.llmProvider;
5959
6021
  this.mcpRegistry = options.mcpRegistry;
5960
- this.documentSearch = options.documentSearch;
6022
+ this.retriever = options.retriever;
5961
6023
  this.maxIterations = (_a2 = options.maxIterations) != null ? _a2 : 5;
5962
6024
  }
5963
6025
  /**
@@ -5991,18 +6053,17 @@ var MultiAgentCoordinator = class {
5991
6053
  throw error[0];
5992
6054
  }
5993
6055
  }
5994
- return {
5995
- reply: finalReply,
5996
- sources
5997
- };
6056
+ return { reply: finalReply, sources };
5998
6057
  }
5999
6058
  /**
6000
- * Run the multi-agent coordination loop as an async stream, yielding intermediate thought blocks and tool execution events.
6059
+ * Run the multi-agent coordination loop as an async stream,
6060
+ * yielding intermediate thought blocks and tool execution events.
6001
6061
  */
6002
6062
  runStream(_0) {
6003
6063
  return __asyncGenerator(this, arguments, function* (question, history = []) {
6064
+ var _a2;
6004
6065
  const mcpTools = yield new __await(this.mcpRegistry.getAllTools());
6005
- let systemPrompt = `You are a goal-driven supervisor agent coordinating a RAG search engine and external MCP servers to solve the user's task.
6066
+ const systemPrompt = `You are a goal-driven supervisor agent coordinating a RAG search engine and external MCP servers to solve the user's task.
6006
6067
  You must think step-by-step before acting. Write your internal thinking process inside a \`<think>...</think>\` block first, then act or answer.
6007
6068
 
6008
6069
  Available Tools:
@@ -6010,8 +6071,8 @@ Available Tools:
6010
6071
 
6011
6072
  ${mcpTools.length > 0 ? "MCP Server Tools:" : ""}
6012
6073
  ${mcpTools.map((mt) => {
6013
- var _a2;
6014
- return `- ${mt.tool.name}(${JSON.stringify(((_a2 = mt.tool.inputSchema) == null ? void 0 : _a2.properties) || {})}): ${mt.tool.description || "No description provided."}`;
6074
+ var _a3;
6075
+ return `- ${mt.tool.name}(${JSON.stringify(((_a3 = mt.tool.inputSchema) == null ? void 0 : _a3.properties) || {})}): ${mt.tool.description || "No description provided."}`;
6015
6076
  }).join("\n")}
6016
6077
 
6017
6078
  Tool Calling Protocol:
@@ -6019,7 +6080,7 @@ If you need to call a tool, write:
6019
6080
  TOOL_CALL: <toolName>({ "argName": "value" })
6020
6081
  And then STOP generating immediately.
6021
6082
 
6022
- Once you have gathered enough information to fully satisfy the user's query, write your final response directly after the closing \`</think>\` block.
6083
+ Once you have gathered enough information to fully satisfy the user query, write your final response directly after the closing \`</think>\` block.
6023
6084
  `;
6024
6085
  const supervisorHistory = [
6025
6086
  { role: "system", content: systemPrompt },
@@ -6027,7 +6088,6 @@ Once you have gathered enough information to fully satisfy the user's query, wri
6027
6088
  { role: "user", content: question }
6028
6089
  ];
6029
6090
  let iteration = 0;
6030
- const allSources = [];
6031
6091
  while (iteration < this.maxIterations) {
6032
6092
  iteration++;
6033
6093
  const context = "";
@@ -6040,24 +6100,14 @@ Once you have gathered enough information to fully satisfy the user's query, wri
6040
6100
  fullModelResponse = reply;
6041
6101
  const thinkIndex = reply.indexOf("<think>");
6042
6102
  const endThinkIndex = reply.indexOf("</think>");
6043
- let thinkingText = "";
6044
6103
  let answerText = reply;
6045
6104
  if (thinkIndex !== -1 && endThinkIndex !== -1) {
6046
- thinkingText = reply.substring(thinkIndex + 7, endThinkIndex);
6105
+ const thinkingText = reply.substring(thinkIndex + 7, endThinkIndex);
6047
6106
  answerText = reply.substring(endThinkIndex + 8);
6048
6107
  yield { type: "thinking", text: thinkingText };
6049
6108
  }
6050
- const toolMatch = answerText.match(/TOOL_CALL:\s*(\w+)\s*\(([\s\S]*)\)/);
6051
- if (toolMatch) {
6052
- const toolName = toolMatch[1];
6053
- const toolArgsStr = toolMatch[2];
6054
- try {
6055
- const toolArgs = JSON.parse(toolArgsStr.trim());
6056
- toolCallDetected = { name: toolName, args: toolArgs };
6057
- } catch (e) {
6058
- console.error("[MultiAgentCoordinator] Failed to parse tool args:", toolArgsStr, e);
6059
- }
6060
- } else {
6109
+ toolCallDetected = parseToolCall(answerText);
6110
+ if (!toolCallDetected) {
6061
6111
  yield answerText;
6062
6112
  }
6063
6113
  } else {
@@ -6086,9 +6136,7 @@ Once you have gathered enough information to fully satisfy the user's query, wri
6086
6136
  if (!inThinking && textBuffer.includes("TOOL_CALL:")) {
6087
6137
  const idx = textBuffer.indexOf("TOOL_CALL:");
6088
6138
  const precedingText = textBuffer.slice(0, idx);
6089
- if (precedingText.trim()) {
6090
- yield precedingText;
6091
- }
6139
+ if (precedingText.trim()) yield precedingText;
6092
6140
  textBuffer = textBuffer.slice(idx);
6093
6141
  }
6094
6142
  if (!inThinking && !textBuffer.includes("TOOL_CALL:") && textBuffer.length > 0) {
@@ -6107,17 +6155,8 @@ Once you have gathered enough information to fully satisfy the user's query, wri
6107
6155
  }
6108
6156
  }
6109
6157
  if (textBuffer.trim()) {
6110
- const toolMatch = textBuffer.match(/TOOL_CALL:\s*(\w+)\s*\(([\s\S]*)\)/);
6111
- if (toolMatch) {
6112
- const toolName = toolMatch[1];
6113
- const toolArgsStr = toolMatch[2];
6114
- try {
6115
- const toolArgs = JSON.parse(toolArgsStr.trim());
6116
- toolCallDetected = { name: toolName, args: toolArgs };
6117
- } catch (e) {
6118
- console.error("[MultiAgentCoordinator] Failed to parse tool args:", toolArgsStr, e);
6119
- }
6120
- } else {
6158
+ toolCallDetected = parseToolCall(textBuffer);
6159
+ if (!toolCallDetected) {
6121
6160
  yield textBuffer;
6122
6161
  }
6123
6162
  }
@@ -6134,14 +6173,16 @@ Once you have gathered enough information to fully satisfy the user's query, wri
6134
6173
  let toolResultText = "";
6135
6174
  try {
6136
6175
  if (toolName === "document_search") {
6137
- const searchRes = yield new __await(this.documentSearch(toolArgs.query || ""));
6138
- toolResultText = `document_search returned:
6139
- ${searchRes.reply}
6140
-
6141
- Sources count: ${searchRes.sources.length}`;
6142
- if (searchRes.sources && searchRes.sources.length > 0) {
6143
- allSources.push(...searchRes.sources);
6144
- yield { reply: "", sources: searchRes.sources };
6176
+ const query = String((_a2 = toolArgs.query) != null ? _a2 : "");
6177
+ const { sources } = yield new __await(this.retriever.retrieve(query));
6178
+ const snippets = sources.slice(0, 5).map((s, i) => {
6179
+ var _a3;
6180
+ return `[${i + 1}] ${String((_a3 = s.content) != null ? _a3 : "").slice(0, 400)}`;
6181
+ }).join("\n\n");
6182
+ toolResultText = `document_search returned ${sources.length} source(s):
6183
+ ${snippets}`;
6184
+ if (sources.length > 0) {
6185
+ yield { reply: "", sources };
6145
6186
  }
6146
6187
  } else {
6147
6188
  const mcpRes = yield new __await(this.mcpRegistry.callTool(toolName, toolArgs));
@@ -9053,8 +9094,30 @@ var Pipeline = class {
9053
9094
  getLLMProvider() {
9054
9095
  return this.initialised ? this.llmProvider : void 0;
9055
9096
  }
9097
+ /**
9098
+ * True when multi-agent / MCP mode is active:
9099
+ * architecture === 'multi-agent' OR mcpServers array is non-empty.
9100
+ * Uses MultiAgentCoordinator (custom supervisor loop).
9101
+ */
9102
+ get isMultiAgentMode() {
9103
+ var _a2, _b, _c;
9104
+ 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;
9105
+ }
9106
+ /**
9107
+ * True when the LangChain ReAct agent mode is selected:
9108
+ * architecture === 'agentic'.
9109
+ * Uses LangChainAgent (LangGraph-backed ReAct, optional peer dep).
9110
+ */
9111
+ get isLangChainAgentMode() {
9112
+ var _a2;
9113
+ return ((_a2 = this.config.rag) == null ? void 0 : _a2.architecture) === "agentic";
9114
+ }
9115
+ /** Union of both agentic execution paths — used to gate the normal RAG pipeline. */
9116
+ get isAgenticMode() {
9117
+ return this.isMultiAgentMode || this.isLangChainAgentMode;
9118
+ }
9056
9119
  async initialize() {
9057
- var _a2, _b, _c, _d;
9120
+ var _a2, _b, _c;
9058
9121
  if (this.initialised) return;
9059
9122
  const chartInstruction = `You are a helpful assistant. Use the provided context to answer questions accurately.
9060
9123
 
@@ -9097,19 +9160,22 @@ var Pipeline = class {
9097
9160
  this.entityExtractor = new EntityExtractor(this.llmProvider);
9098
9161
  }
9099
9162
  await this.vectorDB.initialize();
9100
- 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) {
9163
+ if (this.isMultiAgentMode) {
9164
+ console.log("[Pipeline] Initializing MultiAgentCoordinator (multi-agent / MCP mode).");
9101
9165
  this.mcpRegistry = new MCPRegistry(this.config.mcpServers || []);
9102
9166
  this.multiAgentCoordinator = new MultiAgentCoordinator({
9103
9167
  llmProvider: this.llmProvider,
9104
9168
  mcpRegistry: this.mcpRegistry,
9105
- documentSearch: async (query) => {
9106
- return this.runNormalQuery(query);
9107
- }
9169
+ // Pass Pipeline itself as the IRetriever — avoids closure over internal methods.
9170
+ retriever: this,
9171
+ maxIterations: (_a2 = this.config.rag) == null ? void 0 : _a2.agentMaxIterations
9108
9172
  });
9173
+ } else if (this.isLangChainAgentMode) {
9174
+ console.log("[Pipeline] Initializing LangChainAgent (agentic / ReAct mode).");
9109
9175
  this.agent = new LangChainAgent(this, this.config);
9110
9176
  await this.agent.initialize(this.llmProvider);
9111
9177
  }
9112
- if ((_d = (_c = this.config.rag) == null ? void 0 : _c.cag) == null ? void 0 : _d.enabled) {
9178
+ if ((_c = (_b = this.config.rag) == null ? void 0 : _b.cag) == null ? void 0 : _c.enabled) {
9113
9179
  await this.loadColdContext(this.config.projectId);
9114
9180
  }
9115
9181
  this.initialised = true;
@@ -9272,11 +9338,14 @@ ${m.content}`).join("\n\n---\n\n");
9272
9338
  return { reply, sources };
9273
9339
  }
9274
9340
  async ask(question, history = [], namespace) {
9275
- var _a2, _b;
9276
9341
  await this.initialize();
9277
- 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) {
9342
+ if (this.isMultiAgentMode && this.multiAgentCoordinator) {
9278
9343
  return await this.multiAgentCoordinator.run(question, history);
9279
9344
  }
9345
+ if (this.isLangChainAgentMode && this.agent) {
9346
+ const reply2 = await this.agent.run(question, history);
9347
+ return { reply: reply2, sources: [] };
9348
+ }
9280
9349
  const stream = this.askStream(question, history, namespace);
9281
9350
  let reply = "";
9282
9351
  let sources = [];
@@ -9309,9 +9378,8 @@ ${m.content}`).join("\n\n---\n\n");
9309
9378
  }
9310
9379
  askStream(_0) {
9311
9380
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9312
- var _a2, _b;
9313
9381
  yield new __await(this.initialize());
9314
- 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) {
9382
+ if (this.isMultiAgentMode && this.multiAgentCoordinator) {
9315
9383
  const stream2 = this.multiAgentCoordinator.runStream(question, history);
9316
9384
  try {
9317
9385
  for (var iter = __forAwait(stream2), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
@@ -9330,6 +9398,11 @@ ${m.content}`).join("\n\n---\n\n");
9330
9398
  }
9331
9399
  return;
9332
9400
  }
9401
+ if (this.isLangChainAgentMode && this.agent) {
9402
+ const reply = yield new __await(this.agent.run(question, history));
9403
+ yield reply;
9404
+ return;
9405
+ }
9333
9406
  const stream = this.askStreamInternal(question, history, namespace);
9334
9407
  try {
9335
9408
  for (var iter2 = __forAwait(stream), more2, temp2, error2; more2 = !(temp2 = yield new __await(iter2.next())).done; more2 = false) {
@@ -9360,7 +9433,7 @@ ${m.content}`).join("\n\n---\n\n");
9360
9433
  */
9361
9434
  askStreamInternal(_0) {
9362
9435
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9363
- 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;
9436
+ 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;
9364
9437
  yield new __await(this.initialize());
9365
9438
  const ns = formatNamespace(namespace != null ? namespace : this.config.projectId);
9366
9439
  const topK = (_b = (_a2 = this.config.rag) == null ? void 0 : _a2.topK) != null ? _b : 5;
@@ -9405,10 +9478,11 @@ ${m.content}`).join("\n\n---\n\n");
9405
9478
  const hasNumericPredicates = Array.isArray(filter.__numericPredicates) && filter.__numericPredicates.length > 0;
9406
9479
  const wantsExhaustiveList = true;
9407
9480
  const retrievalLimit = Math.max(topK * 50, 1e3);
9481
+ const retrieveStart = performance.now();
9408
9482
  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"));
9409
9483
  const retrieveEnd = performance.now();
9410
- const embedMs = retrieveEnd - embedStart;
9411
- const retrieveMs = retrieveEnd - embedStart;
9484
+ const embedMs = retrieveStart - embedStart;
9485
+ const retrieveMs = retrieveEnd - retrieveStart;
9412
9486
  const rerankStart = performance.now();
9413
9487
  const structuredSources = this.applyStructuredFilters(rawSources, filter).filter((s) => Boolean(s && typeof s === "object"));
9414
9488
  let fullSources = hasNumericPredicates ? structuredSources : structuredSources.filter((m) => {
@@ -9631,7 +9705,7 @@ ${context}`;
9631
9705
  const latency = {
9632
9706
  embedMs: Math.round(embedMs),
9633
9707
  retrieveMs: Math.round(retrieveMs),
9634
- rerankMs: arch !== "naive" && (arch === "retrieve-and-rerank" || ((_x = this.config.rag) == null ? void 0 : _x.useReranking)) ? Math.round(rerankMs) : void 0,
9708
+ rerankMs: useReranking ? Math.round(rerankMs) : void 0,
9635
9709
  generateMs: Math.round(generateMs),
9636
9710
  totalMs: Math.round(totalMs)
9637
9711
  };
@@ -9644,7 +9718,7 @@ ${context}`;
9644
9718
  totalTokens: promptTokens + completionTokens,
9645
9719
  estimatedCostUsd: estimateCostUsd(promptTokens, completionTokens, this.config.llm.model)
9646
9720
  };
9647
- const awaitHallucination = ((_y = this.config.llm.options) == null ? void 0 : _y.awaitHallucination) === true;
9721
+ const awaitHallucination = ((_x = this.config.llm.options) == null ? void 0 : _x.awaitHallucination) === true;
9648
9722
  const [uiTransformation, hallucinationResult] = yield new __await(Promise.all([
9649
9723
  uiTransformationPromise,
9650
9724
  awaitHallucination ? hallucinationScoringPromise : Promise.resolve(void 0)
@@ -9673,10 +9747,10 @@ ${context}`;
9673
9747
  hallucinationReason: hScore.reason
9674
9748
  } : {});
9675
9749
  const trace = buildTrace(hallucinationResult);
9676
- const isTelemetryActive = (_A = (_z = this.config.telemetry) == null ? void 0 : _z.enabled) != null ? _A : Boolean(this.config.licenseKey);
9750
+ const isTelemetryActive = (_z = (_y = this.config.telemetry) == null ? void 0 : _y.enabled) != null ? _z : Boolean(this.config.licenseKey);
9677
9751
  if (isTelemetryActive) {
9678
9752
  const defaultUrl = "https://www.retrivora.com/api/telemetry";
9679
- const telemetryUrl = ((_B = this.config.telemetry) == null ? void 0 : _B.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
9753
+ const telemetryUrl = ((_A = this.config.telemetry) == null ? void 0 : _A.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
9680
9754
  const absoluteUrl = telemetryUrl.startsWith("http") ? telemetryUrl : "https://www.retrivora.com" + (telemetryUrl.startsWith("/") ? telemetryUrl : "/" + telemetryUrl);
9681
9755
  (async () => {
9682
9756
  var _a3, _b2, _c2, _d2, _e2;
@@ -9850,7 +9924,7 @@ ${context}`;
9850
9924
  return Number.isFinite(numeric) ? numeric : null;
9851
9925
  }
9852
9926
  async retrieve(query, options) {
9853
- var _a2, _b, _c, _d, _e, _f, _g2;
9927
+ var _a2, _b, _c, _d, _e, _f;
9854
9928
  const ns = formatNamespace((_a2 = options.namespace) != null ? _a2 : this.config.projectId);
9855
9929
  const topK = (_b = options.topK) != null ? _b : 5;
9856
9930
  const cacheKey = `${ns}::${query}`;
@@ -9881,16 +9955,7 @@ ${context}`;
9881
9955
  await this.vectorDB.query(queryVector, retrievalLimit, ns, baseFilter),
9882
9956
  baseFilter
9883
9957
  ) : [];
9884
- const resolvedSources = [];
9885
- for (const source of sources) {
9886
- const parentId = (_g2 = source.metadata) == null ? void 0 : _g2.parent_id;
9887
- if (parentId) {
9888
- console.log(`[Pipeline] Multi-Vector: Found child chunk. Parent ID: ${parentId}`);
9889
- resolvedSources.push(source);
9890
- } else {
9891
- resolvedSources.push(source);
9892
- }
9893
- }
9958
+ const resolvedSources = sources;
9894
9959
  console.log("[Pipeline] retrieve() response:", {
9895
9960
  query,
9896
9961
  namespace: ns,