@xmoxmo/bncr 0.6.7 → 0.6.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/src/channel.ts CHANGED
@@ -348,6 +348,7 @@ class BncrBridgeRuntime {
348
348
  // 内置健康/回归计数(替代独立脚本)
349
349
  private startedAt = now();
350
350
  private stopped = false;
351
+ private serviceStarted = false;
351
352
  private connectEventsByAccount = new Map<string, number>();
352
353
  private inboundEventsByAccount = new Map<string, number>();
353
354
  private activityEventsByAccount = new Map<string, number>();
@@ -840,8 +841,21 @@ class BncrBridgeRuntime {
840
841
  return BNCR_DEBUG_VERBOSE;
841
842
  }
842
843
 
843
- startService = async (ctx: OpenClawPluginServiceContext, debug?: boolean) => {
844
- await startBncrBridgeService(
844
+ getRuntimeObservation() {
845
+ const activeChannelAccounts = this.channelAccountWorkers.size;
846
+ return {
847
+ serviceRunning: this.serviceStarted,
848
+ channelRunning: activeChannelAccounts > 0,
849
+ activeChannelAccounts,
850
+ };
851
+ }
852
+
853
+ startService = async (
854
+ ctx: OpenClawPluginServiceContext,
855
+ debug?: boolean,
856
+ isStartCurrent?: () => boolean,
857
+ ) => {
858
+ const started = await startBncrBridgeService(
845
859
  {
846
860
  bridgeId: this.bridgeId,
847
861
  setStopped: (value) => {
@@ -865,11 +879,19 @@ class BncrBridgeRuntime {
865
879
  (((cfg as BncrChannelConfigRoot | null | undefined)?.channels || null)?.[CHANNEL_ID] as
866
880
  | Record<string, unknown>
867
881
  | undefined) || {},
882
+ resumeOutboxDrain: () => {
883
+ if (this.outbox.size === 0 || this.connections.size === 0) return;
884
+ this.schedulePushDrain(0);
885
+ },
886
+ isStartCurrent,
868
887
  },
869
888
  ctx,
870
889
  debug,
871
890
  );
891
+ if (!started || this.stopped || !(isStartCurrent?.() ?? true)) return false;
872
892
  this.startHistoryShardWorker();
893
+ this.serviceStarted = true;
894
+ return true;
873
895
  };
874
896
 
875
897
  stopService = async () => {
@@ -887,6 +909,7 @@ class BncrBridgeRuntime {
887
909
  }
888
910
 
889
911
  private cleanupRuntimeWaitersAndTimers(reason: string) {
912
+ this.serviceStarted = false;
890
913
  this.stopHistoryShardWorker(reason);
891
914
  this.clientRpcRuntime.shutdown();
892
915
  cleanupBncrBridgeRuntime(
@@ -59,13 +59,15 @@ export function startBncrBridgeService(
59
59
  getChannelConfigRoot: (
60
60
  cfg: BncrChannelConfigRoot,
61
61
  ) => BncrChannelPolicyConfig | null | undefined;
62
+ resumeOutboxDrain?: () => void;
63
+ isStartCurrent?: () => boolean;
62
64
  },
63
65
  ctx: OpenClawPluginServiceContext,
64
66
  debug?: boolean,
65
67
  ) {
66
- runtime.setStopped(false);
67
68
  runtime.setStatePath(path.join(ctx.stateDir, 'bncr-bridge-state.json'));
68
69
  return (async () => {
70
+ const isStartCurrent = () => runtime.isStartCurrent?.() ?? true;
69
71
  try {
70
72
  const cfg = runtime.getRuntimeConfig();
71
73
  runtime.initializeCanonicalAgentId(cfg);
@@ -76,8 +78,10 @@ export function startBncrBridgeService(
76
78
  // ignore startup canonical agent initialization errors
77
79
  }
78
80
  await runtime.loadState();
81
+ if (!isStartCurrent()) return false;
79
82
  if (process.env.BNCR_SQLITE_CUTOVER === '1' && runtime.cutoverToSqlite) {
80
83
  const cutover = await runtime.cutoverToSqlite();
84
+ if (!isStartCurrent()) return false;
81
85
  runtime.logInfo(
82
86
  'sqlite',
83
87
  `cutover completed backup=${cutover.backupPath || 'none'} storeMode=${cutover.storeMode}`,
@@ -85,6 +89,8 @@ export function startBncrBridgeService(
85
89
  }
86
90
  if (typeof debug === 'boolean') runtime.setDebugFlag(debug);
87
91
  await runtime.refreshDebugFlagFromConfig({ forceLog: true });
92
+ if (!isStartCurrent()) return false;
93
+ runtime.setStopped(false);
88
94
  const bootDiag = runtime.buildIntegratedDiagnostics(BNCR_DEFAULT_ACCOUNT_ID);
89
95
  runtime.logInfo(
90
96
  'startup',
@@ -95,7 +101,12 @@ export function startBncrBridgeService(
95
101
  `service started bridge=${runtime.bridgeId} diag.ok=${bootDiag.regression.ok} routes=${bootDiag.regression.totalKnownRoutes} pending=${bootDiag.health.pending} dead=${bootDiag.health.deadLetter}`,
96
102
  { debugOnly: true },
97
103
  );
98
- })();
104
+ runtime.resumeOutboxDrain?.();
105
+ return true;
106
+ })().catch((error) => {
107
+ runtime.setStopped(true);
108
+ throw error;
109
+ });
99
110
  }
100
111
 
101
112
  export function cleanupBncrBridgeRuntime(