@moxt-ai/mobius 0.0.98 → 0.0.99

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.
Files changed (34) hide show
  1. package/dist/cli.js +274 -276
  2. package/dist/cli.js.map +4 -4
  3. package/dist/daemon-observers.d.ts +30 -0
  4. package/dist/daemon-observers.d.ts.map +1 -0
  5. package/dist/daemon-observers.js +137 -0
  6. package/dist/daemon-observers.js.map +1 -0
  7. package/dist/daemon-turn-reconciliation.d.ts +27 -0
  8. package/dist/daemon-turn-reconciliation.d.ts.map +1 -0
  9. package/dist/daemon-turn-reconciliation.js +121 -0
  10. package/dist/daemon-turn-reconciliation.js.map +1 -0
  11. package/dist/index.js +273 -275
  12. package/dist/index.js.map +4 -4
  13. package/dist/native-sessions/acp-adapter.d.ts +3 -0
  14. package/dist/native-sessions/acp-adapter.d.ts.map +1 -0
  15. package/dist/native-sessions/{claude-adapter.js → acp-adapter.js} +2 -2
  16. package/dist/native-sessions/acp-adapter.js.map +1 -0
  17. package/dist/native-sessions/agent-adapter.d.ts.map +1 -1
  18. package/dist/native-sessions/agent-adapter.js +3 -7
  19. package/dist/native-sessions/agent-adapter.js.map +1 -1
  20. package/dist/native-sessions/chat-agent.js +2 -2
  21. package/dist/native-sessions/chat-agent.js.map +1 -1
  22. package/dist/run-daemon.d.ts.map +1 -1
  23. package/dist/run-daemon.js +6 -255
  24. package/dist/run-daemon.js.map +1 -1
  25. package/dist/runtime.js +273 -275
  26. package/dist/runtime.js.map +4 -4
  27. package/package.json +1 -1
  28. package/dist/native-sessions/claude-adapter.d.ts +0 -3
  29. package/dist/native-sessions/claude-adapter.d.ts.map +0 -1
  30. package/dist/native-sessions/claude-adapter.js.map +0 -1
  31. package/dist/native-sessions/opencode-adapter.d.ts +0 -3
  32. package/dist/native-sessions/opencode-adapter.d.ts.map +0 -1
  33. package/dist/native-sessions/opencode-adapter.js +0 -7
  34. package/dist/native-sessions/opencode-adapter.js.map +0 -1
