@matthewdunbar/amazon-bedrock-mantle 2.0.30 → 2.0.31
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.
Potentially problematic release.
This version of @matthewdunbar/amazon-bedrock-mantle might be problematic. Click here for more details.
- package/dist/index.d.mts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +138 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +138 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/chat/openai-compatible-chat-language-model.ts +1 -1
- package/src/load-aws-profile-credentials.ts +183 -0
- package/src/openai-compatible-provider.ts +49 -16
package/dist/index.d.mts
CHANGED
|
@@ -275,6 +275,11 @@ interface OpenAICompatibleProviderSettings {
|
|
|
275
275
|
* The AWS session token to use for request signing. Defaults to `AWS_SESSION_TOKEN`.
|
|
276
276
|
*/
|
|
277
277
|
sessionToken?: string;
|
|
278
|
+
/**
|
|
279
|
+
* The AWS shared profile name to load credentials from (e.g. `default`, `dev`).
|
|
280
|
+
* Falls back to `AWS_PROFILE` when not provided.
|
|
281
|
+
*/
|
|
282
|
+
profile?: string;
|
|
278
283
|
/**
|
|
279
284
|
* Optional async credentials provider used for request signing.
|
|
280
285
|
*/
|
package/dist/index.d.ts
CHANGED
|
@@ -275,6 +275,11 @@ interface OpenAICompatibleProviderSettings {
|
|
|
275
275
|
* The AWS session token to use for request signing. Defaults to `AWS_SESSION_TOKEN`.
|
|
276
276
|
*/
|
|
277
277
|
sessionToken?: string;
|
|
278
|
+
/**
|
|
279
|
+
* The AWS shared profile name to load credentials from (e.g. `default`, `dev`).
|
|
280
|
+
* Falls back to `AWS_PROFILE` when not provided.
|
|
281
|
+
*/
|
|
282
|
+
profile?: string;
|
|
278
283
|
/**
|
|
279
284
|
* Optional async credentials provider used for request signing.
|
|
280
285
|
*/
|
package/dist/index.js
CHANGED
|
@@ -648,7 +648,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
648
648
|
let usage = void 0;
|
|
649
649
|
let isFirstChunk = true;
|
|
650
650
|
const providerOptionsName = this.providerOptionsName;
|
|
651
|
-
const shouldTerminateOnFinishReason = providerOptionsName.includes("bedrock")
|
|
651
|
+
const shouldTerminateOnFinishReason = providerOptionsName.includes("bedrock");
|
|
652
652
|
let isActiveReasoning = false;
|
|
653
653
|
let isActiveText = false;
|
|
654
654
|
let hasFinished = false;
|
|
@@ -1772,8 +1772,132 @@ function prepareBodyString(body) {
|
|
|
1772
1772
|
return JSON.stringify(body);
|
|
1773
1773
|
}
|
|
1774
1774
|
|
|
1775
|
+
// src/load-aws-profile-credentials.ts
|
|
1776
|
+
var import_promises = require("fs/promises");
|
|
1777
|
+
var import_node_os = require("os");
|
|
1778
|
+
var import_node_path = require("path");
|
|
1779
|
+
var import_node_child_process = require("child_process");
|
|
1780
|
+
var import_node_util = require("util");
|
|
1781
|
+
var execFileAsync = (0, import_node_util.promisify)(import_node_child_process.execFile);
|
|
1782
|
+
async function loadAwsProfileCredentials({
|
|
1783
|
+
profile,
|
|
1784
|
+
credentialsFilePath,
|
|
1785
|
+
configFilePath
|
|
1786
|
+
}) {
|
|
1787
|
+
var _a, _b, _c;
|
|
1788
|
+
const sharedCredentialsPath = (_a = credentialsFilePath != null ? credentialsFilePath : process.env.AWS_SHARED_CREDENTIALS_FILE) != null ? _a : (0, import_node_path.join)((0, import_node_os.homedir)(), ".aws", "credentials");
|
|
1789
|
+
const sharedConfigPath = (_b = configFilePath != null ? configFilePath : process.env.AWS_CONFIG_FILE) != null ? _b : (0, import_node_path.join)((0, import_node_os.homedir)(), ".aws", "config");
|
|
1790
|
+
const [credentialsIni, configIni] = await Promise.all([
|
|
1791
|
+
readIniFile(sharedCredentialsPath),
|
|
1792
|
+
readIniFile(sharedConfigPath)
|
|
1793
|
+
]);
|
|
1794
|
+
const credentialsSection = credentialsIni == null ? void 0 : credentialsIni[profile];
|
|
1795
|
+
const configSection = configIni == null ? void 0 : configIni[profile === "default" ? "default" : `profile ${profile}`];
|
|
1796
|
+
if (credentialsSection == null && configSection == null) {
|
|
1797
|
+
return loadCredentialsViaAwsCli(profile);
|
|
1798
|
+
}
|
|
1799
|
+
const accessKeyId = credentialsSection == null ? void 0 : credentialsSection.aws_access_key_id;
|
|
1800
|
+
const secretAccessKey = credentialsSection == null ? void 0 : credentialsSection.aws_secret_access_key;
|
|
1801
|
+
if (accessKeyId == null || secretAccessKey == null) {
|
|
1802
|
+
return loadCredentialsViaAwsCli(profile);
|
|
1803
|
+
}
|
|
1804
|
+
const sessionToken = credentialsSection == null ? void 0 : credentialsSection.aws_session_token;
|
|
1805
|
+
const region = (_c = credentialsSection == null ? void 0 : credentialsSection.region) != null ? _c : configSection == null ? void 0 : configSection.region;
|
|
1806
|
+
return {
|
|
1807
|
+
accessKeyId,
|
|
1808
|
+
secretAccessKey,
|
|
1809
|
+
sessionToken,
|
|
1810
|
+
region
|
|
1811
|
+
};
|
|
1812
|
+
}
|
|
1813
|
+
async function loadCredentialsViaAwsCli(profile) {
|
|
1814
|
+
try {
|
|
1815
|
+
const { stdout } = await execFileAsync(
|
|
1816
|
+
"aws",
|
|
1817
|
+
[
|
|
1818
|
+
"configure",
|
|
1819
|
+
"export-credentials",
|
|
1820
|
+
"--profile",
|
|
1821
|
+
profile,
|
|
1822
|
+
"--format",
|
|
1823
|
+
"process"
|
|
1824
|
+
],
|
|
1825
|
+
{
|
|
1826
|
+
timeout: 5e3
|
|
1827
|
+
}
|
|
1828
|
+
);
|
|
1829
|
+
const parsed = parseProcessCredentials(stdout);
|
|
1830
|
+
if (parsed == null) {
|
|
1831
|
+
return void 0;
|
|
1832
|
+
}
|
|
1833
|
+
return {
|
|
1834
|
+
accessKeyId: parsed.accessKeyId,
|
|
1835
|
+
secretAccessKey: parsed.secretAccessKey,
|
|
1836
|
+
sessionToken: parsed.sessionToken
|
|
1837
|
+
};
|
|
1838
|
+
} catch (e) {
|
|
1839
|
+
return void 0;
|
|
1840
|
+
}
|
|
1841
|
+
}
|
|
1842
|
+
function parseProcessCredentials(value) {
|
|
1843
|
+
try {
|
|
1844
|
+
const parsed = JSON.parse(value);
|
|
1845
|
+
if (parsed.AccessKeyId == null || parsed.SecretAccessKey == null) {
|
|
1846
|
+
return void 0;
|
|
1847
|
+
}
|
|
1848
|
+
return {
|
|
1849
|
+
accessKeyId: parsed.AccessKeyId,
|
|
1850
|
+
secretAccessKey: parsed.SecretAccessKey,
|
|
1851
|
+
sessionToken: parsed.SessionToken
|
|
1852
|
+
};
|
|
1853
|
+
} catch (e) {
|
|
1854
|
+
return void 0;
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
async function readIniFile(filePath) {
|
|
1858
|
+
try {
|
|
1859
|
+
const contents = await (0, import_promises.readFile)(filePath, "utf8");
|
|
1860
|
+
return parseIni(contents);
|
|
1861
|
+
} catch (error) {
|
|
1862
|
+
if (error != null && typeof error === "object" && "code" in error && error.code === "ENOENT") {
|
|
1863
|
+
return void 0;
|
|
1864
|
+
}
|
|
1865
|
+
throw error;
|
|
1866
|
+
}
|
|
1867
|
+
}
|
|
1868
|
+
function parseIni(contents) {
|
|
1869
|
+
var _a;
|
|
1870
|
+
const sections = {};
|
|
1871
|
+
let currentSection;
|
|
1872
|
+
for (const rawLine of contents.split(/\r?\n/u)) {
|
|
1873
|
+
const line = rawLine.trim();
|
|
1874
|
+
if (line.length === 0 || line.startsWith("#") || line.startsWith(";")) {
|
|
1875
|
+
continue;
|
|
1876
|
+
}
|
|
1877
|
+
if (line.startsWith("[") && line.endsWith("]")) {
|
|
1878
|
+
currentSection = line.slice(1, -1).trim();
|
|
1879
|
+
(_a = sections[currentSection]) != null ? _a : sections[currentSection] = {};
|
|
1880
|
+
continue;
|
|
1881
|
+
}
|
|
1882
|
+
if (currentSection == null) {
|
|
1883
|
+
continue;
|
|
1884
|
+
}
|
|
1885
|
+
const delimiterIndex = line.indexOf("=");
|
|
1886
|
+
if (delimiterIndex === -1) {
|
|
1887
|
+
continue;
|
|
1888
|
+
}
|
|
1889
|
+
const key = line.slice(0, delimiterIndex).trim();
|
|
1890
|
+
const value = line.slice(delimiterIndex + 1).trim();
|
|
1891
|
+
if (key.length > 0) {
|
|
1892
|
+
sections[currentSection][key] = value;
|
|
1893
|
+
}
|
|
1894
|
+
}
|
|
1895
|
+
return sections;
|
|
1896
|
+
}
|
|
1897
|
+
|
|
1775
1898
|
// src/openai-compatible-provider.ts
|
|
1776
1899
|
function createOpenAICompatible(options) {
|
|
1900
|
+
var _a;
|
|
1777
1901
|
const baseURL = (0, import_provider_utils7.withoutTrailingSlash)(options.baseURL);
|
|
1778
1902
|
const providerName = options.name;
|
|
1779
1903
|
const headers = {
|
|
@@ -1783,10 +1907,18 @@ function createOpenAICompatible(options) {
|
|
|
1783
1907
|
const hasAuthorizationHeader = Object.keys(headers).some(
|
|
1784
1908
|
(key) => key.toLowerCase() === "authorization"
|
|
1785
1909
|
);
|
|
1910
|
+
const profile = (_a = options.profile) != null ? _a : (0, import_provider_utils7.loadOptionalSetting)({
|
|
1911
|
+
settingValue: options.profile,
|
|
1912
|
+
environmentVariableName: "AWS_PROFILE"
|
|
1913
|
+
});
|
|
1914
|
+
const profileCredentialsPromise = profile == null ? void 0 : loadAwsProfileCredentials({
|
|
1915
|
+
profile
|
|
1916
|
+
});
|
|
1786
1917
|
const getHeaders = () => (0, import_provider_utils7.withUserAgentSuffix)(headers, `ai-sdk/openai-compatible/${VERSION}`);
|
|
1787
1918
|
const sigV4Fetch = createMantleSigV4FetchFunction(async () => {
|
|
1788
|
-
var
|
|
1789
|
-
const
|
|
1919
|
+
var _a2, _b, _c, _d, _e, _f, _g, _h;
|
|
1920
|
+
const profileCredentials = await profileCredentialsPromise;
|
|
1921
|
+
const resolvedRegion = (_b = (_a2 = options.region) != null ? _a2 : profileCredentials == null ? void 0 : profileCredentials.region) != null ? _b : (0, import_provider_utils7.loadSetting)({
|
|
1790
1922
|
settingValue: options.region,
|
|
1791
1923
|
settingName: "region",
|
|
1792
1924
|
environmentVariableName: "AWS_REGION",
|
|
@@ -1801,19 +1933,19 @@ function createOpenAICompatible(options) {
|
|
|
1801
1933
|
}
|
|
1802
1934
|
return {
|
|
1803
1935
|
region: resolvedRegion,
|
|
1804
|
-
accessKeyId: (0, import_provider_utils7.loadSetting)({
|
|
1936
|
+
accessKeyId: (_d = (_c = options.accessKeyId) != null ? _c : profileCredentials == null ? void 0 : profileCredentials.accessKeyId) != null ? _d : (0, import_provider_utils7.loadSetting)({
|
|
1805
1937
|
settingValue: options.accessKeyId,
|
|
1806
1938
|
settingName: "accessKeyId",
|
|
1807
1939
|
environmentVariableName: "AWS_ACCESS_KEY_ID",
|
|
1808
1940
|
description: "AWS access key ID"
|
|
1809
1941
|
}),
|
|
1810
|
-
secretAccessKey: (0, import_provider_utils7.loadSetting)({
|
|
1942
|
+
secretAccessKey: (_f = (_e = options.secretAccessKey) != null ? _e : profileCredentials == null ? void 0 : profileCredentials.secretAccessKey) != null ? _f : (0, import_provider_utils7.loadSetting)({
|
|
1811
1943
|
settingValue: options.secretAccessKey,
|
|
1812
1944
|
settingName: "secretAccessKey",
|
|
1813
1945
|
environmentVariableName: "AWS_SECRET_ACCESS_KEY",
|
|
1814
1946
|
description: "AWS secret access key"
|
|
1815
1947
|
}),
|
|
1816
|
-
sessionToken: (0, import_provider_utils7.loadOptionalSetting)({
|
|
1948
|
+
sessionToken: (_h = (_g = options.sessionToken) != null ? _g : profileCredentials == null ? void 0 : profileCredentials.sessionToken) != null ? _h : (0, import_provider_utils7.loadOptionalSetting)({
|
|
1817
1949
|
settingValue: options.sessionToken,
|
|
1818
1950
|
environmentVariableName: "AWS_SESSION_TOKEN"
|
|
1819
1951
|
})
|