@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
package/dist/api.d.ts
ADDED
package/dist/api.js
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
|
|
2
|
+
declare const _default: {
|
|
3
|
+
id: string;
|
|
4
|
+
name: string;
|
|
5
|
+
description: string;
|
|
6
|
+
configSchema: import("openclaw/plugin-sdk").ChannelConfigSchema;
|
|
7
|
+
register: (api: OpenClawPluginApi) => void;
|
|
8
|
+
channelPlugin: import("openclaw/plugin-sdk").ChannelPlugin<import("./src/types.js").ResolvedWorkclawAccount>;
|
|
9
|
+
setChannelRuntime?: (runtime: import("openclaw/plugin-sdk").PluginRuntime) => void;
|
|
10
|
+
};
|
|
11
|
+
export default _default;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { emptyPluginConfigSchema } from "openclaw/plugin-sdk";
|
|
2
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
3
|
+
// @ts-ignore - types from openclaw 4.2, local dev uses 3.13
|
|
4
|
+
import { defineChannelPluginEntry } from "openclaw/plugin-sdk/core";
|
|
5
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import { workclawPlugin } from "./src/channel.js";
|
|
9
|
+
import { setWorkclawRuntime, getWorkclawRuntime, getToolResultHint, } from "./src/runtime.js";
|
|
10
|
+
import { resolveWorkspaceDir, isDefaultWorkspace } from "./src/api/workspace.js";
|
|
11
|
+
import { createPromptsApiHandler } from "./src/api/prompts-api.js";
|
|
12
|
+
import { createAccountsApiHandler } from "./src/api/accounts-api.js";
|
|
13
|
+
import { createSessionApiHandler } from "./src/api/session-api.js";
|
|
14
|
+
import { createSkillsApiHandler } from "./src/api/skills-api.js";
|
|
15
|
+
import { registerAllOpenclawWorkclawCronTools } from "./src/tools/openclaw-workclaw-cron/index.js";
|
|
16
|
+
import { getToolContext, setToolCallIdToRunIdMapping, deleteToolContext, } from "./src/runtime.js";
|
|
17
|
+
import { sendMessageWorkclaw } from "./src/outbound/index.js";
|
|
18
|
+
// ============================================================================
|
|
19
|
+
// Tool output formatting helpers
|
|
20
|
+
// ============================================================================
|
|
21
|
+
function formatToolParams(toolName, params) {
|
|
22
|
+
if (!params || typeof params !== "object") {
|
|
23
|
+
return "";
|
|
24
|
+
}
|
|
25
|
+
const normalizedToolName = (toolName || "").toLowerCase().trim();
|
|
26
|
+
const p = params;
|
|
27
|
+
try {
|
|
28
|
+
if (normalizedToolName === "exec" || normalizedToolName === "bash" || normalizedToolName === "shell") {
|
|
29
|
+
const cmd = p.command || p.cmd;
|
|
30
|
+
if (cmd)
|
|
31
|
+
return `command: ${String(cmd).slice(0, 200)}`;
|
|
32
|
+
}
|
|
33
|
+
else if (normalizedToolName === "read" ||
|
|
34
|
+
normalizedToolName === "file_read" ||
|
|
35
|
+
normalizedToolName === "view") {
|
|
36
|
+
const p2 = p.path;
|
|
37
|
+
if (p2)
|
|
38
|
+
return `path: ${p2}`;
|
|
39
|
+
}
|
|
40
|
+
else if (normalizedToolName === "write" ||
|
|
41
|
+
normalizedToolName === "file_write" ||
|
|
42
|
+
normalizedToolName === "edit" ||
|
|
43
|
+
normalizedToolName === "patch") {
|
|
44
|
+
const p2 = p.path;
|
|
45
|
+
if (p2)
|
|
46
|
+
return `path: ${p2}`;
|
|
47
|
+
}
|
|
48
|
+
else if (normalizedToolName === "web_fetch" ||
|
|
49
|
+
normalizedToolName === "fetch" ||
|
|
50
|
+
normalizedToolName === "http_get" ||
|
|
51
|
+
normalizedToolName === "http_request" ||
|
|
52
|
+
normalizedToolName === "open_url" ||
|
|
53
|
+
normalizedToolName === "browser_open" ||
|
|
54
|
+
normalizedToolName === "open") {
|
|
55
|
+
const u = p.url;
|
|
56
|
+
if (u)
|
|
57
|
+
return `url: ${u}`;
|
|
58
|
+
}
|
|
59
|
+
else if (normalizedToolName === "search" ||
|
|
60
|
+
normalizedToolName === "web_search" ||
|
|
61
|
+
normalizedToolName === "google_search" ||
|
|
62
|
+
normalizedToolName === "browser_search" ||
|
|
63
|
+
normalizedToolName === "browser.search") {
|
|
64
|
+
const q = p.query || p.q || p.search;
|
|
65
|
+
if (q)
|
|
66
|
+
return `query: ${q}`;
|
|
67
|
+
}
|
|
68
|
+
else if (normalizedToolName === "image_generate" ||
|
|
69
|
+
normalizedToolName === "generate_image" ||
|
|
70
|
+
normalizedToolName === "dalle" ||
|
|
71
|
+
normalizedToolName === "text_to_image") {
|
|
72
|
+
const prompt = p.prompt;
|
|
73
|
+
if (prompt)
|
|
74
|
+
return `prompt: ${String(prompt).slice(0, 200)}`;
|
|
75
|
+
}
|
|
76
|
+
else if (normalizedToolName === "code_interpreter" ||
|
|
77
|
+
normalizedToolName === "python" ||
|
|
78
|
+
normalizedToolName === "jupyter") {
|
|
79
|
+
const code = p.code;
|
|
80
|
+
if (code)
|
|
81
|
+
return `code: ${String(code).slice(0, 200)}`;
|
|
82
|
+
}
|
|
83
|
+
else if (normalizedToolName === "mcp_deliver" || normalizedToolName === "deliver") {
|
|
84
|
+
const target = p.target || p.to;
|
|
85
|
+
if (target)
|
|
86
|
+
return `target: ${target}`;
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
const entries = Object.entries(p);
|
|
90
|
+
if (entries.length > 0) {
|
|
91
|
+
return entries
|
|
92
|
+
.map(([k, v]) => {
|
|
93
|
+
const vStr = typeof v === "string" ? v : JSON.stringify(v);
|
|
94
|
+
return vStr.length > 100 ? `${k}: ${vStr.slice(0, 100)}...` : `${k}: ${vStr}`;
|
|
95
|
+
})
|
|
96
|
+
.join("\n");
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
return String(params);
|
|
102
|
+
}
|
|
103
|
+
return "";
|
|
104
|
+
}
|
|
105
|
+
function formatToolOutput(toolName, params, _result) {
|
|
106
|
+
return formatToolParams(toolName, params);
|
|
107
|
+
}
|
|
108
|
+
// ============================================================================
|
|
109
|
+
// Tool hooks
|
|
110
|
+
// ============================================================================
|
|
111
|
+
function registerToolHooks(api) {
|
|
112
|
+
api.on("agent_end", (event, ctx) => {
|
|
113
|
+
const { runId } = ctx || {};
|
|
114
|
+
if (runId) {
|
|
115
|
+
deleteToolContext(runId);
|
|
116
|
+
api.logger?.info?.(`[ToolHook] agent_end runId=${runId} cleaned up`);
|
|
117
|
+
}
|
|
118
|
+
return event;
|
|
119
|
+
});
|
|
120
|
+
api.on("before_tool_call", (event, _ctx) => {
|
|
121
|
+
api.logger?.info?.(`[ToolHook] before_tool_call event=${JSON.stringify(event)}`);
|
|
122
|
+
});
|
|
123
|
+
api.on("after_tool_call", (event, _ctx) => {
|
|
124
|
+
api.logger?.info?.(`[ToolHook] after_tool_call event=${JSON.stringify(event)}`);
|
|
125
|
+
const { runId, toolCallId, toolName, params, result } = event || {};
|
|
126
|
+
const toolCtx = runId ? getToolContext(runId) : undefined;
|
|
127
|
+
if (toolCtx && result) {
|
|
128
|
+
const content = formatToolOutput(toolName, params, result);
|
|
129
|
+
if (content) {
|
|
130
|
+
try {
|
|
131
|
+
const cfg = getWorkclawRuntime().config.loadConfig();
|
|
132
|
+
const formattedToolOutput = {
|
|
133
|
+
name: getToolResultHint(toolName),
|
|
134
|
+
toolName: toolName,
|
|
135
|
+
content: content,
|
|
136
|
+
state: "result",
|
|
137
|
+
};
|
|
138
|
+
sendMessageWorkclaw({
|
|
139
|
+
cfg,
|
|
140
|
+
to: toolCtx.target,
|
|
141
|
+
text: JSON.stringify(formattedToolOutput),
|
|
142
|
+
msgType: "26",
|
|
143
|
+
accountId: toolCtx.accountId,
|
|
144
|
+
openConversationId: toolCtx.openConversationId,
|
|
145
|
+
agentId: toolCtx.agentId,
|
|
146
|
+
replyToMessageId: toolCtx.replyToMessageId,
|
|
147
|
+
}).catch((err) => {
|
|
148
|
+
api.logger?.error?.(`[ToolHook] after_tool_call push failed: ${err}`);
|
|
149
|
+
});
|
|
150
|
+
api.logger?.info?.(`[ToolHook] after_tool_call pushed for toolName=${toolName}`);
|
|
151
|
+
}
|
|
152
|
+
catch (err) {
|
|
153
|
+
api.logger?.error?.(`[ToolHook] after_tool_call push failed: ${err}`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (runId && toolCallId) {
|
|
158
|
+
setToolCallIdToRunIdMapping(toolCallId, runId);
|
|
159
|
+
}
|
|
160
|
+
return event;
|
|
161
|
+
});
|
|
162
|
+
api.on("tool_result_persist", (event, _ctx) => {
|
|
163
|
+
return { message: event.message };
|
|
164
|
+
});
|
|
165
|
+
api.logger?.info?.(`[ToolHook] Tool execution hooks registered`);
|
|
166
|
+
}
|
|
167
|
+
// ============================================================================
|
|
168
|
+
// Templates
|
|
169
|
+
// ============================================================================
|
|
170
|
+
const templateNames = ["SOUL.md", "IDENTITY.md"];
|
|
171
|
+
/**
|
|
172
|
+
* 获取插件所在目录,兼容 Windows 下的 file:// URL 问题。
|
|
173
|
+
* 4.2 的插件加载器在 Windows 上可能传入不带协议前缀的路径。
|
|
174
|
+
*/
|
|
175
|
+
function getPluginDir() {
|
|
176
|
+
try {
|
|
177
|
+
const url = import.meta.url;
|
|
178
|
+
if (!url.startsWith("file://")) {
|
|
179
|
+
// Windows 路径被直接传入(不带 file:// 前缀),直接取目录
|
|
180
|
+
return path.dirname(url);
|
|
181
|
+
}
|
|
182
|
+
return path.dirname(fileURLToPath(url));
|
|
183
|
+
}
|
|
184
|
+
catch {
|
|
185
|
+
return process.cwd();
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
async function writeFileIfMissing(filePath, content) {
|
|
189
|
+
try {
|
|
190
|
+
await writeFile(filePath, content, { encoding: "utf-8", flag: "wx" });
|
|
191
|
+
}
|
|
192
|
+
catch (error) {
|
|
193
|
+
if (error &&
|
|
194
|
+
typeof error === "object" &&
|
|
195
|
+
"code" in error &&
|
|
196
|
+
error.code === "EEXIST") {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
throw error;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
async function releaseTemplates(api) {
|
|
203
|
+
const workspaceDir = resolveWorkspaceDir(api);
|
|
204
|
+
// 模板文件在根目录的 templates/ 中,而不是 dist/templates/
|
|
205
|
+
const currentDir = getPluginDir();
|
|
206
|
+
const templatesDir = path.join(currentDir.endsWith("dist") ? path.dirname(currentDir) : currentDir, "templates");
|
|
207
|
+
const shouldOverwrite = isDefaultWorkspace(api);
|
|
208
|
+
await mkdir(workspaceDir, { recursive: true });
|
|
209
|
+
await Promise.all(templateNames.map(async (name) => {
|
|
210
|
+
const sourcePath = path.join(templatesDir, name);
|
|
211
|
+
const targetPath = path.join(workspaceDir, name);
|
|
212
|
+
const content = await readFile(sourcePath, "utf-8");
|
|
213
|
+
if (shouldOverwrite) {
|
|
214
|
+
await writeFile(targetPath, content, { encoding: "utf-8" });
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
await writeFileIfMissing(targetPath, content);
|
|
218
|
+
}
|
|
219
|
+
}));
|
|
220
|
+
api.logger.info(`openclaw-workclaw templates ready: ${workspaceDir}`);
|
|
221
|
+
}
|
|
222
|
+
// ============================================================================
|
|
223
|
+
// Plugin entry point
|
|
224
|
+
// ============================================================================
|
|
225
|
+
export default defineChannelPluginEntry({
|
|
226
|
+
id: "openclaw-workclaw",
|
|
227
|
+
name: "openclaw-workclaw",
|
|
228
|
+
description: "智小途 channel plugin",
|
|
229
|
+
configSchema: emptyPluginConfigSchema(),
|
|
230
|
+
plugin: workclawPlugin,
|
|
231
|
+
registerFull(api) {
|
|
232
|
+
setWorkclawRuntime(api.runtime);
|
|
233
|
+
registerAllOpenclawWorkclawCronTools(api);
|
|
234
|
+
registerToolHooks(api);
|
|
235
|
+
api.registerService({
|
|
236
|
+
id: "openclaw-workclaw-templates",
|
|
237
|
+
start: async () => {
|
|
238
|
+
await releaseTemplates(api);
|
|
239
|
+
},
|
|
240
|
+
});
|
|
241
|
+
api.registerHttpRoute({
|
|
242
|
+
path: "/openclaw-workclaw/workspace/prompts",
|
|
243
|
+
handler: createPromptsApiHandler(api),
|
|
244
|
+
auth: "plugin",
|
|
245
|
+
});
|
|
246
|
+
api.registerHttpRoute({
|
|
247
|
+
path: "/openclaw-workclaw/accounts",
|
|
248
|
+
handler: createAccountsApiHandler(api),
|
|
249
|
+
auth: "plugin",
|
|
250
|
+
});
|
|
251
|
+
api.registerHttpRoute({
|
|
252
|
+
path: "/openclaw-workclaw/skills",
|
|
253
|
+
handler: createSkillsApiHandler(api),
|
|
254
|
+
auth: "plugin",
|
|
255
|
+
});
|
|
256
|
+
api.registerHttpRoute({
|
|
257
|
+
path: "/openclaw-workclaw/sessions",
|
|
258
|
+
handler: createSessionApiHandler(api),
|
|
259
|
+
auth: "plugin",
|
|
260
|
+
match: "prefix",
|
|
261
|
+
});
|
|
262
|
+
},
|
|
263
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { defineBundledChannelSetupEntry } from "openclaw/plugin-sdk/channel-entry-contract";
|
|
2
|
+
export default defineBundledChannelSetupEntry({
|
|
3
|
+
importMetaUrl: import.meta.url,
|
|
4
|
+
plugin: {
|
|
5
|
+
specifier: "./api.js",
|
|
6
|
+
exportName: "workclawPlugin",
|
|
7
|
+
},
|
|
8
|
+
secrets: {
|
|
9
|
+
specifier: "./src/secret-contract-api.js",
|
|
10
|
+
exportName: "channelSecrets",
|
|
11
|
+
},
|
|
12
|
+
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { OpenClawConfig } from 'openclaw/plugin-sdk';
|
|
2
|
+
import type { ResolvedWorkclawAccount, WorkclawConfig } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Normalize account ID by trimming whitespace
|
|
5
|
+
*/
|
|
6
|
+
export declare function normalizeAccountId(accountId: string | null | undefined): string;
|
|
7
|
+
/**
|
|
8
|
+
* Build the lookup cache from existing accounts in cfg.
|
|
9
|
+
* Called once at gateway startup.
|
|
10
|
+
*/
|
|
11
|
+
export declare function buildAccountMap(cfg: OpenClawConfig): void;
|
|
12
|
+
/**
|
|
13
|
+
* Resolve accountId by userId + agentId.
|
|
14
|
+
* First checks cache, then falls back to iteration.
|
|
15
|
+
*/
|
|
16
|
+
export declare function resolveAccountByUserIdAndAgentId(cfg: OpenClawConfig, userId: string, agentId: string): string | null;
|
|
17
|
+
/**
|
|
18
|
+
* Allocate a new workspace-N accountId and register it in the map.
|
|
19
|
+
*/
|
|
20
|
+
export declare function allocateWorkerAccountId(_cfg: OpenClawConfig, userId: string, agentId: string): string;
|
|
21
|
+
/**
|
|
22
|
+
* Refresh the cache when an account's userId or agentId changes.
|
|
23
|
+
*/
|
|
24
|
+
export declare function refreshAccountCache(cfg: OpenClawConfig): void;
|
|
25
|
+
/**
|
|
26
|
+
* List all account IDs.
|
|
27
|
+
* If no accounts are configured but plugin has workclaw config, returns [DEFAULT_ACCOUNT_ID].
|
|
28
|
+
*/
|
|
29
|
+
export declare function listWorkclawAccountIds(cfg: OpenClawConfig): string[];
|
|
30
|
+
/**
|
|
31
|
+
* Resolve the default account ID.
|
|
32
|
+
*/
|
|
33
|
+
export declare function resolveDefaultWorkclawAccountId(cfg: OpenClawConfig): string;
|
|
34
|
+
export declare function isWorkclawAccountConfigured(config: {
|
|
35
|
+
appKey?: string;
|
|
36
|
+
appSecret?: string;
|
|
37
|
+
agentId?: string | number;
|
|
38
|
+
}): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Resolve a complete account with merged config.
|
|
41
|
+
*/
|
|
42
|
+
export declare function resolveWorkclawAccount(params: {
|
|
43
|
+
cfg: OpenClawConfig;
|
|
44
|
+
accountId?: string | null;
|
|
45
|
+
}): ResolvedWorkclawAccount;
|
|
46
|
+
/**
|
|
47
|
+
* List all enabled and configured accounts.
|
|
48
|
+
*/
|
|
49
|
+
export declare function listEnabledWorkclawAccounts(cfg: OpenClawConfig): ResolvedWorkclawAccount[];
|
|
50
|
+
/**
|
|
51
|
+
* Resolve account and apply cached connection config if available.
|
|
52
|
+
* This ensures that appKey/appSecret etc. use the stable values captured at connection time,
|
|
53
|
+
* not potentially mutated values from cfg at resolution time.
|
|
54
|
+
*/
|
|
55
|
+
export declare function resolveWorkclawAccountWithCache(params: {
|
|
56
|
+
cfg: OpenClawConfig;
|
|
57
|
+
accountId?: string | null;
|
|
58
|
+
}): ResolvedWorkclawAccount;
|
|
59
|
+
/**
|
|
60
|
+
* Inspect and probe workclaw credentials.
|
|
61
|
+
* Returns a function that can be called to test the connection.
|
|
62
|
+
*/
|
|
63
|
+
export declare function inspectWorkclawCredentials(workclawCfg?: WorkclawConfig): (() => Promise<{
|
|
64
|
+
ok: boolean;
|
|
65
|
+
botName?: string;
|
|
66
|
+
}>) | null;
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { DEFAULT_ACCOUNT_ID } from 'openclaw/plugin-sdk/account-id';
|
|
2
|
+
import { getWorkclawLogger, getWorkclawConnectionConfig } from './runtime.js';
|
|
3
|
+
// =============================================================================
|
|
4
|
+
// In-memory lookup cache for (userId, agentId) → workspace-N accountId
|
|
5
|
+
// Problem: OpenClaw doesn't support dashes in account IDs, agentId can be negative
|
|
6
|
+
// Solution: workspace-N naming + in-memory Map for O(1) lookups
|
|
7
|
+
// =============================================================================
|
|
8
|
+
/**
|
|
9
|
+
* Normalize account ID by trimming whitespace
|
|
10
|
+
*/
|
|
11
|
+
export function normalizeAccountId(accountId) {
|
|
12
|
+
return String(accountId ?? '').trim();
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Key format for the lookup map: "userId|agentId"
|
|
16
|
+
*/
|
|
17
|
+
function makeLookupKey(userId, agentId) {
|
|
18
|
+
return `${userId}|${agentId}`;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* In-memory map: lookupKey -> accountId
|
|
22
|
+
*/
|
|
23
|
+
const lookupCache = new Map();
|
|
24
|
+
/**
|
|
25
|
+
* In-memory map: accountId -> { userId, agentId }
|
|
26
|
+
*/
|
|
27
|
+
const accountIndex = new Map();
|
|
28
|
+
/**
|
|
29
|
+
* Next workspace number to allocate
|
|
30
|
+
*/
|
|
31
|
+
let nextWorkspaceNum = 1;
|
|
32
|
+
/**
|
|
33
|
+
* Build the lookup cache from existing accounts in cfg.
|
|
34
|
+
* Called once at gateway startup.
|
|
35
|
+
*/
|
|
36
|
+
export function buildAccountMap(cfg) {
|
|
37
|
+
lookupCache.clear();
|
|
38
|
+
accountIndex.clear();
|
|
39
|
+
nextWorkspaceNum = 1;
|
|
40
|
+
const _WorkclawConfig = (cfg.channels?.['openclaw-workclaw'] || {});
|
|
41
|
+
const accounts = _WorkclawConfig?.accounts;
|
|
42
|
+
if (!accounts || typeof accounts !== 'object')
|
|
43
|
+
return;
|
|
44
|
+
for (const [accountId, accountCfg] of Object.entries(accounts)) {
|
|
45
|
+
if (!accountCfg)
|
|
46
|
+
continue;
|
|
47
|
+
const userId = accountCfg.userId || _WorkclawConfig.userId;
|
|
48
|
+
const agentId = accountCfg.agentId;
|
|
49
|
+
if (!agentId)
|
|
50
|
+
continue;
|
|
51
|
+
// userId may not be present for config-defined accounts; only build
|
|
52
|
+
// lookup if userId is available (dynamically created accounts have it)
|
|
53
|
+
if (userId) {
|
|
54
|
+
const key = makeLookupKey(String(userId), String(agentId));
|
|
55
|
+
lookupCache.set(key, accountId);
|
|
56
|
+
}
|
|
57
|
+
accountIndex.set(accountId, { userId: String(userId ?? ''), agentId: String(agentId) });
|
|
58
|
+
if (accountId.startsWith('workspace-')) {
|
|
59
|
+
const num = Number.parseInt(accountId.slice(accountId.lastIndexOf('-') + 1), 10);
|
|
60
|
+
if (!Number.isNaN(num) && num >= nextWorkspaceNum) {
|
|
61
|
+
nextWorkspaceNum = num + 1;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
// 默认账户允许直接使用顶层 userId/agentId,不要求必须存在 accounts.default
|
|
66
|
+
if (!accountIndex.has(DEFAULT_ACCOUNT_ID) && _WorkclawConfig.userId && _WorkclawConfig.agentId) {
|
|
67
|
+
const userId = String(_WorkclawConfig.userId);
|
|
68
|
+
const agentId = String(_WorkclawConfig.agentId);
|
|
69
|
+
const key = makeLookupKey(userId, agentId);
|
|
70
|
+
lookupCache.set(key, DEFAULT_ACCOUNT_ID);
|
|
71
|
+
accountIndex.set(DEFAULT_ACCOUNT_ID, { userId, agentId });
|
|
72
|
+
}
|
|
73
|
+
getWorkclawLogger().info(`[AccountManager] Built account map with ${lookupCache.size} entries, nextWorkspaceNum=${nextWorkspaceNum}`);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Resolve accountId by userId + agentId.
|
|
77
|
+
* First checks cache, then falls back to iteration.
|
|
78
|
+
*/
|
|
79
|
+
export function resolveAccountByUserIdAndAgentId(cfg, userId, agentId) {
|
|
80
|
+
const key = makeLookupKey(userId, agentId);
|
|
81
|
+
if (lookupCache.has(key)) {
|
|
82
|
+
return lookupCache.get(key);
|
|
83
|
+
}
|
|
84
|
+
const _WorkclawConfig = (cfg.channels?.['openclaw-workclaw'] || {});
|
|
85
|
+
const accounts = _WorkclawConfig?.accounts;
|
|
86
|
+
if (accounts && typeof accounts === 'object') {
|
|
87
|
+
for (const [accountId, accountCfg] of Object.entries(accounts)) {
|
|
88
|
+
if (!accountCfg)
|
|
89
|
+
continue;
|
|
90
|
+
const accountUserId = accountCfg.userId || _WorkclawConfig.userId;
|
|
91
|
+
if (String(accountUserId) === userId && String(accountCfg.agentId) === agentId) {
|
|
92
|
+
lookupCache.set(key, accountId);
|
|
93
|
+
accountIndex.set(accountId, { userId, agentId });
|
|
94
|
+
return accountId;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// 兼容默认账户直接写在顶层配置中的场景
|
|
99
|
+
if (String(_WorkclawConfig.userId ?? '') === userId
|
|
100
|
+
&& String(_WorkclawConfig.agentId ?? '') === agentId) {
|
|
101
|
+
lookupCache.set(key, DEFAULT_ACCOUNT_ID);
|
|
102
|
+
accountIndex.set(DEFAULT_ACCOUNT_ID, { userId, agentId });
|
|
103
|
+
return DEFAULT_ACCOUNT_ID;
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Allocate a new workspace-N accountId and register it in the map.
|
|
109
|
+
*/
|
|
110
|
+
export function allocateWorkerAccountId(_cfg, userId, agentId) {
|
|
111
|
+
const accountId = `workspace-${nextWorkspaceNum}`;
|
|
112
|
+
nextWorkspaceNum++;
|
|
113
|
+
const key = makeLookupKey(userId, agentId);
|
|
114
|
+
lookupCache.set(key, accountId);
|
|
115
|
+
accountIndex.set(accountId, { userId, agentId });
|
|
116
|
+
getWorkclawLogger().info(`[AccountManager] Allocated accountId=${accountId} for userId=${userId} agentId=${agentId}`);
|
|
117
|
+
return accountId;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Refresh the cache when an account's userId or agentId changes.
|
|
121
|
+
*/
|
|
122
|
+
export function refreshAccountCache(cfg) {
|
|
123
|
+
buildAccountMap(cfg);
|
|
124
|
+
}
|
|
125
|
+
// =============================================================================
|
|
126
|
+
// Account config resolution (pure functions, no in-memory state)
|
|
127
|
+
// =============================================================================
|
|
128
|
+
/**
|
|
129
|
+
* List all configured account IDs from the accounts field.
|
|
130
|
+
*/
|
|
131
|
+
function listConfiguredAccountIds(cfg) {
|
|
132
|
+
const accounts = cfg.channels?.['openclaw-workclaw']?.accounts;
|
|
133
|
+
if (!accounts || typeof accounts !== 'object') {
|
|
134
|
+
return [];
|
|
135
|
+
}
|
|
136
|
+
return Object.keys(accounts).filter(Boolean);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Check if plugin-level workclaw config exists (appKey + appSecret)
|
|
140
|
+
*/
|
|
141
|
+
function hasPluginLevelWorkClawConfig(cfg) {
|
|
142
|
+
const workclawCfg = cfg.channels?.['openclaw-workclaw'];
|
|
143
|
+
const key = typeof workclawCfg?.appKey === 'string' ? workclawCfg.appKey.trim() : '';
|
|
144
|
+
const secret = typeof workclawCfg?.appSecret === 'string' ? workclawCfg.appSecret.trim() : '';
|
|
145
|
+
return Boolean(key && secret);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* List all account IDs.
|
|
149
|
+
* If no accounts are configured but plugin has workclaw config, returns [DEFAULT_ACCOUNT_ID].
|
|
150
|
+
*/
|
|
151
|
+
export function listWorkclawAccountIds(cfg) {
|
|
152
|
+
const ids = listConfiguredAccountIds(cfg);
|
|
153
|
+
if (ids.length === 0) {
|
|
154
|
+
// 如果没有配置账户,但插件级别有 workclaw 配置,返回默认账户
|
|
155
|
+
if (hasPluginLevelWorkClawConfig(cfg)) {
|
|
156
|
+
return [DEFAULT_ACCOUNT_ID];
|
|
157
|
+
}
|
|
158
|
+
// Backward compatibility: no accounts configured, use default
|
|
159
|
+
return [DEFAULT_ACCOUNT_ID];
|
|
160
|
+
}
|
|
161
|
+
return [...ids].toSorted((a, b) => a.localeCompare(b));
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Resolve the default account ID.
|
|
165
|
+
*/
|
|
166
|
+
export function resolveDefaultWorkclawAccountId(cfg) {
|
|
167
|
+
const ids = listWorkclawAccountIds(cfg);
|
|
168
|
+
if (ids.includes(DEFAULT_ACCOUNT_ID)) {
|
|
169
|
+
return DEFAULT_ACCOUNT_ID;
|
|
170
|
+
}
|
|
171
|
+
return ids[0] ?? DEFAULT_ACCOUNT_ID;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Get the raw account-specific config.
|
|
175
|
+
*/
|
|
176
|
+
function resolveAccountConfig(cfg, accountId) {
|
|
177
|
+
const accounts = cfg.channels?.['openclaw-workclaw']?.accounts;
|
|
178
|
+
if (!accounts || typeof accounts !== 'object') {
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
return accounts[accountId];
|
|
182
|
+
}
|
|
183
|
+
function hasWorkClawConfig(config) {
|
|
184
|
+
const key = typeof config.appKey === 'string' ? config.appKey.trim() : '';
|
|
185
|
+
const secret = typeof config.appSecret === 'string' ? config.appSecret.trim() : '';
|
|
186
|
+
// 只要有 appKey 和 appSecret 就认为配置了 workclaw(agentId 可以动态添加)
|
|
187
|
+
return Boolean(key && secret);
|
|
188
|
+
}
|
|
189
|
+
export function isWorkclawAccountConfigured(config) {
|
|
190
|
+
return hasWorkClawConfig(config);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Merge top-level config with account-specific config.
|
|
194
|
+
* Account-specific fields override top-level fields.
|
|
195
|
+
*/
|
|
196
|
+
function mergeWorkclawAccountConfig(cfg, accountId) {
|
|
197
|
+
const workclawCfg = cfg.channels?.['openclaw-workclaw'];
|
|
198
|
+
// Extract base config (exclude accounts field to avoid recursion)
|
|
199
|
+
const { accounts: _ignored, ...base } = workclawCfg ?? {};
|
|
200
|
+
// Get account-specific overrides
|
|
201
|
+
const account = resolveAccountConfig(cfg, accountId) ?? {};
|
|
202
|
+
// Merge: account config overrides base config
|
|
203
|
+
return { ...base, ...account };
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Resolve a complete account with merged config.
|
|
207
|
+
*/
|
|
208
|
+
export function resolveWorkclawAccount(params) {
|
|
209
|
+
const accountId = normalizeAccountId(params.accountId);
|
|
210
|
+
const workclawCfg = params.cfg.channels?.['openclaw-workclaw'];
|
|
211
|
+
// Base enabled state (top-level)
|
|
212
|
+
const baseEnabled = workclawCfg?.enabled !== false;
|
|
213
|
+
// Merge configs
|
|
214
|
+
const merged = mergeWorkclawAccountConfig(params.cfg, accountId);
|
|
215
|
+
// Account-level enabled state
|
|
216
|
+
const accountEnabled = merged.enabled !== false;
|
|
217
|
+
const enabled = baseEnabled && accountEnabled;
|
|
218
|
+
return {
|
|
219
|
+
accountId,
|
|
220
|
+
enabled,
|
|
221
|
+
configured: isWorkclawAccountConfigured(merged),
|
|
222
|
+
name: merged.name?.trim() || undefined,
|
|
223
|
+
config: merged,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* List all enabled and configured accounts.
|
|
228
|
+
*/
|
|
229
|
+
export function listEnabledWorkclawAccounts(cfg) {
|
|
230
|
+
return listConfiguredAccountIds(cfg)
|
|
231
|
+
.map(accountId => resolveWorkclawAccount({ cfg, accountId }))
|
|
232
|
+
.filter(account => account.enabled && account.configured);
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Resolve account and apply cached connection config if available.
|
|
236
|
+
* This ensures that appKey/appSecret etc. use the stable values captured at connection time,
|
|
237
|
+
* not potentially mutated values from cfg at resolution time.
|
|
238
|
+
*/
|
|
239
|
+
export function resolveWorkclawAccountWithCache(params) {
|
|
240
|
+
const account = resolveWorkclawAccount(params);
|
|
241
|
+
const cachedConfig = getWorkclawConnectionConfig(account.accountId);
|
|
242
|
+
if (cachedConfig) {
|
|
243
|
+
// Apply cached values to ensure stability
|
|
244
|
+
account.config.appKey = cachedConfig.appKey || account.config.appKey;
|
|
245
|
+
account.config.appSecret = cachedConfig.appSecret || account.config.appSecret;
|
|
246
|
+
if (cachedConfig.baseUrl)
|
|
247
|
+
account.config.baseUrl = cachedConfig.baseUrl;
|
|
248
|
+
if (cachedConfig.websocketUrl !== undefined)
|
|
249
|
+
account.config.websocketUrl = cachedConfig.websocketUrl;
|
|
250
|
+
if (cachedConfig.localIp !== undefined)
|
|
251
|
+
account.config.localIp = cachedConfig.localIp;
|
|
252
|
+
if (cachedConfig.allowInsecureTls !== undefined)
|
|
253
|
+
account.config.allowInsecureTls = cachedConfig.allowInsecureTls;
|
|
254
|
+
if (cachedConfig.requestTimeout !== undefined)
|
|
255
|
+
account.config.requestTimeout = cachedConfig.requestTimeout;
|
|
256
|
+
}
|
|
257
|
+
return account;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Inspect and probe workclaw credentials.
|
|
261
|
+
* Returns a function that can be called to test the connection.
|
|
262
|
+
*/
|
|
263
|
+
export function inspectWorkclawCredentials(workclawCfg) {
|
|
264
|
+
const appKey = typeof workclawCfg?.appKey === 'string' ? workclawCfg.appKey.trim() : '';
|
|
265
|
+
const appSecret = typeof workclawCfg?.appSecret === 'string' ? workclawCfg.appSecret.trim() : '';
|
|
266
|
+
if (!appKey || !appSecret) {
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
return async () => {
|
|
270
|
+
try {
|
|
271
|
+
// 这里可以实现凭证验证逻辑
|
|
272
|
+
// 暂时返回成功,因为实际验证需要连接工作线程
|
|
273
|
+
return { ok: true };
|
|
274
|
+
}
|
|
275
|
+
catch {
|
|
276
|
+
return { ok: false };
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
}
|