package/dist/runtime.js CHANGED
@@ -26297,7 +26297,7 @@ function createDaemonConfig(connection, agents = [], installableAgents = [], loc
26297
26297
  // src/run-daemon.ts
26298
26298
  import { randomUUID as randomUUID4 } from "node:crypto";
26299
26299
  import { styleText } from "node:util";
26300
- import { fetch as fetch4, WebSocket as WebSocket4 } from "undici";
26300
+ import { WebSocket as WebSocket5 } from "undici";
26301
26301
 
26302
26302
  // src/infra/operation-metrics.ts
26303
26303
  var AccumulatingOperationMetrics = class {
@@ -26352,7 +26352,7 @@ var nativeSessionPageSchema = external_exports.object({
26352
26352
 
26353
26353
  // src/native-sessions/chat-agent.ts
26354
26354
  function supportsNativeChat(agentId) {
26355
- return agentId === "codex" || agentId === "claude" || agentId === "opencode";
26355
+ return agentId === "codex" || agentId === "claude" || agentId === "opencode" || agentId === "pi";
26356
26356
  }
26357
26357
  function isNativeChatSessionId(agentId, sessionId) {
26358
26358
  if (!nativeSessionIdSchema.safeParse(sessionId).success) {
@@ -26361,7 +26361,7 @@ function isNativeChatSessionId(agentId, sessionId) {
26361
26361
  if (agentId === "opencode") {
26362
26362
  return /^ses_[a-zA-Z0-9]+$/.test(sessionId);
26363
26363
  }
26364
- return (agentId === "codex" || agentId === "claude") && /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/iu.test(sessionId);
26364
+ return (agentId === "codex" || agentId === "claude" || agentId === "pi") && /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/iu.test(sessionId);
26365
26365
  }
26366
26366
 
26367
26367
  // src/acp/process-capacity.ts
@@ -31985,6 +31985,9 @@ var DaemonDiagnosticLog = class {
31985
31985
  };
31986
31986
  };
31987
31987
 
31988
+ // src/daemon-observers.ts
31989
+ import { WebSocket as WebSocket2 } from "undici";
31990
+
31988
31991
  // src/daemon-session-commands.ts
31989
31992
  import { basename as basename5 } from "node:path";
31990
31993
  import { WebSocket } from "undici";
@@ -33182,6 +33185,253 @@ async function executePrompt(outbox, publisher, command, config2, directories, a
33182
33185
  }
33183
33186
  }
33184
33187
 
33188
+ // src/daemon-observers.ts
33189
+ var DaemonAcpObserver = class {
33190
+ #record;
33191
+ constructor(record2) {
33192
+ this.#record = record2;
33193
+ }
33194
+ onCommands = (event) => {
33195
+ this.#record(event.sessionId, event);
33196
+ return Promise.resolve();
33197
+ };
33198
+ onConfig = (event) => {
33199
+ this.#record(event.sessionId, event);
33200
+ return Promise.resolve();
33201
+ };
33202
+ onContent = (event) => {
33203
+ this.#record(event.sessionId, event);
33204
+ return Promise.resolve();
33205
+ };
33206
+ onElicitation = (event) => {
33207
+ this.#record(event.sessionId, event);
33208
+ return Promise.resolve();
33209
+ };
33210
+ onElicitationResolved = (event) => {
33211
+ this.#record(event.sessionId, event);
33212
+ return Promise.resolve();
33213
+ };
33214
+ onInfo = (event) => {
33215
+ this.#record(event.sessionId, event);
33216
+ return Promise.resolve();
33217
+ };
33218
+ onPermission = (event) => {
33219
+ this.#record(event.sessionId, event);
33220
+ return Promise.resolve();
33221
+ };
33222
+ onPermissionResolved = (event) => {
33223
+ this.#record(event.sessionId, event);
33224
+ return Promise.resolve();
33225
+ };
33226
+ onUsage = (event) => {
33227
+ this.#record(event.sessionId, event);
33228
+ return Promise.resolve();
33229
+ };
33230
+ };
33231
+ var AuditInstallationEvents = class {
33232
+ #audit;
33233
+ constructor(audit) {
33234
+ this.#audit = audit;
33235
+ }
33236
+ record = (event, requestId, agentId, details = {}) => {
33237
+ this.#audit.record({
33238
+ commandId: requestId,
33239
+ details,
33240
+ event,
33241
+ sessionId: `agent-installation:${agentId}`
33242
+ });
33243
+ };
33244
+ };
33245
+ var DaemonInstallationObserver = class {
33246
+ #started = /* @__PURE__ */ new Set();
33247
+ #socket = null;
33248
+ connect = (socket) => {
33249
+ this.#socket = socket;
33250
+ };
33251
+ disconnect = (socket) => {
33252
+ if (this.#socket === socket) {
33253
+ this.#socket = null;
33254
+ }
33255
+ };
33256
+ #send = (socket, message) => {
33257
+ try {
33258
+ send(socket, message);
33259
+ } catch {
33260
+ if (this.#socket === socket) {
33261
+ this.#socket = null;
33262
+ }
33263
+ }
33264
+ };
33265
+ onState = (state) => {
33266
+ const socket = this.#socket;
33267
+ if (socket === null || socket.readyState !== WebSocket2.OPEN) {
33268
+ return Promise.resolve();
33269
+ }
33270
+ if (state instanceof RequestedInstallation) {
33271
+ return Promise.resolve();
33272
+ }
33273
+ if (state instanceof RunningInstallation) {
33274
+ if (!this.#started.has(state.requestId)) {
33275
+ this.#started.add(state.requestId);
33276
+ this.#send(socket, {
33277
+ agentId: state.agentId,
33278
+ kind: AGENT_INSTALL_STARTED_KIND,
33279
+ requestId: state.requestId
33280
+ });
33281
+ }
33282
+ this.#send(socket, {
33283
+ agentId: state.agentId,
33284
+ kind: AGENT_INSTALL_PROGRESS_KIND,
33285
+ requestId: state.requestId,
33286
+ stage: state.stage
33287
+ });
33288
+ return Promise.resolve();
33289
+ }
33290
+ if (state instanceof SucceededInstallation) {
33291
+ this.#send(socket, {
33292
+ agentId: state.agentId,
33293
+ authenticationMayBeRequired: state.authenticationMayBeRequired,
33294
+ kind: AGENT_INSTALL_COMPLETED_KIND,
33295
+ requestId: state.requestId
33296
+ });
33297
+ return Promise.resolve();
33298
+ }
33299
+ if (state instanceof FailedInstallation) {
33300
+ this.#send(socket, {
33301
+ agentId: state.agentId,
33302
+ code: state.code,
33303
+ kind: AGENT_INSTALL_FAILED_KIND,
33304
+ message: state.message,
33305
+ requestId: state.requestId
33306
+ });
33307
+ return Promise.resolve();
33308
+ }
33309
+ if (state instanceof CancelledInstallation) {
33310
+ this.#send(socket, {
33311
+ agentId: state.agentId,
33312
+ kind: AGENT_INSTALL_CANCELLED_KIND,
33313
+ requestId: state.requestId
33314
+ });
33315
+ return Promise.resolve();
33316
+ }
33317
+ throw new Error("Unsupported Agent integration installation state");
33318
+ };
33319
+ };
33320
+
33321
+ // src/daemon-turn-reconciliation.ts
33322
+ import { fetch as fetch4 } from "undici";
33323
+ var ACTIVE_TURN_RECONCILIATION_TIMEOUT_MILLISECONDS = 2e4;
33324
+ var ActiveDaemonTurn = class {
33325
+ attemptId;
33326
+ command;
33327
+ #controller;
33328
+ #finished = Promise.withResolvers();
33329
+ #cancellationRequested = false;
33330
+ constructor(command, attemptId) {
33331
+ this.attemptId = attemptId;
33332
+ this.command = command;
33333
+ this.#controller = new AbortController();
33334
+ }
33335
+ abortDaemon = () => {
33336
+ this.#controller.abort();
33337
+ };
33338
+ finish = () => this.#finished.resolve();
33339
+ matches = (cancellation) => cancellation.commandId === this.command.commandId && cancellation.sessionId === this.command.sessionId;
33340
+ requestCancellation = () => {
33341
+ if (this.#cancellationRequested) {
33342
+ return false;
33343
+ }
33344
+ this.#cancellationRequested = true;
33345
+ return true;
33346
+ };
33347
+ get signal() {
33348
+ return this.#controller.signal;
33349
+ }
33350
+ get completion() {
33351
+ return this.#finished.promise;
33352
+ }
33353
+ };
33354
+ var ActiveTurnReconciliationError = class extends Error {
33355
+ code = "active_turn_reconciliation_failed";
33356
+ diagnosticDetails;
33357
+ relayErrorCode;
33358
+ terminal;
33359
+ constructor(message, options = {}) {
33360
+ super(message, options);
33361
+ this.name = "ActiveTurnReconciliationError";
33362
+ this.relayErrorCode = options.relayErrorCode ?? null;
33363
+ this.terminal = options.terminal ?? false;
33364
+ this.diagnosticDetails = this.relayErrorCode === null ? {} : { relayErrorCode: this.relayErrorCode };
33365
+ }
33366
+ };
33367
+ var TERMINAL_RECONCILIATION_RELAY_CODES = /* @__PURE__ */ new Set(["session_conflict"]);
33368
+ async function relayErrorCode(response) {
33369
+ try {
33370
+ const body = JSON.parse(await response.text());
33371
+ if (typeof body !== "object" || body === null) {
33372
+ return null;
33373
+ }
33374
+ const error62 = Reflect.get(body, "error");
33375
+ if (typeof error62 !== "object" || error62 === null) {
33376
+ return null;
33377
+ }
33378
+ const code = Reflect.get(error62, "code");
33379
+ return typeof code === "string" && code.length > 0 ? code : null;
33380
+ } catch {
33381
+ return null;
33382
+ }
33383
+ }
33384
+ function activeTurnReconciliationEndpoint(config2, turn) {
33385
+ const endpoint = relayHttpEndpoint(config2.relayUrl);
33386
+ let channelPath = "";
33387
+ if (endpoint.pathname.endsWith("/daemon")) {
33388
+ channelPath = endpoint.pathname.slice(0, -"/daemon".length);
33389
+ } else {
33390
+ const runtimePath = "/v1/runtimes/connect";
33391
+ if (!endpoint.pathname.endsWith(runtimePath)) {
33392
+ throw new ActiveTurnReconciliationError("The Relay URL is invalid");
33393
+ }
33394
+ channelPath = `${endpoint.pathname.slice(0, -runtimePath.length)}/channels/${encodeURIComponent(config2.channelId)}`;
33395
+ }
33396
+ endpoint.pathname = `${channelPath}/sessions/${encodeURIComponent(turn.command.sessionId)}/attempts/${encodeURIComponent(turn.attemptId)}/reconciliation`;
33397
+ return endpoint;
33398
+ }
33399
+ async function reconcileActiveTurn(config2, turn, signal) {
33400
+ signal.throwIfAborted();
33401
+ const timeoutSignal = AbortSignal.timeout(ACTIVE_TURN_RECONCILIATION_TIMEOUT_MILLISECONDS);
33402
+ const requestSignal = AbortSignal.any([signal, timeoutSignal]);
33403
+ let response;
33404
+ try {
33405
+ response = await fetch4(activeTurnReconciliationEndpoint(config2, turn), {
33406
+ body: JSON.stringify({ outcome: { kind: "running" } }),
33407
+ dispatcher: environmentProxyDispatcher,
33408
+ headers: { ...runtimeAuthorization(config2.relayUrl), "content-type": "application/json" },
33409
+ method: "POST",
33410
+ signal: requestSignal
33411
+ });
33412
+ } catch (error62) {
33413
+ if (signal.aborted) {
33414
+ signal.throwIfAborted();
33415
+ }
33416
+ throw new ActiveTurnReconciliationError(
33417
+ timeoutSignal.aborted ? "The active Agent turn reconciliation timed out" : "The active Agent turn could not be reconciled",
33418
+ { cause: error62 }
33419
+ );
33420
+ }
33421
+ if (!response.ok) {
33422
+ const errorCode = await relayErrorCode(response);
33423
+ const terminal = errorCode !== null && TERMINAL_RECONCILIATION_RELAY_CODES.has(errorCode);
33424
+ throw new ActiveTurnReconciliationError(
33425
+ terminal ? `The Relay reported that the active Agent turn can no longer be reconciled (${errorCode})` : `The Relay rejected the active Agent turn reconciliation with status ${response.status.toString()}`,
33426
+ { relayErrorCode: errorCode, terminal }
33427
+ );
33428
+ }
33429
+ await response.body?.cancel();
33430
+ }
33431
+ async function reconcileActiveTurns(config2, turns, signal) {
33432
+ await Promise.all(turns.map(async (turn) => await reconcileActiveTurn(config2, turn, signal)));
33433
+ }
33434
+
33185
33435
  // src/native-sessions/codex-history.ts
