@blade-hq/agent-client 2612.0.0-beta.87 → 2612.0.0-beta.88

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.
@@ -254,7 +254,7 @@ var ToolProvider = class {
254
254
 
255
255
  // src/version.ts
256
256
  var SDK_NAME = "agent-client";
257
- var SDK_VERSION = true ? "2612.0.0-beta.87" : "1.1.1";
257
+ var SDK_VERSION = true ? "2612.0.0-beta.88" : "1.1.1";
258
258
 
259
259
  // src/auth-login.ts
260
260
  function isAuthCallbackMessage(value) {
@@ -1712,6 +1712,11 @@ var ClientProjectionBuilder = class {
1712
1712
  latestTurns.set(turn.loop_id, turn);
1713
1713
  }
1714
1714
  }
1715
+ for (const id of this.activeCompactions.keys()) {
1716
+ if (turnsById.get(`compaction:${id}`)?.status !== "streaming") {
1717
+ this.activeCompactions.delete(id);
1718
+ }
1719
+ }
1715
1720
  this.latestTurns = latestTurns;
1716
1721
  this.turnsById = turnsById;
1717
1722
  }
@@ -3154,27 +3159,31 @@ function sortedUnique(turns) {
3154
3159
  for (const turn of turns) byId.set(turn.turn_id, turn);
3155
3160
  return [...byId.values()].sort((left, right) => left.sequence - right.sequence);
3156
3161
  }
3162
+ function mergeWithAuthoritative(live, authoritative) {
3163
+ if (!authoritative) return live;
3164
+ if (authoritative.kind === "compaction" && authoritative.status !== "streaming" && live.kind === "compaction" && live.status === "streaming") {
3165
+ return authoritative;
3166
+ }
3167
+ return { ...live, sequence: authoritative.sequence };
3168
+ }
3157
3169
  function prependOlder(current, page) {
3158
3170
  const currentById = new Map(current.map((turn) => [turn.turn_id, turn]));
3159
3171
  return sortedUnique([
3160
3172
  ...current,
3161
3173
  ...page.map((turn) => {
3162
3174
  const live = currentById.get(turn.turn_id);
3163
- return live ? { ...live, sequence: turn.sequence } : turn;
3175
+ return live ? mergeWithAuthoritative(live, turn) : turn;
3164
3176
  })
3165
3177
  ]);
3166
3178
  }
3167
3179
  function replaceWindow(current, page, liveRevisionAtStart, liveRevisions) {
3168
3180
  const pageById = new Map(page.map((turn) => [turn.turn_id, turn]));
3169
3181
  const newerLive = current.filter(
3170
- (turn) => turn.turn_id.startsWith("local-user-") || (liveRevisions.byTurnId.get(turn.turn_id) ?? 0) > liveRevisionAtStart
3182
+ (turn) => turn.turn_id.startsWith("local-user-") || turn.kind === "compaction" && turn.status === "streaming" || (liveRevisions.byTurnId.get(turn.turn_id) ?? 0) > liveRevisionAtStart
3171
3183
  );
3172
3184
  return sortedUnique([
3173
3185
  ...page,
3174
- ...newerLive.map((turn) => {
3175
- const snapshot2 = pageById.get(turn.turn_id);
3176
- return snapshot2 ? { ...turn, sequence: snapshot2.sequence } : turn;
3177
- })
3186
+ ...newerLive.map((turn) => mergeWithAuthoritative(turn, pageById.get(turn.turn_id)))
3178
3187
  ]);
3179
3188
  }
3180
3189
 
@@ -4994,6 +5003,9 @@ function isUserConfirmation(turn) {
4994
5003
  }
