@gitlab/gitlab-ai-provider 3.5.0 → 3.5.1

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
@@ -1460,6 +1460,7 @@ var GitLabOpenAILanguageModel = class {
1460
1460
  };
1461
1461
 
1462
1462
  // src/gitlab-oauth-types.ts
1463
+ var OPENCODE_GITLAB_AUTH_CLIENT_ID = "1d89f9fdb23ee96d4e603201f6861dab6e143c5c3c00469a018a2d94bdc03d4e";
1463
1464
  var BUNDLED_CLIENT_ID = "36f2a70cddeb5a0889d4fd8295c241b7e9848e89cf9e599d0eed2d8e5350fbf5";
1464
1465
  var GITLAB_COM_URL = "https://gitlab.com";
1465
1466
  var TOKEN_EXPIRY_SKEW_MS = 5 * 60 * 1e3;
@@ -1533,14 +1534,21 @@ var GitLabOAuthManager = class {
1533
1534
  return this.createTokensFromResponse(tokenResponse, instanceUrl);
1534
1535
  }
1535
1536
  /**
1536
- * Get the OAuth client ID for an instance
1537
+ * Get the OAuth client ID for an instance.
1538
+ * Priority: env var > opencode-gitlab-auth default (for GitLab.com).
1539
+ * Note: callers (e.g. exchangeRefreshToken) may pass an explicit clientId
1540
+ * that bypasses this method entirely.
1537
1541
  */
1538
1542
  getClientId(instanceUrl) {
1543
+ const envClientId = process.env["GITLAB_OAUTH_CLIENT_ID"];
1544
+ if (envClientId) {
1545
+ return envClientId;
1546
+ }
1539
1547
  if (instanceUrl === GITLAB_COM_URL) {
1540
- return BUNDLED_CLIENT_ID;
1548
+ return OPENCODE_GITLAB_AUTH_CLIENT_ID;
1541
1549
  }
1542
1550
  throw new GitLabError({
1543
- message: `No OAuth client ID configured for instance ${instanceUrl}. Please provide a clientId parameter.`
1551
+ message: `No OAuth client ID configured for instance ${instanceUrl}. Please provide a clientId parameter or set GITLAB_OAUTH_CLIENT_ID environment variable.`
1544
1552
  });
1545
1553
  }
1546
1554
  /**
@@ -1622,7 +1630,7 @@ var GitLabOAuthManager = class {
1622
1630
  };
1623
1631
 
1624
1632
  // src/version.ts
1625
- var VERSION = true ? "3.4.1" : "0.0.0-dev";
1633
+ var VERSION = true ? "3.5.0" : "0.0.0-dev";
1626
1634
 
1627
1635
  // src/gitlab-provider.ts
1628
1636
  import * as fs from "fs";
@@ -1675,20 +1683,26 @@ async function loadApiKey(options, instanceUrl, clientId) {
1675
1683
  });
1676
1684
  const authPath = getOpenCodeAuthPath();
1677
1685
  const authData = JSON.parse(fs.readFileSync(authPath, "utf-8"));
1678
- const normalizedUrl = instanceUrl.replace(/\/$/, "");
1679
- authData[normalizedUrl] = {
1686
+ authData.gitlab = {
1680
1687
  type: "oauth",
1681
1688
  refresh: refreshed.refreshToken,
1682
1689
  access: refreshed.accessToken,
1683
1690
  expires: refreshed.expiresAt,
1684
- instanceUrl
1691
+ enterpriseUrl: instanceUrl
1692
+ // Use enterpriseUrl to match auth plugin format
1685
1693
  };
1686
- fs.writeFileSync(authPath, JSON.stringify(authData, null, 2));
1694
+ fs.writeFileSync(authPath, JSON.stringify(authData, null, 2), { mode: 384 });
1687
1695
  return refreshed.accessToken;
1688
1696
  } catch (error) {
1689
- console.warn(
1690
- `Failed to refresh OAuth token: ${error instanceof Error ? error.message : String(error)}`
1691
- );
1697
+ const refreshErrorMsg = error instanceof Error ? error.message : String(error);
1698
+ console.warn(`Failed to refresh OAuth token: ${refreshErrorMsg}`);
1699
+ const envApiKey = process.env[options.environmentVariableName];
1700
+ if (envApiKey) {
1701
+ return envApiKey;
1702
+ }
1703
+ throw new GitLabError({
1704
+ message: `OAuth token refresh failed and no fallback ${options.environmentVariableName} environment variable is set. Refresh error: ${refreshErrorMsg}. Re-authenticate with 'opencode auth login gitlab' or set ${options.environmentVariableName}.`
1705
+ });
1692
1706
  }
1693
1707
  } else {
1694
1708
  return auth.access;
@@ -1737,7 +1751,16 @@ function createGitLab(options = {}) {
1737
1751
  const refreshApiKey = async () => {
1738
1752
  cachedApiKey = void 0;
1739
1753
  apiKeyPromise = void 0;
1740
- cachedApiKey = await getApiKey();
1754
+ cachedApiKey = await loadApiKey(
1755
+ {
1756
+ apiKey: void 0,
1757
+ // Bypass stale options.apiKey to force auth.json read
1758
+ environmentVariableName: "GITLAB_TOKEN",
1759
+ description: "GitLab"
1760
+ },
1761
+ instanceUrl,
1762
+ options.clientId
1763
+ );
1741
1764
  };
1742
1765
  const getHeaders = () => {
1743
1766
  const apiKey = cachedApiKey || options.apiKey || process.env["GITLAB_TOKEN"] || "";
@@ -2113,6 +2136,7 @@ export {
2113
2136
  MODEL_ID_TO_ANTHROPIC_MODEL,
2114
2137
  MODEL_MAPPINGS,
2115
2138
  OAUTH_SCOPES,
2139
+ OPENCODE_GITLAB_AUTH_CLIENT_ID,
2116
2140
  TOKEN_EXPIRY_SKEW_MS,
2117
2141
  VERSION,
2118
2142
  createGitLab,