@aka_openclaw_plugin/mychat 0.1.10 → 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 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-BTGCJxf9.js";
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
- console.error(`[mychat] HTTP ${method} ${url} failed: ${response.status} ${response.statusText}${errorBody ? ` body=${errorBody.slice(0, 200)}` : ""}`);
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
- console.error(`[mychat] HTTP ${method} ${url} error: ${errMsg}`);
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
- console.error(`[mychat] HTTP POST ${url} failed: ${response.status} ${response.statusText}${errorBody ? ` body=${errorBody.slice(0, 200)}` : ""}`);
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
- console.error(`[mychat] HTTP POST ${url} error: ${errMsg}`);
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) console.error(`[mychat] Health check failed: ${url} status=${response.status} latencyMs=${latencyMs}`);
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
- console.error(`[mychat] Health check error: ${url} error=${errMsg} latencyMs=${Date.now() - start}`);
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
- console.log(`[mychat] WS will reconnect in ${currentDelay}ms`);
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,18 +3805,23 @@ 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
- console.log(`[mychat] WS connecting to ${url}`);
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
- console.log(`[mychat] WS connected botSelfId=${botSelfId ?? "none"}`);
3790
- if (botSelfId) ws?.send(JSON.stringify({
3791
- type: "subscribe",
3792
- body: { subscribe: { conversationId: botSelfId } }
3793
- }));
3816
+ logger?.info(`${prefix} connected botSelfId=${botSelfId ?? "none"}`);
3817
+ if (botSelfId) {
3818
+ const subMsg = JSON.stringify({
3819
+ type: "subscribe",
3820
+ body: { subscribe: { conversationId: botSelfId } }
3821
+ });
3822
+ logger?.info(`${prefix} subscribing to conversationId=${botSelfId}`);
3823
+ ws?.send(subMsg);
3824
+ }
3794
3825
  });
