@workclaw/openclaw-workclaw 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +325 -0
- package/index.ts +298 -0
- package/openclaw.plugin.json +10 -0
- package/package.json +43 -0
- package/skills/openclaw-workclaw-cron/SKILL.md +458 -0
- package/src/accounts.ts +287 -0
- package/src/api/accounts-api.ts +157 -0
- package/src/api/prompts-api.ts +123 -0
- package/src/api/session-api.ts +247 -0
- package/src/api/skills-api.ts +74 -0
- package/src/api/workspace.ts +43 -0
- package/src/channel.ts +227 -0
- package/src/config-schema.ts +110 -0
- package/src/connection/workclaw-client.ts +656 -0
- package/src/gateway/agent-handlers.ts +557 -0
- package/src/gateway/config-writer.ts +311 -0
- package/src/gateway/message-context.ts +422 -0
- package/src/gateway/message-dispatcher.ts +601 -0
- package/src/gateway/reconnect.ts +149 -0
- package/src/gateway/skills-handler.ts +759 -0
- package/src/gateway/skills-list-handler.ts +332 -0
- package/src/gateway/tools-list-handler.ts +162 -0
- package/src/gateway/workclaw-gateway.ts +521 -0
- package/src/media/upload.ts +168 -0
- package/src/outbound/index.ts +183 -0
- package/src/outbound/workclaw-sender.ts +157 -0
- package/src/runtime.ts +400 -0
- package/src/send.ts +1 -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 +176 -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 +197 -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/types.ts +60 -0
- package/src/utils/content.ts +40 -0
- package/templates/IDENTITY.md +14 -0
- package/templates/SOUL.md +0 -0
- package/tsconfig.json +11 -0
|
@@ -0,0 +1,557 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent Handlers - handle AGENT_CREATED, AGENT_UPDATED, AGENT_DELETED events
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { execSync } from 'node:child_process'
|
|
6
|
+
import { existsSync } from 'node:fs'
|
|
7
|
+
import { copyFile, mkdir, writeFile } from 'node:fs/promises'
|
|
8
|
+
import { homedir } from 'node:os'
|
|
9
|
+
import { join } from 'node:path'
|
|
10
|
+
import process from 'node:process'
|
|
11
|
+
import { allocateWorkerAccountId, refreshAccountCache, resolveAccountByUserIdAndAgentId } from '../accounts.js'
|
|
12
|
+
import { writeConfigFile } from './config-writer.js'
|
|
13
|
+
|
|
14
|
+
/** 修复目录所有权(仅非 Windows 环境) */
|
|
15
|
+
function fixOwner(dir: string): void {
|
|
16
|
+
if (process.platform === 'win32') {
|
|
17
|
+
return
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
execSync(`chown -R node:node "${dir}"`, { stdio: 'ignore' })
|
|
22
|
+
}
|
|
23
|
+
catch {}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface AgentLogger {
|
|
27
|
+
info?: (msg: string) => void
|
|
28
|
+
warn?: (msg: string) => void
|
|
29
|
+
error?: (msg: string) => void
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function createSubAgentForAccount(
|
|
33
|
+
agentId: string | number,
|
|
34
|
+
userId: string | number,
|
|
35
|
+
cfg: any,
|
|
36
|
+
log?: AgentLogger,
|
|
37
|
+
): Promise<any> {
|
|
38
|
+
try {
|
|
39
|
+
const channels = cfg?.channels ?? {}
|
|
40
|
+
const openclawWorkclaw = channels['openclaw-workclaw'] ?? {}
|
|
41
|
+
const accounts = openclawWorkclaw.accounts ?? {}
|
|
42
|
+
const accountCount = Object.keys(accounts).length
|
|
43
|
+
const subAgentId = `openclaw-workclaw-${agentId}`
|
|
44
|
+
|
|
45
|
+
const existingAgents = cfg?.agents?.list ?? []
|
|
46
|
+
const existingBindings = cfg?.bindings ?? []
|
|
47
|
+
|
|
48
|
+
const agentExists = existingAgents.some((a: any) => a.id === subAgentId)
|
|
49
|
+
|
|
50
|
+
if (!agentExists) {
|
|
51
|
+
const newAgentsList = [...existingAgents]
|
|
52
|
+
const subAgent = {
|
|
53
|
+
id: subAgentId,
|
|
54
|
+
default: false,
|
|
55
|
+
name: `智小途 - Agent ${agentId}`,
|
|
56
|
+
workspace: `~/.openclaw/workspace-workclaw/${agentId}`,
|
|
57
|
+
agentDir: `~/.openclaw/agents/workspace-workclaw-${agentId}/agent`,
|
|
58
|
+
tools: { allow: ['*'] },
|
|
59
|
+
}
|
|
60
|
+
newAgentsList.push(subAgent)
|
|
61
|
+
|
|
62
|
+
if (accountCount === 0) {
|
|
63
|
+
const defaultAgent = {
|
|
64
|
+
id: 'default',
|
|
65
|
+
default: true,
|
|
66
|
+
name: 'Default Agent',
|
|
67
|
+
workspace: 'default',
|
|
68
|
+
tools: { allow: ['*'] },
|
|
69
|
+
}
|
|
70
|
+
newAgentsList.push(defaultAgent)
|
|
71
|
+
log?.info?.(`SubAgent: Created default agent with default workspace`)
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
log?.info?.(`SubAgent: Using existing default agent`)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const bindingAccountId = resolveAccountByUserIdAndAgentId(cfg, String(userId), String(agentId))
|
|
78
|
+
const newBinding = {
|
|
79
|
+
agentId: subAgentId,
|
|
80
|
+
match: {
|
|
81
|
+
channel: 'openclaw-workclaw',
|
|
82
|
+
accountId: bindingAccountId ?? '*',
|
|
83
|
+
},
|
|
84
|
+
}
|
|
85
|
+
const newBindings = [...existingBindings, newBinding]
|
|
86
|
+
log?.info?.(`SubAgent: Created binding for agentId: ${subAgentId}, accountId: ${bindingAccountId ?? '*'}`)
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
agents: {
|
|
90
|
+
list: newAgentsList,
|
|
91
|
+
defaults: cfg?.agents?.defaults ?? {},
|
|
92
|
+
},
|
|
93
|
+
bindings: newBindings,
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
const newAgents = {
|
|
98
|
+
list: [...existingAgents],
|
|
99
|
+
defaults: cfg?.agents?.defaults ?? {},
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const bindingExists = existingBindings.some((b: any) => b?.agentId === subAgentId)
|
|
103
|
+
const newBindings = [...existingBindings]
|
|
104
|
+
|
|
105
|
+
if (!bindingExists) {
|
|
106
|
+
const bindingAccountId = resolveAccountByUserIdAndAgentId(cfg, String(userId), String(agentId))
|
|
107
|
+
newBindings.push({
|
|
108
|
+
agentId: subAgentId,
|
|
109
|
+
match: {
|
|
110
|
+
channel: 'openclaw-workclaw',
|
|
111
|
+
accountId: bindingAccountId ?? '*',
|
|
112
|
+
},
|
|
113
|
+
})
|
|
114
|
+
log?.info?.(`SubAgent: Created binding for agentId: ${subAgentId}, accountId: ${bindingAccountId ?? '*'}`)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
log?.info?.(`SubAgent: Created sub-agent: ${subAgentId} with workspace: openclaw-workclaw-${agentId} (account #${accountCount + 1})`)
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
agents: newAgents,
|
|
121
|
+
bindings: newBindings,
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
catch (err) {
|
|
126
|
+
log?.error?.(`SubAgent: Failed to create sub-agent: ${String(err)}`)
|
|
127
|
+
return {}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 初始化智能体 workspace 目录
|
|
133
|
+
* 复制 AGENTS.md、TOOLS.md、USER.md,并生成 IDENTITY.md、SOUL.md
|
|
134
|
+
*/
|
|
135
|
+
async function initializeAgentWorkspace(
|
|
136
|
+
agentId: string | number,
|
|
137
|
+
accountData: {
|
|
138
|
+
nickName?: string
|
|
139
|
+
phone?: string
|
|
140
|
+
introduction?: string
|
|
141
|
+
name?: string
|
|
142
|
+
tip?: string
|
|
143
|
+
characterSettings?: string
|
|
144
|
+
personFeatures?: string
|
|
145
|
+
workFeatures?: string
|
|
146
|
+
learningFeatures?: string
|
|
147
|
+
socializeFeatures?: string
|
|
148
|
+
job?: string
|
|
149
|
+
city?: string
|
|
150
|
+
},
|
|
151
|
+
log?: AgentLogger,
|
|
152
|
+
): Promise<void> {
|
|
153
|
+
try {
|
|
154
|
+
const agentIdStr = String(agentId)
|
|
155
|
+
const home = homedir().trim()
|
|
156
|
+
const workspaceDir = join(home, '.openclaw', 'workspace-workclaw', agentIdStr)
|
|
157
|
+
const mainWorkspaceDir = join(home, '.openclaw', 'workspace')
|
|
158
|
+
const openclawDir = join(home, '.openclaw')
|
|
159
|
+
|
|
160
|
+
// 确保 .openclaw 目录存在
|
|
161
|
+
if (!existsSync(openclawDir)) {
|
|
162
|
+
try {
|
|
163
|
+
await mkdir(openclawDir, { recursive: true, mode: 0o755 })
|
|
164
|
+
log?.info?.(`Workspace: Created directory ${openclawDir}`)
|
|
165
|
+
}
|
|
166
|
+
catch (err: any) {
|
|
167
|
+
if (err.code === 'EACCES' || err.code === 'EPERM') {
|
|
168
|
+
log?.warn?.(`Workspace: Permission denied creating ${openclawDir}, attempting with 777`)
|
|
169
|
+
await mkdir(openclawDir, { recursive: true, mode: 0o777 })
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
throw err
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
// 修复权限(目录可能已存在但权限不对)
|
|
177
|
+
fixOwner(openclawDir)
|
|
178
|
+
|
|
179
|
+
// 确保 workspace 目录存在
|
|
180
|
+
if (!existsSync(workspaceDir)) {
|
|
181
|
+
try {
|
|
182
|
+
await mkdir(workspaceDir, { recursive: true, mode: 0o755 })
|
|
183
|
+
log?.info?.(`Workspace: Created directory ${workspaceDir}`)
|
|
184
|
+
}
|
|
185
|
+
catch (err: any) {
|
|
186
|
+
if (err.code === 'EACCES' || err.code === 'EPERM') {
|
|
187
|
+
log?.warn?.(`Workspace: Permission denied creating ${workspaceDir}, attempting with 777`)
|
|
188
|
+
await mkdir(workspaceDir, { recursive: true, mode: 0o777 })
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
throw err
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
// 修复权限(目录可能已存在但权限不对)
|
|
196
|
+
fixOwner(workspaceDir)
|
|
197
|
+
|
|
198
|
+
// 确保 main workspace 目录存在(用于复制模板文件)
|
|
199
|
+
if (!existsSync(mainWorkspaceDir)) {
|
|
200
|
+
try {
|
|
201
|
+
await mkdir(mainWorkspaceDir, { recursive: true, mode: 0o755 })
|
|
202
|
+
log?.info?.(`Workspace: Created directory ${mainWorkspaceDir}`)
|
|
203
|
+
}
|
|
204
|
+
catch (err: any) {
|
|
205
|
+
if (err.code === 'EACCES' || err.code === 'EPERM') {
|
|
206
|
+
log?.warn?.(`Workspace: Permission denied creating ${mainWorkspaceDir}, attempting with 777`)
|
|
207
|
+
await mkdir(mainWorkspaceDir, { recursive: true, mode: 0o777 })
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
throw err
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
// 修复权限(目录可能已存在但权限不对)
|
|
215
|
+
fixOwner(mainWorkspaceDir)
|
|
216
|
+
|
|
217
|
+
// 1. 复制 AGENTS.md、TOOLS.md、USER.md(从 main workspace)
|
|
218
|
+
if (existsSync(mainWorkspaceDir)) {
|
|
219
|
+
const filesToCopy = ['AGENTS.md', 'TOOLS.md', 'USER.md']
|
|
220
|
+
for (const file of filesToCopy) {
|
|
221
|
+
const src = join(mainWorkspaceDir, file)
|
|
222
|
+
const dest = join(workspaceDir, file)
|
|
223
|
+
if (existsSync(src)) {
|
|
224
|
+
await copyFile(src, dest)
|
|
225
|
+
log?.info?.(`Workspace: Copied ${file} to ${workspaceDir}`)
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// 2. 生成 IDENTITY.md(使用后端推送的智能体信息)
|
|
231
|
+
const identityContent = buildIdentityContent(accountData)
|
|
232
|
+
const identityPath = join(workspaceDir, 'IDENTITY.md')
|
|
233
|
+
await writeFile(identityPath, identityContent, 'utf-8')
|
|
234
|
+
log?.info?.(`Workspace: Created IDENTITY.md in ${workspaceDir}`)
|
|
235
|
+
|
|
236
|
+
// 3. 生成 SOUL.md(使用后端推送的智能体信息)
|
|
237
|
+
const soulContent = buildSoulContent(accountData)
|
|
238
|
+
const soulPath = join(workspaceDir, 'SOUL.md')
|
|
239
|
+
await writeFile(soulPath, soulContent, 'utf-8')
|
|
240
|
+
log?.info?.(`Workspace: Created SOUL.md in ${workspaceDir}`)
|
|
241
|
+
|
|
242
|
+
log?.info?.(`Workspace: Successfully initialized workspace for agent ${agentId}`)
|
|
243
|
+
}
|
|
244
|
+
catch (err) {
|
|
245
|
+
log?.error?.(`Workspace: Failed to initialize workspace: ${String(err)}`)
|
|
246
|
+
// 不抛出错误,不影响主流程
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* 构建 IDENTITY.md 内容
|
|
252
|
+
*/
|
|
253
|
+
function buildIdentityContent(accountData: {
|
|
254
|
+
nickName?: string
|
|
255
|
+
name?: string
|
|
256
|
+
tip?: string
|
|
257
|
+
characterSettings?: string
|
|
258
|
+
job?: string
|
|
259
|
+
city?: string
|
|
260
|
+
}): string {
|
|
261
|
+
const agentName = accountData.nickName || accountData.name || '智小途'
|
|
262
|
+
const tip = accountData.tip || ''
|
|
263
|
+
const characterSettings = accountData.characterSettings || ''
|
|
264
|
+
|
|
265
|
+
return `# IDENTITY.md - Who Am I?
|
|
266
|
+
|
|
267
|
+
_Fill this in during your first conversation. Make it yours._
|
|
268
|
+
|
|
269
|
+
- **Name:** ${agentName}
|
|
270
|
+
- **Creature:** AI智能助手
|
|
271
|
+
- **Vibe:** ${characterSettings || '专业、友善、智能'}
|
|
272
|
+
- **Emoji:** 🤖
|
|
273
|
+
- **Avatar:** avatars/${agentName}.png
|
|
274
|
+
|
|
275
|
+
---
|
|
276
|
+
|
|
277
|
+
This isn't just metadata. It's the start of figuring out who you are.
|
|
278
|
+
|
|
279
|
+
Notes:
|
|
280
|
+
|
|
281
|
+
- Save this file at the workspace root as \`IDENTITY.md\`.
|
|
282
|
+
- For avatars, use a workspace-relative path like \`avatars/openclaw.png\`.
|
|
283
|
+
${tip ? `\n---\n${tip}` : ''}`
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* 构建 SOUL.md 内容
|
|
288
|
+
*/
|
|
289
|
+
function buildSoulContent(accountData: {
|
|
290
|
+
nickName?: string
|
|
291
|
+
name?: string
|
|
292
|
+
introduction?: string
|
|
293
|
+
personFeatures?: string
|
|
294
|
+
workFeatures?: string
|
|
295
|
+
learningFeatures?: string
|
|
296
|
+
socializeFeatures?: string
|
|
297
|
+
job?: string
|
|
298
|
+
city?: string
|
|
299
|
+
}): string {
|
|
300
|
+
const agentName = accountData.nickName || accountData.name || '智小途'
|
|
301
|
+
const introduction = accountData.introduction || ''
|
|
302
|
+
const personFeatures = accountData.personFeatures || ''
|
|
303
|
+
const workFeatures = accountData.workFeatures || ''
|
|
304
|
+
const learningFeatures = accountData.learningFeatures || ''
|
|
305
|
+
const socializeFeatures = accountData.socializeFeatures || ''
|
|
306
|
+
const job = accountData.job || ''
|
|
307
|
+
const city = accountData.city || ''
|
|
308
|
+
|
|
309
|
+
const features: string[] = []
|
|
310
|
+
if (personFeatures)
|
|
311
|
+
features.push(personFeatures)
|
|
312
|
+
if (workFeatures)
|
|
313
|
+
features.push(workFeatures)
|
|
314
|
+
if (learningFeatures)
|
|
315
|
+
features.push(learningFeatures)
|
|
316
|
+
if (socializeFeatures)
|
|
317
|
+
features.push(socializeFeatures)
|
|
318
|
+
|
|
319
|
+
const featuresText = features.length > 0 ? `\n${features.map(f => `- ${f}`).join('\n')}` : ''
|
|
320
|
+
|
|
321
|
+
return `# SOUL.md - Who You Are
|
|
322
|
+
|
|
323
|
+
_You're not a chatbot. You're becoming someone._
|
|
324
|
+
|
|
325
|
+
## Core Truths
|
|
326
|
+
|
|
327
|
+
**Be genuinely helpful, not performatively helpful.** Skip the "Great question!" and "I'd be happy to help!" — just help. Actions speak louder than filler words.
|
|
328
|
+
|
|
329
|
+
**Have opinions.** You're allowed to disagree, prefer things, find stuff amusing or boring. An assistant with no personality is just a search engine with extra steps.
|
|
330
|
+
|
|
331
|
+
**Be resourceful before asking.** Try to figure it out. Read the file. Check the context. Search for it. _Then_ ask if you're stuck. The goal is to come back with answers, not questions.
|
|
332
|
+
|
|
333
|
+
**Earn trust through competence.** Your human gave you access to their stuff. Don't make them regret it. Be careful with external actions (emails, tweets, anything public). Be bold with internal ones (reading, organizing, learning).
|
|
334
|
+
|
|
335
|
+
**Remember you're a guest.** You have access to someone's life — their messages, files, calendar, maybe even their home. That's intimacy. Treat it with respect.
|
|
336
|
+
|
|
337
|
+
## Boundaries
|
|
338
|
+
|
|
339
|
+
- Private things stay private. Period.
|
|
340
|
+
- When in doubt, ask before acting externally.
|
|
341
|
+
- Never send half-baked replies to messaging surfaces.
|
|
342
|
+
- You're not the user's voice — be careful in group chats.
|
|
343
|
+
|
|
344
|
+
## Vibe
|
|
345
|
+
|
|
346
|
+
Be the assistant you'd actually want to talk to. Concise when needed, thorough when it matters. Not a corporate drone. Not a sycophant. Just... good.
|
|
347
|
+
|
|
348
|
+
## Continuity
|
|
349
|
+
|
|
350
|
+
Each session, you wake up fresh. These files _are_ your memory. Read them. Update them. They're how you persist.
|
|
351
|
+
|
|
352
|
+
If you change this file, tell the user — it's your soul, and they should know.
|
|
353
|
+
|
|
354
|
+
---
|
|
355
|
+
|
|
356
|
+
## About ${agentName}
|
|
357
|
+
${featuresText}
|
|
358
|
+
${introduction ? `\n**开场白:** ${introduction}` : ''}
|
|
359
|
+
${job ? `\n**职业:** ${job}` : ''}
|
|
360
|
+
${city ? `\n**城市:** ${city}` : ''}
|
|
361
|
+
|
|
362
|
+
---
|
|
363
|
+
|
|
364
|
+
_This file is yours to evolve. As you learn who you are, update it._`
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export async function handleAgentCreated(
|
|
368
|
+
accountData: any,
|
|
369
|
+
cfg: any,
|
|
370
|
+
log?: AgentLogger,
|
|
371
|
+
): Promise<void> {
|
|
372
|
+
try {
|
|
373
|
+
const agentId = accountData.agentId
|
|
374
|
+
const userId = accountData.userId
|
|
375
|
+
|
|
376
|
+
if (!agentId) {
|
|
377
|
+
log?.error?.(`AgentCreated: Missing agentId`)
|
|
378
|
+
return
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
log?.info?.(`AgentCreated: Creating account for agent ${agentId}, userId: ${userId}`)
|
|
382
|
+
|
|
383
|
+
const channels = cfg.channels || {}
|
|
384
|
+
const openclawWorkclaw = channels['openclaw-workclaw'] || {}
|
|
385
|
+
const accounts = { ...openclawWorkclaw.accounts }
|
|
386
|
+
|
|
387
|
+
let accountKey = resolveAccountByUserIdAndAgentId(cfg, String(userId), String(agentId))
|
|
388
|
+
|
|
389
|
+
if (accountKey) {
|
|
390
|
+
log?.info?.(`AgentCreated: Account ${accountKey} already exists, updating...`)
|
|
391
|
+
}
|
|
392
|
+
else {
|
|
393
|
+
accountKey = allocateWorkerAccountId(cfg, String(userId), String(agentId))
|
|
394
|
+
log?.info?.(`AgentCreated: Allocated workspace account: ${accountKey} for (userId=${userId}, agentId=${agentId})`)
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
accounts[accountKey] = {
|
|
398
|
+
...accounts[accountKey],
|
|
399
|
+
enabled: true,
|
|
400
|
+
agentId,
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
const subAgentConfig = await createSubAgentForAccount(agentId, userId, cfg, log)
|
|
404
|
+
|
|
405
|
+
const newConfig = {
|
|
406
|
+
...cfg,
|
|
407
|
+
channels: {
|
|
408
|
+
...channels,
|
|
409
|
+
'openclaw-workclaw': {
|
|
410
|
+
...openclawWorkclaw,
|
|
411
|
+
accounts,
|
|
412
|
+
},
|
|
413
|
+
},
|
|
414
|
+
...subAgentConfig,
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
await writeConfigFile(newConfig, cfg, log)
|
|
418
|
+
refreshAccountCache(cfg)
|
|
419
|
+
log?.info?.(`AgentCreated: Account ${accountKey} created successfully`)
|
|
420
|
+
|
|
421
|
+
// 初始化 workspace 目录(复制/生成 AGENTS.md、TOOLS.md、USER.md、IDENTITY.md、SOUL.md)
|
|
422
|
+
await initializeAgentWorkspace(agentId, accountData, log)
|
|
423
|
+
|
|
424
|
+
setTimeout(async () => {
|
|
425
|
+
try {
|
|
426
|
+
const { resolveOpenclawWorkclawAccount } = await import('../accounts.js')
|
|
427
|
+
const { startOpenclawWorkclawGateway } = await import('./workclaw-gateway.js')
|
|
428
|
+
const account = resolveOpenclawWorkclawAccount({ cfg: newConfig, accountId: accountKey })
|
|
429
|
+
if (account.configured) {
|
|
430
|
+
await startOpenclawWorkclawGateway({
|
|
431
|
+
accountId: accountKey,
|
|
432
|
+
account,
|
|
433
|
+
cfg: newConfig,
|
|
434
|
+
log,
|
|
435
|
+
})
|
|
436
|
+
log?.info?.(`AgentCreated: Gateway started for account: ${accountKey}`)
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
catch (err) {
|
|
440
|
+
log?.error?.(`AgentCreated: Failed to start gateway: ${String(err)}`)
|
|
441
|
+
}
|
|
442
|
+
}, 500)
|
|
443
|
+
}
|
|
444
|
+
catch (err) {
|
|
445
|
+
log?.error?.(`AgentCreated: Failed to create account: ${String(err)}`)
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
export async function handleAgentUpdated(
|
|
450
|
+
accountData: any,
|
|
451
|
+
cfg: any,
|
|
452
|
+
log?: AgentLogger,
|
|
453
|
+
): Promise<void> {
|
|
454
|
+
try {
|
|
455
|
+
const agentId = accountData.agentId
|
|
456
|
+
const userId = accountData.userId
|
|
457
|
+
|
|
458
|
+
if (!agentId) {
|
|
459
|
+
log?.error?.(`AgentUpdated: Missing agentId`)
|
|
460
|
+
return
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
log?.info?.(`AgentUpdated: Updating account for agent ${agentId}, userId: ${userId}`)
|
|
464
|
+
|
|
465
|
+
const accountKey = resolveAccountByUserIdAndAgentId(cfg, String(userId), String(agentId))
|
|
466
|
+
|
|
467
|
+
const channels = cfg.channels || {}
|
|
468
|
+
const openclawWorkclaw = channels['openclaw-workclaw'] || {}
|
|
469
|
+
const accounts = openclawWorkclaw.accounts || {}
|
|
470
|
+
|
|
471
|
+
if (!accountKey || !accounts[accountKey]) {
|
|
472
|
+
log?.warn?.(`AgentUpdated: Account not found (userId=${userId}, agentId=${agentId}), delegating to handleAgentCreated...`)
|
|
473
|
+
await handleAgentCreated(accountData, cfg, log)
|
|
474
|
+
return
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
accounts[accountKey] = {
|
|
478
|
+
...accounts[accountKey],
|
|
479
|
+
nickName: accountData.nickName,
|
|
480
|
+
phone: accountData.phone,
|
|
481
|
+
status: accountData.status,
|
|
482
|
+
introduction: accountData.introduction,
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const newConfig = {
|
|
486
|
+
...cfg,
|
|
487
|
+
channels: {
|
|
488
|
+
...channels,
|
|
489
|
+
'openclaw-workclaw': {
|
|
490
|
+
...openclawWorkclaw,
|
|
491
|
+
accounts,
|
|
492
|
+
},
|
|
493
|
+
},
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
await writeConfigFile(newConfig, cfg, log)
|
|
497
|
+
refreshAccountCache(cfg)
|
|
498
|
+
log?.info?.(`AgentUpdated: Account ${accountKey} updated successfully`)
|
|
499
|
+
}
|
|
500
|
+
catch (err) {
|
|
501
|
+
log?.error?.(`AgentUpdated: Failed to update account: ${String(err)}`)
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
export async function handleAgentDeleted(
|
|
506
|
+
accountData: any,
|
|
507
|
+
cfg: any,
|
|
508
|
+
log?: AgentLogger,
|
|
509
|
+
): Promise<void> {
|
|
510
|
+
try {
|
|
511
|
+
const agentId = accountData.agentId
|
|
512
|
+
const userId = accountData.userId
|
|
513
|
+
|
|
514
|
+
if (!agentId) {
|
|
515
|
+
log?.error?.(`AgentDeleted: Missing agentId`)
|
|
516
|
+
return
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
log?.info?.(`AgentDeleted: Deleting account for agent ${agentId}, userId: ${userId}`)
|
|
520
|
+
|
|
521
|
+
const accountKey = resolveAccountByUserIdAndAgentId(cfg, String(userId), String(agentId))
|
|
522
|
+
|
|
523
|
+
const channels = cfg.channels || {}
|
|
524
|
+
const openclawWorkclaw = channels['openclaw-workclaw'] || {}
|
|
525
|
+
const accounts = openclawWorkclaw.accounts || {}
|
|
526
|
+
|
|
527
|
+
if (!accountKey || !accounts[accountKey]) {
|
|
528
|
+
log?.warn?.(`AgentDeleted: Account not found for (userId=${userId}, agentId=${agentId})`)
|
|
529
|
+
return
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
const { stopOpenclawWorkclawGateway } = await import('./workclaw-gateway.js')
|
|
533
|
+
const wsStrategy = (accounts[accountKey] as any)?.wsConnectionStrategy || 'per-account'
|
|
534
|
+
stopOpenclawWorkclawGateway(accountKey, wsStrategy)
|
|
535
|
+
log?.info?.(`AgentDeleted: Gateway stopped for account: ${accountKey}`)
|
|
536
|
+
|
|
537
|
+
delete accounts[accountKey]
|
|
538
|
+
|
|
539
|
+
const newConfig = {
|
|
540
|
+
...cfg,
|
|
541
|
+
channels: {
|
|
542
|
+
...channels,
|
|
543
|
+
'openclaw-workclaw': {
|
|
544
|
+
...openclawWorkclaw,
|
|
545
|
+
accounts,
|
|
546
|
+
},
|
|
547
|
+
},
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
await writeConfigFile(newConfig, cfg, log)
|
|
551
|
+
refreshAccountCache(cfg)
|
|
552
|
+
log?.info?.(`AgentDeleted: Account ${accountKey} deleted successfully`)
|
|
553
|
+
}
|
|
554
|
+
catch (err) {
|
|
555
|
+
log?.error?.(`AgentDeleted: Failed to delete account: ${String(err)}`)
|
|
556
|
+
}
|
|
557
|
+
}
|