opencode-codebuddy-external-auth 1.0.17 → 1.0.18

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 +79 -7
  2. package/package.json +1 -1
package/dist/plugin.js CHANGED
@@ -94,9 +94,52 @@ function resolveModel(inputModel) {
94
94
  let cachedTenantId = "";
95
95
  let cachedEnterpriseId = "";
96
96
  let cachedUserId = "";
97
- async function fetchEnterpriseId(accessToken, tenantId) {
97
+ let cachedModelIds = [];
98
+ let warnedModelFallback = false;
99
+ function extractModelIds(payload) {
100
+ const results = [];
101
+ const collect = (list) => {
102
+ if (!Array.isArray(list))
103
+ return;
104
+ for (const item of list) {
105
+ if (!item)
106
+ continue;
107
+ if (typeof item === "string") {
108
+ results.push(item);
109
+ continue;
110
+ }
111
+ if (typeof item === "object") {
112
+ const id = item.id ||
113
+ item.model ||
114
+ item.modelId ||
115
+ item.model_id ||
116
+ item.code ||
117
+ item.name;
118
+ if (typeof id === "string")
119
+ results.push(id);
120
+ if (Array.isArray(item.models))
121
+ collect(item.models);
122
+ }
123
+ }
124
+ };
125
+ const candidates = [
126
+ payload?.data?.models,
127
+ payload?.data?.modelList,
128
+ payload?.data?.availableModels,
129
+ payload?.models,
130
+ payload?.modelList,
131
+ payload?.availableModels,
132
+ payload?.data?.modelGroups,
133
+ payload?.modelGroups,
134
+ ];
135
+ for (const list of candidates) {
136
+ collect(list);
137
+ }
138
+ return Array.from(new Set(results)).filter(Boolean);
139
+ }
140
+ async function fetchConfigModels(accessToken, tenantId) {
98
141
  if (!tenantId)
99
- return "";
142
+ return { enterpriseId: "", models: [] };
100
143
  try {
101
144
  const url = `${CONFIG.serverUrl}/console/enterprises/${tenantId}/config/models`;
102
145
  const response = await fetch(url, {
@@ -108,21 +151,44 @@ async function fetchEnterpriseId(accessToken, tenantId) {
108
151
  },
109
152
  });
110
153
  if (!response.ok) {
111
- return "";
154
+ return { enterpriseId: "", models: [] };
112
155
  }
113
156
  const data = await response.json();
114
- return data?.data?.enterpriseId || data?.enterpriseId || "";
157
+ const enterpriseId = data?.data?.enterpriseId || data?.enterpriseId || "";
158
+ const models = extractModelIds(data);
159
+ return { enterpriseId, models };
115
160
  }
116
161
  catch {
117
- return "";
162
+ return { enterpriseId: "", models: [] };
163
+ }
164
+ }
165
+ async function getSupportedModels(accessToken, tenantId) {
166
+ if (cachedModelIds.length > 0)
167
+ return cachedModelIds;
168
+ const config = await fetchConfigModels(accessToken, tenantId);
169
+ if (config.enterpriseId && !cachedEnterpriseId) {
170
+ cachedEnterpriseId = config.enterpriseId;
171
+ }
172
+ if (config.models.length) {
173
+ cachedModelIds = config.models;
118
174
  }
175
+ return cachedModelIds;
176
+ }
177
+ function pickFallbackModel(models) {
178
+ if (models.includes("glm-4.7-ioa"))
179
+ return "glm-4.7-ioa";
180
+ return models[0] || "";
119
181
  }
120
182
  async function buildAuthHeaders(accessToken) {
121
183
  const tenantId = cachedTenantId || resolveTenantId(accessToken);
122
184
  let enterpriseId = cachedEnterpriseId || resolveEnterpriseId(accessToken);
123
185
  const userId = cachedUserId || resolveUserId(accessToken);
124
186
  if (!enterpriseId) {
125
- enterpriseId = await fetchEnterpriseId(accessToken, tenantId);
187
+ const config = await fetchConfigModels(accessToken, tenantId);
188
+ enterpriseId = config.enterpriseId || enterpriseId;
189
+ if (config.models.length) {
190
+ cachedModelIds = config.models;
191
+ }
126
192
  }
127
193
  if (tenantId)
128
194
  cachedTenantId = tenantId;
@@ -378,10 +444,16 @@ async function executeViaAuthApi(openaiRequest, auth) {
378
444
  throw new Error("缺少 access token,无法调用 CodeBuddy API");
379
445
  }
380
446
  let accessToken = auth.access;
447
+ const tenantId = resolveTenantId(accessToken);
381
448
  const resolvedModel = resolveModel(openaiRequest.model);
449
+ const supportedModels = await getSupportedModels(accessToken, tenantId);
382
450
  if (!resolvedModel) {
383
- throw new Error("未设置模型,请设置 CODEBUDDY_DEFAULT_MODEL 或在 OpenCode 选择模型");
451
+ throw new Error("未设置模型,请在 OpenCode 选择模型");
452
+ }
453
+ if (supportedModels.length && !supportedModels.includes(resolvedModel)) {
454
+ throw new Error(`[codebuddy-external] 模型 ${resolvedModel} 不在可用列表,请更换模型`);
384
455
  }
456
+ console.log(`[codebuddy-external] 使用模型: ${resolvedModel}`);
385
457
  const requestBody = {
386
458
  ...openaiRequest,
387
459
  model: resolvedModel,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-codebuddy-external-auth",
3
- "version": "1.0.17",
3
+ "version": "1.0.18",
4
4
  "description": "OpenCode plugin for CodeBuddy External (IOA) authentication",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",