@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.
@@ -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.7",
321
+ version: "2.4.9",
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",
@@ -525,10 +525,10 @@ var init_ConfigFetcher = __esm({
525
525
  const url = `${baseUrl}/api/v1/config?projectId=${encodeURIComponent(projectId)}`;
526
526
  const res = await fetch(url, {
527
527
  method: "GET",
528
- headers: __spreadValues({
528
+ headers: __spreadValues(__spreadValues({
529
529
  "Content-Type": "application/json",
530
530
  "x-sdk-version": SDK_VERSION
531
- }, licenseKey ? { "x-license-key": licenseKey } : {})
531
+ }, licenseKey ? { "x-license-key": licenseKey } : {}), projectId ? { "x-project-id": projectId } : {})
532
532
  });
533
533
  if (!res.ok) {
534
534
  console.warn(`[ConfigFetcher] Error text: ${await res.text()}`);
@@ -580,9 +580,9 @@ var init_ConfigFetcher = __esm({
580
580
  const credUrl = `${baseUrl}/api/v1/credentials`;
581
581
  const credRes = await fetch(credUrl, {
582
582
  method: "POST",
583
- headers: {
583
+ headers: __spreadValues(__spreadValues({
584
584
  "Content-Type": "application/json"
585
- },
585
+ }, licenseKey ? { "x-license-key": licenseKey } : {}), fetchedProjectId ? { "x-project-id": fetchedProjectId } : {}),
586
586
  body: JSON.stringify({ licenseKey, projectId: fetchedProjectId })
587
587
  });
588
588
  if (credRes.ok) {
@@ -768,7 +768,10 @@ var init_PineconeProvider = __esm({
768
768
  if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
769
769
  const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
770
770
  method: "POST",
771
- headers: { "Content-Type": "application/json", "x-license-key": this.licenseKey },
771
+ headers: __spreadValues({
772
+ "Content-Type": "application/json",
773
+ "x-license-key": this.licenseKey
774
+ }, this.projectId ? { "x-project-id": this.projectId } : {}),
772
775
  body: JSON.stringify({
773
776
  projectId: this.projectId,
774
777
  indexName: this.indexName,
@@ -803,7 +806,10 @@ var init_PineconeProvider = __esm({
803
806
  });
804
807
  const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
805
808
  method: "POST",
806
- headers: { "Content-Type": "application/json", "x-license-key": this.licenseKey },
809
+ headers: __spreadValues({
810
+ "Content-Type": "application/json",
811
+ "x-license-key": this.licenseKey
812
+ }, this.projectId ? { "x-project-id": this.projectId } : {}),
807
813
  body: JSON.stringify({
808
814
  projectId: this.projectId,
809
815
  indexName: this.indexName,
@@ -835,7 +841,10 @@ var init_PineconeProvider = __esm({
835
841
  if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
836
842
  const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/query`, {
837
843
  method: "POST",
838
- headers: { "Content-Type": "application/json", "x-license-key": this.licenseKey },
844
+ headers: __spreadValues({
845
+ "Content-Type": "application/json",
846
+ "x-license-key": this.licenseKey
847
+ }, this.projectId ? { "x-project-id": this.projectId } : {}),
839
848
  body: JSON.stringify({
840
849
  projectId: this.projectId,
841
850
  indexName: this.indexName,
@@ -1526,6 +1535,7 @@ var init_MongoDBProvider = __esm({
1526
1535
  MongoDBProvider = class extends BaseVectorProvider {
1527
1536
  constructor(config) {
1528
1537
  super(config);
1538
+ this.initialized = false;
1529
1539
  const opts = config.options;
1530
1540
  if (!opts.uri || !opts.database || !opts.collection) {
1531
1541
  throw new Error("[MongoDBProvider] options uri, database, and collection are required.");
@@ -1603,9 +1613,11 @@ var init_MongoDBProvider = __esm({
1603
1613
  };
1604
1614
  }
1605
1615
  async initialize() {
1616
+ if (this.initialized) return;
1606
1617
  await this.client.connect();
1607
1618
  this.db = this.client.db(this.dbName);
1608
1619
  this.collection = this.db.collection(this.collectionName);
1620
+ this.initialized = true;
1609
1621
  }
1610
1622
  async upsert(doc, namespace) {
1611
1623
  const document = __spreadValues({
@@ -1697,7 +1709,6 @@ var init_MongoDBProvider = __esm({
1697
1709
  const sanitized = super.sanitizeFilter(filter);
1698
1710
  const mongoFilter = {};
1699
1711
  for (const [key, value] of Object.entries(sanitized)) {
1700
- if (key.startsWith("__") || key === "keywords" || key === "queryText") continue;
1701
1712
  if (key === "namespace") {
1702
1713
  mongoFilter[key] = value;
1703
1714
  } else {
@@ -1716,6 +1727,7 @@ var init_MongoDBProvider = __esm({
1716
1727
  }
1717
1728
  async disconnect() {
1718
1729
  await this.client.close();
1730
+ this.initialized = false;
1719
1731
  }
1720
1732
  };
1721
1733
  }
@@ -1760,15 +1772,19 @@ var init_MilvusProvider = __esm({
1760
1772
  await this.http.post("/v1/vector/upsert", payload);
1761
1773
  }
1762
1774
  async batchUpsert(docs, namespace) {
1763
- const payload = {
1764
- collectionName: this.indexName,
1765
- data: docs.map((doc) => __spreadValues({
1766
- vector: doc.vector,
1767
- content: doc.content,
1768
- metadata: doc.metadata || {}
1769
- }, namespace ? { namespace } : {}))
1770
- };
1771
- 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
+ }
1772
1788
  }
1773
1789
  async query(vector, topK, namespace, _filter) {
1774
1790
  const sanitizedFilter = this.sanitizeFilter(_filter);
@@ -1779,7 +1795,8 @@ var init_MilvusProvider = __esm({
1779
1795
  for (const [key, value] of Object.entries(sanitizedFilter)) {
1780
1796
  if (key === "queryText" || key === "keywords") continue;
1781
1797
  if (typeof value === "string") {
1782
- filterParts.push(`metadata["${key}"] == "${value.replace(/"/g, '\\"')}"`);
1798
+ const escaped = value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
1799
+ filterParts.push(`metadata["${key}"] == "${escaped}"`);
1783
1800
  } else if (typeof value === "number") {
1784
1801
  filterParts.push(`metadata["${key}"] == ${value}`);
1785
1802
  }
@@ -1803,7 +1820,7 @@ var init_MilvusProvider = __esm({
1803
1820
  metadata: res["metadata"] || {}
1804
1821
  }));
1805
1822
  }
1806
- async delete(id, _namespace) {
1823
+ async delete(id, namespace) {
1807
1824
  await this.http.post("/v1/vector/delete", {
1808
1825
  collectionName: this.indexName,
1809
1826
  id
@@ -1875,7 +1892,6 @@ var init_QdrantProvider = __esm({
1875
1892
  return this.ensureIndex(fieldName, schemaType || "keyword");
1876
1893
  })
1877
1894
  );
1878
- } else {
1879
1895
  }
1880
1896
  this.schemaDiscovered = true;
1881
1897
  }
@@ -2121,7 +2137,7 @@ var init_ChromaDBProvider = __esm({
2121
2137
  documents: docs.map((d) => d.content),
2122
2138
  metadatas: docs.map((d) => __spreadValues(__spreadValues({}, d.metadata || {}), namespace ? { namespace } : {}))
2123
2139
  };
2124
- await this.http.post(`/api/v1/collections/${this.collectionId}/add`, payload);
2140
+ await this.http.post(`/api/v1/collections/${this.collectionId}/upsert`, payload);
2125
2141
  }
2126
2142
  async query(vector, topK, namespace, _filter) {
2127
2143
  const sanitizedFilter = this.sanitizeFilter(_filter);
@@ -2205,17 +2221,16 @@ var init_RedisProvider = __esm({
2205
2221
  await this.ping();
2206
2222
  }
2207
2223
  async upsert(doc, namespace) {
2208
- const key = namespace ? `${namespace}:${doc.id}` : String(doc.id);
2209
- await this.http.post("/", ["JSON.SET", key, "$", JSON.stringify({
2210
- vector: doc.vector,
2211
- content: doc.content,
2212
- metadata: doc.metadata || {}
2213
- })]);
2224
+ await this.batchUpsert([doc], namespace);
2214
2225
  }
2215
2226
  async batchUpsert(docs, namespace) {
2216
- for (const doc of docs) {
2217
- await this.upsert(doc, namespace);
2218
- }
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);
2219
2234
  }
2220
2235
  async query(vector, topK, namespace, _filter) {
2221
2236
  const payload = {
@@ -2233,19 +2248,22 @@ var init_RedisProvider = __esm({
2233
2248
  }));
2234
2249
  }
2235
2250
  async delete(id, namespace) {
2236
- const key = namespace ? `${namespace}:${id}` : id;
2237
- await this.http.post("/", ["DEL", key]);
2251
+ await this.http.post("/delete", __spreadValues({
2252
+ ids: [id]
2253
+ }, namespace ? { namespace } : {}));
2238
2254
  }
2239
2255
  async deleteNamespace(namespace) {
2240
- 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 });
2241
2260
  }
2242
2261
  /**
2243
- * Check reachability via a PING command.
2244
- * Returns false on connection failure so health checks surface real problems.
2262
+ * Check reachability of the Upstash Vector API.
2245
2263
  */
2246
2264
  async ping() {
2247
2265
  try {
2248
- await this.http.post("/", ["PING"]);
2266
+ await this.http.get("/");
2249
2267
  return true;
2250
2268
  } catch (e) {
2251
2269
  return false;
@@ -2276,11 +2294,12 @@ var init_WeaviateProvider = __esm({
2276
2294
  if (!baseUrl) throw new Error("[WeaviateProvider] options.baseUrl is required");
2277
2295
  this.http = import_axios7.default.create({
2278
2296
  baseURL: baseUrl,
2279
- headers: __spreadValues({
2297
+ headers: __spreadValues(__spreadValues({
2280
2298
  "Content-Type": "application/json"
2281
2299
  }, opts.apiKey ? {
2282
- "X-OpenAI-Api-Key": opts.apiKey,
2283
2300
  "Authorization": `Bearer ${opts.apiKey}`
2301
+ } : {}), opts.openaiApiKey ? {
2302
+ "X-OpenAI-Api-Key": opts.openaiApiKey
2284
2303
  } : {})
2285
2304
  });
2286
2305
  }
@@ -2357,10 +2376,12 @@ var init_WeaviateProvider = __esm({
2357
2376
  await this.http.delete(`/v1/objects/${this.indexName}/${id}`);
2358
2377
  }
2359
2378
  async deleteNamespace(namespace) {
2360
- await this.http.post("/v1/batch/objects", {
2361
- match: {
2362
- class: this.indexName,
2363
- 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
+ }
2364
2385
  }
2365
2386
  });
2366
2387
  }
@@ -2591,6 +2612,9 @@ var init_SimpleGraphProvider = __esm({
2591
2612
  this.edges = [];
2592
2613
  }
2593
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
+ );
2594
2618
  }
2595
2619
  async addNodes(nodes) {
2596
2620
  for (const node of nodes) {
@@ -2612,15 +2636,21 @@ var init_SimpleGraphProvider = __esm({
2612
2636
  for (const edge of matchedEdges) {
2613
2637
  if (!matchedNodeIds.has(edge.source)) {
2614
2638
  const source = this.nodes.get(edge.source);
2615
- if (source) matchedNodes.push(source);
2639
+ if (source) {
2640
+ matchedNodes.push(source);
2641
+ matchedNodeIds.add(edge.source);
2642
+ }
2616
2643
  }
2617
2644
  if (!matchedNodeIds.has(edge.target)) {
2618
2645
  const target = this.nodes.get(edge.target);
2619
- if (target) matchedNodes.push(target);
2646
+ if (target) {
2647
+ matchedNodes.push(target);
2648
+ matchedNodeIds.add(edge.target);
2649
+ }
2620
2650
  }
2621
2651
  }
2622
2652
  return {
2623
- nodes: Array.from(new Set(matchedNodes)),
2653
+ nodes: matchedNodes,
2624
2654
  edges: matchedEdges
2625
2655
  };
2626
2656
  }
@@ -5762,27 +5792,42 @@ var MCPClient = class {
5762
5792
  var MCPRegistry = class {
5763
5793
  constructor(servers = []) {
5764
5794
  this.clients = [];
5795
+ /** Cache populated by getAllTools(); maps tool name → the client that owns it. */
5796
+ this.toolClientCache = null;
5765
5797
  this.clients = servers.map((cfg) => new MCPClient(cfg));
5766
5798
  }
5767
5799
  async getAllTools() {
5768
5800
  const list = [];
5801
+ const cache = /* @__PURE__ */ new Map();
5769
5802
  await Promise.all(
5770
5803
  this.clients.map(async (client) => {
5771
5804
  try {
5772
5805
  const tools = await client.listTools();
5773
- 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
+ });
5774
5810
  } catch (e) {
5775
5811
  console.warn(`[MCPRegistry] Failed to fetch tools from server "${client["config"].name}":`, e);
5776
5812
  }
5777
5813
  })
5778
5814
  );
5815
+ this.toolClientCache = cache;
5779
5816
  return list;
5780
5817
  }
5781
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.`);
5782
5825
  for (const client of this.clients) {
5783
5826
  try {
5784
5827
  const tools = await client.listTools();
5785
5828
  if (tools.some((t) => t.name === toolName)) {
5829
+ if (!this.toolClientCache) this.toolClientCache = /* @__PURE__ */ new Map();
5830
+ this.toolClientCache.set(toolName, client);
5786
5831
  return await client.callTool(toolName, args);
5787
5832
  }
5788
5833
  } catch (e) {
@@ -5790,18 +5835,44 @@ var MCPRegistry = class {
5790
5835
  }
5791
5836
  throw new Error(`[MCPRegistry] Tool "${toolName}" not found on any registered MCP server.`);
5792
5837
  }
5838
+ /** Invalidate the tool cache and disconnect all MCP clients. */
5793
5839
  disconnectAll() {
5840
+ this.toolClientCache = null;
5794
5841
  this.clients.forEach((c) => c.disconnect());
5795
5842
  }
5796
5843
  };
5797
5844
 
5798
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
+ }
5799
5870
  var MultiAgentCoordinator = class {
5800
5871
  constructor(options) {
5801
5872
  var _a2;
5802
5873
  this.llmProvider = options.llmProvider;
5803
5874
  this.mcpRegistry = options.mcpRegistry;
5804
- this.documentSearch = options.documentSearch;
5875
+ this.retriever = options.retriever;
5805
5876
  this.maxIterations = (_a2 = options.maxIterations) != null ? _a2 : 5;
5806
5877
  }
5807
5878
  /**
@@ -5835,18 +5906,17 @@ var MultiAgentCoordinator = class {
5835
5906
  throw error[0];
5836
5907
  }
5837
5908
  }
5838
- return {
5839
- reply: finalReply,
5840
- sources
5841
- };
5909
+ return { reply: finalReply, sources };
5842
5910
  }
5843
5911
  /**
5844
- * 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.
5845
5914
  */
5846
5915
  runStream(_0) {
5847
5916
  return __asyncGenerator(this, arguments, function* (question, history = []) {
5917
+ var _a2;
5848
5918
  const mcpTools = yield new __await(this.mcpRegistry.getAllTools());
5849
- 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.
5850
5920
  You must think step-by-step before acting. Write your internal thinking process inside a \`<think>...</think>\` block first, then act or answer.
5851
5921
 
5852
5922
  Available Tools:
@@ -5854,8 +5924,8 @@ Available Tools:
5854
5924
 
5855
5925
  ${mcpTools.length > 0 ? "MCP Server Tools:" : ""}
5856
5926
  ${mcpTools.map((mt) => {
5857
- var _a2;
5858
- 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."}`;
5859
5929
  }).join("\n")}
5860
5930
 
5861
5931
  Tool Calling Protocol:
@@ -5863,7 +5933,7 @@ If you need to call a tool, write:
5863
5933
  TOOL_CALL: <toolName>({ "argName": "value" })
5864
5934
  And then STOP generating immediately.
5865
5935
 
5866
- 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.
5867
5937
  `;
5868
5938
  const supervisorHistory = [
5869
5939
  { role: "system", content: systemPrompt },
@@ -5871,7 +5941,6 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5871
5941
  { role: "user", content: question }
5872
5942
  ];
5873
5943
  let iteration = 0;
5874
- const allSources = [];
5875
5944
  while (iteration < this.maxIterations) {
5876
5945
  iteration++;
5877
5946
  const context = "";
@@ -5884,24 +5953,14 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5884
5953
  fullModelResponse = reply;
5885
5954
  const thinkIndex = reply.indexOf("<think>");
5886
5955
  const endThinkIndex = reply.indexOf("</think>");
5887
- let thinkingText = "";
5888
5956
  let answerText = reply;
5889
5957
  if (thinkIndex !== -1 && endThinkIndex !== -1) {
5890
- thinkingText = reply.substring(thinkIndex + 7, endThinkIndex);
5958
+ const thinkingText = reply.substring(thinkIndex + 7, endThinkIndex);
5891
5959
  answerText = reply.substring(endThinkIndex + 8);
5892
5960
  yield { type: "thinking", text: thinkingText };
5893
5961
  }
5894
- const toolMatch = answerText.match(/TOOL_CALL:\s*(\w+)\s*\(([\s\S]*)\)/);
5895
- if (toolMatch) {
5896
- const toolName = toolMatch[1];
5897
- const toolArgsStr = toolMatch[2];
5898
- try {
5899
- const toolArgs = JSON.parse(toolArgsStr.trim());
5900
- toolCallDetected = { name: toolName, args: toolArgs };
5901
- } catch (e) {
5902
- console.error("[MultiAgentCoordinator] Failed to parse tool args:", toolArgsStr, e);
5903
- }
5904
- } else {
5962
+ toolCallDetected = parseToolCall(answerText);
5963
+ if (!toolCallDetected) {
5905
5964
  yield answerText;
5906
5965
  }
5907
5966
  } else {
@@ -5930,9 +5989,7 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5930
5989
  if (!inThinking && textBuffer.includes("TOOL_CALL:")) {
5931
5990
  const idx = textBuffer.indexOf("TOOL_CALL:");
5932
5991
  const precedingText = textBuffer.slice(0, idx);
5933
- if (precedingText.trim()) {
5934
- yield precedingText;
5935
- }
5992
+ if (precedingText.trim()) yield precedingText;
5936
5993
  textBuffer = textBuffer.slice(idx);
5937
5994
  }
5938
5995
  if (!inThinking && !textBuffer.includes("TOOL_CALL:") && textBuffer.length > 0) {
@@ -5951,17 +6008,8 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5951
6008
  }
5952
6009
  }
5953
6010
  if (textBuffer.trim()) {
5954
- const toolMatch = textBuffer.match(/TOOL_CALL:\s*(\w+)\s*\(([\s\S]*)\)/);
5955
- if (toolMatch) {
5956
- const toolName = toolMatch[1];
5957
- const toolArgsStr = toolMatch[2];
5958
- try {
5959
- const toolArgs = JSON.parse(toolArgsStr.trim());
5960
- toolCallDetected = { name: toolName, args: toolArgs };
5961
- } catch (e) {
5962
- console.error("[MultiAgentCoordinator] Failed to parse tool args:", toolArgsStr, e);
5963
- }
5964
- } else {
6011
+ toolCallDetected = parseToolCall(textBuffer);
6012
+ if (!toolCallDetected) {
5965
6013
  yield textBuffer;
5966
6014
  }
5967
6015
  }
@@ -5978,14 +6026,16 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5978
6026
  let toolResultText = "";
5979
6027
  try {
5980
6028
  if (toolName === "document_search") {
5981
- const searchRes = yield new __await(this.documentSearch(toolArgs.query || ""));
5982
- toolResultText = `document_search returned:
5983
- ${searchRes.reply}
5984
-
5985
- Sources count: ${searchRes.sources.length}`;
5986
- if (searchRes.sources && searchRes.sources.length > 0) {
5987
- allSources.push(...searchRes.sources);
5988
- 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 };
5989
6039
  }
5990
6040
  } else {
5991
6041
  const mcpRes = yield new __await(this.mcpRegistry.callTool(toolName, toolArgs));
@@ -8883,8 +8933,30 @@ var Pipeline = class {
8883
8933
  getLLMProvider() {
8884
8934
  return this.initialised ? this.llmProvider : void 0;
8885
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
+ }
8886
8958
  async initialize() {
8887
- var _a2, _b, _c, _d;
8959
+ var _a2, _b, _c;
8888
8960
  if (this.initialised) return;
8889
8961
  const chartInstruction = `You are a helpful assistant. Use the provided context to answer questions accurately.
8890
8962
 
@@ -8927,19 +8999,22 @@ var Pipeline = class {
8927
8999
  this.entityExtractor = new EntityExtractor(this.llmProvider);
8928
9000
  }
8929
9001
  await this.vectorDB.initialize();
8930
- 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).");
8931
9004
  this.mcpRegistry = new MCPRegistry(this.config.mcpServers || []);
8932
9005
  this.multiAgentCoordinator = new MultiAgentCoordinator({
8933
9006
  llmProvider: this.llmProvider,
8934
9007
  mcpRegistry: this.mcpRegistry,
8935
- documentSearch: async (query) => {
8936
- return this.runNormalQuery(query);
8937
- }
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
8938
9011
  });
9012
+ } else if (this.isLangChainAgentMode) {
9013
+ console.log("[Pipeline] Initializing LangChainAgent (agentic / ReAct mode).");
8939
9014
  this.agent = new LangChainAgent(this, this.config);
8940
9015
  await this.agent.initialize(this.llmProvider);
8941
9016
  }
8942
- 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) {
8943
9018
  await this.loadColdContext(this.config.projectId);
8944
9019
  }
8945
9020
  this.initialised = true;
@@ -9102,11 +9177,14 @@ ${m.content}`).join("\n\n---\n\n");
9102
9177
  return { reply, sources };
9103
9178
  }
9104
9179
  async ask(question, history = [], namespace) {
9105
- var _a2, _b;
9106
9180
  await this.initialize();
9107
- 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) {
9108
9182
  return await this.multiAgentCoordinator.run(question, history);
9109
9183
  }
9184
+ if (this.isLangChainAgentMode && this.agent) {
9185
+ const reply2 = await this.agent.run(question, history);
9186
+ return { reply: reply2, sources: [] };
9187
+ }
9110
9188
  const stream = this.askStream(question, history, namespace);
9111
9189
  let reply = "";
9112
9190
  let sources = [];
@@ -9139,9 +9217,8 @@ ${m.content}`).join("\n\n---\n\n");
9139
9217
  }
9140
9218
  askStream(_0) {
9141
9219
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9142
- var _a2, _b;
9143
9220
  yield new __await(this.initialize());
9144
- 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) {
9145
9222
  const stream2 = this.multiAgentCoordinator.runStream(question, history);
9146
9223
  try {
9147
9224
  for (var iter = __forAwait(stream2), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
@@ -9160,6 +9237,11 @@ ${m.content}`).join("\n\n---\n\n");
9160
9237
  }
9161
9238
  return;
9162
9239
  }
9240
+ if (this.isLangChainAgentMode && this.agent) {
9241
+ const reply = yield new __await(this.agent.run(question, history));
9242
+ yield reply;
9243
+ return;
9244
+ }
9163
9245
  const stream = this.askStreamInternal(question, history, namespace);
9164
9246
  try {
9165
9247
  for (var iter2 = __forAwait(stream), more2, temp2, error2; more2 = !(temp2 = yield new __await(iter2.next())).done; more2 = false) {
@@ -9190,7 +9272,7 @@ ${m.content}`).join("\n\n---\n\n");
9190
9272
  */
9191
9273
  askStreamInternal(_0) {
9192
9274
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9193
- 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;
9194
9276
  yield new __await(this.initialize());
9195
9277
  const ns = formatNamespace(namespace != null ? namespace : this.config.projectId);
9196
9278
  const topK = (_b = (_a2 = this.config.rag) == null ? void 0 : _a2.topK) != null ? _b : 5;
@@ -9235,10 +9317,11 @@ ${m.content}`).join("\n\n---\n\n");
9235
9317
  const hasNumericPredicates = Array.isArray(filter.__numericPredicates) && filter.__numericPredicates.length > 0;
9236
9318
  const wantsExhaustiveList = true;
9237
9319
  const retrievalLimit = Math.max(topK * 50, 1e3);
9320
+ const retrieveStart = performance.now();
9238
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"));
9239
9322
  const retrieveEnd = performance.now();
9240
- const embedMs = retrieveEnd - embedStart;
9241
- const retrieveMs = retrieveEnd - embedStart;
9323
+ const embedMs = retrieveStart - embedStart;
9324
+ const retrieveMs = retrieveEnd - retrieveStart;
9242
9325
  const rerankStart = performance.now();
9243
9326
  const structuredSources = this.applyStructuredFilters(rawSources, filter).filter((s) => Boolean(s && typeof s === "object"));
9244
9327
  let fullSources = hasNumericPredicates ? structuredSources : structuredSources.filter((m) => {
@@ -9461,7 +9544,7 @@ ${context}`;
9461
9544
  const latency = {
9462
9545
  embedMs: Math.round(embedMs),
9463
9546
  retrieveMs: Math.round(retrieveMs),
9464
- 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,
9465
9548
  generateMs: Math.round(generateMs),
9466
9549
  totalMs: Math.round(totalMs)
9467
9550
  };
@@ -9474,7 +9557,7 @@ ${context}`;
9474
9557
  totalTokens: promptTokens + completionTokens,
9475
9558
  estimatedCostUsd: estimateCostUsd(promptTokens, completionTokens, this.config.llm.model)
9476
9559
  };
9477
- 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;
9478
9561
  const [uiTransformation, hallucinationResult] = yield new __await(Promise.all([
9479
9562
  uiTransformationPromise,
9480
9563
  awaitHallucination ? hallucinationScoringPromise : Promise.resolve(void 0)
@@ -9503,10 +9586,10 @@ ${context}`;
9503
9586
  hallucinationReason: hScore.reason
9504
9587
  } : {});
9505
9588
  const trace = buildTrace(hallucinationResult);
9506
- 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);
9507
9590
  if (isTelemetryActive) {
9508
9591
  const defaultUrl = "https://www.retrivora.com/api/telemetry";
9509
- 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;
9510
9593
  const absoluteUrl = telemetryUrl.startsWith("http") ? telemetryUrl : "https://www.retrivora.com" + (telemetryUrl.startsWith("/") ? telemetryUrl : "/" + telemetryUrl);
9511
9594
  (async () => {
9512
9595
  var _a3, _b2, _c2, _d2, _e2;
@@ -9680,7 +9763,7 @@ ${context}`;
9680
9763
  return Number.isFinite(numeric) ? numeric : null;
9681
9764
  }
9682
9765
  async retrieve(query, options) {
9683
- var _a2, _b, _c, _d, _e, _f, _g2;
9766
+ var _a2, _b, _c, _d, _e, _f;
9684
9767
  const ns = formatNamespace((_a2 = options.namespace) != null ? _a2 : this.config.projectId);
9685
9768
  const topK = (_b = options.topK) != null ? _b : 5;
9686
9769
  const cacheKey = `${ns}::${query}`;
@@ -9711,16 +9794,7 @@ ${context}`;
9711
9794
  await this.vectorDB.query(queryVector, retrievalLimit, ns, baseFilter),
9712
9795
  baseFilter
9713
9796
  ) : [];
9714
- const resolvedSources = [];
9715
- for (const source of sources) {
9716
- const parentId = (_g2 = source.metadata) == null ? void 0 : _g2.parent_id;
9717
- if (parentId) {
9718
- console.log(`[Pipeline] Multi-Vector: Found child chunk. Parent ID: ${parentId}`);
9719
- resolvedSources.push(source);
9720
- } else {
9721
- resolvedSources.push(source);
9722
- }
9723
- }
9797
+ const resolvedSources = sources;
9724
9798
  console.log("[Pipeline] retrieve() response:", {
9725
9799
  query,
9726
9800
  namespace: ns,