@workclaw/openclaw-workclaw 1.0.18 → 1.0.19
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 +364 -345
- package/dist/api.d.ts +3 -0
- package/dist/api.js +3 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +263 -0
- package/dist/setup-entry.d.ts +2 -0
- package/dist/setup-entry.js +12 -0
- package/dist/src/accounts.d.ts +66 -0
- package/dist/src/accounts.js +279 -0
- package/dist/src/api/accounts-api.d.ts +2 -0
- package/dist/src/api/accounts-api.js +130 -0
- package/dist/src/api/prompts-api.d.ts +2 -0
- package/dist/src/api/prompts-api.js +103 -0
- package/dist/src/api/session-api.d.ts +2 -0
- package/dist/src/api/session-api.js +207 -0
- package/dist/src/api/skills-api.d.ts +2 -0
- package/dist/src/api/skills-api.js +64 -0
- package/dist/src/api/workspace.d.ts +3 -0
- package/dist/src/api/workspace.js +28 -0
- package/dist/src/channel.d.ts +11 -0
- package/{src/channel.ts → dist/src/channel.js} +198 -225
- package/dist/src/config-schema.d.ts +93 -0
- package/dist/src/config-schema.js +56 -0
- package/dist/src/connection/workclaw-client.d.ts +147 -0
- package/dist/src/connection/workclaw-client.js +351 -0
- package/dist/src/gateway/agent-handlers.d.ts +12 -0
- package/{src/gateway/agent-handlers.ts → dist/src/gateway/agent-handlers.js} +433 -523
- package/dist/src/gateway/config-writer.d.ts +71 -0
- package/dist/src/gateway/config-writer.js +312 -0
- package/dist/src/gateway/message-context.d.ts +76 -0
- package/{src/gateway/message-context.ts → dist/src/gateway/message-context.js} +499 -594
- package/dist/src/gateway/message-dispatcher.d.ts +19 -0
- package/{src/gateway/message-dispatcher.ts → dist/src/gateway/message-dispatcher.js} +512 -641
- package/dist/src/gateway/reconnect.d.ts +27 -0
- package/{src/gateway/reconnect.ts → dist/src/gateway/reconnect.js} +206 -253
- package/dist/src/gateway/skills-handler.d.ts +29 -0
- package/dist/src/gateway/skills-handler.js +576 -0
- package/dist/src/gateway/skills-list-handler.d.ts +27 -0
- package/dist/src/gateway/skills-list-handler.js +233 -0
- package/dist/src/gateway/tools-list-handler.d.ts +30 -0
- package/dist/src/gateway/tools-list-handler.js +101 -0
- package/dist/src/gateway/workclaw-gateway.d.ts +14 -0
- package/dist/src/gateway/workclaw-gateway.js +223 -0
- package/dist/src/media/upload.d.ts +13 -0
- package/dist/src/media/upload.js +125 -0
- package/dist/src/outbound/index.d.ts +36 -0
- package/dist/src/outbound/index.js +123 -0
- package/dist/src/outbound/workclaw-sender.d.ts +36 -0
- package/{src/outbound/workclaw-sender.ts → dist/src/outbound/workclaw-sender.js} +95 -158
- package/dist/src/runtime.d.ts +116 -0
- package/dist/src/runtime.js +374 -0
- package/dist/src/secret-contract-api.d.ts +4 -0
- package/dist/src/secret-contract-api.js +4 -0
- package/{src/send.ts → dist/src/send.d.ts} +1 -1
- package/dist/src/send.js +1 -0
- package/dist/src/setup-api.d.ts +3 -0
- package/dist/src/setup-api.js +3 -0
- package/dist/src/setup-core.d.ts +3 -0
- package/dist/src/setup-core.js +13 -0
- package/dist/src/setup-surface.d.ts +7 -0
- package/dist/src/setup-surface.js +363 -0
- package/dist/src/tools/openclaw-workclaw-cron/api/index.d.ts +93 -0
- package/dist/src/tools/openclaw-workclaw-cron/api/index.js +209 -0
- package/dist/src/tools/openclaw-workclaw-cron/index.d.ts +10 -0
- package/{src/tools/openclaw-workclaw-cron/index.ts → dist/src/tools/openclaw-workclaw-cron/index.js} +33 -39
- package/dist/src/tools/openclaw-workclaw-cron/src/add/params.d.ts +16 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/add/params.js +152 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/add/sync.d.ts +18 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/add/sync.js +162 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/disable/params.d.ts +14 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/disable/params.js +80 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/disable/sync.d.ts +14 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/disable/sync.js +107 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/enable/params.d.ts +14 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/enable/params.js +80 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/enable/sync.d.ts +14 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/enable/sync.js +107 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/notify/sync.d.ts +17 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/notify/sync.js +127 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/remove/params.d.ts +14 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/remove/params.js +87 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/remove/sync.d.ts +14 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/remove/sync.js +107 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/update/params.d.ts +17 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/update/params.js +164 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/update/sync.d.ts +18 -0
- package/dist/src/tools/openclaw-workclaw-cron/src/update/sync.js +135 -0
- package/dist/src/tools/openclaw-workclaw-cron/types/index.d.ts +52 -0
- package/dist/src/tools/openclaw-workclaw-cron/types/index.js +4 -0
- package/dist/src/tools/openclaw-workclaw-cron/utils/index.d.ts +40 -0
- package/dist/src/tools/openclaw-workclaw-cron/utils/index.js +122 -0
- package/dist/src/tools/openclaw-workclaw-system/index.d.ts +10 -0
- package/{src/tools/openclaw-workclaw-system/index.ts → dist/src/tools/openclaw-workclaw-system/index.js} +15 -17
- package/dist/src/tools/openclaw-workclaw-system/src/get/index.d.ts +8 -0
- package/dist/src/tools/openclaw-workclaw-system/src/get/index.js +64 -0
- package/dist/src/tools/openclaw-workclaw-system/src/token/index.d.ts +8 -0
- package/dist/src/tools/openclaw-workclaw-system/src/token/index.js +78 -0
- package/{src/types.ts → dist/src/types.d.ts} +39 -58
- package/dist/src/types.js +1 -0
- package/dist/src/utils/content.d.ts +15 -0
- package/{src/utils/content.ts → dist/src/utils/content.js} +38 -35
- package/package.json +62 -49
- package/index.ts +0 -302
- package/setup-entry.ts +0 -6
- package/src/accounts.ts +0 -312
- package/src/api/accounts-api.ts +0 -156
- package/src/api/prompts-api.ts +0 -116
- package/src/api/session-api.ts +0 -238
- package/src/api/skills-api.ts +0 -72
- package/src/api/workspace.ts +0 -41
- package/src/config-schema.ts +0 -110
- package/src/connection/workclaw-client.ts +0 -643
- package/src/gateway/config-writer.ts +0 -296
- package/src/gateway/skills-handler.ts +0 -741
- package/src/gateway/skills-list-handler.ts +0 -332
- package/src/gateway/tools-list-handler.ts +0 -160
- package/src/gateway/workclaw-gateway.ts +0 -367
- package/src/media/upload.ts +0 -157
- package/src/outbound/index.ts +0 -185
- package/src/runtime.ts +0 -497
- package/src/tools/openclaw-workclaw-cron/api/index.ts +0 -326
- package/src/tools/openclaw-workclaw-cron/src/add/params.ts +0 -177
- package/src/tools/openclaw-workclaw-cron/src/add/sync.ts +0 -188
- package/src/tools/openclaw-workclaw-cron/src/disable/params.ts +0 -100
- package/src/tools/openclaw-workclaw-cron/src/disable/sync.ts +0 -127
- package/src/tools/openclaw-workclaw-cron/src/enable/params.ts +0 -100
- package/src/tools/openclaw-workclaw-cron/src/enable/sync.ts +0 -127
- package/src/tools/openclaw-workclaw-cron/src/notify/sync.ts +0 -148
- package/src/tools/openclaw-workclaw-cron/src/remove/params.ts +0 -109
- package/src/tools/openclaw-workclaw-cron/src/remove/sync.ts +0 -127
- package/src/tools/openclaw-workclaw-cron/src/update/params.ts +0 -195
- package/src/tools/openclaw-workclaw-cron/src/update/sync.ts +0 -161
- package/src/tools/openclaw-workclaw-cron/types/index.ts +0 -55
- package/src/tools/openclaw-workclaw-cron/utils/index.ts +0 -141
- package/src/tools/openclaw-workclaw-system/src/get/index.ts +0 -77
- package/src/tools/openclaw-workclaw-system/src/token/index.ts +0 -93
- package/tests/accounts.test.ts +0 -285
- package/tests/message-context.test.ts +0 -313
- package/tests/reconnect.test.ts +0 -257
- package/tests/workclaw-client.test.ts +0 -112
- package/tsconfig.json +0 -14
- package/vitest.config.ts +0 -8
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { readFile, stat } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { Agent, fetch as undiciFetch } from "undici";
|
|
6
|
+
export function isLocalMediaSource(value) {
|
|
7
|
+
const trimmed = value.trim();
|
|
8
|
+
return (trimmed.startsWith("/") ||
|
|
9
|
+
trimmed.startsWith("./") ||
|
|
10
|
+
trimmed.startsWith("../") ||
|
|
11
|
+
trimmed.startsWith("~") ||
|
|
12
|
+
trimmed.startsWith("file://"));
|
|
13
|
+
}
|
|
14
|
+
export function resolveLocalPath(input) {
|
|
15
|
+
if (input.startsWith("file://")) {
|
|
16
|
+
return fileURLToPath(input);
|
|
17
|
+
}
|
|
18
|
+
if (input.startsWith("~")) {
|
|
19
|
+
return path.join(os.homedir(), input.slice(1));
|
|
20
|
+
}
|
|
21
|
+
return path.resolve(input);
|
|
22
|
+
}
|
|
23
|
+
function readResponseUrlPath(data, pathValue) {
|
|
24
|
+
if (!pathValue)
|
|
25
|
+
return undefined;
|
|
26
|
+
const parts = pathValue.split(".").filter(Boolean);
|
|
27
|
+
let current = data;
|
|
28
|
+
for (const part of parts) {
|
|
29
|
+
if (!current || typeof current !== "object")
|
|
30
|
+
return undefined;
|
|
31
|
+
current = current[part];
|
|
32
|
+
}
|
|
33
|
+
return typeof current === "string" ? current : undefined;
|
|
34
|
+
}
|
|
35
|
+
function extractUploadedUrl(responseText, responseData, pathValue) {
|
|
36
|
+
const fromPath = readResponseUrlPath(responseData, pathValue);
|
|
37
|
+
if (fromPath)
|
|
38
|
+
return fromPath;
|
|
39
|
+
const direct = responseData.url ??
|
|
40
|
+
responseData.mediaUrl;
|
|
41
|
+
if (typeof direct === "string" && direct.trim())
|
|
42
|
+
return direct;
|
|
43
|
+
const dataObj = responseData.data;
|
|
44
|
+
if (dataObj && typeof dataObj === "object") {
|
|
45
|
+
const dataUrl = dataObj.url;
|
|
46
|
+
const dataMedia = dataObj.mediaUrl;
|
|
47
|
+
if (typeof dataUrl === "string" && dataUrl.trim())
|
|
48
|
+
return dataUrl;
|
|
49
|
+
if (typeof dataMedia === "string" && dataMedia.trim())
|
|
50
|
+
return dataMedia;
|
|
51
|
+
}
|
|
52
|
+
const resultObj = responseData.result;
|
|
53
|
+
if (resultObj && typeof resultObj === "object") {
|
|
54
|
+
const resultUrl = resultObj.url;
|
|
55
|
+
const resultMedia = resultObj.mediaUrl;
|
|
56
|
+
if (typeof resultUrl === "string" && resultUrl.trim())
|
|
57
|
+
return resultUrl;
|
|
58
|
+
if (typeof resultMedia === "string" && resultMedia.trim())
|
|
59
|
+
return resultMedia;
|
|
60
|
+
}
|
|
61
|
+
if (responseText.trim() && /^https?:\/\//i.test(responseText.trim())) {
|
|
62
|
+
return responseText.trim();
|
|
63
|
+
}
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
export async function uploadLocalMedia(params) {
|
|
67
|
+
const stats = await stat(params.filePath);
|
|
68
|
+
if (!stats.isFile()) {
|
|
69
|
+
throw new Error(`mediaUrl is not a file: ${params.filePath}`);
|
|
70
|
+
}
|
|
71
|
+
const content = await readFile(params.filePath);
|
|
72
|
+
const form = new FormData();
|
|
73
|
+
const fieldName = params.uploadFieldName?.trim() || "file";
|
|
74
|
+
const fileName = path.basename(params.filePath);
|
|
75
|
+
form.set(fieldName, new Blob([content], { type: "application/octet-stream" }), fileName);
|
|
76
|
+
if (params.uploadFormFields) {
|
|
77
|
+
for (const [key, value] of Object.entries(params.uploadFormFields)) {
|
|
78
|
+
form.set(key, String(value));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const controller = new AbortController();
|
|
82
|
+
const timeoutId = setTimeout(() => controller.abort(), params.requestTimeout);
|
|
83
|
+
let dispatcher = undefined;
|
|
84
|
+
let doFetch = globalThis.fetch.bind(globalThis);
|
|
85
|
+
if (params.allowInsecureTls) {
|
|
86
|
+
try {
|
|
87
|
+
dispatcher = new Agent({ connect: { rejectUnauthorized: false } });
|
|
88
|
+
doFetch = undiciFetch;
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
92
|
+
throw new Error(`allowInsecureTls requires undici. ${message}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
const response = await doFetch(params.uploadUrl, {
|
|
97
|
+
method: "POST",
|
|
98
|
+
headers: params.uploadHeaders ?? {},
|
|
99
|
+
body: form,
|
|
100
|
+
signal: controller.signal,
|
|
101
|
+
...(dispatcher ? { dispatcher } : {}),
|
|
102
|
+
});
|
|
103
|
+
const responseText = await response.text().catch(() => "");
|
|
104
|
+
if (!response.ok) {
|
|
105
|
+
throw new Error(`Upload failed: ${response.status} ${responseText}`);
|
|
106
|
+
}
|
|
107
|
+
let data = {};
|
|
108
|
+
if (responseText) {
|
|
109
|
+
try {
|
|
110
|
+
data = JSON.parse(responseText);
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
data = {};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const uploadedUrl = extractUploadedUrl(responseText, data, params.uploadResponseUrlPath);
|
|
117
|
+
if (!uploadedUrl) {
|
|
118
|
+
throw new Error("Upload response missing file URL");
|
|
119
|
+
}
|
|
120
|
+
return uploadedUrl;
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
clearTimeout(timeoutId);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { OpenClawConfig } from "openclaw/plugin-sdk";
|
|
2
|
+
import type { WorkclawSendResult } from "../types.js";
|
|
3
|
+
export type SendWorkclawMessageParams = {
|
|
4
|
+
cfg: OpenClawConfig;
|
|
5
|
+
to: string;
|
|
6
|
+
text: string;
|
|
7
|
+
mediaUrl?: string;
|
|
8
|
+
replyToMessageId?: string;
|
|
9
|
+
accountId?: string;
|
|
10
|
+
openConversationId?: string;
|
|
11
|
+
agentId?: string | number;
|
|
12
|
+
/**
|
|
13
|
+
* 消息类型:
|
|
14
|
+
* - "reply": 回复消息(用户触发)
|
|
15
|
+
* - "push": 主动推送(定时任务触发)
|
|
16
|
+
*/
|
|
17
|
+
messageType?: 'reply' | 'push';
|
|
18
|
+
msgType?: string;
|
|
19
|
+
last?: boolean;
|
|
20
|
+
};
|
|
21
|
+
export declare function sendMessageWorkclaw(params: SendWorkclawMessageParams): Promise<WorkclawSendResult>;
|
|
22
|
+
export declare function getMessageWorkclaw(_params: {
|
|
23
|
+
cfg: OpenClawConfig;
|
|
24
|
+
messageId: string;
|
|
25
|
+
accountId?: string;
|
|
26
|
+
}): Promise<null>;
|
|
27
|
+
/**
|
|
28
|
+
* 发送主动推送消息(定时任务使用)
|
|
29
|
+
* 使用 pushEndpoint 配置的端口
|
|
30
|
+
*/
|
|
31
|
+
export declare function sendPushMessageWorkclaw(params: Omit<SendWorkclawMessageParams, "messageType">): Promise<WorkclawSendResult>;
|
|
32
|
+
/**
|
|
33
|
+
* 发送回复消息(用户触发)
|
|
34
|
+
* 使用 replyEndpoint 配置的端口
|
|
35
|
+
*/
|
|
36
|
+
export declare function sendReplyMessageWorkclaw(params: Omit<SendWorkclawMessageParams, "messageType">): Promise<WorkclawSendResult>;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { resolveWorkclawAccountWithCache } from "../accounts.js";
|
|
2
|
+
import { resolveAccountByUserIdAndAgentId } from "../accounts.js";
|
|
3
|
+
import { isLocalMediaSource, resolveLocalPath, uploadLocalMedia } from "../media/upload.js";
|
|
4
|
+
import { sendWorkclawOutboundMessage, } from "./workclaw-sender.js";
|
|
5
|
+
import { getWorkclawLogger, setLastOutboundAt } from "../runtime.js";
|
|
6
|
+
import { loadOpenConversationId } from "../gateway/config-writer.js";
|
|
7
|
+
import { stat } from "node:fs/promises";
|
|
8
|
+
/**
|
|
9
|
+
* 根据 target (userId) 和可选的 agentId 查找匹配的账户
|
|
10
|
+
* 如果同时提供 userId 和 agentId,走 O(1) 查找
|
|
11
|
+
* 否则遍历所有账户,查找 userId 等于 target 的账户
|
|
12
|
+
*/
|
|
13
|
+
function findAccountIdByTarget(cfg, target, agentId) {
|
|
14
|
+
// O(1) 查找:userId + agentId 唯一确定一个账户
|
|
15
|
+
if (agentId !== undefined && agentId !== null) {
|
|
16
|
+
return resolveAccountByUserIdAndAgentId(cfg, target, String(agentId)) ?? undefined;
|
|
17
|
+
}
|
|
18
|
+
// 遍历:只有 userId 时,无法唯一确定账户(一个 userId 对应多个 agentId)
|
|
19
|
+
// 需要通过 bindings 或其他方式 resolve,这里暂不支持
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
async function resolveMediaUrl(mediaUrl, config) {
|
|
23
|
+
if (!mediaUrl)
|
|
24
|
+
return undefined;
|
|
25
|
+
if (!isLocalMediaSource(mediaUrl))
|
|
26
|
+
return mediaUrl;
|
|
27
|
+
const uploadUrl = config.uploadUrl;
|
|
28
|
+
if (!uploadUrl) {
|
|
29
|
+
throw new Error('uploadUrl not configured for local mediaUrl');
|
|
30
|
+
}
|
|
31
|
+
const filePath = resolveLocalPath(mediaUrl);
|
|
32
|
+
if (typeof config.mediaMaxMb === "number" && config.mediaMaxMb > 0) {
|
|
33
|
+
const stats = await stat(filePath);
|
|
34
|
+
const maxBytes = config.mediaMaxMb * 1024 * 1024;
|
|
35
|
+
if (stats.size > maxBytes) {
|
|
36
|
+
throw new Error(`mediaUrl exceeds limit (${config.mediaMaxMb} MB)`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return uploadLocalMedia({
|
|
40
|
+
uploadUrl,
|
|
41
|
+
filePath,
|
|
42
|
+
uploadFieldName: config.uploadFieldName,
|
|
43
|
+
uploadHeaders: config.uploadHeaders,
|
|
44
|
+
uploadFormFields: config.uploadFormFields,
|
|
45
|
+
uploadResponseUrlPath: config.uploadResponseUrlPath,
|
|
46
|
+
requestTimeout: config.requestTimeout ?? 30000,
|
|
47
|
+
allowInsecureTls: config.allowInsecureTls,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
export async function sendMessageWorkclaw(params) {
|
|
51
|
+
const { cfg, to, text, mediaUrl, replyToMessageId, accountId, openConversationId, agentId, messageType, msgType, last } = params;
|
|
52
|
+
// 如果没有指定 messageType,根据是否有 replyToMessageId 来判断
|
|
53
|
+
// - 有 replyToMessageId: 回复消息
|
|
54
|
+
// - 没有 replyToMessageId: 主动推送
|
|
55
|
+
const resolvedMessageType = messageType ?? (replyToMessageId ? 'reply' : 'push');
|
|
56
|
+
// 如果没有指定 accountId,尝试根据 target (to) 和 agentId 查找匹配的账户
|
|
57
|
+
let resolvedAccountId = accountId;
|
|
58
|
+
if (!resolvedAccountId) {
|
|
59
|
+
const foundAccountId = findAccountIdByTarget(cfg, to, agentId);
|
|
60
|
+
if (foundAccountId) {
|
|
61
|
+
resolvedAccountId = foundAccountId;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const account = resolveWorkclawAccountWithCache({ cfg, accountId: resolvedAccountId });
|
|
65
|
+
if (!account.configured) {
|
|
66
|
+
throw new Error(`Workclaw account "${account.accountId}" not configured`);
|
|
67
|
+
}
|
|
68
|
+
const resolvedMediaUrl = await resolveMediaUrl(mediaUrl, account.config);
|
|
69
|
+
const cfgChannel = cfg.channels?.['openclaw-workclaw'] ?? {};
|
|
70
|
+
const cfgAccounts = cfgChannel.accounts ?? {};
|
|
71
|
+
const cfgAccount = cfgAccounts[resolvedAccountId] ?? {};
|
|
72
|
+
const cfgOpenConversationId = cfgAccount?.openConversationId ?? cfgChannel?.openConversationId;
|
|
73
|
+
// 最后尝试从 state 文件恢复(进程重启后 cfg 没有持久化的 openConversationId)
|
|
74
|
+
const stateOpenConversationId = loadOpenConversationId(resolvedAccountId, to);
|
|
75
|
+
const effectiveOpenConversationId = openConversationId ?? cfgOpenConversationId ?? stateOpenConversationId;
|
|
76
|
+
getWorkclawLogger().info(`sendMessageWorkclaw - resolvedAccountId: ${resolvedAccountId}, openConversationId param: ${openConversationId}, cfgOpenConversationId: ${cfgOpenConversationId}, effectiveOpenConversationId: ${effectiveOpenConversationId}, messageType: ${resolvedMessageType}, replyToMessageId: ${replyToMessageId}`);
|
|
77
|
+
// 使用 appKey 作为缓存键,让所有账户共享同一个 token
|
|
78
|
+
const tokenCacheKey = account.config.appKey || account.accountId;
|
|
79
|
+
const result = await sendWorkclawOutboundMessage({
|
|
80
|
+
cacheKey: tokenCacheKey,
|
|
81
|
+
to,
|
|
82
|
+
text,
|
|
83
|
+
msgType,
|
|
84
|
+
mediaUrl: resolvedMediaUrl,
|
|
85
|
+
replyToMessageId,
|
|
86
|
+
openConversationId: effectiveOpenConversationId,
|
|
87
|
+
agentId,
|
|
88
|
+
config: account.config,
|
|
89
|
+
messageType: resolvedMessageType,
|
|
90
|
+
last,
|
|
91
|
+
});
|
|
92
|
+
// 更新上次出站时间
|
|
93
|
+
if (resolvedAccountId) {
|
|
94
|
+
setLastOutboundAt(resolvedAccountId, Date.now());
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
messageId: result.messageId,
|
|
98
|
+
chatId: to,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
export async function getMessageWorkclaw(_params) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* 发送主动推送消息(定时任务使用)
|
|
106
|
+
* 使用 pushEndpoint 配置的端口
|
|
107
|
+
*/
|
|
108
|
+
export async function sendPushMessageWorkclaw(params) {
|
|
109
|
+
return sendMessageWorkclaw({
|
|
110
|
+
...params,
|
|
111
|
+
messageType: 'push',
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* 发送回复消息(用户触发)
|
|
116
|
+
* 使用 replyEndpoint 配置的端口
|
|
117
|
+
*/
|
|
118
|
+
export async function sendReplyMessageWorkclaw(params) {
|
|
119
|
+
return sendMessageWorkclaw({
|
|
120
|
+
...params,
|
|
121
|
+
messageType: 'reply',
|
|
122
|
+
});
|
|
123
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { WorkclawFullAccountConfig } from "../types.js";
|
|
2
|
+
export interface WorkClawSenderParams {
|
|
3
|
+
cacheKey: string;
|
|
4
|
+
to: string;
|
|
5
|
+
text: string;
|
|
6
|
+
mediaUrl?: string;
|
|
7
|
+
replyToMessageId?: string;
|
|
8
|
+
openConversationId?: string;
|
|
9
|
+
agentId?: string | number;
|
|
10
|
+
config: WorkclawFullAccountConfig;
|
|
11
|
+
/**
|
|
12
|
+
* 消息类型:
|
|
13
|
+
* - "reply": 回复消息(用户触发)
|
|
14
|
+
* - "push": 主动推送(定时任务触发)
|
|
15
|
+
*/
|
|
16
|
+
messageType?: "reply" | "push";
|
|
17
|
+
msgType?: string;
|
|
18
|
+
last?: boolean;
|
|
19
|
+
}
|
|
20
|
+
export declare function sendWorkclawOutboundMessage(params: WorkClawSenderParams): Promise<{
|
|
21
|
+
messageId: string;
|
|
22
|
+
}>;
|
|
23
|
+
/**
|
|
24
|
+
* 发送主动推送消息(定时任务使用)
|
|
25
|
+
* POST /im/v1/messages
|
|
26
|
+
*/
|
|
27
|
+
export declare function sendWorkClawPushMessage(params: Omit<WorkClawSenderParams, "messageType">): Promise<{
|
|
28
|
+
messageId: string;
|
|
29
|
+
}>;
|
|
30
|
+
/**
|
|
31
|
+
* 发送回复消息(用户触发)
|
|
32
|
+
* POST /im/v1/messages/{messageId}/reply
|
|
33
|
+
*/
|
|
34
|
+
export declare function sendWorkclawReplyMessage(params: Omit<WorkClawSenderParams, "messageType">): Promise<{
|
|
35
|
+
messageId: string;
|
|
36
|
+
}>;
|
|
@@ -1,158 +1,95 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
openConversationId
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
(
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
cacheKey,
|
|
97
|
-
config: workclawConfig,
|
|
98
|
-
agentId: resolvedAgentId,
|
|
99
|
-
receiveId,
|
|
100
|
-
msgType: messageTypeCode,
|
|
101
|
-
content,
|
|
102
|
-
messageId: replyToMessageId,
|
|
103
|
-
openConversationId: resolvedOpenConversationId,
|
|
104
|
-
endpoint: replyEndpoint.replace("{messageId}", String(replyToMessageId)),
|
|
105
|
-
last,
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
return { messageId: msgId };
|
|
109
|
-
} else {
|
|
110
|
-
// 主动推送消息
|
|
111
|
-
getWorkclawLogger().info(` Sending proactive message`);
|
|
112
|
-
|
|
113
|
-
// 优先使用传入的 openConversationId,其次使用配置中持久化的
|
|
114
|
-
const resolvedOpenConversationId = openConversationId ?? config.openConversationId;
|
|
115
|
-
getWorkclawLogger().info(` Resolved openConversationId: ${resolvedOpenConversationId}`);
|
|
116
|
-
getWorkclawLogger().info(` Config openConversationId: ${config.openConversationId}`);
|
|
117
|
-
|
|
118
|
-
const msgId = await sendWorkclawMessage({
|
|
119
|
-
cacheKey,
|
|
120
|
-
config: workclawConfig,
|
|
121
|
-
agentId: resolvedAgentId,
|
|
122
|
-
receiveId,
|
|
123
|
-
msgType: messageTypeCode,
|
|
124
|
-
content,
|
|
125
|
-
openConversationId: resolvedOpenConversationId,
|
|
126
|
-
endpoint: pushEndpoint,
|
|
127
|
-
last,
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
return { messageId: msgId };
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* 发送主动推送消息(定时任务使用)
|
|
136
|
-
* POST /im/v1/messages
|
|
137
|
-
*/
|
|
138
|
-
export async function sendWorkClawPushMessage(
|
|
139
|
-
params: Omit<WorkClawSenderParams, "messageType">,
|
|
140
|
-
): Promise<{ messageId: string }> {
|
|
141
|
-
return sendWorkclawOutboundMessage({
|
|
142
|
-
...params,
|
|
143
|
-
messageType: "push",
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* 发送回复消息(用户触发)
|
|
149
|
-
* POST /im/v1/messages/{messageId}/reply
|
|
150
|
-
*/
|
|
151
|
-
export async function sendWorkclawReplyMessage(
|
|
152
|
-
params: Omit<WorkClawSenderParams, "messageType">,
|
|
153
|
-
): Promise<{ messageId: string }> {
|
|
154
|
-
return sendWorkclawOutboundMessage({
|
|
155
|
-
...params,
|
|
156
|
-
messageType: "reply",
|
|
157
|
-
});
|
|
158
|
-
}
|
|
1
|
+
import { sendWorkclawMessage, sendWorkclawReplyMessage as sendWorkclawReplyToMessage, resolveWorkclawMessage, } from "../connection/workclaw-client.js";
|
|
2
|
+
import { getWorkclawLogger } from "../runtime.js";
|
|
3
|
+
export async function sendWorkclawOutboundMessage(params) {
|
|
4
|
+
const { cacheKey, to, mediaUrl, replyToMessageId, openConversationId, agentId, config, messageType = "reply", last, } = params;
|
|
5
|
+
let msgType = params.msgType;
|
|
6
|
+
let content = params.text;
|
|
7
|
+
// 优先级: 传入参数 > 配置
|
|
8
|
+
const resolvedAgentId = agentId ?? config.agentId;
|
|
9
|
+
if (resolvedAgentId === undefined || resolvedAgentId === null || String(resolvedAgentId).trim() === "") {
|
|
10
|
+
throw new Error("agentId is required for sending messages. " +
|
|
11
|
+
"Please create an account via cloud platform command or configure accounts in openclaw.json");
|
|
12
|
+
}
|
|
13
|
+
if (!msgType) {
|
|
14
|
+
({ msgType, content } = resolveWorkclawMessage(content, mediaUrl));
|
|
15
|
+
}
|
|
16
|
+
// msgType 转换为 "1"(文本)或其他类型码
|
|
17
|
+
const messageTypeCode = msgType === "text" ? "1" : msgType;
|
|
18
|
+
// 构建 WorkclawConnectionConfig(短名格式,用于 connection 模块)
|
|
19
|
+
const workclawConfig = {
|
|
20
|
+
baseUrl: config.baseUrl ?? "",
|
|
21
|
+
appKey: config.appKey ?? "",
|
|
22
|
+
appSecret: config.appSecret ?? "",
|
|
23
|
+
localIp: config.localIp,
|
|
24
|
+
allowInsecureTls: config.allowInsecureTls,
|
|
25
|
+
requestTimeout: config.requestTimeout,
|
|
26
|
+
};
|
|
27
|
+
// 解析 userId 作为 receiveId(优先使用配置中保存的原始 userId)
|
|
28
|
+
// 配置中的 userId 是原始值(可能为负数),to 是绝对值(OpenClaw 要求)
|
|
29
|
+
const receiveId = config.userId ?? to;
|
|
30
|
+
// 使用固定的 endpoint
|
|
31
|
+
const replyEndpoint = "/open-apis/im/v1/messages/{messageId}/reply";
|
|
32
|
+
const pushEndpoint = "/open-apis/im/v1/messages";
|
|
33
|
+
if (messageType === "reply") {
|
|
34
|
+
// 回复消息需要 replyToMessageId
|
|
35
|
+
if (!replyToMessageId) {
|
|
36
|
+
throw new Error("replyToMessageId is required for reply message");
|
|
37
|
+
}
|
|
38
|
+
// 优先使用传入的 openConversationId,其次使用配置中持久化的
|
|
39
|
+
const resolvedOpenConversationId = openConversationId ?? config.openConversationId;
|
|
40
|
+
getWorkclawLogger().info(`Sending reply message to messageId: ${replyToMessageId}, openConversationId: ${resolvedOpenConversationId}`);
|
|
41
|
+
const msgId = await sendWorkclawReplyToMessage({
|
|
42
|
+
cacheKey,
|
|
43
|
+
config: workclawConfig,
|
|
44
|
+
agentId: resolvedAgentId,
|
|
45
|
+
receiveId,
|
|
46
|
+
msgType: messageTypeCode,
|
|
47
|
+
content,
|
|
48
|
+
messageId: replyToMessageId,
|
|
49
|
+
openConversationId: resolvedOpenConversationId,
|
|
50
|
+
endpoint: replyEndpoint.replace("{messageId}", String(replyToMessageId)),
|
|
51
|
+
last,
|
|
52
|
+
});
|
|
53
|
+
return { messageId: msgId };
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
// 主动推送消息
|
|
57
|
+
getWorkclawLogger().info(` Sending proactive message`);
|
|
58
|
+
// 优先使用传入的 openConversationId,其次使用配置中持久化的
|
|
59
|
+
const resolvedOpenConversationId = openConversationId ?? config.openConversationId;
|
|
60
|
+
getWorkclawLogger().info(` Resolved openConversationId: ${resolvedOpenConversationId}`);
|
|
61
|
+
getWorkclawLogger().info(` Config openConversationId: ${config.openConversationId}`);
|
|
62
|
+
const msgId = await sendWorkclawMessage({
|
|
63
|
+
cacheKey,
|
|
64
|
+
config: workclawConfig,
|
|
65
|
+
agentId: resolvedAgentId,
|
|
66
|
+
receiveId,
|
|
67
|
+
msgType: messageTypeCode,
|
|
68
|
+
content,
|
|
69
|
+
openConversationId: resolvedOpenConversationId,
|
|
70
|
+
endpoint: pushEndpoint,
|
|
71
|
+
last,
|
|
72
|
+
});
|
|
73
|
+
return { messageId: msgId };
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* 发送主动推送消息(定时任务使用)
|
|
78
|
+
* POST /im/v1/messages
|
|
79
|
+
*/
|
|
80
|
+
export async function sendWorkClawPushMessage(params) {
|
|
81
|
+
return sendWorkclawOutboundMessage({
|
|
82
|
+
...params,
|
|
83
|
+
messageType: "push",
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* 发送回复消息(用户触发)
|
|
88
|
+
* POST /im/v1/messages/{messageId}/reply
|
|
89
|
+
*/
|
|
90
|
+
export async function sendWorkclawReplyMessage(params) {
|
|
91
|
+
return sendWorkclawOutboundMessage({
|
|
92
|
+
...params,
|
|
93
|
+
messageType: "reply",
|
|
94
|
+
});
|
|
95
|
+
}
|