ddchat 0.4.1 → 0.4.2
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/CLAUDE.md +51 -0
- package/OPTIMIZATION.md +105 -0
- package/README.md +22 -0
- package/{index.js → index.ts} +7 -5
- package/openclaw.plugin.json +3 -137
- package/package.json +6 -6
- package/setup-entry.ts +4 -0
- package/src/channel.ts +101 -0
- package/src/{constants.js → constants.ts} +2 -1
- package/src/dedupe.ts +31 -0
- package/src/gateway.ts +237 -0
- package/src/inbound.ts +394 -0
- package/src/outbound.ts +183 -0
- package/src/pairing.ts +9 -0
- package/src/runtime.ts +27 -0
- package/src/session.ts +19 -0
- package/src/types.ts +126 -0
- package/task/BLOCKERS.md +3 -0
- package/task/DOING.md +3 -0
- package/task/DONE.md +8 -0
- package/task/README.md +17 -0
- package/task/TODO.md +10 -0
- package/test/README.md +48 -0
- package/test/chat.html +304 -0
- package/test/server.mjs +143 -0
- package/setup-entry.js +0 -8
- package/src/channel.js +0 -99
- package/src/dedupe.js +0 -44
- package/src/gateway.js +0 -211
- package/src/inbound.js +0 -363
- package/src/outbound.js +0 -150
- package/src/pairing.js +0 -8
- package/src/runtime.js +0 -20
- package/src/session.js +0 -13
- package/src/types.js +0 -73
package/src/outbound.js
DELETED
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { pathToFileURL } from "node:url";
|
|
3
|
-
import { loadWebMedia } from "openclaw/plugin-sdk/web-media";
|
|
4
|
-
import { DDCHAT_CHANNEL_ID } from "./constants.js";
|
|
5
|
-
import { getDdchatWsRuntime } from "./runtime.js";
|
|
6
|
-
import { resolveDdchatMediaMaxBytes } from "./types.js";
|
|
7
|
-
function createDdchatMessageId(prefix) {
|
|
8
|
-
return `${prefix}-${randomUUID()}`;
|
|
9
|
-
}
|
|
10
|
-
function isLocalFilePath(url) {
|
|
11
|
-
if (url.startsWith("file://"))
|
|
12
|
-
return true;
|
|
13
|
-
if (/^[a-zA-Z]:[\\/]/.test(url))
|
|
14
|
-
return true;
|
|
15
|
-
if (url.startsWith("/") && !url.startsWith("//"))
|
|
16
|
-
return true;
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
|
-
function toFileUrl(path) {
|
|
20
|
-
if (path.startsWith("file://"))
|
|
21
|
-
return path;
|
|
22
|
-
return pathToFileURL(path).href;
|
|
23
|
-
}
|
|
24
|
-
export async function resolveDdchatOutboundMediaFields(cfg, mediaUrl) {
|
|
25
|
-
try {
|
|
26
|
-
let resolvedUrl = mediaUrl;
|
|
27
|
-
if (isLocalFilePath(mediaUrl)) {
|
|
28
|
-
resolvedUrl = toFileUrl(mediaUrl);
|
|
29
|
-
}
|
|
30
|
-
console.log(`[ddchat] Resolved media URL: ${resolvedUrl}`);
|
|
31
|
-
const media = await loadWebMedia(resolvedUrl, {
|
|
32
|
-
maxBytes: resolveDdchatMediaMaxBytes(cfg),
|
|
33
|
-
});
|
|
34
|
-
console.log(`[ddchat] Media loaded successfully: ${media.fileName}, type: ${media.contentType}, size: ${media.buffer.length}`);
|
|
35
|
-
return {
|
|
36
|
-
mediaBase64: media.buffer.toString("base64"),
|
|
37
|
-
mediaType: media.contentType,
|
|
38
|
-
mediaName: media.fileName,
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
catch (error) {
|
|
42
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
43
|
-
console.error(`[ddchat] Failed to load media from ${mediaUrl}: ${errorMsg}`);
|
|
44
|
-
return { error: errorMsg };
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
function resolveTarget(to) {
|
|
48
|
-
const normalized = to.trim();
|
|
49
|
-
if (normalized.startsWith("group:")) {
|
|
50
|
-
return { targetType: "group", targetId: normalized.slice("group:".length) };
|
|
51
|
-
}
|
|
52
|
-
if (normalized.startsWith("chat:")) {
|
|
53
|
-
return { targetType: "group", targetId: normalized.slice("chat:".length) };
|
|
54
|
-
}
|
|
55
|
-
if (normalized.startsWith("user:")) {
|
|
56
|
-
return { targetType: "direct", targetId: normalized.slice("user:".length) };
|
|
57
|
-
}
|
|
58
|
-
return { targetType: "direct", targetId: normalized };
|
|
59
|
-
}
|
|
60
|
-
export const ddchatOutbound = {
|
|
61
|
-
deliveryMode: "direct",
|
|
62
|
-
textChunkLimit: 4000,
|
|
63
|
-
chunkerMode: "markdown",
|
|
64
|
-
sendText: async ({ to, text, accountId = "default" }) => {
|
|
65
|
-
const messageId = createDdchatMessageId("ddchat-text");
|
|
66
|
-
const target = resolveTarget(to);
|
|
67
|
-
const runtime = getDdchatWsRuntime(accountId);
|
|
68
|
-
if (!runtime.send || !runtime.connected) {
|
|
69
|
-
throw new Error("DDChat WebSocket not connected");
|
|
70
|
-
}
|
|
71
|
-
const payload = {
|
|
72
|
-
type: "outbound_message",
|
|
73
|
-
from: "claw",
|
|
74
|
-
channel: DDCHAT_CHANNEL_ID,
|
|
75
|
-
accountId,
|
|
76
|
-
messageId,
|
|
77
|
-
targetType: target.targetType,
|
|
78
|
-
targetId: target.targetId,
|
|
79
|
-
text,
|
|
80
|
-
};
|
|
81
|
-
console.log(`[ddchat] Sending text payload:`, JSON.stringify(payload, null, 2));
|
|
82
|
-
const sent = runtime.send(payload);
|
|
83
|
-
if (!sent) {
|
|
84
|
-
throw new Error("Failed to send text message via WebSocket");
|
|
85
|
-
}
|
|
86
|
-
return {
|
|
87
|
-
messageId,
|
|
88
|
-
to,
|
|
89
|
-
channel: DDCHAT_CHANNEL_ID,
|
|
90
|
-
text,
|
|
91
|
-
transport: "ws",
|
|
92
|
-
};
|
|
93
|
-
},
|
|
94
|
-
sendMedia: async ({ cfg, to, text, mediaUrl, accountId = "default" }) => {
|
|
95
|
-
const messageId = createDdchatMessageId("ddchat-media");
|
|
96
|
-
const target = resolveTarget(to);
|
|
97
|
-
const runtime = getDdchatWsRuntime(accountId);
|
|
98
|
-
console.log(`[ddchat] sendMedia called - wsConnected: ${runtime.connected}, wsSend available: ${!!runtime.send}`);
|
|
99
|
-
if (!runtime.send || !runtime.connected) {
|
|
100
|
-
throw new Error("DDChat WebSocket not connected");
|
|
101
|
-
}
|
|
102
|
-
console.log(`[ddchat] Loading media from: ${mediaUrl}`);
|
|
103
|
-
const { mediaBase64, mediaType, mediaName, error } = await resolveDdchatOutboundMediaFields(cfg, mediaUrl);
|
|
104
|
-
if (error) {
|
|
105
|
-
console.error(`[ddchat] Media loading failed: ${error}`);
|
|
106
|
-
throw new Error(`Failed to load media: ${error}`);
|
|
107
|
-
}
|
|
108
|
-
if (!mediaBase64) {
|
|
109
|
-
console.error(`[ddchat] No media data available for ${mediaUrl}`);
|
|
110
|
-
throw new Error("No media data available");
|
|
111
|
-
}
|
|
112
|
-
const payload = {
|
|
113
|
-
type: "outbound_message",
|
|
114
|
-
from: "claw",
|
|
115
|
-
channel: DDCHAT_CHANNEL_ID,
|
|
116
|
-
accountId,
|
|
117
|
-
messageId,
|
|
118
|
-
targetType: target.targetType,
|
|
119
|
-
targetId: target.targetId,
|
|
120
|
-
text,
|
|
121
|
-
mediaUrl,
|
|
122
|
-
mediaBase64,
|
|
123
|
-
mediaType,
|
|
124
|
-
mediaName,
|
|
125
|
-
};
|
|
126
|
-
console.log(`[ddchat] Sending media message:`, {
|
|
127
|
-
messageId,
|
|
128
|
-
targetType: target.targetType,
|
|
129
|
-
targetId: target.targetId,
|
|
130
|
-
mediaType,
|
|
131
|
-
mediaName,
|
|
132
|
-
mediaSize: mediaBase64?.length,
|
|
133
|
-
});
|
|
134
|
-
const sent = runtime.send(payload);
|
|
135
|
-
if (!sent) {
|
|
136
|
-
console.error(`[ddchat] Failed to send media message via WebSocket`);
|
|
137
|
-
throw new Error("Failed to send media message via WebSocket");
|
|
138
|
-
}
|
|
139
|
-
return {
|
|
140
|
-
messageId,
|
|
141
|
-
to,
|
|
142
|
-
channel: DDCHAT_CHANNEL_ID,
|
|
143
|
-
text,
|
|
144
|
-
mediaUrl,
|
|
145
|
-
mediaType,
|
|
146
|
-
mediaName,
|
|
147
|
-
transport: "ws",
|
|
148
|
-
};
|
|
149
|
-
},
|
|
150
|
-
};
|
package/src/pairing.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { createPairingPrefixStripper } from "openclaw/plugin-sdk/channel-pairing";
|
|
2
|
-
export const ddchatPairing = {
|
|
3
|
-
idLabel: "ddchatUserId",
|
|
4
|
-
normalizeAllowEntry: createPairingPrefixStripper(/^(ddchat|user):/i),
|
|
5
|
-
notifyApproval: async ({ runtime, id }) => {
|
|
6
|
-
runtime?.log?.(`[ddchat] pairing approved for ${id}`);
|
|
7
|
-
},
|
|
8
|
-
};
|
package/src/runtime.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { DdchatDedupeStore } from "./dedupe.js";
|
|
2
|
-
const state = {
|
|
3
|
-
dedupe: new DdchatDedupeStore(),
|
|
4
|
-
wsByAccount: new Map(),
|
|
5
|
-
};
|
|
6
|
-
export function getDdchatState() {
|
|
7
|
-
return state;
|
|
8
|
-
}
|
|
9
|
-
export function getDdchatWsRuntime(accountId) {
|
|
10
|
-
return state.wsByAccount.get(accountId) ?? { connected: false };
|
|
11
|
-
}
|
|
12
|
-
export function setDdchatWsRuntime(params) {
|
|
13
|
-
state.wsByAccount.set(params.accountId, {
|
|
14
|
-
send: params.send,
|
|
15
|
-
connected: params.connected,
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
export function clearDdchatWsRuntime(accountId) {
|
|
19
|
-
state.wsByAccount.delete(accountId);
|
|
20
|
-
}
|
package/src/session.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export function buildDdchatSessionKey(params) {
|
|
2
|
-
const base = params.peerKind === "group"
|
|
3
|
-
? `ddchat:group:${params.peerId}`
|
|
4
|
-
: `ddchat:direct:${params.peerId}`;
|
|
5
|
-
const thread = params.threadId?.trim();
|
|
6
|
-
if (!thread) {
|
|
7
|
-
return base;
|
|
8
|
-
}
|
|
9
|
-
if (params.peerKind === "group" && params.senderId) {
|
|
10
|
-
return `${base}:thread:${thread}:sender:${params.senderId}`;
|
|
11
|
-
}
|
|
12
|
-
return `${base}:thread:${thread}`;
|
|
13
|
-
}
|
package/src/types.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { DDCHAT_CHANNEL_ID, DDCHAT_DEFAULT_ACCOUNT_ID } from "./constants.js";
|
|
2
|
-
export function resolveDdchatMediaMaxBytes(cfg) {
|
|
3
|
-
const mb = cfg.agents?.defaults?.mediaMaxMb;
|
|
4
|
-
if (!mb || mb <= 0) {
|
|
5
|
-
return undefined;
|
|
6
|
-
}
|
|
7
|
-
return mb * 1024 * 1024;
|
|
8
|
-
}
|
|
9
|
-
function toStringList(input) {
|
|
10
|
-
if (!input) {
|
|
11
|
-
return [];
|
|
12
|
-
}
|
|
13
|
-
return input.map((entry) => String(entry).trim()).filter(Boolean);
|
|
14
|
-
}
|
|
15
|
-
function normalizeWebhookPath(path) {
|
|
16
|
-
const trimmed = path?.trim();
|
|
17
|
-
return trimmed?.startsWith("/") ? trimmed : "/ddchat/webhook";
|
|
18
|
-
}
|
|
19
|
-
function normalizeConnectionMode(mode) {
|
|
20
|
-
return mode === "webhook" ? "webhook" : "websocket";
|
|
21
|
-
}
|
|
22
|
-
function normalizeStreamingMode(mode) {
|
|
23
|
-
return mode === "token" ? "token" : "chunk";
|
|
24
|
-
}
|
|
25
|
-
function readChannelConfig(cfg) {
|
|
26
|
-
return (cfg.channels?.[DDCHAT_CHANNEL_ID] ?? {});
|
|
27
|
-
}
|
|
28
|
-
function mergeAccountConfig(params) {
|
|
29
|
-
const { root, account } = params;
|
|
30
|
-
return {
|
|
31
|
-
...root,
|
|
32
|
-
...account,
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
export function listDdchatAccountIds(cfg) {
|
|
36
|
-
const channelCfg = readChannelConfig(cfg);
|
|
37
|
-
const keys = Object.keys(channelCfg.accounts ?? {}).filter((key) => key.trim().length > 0);
|
|
38
|
-
return keys.length > 0 ? keys : [DDCHAT_DEFAULT_ACCOUNT_ID];
|
|
39
|
-
}
|
|
40
|
-
export function resolveDefaultDdchatAccountId(cfg) {
|
|
41
|
-
const channelCfg = readChannelConfig(cfg);
|
|
42
|
-
const ids = listDdchatAccountIds(cfg);
|
|
43
|
-
const configured = channelCfg.defaultAccount?.trim();
|
|
44
|
-
return configured && ids.includes(configured) ? configured : ids[0];
|
|
45
|
-
}
|
|
46
|
-
export function resolveDdchatAccount(cfg, accountId) {
|
|
47
|
-
const channelCfg = readChannelConfig(cfg);
|
|
48
|
-
const resolvedAccountId = accountId?.trim() || resolveDefaultDdchatAccountId(cfg);
|
|
49
|
-
const named = channelCfg.accounts?.[resolvedAccountId];
|
|
50
|
-
const merged = mergeAccountConfig({ root: channelCfg, account: named });
|
|
51
|
-
const token = typeof merged.token === "string" ? merged.token.trim() : "";
|
|
52
|
-
const wsUrl = typeof merged.wsUrl === "string" ? merged.wsUrl.trim() : "";
|
|
53
|
-
return {
|
|
54
|
-
accountId: resolvedAccountId,
|
|
55
|
-
enabled: merged.enabled !== false,
|
|
56
|
-
configured: Boolean(token),
|
|
57
|
-
token: token || undefined,
|
|
58
|
-
wsUrl: wsUrl || undefined,
|
|
59
|
-
webhookPath: normalizeWebhookPath(merged.webhookPath),
|
|
60
|
-
webhookPort: typeof merged.webhookPort === "number" && Number.isFinite(merged.webhookPort)
|
|
61
|
-
? merged.webhookPort
|
|
62
|
-
: 3010,
|
|
63
|
-
connectionMode: normalizeConnectionMode(merged.connectionMode),
|
|
64
|
-
dmPolicy: merged.dmPolicy ?? "pairing",
|
|
65
|
-
groupPolicy: merged.groupPolicy ?? "allowlist",
|
|
66
|
-
requireMention: merged.requireMention === true,
|
|
67
|
-
streaming: merged.streaming !== false,
|
|
68
|
-
streamingMode: normalizeStreamingMode(merged.streamingMode),
|
|
69
|
-
allowFrom: toStringList(merged.allowFrom),
|
|
70
|
-
groupAllowFrom: toStringList(merged.groupAllowFrom),
|
|
71
|
-
heartbeatSec: Math.max(15, Number(merged.heartbeatSec ?? 60) || 60),
|
|
72
|
-
};
|
|
73
|
-
}
|