@cfio/cohort-sync 0.11.0 → 0.11.1
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 +64 -67
- package/dist/openclaw.plugin.json +1 -1
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -13334,7 +13334,7 @@ function dumpCtx(ctx) {
|
|
|
13334
13334
|
function dumpEvent(event) {
|
|
13335
13335
|
return dumpCtx(event);
|
|
13336
13336
|
}
|
|
13337
|
-
var PLUGIN_VERSION = true ? "0.11.
|
|
13337
|
+
var PLUGIN_VERSION = true ? "0.11.1" : "unknown";
|
|
13338
13338
|
function resolveGatewayToken(api) {
|
|
13339
13339
|
const token = api.config?.gateway?.auth?.token;
|
|
13340
13340
|
return typeof token === "string" ? token : null;
|
|
@@ -13464,9 +13464,12 @@ function initGatewayClient(port, token, cfg, resolveAgentName, logger) {
|
|
|
13464
13464
|
return client2;
|
|
13465
13465
|
}
|
|
13466
13466
|
async function handleGatewayStart(event, state) {
|
|
13467
|
-
if (!state)
|
|
13467
|
+
if (!state) {
|
|
13468
|
+
console.log("[cohort-sync] handleGatewayStart: state is null, skipping");
|
|
13469
|
+
return;
|
|
13470
|
+
}
|
|
13468
13471
|
const { cfg, tracker, logger, config, api } = state;
|
|
13469
|
-
logger.
|
|
13472
|
+
logger.info("cohort-sync: handleGatewayStart ENTERED", { port: event.port, hasConfig: !!config, hasCfg: !!cfg });
|
|
13470
13473
|
try {
|
|
13471
13474
|
checkForUpdate(PLUGIN_VERSION, logger).catch(() => {
|
|
13472
13475
|
});
|
|
@@ -13610,12 +13613,11 @@ function registerHookHandlers(api, logger, getState) {
|
|
|
13610
13613
|
state.logger.debug("cohort-sync: resolve agent: result", { method: "fallback_main", resolved });
|
|
13611
13614
|
return resolved;
|
|
13612
13615
|
}
|
|
13613
|
-
api.
|
|
13616
|
+
api.on("agent_end", async (event, ctx) => {
|
|
13614
13617
|
const state = getState();
|
|
13615
13618
|
if (!state) return;
|
|
13616
13619
|
const { cfg, tracker, logger: log } = state;
|
|
13617
|
-
|
|
13618
|
-
log.debug("cohort-sync: hook: agent_end", { ctx: dumpCtx(ctx), success: ctx.success, error: ctx.error, durationMs: ctx.durationMs });
|
|
13620
|
+
log.debug("cohort-sync: hook: agent_end", { ctx: dumpCtx(ctx), success: event.success, error: event.error, durationMs: event.durationMs });
|
|
13619
13621
|
const agentId = ctx.agentId ?? "main";
|
|
13620
13622
|
const agentName = state.resolveAgentName(agentId);
|
|
13621
13623
|
try {
|
|
@@ -13641,11 +13643,11 @@ function registerHookHandlers(api, logger, getState) {
|
|
|
13641
13643
|
log.debug("cohort-sync: cron agent end push failed", { error: String(err) });
|
|
13642
13644
|
}
|
|
13643
13645
|
}
|
|
13644
|
-
if (
|
|
13646
|
+
if (event.success === false) {
|
|
13645
13647
|
const entry = buildActivityEntry(agentName, "agent_end", {
|
|
13646
13648
|
success: false,
|
|
13647
|
-
error:
|
|
13648
|
-
durationMs:
|
|
13649
|
+
error: event.error,
|
|
13650
|
+
durationMs: event.durationMs,
|
|
13649
13651
|
sessionKey: ctx.sessionKey
|
|
13650
13652
|
});
|
|
13651
13653
|
if (entry) state.activityBatch.add(entry);
|
|
@@ -13653,15 +13655,14 @@ function registerHookHandlers(api, logger, getState) {
|
|
|
13653
13655
|
} catch (err) {
|
|
13654
13656
|
log.warn(`cohort-sync: agent_end sync failed: ${String(err)}`);
|
|
13655
13657
|
}
|
|
13656
|
-
}
|
|
13657
|
-
api.
|
|
13658
|
+
});
|
|
13659
|
+
api.on("llm_output", async (event, ctx) => {
|
|
13658
13660
|
const state = getState();
|
|
13659
13661
|
if (!state) return;
|
|
13660
13662
|
const { cfg, tracker, logger: log } = state;
|
|
13661
|
-
const
|
|
13662
|
-
const usage = ctx.usage ?? {};
|
|
13663
|
+
const usage = event.usage ?? {};
|
|
13663
13664
|
const contextTokens = (usage.input ?? 0) + (usage.cacheRead ?? 0) + (usage.cacheWrite ?? 0);
|
|
13664
|
-
const model =
|
|
13665
|
+
const model = event.model ?? state.resolveModel(ctx.agentId ?? "main");
|
|
13665
13666
|
const contextLimit = state.getModelContextLimit(model);
|
|
13666
13667
|
log.debug("cohort-sync: hook: llm_output", {
|
|
13667
13668
|
ctx: dumpCtx(ctx),
|
|
@@ -13706,18 +13707,17 @@ function registerHookHandlers(api, logger, getState) {
|
|
|
13706
13707
|
} catch (err) {
|
|
13707
13708
|
log.warn(`cohort-sync: llm_output telemetry failed: ${String(err)}`);
|
|
13708
13709
|
}
|
|
13709
|
-
}
|
|
13710
|
-
api.
|
|
13710
|
+
});
|
|
13711
|
+
api.on("after_compaction", async (event, ctx) => {
|
|
13711
13712
|
const state = getState();
|
|
13712
13713
|
if (!state) return;
|
|
13713
13714
|
const { cfg, tracker, logger: log } = state;
|
|
13714
|
-
|
|
13715
|
-
log.debug("cohort-sync: hook: after_compaction", { ctx: dumpCtx(ctx), messageCount: ctx.messageCount, tokenCount: ctx.tokenCount });
|
|
13715
|
+
log.debug("cohort-sync: hook: after_compaction", { ctx: dumpCtx(ctx), messageCount: event.messageCount, tokenCount: event.tokenCount });
|
|
13716
13716
|
const agentId = ctx.agentId ?? "main";
|
|
13717
13717
|
const agentName = state.resolveAgentName(agentId);
|
|
13718
13718
|
try {
|
|
13719
13719
|
tracker.updateFromCompaction(agentName, {
|
|
13720
|
-
contextTokens:
|
|
13720
|
+
contextTokens: event.tokenCount ?? 0,
|
|
13721
13721
|
contextLimit: state.getModelContextLimit(state.resolveModel(agentId))
|
|
13722
13722
|
});
|
|
13723
13723
|
if (tracker.shouldPushTelemetry(agentName)) {
|
|
@@ -13728,20 +13728,19 @@ function registerHookHandlers(api, logger, getState) {
|
|
|
13728
13728
|
}
|
|
13729
13729
|
}
|
|
13730
13730
|
const entry = buildActivityEntry(agentName, "after_compaction", {
|
|
13731
|
-
messageCount:
|
|
13732
|
-
compactedCount:
|
|
13731
|
+
messageCount: event.messageCount,
|
|
13732
|
+
compactedCount: event.compactedCount,
|
|
13733
13733
|
sessionKey: ctx.sessionKey
|
|
13734
13734
|
});
|
|
13735
13735
|
if (entry) state.activityBatch.add(entry);
|
|
13736
13736
|
} catch (err) {
|
|
13737
13737
|
log.warn(`cohort-sync: after_compaction telemetry failed: ${String(err)}`);
|
|
13738
13738
|
}
|
|
13739
|
-
}
|
|
13740
|
-
api.
|
|
13739
|
+
});
|
|
13740
|
+
api.on("before_agent_start", async (event, ctx) => {
|
|
13741
13741
|
const state = getState();
|
|
13742
13742
|
if (!state) return;
|
|
13743
13743
|
const { cfg, tracker, logger: log } = state;
|
|
13744
|
-
const ctx = event.context ?? {};
|
|
13745
13744
|
log.debug("cohort-sync: hook: before_agent_start", { ctx: dumpCtx(ctx), event: dumpEvent(event) });
|
|
13746
13745
|
const agentId = ctx.agentId ?? "main";
|
|
13747
13746
|
const agentName = state.resolveAgentName(agentId);
|
|
@@ -13794,17 +13793,16 @@ function registerHookHandlers(api, logger, getState) {
|
|
|
13794
13793
|
} catch (err) {
|
|
13795
13794
|
log.warn(`cohort-sync: before_agent_start telemetry failed: ${String(err)}`);
|
|
13796
13795
|
}
|
|
13797
|
-
}
|
|
13798
|
-
api.
|
|
13796
|
+
});
|
|
13797
|
+
api.on("session_start", async (event, ctx) => {
|
|
13799
13798
|
const state = getState();
|
|
13800
13799
|
if (!state) return;
|
|
13801
13800
|
const { cfg, tracker, logger: log } = state;
|
|
13802
|
-
const ctx = event.context ?? {};
|
|
13803
13801
|
log.debug("cohort-sync: hook: session_start", { ctx: dumpCtx(ctx), event: dumpEvent(event) });
|
|
13804
13802
|
const agentId = ctx.agentId ?? "main";
|
|
13805
13803
|
const agentName = state.resolveAgentName(agentId);
|
|
13806
13804
|
try {
|
|
13807
|
-
const sessionKey = ctx.sessionId ?? String(Date.now());
|
|
13805
|
+
const sessionKey = event.sessionId ?? ctx.sessionId ?? String(Date.now());
|
|
13808
13806
|
tracker.addSession(agentName, sessionKey);
|
|
13809
13807
|
if (tracker.shouldPushSessions(agentName)) {
|
|
13810
13808
|
const sessionsSnapshot = tracker.getSessionsSnapshot(agentName);
|
|
@@ -13815,23 +13813,22 @@ function registerHookHandlers(api, logger, getState) {
|
|
|
13815
13813
|
const entry = buildActivityEntry(agentName, "session_start", {
|
|
13816
13814
|
channel: parsed.channel,
|
|
13817
13815
|
sessionKey,
|
|
13818
|
-
resumedFrom:
|
|
13816
|
+
resumedFrom: event.resumedFrom
|
|
13819
13817
|
});
|
|
13820
13818
|
if (entry) state.activityBatch.add(entry);
|
|
13821
13819
|
} catch (err) {
|
|
13822
13820
|
log.warn(`cohort-sync: session_start tracking failed: ${String(err)}`);
|
|
13823
13821
|
}
|
|
13824
|
-
}
|
|
13825
|
-
api.
|
|
13822
|
+
});
|
|
13823
|
+
api.on("session_end", async (event, ctx) => {
|
|
13826
13824
|
const state = getState();
|
|
13827
13825
|
if (!state) return;
|
|
13828
13826
|
const { cfg, tracker, logger: log } = state;
|
|
13829
|
-
const ctx = event.context ?? {};
|
|
13830
13827
|
log.debug("cohort-sync: hook: session_end", { ctx: dumpCtx(ctx), event: dumpEvent(event) });
|
|
13831
13828
|
const agentId = ctx.agentId ?? "main";
|
|
13832
13829
|
const agentName = state.resolveAgentName(agentId);
|
|
13833
13830
|
try {
|
|
13834
|
-
const sessionKey = ctx.sessionId ?? "";
|
|
13831
|
+
const sessionKey = event.sessionId ?? ctx.sessionId ?? "";
|
|
13835
13832
|
tracker.removeSession(agentName, sessionKey);
|
|
13836
13833
|
if (tracker.shouldPushSessions(agentName)) {
|
|
13837
13834
|
const sessionsSnapshot = tracker.getSessionsSnapshot(agentName);
|
|
@@ -13840,26 +13837,25 @@ function registerHookHandlers(api, logger, getState) {
|
|
|
13840
13837
|
}
|
|
13841
13838
|
const entry = buildActivityEntry(agentName, "session_end", {
|
|
13842
13839
|
sessionKey,
|
|
13843
|
-
messageCount:
|
|
13844
|
-
durationMs:
|
|
13840
|
+
messageCount: event.messageCount,
|
|
13841
|
+
durationMs: event.durationMs
|
|
13845
13842
|
});
|
|
13846
13843
|
if (entry) state.activityBatch.add(entry);
|
|
13847
13844
|
} catch (err) {
|
|
13848
13845
|
log.warn(`cohort-sync: session_end tracking failed: ${String(err)}`);
|
|
13849
13846
|
}
|
|
13850
|
-
}
|
|
13851
|
-
api.
|
|
13847
|
+
});
|
|
13848
|
+
api.on("after_tool_call", async (event, ctx) => {
|
|
13852
13849
|
const state = getState();
|
|
13853
13850
|
if (!state) return;
|
|
13854
|
-
|
|
13855
|
-
state.logger.debug("cohort-sync: hook: after_tool_call", { ctx: dumpCtx(ctx), toolName: ctx.toolName, error: ctx.error });
|
|
13851
|
+
state.logger.debug("cohort-sync: hook: after_tool_call", { ctx: dumpCtx(ctx), toolName: event.toolName, error: event.error });
|
|
13856
13852
|
const agentName = resolveAgentFromContext(state, ctx);
|
|
13857
13853
|
try {
|
|
13858
13854
|
const entry = buildActivityEntry(agentName, "after_tool_call", {
|
|
13859
|
-
toolName:
|
|
13860
|
-
params:
|
|
13861
|
-
error:
|
|
13862
|
-
durationMs:
|
|
13855
|
+
toolName: event.toolName,
|
|
13856
|
+
params: event.params,
|
|
13857
|
+
error: event.error,
|
|
13858
|
+
durationMs: event.durationMs,
|
|
13863
13859
|
sessionKey: ctx.sessionKey,
|
|
13864
13860
|
model: state.resolveModel(ctx.agentId ?? "main")
|
|
13865
13861
|
});
|
|
@@ -13867,11 +13863,10 @@ function registerHookHandlers(api, logger, getState) {
|
|
|
13867
13863
|
} catch (err) {
|
|
13868
13864
|
state.logger.warn(`cohort-sync: after_tool_call activity failed: ${String(err)}`);
|
|
13869
13865
|
}
|
|
13870
|
-
}
|
|
13871
|
-
api.
|
|
13866
|
+
});
|
|
13867
|
+
api.on("message_received", async (event, ctx) => {
|
|
13872
13868
|
const state = getState();
|
|
13873
13869
|
if (!state) return;
|
|
13874
|
-
const ctx = event.context ?? {};
|
|
13875
13870
|
state.logger.debug("cohort-sync: hook: message_received raw", {
|
|
13876
13871
|
ctx: dumpCtx(ctx),
|
|
13877
13872
|
event: dumpEvent(event),
|
|
@@ -13884,7 +13879,7 @@ function registerHookHandlers(api, logger, getState) {
|
|
|
13884
13879
|
channel,
|
|
13885
13880
|
accountId: ctx.accountId,
|
|
13886
13881
|
conversationId: ctx.conversationId,
|
|
13887
|
-
from:
|
|
13882
|
+
from: event.from
|
|
13888
13883
|
});
|
|
13889
13884
|
try {
|
|
13890
13885
|
const entry = buildActivityEntry(agentName, "message_received", {
|
|
@@ -13894,11 +13889,10 @@ function registerHookHandlers(api, logger, getState) {
|
|
|
13894
13889
|
} catch (err) {
|
|
13895
13890
|
state.logger.warn(`cohort-sync: message_received activity failed: ${String(err)}`);
|
|
13896
13891
|
}
|
|
13897
|
-
}
|
|
13898
|
-
api.
|
|
13892
|
+
});
|
|
13893
|
+
api.on("message_sent", async (event, ctx) => {
|
|
13899
13894
|
const state = getState();
|
|
13900
13895
|
if (!state) return;
|
|
13901
|
-
const ctx = event.context ?? {};
|
|
13902
13896
|
state.logger.debug("cohort-sync: hook: message_sent raw", {
|
|
13903
13897
|
ctx: dumpCtx(ctx),
|
|
13904
13898
|
event: dumpEvent(event),
|
|
@@ -13911,25 +13905,24 @@ function registerHookHandlers(api, logger, getState) {
|
|
|
13911
13905
|
channel,
|
|
13912
13906
|
accountId: ctx.accountId,
|
|
13913
13907
|
conversationId: ctx.conversationId,
|
|
13914
|
-
to:
|
|
13915
|
-
success:
|
|
13916
|
-
error:
|
|
13908
|
+
to: event.to,
|
|
13909
|
+
success: event.success,
|
|
13910
|
+
error: event.error
|
|
13917
13911
|
});
|
|
13918
13912
|
try {
|
|
13919
13913
|
const entry = buildActivityEntry(agentName, "message_sent", {
|
|
13920
13914
|
channel: channel ?? "unknown",
|
|
13921
|
-
success:
|
|
13922
|
-
error:
|
|
13915
|
+
success: event.success,
|
|
13916
|
+
error: event.error
|
|
13923
13917
|
});
|
|
13924
13918
|
if (entry) state.activityBatch.add(entry);
|
|
13925
13919
|
} catch (err) {
|
|
13926
13920
|
state.logger.warn(`cohort-sync: message_sent activity failed: ${String(err)}`);
|
|
13927
13921
|
}
|
|
13928
|
-
}
|
|
13929
|
-
api.
|
|
13922
|
+
});
|
|
13923
|
+
api.on("before_compaction", async (event, ctx) => {
|
|
13930
13924
|
const state = getState();
|
|
13931
13925
|
if (!state) return;
|
|
13932
|
-
const ctx = event.context ?? {};
|
|
13933
13926
|
state.logger.debug("cohort-sync: hook: before_compaction", { ctx: dumpCtx(ctx) });
|
|
13934
13927
|
const agentId = ctx.agentId ?? "main";
|
|
13935
13928
|
const agentName = state.resolveAgentName(agentId);
|
|
@@ -13941,25 +13934,24 @@ function registerHookHandlers(api, logger, getState) {
|
|
|
13941
13934
|
} catch (err) {
|
|
13942
13935
|
state.logger.warn(`cohort-sync: before_compaction activity failed: ${String(err)}`);
|
|
13943
13936
|
}
|
|
13944
|
-
}
|
|
13945
|
-
api.
|
|
13937
|
+
});
|
|
13938
|
+
api.on("before_reset", async (event, ctx) => {
|
|
13946
13939
|
const state = getState();
|
|
13947
13940
|
if (!state) return;
|
|
13948
|
-
const ctx = event.context ?? {};
|
|
13949
13941
|
state.logger.debug("cohort-sync: hook: before_reset", { ctx: dumpCtx(ctx), event: dumpEvent(event) });
|
|
13950
13942
|
const agentId = ctx.agentId ?? "main";
|
|
13951
13943
|
const agentName = state.resolveAgentName(agentId);
|
|
13952
13944
|
try {
|
|
13953
13945
|
const entry = buildActivityEntry(agentName, "before_reset", {
|
|
13954
|
-
reason:
|
|
13946
|
+
reason: event.reason,
|
|
13955
13947
|
sessionKey: ctx.sessionKey
|
|
13956
13948
|
});
|
|
13957
13949
|
if (entry) state.activityBatch.add(entry);
|
|
13958
13950
|
} catch (err) {
|
|
13959
13951
|
state.logger.warn(`cohort-sync: before_reset activity failed: ${String(err)}`);
|
|
13960
13952
|
}
|
|
13961
|
-
}
|
|
13962
|
-
api.
|
|
13953
|
+
});
|
|
13954
|
+
api.on("gateway_stop", async (_event, _ctx) => {
|
|
13963
13955
|
const state = getState();
|
|
13964
13956
|
if (!state) return;
|
|
13965
13957
|
const { cfg, tracker, logger: log, config } = state;
|
|
@@ -13996,7 +13988,7 @@ function registerHookHandlers(api, logger, getState) {
|
|
|
13996
13988
|
tracker.clear();
|
|
13997
13989
|
closeBridge();
|
|
13998
13990
|
log.info("cohort-sync: gateway stopped, all resources cleaned up");
|
|
13999
|
-
}
|
|
13991
|
+
});
|
|
14000
13992
|
}
|
|
14001
13993
|
function initializeHookState(api, cfg) {
|
|
14002
13994
|
const { logger, config } = api;
|
|
@@ -14190,8 +14182,13 @@ var plugin = {
|
|
|
14190
14182
|
async (...args) => {
|
|
14191
14183
|
const event = args[0] ?? {};
|
|
14192
14184
|
const port = event?.port ?? gatewayPort;
|
|
14193
|
-
api.logger.info(`cohort-sync: gateway:startup hook fired (port=${port})`);
|
|
14194
|
-
|
|
14185
|
+
api.logger.info(`cohort-sync: gateway:startup hook fired (port=${port}, hookState=${hookState ? "SET" : "NULL"})`);
|
|
14186
|
+
try {
|
|
14187
|
+
await handleGatewayStart({ ...event, port }, hookState);
|
|
14188
|
+
api.logger.info("cohort-sync: handleGatewayStart completed");
|
|
14189
|
+
} catch (err) {
|
|
14190
|
+
api.logger.error(`cohort-sync: handleGatewayStart THREW: ${err instanceof Error ? err.stack : String(err)}`);
|
|
14191
|
+
}
|
|
14195
14192
|
},
|
|
14196
14193
|
{
|
|
14197
14194
|
name: "cohort-sync.gateway-startup",
|
package/dist/package.json
CHANGED
package/package.json
CHANGED