fix(discord): centralize model picker numeric parsing · openclaw/openclaw@721cedf
steipete
·
2026-05-29
·
via Recent Commits to openclaw:main
File tree
extensions/discord/src/monitor
| Original file line number | Diff line number | Diff line change |
|---|
|
1 | 1 | import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts"; |
2 | 2 | import type { ModelsProviderData } from "openclaw/plugin-sdk/models-provider-runtime"; |
| 3 | +import { parseStrictInteger, parseStrictPositiveInteger } from "openclaw/plugin-sdk/number-runtime"; |
3 | 4 | import { normalizeProviderId } from "openclaw/plugin-sdk/provider-model-shared"; |
4 | 5 | import type { ComponentData } from "../internal/discord.js"; |
5 | 6 | |
@@ -148,28 +149,17 @@ function parseRawPage(value: unknown): number {
|
148 | 149 | if (typeof value === "number") { |
149 | 150 | return normalizeModelPickerPage(value); |
150 | 151 | } |
151 | | -if (typeof value === "string" && /^[+-]?\d+$/.test(value.trim())) { |
152 | | -const parsed = Number(value.trim()); |
153 | | -if (Number.isSafeInteger(parsed)) { |
| 152 | +if (typeof value === "string") { |
| 153 | +const parsed = parseStrictInteger(value); |
| 154 | +if (parsed !== undefined) { |
154 | 155 | return normalizeModelPickerPage(parsed); |
155 | 156 | } |
156 | 157 | } |
157 | 158 | return 1; |
158 | 159 | } |
159 | 160 | |
160 | 161 | function parseRawPositiveInt(value: unknown): number | undefined { |
161 | | -if (typeof value !== "string" && typeof value !== "number") { |
162 | | -return undefined; |
163 | | -} |
164 | | -const raw = String(value).trim(); |
165 | | -if (!/^[+]?\d+$/.test(raw)) { |
166 | | -return undefined; |
167 | | -} |
168 | | -const parsed = Number(raw); |
169 | | -if (!Number.isSafeInteger(parsed) || parsed < 1) { |
170 | | -return undefined; |
171 | | -} |
172 | | -return parsed; |
| 162 | +return parseStrictPositiveInteger(value); |
173 | 163 | } |
174 | 164 | |
175 | 165 | function coerceString(value: unknown): string { |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -160,6 +160,34 @@ describe("Discord model picker custom_id", () => {
|
160 | 160 | }); |
161 | 161 | }); |
162 | 162 | |
| 163 | +it("parses plus-signed compact numeric fields", () => { |
| 164 | +const parsed = parseDiscordModelPickerData({ |
| 165 | +c: "models", |
| 166 | +a: "submit", |
| 167 | +v: "recents", |
| 168 | +u: "42", |
| 169 | +p: "openai", |
| 170 | +g: "+03", |
| 171 | +pp: "+02", |
| 172 | +mi: "+07", |
| 173 | +ri: "+04", |
| 174 | +rs: "+01", |
| 175 | +}); |
| 176 | + |
| 177 | +expect(parsed).toEqual({ |
| 178 | +command: "models", |
| 179 | +action: "submit", |
| 180 | +view: "recents", |
| 181 | +userId: "42", |
| 182 | +provider: "openai", |
| 183 | +page: 3, |
| 184 | +providerPage: 2, |
| 185 | +modelIndex: 7, |
| 186 | +runtimeIndex: 4, |
| 187 | +recentSlot: 1, |
| 188 | +}); |
| 189 | +}); |
| 190 | + |
163 | 191 | it("parses optional submit model index", () => { |
164 | 192 | const parsed = parseDiscordModelPickerData({ |
165 | 193 | cmd: "models", |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。