@alfe.ai/openclaw-metrics 0.0.6 → 0.0.8

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/dist/plugin.cjs CHANGED
@@ -1,44 +1,5 @@
1
1
  let _alfe_ai_config = require("@alfe.ai/config");
2
2
  let _alfe_ai_agent_api_client = require("@alfe.ai/agent-api-client");
3
- //#region src/session-keys.ts
4
- /** Known channel segments in canonical session keys. */
5
- const KNOWN_CHANNELS = new Set([
6
- "alfe",
7
- "discord",
8
- "slack",
9
- "telegram",
10
- "whatsapp",
11
- "signal",
12
- "irc",
13
- "googlechat",
14
- "msteams",
15
- "imessage",
16
- "feishu"
17
- ]);
18
- /**
19
- * Parse a session key for userId and channel.
20
- *
21
- * Returns empty strings for fields that cannot be determined.
22
- */
23
- function parseSessionKey(key) {
24
- const canonicalMatch = /^agent:[^:]+:([^:]+):(?:default:)?direct:([^:]+)/.exec(key);
25
- if (canonicalMatch) {
26
- const [, channel, userId] = canonicalMatch;
27
- return {
28
- userId,
29
- channel: KNOWN_CHANNELS.has(channel) ? channel : "unknown"
30
- };
31
- }
32
- if (key.includes("chat-")) return {
33
- userId: "",
34
- channel: "alfe"
35
- };
36
- return {
37
- userId: "",
38
- channel: "unknown"
39
- };
40
- }
41
- //#endregion
42
3
  //#region src/plugin.ts
43
4
  /**
44
5
  * @alfe.ai/openclaw-metrics — OpenClaw metrics plugin for Alfe.
@@ -56,6 +17,15 @@ const plugin = {
56
17
  id: "@alfe.ai/openclaw-metrics",
57
18
  activate(api) {
58
19
  const log = api.logger;
20
+ if (!api.runtime) {
21
+ log.debug("Management command context — skipping metrics init");
22
+ return;
23
+ }
24
+ if (globalThis.__alfeMetricsPluginActivated === true) {
25
+ log.debug("Alfe Metrics plugin already activated — skipping duplicate");
26
+ return;
27
+ }
28
+ globalThis.__alfeMetricsPluginActivated = true;
59
29
  log.info("Alfe Metrics plugin activating...");
60
30
  try {
61
31
  const cfg = (0, _alfe_ai_config.resolveConfig)();
@@ -67,16 +37,25 @@ const plugin = {
67
37
  } catch (err) {
68
38
  log.warn(`Failed to resolve Alfe config — metrics will not be reported: ${err instanceof Error ? err.message : String(err)}`);
69
39
  }
70
- api.on("message", (...eventArgs) => {
40
+ api.on("message_received", (...eventArgs) => {
41
+ if (!client) return;
42
+ const event = eventArgs[0];
43
+ const ctx = eventArgs[1];
44
+ if (!event.from) return;
45
+ client.recordActivity({
46
+ userId: event.from,
47
+ channel: ctx.channelId,
48
+ role: "user"
49
+ }).catch((err) => {
50
+ log.debug(`Metrics report failed: ${err instanceof Error ? err.message : String(err)}`);
51
+ });
52
+ });
53
+ api.on("message_sending", (...eventArgs) => {
71
54
  if (!client) return;
72
- const { sessionKey, role } = eventArgs[0];
73
- if (!sessionKey) return;
74
- const parsed = parseSessionKey(sessionKey);
75
- if (!parsed.userId) return;
55
+ const ctx = eventArgs[1];
76
56
  client.recordActivity({
77
- userId: parsed.userId,
78
- channel: parsed.channel,
79
- role
57
+ channel: ctx?.channelId ?? "unknown",
58
+ role: "assistant"
80
59
  }).catch((err) => {
81
60
  log.debug(`Metrics report failed: ${err instanceof Error ? err.message : String(err)}`);
82
61
  });
@@ -86,6 +65,7 @@ const plugin = {
86
65
  deactivate(api) {
87
66
  const log = api.logger;
88
67
  client = null;
68
+ globalThis.__alfeMetricsPluginActivated = false;
89
69
  log.info("Alfe Metrics plugin deactivated");
90
70
  }
91
71
  };
package/dist/plugin.d.cts CHANGED
@@ -18,6 +18,7 @@ interface PluginLogger {
18
18
  }
19
19
  interface PluginApi {
20
20
  logger: PluginLogger;
21
+ runtime?: unknown;
21
22
  on(event: string, handler: (...args: unknown[]) => void | Promise<void>): void;
22
23
  }
23
24
  declare const plugin: {
package/dist/plugin.d.ts CHANGED
@@ -18,6 +18,7 @@ interface PluginLogger {
18
18
  }
19
19
  interface PluginApi {
20
20
  logger: PluginLogger;
21
+ runtime?: unknown;
21
22
  on(event: string, handler: (...args: unknown[]) => void | Promise<void>): void;
22
23
  }
23
24
  declare const plugin: {
package/dist/plugin.js CHANGED
@@ -1,44 +1,5 @@
1
1
  import { resolveConfig } from "@alfe.ai/config";
2
2
  import { AgentApiClient } from "@alfe.ai/agent-api-client";
3
- //#region src/session-keys.ts
4
- /** Known channel segments in canonical session keys. */
5
- const KNOWN_CHANNELS = new Set([
6
- "alfe",
7
- "discord",
8
- "slack",
9
- "telegram",
10
- "whatsapp",
11
- "signal",
12
- "irc",
13
- "googlechat",
14
- "msteams",
15
- "imessage",
16
- "feishu"
17
- ]);
18
- /**
19
- * Parse a session key for userId and channel.
20
- *
21
- * Returns empty strings for fields that cannot be determined.
22
- */
23
- function parseSessionKey(key) {
24
- const canonicalMatch = /^agent:[^:]+:([^:]+):(?:default:)?direct:([^:]+)/.exec(key);
25
- if (canonicalMatch) {
26
- const [, channel, userId] = canonicalMatch;
27
- return {
28
- userId,
29
- channel: KNOWN_CHANNELS.has(channel) ? channel : "unknown"
30
- };
31
- }
32
- if (key.includes("chat-")) return {
33
- userId: "",
34
- channel: "alfe"
35
- };
36
- return {
37
- userId: "",
38
- channel: "unknown"
39
- };
40
- }
41
- //#endregion
42
3
  //#region src/plugin.ts
