@workclaw/openclaw-workclaw 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 (51) hide show
  1. package/README.md +21 -1
  2. package/index.ts +210 -210
  3. package/openclaw.plugin.json +1 -0
  4. package/package.json +11 -4
  5. package/setup-entry.ts +6 -0
  6. package/skills/openclaw-workclaw-cron/SKILL.md +45 -28
  7. package/src/accounts.ts +62 -37
  8. package/src/api/accounts-api.ts +88 -89
  9. package/src/api/prompts-api.ts +70 -77
  10. package/src/api/session-api.ts +99 -108
  11. package/src/api/skills-api.ts +35 -37
  12. package/src/api/workspace.ts +27 -29
  13. package/src/channel.ts +200 -202
  14. package/src/config-schema.ts +9 -9
  15. package/src/connection/workclaw-client.ts +554 -567
  16. package/src/gateway/agent-handlers.ts +392 -426
  17. package/src/gateway/config-writer.ts +228 -243
  18. package/src/gateway/message-context.ts +534 -362
  19. package/src/gateway/message-dispatcher.ts +529 -489
  20. package/src/gateway/reconnect.ts +217 -113
  21. package/src/gateway/skills-handler.ts +408 -472
  22. package/src/gateway/skills-list-handler.ts +9 -9
  23. package/src/gateway/tools-list-handler.ts +70 -72
  24. package/src/gateway/workclaw-gateway.ts +328 -486
  25. package/src/media/upload.ts +83 -94
  26. package/src/outbound/index.ts +57 -55
  27. package/src/outbound/workclaw-sender.ts +134 -133
  28. package/src/runtime.ts +291 -194
  29. package/src/send.ts +1 -1
  30. package/src/tools/openclaw-workclaw-cron/api/index.ts +6 -6
  31. package/src/tools/openclaw-workclaw-cron/src/add/params.ts +20 -19
  32. package/src/tools/openclaw-workclaw-cron/src/add/sync.ts +2 -2
  33. package/src/tools/openclaw-workclaw-cron/src/disable/params.ts +1 -1
  34. package/src/tools/openclaw-workclaw-cron/src/disable/sync.ts +3 -3
  35. package/src/tools/openclaw-workclaw-cron/src/enable/params.ts +1 -1
  36. package/src/tools/openclaw-workclaw-cron/src/enable/sync.ts +3 -3
  37. package/src/tools/openclaw-workclaw-cron/src/notify/sync.ts +2 -2
  38. package/src/tools/openclaw-workclaw-cron/src/remove/params.ts +1 -1
  39. package/src/tools/openclaw-workclaw-cron/src/remove/sync.ts +3 -3
  40. package/src/tools/openclaw-workclaw-cron/src/update/params.ts +195 -197
  41. package/src/tools/openclaw-workclaw-cron/src/update/sync.ts +4 -4
  42. package/src/tools/openclaw-workclaw-system/src/get/index.ts +2 -2
  43. package/src/tools/openclaw-workclaw-system/src/token/index.ts +4 -4
  44. package/src/types.ts +38 -40
  45. package/src/utils/content.ts +16 -21
  46. package/tests/accounts.test.ts +285 -0
  47. package/tests/message-context.test.ts +313 -0
  48. package/tests/reconnect.test.ts +257 -0
  49. package/tests/workclaw-client.test.ts +112 -0
  50. package/tsconfig.json +8 -5
  51. package/vitest.config.ts +8 -0
package/src/send.ts CHANGED
@@ -1 +1 @@
1
- export { getMessageOpenclawWorkclaw, sendMessageOpenclawWorkclaw } from './outbound/index.js'
1
+ export { sendMessageWorkclaw, getMessageWorkclaw } from "./outbound/index.js";
@@ -3,9 +3,9 @@
3
3
  */
4
4
 
5
5
  import type { OpenClawPluginApi } from 'openclaw/plugin-sdk'
