@caupulican/pi-adaptative 0.75.5 → 0.75.7

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,8 @@ 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();
135
+ this._installAgentTurnRefresh();
134
136
  this._buildRuntime({
135
137
  activeToolNames: this._initialActiveToolNames,
136
138
  includeAllExtensionTools: true,
@@ -174,6 +176,74 @@ export class AgentSession {
174
176
  * registered tool execution to the extension context. Tool call and tool result interception now
175
177
  * happens here instead of in wrappers.
176
178
  */
179
+ _installAgentContextTransform() {
180
+ const previousTransformContext = this.agent.transformContext?.bind(this.agent);
181
+ this.agent.transformContext = async (messages, signal) => {
182
+ const transformed = previousTransformContext ? await previousTransformContext(messages, signal) : messages;
183
+ const authoritativeMessages = this.agent.state.messages.length > 0 ? this.agent.state.messages : transformed;
184
+ try {
185
+ const settings = this.settingsManager.getCompactionSettings();
186
+ const contextWindow = this.model?.contextWindow ?? 0;
187
+ if (!settings.enabled || contextWindow <= 0 || this.isCompacting) {
188
+ return authoritativeMessages;
189
+ }
190
+ const contextTokens = this._estimateCurrentContextTokens(authoritativeMessages);
191
+ if (!shouldCompact(contextTokens, contextWindow, settings)) {
192
+ return authoritativeMessages;
193
+ }
194
+ const latestBefore = getLatestCompactionEntry(this.sessionManager.getBranch())?.id;
195
+ await this._runAutoCompaction("threshold", false);
196
+ const latestAfter = getLatestCompactionEntry(this.sessionManager.getBranch())?.id;
197
+ return latestAfter && latestAfter !== latestBefore
198
+ ? this.agent.state.messages.slice()
199
+ : authoritativeMessages;
200
+ }
201
+ catch {
202
+ return authoritativeMessages;
203
+ }
204
+ };
205
+ }
206
+ _installAgentTurnRefresh() {
207
+ const previousPrepareNextTurn = this.agent.prepareNextTurn?.bind(this.agent);
208
+ this.agent.prepareNextTurn = async (signal) => {
209
+ const previous = previousPrepareNextTurn ? await previousPrepareNextTurn(signal) : undefined;
210
+ const snapshot = this._createAgentContextSnapshot();
211
+ return {
212
+ ...previous,
213
+ context: {
214
+ ...(previous?.context ?? snapshot),
215
+ systemPrompt: snapshot.systemPrompt,
216
+ tools: snapshot.tools,
217
+ },
218
+ model: previous?.model ?? this.agent.state.model,
219
+ thinkingLevel: previous?.thinkingLevel ?? this.agent.state.thinkingLevel,
220
+ };
221
+ };
222
+ }
223
+ _createAgentContextSnapshot() {
224
+ return {
225
+ systemPrompt: this.agent.state.systemPrompt,
226
+ messages: this.agent.state.messages.slice(),
227
+ tools: this.agent.state.tools.slice(),
228
+ };
229
+ }
230
+ _estimateCurrentContextTokens(messages) {
231
+ const estimate = estimateContextTokens(messages);
232
+ const compactionEntry = getLatestCompactionEntry(this.sessionManager.getBranch());
233
+ if (estimate.lastUsageIndex === null || !compactionEntry) {
234
+ return estimate.tokens;
235
+ }
236
+ const usageMessage = messages[estimate.lastUsageIndex];
237
+ if (usageMessage?.role !== "assistant") {
238
+ return estimate.tokens;
239
+ }
240
+ const usageTimestamp = usageMessage.timestamp;
241
+ const compactionTimestamp = new Date(compactionEntry.timestamp).getTime();
242
+ if (usageTimestamp <= compactionTimestamp) {
243
+ return estimate.trailingTokens;
244
+ }
245
+ return estimate.tokens;
246
+ }
177
247
  _installAgentToolHooks() {
178
248
  this.agent.beforeToolCall = async ({ toolCall, args }) => {
179
249
  const runner = this._extensionRunner;
@@ -1478,6 +1548,16 @@ export class AgentSession {
1478
1548
  }
1479
1549
  else {
1480
1550
  contextTokens = calculateContextTokens(assistantMessage.usage);
1551
+ const estimate = estimateContextTokens(this.agent.state.messages);
1552
+ if (estimate.lastUsageIndex !== null) {
1553
+ const usageMsg = this.agent.state.messages[estimate.lastUsageIndex];
1554
+ const usageIsPostCompaction = !(compactionEntry &&
1555
+ usageMsg.role === "assistant" &&
1556
+ usageMsg.timestamp <= new Date(compactionEntry.timestamp).getTime());
1557
+ if (usageIsPostCompaction) {
1558
+ contextTokens = Math.max(contextTokens, estimate.tokens);
1559
+ }
1560
+ }
1481
1561
  }
1482
1562
  if (shouldCompact(contextTokens, contextWindow, settings)) {
1483
1563
  return await this._runAutoCompaction("threshold", false);
@@ -1822,12 +1902,9 @@ export class AgentSession {
1822
1902
  })();
1823
1903
  },
1824
1904
  reload: () => {
1825
- if (this.isStreaming) {
1826
- throw new Error("Cannot reload while the agent is streaming.");
1827
- }
1828
1905
  const actions = this._extensionCommandContextActions;
1829
- if (!actions) {
1830
- throw new Error("Reload is unavailable before extension command context is bound.");
1906
+ if (this.isStreaming || !actions) {
1907
+ return this.reload();
1831
1908
  }
1832
1909
  return actions.reload();
1833
1910
  },