@workclaw/openclaw-workclaw 1.0.19 → 1.0.23
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 +363 -364
- package/api.ts +3 -0
- package/dist/index.js +2 -1
- package/dist/src/api/workspace.js +2 -0
- package/dist/src/channel.js +1 -0
- package/dist/src/gateway/agent-handlers.js +87 -87
- package/dist/src/gateway/config-writer.js +4 -14
- package/dist/src/gateway/cron-tasks-handler.d.ts +19 -0
- package/dist/src/gateway/cron-tasks-handler.js +188 -0
- package/dist/src/gateway/message-context.d.ts +8 -4
- package/dist/src/gateway/message-context.js +148 -138
- package/dist/src/gateway/message-dispatcher.d.ts +0 -1
- package/dist/src/gateway/message-dispatcher.js +383 -323
- package/dist/src/gateway/reconnect.js +4 -0
- package/dist/src/gateway/skills-handler.js +187 -148
- package/dist/src/gateway/workclaw-gateway.d.ts +1 -1
- package/dist/src/gateway/workclaw-gateway.js +47 -33
- package/dist/src/tools/openclaw-workclaw-cron/src/update/params.js +17 -17
- package/index.ts +325 -0
- package/package.json +45 -62
- package/setup-entry.ts +13 -0
- package/src/accounts.ts +360 -0
- package/src/api/accounts-api.ts +156 -0
- package/src/api/prompts-api.ts +122 -0
- package/src/api/session-api.ts +246 -0
- package/src/api/skills-api.ts +74 -0
- package/src/api/workspace.ts +45 -0
- package/src/channel.ts +226 -0
- package/src/config-schema.ts +60 -0
- package/src/connection/workclaw-client.ts +618 -0
- package/src/gateway/agent-handlers.ts +551 -0
- package/src/gateway/config-writer.ts +378 -0
- package/src/gateway/cron-tasks-handler.ts +230 -0
- package/src/gateway/message-context.ts +645 -0
- package/src/gateway/message-dispatcher.ts +688 -0
- package/src/gateway/reconnect.ts +260 -0
- package/src/gateway/skills-handler.ts +805 -0
- package/src/gateway/skills-list-handler.ts +332 -0
- package/src/gateway/tools-list-handler.ts +161 -0
- package/src/gateway/workclaw-gateway.ts +298 -0
- package/src/media/upload.ts +168 -0
- package/src/outbound/index.ts +191 -0
- package/src/outbound/workclaw-sender.ts +161 -0
- package/src/runtime.ts +520 -0
- package/src/secret-contract-api.ts +4 -0
- package/src/send.ts +1 -0
- package/src/setup-api.ts +3 -0
- package/src/setup-core.ts +25 -0
- package/src/setup-surface.ts +498 -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 +177 -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 +195 -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/tools/openclaw-workclaw-system/index.ts +17 -0
- package/src/tools/openclaw-workclaw-system/src/get/index.ts +77 -0
- package/src/tools/openclaw-workclaw-system/src/token/index.ts +93 -0
- package/src/types.ts +50 -0
- package/src/utils/content.ts +40 -0
- package/tsconfig.json +34 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 智小途定时任务工具 - 同步修改定时任务到后端
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { OpenClawPluginApi } from 'openclaw/plugin-sdk'
|
|
6
|
+
import { resolveWorkclawAccountWithCache } from '../../../../accounts.js'
|
|
7
|
+
import { callCronJobUpdate } from '../../api/index.js'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 同步修改定时任务参数
|
|
11
|
+
*/
|
|
12
|
+
export interface UpdateSyncParams {
|
|
13
|
+
jobId: string
|
|
14
|
+
name: string
|
|
15
|
+
kind: 'cron'
|
|
16
|
+
expr?: string
|
|
17
|
+
message: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 参数 Schema 定义
|
|
22
|
+
*/
|
|
23
|
+
const UpdateSyncSchema = {
|
|
24
|
+
type: 'object',
|
|
25
|
+
properties: {
|
|
26
|
+
jobId: {
|
|
27
|
+
type: 'string',
|
|
28
|
+
description: `OpenClaw 任务 ID(必须是数字或字符串,不是 cron 表达式)`,
|
|
29
|
+
},
|
|
30
|
+
name: {
|
|
31
|
+
type: 'string',
|
|
32
|
+
description: '任务名称',
|
|
33
|
+
},
|
|
34
|
+
kind: {
|
|
35
|
+
type: 'string',
|
|
36
|
+
description: '任务类型(仅支持 cron)',
|
|
37
|
+
enum: ['cron'],
|
|
38
|
+
},
|
|
39
|
+
expr: {
|
|
40
|
+
type: 'string',
|
|
41
|
+
description: 'Cron 表达式(kind=cron 时必填)',
|
|
42
|
+
},
|
|
43
|
+
message: {
|
|
44
|
+
type: 'string',
|
|
45
|
+
description: '提醒消息',
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
required: ['jobId', 'name', 'kind', 'message'],
|
|
49
|
+
} as const
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 构造工具返回结果
|
|
53
|
+
*/
|
|
54
|
+
function json(data: unknown): any {
|
|
55
|
+
return {
|
|
56
|
+
content: [{ type: 'text' as const, text: JSON.stringify(data, null, 2) }],
|
|
57
|
+
details: data,
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 获取同步修改定时任务到后端工具
|
|
63
|
+
*/
|
|
64
|
+
function getOpenclawWorkclawCronUpdateSyncTool(api: OpenClawPluginApi) {
|
|
65
|
+
return (ctx: any) => {
|
|
66
|
+
return {
|
|
67
|
+
name: 'openclaw-workclaw-cron-update-sync',
|
|
68
|
+
label: '智小途同步修改提醒',
|
|
69
|
+
description: '将修改定时任务同步到后端。在 cron 工具修改任务后调用此工具同步后端。',
|
|
70
|
+
parameters: UpdateSyncSchema,
|
|
71
|
+
execute: async (_toolCallId: string, params: UpdateSyncParams) => {
|
|
72
|
+
const p = params as UpdateSyncParams
|
|
73
|
+
|
|
74
|
+
api.logger.info(`[智小途-同步修改] 开始同步 jobId=${p.jobId} name=${p.name}`)
|
|
75
|
+
|
|
76
|
+
// 参数校验
|
|
77
|
+
if (!p.jobId) {
|
|
78
|
+
api.logger.warn('[智小途-同步修改] 缺少 jobId 参数')
|
|
79
|
+
return json({ error: 'jobId 为必填参数' })
|
|
80
|
+
}
|
|
81
|
+
if (!p.name) {
|
|
82
|
+
api.logger.warn('[智小途-同步修改] 缺少 name 参数')
|
|
83
|
+
return json({ error: 'name 为必填参数' })
|
|
84
|
+
}
|
|
85
|
+
if (!p.message) {
|
|
86
|
+
api.logger.warn('[智小途-同步修改] 缺少 message 参数')
|
|
87
|
+
return json({ error: 'message 为必填参数' })
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// 从 ctx 获取必要参数
|
|
91
|
+
const accountId = ctx?.agentAccountId
|
|
92
|
+
const channelUserId = ctx?.From
|
|
93
|
+
|
|
94
|
+
// 校验必要参数
|
|
95
|
+
if (!accountId) {
|
|
96
|
+
api.logger.warn('[智小途-同步修改] 无法获取账户 ID')
|
|
97
|
+
return json({ error: '无法获取账户 ID(accountId),请确保已配置账户' })
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const accountConfig = resolveWorkclawAccountWithCache({
|
|
101
|
+
cfg: ctx.config,
|
|
102
|
+
accountId,
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
const channelAgentId = accountConfig.configured ? String(accountConfig.config.agentId) : '0'
|
|
106
|
+
|
|
107
|
+
api.logger.info(`[智小途-同步修改] accountId=${accountId} channelUserId=${channelUserId} channelAgentId=${channelAgentId}`)
|
|
108
|
+
|
|
109
|
+
try {
|
|
110
|
+
const result = await callCronJobUpdate(api, accountId, {
|
|
111
|
+
jobId: p.jobId,
|
|
112
|
+
name: p.name,
|
|
113
|
+
kind: p.kind,
|
|
114
|
+
expr: p.expr,
|
|
115
|
+
message: p.message,
|
|
116
|
+
agentId: channelAgentId,
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
if (result.success) {
|
|
120
|
+
api.logger.info(`[智小途-同步修改] 后端同步成功 jobId=${p.jobId}`)
|
|
121
|
+
return json({
|
|
122
|
+
code: 0,
|
|
123
|
+
message: '定时任务已修改并同步到后端',
|
|
124
|
+
data: {
|
|
125
|
+
jobId: p.jobId,
|
|
126
|
+
name: p.name,
|
|
127
|
+
message: p.message,
|
|
128
|
+
backendSynced: true,
|
|
129
|
+
},
|
|
130
|
+
})
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
api.logger.error(`[智小途-同步修改] 后端同步失败 ${result.error}`)
|
|
134
|
+
return json({
|
|
135
|
+
code: -1,
|
|
136
|
+
error: result.error || '后端同步失败',
|
|
137
|
+
data: { jobId: p.jobId, name: p.name },
|
|
138
|
+
})
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
catch (error: any) {
|
|
142
|
+
api.logger.error(`[智小途-同步修改] 后端同步异常 ${error.message}`)
|
|
143
|
+
return json({
|
|
144
|
+
code: -1,
|
|
145
|
+
error: error.message,
|
|
146
|
+
data: { jobId: p.jobId, name: p.name },
|
|
147
|
+
})
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* 注册同步修改定时任务到后端工具
|
|
156
|
+
*/
|
|
157
|
+
export function registerOpenclawWorkclawCronUpdateSyncTool(api: OpenClawPluginApi): void {
|
|
158
|
+
(api.registerTool as any)(getOpenclawWorkclawCronUpdateSyncTool(api), {
|
|
159
|
+
name: 'openclaw-workclaw-cron-update-sync',
|
|
160
|
+
})
|
|
161
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 智小途定时任务工具 - 类型定义
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 后端接口请求体
|
|
7
|
+
*/
|
|
8
|
+
export interface CronJobPayload {
|
|
9
|
+
agentId: string
|
|
10
|
+
messageId: string
|
|
11
|
+
clawJobId: string
|
|
12
|
+
name: string
|
|
13
|
+
kind: 'cron'
|
|
14
|
+
expr?: string
|
|
15
|
+
message: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* OpenClaw cron 工具参数
|
|
20
|
+
*/
|
|
21
|
+
export interface OpenClawCronParams {
|
|
22
|
+
action: 'add' | 'list' | 'remove' | 'update' | 'enable' | 'disable'
|
|
23
|
+
job?: {
|
|
24
|
+
agentId: string
|
|
25
|
+
name: string
|
|
26
|
+
schedule: {
|
|
27
|
+
kind: 'cron'
|
|
28
|
+
expr: string
|
|
29
|
+
tz: string
|
|
30
|
+
}
|
|
31
|
+
sessionTarget: 'isolated'
|
|
32
|
+
wakeMode: 'now'
|
|
33
|
+
deleteAfterRun?: boolean
|
|
34
|
+
payload: {
|
|
35
|
+
kind: 'agentTurn'
|
|
36
|
+
message: string
|
|
37
|
+
}
|
|
38
|
+
delivery: {
|
|
39
|
+
mode: 'announce'
|
|
40
|
+
channel: string
|
|
41
|
+
to: string
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
jobId?: string
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 工具返回结果
|
|
49
|
+
*/
|
|
50
|
+
export interface CronToolResult {
|
|
51
|
+
success: boolean
|
|
52
|
+
jobId?: string
|
|
53
|
+
jobs?: any[]
|
|
54
|
+
error?: string
|
|
55
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 智小途定时任务工具 - 工具函数
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 解析相对时间字符串
|
|
7
|
+
* 支持格式:5m, 1h, 1h30m, 2d, 30s, 1d2h30m 等
|
|
8
|
+
* @param timeStr - 时间字符串
|
|
9
|
+
* @returns 毫秒数,解析失败返回 null
|
|
10
|
+
*/
|
|
11
|
+
export function parseRelativeTime(timeStr: string): number | null {
|
|
12
|
+
const s = timeStr.trim().toLowerCase()
|
|
13
|
+
|
|
14
|
+
if (/^\d+$/.test(s)) {
|
|
15
|
+
return Number.parseInt(s, 10) * 60_000
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let totalMs = 0
|
|
19
|
+
let matched = false
|
|
20
|
+
const regex = /(\d+(?:\.\d+)?)\s*([dhms])/g
|
|
21
|
+
|
|
22
|
+
let match: RegExpExecArray | null
|
|
23
|
+
// eslint-disable-next-line no-cond-assign
|
|
24
|
+
while ((match = regex.exec(s)) !== null) {
|
|
25
|
+
matched = true
|
|
26
|
+
const value = Number.parseFloat(match[1])
|
|
27
|
+
const unit = match[2]
|
|
28
|
+
switch (unit) {
|
|
29
|
+
case 'd': totalMs += value * 86_400_000
|
|
30
|
+
break
|
|
31
|
+
case 'h': totalMs += value * 3_600_000
|
|
32
|
+
break
|
|
33
|
+
case 'm': totalMs += value * 60_000
|
|
34
|
+
break
|
|
35
|
+
case 's': totalMs += value * 1_000
|
|
36
|
+
break
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return matched ? Math.round(totalMs) : null
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 判断是否为 cron 表达式
|
|
45
|
+
* @param timeStr - 时间字符串
|
|
46
|
+
* @returns 是否为 cron 表达式
|
|
47
|
+
*/
|
|
48
|
+
export function isCronExpression(timeStr: string): boolean {
|
|
49
|
+
const parts = timeStr.trim().split(/\s+/)
|
|
50
|
+
return parts.length >= 3 && parts.length <= 6
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 校验 cron 表达式格式
|
|
55
|
+
* @param timeStr - 时间字符串
|
|
56
|
+
* @returns 校验结果和错误信息
|
|
57
|
+
*/
|
|
58
|
+
export function validateCronExpression(timeStr: string): { valid: boolean, error?: string } {
|
|
59
|
+
const parts = timeStr.trim().split(/\s+/)
|
|
60
|
+
|
|
61
|
+
if (parts.length < 3 || parts.length > 6) {
|
|
62
|
+
return { valid: false, error: `无效的 cron 表达式:字段数量不正确(期望 3-6 个字段,实际 ${parts.length} 个)` }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const patterns = [
|
|
66
|
+
/^(\*|[0-5]?\d(-[0-5]?\d)?(\/[0-5]?\d)?|L(-[0-5]?\d)?|W?)$/,
|
|
67
|
+
/^(\*|[0-5]?\d(-[0-5]?\d)?(\/[0-5]?\d)?|L(-[0-5]?\d)?|W?)$/,
|
|
68
|
+
/^(\*|[01]?\d|2[0-3](-[01]?\d|2[0-3])?(\/\d|[01]?\d|2[0-3])?|L(-[01]?\d|2[0-3])?|W)?$/,
|
|
69
|
+
/^(\*|1[0-2]|0?[1-9](-[1-9]|0?[1-9])?(\/[1-9]|0?[1-9])?|L(-1[0-2]|0?[1-9])?|W)?$/,
|
|
70
|
+
/^([*LW]|[0-6](-[0-6])?(\/[0-6])?|L-[0-6])?$/,
|
|
71
|
+
/^(\*|L|(\*\/)?\d{1,2})?$/,
|
|
72
|
+
]
|
|
73
|
+
|
|
74
|
+
const fieldNames = ['秒', '分钟', '小时', '日期', '月份', '星期']
|
|
75
|
+
|
|
76
|
+
for (let i = 0; i < parts.length && i < patterns.length; i++) {
|
|
77
|
+
const part = parts[i].trim()
|
|
78
|
+
const offset = parts.length - patterns.length
|
|
79
|
+
|
|
80
|
+
if (i < offset)
|
|
81
|
+
continue
|
|
82
|
+
|
|
83
|
+
if (!patterns[i].test(part)) {
|
|
84
|
+
return { valid: false, error: `无效的 cron 表达式:${fieldNames[i]}字段 "${part}" 格式不正确` }
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return { valid: true }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 校验时间格式(相对时间或 cron 表达式)
|
|
93
|
+
* @param timeStr - 时间字符串
|
|
94
|
+
* @returns 校验结果和错误信息
|
|
95
|
+
*/
|
|
96
|
+
export function validateTimeFormat(timeStr: string): { valid: boolean, error?: string } {
|
|
97
|
+
if (!timeStr || typeof timeStr !== 'string' || timeStr.trim() === '') {
|
|
98
|
+
return { valid: false, error: '时间参数不能为空' }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const trimmed = timeStr.trim()
|
|
102
|
+
|
|
103
|
+
if (trimmed.startsWith('+') || /^\d+$/.test(trimmed) || /^\d+[dhms]/.test(trimmed)) {
|
|
104
|
+
const relativeMs = parseRelativeTime(trimmed)
|
|
105
|
+
if (relativeMs === null) {
|
|
106
|
+
return { valid: false, error: `无法解析相对时间 "${trimmed}",支持的格式:5m、1h、1h30m、2d、30s 等` }
|
|
107
|
+
}
|
|
108
|
+
if (relativeMs < 30_000) {
|
|
109
|
+
return { valid: false, error: '提醒时间不能少于 30 秒' }
|
|
110
|
+
}
|
|
111
|
+
return { valid: true }
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (isCronExpression(trimmed)) {
|
|
115
|
+
return validateCronExpression(trimmed)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return { valid: false, error: `无法解析时间 "${trimmed}",请使用相对时间(如 5m、1h)或 cron 表达式(如 0 8 * * *)` }
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 将相对时间转换为标准 cron 表达式
|
|
123
|
+
* @param timeStr - 相对时间字符串,如 "5m", "1h", "2h30m"
|
|
124
|
+
* @returns 标准 cron 表达式(秒 分 时 日 月 星期),格式为 "SS MM HH DD MM *"
|
|
125
|
+
*/
|
|
126
|
+
export function relativeToCron(timeStr: string): string {
|
|
127
|
+
const delayMs = parseRelativeTime(timeStr)
|
|
128
|
+
if (delayMs === null) {
|
|
129
|
+
return timeStr
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const futureTime = new Date(Date.now() + delayMs)
|
|
133
|
+
|
|
134
|
+
const seconds = futureTime.getSeconds()
|
|
135
|
+
const minutes = futureTime.getMinutes()
|
|
136
|
+
const hours = futureTime.getHours()
|
|
137
|
+
const dayOfMonth = futureTime.getDate()
|
|
138
|
+
const month = futureTime.getMonth() + 1
|
|
139
|
+
|
|
140
|
+
return `${seconds} ${minutes} ${hours} ${dayOfMonth} ${month} *`
|
|
141
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 智小途系统信息工具 - 统一出口
|
|
3
|
+
*
|
|
4
|
+
* 提供统一的工具注册入口,一次性注册所有系统信息相关工具
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { registerOpenclawWorkclawSystemConfigTool } from './src/get/index.js'
|
|
8
|
+
import { registerOpenclawWorkclawSystemTokenTool } from './src/token/index.js'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 统一注册所有系统信息工具
|
|
12
|
+
* @param api - OpenClaw API 实例
|
|
13
|
+
*/
|
|
14
|
+
export function registerAllOpenclawWorkclawSystemTools(api: any): void {
|
|
15
|
+
registerOpenclawWorkclawSystemConfigTool(api)
|
|
16
|
+
registerOpenclawWorkclawSystemTokenTool(api)
|
|
17
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 智小途系统信息工具 - 获取系统配置信息
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { OpenClawPluginApi } from 'openclaw/plugin-sdk'
|
|
6
|
+
import { resolveWorkclawAccountWithCache } from '../../../../accounts.js'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 构造工具返回结果
|
|
10
|
+
*/
|
|
11
|
+
function json(data: unknown): any {
|
|
12
|
+
return {
|
|
13
|
+
content: [{ type: 'text' as const, text: JSON.stringify(data, null, 2) }],
|
|
14
|
+
details: data,
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 获取系统配置信息工具
|
|
20
|
+
*/
|
|
21
|
+
function getOpenclawWorkclawSystemConfigTool(api: OpenClawPluginApi) {
|
|
22
|
+
return (ctx: any) => {
|
|
23
|
+
return {
|
|
24
|
+
name: 'openclaw-workclaw-system-config',
|
|
25
|
+
label: '智小途获取系统信息',
|
|
26
|
+
description: '获取当前渠道的系统配置信息,包括 appKey、appSecret、agentId、userId 等',
|
|
27
|
+
execute: async () => {
|
|
28
|
+
api.logger.info('[智小途-系统信息] 开始获取系统配置信息')
|
|
29
|
+
|
|
30
|
+
const accountId = ctx?.agentAccountId
|
|
31
|
+
if (!accountId) {
|
|
32
|
+
api.logger.warn('[智小途-系统信息] 无法获取账户 ID')
|
|
33
|
+
return json({ error: '无法获取账户 ID(accountId),请确保已配置账户' })
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
api.logger.info(`[智小途-系统信息] accountId=${accountId}`)
|
|
37
|
+
|
|
38
|
+
const accountConfig = resolveWorkclawAccountWithCache({
|
|
39
|
+
cfg: ctx.config,
|
|
40
|
+
accountId,
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
if (!accountConfig.configured) {
|
|
44
|
+
api.logger.error('[智小途-系统信息] 账户未配置')
|
|
45
|
+
return json({ error: '账户未配置,请检查账户配置' })
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const accConfig = accountConfig.config
|
|
49
|
+
|
|
50
|
+
const result = {
|
|
51
|
+
accountId,
|
|
52
|
+
appKey: accConfig.appKey,
|
|
53
|
+
appSecret: accConfig.appSecret,
|
|
54
|
+
agentId: accConfig.agentId,
|
|
55
|
+
userId: accConfig.userId,
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
api.logger.info(`[智小途-系统信息] 获取成功${JSON.stringify(result)}`)
|
|
59
|
+
|
|
60
|
+
return json({
|
|
61
|
+
code: 0,
|
|
62
|
+
message: '系统信息获取成功',
|
|
63
|
+
data: result,
|
|
64
|
+
})
|
|
65
|
+
},
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 注册获取系统配置信息工具
|
|
72
|
+
*/
|
|
73
|
+
export function registerOpenclawWorkclawSystemConfigTool(api: OpenClawPluginApi): void {
|
|
74
|
+
(api.registerTool as any)(getOpenclawWorkclawSystemConfigTool(api), {
|
|
75
|
+
name: 'openclaw-workclaw-system-config',
|
|
76
|
+
})
|
|
77
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 智小途系统信息工具 - 获取用户 AccessToken
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { OpenClawPluginApi } from 'openclaw/plugin-sdk'
|
|
6
|
+
import { resolveWorkclawAccountWithCache } from '../../../../accounts.js'
|
|
7
|
+
import { getWorkclawAccessToken } from '../../../../connection/workclaw-client.js'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 构造工具返回结果
|
|
11
|
+
*/
|
|
12
|
+
function json(data: unknown): any {
|
|
13
|
+
return {
|
|
14
|
+
content: [{ type: 'text' as const, text: JSON.stringify(data, null, 2) }],
|
|
15
|
+
details: data,
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 获取用户 AccessToken 工具
|
|
21
|
+
*/
|
|
22
|
+
function getOpenclawWorkclawSystemTokenTool(api: OpenClawPluginApi) {
|
|
23
|
+
return (ctx: any) => {
|
|
24
|
+
return {
|
|
25
|
+
name: 'openclaw-workclaw-system-token',
|
|
26
|
+
label: '智小途获取 AccessToken',
|
|
27
|
+
description: '获取用户的 AccessToken',
|
|
28
|
+
execute: async () => {
|
|
29
|
+
api.logger.info('[智小途-系统信息] 开始获取 AccessToken')
|
|
30
|
+
|
|
31
|
+
const accountId = ctx?.agentAccountId
|
|
32
|
+
if (!accountId) {
|
|
33
|
+
api.logger.warn('[智小途-系统信息] 无法获取账户 ID')
|
|
34
|
+
return json({ error: '无法获取账户 ID(accountId),请确保已配置账户' })
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
api.logger.info(`[智小途-系统信息] accountId=${accountId}`)
|
|
38
|
+
|
|
39
|
+
const accountConfig = resolveWorkclawAccountWithCache({
|
|
40
|
+
cfg: ctx.config,
|
|
41
|
+
accountId,
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
if (!accountConfig.configured) {
|
|
45
|
+
api.logger.error('[智小途-系统信息] 账户未配置')
|
|
46
|
+
return json({ error: '账户未配置,请检查账户配置' })
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const accConfig = accountConfig.config
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
const tokenCacheKey = accConfig.appKey || accountId
|
|
53
|
+
|
|
54
|
+
api.logger.info(`[智小途-系统信息] 正在获取 AccessToken,accountId=${accountId}`)
|
|
55
|
+
|
|
56
|
+
const accessToken = await getWorkclawAccessToken(tokenCacheKey, {
|
|
57
|
+
baseUrl: accConfig.baseUrl,
|
|
58
|
+
appKey: accConfig.appKey,
|
|
59
|
+
appSecret: accConfig.appSecret,
|
|
60
|
+
allowInsecureTls: accConfig.allowInsecureTls,
|
|
61
|
+
requestTimeout: accConfig.requestTimeout,
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
api.logger.info('[智小途-系统信息] AccessToken 获取成功')
|
|
65
|
+
|
|
66
|
+
return json({
|
|
67
|
+
code: 0,
|
|
68
|
+
message: 'AccessToken 获取成功',
|
|
69
|
+
data: {
|
|
70
|
+
accessToken,
|
|
71
|
+
},
|
|
72
|
+
})
|
|
73
|
+
}
|
|
74
|
+
catch (error: any) {
|
|
75
|
+
api.logger.error(`[智小途-系统信息] AccessToken 获取失败 ${error.message}`)
|
|
76
|
+
return json({
|
|
77
|
+
code: -1,
|
|
78
|
+
error: error.message || 'AccessToken 获取失败',
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 注册获取用户 AccessToken 工具
|
|
88
|
+
*/
|
|
89
|
+
export function registerOpenclawWorkclawSystemTokenTool(api: OpenClawPluginApi): void {
|
|
90
|
+
(api.registerTool as any)(getOpenclawWorkclawSystemTokenTool(api), {
|
|
91
|
+
name: 'openclaw-workclaw-system-token',
|
|
92
|
+
})
|
|
93
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
workclawAccountConfigSchema,
|
|
3
|
+
workclawConfigSchema,
|
|
4
|
+
z,
|
|
5
|
+
} from './config-schema.js'
|
|
6
|
+
|
|
7
|
+
export type WorkclawConfig = z.infer<typeof workclawConfigSchema>
|
|
8
|
+
export type WorkclawAccountConfig = z.infer<typeof workclawAccountConfigSchema>
|
|
9
|
+
|
|
10
|
+
export interface ResolvedWorkclawAccount {
|
|
11
|
+
accountId: string
|
|
12
|
+
enabled: boolean
|
|
13
|
+
configured: boolean
|
|
14
|
+
name?: string
|
|
15
|
+
config: WorkclawFullAccountConfig
|
|
16
|
+
}
|
|
17
|
+
export type ResolvedOpenClawWorkclawAccount = ResolvedWorkclawAccount
|
|
18
|
+
|
|
19
|
+
export type WorkclawIdType = 'open_id' | 'user_id' | 'union_id' | 'chat_id'
|
|
20
|
+
|
|
21
|
+
export interface WorkclawSendResult {
|
|
22
|
+
messageId: string
|
|
23
|
+
chatId: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface WorkClawBaseConfig {
|
|
27
|
+
baseUrl?: string
|
|
28
|
+
websocketUrl?: string
|
|
29
|
+
appKey?: string
|
|
30
|
+
appSecret?: string
|
|
31
|
+
localIp?: string
|
|
32
|
+
allowInsecureTls?: boolean
|
|
33
|
+
requestTimeout?: number
|
|
34
|
+
userId?: string | number
|
|
35
|
+
replyEndpoint?: string
|
|
36
|
+
pushEndpoint?: string
|
|
37
|
+
[key: string]: any
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface WorkClawAccountConfig {
|
|
41
|
+
agentId?: string | number
|
|
42
|
+
userId?: string | number
|
|
43
|
+
openConversationId?: string
|
|
44
|
+
[key: string]: any
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export type WorkClawConfig = WorkClawBaseConfig & WorkClawAccountConfig
|
|
48
|
+
|
|
49
|
+
export type WorkclawFullAccountConfig = WorkclawConfig & Partial<WorkclawAccountConfig>
|
|
50
|
+
export type ResolvedWorkclawAccountConfig = WorkclawFullAccountConfig
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content extraction utilities - copied from SDK's internal implementations
|
|
3
|
+
* node_modules/openclaw/dist/plugin-sdk/thread-bindings-SYAnWHuW.js (collectTextContentBlocks)
|
|
4
|
+
* node_modules/openclaw/dist/auth-profiles-DRjqKE3G.js (extractToolResultText)
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Extract text blocks from chat content array.
|
|
9
|
+
* Handles content format: [{ type: "text", text: "..." }]
|
|
10
|
+
*/
|
|
11
|
+
export function collectTextContentBlocks(content: unknown): string[] {
|
|
12
|
+
if (!Array.isArray(content))
|
|
13
|
+
return []
|
|
14
|
+
const parts: string[] = []
|
|
15
|
+
for (const block of content) {
|
|
16
|
+
if (!block || typeof block !== 'object')
|
|
17
|
+
continue
|
|
18
|
+
const rec = block as { type?: string, text?: string }
|
|
19
|
+
if (rec.type === 'text' && typeof rec.text === 'string')
|
|
20
|
+
parts.push(rec.text)
|
|
21
|
+
}
|
|
22
|
+
return parts
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Extract plain text from a tool result object.
|
|
27
|
+
* Handles result.content as array of text blocks or direct text fields.
|
|
28
|
+
*/
|
|
29
|
+
export function extractToolResultText(result: unknown): string | undefined {
|
|
30
|
+
if (!result || typeof result !== 'object')
|
|
31
|
+
return
|
|
32
|
+
const record = result as { content?: unknown }
|
|
33
|
+
const texts = collectTextContentBlocks(record.content).map((item) => {
|
|
34
|
+
const trimmed = item.trim()
|
|
35
|
+
return trimmed || void 0
|
|
36
|
+
}).filter(value => Boolean(value))
|
|
37
|
+
if (texts.length === 0)
|
|
38
|
+
return
|
|
39
|
+
return texts.join('\n')
|
|
40
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"lib": [
|
|
5
|
+
"ES2022",
|
|
6
|
+
"ESNext"
|
|
7
|
+
],
|
|
8
|
+
"moduleDetection": "force",
|
|
9
|
+
"useDefineForClassFields": true,
|
|
10
|
+
"module": "ESNext",
|
|
11
|
+
"moduleResolution": "bundler",
|
|
12
|
+
"resolveJsonModule": true,
|
|
13
|
+
"types": [
|
|
14
|
+
"node"
|
|
15
|
+
],
|
|
16
|
+
"allowImportingTsExtensions": false,
|
|
17
|
+
"allowJs": true,
|
|
18
|
+
"strict": true,
|
|
19
|
+
"noFallthroughCasesInSwitch": true,
|
|
20
|
+
"noUnusedLocals": false,
|
|
21
|
+
"noUnusedParameters": false,
|
|
22
|
+
"noEmit": true,
|
|
23
|
+
"esModuleInterop": true,
|
|
24
|
+
"forceConsistentCasingInFileNames": true,
|
|
25
|
+
"isolatedModules": true,
|
|
26
|
+
"skipLibCheck": true,
|
|
27
|
+
"rootDir": ".",
|
|
28
|
+
"declaration": true,
|
|
29
|
+
"outDir": "dist",
|
|
30
|
+
},
|
|
31
|
+
"include": ["./**/*.ts", "./index.ts"],
|
|
32
|
+
"exclude": ["node_modules", "dist"]
|
|
33
|
+
}
|
|
34
|
+
|