fix: reject partial qq reminder durations · openclaw/openclaw@1dcb677
steipete
·
2026-05-29
·
via Recent Commits to openclaw:main
File tree
extensions/qqbot/src/engine/tools
| Original file line number | Diff line number | Diff line change |
|---|
@@ -25,6 +25,10 @@ describe("engine/tools/remind-logic", () => {
|
25 | 25 | expect(parseRelativeTime("1h30m")).toBe(90 * 60_000); |
26 | 26 | }); |
27 | 27 | |
| 28 | +it("parses separated relative time tokens", () => { |
| 29 | +expect(parseRelativeTime("1h 30m")).toBe(90 * 60_000); |
| 30 | +}); |
| 31 | + |
28 | 32 | it("parses days", () => { |
29 | 33 | expect(parseRelativeTime("2d")).toBe(2 * 86_400_000); |
30 | 34 | }); |
@@ -39,6 +43,8 @@ describe("engine/tools/remind-logic", () => {
|
39 | 43 | |
40 | 44 | it("returns null for unparseable input", () => { |
41 | 45 | expect(parseRelativeTime("never")).toBeNull(); |
| 46 | +expect(parseRelativeTime("5m later")).toBeNull(); |
| 47 | +expect(parseRelativeTime("about 5m")).toBeNull(); |
42 | 48 | }); |
43 | 49 | |
44 | 50 | it("is case insensitive", () => { |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -117,17 +117,22 @@ export const RemindSchema = {
|
117 | 117 | * @returns Milliseconds or null if unparseable. |
118 | 118 | */ |
119 | 119 | export function parseRelativeTime(timeStr: string): number | null { |
120 | | -const s = timeStr.toLowerCase(); |
| 120 | +const s = timeStr.trim().toLowerCase(); |
121 | 121 | if (/^\d+$/.test(s)) { |
122 | 122 | return Number.parseInt(s, 10) * 60_000; |
123 | 123 | } |
124 | 124 | |
125 | 125 | let totalMs = 0; |
126 | 126 | let matched = false; |
127 | | -const regex = /(\d+(?:\.\d+)?)\s*(d|h|m|s)/g; |
| 127 | +let consumed = 0; |
| 128 | +const regex = /(\d+(?:\.\d+)?)\s*(d|h|m|s)\s*/g; |
128 | 129 | let match: RegExpExecArray | null; |
129 | 130 | while ((match = regex.exec(s)) !== null) { |
| 131 | +if (match.index !== consumed) { |
| 132 | +return null; |
| 133 | +} |
130 | 134 | matched = true; |
| 135 | +consumed = regex.lastIndex; |
131 | 136 | const value = Number.parseFloat(match[1]); |
132 | 137 | const unit = match[2]; |
133 | 138 | switch (unit) { |
@@ -145,7 +150,7 @@ export function parseRelativeTime(timeStr: string): number | null {
|
145 | 150 | break; |
146 | 151 | } |
147 | 152 | } |
148 | | -return matched ? Math.round(totalMs) : null; |
| 153 | +return matched && consumed === s.length ? Math.round(totalMs) : null; |
149 | 154 | } |
150 | 155 | |
151 | 156 | /** |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。