@cfio/cohort-sync 0.10.1 → 0.10.2

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/index.js CHANGED
@@ -13381,7 +13381,7 @@ function dumpCtx(ctx) {
13381
13381
  function dumpEvent(event) {
13382
13382
  return dumpCtx(event);
13383
13383
  }
13384
- var PLUGIN_VERSION = true ? "0.10.1" : "unknown";
13384
+ var PLUGIN_VERSION = true ? "0.10.2" : "unknown";
13385
13385
  var _gatewayStartHandler = null;
13386
13386
  async function handleGatewayStart(event) {
13387
13387
  if (_gatewayStartHandler) {
@@ -13779,8 +13779,9 @@ function registerHooks(api, cfg) {
13779
13779
  }, KEEPALIVE_INTERVAL_MS);
13780
13780
  logger.info(`cohort-sync: keepalive interval started (${KEEPALIVE_INTERVAL_MS / 1e3}s)`);
13781
13781
  };
13782
- api.on("agent_end", async (event, ctx) => {
13783
- logger.debug("cohort-sync: hook: agent_end", { ctx: dumpCtx(ctx), success: event.success, error: event.error, durationMs: event.durationMs });
13782
+ api.registerHook("agent_end", async (event) => {
13783
+ const ctx = event.context ?? {};
13784
+ logger.debug("cohort-sync: hook: agent_end", { ctx: dumpCtx(ctx), success: ctx.success, error: ctx.error, durationMs: ctx.durationMs });
13784
13785
  const agentId = ctx.agentId ?? "main";
13785
13786
  const agentName = resolveAgentName(agentId);
13786
13787
  try {
@@ -13806,11 +13807,11 @@ function registerHooks(api, cfg) {
13806
13807
  logger.debug("cohort-sync: cron agent end push failed", { error: String(err) });
13807
13808
  }
13808
13809
  }
13809
- if (event.success === false) {
13810
+ if (ctx.success === false) {
13810
13811
  const entry = buildActivityEntry(agentName, "agent_end", {
13811
13812
  success: false,
13812
- error: event.error,
13813
- durationMs: event.durationMs,
13813
+ error: ctx.error,
13814
+ durationMs: ctx.durationMs,
13814
13815
  sessionKey: ctx.sessionKey
13815
13816
  });
13816
13817
  if (entry) activityBatch.add(entry);
@@ -13818,11 +13819,12 @@ function registerHooks(api, cfg) {
13818
13819
  } catch (err) {
13819
13820
  logger.warn(`cohort-sync: agent_end sync failed: ${String(err)}`);
13820
13821
  }
13821
- });
13822
- api.on("llm_output", async (event, ctx) => {
13823
- const usage = event.usage ?? {};
13822
+ }, { name: "cohort-sync.agent_end", description: "Mark idle, push telemetry, emit activity on failure" });
13823
+ api.registerHook("llm_output", async (event) => {
13824
+ const ctx = event.context ?? {};
13825
+ const usage = ctx.usage ?? {};
13824
13826
  const contextTokens = (usage.input ?? 0) + (usage.cacheRead ?? 0) + (usage.cacheWrite ?? 0);
13825
- const model = event.model ?? resolveModel(ctx.agentId ?? "main");
13827
+ const model = ctx.model ?? resolveModel(ctx.agentId ?? "main");
13826
13828
  const contextLimit = getModelContextLimit(model);
13827
13829
  logger.debug("cohort-sync: hook: llm_output", {
13828
13830
  ctx: dumpCtx(ctx),
@@ -13867,14 +13869,15 @@ function registerHooks(api, cfg) {
13867
13869
  } catch (err) {
13868
13870
  logger.warn(`cohort-sync: llm_output telemetry failed: ${String(err)}`);
13869
13871
  }
13870
- });
13871
- api.on("after_compaction", async (event, ctx) => {
13872
- logger.debug("cohort-sync: hook: after_compaction", { ctx: dumpCtx(ctx), messageCount: event.messageCount, tokenCount: event.tokenCount });
13872
+ }, { name: "cohort-sync.llm_output", description: "Accumulate token usage, push telemetry and sessions" });
13873
+ api.registerHook("after_compaction", async (event) => {
13874
+ const ctx = event.context ?? {};
13875
+ logger.debug("cohort-sync: hook: after_compaction", { ctx: dumpCtx(ctx), messageCount: ctx.messageCount, tokenCount: ctx.tokenCount });
13873
13876
  const agentId = ctx.agentId ?? "main";
13874
13877
  const agentName = resolveAgentName(agentId);
13875
13878
  try {
13876
13879
  tracker2.updateFromCompaction(agentName, {
13877
- contextTokens: event.tokenCount ?? 0,
13880
+ contextTokens: ctx.tokenCount ?? 0,
13878
13881
  contextLimit: getModelContextLimit(resolveModel(agentId))
13879
13882
  });
13880
13883
  if (tracker2.shouldPushTelemetry(agentName)) {
@@ -13885,17 +13888,18 @@ function registerHooks(api, cfg) {
13885
13888
  }
13886
13889
  }
13887
13890
  const entry = buildActivityEntry(agentName, "after_compaction", {
13888
- messageCount: event.messageCount,
13889
- compactedCount: event.compactedCount,
13891
+ messageCount: ctx.messageCount,
13892
+ compactedCount: ctx.compactedCount,
13890
13893
  sessionKey: ctx.sessionKey
13891
13894
  });
13892
13895
  if (entry) activityBatch.add(entry);
13893
13896
  } catch (err) {
13894
13897
  logger.warn(`cohort-sync: after_compaction telemetry failed: ${String(err)}`);
13895
13898
  }
13896
- });
13897
- api.on("before_agent_start", async (_event, ctx) => {
13898
- logger.debug("cohort-sync: hook: before_agent_start", { ctx: dumpCtx(ctx), event: dumpEvent(_event) });
13899
+ }, { name: "cohort-sync.after_compaction", description: "Increment compaction count, push telemetry" });
13900
+ api.registerHook("before_agent_start", async (event) => {
13901
+ const ctx = event.context ?? {};
13902
+ logger.debug("cohort-sync: hook: before_agent_start", { ctx: dumpCtx(ctx), event: dumpEvent(event) });
13899
13903
  const agentId = ctx.agentId ?? "main";
13900
13904
  const agentName = resolveAgentName(agentId);
13901
13905
  logger.debug("cohort-sync: hook: before_agent_start resolved", { agentId, agentName, ctxChannelId: ctx.channelId, ctxMessageProvider: ctx.messageProvider, ctxSessionKey: ctx.sessionKey, ctxAccountId: ctx.accountId });
@@ -13945,8 +13949,9 @@ function registerHooks(api, cfg) {
13945
13949
  } catch (err) {
13946
13950
  logger.warn(`cohort-sync: before_agent_start telemetry failed: ${String(err)}`);
13947
13951
  }
13948
- });
13949
- api.on("session_start", async (event, ctx) => {
13952
+ }, { name: "cohort-sync.before_agent_start", description: "Mark working, infer sessions, populate channel bridge" });
13953
+ api.registerHook("session_start", async (event) => {
13954
+ const ctx = event.context ?? {};
13950
13955
  logger.debug("cohort-sync: hook: session_start", { ctx: dumpCtx(ctx), event: dumpEvent(event) });
13951
13956
  const agentId = ctx.agentId ?? "main";
13952
13957
  const agentName = resolveAgentName(agentId);
@@ -13962,14 +13967,15 @@ function registerHooks(api, cfg) {
13962
13967
  const entry = buildActivityEntry(agentName, "session_start", {
13963
13968
  channel: parsed.channel,
13964
13969
  sessionKey,
13965
- resumedFrom: event.resumedFrom
13970
+ resumedFrom: ctx.resumedFrom
13966
13971
  });
13967
13972
  if (entry) activityBatch.add(entry);
13968
13973
  } catch (err) {
13969
13974
  logger.warn(`cohort-sync: session_start tracking failed: ${String(err)}`);
13970
13975
  }
13971
- });
13972
- api.on("session_end", async (event, ctx) => {
13976
+ }, { name: "cohort-sync.session_start", description: "Track session start, push sessions" });
13977
+ api.registerHook("session_end", async (event) => {
13978
+ const ctx = event.context ?? {};
13973
13979
  logger.debug("cohort-sync: hook: session_end", { ctx: dumpCtx(ctx), event: dumpEvent(event) });
13974
13980
  const agentId = ctx.agentId ?? "main";
13975
13981
  const agentName = resolveAgentName(agentId);
@@ -13983,23 +13989,24 @@ function registerHooks(api, cfg) {
13983
13989
  }
13984
13990
  const entry = buildActivityEntry(agentName, "session_end", {
13985
13991
  sessionKey,
13986
- messageCount: event.messageCount,
13987
- durationMs: event.durationMs
13992
+ messageCount: ctx.messageCount,
13993
+ durationMs: ctx.durationMs
13988
13994
  });
13989
13995
  if (entry) activityBatch.add(entry);
13990
13996
  } catch (err) {
13991
13997
  logger.warn(`cohort-sync: session_end tracking failed: ${String(err)}`);
13992
13998
  }
13993
- });
13994
- api.on("after_tool_call", async (event, ctx) => {
13995
- logger.debug("cohort-sync: hook: after_tool_call", { ctx: dumpCtx(ctx), toolName: event.toolName, error: event.error });
13999
+ }, { name: "cohort-sync.session_end", description: "Remove session, push sessions" });
14000
+ api.registerHook("after_tool_call", async (event) => {
14001
+ const ctx = event.context ?? {};
14002
+ logger.debug("cohort-sync: hook: after_tool_call", { ctx: dumpCtx(ctx), toolName: ctx.toolName, error: ctx.error });
13996
14003
  const agentName = resolveAgentFromContext(ctx);
13997
14004
  try {
13998
14005
  const entry = buildActivityEntry(agentName, "after_tool_call", {
13999
- toolName: event.toolName,
14000
- params: event.params,
14001
- error: event.error,
14002
- durationMs: event.durationMs,
14006
+ toolName: ctx.toolName,
14007
+ params: ctx.params,
14008
+ error: ctx.error,
14009
+ durationMs: ctx.durationMs,
14003
14010
  sessionKey: ctx.sessionKey,
14004
14011
  model: resolveModel(ctx.agentId ?? "main")
14005
14012
  });
@@ -14007,11 +14014,12 @@ function registerHooks(api, cfg) {
14007
14014
  } catch (err) {
14008
14015
  logger.warn(`cohort-sync: after_tool_call activity failed: ${String(err)}`);
14009
14016
  }
14010
- });
14011
- api.on("message_received", async (_event, ctx) => {
14017
+ }, { name: "cohort-sync.after_tool_call", description: "Emit activity entry for tool calls" });
14018
+ api.registerHook("message_received", async (event) => {
14019
+ const ctx = event.context ?? {};
14012
14020
  logger.debug("cohort-sync: hook: message_received raw", {
14013
14021
  ctx: dumpCtx(ctx),
14014
- event: dumpEvent(_event),
14022
+ event: dumpEvent(event),
14015
14023
  bridgeStateBefore: Object.fromEntries(getChannelAgentBridge())
14016
14024
  });
14017
14025
  const agentName = resolveAgentFromContext(ctx);
@@ -14021,7 +14029,7 @@ function registerHooks(api, cfg) {
14021
14029
  channel,
14022
14030
  accountId: ctx.accountId,
14023
14031
  conversationId: ctx.conversationId,
14024
- from: _event.from
14032
+ from: ctx.from
14025
14033
  });
14026
14034
  try {
14027
14035
  const entry = buildActivityEntry(agentName, "message_received", {
@@ -14031,8 +14039,9 @@ function registerHooks(api, cfg) {
14031
14039
  } catch (err) {
14032
14040
  logger.warn(`cohort-sync: message_received activity failed: ${String(err)}`);
14033
14041
  }
14034
- });
14035
- api.on("message_sent", async (event, ctx) => {
14042
+ }, { name: "cohort-sync.message_received", description: "Emit activity entry for received messages" });
14043
+ api.registerHook("message_sent", async (event) => {
14044
+ const ctx = event.context ?? {};
14036
14045
  logger.debug("cohort-sync: hook: message_sent raw", {
14037
14046
  ctx: dumpCtx(ctx),
14038
14047
  event: dumpEvent(event),
@@ -14045,22 +14054,23 @@ function registerHooks(api, cfg) {
14045
14054
  channel,
14046
14055
  accountId: ctx.accountId,
14047
14056
  conversationId: ctx.conversationId,
14048
- to: event.to,
14049
- success: event.success,
14050
- error: event.error
14057
+ to: ctx.to,
14058
+ success: ctx.success,
14059
+ error: ctx.error
14051
14060
  });
14052
14061
  try {
14053
14062
  const entry = buildActivityEntry(agentName, "message_sent", {
14054
14063
  channel: channel ?? "unknown",
14055
- success: event.success,
14056
- error: event.error
14064
+ success: ctx.success,
14065
+ error: ctx.error
14057
14066
  });
14058
14067
  if (entry) activityBatch.add(entry);
14059
14068
  } catch (err) {
14060
14069
  logger.warn(`cohort-sync: message_sent activity failed: ${String(err)}`);
14061
14070
  }
14062
- });
14063
- api.on("before_compaction", async (_event, ctx) => {
14071
+ }, { name: "cohort-sync.message_sent", description: "Emit activity entry for sent messages" });
14072
+ api.registerHook("before_compaction", async (event) => {
14073
+ const ctx = event.context ?? {};
14064
14074
  logger.debug("cohort-sync: hook: before_compaction", { ctx: dumpCtx(ctx) });
14065
14075
  const agentId = ctx.agentId ?? "main";
14066
14076
  const agentName = resolveAgentName(agentId);
@@ -14072,22 +14082,23 @@ function registerHooks(api, cfg) {
14072
14082
  } catch (err) {
14073
14083
  logger.warn(`cohort-sync: before_compaction activity failed: ${String(err)}`);
14074
14084
  }
14075
- });
14076
- api.on("before_reset", async (event, ctx) => {
14085
+ }, { name: "cohort-sync.before_compaction", description: "Emit activity entry before compaction" });
14086
+ api.registerHook("before_reset", async (event) => {
14087
+ const ctx = event.context ?? {};
14077
14088
  logger.debug("cohort-sync: hook: before_reset", { ctx: dumpCtx(ctx), event: dumpEvent(event) });
14078
14089
  const agentId = ctx.agentId ?? "main";
14079
14090
  const agentName = resolveAgentName(agentId);
14080
14091
  try {
14081
14092
  const entry = buildActivityEntry(agentName, "before_reset", {
14082
- reason: event.reason,
14093
+ reason: ctx.reason,
14083
14094
  sessionKey: ctx.sessionKey
14084
14095
  });
14085
14096
  if (entry) activityBatch.add(entry);
14086
14097
  } catch (err) {
14087
14098
  logger.warn(`cohort-sync: before_reset activity failed: ${String(err)}`);
14088
14099
  }
14089
- });
14090
- api.on("gateway_stop", async () => {
14100
+ }, { name: "cohort-sync.before_reset", description: "Emit activity entry before reset" });
14101
+ api.registerHook("gateway_stop", async () => {
14091
14102
  logger.debug("cohort-sync: hook: gateway_stop", { bridgeState: Object.fromEntries(getChannelAgentBridge()) });
14092
14103
  if (keepaliveInterval) {
14093
14104
  clearInterval(keepaliveInterval);
@@ -14121,7 +14132,7 @@ function registerHooks(api, cfg) {
14121
14132
  tracker2.clear();
14122
14133
  closeBridge();
14123
14134
  logger.info("cohort-sync: gateway stopped, all resources cleaned up");
14124
- });
14135
+ }, { name: "cohort-sync.gateway_stop", description: "Cleanup, final telemetry, mark unreachable" });
14125
14136
  }
14126
14137
 
14127
14138
  // index.ts
@@ -55,5 +55,5 @@
55
55
  }
56
56
  }
57
57
  },
58
- "version": "0.10.1"
58
+ "version": "0.10.2"
59
59
  }
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cfio/cohort-sync",
3
- "version": "0.10.1",
3
+ "version": "0.10.2",
4
4
  "description": "OpenClaw plugin — syncs agent telemetry, sessions, and activity to the Cohort dashboard",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cfio/cohort-sync",
3
- "version": "0.10.1",
3
+ "version": "0.10.2",
4
4
  "description": "OpenClaw plugin — syncs agent telemetry, sessions, and activity to the Cohort dashboard",
5
5
  "license": "MIT",
6
6
  "homepage": "https://docs.cohort.bot/gateway",