Skip to content

Commit 2849751

Browse files
GaosCodesteipete
andauthored
fix(qqbot): schedule reminders through cron gateway (#70937)
* fix(qqbot): schedule reminders through cron gateway * fix(qqbot): update reminder cron instruction * fix(qqbot): schedule reminders directly (#70937) (thanks @GaosCode) --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 73caceb commit 2849751

6 files changed

Lines changed: 463 additions & 137 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ Docs: https://docs.openclaw.ai
6262

6363
### Fixes
6464

65+
- QQ Bot: make `qqbot_remind` schedule, list, and remove Gateway cron jobs
66+
directly for owner-authorized senders instead of returning `cronParams` and
67+
relying on a follow-up generic `cron` tool call. Fixes #70865. (#70937)
68+
Thanks @GaosCode.
6569
- Agents/subagents: keep queued subagent announces session-only when the
6670
requester has no external channel target, avoiding ambiguous multi-channel
6771
delivery failures. Fixes #59201. Thanks @larrylhollan.

‎extensions/qqbot/skills/qqbot-remind/SKILL.md‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ metadata: { "openclaw": { "emoji": "⏰", "requires": { "config": ["channels.qqb
1414

1515
---
1616

17-
## 推荐流程(优先使用 `qqbot_remind` 工具)
17+
## 推荐流程(使用 `qqbot_remind` 工具)
1818

1919
**第一步**:调用 `qqbot_remind` 工具,传入简单参数:
2020

@@ -26,23 +26,20 @@ metadata: { "openclaw": { "emoji": "⏰", "requires": { "config": ["channels.qqb
2626
| `time` | 时间(相对时间或 cron 表达式) | `"5m"` / `"1h30m"` / `"0 8 * * *"` |
2727
| `jobId` | 任务 ID(仅 remove) | `"xxx"` |
2828

29-
**第二步**:`qqbot_remind` 会返回 `cronParams`,你必须**立即**将 `cronParams` 的值作为参数调用 `cron` 工具。
30-
31-
**第三步**:根据 `cron` 工具的返回结果,回复用户。
29+
**第二步**:根据 `qqbot_remind` 的返回结果,回复用户。`qqbot_remind` 会直接创建、查询或取消 Gateway cron 任务;成功后不要再调用 `cron` 工具。
3230

3331
### 示例
3432

3533
用户说:"5分钟后提醒我喝水"
3634

3735
1. 调用 `qqbot_remind`:`{ "action": "add", "content": "喝水", "time": "5m" }`
38-
2. 收到返回的 `cronParams` → 立即调用 `cron` 工具,参数为该 `cronParams`
39-
3. 回复用户:`⏰ 好的,5分钟后提醒你喝水~`
36+
2. 工具返回成功后,回复用户:`⏰ 好的,5分钟后提醒你喝水~`
4037

4138
---
4239

4340
## 备用方案(直接使用 `cron` 工具)
4441

45-
> 仅当 `qqbot_remind` 工具不可用时使用以下方式。
42+
> 仅当 `qqbot_remind` 工具不可用但 `cron` 工具可用时使用以下方式。
4643
4744
### 核心规则
4845

@@ -113,7 +110,6 @@ metadata: { "openclaw": { "emoji": "⏰", "requires": { "config": ["channels.qqb
113110
```
114111

115112
> 周期任务**不加** `deleteAfterRun`。群聊 `delivery.to` 格式为 `"qqbot:group:{group_openid}"`。
116-
> 若通过 `qqbot_remind` 工具生成 cronParams,**必须**原样传给 `cron` 工具,不要修改或省略任何字段,特别是 `delivery.accountId`。
117113
118114
---
119115

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
3+
const { callGatewayToolMock } = vi.hoisted(() => ({
4+
callGatewayToolMock: vi.fn(),
5+
}));
6+
7+
vi.mock("openclaw/plugin-sdk/agent-harness-runtime", () => ({
8+
callGatewayTool: callGatewayToolMock,
9+
}));
10+
11+
import { createRemindTool } from "./remind.js";
12+
13+
describe("bridge/tools/remind", () => {
14+
beforeEach(() => {
15+
callGatewayToolMock.mockReset();
16+
callGatewayToolMock.mockResolvedValue({ ok: true });
17+
});
18+
19+
it("marks qqbot_remind as owner-only", () => {
20+
const tool = createRemindTool();
21+
expect(tool.ownerOnly).toBe(true);
22+
});
23+
24+
it("schedules reminders directly through Gateway cron with ambient QQ delivery context", async () => {
25+
callGatewayToolMock.mockResolvedValue({ id: "job-1" });
26+
const tool = createRemindTool({
27+
senderIsOwner: true,
28+
deliveryContext: { to: "qqbot:c2c:user-openid", accountId: "bot2" },
29+
});
30+
31+
const result = await tool.execute("tool-call-1", {
32+
action: "add",
33+
content: "drink water",
34+
time: "5m",
35+
});
36+
37+
expect(callGatewayToolMock).toHaveBeenCalledWith(
38+
"cron.add",
39+
{ timeoutMs: 60_000 },
40+
{
41+
job: expect.objectContaining({
42+
sessionTarget: "isolated",
43+
payload: { kind: "agentTurn", message: expect.stringContaining("drink water") },
44+
delivery: {
45+
mode: "announce",
46+
channel: "qqbot",
47+
to: "qqbot:c2c:user-openid",
48+
accountId: "bot2",
49+
},
50+
}),
51+
},
52+
);
53+
expect(result.details).toEqual({
54+
ok: true,
55+
action: "add",
56+
summary: '⏰ Reminder in 5m: "drink water"',
57+
cronResult: { id: "job-1" },
58+
});
59+
});
60+
61+
it("routes list and remove through Gateway cron without exposing generic cron to the model", async () => {
62+
const tool = createRemindTool({ senderIsOwner: true });
63+
64+
await tool.execute("tool-call-1", { action: "list" });
65+
await tool.execute("tool-call-2", { action: "remove", jobId: "job-1" });
66+
67+
expect(callGatewayToolMock).toHaveBeenNthCalledWith(1, "cron.list", { timeoutMs: 60_000 }, {});
68+
expect(callGatewayToolMock).toHaveBeenNthCalledWith(
69+
2,
70+
"cron.remove",
71+
{ timeoutMs: 60_000 },
72+
{ jobId: "job-1" },
73+
);
74+
});
75+
76+
it("supports injected cron scheduler dependencies for engine-level tests", async () => {
77+
const callCron = vi.fn(async () => ({ id: "job-1" }));
78+
const tool = createRemindTool(
79+
{
80+
senderIsOwner: true,
81+
deliveryContext: { to: "qqbot:c2c:user-openid", accountId: "bot2" },
82+
},
83+
{ callCron },
84+
);
85+
86+
await tool.execute("tool-call-1", {
87+
action: "add",
88+
content: "drink water",
89+
time: "5m",
90+
});
91+
92+
expect(callCron).toHaveBeenCalledWith(
93+
expect.objectContaining({
94+
action: "add",
95+
job: expect.objectContaining({
96+
delivery: expect.objectContaining({ to: "qqbot:c2c:user-openid", accountId: "bot2" }),
97+
}),
98+
}),
99+
);
100+
expect(callGatewayToolMock).not.toHaveBeenCalled();
101+
});
102+
103+
it("does not schedule when sender ownership is missing", async () => {
104+
const callCron = vi.fn(async () => ({ id: "job-1" }));
105+
const tool = createRemindTool(
106+
{
107+
deliveryContext: { to: "qqbot:c2c:user-openid", accountId: "bot2" },
108+
},
109+
{ callCron },
110+
);
111+
112+
const result = await tool.execute("tool-call-1", {
113+
action: "add",
114+
content: "drink water",
115+
time: "5m",
116+
});
117+
118+
expect(callCron).not.toHaveBeenCalled();
119+
expect(callGatewayToolMock).not.toHaveBeenCalled();
120+
expect(result.details).toEqual({
121+
error: "QQ reminders require an owner-authorized sender.",
122+
});
123+
});
124+
});
Lines changed: 87 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,91 @@
1-
import type { OpenClawPluginApi } from "openclaw/plugin-sdk/core";
2-
import { RemindSchema, executeRemind } from "../../engine/tools/remind-logic.js";
3-
import type { RemindParams } from "../../engine/tools/remind-logic.js";
1+
import { callGatewayTool } from "openclaw/plugin-sdk/agent-harness-runtime";
2+
import type {
3+
AnyAgentTool,
4+
OpenClawPluginApi,
5+
OpenClawPluginToolContext,
6+
} from "openclaw/plugin-sdk/core";
7+
import { RemindSchema, executeScheduledRemind } from "../../engine/tools/remind-logic.js";
8+
import type { RemindCronAction, RemindParams } from "../../engine/tools/remind-logic.js";
49
import { getRequestContext } from "../../engine/utils/request-context.js";
510

6-
export function registerRemindTool(api: OpenClawPluginApi): void {
7-
api.registerTool(
8-
{
9-
name: "qqbot_remind",
10-
label: "QQBot Reminder",
11-
description:
12-
"Create, list, and remove QQ reminders. " +
13-
"Use simple parameters without manually building cron JSON.\n" +
14-
"Create: action=add, content=message, time=schedule (to is optional, " +
15-
"resolved automatically from the current conversation)\n" +
16-
"List: action=list\n" +
17-
"Remove: action=remove, jobId=job id from list\n" +
18-
'Time examples: "5m", "1h", "0 8 * * *"',
19-
parameters: RemindSchema,
20-
async execute(_toolCallId, params) {
21-
const ctx = getRequestContext();
22-
return executeRemind(params as RemindParams, {
23-
fallbackTo: ctx?.target,
24-
fallbackAccountId: ctx?.accountId,
25-
});
26-
},
11+
type CronGatewayCaller = (params: RemindCronAction) => Promise<unknown>;
12+
13+
type RemindToolDeps = {
14+
callCron: CronGatewayCaller;
15+
};
16+
17+
const DEFAULT_GATEWAY_TIMEOUT_MS = 60_000;
18+
19+
function unexpectedCronParams(params: never): never {
20+
throw new Error(`Unsupported reminder cron action: ${JSON.stringify(params)}`);
21+
}
22+
23+
const defaultDeps: RemindToolDeps = {
24+
callCron: async (params) => {
25+
switch (params.action) {
26+
case "list":
27+
return await callGatewayTool("cron.list", { timeoutMs: DEFAULT_GATEWAY_TIMEOUT_MS }, {});
28+
case "remove":
29+
return await callGatewayTool(
30+
"cron.remove",
31+
{ timeoutMs: DEFAULT_GATEWAY_TIMEOUT_MS },
32+
{ jobId: params.jobId },
33+
);
34+
case "add":
35+
return await callGatewayTool(
36+
"cron.add",
37+
{ timeoutMs: DEFAULT_GATEWAY_TIMEOUT_MS },
38+
{ job: params.job },
39+
);
40+
}
41+
return unexpectedCronParams(params);
42+
},
43+
};
44+
45+
export function createRemindTool(
46+
toolContext: OpenClawPluginToolContext = {},
47+
deps: RemindToolDeps = defaultDeps,
48+
): AnyAgentTool {
49+
return {
50+
name: "qqbot_remind",
51+
label: "QQBot Reminder",
52+
ownerOnly: true,
53+
description:
54+
"Create, list, and remove QQ reminders. " +
55+
"This tool schedules Gateway cron jobs directly; do not call the cron tool after it succeeds.\n" +
56+
"Create: action=add, content=message, time=schedule (to is optional, " +
57+
"resolved automatically from the current conversation)\n" +
58+
"List: action=list\n" +
59+
"Remove: action=remove, jobId=job id from list\n" +
60+
'Time examples: "5m", "1h", "0 8 * * *"',
61+
parameters: RemindSchema,
62+
async execute(_toolCallId, params) {
63+
if (toolContext.senderIsOwner !== true) {
64+
return {
65+
content: [
66+
{
67+
type: "text" as const,
68+
text: JSON.stringify({
69+
error: "QQ reminders require an owner-authorized sender.",
70+
}),
71+
},
72+
],
73+
details: { error: "QQ reminders require an owner-authorized sender." },
74+
};
75+
}
76+
const ctx = getRequestContext();
77+
return await executeScheduledRemind(
78+
params as RemindParams,
79+
{
80+
fallbackTo: ctx?.target ?? toolContext.deliveryContext?.to,
81+
fallbackAccountId: ctx?.accountId ?? toolContext.deliveryContext?.accountId,
82+
},
83+
deps.callCron,
84+
);
2785
},
28-
{ name: "qqbot_remind" },
29-
);
86+
};
87+
}
88+
89+
export function registerRemindTool(api: OpenClawPluginApi): void {
90+
api.registerTool((ctx) => createRemindTool(ctx), { name: "qqbot_remind" });
3091
}

0 commit comments

Comments
 (0)