@workclaw/openclaw-workclaw 1.0.0 → 1.0.11
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 -325
- package/package.json +43 -43
- package/src/config-schema.ts +1 -1
- package/src/gateway/tools-list-handler.ts +162 -162
- package/src/media/upload.ts +12 -12
- package/src/outbound/index.ts +183 -183
- package/src/outbound/workclaw-sender.ts +2 -2
- package/src/tools/openclaw-workclaw-cron/api/index.ts +321 -321
- package/src/types.ts +2 -2
- package/src/utils/content.ts +5 -5
- package/templates/IDENTITY.md +1 -1
package/src/outbound/index.ts
CHANGED
|
@@ -1,183 +1,183 @@
|
|
|
1
|
-
import type { OpenClawConfig } from 'openclaw/plugin-sdk'
|
|
2
|
-
import type { OpenclawWorkclawConfig, OpenclawWorkclawSendResult } from '../types.js'
|
|
3
|
-
import { resolveAccountByUserIdAndAgentId, resolveOpenclawWorkclawAccount } from '../accounts.js'
|
|
4
|
-
|
|
5
|
-
import { isLocalMediaSource, resolveLocalPath, uploadLocalMedia } from '../media/upload.js'
|
|
6
|
-
import { getOpenclawWorkclawLogger } from '../runtime.js'
|
|
7
|
-
import {
|
|
8
|
-
sendOpenclawWorkclawOutboundMessage,
|
|
9
|
-
} from './workclaw-sender.js'
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* 根据 target (userId) 和可选的 agentId 查找匹配的账户
|
|
13
|
-
* 如果同时提供 userId 和 agentId,走 O(1) 查找
|
|
14
|
-
* 否则遍历所有账户,查找 userId 等于 target 的账户
|
|
15
|
-
*/
|
|
16
|
-
function findAccountIdByTarget(
|
|
17
|
-
cfg: OpenClawConfig,
|
|
18
|
-
target: string,
|
|
19
|
-
agentId?: string | number,
|
|
20
|
-
): string | undefined {
|
|
21
|
-
// O(1) 查找:userId + agentId 唯一确定一个账户
|
|
22
|
-
if (agentId !== undefined && agentId !== null) {
|
|
23
|
-
return resolveAccountByUserIdAndAgentId(cfg, target, String(agentId)) ?? undefined
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// 遍历:只有 userId 时,无法唯一确定账户(一个 userId 对应多个 agentId)
|
|
27
|
-
// 需要通过 bindings 或其他方式 resolve,这里暂不支持
|
|
28
|
-
return undefined
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface SendOpenclawWorkclawMessageParams {
|
|
32
|
-
cfg: OpenClawConfig
|
|
33
|
-
to: string
|
|
34
|
-
text: string
|
|
35
|
-
mediaUrl?: string
|
|
36
|
-
replyToMessageId?: string
|
|
37
|
-
accountId?: string
|
|
38
|
-
openConversationId?: string
|
|
39
|
-
agentId?: string | number
|
|
40
|
-
/**
|
|
41
|
-
* 消息类型:
|
|
42
|
-
* - "reply": 回复消息(用户触发)
|
|
43
|
-
* - "push": 主动推送(定时任务触发)
|
|
44
|
-
*/
|
|
45
|
-
messageType?: 'reply' | 'push'
|
|
46
|
-
msgType?: string
|
|
47
|
-
last?: boolean
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
async function resolveMediaUrl(
|
|
51
|
-
mediaUrl: string | undefined,
|
|
52
|
-
config: OpenclawWorkclawConfig,
|
|
53
|
-
): Promise<string | undefined> {
|
|
54
|
-
if (!mediaUrl)
|
|
55
|
-
return undefined
|
|
56
|
-
if (!isLocalMediaSource(mediaUrl))
|
|
57
|
-
return mediaUrl
|
|
58
|
-
|
|
59
|
-
const uploadUrl = config.uploadUrl
|
|
60
|
-
if (!uploadUrl) {
|
|
61
|
-
throw new Error('uploadUrl not configured for local mediaUrl')
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const filePath = resolveLocalPath(mediaUrl)
|
|
65
|
-
|
|
66
|
-
if (typeof config.mediaMaxMb === 'number' && config.mediaMaxMb > 0) {
|
|
67
|
-
const { stat } = await import('node:fs/promises')
|
|
68
|
-
const stats = await stat(filePath)
|
|
69
|
-
const maxBytes = config.mediaMaxMb * 1024 * 1024
|
|
70
|
-
if (stats.size > maxBytes) {
|
|
71
|
-
throw new Error(`mediaUrl exceeds limit (${config.mediaMaxMb} MB)`)
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return uploadLocalMedia({
|
|
76
|
-
uploadUrl,
|
|
77
|
-
filePath,
|
|
78
|
-
uploadFieldName: config.uploadFieldName,
|
|
79
|
-
uploadHeaders: config.uploadHeaders as Record<string, string> | undefined,
|
|
80
|
-
uploadFormFields: config.uploadFormFields as
|
|
81
|
-
| Record<string, string | number | boolean>
|
|
82
|
-
| undefined,
|
|
83
|
-
uploadResponseUrlPath: config.uploadResponseUrlPath,
|
|
84
|
-
requestTimeout: config.requestTimeout ?? 30000,
|
|
85
|
-
allowInsecureTls: config.allowInsecureTls,
|
|
86
|
-
})
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export async function sendMessageOpenclawWorkclaw(
|
|
90
|
-
params: SendOpenclawWorkclawMessageParams,
|
|
91
|
-
): Promise<OpenclawWorkclawSendResult> {
|
|
92
|
-
const { cfg, to, text, mediaUrl, replyToMessageId, accountId, openConversationId, agentId, messageType, msgType, last }
|
|
93
|
-
= params
|
|
94
|
-
|
|
95
|
-
// 如果没有指定 messageType,根据是否有 replyToMessageId 来判断
|
|
96
|
-
// - 有 replyToMessageId: 回复消息
|
|
97
|
-
// - 没有 replyToMessageId: 主动推送
|
|
98
|
-
const resolvedMessageType = messageType ?? (replyToMessageId ? 'reply' : 'push')
|
|
99
|
-
|
|
100
|
-
// 如果没有指定 accountId,尝试根据 target (to) 和 agentId 查找匹配的账户
|
|
101
|
-
let resolvedAccountId = accountId
|
|
102
|
-
if (!resolvedAccountId) {
|
|
103
|
-
const foundAccountId = findAccountIdByTarget(cfg, to, agentId)
|
|
104
|
-
if (foundAccountId) {
|
|
105
|
-
resolvedAccountId = foundAccountId
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (!resolvedAccountId) {
|
|
110
|
-
throw new Error('无法确定账户ID')
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const account = resolveOpenclawWorkclawAccount({ cfg, accountId: resolvedAccountId })
|
|
114
|
-
if (!account.configured) {
|
|
115
|
-
throw new Error(`OpenclawWorkclaw account "${account.accountId}" not configured`)
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const resolvedMediaUrl = await resolveMediaUrl(mediaUrl, account.config)
|
|
119
|
-
|
|
120
|
-
// 优先使用传入的 openConversationId,其次从 cfg 中直接读取(绕过 account.config 快照问题)
|
|
121
|
-
// 这样可以获取到运行时动态保存的 openConversationId
|
|
122
|
-
interface ChannelWithAccounts { accounts?: Record<string, any>, openConversationId?: string }
|
|
123
|
-
const cfgChannel = (cfg.channels?.['openclaw-workclaw'] as unknown as ChannelWithAccounts) ?? {}
|
|
124
|
-
const cfgAccounts = cfgChannel.accounts ?? {}
|
|
125
|
-
const cfgAccount = cfgAccounts[resolvedAccountId] ?? {}
|
|
126
|
-
const cfgOpenConversationId = cfgAccount?.openConversationId ?? cfgChannel?.openConversationId
|
|
127
|
-
const effectiveOpenConversationId = openConversationId ?? cfgOpenConversationId
|
|
128
|
-
getOpenclawWorkclawLogger().info(`sendMessageOpenclawWorkclaw - resolvedAccountId: ${resolvedAccountId}, openConversationId param: ${openConversationId}, cfgOpenConversationId: ${cfgOpenConversationId}, effectiveOpenConversationId: ${effectiveOpenConversationId}, messageType: ${resolvedMessageType}, replyToMessageId: ${replyToMessageId}`)
|
|
129
|
-
|
|
130
|
-
// 使用 appKey 作为缓存键,让所有账户共享同一个 token
|
|
131
|
-
const tokenCacheKey = account.config.appKey || account.accountId
|
|
132
|
-
const result = await sendOpenclawWorkclawOutboundMessage({
|
|
133
|
-
cacheKey: tokenCacheKey,
|
|
134
|
-
to,
|
|
135
|
-
text,
|
|
136
|
-
msgType,
|
|
137
|
-
mediaUrl: resolvedMediaUrl,
|
|
138
|
-
replyToMessageId,
|
|
139
|
-
openConversationId: effectiveOpenConversationId,
|
|
140
|
-
agentId,
|
|
141
|
-
config: account.config,
|
|
142
|
-
messageType: resolvedMessageType,
|
|
143
|
-
last,
|
|
144
|
-
})
|
|
145
|
-
return {
|
|
146
|
-
messageId: result.messageId,
|
|
147
|
-
chatId: to,
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export async function getMessageOpenclawWorkclaw(_params: {
|
|
152
|
-
cfg: OpenClawConfig
|
|
153
|
-
messageId: string
|
|
154
|
-
accountId?: string
|
|
155
|
-
}): Promise<null> {
|
|
156
|
-
return null
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* 发送主动推送消息(定时任务使用)
|
|
161
|
-
* 使用 pushEndpoint 配置的端口
|
|
162
|
-
*/
|
|
163
|
-
export async function sendPushMessageOpenclawWorkclaw(
|
|
164
|
-
params: Omit<SendOpenclawWorkclawMessageParams, 'messageType'>,
|
|
165
|
-
): Promise<OpenclawWorkclawSendResult> {
|
|
166
|
-
return sendMessageOpenclawWorkclaw({
|
|
167
|
-
...params,
|
|
168
|
-
messageType: 'push',
|
|
169
|
-
})
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* 发送回复消息(用户触发)
|
|
174
|
-
* 使用 replyEndpoint 配置的端口
|
|
175
|
-
*/
|
|
176
|
-
export async function sendReplyMessageOpenclawWorkclaw(
|
|
177
|
-
params: Omit<SendOpenclawWorkclawMessageParams, 'messageType'>,
|
|
178
|
-
): Promise<OpenclawWorkclawSendResult> {
|
|
179
|
-
return sendMessageOpenclawWorkclaw({
|
|
180
|
-
...params,
|
|
181
|
-
messageType: 'reply',
|
|
182
|
-
})
|
|
183
|
-
}
|
|
1
|
+
import type { OpenClawConfig } from 'openclaw/plugin-sdk'
|
|
2
|
+
import type { OpenclawWorkclawConfig, OpenclawWorkclawSendResult } from '../types.js'
|
|
3
|
+
import { resolveAccountByUserIdAndAgentId, resolveOpenclawWorkclawAccount } from '../accounts.js'
|
|
4
|
+
|
|
5
|
+
import { isLocalMediaSource, resolveLocalPath, uploadLocalMedia } from '../media/upload.js'
|
|
6
|
+
import { getOpenclawWorkclawLogger } from '../runtime.js'
|
|
7
|
+
import {
|
|
8
|
+
sendOpenclawWorkclawOutboundMessage,
|
|
9
|
+
} from './workclaw-sender.js'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 根据 target (userId) 和可选的 agentId 查找匹配的账户
|
|
13
|
+
* 如果同时提供 userId 和 agentId,走 O(1) 查找
|
|
14
|
+
* 否则遍历所有账户,查找 userId 等于 target 的账户
|
|
15
|
+
*/
|
|
16
|
+
function findAccountIdByTarget(
|
|
17
|
+
cfg: OpenClawConfig,
|
|
18
|
+
target: string,
|
|
19
|
+
agentId?: string | number,
|
|
20
|
+
): string | undefined {
|
|
21
|
+
// O(1) 查找:userId + agentId 唯一确定一个账户
|
|
22
|
+
if (agentId !== undefined && agentId !== null) {
|
|
23
|
+
return resolveAccountByUserIdAndAgentId(cfg, target, String(agentId)) ?? undefined
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// 遍历:只有 userId 时,无法唯一确定账户(一个 userId 对应多个 agentId)
|
|
27
|
+
// 需要通过 bindings 或其他方式 resolve,这里暂不支持
|
|
28
|
+
return undefined
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface SendOpenclawWorkclawMessageParams {
|
|
32
|
+
cfg: OpenClawConfig
|
|
33
|
+
to: string
|
|
34
|
+
text: string
|
|
35
|
+
mediaUrl?: string
|
|
36
|
+
replyToMessageId?: string
|
|
37
|
+
accountId?: string
|
|
38
|
+
openConversationId?: string
|
|
39
|
+
agentId?: string | number
|
|
40
|
+
/**
|
|
41
|
+
* 消息类型:
|
|
42
|
+
* - "reply": 回复消息(用户触发)
|
|
43
|
+
* - "push": 主动推送(定时任务触发)
|
|
44
|
+
*/
|
|
45
|
+
messageType?: 'reply' | 'push'
|
|
46
|
+
msgType?: string
|
|
47
|
+
last?: boolean
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function resolveMediaUrl(
|
|
51
|
+
mediaUrl: string | undefined,
|
|
52
|
+
config: OpenclawWorkclawConfig,
|
|
53
|
+
): Promise<string | undefined> {
|
|
54
|
+
if (!mediaUrl)
|
|
55
|
+
return undefined
|
|
56
|
+
if (!isLocalMediaSource(mediaUrl))
|
|
57
|
+
return mediaUrl
|
|
58
|
+
|
|
59
|
+
const uploadUrl = config.uploadUrl
|
|
60
|
+
if (!uploadUrl) {
|
|
61
|
+
throw new Error('uploadUrl not configured for local mediaUrl')
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const filePath = resolveLocalPath(mediaUrl)
|
|
65
|
+
|
|
66
|
+
if (typeof config.mediaMaxMb === 'number' && config.mediaMaxMb > 0) {
|
|
67
|
+
const { stat } = await import('node:fs/promises')
|
|
68
|
+
const stats = await stat(filePath)
|
|
69
|
+
const maxBytes = config.mediaMaxMb * 1024 * 1024
|
|
70
|
+
if (stats.size > maxBytes) {
|
|
71
|
+
throw new Error(`mediaUrl exceeds limit (${config.mediaMaxMb} MB)`)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return uploadLocalMedia({
|
|
76
|
+
uploadUrl,
|
|
77
|
+
filePath,
|
|
78
|
+
uploadFieldName: config.uploadFieldName,
|
|
79
|
+
uploadHeaders: config.uploadHeaders as Record<string, string> | undefined,
|
|
80
|
+
uploadFormFields: config.uploadFormFields as
|
|
81
|
+
| Record<string, string | number | boolean>
|
|
82
|
+
| undefined,
|
|
83
|
+
uploadResponseUrlPath: config.uploadResponseUrlPath,
|
|
84
|
+
requestTimeout: config.requestTimeout ?? 30000,
|
|
85
|
+
allowInsecureTls: config.allowInsecureTls,
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export async function sendMessageOpenclawWorkclaw(
|
|
90
|
+
params: SendOpenclawWorkclawMessageParams,
|
|
91
|
+
): Promise<OpenclawWorkclawSendResult> {
|
|
92
|
+
const { cfg, to, text, mediaUrl, replyToMessageId, accountId, openConversationId, agentId, messageType, msgType, last }
|
|
93
|
+
= params
|
|
94
|
+
|
|
95
|
+
// 如果没有指定 messageType,根据是否有 replyToMessageId 来判断
|
|
96
|
+
// - 有 replyToMessageId: 回复消息
|
|
97
|
+
// - 没有 replyToMessageId: 主动推送
|
|
98
|
+
const resolvedMessageType = messageType ?? (replyToMessageId ? 'reply' : 'push')
|
|
99
|
+
|
|
100
|
+
// 如果没有指定 accountId,尝试根据 target (to) 和 agentId 查找匹配的账户
|
|
101
|
+
let resolvedAccountId = accountId
|
|
102
|
+
if (!resolvedAccountId) {
|
|
103
|
+
const foundAccountId = findAccountIdByTarget(cfg, to, agentId)
|
|
104
|
+
if (foundAccountId) {
|
|
105
|
+
resolvedAccountId = foundAccountId
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (!resolvedAccountId) {
|
|
110
|
+
throw new Error('无法确定账户ID')
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const account = resolveOpenclawWorkclawAccount({ cfg, accountId: resolvedAccountId })
|
|
114
|
+
if (!account.configured) {
|
|
115
|
+
throw new Error(`OpenclawWorkclaw account "${account.accountId}" not configured`)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const resolvedMediaUrl = await resolveMediaUrl(mediaUrl, account.config)
|
|
119
|
+
|
|
120
|
+
// 优先使用传入的 openConversationId,其次从 cfg 中直接读取(绕过 account.config 快照问题)
|
|
121
|
+
// 这样可以获取到运行时动态保存的 openConversationId
|
|
122
|
+
interface ChannelWithAccounts { accounts?: Record<string, any>, openConversationId?: string }
|
|
123
|
+
const cfgChannel = (cfg.channels?.['openclaw-workclaw'] as unknown as ChannelWithAccounts) ?? {}
|
|
124
|
+
const cfgAccounts = cfgChannel.accounts ?? {}
|
|
125
|
+
const cfgAccount = cfgAccounts[resolvedAccountId] ?? {}
|
|
126
|
+
const cfgOpenConversationId = cfgAccount?.openConversationId ?? cfgChannel?.openConversationId
|
|
127
|
+
const effectiveOpenConversationId = openConversationId ?? cfgOpenConversationId
|
|
128
|
+
getOpenclawWorkclawLogger().info(`sendMessageOpenclawWorkclaw - resolvedAccountId: ${resolvedAccountId}, openConversationId param: ${openConversationId}, cfgOpenConversationId: ${cfgOpenConversationId}, effectiveOpenConversationId: ${effectiveOpenConversationId}, messageType: ${resolvedMessageType}, replyToMessageId: ${replyToMessageId}`)
|
|
129
|
+
|
|
130
|
+
// 使用 appKey 作为缓存键,让所有账户共享同一个 token
|
|
131
|
+
const tokenCacheKey = account.config.appKey || account.accountId
|
|
132
|
+
const result = await sendOpenclawWorkclawOutboundMessage({
|
|
133
|
+
cacheKey: tokenCacheKey,
|
|
134
|
+
to,
|
|
135
|
+
text,
|
|
136
|
+
msgType,
|
|
137
|
+
mediaUrl: resolvedMediaUrl,
|
|
138
|
+
replyToMessageId,
|
|
139
|
+
openConversationId: effectiveOpenConversationId,
|
|
140
|
+
agentId,
|
|
141
|
+
config: account.config,
|
|
142
|
+
messageType: resolvedMessageType,
|
|
143
|
+
last,
|
|
144
|
+
})
|
|
145
|
+
return {
|
|
146
|
+
messageId: result.messageId,
|
|
147
|
+
chatId: to,
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export async function getMessageOpenclawWorkclaw(_params: {
|
|
152
|
+
cfg: OpenClawConfig
|
|
153
|
+
messageId: string
|
|
154
|
+
accountId?: string
|
|
155
|
+
}): Promise<null> {
|
|
156
|
+
return null
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* 发送主动推送消息(定时任务使用)
|
|
161
|
+
* 使用 pushEndpoint 配置的端口
|
|
162
|
+
*/
|
|
163
|
+
export async function sendPushMessageOpenclawWorkclaw(
|
|
164
|
+
params: Omit<SendOpenclawWorkclawMessageParams, 'messageType'>,
|
|
165
|
+
): Promise<OpenclawWorkclawSendResult> {
|
|
166
|
+
return sendMessageOpenclawWorkclaw({
|
|
167
|
+
...params,
|
|
168
|
+
messageType: 'push',
|
|
169
|
+
})
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* 发送回复消息(用户触发)
|
|
174
|
+
* 使用 replyEndpoint 配置的端口
|
|
175
|
+
*/
|
|
176
|
+
export async function sendReplyMessageOpenclawWorkclaw(
|
|
177
|
+
params: Omit<SendOpenclawWorkclawMessageParams, 'messageType'>,
|
|
178
|
+
): Promise<OpenclawWorkclawSendResult> {
|
|
179
|
+
return sendMessageOpenclawWorkclaw({
|
|
180
|
+
...params,
|
|
181
|
+
messageType: 'reply',
|
|
182
|
+
})
|
|
183
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { OpenclawWorkclawConnectionConfig } from '../connection/workclaw-client.js'
|
|
2
|
-
import type { OpenclawWorkclawFullAccountConfig } from '../types.js'
|
|
2
|
+
import type { OpenclawWorkclawFullAccountConfig } from '../types.js'
|
|
3
3
|
import {
|
|
4
4
|
resolveOpenclawWorkclawMessage,
|
|
5
5
|
sendOpenclawWorkclawMessage,
|
|
@@ -104,7 +104,7 @@ export async function sendOpenclawWorkclawOutboundMessage(
|
|
|
104
104
|
})
|
|
105
105
|
|
|
106
106
|
return { messageId: msgId }
|
|
107
|
-
}
|
|
107
|
+
}
|
|
108
108
|
else {
|
|
109
109
|
// 主动推送消息
|
|
110
110
|
getOpenclawWorkclawLogger().info(` Sending proactive message`)
|