33186
33436
  import { spawn as spawn5 } from "node:child_process";
33187
33437
  var responseSchema = external_exports.union([
@@ -33644,8 +33894,8 @@ async function readAcpHistory(sessions, directories, bindingId, options, nativeS
33644
33894
  throw new AcpExecutionError("The native Session was not found.", {}, "native_session_not_found");
33645
33895
  }
33646
33896
 
33647
- // src/native-sessions/claude-adapter.ts
33648
- var claudeSessionAdapter = {
33897
+ // src/native-sessions/acp-adapter.ts
33898
+ var acpSessionAdapter = {
33649
33899
  linkedHistory: async ({ sessions, directories, bindingId, options, nativeSessionId }) => await readAcpHistory(sessions, directories, bindingId, options, nativeSessionId),
33650
33900
  history: async ({ sessions, directories, bindingId, options, nativeSessionId }) => await readAcpHistory(sessions, directories, bindingId, options, nativeSessionId),
33651
33901
  preview: async ({ sessions, options, nativeSessionId }) => await sessions.previewNativeSession(options, nativeSessionId)
@@ -33673,13 +33923,6 @@ var codexSessionAdapter = {
33673
33923
  preview: async (context) => await verifiedHistory(context, false)
33674
33924
  };
33675
33925
 
33676
- // src/native-sessions/opencode-adapter.ts
33677
- var openCodeSessionAdapter = {
33678
- linkedHistory: async ({ sessions, directories, bindingId, options, nativeSessionId }) => await readAcpHistory(sessions, directories, bindingId, options, nativeSessionId),
33679
- history: async ({ sessions, directories, bindingId, options, nativeSessionId }) => await readAcpHistory(sessions, directories, bindingId, options, nativeSessionId),
33680
- preview: async ({ sessions, options, nativeSessionId }) => await sessions.previewNativeSession(options, nativeSessionId)
33681
- };
33682
-
33683
33926
  // src/native-sessions/agent-adapter.ts
33684
33927
  var genericSessionAdapter = {
33685
33928
  linkedHistory: async () => {
@@ -33694,11 +33937,8 @@ function nativeSessionAdapter(agentId) {
33694
33937
  if (agentId === "codex") {
33695
33938
  return codexSessionAdapter;
33696
33939
  }
33697
- if (agentId === "claude") {
33698
- return claudeSessionAdapter;
33699
- }
33700
- if (agentId === "opencode") {
33701
- return openCodeSessionAdapter;
33940
+ if (agentId === "claude" || agentId === "opencode" || agentId === "pi") {
33941
+ return acpSessionAdapter;
33702
33942
  }
33703
33943
  return genericSessionAdapter;
33704
33944
  }
@@ -34184,7 +34424,7 @@ var NativeSessionService = class {
34184
34424
  };
34185
34425
 
34186
34426
  // src/relay-connection/connect.ts
34187
- import { WebSocket as WebSocket2 } from "undici";
34427
+ import { WebSocket as WebSocket3 } from "undici";
34188
34428
 
34189
34429
  // src/relay-connection/runtime-revocation.ts
34190
34430
  var runtimeRevocationCloseCode = 1008;
@@ -34222,7 +34462,7 @@ function connectRelay(url2, signal) {
34222
34462
  const endpoint = new URL(url2);
34223
34463
  const headers = runtimeAuthorization(endpoint);
34224
34464
  endpoint.searchParams.delete("token");
34225
- const socket = new WebSocket2(endpoint, {
34465
+ const socket = new WebSocket3(endpoint, {
34226
34466
  dispatcher: environmentProxyDispatcher,
34227
34467
  headers
34228
34468
  });
@@ -35111,7 +35351,7 @@ function isDaemonExecutionState(value) {
35111
35351
  }
35112
35352
 
35113
35353
  // src/relay-connection/session-event-publisher.ts
35114
- import { WebSocket as WebSocket3 } from "undici";
35354
+ import { WebSocket as WebSocket4 } from "undici";
35115
35355
  var DaemonSessionEventConnectionState = class {
35116
35356
  };
35117
35357
  var DisconnectedDaemonSessionEventState = class extends DaemonSessionEventConnectionState {
@@ -35128,7 +35368,7 @@ var ConnectedDaemonSessionEventState = class extends DaemonSessionEventConnectio
35128
35368
  disconnect = (socket) => socket === this.#socket ? new DisconnectedDaemonSessionEventState() : this;
35129
35369
  matches = (socket) => socket === this.#socket;
35130
35370
  send = (message) => {
35131
- if (this.#socket.readyState !== WebSocket3.OPEN) {
35371
+ if (this.#socket.readyState !== WebSocket4.OPEN) {
35132
35372
  return false;
35133
35373
  }
35134
35374
  try {
@@ -35355,249 +35595,7 @@ var systemDaemonTimer = new SystemDaemonTimer();
35355
35595
 
35356
35596
  // src/run-daemon.ts
35357
35597
  var RELAY_RECONNECT_DELAY_MILLISECONDS = 500;
35358
- var ACTIVE_TURN_RECONCILIATION_TIMEOUT_MILLISECONDS = 2e4;
35359
35598
  var ACTIVE_TURN_RECONCILIATION_CLOSE_CODE = 4009;
35360
- var DaemonAcpObserver = class {
35361
- #record;
35362
- constructor(record2) {
35363
- this.#record = record2;
35364
- }
35365
- onCommands = (event) => {
35366
- this.#record(event.sessionId, event);
35367
- return Promise.resolve();
35368
- };
35369
- onConfig = (event) => {
35370
- this.#record(event.sessionId, event);
35371
- return Promise.resolve();
35372
- };
35373
- onContent = (event) => {
35374
- this.#record(event.sessionId, event);
35375
- return Promise.resolve();
35376
- };
35377
- onElicitation = (event) => {
35378
- this.#record(event.sessionId, event);
35379
- return Promise.resolve();
35380
- };
35381
- onElicitationResolved = (event) => {
35382
- this.#record(event.sessionId, event);
35383
- return Promise.resolve();
35384
- };
35385
- onInfo = (event) => {
35386
- this.#record(event.sessionId, event);
35387
- return Promise.resolve();
35388
- };
35389
- onPermission = (event) => {
35390
- this.#record(event.sessionId, event);
35391
- return Promise.resolve();
35392
- };
35393
- onPermissionResolved = (event) => {
35394
- this.#record(event.sessionId, event);
35395
- return Promise.resolve();
35396
- };
35397
- onUsage = (event) => {
35398
- this.#record(event.sessionId, event);
35399
- return Promise.resolve();
35400
- };
35401
- };
35402
- var AuditInstallationEvents = class {
35403
- #audit;
35404
- constructor(audit) {
35405
- this.#audit = audit;
35406
- }
35407
- record = (event, requestId, agentId, details = {}) => {
35408
- this.#audit.record({
35409
- commandId: requestId,
35410
- details,
35411
- event,
35412
- sessionId: `agent-installation:${agentId}`
35413
- });
35414
- };
35415
- };
35416
- var DaemonInstallationObserver = class {
35417
- #started = /* @__PURE__ */ new Set();
35418
- #socket = null;
35419
- connect = (socket) => {
35420
- this.#socket = socket;
35421
- };
35422
- disconnect = (socket) => {
35423
- if (this.#socket === socket) {
35424
- this.#socket = null;
35425
- }
35426
- };
35427
- #send = (socket, message) => {
35428
- try {
35429
- send(socket, message);
35430
- } catch {
35431
- if (this.#socket === socket) {
35432
- this.#socket = null;
35433
- }
35434
- }
35435
- };
35436
- onState = (state) => {
35437
- const socket = this.#socket;
35438
- if (socket === null || socket.readyState !== WebSocket4.OPEN) {
35439
- return Promise.resolve();
35440
- }
35441
- if (state instanceof RequestedInstallation) {
35442
- return Promise.resolve();
35443
- }
35444
- if (state instanceof RunningInstallation) {
35445
- if (!this.#started.has(state.requestId)) {
35446
- this.#started.add(state.requestId);
35447
- this.#send(socket, {
35448
- agentId: state.agentId,
35449
- kind: AGENT_INSTALL_STARTED_KIND,
35450
- requestId: state.requestId
35451
- });
35452
- }
35453
- this.#send(socket, {
35454
- agentId: state.agentId,
35455
- kind: AGENT_INSTALL_PROGRESS_KIND,
35456
- requestId: state.requestId,
35457
- stage: state.stage
35458
- });
35459
- return Promise.resolve();
35460
- }
35461
- if (state instanceof SucceededInstallation) {
35462
- this.#send(socket, {
35463
- agentId: state.agentId,
35464
- authenticationMayBeRequired: state.authenticationMayBeRequired,
35465
- kind: AGENT_INSTALL_COMPLETED_KIND,
35466
- requestId: state.requestId
35467
- });
35468
- return Promise.resolve();
35469
- }
35470
- if (state instanceof FailedInstallation) {
35471
- this.#send(socket, {
35472
- agentId: state.agentId,
35473
- code: state.code,
35474
- kind: AGENT_INSTALL_FAILED_KIND,
35475
- message: state.message,
35476
- requestId: state.requestId
35477
- });
35478
- return Promise.resolve();
35479
- }
35480
- if (state instanceof CancelledInstallation) {
35481
- this.#send(socket, {
35482
- agentId: state.agentId,
35483
- kind: AGENT_INSTALL_CANCELLED_KIND,
35484
- requestId: state.requestId
35485
- });
35486
- return Promise.resolve();
35487
- }
35488
- throw new Error("Unsupported Agent integration installation state");
35489
- };
35490
- };
35491
- var ActiveDaemonTurn = class {
35492
- attemptId;
35493
- command;
35494
- #controller;
35495
- #finished = Promise.withResolvers();
35496
- #cancellationRequested = false;
35497
- constructor(command, attemptId) {
35498
- this.attemptId = attemptId;
35499
- this.command = command;
35500
- this.#controller = new AbortController();
35501
- }
35502
- abortDaemon = () => {
35503
- this.#controller.abort();
35504
- };
35505
- finish = () => this.#finished.resolve();
35506
- matches = (cancellation) => cancellation.commandId === this.command.commandId && cancellation.sessionId === this.command.sessionId;
35507
- requestCancellation = () => {
35508
- if (this.#cancellationRequested) {
35509
- return false;
35510
- }
35511
- this.#cancellationRequested = true;
35512
- return true;
35513
- };
35514
- get signal() {
35515
- return this.#controller.signal;
35516
- }
35517
- get completion() {
35518
- return this.#finished.promise;
35519
- }
35520
- };
35521
- var ActiveTurnReconciliationError = class extends Error {
35522
- code = "active_turn_reconciliation_failed";
35523
- diagnosticDetails;
35524
- relayErrorCode;
35525
- terminal;
35526
- constructor(message, options = {}) {
35527
- super(message, options);
35528
- this.name = "ActiveTurnReconciliationError";
35529
- this.relayErrorCode = options.relayErrorCode ?? null;
35530
- this.terminal = options.terminal ?? false;
35531
- this.diagnosticDetails = this.relayErrorCode === null ? {} : { relayErrorCode: this.relayErrorCode };
35532
- }
35533
- };
35534
- var TERMINAL_RECONCILIATION_RELAY_CODES = /* @__PURE__ */ new Set(["session_conflict"]);
35535
- async function relayErrorCode(response) {
35536
- try {
35537
- const body = JSON.parse(await response.text());
35538
- if (typeof body !== "object" || body === null) {
35539
- return null;
35540
- }
35541
- const error62 = Reflect.get(body, "error");
35542
- if (typeof error62 !== "object" || error62 === null) {
35543
- return null;
35544
- }
35545
- const code = Reflect.get(error62, "code");
35546
- return typeof code === "string" && code.length > 0 ? code : null;
35547
- } catch {
35548
- return null;
35549
- }
35550
- }
35551
- function activeTurnReconciliationEndpoint(config2, turn) {
35552
- const endpoint = relayHttpEndpoint(config2.relayUrl);
35553
- let channelPath = "";
35554
- if (endpoint.pathname.endsWith("/daemon")) {
35555
- channelPath = endpoint.pathname.slice(0, -"/daemon".length);
35556
- } else {
35557
- const runtimePath = "/v1/runtimes/connect";
35558
- if (!endpoint.pathname.endsWith(runtimePath)) {
35559
- throw new ActiveTurnReconciliationError("The Relay URL is invalid");
35560
- }
35561
- channelPath = `${endpoint.pathname.slice(0, -runtimePath.length)}/channels/${encodeURIComponent(config2.channelId)}`;
35562
- }
35563
- endpoint.pathname = `${channelPath}/sessions/${encodeURIComponent(turn.command.sessionId)}/attempts/${encodeURIComponent(turn.attemptId)}/reconciliation`;
35564
- return endpoint;
35565
- }
35566
- async function reconcileActiveTurn(config2, turn, signal) {
35567
- signal.throwIfAborted();
35568
- const timeoutSignal = AbortSignal.timeout(ACTIVE_TURN_RECONCILIATION_TIMEOUT_MILLISECONDS);
35569
- const requestSignal = AbortSignal.any([signal, timeoutSignal]);
35570
- let response;
35571
- try {
35572
- response = await fetch4(activeTurnReconciliationEndpoint(config2, turn), {
35573
- body: JSON.stringify({ outcome: { kind: "running" } }),
35574
- dispatcher: environmentProxyDispatcher,
35575
- headers: { ...runtimeAuthorization(config2.relayUrl), "content-type": "application/json" },
35576
- method: "POST",
35577
- signal: requestSignal
35578
- });
35579
- } catch (error62) {
35580
- if (signal.aborted) {
35581
- signal.throwIfAborted();
35582
- }
35583
- throw new ActiveTurnReconciliationError(
35584
- timeoutSignal.aborted ? "The active Agent turn reconciliation timed out" : "The active Agent turn could not be reconciled",
35585
- { cause: error62 }
35586
- );
35587
- }
35588
- if (!response.ok) {
35589
- const errorCode = await relayErrorCode(response);
35590
- const terminal = errorCode !== null && TERMINAL_RECONCILIATION_RELAY_CODES.has(errorCode);
35591
- throw new ActiveTurnReconciliationError(
35592
- terminal ? `The Relay reported that the active Agent turn can no longer be reconciled (${errorCode})` : `The Relay rejected the active Agent turn reconciliation with status ${response.status.toString()}`,
35593
- { relayErrorCode: errorCode, terminal }
35594
- );
35595
- }
35596
- await response.body?.cancel();
35597
- }
35598
- async function reconcileActiveTurns(config2, turns, signal) {
35599
- await Promise.all(turns.map(async (turn) => await reconcileActiveTurn(config2, turn, signal)));
35600
- }
35601
35599
  function closeRelaySocket(socket, code, reason) {
35602
35600
  try {
35603
35601
  socket.close(code, reason);
@@ -35965,14 +35963,14 @@ async function runRelayConnection(config2, directories, audit, sessions, install
35965
35963
  };
35966
35964
  const connectionLiveness = new RelayConnectionLiveness(timer, {
35967
35965
  send: (message) => {
35968
- if (socket.readyState !== WebSocket4.OPEN) {
35966
+ if (socket.readyState !== WebSocket5.OPEN) {
35969
35967
  return false;
35970
35968
  }
35971
35969
  try {
35972
35970
  send(socket, message);
35973
35971
  return true;
35974
35972
  } catch (error62) {
35975
- if (socket.readyState !== WebSocket4.OPEN) {
35973
+ if (socket.readyState !== WebSocket5.OPEN) {
35976
35974
  return false;
35977
35975
  }
35978
35976
  throw error62;
@@ -36098,7 +36096,7 @@ async function runRelayConnection(config2, directories, audit, sessions, install
36098
36096
  (request) => request.agentId === command2.agentId && !request.controller.signal.aborted
36099
36097
  );
36100
36098
  if (agentSettingsDiscoveries.has(command2.requestId) || existing !== void 0) {
36101
- if (socket.readyState === WebSocket4.OPEN) {
36099
+ if (socket.readyState === WebSocket5.OPEN) {
36102
36100
  send(socket, {
36103
36101
  code: "agent_settings_discovery_busy",
36104
36102
  kind: AGENT_SETTINGS_DISCOVERY_FAILED_KIND,
@@ -36109,7 +36107,7 @@ async function runRelayConnection(config2, directories, audit, sessions, install
36109
36107
  return;
36110
36108
  }
36111
36109
  if (agentSettingsDiscoveries.size >= 4) {
36112
- if (socket.readyState === WebSocket4.OPEN) {
36110
+ if (socket.readyState === WebSocket5.OPEN) {
36113
36111
  send(socket, {
36114
36112
  code: "agent_settings_discovery_busy",
36115
36113
  kind: AGENT_SETTINGS_DISCOVERY_FAILED_KIND,
@@ -36167,7 +36165,7 @@ async function runRelayConnection(config2, directories, audit, sessions, install
36167
36165
  event: "agent_settings_discovered",
36168
36166
  sessionId: `agent-settings:${command2.agentId}`
36169
36167
  });
36170
- if (socket.readyState === WebSocket4.OPEN) {
36168
+ if (socket.readyState === WebSocket5.OPEN) {
36171
36169
  send(socket, result.event);
36172
36170
  }
36173
36171
  } catch (error62) {
@@ -36195,7 +36193,7 @@ async function runRelayConnection(config2, directories, audit, sessions, install
36195
36193
  `
36196
36194
  );
36197
36195
  }
36198
- if (socket.readyState === WebSocket4.OPEN) {
36196
+ if (socket.readyState === WebSocket5.OPEN) {
36199
36197
  send(socket, {
36200
36198
  code: operationErrorCode(failure2),
36201
36199
  kind: AGENT_SETTINGS_DISCOVERY_FAILED_KIND,
@@ -36218,7 +36216,7 @@ async function runRelayConnection(config2, directories, audit, sessions, install
36218
36216
  if (kind === AGENT_CATALOG_REFRESH_KIND) {
36219
36217
  const command2 = message;
36220
36218
  if (agentCatalogRefreshes.has(command2.requestId) || agentCatalogRefreshes.size > 0) {
36221
- if (socket.readyState === WebSocket4.OPEN) {
36219
+ if (socket.readyState === WebSocket5.OPEN) {
36222
36220
  send(socket, {
36223
36221
  code: "agent_catalog_refresh_busy",
36224
36222
  kind: AGENT_CATALOG_REFRESH_FAILED_KIND,
@@ -36248,7 +36246,7 @@ async function runRelayConnection(config2, directories, audit, sessions, install
36248
36246
  event: "agent_catalog_refreshed",
36249
36247
  sessionId: "agent-catalog"
36250
36248
  });
36251
- if (socket.readyState === WebSocket4.OPEN) {
36249
+ if (socket.readyState === WebSocket5.OPEN) {
36252
36250
  send(socket, {
36253
36251
  kind: AGENT_CATALOG_REFRESHED_KIND,
36254
36252
  requestId: command2.requestId
@@ -36272,7 +36270,7 @@ async function runRelayConnection(config2, directories, audit, sessions, install
36272
36270
  { code: operationErrorCode(failure2) },
36273
36271
  failure2
36274
36272
  );
36275
- if (socket.readyState === WebSocket4.OPEN) {
36273
+ if (socket.readyState === WebSocket5.OPEN) {
36276
36274
  send(socket, {
36277
36275
  code: operationErrorCode(failure2),
36278
36276
  kind: AGENT_CATALOG_REFRESH_FAILED_KIND,
@@ -36341,7 +36339,7 @@ async function runRelayConnection(config2, directories, audit, sessions, install
36341
36339
  }
36342
36340
  operationSignal.throwIfAborted();
36343
36341
  const result = await nativeSessions.execute(command2, operationSignal);
36344
- if (socket.readyState === WebSocket4.OPEN) {
36342
+ if (socket.readyState === WebSocket5.OPEN) {
36345
36343
  send(socket, result);
36346
36344
  }
36347
36345
  } catch (error62) {
@@ -36432,7 +36430,7 @@ async function runRelayConnection(config2, directories, audit, sessions, install
36432
36430
  }
36433
36431
  operationSignal.throwIfAborted();
36434
36432
  const result = await nativeSessions.executeTurnFork(command2, operationSignal);
36435
- if (socket.readyState === WebSocket4.OPEN) {
36433
+ if (socket.readyState === WebSocket5.OPEN) {
36436
36434
  send(socket, result);
36437
36435
  }
36438
36436
  } catch (error62) {
@@ -36486,7 +36484,7 @@ async function runRelayConnection(config2, directories, audit, sessions, install
36486
36484
  (message2) => send(socket, message2),
36487
36485
  connectionController.signal
36488
36486
  ).catch((error62) => {
36489
- if (socket.readyState !== WebSocket4.OPEN || connectionController.signal.aborted) {
36487
+ if (socket.readyState !== WebSocket5.OPEN || connectionController.signal.aborted) {
36490
36488
  return;
36491
36489
  }
36492
36490
  send(socket, {