@flutchai/flutch-sdk 0.2.15 → 0.2.16
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 +120 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +57 -31
- package/dist/index.d.ts +57 -31
- package/dist/index.js +114 -32
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -25,6 +25,7 @@ var openai = require('@langchain/openai');
|
|
|
25
25
|
var aws = require('@langchain/aws');
|
|
26
26
|
var anthropic = require('@langchain/anthropic');
|
|
27
27
|
var cohere = require('@langchain/cohere');
|
|
28
|
+
var cohereAi = require('cohere-ai');
|
|
28
29
|
var document_compressors = require('@langchain/core/retrievers/document_compressors');
|
|
29
30
|
var mistralai = require('@langchain/mistralai');
|
|
30
31
|
|
|
@@ -6466,6 +6467,12 @@ function hashToolsConfig(toolsConfig) {
|
|
|
6466
6467
|
const sorted = toolsConfig.map((t) => `${t.toolName}:${t.enabled}:${JSON.stringify(t.config || {})}`).sort().join("|");
|
|
6467
6468
|
return crypto.createHash("md5").update(sorted).digest("hex").slice(0, 16);
|
|
6468
6469
|
}
|
|
6470
|
+
function normalizeToolConfigs(tools) {
|
|
6471
|
+
if (!tools || tools.length === 0) return void 0;
|
|
6472
|
+
return tools.map(
|
|
6473
|
+
(t) => typeof t === "string" ? { toolName: t, enabled: true } : { toolName: t.name, enabled: t.enabled !== false, config: t.config }
|
|
6474
|
+
);
|
|
6475
|
+
}
|
|
6469
6476
|
var DEFAULT_ROUTER_URL = "https://router.flutch.ai";
|
|
6470
6477
|
function resolveRouterURL(baseURL) {
|
|
6471
6478
|
return baseURL ?? process.env.FLUTCH_ROUTER_URL ?? DEFAULT_ROUTER_URL;
|
|
@@ -6508,13 +6515,14 @@ var VoyageAIRerank = class extends document_compressors.BaseDocumentCompressor {
|
|
|
6508
6515
|
model;
|
|
6509
6516
|
topN;
|
|
6510
6517
|
truncation;
|
|
6511
|
-
baseUrl
|
|
6518
|
+
baseUrl;
|
|
6512
6519
|
constructor(config) {
|
|
6513
6520
|
super();
|
|
6514
6521
|
this.apiKey = config.apiKey || process.env.VOYAGEAI_API_KEY || "";
|
|
6515
6522
|
this.model = config.model || "rerank-2";
|
|
6516
6523
|
this.topN = config.topN || 20;
|
|
6517
6524
|
this.truncation = config.truncation ?? true;
|
|
6525
|
+
this.baseUrl = config.baseUrl ? `${config.baseUrl}/v1/rerank` : "https://api.voyageai.com/v1/rerank";
|
|
6518
6526
|
if (!this.apiKey) {
|
|
6519
6527
|
throw new Error(
|
|
6520
6528
|
"VoyageAI API key is required. Set VOYAGEAI_API_KEY environment variable or pass apiKey in config."
|
|
@@ -6645,11 +6653,15 @@ var ModelInitializer = class _ModelInitializer {
|
|
|
6645
6653
|
modelName,
|
|
6646
6654
|
defaultTemperature,
|
|
6647
6655
|
defaultMaxTokens,
|
|
6648
|
-
apiToken
|
|
6656
|
+
apiToken,
|
|
6657
|
+
baseURL
|
|
6649
6658
|
}) => new cohere.ChatCohere({
|
|
6650
6659
|
model: modelName,
|
|
6651
6660
|
temperature: defaultTemperature,
|
|
6652
|
-
|
|
6661
|
+
client: new cohereAi.CohereClient({
|
|
6662
|
+
token: apiToken || this.resolveApiKey("cohere" /* COHERE */),
|
|
6663
|
+
baseUrl: resolveRouterURL(baseURL)
|
|
6664
|
+
})
|
|
6653
6665
|
}),
|
|
6654
6666
|
["mistral" /* MISTRAL */]: ({
|
|
6655
6667
|
modelName,
|
|
@@ -6673,18 +6685,32 @@ var ModelInitializer = class _ModelInitializer {
|
|
|
6673
6685
|
};
|
|
6674
6686
|
// Rerank model creators
|
|
6675
6687
|
rerankModelCreators = {
|
|
6676
|
-
["cohere" /* COHERE */]: ({
|
|
6688
|
+
["cohere" /* COHERE */]: ({
|
|
6689
|
+
modelName,
|
|
6690
|
+
apiToken,
|
|
6691
|
+
maxDocuments,
|
|
6692
|
+
baseURL
|
|
6693
|
+
}) => {
|
|
6677
6694
|
return new cohere.CohereRerank({
|
|
6678
|
-
apiKey: apiToken || this.resolveApiKey("cohere" /* COHERE */),
|
|
6679
6695
|
model: modelName,
|
|
6680
|
-
topN: maxDocuments || 20
|
|
6696
|
+
topN: maxDocuments || 20,
|
|
6697
|
+
client: new cohereAi.CohereClient({
|
|
6698
|
+
token: apiToken || this.resolveApiKey("cohere" /* COHERE */),
|
|
6699
|
+
baseUrl: resolveRouterURL(baseURL)
|
|
6700
|
+
})
|
|
6681
6701
|
});
|
|
6682
6702
|
},
|
|
6683
|
-
["voyageai" /* VOYAGEAI */]: ({
|
|
6703
|
+
["voyageai" /* VOYAGEAI */]: ({
|
|
6704
|
+
modelName,
|
|
6705
|
+
apiToken,
|
|
6706
|
+
maxDocuments,
|
|
6707
|
+
baseURL
|
|
6708
|
+
}) => {
|
|
6684
6709
|
return new VoyageAIRerank({
|
|
6685
6710
|
apiKey: apiToken || this.resolveApiKey("voyageai" /* VOYAGEAI */),
|
|
6686
6711
|
model: modelName,
|
|
6687
|
-
topN: maxDocuments || 20
|
|
6712
|
+
topN: maxDocuments || 20,
|
|
6713
|
+
baseUrl: resolveRouterURL(baseURL)
|
|
6688
6714
|
});
|
|
6689
6715
|
},
|
|
6690
6716
|
// Other providers don't support rerank yet
|
|
@@ -6695,9 +6721,10 @@ var ModelInitializer = class _ModelInitializer {
|
|
|
6695
6721
|
};
|
|
6696
6722
|
// Embedding model creators
|
|
6697
6723
|
embeddingModelCreators = {
|
|
6698
|
-
["openai" /* OPENAI */]: ({ modelName, apiToken }) => new openai.OpenAIEmbeddings({
|
|
6724
|
+
["openai" /* OPENAI */]: ({ modelName, apiToken, baseURL }) => new openai.OpenAIEmbeddings({
|
|
6699
6725
|
model: modelName,
|
|
6700
|
-
apiKey: apiToken || this.resolveApiKey("openai" /* OPENAI */)
|
|
6726
|
+
apiKey: apiToken || this.resolveApiKey("openai" /* OPENAI */),
|
|
6727
|
+
configuration: { baseURL: `${resolveRouterURL(baseURL)}/v1` }
|
|
6701
6728
|
}),
|
|
6702
6729
|
// Other providers not yet implemented for embeddings
|
|
6703
6730
|
["anthropic" /* ANTHROPIC */]: void 0,
|
|
@@ -6706,7 +6733,82 @@ var ModelInitializer = class _ModelInitializer {
|
|
|
6706
6733
|
["aws" /* AWS */]: void 0,
|
|
6707
6734
|
["voyageai" /* VOYAGEAI */]: void 0
|
|
6708
6735
|
};
|
|
6709
|
-
|
|
6736
|
+
// ══════════════════════════════════════════════════════════════
|
|
6737
|
+
// initializeChatModel — overloaded: ModelConfig | ModelByIdConfig
|
|
6738
|
+
// ══════════════════════════════════════════════════════════════
|
|
6739
|
+
async initializeChatModel(config, customTools) {
|
|
6740
|
+
if ("provider" in config && "modelName" in config) {
|
|
6741
|
+
return this.initializeChatModelDirect(config, customTools);
|
|
6742
|
+
}
|
|
6743
|
+
return this.initializeChatModelByIdInternal(config);
|
|
6744
|
+
}
|
|
6745
|
+
/**
|
|
6746
|
+
* Direct initialization by provider + modelName (no DB lookup).
|
|
6747
|
+
*/
|
|
6748
|
+
async initializeChatModelDirect(config, customTools) {
|
|
6749
|
+
const toolsConfig = normalizeToolConfigs(config.tools);
|
|
6750
|
+
const modelIdentifier = `${config.provider}:${config.modelName}`;
|
|
6751
|
+
const cacheKey = generateModelCacheKey(
|
|
6752
|
+
modelIdentifier,
|
|
6753
|
+
config.temperature,
|
|
6754
|
+
config.maxTokens,
|
|
6755
|
+
toolsConfig,
|
|
6756
|
+
config.baseURL
|
|
6757
|
+
);
|
|
6758
|
+
const cached = this.modelInstanceCache.get(cacheKey);
|
|
6759
|
+
if (cached) {
|
|
6760
|
+
this.logger.debug(`Using cached chat model instance: ${cacheKey}`);
|
|
6761
|
+
return cached;
|
|
6762
|
+
}
|
|
6763
|
+
const provider = config.provider;
|
|
6764
|
+
if (!Object.values(ModelProvider).includes(provider)) {
|
|
6765
|
+
throw new Error(
|
|
6766
|
+
`Unknown provider "${provider}". Valid: ${Object.values(ModelProvider).join(", ")}`
|
|
6767
|
+
);
|
|
6768
|
+
}
|
|
6769
|
+
const apiToken = this.resolveApiKey(provider);
|
|
6770
|
+
const temperature = config.temperature ?? 0.7;
|
|
6771
|
+
const maxTokens = config.maxTokens ?? 4096;
|
|
6772
|
+
const modelConfig = {
|
|
6773
|
+
modelId: modelIdentifier,
|
|
6774
|
+
modelName: config.modelName,
|
|
6775
|
+
provider,
|
|
6776
|
+
modelType: "chat" /* CHAT */,
|
|
6777
|
+
defaultTemperature: Number(temperature),
|
|
6778
|
+
defaultMaxTokens: Number(maxTokens),
|
|
6779
|
+
apiToken,
|
|
6780
|
+
requiresApiKey: true,
|
|
6781
|
+
baseURL: config.baseURL
|
|
6782
|
+
};
|
|
6783
|
+
this.logger.debug(
|
|
6784
|
+
`Creating chat model: ${modelIdentifier} (apiKeyResolved=${!!apiToken})`
|
|
6785
|
+
);
|
|
6786
|
+
const creator = this.chatModelCreators[provider];
|
|
6787
|
+
if (!creator) {
|
|
6788
|
+
throw new Error(`Chat models not supported for provider: ${provider}`);
|
|
6789
|
+
}
|
|
6790
|
+
const model = creator(modelConfig);
|
|
6791
|
+
model.metadata = {
|
|
6792
|
+
...model.metadata,
|
|
6793
|
+
modelId: modelIdentifier
|
|
6794
|
+
};
|
|
6795
|
+
if (toolsConfig || customTools) {
|
|
6796
|
+
const boundModel = await this.bindToolsToModel(
|
|
6797
|
+
model,
|
|
6798
|
+
toolsConfig,
|
|
6799
|
+
customTools
|
|
6800
|
+
);
|
|
6801
|
+
this.modelInstanceCache.set(cacheKey, boundModel);
|
|
6802
|
+
return boundModel;
|
|
6803
|
+
}
|
|
6804
|
+
this.modelInstanceCache.set(cacheKey, model);
|
|
6805
|
+
return model;
|
|
6806
|
+
}
|
|
6807
|
+
/**
|
|
6808
|
+
* Legacy initialization by model ID (DB lookup via configFetcher).
|
|
6809
|
+
* @deprecated Pass ModelConfig with provider + modelName instead.
|
|
6810
|
+
*/
|
|
6811
|
+
async initializeChatModelByIdInternal(config) {
|
|
6710
6812
|
const cacheKey = this.generateModelCacheKey(config);
|
|
6711
6813
|
const cachedModel = this.modelInstanceCache.get(cacheKey);
|
|
6712
6814
|
if (cachedModel) {
|
|
@@ -6732,9 +6834,6 @@ var ModelInitializer = class _ModelInitializer {
|
|
|
6732
6834
|
this.logger.debug(`Creating new chat model instance: ${cacheKey}`);
|
|
6733
6835
|
let model;
|
|
6734
6836
|
if (finalConfig.useBedrock && finalConfig.bedrockModelId) {
|
|
6735
|
-
this.logger.debug(
|
|
6736
|
-
`Using Bedrock for model ${finalConfig.modelName}, bedrockModelId: ${finalConfig.bedrockModelId}`
|
|
6737
|
-
);
|
|
6738
6837
|
model = new aws.ChatBedrockConverse({
|
|
6739
6838
|
model: finalConfig.bedrockModelId,
|
|
6740
6839
|
region: this.resolveBedrockRegion(),
|
|
@@ -6755,29 +6854,12 @@ var ModelInitializer = class _ModelInitializer {
|
|
|
6755
6854
|
...model.metadata,
|
|
6756
6855
|
modelId: config.modelId
|
|
6757
6856
|
};
|
|
6758
|
-
this.logger.debug("\u{1F527} Model initialized with metadata", {
|
|
6759
|
-
modelId: config.modelId,
|
|
6760
|
-
metadataKeys: Object.keys(model.metadata || {}),
|
|
6761
|
-
hasModelId: !!model.metadata?.modelId
|
|
6762
|
-
});
|
|
6763
|
-
this.logger.debug(
|
|
6764
|
-
`[TOOLS CHECK] toolsConfig exists: ${!!config.toolsConfig}, customTools exists: ${!!config.customTools}`
|
|
6765
|
-
);
|
|
6766
|
-
if (config.toolsConfig) {
|
|
6767
|
-
this.logger.debug(
|
|
6768
|
-
`[TOOLS CHECK] toolsConfig length: ${config.toolsConfig.length}, content: ${JSON.stringify(config.toolsConfig)}`
|
|
6769
|
-
);
|
|
6770
|
-
}
|
|
6771
6857
|
if (config.toolsConfig || config.customTools) {
|
|
6772
|
-
this.logger.debug(
|
|
6773
|
-
`[TOOLS] Calling bindToolsToModel with toolsConfig: ${JSON.stringify(config.toolsConfig)}`
|
|
6774
|
-
);
|
|
6775
6858
|
const boundModel = await this.bindToolsToModel(
|
|
6776
6859
|
model,
|
|
6777
6860
|
config.toolsConfig,
|
|
6778
6861
|
config.customTools
|
|
6779
6862
|
);
|
|
6780
|
-
this.logger.debug(`[TOOLS] bindToolsToModel returned successfully`);
|
|
6781
6863
|
this.modelInstanceCache.set(cacheKey, boundModel);
|
|
6782
6864
|
return boundModel;
|
|
6783
6865
|
}
|
|
@@ -7612,6 +7694,7 @@ exports.CallbackRegistry = CallbackRegistry;
|
|
|
7612
7694
|
exports.CallbackStore = CallbackStore;
|
|
7613
7695
|
exports.ChatFeature = ChatFeature;
|
|
7614
7696
|
exports.DEFAULT_ATTACHMENT_THRESHOLD = DEFAULT_ATTACHMENT_THRESHOLD;
|
|
7697
|
+
exports.DEFAULT_ROUTER_URL = DEFAULT_ROUTER_URL;
|
|
7615
7698
|
exports.DEFAULT_TRACER_OPTIONS = DEFAULT_TRACER_OPTIONS;
|
|
7616
7699
|
exports.Endpoint = Endpoint;
|
|
7617
7700
|
exports.FileTokenStore = FileTokenStore;
|
|
@@ -7638,6 +7721,7 @@ exports.WithUIEndpoints = WithUIEndpoints;
|
|
|
7638
7721
|
exports._internals = _internals;
|
|
7639
7722
|
exports.bootstrap = bootstrap;
|
|
7640
7723
|
exports.buildOAuthAuthorizationUrl = buildOAuthAuthorizationUrl;
|
|
7724
|
+
exports.buildOpenAIModelConfig = buildOpenAIModelConfig;
|
|
7641
7725
|
exports.clearAttachmentDataStore = clearAttachmentDataStore;
|
|
7642
7726
|
exports.createEndpointDescriptors = createEndpointDescriptors;
|
|
7643
7727
|
exports.createGraphAttachment = createGraphAttachment;
|
|
@@ -7650,6 +7734,7 @@ exports.executeToolWithAttachments = executeToolWithAttachments;
|
|
|
7650
7734
|
exports.findCallbackMethod = findCallbackMethod;
|
|
7651
7735
|
exports.findEndpointMethod = findEndpointMethod;
|
|
7652
7736
|
exports.generateAttachmentSummary = generateAttachmentSummary;
|
|
7737
|
+
exports.generateModelCacheKey = generateModelCacheKey;
|
|
7653
7738
|
exports.getAttachmentData = getAttachmentData;
|
|
7654
7739
|
exports.getCallbackMetadata = getCallbackMetadata;
|
|
7655
7740
|
exports.getEndpointMetadata = getEndpointMetadata;
|
|
@@ -7659,10 +7744,14 @@ exports.getUIEndpointClassMetadata = getUIEndpointClassMetadata;
|
|
|
7659
7744
|
exports.getUIEndpointMethodsMetadata = getUIEndpointMethodsMetadata;
|
|
7660
7745
|
exports.hasCallbacks = hasCallbacks;
|
|
7661
7746
|
exports.hasUIEndpoints = hasUIEndpoints;
|
|
7747
|
+
exports.hashToolsConfig = hashToolsConfig;
|
|
7748
|
+
exports.isReasoningModel = isReasoningModel;
|
|
7662
7749
|
exports.loadOAuthProviders = loadOAuthProviders;
|
|
7750
|
+
exports.normalizeToolConfigs = normalizeToolConfigs;
|
|
7663
7751
|
exports.registerFinanceExampleCallback = registerFinanceExampleCallback;
|
|
7664
7752
|
exports.registerUIEndpointsFromClass = registerUIEndpointsFromClass;
|
|
7665
7753
|
exports.resolveOAuthProviderConfig = resolveOAuthProviderConfig;
|
|
7754
|
+
exports.resolveRouterURL = resolveRouterURL;
|
|
7666
7755
|
exports.sanitizeTraceData = sanitizeTraceData;
|
|
7667
7756
|
exports.storeAttachmentData = storeAttachmentData;
|
|
7668
7757
|
exports.traceApiCall = traceApiCall;
|