@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.mjs CHANGED
@@ -632,7 +632,7 @@ var OpenAICompatibleChatLanguageModel = class {
632
632
  let usage = void 0;
633
633
  let isFirstChunk = true;
634
634
  const providerOptionsName = this.providerOptionsName;
635
- const shouldTerminateOnFinishReason = providerOptionsName.includes("bedrock") && !this.config.includeUsage;
635
+ const shouldTerminateOnFinishReason = providerOptionsName.includes("bedrock");
636
636
  let isActiveReasoning = false;
637
637
  let isActiveText = false;
638
638
  let hasFinished = false;
@@ -1793,8 +1793,132 @@ function prepareBodyString(body) {
1793
1793
  return JSON.stringify(body);
1794
1794
  }
1795
1795
 
1796
+ // src/load-aws-profile-credentials.ts
1797
+ import { readFile } from "node:fs/promises";
1798
+ import { homedir } from "node:os";
1799
+ import { join } from "node:path";
1800
+ import { execFile } from "node:child_process";
1801
+ import { promisify } from "node:util";
1802
+ var execFileAsync = promisify(execFile);
1803
+ async function loadAwsProfileCredentials({
1804
+ profile,
1805
+ credentialsFilePath,
1806
+ configFilePath
1807
+ }) {
1808
+ var _a, _b, _c;
1809
+ const sharedCredentialsPath = (_a = credentialsFilePath != null ? credentialsFilePath : process.env.AWS_SHARED_CREDENTIALS_FILE) != null ? _a : join(homedir(), ".aws", "credentials");
1810
+ const sharedConfigPath = (_b = configFilePath != null ? configFilePath : process.env.AWS_CONFIG_FILE) != null ? _b : join(homedir(), ".aws", "config");
1811
+ const [credentialsIni, configIni] = await Promise.all([
1812
+ readIniFile(sharedCredentialsPath),
1813
+ readIniFile(sharedConfigPath)
1814
+ ]);
1815
+ const credentialsSection = credentialsIni == null ? void 0 : credentialsIni[profile];
1816
+ const configSection = configIni == null ? void 0 : configIni[profile === "default" ? "default" : `profile ${profile}`];
1817
+ if (credentialsSection == null && configSection == null) {
1818
+ return loadCredentialsViaAwsCli(profile);
1819
+ }
1820
+ const accessKeyId = credentialsSection == null ? void 0 : credentialsSection.aws_access_key_id;
1821
+ const secretAccessKey = credentialsSection == null ? void 0 : credentialsSection.aws_secret_access_key;
1822
+ if (accessKeyId == null || secretAccessKey == null) {
1823
+ return loadCredentialsViaAwsCli(profile);
1824
+ }
1825
+ const sessionToken = credentialsSection == null ? void 0 : credentialsSection.aws_session_token;
1826
+ const region = (_c = credentialsSection == null ? void 0 : credentialsSection.region) != null ? _c : configSection == null ? void 0 : configSection.region;
1827
+ return {
1828
+ accessKeyId,
1829
+ secretAccessKey,
1830
+ sessionToken,
1831
+ region
1832
+ };
1833
+ }
1834
+ async function loadCredentialsViaAwsCli(profile) {
1835
+ try {
1836
+ const { stdout } = await execFileAsync(
1837
+ "aws",
1838
+ [
1839
+ "configure",
1840
+ "export-credentials",
1841
+ "--profile",
1842
+ profile,
1843
+ "--format",
1844
+ "process"
1845
+ ],
1846
+ {
1847
+ timeout: 5e3
1848
+ }
1849
+ );
1850
+ const parsed = parseProcessCredentials(stdout);
1851
+ if (parsed == null) {
1852
+ return void 0;
1853
+ }
1854
+ return {
1855
+ accessKeyId: parsed.accessKeyId,
1856
+ secretAccessKey: parsed.secretAccessKey,
1857
+ sessionToken: parsed.sessionToken
1858
+ };
1859
+ } catch (e) {
1860
+ return void 0;
1861
+ }
1862
+ }
1863
+ function parseProcessCredentials(value) {
1864
+ try {
1865
+ const parsed = JSON.parse(value);
1866
+ if (parsed.AccessKeyId == null || parsed.SecretAccessKey == null) {
1867
+ return void 0;
1868
+ }
1869
+ return {
1870
+ accessKeyId: parsed.AccessKeyId,
1871
+ secretAccessKey: parsed.SecretAccessKey,
1872
+ sessionToken: parsed.SessionToken
1873
+ };
1874
+ } catch (e) {
1875
+ return void 0;
1876
+ }
1877
+ }
1878
+ async function readIniFile(filePath) {
1879
+ try {
1880
+ const contents = await readFile(filePath, "utf8");
1881
+ return parseIni(contents);
1882
+ } catch (error) {
1883
+ if (error != null && typeof error === "object" && "code" in error && error.code === "ENOENT") {
1884
+ return void 0;
1885
+ }
1886
+ throw error;
1887
+ }
1888
+ }
1889
+ function parseIni(contents) {
1890
+ var _a;
1891
+ const sections = {};
1892
+ let currentSection;
1893
+ for (const rawLine of contents.split(/\r?\n/u)) {
1894
+ const line = rawLine.trim();
1895
+ if (line.length === 0 || line.startsWith("#") || line.startsWith(";")) {
1896
+ continue;
1897
+ }
1898
+ if (line.startsWith("[") && line.endsWith("]")) {
1899
+ currentSection = line.slice(1, -1).trim();
1900
+ (_a = sections[currentSection]) != null ? _a : sections[currentSection] = {};
1901
+ continue;
1902
+ }
1903
+ if (currentSection == null) {
1904
+ continue;
1905
+ }
1906
+ const delimiterIndex = line.indexOf("=");
1907
+ if (delimiterIndex === -1) {
1908
+ continue;
1909
+ }
1910
+ const key = line.slice(0, delimiterIndex).trim();
1911
+ const value = line.slice(delimiterIndex + 1).trim();
1912
+ if (key.length > 0) {
1913
+ sections[currentSection][key] = value;
1914
+ }
1915
+ }
1916
+ return sections;
1917
+ }
1918
+
1796
1919
  // src/openai-compatible-provider.ts
1797
1920
  function createOpenAICompatible(options) {
1921
+ var _a;
1798
1922
  const baseURL = withoutTrailingSlash(options.baseURL);
1799
1923
  const providerName = options.name;
1800
1924
  const headers = {
@@ -1804,10 +1928,18 @@ function createOpenAICompatible(options) {
1804
1928
  const hasAuthorizationHeader = Object.keys(headers).some(
1805
1929
  (key) => key.toLowerCase() === "authorization"
1806
1930
  );
1931
+ const profile = (_a = options.profile) != null ? _a : loadOptionalSetting({
1932
+ settingValue: options.profile,
1933
+ environmentVariableName: "AWS_PROFILE"
1934
+ });
1935
+ const profileCredentialsPromise = profile == null ? void 0 : loadAwsProfileCredentials({
1936
+ profile
1937
+ });
1807
1938
  const getHeaders = () => withUserAgentSuffix2(headers, `ai-sdk/openai-compatible/${VERSION}`);
1808
1939
  const sigV4Fetch = createMantleSigV4FetchFunction(async () => {
1809
- var _a;
1810
- const resolvedRegion = (_a = options.region) != null ? _a : loadSetting({
1940
+ var _a2, _b, _c, _d, _e, _f, _g, _h;
1941
+ const profileCredentials = await profileCredentialsPromise;
1942
+ const resolvedRegion = (_b = (_a2 = options.region) != null ? _a2 : profileCredentials == null ? void 0 : profileCredentials.region) != null ? _b : loadSetting({
1811
1943
  settingValue: options.region,
1812
1944
  settingName: "region",
1813
1945
  environmentVariableName: "AWS_REGION",
@@ -1822,19 +1954,19 @@ function createOpenAICompatible(options) {
1822
1954
  }
1823
1955
  return {
1824
1956
  region: resolvedRegion,
1825
- accessKeyId: loadSetting({
1957
+ accessKeyId: (_d = (_c = options.accessKeyId) != null ? _c : profileCredentials == null ? void 0 : profileCredentials.accessKeyId) != null ? _d : loadSetting({
1826
1958
  settingValue: options.accessKeyId,
1827
1959
  settingName: "accessKeyId",
1828
1960
  environmentVariableName: "AWS_ACCESS_KEY_ID",
1829
1961
  description: "AWS access key ID"
1830
1962
  }),
1831
- secretAccessKey: loadSetting({
1963
+ secretAccessKey: (_f = (_e = options.secretAccessKey) != null ? _e : profileCredentials == null ? void 0 : profileCredentials.secretAccessKey) != null ? _f : loadSetting({
1832
1964
  settingValue: options.secretAccessKey,
1833
1965
  settingName: "secretAccessKey",
1834
1966
  environmentVariableName: "AWS_SECRET_ACCESS_KEY",
1835
1967
  description: "AWS secret access key"
1836
1968
  }),
1837
- sessionToken: loadOptionalSetting({
1969
+ sessionToken: (_h = (_g = options.sessionToken) != null ? _g : profileCredentials == null ? void 0 : profileCredentials.sessionToken) != null ? _h : loadOptionalSetting({
1838
1970
  settingValue: options.sessionToken,
1839
1971
  environmentVariableName: "AWS_SESSION_TOKEN"
1840
1972
  })