43
4
  /**
44
5
  * @alfe.ai/openclaw-metrics — OpenClaw metrics plugin for Alfe.
@@ -56,6 +17,15 @@ const plugin = {
56
17
  id: "@alfe.ai/openclaw-metrics",
57
18
  activate(api) {
58
19
  const log = api.logger;
20
+ if (!api.runtime) {
21
+ log.debug("Management command context — skipping metrics init");
22
+ return;
23
+ }
24
+ if (globalThis.__alfeMetricsPluginActivated === true) {
25
+ log.debug("Alfe Metrics plugin already activated — skipping duplicate");
26
+ return;
27
+ }
28
+ globalThis.__alfeMetricsPluginActivated = true;
59
29
  log.info("Alfe Metrics plugin activating...");
60
30
  try {
61
31
  const cfg = resolveConfig();
@@ -67,16 +37,25 @@ const plugin = {
67
37
  } catch (err) {
68
38
  log.warn(`Failed to resolve Alfe config — metrics will not be reported: ${err instanceof Error ? err.message : String(err)}`);
69
39
  }
70
- api.on("message", (...eventArgs) => {
40
+ api.on("message_received", (...eventArgs) => {
41
+ if (!client) return;
42
+ const event = eventArgs[0];
43
+ const ctx = eventArgs[1];
44
+ if (!event.from) return;
45
+ client.recordActivity({
46
+ userId: event.from,
47
+ channel: ctx.channelId,
48
+ role: "user"
49
+ }).catch((err) => {
50
+ log.debug(`Metrics report failed: ${err instanceof Error ? err.message : String(err)}`);
51
+ });
52
+ });
53
+ api.on("message_sending", (...eventArgs) => {
71
54
  if (!client) return;
72
- const { sessionKey, role } = eventArgs[0];
73
- if (!sessionKey) return;
74
- const parsed = parseSessionKey(sessionKey);
75
- if (!parsed.userId) return;
55
+ const ctx = eventArgs[1];
76
56
  client.recordActivity({
77
- userId: parsed.userId,
78
- channel: parsed.channel,
79
- role
57
+ channel: ctx?.channelId ?? "unknown",
58
+ role: "assistant"
80
59
  }).catch((err) => {
81
60
  log.debug(`Metrics report failed: ${err instanceof Error ? err.message : String(err)}`);
82
61
  });
@@ -86,6 +65,7 @@ const plugin = {
86
65
  deactivate(api) {
87
66
  const log = api.logger;
88
67
  client = null;
68
+ globalThis.__alfeMetricsPluginActivated = false;
89
69
  log.info("Alfe Metrics plugin deactivated");
90
70
  }
91
71
  };
@@ -1,5 +1,9 @@
1
1
  {
2
2
  "id": "@alfe.ai/openclaw-metrics",
3
+ "name": "Alfe Metrics",
4
+ "version": "0.0.7",
5
+ "description": "Per-user activity tracking across all channels",
6
+ "entry": "./dist/plugin.js",
3
7
  "configSchema": {
4
8
  "type": "object",
5
9
  "additionalProperties": false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alfe.ai/openclaw-metrics",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "OpenClaw metrics plugin for Alfe — per-user activity tracking",
5
5
  "type": "module",
6
6
  "main": "./dist/plugin.js",
@@ -23,7 +23,7 @@
23
23
  ],
24
24
  "dependencies": {
25
25
  "@alfe.ai/config": "^0.0.7",
26
- "@alfe.ai/agent-api-client": "^0.0.6"
26
+ "@alfe.ai/agent-api-client": "^0.0.7"
27
27
  },
28
28
  "peerDependencies": {
29
29
  "openclaw": ">=2026.3.0"