@flutchai/flutch-sdk 0.2.8 → 0.2.10

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.js CHANGED
@@ -9,6 +9,7 @@ import * as net from 'net';
9
9
  import { ConfigService, ConfigModule } from '@nestjs/config';
10
10
  import mongoose from 'mongoose';
11
11
  import { MongoDBSaver } from '@langchain/langgraph-checkpoint-mongodb';
12
+ import * as crypto from 'crypto';
12
13
  import { createHash, randomUUID, randomBytes } from 'crypto';
13
14
  import { Registry, collectDefaultMetrics, Counter, Histogram, Gauge } from 'prom-client';
14
15
  import { ToolMessage, AIMessage } from '@langchain/core/messages';
@@ -20,7 +21,7 @@ import axios2 from 'axios';
20
21
  import { zodToJsonSchema } from 'zod-to-json-schema';
21
22
  import { parseCallbackConfigArg, CallbackManager } from '@langchain/core/callbacks/manager';
22
23
  import { ChatOpenAI, OpenAIEmbeddings } from '@langchain/openai';
23
- import { AzureChatOpenAI } from '@langchain/azure-openai';
24
+ import { ChatBedrockConverse } from '@langchain/aws';
24
25
  import { ChatAnthropic } from '@langchain/anthropic';
25
26
  import { ChatCohere, CohereRerank } from '@langchain/cohere';
26
27
  import { BaseDocumentCompressor } from '@langchain/core/retrievers/document_compressors';
@@ -1335,10 +1336,10 @@ var AbstractGraphBuilder = class {
1335
1336
  return null;
1336
1337
  }
