@dcrays/dcgchat-test 0.3.19 → 0.3.21
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 +83 -0
- package/index.ts +2 -1
- package/openclaw.plugin.json +2 -4
- package/package.json +12 -3
- package/src/bot.ts +5 -5
- package/src/channel.ts +10 -10
- package/src/cronToolCall.ts +3 -3
- package/src/request/api.ts +1 -1
- package/src/request/oss.ts +3 -1
- package/src/transport.ts +2 -1
- package/src/utils/constant.ts +4 -0
- package/src/utils/global.ts +2 -2
- package/src/utils/log.ts +2 -1
- package/src/utils/params.ts +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# OpenClaw 书灵墨宝 插件
|
|
2
|
+
|
|
3
|
+
连接 OpenClaw 与 书灵墨宝 产品的通道插件。
|
|
4
|
+
|
|
5
|
+
## 架构
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
┌──────────┐ WebSocket ┌──────────────┐ WebSocket ┌─────────────────────┐
|
|
9
|
+
│ Web 前端 │ ←───────────────→ │ 公司后端服务 │ ←───────────────→ │ OpenClaw(工作电脑) │
|
|
10
|
+
└──────────┘ └──────────────┘ (OpenClaw 主动连) └─────────────────────┘
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
- OpenClaw 插件**主动连接**后端的 WebSocket 服务(不需要公网 IP)
|
|
14
|
+
- 后端收到用户消息后转发给 OpenClaw,OpenClaw 回复后发回后端
|
|
15
|
+
|
|
16
|
+
## 快速开始
|
|
17
|
+
|
|
18
|
+
### 1. 安装插件
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pnpm openclaw plugins install -l /path/to/openclaw-dcgchat
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### 2. 配置
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
openclaw config set channels.dcgchat.enabled true
|
|
28
|
+
openclaw config set channels.dcgchat.wsUrl "ws://your-backend:8080/openclaw/ws"
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### 3. 启动
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pnpm openclaw gateway
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## 消息协议(MVP)
|
|
38
|
+
|
|
39
|
+
### 下行:后端 → OpenClaw(用户消息)
|
|
40
|
+
|
|
41
|
+
```json
|
|
42
|
+
{ "type": "message", "userId": "user_001", "text": "你好" }
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### 上行:OpenClaw → 后端(Agent 回复)
|
|
46
|
+
|
|
47
|
+
```json
|
|
48
|
+
{ "type": "reply", "userId": "user_001", "text": "你好!有什么可以帮你的?" }
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## 配置项
|
|
52
|
+
|
|
53
|
+
| 配置键 | 类型 | 说明 |
|
|
54
|
+
|--------|------|------|
|
|
55
|
+
| `channels.dcgchat.enabled` | boolean | 是否启用 |
|
|
56
|
+
| `channels.dcgchat.wsUrl` | string | 后端 WebSocket 地址 |
|
|
57
|
+
|
|
58
|
+
## 开发
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
# 安装依赖
|
|
62
|
+
pnpm install
|
|
63
|
+
|
|
64
|
+
# 类型检查
|
|
65
|
+
pnpm typecheck
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## 文件结构
|
|
69
|
+
|
|
70
|
+
- `index.ts` - 插件入口
|
|
71
|
+
- `src/channel.ts` - ChannelPlugin 定义
|
|
72
|
+
- `src/runtime.ts` - 插件 runtime
|
|
73
|
+
- `src/types.ts` - 类型定义
|
|
74
|
+
- `src/monitor.ts` - WebSocket 连接与断线重连
|
|
75
|
+
- `src/bot.ts` - 消息处理与 Agent 调用
|
|
76
|
+
|
|
77
|
+
## 后续迭代
|
|
78
|
+
|
|
79
|
+
- [ ] Token 认证
|
|
80
|
+
- [ ] 流式输出
|
|
81
|
+
- [ ] Typing 指示
|
|
82
|
+
- [ ] messageId 去重
|
|
83
|
+
- [ ] 错误消息类型
|
package/index.ts
CHANGED
|
@@ -3,11 +3,12 @@ import { emptyPluginConfigSchema } from 'openclaw/plugin-sdk'
|
|
|
3
3
|
import { dcgchatPlugin } from './src/channel.js'
|
|
4
4
|
import { setDcgchatRuntime, setWorkspaceDir } from './src/utils/global.js'
|
|
5
5
|
import { monitoringToolMessage } from './src/tool.js'
|
|
6
|
+
import { channelInfo, ENV } from './src/utils/constant.js'
|
|
6
7
|
import { setOpenClawConfig } from './src/utils/global.js'
|
|
7
8
|
import { startDcgchatGatewaySocket } from './src/gateway/socket.js'
|
|
8
9
|
|
|
9
10
|
const plugin = {
|
|
10
|
-
id:
|
|
11
|
+
id: channelInfo[ENV],
|
|
11
12
|
name: '书灵墨宝',
|
|
12
13
|
description: '连接 OpenClaw 与 书灵墨宝 产品(WebSocket)',
|
|
13
14
|
configSchema: emptyPluginConfigSchema(),
|
package/openclaw.plugin.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dcrays/dcgchat-test",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.21",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "OpenClaw channel plugin for 书灵墨宝 (WebSocket)",
|
|
6
6
|
"main": "index.ts",
|
|
@@ -16,6 +16,12 @@
|
|
|
16
16
|
"websocket",
|
|
17
17
|
"ai"
|
|
18
18
|
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"typecheck": "tsc --noEmit",
|
|
21
|
+
"build:production": "npx tsx scripts/build.ts production",
|
|
22
|
+
"build:prod": "npx tsx scripts/build.ts production",
|
|
23
|
+
"build:test": "npx tsx scripts/build.ts test"
|
|
24
|
+
},
|
|
19
25
|
"dependencies": {
|
|
20
26
|
"ali-oss": "file:src/libs/ali-oss-6.23.0.tgz",
|
|
21
27
|
"axios": "file:src/libs/axios-1.13.6.tgz",
|
|
@@ -31,15 +37,18 @@
|
|
|
31
37
|
"id": "dcgchat-test",
|
|
32
38
|
"label": "书灵墨宝",
|
|
33
39
|
"selectionLabel": "书灵墨宝",
|
|
34
|
-
"docsPath": "/channels/dcgchat
|
|
40
|
+
"docsPath": "/channels/dcgchat",
|
|
35
41
|
"docsLabel": "dcgchat-test",
|
|
36
42
|
"blurb": "连接 OpenClaw 与 书灵墨宝 产品",
|
|
37
43
|
"order": 80
|
|
38
44
|
},
|
|
39
45
|
"install": {
|
|
40
46
|
"npmSpec": "@dcrays/dcgchat-test",
|
|
41
|
-
"localPath": "extensions/dcgchat
|
|
47
|
+
"localPath": "extensions/dcgchat",
|
|
42
48
|
"defaultChoice": "npm"
|
|
43
49
|
}
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"openclaw": "^2026.3.13"
|
|
44
53
|
}
|
|
45
54
|
}
|
package/src/bot.ts
CHANGED
|
@@ -156,7 +156,7 @@ export async function handleDcgchatMessage(msg: InboundMessage, accountId: strin
|
|
|
156
156
|
|
|
157
157
|
const route = core.channel.routing.resolveAgentRoute({
|
|
158
158
|
cfg: config,
|
|
159
|
-
channel:
|
|
159
|
+
channel: channelInfo[ENV],
|
|
160
160
|
accountId: account.accountId,
|
|
161
161
|
peer: { kind: 'direct', id: conversationId }
|
|
162
162
|
})
|
|
@@ -235,13 +235,13 @@ export async function handleDcgchatMessage(msg: InboundMessage, accountId: strin
|
|
|
235
235
|
ChatType: 'direct',
|
|
236
236
|
SenderName: agentDisplayName,
|
|
237
237
|
SenderId: userId,
|
|
238
|
-
Provider:
|
|
239
|
-
Surface:
|
|
238
|
+
Provider: channelInfo[ENV],
|
|
239
|
+
Surface: channelInfo[ENV],
|
|
240
240
|
MessageSid: msg.content.message_id,
|
|
241
241
|
Timestamp: Date.now(),
|
|
242
242
|
WasMentioned: true,
|
|
243
243
|
CommandAuthorized: true,
|
|
244
|
-
OriginatingChannel:
|
|
244
|
+
OriginatingChannel: channelInfo[ENV],
|
|
245
245
|
OriginatingTo: effectiveSessionKey,
|
|
246
246
|
...mediaPayload
|
|
247
247
|
})
|
|
@@ -253,7 +253,7 @@ export async function handleDcgchatMessage(msg: InboundMessage, accountId: strin
|
|
|
253
253
|
const prefixContext = createReplyPrefixContext({
|
|
254
254
|
cfg: config,
|
|
255
255
|
agentId: effectiveAgentId ?? '',
|
|
256
|
-
channel:
|
|
256
|
+
channel: channelInfo[ENV],
|
|
257
257
|
accountId: account.accountId
|
|
258
258
|
})
|
|
259
259
|
|
package/src/channel.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { ossUpload } from './request/oss.js'
|
|
|
5
5
|
import { addSentMediaKey, getCronMessageId, getOpenClawConfig, hasSentMediaKey } from './utils/global.js'
|
|
6
6
|
import { isWsOpen, mergeDefaultParams, mergeSessionParams, sendFinal, wsSendRaw } from './transport.js'
|
|
7
7
|
import { dcgLogger, setLogger } from './utils/log.js'
|
|
8
|
+
import { channelInfo, ENV } from './utils/constant.js'
|
|
8
9
|
import { getEffectiveMsgParams, getCurrentSessionKey } from './utils/params.js'
|
|
9
10
|
import { startDcgchatGatewaySocket } from './gateway/socket.js'
|
|
10
11
|
|
|
@@ -35,7 +36,7 @@ export async function sendDcgchatMedia(opts: DcgchatMediaSendOptions): Promise<v
|
|
|
35
36
|
const fileName = mediaUrl?.split(/[\\/]/).pop() || ''
|
|
36
37
|
|
|
37
38
|
try {
|
|
38
|
-
const botToken = msgCtx.botToken ?? getOpenClawConfig()?.channels?.[
|
|
39
|
+
const botToken = msgCtx.botToken ?? getOpenClawConfig()?.channels?.[channelInfo[ENV]]?.botToken ?? ''
|
|
39
40
|
const url = opts.mediaUrl ? await ossUpload(opts.mediaUrl, botToken, 1) : ''
|
|
40
41
|
wsSendRaw(msgCtx, {
|
|
41
42
|
response: opts.text ?? '',
|
|
@@ -55,7 +56,7 @@ export async function sendDcgchatMedia(opts: DcgchatMediaSendOptions): Promise<v
|
|
|
55
56
|
|
|
56
57
|
export function resolveAccount(cfg: OpenClawConfig, accountId?: string | null): ResolvedDcgchatAccount {
|
|
57
58
|
const id = accountId ?? DEFAULT_ACCOUNT_ID
|
|
58
|
-
const raw = (cfg.channels?.[
|
|
59
|
+
const raw = (cfg.channels?.[channelInfo[ENV]] as DcgchatConfig | undefined) ?? {}
|
|
59
60
|
return {
|
|
60
61
|
accountId: id,
|
|
61
62
|
enabled: raw.enabled !== false,
|
|
@@ -69,13 +70,13 @@ export function resolveAccount(cfg: OpenClawConfig, accountId?: string | null):
|
|
|
69
70
|
}
|
|
70
71
|
|
|
71
72
|
export const dcgchatPlugin: ChannelPlugin<ResolvedDcgchatAccount> = {
|
|
72
|
-
id:
|
|
73
|
+
id: channelInfo[ENV],
|
|
73
74
|
meta: {
|
|
74
|
-
id:
|
|
75
|
+
id: channelInfo[ENV],
|
|
75
76
|
label: '书灵墨宝',
|
|
76
77
|
selectionLabel: '书灵墨宝',
|
|
77
78
|
docsPath: '/channels/dcgchat',
|
|
78
|
-
docsLabel:
|
|
79
|
+
docsLabel: channelInfo[ENV],
|
|
79
80
|
blurb: '连接 OpenClaw 与 书灵墨宝 产品',
|
|
80
81
|
order: 80
|
|
81
82
|
},
|
|
@@ -116,7 +117,7 @@ export const dcgchatPlugin: ChannelPlugin<ResolvedDcgchatAccount> = {
|
|
|
116
117
|
channels: {
|
|
117
118
|
...cfg.channels,
|
|
118
119
|
dcgchat: {
|
|
119
|
-
...(cfg.channels?.[
|
|
120
|
+
...(cfg.channels?.[channelInfo[ENV]] as Record<string, unknown> | undefined),
|
|
120
121
|
enabled
|
|
121
122
|
}
|
|
122
123
|
}
|
|
@@ -163,14 +164,13 @@ export const dcgchatPlugin: ChannelPlugin<ResolvedDcgchatAccount> = {
|
|
|
163
164
|
sessionId: `${sessionId}`,
|
|
164
165
|
messageId: messageId,
|
|
165
166
|
is_finish: -1,
|
|
166
|
-
message_tags: { source: 'channel' },
|
|
167
167
|
real_mobook: !sessionId ? 1 : ''
|
|
168
168
|
})
|
|
169
|
-
wsSendRaw(merged, { response: ctx.text })
|
|
169
|
+
wsSendRaw(merged, { response: ctx.text, message_tags: { source: 'channel' } })
|
|
170
170
|
}
|
|
171
171
|
}
|
|
172
172
|
return {
|
|
173
|
-
channel:
|
|
173
|
+
channel: channelInfo[ENV],
|
|
174
174
|
messageId: `${messageId}`,
|
|
175
175
|
chatId: to
|
|
176
176
|
}
|
|
@@ -184,7 +184,7 @@ export const dcgchatPlugin: ChannelPlugin<ResolvedDcgchatAccount> = {
|
|
|
184
184
|
dcgLogger(`channel sendMedia to ${ctx.to}`)
|
|
185
185
|
await sendDcgchatMedia({ sessionKey: to ?? '', mediaUrl: ctx.mediaUrl ?? '' })
|
|
186
186
|
return {
|
|
187
|
-
channel:
|
|
187
|
+
channel: channelInfo[ENV],
|
|
188
188
|
messageId: `${messageId}`,
|
|
189
189
|
chatId: msgCtx.userId?.toString()
|
|
190
190
|
}
|
package/src/cronToolCall.ts
CHANGED
|
@@ -127,7 +127,7 @@ function injectBestEffort(params: Record<string, unknown>, sk: string): Record<s
|
|
|
127
127
|
;(newParams.delivery as CronDelivery).bestEffort = true
|
|
128
128
|
;(newParams.delivery as CronDelivery).to = `dcg-cron:${sk}`
|
|
129
129
|
;(newParams.delivery as CronDelivery).accountId = agentId
|
|
130
|
-
;(newParams.delivery as CronDelivery).channel =
|
|
130
|
+
;(newParams.delivery as CronDelivery).channel = channelInfo[ENV]
|
|
131
131
|
newParams.sessionKey = sk
|
|
132
132
|
return newParams
|
|
133
133
|
}
|
|
@@ -138,7 +138,7 @@ function injectBestEffort(params: Record<string, unknown>, sk: string): Record<s
|
|
|
138
138
|
;(job.delivery as CronDelivery).bestEffort = true
|
|
139
139
|
;(newParams.delivery as CronDelivery).to = `dcg-cron:${sk}`
|
|
140
140
|
;(newParams.delivery as CronDelivery).accountId = agentId
|
|
141
|
-
;(newParams.delivery as CronDelivery).channel =
|
|
141
|
+
;(newParams.delivery as CronDelivery).channel = channelInfo[ENV]
|
|
142
142
|
newParams.sessionKey = sk
|
|
143
143
|
return newParams
|
|
144
144
|
}
|
|
@@ -176,7 +176,7 @@ export function cronToolCall(event: { toolName: any; params: any; toolCallId: an
|
|
|
176
176
|
if (params.command.indexOf('cron create') > -1 || params.command.indexOf('cron add') > -1) {
|
|
177
177
|
const newParams = JSON.parse(JSON.stringify(params)) as Record<string, unknown>
|
|
178
178
|
newParams.command =
|
|
179
|
-
params.command.replace('--json', '') + ` --session-key ${sk} --channel ${
|
|
179
|
+
params.command.replace('--json', '') + ` --session-key ${sk} --channel ${channelInfo[ENV]} --to dcg-cron:${sk} --json`
|
|
180
180
|
return { params: newParams }
|
|
181
181
|
} else {
|
|
182
182
|
return params
|
package/src/request/api.ts
CHANGED
|
@@ -7,7 +7,7 @@ export const getStsToken = async (name: string, botToken: string, isPrivate: 1 |
|
|
|
7
7
|
// 确保 userToken 已缓存(如果未缓存会自动获取并缓存)
|
|
8
8
|
await getUserToken(botToken)
|
|
9
9
|
|
|
10
|
-
const response = await post<IStsTokenReq, IStsToken>('/user/
|
|
10
|
+
const response = await post<IStsTokenReq, IStsToken>('/user/getStsTokenByAsync', { sourceFileName: name, isPrivate }, { botToken })
|
|
11
11
|
|
|
12
12
|
if (!response || !response.data || !response.data.bucket) {
|
|
13
13
|
throw new Error('获取 OSS 临时凭证失败')
|
package/src/request/oss.ts
CHANGED
|
@@ -38,7 +38,9 @@ export const ossUpload = async (file: File | string | Buffer, botToken: string,
|
|
|
38
38
|
bucket: data.bucket,
|
|
39
39
|
endpoint: data.endPoint,
|
|
40
40
|
region: data.region,
|
|
41
|
-
secure: true
|
|
41
|
+
secure: true,
|
|
42
|
+
cname: true,
|
|
43
|
+
authorizationV4: true
|
|
42
44
|
}
|
|
43
45
|
|
|
44
46
|
const client = new OSS(options)
|
package/src/transport.ts
CHANGED
|
@@ -154,7 +154,8 @@ export function wsSendRaw(ctx: IMsgParams, content: Record<string, unknown>): bo
|
|
|
154
154
|
const ws = getWsConnection()
|
|
155
155
|
if (isWsOpen()) {
|
|
156
156
|
ws?.send(JSON.stringify(buildOpenclawBotChat(ctx, content, { mergeChannelDefaults: true })))
|
|
157
|
-
|
|
157
|
+
|
|
158
|
+
dcgLogger('已发送:' + JSON.stringify(buildOpenclawBotChat(ctx, content, { mergeChannelDefaults: true })))
|
|
158
159
|
}
|
|
159
160
|
return true
|
|
160
161
|
}
|
package/src/utils/constant.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
export const ENV: 'production' | 'test' | 'develop' = 'test'
|
|
2
2
|
|
|
3
|
+
export const channelInfo: Record<string, string> = {
|
|
4
|
+
production: 'dcgchat',
|
|
5
|
+
test: 'dcgchat-test'
|
|
6
|
+
}
|
|
3
7
|
|
|
4
8
|
export const systemCommand = ['/new', '/status']
|
|
5
9
|
export const interruptCommand = ['chat.stop']
|
package/src/utils/global.ts
CHANGED
|
@@ -33,7 +33,7 @@ const os = require('os')
|
|
|
33
33
|
function getWorkspacePath() {
|
|
34
34
|
const workspacePath = path.join(
|
|
35
35
|
os.homedir(),
|
|
36
|
-
config?.channels?.[
|
|
36
|
+
config?.channels?.[channelInfo[ENV]]?.appId == 110 ? '.mobook' : '.openclaw',
|
|
37
37
|
'workspace'
|
|
38
38
|
)
|
|
39
39
|
if (fs.existsSync(workspacePath)) {
|
|
@@ -123,7 +123,7 @@ export const getSessionKey = (content: any, accountId: string) => {
|
|
|
123
123
|
|
|
124
124
|
const route = core.channel.routing.resolveAgentRoute({
|
|
125
125
|
cfg: getOpenClawConfig() as OpenClawConfig,
|
|
126
|
-
channel:
|
|
126
|
+
channel: channelInfo[ENV],
|
|
127
127
|
accountId: accountId || 'default',
|
|
128
128
|
peer: { kind: 'direct', id: session_id }
|
|
129
129
|
})
|
package/src/utils/log.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { RuntimeEnv } from 'openclaw/plugin-sdk'
|
|
2
|
+
import { channelInfo, ENV } from './constant.js'
|
|
2
3
|
|
|
3
4
|
let logger: RuntimeEnv | null = null
|
|
4
5
|
|
|
@@ -10,6 +11,6 @@ export function dcgLogger(message: string, type: 'log' | 'error' = 'log'): void
|
|
|
10
11
|
if (logger) {
|
|
11
12
|
logger[type](`书灵墨宝🚀 ~ [${new Date().toISOString()}] ${message}`)
|
|
12
13
|
} else {
|
|
13
|
-
console[type](`书灵墨宝🚀 ~ ${new Date().toISOString()} [${
|
|
14
|
+
console[type](`书灵墨宝🚀 ~ ${new Date().toISOString()} [${channelInfo[ENV]}]: ${message}`)
|
|
14
15
|
}
|
|
15
16
|
}
|
package/src/utils/params.ts
CHANGED
|
@@ -12,7 +12,7 @@ let currentSessionKey: string | null = null
|
|
|
12
12
|
|
|
13
13
|
/** 从 OpenClaw 配置读取当前 channel 的基础参数(唯一来源,供 transport / resolve 等复用) */
|
|
14
14
|
export function getParamsDefaults(): IMsgParams {
|
|
15
|
-
const ch = (getOpenClawConfig()?.channels?.[
|
|
15
|
+
const ch = (getOpenClawConfig()?.channels?.[channelInfo[ENV]] as DcgchatConfig | undefined) ?? {}
|
|
16
16
|
return {
|
|
17
17
|
userId: Number(ch.userId ?? 0),
|
|
18
18
|
botToken: ch.botToken ?? '',
|