opencode-codebuddy-external-auth 1.0.15 → 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 +62 -8
  2. package/package.json +1 -1
package/dist/plugin.js CHANGED
@@ -24,6 +24,8 @@ const CONFIG = {
24
24
  tenantId: process.env.CODEBUDDY_TENANT_ID || "",
25
25
  enterpriseId: process.env.CODEBUDDY_ENTERPRISE_ID || "",
26
26
  userId: process.env.CODEBUDDY_USER_ID || "",
27
+ // 强制覆盖模型(避免 OpenCode 仍使用旧模型)
28
+ defaultModel: process.env.CODEBUDDY_DEFAULT_MODEL || "",
27
29
  };
28
30
  // ============================================================================
29
31
  // Utility Functions
@@ -57,6 +59,9 @@ function extractTenantIdFromIss(iss) {
57
59
  const match = iss.match(/realms\/sso-([^/]+)$/);
58
60
  return match?.[1] || "";
59
61
  }
62
+ let warnedTenantId = false;
63
+ let warnedEnterpriseId = false;
64
+ let warnedUserId = false;
60
65
  function resolveTenantId(accessToken) {
61
66
  if (CONFIG.tenantId)
62
67
  return CONFIG.tenantId;
@@ -81,17 +86,60 @@ function resolveUserId(accessToken) {
81
86
  const payload = decodeJwtPayload(accessToken);
82
87
  return payload?.user_id || payload?.userId || payload?.uid || payload?.sub || "";
83
88
  }
84
- function buildAuthHeaders(accessToken) {
85
- const tenantId = resolveTenantId(accessToken);
86
- const enterpriseId = resolveEnterpriseId(accessToken);
87
- const userId = resolveUserId(accessToken);
88
- if (!tenantId) {
89
- console.warn("[codebuddy-external] 未获取到 X-Tenant-Id,请设置 CODEBUDDY_TENANT_ID");
89
+ function resolveModel(inputModel) {
90
+ if (CONFIG.defaultModel)
91
+ return CONFIG.defaultModel;
92
+ return inputModel || "";
93
+ }
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 || "";
90
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);
91
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;
133
+ if (!tenantId && !warnedTenantId) {
134
+ warnedTenantId = true;
135
+ console.warn("[codebuddy-external] 未获取到 X-Tenant-Id,请设置 CODEBUDDY_TENANT_ID");
136
+ }
137
+ if (!enterpriseId && !warnedEnterpriseId) {
138
+ warnedEnterpriseId = true;
92
139
  console.warn("[codebuddy-external] 未获取到 X-Enterprise-Id,请设置 CODEBUDDY_ENTERPRISE_ID");
93
140
  }
94
- if (!userId) {
141
+ if (!userId && !warnedUserId) {
142
+ warnedUserId = true;
95
143
  console.warn("[codebuddy-external] 未获取到 X-User-Id,请设置 CODEBUDDY_USER_ID");
96
144
  }
97
145
  const conversationId = generateUuid();
@@ -330,15 +378,21 @@ async function executeViaAuthApi(openaiRequest, auth) {
330
378
  throw new Error("缺少 access token,无法调用 CodeBuddy API");
331
379
  }
332
380
  let accessToken = auth.access;
381
+ const resolvedModel = resolveModel(openaiRequest.model);
382
+ if (!resolvedModel) {
383
+ throw new Error("未设置模型,请设置 CODEBUDDY_DEFAULT_MODEL 或在 OpenCode 选择模型");
384
+ }
333
385
  const requestBody = {
334
386
  ...openaiRequest,
387
+ model: resolvedModel,
335
388
  response_format: openaiRequest.response_format || { type: "text" },
336
389
  stream: openaiRequest.stream ?? true,
337
390
  };
338
391
  const doRequest = async (token) => {
392
+ const headers = await buildAuthHeaders(token);
339
393
  const response = await fetch(`${CONFIG.serverUrl}${CONFIG.chatCompletionsPath}`, {
340
394
  method: "POST",
341
- headers: buildAuthHeaders(token),
395
+ headers,
342
396
  body: JSON.stringify(requestBody),
343
397
  });
344
398
  return response;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-codebuddy-external-auth",
3
- "version": "1.0.15",
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",