@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.
Files changed (48) hide show
  1. package/README.md +325 -0
  2. package/index.ts +298 -0
  3. package/openclaw.plugin.json +10 -0
  4. package/package.json +43 -0
  5. package/skills/openclaw-workclaw-cron/SKILL.md +458 -0
  6. package/src/accounts.ts +287 -0
  7. package/src/api/accounts-api.ts +157 -0
  8. package/src/api/prompts-api.ts +123 -0
  9. package/src/api/session-api.ts +247 -0
  10. package/src/api/skills-api.ts +74 -0
  11. package/src/api/workspace.ts +43 -0
  12. package/src/channel.ts +227 -0
  13. package/src/config-schema.ts +110 -0
  14. package/src/connection/workclaw-client.ts +656 -0
  15. package/src/gateway/agent-handlers.ts +557 -0
  16. package/src/gateway/config-writer.ts +311 -0
  17. package/src/gateway/message-context.ts +422 -0
  18. package/src/gateway/message-dispatcher.ts +601 -0
  19. package/src/gateway/reconnect.ts +149 -0
  20. package/src/gateway/skills-handler.ts +759 -0
  21. package/src/gateway/skills-list-handler.ts +332 -0
  22. package/src/gateway/tools-list-handler.ts +162 -0
  23. package/src/gateway/workclaw-gateway.ts +521 -0
  24. package/src/media/upload.ts +168 -0
  25. package/src/outbound/index.ts +183 -0
  26. package/src/outbound/workclaw-sender.ts +157 -0
  27. package/src/runtime.ts +400 -0
  28. package/src/send.ts +1 -0
  29. package/src/tools/openclaw-workclaw-cron/api/index.ts +326 -0
  30. package/src/tools/openclaw-workclaw-cron/index.ts +39 -0
  31. package/src/tools/openclaw-workclaw-cron/src/add/params.ts +176 -0
  32. package/src/tools/openclaw-workclaw-cron/src/add/sync.ts +188 -0
  33. package/src/tools/openclaw-workclaw-cron/src/disable/params.ts +100 -0
  34. package/src/tools/openclaw-workclaw-cron/src/disable/sync.ts +127 -0
  35. package/src/tools/openclaw-workclaw-cron/src/enable/params.ts +100 -0
  36. package/src/tools/openclaw-workclaw-cron/src/enable/sync.ts +127 -0
  37. package/src/tools/openclaw-workclaw-cron/src/notify/sync.ts +148 -0
  38. package/src/tools/openclaw-workclaw-cron/src/remove/params.ts +109 -0
  39. package/src/tools/openclaw-workclaw-cron/src/remove/sync.ts +127 -0
  40. package/src/tools/openclaw-workclaw-cron/src/update/params.ts +197 -0
  41. package/src/tools/openclaw-workclaw-cron/src/update/sync.ts +161 -0
  42. package/src/tools/openclaw-workclaw-cron/types/index.ts +55 -0
  43. package/src/tools/openclaw-workclaw-cron/utils/index.ts +141 -0
  44. package/src/types.ts +60 -0
  45. package/src/utils/content.ts +40 -0
  46. package/templates/IDENTITY.md +14 -0
  47. package/templates/SOUL.md +0 -0
  48. package/tsconfig.json +11 -0
