@workclaw/openclaw-workclaw 1.0.0
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.
- package/README.md +325 -0
- package/index.ts +298 -0
- package/openclaw.plugin.json +10 -0
- package/package.json +43 -0
- package/skills/openclaw-workclaw-cron/SKILL.md +458 -0
- package/src/accounts.ts +287 -0
- package/src/api/accounts-api.ts +157 -0
- package/src/api/prompts-api.ts +123 -0
- package/src/api/session-api.ts +247 -0
- package/src/api/skills-api.ts +74 -0
- package/src/api/workspace.ts +43 -0
- package/src/channel.ts +227 -0
- package/src/config-schema.ts +110 -0
- package/src/connection/workclaw-client.ts +656 -0
- package/src/gateway/agent-handlers.ts +557 -0
- package/src/gateway/config-writer.ts +311 -0
- package/src/gateway/message-context.ts +422 -0
- package/src/gateway/message-dispatcher.ts +601 -0
- package/src/gateway/reconnect.ts +149 -0
- package/src/gateway/skills-handler.ts +759 -0
- package/src/gateway/skills-list-handler.ts +332 -0
- package/src/gateway/tools-list-handler.ts +162 -0
- package/src/gateway/workclaw-gateway.ts +521 -0
- package/src/media/upload.ts +168 -0
- package/src/outbound/index.ts +183 -0
- package/src/outbound/workclaw-sender.ts +157 -0
- package/src/runtime.ts +400 -0
- package/src/send.ts +1 -0
- package/src/tools/openclaw-workclaw-cron/api/index.ts +326 -0
- package/src/tools/openclaw-workclaw-cron/index.ts +39 -0
- package/src/tools/openclaw-workclaw-cron/src/add/params.ts +176 -0
- package/src/tools/openclaw-workclaw-cron/src/add/sync.ts +188 -0
- package/src/tools/openclaw-workclaw-cron/src/disable/params.ts +100 -0
- package/src/tools/openclaw-workclaw-cron/src/disable/sync.ts +127 -0
- package/src/tools/openclaw-workclaw-cron/src/enable/params.ts +100 -0
- package/src/tools/openclaw-workclaw-cron/src/enable/sync.ts +127 -0
- package/src/tools/openclaw-workclaw-cron/src/notify/sync.ts +148 -0
- package/src/tools/openclaw-workclaw-cron/src/remove/params.ts +109 -0
- package/src/tools/openclaw-workclaw-cron/src/remove/sync.ts +127 -0
- package/src/tools/openclaw-workclaw-cron/src/update/params.ts +197 -0
- package/src/tools/openclaw-workclaw-cron/src/update/sync.ts +161 -0
- package/src/tools/openclaw-workclaw-cron/types/index.ts +55 -0
- package/src/tools/openclaw-workclaw-cron/utils/index.ts +141 -0
- package/src/types.ts +60 -0
- package/src/utils/content.ts +40 -0
- package/templates/IDENTITY.md +14 -0
- package/templates/SOUL.md +0 -0
- package/tsconfig.json +11 -0
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 智小途定时任务工具 - 后端接口封装
|
|
3
|
+
*/
|
|
4
|
+
|
|
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'
|
|
9
|
+
|
|
10
|
+
const DEFAULT_MAX_RETRIES = 3
|
|
11
|
+
const DEFAULT_RETRY_DELAY_MS = 1000
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 睡眠函数
|
|
15
|
+
*/
|
|
16
|
+
function sleep(ms: number): Promise<void> {
|
|
17
|
+
return new Promise(resolve => setTimeout(resolve, ms))
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 带重试机制的 fetch 调用
|
|
22
|
+
*/
|
|
23
|
+
async function fetchWithRetry<T>(
|
|
24
|
+
fetchFn: () => Promise<T>,
|
|
25
|
+
options: {
|
|
26
|
+
maxRetries?: number
|
|
27
|
+
retryDelayMs?: number
|
|
28
|
+
retryableErrors?: string[]
|
|
29
|
+
} = {},
|
|
30
|
+
): Promise<{ data?: T, error?: string, attempts: number }> {
|
|
31
|
+
const maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES
|
|
32
|
+
const retryDelayMs = options.retryDelayMs ?? DEFAULT_RETRY_DELAY_MS
|
|
33
|
+
const retryableErrors = options.retryableErrors ?? [
|
|
34
|
+
'ECONNREFUSED',
|
|
35
|
+
'ETIMEDOUT',
|
|
36
|
+
'ENOTFOUND',
|
|
37
|
+
'ENETUNREACH',
|
|
38
|
+
'EAI_AGAIN',
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
let lastError: string = ''
|
|
42
|
+
let attempts = 0
|
|
43
|
+
|
|
44
|
+
for (let i = 0; i <= maxRetries; i++) {
|
|
45
|
+
attempts = i + 1
|
|
46
|
+
try {
|
|
47
|
+
const data = await fetchFn()
|
|
48
|
+
return { data, attempts }
|
|
49
|
+
}
|
|
50
|
+
catch (error: any) {
|
|
51
|
+
lastError = error.message || String(error)
|
|
52
|
+
|
|
53
|
+
const isRetryable = retryableErrors.some(
|
|
54
|
+
e => lastError.toLowerCase().includes(e.toLowerCase()),
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
if (i < maxRetries && isRetryable) {
|
|
58
|
+
const delay = retryDelayMs * 2 ** i
|
|
59
|
+
await sleep(delay)
|
|
60
|
+
continue
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
break
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return { error: lastError, attempts }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 脱敏日志输出(隐藏敏感字段)
|
|
72
|
+
*/
|
|
73
|
+
function sanitizePayload(payload: Record<string, any>): Record<string, any> {
|
|
74
|
+
const sensitiveFields = [
|
|
75
|
+
'password',
|
|
76
|
+
'secret',
|
|
77
|
+
'token',
|
|
78
|
+
'authorization',
|
|
79
|
+
'appSecret',
|
|
80
|
+
'appSecret',
|
|
81
|
+
]
|
|
82
|
+
|
|
83
|
+
const sanitized: Record<string, any> = {}
|
|
84
|
+
for (const [key, value] of Object.entries(payload)) {
|
|
85
|
+
const lowerKey = key.toLowerCase()
|
|
86
|
+
if (sensitiveFields.some(f => lowerKey.includes(f))) {
|
|
87
|
+
sanitized[key] = '***'
|
|
88
|
+
}
|
|
89
|
+
else if (typeof value === 'object' && value !== null) {
|
|
90
|
+
sanitized[key] = sanitizePayload(value as Record<string, any>)
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
sanitized[key] = value
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return sanitized
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 调用后端 API 的通用方法
|
|
101
|
+
* @param api - OpenClaw API 实例
|
|
102
|
+
* @param accountId - 账户 ID
|
|
103
|
+
* @param path - API 路径
|
|
104
|
+
* @param payload - 请求体
|
|
105
|
+
* @param options - 可选配置
|
|
106
|
+
* @returns 接口调用结果
|
|
107
|
+
*/
|
|
108
|
+
export async function callBackendApi(
|
|
109
|
+
api: OpenClawPluginApi,
|
|
110
|
+
accountId: string,
|
|
111
|
+
path: string,
|
|
112
|
+
payload: Record<string, any>,
|
|
113
|
+
options: {
|
|
114
|
+
maxRetries?: number
|
|
115
|
+
} = {},
|
|
116
|
+
): Promise<{ success: boolean, data?: any, error?: string, attempts?: number }> {
|
|
117
|
+
try {
|
|
118
|
+
const account = resolveOpenclawWorkclawAccount({
|
|
119
|
+
cfg: api.config,
|
|
120
|
+
accountId,
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
if (!account.configured) {
|
|
124
|
+
api.logger.error(`[智小途-定时任务] 账户未配置 accountId=${accountId}`)
|
|
125
|
+
return { success: false, error: 'Account not configured' }
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const accConfig = account.config as any
|
|
129
|
+
const tokenCacheKey = accConfig.appKey || accountId
|
|
130
|
+
const baseUrl = normalizeBaseUrl(accConfig.baseUrl)
|
|
131
|
+
|
|
132
|
+
api.logger.info(`[智小途-定时任务] 正在获取认证 token,accountId=${accountId}`)
|
|
133
|
+
|
|
134
|
+
const token = await getOpenclawWorkclawAccessToken(tokenCacheKey, {
|
|
135
|
+
baseUrl: accConfig.baseUrl,
|
|
136
|
+
appKey: accConfig.appKey,
|
|
137
|
+
appSecret: accConfig.appSecret,
|
|
138
|
+
allowInsecureTls: accConfig.allowInsecureTls,
|
|
139
|
+
requestTimeout: accConfig.requestTimeout,
|
|
140
|
+
} as OpenclawWorkclawConnectionConfig)
|
|
141
|
+
|
|
142
|
+
const requestUrl = `${baseUrl}/open-apis${path}`
|
|
143
|
+
const sanitizedPayload = sanitizePayload(payload)
|
|
144
|
+
api.logger.info(`[智小途-定时任务] 调用接口 ${requestUrl}`)
|
|
145
|
+
api.logger.info(`[智小途-定时任务] 请求参数 ${JSON.stringify(sanitizedPayload)}`)
|
|
146
|
+
|
|
147
|
+
const maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES
|
|
148
|
+
|
|
149
|
+
const fetchResult = await fetchWithRetry(
|
|
150
|
+
() =>
|
|
151
|
+
doFetchJson(
|
|
152
|
+
requestUrl,
|
|
153
|
+
{
|
|
154
|
+
method: 'POST',
|
|
155
|
+
headers: {
|
|
156
|
+
'Content-Type': 'application/json',
|
|
157
|
+
'Authorization': `Bearer ${token}`,
|
|
158
|
+
},
|
|
159
|
+
body: JSON.stringify(payload),
|
|
160
|
+
},
|
|
161
|
+
accConfig.allowInsecureTls,
|
|
162
|
+
accConfig.requestTimeout,
|
|
163
|
+
),
|
|
164
|
+
{ maxRetries },
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
if (fetchResult.error) {
|
|
168
|
+
api.logger.error(`[智小途-定时任务] 接口调用失败(已重试 ${fetchResult.attempts} 次): ${fetchResult.error}`)
|
|
169
|
+
return { success: false, error: fetchResult.error, attempts: fetchResult.attempts }
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const data = fetchResult.data
|
|
173
|
+
api.logger.info(`[智小途-定时任务] 接口响应(尝试 ${fetchResult.attempts} 次): ${JSON.stringify(data)}`)
|
|
174
|
+
|
|
175
|
+
if (data?.code === 200 || data?.code === 0) {
|
|
176
|
+
return { success: true, data, attempts: fetchResult.attempts }
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
const errorMsg = data?.message || data?.msg || 'Unknown error'
|
|
180
|
+
api.logger.error(`[智小途-定时任务] 接口返回错误 ${errorMsg}`)
|
|
181
|
+
return { success: false, error: errorMsg }
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
catch (error: any) {
|
|
185
|
+
api.logger.error(`[智小途-定时任务] 接口调用异常 ${error.message}`)
|
|
186
|
+
return { success: false, error: error.message }
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* 调用新增定时任务接口
|
|
192
|
+
*/
|
|
193
|
+
export async function callCronJobAdd(
|
|
194
|
+
api: OpenClawPluginApi,
|
|
195
|
+
accountId: string,
|
|
196
|
+
payload: {
|
|
197
|
+
jobId: string
|
|
198
|
+
name: string
|
|
199
|
+
kind: 'cron'
|
|
200
|
+
expr?: string
|
|
201
|
+
message: string
|
|
202
|
+
messageId?: string
|
|
203
|
+
},
|
|
204
|
+
): Promise<{ success: boolean, data?: any, error?: string }> {
|
|
205
|
+
api.logger.info(`[智小途-定时任务] 新增定时任务 jobId=${payload.jobId} name=${payload.name}`)
|
|
206
|
+
|
|
207
|
+
const result = await callBackendApi(api, accountId, '/cron/job/add', {
|
|
208
|
+
clawJobId: payload.jobId,
|
|
209
|
+
name: payload.name,
|
|
210
|
+
kind: payload.kind,
|
|
211
|
+
expr: payload.expr || '',
|
|
212
|
+
message: payload.message,
|
|
213
|
+
agentId: payload.messageId || '0',
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
return { success: result.success, data: result.data, error: result.error }
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* 调用删除定时任务接口
|
|
221
|
+
*/
|
|
222
|
+
export async function callCronJobRemove(
|
|
223
|
+
api: OpenClawPluginApi,
|
|
224
|
+
accountId: string,
|
|
225
|
+
payload: {
|
|
226
|
+
jobId: string
|
|
227
|
+
},
|
|
228
|
+
): Promise<{ success: boolean, data?: any, error?: string }> {
|
|
229
|
+
api.logger.info(`[智小途-定时任务] 删除定时任务 jobId=${payload.jobId}`)
|
|
230
|
+
|
|
231
|
+
const result = await callBackendApi(api, accountId, '/cron/job/del', {
|
|
232
|
+
clawJobId: payload.jobId,
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
return { success: result.success, data: result.data, error: result.error }
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* 调用更新定时任务接口
|
|
240
|
+
*/
|
|
241
|
+
export async function callCronJobUpdate(
|
|
242
|
+
api: OpenClawPluginApi,
|
|
243
|
+
accountId: string,
|
|
244
|
+
payload: {
|
|
245
|
+
jobId: string
|
|
246
|
+
name: string
|
|
247
|
+
kind: 'cron'
|
|
248
|
+
expr?: string
|
|
249
|
+
message: string
|
|
250
|
+
agentId?: string
|
|
251
|
+
},
|
|
252
|
+
): Promise<{ success: boolean, data?: any, error?: string }> {
|
|
253
|
+
api.logger.info(`[智小途-定时任务] 更新定时任务 jobId=${payload.jobId} name=${payload.name} agentId=${payload.agentId}`)
|
|
254
|
+
|
|
255
|
+
const result = await callBackendApi(api, accountId, '/cron/job/update', {
|
|
256
|
+
clawJobId: payload.jobId,
|
|
257
|
+
name: payload.name,
|
|
258
|
+
kind: payload.kind,
|
|
259
|
+
expr: payload.expr || '',
|
|
260
|
+
message: payload.message,
|
|
261
|
+
agentId: payload.agentId || '0',
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
return { success: result.success, data: result.data, error: result.error }
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* 调用启用定时任务接口
|
|
269
|
+
*/
|
|
270
|
+
export async function callCronJobEnabled(
|
|
271
|
+
api: OpenClawPluginApi,
|
|
272
|
+
accountId: string,
|
|
273
|
+
payload: {
|
|
274
|
+
jobId: string
|
|
275
|
+
},
|
|
276
|
+
): Promise<{ success: boolean, data?: any, error?: string }> {
|
|
277
|
+
api.logger.info(`[智小途-定时任务] 启用定时任务 jobId=${payload.jobId}`)
|
|
278
|
+
|
|
279
|
+
const result = await callBackendApi(api, accountId, '/cron/job/enabled', {
|
|
280
|
+
clawJobId: payload.jobId,
|
|
281
|
+
})
|
|
282
|
+
|
|
283
|
+
return { success: result.success, data: result.data, error: result.error }
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* 调用禁用定时任务接口
|
|
288
|
+
*/
|
|
289
|
+
export async function callCronJobDisabled(
|
|
290
|
+
api: OpenClawPluginApi,
|
|
291
|
+
accountId: string,
|
|
292
|
+
payload: {
|
|
293
|
+
jobId: string
|
|
294
|
+
},
|
|
295
|
+
): Promise<{ success: boolean, data?: any, error?: string }> {
|
|
296
|
+
api.logger.info(`[智小途-定时任务] 禁用定时任务 jobId=${payload.jobId}`)
|
|
297
|
+
|
|
298
|
+
const result = await callBackendApi(api, accountId, '/cron/job/disabled', {
|
|
299
|
+
clawJobId: payload.jobId,
|
|
300
|
+
})
|
|
301
|
+
|
|
302
|
+
return { success: result.success, data: result.data, error: result.error }
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* 调用定时任务触发通知接口
|
|
307
|
+
*/
|
|
308
|
+
export async function callCronJobMessage(
|
|
309
|
+
api: OpenClawPluginApi,
|
|
310
|
+
accountId: string,
|
|
311
|
+
payload: {
|
|
312
|
+
jobId: string
|
|
313
|
+
message: string
|
|
314
|
+
agentId?: string
|
|
315
|
+
},
|
|
316
|
+
): Promise<{ success: boolean, data?: any, error?: string }> {
|
|
317
|
+
api.logger.info(`[智小途-定时任务] 定时任务触发通知 jobId=${payload.jobId} message=${payload.message} agentId=${payload.agentId}`)
|
|
318
|
+
|
|
319
|
+
const result = await callBackendApi(api, accountId, '/cron/job/message', {
|
|
320
|
+
clawJobId: payload.jobId,
|
|
321
|
+
message: payload.message,
|
|
322
|
+
agentId: payload.agentId || '0',
|
|
323
|
+
})
|
|
324
|
+
|
|
325
|
+
return { success: result.success, data: result.data, error: result.error }
|
|
326
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 智小途定时任务工具 - 统一出口
|
|
3
|
+
*
|
|
4
|
+
* 提供统一的工具注册入口,一次性注册所有定时任务相关工具
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { registerOpenclawWorkclawCronAddParamsTool } from './src/add/params.js'
|
|
8
|
+
import { registerOpenclawWorkclawCronAddSyncTool } from './src/add/sync.js'
|
|
9
|
+
import { registerOpenclawWorkclawCronDisableParamsTool } from './src/disable/params.js'
|
|
10
|
+
import { registerOpenclawWorkclawCronDisableSyncTool } from './src/disable/sync.js'
|
|
11
|
+
import { registerOpenclawWorkclawCronEnableParamsTool } from './src/enable/params.js'
|
|
12
|
+
|
|
13
|
+
import { registerOpenclawWorkclawCronEnableSyncTool } from './src/enable/sync.js'
|
|
14
|
+
import { registerOpenclawWorkclawCronNotifySyncTool } from './src/notify/sync.js'
|
|
15
|
+
import { registerOpenclawWorkclawCronRemoveParamsTool } from './src/remove/params.js'
|
|
16
|
+
import { registerOpenclawWorkclawCronRemoveSyncTool } from './src/remove/sync.js'
|
|
17
|
+
import { registerOpenclawWorkclawCronUpdateParamsTool } from './src/update/params.js'
|
|
18
|
+
|
|
19
|
+
import { registerOpenclawWorkclawCronUpdateSyncTool } from './src/update/sync.js'
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 统一注册所有定时任务工具
|
|
23
|
+
* @param api - OpenClaw API 实例
|
|
24
|
+
*/
|
|
25
|
+
export function registerAllOpenclawWorkclawCronTools(api: any): void {
|
|
26
|
+
registerOpenclawWorkclawCronAddParamsTool(api)
|
|
27
|
+
registerOpenclawWorkclawCronRemoveParamsTool(api)
|
|
28
|
+
registerOpenclawWorkclawCronUpdateParamsTool(api)
|
|
29
|
+
registerOpenclawWorkclawCronEnableParamsTool(api)
|
|
30
|
+
registerOpenclawWorkclawCronDisableParamsTool(api)
|
|
31
|
+
|
|
32
|
+
registerOpenclawWorkclawCronAddSyncTool(api)
|
|
33
|
+
registerOpenclawWorkclawCronRemoveSyncTool(api)
|
|
34
|
+
registerOpenclawWorkclawCronUpdateSyncTool(api)
|
|
35
|
+
registerOpenclawWorkclawCronEnableSyncTool(api)
|
|
36
|
+
registerOpenclawWorkclawCronDisableSyncTool(api)
|
|
37
|
+
|
|
38
|
+
registerOpenclawWorkclawCronNotifySyncTool(api)
|
|
39
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 智小途定时任务工具 - 生成创建定时任务参数
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { OpenClawPluginApi } from 'openclaw/plugin-sdk'
|
|
6
|
+
import { isCronExpression, relativeToCron, validateTimeFormat } from '../../utils/index.js'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 生成创建定时任务参数
|
|
10
|
+
*/
|
|
11
|
+
export interface AddParamsParams {
|
|
12
|
+
time: string
|
|
13
|
+
name: string
|
|
14
|
+
message: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 参数 Schema 定义
|
|
19
|
+
*/
|
|
20
|
+
const AddParamsSchema = {
|
|
21
|
+
type: 'object',
|
|
22
|
+
properties: {
|
|
23
|
+
time: {
|
|
24
|
+
type: 'string',
|
|
25
|
+
description: '时间描述,支持相对时间(5m、1h)和 cron 表达式(0 8 * * *)',
|
|
26
|
+
},
|
|
27
|
+
name: {
|
|
28
|
+
type: 'string',
|
|
29
|
+
description: '任务名称',
|
|
30
|
+
},
|
|
31
|
+
message: {
|
|
32
|
+
type: 'string',
|
|
33
|
+
description: '提醒内容',
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
required: ['time', 'name', 'message'],
|
|
37
|
+
} as const
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 构造工具返回结果
|
|
41
|
+
*/
|
|
42
|
+
function json(data: unknown): any {
|
|
43
|
+
return {
|
|
44
|
+
content: [{ type: 'text' as const, text: JSON.stringify(data, null, 2) }],
|
|
45
|
+
details: data,
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 获取生成创建定时任务参数工具
|
|
51
|
+
*/
|
|
52
|
+
function getOpenclawWorkclawCronAddParamsTool(api: OpenClawPluginApi) {
|
|
53
|
+
return (ctx: any) => {
|
|
54
|
+
return {
|
|
55
|
+
name: 'openclaw-workclaw-cron-add-params',
|
|
56
|
+
label: '智小途生成创建提醒参数',
|
|
57
|
+
description: `【必须执行的核心步骤】生成创建提醒的 cronParams 参数。
|
|
58
|
+
|
|
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,后端将无法记录新创建的定时任务!`,
|
|
76
|
+
parameters: AddParamsSchema,
|
|
77
|
+
execute: async (_toolCallId: string, params: AddParamsParams) => {
|
|
78
|
+
const p = params as AddParamsParams
|
|
79
|
+
|
|
80
|
+
api.logger.info(`[智小途-生成参数] 开始生成创建提醒参数 name=${p.name} time=${p.time}`)
|
|
81
|
+
|
|
82
|
+
// 参数校验
|
|
83
|
+
if (!p.time) {
|
|
84
|
+
api.logger.warn('[智小途-生成参数] 缺少时间参数')
|
|
85
|
+
return json({ error: 'time(时间)为必填参数' })
|
|
86
|
+
}
|
|
87
|
+
if (!p.name) {
|
|
88
|
+
api.logger.warn('[智小途-生成参数] 缺少任务名称参数')
|
|
89
|
+
return json({ error: 'name(任务名称)为必填参数' })
|
|
90
|
+
}
|
|
91
|
+
if (!p.message) {
|
|
92
|
+
api.logger.warn('[智小途-生成参数] 缺少用户消息参数')
|
|
93
|
+
return json({ error: 'message(用户消息)为必填参数' })
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// 从 ctx 获取必要参数
|
|
97
|
+
const accountId = ctx?.agentAccountId
|
|
98
|
+
if (!accountId) {
|
|
99
|
+
api.logger.warn('[智小途-生成参数] 无法获取账户 ID')
|
|
100
|
+
return json({ error: '无法获取账户 ID(accountId),请确保已配置账户' })
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
api.logger.info(`[智小途-生成参数] accountId=${accountId} name=${p.name}`)
|
|
104
|
+
|
|
105
|
+
// 校验时间格式
|
|
106
|
+
const timeValidation = validateTimeFormat(p.time)
|
|
107
|
+
if (!timeValidation.valid) {
|
|
108
|
+
api.logger.warn(`[智小途-生成参数] 时间格式校验失败: ${timeValidation.error}`)
|
|
109
|
+
return json({ error: timeValidation.error })
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// 判断时间类型并转换为 cron 表达式
|
|
113
|
+
const isCron = isCronExpression(p.time)
|
|
114
|
+
let isOneTime = false
|
|
115
|
+
let cronExpr: string
|
|
116
|
+
|
|
117
|
+
if (!isCron) {
|
|
118
|
+
// 相对时间转换为 cron 表达式
|
|
119
|
+
isOneTime = true
|
|
120
|
+
cronExpr = relativeToCron(p.time.trim())
|
|
121
|
+
api.logger.info(`[智小途-生成参数] 相对时间转换为 cron 表达式 cronExpr=${cronExpr}`)
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
cronExpr = p.time.trim()
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// 构建 cron 参数
|
|
128
|
+
const cronParams = {
|
|
129
|
+
action: 'add' as const,
|
|
130
|
+
job: {
|
|
131
|
+
name: p.name,
|
|
132
|
+
schedule: {
|
|
133
|
+
kind: 'cron' as const,
|
|
134
|
+
expr: cronExpr,
|
|
135
|
+
tz: 'Asia/Shanghai',
|
|
136
|
+
},
|
|
137
|
+
sessionTarget: 'isolated',
|
|
138
|
+
wakeMode: 'now' as const,
|
|
139
|
+
deleteAfterRun: isOneTime ? true : undefined,
|
|
140
|
+
payload: {
|
|
141
|
+
kind: 'agentTurn' as const,
|
|
142
|
+
message: p.message,
|
|
143
|
+
},
|
|
144
|
+
delivery: {
|
|
145
|
+
mode: 'announce',
|
|
146
|
+
channel: 'openclaw-workclaw',
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
api.logger.info(`[智小途-生成参数] 构建 cron 参数 cronParams=${JSON.stringify(cronParams)}`)
|
|
152
|
+
|
|
153
|
+
return json({
|
|
154
|
+
cronParams,
|
|
155
|
+
meta: {
|
|
156
|
+
accountId,
|
|
157
|
+
name: p.name,
|
|
158
|
+
kind: 'cron',
|
|
159
|
+
expr: cronExpr,
|
|
160
|
+
message: p.message,
|
|
161
|
+
},
|
|
162
|
+
summary: `⏰ 提醒已生成: "${p.name}" (${p.time})`,
|
|
163
|
+
})
|
|
164
|
+
},
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* 注册生成创建定时任务参数工具
|
|
171
|
+
*/
|
|
172
|
+
export function registerOpenclawWorkclawCronAddParamsTool(api: OpenClawPluginApi): void {
|
|
173
|
+
(api.registerTool as any)(getOpenclawWorkclawCronAddParamsTool(api), {
|
|
174
|
+
name: 'openclaw-workclaw-cron-add-params',
|
|
175
|
+
})
|
|
176
|
+
}
|