gitlab-ai-provider 6.9.2 → 6.9.3

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.mjs CHANGED
@@ -648,6 +648,9 @@ var GitLabDirectAccessClient = class {
648
648
  if (this.config.featureFlags && Object.keys(this.config.featureFlags).length > 0) {
649
649
  requestBody.feature_flags = this.config.featureFlags;
650
650
  }
651
+ if (this.config.ensureApiKey) {
652
+ await this.config.ensureApiKey();
653
+ }
651
654
  try {
652
655
  const response = await this.fetchFn(url, {
653
656
  method: "POST",
@@ -743,6 +746,7 @@ var GitLabAnthropicLanguageModel = class {
743
746
  this.directAccessClient = new GitLabDirectAccessClient({
744
747
  instanceUrl: config.instanceUrl,
745
748
  getHeaders: config.getHeaders,
749
+ ensureApiKey: config.ensureApiKey,
746
750
  refreshApiKey: config.refreshApiKey,
747
751
  fetch: config.fetch,
748
752
  featureFlags: config.featureFlags,
@@ -1476,6 +1480,7 @@ var GitLabOpenAILanguageModel = class {
1476
1480
  this.directAccessClient = new GitLabDirectAccessClient({
1477
1481
  instanceUrl: config.instanceUrl,
1478
1482
  getHeaders: config.getHeaders,
1483
+ ensureApiKey: config.ensureApiKey,
1479
1484
  refreshApiKey: config.refreshApiKey,
1480
1485
  fetch: config.fetch,
1481
1486
  featureFlags: config.featureFlags,
@@ -2417,7 +2422,7 @@ import { AsyncResource } from "async_hooks";
2417
2422
  import WebSocket from "isomorphic-ws";
2418
2423
 
2419
2424
  // src/version.ts
2420
- var VERSION = true ? "6.9.1" : "0.0.0-dev";
2425
+ var VERSION = true ? "6.9.2" : "0.0.0-dev";
2421
2426
 
2422
2427
  // src/gitlab-workflow-client.ts
2423
2428
  var WS_CONNECT_TIMEOUT_MS = 3e4;
@@ -4754,9 +4759,10 @@ var TOKEN_EXPIRY_SKEW_MS = 5 * 60 * 1e3;
4754
4759
  var OAUTH_SCOPES = ["api"];
4755
4760
 
4756
4761
  // src/gitlab-oauth-manager.ts
4762
+ var globalInFlightRefreshes = /* @__PURE__ */ new Map();
4757
4763
  var GitLabOAuthManager = class {
4758
4764
  fetch;
4759
- inFlightRefreshes = /* @__PURE__ */ new Map();
4765
+ inFlightRefreshes = globalInFlightRefreshes;
4760
4766
  constructor(fetchImpl = fetch) {
4761
4767
  this.fetch = fetchImpl;
4762
4768
  }
@@ -4985,6 +4991,39 @@ async function loadOpenCodeAuth(instanceUrl) {
4985
4991
  return void 0;
4986
4992
  }
4987
4993
  }
4994
+ var inFlightOAuthRefreshes = /* @__PURE__ */ new Map();
4995
+ async function refreshOAuthToken(auth, instanceUrl, clientId) {
4996
+ const existing = inFlightOAuthRefreshes.get(instanceUrl);
4997
+ if (existing) {
4998
+ return existing;
4999
+ }
5000
+ const refreshPromise = (async () => {
5001
+ const oauthManager = new GitLabOAuthManager();
5002
+ const refreshed = await oauthManager.exchangeRefreshToken({
5003
+ instanceUrl,
5004
+ refreshToken: auth.refresh,
5005
+ clientId
5006
+ });
5007
+ const authPath = getOpenCodeAuthPath();
5008
+ const authData = JSON.parse(fs3.readFileSync(authPath, "utf-8"));
5009
+ authData.gitlab = {
5010
+ type: "oauth",
5011
+ refresh: refreshed.refreshToken,
5012
+ access: refreshed.accessToken,
5013
+ expires: refreshed.expiresAt,
5014
+ enterpriseUrl: instanceUrl
5015
+ // Use enterpriseUrl to match auth plugin format
5016
+ };
5017
+ fs3.writeFileSync(authPath, JSON.stringify(authData, null, 2), { mode: 384 });
5018
+ return refreshed.accessToken;
5019
+ })();
5020
+ inFlightOAuthRefreshes.set(instanceUrl, refreshPromise);
5021
+ try {
5022
+ return await refreshPromise;
5023
+ } finally {
5024
+ inFlightOAuthRefreshes.delete(instanceUrl);
5025
+ }
5026
+ }
4988
5027
  async function loadApiKey(options, instanceUrl, clientId) {
4989
5028
  if (options.apiKey) {
4990
5029
  return options.apiKey;
@@ -4997,23 +5036,7 @@ async function loadApiKey(options, instanceUrl, clientId) {
4997
5036
  const oauthManager = new GitLabOAuthManager();
4998
5037
  if (oauthManager.needsRefresh(auth.expires)) {
4999
5038
  try {
5000
- const refreshed = await oauthManager.exchangeRefreshToken({
5001
- instanceUrl,
5002
- refreshToken: auth.refresh,
5003
- clientId
5004
- });
5005
- const authPath = getOpenCodeAuthPath();
5006
- const authData = JSON.parse(fs3.readFileSync(authPath, "utf-8"));
5007
- authData.gitlab = {
5008
- type: "oauth",
5009
- refresh: refreshed.refreshToken,
5010
- access: refreshed.accessToken,
5011
- expires: refreshed.expiresAt,
5012
- enterpriseUrl: instanceUrl
5013
- // Use enterpriseUrl to match auth plugin format
5014
- };
5015
- fs3.writeFileSync(authPath, JSON.stringify(authData, null, 2), { mode: 384 });
5016
- return refreshed.accessToken;
5039
+ return await refreshOAuthToken(auth, instanceUrl, clientId);
5017
5040
  } catch (error) {
5018
5041
  const refreshErrorMsg = error instanceof Error ? error.message : String(error);
5019
5042
  const envApiKey = process.env[options.environmentVariableName];
@@ -5112,6 +5135,12 @@ function createGitLab(options = {}) {
5112
5135
  `ai-sdk-gitlab/${VERSION}`
5113
5136
  );
5114
5137
  };
5138
+ const ensureApiKey = async () => {
5139
+ try {
5140
+ await getApiKey();
5141
+ } catch {
5142
+ }
5143
+ };
5115
5144
  getApiKey().catch(() => {
5116
5145
  });
5117
5146
  const createAgenticChatModel = (modelId, agenticOptions) => {
@@ -5146,6 +5175,7 @@ function createGitLab(options = {}) {
5146
5175
  provider: `${providerName}.agentic`,
5147
5176
  instanceUrl,
5148
5177
  getHeaders,
5178
+ ensureApiKey,
5149
5179
  refreshApiKey,
5150
5180
  fetch: options.fetch,
5151
5181
  maxTokens: agenticOptions?.maxTokens,