@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.
@@ -318,7 +318,7 @@ var init_package = __esm({
318
318
  "package.json"() {
319
319
  package_default = {
320
320
  name: "@retrivora-ai/rag-engine",
321
- version: "2.4.8",
321
+ version: "2.5.0",
322
322
  description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
323
323
  author: "Abhinav Alkuchi",
324
324
  license: "UNLICENSED",
@@ -1535,6 +1535,7 @@ var init_MongoDBProvider = __esm({
1535
1535
  MongoDBProvider = class extends BaseVectorProvider {
1536
1536
  constructor(config) {
1537
1537
  super(config);
1538
+ this.initialized = false;
1538
1539
  const opts = config.options;
1539
1540
  if (!opts.uri || !opts.database || !opts.collection) {
1540
1541
  throw new Error("[MongoDBProvider] options uri, database, and collection are required.");
@@ -1612,9 +1613,11 @@ var init_MongoDBProvider = __esm({
1612
1613
  };
1613
1614
  }
1614
1615
  async initialize() {
1616
+ if (this.initialized) return;
1615
1617
  await this.client.connect();
1616
1618
  this.db = this.client.db(this.dbName);
1617
1619
  this.collection = this.db.collection(this.collectionName);
1620
+ this.initialized = true;
1618
1621
  }
1619
1622
  async upsert(doc, namespace) {
1620
1623
  const document = __spreadValues({
@@ -1706,7 +1709,6 @@ var init_MongoDBProvider = __esm({
1706
1709
  const sanitized = super.sanitizeFilter(filter);
1707
1710
  const mongoFilter = {};
1708
1711
  for (const [key, value] of Object.entries(sanitized)) {
1709
- if (key.startsWith("__") || key === "keywords" || key === "queryText") continue;
1710
1712
  if (key === "namespace") {
1711
1713
  mongoFilter[key] = value;
1712
1714
  } else {
@@ -1725,6 +1727,7 @@ var init_MongoDBProvider = __esm({
1725
1727
  }
1726
1728
  async disconnect() {
1727
1729
  await this.client.close();
1730
+ this.initialized = false;
1728
1731
  }
1729
1732
  };
1730
1733
  }
@@ -1769,15 +1772,19 @@ var init_MilvusProvider = __esm({
1769
1772
  await this.http.post("/v1/vector/upsert", payload);
1770
1773
  }
1771
1774
  async batchUpsert(docs, namespace) {
1772
- const payload = {
1773
- collectionName: this.indexName,
1774
- data: docs.map((doc) => __spreadValues({
1775
- vector: doc.vector,
1776
- content: doc.content,
1777
- metadata: doc.metadata || {}
1778
- }, namespace ? { namespace } : {}))
1779
- };
1780
- await this.http.post("/v1/vector/upsert", payload);
1775
+ const batchSize = 100;
1776
+ for (let i = 0; i < docs.length; i += batchSize) {
1777
+ const batch = docs.slice(i, i + batchSize);
1778
+ const payload = {
1779
+ collectionName: this.indexName,
1780
+ data: batch.map((doc) => __spreadValues({
1781
+ vector: doc.vector,
1782
+ content: doc.content,
1783
+ metadata: doc.metadata || {}
1784
+ }, namespace ? { namespace } : {}))
1785
+ };
1786
+ await this.http.post("/v1/vector/upsert", payload);
1787
+ }
1781
1788
  }
1782
1789
  async query(vector, topK, namespace, _filter) {
1783
1790
  const sanitizedFilter = this.sanitizeFilter(_filter);
@@ -1788,7 +1795,8 @@ var init_MilvusProvider = __esm({
1788
1795
  for (const [key, value] of Object.entries(sanitizedFilter)) {
1789
1796
  if (key === "queryText" || key === "keywords") continue;
1790
1797
  if (typeof value === "string") {
1791
- filterParts.push(`metadata["${key}"] == "${value.replace(/"/g, '\\"')}"`);
1798
+ const escaped = value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
1799
+ filterParts.push(`metadata["${key}"] == "${escaped}"`);
1792
1800
  } else if (typeof value === "number") {
1793
1801
  filterParts.push(`metadata["${key}"] == ${value}`);
1794
1802
  }
@@ -1812,7 +1820,7 @@ var init_MilvusProvider = __esm({
1812
1820
  metadata: res["metadata"] || {}
1813
1821
  }));
1814
1822
  }
1815
- async delete(id, _namespace) {
1823
+ async delete(id, namespace) {
1816
1824
  await this.http.post("/v1/vector/delete", {
1817
1825
  collectionName: this.indexName,
1818
1826
  id
@@ -1884,7 +1892,6 @@ var init_QdrantProvider = __esm({
1884
1892
  return this.ensureIndex(fieldName, schemaType || "keyword");
1885
1893
  })
1886
1894
  );
1887
- } else {
1888
1895
  }
1889
1896
  this.schemaDiscovered = true;
1890
1897
  }
@@ -2130,7 +2137,7 @@ var init_ChromaDBProvider = __esm({
2130
2137
  documents: docs.map((d) => d.content),
2131
2138
  metadatas: docs.map((d) => __spreadValues(__spreadValues({}, d.metadata || {}), namespace ? { namespace } : {}))
2132
2139
  };
2133
- await this.http.post(`/api/v1/collections/${this.collectionId}/add`, payload);
2140
+ await this.http.post(`/api/v1/collections/${this.collectionId}/upsert`, payload);
2134
2141
  }
2135
2142
  async query(vector, topK, namespace, _filter) {
2136
2143
  const sanitizedFilter = this.sanitizeFilter(_filter);
@@ -2214,17 +2221,16 @@ var init_RedisProvider = __esm({
2214
2221
  await this.ping();
2215
2222
  }
2216
2223
  async upsert(doc, namespace) {
2217
- const key = namespace ? `${namespace}:${doc.id}` : String(doc.id);
2218
- await this.http.post("/", ["JSON.SET", key, "$", JSON.stringify({
2219
- vector: doc.vector,
2220
- content: doc.content,
2221
- metadata: doc.metadata || {}
2222
- })]);
2224
+ await this.batchUpsert([doc], namespace);
2223
2225
  }
2224
2226
  async batchUpsert(docs, namespace) {
2225
- for (const doc of docs) {
2226
- await this.upsert(doc, namespace);
2227
- }
2227
+ const payload = docs.map((doc) => __spreadValues({
2228
+ id: String(doc.id),
2229
+ vector: doc.vector,
2230
+ data: doc.content,
2231
+ metadata: doc.metadata || {}
2232
+ }, namespace ? { namespace } : {}));
2233
+ await this.http.post("/upsert", payload);
2228
2234
  }
2229
2235
  async query(vector, topK, namespace, _filter) {
2230
2236
  const payload = {
@@ -2242,19 +2248,22 @@ var init_RedisProvider = __esm({
2242
2248
  }));
2243
2249
  }
2244
2250
  async delete(id, namespace) {
2245
- const key = namespace ? `${namespace}:${id}` : id;
2246
- await this.http.post("/", ["DEL", key]);
2251
+ await this.http.post("/delete", __spreadValues({
2252
+ ids: [id]
2253
+ }, namespace ? { namespace } : {}));
2247
2254
  }
2248
2255
  async deleteNamespace(namespace) {
2249
- console.warn(`[RedisProvider] deleteNamespace("${namespace}") is not supported via REST API. Use Redis CLI: SCAN + DEL`);
2256
+ if (!namespace) {
2257
+ throw new Error("[RedisProvider] namespace is required for deleteNamespace");
2258
+ }
2259
+ await this.http.post("/reset", { namespace });
2250
2260
  }
2251
2261
  /**
2252
- * Check reachability via a PING command.
2253
- * Returns false on connection failure so health checks surface real problems.
2262
+ * Check reachability of the Upstash Vector API.
2254
2263
  */
2255
2264
  async ping() {
2256
2265
  try {
2257
- await this.http.post("/", ["PING"]);
2266
+ await this.http.get("/");
2258
2267
  return true;
2259
2268
  } catch (e) {
2260
2269
  return false;
@@ -2285,11 +2294,12 @@ var init_WeaviateProvider = __esm({
2285
2294
  if (!baseUrl) throw new Error("[WeaviateProvider] options.baseUrl is required");
2286
2295
  this.http = import_axios7.default.create({
2287
2296
  baseURL: baseUrl,
2288
- headers: __spreadValues({
2297
+ headers: __spreadValues(__spreadValues({
2289
2298
  "Content-Type": "application/json"
2290
2299
  }, opts.apiKey ? {
2291
- "X-OpenAI-Api-Key": opts.apiKey,
2292
2300
  "Authorization": `Bearer ${opts.apiKey}`
2301
+ } : {}), opts.openaiApiKey ? {
2302
+ "X-OpenAI-Api-Key": opts.openaiApiKey
2293
2303
  } : {})
2294
2304
  });
2295
2305
  }
@@ -2366,10 +2376,12 @@ var init_WeaviateProvider = __esm({
2366
2376
  await this.http.delete(`/v1/objects/${this.indexName}/${id}`);
2367
2377
  }
2368
2378
  async deleteNamespace(namespace) {
2369
- await this.http.post("/v1/batch/objects", {
2370
- match: {
2371
- class: this.indexName,
2372
- where: { path: ["namespace"], operator: "Equal", valueString: namespace }
2379
+ await this.http.delete("/v1/batch/objects", {
2380
+ data: {
2381
+ match: {
2382
+ class: this.indexName,
2383
+ where: { path: ["namespace"], operator: "Equal", valueString: namespace }
2384
+ }
2373
2385
  }
2374
2386
  });
2375
2387
  }
@@ -2600,6 +2612,9 @@ var init_SimpleGraphProvider = __esm({
2600
2612
  this.edges = [];
2601
2613
  }
2602
2614
  async initialize() {
2615
+ console.warn(
2616
+ "[SimpleGraphProvider] Warning: SimpleGraphProvider is an in-memory database meant for demonstration and local development. Graph data is NOT persisted across restarts."
2617
+ );
2603
2618
  }
2604
2619
  async addNodes(nodes) {
2605
2620
  for (const node of nodes) {
@@ -2621,15 +2636,21 @@ var init_SimpleGraphProvider = __esm({
2621
2636
  for (const edge of matchedEdges) {
2622
2637
  if (!matchedNodeIds.has(edge.source)) {
2623
2638
  const source = this.nodes.get(edge.source);
2624
- if (source) matchedNodes.push(source);
2639
+ if (source) {
2640
+ matchedNodes.push(source);
2641
+ matchedNodeIds.add(edge.source);
2642
+ }
2625
2643
  }
2626
2644
  if (!matchedNodeIds.has(edge.target)) {
2627
2645
  const target = this.nodes.get(edge.target);
2628
- if (target) matchedNodes.push(target);
2646
+ if (target) {
2647
+ matchedNodes.push(target);
2648
+ matchedNodeIds.add(edge.target);
2649
+ }
2629
2650
  }
2630
2651
  }
2631
2652
  return {
2632
- nodes: Array.from(new Set(matchedNodes)),
2653
+ nodes: matchedNodes,
2633
2654
  edges: matchedEdges
2634
2655
  };
2635
2656
  }
@@ -5771,27 +5792,42 @@ var MCPClient = class {
5771
5792
  var MCPRegistry = class {
5772
5793
  constructor(servers = []) {
5773
5794
  this.clients = [];
5795
+ /** Cache populated by getAllTools(); maps tool name → the client that owns it. */
5796
+ this.toolClientCache = null;
5774
5797
  this.clients = servers.map((cfg) => new MCPClient(cfg));
5775
5798
  }
5776
5799
  async getAllTools() {
5777
5800
  const list = [];
5801
+ const cache = /* @__PURE__ */ new Map();
5778
5802
  await Promise.all(
5779
5803
  this.clients.map(async (client) => {
5780
5804
  try {
5781
5805
  const tools = await client.listTools();
5782
- tools.forEach((t) => list.push({ serverName: client["config"].name, tool: t }));
5806
+ tools.forEach((t) => {
5807
+ list.push({ serverName: client["config"].name, tool: t });
5808
+ cache.set(t.name, client);
5809
+ });
5783
5810
  } catch (e) {
5784
5811
  console.warn(`[MCPRegistry] Failed to fetch tools from server "${client["config"].name}":`, e);
5785
5812
  }
5786
5813
  })
5787
5814
  );
5815
+ this.toolClientCache = cache;
5788
5816
  return list;
5789
5817
  }
5790
5818
  async callTool(toolName, args = {}) {
5819
+ var _a2;
5820
+ const cachedClient = (_a2 = this.toolClientCache) == null ? void 0 : _a2.get(toolName);
5821
+ if (cachedClient) {
5822
+ return cachedClient.callTool(toolName, args);
5823
+ }
5824
+ console.warn(`[MCPRegistry] Cache miss for tool "${toolName}", falling back to full scan.`);
5791
5825
  for (const client of this.clients) {
5792
5826
  try {
5793
5827
  const tools = await client.listTools();
5794
5828
  if (tools.some((t) => t.name === toolName)) {
5829
+ if (!this.toolClientCache) this.toolClientCache = /* @__PURE__ */ new Map();
5830
+ this.toolClientCache.set(toolName, client);
5795
5831
  return await client.callTool(toolName, args);
5796
5832
  }
5797
5833
  } catch (e) {
@@ -5799,18 +5835,44 @@ var MCPRegistry = class {
5799
5835
  }
5800
5836
  throw new Error(`[MCPRegistry] Tool "${toolName}" not found on any registered MCP server.`);
5801
5837
  }
5838
+ /** Invalidate the tool cache and disconnect all MCP clients. */
5802
5839
  disconnectAll() {
5840
+ this.toolClientCache = null;
5803
5841
  this.clients.forEach((c) => c.disconnect());
5804
5842
  }
5805
5843
  };
5806
5844
 
5807
5845
  // src/core/MultiAgentCoordinator.ts
5846
+ function parseToolCall(text) {
5847
+ const match = text.match(/TOOL_CALL:\s*([\w-]+)\s*\(\s*([\s\S]*?)\s*\)\s*[.,;]?\s*$/m);
5848
+ if (!match) return null;
5849
+ const name = match[1].trim();
5850
+ let argsRaw = match[2].trim();
5851
+ argsRaw = argsRaw.replace(/'/g, '"');
5852
+ if (argsRaw && !argsRaw.startsWith("{")) {
5853
+ argsRaw = `{${argsRaw}}`;
5854
+ }
5855
+ let args = {};
5856
+ if (argsRaw) {
5857
+ try {
5858
+ args = JSON.parse(argsRaw);
5859
+ } catch (e) {
5860
+ const pairs = [...argsRaw.matchAll(/"?([\w-]+)"?\s*:\s*"([^"]*)"/g)];
5861
+ if (pairs.length === 0) {
5862
+ console.warn("[parseToolCall] Could not parse tool args:", argsRaw);
5863
+ return null;
5864
+ }
5865
+ for (const [, k, v] of pairs) args[k] = v;
5866
+ }
5867
+ }
5868
+ return { name, args };
5869
+ }
5808
5870
  var MultiAgentCoordinator = class {
5809
5871
  constructor(options) {
5810
5872
  var _a2;
5811
5873
  this.llmProvider = options.llmProvider;
5812
5874
  this.mcpRegistry = options.mcpRegistry;
5813
- this.documentSearch = options.documentSearch;
5875
+ this.retriever = options.retriever;
5814
5876
  this.maxIterations = (_a2 = options.maxIterations) != null ? _a2 : 5;
5815
5877
  }
5816
5878
  /**
@@ -5844,18 +5906,17 @@ var MultiAgentCoordinator = class {
5844
5906
  throw error[0];
5845
5907
  }
5846
5908
  }
5847
- return {
5848
- reply: finalReply,
5849
- sources
5850
- };
5909
+ return { reply: finalReply, sources };
5851
5910
  }
5852
5911
  /**
5853
- * Run the multi-agent coordination loop as an async stream, yielding intermediate thought blocks and tool execution events.
5912
+ * Run the multi-agent coordination loop as an async stream,
5913
+ * yielding intermediate thought blocks and tool execution events.
5854
5914
  */
5855
5915
  runStream(_0) {
5856
5916
  return __asyncGenerator(this, arguments, function* (question, history = []) {
5917
+ var _a2;
5857
5918
  const mcpTools = yield new __await(this.mcpRegistry.getAllTools());
5858
- let systemPrompt = `You are a goal-driven supervisor agent coordinating a RAG search engine and external MCP servers to solve the user's task.
5919
+ const systemPrompt = `You are a goal-driven supervisor agent coordinating a RAG search engine and external MCP servers to solve the user's task.
5859
5920
  You must think step-by-step before acting. Write your internal thinking process inside a \`<think>...</think>\` block first, then act or answer.
5860
5921
 
5861
5922
  Available Tools:
@@ -5863,8 +5924,8 @@ Available Tools:
5863
5924
 
5864
5925
  ${mcpTools.length > 0 ? "MCP Server Tools:" : ""}
5865
5926
  ${mcpTools.map((mt) => {
5866
- var _a2;
5867
- return `- ${mt.tool.name}(${JSON.stringify(((_a2 = mt.tool.inputSchema) == null ? void 0 : _a2.properties) || {})}): ${mt.tool.description || "No description provided."}`;
5927
+ var _a3;
5928
+ return `- ${mt.tool.name}(${JSON.stringify(((_a3 = mt.tool.inputSchema) == null ? void 0 : _a3.properties) || {})}): ${mt.tool.description || "No description provided."}`;
5868
5929
  }).join("\n")}
5869
5930
 
5870
5931
  Tool Calling Protocol:
@@ -5872,7 +5933,7 @@ If you need to call a tool, write:
5872
5933
  TOOL_CALL: <toolName>({ "argName": "value" })
5873
5934
  And then STOP generating immediately.
5874
5935
 
5875
- Once you have gathered enough information to fully satisfy the user's query, write your final response directly after the closing \`</think>\` block.
5936
+ Once you have gathered enough information to fully satisfy the user query, write your final response directly after the closing \`</think>\` block.
5876
5937
  `;
5877
5938
  const supervisorHistory = [
5878
5939
  { role: "system", content: systemPrompt },
@@ -5880,7 +5941,6 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5880
5941
  { role: "user", content: question }
5881
5942
  ];
5882
5943
  let iteration = 0;
5883
- const allSources = [];
5884
5944
  while (iteration < this.maxIterations) {
5885
5945
  iteration++;
5886
5946
  const context = "";
@@ -5893,24 +5953,14 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5893
5953
  fullModelResponse = reply;
5894
5954
  const thinkIndex = reply.indexOf("<think>");
5895
5955
  const endThinkIndex = reply.indexOf("</think>");
5896
- let thinkingText = "";
5897
5956
  let answerText = reply;
5898
5957
  if (thinkIndex !== -1 && endThinkIndex !== -1) {
5899
- thinkingText = reply.substring(thinkIndex + 7, endThinkIndex);
5958
+ const thinkingText = reply.substring(thinkIndex + 7, endThinkIndex);
5900
5959
  answerText = reply.substring(endThinkIndex + 8);
5901
5960
  yield { type: "thinking", text: thinkingText };
5902
5961
  }
5903
- const toolMatch = answerText.match(/TOOL_CALL:\s*(\w+)\s*\(([\s\S]*)\)/);
5904
- if (toolMatch) {
5905
- const toolName = toolMatch[1];
5906
- const toolArgsStr = toolMatch[2];
5907
- try {
5908
- const toolArgs = JSON.parse(toolArgsStr.trim());
5909
- toolCallDetected = { name: toolName, args: toolArgs };
5910
- } catch (e) {
5911
- console.error("[MultiAgentCoordinator] Failed to parse tool args:", toolArgsStr, e);
5912
- }
5913
- } else {
5962
+ toolCallDetected = parseToolCall(answerText);
5963
+ if (!toolCallDetected) {
5914
5964
  yield answerText;
5915
5965
  }
5916
5966
  } else {
@@ -5939,9 +5989,7 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5939
5989
  if (!inThinking && textBuffer.includes("TOOL_CALL:")) {
5940
5990
  const idx = textBuffer.indexOf("TOOL_CALL:");
5941
5991
  const precedingText = textBuffer.slice(0, idx);
5942
- if (precedingText.trim()) {
5943
- yield precedingText;
5944
- }
5992
+ if (precedingText.trim()) yield precedingText;
5945
5993
  textBuffer = textBuffer.slice(idx);
5946
5994
  }
5947
5995
  if (!inThinking && !textBuffer.includes("TOOL_CALL:") && textBuffer.length > 0) {
@@ -5960,17 +6008,8 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5960
6008
  }
5961
6009
  }
5962
6010
  if (textBuffer.trim()) {
5963
- const toolMatch = textBuffer.match(/TOOL_CALL:\s*(\w+)\s*\(([\s\S]*)\)/);
5964
- if (toolMatch) {
5965
- const toolName = toolMatch[1];
5966
- const toolArgsStr = toolMatch[2];
5967
- try {
5968
- const toolArgs = JSON.parse(toolArgsStr.trim());
5969
- toolCallDetected = { name: toolName, args: toolArgs };
5970
- } catch (e) {
5971
- console.error("[MultiAgentCoordinator] Failed to parse tool args:", toolArgsStr, e);
5972
- }
5973
- } else {
6011
+ toolCallDetected = parseToolCall(textBuffer);
6012
+ if (!toolCallDetected) {
5974
6013
  yield textBuffer;
5975
6014
  }
5976
6015
  }
@@ -5987,14 +6026,16 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5987
6026
  let toolResultText = "";
5988
6027
  try {
5989
6028
  if (toolName === "document_search") {
5990
- const searchRes = yield new __await(this.documentSearch(toolArgs.query || ""));
5991
- toolResultText = `document_search returned:
5992
- ${searchRes.reply}
5993
-
5994
- Sources count: ${searchRes.sources.length}`;
5995
- if (searchRes.sources && searchRes.sources.length > 0) {
5996
- allSources.push(...searchRes.sources);
5997
- yield { reply: "", sources: searchRes.sources };
6029
+ const query = String((_a2 = toolArgs.query) != null ? _a2 : "");
6030
+ const { sources } = yield new __await(this.retriever.retrieve(query));
6031
+ const snippets = sources.slice(0, 5).map((s, i) => {
6032
+ var _a3;
6033
+ return `[${i + 1}] ${String((_a3 = s.content) != null ? _a3 : "").slice(0, 400)}`;
6034
+ }).join("\n\n");
6035
+ toolResultText = `document_search returned ${sources.length} source(s):
6036
+ ${snippets}`;
6037
+ if (sources.length > 0) {
6038
+ yield { reply: "", sources };
5998
6039
  }
5999
6040
  } else {
6000
6041
  const mcpRes = yield new __await(this.mcpRegistry.callTool(toolName, toolArgs));
@@ -8892,8 +8933,30 @@ var Pipeline = class {
8892
8933
  getLLMProvider() {
8893
8934
  return this.initialised ? this.llmProvider : void 0;
8894
8935
  }
8936
+ /**
8937
+ * True when multi-agent / MCP mode is active:
8938
+ * architecture === 'multi-agent' OR mcpServers array is non-empty.
8939
+ * Uses MultiAgentCoordinator (custom supervisor loop).
8940
+ */
8941
+ get isMultiAgentMode() {
8942
+ var _a2, _b, _c;
8943
+ 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;
8944
+ }
8945
+ /**
8946
+ * True when the LangChain ReAct agent mode is selected:
8947
+ * architecture === 'agentic'.
8948
+ * Uses LangChainAgent (LangGraph-backed ReAct, optional peer dep).
8949
+ */
8950
+ get isLangChainAgentMode() {
8951
+ var _a2;
8952
+ return ((_a2 = this.config.rag) == null ? void 0 : _a2.architecture) === "agentic";
8953
+ }
8954
+ /** Union of both agentic execution paths — used to gate the normal RAG pipeline. */
8955
+ get isAgenticMode() {
8956
+ return this.isMultiAgentMode || this.isLangChainAgentMode;
8957
+ }
8895
8958
  async initialize() {
8896
- var _a2, _b, _c, _d;
8959
+ var _a2, _b, _c;
8897
8960
  if (this.initialised) return;
8898
8961
  const chartInstruction = `You are a helpful assistant. Use the provided context to answer questions accurately.
8899
8962
 
@@ -8936,19 +8999,22 @@ var Pipeline = class {
8936
8999
  this.entityExtractor = new EntityExtractor(this.llmProvider);
8937
9000
  }
8938
9001
  await this.vectorDB.initialize();
8939
- 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) {
9002
+ if (this.isMultiAgentMode) {
9003
+ console.log("[Pipeline] Initializing MultiAgentCoordinator (multi-agent / MCP mode).");
8940
9004
  this.mcpRegistry = new MCPRegistry(this.config.mcpServers || []);
8941
9005
  this.multiAgentCoordinator = new MultiAgentCoordinator({
8942
9006
  llmProvider: this.llmProvider,
8943
9007
  mcpRegistry: this.mcpRegistry,
8944
- documentSearch: async (query) => {
8945
- return this.runNormalQuery(query);
8946
- }
9008
+ // Pass Pipeline itself as the IRetriever — avoids closure over internal methods.
9009
+ retriever: this,
9010
+ maxIterations: (_a2 = this.config.rag) == null ? void 0 : _a2.agentMaxIterations
8947
9011
  });
9012
+ } else if (this.isLangChainAgentMode) {
9013
+ console.log("[Pipeline] Initializing LangChainAgent (agentic / ReAct mode).");
8948
9014
  this.agent = new LangChainAgent(this, this.config);
8949
9015
  await this.agent.initialize(this.llmProvider);
8950
9016
  }
8951
- if ((_d = (_c = this.config.rag) == null ? void 0 : _c.cag) == null ? void 0 : _d.enabled) {
9017
+ if ((_c = (_b = this.config.rag) == null ? void 0 : _b.cag) == null ? void 0 : _c.enabled) {
8952
9018
  await this.loadColdContext(this.config.projectId);
8953
9019
  }
8954
9020
  this.initialised = true;
@@ -9111,11 +9177,14 @@ ${m.content}`).join("\n\n---\n\n");
9111
9177
  return { reply, sources };
9112
9178
  }
9113
9179
  async ask(question, history = [], namespace) {
9114
- var _a2, _b;
9115
9180
  await this.initialize();
9116
- 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) {
9181
+ if (this.isMultiAgentMode && this.multiAgentCoordinator) {
9117
9182
  return await this.multiAgentCoordinator.run(question, history);
9118
9183
  }
9184
+ if (this.isLangChainAgentMode && this.agent) {
9185
+ const reply2 = await this.agent.run(question, history);
9186
+ return { reply: reply2, sources: [] };
9187
+ }
9119
9188
  const stream = this.askStream(question, history, namespace);
9120
9189
  let reply = "";
9121
9190
  let sources = [];
@@ -9148,9 +9217,8 @@ ${m.content}`).join("\n\n---\n\n");
9148
9217
  }
9149
9218
  askStream(_0) {
9150
9219
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9151
- var _a2, _b;
9152
9220
  yield new __await(this.initialize());
9153
- 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) {
9221
+ if (this.isMultiAgentMode && this.multiAgentCoordinator) {
9154
9222
  const stream2 = this.multiAgentCoordinator.runStream(question, history);
9155
9223
  try {
9156
9224
  for (var iter = __forAwait(stream2), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
@@ -9169,6 +9237,11 @@ ${m.content}`).join("\n\n---\n\n");
9169
9237
  }
9170
9238
  return;
9171
9239
  }
9240
+ if (this.isLangChainAgentMode && this.agent) {
9241
+ const reply = yield new __await(this.agent.run(question, history));
9242
+ yield reply;
9243
+ return;
9244
+ }
9172
9245
  const stream = this.askStreamInternal(question, history, namespace);
9173
9246
  try {
9174
9247
  for (var iter2 = __forAwait(stream), more2, temp2, error2; more2 = !(temp2 = yield new __await(iter2.next())).done; more2 = false) {
@@ -9199,7 +9272,7 @@ ${m.content}`).join("\n\n---\n\n");
9199
9272
  */
9200
9273
  askStreamInternal(_0) {
9201
9274
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9202
- 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;
9275
+ 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;
9203
9276
  yield new __await(this.initialize());
9204
9277
  const ns = formatNamespace(namespace != null ? namespace : this.config.projectId);
9205
9278
  const topK = (_b = (_a2 = this.config.rag) == null ? void 0 : _a2.topK) != null ? _b : 5;
@@ -9244,10 +9317,11 @@ ${m.content}`).join("\n\n---\n\n");
9244
9317
  const hasNumericPredicates = Array.isArray(filter.__numericPredicates) && filter.__numericPredicates.length > 0;
9245
9318
  const wantsExhaustiveList = true;
9246
9319
  const retrievalLimit = Math.max(topK * 50, 1e3);
9320
+ const retrieveStart = performance.now();
9247
9321
  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"));
9248
9322
  const retrieveEnd = performance.now();
9249
- const embedMs = retrieveEnd - embedStart;
9250
- const retrieveMs = retrieveEnd - embedStart;
9323
+ const embedMs = retrieveStart - embedStart;
9324
+ const retrieveMs = retrieveEnd - retrieveStart;
9251
9325
  const rerankStart = performance.now();
9252
9326
  const structuredSources = this.applyStructuredFilters(rawSources, filter).filter((s) => Boolean(s && typeof s === "object"));
9253
9327
  let fullSources = hasNumericPredicates ? structuredSources : structuredSources.filter((m) => {
@@ -9470,7 +9544,7 @@ ${context}`;
9470
9544
  const latency = {
9471
9545
  embedMs: Math.round(embedMs),
9472
9546
  retrieveMs: Math.round(retrieveMs),
9473
- rerankMs: arch !== "naive" && (arch === "retrieve-and-rerank" || ((_x = this.config.rag) == null ? void 0 : _x.useReranking)) ? Math.round(rerankMs) : void 0,
9547
+ rerankMs: useReranking ? Math.round(rerankMs) : void 0,
9474
9548
  generateMs: Math.round(generateMs),
9475
9549
  totalMs: Math.round(totalMs)
9476
9550
  };
@@ -9483,7 +9557,7 @@ ${context}`;
9483
9557
  totalTokens: promptTokens + completionTokens,
9484
9558
  estimatedCostUsd: estimateCostUsd(promptTokens, completionTokens, this.config.llm.model)
9485
9559
  };
9486
- const awaitHallucination = ((_y = this.config.llm.options) == null ? void 0 : _y.awaitHallucination) === true;
9560
+ const awaitHallucination = ((_x = this.config.llm.options) == null ? void 0 : _x.awaitHallucination) === true;
9487
9561
  const [uiTransformation, hallucinationResult] = yield new __await(Promise.all([
9488
9562
  uiTransformationPromise,
9489
9563
  awaitHallucination ? hallucinationScoringPromise : Promise.resolve(void 0)
@@ -9512,10 +9586,10 @@ ${context}`;
9512
9586
  hallucinationReason: hScore.reason
9513
9587
  } : {});
9514
9588
  const trace = buildTrace(hallucinationResult);
9515
- const isTelemetryActive = (_A = (_z = this.config.telemetry) == null ? void 0 : _z.enabled) != null ? _A : Boolean(this.config.licenseKey);
9589
+ const isTelemetryActive = (_z = (_y = this.config.telemetry) == null ? void 0 : _y.enabled) != null ? _z : Boolean(this.config.licenseKey);
9516
9590
  if (isTelemetryActive) {
9517
9591
  const defaultUrl = "https://www.retrivora.com/api/telemetry";
9518
- const telemetryUrl = ((_B = this.config.telemetry) == null ? void 0 : _B.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
9592
+ const telemetryUrl = ((_A = this.config.telemetry) == null ? void 0 : _A.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
9519
9593
  const absoluteUrl = telemetryUrl.startsWith("http") ? telemetryUrl : "https://www.retrivora.com" + (telemetryUrl.startsWith("/") ? telemetryUrl : "/" + telemetryUrl);
9520
9594
  (async () => {
9521
9595
  var _a3, _b2, _c2, _d2, _e2;
@@ -9689,7 +9763,7 @@ ${context}`;
9689
9763
  return Number.isFinite(numeric) ? numeric : null;
9690
9764
  }
9691
9765
  async retrieve(query, options) {
9692
- var _a2, _b, _c, _d, _e, _f, _g2;
9766
+ var _a2, _b, _c, _d, _e, _f;
9693
9767
  const ns = formatNamespace((_a2 = options.namespace) != null ? _a2 : this.config.projectId);
9694
9768
  const topK = (_b = options.topK) != null ? _b : 5;
9695
9769
  const cacheKey = `${ns}::${query}`;
@@ -9720,16 +9794,7 @@ ${context}`;
9720
9794
  await this.vectorDB.query(queryVector, retrievalLimit, ns, baseFilter),
9721
9795
  baseFilter
9722
9796
  ) : [];
9723
- const resolvedSources = [];
9724
- for (const source of sources) {
9725
- const parentId = (_g2 = source.metadata) == null ? void 0 : _g2.parent_id;
9726
- if (parentId) {
9727
- console.log(`[Pipeline] Multi-Vector: Found child chunk. Parent ID: ${parentId}`);
9728
- resolvedSources.push(source);
9729
- } else {
9730
- resolvedSources.push(source);
9731
- }
9732
- }
9797
+ const resolvedSources = sources;
9733
9798
  console.log("[Pipeline] retrieve() response:", {
9734
9799
  query,
9735
9800
  namespace: ns,