@tscgoods/effort-discovery-core 0.5.2 → 0.5.5

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/package.json +1 -1
  2. package/src/index.js +22 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tscgoods/effort-discovery-core",
3
- "version": "0.5.2",
3
+ "version": "0.5.5",
4
4
  "description": "Core probe/catalog/cache logic shared by every effort-discovery host plugin.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/index.js CHANGED
@@ -84,6 +84,20 @@ export function parseLevels(text) {
84
84
  return null
85
85
  }
86
86
 
87
+ /**
88
+ * 通用网关有时把所有枚举值都回显出来(serde 的 expected one of),
89
+ * 只有互相印证的窄子集才能作为模型级证据。
90
+ */
91
+ export function pickLevels(candidates) {
92
+ const specific = candidates.filter((levels) => levels.length > 0 && levels.length < 6)
93
+ if (specific.length === 0) return null
94
+ let result = specific[0]
95
+ for (const levels of specific.slice(1)) {
96
+ result = result.filter((level) => levels.includes(level))
97
+ }
98
+ return result.length > 0 ? result : null
99
+ }
100
+
87
101
  /**
88
102
  * 用非法值探一次,读回上游自己声明的合法档位。
89
103
  * 个别网关只对特定非法值透传上游报错,因此按 sentinels 依次尝试。
@@ -98,6 +112,7 @@ export async function probeEffort(input) {
98
112
 
99
113
  let last = { levels: null, source: "error", at: Date.now() }
100
114
  let sawOk = false
115
+ const candidates = []
101
116
 
102
117
  for (const sentinel of sentinels) {
103
118
  let response
@@ -124,13 +139,19 @@ export async function probeEffort(input) {
124
139
  if (response.ok) { sawOk = true; continue }
125
140
 
126
141
  const levels = parseLevels(text)
127
- if (levels) return { levels, source: "validated", at: Date.now() }
142
+ if (levels) {
143
+ candidates.push(levels)
144
+ continue
145
+ }
128
146
 
129
147
  last = { levels: null, source: `http-${response.status}`, at: Date.now() }
130
148
  // 限流/鉴权错误换 sentinel 也没用,直接返回
131
149
  if (response.status === 401 || response.status === 403 || response.status === 429) return last
132
150
  }
133
151
 
152
+ const levels = pickLevels(candidates)
153
+ if (levels) return { levels, source: "validated", at: Date.now() }
154
+ if (candidates.length > 0) return { levels: null, source: "no-validation", at: Date.now() }
134
155
  return sawOk ? { levels: null, source: "no-validation", at: Date.now() } : last
135
156
  }
136
157