@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.
package/dist/server.js CHANGED
@@ -332,7 +332,7 @@ var init_package = __esm({
332
332
  "package.json"() {
333
333
  package_default = {
334
334
  name: "@retrivora-ai/rag-engine",
335
- version: "2.4.7",
335
+ version: "2.4.9",
336
336
  description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
337
337
  author: "Abhinav Alkuchi",
338
338
  license: "UNLICENSED",
@@ -539,10 +539,10 @@ var init_ConfigFetcher = __esm({
539
539
  const url = `${baseUrl}/api/v1/config?projectId=${encodeURIComponent(projectId)}`;
540
540
  const res = await fetch(url, {
541
541
  method: "GET",
542
- headers: __spreadValues({
542
+ headers: __spreadValues(__spreadValues({
543
543
  "Content-Type": "application/json",
544
544
  "x-sdk-version": SDK_VERSION
545
- }, licenseKey ? { "x-license-key": licenseKey } : {})
545
+ }, licenseKey ? { "x-license-key": licenseKey } : {}), projectId ? { "x-project-id": projectId } : {})
546
546
  });
547
547
  if (!res.ok) {
548
548
  console.warn(`[ConfigFetcher] Error text: ${await res.text()}`);
@@ -594,9 +594,9 @@ var init_ConfigFetcher = __esm({
594
594
  const credUrl = `${baseUrl}/api/v1/credentials`;
595
595
  const credRes = await fetch(credUrl, {
596
596
  method: "POST",
597
- headers: {
597
+ headers: __spreadValues(__spreadValues({
598
598
  "Content-Type": "application/json"
599
- },
599
+ }, licenseKey ? { "x-license-key": licenseKey } : {}), fetchedProjectId ? { "x-project-id": fetchedProjectId } : {}),
600
600
  body: JSON.stringify({ licenseKey, projectId: fetchedProjectId })
601
601
  });
602
602
  if (credRes.ok) {
@@ -782,7 +782,10 @@ var init_PineconeProvider = __esm({
782
782
  if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
783
783
  const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
784
784
  method: "POST",
785
- headers: { "Content-Type": "application/json", "x-license-key": this.licenseKey },
785
+ headers: __spreadValues({
786
+ "Content-Type": "application/json",
787
+ "x-license-key": this.licenseKey
788
+ }, this.projectId ? { "x-project-id": this.projectId } : {}),
786
789
  body: JSON.stringify({
787
790
  projectId: this.projectId,
788
791
  indexName: this.indexName,
@@ -817,7 +820,10 @@ var init_PineconeProvider = __esm({
817
820
  });
818
821
  const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
819
822
  method: "POST",
820
- headers: { "Content-Type": "application/json", "x-license-key": this.licenseKey },
823
+ headers: __spreadValues({
824
+ "Content-Type": "application/json",
825
+ "x-license-key": this.licenseKey
826
+ }, this.projectId ? { "x-project-id": this.projectId } : {}),
821
827
  body: JSON.stringify({
822
828
  projectId: this.projectId,
823
829
  indexName: this.indexName,
@@ -849,7 +855,10 @@ var init_PineconeProvider = __esm({
849
855
  if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
850
856
  const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/query`, {
851
857
  method: "POST",
852
- headers: { "Content-Type": "application/json", "x-license-key": this.licenseKey },
858
+ headers: __spreadValues({
859
+ "Content-Type": "application/json",
860
+ "x-license-key": this.licenseKey
861
+ }, this.projectId ? { "x-project-id": this.projectId } : {}),
853
862
  body: JSON.stringify({
854
863
  projectId: this.projectId,
855
864
  indexName: this.indexName,
@@ -1540,6 +1549,7 @@ var init_MongoDBProvider = __esm({
1540
1549
  MongoDBProvider = class extends BaseVectorProvider {
1541
1550
  constructor(config) {
1542
1551
  super(config);
1552
+ this.initialized = false;
1543
1553
  const opts = config.options;
1544
1554
  if (!opts.uri || !opts.database || !opts.collection) {
1545
1555
  throw new Error("[MongoDBProvider] options uri, database, and collection are required.");
@@ -1617,9 +1627,11 @@ var init_MongoDBProvider = __esm({
1617
1627
  };
1618
1628
  }
1619
1629
  async initialize() {
1630
+ if (this.initialized) return;
1620
1631
  await this.client.connect();
1621
1632
  this.db = this.client.db(this.dbName);
1622
1633
  this.collection = this.db.collection(this.collectionName);
1634
+ this.initialized = true;
1623
1635
  }
1624
1636
  async upsert(doc, namespace) {
1625
1637
  const document = __spreadValues({
@@ -1711,7 +1723,6 @@ var init_MongoDBProvider = __esm({
1711
1723
  const sanitized = super.sanitizeFilter(filter);
1712
1724
  const mongoFilter = {};
1713
1725
  for (const [key, value] of Object.entries(sanitized)) {
1714
- if (key.startsWith("__") || key === "keywords" || key === "queryText") continue;
1715
1726
  if (key === "namespace") {
1716
1727
  mongoFilter[key] = value;
1717
1728
  } else {
@@ -1730,6 +1741,7 @@ var init_MongoDBProvider = __esm({
1730
1741
  }
1731
1742
  async disconnect() {
1732
1743
  await this.client.close();
1744
+ this.initialized = false;
1733
1745
  }
1734
1746
  };
1735
1747
  }
@@ -1774,15 +1786,19 @@ var init_MilvusProvider = __esm({
1774
1786
  await this.http.post("/v1/vector/upsert", payload);
1775
1787
  }
1776
1788
  async batchUpsert(docs, namespace) {
1777
- const payload = {
1778
- collectionName: this.indexName,
1779
- data: docs.map((doc) => __spreadValues({
1780
- vector: doc.vector,
1781
- content: doc.content,
1782
- metadata: doc.metadata || {}
1783
- }, namespace ? { namespace } : {}))
1784
- };
1785
- await this.http.post("/v1/vector/upsert", payload);
1789
+ const batchSize = 100;
1790
+ for (let i = 0; i < docs.length; i += batchSize) {
1791
+ const batch = docs.slice(i, i + batchSize);
1792
+ const payload = {
1793
+ collectionName: this.indexName,
1794
+ data: batch.map((doc) => __spreadValues({
1795
+ vector: doc.vector,
1796
+ content: doc.content,
1797
+ metadata: doc.metadata || {}
1798
+ }, namespace ? { namespace } : {}))
1799
+ };
1800
+ await this.http.post("/v1/vector/upsert", payload);
1801
+ }
1786
1802
  }
1787
1803
  async query(vector, topK, namespace, _filter) {
1788
1804
  const sanitizedFilter = this.sanitizeFilter(_filter);
@@ -1793,7 +1809,8 @@ var init_MilvusProvider = __esm({
1793
1809
  for (const [key, value] of Object.entries(sanitizedFilter)) {
1794
1810
  if (key === "queryText" || key === "keywords") continue;
1795
1811
  if (typeof value === "string") {
1796
- filterParts.push(`metadata["${key}"] == "${value.replace(/"/g, '\\"')}"`);
1812
+ const escaped = value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
1813
+ filterParts.push(`metadata["${key}"] == "${escaped}"`);
1797
1814
  } else if (typeof value === "number") {
1798
1815
  filterParts.push(`metadata["${key}"] == ${value}`);
1799
1816
  }
@@ -1817,7 +1834,7 @@ var init_MilvusProvider = __esm({
1817
1834
  metadata: res["metadata"] || {}
1818
1835
  }));
1819
1836
  }
1820
- async delete(id, _namespace) {
1837
+ async delete(id, namespace) {
1821
1838
  await this.http.post("/v1/vector/delete", {
1822
1839
  collectionName: this.indexName,
1823
1840
  id
@@ -1889,7 +1906,6 @@ var init_QdrantProvider = __esm({
1889
1906
  return this.ensureIndex(fieldName, schemaType || "keyword");
1890
1907
  })
1891
1908
  );
1892
- } else {
1893
1909
  }
1894
1910
  this.schemaDiscovered = true;
1895
1911
  }
@@ -2135,7 +2151,7 @@ var init_ChromaDBProvider = __esm({
2135
2151
  documents: docs.map((d) => d.content),
2136
2152
  metadatas: docs.map((d) => __spreadValues(__spreadValues({}, d.metadata || {}), namespace ? { namespace } : {}))
2137
2153
  };
2138
- await this.http.post(`/api/v1/collections/${this.collectionId}/add`, payload);
2154
+ await this.http.post(`/api/v1/collections/${this.collectionId}/upsert`, payload);
2139
2155
  }
2140
2156
  async query(vector, topK, namespace, _filter) {
2141
2157
  const sanitizedFilter = this.sanitizeFilter(_filter);
@@ -2219,17 +2235,16 @@ var init_RedisProvider = __esm({
2219
2235
  await this.ping();
2220
2236
  }
2221
2237
  async upsert(doc, namespace) {
2222
- const key = namespace ? `${namespace}:${doc.id}` : String(doc.id);
2223
- await this.http.post("/", ["JSON.SET", key, "$", JSON.stringify({
2224
- vector: doc.vector,
2225
- content: doc.content,
2226
- metadata: doc.metadata || {}
2227
- })]);
2238
+ await this.batchUpsert([doc], namespace);
2228
2239
  }
2229
2240
  async batchUpsert(docs, namespace) {
2230
- for (const doc of docs) {
2231
- await this.upsert(doc, namespace);
2232
- }
2241
+ const payload = docs.map((doc) => __spreadValues({
2242
+ id: String(doc.id),
2243
+ vector: doc.vector,
2244
+ data: doc.content,
2245
+ metadata: doc.metadata || {}
2246
+ }, namespace ? { namespace } : {}));
2247
+ await this.http.post("/upsert", payload);
2233
2248
  }
2234
2249
  async query(vector, topK, namespace, _filter) {
2235
2250
  const payload = {
@@ -2247,19 +2262,22 @@ var init_RedisProvider = __esm({
2247
2262
  }));
2248
2263
  }
2249
2264
  async delete(id, namespace) {
2250
- const key = namespace ? `${namespace}:${id}` : id;
2251
- await this.http.post("/", ["DEL", key]);
2265
+ await this.http.post("/delete", __spreadValues({
2266
+ ids: [id]
2267
+ }, namespace ? { namespace } : {}));
2252
2268
  }
2253
2269
  async deleteNamespace(namespace) {
2254
- console.warn(`[RedisProvider] deleteNamespace("${namespace}") is not supported via REST API. Use Redis CLI: SCAN + DEL`);
2270
+ if (!namespace) {
2271
+ throw new Error("[RedisProvider] namespace is required for deleteNamespace");
2272
+ }
2273
+ await this.http.post("/reset", { namespace });
2255
2274
  }
2256
2275
  /**
2257
- * Check reachability via a PING command.
2258
- * Returns false on connection failure so health checks surface real problems.
2276
+ * Check reachability of the Upstash Vector API.
2259
2277
  */
2260
2278
  async ping() {
2261
2279
  try {
2262
- await this.http.post("/", ["PING"]);
2280
+ await this.http.get("/");
2263
2281
  return true;
2264
2282
  } catch (e) {
2265
2283
  return false;
@@ -2290,11 +2308,12 @@ var init_WeaviateProvider = __esm({
2290
2308
  if (!baseUrl) throw new Error("[WeaviateProvider] options.baseUrl is required");
2291
2309
  this.http = import_axios7.default.create({
2292
2310
  baseURL: baseUrl,
2293
- headers: __spreadValues({
2311
+ headers: __spreadValues(__spreadValues({
2294
2312
  "Content-Type": "application/json"
2295
2313
  }, opts.apiKey ? {
2296
- "X-OpenAI-Api-Key": opts.apiKey,
2297
2314
  "Authorization": `Bearer ${opts.apiKey}`
2315
+ } : {}), opts.openaiApiKey ? {
2316
+ "X-OpenAI-Api-Key": opts.openaiApiKey
2298
2317
  } : {})
2299
2318
  });
2300
2319
  }
@@ -2371,10 +2390,12 @@ var init_WeaviateProvider = __esm({
2371
2390
  await this.http.delete(`/v1/objects/${this.indexName}/${id}`);
2372
2391
  }
2373
2392
  async deleteNamespace(namespace) {
2374
- await this.http.post("/v1/batch/objects", {
2375
- match: {
2376
- class: this.indexName,
2377
- where: { path: ["namespace"], operator: "Equal", valueString: namespace }
2393
+ await this.http.delete("/v1/batch/objects", {
2394
+ data: {
2395
+ match: {
2396
+ class: this.indexName,
2397
+ where: { path: ["namespace"], operator: "Equal", valueString: namespace }
2398
+ }
2378
2399
  }
2379
2400
  });
2380
2401
  }
@@ -2605,6 +2626,9 @@ var init_SimpleGraphProvider = __esm({
2605
2626
  this.edges = [];
2606
2627
  }
2607
2628
  async initialize() {
2629
+ console.warn(
2630
+ "[SimpleGraphProvider] Warning: SimpleGraphProvider is an in-memory database meant for demonstration and local development. Graph data is NOT persisted across restarts."
2631
+ );
2608
2632
  }
2609
2633
  async addNodes(nodes) {
2610
2634
  for (const node of nodes) {
@@ -2626,15 +2650,21 @@ var init_SimpleGraphProvider = __esm({
2626
2650
  for (const edge of matchedEdges) {
2627
2651
  if (!matchedNodeIds.has(edge.source)) {
2628
2652
  const source = this.nodes.get(edge.source);
2629
- if (source) matchedNodes.push(source);
2653
+ if (source) {
2654
+ matchedNodes.push(source);
2655
+ matchedNodeIds.add(edge.source);
2656
+ }
2630
2657
  }
2631
2658
  if (!matchedNodeIds.has(edge.target)) {
2632
2659
  const target = this.nodes.get(edge.target);
2633
- if (target) matchedNodes.push(target);
2660
+ if (target) {
2661
+ matchedNodes.push(target);
2662
+ matchedNodeIds.add(edge.target);
2663
+ }
2634
2664
  }
2635
2665
  }
2636
2666
  return {
2637
- nodes: Array.from(new Set(matchedNodes)),
2667
+ nodes: matchedNodes,
2638
2668
  edges: matchedEdges
2639
2669
  };
2640
2670
  }
@@ -5909,27 +5939,42 @@ var MCPClient = class {
5909
5939
  var MCPRegistry = class {
5910
5940
  constructor(servers = []) {
5911
5941
  this.clients = [];
5942
+ /** Cache populated by getAllTools(); maps tool name → the client that owns it. */
5943
+ this.toolClientCache = null;
5912
5944
  this.clients = servers.map((cfg) => new MCPClient(cfg));
5913
5945
  }
5914
5946
  async getAllTools() {
5915
5947
  const list = [];
5948
+ const cache = /* @__PURE__ */ new Map();
5916
5949
  await Promise.all(
5917
5950
  this.clients.map(async (client) => {
5918
5951
  try {
5919
5952
  const tools = await client.listTools();
5920
- tools.forEach((t) => list.push({ serverName: client["config"].name, tool: t }));
5953
+ tools.forEach((t) => {
5954
+ list.push({ serverName: client["config"].name, tool: t });
5955
+ cache.set(t.name, client);
5956
+ });
5921
5957
  } catch (e) {
5922
5958
  console.warn(`[MCPRegistry] Failed to fetch tools from server "${client["config"].name}":`, e);
5923
5959
  }
5924
5960
  })
5925
5961
  );
5962
+ this.toolClientCache = cache;
5926
5963
  return list;
5927
5964
  }
5928
5965
  async callTool(toolName, args = {}) {
5966
+ var _a2;
5967
+ const cachedClient = (_a2 = this.toolClientCache) == null ? void 0 : _a2.get(toolName);
5968
+ if (cachedClient) {
5969
+ return cachedClient.callTool(toolName, args);
5970
+ }
5971
+ console.warn(`[MCPRegistry] Cache miss for tool "${toolName}", falling back to full scan.`);
5929
5972
  for (const client of this.clients) {
5930
5973
  try {
5931
5974
  const tools = await client.listTools();
5932
5975
  if (tools.some((t) => t.name === toolName)) {
5976
+ if (!this.toolClientCache) this.toolClientCache = /* @__PURE__ */ new Map();
5977
+ this.toolClientCache.set(toolName, client);
5933
5978
  return await client.callTool(toolName, args);
5934
5979
  }
5935
5980
  } catch (e) {
@@ -5937,18 +5982,44 @@ var MCPRegistry = class {
5937
5982
  }
5938
5983
  throw new Error(`[MCPRegistry] Tool "${toolName}" not found on any registered MCP server.`);
5939
5984
  }
5985
+ /** Invalidate the tool cache and disconnect all MCP clients. */
5940
5986
  disconnectAll() {
5987
+ this.toolClientCache = null;
5941
5988
  this.clients.forEach((c) => c.disconnect());
5942
5989
  }
5943
5990
  };
5944
5991
 
5945
5992
  // src/core/MultiAgentCoordinator.ts
5993
+ function parseToolCall(text) {
5994
+ const match = text.match(/TOOL_CALL:\s*([\w-]+)\s*\(\s*([\s\S]*?)\s*\)\s*[.,;]?\s*$/m);
5995
+ if (!match) return null;
5996
+ const name = match[1].trim();
5997
+ let argsRaw = match[2].trim();
5998
+ argsRaw = argsRaw.replace(/'/g, '"');
5999
+ if (argsRaw && !argsRaw.startsWith("{")) {
6000
+ argsRaw = `{${argsRaw}}`;
6001
+ }
6002
+ let args = {};
6003
+ if (argsRaw) {
6004
+ try {
6005
+ args = JSON.parse(argsRaw);
6006
+ } catch (e) {
6007
+ const pairs = [...argsRaw.matchAll(/"?([\w-]+)"?\s*:\s*"([^"]*)"/g)];
6008
+ if (pairs.length === 0) {
6009
+ console.warn("[parseToolCall] Could not parse tool args:", argsRaw);
6010
+ return null;
6011
+ }
6012
+ for (const [, k, v] of pairs) args[k] = v;
6013
+ }
6014
+ }
6015
+ return { name, args };
6016
+ }
5946
6017
  var MultiAgentCoordinator = class {
5947
6018
  constructor(options) {
5948
6019
  var _a2;
5949
6020
  this.llmProvider = options.llmProvider;
5950
6021
  this.mcpRegistry = options.mcpRegistry;
5951
- this.documentSearch = options.documentSearch;
6022
+ this.retriever = options.retriever;
5952
6023
  this.maxIterations = (_a2 = options.maxIterations) != null ? _a2 : 5;
5953
6024
  }
5954
6025
  /**
@@ -5982,18 +6053,17 @@ var MultiAgentCoordinator = class {
5982
6053
  throw error[0];
5983
6054
  }
5984
6055
  }
5985
- return {
5986
- reply: finalReply,
5987
- sources
5988
- };
6056
+ return { reply: finalReply, sources };
5989
6057
  }
5990
6058
  /**
5991
- * Run the multi-agent coordination loop as an async stream, yielding intermediate thought blocks and tool execution events.
6059
+ * Run the multi-agent coordination loop as an async stream,
6060
+ * yielding intermediate thought blocks and tool execution events.
5992
6061
  */
5993
6062
  runStream(_0) {
5994
6063
  return __asyncGenerator(this, arguments, function* (question, history = []) {
6064
+ var _a2;
5995
6065
  const mcpTools = yield new __await(this.mcpRegistry.getAllTools());
5996
- let systemPrompt = `You are a goal-driven supervisor agent coordinating a RAG search engine and external MCP servers to solve the user's task.
6066
+ const systemPrompt = `You are a goal-driven supervisor agent coordinating a RAG search engine and external MCP servers to solve the user's task.
5997
6067
  You must think step-by-step before acting. Write your internal thinking process inside a \`<think>...</think>\` block first, then act or answer.
5998
6068
 
5999
6069
  Available Tools:
@@ -6001,8 +6071,8 @@ Available Tools:
6001
6071
 
6002
6072
  ${mcpTools.length > 0 ? "MCP Server Tools:" : ""}
6003
6073
  ${mcpTools.map((mt) => {
6004
- var _a2;
6005
- return `- ${mt.tool.name}(${JSON.stringify(((_a2 = mt.tool.inputSchema) == null ? void 0 : _a2.properties) || {})}): ${mt.tool.description || "No description provided."}`;
6074
+ var _a3;
6075
+ return `- ${mt.tool.name}(${JSON.stringify(((_a3 = mt.tool.inputSchema) == null ? void 0 : _a3.properties) || {})}): ${mt.tool.description || "No description provided."}`;
6006
6076
  }).join("\n")}
6007
6077
 
6008
6078
  Tool Calling Protocol:
@@ -6010,7 +6080,7 @@ If you need to call a tool, write:
6010
6080
  TOOL_CALL: <toolName>({ "argName": "value" })
6011
6081
  And then STOP generating immediately.
6012
6082
 
6013
- Once you have gathered enough information to fully satisfy the user's query, write your final response directly after the closing \`</think>\` block.
6083
+ Once you have gathered enough information to fully satisfy the user query, write your final response directly after the closing \`</think>\` block.
6014
6084
  `;
6015
6085
  const supervisorHistory = [
6016
6086
  { role: "system", content: systemPrompt },
@@ -6018,7 +6088,6 @@ Once you have gathered enough information to fully satisfy the user's query, wri
6018
6088
  { role: "user", content: question }
6019
6089
  ];
6020
6090
  let iteration = 0;
6021
- const allSources = [];
6022
6091
  while (iteration < this.maxIterations) {
6023
6092
  iteration++;
6024
6093
  const context = "";
@@ -6031,24 +6100,14 @@ Once you have gathered enough information to fully satisfy the user's query, wri
6031
6100
  fullModelResponse = reply;
6032
6101
  const thinkIndex = reply.indexOf("<think>");
6033
6102
  const endThinkIndex = reply.indexOf("</think>");
6034
- let thinkingText = "";
6035
6103
  let answerText = reply;
6036
6104
  if (thinkIndex !== -1 && endThinkIndex !== -1) {
6037
- thinkingText = reply.substring(thinkIndex + 7, endThinkIndex);
6105
+ const thinkingText = reply.substring(thinkIndex + 7, endThinkIndex);
6038
6106
  answerText = reply.substring(endThinkIndex + 8);
6039
6107
  yield { type: "thinking", text: thinkingText };
6040
6108
  }
6041
- const toolMatch = answerText.match(/TOOL_CALL:\s*(\w+)\s*\(([\s\S]*)\)/);
6042
- if (toolMatch) {
6043
- const toolName = toolMatch[1];
6044
- const toolArgsStr = toolMatch[2];
6045
- try {
6046
- const toolArgs = JSON.parse(toolArgsStr.trim());
6047
- toolCallDetected = { name: toolName, args: toolArgs };
6048
- } catch (e) {
6049
- console.error("[MultiAgentCoordinator] Failed to parse tool args:", toolArgsStr, e);
6050
- }
6051
- } else {
6109
+ toolCallDetected = parseToolCall(answerText);
6110
+ if (!toolCallDetected) {
6052
6111
  yield answerText;
6053
6112
  }
6054
6113
  } else {
@@ -6077,9 +6136,7 @@ Once you have gathered enough information to fully satisfy the user's query, wri
6077
6136
  if (!inThinking && textBuffer.includes("TOOL_CALL:")) {
6078
6137
  const idx = textBuffer.indexOf("TOOL_CALL:");
6079
6138
  const precedingText = textBuffer.slice(0, idx);
6080
- if (precedingText.trim()) {
6081
- yield precedingText;
6082
- }
6139
+ if (precedingText.trim()) yield precedingText;
6083
6140
  textBuffer = textBuffer.slice(idx);
6084
6141
  }
6085
6142
  if (!inThinking && !textBuffer.includes("TOOL_CALL:") && textBuffer.length > 0) {
@@ -6098,17 +6155,8 @@ Once you have gathered enough information to fully satisfy the user's query, wri
6098
6155
  }
6099
6156
  }
6100
6157
  if (textBuffer.trim()) {
6101
- const toolMatch = textBuffer.match(/TOOL_CALL:\s*(\w+)\s*\(([\s\S]*)\)/);
6102
- if (toolMatch) {
6103
- const toolName = toolMatch[1];
6104
- const toolArgsStr = toolMatch[2];
6105
- try {
6106
- const toolArgs = JSON.parse(toolArgsStr.trim());
6107
- toolCallDetected = { name: toolName, args: toolArgs };
6108
- } catch (e) {
6109
- console.error("[MultiAgentCoordinator] Failed to parse tool args:", toolArgsStr, e);
6110
- }
6111
- } else {
6158
+ toolCallDetected = parseToolCall(textBuffer);
6159
+ if (!toolCallDetected) {
6112
6160
  yield textBuffer;
6113
6161
  }
6114
6162
  }
@@ -6125,14 +6173,16 @@ Once you have gathered enough information to fully satisfy the user's query, wri
6125
6173
  let toolResultText = "";
6126
6174
  try {
6127
6175
  if (toolName === "document_search") {
6128
- const searchRes = yield new __await(this.documentSearch(toolArgs.query || ""));
6129
- toolResultText = `document_search returned:
6130
- ${searchRes.reply}
6131
-
6132
- Sources count: ${searchRes.sources.length}`;
6133
- if (searchRes.sources && searchRes.sources.length > 0) {
6134
- allSources.push(...searchRes.sources);
6135
- yield { reply: "", sources: searchRes.sources };
6176
+ const query = String((_a2 = toolArgs.query) != null ? _a2 : "");
6177
+ const { sources } = yield new __await(this.retriever.retrieve(query));
6178
+ const snippets = sources.slice(0, 5).map((s, i) => {
6179
+ var _a3;
6180
+ return `[${i + 1}] ${String((_a3 = s.content) != null ? _a3 : "").slice(0, 400)}`;
6181
+ }).join("\n\n");
6182
+ toolResultText = `document_search returned ${sources.length} source(s):
6183
+ ${snippets}`;
6184
+ if (sources.length > 0) {
6185
+ yield { reply: "", sources };
6136
6186
  }
6137
6187
  } else {
6138
6188
  const mcpRes = yield new __await(this.mcpRegistry.callTool(toolName, toolArgs));
@@ -9044,8 +9094,30 @@ var Pipeline = class {
9044
9094
  getLLMProvider() {
9045
9095
  return this.initialised ? this.llmProvider : void 0;
9046
9096
  }
9097
+ /**
9098
+ * True when multi-agent / MCP mode is active:
9099
+ * architecture === 'multi-agent' OR mcpServers array is non-empty.
9100
+ * Uses MultiAgentCoordinator (custom supervisor loop).
9101
+ */
9102
+ get isMultiAgentMode() {
9103
+ var _a2, _b, _c;
9104
+ return ((_a2 = this.config.rag) == null ? void 0 : _a2.architecture) === "multi-agent" || ((_c = (_b = this.config.mcpServers) == null ? void 0 : _b.length) != null ? _c : 0) > 0;
9105
+ }
9106
+ /**
9107
+ * True when the LangChain ReAct agent mode is selected:
9108
+ * architecture === 'agentic'.
9109
+ * Uses LangChainAgent (LangGraph-backed ReAct, optional peer dep).
9110
+ */
9111
+ get isLangChainAgentMode() {
9112
+ var _a2;
9113
+ return ((_a2 = this.config.rag) == null ? void 0 : _a2.architecture) === "agentic";
9114
+ }
9115
+ /** Union of both agentic execution paths — used to gate the normal RAG pipeline. */
9116
+ get isAgenticMode() {
9117
+ return this.isMultiAgentMode || this.isLangChainAgentMode;
9118
+ }
9047
9119
  async initialize() {
9048
- var _a2, _b, _c, _d;
9120
+ var _a2, _b, _c;
9049
9121
  if (this.initialised) return;
9050
9122
  const chartInstruction = `You are a helpful assistant. Use the provided context to answer questions accurately.
9051
9123
 
@@ -9088,19 +9160,22 @@ var Pipeline = class {
9088
9160
  this.entityExtractor = new EntityExtractor(this.llmProvider);
9089
9161
  }
9090
9162
  await this.vectorDB.initialize();
9091
- if (((_a2 = this.config.rag) == null ? void 0 : _a2.architecture) === "agentic" || ((_b = this.config.rag) == null ? void 0 : _b.architecture) === "multi-agent" || this.config.mcpServers && this.config.mcpServers.length > 0) {
9163
+ if (this.isMultiAgentMode) {
9164
+ console.log("[Pipeline] Initializing MultiAgentCoordinator (multi-agent / MCP mode).");
9092
9165
  this.mcpRegistry = new MCPRegistry(this.config.mcpServers || []);
9093
9166
  this.multiAgentCoordinator = new MultiAgentCoordinator({
9094
9167
  llmProvider: this.llmProvider,
9095
9168
  mcpRegistry: this.mcpRegistry,
9096
- documentSearch: async (query) => {
9097
- return this.runNormalQuery(query);
9098
- }
9169
+ // Pass Pipeline itself as the IRetriever — avoids closure over internal methods.
9170
+ retriever: this,
9171
+ maxIterations: (_a2 = this.config.rag) == null ? void 0 : _a2.agentMaxIterations
9099
9172
  });
9173
+ } else if (this.isLangChainAgentMode) {
9174
+ console.log("[Pipeline] Initializing LangChainAgent (agentic / ReAct mode).");
9100
9175
  this.agent = new LangChainAgent(this, this.config);
9101
9176
  await this.agent.initialize(this.llmProvider);
9102
9177
  }
9103
- if ((_d = (_c = this.config.rag) == null ? void 0 : _c.cag) == null ? void 0 : _d.enabled) {
9178
+ if ((_c = (_b = this.config.rag) == null ? void 0 : _b.cag) == null ? void 0 : _c.enabled) {
9104
9179
  await this.loadColdContext(this.config.projectId);
9105
9180
  }
9106
9181
  this.initialised = true;
@@ -9263,11 +9338,14 @@ ${m.content}`).join("\n\n---\n\n");
9263
9338
  return { reply, sources };
9264
9339
  }
9265
9340
  async ask(question, history = [], namespace) {
9266
- var _a2, _b;
9267
9341
  await this.initialize();
9268
- if ((((_a2 = this.config.rag) == null ? void 0 : _a2.architecture) === "agentic" || ((_b = this.config.rag) == null ? void 0 : _b.architecture) === "multi-agent" || this.config.mcpServers && this.config.mcpServers.length > 0) && this.multiAgentCoordinator) {
9342
+ if (this.isMultiAgentMode && this.multiAgentCoordinator) {
9269
9343
  return await this.multiAgentCoordinator.run(question, history);
9270
9344
  }
9345
+ if (this.isLangChainAgentMode && this.agent) {
9346
+ const reply2 = await this.agent.run(question, history);
9347
+ return { reply: reply2, sources: [] };
9348
+ }
9271
9349
  const stream = this.askStream(question, history, namespace);
9272
9350
  let reply = "";
9273
9351
  let sources = [];
@@ -9300,9 +9378,8 @@ ${m.content}`).join("\n\n---\n\n");
9300
9378
  }
9301
9379
  askStream(_0) {
9302
9380
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9303
- var _a2, _b;
9304
9381
  yield new __await(this.initialize());
9305
- if ((((_a2 = this.config.rag) == null ? void 0 : _a2.architecture) === "agentic" || ((_b = this.config.rag) == null ? void 0 : _b.architecture) === "multi-agent" || this.config.mcpServers && this.config.mcpServers.length > 0) && this.multiAgentCoordinator) {
9382
+ if (this.isMultiAgentMode && this.multiAgentCoordinator) {
9306
9383
  const stream2 = this.multiAgentCoordinator.runStream(question, history);
9307
9384
  try {
9308
9385
  for (var iter = __forAwait(stream2), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
@@ -9321,6 +9398,11 @@ ${m.content}`).join("\n\n---\n\n");
9321
9398
  }
9322
9399
  return;
9323
9400
  }
9401
+ if (this.isLangChainAgentMode && this.agent) {
9402
+ const reply = yield new __await(this.agent.run(question, history));
9403
+ yield reply;
9404
+ return;
9405
+ }
9324
9406
  const stream = this.askStreamInternal(question, history, namespace);
9325
9407
  try {
9326
9408
  for (var iter2 = __forAwait(stream), more2, temp2, error2; more2 = !(temp2 = yield new __await(iter2.next())).done; more2 = false) {
@@ -9351,7 +9433,7 @@ ${m.content}`).join("\n\n---\n\n");
9351
9433
  */
9352
9434
  askStreamInternal(_0) {
9353
9435
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9354
- var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B;
9436
+ var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A;
9355
9437
  yield new __await(this.initialize());
9356
9438
  const ns = formatNamespace(namespace != null ? namespace : this.config.projectId);
9357
9439
  const topK = (_b = (_a2 = this.config.rag) == null ? void 0 : _a2.topK) != null ? _b : 5;
@@ -9396,10 +9478,11 @@ ${m.content}`).join("\n\n---\n\n");
9396
9478
  const hasNumericPredicates = Array.isArray(filter.__numericPredicates) && filter.__numericPredicates.length > 0;
9397
9479
  const wantsExhaustiveList = true;
9398
9480
  const retrievalLimit = Math.max(topK * 50, 1e3);
9481
+ const retrieveStart = performance.now();
9399
9482
  const rawSources = ((strategyResult === "vector" || strategyResult === "both") && queryVector && queryVector.length > 0 ? yield new __await(this.vectorDB.query(queryVector, retrievalLimit, ns, filter)) : []).filter((s) => Boolean(s && typeof s === "object"));
9400
9483
  const retrieveEnd = performance.now();
9401
- const embedMs = retrieveEnd - embedStart;
9402
- const retrieveMs = retrieveEnd - embedStart;
9484
+ const embedMs = retrieveStart - embedStart;
9485
+ const retrieveMs = retrieveEnd - retrieveStart;
9403
9486
  const rerankStart = performance.now();
9404
9487
  const structuredSources = this.applyStructuredFilters(rawSources, filter).filter((s) => Boolean(s && typeof s === "object"));
9405
9488
  let fullSources = hasNumericPredicates ? structuredSources : structuredSources.filter((m) => {
@@ -9622,7 +9705,7 @@ ${context}`;
9622
9705
  const latency = {
9623
9706
  embedMs: Math.round(embedMs),
9624
9707
  retrieveMs: Math.round(retrieveMs),
9625
- rerankMs: arch !== "naive" && (arch === "retrieve-and-rerank" || ((_x = this.config.rag) == null ? void 0 : _x.useReranking)) ? Math.round(rerankMs) : void 0,
9708
+ rerankMs: useReranking ? Math.round(rerankMs) : void 0,
9626
9709
  generateMs: Math.round(generateMs),
9627
9710
  totalMs: Math.round(totalMs)
9628
9711
  };
@@ -9635,7 +9718,7 @@ ${context}`;
9635
9718
  totalTokens: promptTokens + completionTokens,
9636
9719
  estimatedCostUsd: estimateCostUsd(promptTokens, completionTokens, this.config.llm.model)
9637
9720
  };
9638
- const awaitHallucination = ((_y = this.config.llm.options) == null ? void 0 : _y.awaitHallucination) === true;
9721
+ const awaitHallucination = ((_x = this.config.llm.options) == null ? void 0 : _x.awaitHallucination) === true;
9639
9722
  const [uiTransformation, hallucinationResult] = yield new __await(Promise.all([
9640
9723
  uiTransformationPromise,
9641
9724
  awaitHallucination ? hallucinationScoringPromise : Promise.resolve(void 0)
@@ -9664,10 +9747,10 @@ ${context}`;
9664
9747
  hallucinationReason: hScore.reason
9665
9748
  } : {});
9666
9749
  const trace = buildTrace(hallucinationResult);
9667
- const isTelemetryActive = (_A = (_z = this.config.telemetry) == null ? void 0 : _z.enabled) != null ? _A : Boolean(this.config.licenseKey);
9750
+ const isTelemetryActive = (_z = (_y = this.config.telemetry) == null ? void 0 : _y.enabled) != null ? _z : Boolean(this.config.licenseKey);
9668
9751
  if (isTelemetryActive) {
9669
9752
  const defaultUrl = "https://www.retrivora.com/api/telemetry";
9670
- const telemetryUrl = ((_B = this.config.telemetry) == null ? void 0 : _B.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
9753
+ const telemetryUrl = ((_A = this.config.telemetry) == null ? void 0 : _A.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
9671
9754
  const absoluteUrl = telemetryUrl.startsWith("http") ? telemetryUrl : "https://www.retrivora.com" + (telemetryUrl.startsWith("/") ? telemetryUrl : "/" + telemetryUrl);
9672
9755
  (async () => {
9673
9756
  var _a3, _b2, _c2, _d2, _e2;
@@ -9841,7 +9924,7 @@ ${context}`;
9841
9924
  return Number.isFinite(numeric) ? numeric : null;
9842
9925
  }
9843
9926
  async retrieve(query, options) {
9844
- var _a2, _b, _c, _d, _e, _f, _g2;
9927
+ var _a2, _b, _c, _d, _e, _f;
9845
9928
  const ns = formatNamespace((_a2 = options.namespace) != null ? _a2 : this.config.projectId);
9846
9929
  const topK = (_b = options.topK) != null ? _b : 5;
9847
9930
  const cacheKey = `${ns}::${query}`;
@@ -9872,16 +9955,7 @@ ${context}`;
9872
9955
  await this.vectorDB.query(queryVector, retrievalLimit, ns, baseFilter),
9873
9956
  baseFilter
9874
9957
  ) : [];
9875
- const resolvedSources = [];
9876
- for (const source of sources) {
9877
- const parentId = (_g2 = source.metadata) == null ? void 0 : _g2.parent_id;
9878
- if (parentId) {
9879
- console.log(`[Pipeline] Multi-Vector: Found child chunk. Parent ID: ${parentId}`);
9880
- resolvedSources.push(source);
9881
- } else {
9882
- resolvedSources.push(source);
9883
- }
9884
- }
9958
+ const resolvedSources = sources;
9885
9959
  console.log("[Pipeline] retrieve() response:", {
9886
9960
  query,
9887
9961
  namespace: ns,