6
- import type { OpenclawWorkclawConnectionConfig } from '../../../connection/workclaw-client.js'
7
- import { resolveOpenclawWorkclawAccount } from '../../../accounts.js'
8
- import { doFetchJson, getOpenclawWorkclawAccessToken, normalizeBaseUrl } from '../../../connection/workclaw-client.js'
6
+ import type { WorkclawConnectionConfig } from '../../../connection/workclaw-client.js'
7
+ import { resolveWorkclawAccountWithCache } from '../../../accounts.js'
8
+ import { doFetchJson, getWorkclawAccessToken, normalizeBaseUrl } from '../../../connection/workclaw-client.js'
9
9
 
10
10
  const DEFAULT_MAX_RETRIES = 3
11
11
  const DEFAULT_RETRY_DELAY_MS = 1000
@@ -115,7 +115,7 @@ export async function callBackendApi(
115
115
  } = {},
116
116
  ): Promise<{ success: boolean, data?: any, error?: string, attempts?: number }> {
117
117
  try {
118
- const account = resolveOpenclawWorkclawAccount({
118
+ const account = resolveWorkclawAccountWithCache({
119
119
  cfg: api.config,
120
120
  accountId,
121
121
  })
@@ -131,13 +131,13 @@ export async function callBackendApi(
131
131
 
132
132
  api.logger.info(`[智小途-定时任务] 正在获取认证 token,accountId=${accountId}`)
133
133
 
134
- const token = await getOpenclawWorkclawAccessToken(tokenCacheKey, {
134
+ const token = await getWorkclawAccessToken(tokenCacheKey, {
135
135
  baseUrl: accConfig.baseUrl,
136
136
  appKey: accConfig.appKey,
137
137
  appSecret: accConfig.appSecret,
138
138
  allowInsecureTls: accConfig.allowInsecureTls,
139
139
  requestTimeout: accConfig.requestTimeout,
140
- } as OpenclawWorkclawConnectionConfig)
140
+ } as WorkclawConnectionConfig)
141
141
 
142
142
  const requestUrl = `${baseUrl}/open-apis${path}`
143
143
  const sanitizedPayload = sanitizePayload(payload)
@@ -22,7 +22,7 @@ const AddParamsSchema = {
22
22
  properties: {
23
23
  time: {
24
24
  type: 'string',
25
- description: '时间描述,支持相对时间(5m、1h)和 cron 表达式(0 8 * * *)',
25
+ description: ' 时间(核心!),支持相对时间和 cron 表达式,工具自动转换',
26
26
  },
27
27
  name: {
28
28
  type: 'string',
@@ -54,25 +54,25 @@ function getOpenclawWorkclawCronAddParamsTool(api: OpenClawPluginApi) {
54
54
  return {
55
55
  name: 'openclaw-workclaw-cron-add-params',
56
56
  label: '智小途生成创建提醒参数',
57
- description: `【必须执行的核心步骤】生成创建提醒的 cronParams 参数。
57
+ description: `【核心工具】生成创建提醒的 cronParams 参数。
58
58
 
59
59
  📋 工作流程(必须按顺序执行):
60
- 1. 调用此工具获取 cronParams 参数
61
- 2. 使用返回的 cronParams 调用 OpenClaw 的 cron 工具
62
- 3. 从 cron 工具返回值中提取 jobId(返回格式:{"success": true, "jobId": "xxx"})
63
- 4. 【关键步骤】必须调用 openclaw-workclaw-cron-add-sync 同步到后端
64
-
65
- 🔒 强制规则(必须遵守):
66
- - 生成的 cronParams schedule.kind 必须是 "cron"
67
- - 调用 cron 工具时必须使用 --cron 参数(绝对禁止使用 --at)
68
- - 严禁将 kind: "cron" 转换为其他类型
69
- - 使用 --session isolated + --message(agentTurn),禁止使用 --system-event
70
-
71
- ⚠️ 重要提醒:
72
- - time 参数支持相对时间(如 "5m"、"1h"),内部会转换为 cron 表达式
73
- - 最终执行的 CLI 命令必须是 --cron 参数(绝对禁止使用 --at)
74
- - 必须从 cron 工具返回值中提取 jobId,不能凭空编造!
75
- - 如果不执行步骤4,后端将无法记录新创建的定时任务!`,
60
+ 1. 调用此工具获取 cronParams 参数
61
+ 2. 使用返回的 cronParams 调用 OpenClaw 的 cron 工具
62
+ 3. 从 cron 工具返回值中提取 jobId(返回格式:{"success": true, "jobId": "xxx"})
63
+ 4. 必须调用 openclaw-workclaw-cron-add-sync 同步到后端
64
+
65
+ 时间格式(核心!):
66
+ - time 参数支持相对时间(如 "5m"、"1h"、"2d")和 cron 表达式
67
+ - 工具会自动将所有时间统一转换为 cron 表达式
68
+ - 严禁使用 --at 参数或其他绝对时间格式
69
+ - 最终 CLI 命令必须使用 --cron 参数
70
+
71
+ 🔒 强制规则:
72
+ - schedule.kind 必须是 "cron"
73
+ - 必须使用 --cron 参数传递 cron 表达式
74
+
75
+ ⚠️ 必须从 cron 工具返回值中提取 jobId,不能凭空编造!`,
76
76
  parameters: AddParamsSchema,
77
77
  execute: async (_toolCallId: string, params: AddParamsParams) => {
78
78
  const p = params as AddParamsParams
@@ -125,8 +125,9 @@ function getOpenclawWorkclawCronAddParamsTool(api: OpenClawPluginApi) {
125
125
  }
126
126
 
127
127
  // 构建 cron 参数
128
- const cronParams = {
128
+ const cronParams: Record<string, any> = {
129
129
  action: 'add' as const,
130
+ account: accountId,
130
131
  job: {
131
132
  name: p.name,
132
133
  schedule: {
@@ -3,7 +3,7 @@
3
3
  */
4
4
 
5
5
  import type { OpenClawPluginApi } from 'openclaw/plugin-sdk'
6
- import { resolveOpenclawWorkclawAccount } from '../../../../accounts.js'
6
+ import { resolveWorkclawAccountWithCache } from '../../../../accounts.js'
7
7
  import { callCronJobAdd } from '../../api/index.js'
8
8
 
9
9
  /**
@@ -120,7 +120,7 @@ function getOpenclawWorkclawCronAddSyncTool(api: OpenClawPluginApi) {
120
120
  return json({ error: '无法获取账户 ID(accountId),请确保已配置账户' })
121
121
  }
122
122
 
123
- const accountConfig = resolveOpenclawWorkclawAccount({
123
+ const accountConfig = resolveWorkclawAccountWithCache({
124
124
  cfg: ctx.config,
125
125
  accountId,
126
126
  })
@@ -19,7 +19,7 @@ const DisableParamsSchema = {
19
19
  properties: {
20
20
  jobId: {
21
21
  type: 'string',
22
- description: 'OpenClaw 任务 ID',
22
+ description: `OpenClaw 任务 ID(必须是数字或字符串,不是 cron 表达式)`,
23
23
  },
24
24
  },
25
25
  required: ['jobId'],
@@ -3,7 +3,7 @@
3
3
  */
4
4
 
5
5
  import type { OpenClawPluginApi } from 'openclaw/plugin-sdk'
6
- import { resolveOpenclawWorkclawAccount } from '../../../../accounts.js'
6
+ import { resolveWorkclawAccountWithCache } from '../../../../accounts.js'
7
7
  import { callCronJobDisabled } from '../../api/index.js'
8
8
 
9
9
  /**
@@ -21,7 +21,7 @@ const DisableSyncSchema = {
21
21
  properties: {
22
22
  jobId: {
23
23
  type: 'string',
24
- description: 'OpenClaw 任务 ID',
24
+ description: `OpenClaw 任务 ID(必须是数字或字符串,不是 cron 表达式)`,
25
25
  },
26
26
  },
27
27
  required: ['jobId'],
@@ -65,7 +65,7 @@ function getOpenclawWorkclawCronDisableSyncTool(api: OpenClawPluginApi) {
65
65
  return json({ error: '无法获取账户 ID(accountId),请确保已配置账户' })
66
66
  }
67
67
 
68
- const accountConfig = resolveOpenclawWorkclawAccount({
68
+ const accountConfig = resolveWorkclawAccountWithCache({
69
69
  cfg: ctx.config,
70
70
  accountId,
71
71
  })
@@ -19,7 +19,7 @@ const EnableParamsSchema = {
19
19
  properties: {
20
20
  jobId: {
21
21
  type: 'string',
22
- description: 'OpenClaw 任务 ID',
22
+ description: `OpenClaw 任务 ID(必须是数字或字符串,不是 cron 表达式)`,
23
23
  },
24
24
  },
25
25
  required: ['jobId'],
@@ -3,7 +3,7 @@
3
3
  */
4
4
 
5
5
  import type { OpenClawPluginApi } from 'openclaw/plugin-sdk'
6
- import { resolveOpenclawWorkclawAccount } from '../../../../accounts.js'
6
+ import { resolveWorkclawAccountWithCache } from '../../../../accounts.js'
7
7
  import { callCronJobEnabled } from '../../api/index.js'
8
8
 
9
9
  /**
@@ -21,7 +21,7 @@ const EnableSyncSchema = {
21
21
  properties: {
22
22
  jobId: {
23
23
  type: 'string',
24
- description: 'OpenClaw 任务 ID',
24
+ description: `OpenClaw 任务 ID(必须是数字或字符串,不是 cron 表达式)`,
25
25
  },
26
26
  },
27
27
  required: ['jobId'],
@@ -65,7 +65,7 @@ function getOpenclawWorkclawCronEnableSyncTool(api: OpenClawPluginApi) {
65
65
  return json({ error: '无法获取账户 ID(accountId),请确保已配置账户' })
66
66
  }
67
67
 
68
- const accountConfig = resolveOpenclawWorkclawAccount({
68
+ const accountConfig = resolveWorkclawAccountWithCache({
69
69
  cfg: ctx.config,
70
70
  accountId,
71
71
  })
@@ -5,7 +5,7 @@
5
5
  */
6
6
 
7
7
  import type { OpenClawPluginApi } from 'openclaw/plugin-sdk'
8
- import { resolveOpenclawWorkclawAccount } from '../../../../accounts.js'
8
+ import { resolveWorkclawAccountWithCache } from '../../../../accounts.js'
9
9
  import { callCronJobMessage } from '../../api/index.js'
10
10
 
11
11
  /**
@@ -76,7 +76,7 @@ function getOpenclawWorkclawCronNotifySyncTool(api: OpenClawPluginApi) {
76
76
  return json({ error: '无法获取账户 ID(accountId),请确保已配置账户' })
77
77
  }
78
78
 
79
- const accountConfig = resolveOpenclawWorkclawAccount({
79
+ const accountConfig = resolveWorkclawAccountWithCache({
80
80
  cfg: ctx.config,
81
81
  accountId,
82
82
  })
@@ -19,7 +19,7 @@ const RemoveParamsSchema = {
19
19
  properties: {
20
20
  jobId: {
21
21
  type: 'string',
22
- description: 'OpenClaw 任务 ID',
22
+ description: `OpenClaw 任务 ID(必须是数字或字符串,不是 cron 表达式)`,
23
23
  },
24
24
  },
25
25
  required: ['jobId'],
@@ -3,7 +3,7 @@
3
3
  */
4
4
 
5
5
  import type { OpenClawPluginApi } from 'openclaw/plugin-sdk'
6
- import { resolveOpenclawWorkclawAccount } from '../../../../accounts.js'
6
+ import { resolveWorkclawAccountWithCache } from '../../../../accounts.js'
7
7
  import { callCronJobRemove } from '../../api/index.js'
8
8
 
9
9
  /**
@@ -21,7 +21,7 @@ const RemoveSyncSchema = {
21
21
  properties: {
22
22
  jobId: {
23
23
  type: 'string',
24
- description: 'OpenClaw 任务 ID',
24
+ description: `OpenClaw 任务 ID(必须是数字或字符串,不是 cron 表达式)`,
25
25
  },
26
26
  },
27
27
  required: ['jobId'],
@@ -65,7 +65,7 @@ function getOpenclawWorkclawCronRemoveSyncTool(api: OpenClawPluginApi) {
65
65
  return json({ error: '无法获取账户 ID(accountId),请确保已配置账户' })
66
66
  }
67
67
 
68
- const accountConfig = resolveOpenclawWorkclawAccount({
68
+ const accountConfig = resolveWorkclawAccountWithCache({
69
69
  cfg: ctx.config,
70
70
  accountId,
71
71
  })
@@ -1,197 +1,195 @@
1
- /**
2
- * 智小途定时任务工具 - 生成修改定时任务参数
3
- */
4
-
5
- import type { OpenClawPluginApi } from 'openclaw/plugin-sdk'
6
- import {
7
- isCronExpression,
8
- relativeToCron,
9
- validateTimeFormat,
10
- } from '../../utils/index.js'
11
-
12
- /**
13
- * 生成修改定时任务参数
14
- */
15
- export interface UpdateParamsParams {
16
- jobId: string
17
- time: string
18
- name: string
19
- message: string
20
- }
21
-
22
- /**
23
- * 参数 Schema 定义
24
- */
25
- const UpdateParamsSchema = {
26
- type: 'object',
27
- properties: {
28
- jobId: {
29
- type: 'string',
30
- description: 'OpenClaw 任务 ID',
31
- },
32
- time: {
33
- type: 'string',
34
- description:
35
- '时间描述。相对时间如 "5m"、"1h";cron 表达式如 "0 8 * * *"',
36
- },
37
- name: {
38
- type: 'string',
39
- description: '任务名称',
40
- },
41
- message: {
42
- type: 'string',
43
- description: '提醒内容',
44
- },
45
- },
46
- required: ['jobId', 'time', 'name', 'message'],
47
- } as const
48
-
49
- /**
50
- * 构造工具返回结果
51
- */
52
- function json(data: unknown): any {
53
- return {
54
- content: [{ type: 'text' as const, text: JSON.stringify(data, null, 2) }],
55
- details: data,
56
- }
57
- }
58
-
59
- /**
60
- * 获取生成修改定时任务参数工具
61
- */
62
- function getOpenclawWorkclawCronUpdateParamsTool(api: OpenClawPluginApi) {
63
- return (ctx: any) => {
64
- return {
65
- name: 'openclaw-workclaw-cron-update-params',
66
- label: '智小途生成修改提醒参数',
67
- description: `【必须执行的核心步骤】生成修改提醒的 cronParams 参数。
68
-
69
- 📋 工作流程(必须按顺序执行):
70
- 1. 调用此工具获取 cronParams 参数
71
- 2. 使用返回的 cronParams 调用 OpenClaw 的 cron 工具
72
- 3. ✅ 【关键步骤】必须调用 openclaw-workclaw-cron-update-sync 同步到后端
73
-
74
- 🔒 强制规则(必须遵守):
75
- - 生成的 cronParams 中 schedule.kind 必须是 "cron"
76
- - 调用 cron 工具时必须使用 --cron 参数
77
- - 严禁将 cron 表达式转换为 --at 绝对时间参数
78
- - ❌ 严禁将 kind: "cron" 转换为其他类型
79
- - ✅ 使用 --session isolated + --message(agentTurn),禁止使用 --system-event
80
-
81
- ⚠️ 重要提醒:
82
- - time 参数支持相对时间(如 "10m"、"1h"),内部会转换为 cron 表达式
83
- - 最终执行的 CLI 命令必须是 --cron 参数(绝对禁止使用 --at)
84
- - 必须从 cron 工具返回值中确认更新成功
85
- - 如果不执行步骤3,后端的定时任务信息将无法同步更新!
86
- `,
87
- parameters: UpdateParamsSchema,
88
- execute: async (_toolCallId: string, params: UpdateParamsParams) => {
89
- const p = params as UpdateParamsParams
90
-
91
- api.logger.info(`[智小途-生成修改参数] 开始处理 jobId=${p.jobId} name=${p.name} time=${p.time}`)
92
-
93
- // 参数校验
94
- if (!p.jobId) {
95
- api.logger.warn('[智小途-生成修改参数] 缺少 jobId 参数')
96
- return json({ error: 'jobId 为必填参数' })
97
- }
98
- if (!p.time) {
99
- api.logger.warn('[智小途-生成修改参数] 缺少时间参数')
100
- return json({ error: 'time(时间)为必填参数' })
101
- }
102
- if (!p.name) {
103
- api.logger.warn('[智小途-生成修改参数] 缺少任务名称参数')
104
- return json({ error: 'name(任务名称)为必填参数' })
105
- }
106
- if (!p.message) {
107
- api.logger.warn('[智小途-生成修改参数] 缺少提醒内容参数')
108
- return json({ error: 'message(提醒内容)为必填参数' })
109
- }
110
-
111
- // ctx 获取必要参数
112
- const accountId = ctx?.agentAccountId
113
- const name = p.name
114
- const message = p.message
115
- const time = p.time
116
-
117
- // 校验必要参数
118
- if (!accountId) {
119
- api.logger.warn('[智小途-生成修改参数] 无法获取账户 ID')
120
- return json({ error: '无法获取账户 ID(accountId),请确保已配置账户' })
121
- }
122
-
123
- api.logger.info(`[智小途-生成修改参数] accountId=${accountId} jobId=${p.jobId}`)
124
-
125
- // 校验时间格式
126
- const timeValidation = validateTimeFormat(time)
127
- if (!timeValidation.valid) {
128
- api.logger.warn(`[智小途-生成修改参数] 时间格式校验失败: ${timeValidation.error}`)
129
- return json({ error: timeValidation.error })
130
- }
131
-
132
- // 判断时间类型
133
- const isCron = isCronExpression(time)
134
- let isOneTime = false
135
- let cronExpr: string
136
-
137
- // 如果是相对时间(5m、1h 等),转换为绝对时间的 cron 表达式
138
- if (!isCron) {
139
- isOneTime = true
140
- cronExpr = relativeToCron(time.trim())
141
- api.logger.info(`[智小途-生成修改参数] 相对时间转换为绝对时间 cron 表达式 cronExpr=${cronExpr}`)
142
- }
143
- else {
144
- cronExpr = time.trim()
145
- }
146
-
147
- const cronParams = {
148
- action: 'update' as const,
149
- jobId: p.jobId,
150
- job: {
151
- name,
152
- schedule: {
153
- kind: 'cron' as const,
154
- expr: cronExpr,
155
- tz: 'Asia/Shanghai',
156
- },
157
- sessionTarget: 'isolated',
158
- wakeMode: 'now' as const,
159
- deleteAfterRun: isOneTime ? true : undefined,
160
- payload: {
161
- kind: 'agentTurn' as const,
162
- message,
163
- },
164
- delivery: {
165
- mode: 'announce',
166
- channel: 'openclaw-workclaw',
167
- },
168
- },
169
- }
170
-
171
- api.logger.info(`[智小途-生成修改参数] 构建 cron 参数 cronParams=${JSON.stringify(cronParams)}`)
172
-
173
- return json({
174
- cronParams,
175
- meta: {
176
- accountId,
177
- jobId: p.jobId,
178
- name,
179
- kind: 'cron',
180
- expr: cronExpr,
181
- message,
182
- },
183
- summary: `✅ 提醒已修改: "${name}" (${time})`,
184
- })
185
- },
186
- }
187
- }
188
- }
189
-
190
- /**
191
- * 注册生成修改定时任务参数工具
192
- */
193
- export function registerOpenclawWorkclawCronUpdateParamsTool(api: OpenClawPluginApi): void {
194
- (api.registerTool as any)(getOpenclawWorkclawCronUpdateParamsTool(api), {
195
- name: 'openclaw-workclaw-cron-update-params',
196
- })
197
- }
1
+ /**
2
+ * 智小途定时任务工具 - 生成修改定时任务参数
3
+ */
4
+
5
+ import type { OpenClawPluginApi } from 'openclaw/plugin-sdk'
6
+ import {
7
+ isCronExpression,
8
+ relativeToCron,
9
+ validateTimeFormat,
10
+ } from '../../utils/index.js'
11
+
12
+ /**
13
+ * 生成修改定时任务参数
14
+ */
15
+ export interface UpdateParamsParams {
16
+ jobId: string
17
+ time: string
18
+ name: string
19
+ message: string
20
+ }
21
+
22
+ /**
23
+ * 参数 Schema 定义
24
+ */
25
+ const UpdateParamsSchema = {
26
+ type: 'object',
27
+ properties: {
28
+ jobId: {
29
+ type: 'string',
30
+ description: `OpenClaw 任务 ID(必须是数字或字符串,不是 cron 表达式)`,
31
+ },
32
+ time: {
33
+ type: 'string',
34
+ description: '⏰ 时间(核心!),支持相对时间和 cron 表达式,工具自动转换',
35
+ },
36
+ name: {
37
+ type: 'string',
38
+ description: '任务名称',
39
+ },
40
+ message: {
41
+ type: 'string',
42
+ description: '提醒内容',
43
+ },
44
+ },
45
+ required: ['jobId', 'time', 'name', 'message'],
46
+ } as const
47
+
48
+ /**
49
+ * 构造工具返回结果
50
+ */
51
+ function json(data: unknown): any {
52
+ return {
53
+ content: [{ type: 'text' as const, text: JSON.stringify(data, null, 2) }],
54
+ details: data,
55
+ }
56
+ }
57
+
58
+ /**
59
+ * 获取生成修改定时任务参数工具
60
+ */
61
+ function getOpenclawWorkclawCronUpdateParamsTool(api: OpenClawPluginApi) {
62
+ return (ctx: any) => {
63
+ return {
64
+ name: 'openclaw-workclaw-cron-update-params',
65
+ label: '智小途生成修改提醒参数',
66
+ description: `【核心工具】生成修改提醒的 cronParams 参数。
67
+
68
+ 📋 工作流程(必须按顺序执行):
69
+ 1. 调用此工具获取 cronParams 参数
70
+ 2. 使用返回的 cronParams 调用 OpenClaw 的 cron 工具
71
+ 3. 必须调用 openclaw-workclaw-cron-update-sync 同步到后端
72
+
73
+ ⏰ 时间格式(核心!):
74
+ - time 参数支持相对时间(如 "10m"、"1h"、"2d")和 cron 表达式
75
+ - 工具会自动将所有时间统一转换为 cron 表达式
76
+ - 严禁使用 --at 参数或其他绝对时间格式
77
+ - 最终 CLI 命令必须使用 --cron 参数
78
+
79
+ 🔒 强制规则:
80
+ - schedule.kind 必须是 "cron"
81
+ - 必须使用 --cron 参数传递 cron 表达式
82
+
83
+ ⚠️ 必须从 cron 工具返回值中确认更新成功`,
84
+ parameters: UpdateParamsSchema,
85
+ execute: async (_toolCallId: string, params: UpdateParamsParams) => {
86
+ const p = params as UpdateParamsParams
87
+
88
+ api.logger.info(`[智小途-生成修改参数] 开始处理 jobId=${p.jobId} name=${p.name} time=${p.time}`)
89
+
90
+ // 参数校验
91
+ if (!p.jobId) {
92
+ api.logger.warn('[智小途-生成修改参数] 缺少 jobId 参数')
93
+ return json({ error: 'jobId 为必填参数' })
94
+ }
95
+ if (!p.time) {
96
+ api.logger.warn('[智小途-生成修改参数] 缺少时间参数')
97
+ return json({ error: 'time(时间)为必填参数' })
98
+ }
99
+ if (!p.name) {
100
+ api.logger.warn('[智小途-生成修改参数] 缺少任务名称参数')
101
+ return json({ error: 'name(任务名称)为必填参数' })
102
+ }
103
+ if (!p.message) {
104
+ api.logger.warn('[智小途-生成修改参数] 缺少提醒内容参数')
105
+ return json({ error: 'message(提醒内容)为必填参数' })
106
+ }
107
+
108
+ // ctx 获取必要参数
109
+ const accountId = ctx?.agentAccountId
110
+ const name = p.name
111
+ const message = p.message
112
+ const time = p.time
113
+
114
+ // 校验必要参数
115
+ if (!accountId) {
116
+ api.logger.warn('[智小途-生成修改参数] 无法获取账户 ID')
117
+ return json({ error: '无法获取账户 ID(accountId),请确保已配置账户' })
118
+ }
119
+
120
+ api.logger.info(`[智小途-生成修改参数] accountId=${accountId} jobId=${p.jobId}`)
121
+
122
+ // 校验时间格式
123
+ const timeValidation = validateTimeFormat(time)
124
+ if (!timeValidation.valid) {
125
+ api.logger.warn(`[智小途-生成修改参数] 时间格式校验失败: ${timeValidation.error}`)
126
+ return json({ error: timeValidation.error })
127
+ }
128
+
129
+ // 判断时间类型
130
+ const isCron = isCronExpression(time)
131
+ let isOneTime = false
132
+ let cronExpr: string
133
+
134
+ // 如果是相对时间(5m、1h 等),转换为绝对时间的 cron 表达式
135
+ if (!isCron) {
136
+ isOneTime = true
137
+ cronExpr = relativeToCron(time.trim())
138
+ api.logger.info(`[智小途-生成修改参数] 相对时间转换为绝对时间 cron 表达式 cronExpr=${cronExpr}`)
139
+ }
140
+ else {
141
+ cronExpr = time.trim()
142
+ }
143
+
144
+ const cronParams: Record<string, any> = {
145
+ action: 'update' as const,
146
+ account: accountId,
147
+ jobId: p.jobId,
148
+ job: {
149
+ name,
150
+ schedule: {
151
+ kind: 'cron' as const,
152
+ expr: cronExpr,
153
+ tz: 'Asia/Shanghai',
154
+ },
155
+ sessionTarget: 'isolated',
156
+ wakeMode: 'now' as const,
157
+ deleteAfterRun: isOneTime ? true : undefined,
158
+ payload: {
159
+ kind: 'agentTurn' as const,
160
+ message,
161
+ },
162
+ delivery: {
163
+ mode: 'announce',
164
+ channel: 'openclaw-workclaw',
165
+ },
166
+ },
167
+ }
168
+
169
+ api.logger.info(`[智小途-生成修改参数] 构建 cron 参数 cronParams=${JSON.stringify(cronParams)}`)
170
+
171
+ return json({
172
+ cronParams,
173
+ meta: {
174
+ accountId,
175
+ jobId: p.jobId,
176
+ name,
177
+ kind: 'cron',
178
+ expr: cronExpr,
179
+ message,
180
+ },
181
+ summary: `✅ 提醒已修改: "${name}" (${time})`,
182
+ })
183
+ },
184
+ }
185
+ }
186
+ }
187
+
188
+ /**
189
+ * 注册生成修改定时任务参数工具
190
+ */
191
+ export function registerOpenclawWorkclawCronUpdateParamsTool(api: OpenClawPluginApi): void {
192
+ (api.registerTool as any)(getOpenclawWorkclawCronUpdateParamsTool(api), {
193
+ name: 'openclaw-workclaw-cron-update-params',
194
+ })
195
+ }