opencode-codebuddy-external-auth 1.0.16 → 1.0.17

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.
Files changed (2) hide show
  1. package/dist/plugin.js +41 -5
  2. package/package.json +1 -1
package/dist/plugin.js CHANGED
@@ -91,10 +91,45 @@ function resolveModel(inputModel) {
91
91
  return CONFIG.defaultModel;
92
92
  return inputModel || "";
93
93
  }
94
- function buildAuthHeaders(accessToken) {
95
- const tenantId = resolveTenantId(accessToken);
96
- const enterpriseId = resolveEnterpriseId(accessToken);
97
- const userId = resolveUserId(accessToken);
94
+ let cachedTenantId = "";
95
+ let cachedEnterpriseId = "";
96
+ let cachedUserId = "";
97
+ async function fetchEnterpriseId(accessToken, tenantId) {
98
+ if (!tenantId)
99
+ return "";
100
+ try {
101
+ const url = `${CONFIG.serverUrl}/console/enterprises/${tenantId}/config/models`;
102
+ const response = await fetch(url, {
103
+ method: "GET",
104
+ headers: {
105
+ "Accept": "application/json, text/plain, */*",
106
+ "X-Requested-With": "XMLHttpRequest",
107
+ "Authorization": `Bearer ${accessToken}`,
108
+ },
109
+ });
110
+ if (!response.ok) {
111
+ return "";
112
+ }
113
+ const data = await response.json();
114
+ return data?.data?.enterpriseId || data?.enterpriseId || "";
115
+ }
116
+ catch {
117
+ return "";
118
+ }
119
+ }
120
+ async function buildAuthHeaders(accessToken) {
121
+ const tenantId = cachedTenantId || resolveTenantId(accessToken);
122
+ let enterpriseId = cachedEnterpriseId || resolveEnterpriseId(accessToken);
123
+ const userId = cachedUserId || resolveUserId(accessToken);
124
+ if (!enterpriseId) {
125
+ enterpriseId = await fetchEnterpriseId(accessToken, tenantId);
126
+ }
127
+ if (tenantId)
128
+ cachedTenantId = tenantId;
129
+ if (enterpriseId)
130
+ cachedEnterpriseId = enterpriseId;
131
+ if (userId)
132
+ cachedUserId = userId;
98
133
  if (!tenantId && !warnedTenantId) {
99
134
  warnedTenantId = true;
100
135
  console.warn("[codebuddy-external] 未获取到 X-Tenant-Id,请设置 CODEBUDDY_TENANT_ID");
@@ -354,9 +389,10 @@ async function executeViaAuthApi(openaiRequest, auth) {
354
389
  stream: openaiRequest.stream ?? true,
355
390
  };
356
391
  const doRequest = async (token) => {
392
+ const headers = await buildAuthHeaders(token);
357
393
  const response = await fetch(`${CONFIG.serverUrl}${CONFIG.chatCompletionsPath}`, {
358
394
  method: "POST",
359
- headers: buildAuthHeaders(token),
395
+ headers,
360
396
  body: JSON.stringify(requestBody),
361
397
  });
362
398
  return response;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-codebuddy-external-auth",
3
- "version": "1.0.16",
3
+ "version": "1.0.17",
4
4
  "description": "OpenCode plugin for CodeBuddy External (IOA) authentication",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",