@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.mjs CHANGED
@@ -326,7 +326,7 @@ var init_package = __esm({
326
326
  "package.json"() {
327
327
  package_default = {
328
328
  name: "@retrivora-ai/rag-engine",
329
- version: "2.4.7",
329
+ version: "2.4.9",
330
330
  description: "Retrivora AI is a plug-and-play AI engine for RAG chat experiences \u2014 generic vector DB + LLM provider, embeddable or standalone.",
331
331
  author: "Abhinav Alkuchi",
332
332
  license: "UNLICENSED",
@@ -533,10 +533,10 @@ var init_ConfigFetcher = __esm({
533
533
  const url = `${baseUrl}/api/v1/config?projectId=${encodeURIComponent(projectId)}`;
534
534
  const res = await fetch(url, {
535
535
  method: "GET",
536
- headers: __spreadValues({
536
+ headers: __spreadValues(__spreadValues({
537
537
  "Content-Type": "application/json",
538
538
  "x-sdk-version": SDK_VERSION
539
- }, licenseKey ? { "x-license-key": licenseKey } : {})
539
+ }, licenseKey ? { "x-license-key": licenseKey } : {}), projectId ? { "x-project-id": projectId } : {})
540
540
  });
541
541
  if (!res.ok) {
542
542
  console.warn(`[ConfigFetcher] Error text: ${await res.text()}`);
@@ -588,9 +588,9 @@ var init_ConfigFetcher = __esm({
588
588
  const credUrl = `${baseUrl}/api/v1/credentials`;
589
589
  const credRes = await fetch(credUrl, {
590
590
  method: "POST",
591
- headers: {
591
+ headers: __spreadValues(__spreadValues({
592
592
  "Content-Type": "application/json"
593
- },
593
+ }, licenseKey ? { "x-license-key": licenseKey } : {}), fetchedProjectId ? { "x-project-id": fetchedProjectId } : {}),
594
594
  body: JSON.stringify({ licenseKey, projectId: fetchedProjectId })
595
595
  });
596
596
  if (credRes.ok) {
@@ -776,7 +776,10 @@ var init_PineconeProvider = __esm({
776
776
  if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
777
777
  const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
778
778
  method: "POST",
779
- headers: { "Content-Type": "application/json", "x-license-key": this.licenseKey },
779
+ headers: __spreadValues({
780
+ "Content-Type": "application/json",
781
+ "x-license-key": this.licenseKey
782
+ }, this.projectId ? { "x-project-id": this.projectId } : {}),
780
783
  body: JSON.stringify({
781
784
  projectId: this.projectId,
782
785
  indexName: this.indexName,
@@ -811,7 +814,10 @@ var init_PineconeProvider = __esm({
811
814
  });
812
815
  const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
813
816
  method: "POST",
814
- headers: { "Content-Type": "application/json", "x-license-key": this.licenseKey },
817
+ headers: __spreadValues({
818
+ "Content-Type": "application/json",
819
+ "x-license-key": this.licenseKey
820
+ }, this.projectId ? { "x-project-id": this.projectId } : {}),
815
821
  body: JSON.stringify({
816
822
  projectId: this.projectId,
817
823
  indexName: this.indexName,
@@ -843,7 +849,10 @@ var init_PineconeProvider = __esm({
843
849
  if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
844
850
  const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/query`, {
845
851
  method: "POST",
846
- headers: { "Content-Type": "application/json", "x-license-key": this.licenseKey },
852
+ headers: __spreadValues({
853
+ "Content-Type": "application/json",
854
+ "x-license-key": this.licenseKey
855
+ }, this.projectId ? { "x-project-id": this.projectId } : {}),
847
856
  body: JSON.stringify({
848
857
  projectId: this.projectId,
849
858
  indexName: this.indexName,
@@ -1534,6 +1543,7 @@ var init_MongoDBProvider = __esm({
1534
1543
  MongoDBProvider = class extends BaseVectorProvider {
1535
1544
  constructor(config) {
1536
1545
  super(config);
1546
+ this.initialized = false;
1537
1547
  const opts = config.options;
1538
1548
  if (!opts.uri || !opts.database || !opts.collection) {
1539
1549
  throw new Error("[MongoDBProvider] options uri, database, and collection are required.");
@@ -1611,9 +1621,11 @@ var init_MongoDBProvider = __esm({
1611
1621
  };
1612
1622
  }
1613
1623
  async initialize() {
1624
+ if (this.initialized) return;
1614
1625
  await this.client.connect();
1615
1626
  this.db = this.client.db(this.dbName);
1616
1627
  this.collection = this.db.collection(this.collectionName);
1628
+ this.initialized = true;
1617
1629
  }
1618
1630
  async upsert(doc, namespace) {
1619
1631
  const document = __spreadValues({
@@ -1705,7 +1717,6 @@ var init_MongoDBProvider = __esm({
1705
1717
  const sanitized = super.sanitizeFilter(filter);
1706
1718
  const mongoFilter = {};
1707
1719
  for (const [key, value] of Object.entries(sanitized)) {
1708
- if (key.startsWith("__") || key === "keywords" || key === "queryText") continue;
1709
1720
  if (key === "namespace") {
1710
1721
  mongoFilter[key] = value;
1711
1722
  } else {
@@ -1724,6 +1735,7 @@ var init_MongoDBProvider = __esm({
1724
1735
  }
1725
1736
  async disconnect() {
1726
1737
  await this.client.close();
1738
+ this.initialized = false;
1727
1739
  }
1728
1740
  };
1729
1741
  }
@@ -1768,15 +1780,19 @@ var init_MilvusProvider = __esm({
1768
1780
  await this.http.post("/v1/vector/upsert", payload);
1769
1781
  }
1770
1782
  async batchUpsert(docs, namespace) {
1771
- const payload = {
1772
- collectionName: this.indexName,
1773
- data: docs.map((doc) => __spreadValues({
1774
- vector: doc.vector,
1775
- content: doc.content,
1776
- metadata: doc.metadata || {}
1777
- }, namespace ? { namespace } : {}))
1778
- };
1779
- await this.http.post("/v1/vector/upsert", payload);
1783
+ const batchSize = 100;
1784
+ for (let i = 0; i < docs.length; i += batchSize) {
1785
+ const batch = docs.slice(i, i + batchSize);
1786
+ const payload = {
1787
+ collectionName: this.indexName,
1788
+ data: batch.map((doc) => __spreadValues({
1789
+ vector: doc.vector,
1790
+ content: doc.content,
1791
+ metadata: doc.metadata || {}
1792
+ }, namespace ? { namespace } : {}))
1793
+ };
1794
+ await this.http.post("/v1/vector/upsert", payload);
1795
+ }
1780
1796
  }
1781
1797
  async query(vector, topK, namespace, _filter) {
1782
1798
  const sanitizedFilter = this.sanitizeFilter(_filter);
@@ -1787,7 +1803,8 @@ var init_MilvusProvider = __esm({
1787
1803
  for (const [key, value] of Object.entries(sanitizedFilter)) {
1788
1804
  if (key === "queryText" || key === "keywords") continue;
1789
1805
  if (typeof value === "string") {
1790
- filterParts.push(`metadata["${key}"] == "${value.replace(/"/g, '\\"')}"`);
1806
+ const escaped = value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
1807
+ filterParts.push(`metadata["${key}"] == "${escaped}"`);
1791
1808
  } else if (typeof value === "number") {
1792
1809
  filterParts.push(`metadata["${key}"] == ${value}`);
1793
1810
  }
@@ -1811,7 +1828,7 @@ var init_MilvusProvider = __esm({
1811
1828
  metadata: res["metadata"] || {}
1812
1829
  }));
1813
1830
  }
1814
- async delete(id, _namespace) {
1831
+ async delete(id, namespace) {
1815
1832
  await this.http.post("/v1/vector/delete", {
1816
1833
  collectionName: this.indexName,
1817
1834
  id
@@ -1883,7 +1900,6 @@ var init_QdrantProvider = __esm({
1883
1900
  return this.ensureIndex(fieldName, schemaType || "keyword");
1884
1901
  })
1885
1902
  );
1886
- } else {
1887
1903
  }
1888
1904
  this.schemaDiscovered = true;
1889
1905
  }
@@ -2129,7 +2145,7 @@ var init_ChromaDBProvider = __esm({
2129
2145
  documents: docs.map((d) => d.content),
2130
2146
  metadatas: docs.map((d) => __spreadValues(__spreadValues({}, d.metadata || {}), namespace ? { namespace } : {}))
2131
2147
  };
2132
- await this.http.post(`/api/v1/collections/${this.collectionId}/add`, payload);
2148
+ await this.http.post(`/api/v1/collections/${this.collectionId}/upsert`, payload);
2133
2149
  }
2134
2150
  async query(vector, topK, namespace, _filter) {
2135
2151
  const sanitizedFilter = this.sanitizeFilter(_filter);
@@ -2213,17 +2229,16 @@ var init_RedisProvider = __esm({
2213
2229
  await this.ping();
2214
2230
  }
2215
2231
  async upsert(doc, namespace) {
2216
- const key = namespace ? `${namespace}:${doc.id}` : String(doc.id);
2217
- await this.http.post("/", ["JSON.SET", key, "$", JSON.stringify({
2218
- vector: doc.vector,
2219
- content: doc.content,
2220
- metadata: doc.metadata || {}
2221
- })]);
2232
+ await this.batchUpsert([doc], namespace);
2222
2233
  }
2223
2234
  async batchUpsert(docs, namespace) {
2224
- for (const doc of docs) {
2225
- await this.upsert(doc, namespace);
2226
- }
2235
+ const payload = docs.map((doc) => __spreadValues({
2236
+ id: String(doc.id),
2237
+ vector: doc.vector,
2238
+ data: doc.content,
2239
+ metadata: doc.metadata || {}
2240
+ }, namespace ? { namespace } : {}));
2241
+ await this.http.post("/upsert", payload);
2227
2242
  }
2228
2243
  async query(vector, topK, namespace, _filter) {
2229
2244
  const payload = {
@@ -2241,19 +2256,22 @@ var init_RedisProvider = __esm({
2241
2256
  }));
2242
2257
  }
2243
2258
  async delete(id, namespace) {
2244
- const key = namespace ? `${namespace}:${id}` : id;
2245
- await this.http.post("/", ["DEL", key]);
2259
+ await this.http.post("/delete", __spreadValues({
2260
+ ids: [id]
2261
+ }, namespace ? { namespace } : {}));
2246
2262
  }
2247
2263
  async deleteNamespace(namespace) {
2248
- console.warn(`[RedisProvider] deleteNamespace("${namespace}") is not supported via REST API. Use Redis CLI: SCAN + DEL`);
2264
+ if (!namespace) {
2265
+ throw new Error("[RedisProvider] namespace is required for deleteNamespace");
2266
+ }
2267
+ await this.http.post("/reset", { namespace });
2249
2268
  }
2250
2269
  /**
2251
- * Check reachability via a PING command.
2252
- * Returns false on connection failure so health checks surface real problems.
2270
+ * Check reachability of the Upstash Vector API.
2253
2271
  */
2254
2272
  async ping() {
2255
2273
  try {
2256
- await this.http.post("/", ["PING"]);
2274
+ await this.http.get("/");
2257
2275
  return true;
2258
2276
  } catch (e) {
2259
2277
  return false;
@@ -2284,11 +2302,12 @@ var init_WeaviateProvider = __esm({
2284
2302
  if (!baseUrl) throw new Error("[WeaviateProvider] options.baseUrl is required");
2285
2303
  this.http = axios7.create({
2286
2304
  baseURL: baseUrl,
2287
- headers: __spreadValues({
2305
+ headers: __spreadValues(__spreadValues({
2288
2306
  "Content-Type": "application/json"
2289
2307
  }, opts.apiKey ? {
2290
- "X-OpenAI-Api-Key": opts.apiKey,
2291
2308
  "Authorization": `Bearer ${opts.apiKey}`
2309
+ } : {}), opts.openaiApiKey ? {
2310
+ "X-OpenAI-Api-Key": opts.openaiApiKey
2292
2311
  } : {})
2293
2312
  });
2294
2313
  }
@@ -2365,10 +2384,12 @@ var init_WeaviateProvider = __esm({
2365
2384
  await this.http.delete(`/v1/objects/${this.indexName}/${id}`);
2366
2385
  }
2367
2386
  async deleteNamespace(namespace) {
2368
- await this.http.post("/v1/batch/objects", {
2369
- match: {
2370
- class: this.indexName,
2371
- where: { path: ["namespace"], operator: "Equal", valueString: namespace }
2387
+ await this.http.delete("/v1/batch/objects", {
2388
+ data: {
2389
+ match: {
2390
+ class: this.indexName,
2391
+ where: { path: ["namespace"], operator: "Equal", valueString: namespace }
2392
+ }
2372
2393
  }
2373
2394
  });
2374
2395
  }
@@ -2599,6 +2620,9 @@ var init_SimpleGraphProvider = __esm({
2599
2620
  this.edges = [];
2600
2621
  }
2601
2622
  async initialize() {
2623
+ console.warn(
2624
+ "[SimpleGraphProvider] Warning: SimpleGraphProvider is an in-memory database meant for demonstration and local development. Graph data is NOT persisted across restarts."
2625
+ );
2602
2626
  }
2603
2627
  async addNodes(nodes) {
2604
2628
  for (const node of nodes) {
@@ -2620,15 +2644,21 @@ var init_SimpleGraphProvider = __esm({
2620
2644
  for (const edge of matchedEdges) {
2621
2645
  if (!matchedNodeIds.has(edge.source)) {
2622
2646
  const source = this.nodes.get(edge.source);
2623
- if (source) matchedNodes.push(source);
2647
+ if (source) {
2648
+ matchedNodes.push(source);
2649
+ matchedNodeIds.add(edge.source);
2650
+ }
2624
2651
  }
2625
2652
  if (!matchedNodeIds.has(edge.target)) {
2626
2653
  const target = this.nodes.get(edge.target);
2627
- if (target) matchedNodes.push(target);
2654
+ if (target) {
2655
+ matchedNodes.push(target);
2656
+ matchedNodeIds.add(edge.target);
2657
+ }
2628
2658
  }
2629
2659
  }
2630
2660
  return {
2631
- nodes: Array.from(new Set(matchedNodes)),
2661
+ nodes: matchedNodes,
2632
2662
  edges: matchedEdges
2633
2663
  };
2634
2664
  }
@@ -5804,27 +5834,42 @@ var MCPClient = class {
5804
5834
  var MCPRegistry = class {
5805
5835
  constructor(servers = []) {
5806
5836
  this.clients = [];
5837
+ /** Cache populated by getAllTools(); maps tool name → the client that owns it. */
5838
+ this.toolClientCache = null;
5807
5839
  this.clients = servers.map((cfg) => new MCPClient(cfg));
5808
5840
  }
5809
5841
  async getAllTools() {
5810
5842
  const list = [];
5843
+ const cache = /* @__PURE__ */ new Map();
5811
5844
  await Promise.all(
5812
5845
  this.clients.map(async (client) => {
5813
5846
  try {
5814
5847
  const tools = await client.listTools();
5815
- tools.forEach((t) => list.push({ serverName: client["config"].name, tool: t }));
5848
+ tools.forEach((t) => {
5849
+ list.push({ serverName: client["config"].name, tool: t });
5850
+ cache.set(t.name, client);
5851
+ });
5816
5852
  } catch (e) {
5817
5853
  console.warn(`[MCPRegistry] Failed to fetch tools from server "${client["config"].name}":`, e);
5818
5854
  }
5819
5855
  })
5820
5856
  );
5857
+ this.toolClientCache = cache;
5821
5858
  return list;
5822
5859
  }
5823
5860
  async callTool(toolName, args = {}) {
5861
+ var _a2;
5862
+ const cachedClient = (_a2 = this.toolClientCache) == null ? void 0 : _a2.get(toolName);
5863
+ if (cachedClient) {
5864
+ return cachedClient.callTool(toolName, args);
5865
+ }
5866
+ console.warn(`[MCPRegistry] Cache miss for tool "${toolName}", falling back to full scan.`);
5824
5867
  for (const client of this.clients) {
5825
5868
  try {
5826
5869
  const tools = await client.listTools();
5827
5870
  if (tools.some((t) => t.name === toolName)) {
5871
+ if (!this.toolClientCache) this.toolClientCache = /* @__PURE__ */ new Map();
5872
+ this.toolClientCache.set(toolName, client);
5828
5873
  return await client.callTool(toolName, args);
5829
5874
  }
5830
5875
  } catch (e) {
@@ -5832,18 +5877,44 @@ var MCPRegistry = class {
5832
5877
  }
5833
5878
  throw new Error(`[MCPRegistry] Tool "${toolName}" not found on any registered MCP server.`);
5834
5879
  }
5880
+ /** Invalidate the tool cache and disconnect all MCP clients. */
5835
5881
  disconnectAll() {
5882
+ this.toolClientCache = null;
5836
5883
  this.clients.forEach((c) => c.disconnect());
5837
5884
  }
5838
5885
  };
5839
5886
 
5840
5887
  // src/core/MultiAgentCoordinator.ts
5888
+ function parseToolCall(text) {
5889
+ const match = text.match(/TOOL_CALL:\s*([\w-]+)\s*\(\s*([\s\S]*?)\s*\)\s*[.,;]?\s*$/m);
5890
+ if (!match) return null;
5891
+ const name = match[1].trim();
5892
+ let argsRaw = match[2].trim();
5893
+ argsRaw = argsRaw.replace(/'/g, '"');
5894
+ if (argsRaw && !argsRaw.startsWith("{")) {
5895
+ argsRaw = `{${argsRaw}}`;
5896
+ }
5897
+ let args = {};
5898
+ if (argsRaw) {
5899
+ try {
5900
+ args = JSON.parse(argsRaw);
5901
+ } catch (e) {
5902
+ const pairs = [...argsRaw.matchAll(/"?([\w-]+)"?\s*:\s*"([^"]*)"/g)];
5903
+ if (pairs.length === 0) {
5904
+ console.warn("[parseToolCall] Could not parse tool args:", argsRaw);
5905
+ return null;
5906
+ }
5907
+ for (const [, k, v] of pairs) args[k] = v;
5908
+ }
5909
+ }
5910
+ return { name, args };
5911
+ }
5841
5912
  var MultiAgentCoordinator = class {
5842
5913
  constructor(options) {
5843
5914
  var _a2;
5844
5915
  this.llmProvider = options.llmProvider;
5845
5916
  this.mcpRegistry = options.mcpRegistry;
5846
- this.documentSearch = options.documentSearch;
5917
+ this.retriever = options.retriever;
5847
5918
  this.maxIterations = (_a2 = options.maxIterations) != null ? _a2 : 5;
5848
5919
  }
5849
5920
  /**
@@ -5877,18 +5948,17 @@ var MultiAgentCoordinator = class {
5877
5948
  throw error[0];
5878
5949
  }
5879
5950
  }
5880
- return {
5881
- reply: finalReply,
5882
- sources
5883
- };
5951
+ return { reply: finalReply, sources };
5884
5952
  }
5885
5953
  /**
5886
- * Run the multi-agent coordination loop as an async stream, yielding intermediate thought blocks and tool execution events.
5954
+ * Run the multi-agent coordination loop as an async stream,
5955
+ * yielding intermediate thought blocks and tool execution events.
5887
5956
  */
5888
5957
  runStream(_0) {
5889
5958
  return __asyncGenerator(this, arguments, function* (question, history = []) {
5959
+ var _a2;
5890
5960
  const mcpTools = yield new __await(this.mcpRegistry.getAllTools());
5891
- let systemPrompt = `You are a goal-driven supervisor agent coordinating a RAG search engine and external MCP servers to solve the user's task.
5961
+ const systemPrompt = `You are a goal-driven supervisor agent coordinating a RAG search engine and external MCP servers to solve the user's task.
5892
5962
  You must think step-by-step before acting. Write your internal thinking process inside a \`<think>...</think>\` block first, then act or answer.
5893
5963
 
5894
5964
  Available Tools:
@@ -5896,8 +5966,8 @@ Available Tools:
5896
5966
 
5897
5967
  ${mcpTools.length > 0 ? "MCP Server Tools:" : ""}
5898
5968
  ${mcpTools.map((mt) => {
5899
- var _a2;
5900
- return `- ${mt.tool.name}(${JSON.stringify(((_a2 = mt.tool.inputSchema) == null ? void 0 : _a2.properties) || {})}): ${mt.tool.description || "No description provided."}`;
5969
+ var _a3;
5970
+ return `- ${mt.tool.name}(${JSON.stringify(((_a3 = mt.tool.inputSchema) == null ? void 0 : _a3.properties) || {})}): ${mt.tool.description || "No description provided."}`;
5901
5971
  }).join("\n")}
5902
5972
 
5903
5973
  Tool Calling Protocol:
@@ -5905,7 +5975,7 @@ If you need to call a tool, write:
5905
5975
  TOOL_CALL: <toolName>({ "argName": "value" })
5906
5976
  And then STOP generating immediately.
5907
5977
 
5908
- Once you have gathered enough information to fully satisfy the user's query, write your final response directly after the closing \`</think>\` block.
5978
+ Once you have gathered enough information to fully satisfy the user query, write your final response directly after the closing \`</think>\` block.
5909
5979
  `;
5910
5980
  const supervisorHistory = [
5911
5981
  { role: "system", content: systemPrompt },
@@ -5913,7 +5983,6 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5913
5983
  { role: "user", content: question }
5914
5984
  ];
5915
5985
  let iteration = 0;
5916
- const allSources = [];
5917
5986
  while (iteration < this.maxIterations) {
5918
5987
  iteration++;
5919
5988
  const context = "";
@@ -5926,24 +5995,14 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5926
5995
  fullModelResponse = reply;
5927
5996
  const thinkIndex = reply.indexOf("<think>");
5928
5997
  const endThinkIndex = reply.indexOf("</think>");
5929
- let thinkingText = "";
5930
5998
  let answerText = reply;
5931
5999
  if (thinkIndex !== -1 && endThinkIndex !== -1) {
5932
- thinkingText = reply.substring(thinkIndex + 7, endThinkIndex);
6000
+ const thinkingText = reply.substring(thinkIndex + 7, endThinkIndex);
5933
6001
  answerText = reply.substring(endThinkIndex + 8);
5934
6002
  yield { type: "thinking", text: thinkingText };
5935
6003
  }
5936
- const toolMatch = answerText.match(/TOOL_CALL:\s*(\w+)\s*\(([\s\S]*)\)/);
5937
- if (toolMatch) {
5938
- const toolName = toolMatch[1];
5939
- const toolArgsStr = toolMatch[2];
5940
- try {
5941
- const toolArgs = JSON.parse(toolArgsStr.trim());
5942
- toolCallDetected = { name: toolName, args: toolArgs };
5943
- } catch (e) {
5944
- console.error("[MultiAgentCoordinator] Failed to parse tool args:", toolArgsStr, e);
5945
- }
5946
- } else {
6004
+ toolCallDetected = parseToolCall(answerText);
6005
+ if (!toolCallDetected) {
5947
6006
  yield answerText;
5948
6007
  }
5949
6008
  } else {
@@ -5972,9 +6031,7 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5972
6031
  if (!inThinking && textBuffer.includes("TOOL_CALL:")) {
5973
6032
  const idx = textBuffer.indexOf("TOOL_CALL:");
5974
6033
  const precedingText = textBuffer.slice(0, idx);
5975
- if (precedingText.trim()) {
5976
- yield precedingText;
5977
- }
6034
+ if (precedingText.trim()) yield precedingText;
5978
6035
  textBuffer = textBuffer.slice(idx);
5979
6036
  }
5980
6037
  if (!inThinking && !textBuffer.includes("TOOL_CALL:") && textBuffer.length > 0) {
@@ -5993,17 +6050,8 @@ Once you have gathered enough information to fully satisfy the user's query, wri
5993
6050
  }
5994
6051
  }
5995
6052
  if (textBuffer.trim()) {
5996
- const toolMatch = textBuffer.match(/TOOL_CALL:\s*(\w+)\s*\(([\s\S]*)\)/);
5997
- if (toolMatch) {
5998
- const toolName = toolMatch[1];
5999
- const toolArgsStr = toolMatch[2];
6000
- try {
6001
- const toolArgs = JSON.parse(toolArgsStr.trim());
6002
- toolCallDetected = { name: toolName, args: toolArgs };
6003
- } catch (e) {
6004
- console.error("[MultiAgentCoordinator] Failed to parse tool args:", toolArgsStr, e);
6005
- }
6006
- } else {
6053
+ toolCallDetected = parseToolCall(textBuffer);
6054
+ if (!toolCallDetected) {
6007
6055
  yield textBuffer;
6008
6056
  }
6009
6057
  }
@@ -6020,14 +6068,16 @@ Once you have gathered enough information to fully satisfy the user's query, wri
6020
6068
  let toolResultText = "";
6021
6069
  try {
6022
6070
  if (toolName === "document_search") {
6023
- const searchRes = yield new __await(this.documentSearch(toolArgs.query || ""));
6024
- toolResultText = `document_search returned:
6025
- ${searchRes.reply}
6026
-
6027
- Sources count: ${searchRes.sources.length}`;
6028
- if (searchRes.sources && searchRes.sources.length > 0) {
6029
- allSources.push(...searchRes.sources);
6030
- yield { reply: "", sources: searchRes.sources };
6071
+ const query = String((_a2 = toolArgs.query) != null ? _a2 : "");
6072
+ const { sources } = yield new __await(this.retriever.retrieve(query));
6073
+ const snippets = sources.slice(0, 5).map((s, i) => {
6074
+ var _a3;
6075
+ return `[${i + 1}] ${String((_a3 = s.content) != null ? _a3 : "").slice(0, 400)}`;
6076
+ }).join("\n\n");
6077
+ toolResultText = `document_search returned ${sources.length} source(s):
6078
+ ${snippets}`;
6079
+ if (sources.length > 0) {
6080
+ yield { reply: "", sources };
6031
6081
  }
6032
6082
  } else {
6033
6083
  const mcpRes = yield new __await(this.mcpRegistry.callTool(toolName, toolArgs));
@@ -8939,8 +8989,30 @@ var Pipeline = class {
8939
8989
  getLLMProvider() {
8940
8990
  return this.initialised ? this.llmProvider : void 0;
8941
8991
  }
8992
+ /**
8993
+ * True when multi-agent / MCP mode is active:
8994
+ * architecture === 'multi-agent' OR mcpServers array is non-empty.
8995
+ * Uses MultiAgentCoordinator (custom supervisor loop).
8996
+ */
8997
+ get isMultiAgentMode() {
8998
+ var _a2, _b, _c;
8999
+ 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;
9000
+ }
9001
+ /**
9002
+ * True when the LangChain ReAct agent mode is selected:
9003
+ * architecture === 'agentic'.
9004
+ * Uses LangChainAgent (LangGraph-backed ReAct, optional peer dep).
9005
+ */
9006
+ get isLangChainAgentMode() {
9007
+ var _a2;
9008
+ return ((_a2 = this.config.rag) == null ? void 0 : _a2.architecture) === "agentic";
9009
+ }
9010
+ /** Union of both agentic execution paths — used to gate the normal RAG pipeline. */
9011
+ get isAgenticMode() {
9012
+ return this.isMultiAgentMode || this.isLangChainAgentMode;
9013
+ }
8942
9014
  async initialize() {
8943
- var _a2, _b, _c, _d;
9015
+ var _a2, _b, _c;
8944
9016
  if (this.initialised) return;
8945
9017
  const chartInstruction = `You are a helpful assistant. Use the provided context to answer questions accurately.
8946
9018
 
@@ -8983,19 +9055,22 @@ var Pipeline = class {
8983
9055
  this.entityExtractor = new EntityExtractor(this.llmProvider);
8984
9056
  }
8985
9057
  await this.vectorDB.initialize();
8986
- 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) {
9058
+ if (this.isMultiAgentMode) {
9059
+ console.log("[Pipeline] Initializing MultiAgentCoordinator (multi-agent / MCP mode).");
8987
9060
  this.mcpRegistry = new MCPRegistry(this.config.mcpServers || []);
8988
9061
  this.multiAgentCoordinator = new MultiAgentCoordinator({
8989
9062
  llmProvider: this.llmProvider,
8990
9063
  mcpRegistry: this.mcpRegistry,
8991
- documentSearch: async (query) => {
8992
- return this.runNormalQuery(query);
8993
- }
9064
+ // Pass Pipeline itself as the IRetriever — avoids closure over internal methods.
9065
+ retriever: this,
9066
+ maxIterations: (_a2 = this.config.rag) == null ? void 0 : _a2.agentMaxIterations
8994
9067
  });
9068
+ } else if (this.isLangChainAgentMode) {
9069
+ console.log("[Pipeline] Initializing LangChainAgent (agentic / ReAct mode).");
8995
9070
  this.agent = new LangChainAgent(this, this.config);
8996
9071
  await this.agent.initialize(this.llmProvider);
8997
9072
  }
8998
- if ((_d = (_c = this.config.rag) == null ? void 0 : _c.cag) == null ? void 0 : _d.enabled) {
9073
+ if ((_c = (_b = this.config.rag) == null ? void 0 : _b.cag) == null ? void 0 : _c.enabled) {
8999
9074
  await this.loadColdContext(this.config.projectId);
9000
9075
  }
9001
9076
  this.initialised = true;
@@ -9158,11 +9233,14 @@ ${m.content}`).join("\n\n---\n\n");
9158
9233
  return { reply, sources };
9159
9234
  }
9160
9235
  async ask(question, history = [], namespace) {
9161
- var _a2, _b;
9162
9236
  await this.initialize();
9163
- 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) {
9237
+ if (this.isMultiAgentMode && this.multiAgentCoordinator) {
9164
9238
  return await this.multiAgentCoordinator.run(question, history);
9165
9239
  }
9240
+ if (this.isLangChainAgentMode && this.agent) {
9241
+ const reply2 = await this.agent.run(question, history);
9242
+ return { reply: reply2, sources: [] };
9243
+ }
9166
9244
  const stream = this.askStream(question, history, namespace);
9167
9245
  let reply = "";
9168
9246
  let sources = [];
@@ -9195,9 +9273,8 @@ ${m.content}`).join("\n\n---\n\n");
9195
9273
  }
9196
9274
  askStream(_0) {
9197
9275
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9198
- var _a2, _b;
9199
9276
  yield new __await(this.initialize());
9200
- 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) {
9277
+ if (this.isMultiAgentMode && this.multiAgentCoordinator) {
9201
9278
  const stream2 = this.multiAgentCoordinator.runStream(question, history);
9202
9279
  try {
9203
9280
  for (var iter = __forAwait(stream2), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
@@ -9216,6 +9293,11 @@ ${m.content}`).join("\n\n---\n\n");
9216
9293
  }
9217
9294
  return;
9218
9295
  }
9296
+ if (this.isLangChainAgentMode && this.agent) {
9297
+ const reply = yield new __await(this.agent.run(question, history));
9298
+ yield reply;
9299
+ return;
9300
+ }
9219
9301
  const stream = this.askStreamInternal(question, history, namespace);
9220
9302
  try {
9221
9303
  for (var iter2 = __forAwait(stream), more2, temp2, error2; more2 = !(temp2 = yield new __await(iter2.next())).done; more2 = false) {
@@ -9246,7 +9328,7 @@ ${m.content}`).join("\n\n---\n\n");
9246
9328
  */
9247
9329
  askStreamInternal(_0) {
9248
9330
  return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
9249
- 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;
9331
+ 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;
9250
9332
  yield new __await(this.initialize());
9251
9333
  const ns = formatNamespace(namespace != null ? namespace : this.config.projectId);
9252
9334
  const topK = (_b = (_a2 = this.config.rag) == null ? void 0 : _a2.topK) != null ? _b : 5;
@@ -9291,10 +9373,11 @@ ${m.content}`).join("\n\n---\n\n");
9291
9373
  const hasNumericPredicates = Array.isArray(filter.__numericPredicates) && filter.__numericPredicates.length > 0;
9292
9374
  const wantsExhaustiveList = true;
9293
9375
  const retrievalLimit = Math.max(topK * 50, 1e3);
9376
+ const retrieveStart = performance.now();
9294
9377
  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"));
9295
9378
  const retrieveEnd = performance.now();
9296
- const embedMs = retrieveEnd - embedStart;
9297
- const retrieveMs = retrieveEnd - embedStart;
9379
+ const embedMs = retrieveStart - embedStart;
9380
+ const retrieveMs = retrieveEnd - retrieveStart;
9298
9381
  const rerankStart = performance.now();
9299
9382
  const structuredSources = this.applyStructuredFilters(rawSources, filter).filter((s) => Boolean(s && typeof s === "object"));
9300
9383
  let fullSources = hasNumericPredicates ? structuredSources : structuredSources.filter((m) => {
@@ -9517,7 +9600,7 @@ ${context}`;
9517
9600
  const latency = {
9518
9601
  embedMs: Math.round(embedMs),
9519
9602
  retrieveMs: Math.round(retrieveMs),
9520
- rerankMs: arch !== "naive" && (arch === "retrieve-and-rerank" || ((_x = this.config.rag) == null ? void 0 : _x.useReranking)) ? Math.round(rerankMs) : void 0,
9603
+ rerankMs: useReranking ? Math.round(rerankMs) : void 0,
9521
9604
  generateMs: Math.round(generateMs),
9522
9605
  totalMs: Math.round(totalMs)
9523
9606
  };
@@ -9530,7 +9613,7 @@ ${context}`;
9530
9613
  totalTokens: promptTokens + completionTokens,
9531
9614
  estimatedCostUsd: estimateCostUsd(promptTokens, completionTokens, this.config.llm.model)
9532
9615
  };
9533
- const awaitHallucination = ((_y = this.config.llm.options) == null ? void 0 : _y.awaitHallucination) === true;
9616
+ const awaitHallucination = ((_x = this.config.llm.options) == null ? void 0 : _x.awaitHallucination) === true;
9534
9617
  const [uiTransformation, hallucinationResult] = yield new __await(Promise.all([
9535
9618
  uiTransformationPromise,
9536
9619
  awaitHallucination ? hallucinationScoringPromise : Promise.resolve(void 0)
@@ -9559,10 +9642,10 @@ ${context}`;
9559
9642
  hallucinationReason: hScore.reason
9560
9643
  } : {});
9561
9644
  const trace = buildTrace(hallucinationResult);
9562
- const isTelemetryActive = (_A = (_z = this.config.telemetry) == null ? void 0 : _z.enabled) != null ? _A : Boolean(this.config.licenseKey);
9645
+ const isTelemetryActive = (_z = (_y = this.config.telemetry) == null ? void 0 : _y.enabled) != null ? _z : Boolean(this.config.licenseKey);
9563
9646
  if (isTelemetryActive) {
9564
9647
  const defaultUrl = "https://www.retrivora.com/api/telemetry";
9565
- const telemetryUrl = ((_B = this.config.telemetry) == null ? void 0 : _B.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
9648
+ const telemetryUrl = ((_A = this.config.telemetry) == null ? void 0 : _A.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
9566
9649
  const absoluteUrl = telemetryUrl.startsWith("http") ? telemetryUrl : "https://www.retrivora.com" + (telemetryUrl.startsWith("/") ? telemetryUrl : "/" + telemetryUrl);
9567
9650
  (async () => {
9568
9651
  var _a3, _b2, _c2, _d2, _e2;
@@ -9736,7 +9819,7 @@ ${context}`;
9736
9819
  return Number.isFinite(numeric) ? numeric : null;
9737
9820
  }
9738
9821
  async retrieve(query, options) {
9739
- var _a2, _b, _c, _d, _e, _f, _g2;
9822
+ var _a2, _b, _c, _d, _e, _f;
9740
9823
  const ns = formatNamespace((_a2 = options.namespace) != null ? _a2 : this.config.projectId);
9741
9824
  const topK = (_b = options.topK) != null ? _b : 5;
9742
9825
  const cacheKey = `${ns}::${query}`;
@@ -9767,16 +9850,7 @@ ${context}`;
9767
9850
  await this.vectorDB.query(queryVector, retrievalLimit, ns, baseFilter),
9768
9851
  baseFilter
9769
9852
  ) : [];
9770
- const resolvedSources = [];
9771
- for (const source of sources) {
9772
- const parentId = (_g2 = source.metadata) == null ? void 0 : _g2.parent_id;
9773
- if (parentId) {
9774
- console.log(`[Pipeline] Multi-Vector: Found child chunk. Parent ID: ${parentId}`);
9775
- resolvedSources.push(source);
9776
- } else {
9777
- resolvedSources.push(source);
9778
- }
9779
- }
9853
+ const resolvedSources = sources;
9780
9854
  console.log("[Pipeline] retrieve() response:", {
9781
9855
  query,
9782
9856
  namespace: ns,