@moxt-ai/mobius 0.0.53 → 0.0.55

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/cli.js CHANGED
@@ -11,7 +11,7 @@ import { mkdir as mkdir5 } from "node:fs/promises";
11
11
  // package.json
12
12
  var package_default = {
13
13
  name: "@moxt-ai/mobius",
14
- version: "0.0.53",
14
+ version: "0.0.55",
15
15
  description: "Mobius CLI for pairing and running local AI agent services.",
16
16
  type: "module",
17
17
  bin: {
@@ -38705,8 +38705,72 @@ async function resolvePromptArtifacts(config2, command, signal) {
38705
38705
  return new ResolvedPromptArtifacts(images, resources);
38706
38706
  }
38707
38707
 
38708
+ // src/relay-connection/coalescing-text-publisher.ts
38709
+ var CoalescingTextPublisher = class {
38710
+ #coalescingMilliseconds;
38711
+ #maximumTextLength;
38712
+ #publish;
38713
+ #timer;
38714
+ #messageId = "";
38715
+ #scheduled = null;
38716
+ #text = "";
38717
+ constructor(timer, publish, coalescingMilliseconds, maximumTextLength) {
38718
+ this.#publish = publish;
38719
+ this.#timer = timer;
38720
+ this.#coalescingMilliseconds = coalescingMilliseconds;
38721
+ this.#maximumTextLength = maximumTextLength;
38722
+ }
38723
+ /** Emit any buffered text now, cancelling the pending flush. */
38724
+ flush = () => {
38725
+ this.#scheduled?.cancel();
38726
+ this.#scheduled = null;
38727
+ const messageId = this.#messageId;
38728
+ const text3 = this.#text;
38729
+ this.#messageId = "";
38730
+ this.#text = "";
38731
+ if (text3.length > 0) {
38732
+ this.#publish(messageId, text3);
38733
+ }
38734
+ };
38735
+ /**
38736
+ * Buffer a chunk, emitting the buffered text when the message changes or the
38737
+ * buffer would exceed the protocol's per-event text limit.
38738
+ */
38739
+ record = (messageId, text3) => {
38740
+ if (text3.length === 0) {
38741
+ return;
38742
+ }
38743
+ if (this.#text.length > 0 && (this.#messageId !== messageId || this.#text.length + text3.length > this.#maximumTextLength)) {
38744
+ this.flush();
38745
+ }
38746
+ this.#messageId = messageId;
38747
+ this.#text = `${this.#text}${text3}`;
38748
+ this.#scheduled ??= this.#timer.schedule(this.flush, this.#coalescingMilliseconds);
38749
+ };
38750
+ };
38751
+
38708
38752
  // src/relay-connection/connect.ts
38709
38753
  import { WebSocket } from "undici";
38754
+
38755
+ // src/relay-connection/runtime-revocation.ts
38756
+ var runtimeRevocationCloseCode = 1008;
38757
+ var runtimeRevocationCloseReason = "Runtime revoked";
38758
+ var RuntimeRelayRevokedError = class extends Error {
38759
+ code = "runtime_relay_revoked";
38760
+ closeCode;
38761
+ closeReason;
38762
+ constructor(closeCode, closeReason, options = {}) {
38763
+ super("The Runtime was revoked by the Relay", options);
38764
+ this.closeCode = closeCode;
38765
+ this.closeReason = closeReason;
38766
+ this.name = "RuntimeRelayRevokedError";
38767
+ }
38768
+ };
38769
+ function isRuntimeRelayRevocation(closeCode, closeReason) {
38770
+ return closeCode === runtimeRevocationCloseCode && closeReason === runtimeRevocationCloseReason;
38771
+ }
38772
+
38773
+ // src/relay-connection/connect.ts
38710
38774
  var CONNECTION_TIMEOUT_MILLISECONDS = 15e3;
38711
38775
  var RelayConnectionError = class extends Error {
38712
38776
  code;
@@ -38759,6 +38823,10 @@ function connectRelay(url2, signal) {
38759
38823
  );
38760
38824
  };
38761
38825
  const onClose = (event) => {
38826
+ if (isRuntimeRelayRevocation(event.code, event.reason)) {
38827
+ fail(new RuntimeRelayRevokedError(event.code, event.reason));
38828
+ return;
38829
+ }
38762
38830
  fail(
38763
38831
  new RelayConnectionError("relay_connection_closed", "Relay connection closed before it was ready", {
38764
38832
  closeCode: event.code.toString(),
@@ -39033,12 +39101,16 @@ var RelayConnectionLiveness = class {
39033
39101
  heartbeatId,
39034
39102
  this.#timer.schedule(this.#handleTimeout, HEARTBEAT_ACK_TIMEOUT_MILLISECONDS)
39035
39103
  );
39036
- this.#transport.send(
39104
+ const sent = this.#transport.send(
39037
39105
  encodeRuntimeHeartbeatEvent({
39038
39106
  heartbeatId,
39039
39107
  kind: RUNTIME_HEARTBEAT_KIND
39040
39108
  })
39041
39109
  );
39110
+ if (!sent && this.#state instanceof AwaitingHeartbeatAckState && this.#state.heartbeatId === heartbeatId) {
39111
+ this.#state.timeout.cancel();
39112
+ this.#state = new FinishedRelayLivenessState();
39113
+ }
39042
39114
  };
39043
39115
  };
39044
39116
 
@@ -39641,6 +39713,8 @@ var systemDaemonTimer = new SystemDaemonTimer();
39641
39713
  // src/run-daemon.ts
39642
39714
  var RELAY_RECONNECT_DELAY_MILLISECONDS = 500;
39643
39715
  var AGENT_ACTIVITY_COALESCING_MILLISECONDS = 100;
39716
+ var AGENT_TEXT_COALESCING_MILLISECONDS = 100;
39717
+ var AGENT_TEXT_MAXIMUM_LENGTH = 1e5;
39644
39718
  var ACTIVE_TURN_RECONCILIATION_TIMEOUT_MILLISECONDS = 2e4;
39645
39719
  var ACTIVE_TURN_RECONCILIATION_CLOSE_CODE = 4009;
39646
39720
  var DaemonAcpObserver = class {
@@ -40541,6 +40615,24 @@ async function executePrompt(outbox, publisher, command, config2, directories, a
40541
40615
  const activities = new CoalescingDaemonActivityPublisher(timer, (activity) => {
40542
40616
  publisher.recordAttempt(command.sessionId, encodeDaemonActivity(command, activity), attemptId);
40543
40617
  });
40618
+ const text3 = new CoalescingTextPublisher(
40619
+ timer,
40620
+ (messageId, delta) => {
40621
+ publisher.recordAttempt(
40622
+ command.sessionId,
40623
+ encodeAgentMessageDeltaEvent({
40624
+ commandId: command.commandId,
40625
+ kind: AGENT_MESSAGE_DELTA_KIND,
40626
+ messageId,
40627
+ sessionId: command.sessionId,
40628
+ text: delta
40629
+ }),
40630
+ attemptId
40631
+ );
40632
+ },
40633
+ AGENT_TEXT_COALESCING_MILLISECONDS,
40634
+ AGENT_TEXT_MAXIMUM_LENGTH
40635
+ );
40544
40636
  try {
40545
40637
  const resolved = await resolveSessionDirectories(
40546
40638
  directories,
@@ -40627,18 +40719,8 @@ async function executePrompt(outbox, publisher, command, config2, directories, a
40627
40719
  disposition: command.disposition,
40628
40720
  images: attachments.images,
40629
40721
  onActivity: activities.record,
40630
- onText: (text3, messageId) => {
40631
- publisher.recordAttempt(
40632
- command.sessionId,
40633
- encodeAgentMessageDeltaEvent({
40634
- commandId: command.commandId,
40635
- kind: AGENT_MESSAGE_DELTA_KIND,
40636
- messageId,
40637
- sessionId: command.sessionId,
40638
- text: text3
40639
- }),
40640
- attemptId
40641
- );
40722
+ onText: (delta, messageId) => {
40723
+ text3.record(messageId, delta);
40642
40724
  return Promise.resolve();
40643
40725
  },
40644
40726
  prompt: command.prompt,
@@ -40649,6 +40731,7 @@ async function executePrompt(outbox, publisher, command, config2, directories, a
40649
40731
  });
40650
40732
  stopReason = result.stopReason;
40651
40733
  }
40734
+ text3.flush();
40652
40735
  activities.flush();
40653
40736
  publisher.recordAttempt(
40654
40737
  command.sessionId,
@@ -40667,6 +40750,7 @@ async function executePrompt(outbox, publisher, command, config2, directories, a
40667
40750
  sessionId: command.sessionId
40668
40751
  });
40669
40752
  } catch (error62) {
40753
+ text3.flush();
40670
40754
  activities.flush();
40671
40755
  audit.record({
40672
40756
  commandId: command.commandId,
@@ -41205,7 +41289,20 @@ async function runRelayConnection(config2, directories, audit, sessions, install
41205
41289
  }
41206
41290
  };
41207
41291
  const connectionLiveness = new RelayConnectionLiveness(timer, {
41208
- send: (message) => send(socket, message),
41292
+ send: (message) => {
41293
+ if (socket.readyState !== WebSocket2.OPEN) {
41294
+ return false;
41295
+ }
41296
+ try {
41297
+ send(socket, message);
41298
+ return true;
41299
+ } catch (error62) {
41300
+ if (socket.readyState !== WebSocket2.OPEN) {
41301
+ return false;
41302
+ }
41303
+ throw error62;
41304
+ }
41305
+ },
41209
41306
  unresponsive: (heartbeatId, timeoutMilliseconds) => {
41210
41307
  diagnostics.heartbeatTimedOut(connectionEpoch, heartbeatId, timeoutMilliseconds);
41211
41308
  diagnostics.connectionClosed(
@@ -41221,6 +41318,10 @@ async function runRelayConnection(config2, directories, audit, sessions, install
41221
41318
  signal.addEventListener("abort", stop, { once: true });
41222
41319
  const handleClose = (event) => {
41223
41320
  diagnostics.connectionClosed(connectionEpoch, event.code, event.reason, "relay");
41321
+ if (isRuntimeRelayRevocation(event.code, event.reason)) {
41322
+ rejectConnection(new RuntimeRelayRevokedError(event.code, event.reason));
41323
+ return;
41324
+ }
41224
41325
  resolveConnection();
41225
41326
  };
41226
41327
  const handleError = (event) => {
@@ -41875,6 +41976,9 @@ async function runDaemon(config2, audit, signal, timer = systemDaemonTimer) {
41875
41976
  if (signal.aborted) {
41876
41977
  return;
41877
41978
  }
41979
+ if (error62 instanceof RuntimeRelayRevokedError) {
41980
+ throw error62;
41981
+ }
41878
41982
  diagnostics.connectionFailed(connectionEpoch, error62, "connection");
41879
41983
  }
41880
41984
  await waitForRelayRetry(signal, timer);