@flutchai/flutch-sdk 0.7.6 → 0.7.8

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/index.cjs CHANGED
@@ -8,10 +8,10 @@ var fs = require('fs');
8
8
  var path2 = require('path');
9
9
  var os = require('os');
10
10
  var config = require('@nestjs/config');
11
+ var express = require('express');
11
12
  var net = require('net');
12
13
  var mongoose = require('mongoose');
13
14
  var langgraphCheckpointMongodb = require('@langchain/langgraph-checkpoint-mongodb');
14
- var express = require('express');
15
15
  var crypto = require('crypto');
16
16
  var promClient = require('prom-client');
17
17
  var messages = require('@langchain/core/messages');
@@ -1990,6 +1990,14 @@ async function bootstrap(AppModule, options = {}) {
1990
1990
  setupRedisMock();
1991
1991
  const app = await core.NestFactory.create(AppModule);
1992
1992
  const logger3 = new common.Logger("Bootstrap");
1993
+ const bodyLimit = options.bodyLimit || process.env.GRAPH_BODY_LIMIT || "10mb";
1994
+ app.use(express.json({
1995
+ limit: bodyLimit
1996
+ }));
1997
+ app.use(express.urlencoded({
1998
+ limit: bodyLimit,
1999
+ extended: true
2000
+ }));
1993
2001
  const requestedPort = options.port || parseInt(process.env.PORT || "3100", 10);
1994
2002
  const port = await findAvailablePort(requestedPort);
1995
2003
  const globalPrefix = options.globalPrefix;
@@ -2123,7 +2131,8 @@ exports.GraphController = class _GraphController {
2123
2131
  async getSupportedGraphTypes() {
2124
2132
  return this.graphService.getSupportedGraphTypes();
2125
2133
  }
2126
- async generateAnswer(payload) {
2134
+ async generateAnswer(payload, res) {
2135
+ this.cancelOnDisconnect(res, payload.requestId);
2127
2136
  try {
2128
2137
  return await this.graphService.generateAnswer(payload);
2129
2138
  } catch (error) {
@@ -2136,6 +2145,7 @@ exports.GraphController = class _GraphController {
2136
2145
  res.setHeader("Transfer-Encoding", "chunked");
2137
2146
  res.setHeader("Cache-Control", "no-cache");
2138
2147
  res.setHeader("Connection", "keep-alive");
2148
+ this.cancelOnDisconnect(res, payload.requestId);
2139
2149
  try {
2140
2150
  this.logger.debug(`[CONTROLLER] Starting streamAnswer for requestId: ${payload.requestId}`);
2141
2151
  const result = await this.graphService.streamAnswer(payload, (chunk) => {
@@ -2173,6 +2183,19 @@ exports.GraphController = class _GraphController {
2173
2183
  res.end();
2174
2184
  }
2175
2185
  }
2186
+ /**
2187
+ * A caller that hangs up before the answer is done — its own timeout, a
2188
+ * restart, a cancelled parent run — no longer waits for it, so the run is
2189
+ * cancelled: left going, it keeps calling models and tools nobody reads.
2190
+ */
2191
+ cancelOnDisconnect(res, requestId) {
2192
+ if (!requestId) return;
2193
+ res.on("close", () => {
2194
+ if (res.writableFinished) return;
2195
+ this.logger.warn(`[CONTROLLER] Caller disconnected before the answer: cancelling ${requestId}`);
2196
+ this.graphService.cancelGeneration(requestId).catch((error) => this.logger.warn(`[CONTROLLER] Cancel after disconnect failed for ${requestId}: ${error.message}`));
2197
+ });
2198
+ }
2176
2199
  async cancelGeneration(requestId) {
2177
2200
  try {
2178
2201
  await this.graphService.cancelGeneration(requestId);
@@ -2246,9 +2269,13 @@ _ts_decorate8([
2246
2269
  description: "Answer generated"
2247
2270
  }),
2248
2271
  _ts_param4(0, common.Body()),
2272
+ _ts_param4(1, common.Res({
2273
+ passthrough: true
2274
+ })),
2249
2275
  _ts_metadata6("design:type", Function),
2250
2276
  _ts_metadata6("design:paramtypes", [
2251
- typeof IGraphRequestPayload === "undefined" ? Object : IGraphRequestPayload
2277
+ typeof IGraphRequestPayload === "undefined" ? Object : IGraphRequestPayload,
2278
+ typeof express.Response === "undefined" ? Object : express.Response
2252
2279
  ]),
2253
2280
  _ts_metadata6("design:returntype", Promise)
2254
2281
  ], exports.GraphController.prototype, "generateAnswer", null);
@@ -7029,7 +7056,7 @@ exports.McpRuntimeHttpClient = class _McpRuntimeHttpClient {
7029
7056
  /**
7030
7057
  * Execute a tool by name with given arguments
7031
7058
  */
7032
- async executeTool(name, args, context, mcpServers) {
7059
+ async executeTool(name, args, context, mcpServers, signal) {
7033
7060
  try {
7034
7061
  this.logger.debug(`Executing tool: ${name} with args:`, args);
7035
7062
  const payload = {
@@ -7042,7 +7069,9 @@ exports.McpRuntimeHttpClient = class _McpRuntimeHttpClient {
7042
7069
  if (mcpServers && mcpServers.length > 0) {
7043
7070
  payload.mcpServers = mcpServers;
7044
7071
  }
7045
- const response = await this.httpClient.post("/tools/execute", payload);
7072
+ const response = signal ? await this.httpClient.post("/tools/execute", payload, {
7073
+ signal
7074
+ }) : await this.httpClient.post("/tools/execute", payload);
7046
7075
  this.logger.log(`Tool ${name} executed successfully`);
7047
7076
  return response.data;
7048
7077
  } catch (error) {
@@ -7110,7 +7139,7 @@ exports.McpRuntimeHttpClient = class _McpRuntimeHttpClient {
7110
7139
  toolName
7111
7140
  ]
7112
7141
  }, JSON.stringify(enrichedArgs), parsedConfig.runId, void 0, parsedConfig.tags, parsedConfig.metadata, toolName);
7113
- const result = await this.executeTool(toolName, enrichedArgs, executionContext, mcpServers);
7142
+ const result = await this.executeTool(toolName, enrichedArgs, executionContext, mcpServers, config?.signal);
7114
7143
  const content = result.success ? JSON.stringify(result) : result.error || JSON.stringify(result);
7115
7144
  await runManager?.handleToolEnd(content);
7116
7145
  return {
@@ -7152,8 +7181,8 @@ function toolSearchTerms(text) {
7152
7181
  }
7153
7182
  __name(toolSearchTerms, "toolSearchTerms");
7154
7183
  function schemaParams(schema) {
7155
- const json = schema;
7156
- const properties = json?.properties ?? json?.shape ?? {};
7184
+ const json2 = schema;
7185
+ const properties = json2?.properties ?? json2?.shape ?? {};
7157
7186
  return Object.entries(properties).flatMap(([name, property]) => property?.description ? [
7158
7187
  name,
7159
7188
  property.description
@@ -8868,8 +8897,8 @@ var IV_LENGTH = 16;
8868
8897
  function encryptTokens(tokens, key) {
8869
8898
  const iv = crypto__namespace.randomBytes(IV_LENGTH);
8870
8899
  const cipher = crypto__namespace.createCipheriv(ALGORITHM, Buffer.from(key), iv);
8871
- const json = JSON.stringify(tokens);
8872
- let encrypted = cipher.update(json, "utf8", "hex");
8900
+ const json2 = JSON.stringify(tokens);
8901
+ let encrypted = cipher.update(json2, "utf8", "hex");
8873
8902
  encrypted += cipher.final("hex");
8874
8903
  return iv.toString("hex") + ":" + encrypted;
8875
8904
  }