@moxt-ai/mobius 0.0.126 → 0.0.127

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/runtime.js CHANGED
@@ -35264,6 +35264,116 @@ var RetainedSessions = class {
35264
35264
  };
35265
35265
  };
35266
35266
 
35267
+ // src/acp/native-session-browsers.ts
35268
+ var NativeSessionBrowsers = class {
35269
+ #sessions;
35270
+ #processes;
35271
+ #observer = new NullAcpSessionObserver();
35272
+ #store = new MemoryAcpSessionStore();
35273
+ constructor(processes) {
35274
+ this.#processes = processes;
35275
+ this.#sessions = new RetainedSessions({ touch: processes.touch, changed: processes.notifyChanged });
35276
+ }
35277
+ retire = async (sessionId, runtime) => {
35278
+ const retained = this.#sessions.detach(sessionId, runtime);
35279
+ if (retained) {
35280
+ this.#store.remove(sessionId);
35281
+ }
35282
+ await this.#processes.retire(runtime, () => runtime.close());
35283
+ };
35284
+ async #startRetained(options) {
35285
+ const runtime = await this.#processes.startRetained(
35286
+ options.signal,
35287
+ () => AcpProcessSession.startNativeSessionBrowser(options, () => this.#observer, this.#store),
35288
+ {
35289
+ isBusy: () => this.#sessions.isBusy(options.sessionId),
35290
+ retire: (runtime2) => this.retire(options.sessionId, runtime2)
35291
+ }
35292
+ );
35293
+ this.#sessions.retain(options.sessionId, runtime);
35294
+ return runtime;
35295
+ }
35296
+ async #startPreparation(options) {
35297
+ return await this.#sessions.prepare(options.sessionId, async () => {
35298
+ return await this.#startRetained(options);
35299
+ });
35300
+ }
35301
+ async #replacePreparation(existing, options) {
35302
+ return await this.#sessions.prepare(options.sessionId, async () => {
35303
+ await this.retire(options.sessionId, existing);
35304
+ return await this.#startRetained(options);
35305
+ });
35306
+ }
35307
+ prepare = async (options) => {
35308
+ const pending = this.#sessions.pending(options.sessionId);
35309
+ if (pending !== void 0) {
35310
+ const runtime2 = await pending;
35311
+ if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
35312
+ this.#sessions.touch(options.sessionId, runtime2);
35313
+ return runtime2.prepared();
35314
+ }
35315
+ return await this.prepare(options);
35316
+ }
35317
+ const existing = this.#sessions.get(options.sessionId);
35318
+ if (existing !== void 0) {
35319
+ if (existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
35320
+ this.#sessions.touch(options.sessionId, existing);
35321
+ return existing.prepared();
35322
+ }
35323
+ if (!existing.canRebind()) {
35324
+ throw new AcpExecutionError(
35325
+ "The agent or workspace cannot change after a conversation has started",
35326
+ {},
35327
+ "session_identity_mismatch"
35328
+ );
35329
+ }
35330
+ const runtime2 = await this.#replacePreparation(existing, options);
35331
+ return runtime2.prepared();
35332
+ }
35333
+ const runtime = await this.#startPreparation(options);
35334
+ return runtime.prepared();
35335
+ };
35336
+ async #ensure(options) {
35337
+ const existing = this.#sessions.get(options.sessionId);
35338
+ if (existing !== void 0) {
35339
+ if (!existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
35340
+ throw new AcpExecutionError(
35341
+ "The agent or workspace cannot change after a conversation has started",
35342
+ {},
35343
+ "session_identity_mismatch"
35344
+ );
35345
+ }
35346
+ this.#sessions.touch(options.sessionId, existing);
35347
+ return existing;
35348
+ }
35349
+ await this.prepare(options);
35350
+ const started = this.#sessions.get(options.sessionId);
35351
+ if (started === void 0) {
35352
+ throw new AcpExecutionError("ACP native session browser did not start");
35353
+ }
35354
+ return started;
35355
+ }
35356
+ use = async (options, operation) => await this.#sessions.use(options.sessionId, async () => await operation(await this.#ensure(options)));
35357
+ preview = async (options, nativeSessionId) => {
35358
+ return await this.#processes.runTransient(
35359
+ options.signal,
35360
+ async () => await AcpProcessSession.startNativeSessionBrowser(
35361
+ options,
35362
+ () => this.#observer,
35363
+ new MemoryAcpSessionStore()
35364
+ ),
35365
+ async (runtime) => {
35366
+ runtime.selectNativeSession(nativeSessionId);
35367
+ return await runtime.loadHistory(options.signal);
35368
+ },
35369
+ async (runtime) => await runtime.closeProcess()
35370
+ );
35371
+ };
35372
+ close = async () => {
35373
+ await Promise.all(this.#sessions.entries().map(async ([id, runtime]) => await this.retire(id, runtime)));
35374
+ };
35375
+ };
35376
+
35267
35377
  // src/acp/session-executions.ts
