ddchat 0.2.0 → 0.3.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/src/outbound.ts CHANGED
@@ -1,183 +1,167 @@
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
- };
1
+ import { randomUUID } from "node:crypto";
2
+ import { pathToFileURL } from "node:url";
3
+ import { loadWebMedia } from "openclaw/plugin-sdk/web-media";
4
+ import type { OpenClawConfig } from "openclaw/plugin-sdk/core";
5
+ import { DDCHAT_CHANNEL_ID } from "./constants.js";
6
+ import { getDdchatWsRuntime } from "./runtime.js";
7
+ import { resolveDdchatMediaMaxBytes } from "./types.js";
8
+
9
+ function createDdchatMessageId(prefix: string): string {
10
+ return `${prefix}-${randomUUID()}`;
11
+ }
12
+
13
+ function isLocalFilePath(url: string): boolean {
14
+ if (url.startsWith("file://")) return true;
15
+ if (/^[a-zA-Z]:[\\/]/.test(url)) return true;
16
+ if (url.startsWith("/") && !url.startsWith("//")) return true;
17
+ return false;
18
+ }
19
+
20
+ function toFileUrl(path: string): string {
21
+ if (path.startsWith("file://")) return path;
22
+ return pathToFileURL(path).href;
23
+ }
24
+
25
+ export async function resolveDdchatOutboundMediaFields(
26
+ cfg: OpenClawConfig,
27
+ mediaUrl: string,
28
+ ): Promise<{ mediaBase64?: string; mediaType?: string; mediaName?: string; error?: string }> {
29
+ try {
30
+ let resolvedUrl = mediaUrl;
31
+ if (isLocalFilePath(mediaUrl)) {
32
+ resolvedUrl = toFileUrl(mediaUrl);
33
+ }
34
+ console.log(`[ddchat] Resolved media URL: ${resolvedUrl}`);
35
+
36
+ const media = await loadWebMedia(resolvedUrl, {
37
+ maxBytes: resolveDdchatMediaMaxBytes(cfg),
38
+ });
39
+ console.log(`[ddchat] Media loaded successfully: ${media.fileName}, type: ${media.contentType}, size: ${media.buffer.length}`);
40
+ return {
41
+ mediaBase64: media.buffer.toString("base64"),
42
+ mediaType: media.contentType,
43
+ mediaName: media.fileName,
44
+ };
45
+ } catch (error) {
46
+ const errorMsg = error instanceof Error ? error.message : String(error);
47
+ console.error(`[ddchat] Failed to load media from ${mediaUrl}: ${errorMsg}`);
48
+ return { error: errorMsg };
49
+ }
50
+ }
51
+
52
+ function resolveTarget(to: string): { targetType: "group" | "direct"; targetId: string } {
53
+ const normalized = to.trim();
54
+ if (normalized.startsWith("group:")) {
55
+ return { targetType: "group", targetId: normalized.slice("group:".length) };
56
+ }
57
+ if (normalized.startsWith("chat:")) {
58
+ return { targetType: "group", targetId: normalized.slice("chat:".length) };
59
+ }
60
+ if (normalized.startsWith("user:")) {
61
+ return { targetType: "direct", targetId: normalized.slice("user:".length) };
62
+ }
63
+ return { targetType: "direct", targetId: normalized };
64
+ }
65
+
66
+ export const ddchatOutbound = {
67
+ deliveryMode: "direct",
68
+ textChunkLimit: 4000,
69
+ chunkerMode: "markdown",
70
+ sendText: async ({ to, text, accountId }) => {
71
+ const messageId = createDdchatMessageId("ddchat-text");
72
+ const target = resolveTarget(to);
73
+
74
+ const runtime = getDdchatWsRuntime(accountId);
75
+ if (!runtime.send || !runtime.connected) {
76
+ throw new Error("DDChat WebSocket not connected");
77
+ }
78
+
79
+ const payload = {
80
+ type: "outbound_message",
81
+ from: "claw",
82
+ channel: DDCHAT_CHANNEL_ID,
83
+ accountId,
84
+ messageId,
85
+ targetType: target.targetType,
86
+ targetId: target.targetId,
87
+ text,
88
+ };
89
+ console.log(`[ddchat] Sending text payload:`, JSON.stringify(payload, null, 2));
90
+ const sent = runtime.send(payload);
91
+ if (!sent) {
92
+ throw new Error("Failed to send text message via WebSocket");
93
+ }
94
+ return {
95
+ messageId,
96
+ to,
97
+ channel: DDCHAT_CHANNEL_ID,
98
+ text,
99
+ transport: "ws",
100
+ };
101
+ },
102
+ sendMedia: async ({ cfg, to, text, mediaUrl, accountId }) => {
103
+ const messageId = createDdchatMessageId("ddchat-media");
104
+ const target = resolveTarget(to);
105
+
106
+ const runtime = getDdchatWsRuntime(accountId);
107
+ console.log(`[ddchat] sendMedia called - wsConnected: ${runtime.connected}, wsSend available: ${!!runtime.send}`);
108
+ if (!runtime.send || !runtime.connected) {
109
+ throw new Error("DDChat WebSocket not connected");
110
+ }
111
+
112
+ console.log(`[ddchat] Loading media from: ${mediaUrl}`);
113
+ const { mediaBase64, mediaType, mediaName, error } = await resolveDdchatOutboundMediaFields(
114
+ cfg,
115
+ mediaUrl,
116
+ );
117
+
118
+ if (error) {
119
+ console.error(`[ddchat] Media loading failed: ${error}`);
120
+ throw new Error(`Failed to load media: ${error}`);
121
+ }
122
+
123
+ if (!mediaBase64) {
124
+ console.error(`[ddchat] No media data available for ${mediaUrl}`);
125
+ throw new Error("No media data available");
126
+ }
127
+
128
+ const payload = {
129
+ type: "outbound_message",
130
+ from: "claw",
131
+ channel: DDCHAT_CHANNEL_ID,
132
+ accountId,
133
+ messageId,
134
+ targetType: target.targetType,
135
+ targetId: target.targetId,
136
+ text,
137
+ mediaUrl,
138
+ mediaBase64,
139
+ mediaType,
140
+ mediaName,
141
+ };
142
+ console.log(`[ddchat] Sending media message:`, {
143
+ messageId,
144
+ targetType: target.targetType,
145
+ targetId: target.targetId,
146
+ mediaType,
147
+ mediaName,
148
+ mediaSize: mediaBase64?.length,
149
+ });
150
+ const sent = runtime.send(payload);
151
+ if (!sent) {
152
+ console.error(`[ddchat] Failed to send media message via WebSocket`);
153
+ throw new Error("Failed to send media message via WebSocket");
154
+ }
155
+
156
+ return {
157
+ messageId,
158
+ to,
159
+ channel: DDCHAT_CHANNEL_ID,
160
+ text,
161
+ mediaUrl,
162
+ mediaType,
163
+ mediaName,
164
+ transport: "ws",
165
+ };
166
+ },
167
+ };
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,27 +1,41 @@
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
- }
1
+ import { DdchatDedupeStore } from "./dedupe.js";
2
+
3
+ export type DdchatWsSend = (payload: Record<string, unknown>) => boolean;
4
+
5
+ export type DdchatWsRuntime = {
6
+ send?: DdchatWsSend;
7
+ connected: boolean;
8
+ };
9
+
10
+ export type DdchatPluginRuntimeState = {
11
+ dedupe: DdchatDedupeStore;
12
+ wsByAccount: Map<string, DdchatWsRuntime>;
13
+ };
14
+
15
+ const state: DdchatPluginRuntimeState = {
16
+ dedupe: new DdchatDedupeStore(),
17
+ wsByAccount: new Map(),
18
+ };
19
+
20
+ export function getDdchatState(): DdchatPluginRuntimeState {
21
+ return state;
22
+ }
23
+
24
+ export function getDdchatWsRuntime(accountId: string): DdchatWsRuntime {
25
+ return state.wsByAccount.get(accountId) ?? { connected: false };
26
+ }
27
+
28
+ export function setDdchatWsRuntime(params: {
29
+ accountId: string;
30
+ send?: DdchatWsSend;
31
+ connected: boolean;
32
+ }): void {
33
+ state.wsByAccount.set(params.accountId, {
34
+ send: params.send,
35
+ connected: params.connected,
36
+ });
37
+ }
38
+
39
+ export function clearDdchatWsRuntime(accountId: string): void {
40
+ state.wsByAccount.delete(accountId);
41
+ }
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
+ }