4995
5004
  function preserveLocalTurnMetadata(next, previous) {
4996
5005
  if (!previous) return next;
5006
+ if (previous.kind === "compaction" && previous.status !== "streaming" && next.kind === "compaction" && next.status === "streaming") {
5007
+ return { ...previous, sequence: next.sequence };
5008
+ }
4997
5009
  const memoryRefs = next.memory_refs?.length ? next.memory_refs : previous.memory_refs;
4998
5010
  const clientRenderId = next.client_render_id || previous.client_render_id;
4999
5011
  if (memoryRefs === next.memory_refs && clientRenderId === next.client_render_id) return next;
@@ -5339,6 +5351,14 @@ var AgentSession = class _AgentSession {
5339
5351
  epochReloadRequested = 0;
5340
5352
  epochReloadDone = 0;
5341
5353
  epochReloadRunning = false;
5354
+ async reconcileManualCompaction() {
5355
+ const request = this.manualCompactionRequest;
5356
+ if (!request?.runId || !this.runtime.fetchRunResult) return;
5357
+ const result = await this.runtime.fetchRunResult(this.sessionId, request.runId);
5358
+ if (!this.disposed && this.manualCompactionRequest === request && result?.run_id === request.runId) {
5359
+ this.setManualCompactionRequest(null);
5360
+ }
5361
+ }
5342
5362
  setManualCompactionRequest(request) {
5343
5363
  this.manualCompactionRequest = request;
5344
5364
  const pending = request !== null;
@@ -5988,13 +6008,11 @@ var AgentSession = class _AgentSession {
5988
6008
  this.subscriptionReplaySource = null;
5989
6009
  this.scheduleHistoricalCommandRestore(commandRecoveryGeneration);
5990
6010
  }
5991
- await Promise.all([
5992
- this.refreshAuthoritativeHistory({
5993
- liveRevisionAtStart: reconnectLiveRevision,
5994
- modeRevisionAtStart: reconnectModeRevision
5995
- }),
5996
- this.syncSessionFromDetail({ fresh: true })
5997
- ]);
6011
+ const replaced = await this.refreshAuthoritativeHistory({
6012
+ liveRevisionAtStart: reconnectLiveRevision,
6013
+ modeRevisionAtStart: reconnectModeRevision
6014
+ });
6015
+ if (!replaced) await this.syncSessionFromDetail({ fresh: true });
5998
6016
  } catch {
5999
6017
  }
6000
6018
  });
@@ -6017,7 +6035,7 @@ var AgentSession = class _AgentSession {
6017
6035
  }
6018
6036
  this.scheduleHistoricalCommandRestore(commandRecoveryGeneration);
6019
6037
  await Promise.all([
6020
- detail && !detail.replay_state ? Promise.resolve(this.applySessionDetail(detail, statusRevisionAtDetail)) : this.syncSessionFromDetail({ fresh: true }),
6038
+ detail && !detail.replay_state && !this.state.activeCompaction ? Promise.resolve(this.applySessionDetail(detail, statusRevisionAtDetail)) : this.syncSessionFromDetail({ fresh: true }),
6021
6039
  this.refreshQueue()
6022
6040
  ]);
6023
6041
  });
@@ -6032,9 +6050,18 @@ var AgentSession = class _AgentSession {
6032
6050
  }
6033
6051
  async syncSessionFromDetail(options) {
6034
6052
  const statusRevisionAtRequest = this.statusRevision;
6053
+ const compactionRevision = this.liveRevision;
6054
+ const epoch = this.timelineEpoch;
6035
6055
  try {
6036
6056
  const info = await this.runtime.fetchSessionInfo(this.sessionId, options);
6057
+ if (this.disposed || epoch !== this.timelineEpoch) return;
6037
6058
  this.applySessionDetail(info, statusRevisionAtRequest);
6059
+ if (options.fresh && info.history_busy === false) {
6060
+ this.update((state) => withTurns(state, state.turns.filter(
6061
+ (turn) => turn.kind !== "compaction" || turn.status !== "streaming" || (this.liveRevisions.get(turn.turn_id) ?? 0) > compactionRevision
6062
+ )));
6063
+ this.projectionBuilder._seedTurns(this.state.turns);
6064
+ }
6038
6065
  } catch {
6039
6066
  }
6040
6067
  }
