@tylerl0706/ahpx 0.2.8 → 0.2.9

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/bin.js CHANGED
@@ -11,7 +11,7 @@ import {
11
11
  createLogger,
12
12
  ensureFileUri,
13
13
  setVerbose
14
- } from "./chunk-B6RV43UP.js";
14
+ } from "./chunk-TCJ6NC7B.js";
15
15
 
16
16
  // src/bin.ts
17
17
  import { randomUUID as randomUUID3 } from "crypto";
@@ -2680,26 +2680,32 @@ async function resolveOrCreateSession(client, serverInfo, opts, cfg, cwd, gitRoo
2680
2680
  function waitForReady(client, sessionUri) {
2681
2681
  return new Promise((resolve3, reject) => {
2682
2682
  const timeout = setTimeout(() => {
2683
+ cleanup();
2683
2684
  reject(new TimeoutError("Timed out waiting for session to be ready"));
2684
2685
  }, 3e4);
2685
- client.on("action", (envelope) => {
2686
+ const onAction = (envelope) => {
2686
2687
  const action = envelope.action;
2687
2688
  if (action.type === "session/ready" /* SessionReady */ && action.session === sessionUri) {
2688
- clearTimeout(timeout);
2689
+ cleanup();
2689
2690
  resolve3();
2690
2691
  } else if (action.type === "session/creationFailed" /* SessionCreationFailed */ && action.session === sessionUri) {
2691
- clearTimeout(timeout);
2692
+ cleanup();
2692
2693
  const sessionState2 = client.state.getSession(sessionUri);
2693
2694
  const errMsg = sessionState2?.creationError?.message ?? "Unknown error";
2694
2695
  reject(new AhpxError(`Session creation failed: ${errMsg}`, ExitCode.Error));
2695
2696
  }
2696
- });
2697
+ };
2698
+ const cleanup = () => {
2699
+ clearTimeout(timeout);
2700
+ client.removeListener("action", onAction);
2701
+ };
2702
+ client.on("action", onAction);
2697
2703
  const sessionState = client.state.getSession(sessionUri);
2698
2704
  if (sessionState?.lifecycle === "ready") {
2699
- clearTimeout(timeout);
2705
+ cleanup();
2700
2706
  resolve3();
2701
2707
  } else if (sessionState?.lifecycle === "creationFailed") {
2702
- clearTimeout(timeout);
2708
+ cleanup();
2703
2709
  const errMsg = sessionState?.creationError?.message ?? "Unknown error";
2704
2710
  reject(new AhpxError(`Session creation failed: ${errMsg}`, ExitCode.Error));
2705
2711
  }
@@ -604,17 +604,41 @@ function sessionReducer(state, action, log7) {
604
604
  }
605
605
  return next;
606
606
  }
607
- case "session/delta" /* SessionDelta */:
608
- return updateResponsePart(state, action.turnId, action.partId, (part) => {
607
+ case "session/delta" /* SessionDelta */: {
608
+ const updated = updateResponsePart(state, action.turnId, action.partId, (part) => {
609
609
  if (part.kind === "markdown" /* Markdown */) {
610
610
  return { ...part, content: part.content + action.content };
611
611
  }
612
612
  return part;
613
613
  });
614
- case "session/responsePart" /* SessionResponsePart */:
614
+ if (updated === state && state.activeTurn && state.activeTurn.id === action.turnId) {
615
+ return {
616
+ ...state,
617
+ activeTurn: {
618
+ ...state.activeTurn,
619
+ responseParts: [
620
+ ...state.activeTurn.responseParts,
621
+ { kind: "markdown" /* Markdown */, id: action.partId, content: action.content }
622
+ ]
623
+ }
624
+ };
625
+ }
626
+ return updated;
627
+ }
628
+ case "session/responsePart" /* SessionResponsePart */: {
615
629
  if (!state.activeTurn || state.activeTurn.id !== action.turnId) {
616
630
  return state;
617
631
  }
632
+ const newPartId = action.part.kind === "toolCall" /* ToolCall */ ? action.part.toolCall.toolCallId : "id" in action.part ? action.part.id : void 0;
633
+ if (newPartId !== void 0) {
634
+ const exists = state.activeTurn.responseParts.some((p) => {
635
+ const id = p.kind === "toolCall" /* ToolCall */ ? p.toolCall.toolCallId : "id" in p ? p.id : void 0;
636
+ return id === newPartId;
637
+ });
638
+ if (exists) {
639
+ return state;
640
+ }
641
+ }
618
642
  return {
619
643
  ...state,
620
644
  activeTurn: {
@@ -622,6 +646,7 @@ function sessionReducer(state, action, log7) {
622
646
  responseParts: [...state.activeTurn.responseParts, action.part]
623
647
  }
624
648
  };
649
+ }
625
650
  case "session/turnComplete" /* SessionTurnComplete */:
626
651
  return endTurn(state, action.turnId, "complete" /* Complete */, "idle" /* Idle */);
627
652
  case "session/turnCancelled" /* SessionTurnCancelled */:
@@ -904,6 +929,7 @@ var StateMirror = class {
904
929
  rootState = { agents: [] };
905
930
  sessions = /* @__PURE__ */ new Map();
906
931
  serverSeq = 0;
932
+ pendingActions = /* @__PURE__ */ new Map();
907
933
  /** Current root state (agents, active session count). */
908
934
  get root() {
909
935
  return this.rootState;
@@ -922,6 +948,7 @@ var StateMirror = class {
922
948
  }
923
949
  /**
924
950
  * Load a snapshot (from initialize, reconnect, or subscribe).
951
+ * After registering a session, replays any actions that arrived before the snapshot.
925
952
  */
926
953
  applySnapshot(snapshot) {
927
954
  if (snapshot.fromSeq > this.serverSeq) {
@@ -932,6 +959,21 @@ var StateMirror = class {
932
959
  } else if ("summary" in snapshot.state) {
933
960
  const sessionState = snapshot.state;
934
961
  this.sessions.set(snapshot.resource, sessionState);
962
+ const buffered = this.pendingActions.get(snapshot.resource);
963
+ if (buffered) {
964
+ this.pendingActions.delete(snapshot.resource);
965
+ for (const env of buffered) {
966
+ if (env.serverSeq > snapshot.fromSeq) {
967
+ const current = this.sessions.get(snapshot.resource);
968
+ if (current) {
969
+ this.sessions.set(
970
+ snapshot.resource,
971
+ sessionReducer(current, env.action)
972
+ );
973
+ }
974
+ }
975
+ }
976
+ }
935
977
  }
936
978
  }
937
979
  /**
@@ -949,6 +991,13 @@ var StateMirror = class {
949
991
  const current = this.sessions.get(sessionUri);
950
992
  if (current) {
951
993
  this.sessions.set(sessionUri, sessionReducer(current, sessionAction));
994
+ } else {
995
+ let buffer = this.pendingActions.get(sessionUri);
996
+ if (!buffer) {
997
+ buffer = [];
998
+ this.pendingActions.set(sessionUri, buffer);
999
+ }
1000
+ buffer.push(envelope);
952
1001
  }
953
1002
  }
954
1003
  }
@@ -958,6 +1007,7 @@ var StateMirror = class {
958
1007
  */
959
1008
  removeSession(uri) {
960
1009
  this.sessions.delete(uri);
1010
+ this.pendingActions.delete(uri);
961
1011
  }
962
1012
  };
963
1013
 
package/dist/index.d.ts CHANGED
@@ -2450,6 +2450,7 @@ declare class StateMirror {
2450
2450
  private rootState;
2451
2451
  private sessions;
2452
2452
  private serverSeq;
2453
+ private pendingActions;
2453
2454
  /** Current root state (agents, active session count). */
2454
2455
  get root(): IRootState;
2455
2456
  /** Current server sequence number. */
@@ -2460,6 +2461,7 @@ declare class StateMirror {
2460
2461
  get sessionUris(): URI[];
2461
2462
  /**
2462
2463
  * Load a snapshot (from initialize, reconnect, or subscribe).
2464
+ * After registering a session, replays any actions that arrived before the snapshot.
2463
2465
  */
2464
2466
  applySnapshot(snapshot: ISnapshot): void;
2465
2467
  /**
package/dist/index.js CHANGED
@@ -25,7 +25,7 @@ import {
25
25
  ensureFileUri,
26
26
  fileUriToDisplayPath,
27
27
  truncatePreview
28
- } from "./chunk-B6RV43UP.js";
28
+ } from "./chunk-TCJ6NC7B.js";
29
29
 
30
30
  // src/client/connection-pool.ts
31
31
  var ConnectionPool = class {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tylerl0706/ahpx",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
4
4
  "type": "module",
5
5
  "description": "Agent Host Protocol client — use as a library or CLI to manage AHP server connections, sessions, and agent interactions",
6
6
  "license": "MIT",