3795
3826
  ws.on("message", (data) => {
3796
3827
  try {
@@ -3801,16 +3832,16 @@ function createMychatWsClient(params) {
3801
3832
  ws.on("close", (code, reason) => {
3802
3833
  connected = false;
3803
3834
  ws = null;
3804
- console.log(`[mychat] WS closed code=${code} reason=${reason?.toString() || "none"}`);
3835
+ logger?.info(`${prefix} closed code=${code} reason=${reason?.toString() || "none"}`);
3805
3836
  scheduleReconnect();
3806
3837
  });
3807
3838
  ws.on("error", (err) => {
3808
3839
  const errMsg = err instanceof Error ? err.message : String(err);
3809
- console.error(`[mychat] WS error: ${errMsg}`);
3840
+ logger?.error(`${prefix} error: ${errMsg}`);
3810
3841
  });
3811
3842
  } catch (err) {
3812
3843
  const errMsg = err instanceof Error ? err.message : String(err);
3813
- console.error(`[mychat] WS connect exception: ${errMsg}`);
3844
+ logger?.error(`${prefix} connect exception: ${errMsg}`);
3814
3845
  scheduleReconnect();
3815
3846
  }
3816
3847
  }
@@ -3838,26 +3869,6 @@ function createMychatWsClient(params) {
3838
3869
  };
3839
3870
  }
3840
3871
  //#endregion
3841
- //#region src/logger.ts
3842
- const PREFIX = "mychat";
3843
- function mychatLogPrefix(feature) {
3844
- return `[${PREFIX}:${feature}]`;
3845
- }
3846
- function getMychatLogger() {
3847
- try {
3848
- const logger = ((init_runtime(), __toCommonJS(runtime_exports)).tryGetMychatRuntime?.())?.logger;
3849
- if (!logger) return null;
3850
- return {
3851
- debug: (m) => logger.debug?.(m),
3852
- info: (m) => logger.info(m),
3853
- warn: (m) => logger.warn(m),
3854
- error: (m) => logger.error(m)
3855
- };
3856
- } catch {
3857
- return null;
3858
- }
3859
- }
3860
- //#endregion
3861
3872
  //#region src/monitor/listeners.ts
3862
3873
  function createMychatMessageListener(params) {
3863
3874
  const { handler } = params;
@@ -3929,8 +3940,7 @@ function createMychatProviderLifecycle(params) {
3929
3940
  return {
3930
3941
  async waitForReady() {
3931
3942
  const start = Date.now();
3932
- console.log(`[mychat:lifecycle] starting timeoutMs=${readyTimeoutMs}`);
3933
- logger?.info(`${prefix} starting connection lifecycle timeoutMs=${readyTimeoutMs}`);
3943
+ logger?.info(`${prefix} starting timeoutMs=${readyTimeoutMs}`);
3934
3944
  wsClient.connect();
3935
3945
  let wsConnectedAt = null;
3936
3946
  let getSelfAttempts = 0;
@@ -3938,29 +3948,24 @@ function createMychatProviderLifecycle(params) {
3938
3948
  if (wsClient.isConnected()) {
3939
3949
  if (!wsConnectedAt) {
3940
3950
  wsConnectedAt = Date.now();
3941
- console.log(`[mychat:lifecycle] ws connected, fetching bot identity elapsedMs=${wsConnectedAt - start}`);
3942
3951
  logger?.info(`${prefix} ws connected, fetching bot identity elapsedMs=${wsConnectedAt - start}`);
3943
3952
  }
3944
3953
  getSelfAttempts++;
3945
3954
  const botSelf = await httpClient.getSelf();
3946
3955
  if (botSelf) {
3947
- console.log(`[mychat:lifecycle] bot connected botId=${botSelf.botId} wsConnectMs=${wsConnectedAt - start} getSelfAttempts=${getSelfAttempts}`);
3948
3956
  logger?.info(`${prefix} bot connected botId=${botSelf.botId} wsConnectMs=${wsConnectedAt - start} getSelfAttempts=${getSelfAttempts}`);
3949
3957
  return botSelf;
3950
3958
  }
3951
- console.warn(`[mychat:lifecycle] getSelf() failed attempt=${getSelfAttempts} elapsedMs=${Date.now() - start}`);
3952
3959
  logger?.warn(`${prefix} getSelf() failed attempt=${getSelfAttempts} elapsedMs=${Date.now() - start}`);
3953
3960
  }
3954
3961
  await new Promise((r) => setTimeout(r, 500));
3955
3962
  }
3956
3963
  const wsConnected = wsClient.isConnected();
3957
3964
  const elapsed = Date.now() - start;
3958
- console.warn(`[mychat:lifecycle] ready timeout wsConnected=${wsConnected} wsConnectedAt=${wsConnectedAt ? wsConnectedAt - start : "never"} getSelfAttempts=${getSelfAttempts} elapsedMs=${elapsed} timeoutMs=${readyTimeoutMs}`);
3959
3965
  logger?.warn(`${prefix} ready timeout wsConnected=${wsConnected} wsConnectedAt=${wsConnectedAt ? wsConnectedAt - start : "never"} getSelfAttempts=${getSelfAttempts} elapsedMs=${elapsed} timeoutMs=${readyTimeoutMs}`);
3960
3966
  return null;
3961
3967
  },
3962
3968
  shutdown() {
3963
- console.log(`[mychat:lifecycle] shutdown`);
3964
3969
  wsClient.disconnect();
3965
3970
  logger?.info(`${prefix} shutdown`);
3966
3971
  }
@@ -4120,7 +4125,6 @@ async function monitorMychatProvider(ctx) {
4120
4125
  const { account, setStatus } = ctx;
4121
4126
  const logger = getMychatLogger();
4122
4127
  const prefix = mychatLogPrefix("provider");
4123
- console.log(`[mychat:provider] starting accountId=${account.accountId}`);
4124
4128
  if (logger) logger.info(`${prefix} starting accountId=${account.accountId}`);
4125
4129
  setStatus({
4126
4130
  connected: false,
@@ -4130,12 +4134,10 @@ async function monitorMychatProvider(ctx) {
4130
4134
  baseUrl: account.baseUrl,
4131
4135
  token: account.token
4132
4136
  });
4133
- console.log(`[mychat:provider] fetching bot identity baseUrl=${account.baseUrl}`);
4134
4137
  if (logger) logger.info(`${prefix} fetching bot identity baseUrl=${account.baseUrl}`);
4135
4138
  const botSelf = await httpClient.getSelf();
4136
4139
  if (!botSelf) {
4137
4140
  const error = `MyChat provider failed to get bot identity (baseUrl=${account.baseUrl}, token=${account.token ? account.token.slice(0, 8) + "..." : "empty"})`;
4138
- console.error(`[mychat:provider] ${error}`);
4139
4141
  if (logger) logger.error(`${prefix} ${error}`);
4140
4142
  setStatus({
4141
4143
  connected: false,
@@ -4143,16 +4145,11 @@ async function monitorMychatProvider(ctx) {
4143
4145
  });
4144
4146
  throw new Error(error);
4145
4147
  }
4146
- console.log(`[mychat:provider] got bot identity botId=${botSelf.botId} name=${botSelf.name ?? "unknown"}`);
4147
4148
  if (logger) logger.info(`${prefix} got bot identity botId=${botSelf.botId} name=${botSelf.name ?? "unknown"}`);
4148
4149
  const health = await httpClient.healthCheck();
4149
4150
  if (!health.ok) {
4150
- console.warn(`[mychat:provider] health check failed latencyMs=${health.latencyMs} baseUrl=${account.baseUrl}`);
4151
4151
  if (logger) logger.warn(`${prefix} health check failed latencyMs=${health.latencyMs} baseUrl=${account.baseUrl}`);
4152
- } else {
4153
- console.log(`[mychat:provider] health check ok latencyMs=${health.latencyMs}`);
4154
- if (logger) logger.info(`${prefix} health check ok latencyMs=${health.latencyMs}`);
4155
- }
4152
+ } else if (logger) logger.info(`${prefix} health check ok latencyMs=${health.latencyMs}`);
4156
4153
  const { wsClient } = createMychatProviderClients(account, botSelf.botId);
4157
4154
  const rt = ctx.runtime;
4158
4155
  rt.mychat = {
@@ -4174,7 +4171,7 @@ async function monitorMychatProvider(ctx) {
4174
4171
  }),
4175
4172
  wsClient
4176
4173
  });
4177
- console.log(`[mychat:provider] starting connection lifecycle`);
4174
+ if (logger) logger.info(`${prefix} starting connection lifecycle`);
4178
4175
  const lifecycle = createMychatProviderLifecycle({
4179
4176
  httpClient,
4180
4177
  wsClient
@@ -4182,7 +4179,6 @@ async function monitorMychatProvider(ctx) {
4182
4179
  const readyBotSelf = await lifecycle.waitForReady();
4183
4180
  if (!readyBotSelf) {
4184
4181
  const error = "MyChat provider failed to connect";
4185
- console.error(`[mychat:provider] ${error}`);
4186
4182
  if (logger) logger.error(`${prefix} ${error}`);
4187
4183
  setStatus({
4188
4184
  connected: false,
@@ -4198,16 +4194,14 @@ async function monitorMychatProvider(ctx) {
4198
4194
  mode: "websocket",
4199
4195
  lastError: null
4200
4196
  });
4201
- console.log(`[mychat:provider] bot connected botId=${readyBotSelf.botId}`);
4202
4197
  if (logger) logger.info(`${prefix} bot connected botId=${readyBotSelf.botId}`);
4203
4198
  ctx.abortSignal?.addEventListener("abort", () => {
4204
- console.log(`[mychat:provider] abort signal received, shutting down`);
4205
4199
  if (logger) logger.info(`${prefix} abort signal received, shutting down`);
4206
4200
  lifecycle.shutdown();
4207
4201
  setStatus({ connected: false });
4208
4202
  });
4209
4203
  return { unsubscribe() {
4210
- console.log(`[mychat:provider] unsubscribe called, shutting down`);
4204
+ if (logger) logger.info(`${prefix} unsubscribe called, shutting down`);
4211
4205
  lifecycle.shutdown();
4212
4206
  setStatus({ connected: false });
4213
4207
  } };
@@ -1,2 +1,2 @@
1
- import { t as mychatPlugin } from "./channel-BTGCJxf9.js";
1
+ import { t as mychatPlugin } from "./channel-BdJp6UKB.js";
2
2
  export { mychatPlugin };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aka_openclaw_plugin/mychat",
3
- "version": "0.1.10",
3
+ "version": "0.1.12",
4
4
  "description": "OpenClaw MyChat channel plugin",
5
5
  "repository": {
6
6
  "type": "git",
package/setup-entry.js CHANGED
@@ -1,2 +1,2 @@
1
- import { t as mychatPlugin } from "./channel-BTGCJxf9.js";
1
+ import { t as mychatPlugin } from "./channel-BdJp6UKB.js";
2
2
  export { mychatPlugin };