@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.
@@ -312,7 +312,7 @@ var init_package = __esm({
312
312
  "package.json"() {
313
313
  package_default = {
314
314
  name: "@retrivora-ai/rag-engine",
315
- version: "2.4.8",
315
+ version: "2.4.9",
316
316
  description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
317
317
  author: "Abhinav Alkuchi",
318
318
  license: "UNLICENSED",
@@ -1529,6 +1529,7 @@ var init_MongoDBProvider = __esm({
1529
1529
  MongoDBProvider = class extends BaseVectorProvider {
1530
1530
  constructor(config) {
1531
1531
  super(config);
1532
+ this.initialized = false;
1532
1533
  const opts = config.options;
1533
1534
  if (!opts.uri || !opts.database || !opts.collection) {
1534
1535
  throw new Error("[MongoDBProvider] options uri, database, and collection are required.");
@@ -1606,9 +1607,11 @@ var init_MongoDBProvider = __esm({
1606
1607
  };
1607
1608
  }
1608
1609
  async initialize() {
1610
+ if (this.initialized) return;
1609
1611
  await this.client.connect();
1610
1612
  this.db = this.client.db(this.dbName);
1611
1613
  this.collection = this.db.collection(this.collectionName);
1614
+ this.initialized = true;
1612
1615
  }
1613
1616
  async upsert(doc, namespace) {
1614
1617
  const document = __spreadValues({
@@ -1700,7 +1703,6 @@ var init_MongoDBProvider = __esm({
1700
1703
  const sanitized = super.sanitizeFilter(filter);
1701
1704
  const mongoFilter = {};
1702
1705
  for (const [key, value] of Object.entries(sanitized)) {
1703
- if (key.startsWith("__") || key === "keywords" || key === "queryText") continue;
1704
1706
  if (key === "namespace") {
1705
1707
  mongoFilter[key] = value;
1706
1708
  } else {
@@ -1719,6 +1721,7 @@ var init_MongoDBProvider = __esm({
1719
1721
  }
1720
1722
  async disconnect() {
1721
1723
  await this.client.close();
1724
+ this.initialized = false;
1722
1725
  }
1723
1726
  };
1724
1727
  }
@@ -1763,15 +1766,19 @@ var init_MilvusProvider = __esm({
1763
1766
  await this.http.post("/v1/vector/upsert", payload);
1764
1767
  }
1765
1768
  async batchUpsert(docs, namespace) {
1766
- const payload = {
1767
- collectionName: this.indexName,
1768
- data: docs.map((doc) => __spreadValues({
1769
- vector: doc.vector,
1770
- content: doc.content,
1771
- metadata: doc.metadata || {}
1772
- }, namespace ? { namespace } : {}))
1773
- };
1774
- await this.http.post("/v1/vector/upsert", payload);
1769
+ const batchSize = 100;
1770
+ for (let i = 0; i < docs.length; i += batchSize) {
1771
+ const batch = docs.slice(i, i + batchSize);
1772
+ const payload = {
1773
+ collectionName: this.indexName,
1774
+ data: batch.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);
1781
+ }
1775
1782
  }
1776
1783
  async query(vector, topK, namespace, _filter) {
1777
1784
  const sanitizedFilter = this.sanitizeFilter(_filter);
@@ -1782,7 +1789,8 @@ var init_MilvusProvider = __esm({
1782
1789
  for (const [key, value] of Object.entries(sanitizedFilter)) {
1783
1790
  if (key === "queryText" || key === "keywords") continue;
1784
1791
  if (typeof value === "string") {
1785
- filterParts.push(`metadata["${key}"] == "${value.replace(/"/g, '\\"')}"`);
1792
+ const escaped = value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
1793
+ filterParts.push(`metadata["${key}"] == "${escaped}"`);
1786
1794
  } else if (typeof value === "number") {
1787
1795
  filterParts.push(`metadata["${key}"] == ${value}`);
1788
1796
  }
@@ -1806,7 +1814,7 @@ var init_MilvusProvider = __esm({
1806
1814
  metadata: res["metadata"] || {}
1807
1815
  }));
1808
1816
  }
1809
- async delete(id, _namespace) {
1817
+ async delete(id, namespace) {
1810
1818
  await this.http.post("/v1/vector/delete", {
1811
1819
  collectionName: this.indexName,
1812
1820
  id
@@ -1878,7 +1886,6 @@ var init_QdrantProvider = __esm({
1878
1886
  return this.ensureIndex(fieldName, schemaType || "keyword");
1879
1887
  })
1880
1888
  );
1881
- } else {
1882
1889
  }
1883
1890
  this.schemaDiscovered = true;
1884
1891
  }
@@ -2124,7 +2131,7 @@ var init_ChromaDBProvider = __esm({
2124
2131
  documents: docs.map((d) => d.content),
2125
2132
  metadatas: docs.map((d) => __spreadValues(__spreadValues({}, d.metadata || {}), namespace ? { namespace } : {}))
2126
2133
  };
2127
- await this.http.post(`/api/v1/collections/${this.collectionId}/add`, payload);
2134
+ await this.http.post(`/api/v1/collections/${this.collectionId}/upsert`, payload);
2128
2135
  }
2129
2136
  async query(vector, topK, namespace, _filter) {
2130
2137
  const sanitizedFilter = this.sanitizeFilter(_filter);
@@ -2208,17 +2215,16 @@ var init_RedisProvider = __esm({
2208
2215
  await this.ping();
2209
2216
  }
2210
2217
  async upsert(doc, namespace) {
2211
- const key = namespace ? `${namespace}:${doc.id}` : String(doc.id);
2212
- await this.http.post("/", ["JSON.SET", key, "$", JSON.stringify({
2213
- vector: doc.vector,
2214
- content: doc.content,
2215
- metadata: doc.metadata || {}
2216
- })]);
2218
+ await this.batchUpsert([doc], namespace);
2217
2219
  }
2218
2220
  async batchUpsert(docs, namespace) {
2219
- for (const doc of docs) {
2220
- await this.upsert(doc, namespace);
2221
- }
2221
+ const payload = docs.map((doc) => __spreadValues({
2222
+ id: String(doc.id),
2223
+ vector: doc.vector,
2224
+ data: doc.content,
2225
+ metadata: doc.metadata || {}
2226
+ }, namespace ? { namespace } : {}));
2227
+ await this.http.post("/upsert", payload);
2222
2228
  }
2223
2229
  async query(vector, topK, namespace, _filter) {
2224
2230
  const payload = {
@@ -2236,19 +2242,22 @@ var init_RedisProvider = __esm({
2236
2242
  }));
2237
2243
  }
2238
2244
  async delete(id, namespace) {
2239
- const key = namespace ? `${namespace}:${id}` : id;
2240
- await this.http.post("/", ["DEL", key]);
2245
+ await this.http.post("/delete", __spreadValues({
2246
+ ids: [id]
2247
+ }, namespace ? { namespace } : {}));
2241
2248
  }
2242
2249
  async deleteNamespace(namespace) {
2243
- console.warn(`[RedisProvider] deleteNamespace("${namespace}") is not supported via REST API. Use Redis CLI: SCAN + DEL`);
2250
+ if (!namespace) {
2251
+ throw new Error("[RedisProvider] namespace is required for deleteNamespace");
2252
+ }
2253
+ await this.http.post("/reset", { namespace });
2244
2254
  }
2245
2255
  /**
2246
- * Check reachability via a PING command.
2247
- * Returns false on connection failure so health checks surface real problems.
2256
+ * Check reachability of the Upstash Vector API.
2248
2257
  */
2249
2258
  async ping() {
2250
2259
  try {
2251
- await this.http.post("/", ["PING"]);
2260
+ await this.http.get("/");
2252
2261
  return true;
2253
2262
  } catch (e) {
2254
2263
  return false;
@@ -2279,11 +2288,12 @@ var init_WeaviateProvider = __esm({
2279
2288
  if (!baseUrl) throw new Error("[WeaviateProvider] options.baseUrl is required");
2280
2289
  this.http = axios7.create({
2281
2290
  baseURL: baseUrl,
2282
- headers: __spreadValues({
2291
+ headers: __spreadValues(__spreadValues({
2283
2292
  "Content-Type": "application/json"
2284
2293
  }, opts.apiKey ? {
2285
- "X-OpenAI-Api-Key": opts.apiKey,
2286
2294
  "Authorization": `Bearer ${opts.apiKey}`
2295
+ } : {}), opts.openaiApiKey ? {
2296
+ "X-OpenAI-Api-Key": opts.openaiApiKey
2287
2297
  } : {})
2288
2298
  });
2289
2299
  }
@@ -2360,10 +2370,12 @@ var init_WeaviateProvider = __esm({
2360
2370
  await this.http.delete(`/v1/objects/${this.indexName}/${id}`);
2361
2371
  }
2362
2372
  async deleteNamespace(namespace) {
2363
- await this.http.post("/v1/batch/objects", {
2364
- match: {
2365
- class: this.indexName,
2366
- where: { path: ["namespace"], operator: "Equal", valueString: namespace }
2373
+ await this.http.delete("/v1/batch/objects", {
2374
+ data: {
2375
+ match: {
2376
+ class: this.indexName,
2377
+ where: { path: ["namespace"], operator: "Equal", valueString: namespace }
2378
+ }
2367
2379
  }
2368
2380
  });
2369
2381
  }
@@ -2594,6 +2606,9 @@ var init_SimpleGraphProvider = __esm({
2594
2606
  this.edges = [];
2595
2607
  }
2596
2608
  async initialize() {
2609
+ console.warn(
2610
+ "[SimpleGraphProvider] Warning: SimpleGraphProvider is an in-memory database meant for demonstration and local development. Graph data is NOT persisted across restarts."
2611
+ );
2597
2612
  }
2598
2613
  async addNodes(nodes) {
2599
2614
  for (const node of nodes) {
@@ -2615,15 +2630,21 @@ var init_SimpleGraphProvider = __esm({
2615
2630
  for (const edge of matchedEdges) {
2616
2631
  if (!matchedNodeIds.has(edge.source)) {
2617
2632
  const source = this.nodes.get(edge.source);
2618
- if (source) matchedNodes.push(source);
2633
+ if (source) {
2634
+ matchedNodes.push(source);
2635
+ matchedNodeIds.add(edge.source);
2636
+ }
2619
2637
  }
2620
2638
  if (!matchedNodeIds.has(edge.target)) {
2621
2639
  const target = this.nodes.get(edge.target);
2622
- if (target) matchedNodes.push(target);
2640
+ if (target) {
2641
+ matchedNodes.push(target);
2642
+ matchedNodeIds.add(edge.target);
2643
+ }
2623
2644
  }
2624
2645
  }
2625
2646
  return {
2626
- nodes: Array.from(new Set(matchedNodes)),
2647
+ nodes: matchedNodes,
2627
2648
  edges: matchedEdges
2628
2649
  };
2629
2650
  }
@@ -5744,27 +5765,42 @@ var MCPClient = class {
5744
5765
  var MCPRegistry = class {
5745
5766
  constructor(servers = []) {
5746
5767
  this.clients = [];
5768
+ /** Cache populated by getAllTools(); maps tool name → the client that owns it. */
5769
+ this.toolClientCache = null;
5747
5770
  this.clients = servers.map((cfg) => new MCPClient(cfg));
5748
5771
  }
5749
5772
  async getAllTools() {
5750
5773
  const list = [];
5774
+ const cache = /* @__PURE__ */ new Map();
5751
5775
  await Promise.all(
5752
5776
  this.clients.map(async (client) => {
5753
5777
  try {
5754
5778
  const tools = await client.listTools();
5755
- tools.forEach((t) => list.push({ serverName: client["config"].name, tool: t }));
5779
+ tools.forEach((t) => {
5780
+ list.push({ serverName: client["config"].name, tool: t });
5781
+ cache.set(t.name, client);
5782
+ });
5756
5783
  } catch (e) {
5757
5784
  console.warn(`[MCPRegistry] Failed to fetch tools from server "${client["config"].name}":`, e);
5758
5785
  }
5759
5786
  })
5760
5787
  );
5788
+ this.toolClientCache = cache;
5761
5789
  return list;
5762
5790
  }
5763
5791
  async callTool(toolName, args = {}) {
5792
+ var _a2;
5793
+ const cachedClient = (_a2 = this.toolClientCache) == null ? void 0 : _a2.get(toolName);
5794
+ if (cachedClient) {
5795
+ return cachedClient.callTool(toolName, args);
5796
+ }
5797
+ console.warn(`[MCPRegistry] Cache miss for tool "${toolName}", falling back to full scan.`);
5764
5798
  for (const client of this.clients) {
5765
5799
  try {
5766
5800
  const tools = await client.listTools();
5767
5801
  if (tools.some((t) => t.name === toolName)) {
5802
+ if (!this.toolClientCache) this.toolClientCache = /* @__PURE__ */ new Map();
5803
+ this.toolClientCache.set(toolName, client);
5768
5804
  return await client.callTool(toolName, args);
5769
5805
  }
5770
5806
  } catch (e) {
@@ -5772,18 +5808,44 @@ var MCPRegistry = class {
5772
5808
  }
5773
5809
  throw new Error(`[MCPRegistry] Tool "${toolName}" not found on any registered MCP server.`);
5774
5810
  }
5811
+ /** Invalidate the tool cache and disconnect all MCP clients. */
5775
5812
  disconnectAll() {
5813
+ this.toolClientCache = null;
5776
5814
  this.clients.forEach((c) => c.disconnect());
5777
5815
  }
5778
5816
  };
5779
5817
 
5780
5818
  // src/core/MultiAgentCoordinator.ts
5819
+ function parseToolCall(text) {
5820
+ const match = text.match(/TOOL_CALL:\s*([\w-]+)\s*\(\s*([\s\S]*?)\s*\)\s*[.,;]?\s*$/m);
5821
+ if (!match) return null;
5822
+ const name = match[1].trim();
5823
+ let argsRaw = match[2].trim();
5824
+ argsRaw = argsRaw.replace(/'/g, '"');
5825
+ if (argsRaw && !argsRaw.startsWith("{")) {
5826
+ argsRaw = `{${argsRaw}}`;
5827
+ }
5828
+ let args = {};
5829
+ if (argsRaw) {
5830
+ try {
5831
+ args = JSON.parse(argsRaw);
5832
+ } catch (e) {
5833
+ const pairs = [...argsRaw.matchAll(/"?([\w-]+)"?\s*:\s*"([^"]*)"/g)];
5834
+ if (pairs.length === 0) {
5835
+ console.warn("[parseToolCall] Could not parse tool args:", argsRaw);
5836
+ return null;
5837
+ }
5838
+ for (const [, k, v] of pairs) args[k] = v;
5839
+ }
5840
+ }
5841
+ return { name, args };
5842
+ }
5781
5843
  var MultiAgentCoordinator = class {
5782
5844
  constructor(options) {
5783
5845
  var _a2;
5784
5846
  this.llmProvider = options.llmProvider;
5785
5847
  this.mcpRegistry = options.mcpRegistry;
5786
- this.documentSearch = options.documentSearch;
5848
+ this.retriever = options.retriever;
5787
5849
  this.maxIterations = (_a2 = options.maxIterations) != null ? _a2 : 5;
5788
5850
  }
5789
5851
  /**
@@ -5817,18 +5879,17 @@ var MultiAgentCoordinator = class {
5817
5879
  throw error[0];
5818
5880
  }
5819
5881
  }
5820
- return {
5821
- reply: finalReply,
5822
- sources
5823
- };
5882
+ return { reply: finalReply, sources };
5824
5883
  }
5825
5884
  /**
5826
- * Run the multi-agent coordination loop as an async stream, yielding intermediate thought blocks and tool execution events.
5885
+ * Run the multi-agent coordination loop as an async stream,
5886
+ * yielding intermediate thought blocks and tool execution events.
5827
5887
  */
5828
5888
  runStream(_0) {
5829
5889
  return __asyncGenerator(this, arguments, function* (question, history = []) {
5890
+ var _a2;
5830
5891
  const mcpTools = yield new __await(this.mcpRegistry.getAllTools());
5831
- let systemPrompt = `You are a goal-driven supervisor agent coordinating a RAG search engine and external MCP servers to solve the user's task.
5892
+ const systemPrompt = `You are a goal-driven supervisor agent coordinating a RAG search engine and external MCP servers to solve the user's task.
5832
5893
  You must think step-by-step before acting. Write your internal thinking process inside a \`<think>...</think>\` block first, then act or answer.
5833
5894
 
5834
5895
  Available Tools:
@@ -5836,8 +5897,8 @@ Available Tools:
5836
5897
 
5837
5898
  ${mcpTools.length > 0 ? "MCP Server Tools:" : ""}
5838
5899
  ${mcpTools.map((mt) => {
5839
- var _a2;
5840
- return `- ${mt.tool.name}(${JSON.stringify(((_a2 = mt.tool.inputSchema) == null ? void 0 : _a2.properties) || {})}): ${mt.tool.description || "No description provided."}`;
5900
+ var _a3;
5901
+ return `- ${mt.tool.name}(${JSON.stringify(((_a3 = mt.tool.inputSchema) == null ? void 0 : _a3.properties) || {})}): ${mt.tool.description || "No description provided."}`;
5841
5902
  }).join("\n")}
5842
5903
 
5843
5904
  Tool Calling Protocol:
@@ -5845,7 +5906,7 @@ If you need to call a tool, write:
5845
5906
  TOOL_CALL: <toolName>({ "argName": "value" })
5846
5907
  And then STOP generating immediately.
5847
5908
 
5848
- Once you have gathered enough information to fully satisfy the user's query, write your final response directly after the closing \`</think>\` block.
5909
+ Once you have gathered enough information to fully satisfy the user query, write your final response directly after the closing \`</think>\` block.
5849
5910
  `;
5850
5911
  const supervisorHistory = [
5851
5912
  { role: "system", content: systemPrompt },
@@ -5853,7 +5914,6 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5853
5914
  { role: "user", content: question }
5854
5915
  ];
5855
5916
  let iteration = 0;
5856
- const allSources = [];
5857
5917
  while (iteration < this.maxIterations) {
5858
5918
  iteration++;
5859
5919
  const context = "";
@@ -5866,24 +5926,14 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5866
5926
  fullModelResponse = reply;
5867
5927
  const thinkIndex = reply.indexOf("<think>");
5868
5928
  const endThinkIndex = reply.indexOf("</think>");
5869
- let thinkingText = "";
5870
5929
  let answerText = reply;
5871
5930
  if (thinkIndex !== -1 && endThinkIndex !== -1) {
5872
- thinkingText = reply.substring(thinkIndex + 7, endThinkIndex);
5931
+ const thinkingText = reply.substring(thinkIndex + 7, endThinkIndex);
5873
5932
  answerText = reply.substring(endThinkIndex + 8);
5874
5933
  yield { type: "thinking", text: thinkingText };
5875
5934
  }
5876
- const toolMatch = answerText.match(/TOOL_CALL:\s*(\w+)\s*\(([\s\S]*)\)/);
5877
- if (toolMatch) {
5878
- const toolName = toolMatch[1];
5879
- const toolArgsStr = toolMatch[2];
5880
- try {
5881
- const toolArgs = JSON.parse(toolArgsStr.trim());
5882
- toolCallDetected = { name: toolName, args: toolArgs };
5883
- } catch (e) {
5884
- console.error("[MultiAgentCoordinator] Failed to parse tool args:", toolArgsStr, e);
5885
- }
5886
- } else {
5935
+ toolCallDetected = parseToolCall(answerText);
5936
+ if (!toolCallDetected) {
5887
5937
  yield answerText;
5888
5938
  }
5889
5939
  } else {
@@ -5912,9 +5962,7 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5912
5962
  if (!inThinking && textBuffer.includes("TOOL_CALL:")) {
5913
5963
  const idx = textBuffer.indexOf("TOOL_CALL:");
5914
5964
  const precedingText = textBuffer.slice(0, idx);
5915
- if (precedingText.trim()) {
5916
- yield precedingText;
5917
- }
5965
+ if (precedingText.trim()) yield precedingText;
5918
5966
  textBuffer = textBuffer.slice(idx);
5919
5967
  }
5920
5968
  if (!inThinking && !textBuffer.includes("TOOL_CALL:") && textBuffer.length > 0) {
@@ -5933,17 +5981,8 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5933
5981
  }
5934
5982
  }
5935
5983
  if (textBuffer.trim()) {
5936
- const toolMatch = textBuffer.match(/TOOL_CALL:\s*(\w+)\s*\(([\s\S]*)\)/);
5937
- if (toolMatch) {
5938
- const toolName = toolMatch[1];
5939
- const toolArgsStr = toolMatch[2];
5940
- try {
5941
- const toolArgs = JSON.parse(toolArgsStr.trim());
5942
- toolCallDetected = { name: toolName, args: toolArgs };
5943
- } catch (e) {
5944
- console.error("[MultiAgentCoordinator] Failed to parse tool args:", toolArgsStr, e);
5945
- }
5946
- } else {
5984
+ toolCallDetected = parseToolCall(textBuffer);
5985
+ if (!toolCallDetected) {
5947
5986
  yield textBuffer;
5948
5987
  }
5949
5988
  }
@@ -5960,14 +5999,16 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5960
5999
  let toolResultText = "";
5961
6000
  try {
5962
6001
  if (toolName === "document_search") {
5963
- const searchRes = yield new __await(this.documentSearch(toolArgs.query || ""));
5964
- toolResultText = `document_search returned:
5965
- ${searchRes.reply}
5966
-
5967
- Sources count: ${searchRes.sources.length}`;
5968
- if (searchRes.sources && searchRes.sources.length > 0) {
5969
- allSources.push(...searchRes.sources);
5970
- yield { reply: "", sources: searchRes.sources };
6002
+ const query = String((_a2 = toolArgs.query) != null ? _a2 : "");
6003
+ const { sources } = yield new __await(this.retriever.retrieve(query));
6004
+ const snippets = sources.slice(0, 5).map((s, i) => {
6005
+ var _a3;
6006
+ return `[${i + 1}] ${String((_a3 = s.content) != null ? _a3 : "").slice(0, 400)}`;
6007
+ }).join("\n\n");
6008
+ toolResultText = `document_search returned ${sources.length} source(s):
6009
+ ${snippets}`;
6010
+ if (sources.length > 0) {
6011
+ yield { reply: "", sources };
5971
6012
  }
5972
6013
  } else {
5973
6014
  const mcpRes = yield new __await(this.mcpRegistry.callTool(toolName, toolArgs));
@@ -8865,8 +8906,30 @@ var Pipeline = class {
8865
8906
  getLLMProvider() {
8866
8907
  return this.initialised ? this.llmProvider : void 0;
8867
8908
  }
8909
+ /**
8910
+ * True when multi-agent / MCP mode is active:
8911
+ * architecture === 'multi-agent' OR mcpServers array is non-empty.
8912
+ * Uses MultiAgentCoordinator (custom supervisor loop).
8913
+ */
8914
+ get isMultiAgentMode() {
8915
+ var _a2, _b, _c;
8916
+ 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;
8917
+ }
8918
+ /**
8919
+ * True when the LangChain ReAct agent mode is selected:
8920
+ * architecture === 'agentic'.
8921
+ * Uses LangChainAgent (LangGraph-backed ReAct, optional peer dep).
8922
+ */
8923
+ get isLangChainAgentMode() {
8924
+ var _a2;
8925
+ return ((_a2 = this.config.rag) == null ? void 0 : _a2.architecture) === "agentic";
8926
+ }
8927
+ /** Union of both agentic execution paths — used to gate the normal RAG pipeline. */
8928
+ get isAgenticMode() {
8929
+ return this.isMultiAgentMode || this.isLangChainAgentMode;
8930
+ }
8868
8931
  async initialize() {
8869
- var _a2, _b, _c, _d;
8932
+ var _a2, _b, _c;
8870
8933
  if (this.initialised) return;
8871
8934
  const chartInstruction = `You are a helpful assistant. Use the provided context to answer questions accurately.
8872
8935
 
@@ -8909,19 +8972,22 @@ var Pipeline = class {
8909
8972
  this.entityExtractor = new EntityExtractor(this.llmProvider);
8910
8973
  }
8911
8974
  await this.vectorDB.initialize();
8912
- 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) {
8975
+ if (this.isMultiAgentMode) {
8976
+ console.log("[Pipeline] Initializing MultiAgentCoordinator (multi-agent / MCP mode).");
8913
8977
  this.mcpRegistry = new MCPRegistry(this.config.mcpServers || []);
8914
8978
  this.multiAgentCoordinator = new MultiAgentCoordinator({
8915
8979
  llmProvider: this.llmProvider,
8916
8980
  mcpRegistry: this.mcpRegistry,
8917
- documentSearch: async (query) => {
8918
- return this.runNormalQuery(query);
8919
- }
8981
+ // Pass Pipeline itself as the IRetriever — avoids closure over internal methods.
8982
+ retriever: this,
8983
+ maxIterations: (_a2 = this.config.rag) == null ? void 0 : _a2.agentMaxIterations
8920
8984
  });
8985
+ } else if (this.isLangChainAgentMode) {
8986
+ console.log("[Pipeline] Initializing LangChainAgent (agentic / ReAct mode).");
8921
8987
  this.agent = new LangChainAgent(this, this.config);
8922
8988
  await this.agent.initialize(this.llmProvider);
8923
8989
  }
8924
- if ((_d = (_c = this.config.rag) == null ? void 0 : _c.cag) == null ? void 0 : _d.enabled) {
8990
+ if ((_c = (_b = this.config.rag) == null ? void 0 : _b.cag) == null ? void 0 : _c.enabled) {
8925
8991
  await this.loadColdContext(this.config.projectId);
8926
8992
  }
8927
8993
  this.initialised = true;
@@ -9084,11 +9150,14 @@ ${m.content}`).join("\n\n---\n\n");
9084
9150
  return { reply, sources };
9085
9151
  }
9086
9152
  async ask(question, history = [], namespace) {
9087
- var _a2, _b;
9088
9153
  await this.initialize();
9089
- 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) {
9154
+ if (this.isMultiAgentMode && this.multiAgentCoordinator) {
9090
9155
  return await this.multiAgentCoordinator.run(question, history);
9091
9156
  }
9157
+ if (this.isLangChainAgentMode && this.agent) {
9158
+ const reply2 = await this.agent.run(question, history);
9159
+ return { reply: reply2, sources: [] };
9160
+ }
9092
9161
  const stream = this.askStream(question, history, namespace);
9093
9162
  let reply = "";
9094
9163
  let sources = [];
@@ -9121,9 +9190,8 @@ ${m.content}`).join("\n\n---\n\n");
9121
9190
  }
9122
9191
  askStream(_0) {
9123
9192
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9124
- var _a2, _b;
9125
9193
  yield new __await(this.initialize());
9126
- 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) {
9194
+ if (this.isMultiAgentMode && this.multiAgentCoordinator) {
9127
9195
  const stream2 = this.multiAgentCoordinator.runStream(question, history);
9128
9196
  try {
9129
9197
  for (var iter = __forAwait(stream2), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
@@ -9142,6 +9210,11 @@ ${m.content}`).join("\n\n---\n\n");
9142
9210
  }
9143
9211
  return;
9144
9212
  }
9213
+ if (this.isLangChainAgentMode && this.agent) {
9214
+ const reply = yield new __await(this.agent.run(question, history));
9215
+ yield reply;
9216
+ return;
9217
+ }
9145
9218
  const stream = this.askStreamInternal(question, history, namespace);
9146
9219
  try {
9147
9220
  for (var iter2 = __forAwait(stream), more2, temp2, error2; more2 = !(temp2 = yield new __await(iter2.next())).done; more2 = false) {
@@ -9172,7 +9245,7 @@ ${m.content}`).join("\n\n---\n\n");
9172
9245
  */
9173
9246
  askStreamInternal(_0) {
9174
9247
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9175
- 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;
9248
+ 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;
9176
9249
  yield new __await(this.initialize());
9177
9250
  const ns = formatNamespace(namespace != null ? namespace : this.config.projectId);
9178
9251
  const topK = (_b = (_a2 = this.config.rag) == null ? void 0 : _a2.topK) != null ? _b : 5;
@@ -9217,10 +9290,11 @@ ${m.content}`).join("\n\n---\n\n");
9217
9290
  const hasNumericPredicates = Array.isArray(filter.__numericPredicates) && filter.__numericPredicates.length > 0;
9218
9291
  const wantsExhaustiveList = true;
9219
9292
  const retrievalLimit = Math.max(topK * 50, 1e3);
9293
+ const retrieveStart = performance.now();
9220
9294
  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"));
9221
9295
  const retrieveEnd = performance.now();
9222
- const embedMs = retrieveEnd - embedStart;
9223
- const retrieveMs = retrieveEnd - embedStart;
9296
+ const embedMs = retrieveStart - embedStart;
9297
+ const retrieveMs = retrieveEnd - retrieveStart;
9224
9298
  const rerankStart = performance.now();
9225
9299
  const structuredSources = this.applyStructuredFilters(rawSources, filter).filter((s) => Boolean(s && typeof s === "object"));
9226
9300
  let fullSources = hasNumericPredicates ? structuredSources : structuredSources.filter((m) => {
@@ -9443,7 +9517,7 @@ ${context}`;
9443
9517
  const latency = {
9444
9518
  embedMs: Math.round(embedMs),
9445
9519
  retrieveMs: Math.round(retrieveMs),
9446
- rerankMs: arch !== "naive" && (arch === "retrieve-and-rerank" || ((_x = this.config.rag) == null ? void 0 : _x.useReranking)) ? Math.round(rerankMs) : void 0,
9520
+ rerankMs: useReranking ? Math.round(rerankMs) : void 0,
9447
9521
  generateMs: Math.round(generateMs),
9448
9522
  totalMs: Math.round(totalMs)
9449
9523
  };
@@ -9456,7 +9530,7 @@ ${context}`;
9456
9530
  totalTokens: promptTokens + completionTokens,
9457
9531
  estimatedCostUsd: estimateCostUsd(promptTokens, completionTokens, this.config.llm.model)
9458
9532
  };
9459
- const awaitHallucination = ((_y = this.config.llm.options) == null ? void 0 : _y.awaitHallucination) === true;
9533
+ const awaitHallucination = ((_x = this.config.llm.options) == null ? void 0 : _x.awaitHallucination) === true;
9460
9534
  const [uiTransformation, hallucinationResult] = yield new __await(Promise.all([
9461
9535
  uiTransformationPromise,
9462
9536
  awaitHallucination ? hallucinationScoringPromise : Promise.resolve(void 0)
@@ -9485,10 +9559,10 @@ ${context}`;
9485
9559
  hallucinationReason: hScore.reason
9486
9560
  } : {});
9487
9561
  const trace = buildTrace(hallucinationResult);
9488
- const isTelemetryActive = (_A = (_z = this.config.telemetry) == null ? void 0 : _z.enabled) != null ? _A : Boolean(this.config.licenseKey);
9562
+ const isTelemetryActive = (_z = (_y = this.config.telemetry) == null ? void 0 : _y.enabled) != null ? _z : Boolean(this.config.licenseKey);
9489
9563
  if (isTelemetryActive) {
9490
9564
  const defaultUrl = "https://www.retrivora.com/api/telemetry";
9491
- const telemetryUrl = ((_B = this.config.telemetry) == null ? void 0 : _B.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
9565
+ const telemetryUrl = ((_A = this.config.telemetry) == null ? void 0 : _A.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
9492
9566
  const absoluteUrl = telemetryUrl.startsWith("http") ? telemetryUrl : "https://www.retrivora.com" + (telemetryUrl.startsWith("/") ? telemetryUrl : "/" + telemetryUrl);
9493
9567
  (async () => {
9494
9568
  var _a3, _b2, _c2, _d2, _e2;
@@ -9662,7 +9736,7 @@ ${context}`;
9662
9736
  return Number.isFinite(numeric) ? numeric : null;
9663
9737
  }
9664
9738
  async retrieve(query, options) {
9665
- var _a2, _b, _c, _d, _e, _f, _g2;
9739
+ var _a2, _b, _c, _d, _e, _f;
9666
9740
  const ns = formatNamespace((_a2 = options.namespace) != null ? _a2 : this.config.projectId);
9667
9741
  const topK = (_b = options.topK) != null ? _b : 5;
9668
9742
  const cacheKey = `${ns}::${query}`;
@@ -9693,16 +9767,7 @@ ${context}`;
9693
9767
  await this.vectorDB.query(queryVector, retrievalLimit, ns, baseFilter),
9694
9768
  baseFilter
9695
9769
  ) : [];
9696
- const resolvedSources = [];
9697
- for (const source of sources) {
9698
- const parentId = (_g2 = source.metadata) == null ? void 0 : _g2.parent_id;
9699
- if (parentId) {
9700
- console.log(`[Pipeline] Multi-Vector: Found child chunk. Parent ID: ${parentId}`);
9701
- resolvedSources.push(source);
9702
- } else {
9703
- resolvedSources.push(source);
9704
- }
9705
- }
9770
+ const resolvedSources = sources;
9706
9771
  console.log("[Pipeline] retrieve() response:", {
9707
9772
  query,
9708
9773
  namespace: ns,
@@ -1,5 +1,5 @@
1
1
  import { NextRequest } from 'next/server';
2
- import { p as RagConfig, I as ILLMProvider, C as ChatMessage, e as ChatResponse, h as IngestDocument } from './ILLMProvider-BYQd0Nys.js';
2
+ import { p as RagConfig, I as ILLMProvider, C as ChatMessage, e as ChatResponse, h as IngestDocument } from './ILLMProvider-BynaFBpy.js';
3
3
 
4
4
  interface ValidationError {
5
5
  field: string;
@@ -1,5 +1,5 @@
1
1
  import { NextRequest } from 'next/server';
2
- import { p as RagConfig, I as ILLMProvider, C as ChatMessage, e as ChatResponse, h as IngestDocument } from './ILLMProvider-BYQd0Nys.mjs';
2
+ import { p as RagConfig, I as ILLMProvider, C as ChatMessage, e as ChatResponse, h as IngestDocument } from './ILLMProvider-BynaFBpy.mjs';
3
3
 
4
4
  interface ValidationError {
5
5
  field: string;
package/dist/index.d.mts CHANGED
@@ -1,8 +1,8 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
- import { C as ChatWidgetProps, a as ChatWindowProps, D as DocumentUploadProps, M as MessageBubbleProps, S as SourceCardProps, b as ConfigProviderProps, c as ClientConfig, P as ProductCardProps, d as ProductCarouselProps, e as ChatViewportSize, R as RemoteConfig } from './BatchProcessor-BZhwvT6o.mjs';
3
- export { A as AuthenticationException, B as BatchOptions, f as BatchProcessor, g as BatchResult, h as ChartType, i as Chunk, j as ChunkOptions, k as CircuitBreaker, l as CircuitBreakerOptions, m as ConfigFetcher, n as ConfigurationException, o as DecisionContext, E as EmbeddingFailedException, I as IRenderRule, p as IRendererStrategy, q as IntentCategory, L as LicenseTierConfig, r as LicenseValidationError, s as LicenseValidationRequest, t as LicenseValidationResponse, u as LicenseValidator, v as ProviderCredentials, w as ProviderNotFoundException, x as RateLimitException, y as RemoteEmbeddingConfig, z as RemoteLLMConfig, F as RemoteVectorConfig, G as RenderDecision, H as RenderType, J as RetrievalException, K as Retrivora, N as RetrivoraError, O as RetrivoraErrorCode, Q as SDKVersionUnsupportedError, T as SDK_VERSION, U as isTransientError } from './BatchProcessor-BZhwvT6o.mjs';
4
- import { O as ObservabilityTrace, P as Product, U as UITransformationResponse, a as UseRagChatOptions, b as UseRagChatReturn, c as ProjectChatSettings } from './ILLMProvider-BYQd0Nys.mjs';
5
- export { A as AuthMode, C as ChatMessage, d as ChatOptions, e as ChatResponse, E as EmbedOptions, f as EmbeddingConfig, g as EmbeddingProvider, I as ILLMProvider, h as IngestDocument, L as LLMConfig, i as LLMProvider, j as LatencyBreakdown, k as ProjectAiSettings, l as ProjectAuthSettings, m as ProjectBrandingSettings, n as ProjectTelemetrySettings, o as ProjectWidgetSettings, R as RAGConfig, p as RagConfig, q as RagMessage, r as RetrievalConfig, s as RetrievedChunk, T as ThemeMode, t as TokenUsage, u as UIConfig, v as UniversalRagConfig, w as UpsertDocument, V as VectorDBConfig, x as VectorDBProvider, y as VectorMatch, W as WidgetPosition, z as WorkflowConfig, B as createDefaultProjectSettings, D as normalizeProjectSettings, F as projectSettingsToUiConfig } from './ILLMProvider-BYQd0Nys.mjs';
2
+ import { C as ChatWidgetProps, a as ChatWindowProps, D as DocumentUploadProps, M as MessageBubbleProps, S as SourceCardProps, b as ConfigProviderProps, c as ClientConfig, P as ProductCardProps, d as ProductCarouselProps, e as ChatViewportSize, R as RemoteConfig } from './BatchProcessor-OVOIlNfK.mjs';
3
+ export { A as AuthenticationException, B as BatchOptions, f as BatchProcessor, g as BatchResult, h as ChartType, i as Chunk, j as ChunkOptions, k as CircuitBreaker, l as CircuitBreakerOptions, m as ConfigFetcher, n as ConfigurationException, o as DecisionContext, E as EmbeddingFailedException, I as IRenderRule, p as IRendererStrategy, q as IntentCategory, L as LicenseTierConfig, r as LicenseValidationError, s as LicenseValidationRequest, t as LicenseValidationResponse, u as LicenseValidator, v as ProviderCredentials, w as ProviderNotFoundException, x as RateLimitException, y as RemoteEmbeddingConfig, z as RemoteLLMConfig, F as RemoteVectorConfig, G as RenderDecision, H as RenderType, J as RetrievalException, K as Retrivora, N as RetrivoraError, O as RetrivoraErrorCode, Q as SDKVersionUnsupportedError, T as SDK_VERSION, U as isTransientError } from './BatchProcessor-OVOIlNfK.mjs';
4
+ import { O as ObservabilityTrace, P as Product, U as UITransformationResponse, a as UseRagChatOptions, b as UseRagChatReturn, c as ProjectChatSettings } from './ILLMProvider-BynaFBpy.mjs';
5
+ export { A as AuthMode, C as ChatMessage, d as ChatOptions, e as ChatResponse, E as EmbedOptions, f as EmbeddingConfig, g as EmbeddingProvider, I as ILLMProvider, h as IngestDocument, L as LLMConfig, i as LLMProvider, j as LatencyBreakdown, k as ProjectAiSettings, l as ProjectAuthSettings, m as ProjectBrandingSettings, n as ProjectTelemetrySettings, o as ProjectWidgetSettings, R as RAGConfig, p as RagConfig, q as RagMessage, r as RetrievalConfig, s as RetrievedChunk, T as ThemeMode, t as TokenUsage, u as UIConfig, v as UniversalRagConfig, w as UpsertDocument, V as VectorDBConfig, x as VectorDBProvider, y as VectorMatch, W as WidgetPosition, z as WorkflowConfig, B as createDefaultProjectSettings, D as normalizeProjectSettings, F as projectSettingsToUiConfig } from './ILLMProvider-BynaFBpy.mjs';
6
6
  import React from 'react';
7
7
 
8
8
  /**