ddchat 0.1.0 → 0.2.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/CLAUDE.md +51 -0
- package/OPTIMIZATION.md +105 -0
- package/README.md +14 -14
- package/index.ts +13 -13
- package/openclaw.plugin.json +15 -15
- package/package.json +36 -36
- package/setup-entry.ts +4 -4
- package/src/channel.ts +101 -101
- package/src/constants.ts +5 -5
- package/src/dedupe.ts +31 -31
- package/src/gateway.ts +237 -237
- package/src/inbound.ts +394 -379
- package/src/outbound.ts +183 -95
- package/src/pairing.ts +9 -9
- package/src/runtime.ts +27 -24
- package/src/session.ts +19 -19
- package/src/types.ts +126 -126
- package/task/BLOCKERS.md +3 -3
- package/task/DOING.md +3 -3
- package/task/DONE.md +8 -8
- package/task/README.md +17 -17
- package/task/TODO.md +10 -10
- package/test/README.md +48 -48
- package/test/chat.html +304 -304
- package/test/server.mjs +143 -143
package/src/outbound.ts
CHANGED
|
@@ -1,95 +1,183 @@
|
|
|
1
|
-
import { loadWebMedia } from "openclaw/plugin-sdk/media
|
|
2
|
-
import type { OpenClawConfig } from "openclaw/plugin-sdk/core";
|
|
3
|
-
import { DDCHAT_CHANNEL_ID } from "./constants.js";
|
|
4
|
-
import { getDdchatState } from "./runtime.js";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if (
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
return {
|
|
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
|
-
|
|
1
|
+
import { loadWebMedia } from "openclaw/plugin-sdk/web-media";
|
|
2
|
+
import type { OpenClawConfig } from "openclaw/plugin-sdk/core";
|
|
3
|
+
import { DDCHAT_CHANNEL_ID } from "./constants.js";
|
|
4
|
+
import { getDdchatState } from "./runtime.js";
|
|
5
|
+
import { pathToFileURL } from "url";
|
|
6
|
+
|
|
7
|
+
function resolveMediaMaxBytes(cfg: OpenClawConfig): number | undefined {
|
|
8
|
+
const mb = cfg.agents?.defaults?.mediaMaxMb;
|
|
9
|
+
if (!mb || mb <= 0) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
return mb * 1024 * 1024;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Check if URL is a local file path */
|
|
16
|
+
function isLocalFilePath(url: string): boolean {
|
|
17
|
+
if (url.startsWith("file://")) return true;
|
|
18
|
+
if (/^[a-zA-Z]:[\\/]/.test(url)) return true; // Windows path
|
|
19
|
+
if (url.startsWith("/") && !url.startsWith("//")) return true; // Unix absolute path
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Convert local path to file:// URL */
|
|
24
|
+
function toFileUrl(path: string): string {
|
|
25
|
+
if (path.startsWith("file://")) return path;
|
|
26
|
+
return pathToFileURL(path).href;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Load media for WS outbound; same limits as sendMedia. URL-only fallback on failure. */
|
|
30
|
+
export async function resolveDdchatOutboundMediaFields(
|
|
31
|
+
cfg: OpenClawConfig,
|
|
32
|
+
mediaUrl: string,
|
|
33
|
+
): Promise<{ mediaBase64?: string; mediaType?: string; mediaName?: string; error?: string }> {
|
|
34
|
+
try {
|
|
35
|
+
// Handle local file paths
|
|
36
|
+
let resolvedUrl = mediaUrl;
|
|
37
|
+
if (isLocalFilePath(mediaUrl)) {
|
|
38
|
+
resolvedUrl = toFileUrl(mediaUrl);
|
|
39
|
+
}
|
|
40
|
+
console.log(`[ddchat] Resolved media URL: ${resolvedUrl}`);
|
|
41
|
+
|
|
42
|
+
const media = await loadWebMedia(resolvedUrl, {
|
|
43
|
+
maxBytes: resolveMediaMaxBytes(cfg),
|
|
44
|
+
});
|
|
45
|
+
console.log(`[ddchat] Media loaded successfully: ${media.fileName}, type: ${media.contentType}, size: ${media.buffer.length}`);
|
|
46
|
+
return {
|
|
47
|
+
mediaBase64: media.buffer.toString("base64"),
|
|
48
|
+
mediaType: media.contentType,
|
|
49
|
+
mediaName: media.fileName,
|
|
50
|
+
};
|
|
51
|
+
} catch (error) {
|
|
52
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
53
|
+
console.error(`[ddchat] Failed to load media from ${mediaUrl}: ${errorMsg}`);
|
|
54
|
+
return { error: errorMsg };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function resolveTarget(to: string): { targetType: "group" | "direct"; targetId: string } {
|
|
59
|
+
const normalized = to.trim();
|
|
60
|
+
if (normalized.startsWith("group:")) {
|
|
61
|
+
return { targetType: "group", targetId: normalized.slice("group:".length) };
|
|
62
|
+
}
|
|
63
|
+
if (normalized.startsWith("chat:")) {
|
|
64
|
+
return { targetType: "group", targetId: normalized.slice("chat:".length) };
|
|
65
|
+
}
|
|
66
|
+
if (normalized.startsWith("user:")) {
|
|
67
|
+
return { targetType: "direct", targetId: normalized.slice("user:".length) };
|
|
68
|
+
}
|
|
69
|
+
return { targetType: "direct", targetId: normalized };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export const ddchatOutbound = {
|
|
73
|
+
deliveryMode: "direct",
|
|
74
|
+
textChunkLimit: 4000,
|
|
75
|
+
chunkerMode: "markdown",
|
|
76
|
+
sendText: async ({ to, text, accountId }) => {
|
|
77
|
+
const messageId = `ddchat-text-${Date.now()}`;
|
|
78
|
+
const target = resolveTarget(to);
|
|
79
|
+
|
|
80
|
+
// Check WebSocket connection
|
|
81
|
+
const state = getDdchatState();
|
|
82
|
+
console.log(`[ddchat] sendText called - wsConnected: ${state.wsConnected}, wsSend available: ${!!state.wsSend}`);
|
|
83
|
+
|
|
84
|
+
if (!state.wsSend || !state.wsConnected) {
|
|
85
|
+
console.error(`[ddchat] WebSocket not connected, cannot send text message`);
|
|
86
|
+
throw new Error("DDChat WebSocket not connected");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const payload = {
|
|
90
|
+
type: "outbound_message",
|
|
91
|
+
from: "plugin",
|
|
92
|
+
channel: DDCHAT_CHANNEL_ID,
|
|
93
|
+
accountId,
|
|
94
|
+
messageId,
|
|
95
|
+
targetType: target.targetType,
|
|
96
|
+
targetId: target.targetId,
|
|
97
|
+
text,
|
|
98
|
+
};
|
|
99
|
+
console.log(`[ddchat] Sending text payload:`, JSON.stringify(payload, null, 2));
|
|
100
|
+
const sent = state.wsSend(payload);
|
|
101
|
+
if (!sent) {
|
|
102
|
+
throw new Error("Failed to send text message via WebSocket");
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
messageId,
|
|
106
|
+
to,
|
|
107
|
+
channel: DDCHAT_CHANNEL_ID,
|
|
108
|
+
text,
|
|
109
|
+
transport: "ws",
|
|
110
|
+
};
|
|
111
|
+
},
|
|
112
|
+
sendMedia: async ({ cfg, to, text, mediaUrl, accountId }) => {
|
|
113
|
+
const messageId = `ddchat-media-${Date.now()}`;
|
|
114
|
+
const target = resolveTarget(to);
|
|
115
|
+
|
|
116
|
+
// Check WebSocket connection
|
|
117
|
+
const state = getDdchatState();
|
|
118
|
+
console.log(`[ddchat] sendMedia called - wsConnected: ${state.wsConnected}, wsSend available: ${!!state.wsSend}`);
|
|
119
|
+
|
|
120
|
+
if (!state.wsSend || !state.wsConnected) {
|
|
121
|
+
console.error(`[ddchat] WebSocket not connected, cannot send media`);
|
|
122
|
+
throw new Error("DDChat WebSocket not connected");
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Load media
|
|
126
|
+
console.log(`[ddchat] Loading media from: ${mediaUrl}`);
|
|
127
|
+
const { mediaBase64, mediaType, mediaName, error } = await resolveDdchatOutboundMediaFields(
|
|
128
|
+
cfg,
|
|
129
|
+
mediaUrl,
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
if (error) {
|
|
133
|
+
console.error(`[ddchat] Media loading failed: ${error}`);
|
|
134
|
+
throw new Error(`Failed to load media: ${error}`);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Validate media data
|
|
138
|
+
if (!mediaBase64) {
|
|
139
|
+
console.error(`[ddchat] No media data available for ${mediaUrl}`);
|
|
140
|
+
throw new Error("No media data available");
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const payload = {
|
|
144
|
+
type: "outbound_message",
|
|
145
|
+
from: "plugin",
|
|
146
|
+
channel: DDCHAT_CHANNEL_ID,
|
|
147
|
+
accountId,
|
|
148
|
+
messageId,
|
|
149
|
+
targetType: target.targetType,
|
|
150
|
+
targetId: target.targetId,
|
|
151
|
+
text,
|
|
152
|
+
mediaUrl,
|
|
153
|
+
mediaBase64,
|
|
154
|
+
mediaType,
|
|
155
|
+
mediaName,
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
console.log(`[ddchat] Sending media message:`, {
|
|
159
|
+
messageId,
|
|
160
|
+
targetType: target.targetType,
|
|
161
|
+
targetId: target.targetId,
|
|
162
|
+
mediaType,
|
|
163
|
+
mediaName,
|
|
164
|
+
mediaSize: mediaBase64?.length,
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
const sent = state.wsSend(payload);
|
|
168
|
+
if (!sent) {
|
|
169
|
+
throw new Error("Failed to send media message via WebSocket");
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return {
|
|
173
|
+
messageId,
|
|
174
|
+
to,
|
|
175
|
+
channel: DDCHAT_CHANNEL_ID,
|
|
176
|
+
text,
|
|
177
|
+
mediaUrl,
|
|
178
|
+
mediaType,
|
|
179
|
+
mediaName,
|
|
180
|
+
transport: "ws",
|
|
181
|
+
};
|
|
182
|
+
},
|
|
183
|
+
};
|
package/src/pairing.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { createPairingPrefixStripper } from "openclaw/plugin-sdk/channel-pairing";
|
|
2
|
-
|
|
3
|
-
export const ddchatPairing = {
|
|
4
|
-
idLabel: "ddchatUserId",
|
|
5
|
-
normalizeAllowEntry: createPairingPrefixStripper(/^(ddchat|user):/i),
|
|
6
|
-
notifyApproval: async ({ runtime, id }) => {
|
|
7
|
-
runtime?.log?.(`[ddchat] pairing approved for ${id}`);
|
|
8
|
-
},
|
|
9
|
-
};
|
|
1
|
+
import { createPairingPrefixStripper } from "openclaw/plugin-sdk/channel-pairing";
|
|
2
|
+
|
|
3
|
+
export const ddchatPairing = {
|
|
4
|
+
idLabel: "ddchatUserId",
|
|
5
|
+
normalizeAllowEntry: createPairingPrefixStripper(/^(ddchat|user):/i),
|
|
6
|
+
notifyApproval: async ({ runtime, id }) => {
|
|
7
|
+
runtime?.log?.(`[ddchat] pairing approved for ${id}`);
|
|
8
|
+
},
|
|
9
|
+
};
|
package/src/runtime.ts
CHANGED
|
@@ -1,24 +1,27 @@
|
|
|
1
|
-
import { DdchatDedupeStore } from "./dedupe.js";
|
|
2
|
-
|
|
3
|
-
export type DdchatPluginRuntimeState = {
|
|
4
|
-
dedupe: DdchatDedupeStore;
|
|
5
|
-
wsSend?: (payload: Record<string, unknown>) => boolean;
|
|
6
|
-
wsConnected: boolean;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
const state: DdchatPluginRuntimeState = {
|
|
10
|
-
dedupe: new DdchatDedupeStore(),
|
|
11
|
-
wsConnected: false,
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export function getDdchatState(): DdchatPluginRuntimeState {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
1
|
+
import { DdchatDedupeStore } from "./dedupe.js";
|
|
2
|
+
|
|
3
|
+
export type DdchatPluginRuntimeState = {
|
|
4
|
+
dedupe: DdchatDedupeStore;
|
|
5
|
+
wsSend?: (payload: Record<string, unknown>) => boolean;
|
|
6
|
+
wsConnected: boolean;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const state: DdchatPluginRuntimeState = {
|
|
10
|
+
dedupe: new DdchatDedupeStore(),
|
|
11
|
+
wsConnected: false,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export function getDdchatState(): DdchatPluginRuntimeState {
|
|
15
|
+
// console.log(`[ddchat] getDdchatState called - wsConnected: ${state.wsConnected}, wsSend available: ${!!state.wsSend}`);
|
|
16
|
+
return state;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function setDdchatWsRuntime(params: {
|
|
20
|
+
send?: (payload: Record<string, unknown>) => boolean;
|
|
21
|
+
connected: boolean;
|
|
22
|
+
}): void {
|
|
23
|
+
// console.log(`[ddchat] setDdchatWsRuntime called - connected: ${params.connected}, send available: ${!!params.send}`);
|
|
24
|
+
state.wsSend = params.send;
|
|
25
|
+
state.wsConnected = params.connected;
|
|
26
|
+
// console.log(`[ddchat] State updated - wsConnected: ${state.wsConnected}, wsSend available: ${!!state.wsSend}`);
|
|
27
|
+
}
|
package/src/session.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
export function buildDdchatSessionKey(params: {
|
|
2
|
-
peerKind: "direct" | "group";
|
|
3
|
-
peerId: string;
|
|
4
|
-
senderId?: string;
|
|
5
|
-
threadId?: string | null;
|
|
6
|
-
}): string {
|
|
7
|
-
const base =
|
|
8
|
-
params.peerKind === "group"
|
|
9
|
-
? `ddchat:group:${params.peerId}`
|
|
10
|
-
: `ddchat:direct:${params.peerId}`;
|
|
11
|
-
const thread = params.threadId?.trim();
|
|
12
|
-
if (!thread) {
|
|
13
|
-
return base;
|
|
14
|
-
}
|
|
15
|
-
if (params.peerKind === "group" && params.senderId) {
|
|
16
|
-
return `${base}:thread:${thread}:sender:${params.senderId}`;
|
|
17
|
-
}
|
|
18
|
-
return `${base}:thread:${thread}`;
|
|
19
|
-
}
|
|
1
|
+
export function buildDdchatSessionKey(params: {
|
|
2
|
+
peerKind: "direct" | "group";
|
|
3
|
+
peerId: string;
|
|
4
|
+
senderId?: string;
|
|
5
|
+
threadId?: string | null;
|
|
6
|
+
}): string {
|
|
7
|
+
const base =
|
|
8
|
+
params.peerKind === "group"
|
|
9
|
+
? `ddchat:group:${params.peerId}`
|
|
10
|
+
: `ddchat:direct:${params.peerId}`;
|
|
11
|
+
const thread = params.threadId?.trim();
|
|
12
|
+
if (!thread) {
|
|
13
|
+
return base;
|
|
14
|
+
}
|
|
15
|
+
if (params.peerKind === "group" && params.senderId) {
|
|
16
|
+
return `${base}:thread:${thread}:sender:${params.senderId}`;
|
|
17
|
+
}
|
|
18
|
+
return `${base}:thread:${thread}`;
|
|
19
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -1,126 +1,126 @@
|
|
|
1
|
-
import type { OpenClawConfig } from "openclaw/plugin-sdk/core";
|
|
2
|
-
import { DDCHAT_CHANNEL_ID, DDCHAT_DEFAULT_ACCOUNT_ID } from "./constants.js";
|
|
3
|
-
|
|
4
|
-
export type DdchatStreamingMode = "chunk" | "token";
|
|
5
|
-
export type DdchatConnectionMode = "websocket" | "webhook";
|
|
6
|
-
export type DdchatPolicy = "open" | "pairing" | "allowlist";
|
|
7
|
-
|
|
8
|
-
export type DdchatResolvedAccount = {
|
|
9
|
-
accountId: string;
|
|
10
|
-
enabled: boolean;
|
|
11
|
-
configured: boolean;
|
|
12
|
-
token?: string;
|
|
13
|
-
wsUrl?: string;
|
|
14
|
-
webhookPath?: string;
|
|
15
|
-
webhookPort?: number;
|
|
16
|
-
connectionMode: DdchatConnectionMode;
|
|
17
|
-
dmPolicy: DdchatPolicy;
|
|
18
|
-
groupPolicy: "open" | "allowlist" | "disabled";
|
|
19
|
-
requireMention: boolean;
|
|
20
|
-
streaming: boolean;
|
|
21
|
-
streamingMode: DdchatStreamingMode;
|
|
22
|
-
allowFrom: string[];
|
|
23
|
-
groupAllowFrom: string[];
|
|
24
|
-
heartbeatSec: number;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
type DdchatAccountConfig = {
|
|
28
|
-
enabled?: boolean;
|
|
29
|
-
token?: string;
|
|
30
|
-
wsUrl?: string;
|
|
31
|
-
webhookPath?: string;
|
|
32
|
-
webhookPort?: number;
|
|
33
|
-
connectionMode?: DdchatConnectionMode;
|
|
34
|
-
dmPolicy?: DdchatPolicy;
|
|
35
|
-
groupPolicy?: "open" | "allowlist" | "disabled";
|
|
36
|
-
requireMention?: boolean;
|
|
37
|
-
streaming?: boolean;
|
|
38
|
-
streamingMode?: DdchatStreamingMode;
|
|
39
|
-
allowFrom?: Array<string | number>;
|
|
40
|
-
groupAllowFrom?: Array<string | number>;
|
|
41
|
-
heartbeatSec?: number;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
type DdchatChannelConfig = DdchatAccountConfig & {
|
|
45
|
-
defaultAccount?: string;
|
|
46
|
-
accounts?: Record<string, DdchatAccountConfig | undefined>;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
function toStringList(input: Array<string | number> | undefined): string[] {
|
|
50
|
-
if (!input) {
|
|
51
|
-
return [];
|
|
52
|
-
}
|
|
53
|
-
return input.map((entry) => String(entry).trim()).filter(Boolean);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function normalizeConnectionMode(mode: string | undefined): DdchatConnectionMode {
|
|
57
|
-
return mode === "webhook" ? "webhook" : "websocket";
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function normalizeStreamingMode(mode: string | undefined): DdchatStreamingMode {
|
|
61
|
-
return mode === "token" ? "token" : "chunk";
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function readChannelConfig(cfg: OpenClawConfig): DdchatChannelConfig {
|
|
65
|
-
return ((cfg.channels as Record<string, unknown> | undefined)?.[DDCHAT_CHANNEL_ID] ?? {}) as DdchatChannelConfig;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function mergeAccountConfig(params: {
|
|
69
|
-
root: DdchatChannelConfig;
|
|
70
|
-
account?: DdchatAccountConfig;
|
|
71
|
-
}): DdchatAccountConfig {
|
|
72
|
-
const { root, account } = params;
|
|
73
|
-
return {
|
|
74
|
-
...root,
|
|
75
|
-
...account,
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export function listDdchatAccountIds(cfg: OpenClawConfig): string[] {
|
|
80
|
-
const channelCfg = readChannelConfig(cfg);
|
|
81
|
-
const keys = Object.keys(channelCfg.accounts ?? {}).filter((key) => key.trim().length > 0);
|
|
82
|
-
return keys.length > 0 ? keys : [DDCHAT_DEFAULT_ACCOUNT_ID];
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export function resolveDefaultDdchatAccountId(cfg: OpenClawConfig): string {
|
|
86
|
-
const channelCfg = readChannelConfig(cfg);
|
|
87
|
-
const ids = listDdchatAccountIds(cfg);
|
|
88
|
-
const configured = channelCfg.defaultAccount?.trim();
|
|
89
|
-
return configured && ids.includes(configured) ? configured : ids[0];
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export function resolveDdchatAccount(
|
|
93
|
-
cfg: OpenClawConfig,
|
|
94
|
-
accountId?: string | null,
|
|
95
|
-
): DdchatResolvedAccount {
|
|
96
|
-
const channelCfg = readChannelConfig(cfg);
|
|
97
|
-
const resolvedAccountId = accountId?.trim() || resolveDefaultDdchatAccountId(cfg);
|
|
98
|
-
const named = channelCfg.accounts?.[resolvedAccountId];
|
|
99
|
-
const merged = mergeAccountConfig({ root: channelCfg, account: named });
|
|
100
|
-
const token = typeof merged.token === "string" ? merged.token.trim() : "";
|
|
101
|
-
const wsUrl = typeof merged.wsUrl === "string" ? merged.wsUrl.trim() : "";
|
|
102
|
-
return {
|
|
103
|
-
accountId: resolvedAccountId,
|
|
104
|
-
enabled: merged.enabled !== false,
|
|
105
|
-
configured: Boolean(token),
|
|
106
|
-
token: token || undefined,
|
|
107
|
-
wsUrl: wsUrl || undefined,
|
|
108
|
-
webhookPath:
|
|
109
|
-
typeof merged.webhookPath === "string" && merged.webhookPath.trim()
|
|
110
|
-
? merged.webhookPath.trim()
|
|
111
|
-
: "/ddchat/webhook",
|
|
112
|
-
webhookPort:
|
|
113
|
-
typeof merged.webhookPort === "number" && Number.isFinite(merged.webhookPort)
|
|
114
|
-
? merged.webhookPort
|
|
115
|
-
: 3010,
|
|
116
|
-
connectionMode: normalizeConnectionMode(merged.connectionMode),
|
|
117
|
-
dmPolicy: merged.dmPolicy ?? "pairing",
|
|
118
|
-
groupPolicy: merged.groupPolicy ?? "allowlist",
|
|
119
|
-
requireMention: merged.requireMention === true,
|
|
120
|
-
streaming: merged.streaming !== false,
|
|
121
|
-
streamingMode: normalizeStreamingMode(merged.streamingMode),
|
|
122
|
-
allowFrom: toStringList(merged.allowFrom),
|
|
123
|
-
groupAllowFrom: toStringList(merged.groupAllowFrom),
|
|
124
|
-
heartbeatSec: Math.max(15, Number(merged.heartbeatSec ?? 60) || 60),
|
|
125
|
-
};
|
|
126
|
-
}
|
|
1
|
+
import type { OpenClawConfig } from "openclaw/plugin-sdk/core";
|
|
2
|
+
import { DDCHAT_CHANNEL_ID, DDCHAT_DEFAULT_ACCOUNT_ID } from "./constants.js";
|
|
3
|
+
|
|
4
|
+
export type DdchatStreamingMode = "chunk" | "token";
|
|
5
|
+
export type DdchatConnectionMode = "websocket" | "webhook";
|
|
6
|
+
export type DdchatPolicy = "open" | "pairing" | "allowlist";
|
|
7
|
+
|
|
8
|
+
export type DdchatResolvedAccount = {
|
|
9
|
+
accountId: string;
|
|
10
|
+
enabled: boolean;
|
|
11
|
+
configured: boolean;
|
|
12
|
+
token?: string;
|
|
13
|
+
wsUrl?: string;
|
|
14
|
+
webhookPath?: string;
|
|
15
|
+
webhookPort?: number;
|
|
16
|
+
connectionMode: DdchatConnectionMode;
|
|
17
|
+
dmPolicy: DdchatPolicy;
|
|
18
|
+
groupPolicy: "open" | "allowlist" | "disabled";
|
|
19
|
+
requireMention: boolean;
|
|
20
|
+
streaming: boolean;
|
|
21
|
+
streamingMode: DdchatStreamingMode;
|
|
22
|
+
allowFrom: string[];
|
|
23
|
+
groupAllowFrom: string[];
|
|
24
|
+
heartbeatSec: number;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
type DdchatAccountConfig = {
|
|
28
|
+
enabled?: boolean;
|
|
29
|
+
token?: string;
|
|
30
|
+
wsUrl?: string;
|
|
31
|
+
webhookPath?: string;
|
|
32
|
+
webhookPort?: number;
|
|
33
|
+
connectionMode?: DdchatConnectionMode;
|
|
34
|
+
dmPolicy?: DdchatPolicy;
|
|
35
|
+
groupPolicy?: "open" | "allowlist" | "disabled";
|
|
36
|
+
requireMention?: boolean;
|
|
37
|
+
streaming?: boolean;
|
|
38
|
+
streamingMode?: DdchatStreamingMode;
|
|
39
|
+
allowFrom?: Array<string | number>;
|
|
40
|
+
groupAllowFrom?: Array<string | number>;
|
|
41
|
+
heartbeatSec?: number;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
type DdchatChannelConfig = DdchatAccountConfig & {
|
|
45
|
+
defaultAccount?: string;
|
|
46
|
+
accounts?: Record<string, DdchatAccountConfig | undefined>;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
function toStringList(input: Array<string | number> | undefined): string[] {
|
|
50
|
+
if (!input) {
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
return input.map((entry) => String(entry).trim()).filter(Boolean);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function normalizeConnectionMode(mode: string | undefined): DdchatConnectionMode {
|
|
57
|
+
return mode === "webhook" ? "webhook" : "websocket";
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function normalizeStreamingMode(mode: string | undefined): DdchatStreamingMode {
|
|
61
|
+
return mode === "token" ? "token" : "chunk";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function readChannelConfig(cfg: OpenClawConfig): DdchatChannelConfig {
|
|
65
|
+
return ((cfg.channels as Record<string, unknown> | undefined)?.[DDCHAT_CHANNEL_ID] ?? {}) as DdchatChannelConfig;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function mergeAccountConfig(params: {
|
|
69
|
+
root: DdchatChannelConfig;
|
|
70
|
+
account?: DdchatAccountConfig;
|
|
71
|
+
}): DdchatAccountConfig {
|
|
72
|
+
const { root, account } = params;
|
|
73
|
+
return {
|
|
74
|
+
...root,
|
|
75
|
+
...account,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function listDdchatAccountIds(cfg: OpenClawConfig): string[] {
|
|
80
|
+
const channelCfg = readChannelConfig(cfg);
|
|
81
|
+
const keys = Object.keys(channelCfg.accounts ?? {}).filter((key) => key.trim().length > 0);
|
|
82
|
+
return keys.length > 0 ? keys : [DDCHAT_DEFAULT_ACCOUNT_ID];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function resolveDefaultDdchatAccountId(cfg: OpenClawConfig): string {
|
|
86
|
+
const channelCfg = readChannelConfig(cfg);
|
|
87
|
+
const ids = listDdchatAccountIds(cfg);
|
|
88
|
+
const configured = channelCfg.defaultAccount?.trim();
|
|
89
|
+
return configured && ids.includes(configured) ? configured : ids[0];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function resolveDdchatAccount(
|
|
93
|
+
cfg: OpenClawConfig,
|
|
94
|
+
accountId?: string | null,
|
|
95
|
+
): DdchatResolvedAccount {
|
|
96
|
+
const channelCfg = readChannelConfig(cfg);
|
|
97
|
+
const resolvedAccountId = accountId?.trim() || resolveDefaultDdchatAccountId(cfg);
|
|
98
|
+
const named = channelCfg.accounts?.[resolvedAccountId];
|
|
99
|
+
const merged = mergeAccountConfig({ root: channelCfg, account: named });
|
|
100
|
+
const token = typeof merged.token === "string" ? merged.token.trim() : "";
|
|
101
|
+
const wsUrl = typeof merged.wsUrl === "string" ? merged.wsUrl.trim() : "";
|
|
102
|
+
return {
|
|
103
|
+
accountId: resolvedAccountId,
|
|
104
|
+
enabled: merged.enabled !== false,
|
|
105
|
+
configured: Boolean(token),
|
|
106
|
+
token: token || undefined,
|
|
107
|
+
wsUrl: wsUrl || undefined,
|
|
108
|
+
webhookPath:
|
|
109
|
+
typeof merged.webhookPath === "string" && merged.webhookPath.trim()
|
|
110
|
+
? merged.webhookPath.trim()
|
|
111
|
+
: "/ddchat/webhook",
|
|
112
|
+
webhookPort:
|
|
113
|
+
typeof merged.webhookPort === "number" && Number.isFinite(merged.webhookPort)
|
|
114
|
+
? merged.webhookPort
|
|
115
|
+
: 3010,
|
|
116
|
+
connectionMode: normalizeConnectionMode(merged.connectionMode),
|
|
117
|
+
dmPolicy: merged.dmPolicy ?? "pairing",
|
|
118
|
+
groupPolicy: merged.groupPolicy ?? "allowlist",
|
|
119
|
+
requireMention: merged.requireMention === true,
|
|
120
|
+
streaming: merged.streaming !== false,
|
|
121
|
+
streamingMode: normalizeStreamingMode(merged.streamingMode),
|
|
122
|
+
allowFrom: toStringList(merged.allowFrom),
|
|
123
|
+
groupAllowFrom: toStringList(merged.groupAllowFrom),
|
|
124
|
+
heartbeatSec: Math.max(15, Number(merged.heartbeatSec ?? 60) || 60),
|
|
125
|
+
};
|
|
126
|
+
}
|
package/task/BLOCKERS.md
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
# BLOCKERS
|
|
2
|
-
|
|
3
|
-
- None currently.
|
|
1
|
+
# BLOCKERS
|
|
2
|
+
|
|
3
|
+
- None currently.
|
package/task/DOING.md
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
# DOING
|
|
2
|
-
|
|
3
|
-
- [ ] Implement websocket-first inbound and chunk streaming for multi-account `ddchat`
|
|
1
|
+
# DOING
|
|
2
|
+
|
|
3
|
+
- [ ] Implement websocket-first inbound and chunk streaming for multi-account `ddchat`
|