@@ -6110,7 +6137,7 @@ var AgentSession = class _AgentSession {
6110
6137
  this.pendingTurnPatches.clear();
6111
6138
  this.epochReloadRequested += 1;
6112
6139
  }
6113
- this.runEpochReload();
6140
+ const activeCompactionIds = new Set(this.state.turns.filter((turn) => turn.kind === "compaction" && turn.status === "streaming").map((turn) => turn.compaction_id));
6114
6141
  for (const event of events) {
6115
6142
  if (event.type === "chat:end") this.recordRunResult(event.payload);
6116
6143
  const playbackId = typeof event.payload.playback_id === "string" ? event.payload.playback_id : null;
@@ -6167,7 +6194,28 @@ var AgentSession = class _AgentSession {
6167
6194
  );
6168
6195
  }
6169
6196
  }
6170
- events = events.filter((event) => event.type !== "turn:snapshot" && event.type !== "log:notification" && !event.type.startsWith("replay:"));
6197
+ events = events.flatMap((event) => {
6198
+ if (event.type === "turn:snapshot" || event.type.startsWith("replay:")) return [];
6199
+ if (event.type !== "log:notification") {
6200
+ if (event.type === "compaction:start") activeCompactionIds.add(String(event.payload.compaction_id));
6201
+ return [event];
6202
+ }
6203
+ const notification = event.payload.event;
6204
+ return isCompactionFinished(notification.type) && activeCompactionIds.has(String(notification.payload.compaction_id)) ? [notification] : [];
6205
+ });
6206
+ let start = 0;
6207
+ for (let index = 0; index < events.length; index += 1) {
6208
+ if (events[index]?.type !== "chat:end") continue;
6209
+ this.processRawEventSlice(events.slice(start, index + 1), replayed);
6210
+ start = index + 1;
6211
+ }
6212
+ this.processRawEventSlice(events.slice(start), replayed);
6213
+ this.runEpochReload();
6214
+ if (replayed && this.subscriptionReplaySource !== "bootstrap_replay" && !delivery.reload && this.replaceInFlight === 0 && this.state.activeCompaction !== null) {
6215
+ void this.refreshAuthoritativeHistory();
6216
+ }
6217
+ }
6218
+ processRawEventSlice(events, replayed) {
6171
6219
  if (!events.length) return;
6172
6220
  for (const event of events) this.handleManualCompactionFinished(event);
6173
6221
  this.dispatchRawCommands(events);
@@ -6175,9 +6223,7 @@ var AgentSession = class _AgentSession {
6175
6223
  if (!terminalChatEndStatus && hasChatRunEvent(events) && this.state.status !== "waiting_for_input") {
6176
6224
  this.markRunningIfIdle();
6177
6225
  }
6178
- if (this.state.turns.length) {
6179
- this.projectionBuilder._seedTurns(this.state.turns);
6180
- }
6226
+ this.projectionBuilder._seedTurns(this.state.turns);
6181
6227
  const updates = this.projectionBuilder.processBatch(events);
6182
6228
  for (const update of updates) {
6183
6229
  if (update.kind === "upsert") {
@@ -6341,7 +6387,9 @@ var AgentSession = class _AgentSession {
6341
6387
  this.update((s) => ({ ...s, status: "running", isStreaming: true }));
6342
6388
  } else if (this.state.isStreaming) {
6343
6389
  this.update((s) => ({ ...s, isStreaming: false }));
6344
- void this.refreshAuthoritativeHistory({ liveRevisionAtStart: liveRevisionAtJoin });
6390
+ if (this.subscriptionReplaySource !== "reconnect_replay") {
6391
+ void this.refreshAuthoritativeHistory({ liveRevisionAtStart: liveRevisionAtJoin });
6392
+ }
6345
6393
  }
6346
6394
  }
6347
6395
  markRunningIfIdle() {
@@ -6594,13 +6642,19 @@ var AgentSession = class _AgentSession {
6594
6642
  return pending;
6595
6643
  }
6596
6644
  readAuthoritativeHistory(options = {}) {
6645
+ const boundary = {
6646
+ ...options,
6647
+ liveRevisionAtStart: options.liveRevisionAtStart ?? this.liveRevision,
6648
+ modeRevisionAtStart: options.modeRevisionAtStart ?? this.modeRevision
6649
+ };
6597
6650
  let entryGone = false;
6598
6651
  const locateTokenAtStart = this.locateToken;
6599
6652
  return this.replaceFromHistory({
6600
- ...options,
6653
+ ...boundary,
6601
6654
  fromEntryId: () => {
6602
- const anchor = this.state.turns[0]?.turn_id;
6603
- return anchor?.startsWith("local-user-") ? void 0 : anchor;
6655
+ return this.state.turns.find(
6656
+ (turn) => !turn.turn_id.startsWith("local-user-") && !(turn.kind === "compaction" && turn.status === "streaming")
6657
+ )?.turn_id;
6604
6658
  },
6605
6659
  onError: (error) => {
6606
6660
  entryGone = isEntryGoneError(error);
@@ -6608,7 +6662,7 @@ var AgentSession = class _AgentSession {
6608
6662
  }).then((located) => {
6609
6663
  if (located || !entryGone) return located;
6610
6664
  if (this.locateToken !== locateTokenAtStart) return false;
6611
- return this.replaceFromHistory(options);
6665
+ return this.replaceFromHistory(boundary);
6612
6666
  });
6613
6667
  }
6614
6668
  replaceFromHistory(options = {}) {
@@ -6677,6 +6731,13 @@ var AgentSession = class _AgentSession {
6677
6731
  });
6678
6732
  this.pruneOptimisticLiveRevisions();
6679
6733
  this.projectionBuilder._seedTurns(this.state.turns);
6734
+ if (this.joined) await this.syncSessionFromDetail({ fresh: true });
6735
+ try {
6736
+ await this.reconcileManualCompaction();
6737
+ } catch (error) {
6738
+ options.onError?.(error);
6739
+ if (options.suppressErrors === false) throw error;
6740
+ }
6680
6741
  this.update((state) => ({ ...state, historyRevision: state.historyRevision + 1 }));
6681
6742
  return true;
6682
6743
  } catch (error) {
@@ -7836,6 +7897,7 @@ var SessionHub = class {
7836
7897
  fetchTurnsPage: (sessionId, query, init) => this.client.sessions.getSessionTurnsPage(sessionId, query, init),
7837
7898
  fetchTurnIndex: (sessionId) => this.client.sessions.getSessionTurnIndex(sessionId),
7838
7899
  fetchCommands: (sessionId) => this.options.restoreHistoricalCommands ? this.client.sessions.getSessionTurnCommands(sessionId) : Promise.resolve([]),
7900
+ fetchRunResult: (sessionId, runId) => this.client.sessions.getRunResult(sessionId, runId),
7839
7901
  fetchSessionInfo: (sessionId, options) => this.options.fetchSessionInfo ? this.options.fetchSessionInfo(sessionId, { fresh: options?.fresh ?? true }) : this.client.sessions.getSession(sessionId),
7840
7902
  updateReplay: (sessionId, payload) => this.client.sessions.updateReplaySession(sessionId, payload).then((result) => result.replay_state ?? null),
7841
7903
  listDir: (sessionId, dirPath) => this.client.sessions.listDir(sessionId, dirPath),
@@ -9481,4 +9543,4 @@ export {
9481
9543
  formatAskUserAnswer,
9482
9544
  attachPackPageBridge
9483
9545
  };
9484
- //# sourceMappingURL=chunk-WXZ5AWAT.js.map
9546
+ //# sourceMappingURL=chunk-6L5WKCWI.js.map