@retrivora-ai/rag-engine 2.4.7 → 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.7",
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",
@@ -519,10 +519,10 @@ var init_ConfigFetcher = __esm({
519
519
  const url = `${baseUrl}/api/v1/config?projectId=${encodeURIComponent(projectId)}`;
520
520
  const res = await fetch(url, {
521
521
  method: "GET",
522
- headers: __spreadValues({
522
+ headers: __spreadValues(__spreadValues({
523
523
  "Content-Type": "application/json",
524
524
  "x-sdk-version": SDK_VERSION
525
- }, licenseKey ? { "x-license-key": licenseKey } : {})
525
+ }, licenseKey ? { "x-license-key": licenseKey } : {}), projectId ? { "x-project-id": projectId } : {})
526
526
  });
527
527
  if (!res.ok) {
528
528
  console.warn(`[ConfigFetcher] Error text: ${await res.text()}`);
@@ -574,9 +574,9 @@ var init_ConfigFetcher = __esm({
574
574
  const credUrl = `${baseUrl}/api/v1/credentials`;
575
575
  const credRes = await fetch(credUrl, {
576
576
  method: "POST",
577
- headers: {
577
+ headers: __spreadValues(__spreadValues({
578
578
  "Content-Type": "application/json"
579
- },
579
+ }, licenseKey ? { "x-license-key": licenseKey } : {}), fetchedProjectId ? { "x-project-id": fetchedProjectId } : {}),
580
580
  body: JSON.stringify({ licenseKey, projectId: fetchedProjectId })
581
581
  });
582
582
  if (credRes.ok) {
@@ -762,7 +762,10 @@ var init_PineconeProvider = __esm({
762
762
  if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
763
763
  const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
764
764
  method: "POST",
765
- headers: { "Content-Type": "application/json", "x-license-key": this.licenseKey },
765
+ headers: __spreadValues({
766
+ "Content-Type": "application/json",
767
+ "x-license-key": this.licenseKey
768
+ }, this.projectId ? { "x-project-id": this.projectId } : {}),
766
769
  body: JSON.stringify({
767
770
  projectId: this.projectId,
768
771
  indexName: this.indexName,
@@ -797,7 +800,10 @@ var init_PineconeProvider = __esm({
797
800
  });
798
801
  const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
799
802
  method: "POST",
800
- headers: { "Content-Type": "application/json", "x-license-key": this.licenseKey },
803
+ headers: __spreadValues({
804
+ "Content-Type": "application/json",
805
+ "x-license-key": this.licenseKey
806
+ }, this.projectId ? { "x-project-id": this.projectId } : {}),
801
807
  body: JSON.stringify({
802
808
  projectId: this.projectId,
803
809
  indexName: this.indexName,
@@ -829,7 +835,10 @@ var init_PineconeProvider = __esm({
829
835
  if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
830
836
  const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/query`, {
831
837
  method: "POST",
832
- headers: { "Content-Type": "application/json", "x-license-key": this.licenseKey },
838
+ headers: __spreadValues({
839
+ "Content-Type": "application/json",
840
+ "x-license-key": this.licenseKey
841
+ }, this.projectId ? { "x-project-id": this.projectId } : {}),
833
842
  body: JSON.stringify({
834
843
  projectId: this.projectId,
835
844
  indexName: this.indexName,
@@ -1520,6 +1529,7 @@ var init_MongoDBProvider = __esm({
1520
1529
  MongoDBProvider = class extends BaseVectorProvider {
1521
1530
  constructor(config) {
1522
1531
  super(config);
1532
+ this.initialized = false;
1523
1533
  const opts = config.options;
1524
1534
  if (!opts.uri || !opts.database || !opts.collection) {
1525
1535
  throw new Error("[MongoDBProvider] options uri, database, and collection are required.");
@@ -1597,9 +1607,11 @@ var init_MongoDBProvider = __esm({
1597
1607
  };
1598
1608
  }
1599
1609
  async initialize() {
1610
+ if (this.initialized) return;
1600
1611
  await this.client.connect();
1601
1612
  this.db = this.client.db(this.dbName);
1602
1613
  this.collection = this.db.collection(this.collectionName);
1614
+ this.initialized = true;
1603
1615
  }
1604
1616
  async upsert(doc, namespace) {
1605
1617
  const document = __spreadValues({
@@ -1691,7 +1703,6 @@ var init_MongoDBProvider = __esm({
1691
1703
  const sanitized = super.sanitizeFilter(filter);
1692
1704
  const mongoFilter = {};
1693
1705
  for (const [key, value] of Object.entries(sanitized)) {
1694
- if (key.startsWith("__") || key === "keywords" || key === "queryText") continue;
1695
1706
  if (key === "namespace") {
1696
1707
  mongoFilter[key] = value;
1697
1708
  } else {
@@ -1710,6 +1721,7 @@ var init_MongoDBProvider = __esm({
1710
1721
  }
1711
1722
  async disconnect() {
1712
1723
  await this.client.close();
1724
+ this.initialized = false;
1713
1725
  }
1714
1726
  };
1715
1727
  }
@@ -1754,15 +1766,19 @@ var init_MilvusProvider = __esm({
1754
1766
  await this.http.post("/v1/vector/upsert", payload);
1755
1767
  }
1756
1768
  async batchUpsert(docs, namespace) {
1757
- const payload = {
1758
- collectionName: this.indexName,
1759
- data: docs.map((doc) => __spreadValues({
1760
- vector: doc.vector,
1761
- content: doc.content,
1762
- metadata: doc.metadata || {}
1763
- }, namespace ? { namespace } : {}))
1764
- };
1765
- 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
+ }
1766
1782
  }
1767
1783
  async query(vector, topK, namespace, _filter) {
1768
1784
  const sanitizedFilter = this.sanitizeFilter(_filter);
@@ -1773,7 +1789,8 @@ var init_MilvusProvider = __esm({
1773
1789
  for (const [key, value] of Object.entries(sanitizedFilter)) {
1774
1790
  if (key === "queryText" || key === "keywords") continue;
1775
1791
  if (typeof value === "string") {
1776
- filterParts.push(`metadata["${key}"] == "${value.replace(/"/g, '\\"')}"`);
1792
+ const escaped = value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
1793
+ filterParts.push(`metadata["${key}"] == "${escaped}"`);
1777
1794
  } else if (typeof value === "number") {
1778
1795
  filterParts.push(`metadata["${key}"] == ${value}`);
1779
1796
  }
@@ -1797,7 +1814,7 @@ var init_MilvusProvider = __esm({
1797
1814
  metadata: res["metadata"] || {}
1798
1815
  }));
1799
1816
  }
1800
- async delete(id, _namespace) {
1817
+ async delete(id, namespace) {
1801
1818
  await this.http.post("/v1/vector/delete", {
1802
1819
  collectionName: this.indexName,
1803
1820
  id
@@ -1869,7 +1886,6 @@ var init_QdrantProvider = __esm({
1869
1886
  return this.ensureIndex(fieldName, schemaType || "keyword");
1870
1887
  })
1871
1888
  );
1872
- } else {
1873
1889
  }
1874
1890
  this.schemaDiscovered = true;
1875
1891
  }
@@ -2115,7 +2131,7 @@ var init_ChromaDBProvider = __esm({
2115
2131
  documents: docs.map((d) => d.content),
2116
2132
  metadatas: docs.map((d) => __spreadValues(__spreadValues({}, d.metadata || {}), namespace ? { namespace } : {}))
2117
2133
  };
2118
- await this.http.post(`/api/v1/collections/${this.collectionId}/add`, payload);
2134
+ await this.http.post(`/api/v1/collections/${this.collectionId}/upsert`, payload);
2119
2135
  }
2120
2136
  async query(vector, topK, namespace, _filter) {
2121
2137
  const sanitizedFilter = this.sanitizeFilter(_filter);
@@ -2199,17 +2215,16 @@ var init_RedisProvider = __esm({
2199
2215
  await this.ping();
2200
2216
  }
2201
2217
  async upsert(doc, namespace) {
2202
- const key = namespace ? `${namespace}:${doc.id}` : String(doc.id);
2203
- await this.http.post("/", ["JSON.SET", key, "$", JSON.stringify({
2204
- vector: doc.vector,
2205
- content: doc.content,
2206
- metadata: doc.metadata || {}
2207
- })]);
2218
+ await this.batchUpsert([doc], namespace);
2208
2219
  }
2209
2220
  async batchUpsert(docs, namespace) {
2210
- for (const doc of docs) {
2211
- await this.upsert(doc, namespace);
2212
- }
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);
2213
2228
  }
2214
2229
  async query(vector, topK, namespace, _filter) {
2215
2230
  const payload = {
@@ -2227,19 +2242,22 @@ var init_RedisProvider = __esm({
2227
2242
  }));
2228
2243
  }
2229
2244
  async delete(id, namespace) {
2230
- const key = namespace ? `${namespace}:${id}` : id;
2231
- await this.http.post("/", ["DEL", key]);
2245
+ await this.http.post("/delete", __spreadValues({
2246
+ ids: [id]
2247
+ }, namespace ? { namespace } : {}));
2232
2248
  }
2233
2249
  async deleteNamespace(namespace) {
2234
- 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 });
2235
2254
  }
2236
2255
  /**
2237
- * Check reachability via a PING command.
2238
- * Returns false on connection failure so health checks surface real problems.
2256
+ * Check reachability of the Upstash Vector API.
2239
2257
  */
2240
2258
  async ping() {
2241
2259
  try {
2242
- await this.http.post("/", ["PING"]);
2260
+ await this.http.get("/");
2243
2261
  return true;
2244
2262
  } catch (e) {
2245
2263
  return false;
@@ -2270,11 +2288,12 @@ var init_WeaviateProvider = __esm({
2270
2288
  if (!baseUrl) throw new Error("[WeaviateProvider] options.baseUrl is required");
2271
2289
  this.http = axios7.create({
2272
2290
  baseURL: baseUrl,
2273
- headers: __spreadValues({
2291
+ headers: __spreadValues(__spreadValues({
2274
2292
  "Content-Type": "application/json"
2275
2293
  }, opts.apiKey ? {
2276
- "X-OpenAI-Api-Key": opts.apiKey,
2277
2294
  "Authorization": `Bearer ${opts.apiKey}`
2295
+ } : {}), opts.openaiApiKey ? {
2296
+ "X-OpenAI-Api-Key": opts.openaiApiKey
2278
2297
  } : {})
2279
2298
  });
2280
2299
  }
@@ -2351,10 +2370,12 @@ var init_WeaviateProvider = __esm({
2351
2370
  await this.http.delete(`/v1/objects/${this.indexName}/${id}`);
2352
2371
  }
2353
2372
  async deleteNamespace(namespace) {
2354
- await this.http.post("/v1/batch/objects", {
2355
- match: {
2356
- class: this.indexName,
2357
- 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
+ }
2358
2379
  }
2359
2380
  });
2360
2381
  }
@@ -2585,6 +2606,9 @@ var init_SimpleGraphProvider = __esm({
2585
2606
  this.edges = [];
2586
2607
  }
2587
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
+ );
2588
2612
  }
2589
2613
  async addNodes(nodes) {
2590
2614
  for (const node of nodes) {
@@ -2606,15 +2630,21 @@ var init_SimpleGraphProvider = __esm({
2606
2630
  for (const edge of matchedEdges) {
2607
2631
  if (!matchedNodeIds.has(edge.source)) {
2608
2632
  const source = this.nodes.get(edge.source);
2609
- if (source) matchedNodes.push(source);
2633
+ if (source) {
2634
+ matchedNodes.push(source);
2635
+ matchedNodeIds.add(edge.source);
2636
+ }
2610
2637
  }
2611
2638
  if (!matchedNodeIds.has(edge.target)) {
2612
2639
  const target = this.nodes.get(edge.target);
2613
- if (target) matchedNodes.push(target);
2640
+ if (target) {
2641
+ matchedNodes.push(target);
2642
+ matchedNodeIds.add(edge.target);
2643
+ }
2614
2644
  }
2615
2645
  }
2616
2646
  return {
2617
- nodes: Array.from(new Set(matchedNodes)),
2647
+ nodes: matchedNodes,
2618
2648
  edges: matchedEdges
2619
2649
  };
2620
2650
  }
@@ -5735,27 +5765,42 @@ var MCPClient = class {
5735
5765
  var MCPRegistry = class {
5736
5766
  constructor(servers = []) {
5737
5767
  this.clients = [];
5768
+ /** Cache populated by getAllTools(); maps tool name → the client that owns it. */
5769
+ this.toolClientCache = null;
5738
5770
  this.clients = servers.map((cfg) => new MCPClient(cfg));
5739
5771
  }
5740
5772
  async getAllTools() {
5741
5773
  const list = [];
5774
+ const cache = /* @__PURE__ */ new Map();
5742
5775
  await Promise.all(
5743
5776
  this.clients.map(async (client) => {
5744
5777
  try {
5745
5778
  const tools = await client.listTools();
5746
- 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
+ });
5747
5783
  } catch (e) {
5748
5784
  console.warn(`[MCPRegistry] Failed to fetch tools from server "${client["config"].name}":`, e);
5749
5785
  }
5750
5786
  })
5751
5787
  );
5788
+ this.toolClientCache = cache;
5752
5789
  return list;
5753
5790
  }
5754
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.`);
5755
5798
  for (const client of this.clients) {
5756
5799
  try {
5757
5800
  const tools = await client.listTools();
5758
5801
  if (tools.some((t) => t.name === toolName)) {
5802
+ if (!this.toolClientCache) this.toolClientCache = /* @__PURE__ */ new Map();
5803
+ this.toolClientCache.set(toolName, client);
5759
5804
  return await client.callTool(toolName, args);
5760
5805
  }
5761
5806
  } catch (e) {
@@ -5763,18 +5808,44 @@ var MCPRegistry = class {
5763
5808
  }
5764
5809
  throw new Error(`[MCPRegistry] Tool "${toolName}" not found on any registered MCP server.`);
5765
5810
  }
5811
+ /** Invalidate the tool cache and disconnect all MCP clients. */
5766
5812
  disconnectAll() {
5813
+ this.toolClientCache = null;
5767
5814
  this.clients.forEach((c) => c.disconnect());
5768
5815
  }
5769
5816
  };
5770
5817
 
5771
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
+ }
5772
5843
  var MultiAgentCoordinator = class {
5773
5844
  constructor(options) {
5774
5845
  var _a2;
5775
5846
  this.llmProvider = options.llmProvider;
5776
5847
  this.mcpRegistry = options.mcpRegistry;
5777
- this.documentSearch = options.documentSearch;
5848
+ this.retriever = options.retriever;
5778
5849
  this.maxIterations = (_a2 = options.maxIterations) != null ? _a2 : 5;
5779
5850
  }
5780
5851
  /**
@@ -5808,18 +5879,17 @@ var MultiAgentCoordinator = class {
5808
5879
  throw error[0];
5809
5880
  }
5810
5881
  }
5811
- return {
5812
- reply: finalReply,
5813
- sources
5814
- };
5882
+ return { reply: finalReply, sources };
5815
5883
  }
5816
5884
  /**
5817
- * 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.
5818
5887
  */
5819
5888
  runStream(_0) {
5820
5889
  return __asyncGenerator(this, arguments, function* (question, history = []) {
5890
+ var _a2;
5821
5891
  const mcpTools = yield new __await(this.mcpRegistry.getAllTools());
5822
- 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.
5823
5893
  You must think step-by-step before acting. Write your internal thinking process inside a \`<think>...</think>\` block first, then act or answer.
5824
5894
 
5825
5895
  Available Tools:
@@ -5827,8 +5897,8 @@ Available Tools:
5827
5897
 
5828
5898
  ${mcpTools.length > 0 ? "MCP Server Tools:" : ""}
5829
5899
  ${mcpTools.map((mt) => {
5830
- var _a2;
5831
- 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."}`;
5832
5902
  }).join("\n")}
5833
5903
 
5834
5904
  Tool Calling Protocol:
@@ -5836,7 +5906,7 @@ If you need to call a tool, write:
5836
5906
  TOOL_CALL: <toolName>({ "argName": "value" })
5837
5907
  And then STOP generating immediately.
5838
5908
 
5839
- 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.
5840
5910
  `;
5841
5911
  const supervisorHistory = [
5842
5912
  { role: "system", content: systemPrompt },
@@ -5844,7 +5914,6 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5844
5914
  { role: "user", content: question }
5845
5915
  ];
5846
5916
  let iteration = 0;
5847
- const allSources = [];
5848
5917
  while (iteration < this.maxIterations) {
5849
5918
  iteration++;
5850
5919
  const context = "";
@@ -5857,24 +5926,14 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5857
5926
  fullModelResponse = reply;
5858
5927
  const thinkIndex = reply.indexOf("<think>");
5859
5928
  const endThinkIndex = reply.indexOf("</think>");
5860
- let thinkingText = "";
5861
5929
  let answerText = reply;
5862
5930
  if (thinkIndex !== -1 && endThinkIndex !== -1) {
5863
- thinkingText = reply.substring(thinkIndex + 7, endThinkIndex);
5931
+ const thinkingText = reply.substring(thinkIndex + 7, endThinkIndex);
5864
5932
  answerText = reply.substring(endThinkIndex + 8);
5865
5933
  yield { type: "thinking", text: thinkingText };
5866
5934
  }
5867
- const toolMatch = answerText.match(/TOOL_CALL:\s*(\w+)\s*\(([\s\S]*)\)/);
5868
- if (toolMatch) {
5869
- const toolName = toolMatch[1];
5870
- const toolArgsStr = toolMatch[2];
5871
- try {
5872
- const toolArgs = JSON.parse(toolArgsStr.trim());
5873
- toolCallDetected = { name: toolName, args: toolArgs };
5874
- } catch (e) {
5875
- console.error("[MultiAgentCoordinator] Failed to parse tool args:", toolArgsStr, e);
5876
- }
5877
- } else {
5935
+ toolCallDetected = parseToolCall(answerText);
5936
+ if (!toolCallDetected) {
5878
5937
  yield answerText;
5879
5938
  }
5880
5939
  } else {
@@ -5903,9 +5962,7 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5903
5962
  if (!inThinking && textBuffer.includes("TOOL_CALL:")) {
5904
5963
  const idx = textBuffer.indexOf("TOOL_CALL:");
5905
5964
  const precedingText = textBuffer.slice(0, idx);
5906
- if (precedingText.trim()) {
5907
- yield precedingText;
5908
- }
5965
+ if (precedingText.trim()) yield precedingText;
5909
5966
  textBuffer = textBuffer.slice(idx);
5910
5967
  }
5911
5968
  if (!inThinking && !textBuffer.includes("TOOL_CALL:") && textBuffer.length > 0) {
@@ -5924,17 +5981,8 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5924
5981
  }
5925
5982
  }
5926
5983
  if (textBuffer.trim()) {
5927
- const toolMatch = textBuffer.match(/TOOL_CALL:\s*(\w+)\s*\(([\s\S]*)\)/);
5928
- if (toolMatch) {
5929
- const toolName = toolMatch[1];
5930
- const toolArgsStr = toolMatch[2];
5931
- try {
5932
- const toolArgs = JSON.parse(toolArgsStr.trim());
5933
- toolCallDetected = { name: toolName, args: toolArgs };
5934
- } catch (e) {
5935
- console.error("[MultiAgentCoordinator] Failed to parse tool args:", toolArgsStr, e);
5936
- }
5937
- } else {
5984
+ toolCallDetected = parseToolCall(textBuffer);
5985
+ if (!toolCallDetected) {
5938
5986
  yield textBuffer;
5939
5987
  }
5940
5988
  }
@@ -5951,14 +5999,16 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5951
5999
  let toolResultText = "";
5952
6000
  try {
5953
6001
  if (toolName === "document_search") {
5954
- const searchRes = yield new __await(this.documentSearch(toolArgs.query || ""));
5955
- toolResultText = `document_search returned:
5956
- ${searchRes.reply}
5957
-
5958
- Sources count: ${searchRes.sources.length}`;
5959
- if (searchRes.sources && searchRes.sources.length > 0) {
5960
- allSources.push(...searchRes.sources);
5961
- 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 };
5962
6012
  }
5963
6013
  } else {
5964
6014
  const mcpRes = yield new __await(this.mcpRegistry.callTool(toolName, toolArgs));
@@ -8856,8 +8906,30 @@ var Pipeline = class {
8856
8906
  getLLMProvider() {
8857
8907
  return this.initialised ? this.llmProvider : void 0;
8858
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
+ }
8859
8931
  async initialize() {
8860
- var _a2, _b, _c, _d;
8932
+ var _a2, _b, _c;
8861
8933
  if (this.initialised) return;
8862
8934
  const chartInstruction = `You are a helpful assistant. Use the provided context to answer questions accurately.
8863
8935
 
@@ -8900,19 +8972,22 @@ var Pipeline = class {
8900
8972
  this.entityExtractor = new EntityExtractor(this.llmProvider);
8901
8973
  }
8902
8974
  await this.vectorDB.initialize();
8903
- 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).");
8904
8977
  this.mcpRegistry = new MCPRegistry(this.config.mcpServers || []);
8905
8978
  this.multiAgentCoordinator = new MultiAgentCoordinator({
8906
8979
  llmProvider: this.llmProvider,
8907
8980
  mcpRegistry: this.mcpRegistry,
8908
- documentSearch: async (query) => {
8909
- return this.runNormalQuery(query);
8910
- }
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
8911
8984
  });
8985
+ } else if (this.isLangChainAgentMode) {
8986
+ console.log("[Pipeline] Initializing LangChainAgent (agentic / ReAct mode).");
8912
8987
  this.agent = new LangChainAgent(this, this.config);
8913
8988
  await this.agent.initialize(this.llmProvider);
8914
8989
  }
8915
- 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) {
8916
8991
  await this.loadColdContext(this.config.projectId);
8917
8992
  }
8918
8993
  this.initialised = true;
@@ -9075,11 +9150,14 @@ ${m.content}`).join("\n\n---\n\n");
9075
9150
  return { reply, sources };
9076
9151
  }
9077
9152
  async ask(question, history = [], namespace) {
9078
- var _a2, _b;
9079
9153
  await this.initialize();
9080
- 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) {
9081
9155
  return await this.multiAgentCoordinator.run(question, history);
9082
9156
  }
9157
+ if (this.isLangChainAgentMode && this.agent) {
9158
+ const reply2 = await this.agent.run(question, history);
9159
+ return { reply: reply2, sources: [] };
9160
+ }
9083
9161
  const stream = this.askStream(question, history, namespace);
9084
9162
  let reply = "";
9085
9163
  let sources = [];
@@ -9112,9 +9190,8 @@ ${m.content}`).join("\n\n---\n\n");
9112
9190
  }
9113
9191
  askStream(_0) {
9114
9192
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9115
- var _a2, _b;
9116
9193
  yield new __await(this.initialize());
9117
- 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) {
9118
9195
  const stream2 = this.multiAgentCoordinator.runStream(question, history);
9119
9196
  try {
9120
9197
  for (var iter = __forAwait(stream2), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
@@ -9133,6 +9210,11 @@ ${m.content}`).join("\n\n---\n\n");
9133
9210
  }
9134
9211
  return;
9135
9212
  }
9213
+ if (this.isLangChainAgentMode && this.agent) {
9214
+ const reply = yield new __await(this.agent.run(question, history));
9215
+ yield reply;
9216
+ return;
9217
+ }
9136
9218
  const stream = this.askStreamInternal(question, history, namespace);
9137
9219
  try {
9138
9220
  for (var iter2 = __forAwait(stream), more2, temp2, error2; more2 = !(temp2 = yield new __await(iter2.next())).done; more2 = false) {
@@ -9163,7 +9245,7 @@ ${m.content}`).join("\n\n---\n\n");
9163
9245
  */
9164
9246
  askStreamInternal(_0) {
9165
9247
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9166
- 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;
9167
9249
  yield new __await(this.initialize());
9168
9250
  const ns = formatNamespace(namespace != null ? namespace : this.config.projectId);
9169
9251
  const topK = (_b = (_a2 = this.config.rag) == null ? void 0 : _a2.topK) != null ? _b : 5;
@@ -9208,10 +9290,11 @@ ${m.content}`).join("\n\n---\n\n");
9208
9290
  const hasNumericPredicates = Array.isArray(filter.__numericPredicates) && filter.__numericPredicates.length > 0;
9209
9291
  const wantsExhaustiveList = true;
9210
9292
  const retrievalLimit = Math.max(topK * 50, 1e3);
9293
+ const retrieveStart = performance.now();
9211
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"));
9212
9295
  const retrieveEnd = performance.now();
9213
- const embedMs = retrieveEnd - embedStart;
9214
- const retrieveMs = retrieveEnd - embedStart;
9296
+ const embedMs = retrieveStart - embedStart;
9297
+ const retrieveMs = retrieveEnd - retrieveStart;
9215
9298
  const rerankStart = performance.now();
9216
9299
  const structuredSources = this.applyStructuredFilters(rawSources, filter).filter((s) => Boolean(s && typeof s === "object"));
9217
9300
  let fullSources = hasNumericPredicates ? structuredSources : structuredSources.filter((m) => {
@@ -9434,7 +9517,7 @@ ${context}`;
9434
9517
  const latency = {
9435
9518
  embedMs: Math.round(embedMs),
9436
9519
  retrieveMs: Math.round(retrieveMs),
9437
- 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,
9438
9521
  generateMs: Math.round(generateMs),
9439
9522
  totalMs: Math.round(totalMs)
9440
9523
  };
@@ -9447,7 +9530,7 @@ ${context}`;
9447
9530
  totalTokens: promptTokens + completionTokens,
9448
9531
  estimatedCostUsd: estimateCostUsd(promptTokens, completionTokens, this.config.llm.model)
9449
9532
  };
9450
- 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;
9451
9534
  const [uiTransformation, hallucinationResult] = yield new __await(Promise.all([
9452
9535
  uiTransformationPromise,
9453
9536
  awaitHallucination ? hallucinationScoringPromise : Promise.resolve(void 0)
@@ -9476,10 +9559,10 @@ ${context}`;
9476
9559
  hallucinationReason: hScore.reason
9477
9560
  } : {});
9478
9561
  const trace = buildTrace(hallucinationResult);
9479
- 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);
9480
9563
  if (isTelemetryActive) {
9481
9564
  const defaultUrl = "https://www.retrivora.com/api/telemetry";
9482
- 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;
9483
9566
  const absoluteUrl = telemetryUrl.startsWith("http") ? telemetryUrl : "https://www.retrivora.com" + (telemetryUrl.startsWith("/") ? telemetryUrl : "/" + telemetryUrl);
9484
9567
  (async () => {
9485
9568
  var _a3, _b2, _c2, _d2, _e2;
@@ -9653,7 +9736,7 @@ ${context}`;
9653
9736
  return Number.isFinite(numeric) ? numeric : null;
9654
9737
  }
9655
9738
  async retrieve(query, options) {
9656
- var _a2, _b, _c, _d, _e, _f, _g2;
9739
+ var _a2, _b, _c, _d, _e, _f;
9657
9740
  const ns = formatNamespace((_a2 = options.namespace) != null ? _a2 : this.config.projectId);
9658
9741
  const topK = (_b = options.topK) != null ? _b : 5;
9659
9742
  const cacheKey = `${ns}::${query}`;
@@ -9684,16 +9767,7 @@ ${context}`;
9684
9767
  await this.vectorDB.query(queryVector, retrievalLimit, ns, baseFilter),
9685
9768
  baseFilter
9686
9769
  ) : [];
9687
- const resolvedSources = [];
9688
- for (const source of sources) {
9689
- const parentId = (_g2 = source.metadata) == null ? void 0 : _g2.parent_id;
9690
- if (parentId) {
9691
- console.log(`[Pipeline] Multi-Vector: Found child chunk. Parent ID: ${parentId}`);
9692
- resolvedSources.push(source);
9693
- } else {
9694
- resolvedSources.push(source);
9695
- }
9696
- }
9770
+ const resolvedSources = sources;
9697
9771
  console.log("[Pipeline] retrieve() response:", {
9698
9772
  query,
9699
9773
  namespace: ns,