35268
35378
  var unreachable = (state) => {
35269
35379
  throw new AcpExecutionError("Unexpected execution state", { cause: state });
@@ -35557,15 +35667,10 @@ var SESSION_CONTROL_TIMEOUT_MILLISECONDS = 3e4;
35557
35667
  var AcpSessionManager = class {
35558
35668
  #processes = new AcpProcessPool();
35559
35669
  #sessions = new RetainedSessions({ touch: this.#processes.touch, changed: this.#processes.notifyChanged });
35560
- #nativeBrowserSessions = new RetainedSessions({
35561
- touch: this.#processes.touch,
35562
- changed: this.#processes.notifyChanged
35563
- });
35670
+ #browsers = new NativeSessionBrowsers(this.#processes);
35564
35671
  #executions = new SessionExecutions();
35565
35672
  #agentSettingsObserver = new NullAcpSessionObserver();
35566
35673
  #nativeOperations = /* @__PURE__ */ new Set();
35567
- #nativeBrowserObserver = new NullAcpSessionObserver();
35568
- #nativeBrowserStore = new MemoryAcpSessionStore();
35569
35674
  #store;
35570
35675
  #nativeHistoryReader;
35571
35676
  #sessionIdentity;
@@ -35583,16 +35688,10 @@ var AcpSessionManager = class {
35583
35688
  const stored = this.#store.read(sessionId);
35584
35689
  return stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`);
35585
35690
  }
35586
- #retention(sessionId, kind) {
35587
- if (kind === "execution") {
35588
- return {
35589
- isBusy: () => this.#executions.isBusy(sessionId) || this.#sessions.isBusy(sessionId) || this.#isNativeSessionOperationActive(sessionId),
35590
- retire: (runtime) => this.#retireSessionProcess(sessionId, runtime, () => runtime.close())
35591
- };
35592
- }
35691
+ #retention(sessionId) {
35593
35692
  return {
35594
- isBusy: () => this.#nativeBrowserSessions.isBusy(sessionId),
35595
- retire: (runtime) => this.#retireNativeBrowserProcess(sessionId, runtime, () => runtime.close())
35693
+ isBusy: () => this.#executions.isBusy(sessionId) || this.#sessions.isBusy(sessionId) || this.#isNativeSessionOperationActive(sessionId),
35694
+ retire: (runtime) => this.#retireSessionProcess(sessionId, runtime, () => runtime.close())
35596
35695
  };
35597
35696
  }
35598
35697
  async #retireSessionProcess(sessionId, runtime, close) {
@@ -35608,56 +35707,11 @@ var AcpSessionManager = class {
35608
35707
  }
35609
35708
  }
35610
35709
  }
35611
- async #retireNativeBrowserProcess(sessionId, runtime, close) {
35612
- const retained = this.#nativeBrowserSessions.detach(sessionId, runtime);
35613
- if (retained) {
35614
- this.#nativeBrowserStore.remove(sessionId);
35615
- }
35616
- await this.#processes.retire(runtime, close);
35617
- }
35618
35710
  async #startRetainedSessionProcess(options, start) {
35619
- const runtime = await this.#processes.startRetained(
35620
- options.signal,
35621
- start,
35622
- this.#retention(options.sessionId, "execution")
35623
- );
35711
+ const runtime = await this.#processes.startRetained(options.signal, start, this.#retention(options.sessionId));
35624
35712
  this.#sessions.retain(options.sessionId, runtime);
35625
35713
  return runtime;
35626
35714
  }
35627
- async #startRetainedNativeBrowserProcess(options, start) {
35628
- const runtime = await this.#processes.startRetained(
35629
- options.signal,
35630
- start,
35631
- this.#retention(options.sessionId, "browser")
35632
- );
35633
- this.#nativeBrowserSessions.retain(options.sessionId, runtime);
35634
- return runtime;
35635
- }
35636
- async #startNativeBrowserPreparation(options) {
35637
- return await this.#nativeBrowserSessions.prepare(options.sessionId, async () => {
35638
- return await this.#startRetainedNativeBrowserProcess(
35639
- options,
35640
- async () => await AcpProcessSession.startNativeSessionBrowser(
35641
- options,
35642
- () => this.#nativeBrowserObserver,
35643
- this.#nativeBrowserStore
35644
- )
35645
- );
35646
- });
35647
- }
35648
- async #replaceNativeBrowserPreparation(existing, options) {
35649
- return await this.#nativeBrowserSessions.prepare(options.sessionId, async () => {
35650
- await this.#retireNativeBrowserProcess(options.sessionId, existing, async () => await existing.close());
35651
- return await this.#startRetainedNativeBrowserProcess(
35652
- options,
35653
- async () => await AcpProcessSession.startNativeSessionBrowser(
35654
- options,
35655
- () => this.#nativeBrowserObserver,
35656
- this.#nativeBrowserStore
35657
- )
35658
- );
35659
- });
35660
- }
35661
35715
  async #startPreparation(options, metrics = unmeasuredOperationMetrics) {
35662
35716
  return await this.#sessions.prepare(options.sessionId, async () => {
35663
35717
  return await this.#startRetainedSessionProcess(
@@ -35854,35 +35908,7 @@ var AcpSessionManager = class {
35854
35908
  }
35855
35909
  return (await this.#replaceExecutionPreparation(existing, options)).prepared();
35856
35910
  };
35857
- prepareNativeSessionBrowser = async (options) => {
35858
- const pending = this.#nativeBrowserSessions.pending(options.sessionId);
35859
- if (pending !== void 0) {
35860
- const runtime2 = await pending;
35861
- if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
35862
- this.#nativeBrowserSessions.touch(options.sessionId, runtime2);
35863
- return runtime2.prepared();
35864
- }
35865
- return await this.prepareNativeSessionBrowser(options);
35866
- }
35867
- const existing = this.#nativeBrowserSessions.get(options.sessionId);
35868
- if (existing !== void 0) {
35869
- if (existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
35870
- this.#nativeBrowserSessions.touch(options.sessionId, existing);
35871
- return existing.prepared();
35872
- }
35873
- if (!existing.canRebind()) {
35874
- throw new AcpExecutionError(
35875
- "The agent or workspace cannot change after a conversation has started",
35876
- {},
35877
- "session_identity_mismatch"
35878
- );
35879
- }
35880
- const runtime2 = await this.#replaceNativeBrowserPreparation(existing, options);
35881
- return runtime2.prepared();
35882
- }
35883
- const runtime = await this.#startNativeBrowserPreparation(options);
35884
- return runtime.prepared();
35885
- };
35911
+ prepareNativeSessionBrowser = (options) => this.#browsers.prepare(options);
35886
35912
  configureSession = async (options) => {
35887
35913
  return await this.#sessions.use(options.sessionId, async () => {
35888
35914
  const runtime = await this.ensureSession(options);
@@ -36015,14 +36041,11 @@ var AcpSessionManager = class {
36015
36041
  };
36016
36042
  close = async () => {
36017
36043
  const sessions = [...this.#sessions.entries()];
36018
- const nativeBrowserSessions = [...this.#nativeBrowserSessions.entries()];
36019
36044
  await Promise.all([
36020
36045
  ...sessions.map(
36021
36046
  async ([sessionId, runtime]) => await this.#retireSessionProcess(sessionId, runtime, async () => await runtime.close())
36022
36047
  ),
36023
- ...nativeBrowserSessions.map(
36024
- async ([sessionId, runtime]) => await this.#retireNativeBrowserProcess(sessionId, runtime, async () => await runtime.close())
36025
- )
36048
+ this.#browsers.close()
36026
36049
  ]);
36027
36050
  };
36028
36051
  findSessions = (sessionIds) => sessionIds.flatMap((sessionId) => {
@@ -36050,7 +36073,7 @@ var AcpSessionManager = class {
36050
36073
  runtime.adoptNativeSessionId(nativeSessionId);
36051
36074
  this.#sessions.detach(temporarySessionId, runtime);
36052
36075
  this.#sessions.retain(nativeSessionId, runtime);
36053
- this.#processes.retain(runtime, this.#retention(nativeSessionId, "execution"));
36076
+ this.#processes.retain(runtime, this.#retention(nativeSessionId));
36054
36077
  adopted = true;
36055
36078
  return nativeSessionId;
36056
36079
  } finally {
@@ -36087,30 +36110,11 @@ var AcpSessionManager = class {
36087
36110
  }
36088
36111
  return started;
36089
36112
  };
36090
- async #ensureNativeSessionBrowser(options) {
36091
- const existing = this.#nativeBrowserSessions.get(options.sessionId);
36092
- if (existing !== void 0) {
36093
- if (!existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
36094
- throw new AcpExecutionError(
36095
- "The agent or workspace cannot change after a conversation has started",
36096
- {},
36097
- "session_identity_mismatch"
36098
- );
36099
- }
36100
- this.#nativeBrowserSessions.touch(options.sessionId, existing);
36101
- return existing;
36102
- }
36103
- await this.prepareNativeSessionBrowser(options);
36104
- const started = this.#nativeBrowserSessions.get(options.sessionId);
36105
- if (started === void 0) {
36106
- throw new AcpExecutionError("ACP native session browser did not start");
36107
- }
36108
- return started;
36109
- }
36110
36113
  listNativeSessions = async (options, cursor) => {
36111
- return await this.#nativeBrowserSessions.use(options.sessionId, async () => {
36112
- return await (await this.#ensureNativeSessionBrowser(options)).listNativeSessions(cursor, options.signal);
36113
- });
36114
+ return await this.#browsers.use(
36115
+ options,
36116
+ async (runtime) => await runtime.listNativeSessions(cursor, options.signal)
36117
+ );
36114
36118
  };
36115
36119
  previewNativeSession = async (options, nativeSessionId) => {
36116
36120
  const key = `${options.agent.id}:${nativeSessionId}`;
@@ -36126,19 +36130,7 @@ var AcpSessionManager = class {
36126
36130
  }
36127
36131
  this.#nativeOperations.add(key);
36128
36132
  try {
36129
- return await this.#processes.runTransient(
36130
- options.signal,
36131
- async () => await AcpProcessSession.startNativeSessionBrowser(
36132
- options,
36133
- () => this.#nativeBrowserObserver,
36134
- new MemoryAcpSessionStore()
36135
- ),
36136
- async (runtime) => {
36137
- runtime.selectNativeSession(nativeSessionId);
36138
- return await runtime.loadHistory(options.signal);
36139
- },
36140
- async (runtime) => await runtime.closeProcess()
36141
- );
36133
+ return await this.#browsers.preview(options, nativeSessionId);
36142
36134
  } finally {
36143
36135
  this.#nativeOperations.delete(key);
36144
36136
  }
@@ -36236,8 +36228,7 @@ var AcpSessionManager = class {
36236
36228
  });
36237
36229
  };
36238
36230
  deleteNativeSession = async (options, nativeSessionId) => {
36239
- return await this.#nativeBrowserSessions.use(options.sessionId, async () => {
36240
- const runtime = await this.#ensureNativeSessionBrowser(options);
36231
+ return await this.#browsers.use(options, async (runtime) => {
36241
36232
  const key = `${options.agent.id}:${nativeSessionId}`;
36242
36233
  const linked = this.#store.findNative(options.agent.id, nativeSessionId);
36243
36234
  if (this.#nativeOperations.has(key) || linked.some(
@@ -36261,11 +36252,7 @@ var AcpSessionManager = class {
36261
36252
  await runtime.deleteNativeSession(nativeSessionId, options.signal);
36262
36253
  const deleted = this.#store.markDeleted(options.agent.id, nativeSessionId);
36263
36254
  if (runtime.nativeSessionId() === nativeSessionId) {
36264
- await this.#retireNativeBrowserProcess(
36265
- options.sessionId,
36266
- runtime,
36267
- async () => await runtime.close()
36268
- );
36255
+ await this.#browsers.retire(options.sessionId, runtime);
36269
36256
  }
36270
36257
  return deleted;
36271
36258
  } finally {