@aka_openclaw_plugin/mychat 0.1.11 → 0.1.12
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/api.js +1 -1
- package/{channel-Notyh4R-.js → channel-BdJp6UKB.js} +45 -57
- package/channel-plugin-api.js +1 -1
- package/package.json +1 -1
- package/setup-entry.js +1 -1
package/api.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { a as tryGetMychatRuntime, i as setMychatRuntime, n as init_runtime, t as getMychatRuntime } from "./runtime-PfFuZ2Rm.js";
|
|
2
|
-
import { t as mychatPlugin } from "./channel-
|
|
2
|
+
import { t as mychatPlugin } from "./channel-BdJp6UKB.js";
|
|
3
3
|
//#region api.ts
|
|
4
4
|
init_runtime();
|
|
5
5
|
//#endregion
|
|
@@ -97,12 +97,34 @@ function resolveMychatAccount(params) {
|
|
|
97
97
|
return accounts.find((a) => a.accountId === params.accountId) ?? accounts[0] ?? null;
|
|
98
98
|
}
|
|
99
99
|
//#endregion
|
|
100
|
+
//#region src/logger.ts
|
|
101
|
+
const PREFIX = "mychat";
|
|
102
|
+
function mychatLogPrefix(feature) {
|
|
103
|
+
return `[${PREFIX}:${feature}]`;
|
|
104
|
+
}
|
|
105
|
+
function getMychatLogger() {
|
|
106
|
+
try {
|
|
107
|
+
const logger = ((init_runtime(), __toCommonJS(runtime_exports)).tryGetMychatRuntime?.())?.logger;
|
|
108
|
+
if (!logger) return null;
|
|
109
|
+
return {
|
|
110
|
+
debug: (m) => logger.debug?.(m),
|
|
111
|
+
info: (m) => logger.info(m),
|
|
112
|
+
warn: (m) => logger.warn(m),
|
|
113
|
+
error: (m) => logger.error(m)
|
|
114
|
+
};
|
|
115
|
+
} catch {
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
//#endregion
|
|
100
120
|
//#region src/client/http-client.ts
|
|
101
121
|
function createMychatHttpClient(params) {
|
|
102
122
|
const { baseUrl, token } = params;
|
|
103
123
|
const base = baseUrl.replace(/\/+$/, "");
|
|
124
|
+
const prefix = mychatLogPrefix("http");
|
|
104
125
|
async function request(method, path, body) {
|
|
105
126
|
const url = `${base}${path}`;
|
|
127
|
+
const logger = getMychatLogger();
|
|
106
128
|
try {
|
|
107
129
|
const headers = { authorization: `Bearer ${token}` };
|
|
108
130
|
if (body !== void 0) headers["content-type"] = "application/json";
|
|
@@ -116,13 +138,14 @@ function createMychatHttpClient(params) {
|
|
|
116
138
|
try {
|
|
117
139
|
errorBody = await response.text();
|
|
118
140
|
} catch {}
|
|
119
|
-
|
|
141
|
+
const msg = `${prefix} ${method} ${url} failed: ${response.status} ${response.statusText}${errorBody ? ` body=${errorBody.slice(0, 200)}` : ""}`;
|
|
142
|
+
logger?.error(msg);
|
|
120
143
|
return null;
|
|
121
144
|
}
|
|
122
145
|
return await response.json();
|
|
123
146
|
} catch (err) {
|
|
124
147
|
const errMsg = err instanceof Error ? `${err.message}` : String(err);
|
|
125
|
-
|
|
148
|
+
logger?.error(`${prefix} ${method} ${url} error: ${errMsg}`);
|
|
126
149
|
return null;
|
|
127
150
|
}
|
|
128
151
|
}
|
|
@@ -155,6 +178,7 @@ function createMychatHttpClient(params) {
|
|
|
155
178
|
},
|
|
156
179
|
async uploadFile(params) {
|
|
157
180
|
const url = `${base}/api/bot/uploads`;
|
|
181
|
+
const logger = getMychatLogger();
|
|
158
182
|
try {
|
|
159
183
|
const formData = new FormData();
|
|
160
184
|
let blob;
|
|
@@ -176,30 +200,31 @@ function createMychatHttpClient(params) {
|
|
|
176
200
|
try {
|
|
177
201
|
errorBody = await response.text();
|
|
178
202
|
} catch {}
|
|
179
|
-
|
|
203
|
+
logger?.error(`${prefix} POST ${url} failed: ${response.status} ${response.statusText}${errorBody ? ` body=${errorBody.slice(0, 200)}` : ""}`);
|
|
180
204
|
return null;
|
|
181
205
|
}
|
|
182
206
|
return await response.json();
|
|
183
207
|
} catch (err) {
|
|
184
208
|
const errMsg = err instanceof Error ? `${err.message}` : String(err);
|
|
185
|
-
|
|
209
|
+
logger?.error(`${prefix} POST ${url} error: ${errMsg}`);
|
|
186
210
|
return null;
|
|
187
211
|
}
|
|
188
212
|
},
|
|
189
213
|
async healthCheck() {
|
|
190
214
|
const url = `${base}/health`;
|
|
215
|
+
const logger = getMychatLogger();
|
|
191
216
|
const start = Date.now();
|
|
192
217
|
try {
|
|
193
218
|
const response = await fetch(url);
|
|
194
219
|
const latencyMs = Date.now() - start;
|
|
195
|
-
if (!response.ok)
|
|
220
|
+
if (!response.ok) logger?.error(`${prefix} health check failed: ${url} status=${response.status} latencyMs=${latencyMs}`);
|
|
196
221
|
return {
|
|
197
222
|
ok: response.ok,
|
|
198
223
|
latencyMs
|
|
199
224
|
};
|
|
200
225
|
} catch (err) {
|
|
201
226
|
const errMsg = err instanceof Error ? `${err.message}` : String(err);
|
|
202
|
-
|
|
227
|
+
logger?.error(`${prefix} health check error: ${url} error=${errMsg} latencyMs=${Date.now() - start}`);
|
|
203
228
|
return {
|
|
204
229
|
ok: false,
|
|
205
230
|
latencyMs: Date.now() - start
|
|
@@ -3756,6 +3781,7 @@ function createMychatWsClient(params) {
|
|
|
3756
3781
|
const initialDelay = reconnect?.initialDelayMs ?? DEFAULT_INITIAL_DELAY_MS;
|
|
3757
3782
|
const maxDelay = reconnect?.maxDelayMs ?? DEFAULT_MAX_DELAY_MS;
|
|
3758
3783
|
const backoffMultiplier = reconnect?.backoffMultiplier ?? DEFAULT_BACKOFF_MULTIPLIER;
|
|
3784
|
+
const prefix = mychatLogPrefix("ws");
|
|
3759
3785
|
let ws = null;
|
|
3760
3786
|
let connected = false;
|
|
3761
3787
|
let reconnectTimer = null;
|
|
@@ -3770,7 +3796,7 @@ function createMychatWsClient(params) {
|
|
|
3770
3796
|
}
|
|
3771
3797
|
function scheduleReconnect() {
|
|
3772
3798
|
if (reconnectTimer) return;
|
|
3773
|
-
|
|
3799
|
+
getMychatLogger()?.info(`${prefix} will reconnect in ${currentDelay}ms`);
|
|
3774
3800
|
reconnectTimer = setTimeout(() => {
|
|
3775
3801
|
reconnectTimer = null;
|
|
3776
3802
|
currentDelay = Math.min(currentDelay * backoffMultiplier, maxDelay);
|
|
@@ -3779,20 +3805,21 @@ function createMychatWsClient(params) {
|
|
|
3779
3805
|
}
|
|
3780
3806
|
function connect() {
|
|
3781
3807
|
if (ws) return;
|
|
3808
|
+
const logger = getMychatLogger();
|
|
3782
3809
|
try {
|
|
3783
3810
|
const url = buildUrl();
|
|
3784
|
-
|
|
3811
|
+
logger?.info(`${prefix} connecting to ${url}`);
|
|
3785
3812
|
ws = new wrapper_default(url, { headers: { Authorization: `Bearer ${token}` } });
|
|
3786
3813
|
ws.on("open", () => {
|
|
3787
3814
|
connected = true;
|
|
3788
3815
|
currentDelay = initialDelay;
|
|
3789
|
-
|
|
3816
|
+
logger?.info(`${prefix} connected botSelfId=${botSelfId ?? "none"}`);
|
|
3790
3817
|
if (botSelfId) {
|
|
3791
3818
|
const subMsg = JSON.stringify({
|
|
3792
3819
|
type: "subscribe",
|
|
3793
3820
|
body: { subscribe: { conversationId: botSelfId } }
|
|
3794
3821
|
});
|
|
3795
|
-
|
|
3822
|
+
logger?.info(`${prefix} subscribing to conversationId=${botSelfId}`);
|
|
3796
3823
|
ws?.send(subMsg);
|
|
3797
3824
|
}
|
|
3798
3825
|
});
|
|
@@ -3805,16 +3832,16 @@ function createMychatWsClient(params) {
|
|
|
3805
3832
|
ws.on("close", (code, reason) => {
|
|
3806
3833
|
connected = false;
|
|
3807
3834
|
ws = null;
|
|
3808
|
-
|
|
3835
|
+
logger?.info(`${prefix} closed code=${code} reason=${reason?.toString() || "none"}`);
|
|
3809
3836
|
scheduleReconnect();
|
|
3810
3837
|
});
|
|
3811
3838
|
ws.on("error", (err) => {
|
|
3812
3839
|
const errMsg = err instanceof Error ? err.message : String(err);
|
|
3813
|
-
|
|
3840
|
+
logger?.error(`${prefix} error: ${errMsg}`);
|
|
3814
3841
|
});
|
|
3815
3842
|
} catch (err) {
|
|
3816
3843
|
const errMsg = err instanceof Error ? err.message : String(err);
|
|
3817
|
-
|
|
3844
|
+
logger?.error(`${prefix} connect exception: ${errMsg}`);
|
|
3818
3845
|
scheduleReconnect();
|
|
3819
3846
|
}
|
|
3820
3847
|
}
|
|
@@ -3837,33 +3864,11 @@ function createMychatWsClient(params) {
|
|
|
3837
3864
|
};
|
|
3838
3865
|
},
|
|
3839
3866
|
isConnected() {
|
|
3840
|
-
|
|
3841
|
-
console.log(`[mychat] WS isConnected() called, returning ${state}`);
|
|
3842
|
-
return state;
|
|
3867
|
+
return connected;
|
|
3843
3868
|
}
|
|
3844
3869
|
};
|
|
3845
3870
|
}
|
|
3846
3871
|
//#endregion
|
|
3847
|
-
//#region src/logger.ts
|
|
3848
|
-
const PREFIX = "mychat";
|
|
3849
|
-
function mychatLogPrefix(feature) {
|
|
3850
|
-
return `[${PREFIX}:${feature}]`;
|
|
3851
|
-
}
|
|
3852
|
-
function getMychatLogger() {
|
|
3853
|
-
try {
|
|
3854
|
-
const logger = ((init_runtime(), __toCommonJS(runtime_exports)).tryGetMychatRuntime?.())?.logger;
|
|
3855
|
-
if (!logger) return null;
|
|
3856
|
-
return {
|
|
3857
|
-
debug: (m) => logger.debug?.(m),
|
|
3858
|
-
info: (m) => logger.info(m),
|
|
3859
|
-
warn: (m) => logger.warn(m),
|
|
3860
|
-
error: (m) => logger.error(m)
|
|
3861
|
-
};
|
|
3862
|
-
} catch {
|
|
3863
|
-
return null;
|
|
3864
|
-
}
|
|
3865
|
-
}
|
|
3866
|
-
//#endregion
|
|
3867
3872
|
//#region src/monitor/listeners.ts
|
|
3868
3873
|
function createMychatMessageListener(params) {
|
|
3869
3874
|
const { handler } = params;
|
|
@@ -3935,8 +3940,7 @@ function createMychatProviderLifecycle(params) {
|
|
|
3935
3940
|
return {
|
|
3936
3941
|
async waitForReady() {
|
|
3937
3942
|
const start = Date.now();
|
|
3938
|
-
|
|
3939
|
-
logger?.info(`${prefix} starting connection lifecycle timeoutMs=${readyTimeoutMs}`);
|
|
3943
|
+
logger?.info(`${prefix} starting timeoutMs=${readyTimeoutMs}`);
|
|
3940
3944
|
wsClient.connect();
|
|
3941
3945
|
let wsConnectedAt = null;
|
|
3942
3946
|
let getSelfAttempts = 0;
|
|
@@ -3944,29 +3948,24 @@ function createMychatProviderLifecycle(params) {
|
|
|
3944
3948
|
if (wsClient.isConnected()) {
|
|
3945
3949
|
if (!wsConnectedAt) {
|
|
3946
3950
|
wsConnectedAt = Date.now();
|
|
3947
|
-
console.log(`[mychat:lifecycle] ws connected, fetching bot identity elapsedMs=${wsConnectedAt - start}`);
|
|
3948
3951
|
logger?.info(`${prefix} ws connected, fetching bot identity elapsedMs=${wsConnectedAt - start}`);
|
|
3949
3952
|
}
|
|
3950
3953
|
getSelfAttempts++;
|
|
3951
3954
|
const botSelf = await httpClient.getSelf();
|
|
3952
3955
|
if (botSelf) {
|
|
3953
|
-
console.log(`[mychat:lifecycle] bot connected botId=${botSelf.botId} wsConnectMs=${wsConnectedAt - start} getSelfAttempts=${getSelfAttempts}`);
|
|
3954
3956
|
logger?.info(`${prefix} bot connected botId=${botSelf.botId} wsConnectMs=${wsConnectedAt - start} getSelfAttempts=${getSelfAttempts}`);
|
|
3955
3957
|
return botSelf;
|
|
3956
3958
|
}
|
|
3957
|
-
console.warn(`[mychat:lifecycle] getSelf() failed attempt=${getSelfAttempts} elapsedMs=${Date.now() - start}`);
|
|
3958
3959
|
logger?.warn(`${prefix} getSelf() failed attempt=${getSelfAttempts} elapsedMs=${Date.now() - start}`);
|
|
3959
3960
|
}
|
|
3960
3961
|
await new Promise((r) => setTimeout(r, 500));
|
|
3961
3962
|
}
|
|
3962
3963
|
const wsConnected = wsClient.isConnected();
|
|
3963
3964
|
const elapsed = Date.now() - start;
|
|
3964
|
-
console.warn(`[mychat:lifecycle] ready timeout wsConnected=${wsConnected} wsConnectedAt=${wsConnectedAt ? wsConnectedAt - start : "never"} getSelfAttempts=${getSelfAttempts} elapsedMs=${elapsed} timeoutMs=${readyTimeoutMs}`);
|
|
3965
3965
|
logger?.warn(`${prefix} ready timeout wsConnected=${wsConnected} wsConnectedAt=${wsConnectedAt ? wsConnectedAt - start : "never"} getSelfAttempts=${getSelfAttempts} elapsedMs=${elapsed} timeoutMs=${readyTimeoutMs}`);
|
|
3966
3966
|
return null;
|
|
3967
3967
|
},
|
|
3968
3968
|
shutdown() {
|
|
3969
|
-
console.log(`[mychat:lifecycle] shutdown`);
|
|
3970
3969
|
wsClient.disconnect();
|
|
3971
3970
|
logger?.info(`${prefix} shutdown`);
|
|
3972
3971
|
}
|
|
@@ -4126,7 +4125,6 @@ async function monitorMychatProvider(ctx) {
|
|
|
4126
4125
|
const { account, setStatus } = ctx;
|
|
4127
4126
|
const logger = getMychatLogger();
|
|
4128
4127
|
const prefix = mychatLogPrefix("provider");
|
|
4129
|
-
console.log(`[mychat:provider] starting accountId=${account.accountId}`);
|
|
4130
4128
|
if (logger) logger.info(`${prefix} starting accountId=${account.accountId}`);
|
|
4131
4129
|
setStatus({
|
|
4132
4130
|
connected: false,
|
|
@@ -4136,12 +4134,10 @@ async function monitorMychatProvider(ctx) {
|
|
|
4136
4134
|
baseUrl: account.baseUrl,
|
|
4137
4135
|
token: account.token
|
|
4138
4136
|
});
|
|
4139
|
-
console.log(`[mychat:provider] fetching bot identity baseUrl=${account.baseUrl}`);
|
|
4140
4137
|
if (logger) logger.info(`${prefix} fetching bot identity baseUrl=${account.baseUrl}`);
|
|
4141
4138
|
const botSelf = await httpClient.getSelf();
|
|
4142
4139
|
if (!botSelf) {
|
|
4143
4140
|
const error = `MyChat provider failed to get bot identity (baseUrl=${account.baseUrl}, token=${account.token ? account.token.slice(0, 8) + "..." : "empty"})`;
|
|
4144
|
-
console.error(`[mychat:provider] ${error}`);
|
|
4145
4141
|
if (logger) logger.error(`${prefix} ${error}`);
|
|
4146
4142
|
setStatus({
|
|
4147
4143
|
connected: false,
|
|
@@ -4149,16 +4145,11 @@ async function monitorMychatProvider(ctx) {
|
|
|
4149
4145
|
});
|
|
4150
4146
|
throw new Error(error);
|
|
4151
4147
|
}
|
|
4152
|
-
console.log(`[mychat:provider] got bot identity botId=${botSelf.botId} name=${botSelf.name ?? "unknown"}`);
|
|
4153
4148
|
if (logger) logger.info(`${prefix} got bot identity botId=${botSelf.botId} name=${botSelf.name ?? "unknown"}`);
|
|
4154
4149
|
const health = await httpClient.healthCheck();
|
|
4155
4150
|
if (!health.ok) {
|
|
4156
|
-
console.warn(`[mychat:provider] health check failed latencyMs=${health.latencyMs} baseUrl=${account.baseUrl}`);
|
|
4157
4151
|
if (logger) logger.warn(`${prefix} health check failed latencyMs=${health.latencyMs} baseUrl=${account.baseUrl}`);
|
|
4158
|
-
} else {
|
|
4159
|
-
console.log(`[mychat:provider] health check ok latencyMs=${health.latencyMs}`);
|
|
4160
|
-
if (logger) logger.info(`${prefix} health check ok latencyMs=${health.latencyMs}`);
|
|
4161
|
-
}
|
|
4152
|
+
} else if (logger) logger.info(`${prefix} health check ok latencyMs=${health.latencyMs}`);
|
|
4162
4153
|
const { wsClient } = createMychatProviderClients(account, botSelf.botId);
|
|
4163
4154
|
const rt = ctx.runtime;
|
|
4164
4155
|
rt.mychat = {
|
|
@@ -4180,7 +4171,7 @@ async function monitorMychatProvider(ctx) {
|
|
|
4180
4171
|
}),
|
|
4181
4172
|
wsClient
|
|
4182
4173
|
});
|
|
4183
|
-
|
|
4174
|
+
if (logger) logger.info(`${prefix} starting connection lifecycle`);
|
|
4184
4175
|
const lifecycle = createMychatProviderLifecycle({
|
|
4185
4176
|
httpClient,
|
|
4186
4177
|
wsClient
|
|
@@ -4188,7 +4179,6 @@ async function monitorMychatProvider(ctx) {
|
|
|
4188
4179
|
const readyBotSelf = await lifecycle.waitForReady();
|
|
4189
4180
|
if (!readyBotSelf) {
|
|
4190
4181
|
const error = "MyChat provider failed to connect";
|
|
4191
|
-
console.error(`[mychat:provider] ${error}`);
|
|
4192
4182
|
if (logger) logger.error(`${prefix} ${error}`);
|
|
4193
4183
|
setStatus({
|
|
4194
4184
|
connected: false,
|
|
@@ -4204,16 +4194,14 @@ async function monitorMychatProvider(ctx) {
|
|
|
4204
4194
|
mode: "websocket",
|
|
4205
4195
|
lastError: null
|
|
4206
4196
|
});
|
|
4207
|
-
console.log(`[mychat:provider] bot connected botId=${readyBotSelf.botId}`);
|
|
4208
4197
|
if (logger) logger.info(`${prefix} bot connected botId=${readyBotSelf.botId}`);
|
|
4209
4198
|
ctx.abortSignal?.addEventListener("abort", () => {
|
|
4210
|
-
console.log(`[mychat:provider] abort signal received, shutting down`);
|
|
4211
4199
|
if (logger) logger.info(`${prefix} abort signal received, shutting down`);
|
|
4212
4200
|
lifecycle.shutdown();
|
|
4213
4201
|
setStatus({ connected: false });
|
|
4214
4202
|
});
|
|
4215
4203
|
return { unsubscribe() {
|
|
4216
|
-
|
|
4204
|
+
if (logger) logger.info(`${prefix} unsubscribe called, shutting down`);
|
|
4217
4205
|
lifecycle.shutdown();
|
|
4218
4206
|
setStatus({ connected: false });
|
|
4219
4207
|
} };
|
package/channel-plugin-api.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as mychatPlugin } from "./channel-
|
|
1
|
+
import { t as mychatPlugin } from "./channel-BdJp6UKB.js";
|
|
2
2
|
export { mychatPlugin };
|
package/package.json
CHANGED
package/setup-entry.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as mychatPlugin } from "./channel-
|
|
1
|
+
import { t as mychatPlugin } from "./channel-BdJp6UKB.js";
|
|
2
2
|
export { mychatPlugin };
|