1337
1338
  try {
1338
- const fs2 = await import('fs/promises');
1339
- const path3 = await import('path');
1340
- const manifestFullPath = path3.resolve(this.manifestPath);
1341
- const manifestContent = await fs2.readFile(manifestFullPath, "utf-8");
1339
+ const fs3 = await import('fs/promises');
1340
+ const path4 = await import('path');
1341
+ const manifestFullPath = path4.resolve(this.manifestPath);
1342
+ const manifestContent = await fs3.readFile(manifestFullPath, "utf-8");
1342
1343
  const manifest = JSON.parse(manifestContent);
1343
1344
  this.manifest = manifest;
1344
1345
  return manifest;
@@ -1357,10 +1358,10 @@ var AbstractGraphBuilder = class {
1357
1358
  return null;
1358
1359
  }
1359
1360
  try {
1360
- const fs2 = __require("fs");
1361
- const path3 = __require("path");
1362
- const manifestFullPath = path3.resolve(this.manifestPath);
1363
- const manifestContent = fs2.readFileSync(manifestFullPath, "utf-8");
1361
+ const fs3 = __require("fs");
1362
+ const path4 = __require("path");
1363
+ const manifestFullPath = path4.resolve(this.manifestPath);
1364
+ const manifestContent = fs3.readFileSync(manifestFullPath, "utf-8");
1364
1365
  const manifest = JSON.parse(manifestContent);
1365
1366
  this.manifest = manifest;
1366
1367
  return manifest;
@@ -1400,12 +1401,12 @@ var AbstractGraphBuilder = class {
1400
1401
  let configSchema = null;
1401
1402
  if (versionConfig.configSchemaPath) {
1402
1403
  try {
1403
- const fs2 = await import('fs/promises');
1404
+ const fs3 = await import('fs/promises');
1404
1405
  const schemaPath = path2.resolve(
1405
1406
  process.cwd(),
1406
1407
  versionConfig.configSchemaPath
1407
1408
  );
1408
- const schemaContent = await fs2.readFile(schemaPath, "utf-8");
1409
+ const schemaContent = await fs3.readFile(schemaPath, "utf-8");
1409
1410
  const schemaData = JSON.parse(schemaContent);
1410
1411
  configSchema = schemaData.schema;
1411
1412
  } catch (error) {
@@ -6390,10 +6391,6 @@ McpRuntimeHttpClient = __decorateClass([
6390
6391
 
6391
6392
  // src/models/enums.ts
6392
6393
  var ModelProvider = /* @__PURE__ */ ((ModelProvider2) => {
6393
- ModelProvider2["FLUTCH"] = "flutch";
6394
- ModelProvider2["FLUTCH_MISTRAL"] = "flutch-mistral";
6395
- ModelProvider2["FLUTCH_OPENAI"] = "flutch-openai";
6396
- ModelProvider2["FLUTCH_ANTHROPIC"] = "flutch-anthropic";
6397
6394
  ModelProvider2["MISTRAL"] = "mistral";
6398
6395
  ModelProvider2["OPENAI"] = "openai";
6399
6396
  ModelProvider2["ANTHROPIC"] = "anthropic";
@@ -6436,6 +6433,25 @@ function generateModelCacheKey(modelId, temperature, maxTokens, toolsConfig) {
6436
6433
  }
6437
6434
  return parts.join(":");
6438
6435
  }
6436
+ function buildOpenAIModelConfig(modelName, temperature, maxTokens, apiToken) {
6437
+ if (isReasoningModel(modelName)) {
6438
+ return {
6439
+ modelName,
6440
+ temperature: 1,
6441
+ // Reasoning models only support temperature=1
6442
+ maxCompletionTokens: maxTokens,
6443
+ streaming: true,
6444
+ openAIApiKey: apiToken
6445
+ };
6446
+ }
6447
+ return {
6448
+ modelName,
6449
+ temperature,
6450
+ maxTokens,
6451
+ streaming: true,
6452
+ openAIApiKey: apiToken
6453
+ };
6454
+ }
6439
6455
  var VoyageAIRerank = class extends BaseDocumentCompressor {
6440
6456
  apiKey;
6441
6457
  model;
@@ -6487,235 +6503,10 @@ var VoyageAIRerank = class extends BaseDocumentCompressor {
6487
6503
  }
6488
6504
  }
6489
6505
  };
6490
- function patchChatOpenAIForGPT5() {
6491
- const logger2 = new Logger("ModelInitializer.Patch");
6492
- logger2.warn(
6493
- `TEMPORARY WORKAROUND: Applying monkey patch for GPT-5 support in LangChain. Fixes: max_tokens->max_completion_tokens, temperature->1. This patch will be removed once LangChain officially supports GPT-5 models.`
6494
- );
6495
- const prototypes = [
6496
- ChatOpenAI.prototype,
6497
- AzureChatOpenAI.prototype
6498
- ];
6499
- prototypes.forEach((prototype, index) => {
6500
- const modelName = index === 0 ? "ChatOpenAI" : "AzureChatOpenAI";
6501
- logger2.warn(`Patching ${modelName} for GPT-5 support`);
6502
- const originalInvocationParams = prototype.invocationParams;
6503
- if (originalInvocationParams) {
6504
- prototype.invocationParams = function(options) {
6505
- const params = originalInvocationParams.call(this, options);
6506
- if (params.model && (params.model.includes("gpt-5") || /^gpt-(5|6|7|8|9)/.test(params.model))) {
6507
- if (params.max_tokens !== void 0) {
6508
- params.max_completion_tokens = params.max_tokens;
6509
- delete params.max_tokens;
6510
- }
6511
- if (params.max_output_tokens !== void 0 && !params.max_completion_tokens) {
6512
- params.max_completion_tokens = params.max_output_tokens;
6513
- delete params.max_output_tokens;
6514
- }
6515
- const originalTemperature = params.temperature;
6516
- if (params.temperature !== void 0 && params.temperature !== 1) {
6517
- params.temperature = 1;
6518
- logger2.debug(
6519
- `Fixed temperature for ${params.model}: ${originalTemperature} -> 1 (GPT-5 models only support temperature=1)`
6520
- );
6521
- }
6522
- }
6523
- if (params.model && (params.model.includes("gpt-5") || /^gpt-(5|6|7|8|9)/.test(params.model))) {
6524
- if (!params.stream_options) {
6525
- params.stream_options = { include_usage: true };
6526
- logger2.warn(
6527
- `[GPT-5 PATCH] Added stream_options.include_usage=true for ${params.model}`
6528
- );
6529
- } else if (params.stream_options.include_usage !== true) {
6530
- params.stream_options.include_usage = true;
6531
- logger2.warn(
6532
- `[GPT-5 PATCH] Updated stream_options.include_usage=true for ${params.model}`
6533
- );
6534
- }
6535
- }
6536
- return params;
6537
- };
6538
- logger2.warn(
6539
- `Successfully patched ${modelName}.invocationParams for GPT-5 support (TEMPORARY WORKAROUND)`
6540
- );
6541
- } else {
6542
- logger2.warn(
6543
- `Could not find invocationParams method to patch in ${modelName}`
6544
- );
6545
- }
6546
- const originalCompletionWithRetry = prototype.completionWithRetry;
6547
- if (originalCompletionWithRetry) {
6548
- prototype.completionWithRetry = async function(request, options) {
6549
- if (request?.model && (request.model.includes("gpt-5") || /^gpt-(5|6|7|8|9)/.test(request.model))) {
6550
- let hasChanges = false;
6551
- if (request.max_tokens !== void 0) {
6552
- request.max_completion_tokens = request.max_tokens;
6553
- delete request.max_tokens;
6554
- hasChanges = true;
6555
- }
6556
- if (request.temperature !== void 0 && request.temperature !== 1) {
6557
- const originalTemp = request.temperature;
6558
- request.temperature = 1;
6559
- logger2.debug(
6560
- `Fixed temperature in completionWithRetry for ${request.model}: ${originalTemp} -> 1`
6561
- );
6562
- hasChanges = true;
6563
- }
6564
- if (!request.stream_options) {
6565
- request.stream_options = { include_usage: true };
6566
- logger2.debug(
6567
- `Added stream_options.include_usage=true in completionWithRetry for ${request.model}`
6568
- );
6569
- hasChanges = true;
6570
- } else if (request.stream_options.include_usage !== true) {
6571
- request.stream_options.include_usage = true;
6572
- logger2.debug(
6573
- `Updated stream_options.include_usage=true in completionWithRetry for ${request.model}`
6574
- );
6575
- hasChanges = true;
6576
- }
6577
- if (hasChanges) {
6578
- logger2.debug(
6579
- `Fixed request params in completionWithRetry for ${request.model}`
6580
- );
6581
- }
6582
- }
6583
- const result = await originalCompletionWithRetry.call(
6584
- this,
6585
- request,
6586
- options
6587
- );
6588
- if (request?.model && (request.model.includes("gpt-5") || /^gpt-(5|6|7|8|9)/.test(request.model))) {
6589
- logger2.warn(
6590
- `[GPT-5 PATCH] Azure OpenAI Response for ${request.model}:`
6591
- );
6592
- logger2.warn(`Response keys: ${Object.keys(result || {}).join(", ")}`);
6593
- if (result?.usage) {
6594
- logger2.warn(
6595
- `Usage found: ${JSON.stringify(result.usage, null, 2)}`
6596
- );
6597
- } else {
6598
- logger2.warn(`No usage found in response`);
6599
- }
6600
- if (result?.choices && result.choices[0]) {
6601
- logger2.warn(
6602
- `First choice keys: ${Object.keys(result.choices[0]).join(", ")}`
6603
- );
6604
- }
6605
- }
6606
- return result;
6607
- };
6608
- logger2.warn(
6609
- `Successfully patched ${modelName}.completionWithRetry for GPT-5 support (TEMPORARY WORKAROUND)`
6610
- );
6611
- }
6612
- const originalIsReasoningModel = prototype.isReasoningModel;
6613
- if (originalIsReasoningModel) {
6614
- prototype.isReasoningModel = function() {
6615
- const model = this.modelName || this.model || this.lc_kwargs?.modelName;
6616
- const isReasoning = /^o\d/.test(model) || model.includes("gpt-5") || /^gpt-(6|7|8|9)/.test(model);
6617
- const originalResult = originalIsReasoningModel.call(this);
6618
- logger2.warn(
6619
- `[GPT-5 PATCH] isReasoningModel check for "${model}": patched=${isReasoning}, original=${originalResult}, modelName=${this.modelName}, model=${this.model}, lc_kwargs=${JSON.stringify(this.lc_kwargs?.modelName)}`
6620
- );
6621
- return isReasoning;
6622
- };
6623
- logger2.warn(
6624
- `Successfully patched ${modelName}.isReasoningModel for GPT-5+ reasoning models (TEMPORARY WORKAROUND)`
6625
- );
6626
- } else {
6627
- logger2.warn(
6628
- `Could not find isReasoningModel method to patch in ${modelName}`
6629
- );
6630
- }
6631
- const originalInvoke = prototype.invoke;
6632
- if (originalInvoke) {
6633
- prototype.invoke = async function(...args) {
6634
- const model = this.modelName || this.model || this.lc_kwargs?.modelName;
6635
- if (model && model.includes("gpt-5")) {
6636
- logger2.warn(`[GPT-5 PATCH] Starting invoke for ${model}`);
6637
- if (args[1]) {
6638
- const config = args[1];
6639
- logger2.warn(
6640
- `[GPT-5 PATCH] Invoke config keys: ${Object.keys(config || {}).join(", ")}`
6641
- );
6642
- if (config.tools) {
6643
- logger2.warn(
6644
- `[GPT-5 PATCH] Tools in config: ${config.tools.length} tools`
6645
- );
6646
- }
6647
- }
6648
- const boundTools = this.bound || this.boundTools || this.tools;
6649
- if (boundTools) {
6650
- logger2.warn(
6651
- `[GPT-5 PATCH] Model has bound tools: ${Array.isArray(boundTools) ? boundTools.length : "yes"}`
6652
- );
6653
- } else {
6654
- logger2.warn(`[GPT-5 PATCH] Model has NO bound tools`);
6655
- }
6656
- }
6657
- let result;
6658
- try {
6659
- result = await originalInvoke.apply(this, args);
6660
- } catch (error) {
6661
- if (model && model.includes("gpt-5")) {
6662
- logger2.error(
6663
- `[GPT-5 PATCH] Azure OpenAI invoke failed for ${model}:`,
6664
- {
6665
- errorMessage: error instanceof Error ? error.message : String(error),
6666
- errorStack: error instanceof Error ? error.stack : void 0,
6667
- errorType: error?.constructor?.name,
6668
- args: args.length,
6669
- hasConfig: !!args[1],
6670
- configKeys: args[1] ? Object.keys(args[1] || {}) : [],
6671
- tools: args[1]?.tools?.length || 0
6672
- }
6673
- );
6674
- }
6675
- throw error;
6676
- }
6677
- if (model && model.includes("gpt-5")) {
6678
- logger2.warn(`[GPT-5 PATCH] Azure OpenAI invoke result for ${model}:`);
6679
- logger2.warn(`Result keys: ${Object.keys(result || {}).join(", ")}`);
6680
- if (result?.usage_metadata || result?.usageMetadata) {
6681
- const usage = result.usage_metadata || result.usageMetadata;
6682
- logger2.warn(
6683
- `Usage metadata found: ${JSON.stringify(usage, null, 2)}`
6684
- );
6685
- }
6686
- if (result?.response_metadata || result?.responseMetadata) {
6687
- const responseMetadata = result.response_metadata || result.responseMetadata;
6688
- logger2.warn(
6689
- `Response metadata found: ${JSON.stringify(responseMetadata, null, 2)}`
6690
- );
6691
- if (!result.usage_metadata && responseMetadata?.estimatedTokenUsage) {
6692
- const estimatedUsage = responseMetadata.estimatedTokenUsage;
6693
- result.usage_metadata = {
6694
- input_tokens: estimatedUsage.promptTokens || 0,
6695
- output_tokens: estimatedUsage.completionTokens || 0,
6696
- total_tokens: estimatedUsage.totalTokens || 0
6697
- };
6698
- logger2.warn(
6699
- `[GPT-5 PATCH] Created usage_metadata from estimatedTokenUsage: ${JSON.stringify(result.usage_metadata, null, 2)}`
6700
- );
6701
- }
6702
- }
6703
- if (!result?.usage_metadata && !result?.usageMetadata) {
6704
- logger2.warn(`No usage_metadata found in invoke result`);
6705
- }
6706
- }
6707
- return result;
6708
- };
6709
- logger2.warn(
6710
- `Successfully patched ${modelName}.invoke for GPT-5 response logging (TEMPORARY WORKAROUND)`
6711
- );
6712
- }
6713
- });
6714
- }
6715
- patchChatOpenAIForGPT5();
6716
6506
  var ModelInitializer = class _ModelInitializer {
6717
- constructor(configFetcher, logger2) {
6507
+ constructor(configFetcher, logger2, apiKeyResolver) {
6718
6508
  this.configFetcher = configFetcher;
6509
+ this.apiKeyResolver = apiKeyResolver;
6719
6510
  this.logger = logger2 || new Logger(_ModelInitializer.name);
6720
6511
  }
6721
6512
  logger;
@@ -6723,9 +6514,30 @@ var ModelInitializer = class _ModelInitializer {
6723
6514
  modelConfigCache = /* @__PURE__ */ new Map();
6724
6515
  // Cache for model instances to avoid recreating identical models
6725
6516
  modelInstanceCache = /* @__PURE__ */ new Map();
6517
+ static DEFAULT_ENV_MAP = {
6518
+ ["openai" /* OPENAI */]: "OPENAI_API_KEY",
6519
+ ["anthropic" /* ANTHROPIC */]: "ANTHROPIC_API_KEY",
6520
+ ["mistral" /* MISTRAL */]: "MISTRAL_API_KEY",
6521
+ ["cohere" /* COHERE */]: "COHERE_API_KEY",
6522
+ ["voyageai" /* VOYAGEAI */]: "VOYAGEAI_API_KEY"
6523
+ };
6726
6524
  /**
6727
- * Generate cache key for model instances based on configuration
6525
+ * Resolve API key for a provider.
6526
+ * Uses custom resolver if provided, falls back to process.env.
6728
6527
  */
6528
+ resolveApiKey(provider) {
6529
+ if (this.apiKeyResolver) {
6530
+ return this.apiKeyResolver(provider);
6531
+ }
6532
+ const envVar = _ModelInitializer.DEFAULT_ENV_MAP[provider];
6533
+ return envVar ? process.env[envVar] : void 0;
6534
+ }
6535
+ /**
6536
+ * Resolve AWS region for Bedrock.
6537
+ */
6538
+ resolveBedrockRegion() {
6539
+ return process.env.BEDROCK_AWS_REGION || process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || "us-east-1";
6540
+ }
6729
6541
  /**
6730
6542
  * Generate hash from toolsConfig for cache key
6731
6543
  * Uses MD5 hash to create short, unique identifier
@@ -6746,29 +6558,7 @@ var ModelInitializer = class _ModelInitializer {
6746
6558
  config.toolsConfig
6747
6559
  );
6748
6560
  }
6749
- /**
6750
- * TEMPORARY SOLUTION for compatibility with new OpenAI models
6751
- *
6752
- * OpenAI changed the API for new models (gpt-5, o-series):
6753
- * - Old models (gpt-3.5, gpt-4, gpt-4o): use maxTokens, support custom temperature
6754
- * - New reasoning models (gpt-5, gpt-o1, gpt-o3, gpt-o4): use maxCompletionTokens, only temperature = 1
6755
- *
6756
- * Patch fixes:
6757
- * 1. max_tokens -> max_completion_tokens for reasoning GPT-5+ models
6758
- * 2. temperature -> 1 (forced) for reasoning GPT-5+ models
6759
- *
6760
- * @param modelName - OpenAI model name
6761
- * @returns true if model requires maxCompletionTokens and temperature = 1
6762
- */
6763
- requiresMaxCompletionTokens(modelName) {
6764
- const requiresNew = isReasoningModel(modelName);
6765
- this.logger.debug(`Checking token parameter for model "${modelName}"`, {
6766
- modelName,
6767
- requiresMaxCompletionTokens: requiresNew
6768
- });
6769
- return requiresNew;
6770
- }
6771
- // Chat model creators (inherit from original LLMInitializer)
6561
+ // Chat model creators
6772
6562
  chatModelCreators = {
6773
6563
  ["openai" /* OPENAI */]: ({
6774
6564
  modelName,
@@ -6776,36 +6566,13 @@ var ModelInitializer = class _ModelInitializer {
6776
6566
  defaultMaxTokens,
6777
6567
  apiToken
6778
6568
  }) => {
6779
- if (this.requiresMaxCompletionTokens(modelName)) {
6780
- const fixedTemperature = 1;
6781
- const config = {
6782
- modelName,
6783
- temperature: fixedTemperature,
6784
- // Force set to 1
6785
- maxCompletionTokens: defaultMaxTokens,
6786
- // Only this parameter for new models
6787
- streaming: true,
6788
- openAIApiKey: apiToken || process.env.OPENAI_API_KEY
6789
- };
6790
- if (defaultTemperature !== 1) {
6791
- this.logger.debug(
6792
- `Fixed temperature for GPT-5+ model ${modelName}: ${defaultTemperature} -> 1 (GPT-5+ models only support temperature=1)`
6793
- );
6794
- }
6795
- const chatOpenAI = new ChatOpenAI(config);
6796
- return chatOpenAI;
6797
- } else {
6798
- const config = {
6799
- modelName,
6800
- temperature: defaultTemperature,
6801
- maxTokens: defaultMaxTokens,
6802
- // Only this parameter for legacy models
6803
- streaming: true,
6804
- openAIApiKey: apiToken || process.env.OPENAI_API_KEY
6805
- };
6806
- const chatOpenAI = new ChatOpenAI(config);
6807
- return chatOpenAI;
6808
- }
6569
+ const config = buildOpenAIModelConfig(
6570
+ modelName,
6571
+ defaultTemperature,
6572
+ defaultMaxTokens,
6573
+ apiToken || this.resolveApiKey("openai" /* OPENAI */) || ""
6574
+ );
6575
+ return new ChatOpenAI(config);
6809
6576
  },
6810
6577
  ["anthropic" /* ANTHROPIC */]: ({
6811
6578
  modelName,
@@ -6816,7 +6583,7 @@ var ModelInitializer = class _ModelInitializer {
6816
6583
  modelName,
6817
6584
  temperature: defaultTemperature,
6818
6585
  maxTokens: defaultMaxTokens,
6819
- anthropicApiKey: apiToken || process.env.ANTHROPIC_API_KEY
6586
+ anthropicApiKey: apiToken || this.resolveApiKey("anthropic" /* ANTHROPIC */)
6820
6587
  }),
6821
6588
  ["cohere" /* COHERE */]: ({
6822
6589
  modelName,
@@ -6826,8 +6593,7 @@ var ModelInitializer = class _ModelInitializer {
6826
6593
  }) => new ChatCohere({
6827
6594
  model: modelName,
6828
6595
  temperature: defaultTemperature,
6829
- // Cohere uses maxTokens via max_tokens parameter, but it's not supported in ChatCohere API
6830
- apiKey: apiToken || process.env.COHERE_API_KEY
6596
+ apiKey: apiToken || this.resolveApiKey("cohere" /* COHERE */)
6831
6597
  }),
6832
6598
  ["mistral" /* MISTRAL */]: ({
6833
6599
  modelName,
@@ -6838,91 +6604,8 @@ var ModelInitializer = class _ModelInitializer {
6838
6604
  model: modelName,
6839
6605
  temperature: defaultTemperature,
6840
6606
  maxTokens: defaultMaxTokens,
6841
- apiKey: apiToken || process.env.MISTRAL_API_KEY
6607
+ apiKey: apiToken || this.resolveApiKey("mistral" /* MISTRAL */)
6842
6608
  }),
6843
- // AWS Bedrock support removed - use Anthropic or OpenAI directly instead
6844
- ["flutch-openai" /* FLUTCH_OPENAI */]: ({
6845
- modelName,
6846
- defaultTemperature,
6847
- defaultMaxTokens,
6848
- apiToken
6849
- }) => {
6850
- if (this.requiresMaxCompletionTokens(modelName)) {
6851
- const fixedTemperature = 1;
6852
- const config = {
6853
- modelName,
6854
- temperature: fixedTemperature,
6855
- // Force set to 1
6856
- maxCompletionTokens: defaultMaxTokens,
6857
- // Only this parameter for new models
6858
- streaming: true,
6859
- openAIApiKey: apiToken || process.env.OPENAI_API_KEY
6860
- };
6861
- if (defaultTemperature !== 1) {
6862
- this.logger.debug(
6863
- `Fixed temperature for FLUTCH GPT-5+ model ${modelName}: ${defaultTemperature} -> 1 (GPT-5+ models only support temperature=1)`
6864
- );
6865
- }
6866
- this.logger.debug(`Creating FLUTCH GPT-5+ model with config`, {
6867
- modelName,
6868
- maxCompletionTokens: defaultMaxTokens,
6869
- temperature: fixedTemperature,
6870
- originalTemperature: defaultTemperature,
6871
- hasApiKey: !!config.openAIApiKey
6872
- });
6873
- const chatOpenAI = new ChatOpenAI(config);
6874
- this.logger.debug(`FLUTCH ChatOpenAI GPT-5+ instance created`, {
6875
- modelName,
6876
- // Use modelName from parameters
6877
- maxTokens: chatOpenAI.maxTokens,
6878
- maxCompletionTokens: chatOpenAI.maxCompletionTokens,
6879
- temperature: chatOpenAI.temperature,
6880
- streaming: chatOpenAI.streaming,
6881
- // Try to get internal parameters
6882
- clientConfig: chatOpenAI.clientConfig,
6883
- kwargs: chatOpenAI.kwargs
6884
- });
6885
- return chatOpenAI;
6886
- } else {
6887
- const config = {
6888
- modelName,
6889
- temperature: defaultTemperature,
6890
- maxTokens: defaultMaxTokens,
6891
- // Only this parameter for legacy models
6892
- streaming: true,
6893
- openAIApiKey: apiToken || process.env.OPENAI_API_KEY
6894
- };
6895
- this.logger.debug(`Creating FLUTCH legacy model with config`, {
6896
- modelName,
6897
- maxTokens: defaultMaxTokens,
6898
- temperature: defaultTemperature,
6899
- hasApiKey: !!config.openAIApiKey
6900
- });
6901
- const chatOpenAI = new ChatOpenAI(config);
6902
- this.logger.debug(`FLUTCH ChatOpenAI legacy instance created`, {
6903
- modelName,
6904
- // Use modelName from parameters
6905
- maxTokens: chatOpenAI.maxTokens,
6906
- maxCompletionTokens: chatOpenAI.maxCompletionTokens,
6907
- temperature: chatOpenAI.temperature,
6908
- streaming: chatOpenAI.streaming,
6909
- // Try to get internal parameters
6910
- clientConfig: chatOpenAI.clientConfig,
6911
- kwargs: chatOpenAI.kwargs
6912
- });
6913
- return chatOpenAI;
6914
- }
6915
- },
6916
- // Other providers not yet implemented for chat
6917
- ["flutch" /* FLUTCH */]: () => {
6918
- throw new Error("Flutch chat models not implemented");
6919
- },
6920
- ["flutch-mistral" /* FLUTCH_MISTRAL */]: () => {
6921
- throw new Error("Flutch Mistral chat models not implemented");
6922
- },
6923
- ["flutch-anthropic" /* FLUTCH_ANTHROPIC */]: () => {
6924
- throw new Error("Flutch Anthropic chat models not implemented");
6925
- },
6926
6609
  ["voyageai" /* VOYAGEAI */]: () => {
6927
6610
  throw new Error("VoyageAI chat models not implemented");
6928
6611
  }
@@ -6931,14 +6614,14 @@ var ModelInitializer = class _ModelInitializer {
6931
6614
  rerankModelCreators = {
6932
6615
  ["cohere" /* COHERE */]: ({ modelName, apiToken, maxDocuments }) => {
6933
6616
  return new CohereRerank({
6934
- apiKey: apiToken || process.env.COHERE_API_KEY,
6617
+ apiKey: apiToken || this.resolveApiKey("cohere" /* COHERE */),
6935
6618
  model: modelName,
6936
6619
  topN: maxDocuments || 20
6937
6620
  });
6938
6621
  },
6939
6622
  ["voyageai" /* VOYAGEAI */]: ({ modelName, apiToken, maxDocuments }) => {
6940
6623
  return new VoyageAIRerank({
6941
- apiKey: apiToken || process.env.VOYAGEAI_API_KEY,
6624
+ apiKey: apiToken || this.resolveApiKey("voyageai" /* VOYAGEAI */),
6942
6625
  model: modelName,
6943
6626
  topN: maxDocuments || 20
6944
6627
  });
@@ -6947,27 +6630,19 @@ var ModelInitializer = class _ModelInitializer {
6947
6630
  ["openai" /* OPENAI */]: void 0,
6948
6631
  ["anthropic" /* ANTHROPIC */]: void 0,
6949
6632
  ["mistral" /* MISTRAL */]: void 0,
6950
- ["aws" /* AWS */]: void 0,
6951
- ["flutch" /* FLUTCH */]: void 0,
6952
- ["flutch-mistral" /* FLUTCH_MISTRAL */]: void 0,
6953
- ["flutch-openai" /* FLUTCH_OPENAI */]: void 0,
6954
- ["flutch-anthropic" /* FLUTCH_ANTHROPIC */]: void 0
6633
+ ["aws" /* AWS */]: void 0
6955
6634
  };
6956
6635
  // Embedding model creators
6957
6636
  embeddingModelCreators = {
6958
6637
  ["openai" /* OPENAI */]: ({ modelName, apiToken }) => new OpenAIEmbeddings({
6959
6638
  model: modelName,
6960
- apiKey: apiToken || process.env.OPENAI_API_KEY
6639
+ apiKey: apiToken || this.resolveApiKey("openai" /* OPENAI */)
6961
6640
  }),
6962
6641
  // Other providers not yet implemented for embeddings
6963
6642
  ["anthropic" /* ANTHROPIC */]: void 0,
6964
6643
  ["cohere" /* COHERE */]: void 0,
6965
6644
  ["mistral" /* MISTRAL */]: void 0,
6966
6645
  ["aws" /* AWS */]: void 0,
6967
- ["flutch" /* FLUTCH */]: void 0,
6968
- ["flutch-mistral" /* FLUTCH_MISTRAL */]: void 0,
6969
- ["flutch-openai" /* FLUTCH_OPENAI */]: void 0,
6970
- ["flutch-anthropic" /* FLUTCH_ANTHROPIC */]: void 0,
6971
6646
  ["voyageai" /* VOYAGEAI */]: void 0
6972
6647
  };
6973
6648
  async initializeChatModel(config) {
@@ -6983,12 +6658,6 @@ var ModelInitializer = class _ModelInitializer {
6983
6658
  `Model ${config.modelId} is not a chat model (type: ${modelConfig.modelType})`
6984
6659
  );
6985
6660
  }
6986
- const creator = this.chatModelCreators[modelConfig.provider];
6987
- if (!creator) {
6988
- throw new Error(
6989
- `Chat models not supported for provider: ${modelConfig.provider}`
6990
- );
6991
- }
6992
6661
  const finalConfig = {
6993
6662
  ...modelConfig,
6994
6663
  defaultTemperature: Number(
@@ -6999,7 +6668,27 @@ var ModelInitializer = class _ModelInitializer {
6999
6668
  )
7000
6669
  };
7001
6670
  this.logger.debug(`Creating new chat model instance: ${cacheKey}`);
7002
- const model = creator(finalConfig);
6671
+ let model;
6672
+ if (finalConfig.useBedrock && finalConfig.bedrockModelId) {
6673
+ this.logger.debug(
6674
+ `Using Bedrock for model ${finalConfig.modelName}, bedrockModelId: ${finalConfig.bedrockModelId}`
6675
+ );
6676
+ model = new ChatBedrockConverse({
6677
+ model: finalConfig.bedrockModelId,
6678
+ region: this.resolveBedrockRegion(),
6679
+ temperature: finalConfig.defaultTemperature,
6680
+ maxTokens: finalConfig.defaultMaxTokens,
6681
+ streaming: true
6682
+ });
6683
+ } else {
6684
+ const creator = this.chatModelCreators[modelConfig.provider];
6685
+ if (!creator) {
6686
+ throw new Error(
6687
+ `Chat models not supported for provider: ${modelConfig.provider}`
6688
+ );
6689
+ }
6690
+ model = creator(finalConfig);
6691
+ }
7003
6692
  model.metadata = {
7004
6693
  ...model.metadata,
7005
6694
  modelId: config.modelId
@@ -7607,7 +7296,206 @@ var StaticDiscovery = class {
7607
7296
  StaticDiscovery = __decorateClass([
7608
7297
  Injectable()
7609
7298
  ], StaticDiscovery);
7299
+ var ALGORITHM = "aes-256-cbc";
7300
+ var IV_LENGTH = 16;
7301
+ function encryptTokens(tokens, key) {
7302
+ const iv = crypto.randomBytes(IV_LENGTH);
7303
+ const cipher = crypto.createCipheriv(
7304
+ ALGORITHM,
7305
+ Buffer.from(key),
7306
+ iv
7307
+ );
7308
+ const json = JSON.stringify(tokens);
7309
+ let encrypted = cipher.update(json, "utf8", "hex");
7310
+ encrypted += cipher.final("hex");
7311
+ return iv.toString("hex") + ":" + encrypted;
7312
+ }
7313
+ function decryptTokens(encrypted, key) {
7314
+ const separatorIndex = encrypted.indexOf(":");
7315
+ if (separatorIndex === -1) {
7316
+ throw new Error("Invalid encrypted token format");
7317
+ }
7318
+ const ivHex = encrypted.substring(0, separatorIndex);
7319
+ const encryptedData = encrypted.substring(separatorIndex + 1);
7320
+ const iv = Buffer.from(ivHex, "hex");
7321
+ const decipher = crypto.createDecipheriv(
7322
+ ALGORITHM,
7323
+ Buffer.from(key),
7324
+ iv
7325
+ );
7326
+ let decrypted = decipher.update(encryptedData, "hex", "utf8");
7327
+ decrypted += decipher.final("utf8");
7328
+ return JSON.parse(decrypted);
7329
+ }
7330
+
7331
+ // src/oauth/oauth-token.manager.ts
7332
+ var EXPIRY_BUFFER_MS = 6e4;
7333
+ var OAuthTokenManager = class {
7334
+ store;
7335
+ encryptionKey;
7336
+ cache = /* @__PURE__ */ new Map();
7337
+ constructor(options) {
7338
+ if (!options.encryptionKey || options.encryptionKey.length < 32) {
7339
+ throw new Error(
7340
+ "OAUTH_ENCRYPTION_KEY must be at least 32 characters for AES-256-CBC"
7341
+ );
7342
+ }
7343
+ this.store = options.store;
7344
+ this.encryptionKey = options.encryptionKey;
7345
+ }
7346
+ /**
7347
+ * Get a fresh access token for the given provider.
7348
+ * Returns from cache → store → refresh flow (in that order).
7349
+ */
7350
+ async getAccessToken(config) {
7351
+ const cached = this.cache.get(config.provider);
7352
+ if (cached && cached.expiresAt > Date.now() + EXPIRY_BUFFER_MS) {
7353
+ return cached.token;
7354
+ }
7355
+ const encrypted = await this.store.get(config.provider);
7356
+ if (!encrypted) {
7357
+ throw new Error(
7358
+ `No OAuth tokens found for "${config.provider}". Complete the OAuth consent flow first and call saveTokens().`
7359
+ );
7360
+ }
7361
+ const tokens = decryptTokens(encrypted, this.encryptionKey);
7362
+ if (tokens.expiresAt > Date.now() + EXPIRY_BUFFER_MS) {
7363
+ this.setCache(config.provider, tokens.accessToken, tokens.expiresAt);
7364
+ return tokens.accessToken;
7365
+ }
7366
+ const refreshed = await this.refreshAccessToken(config, tokens.refreshToken);
7367
+ await this.persistTokens(config.provider, refreshed);
7368
+ return refreshed.accessToken;
7369
+ }
7370
+ /**
7371
+ * Store tokens after the initial OAuth consent flow.
7372
+ * Call this once after the user completes the OAuth redirect.
7373
+ */
7374
+ async saveTokens(provider, tokens) {
7375
+ await this.persistTokens(provider, tokens);
7376
+ }
7377
+ /**
7378
+ * Remove all tokens for a provider.
7379
+ */
7380
+ async revokeTokens(provider) {
7381
+ await this.store.delete(provider);
7382
+ this.cache.delete(provider);
7383
+ }
7384
+ /**
7385
+ * Check if tokens exist for a provider (without decrypting).
7386
+ */
7387
+ async hasTokens(provider) {
7388
+ const encrypted = await this.store.get(provider);
7389
+ return encrypted !== null;
7390
+ }
7391
+ async refreshAccessToken(config, refreshToken) {
7392
+ try {
7393
+ const response = await axios2.post(
7394
+ config.tokenUrl,
7395
+ new URLSearchParams({
7396
+ grant_type: "refresh_token",
7397
+ client_id: config.clientId,
7398
+ client_secret: config.clientSecret,
7399
+ refresh_token: refreshToken
7400
+ }).toString(),
7401
+ {
7402
+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
7403
+ timeout: 1e4
7404
+ }
7405
+ );
7406
+ const data = response.data;
7407
+ return {
7408
+ accessToken: data.access_token,
7409
+ // Some providers rotate refresh tokens; keep the old one if not rotated
7410
+ refreshToken: data.refresh_token || refreshToken,
7411
+ expiresAt: Date.now() + (data.expires_in || 3600) * 1e3
7412
+ };
7413
+ } catch (error) {
7414
+ const status = error?.response?.status;
7415
+ const body = error?.response?.data;
7416
+ throw new Error(
7417
+ `OAuth refresh failed for "${config.provider}": ${status || "network error"} ${JSON.stringify(body) || error.message}`
7418
+ );
7419
+ }
7420
+ }
7421
+ async persistTokens(provider, tokens) {
7422
+ const encrypted = encryptTokens(tokens, this.encryptionKey);
7423
+ await this.store.save(provider, encrypted);
7424
+ this.setCache(provider, tokens.accessToken, tokens.expiresAt);
7425
+ }
7426
+ setCache(provider, token, expiresAt) {
7427
+ this.cache.set(provider, { token, expiresAt });
7428
+ }
7429
+ };
7430
+ var FileTokenStore = class {
7431
+ constructor(filePath) {
7432
+ this.filePath = filePath;
7433
+ }
7434
+ async get(provider) {
7435
+ const data = this.readFile();
7436
+ return data[provider] ?? null;
7437
+ }
7438
+ async save(provider, encrypted) {
7439
+ const data = this.readFile();
7440
+ data[provider] = encrypted;
7441
+ this.writeFile(data);
7442
+ }
7443
+ async delete(provider) {
7444
+ const data = this.readFile();
7445
+ delete data[provider];
7446
+ this.writeFile(data);
7447
+ }
7448
+ readFile() {
7449
+ try {
7450
+ if (fs.existsSync(this.filePath)) {
7451
+ const content = fs.readFileSync(this.filePath, "utf8");
7452
+ return JSON.parse(content);
7453
+ }
7454
+ } catch {
7455
+ }
7456
+ return {};
7457
+ }
7458
+ writeFile(data) {
7459
+ const dir = path2.dirname(this.filePath);
7460
+ if (!fs.existsSync(dir)) {
7461
+ fs.mkdirSync(dir, { recursive: true });
7462
+ }
7463
+ fs.writeFileSync(this.filePath, JSON.stringify(data, null, 2), "utf8");
7464
+ }
7465
+ };
7466
+
7467
+ // src/oauth/stores/mongo-token.store.ts
7468
+ var DEFAULT_COLLECTION = "oauth_tokens";
7469
+ var MongoTokenStore = class {
7470
+ constructor(db, collectionName) {
7471
+ this.db = db;
7472
+ this.collectionName = collectionName ?? DEFAULT_COLLECTION;
7473
+ }
7474
+ collectionName;
7475
+ initialized = false;
7476
+ async get(provider) {
7477
+ await this.ensureIndex();
7478
+ const doc = await this.db.collection(this.collectionName).findOne({ provider });
7479
+ return doc?.encrypted ?? null;
7480
+ }
7481
+ async save(provider, encrypted) {
7482
+ await this.ensureIndex();
7483
+ await this.db.collection(this.collectionName).updateOne(
7484
+ { provider },
7485
+ { $set: { provider, encrypted, updatedAt: /* @__PURE__ */ new Date() } },
7486
+ { upsert: true }
7487
+ );
7488
+ }
7489
+ async delete(provider) {
7490
+ await this.db.collection(this.collectionName).deleteOne({ provider });
7491
+ }
7492
+ async ensureIndex() {
7493
+ if (this.initialized) return;
7494
+ await this.db.collection(this.collectionName).createIndex({ provider: 1 }, { unique: true });
7495
+ this.initialized = true;
7496
+ }
7497
+ };
7610
7498
 
7611
- export { AbstractGraphBuilder, AttachmentType, GraphController as BaseGraphServiceController, UniversalGraphModule as BaseGraphServiceModule, BuilderRegistryService, Callback, CallbackACL, CallbackAuditAction, CallbackAuditor, CallbackController, CallbackMetrics, CallbackPatchService, CallbackRateLimiter, CallbackRegistry, CallbackStore, CallbackTokenGuard, ChatFeature, DEFAULT_ATTACHMENT_THRESHOLD, DEFAULT_TRACER_OPTIONS, ENDPOINT_METADATA_KEY, Endpoint, EndpointRegistry, EventProcessor, ExternalGraphBuilder, FileBasedDiscovery, GraphController, GraphEngineFactory, GraphEngineType, GraphManifestSchema, GraphManifestValidator, GraphServiceTokens, GraphTypeUtils, IdempotencyManager, IdempotencyStatus, LangGraphEngine, McpConverter, McpRuntimeHttpClient, McpToolFilter, ModelInitializer, ModelProvider, ModelType, RetrieverSearchType, RetrieverService, SmartCallbackRouter, StaticDiscovery, StreamChannel, TelegramPatchHandler, UIDispatchController, UIEndpoint, UIEndpointsDiscoveryService, UniversalCallbackService, UniversalGraphModule, UniversalGraphService, VersionedGraphService, VoyageAIRerank, WebPatchHandler, WithCallbacks, WithEndpoints, WithUIEndpoints, _internals, bootstrap, clearAttachmentDataStore, createEndpointDescriptors, createGraphAttachment, createMongoClientAdapter, createStaticMessage, dispatchAttachments, executeToolWithAttachments, findCallbackMethod, findEndpointMethod, generateAttachmentSummary, getAttachmentData, getCallbackMetadata, getEndpointMetadata, getUIEndpointClassMetadata, getUIEndpointMethodsMetadata, hasCallbacks, hasUIEndpoints, registerFinanceExampleCallback, registerUIEndpointsFromClass, sanitizeTraceData, storeAttachmentData, traceApiCall };
7499
+ export { AbstractGraphBuilder, AttachmentType, GraphController as BaseGraphServiceController, UniversalGraphModule as BaseGraphServiceModule, BuilderRegistryService, Callback, CallbackACL, CallbackAuditAction, CallbackAuditor, CallbackController, CallbackMetrics, CallbackPatchService, CallbackRateLimiter, CallbackRegistry, CallbackStore, CallbackTokenGuard, ChatFeature, DEFAULT_ATTACHMENT_THRESHOLD, DEFAULT_TRACER_OPTIONS, ENDPOINT_METADATA_KEY, Endpoint, EndpointRegistry, EventProcessor, ExternalGraphBuilder, FileBasedDiscovery, FileTokenStore, GraphController, GraphEngineFactory, GraphEngineType, GraphManifestSchema, GraphManifestValidator, GraphServiceTokens, GraphTypeUtils, IdempotencyManager, IdempotencyStatus, LangGraphEngine, McpConverter, McpRuntimeHttpClient, McpToolFilter, ModelInitializer, ModelProvider, ModelType, MongoTokenStore, OAuthTokenManager, RetrieverSearchType, RetrieverService, SmartCallbackRouter, StaticDiscovery, StreamChannel, TelegramPatchHandler, UIDispatchController, UIEndpoint, UIEndpointsDiscoveryService, UniversalCallbackService, UniversalGraphModule, UniversalGraphService, VersionedGraphService, VoyageAIRerank, WebPatchHandler, WithCallbacks, WithEndpoints, WithUIEndpoints, _internals, bootstrap, clearAttachmentDataStore, createEndpointDescriptors, createGraphAttachment, createMongoClientAdapter, createStaticMessage, decryptTokens, dispatchAttachments, encryptTokens, executeToolWithAttachments, findCallbackMethod, findEndpointMethod, generateAttachmentSummary, getAttachmentData, getCallbackMetadata, getEndpointMetadata, getUIEndpointClassMetadata, getUIEndpointMethodsMetadata, hasCallbacks, hasUIEndpoints, registerFinanceExampleCallback, registerUIEndpointsFromClass, sanitizeTraceData, storeAttachmentData, traceApiCall };
7612
7500
  //# sourceMappingURL=index.js.map
7613
7501
  //# sourceMappingURL=index.js.map