@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,298 @@
|
|
|
1
|
+
import type { ResolvedWorkclawAccount, WorkclawAccountConfig, WorkClawBaseConfig, WorkclawConfig } from '../types.js'
|
|
2
|
+
import type { MessageDispatcherContext } from './message-dispatcher.js'
|
|
3
|
+
import { buildAccountMap, resolveAccountByUserIdAndAgentId } from '../accounts.js'
|
|
4
|
+
import {
|
|
5
|
+
clearReconnectScheduler,
|
|
6
|
+
clearWorkclawConnectionConfig,
|
|
7
|
+
clearWorkclawWsConnection,
|
|
8
|
+
createLogger,
|
|
9
|
+
finishConnecting,
|
|
10
|
+
getAllDispatchersByAppKey,
|
|
11
|
+
getAppKeyByAccountId,
|
|
12
|
+
getDispatcherByAppKeyAndAccountId,
|
|
13
|
+
getReconnectScheduler,
|
|
14
|
+
getWorkclawWsConnection,
|
|
15
|
+
registerAccountContext,
|
|
16
|
+
setReconnectScheduler,
|
|
17
|
+
setWorkclawConnectionConfig,
|
|
18
|
+
setWorkclawLoggerFromContext,
|
|
19
|
+
tryStartConnecting,
|
|
20
|
+
unregisterAccountContext,
|
|
21
|
+
|
|
22
|
+
} from '../runtime.js'
|
|
23
|
+
import { parseWorkClawMessage } from './message-context.js'
|
|
24
|
+
import { dispatchWorkclawMessage } from './message-dispatcher.js'
|
|
25
|
+
import { createReconnectScheduler } from './reconnect.js'
|
|
26
|
+
|
|
27
|
+
export interface WorkclawGatewayOptions {
|
|
28
|
+
accountId: string
|
|
29
|
+
account: ResolvedWorkclawAccount
|
|
30
|
+
cfg: any
|
|
31
|
+
log?: {
|
|
32
|
+
info?: (msg: string) => void
|
|
33
|
+
warn?: (msg: string) => void
|
|
34
|
+
error?: (msg: string) => void
|
|
35
|
+
debug?: (msg: string) => void
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function startWorkclawGateway(options: WorkclawGatewayOptions): Promise<void> {
|
|
40
|
+
const { account, log } = options
|
|
41
|
+
|
|
42
|
+
const logger = createLogger('', log)
|
|
43
|
+
setWorkclawLoggerFromContext(logger)
|
|
44
|
+
|
|
45
|
+
await startSharedWebSocket(options)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 多个账号共享一个 WebSocket 连接
|
|
50
|
+
*/
|
|
51
|
+
async function startSharedWebSocket(options: WorkclawGatewayOptions): Promise<void> {
|
|
52
|
+
const { accountId, account, cfg, log } = options
|
|
53
|
+
|
|
54
|
+
const logger = createLogger('', log)
|
|
55
|
+
const rawConfig = account.config as unknown as WorkclawConfig & WorkclawAccountConfig
|
|
56
|
+
|
|
57
|
+
const baseConfig: WorkClawBaseConfig = {
|
|
58
|
+
baseUrl: rawConfig.baseUrl,
|
|
59
|
+
websocketUrl: rawConfig.websocketUrl,
|
|
60
|
+
appKey: rawConfig.appKey,
|
|
61
|
+
appSecret: rawConfig.appSecret,
|
|
62
|
+
localIp: rawConfig.localIp,
|
|
63
|
+
allowInsecureTls: rawConfig.allowInsecureTls,
|
|
64
|
+
requestTimeout: rawConfig.requestTimeout,
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const accountConfig: WorkclawAccountConfig = {
|
|
68
|
+
agentId: rawConfig.agentId,
|
|
69
|
+
userId: rawConfig.userId,
|
|
70
|
+
openConversationId: rawConfig.openConversationId,
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const appKey = baseConfig.appKey || accountId
|
|
74
|
+
const appKeyRaw = baseConfig.appKey ?? ''
|
|
75
|
+
|
|
76
|
+
buildAccountMap(cfg)
|
|
77
|
+
|
|
78
|
+
logger.info(
|
|
79
|
+
`Gateway start accountId=${accountId} appKey=${appKeyRaw ? `${appKeyRaw.slice(0, 8)}...` : 'missing'} baseUrl=${baseConfig.baseUrl ?? ''} websocketUrl=${baseConfig.websocketUrl ?? ''}`,
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
// 注册账号上下文
|
|
83
|
+
const ctx: MessageDispatcherContext = {
|
|
84
|
+
accountId,
|
|
85
|
+
account,
|
|
86
|
+
cfg,
|
|
87
|
+
baseConfig,
|
|
88
|
+
accountConfig,
|
|
89
|
+
log: logger,
|
|
90
|
+
scheduleReconnect: () => {
|
|
91
|
+
const scheduler = getReconnectScheduler(appKey)
|
|
92
|
+
scheduler?.scheduleReconnect()
|
|
93
|
+
},
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
registerAccountContext(appKey, accountId, ctx)
|
|
97
|
+
|
|
98
|
+
// 检查是否已存在该 appKey 的 WebSocket
|
|
99
|
+
const existingWs = getWorkclawWsConnection(appKey)
|
|
100
|
+
if (existingWs) {
|
|
101
|
+
logger.info(`Gateway using existing WebSocket for appKey=${appKey}`)
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// 尝试获取连接锁,防止竞态条件
|
|
106
|
+
const startResult = tryStartConnecting(appKey)
|
|
107
|
+
|
|
108
|
+
if (!startResult.isConnector) {
|
|
109
|
+
// 另一个账号正在连接(成功或失败中),等待共享的 connectingPromise
|
|
110
|
+
logger.info(`Gateway another account is connecting, waiting... appKey=${appKey}`)
|
|
111
|
+
const resultWs = await startResult.connectingPromise
|
|
112
|
+
if (resultWs) {
|
|
113
|
+
logger.info(`Gateway connection established by another account, using it appKey=${appKey}`)
|
|
114
|
+
return
|
|
115
|
+
}
|
|
116
|
+
// 连接失败(resultWs === null),当前账号作为新 connector 重试
|
|
117
|
+
logger.warn(`Gateway connection attempt failed, will create own connection appKey=${appKey}`)
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
// 是 connector,创建连接
|
|
121
|
+
logger.info(`Gateway acquired connector lock, creating connection appKey=${appKey}`)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// 需要创建新的 WebSocket(只有获取到锁的账号才执行到这里)
|
|
125
|
+
const scheduler = createReconnectScheduler({
|
|
126
|
+
key: appKey,
|
|
127
|
+
config: baseConfig,
|
|
128
|
+
log: logger,
|
|
129
|
+
onMessage: (data: string) => {
|
|
130
|
+
handleSharedMessage(appKey, data, cfg, logger)
|
|
131
|
+
},
|
|
132
|
+
onClose: () => {
|
|
133
|
+
scheduler.scheduleReconnect()
|
|
134
|
+
},
|
|
135
|
+
// 旧代码在 setWorkclawWsConnection 后、ws.onopen 前就调用 finishConnecting,
|
|
136
|
+
// 这个回调模拟同样的时机:ws 已存入 runtime,通知所有等待者
|
|
137
|
+
onConnectingStarted: (ws: any) => {
|
|
138
|
+
finishConnecting(appKey, ws)
|
|
139
|
+
},
|
|
140
|
+
})
|
|
141
|
+
setReconnectScheduler(appKey, scheduler)
|
|
142
|
+
|
|
143
|
+
try {
|
|
144
|
+
await scheduler.connect()
|
|
145
|
+
}
|
|
146
|
+
catch (err) {
|
|
147
|
+
// 连接失败,通知所有等待者重试(不传 ws 表示失败)
|
|
148
|
+
finishConnecting(appKey)
|
|
149
|
+
throw err
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Cache the connection config to prevent cfg mutation issues during message sends
|
|
153
|
+
// 每个账户都有自己的配置缓存
|
|
154
|
+
setWorkclawConnectionConfig(accountId, {
|
|
155
|
+
appKey: baseConfig.appKey ?? '',
|
|
156
|
+
appSecret: baseConfig.appSecret ?? '',
|
|
157
|
+
baseUrl: baseConfig.baseUrl,
|
|
158
|
+
websocketUrl: baseConfig.websocketUrl,
|
|
159
|
+
localIp: baseConfig.localIp,
|
|
160
|
+
allowInsecureTls: baseConfig.allowInsecureTls,
|
|
161
|
+
requestTimeout: baseConfig.requestTimeout,
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 处理共享 WebSocket 消息
|
|
167
|
+
*/
|
|
168
|
+
function handleSharedMessage(appKey: string, data: string, cfg: any, logger: any): void {
|
|
169
|
+
const parsed = parseWorkClawMessage(data, logger)
|
|
170
|
+
|
|
171
|
+
if (!parsed) {
|
|
172
|
+
logger.warn?.(`Gateway failed to parse message`)
|
|
173
|
+
return
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// ping/pong/disconnect 无需路由
|
|
177
|
+
if (parsed.type === 'ping') {
|
|
178
|
+
const ws = getWorkclawWsConnection(appKey)
|
|
179
|
+
if (ws && ws.readyState === 1) { // OPEN
|
|
180
|
+
const pongResponse = {
|
|
181
|
+
code: 200,
|
|
182
|
+
message: 'pong',
|
|
183
|
+
metadata: { contentType: 'application/json' },
|
|
184
|
+
data: JSON.stringify(parsed.pongData),
|
|
185
|
+
}
|
|
186
|
+
ws.send(JSON.stringify(pongResponse))
|
|
187
|
+
}
|
|
188
|
+
return
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (parsed.type === 'disconnect') {
|
|
192
|
+
const ws = getWorkclawWsConnection(appKey)
|
|
193
|
+
ws?.close()
|
|
194
|
+
return
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// logger.info?.(`Gateway received message: ${data}`);
|
|
198
|
+
|
|
199
|
+
// 根据消息类型确定 userId 和 agentId
|
|
200
|
+
let userId: string = ''
|
|
201
|
+
let agentId: string = ''
|
|
202
|
+
|
|
203
|
+
if (parsed.type === 'agent_message' && parsed.message) {
|
|
204
|
+
userId = String(parsed.message.userId || '')
|
|
205
|
+
agentId = String(parsed.message.agentId || '')
|
|
206
|
+
}
|
|
207
|
+
else if (parsed.type === 'agent_created') {
|
|
208
|
+
// agent_created 触发新账号创建
|
|
209
|
+
const allDispatchers = getAllDispatchersByAppKey(appKey)
|
|
210
|
+
if (allDispatchers && allDispatchers.size > 0) {
|
|
211
|
+
const firstDispatcher = allDispatchers.values().next().value
|
|
212
|
+
dispatchWorkclawMessage(data, firstDispatcher.ctx).catch((err) => {
|
|
213
|
+
logger.error?.(`Dispatch error: ${String(err)}`)
|
|
214
|
+
})
|
|
215
|
+
}
|
|
216
|
+
return
|
|
217
|
+
}
|
|
218
|
+
else if (parsed.type === 'agent_updated' || parsed.type === 'agent_deleted') {
|
|
219
|
+
userId = String(parsed.eventData?.futureId || parsed.eventData?.userId || '')
|
|
220
|
+
agentId = String(parsed.eventData?.id || parsed.eventData?.agentId || '')
|
|
221
|
+
}
|
|
222
|
+
else if (parsed.type === 'tools_list' || parsed.type === 'skills_list' || parsed.type === 'skills_event') {
|
|
223
|
+
userId = String(parsed.eventData?.userId || '')
|
|
224
|
+
agentId = String(parsed.eventData?.agentId || '')
|
|
225
|
+
}
|
|
226
|
+
else if (parsed.type === 'init_agent') {
|
|
227
|
+
userId = String(parsed.eventData?.futureId || parsed.eventData?.userId || '')
|
|
228
|
+
agentId = String(parsed.eventData?.id || parsed.eventData?.agentId || '')
|
|
229
|
+
/**
|
|
230
|
+
* 在 init_agent 中,配置文件的agentId 为空,所有无法通过 resolveAccountByUserIdAndAgentId 查找账户
|
|
231
|
+
*/
|
|
232
|
+
|
|
233
|
+
const accountId = 'default'
|
|
234
|
+
const dispatcher = getDispatcherByAppKeyAndAccountId(appKey, accountId)
|
|
235
|
+
if (dispatcher) {
|
|
236
|
+
dispatchWorkclawMessage(data, dispatcher.ctx).catch((err) => {
|
|
237
|
+
logger.error?.(`Dispatch error: ${String(err)}`)
|
|
238
|
+
})
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
logger.warn?.(`Gateway no dispatcher for accountId=${accountId}`)
|
|
242
|
+
}
|
|
243
|
+
return
|
|
244
|
+
}
|
|
245
|
+
else if (parsed.type === 'cron_task_event') {
|
|
246
|
+
// 任务事件处理,使用默认账户
|
|
247
|
+
const accountId = 'default'
|
|
248
|
+
const dispatcher = getDispatcherByAppKeyAndAccountId(appKey, accountId)
|
|
249
|
+
if (dispatcher) {
|
|
250
|
+
dispatchWorkclawMessage(data, dispatcher.ctx).catch((err) => {
|
|
251
|
+
logger.error?.(`Dispatch error: ${String(err)}`)
|
|
252
|
+
})
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
logger.warn?.(`Gateway (per-appKey) no dispatcher for accountId=${accountId}`)
|
|
256
|
+
}
|
|
257
|
+
return
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
// 未知消息类型,忽略
|
|
261
|
+
return
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const accountId = resolveAccountByUserIdAndAgentId(cfg, userId, agentId)
|
|
265
|
+
if (!accountId) {
|
|
266
|
+
logger.warn?.(`Gateway no account found for userId=${userId} agentId=${agentId}`)
|
|
267
|
+
return
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const dispatcher = getDispatcherByAppKeyAndAccountId(appKey, accountId)
|
|
271
|
+
if (dispatcher) {
|
|
272
|
+
dispatchWorkclawMessage(data, dispatcher.ctx).catch((err) => {
|
|
273
|
+
logger.error?.(`Dispatch error: ${String(err)}`)
|
|
274
|
+
})
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
logger.warn?.(`Gateway no dispatcher for accountId=${accountId}`)
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export function stopWorkclawGateway(accountId: string): void {
|
|
282
|
+
const appKey = getAppKeyByAccountId(accountId)
|
|
283
|
+
if (!appKey)
|
|
284
|
+
return
|
|
285
|
+
|
|
286
|
+
unregisterAccountContext(appKey, accountId)
|
|
287
|
+
|
|
288
|
+
if (getAllDispatchersByAppKey(appKey)?.size === 0) {
|
|
289
|
+
const ws = getWorkclawWsConnection(appKey)
|
|
290
|
+
ws?.close()
|
|
291
|
+
clearWorkclawWsConnection(appKey)
|
|
292
|
+
|
|
293
|
+
const scheduler = getReconnectScheduler(appKey)
|
|
294
|
+
scheduler?.stopReconnect()
|
|
295
|
+
clearReconnectScheduler(appKey)
|
|
296
|
+
}
|
|
297
|
+
clearWorkclawConnectionConfig(accountId)
|
|
298
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { readFile, stat } from 'node:fs/promises'
|
|
2
|
+
import os from 'node:os'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
import { fileURLToPath } from 'node:url'
|
|
5
|
+
import { Agent, fetch as undiciFetch } from 'undici'
|
|
6
|
+
|
|
7
|
+
export function isLocalMediaSource(value: string): boolean {
|
|
8
|
+
const trimmed = value.trim()
|
|
9
|
+
return (
|
|
10
|
+
trimmed.startsWith('/')
|
|
11
|
+
|| trimmed.startsWith('./')
|
|
12
|
+
|| trimmed.startsWith('../')
|
|
13
|
+
|| trimmed.startsWith('~')
|
|
14
|
+
|| trimmed.startsWith('file://')
|
|
15
|
+
)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function resolveLocalPath(input: string): string {
|
|
19
|
+
if (input.startsWith('file://')) {
|
|
20
|
+
return fileURLToPath(input)
|
|
21
|
+
}
|
|
22
|
+
if (input.startsWith('~')) {
|
|
23
|
+
return path.join(os.homedir(), input.slice(1))
|
|
24
|
+
}
|
|
25
|
+
return path.resolve(input)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function readResponseUrlPath(
|
|
29
|
+
data: Record<string, unknown>,
|
|
30
|
+
pathValue?: string,
|
|
31
|
+
): string | undefined {
|
|
32
|
+
if (!pathValue)
|
|
33
|
+
return undefined
|
|
34
|
+
const parts = pathValue.split('.').filter(Boolean)
|
|
35
|
+
let current: unknown = data
|
|
36
|
+
for (const part of parts) {
|
|
37
|
+
if (!current || typeof current !== 'object')
|
|
38
|
+
return undefined
|
|
39
|
+
current = (current as Record<string, unknown>)[part]
|
|
40
|
+
}
|
|
41
|
+
return typeof current === 'string' ? current : undefined
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function extractUploadedUrl(
|
|
45
|
+
responseText: string,
|
|
46
|
+
responseData: Record<string, unknown>,
|
|
47
|
+
pathValue?: string,
|
|
48
|
+
): string | undefined {
|
|
49
|
+
const fromPath = readResponseUrlPath(responseData, pathValue)
|
|
50
|
+
if (fromPath)
|
|
51
|
+
return fromPath
|
|
52
|
+
|
|
53
|
+
const direct
|
|
54
|
+
= (responseData.url as string | undefined)
|
|
55
|
+
?? (responseData.mediaUrl as string | undefined)
|
|
56
|
+
if (typeof direct === 'string' && direct.trim())
|
|
57
|
+
return direct
|
|
58
|
+
|
|
59
|
+
const dataObj = responseData.data
|
|
60
|
+
if (dataObj && typeof dataObj === 'object') {
|
|
61
|
+
const dataUrl = (dataObj as Record<string, unknown>).url
|
|
62
|
+
const dataMedia = (dataObj as Record<string, unknown>).mediaUrl
|
|
63
|
+
if (typeof dataUrl === 'string' && dataUrl.trim())
|
|
64
|
+
return dataUrl
|
|
65
|
+
if (typeof dataMedia === 'string' && dataMedia.trim())
|
|
66
|
+
return dataMedia
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const resultObj = responseData.result
|
|
70
|
+
if (resultObj && typeof resultObj === 'object') {
|
|
71
|
+
const resultUrl = (resultObj as Record<string, unknown>).url
|
|
72
|
+
const resultMedia = (resultObj as Record<string, unknown>).mediaUrl
|
|
73
|
+
if (typeof resultUrl === 'string' && resultUrl.trim())
|
|
74
|
+
return resultUrl
|
|
75
|
+
if (typeof resultMedia === 'string' && resultMedia.trim())
|
|
76
|
+
return resultMedia
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (responseText.trim() && /^https?:\/\//i.test(responseText.trim())) {
|
|
80
|
+
return responseText.trim()
|
|
81
|
+
}
|
|
82
|
+
return undefined
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface UploadLocalMediaParams {
|
|
86
|
+
uploadUrl: string
|
|
87
|
+
filePath: string
|
|
88
|
+
uploadFieldName?: string
|
|
89
|
+
uploadHeaders?: Record<string, string>
|
|
90
|
+
uploadFormFields?: Record<string, string | number | boolean>
|
|
91
|
+
uploadResponseUrlPath?: string
|
|
92
|
+
requestTimeout: number
|
|
93
|
+
allowInsecureTls?: boolean
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export async function uploadLocalMedia(params: UploadLocalMediaParams): Promise<string> {
|
|
97
|
+
const stats = await stat(params.filePath)
|
|
98
|
+
if (!stats.isFile()) {
|
|
99
|
+
throw new Error(`mediaUrl is not a file: ${params.filePath}`)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const content = await readFile(params.filePath)
|
|
103
|
+
const form = new FormData()
|
|
104
|
+
const fieldName = params.uploadFieldName?.trim() || 'file'
|
|
105
|
+
const fileName = path.basename(params.filePath)
|
|
106
|
+
form.set(fieldName, new Blob([content], { type: 'application/octet-stream' }), fileName)
|
|
107
|
+
|
|
108
|
+
if (params.uploadFormFields) {
|
|
109
|
+
for (const [key, value] of Object.entries(params.uploadFormFields)) {
|
|
110
|
+
form.set(key, String(value))
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const controller = new AbortController()
|
|
115
|
+
const timeoutId = setTimeout(() => controller.abort(), params.requestTimeout)
|
|
116
|
+
|
|
117
|
+
let dispatcher: unknown
|
|
118
|
+
let doFetch: any = globalThis.fetch.bind(globalThis)
|
|
119
|
+
|
|
120
|
+
if (params.allowInsecureTls) {
|
|
121
|
+
try {
|
|
122
|
+
dispatcher = new Agent({ connect: { rejectUnauthorized: false } })
|
|
123
|
+
doFetch = undiciFetch as any
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
127
|
+
throw new Error(`allowInsecureTls requires undici. ${message}`)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
try {
|
|
132
|
+
const response = await doFetch(params.uploadUrl, {
|
|
133
|
+
method: 'POST',
|
|
134
|
+
headers: params.uploadHeaders ?? {},
|
|
135
|
+
body: form,
|
|
136
|
+
signal: controller.signal,
|
|
137
|
+
...(dispatcher ? { dispatcher } : {}),
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
const responseText = await response.text().catch(() => '')
|
|
141
|
+
if (!response.ok) {
|
|
142
|
+
throw new Error(`Upload failed: ${response.status} ${responseText}`)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
let data: Record<string, unknown> = {}
|
|
146
|
+
if (responseText) {
|
|
147
|
+
try {
|
|
148
|
+
data = JSON.parse(responseText) as Record<string, unknown>
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
data = {}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const uploadedUrl = extractUploadedUrl(
|
|
156
|
+
responseText,
|
|
157
|
+
data,
|
|
158
|
+
params.uploadResponseUrlPath,
|
|
159
|
+
)
|
|
160
|
+
if (!uploadedUrl) {
|
|
161
|
+
throw new Error('Upload response missing file URL')
|
|
162
|
+
}
|
|
163
|
+
return uploadedUrl
|
|
164
|
+
}
|
|
165
|
+
finally {
|
|
166
|
+
clearTimeout(timeoutId)
|
|
167
|
+
}
|
|
168
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import type { OpenClawConfig } from 'openclaw/plugin-sdk'
|
|
2
|
+
import type { WorkclawConfig, WorkclawSendResult } from '../types.js'
|
|
3
|
+
import { stat } from 'node:fs/promises'
|
|
4
|
+
import { resolveAccountByUserIdAndAgentId, resolveWorkclawAccountWithCache } from '../accounts.js'
|
|
5
|
+
|
|
6
|
+
import { loadOpenConversationId } from '../gateway/config-writer.js'
|
|
7
|
+
import { isLocalMediaSource, resolveLocalPath, uploadLocalMedia } from '../media/upload.js'
|
|
8
|
+
import { getWorkclawLogger, setLastOutboundAt } from '../runtime.js'
|
|
9
|
+
import {
|
|
10
|
+
sendWorkclawOutboundMessage,
|
|
11
|
+
} from './workclaw-sender.js'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 根据 target (userId) 和可选的 agentId 查找匹配的账户
|
|
15
|
+
* 如果同时提供 userId 和 agentId,走 O(1) 查找
|
|
16
|
+
* 否则遍历所有账户,查找 userId 等于 target 的账户
|
|
17
|
+
*/
|
|
18
|
+
function findAccountIdByTarget(
|
|
19
|
+
cfg: OpenClawConfig,
|
|
20
|
+
target: string,
|
|
21
|
+
agentId?: string | number,
|
|
22
|
+
): string | undefined {
|
|
23
|
+
// O(1) 查找:userId + agentId 唯一确定一个账户
|
|
24
|
+
if (agentId !== undefined && agentId !== null) {
|
|
25
|
+
return resolveAccountByUserIdAndAgentId(cfg, target, String(agentId)) ?? undefined
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// 遍历:只有 userId 时,无法唯一确定账户(一个 userId 对应多个 agentId)
|
|
29
|
+
// 需要通过 bindings 或其他方式 resolve,这里暂不支持
|
|
30
|
+
return undefined
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface SendWorkclawMessageParams {
|
|
34
|
+
cfg: OpenClawConfig
|
|
35
|
+
to: string
|
|
36
|
+
text: string
|
|
37
|
+
mediaUrl?: string
|
|
38
|
+
replyToMessageId?: string
|
|
39
|
+
accountId?: string
|
|
40
|
+
openConversationId?: string
|
|
41
|
+
agentId?: string | number
|
|
42
|
+
/**
|
|
43
|
+
* 消息类型:
|
|
44
|
+
* - "reply": 回复消息(用户触发)
|
|
45
|
+
* - "push": 主动推送(定时任务触发)
|
|
46
|
+
*/
|
|
47
|
+
messageType?: 'reply' | 'push'
|
|
48
|
+
msgType?: string
|
|
49
|
+
last?: boolean
|
|
50
|
+
/** 来源:deliver=流式响应回调,sendText=框架直接发送,after_tool_call=工具执行结果推送 */
|
|
51
|
+
source?: 'deliver' | 'sendText' | 'after_tool_call'
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function resolveMediaUrl(
|
|
55
|
+
mediaUrl: string | undefined,
|
|
56
|
+
config: WorkclawConfig,
|
|
57
|
+
): Promise<string | undefined> {
|
|
58
|
+
if (!mediaUrl)
|
|
59
|
+
return undefined
|
|
60
|
+
if (!isLocalMediaSource(mediaUrl))
|
|
61
|
+
return mediaUrl
|
|
62
|
+
|
|
63
|
+
const uploadUrl = config.uploadUrl
|
|
64
|
+
if (!uploadUrl) {
|
|
65
|
+
throw new Error('uploadUrl not configured for local mediaUrl')
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const filePath = resolveLocalPath(mediaUrl)
|
|
69
|
+
|
|
70
|
+
if (typeof config.mediaMaxMb === 'number' && config.mediaMaxMb > 0) {
|
|
71
|
+
const stats = await stat(filePath)
|
|
72
|
+
const maxBytes = config.mediaMaxMb * 1024 * 1024
|
|
73
|
+
if (stats.size > maxBytes) {
|
|
74
|
+
throw new Error(`mediaUrl exceeds limit (${config.mediaMaxMb} MB)`)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return uploadLocalMedia({
|
|
79
|
+
uploadUrl,
|
|
80
|
+
filePath,
|
|
81
|
+
uploadFieldName: config.uploadFieldName,
|
|
82
|
+
uploadHeaders: config.uploadHeaders as Record<string, string> | undefined,
|
|
83
|
+
uploadFormFields: config.uploadFormFields as
|
|
84
|
+
| Record<string, string | number | boolean>
|
|
85
|
+
| undefined,
|
|
86
|
+
uploadResponseUrlPath: config.uploadResponseUrlPath,
|
|
87
|
+
requestTimeout: config.requestTimeout ?? 30000,
|
|
88
|
+
allowInsecureTls: config.allowInsecureTls,
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export async function sendMessageWorkclaw(
|
|
93
|
+
params: SendWorkclawMessageParams,
|
|
94
|
+
): Promise<WorkclawSendResult> {
|
|
95
|
+
const { cfg, to, text, mediaUrl, replyToMessageId, accountId, openConversationId, agentId, messageType, msgType, last }
|
|
96
|
+
= params
|
|
97
|
+
|
|
98
|
+
// 如果没有指定 messageType,根据是否有 replyToMessageId 来判断
|
|
99
|
+
// - 有 replyToMessageId: 回复消息
|
|
100
|
+
// - 没有 replyToMessageId: 主动推送
|
|
101
|
+
const resolvedMessageType = messageType ?? (replyToMessageId ? 'reply' : 'push')
|
|
102
|
+
|
|
103
|
+
// 如果没有指定 accountId,尝试根据 target (to) 和 agentId 查找匹配的账户
|
|
104
|
+
let resolvedAccountId = accountId
|
|
105
|
+
if (!resolvedAccountId) {
|
|
106
|
+
const foundAccountId = findAccountIdByTarget(cfg, to, agentId)
|
|
107
|
+
if (foundAccountId) {
|
|
108
|
+
resolvedAccountId = foundAccountId
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const account = resolveWorkclawAccountWithCache({ cfg, accountId: resolvedAccountId })
|
|
113
|
+
if (!account.configured) {
|
|
114
|
+
throw new Error(`Workclaw account "${account.accountId}" not configured`)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const resolvedMediaUrl = await resolveMediaUrl(mediaUrl, account.config)
|
|
118
|
+
|
|
119
|
+
// 优先使用传入的 openConversationId,其次从 cfg 中直接读取(绕过 account.config 快照问题)
|
|
120
|
+
// 这样可以获取到运行时动态保存的 openConversationId
|
|
121
|
+
interface ChannelWithAccounts { accounts?: Record<string, any>, openConversationId?: string }
|
|
122
|
+
const cfgChannel = (cfg.channels?.['openclaw-workclaw'] as unknown as ChannelWithAccounts) ?? {}
|
|
123
|
+
const cfgAccounts = cfgChannel.accounts ?? {}
|
|
124
|
+
const cfgAccount = cfgAccounts[resolvedAccountId] ?? {}
|
|
125
|
+
const cfgOpenConversationId = cfgAccount?.openConversationId ?? cfgChannel?.openConversationId
|
|
126
|
+
// 最后尝试从 state 文件恢复(进程重启后 cfg 没有持久化的 openConversationId)
|
|
127
|
+
const stateOpenConversationId = loadOpenConversationId(resolvedAccountId, to)
|
|
128
|
+
const effectiveOpenConversationId = openConversationId ?? cfgOpenConversationId ?? stateOpenConversationId
|
|
129
|
+
getWorkclawLogger().info(`[SEND] source=${params.source || '?'} type=${resolvedMessageType} conv=${effectiveOpenConversationId || '(none)'} replyTo=${replyToMessageId}`)
|
|
130
|
+
|
|
131
|
+
// 使用 appKey 作为缓存键,让所有账户共享同一个 token
|
|
132
|
+
const tokenCacheKey = account.config.appKey || account.accountId
|
|
133
|
+
const result = await sendWorkclawOutboundMessage({
|
|
134
|
+
cacheKey: tokenCacheKey,
|
|
135
|
+
to,
|
|
136
|
+
text,
|
|
137
|
+
msgType,
|
|
138
|
+
mediaUrl: resolvedMediaUrl,
|
|
139
|
+
replyToMessageId,
|
|
140
|
+
openConversationId: effectiveOpenConversationId,
|
|
141
|
+
agentId,
|
|
142
|
+
config: account.config,
|
|
143
|
+
messageType: resolvedMessageType,
|
|
144
|
+
last,
|
|
145
|
+
source: params.source,
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
// 更新上次出站时间
|
|
149
|
+
if (resolvedAccountId) {
|
|
150
|
+
setLastOutboundAt(resolvedAccountId, Date.now())
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
messageId: result.messageId,
|
|
155
|
+
chatId: to,
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export async function getMessageWorkclaw(_params: {
|
|
160
|
+
cfg: OpenClawConfig
|
|
161
|
+
messageId: string
|
|
162
|
+
accountId?: string
|
|
163
|
+
}): Promise<null> {
|
|
164
|
+
return null
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* 发送主动推送消息(定时任务使用)
|
|
169
|
+
* 使用 pushEndpoint 配置的端口
|
|
170
|
+
*/
|
|
171
|
+
export async function sendPushMessageWorkclaw(
|
|
172
|
+
params: Omit<SendWorkclawMessageParams, 'messageType'>,
|
|
173
|
+
): Promise<WorkclawSendResult> {
|
|
174
|
+
return sendMessageWorkclaw({
|
|
175
|
+
...params,
|
|
176
|
+
messageType: 'push',
|
|
177
|
+
})
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* 发送回复消息(用户触发)
|
|
182
|
+
* 使用 replyEndpoint 配置的端口
|
|
183
|
+
*/
|
|
184
|
+
export async function sendReplyMessageWorkclaw(
|
|
185
|
+
params: Omit<SendWorkclawMessageParams, 'messageType'>,
|
|
186
|
+
): Promise<WorkclawSendResult> {
|
|
187
|
+
return sendMessageWorkclaw({
|
|
188
|
+
...params,
|
|
189
|
+
messageType: 'reply',
|
|
190
|
+
})
|
|
191
|
+
}
|