@aka_openclaw_plugin/mychat 0.1.11 → 0.1.13

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-Notyh4R-.js";
2
+ import { t as mychatPlugin } from "./channel-BpMr77-3.js";
3
3
  //#region api.ts
4
4
  init_runtime();
5
5
  //#endregion
@@ -97,10 +97,31 @@ 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
- const { baseUrl, token } = params;
122
+ const { baseUrl, token, logger } = 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}`;
106
127
  try {
@@ -116,13 +137,14 @@ function createMychatHttpClient(params) {
116
137
  try {
117
138
  errorBody = await response.text();
118
139
  } catch {}
119
- console.error(`[mychat] HTTP ${method} ${url} failed: ${response.status} ${response.statusText}${errorBody ? ` body=${errorBody.slice(0, 200)}` : ""}`);
140
+ const msg = `${prefix} ${method} ${url} failed: ${response.status} ${response.statusText}${errorBody ? ` body=${errorBody.slice(0, 200)}` : ""}`;
141
+ logger?.error(msg);
120
142
  return null;
121
143
  }
122
144
  return await response.json();
123
145
  } catch (err) {
124
146
  const errMsg = err instanceof Error ? `${err.message}` : String(err);
125
- console.error(`[mychat] HTTP ${method} ${url} error: ${errMsg}`);
147
+ logger?.error(`${prefix} ${method} ${url} error: ${errMsg}`);
126
148
  return null;
127
149
  }
128
150
  }
@@ -176,13 +198,13 @@ function createMychatHttpClient(params) {
176
198
  try {
177
199
  errorBody = await response.text();
178
200
  } catch {}
179
- console.error(`[mychat] HTTP POST ${url} failed: ${response.status} ${response.statusText}${errorBody ? ` body=${errorBody.slice(0, 200)}` : ""}`);
201
+ logger?.error(`${prefix} POST ${url} failed: ${response.status} ${response.statusText}${errorBody ? ` body=${errorBody.slice(0, 200)}` : ""}`);
180
202
  return null;
181
203
  }
182
204
  return await response.json();
183
205
  } catch (err) {
184
206
  const errMsg = err instanceof Error ? `${err.message}` : String(err);
185
- console.error(`[mychat] HTTP POST ${url} error: ${errMsg}`);
207
+ logger?.error(`${prefix} POST ${url} error: ${errMsg}`);
186
208
  return null;
187
209
  }
188
210
  },
@@ -192,14 +214,14 @@ function createMychatHttpClient(params) {
192
214
  try {
193
215
  const response = await fetch(url);
194
216
  const latencyMs = Date.now() - start;
195
- if (!response.ok) console.error(`[mychat] Health check failed: ${url} status=${response.status} latencyMs=${latencyMs}`);
217
+ if (!response.ok) logger?.error(`${prefix} health check failed: ${url} status=${response.status} latencyMs=${latencyMs}`);
196
218
  return {
197
219
  ok: response.ok,
198
220
  latencyMs
199
221
  };
200
222
  } catch (err) {
201
223
  const errMsg = err instanceof Error ? `${err.message}` : String(err);
202
- console.error(`[mychat] Health check error: ${url} error=${errMsg} latencyMs=${Date.now() - start}`);
224
+ logger?.error(`${prefix} health check error: ${url} error=${errMsg} latencyMs=${Date.now() - start}`);
203
225
  return {
204
226
  ok: false,
205
227
  latencyMs: Date.now() - start
@@ -3752,10 +3774,11 @@ const DEFAULT_INITIAL_DELAY_MS = 1e3;
3752
3774
  const DEFAULT_MAX_DELAY_MS = 3e4;
3753
3775
  const DEFAULT_BACKOFF_MULTIPLIER = 2;
3754
3776
  function createMychatWsClient(params) {
3755
- const { wsUrl, token, botSelfId, reconnect } = params;
3777
+ const { wsUrl, token, botSelfId, logger, reconnect } = params;
3756
3778
  const initialDelay = reconnect?.initialDelayMs ?? DEFAULT_INITIAL_DELAY_MS;
3757
3779
  const maxDelay = reconnect?.maxDelayMs ?? DEFAULT_MAX_DELAY_MS;
3758
3780
  const backoffMultiplier = reconnect?.backoffMultiplier ?? DEFAULT_BACKOFF_MULTIPLIER;
3781
+ const prefix = mychatLogPrefix("ws");
3759
3782
  let ws = null;
3760
3783
  let connected = false;
3761
3784
  let reconnectTimer = null;
@@ -3770,7 +3793,7 @@ function createMychatWsClient(params) {
3770
3793
  }
3771
3794
  function scheduleReconnect() {
3772
3795
  if (reconnectTimer) return;
3773
- console.log(`[mychat] WS will reconnect in ${currentDelay}ms`);
3796
+ logger?.info(`${prefix} will reconnect in ${currentDelay}ms`);
3774
3797
  reconnectTimer = setTimeout(() => {
3775
3798
  reconnectTimer = null;
3776
3799
  currentDelay = Math.min(currentDelay * backoffMultiplier, maxDelay);
@@ -3781,18 +3804,18 @@ function createMychatWsClient(params) {
3781
3804
  if (ws) return;
3782
3805
  try {
3783
3806
  const url = buildUrl();
3784
- console.log(`[mychat] WS connecting to ${url}`);
3807
+ logger?.info(`${prefix} connecting to ${url}`);
3785
3808
  ws = new wrapper_default(url, { headers: { Authorization: `Bearer ${token}` } });
3786
3809
  ws.on("open", () => {
3787
3810
  connected = true;
3788
3811
  currentDelay = initialDelay;
3789
- console.log(`[mychat] WS open event fired, connected=true botSelfId=${botSelfId ?? "none"}`);
3812
+ logger?.info(`${prefix} connected botSelfId=${botSelfId ?? "none"}`);
3790
3813
  if (botSelfId) {
3791
3814
  const subMsg = JSON.stringify({
3792
3815
  type: "subscribe",
3793
3816
  body: { subscribe: { conversationId: botSelfId } }
3794
3817
  });
3795
- console.log(`[mychat] WS subscribing to conversationId=${botSelfId}`);
3818
+ logger?.info(`${prefix} subscribing to conversationId=${botSelfId}`);
3796
3819
  ws?.send(subMsg);
3797
3820
  }
3798
3821
  });
@@ -3805,16 +3828,16 @@ function createMychatWsClient(params) {
3805
3828
  ws.on("close", (code, reason) => {
3806
3829
  connected = false;
3807
3830
  ws = null;
3808
- console.log(`[mychat] WS closed code=${code} reason=${reason?.toString() || "none"}`);
3831
+ logger?.info(`${prefix} closed code=${code} reason=${reason?.toString() || "none"}`);
3809
3832
  scheduleReconnect();
3810
3833
  });
3811
3834
  ws.on("error", (err) => {
3812
3835
  const errMsg = err instanceof Error ? err.message : String(err);
3813
- console.error(`[mychat] WS error: ${errMsg}`);
3836
+ logger?.error(`${prefix} error: ${errMsg}`);
3814
3837
  });
3815
3838
  } catch (err) {
3816
3839
  const errMsg = err instanceof Error ? err.message : String(err);
3817
- console.error(`[mychat] WS connect exception: ${errMsg}`);
3840
+ logger?.error(`${prefix} connect exception: ${errMsg}`);
3818
3841
  scheduleReconnect();
3819
3842
  }
3820
3843
  }
@@ -3837,33 +3860,11 @@ function createMychatWsClient(params) {
3837
3860
  };
3838
3861
  },
3839
3862
  isConnected() {
3840
- const state = connected;
3841
- console.log(`[mychat] WS isConnected() called, returning ${state}`);
3842
- return state;
3863
+ return connected;
3843
3864
  }
3844
3865
  };
3845
3866
  }
3846
3867
  //#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
3868
  //#region src/monitor/listeners.ts
3868
3869
  function createMychatMessageListener(params) {
3869
3870
  const { handler } = params;
@@ -3893,11 +3894,12 @@ function createMychatTypingListener(params) {
3893
3894
  }
3894
3895
  //#endregion
3895
3896
  //#region src/monitor/provider.startup.ts
3896
- function createMychatProviderClients(account, botSelfId) {
3897
+ function createMychatProviderClients(account, botSelfId, logger) {
3897
3898
  return { wsClient: createMychatWsClient({
3898
3899
  wsUrl: account.wsUrl,
3899
3900
  token: account.token,
3900
3901
  botSelfId,
3902
+ logger,
3901
3903
  reconnect: account.reconnect
3902
3904
  }) };
3903
3905
  }
@@ -3929,14 +3931,12 @@ function registerMychatMonitorListeners(params) {
3929
3931
  //#region src/monitor/provider.lifecycle.ts
3930
3932
  const DEFAULT_READY_TIMEOUT_MS = 15e3;
3931
3933
  function createMychatProviderLifecycle(params) {
3932
- const { httpClient, wsClient, readyTimeoutMs = DEFAULT_READY_TIMEOUT_MS } = params;
3933
- const logger = getMychatLogger();
3934
+ const { httpClient, wsClient, readyTimeoutMs = DEFAULT_READY_TIMEOUT_MS, logger } = params;
3934
3935
  const prefix = mychatLogPrefix("lifecycle");
3935
3936
  return {
3936
3937
  async waitForReady() {
3937
3938
  const start = Date.now();
3938
- console.log(`[mychat:lifecycle] starting timeoutMs=${readyTimeoutMs}`);
3939
- logger?.info(`${prefix} starting connection lifecycle timeoutMs=${readyTimeoutMs}`);
3939
+ logger?.info(`${prefix} starting timeoutMs=${readyTimeoutMs}`);
3940
3940
  wsClient.connect();
3941
3941
  let wsConnectedAt = null;
3942
3942
  let getSelfAttempts = 0;
@@ -3944,29 +3944,24 @@ function createMychatProviderLifecycle(params) {
3944
3944
  if (wsClient.isConnected()) {
3945
3945
  if (!wsConnectedAt) {
3946
3946
  wsConnectedAt = Date.now();
3947
- console.log(`[mychat:lifecycle] ws connected, fetching bot identity elapsedMs=${wsConnectedAt - start}`);
3948
3947
  logger?.info(`${prefix} ws connected, fetching bot identity elapsedMs=${wsConnectedAt - start}`);
3949
3948
  }
3950
3949
  getSelfAttempts++;
3951
3950
  const botSelf = await httpClient.getSelf();
3952
3951
  if (botSelf) {
3953
- console.log(`[mychat:lifecycle] bot connected botId=${botSelf.botId} wsConnectMs=${wsConnectedAt - start} getSelfAttempts=${getSelfAttempts}`);
3954
3952
  logger?.info(`${prefix} bot connected botId=${botSelf.botId} wsConnectMs=${wsConnectedAt - start} getSelfAttempts=${getSelfAttempts}`);
3955
3953
  return botSelf;
3956
3954
  }
3957
- console.warn(`[mychat:lifecycle] getSelf() failed attempt=${getSelfAttempts} elapsedMs=${Date.now() - start}`);
3958
3955
  logger?.warn(`${prefix} getSelf() failed attempt=${getSelfAttempts} elapsedMs=${Date.now() - start}`);
3959
3956
  }
3960
3957
  await new Promise((r) => setTimeout(r, 500));
3961
3958
  }
3962
3959
  const wsConnected = wsClient.isConnected();
3963
3960
  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
3961
  logger?.warn(`${prefix} ready timeout wsConnected=${wsConnected} wsConnectedAt=${wsConnectedAt ? wsConnectedAt - start : "never"} getSelfAttempts=${getSelfAttempts} elapsedMs=${elapsed} timeoutMs=${readyTimeoutMs}`);
3966
3962
  return null;
3967
3963
  },
3968
3964
  shutdown() {
3969
- console.log(`[mychat:lifecycle] shutdown`);
3970
3965
  wsClient.disconnect();
3971
3966
  logger?.info(`${prefix} shutdown`);
3972
3967
  }
@@ -4123,43 +4118,34 @@ function createMychatMessageHandler(params) {
4123
4118
  //#endregion
4124
4119
  //#region src/monitor/provider.ts
4125
4120
  async function monitorMychatProvider(ctx) {
4126
- const { account, setStatus } = ctx;
4127
- const logger = getMychatLogger();
4121
+ const { account, setStatus, log: logger } = ctx;
4128
4122
  const prefix = mychatLogPrefix("provider");
4129
- console.log(`[mychat:provider] starting accountId=${account.accountId}`);
4130
- if (logger) logger.info(`${prefix} starting accountId=${account.accountId}`);
4123
+ logger?.info(`${prefix} starting accountId=${account.accountId}`);
4131
4124
  setStatus({
4132
4125
  connected: false,
4133
4126
  mode: "websocket"
4134
4127
  });
4135
4128
  const httpClient = createMychatHttpClient({
4136
4129
  baseUrl: account.baseUrl,
4137
- token: account.token
4130
+ token: account.token,
4131
+ logger
4138
4132
  });
4139
- console.log(`[mychat:provider] fetching bot identity baseUrl=${account.baseUrl}`);
4140
- if (logger) logger.info(`${prefix} fetching bot identity baseUrl=${account.baseUrl}`);
4133
+ logger?.info(`${prefix} fetching bot identity baseUrl=${account.baseUrl}`);
4141
4134
  const botSelf = await httpClient.getSelf();
4142
4135
  if (!botSelf) {
4143
4136
  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
- if (logger) logger.error(`${prefix} ${error}`);
4137
+ logger?.error(`${prefix} ${error}`);
4146
4138
  setStatus({
4147
4139
  connected: false,
4148
4140
  lastError: error
4149
4141
  });
4150
4142
  throw new Error(error);
4151
4143
  }
4152
- console.log(`[mychat:provider] got bot identity botId=${botSelf.botId} name=${botSelf.name ?? "unknown"}`);
4153
- if (logger) logger.info(`${prefix} got bot identity botId=${botSelf.botId} name=${botSelf.name ?? "unknown"}`);
4144
+ logger?.info(`${prefix} got bot identity botId=${botSelf.botId} name=${botSelf.name ?? "unknown"}`);
4154
4145
  const health = await httpClient.healthCheck();
4155
- if (!health.ok) {
4156
- console.warn(`[mychat:provider] health check failed latencyMs=${health.latencyMs} baseUrl=${account.baseUrl}`);
4157
- 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
- }
4162
- const { wsClient } = createMychatProviderClients(account, botSelf.botId);
4146
+ if (!health.ok) logger?.warn(`${prefix} health check failed latencyMs=${health.latencyMs} baseUrl=${account.baseUrl}`);
4147
+ else logger?.info(`${prefix} health check ok latencyMs=${health.latencyMs}`);
4148
+ const { wsClient } = createMychatProviderClients(account, botSelf.botId, logger);
4163
4149
  const rt = ctx.runtime;
4164
4150
  rt.mychat = {
4165
4151
  accountId: account.accountId,
@@ -4180,16 +4166,16 @@ async function monitorMychatProvider(ctx) {
4180
4166
  }),
4181
4167
  wsClient
4182
4168
  });
4183
- console.log(`[mychat:provider] starting connection lifecycle`);
4169
+ logger?.info(`${prefix} starting connection lifecycle`);
4184
4170
  const lifecycle = createMychatProviderLifecycle({
4185
4171
  httpClient,
4186
- wsClient
4172
+ wsClient,
4173
+ logger
4187
4174
  });
4188
4175
  const readyBotSelf = await lifecycle.waitForReady();
4189
4176
  if (!readyBotSelf) {
4190
4177
  const error = "MyChat provider failed to connect";
4191
- console.error(`[mychat:provider] ${error}`);
4192
- if (logger) logger.error(`${prefix} ${error}`);
4178
+ logger?.error(`${prefix} ${error}`);
4193
4179
  setStatus({
4194
4180
  connected: false,
4195
4181
  lastError: error
@@ -4204,16 +4190,14 @@ async function monitorMychatProvider(ctx) {
4204
4190
  mode: "websocket",
4205
4191
  lastError: null
4206
4192
  });
4207
- console.log(`[mychat:provider] bot connected botId=${readyBotSelf.botId}`);
4208
- if (logger) logger.info(`${prefix} bot connected botId=${readyBotSelf.botId}`);
4193
+ logger?.info(`${prefix} bot connected botId=${readyBotSelf.botId}`);
4209
4194
  ctx.abortSignal?.addEventListener("abort", () => {
4210
- console.log(`[mychat:provider] abort signal received, shutting down`);
4211
- if (logger) logger.info(`${prefix} abort signal received, shutting down`);
4195
+ logger?.info(`${prefix} abort signal received, shutting down`);
4212
4196
  lifecycle.shutdown();
4213
4197
  setStatus({ connected: false });
4214
4198
  });
4215
4199
  return { unsubscribe() {
4216
- console.log(`[mychat:provider] unsubscribe called, shutting down`);
4200
+ logger?.info(`${prefix} unsubscribe called, shutting down`);
4217
4201
  lifecycle.shutdown();
4218
4202
  setStatus({ connected: false });
4219
4203
  } };
@@ -1,2 +1,2 @@
1
- import { t as mychatPlugin } from "./channel-Notyh4R-.js";
1
+ import { t as mychatPlugin } from "./channel-BpMr77-3.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.11",
3
+ "version": "0.1.13",
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-Notyh4R-.js";
1
+ import { t as mychatPlugin } from "./channel-BpMr77-3.js";
2
2
  export { mychatPlugin };