@@ -0,0 +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
+ }
@@ -0,0 +1,157 @@
1
+ import type { OpenclawWorkclawConnectionConfig } from '../connection/workclaw-client.js'
2
+ import type { OpenclawWorkclawFullAccountConfig } from '../types.js'
3
+ import {
4
+ resolveOpenclawWorkclawMessage,
5
+ sendOpenclawWorkclawMessage,
6
+ sendOpenclawWorkclawReplyMessage as sendWorkclawReplyToMessage,
7
+
8
+ } from '../connection/workclaw-client.js'
9
+ import { getOpenclawWorkclawLogger } from '../runtime.js'
10
+
11
+ export interface WorkClawSenderParams {
12
+ cacheKey: string
13
+ to: string
14
+ text: string
15
+ mediaUrl?: string
16
+ replyToMessageId?: string
17
+ openConversationId?: string
18
+ agentId?: string | number
19
+ config: OpenclawWorkclawFullAccountConfig
20
+ /**
21
+ * 消息类型:
22
+ * - "reply": 回复消息(用户触发)
23
+ * - "push": 主动推送(定时任务触发)
24
+ */
25
+ messageType?: 'reply' | 'push'
26
+ msgType?: string
27
+ last?: boolean
28
+ }
29
+
30
+ export async function sendOpenclawWorkclawOutboundMessage(
31
+ params: WorkClawSenderParams,
32
+ ): Promise<{ messageId: string }> {
33
+ const {
34
+ cacheKey,
35
+ to,
36
+ mediaUrl,
37
+ replyToMessageId,
38
+ openConversationId,
39
+ agentId,
40
+ config,
41
+ messageType = 'reply',
42
+ last,
43
+ } = params
44
+
45
+ let msgType = params.msgType
46
+ let content = params.text
47
+
48
+ // 优先级: 传入参数 > 配置
49
+ const resolvedAgentId = agentId ?? config.agentId
50
+
51
+ if (resolvedAgentId === undefined || resolvedAgentId === null || String(resolvedAgentId).trim() === '') {
52
+ throw new Error(
53
+ 'agentId is required for sending messages. '
54
+ + 'Please create an account via cloud platform command or configure accounts in openclaw.json',
55
+ )
56
+ }
57
+
58
+ if (!msgType) {
59
+ ({ msgType, content } = resolveOpenclawWorkclawMessage(content, mediaUrl))
60
+ }
61
+
62
+ // msgType 转换为 "1"(文本)或其他类型码
63
+ const messageTypeCode = msgType === 'text' ? '1' : msgType
64
+
65
+ // 构建 OpenclawWorkclawConnectionConfig(短名格式,用于 connection 模块)
66
+ const workclawConfig: OpenclawWorkclawConnectionConfig = {
67
+ baseUrl: config.baseUrl ?? '',
68
+ appKey: config.appKey ?? '',
69
+ appSecret: config.appSecret ?? '',
70
+ localIp: config.localIp,
71
+ allowInsecureTls: config.allowInsecureTls,
72
+ requestTimeout: config.requestTimeout,
73
+ }
74
+
75
+ // 解析 userId 作为 receiveId(优先使用配置中保存的原始 userId)
76
+ // 配置中的 userId 是原始值(可能为负数),to 是绝对值(OpenClaw 要求)
77
+ const receiveId = config.userId ?? to
78
+
79
+ // 从配置中读取 endpoint,支持自定义
80
+ const replyEndpoint = config.replyEndpoint || '/open-apis/im/v1/messages/{messageId}/reply'
81
+ const pushEndpoint = config.pushEndpoint || '/open-apis/im/v1/messages'
82
+
83
+ if (messageType === 'reply') {
84
+ // 回复消息需要 replyToMessageId
85
+ if (!replyToMessageId) {
86
+ throw new Error('replyToMessageId is required for reply message')
87
+ }
88
+
89
+ // 优先使用传入的 openConversationId,其次使用配置中持久化的
90
+ const resolvedOpenConversationId = openConversationId ?? config.openConversationId
91
+ getOpenclawWorkclawLogger().info(`Sending reply message to messageId: ${replyToMessageId}, openConversationId: ${resolvedOpenConversationId}`)
92
+
93
+ const msgId = await sendWorkclawReplyToMessage({
94
+ cacheKey,
95
+ config: workclawConfig,
96
+ agentId: resolvedAgentId,
97
+ receiveId,
98
+ msgType: messageTypeCode,
99
+ content,
100
+ messageId: replyToMessageId,
101
+ openConversationId: resolvedOpenConversationId,
102
+ endpoint: replyEndpoint.replace('{messageId}', String(replyToMessageId)),
103
+ last,
104
+ })
105
+
106
+ return { messageId: msgId }
107
+ }
108
+ else {
109
+ // 主动推送消息
110
+ getOpenclawWorkclawLogger().info(` Sending proactive message`)
111
+
112
+ // 优先使用传入的 openConversationId,其次使用配置中持久化的
113
+ const resolvedOpenConversationId = openConversationId ?? config.openConversationId
114
+ getOpenclawWorkclawLogger().info(` Resolved openConversationId: ${resolvedOpenConversationId}`)
115
+ getOpenclawWorkclawLogger().info(` Config openConversationId: ${config.openConversationId}`)
116
+
117
+ const msgId = await sendOpenclawWorkclawMessage({
118
+ cacheKey,
119
+ config: workclawConfig,
120
+ agentId: resolvedAgentId,
121
+ receiveId,
122
+ msgType: messageTypeCode,
123
+ content,
124
+ openConversationId: resolvedOpenConversationId,
125
+ endpoint: pushEndpoint,
126
+ last,
127
+ })
128
+
129
+ return { messageId: msgId }
130
+ }
131
+ }
132
+
133
+ /**
134
+ * 发送主动推送消息(定时任务使用)
135
+ * POST /im/v1/messages
136
+ */
137
+ export async function sendWorkClawPushMessage(
138
+ params: Omit<WorkClawSenderParams, 'messageType'>,
139
+ ): Promise<{ messageId: string }> {
140
+ return sendOpenclawWorkclawOutboundMessage({
141
+ ...params,
142
+ messageType: 'push',
143
+ })
144
+ }
145
+
146
+ /**
147
+ * 发送回复消息(用户触发)
148
+ * POST /im/v1/messages/{messageId}/reply
149
+ */
150
+ export async function sendOpenclawWorkclawReplyMessage(
151
+ params: Omit<WorkClawSenderParams, 'messageType'>,
152
+ ): Promise<{ messageId: string }> {
153
+ return sendOpenclawWorkclawOutboundMessage({
154
+ ...params,
155
+ messageType: 'reply',
156
+ })
157
+ }