@caupulican/pi-adaptative 0.75.5 → 0.75.6

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.
@@ -131,6 +131,7 @@ export class AgentSession {
131
131
  // (session persistence, extensions, auto-compaction, retry logic)
132
132
  this._unsubscribeAgent = this.agent.subscribe(this._handleAgentEvent);
133
133
  this._installAgentToolHooks();
134
+ this._installAgentContextTransform();
134
135
  this._buildRuntime({
135
136
  activeToolNames: this._initialActiveToolNames,
136
137
  includeAllExtensionTools: true,
@@ -174,6 +175,50 @@ export class AgentSession {
174
175
  * registered tool execution to the extension context. Tool call and tool result interception now
175
176
  * happens here instead of in wrappers.
176
177
  */
178
+ _installAgentContextTransform() {
179
+ const previousTransformContext = this.agent.transformContext?.bind(this.agent);
180
+ this.agent.transformContext = async (messages, signal) => {
181
+ const transformed = previousTransformContext ? await previousTransformContext(messages, signal) : messages;
182
+ const authoritativeMessages = this.agent.state.messages.length > 0 ? this.agent.state.messages : transformed;
183
+ try {
184
+ const settings = this.settingsManager.getCompactionSettings();
185
+ const contextWindow = this.model?.contextWindow ?? 0;
186
+ if (!settings.enabled || contextWindow <= 0 || this.isCompacting) {
187
+ return authoritativeMessages;
188
+ }
189
+ const contextTokens = this._estimateCurrentContextTokens(authoritativeMessages);
190
+ if (!shouldCompact(contextTokens, contextWindow, settings)) {
191
+ return authoritativeMessages;
192
+ }
193
+ const latestBefore = getLatestCompactionEntry(this.sessionManager.getBranch())?.id;
194
+ await this._runAutoCompaction("threshold", false);
195
+ const latestAfter = getLatestCompactionEntry(this.sessionManager.getBranch())?.id;
196
+ return latestAfter && latestAfter !== latestBefore
197
+ ? this.agent.state.messages.slice()
198
+ : authoritativeMessages;
199
+ }
200
+ catch {
201
+ return authoritativeMessages;
202
+ }
203
+ };
204
+ }
205
+ _estimateCurrentContextTokens(messages) {
206
+ const estimate = estimateContextTokens(messages);
207
+ const compactionEntry = getLatestCompactionEntry(this.sessionManager.getBranch());
208
+ if (estimate.lastUsageIndex === null || !compactionEntry) {
209
+ return estimate.tokens;
210
+ }
211
+ const usageMessage = messages[estimate.lastUsageIndex];
212
+ if (usageMessage?.role !== "assistant") {
213
+ return estimate.tokens;
214
+ }
215
+ const usageTimestamp = usageMessage.timestamp;
216
+ const compactionTimestamp = new Date(compactionEntry.timestamp).getTime();
217
+ if (usageTimestamp <= compactionTimestamp) {
218
+ return estimate.trailingTokens;
219
+ }
220
+ return estimate.tokens;
221
+ }
177
222
  _installAgentToolHooks() {
178
223
  this.agent.beforeToolCall = async ({ toolCall, args }) => {
179
224
  const runner = this._extensionRunner;
@@ -1478,6 +1523,16 @@ export class AgentSession {
1478
1523
  }
1479
1524
  else {
1480
1525
  contextTokens = calculateContextTokens(assistantMessage.usage);
1526
+ const estimate = estimateContextTokens(this.agent.state.messages);
1527
+ if (estimate.lastUsageIndex !== null) {
1528
+ const usageMsg = this.agent.state.messages[estimate.lastUsageIndex];
1529
+ const usageIsPostCompaction = !(compactionEntry &&
1530
+ usageMsg.role === "assistant" &&
1531
+ usageMsg.timestamp <= new Date(compactionEntry.timestamp).getTime());
1532
+ if (usageIsPostCompaction) {
1533
+ contextTokens = Math.max(contextTokens, estimate.tokens);
1534
+ }
1535
+ }
1481
1536
  }
1482
1537
  if (shouldCompact(contextTokens, contextWindow, settings)) {
1483
1538
  return await this._runAutoCompaction("threshold", false);