pi-openai-toolkit 0.6.0 → 0.7.1

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.
@@ -6,7 +6,7 @@ import type {
6
6
  SessionBeforeCompactEvent,
7
7
  } from "@earendil-works/pi-coding-agent";
8
8
  import { loadToolkitConfig } from "./config";
9
- import { writeDebugArtifact } from "./debug";
9
+ import { writeDebugArtifact, writeReplayFailureArtifact } from "./debug";
10
10
  import { resolveLatestNativeCompactionEntry } from "./details-store";
11
11
  import { runNativeFallbackCompaction } from "./native-fallback";
12
12
  import {
@@ -15,6 +15,7 @@ import {
15
15
  } from "./payload-rewrite";
16
16
  import { getCompactionRequestExtras, rememberRequestContext } from "./request-context-cache";
17
17
  import { executeRemoteV2Compaction } from "./remote-v2-client";
18
+ import { registerInlineCompactionRuntime } from "./inline-compaction";
18
19
  import {
19
20
  resolveNativeCompactionEnvironment,
20
21
  resolveRemoteCompactionExecution,
@@ -25,6 +26,7 @@ import {
25
26
  createNativeCompactionDetails,
26
27
  createNativeCompactionResult,
27
28
  COMPACTION_EXTENSION_ID,
29
+ getLatestDeferredToolCarryover,
28
30
  isNativeCompactionDetails,
29
31
  type CompactionConfig,
30
32
  type NativeCompactionDetails,
@@ -99,7 +101,8 @@ async function runResponsesNativeCompact(
99
101
  ): Promise<ResponsesCompactOutcome> {
100
102
  const { consumer, compactor } = execution;
101
103
  const instructions = buildCompactionInstructions(ctx.getSystemPrompt(), event.customInstructions);
102
- const branchEntries = ctx.sessionManager.getBranch();
104
+ const branchEntries = event.branchEntries ?? ctx.sessionManager.getBranch();
105
+ const deferredToolCarryover = getLatestDeferredToolCarryover(branchEntries);
103
106
  const latestNativeCompaction = resolveLatestNativeCompactionEntry(branchEntries, {
104
107
  baseUrl: consumer.baseUrl,
105
108
  });
@@ -217,6 +220,7 @@ async function runResponsesNativeCompact(
217
220
  model: compactor.model,
218
221
  baseUrl: compactor.baseUrl,
219
222
  },
223
+ deferredToolCarryover,
220
224
  compactedWindow: compactResult.compactedWindow,
221
225
  compactResponseId: compactResult.compactResponseId,
222
226
  createdAt: compactResult.createdAt,
@@ -504,6 +508,30 @@ async function handleBeforeProviderRequest(event: BeforeProviderRequestEvent, ct
504
508
  config,
505
509
  ctx,
506
510
  );
511
+
512
+ // Fail loud instead of letting Pi send the sentinel-only payload: the
513
+ // compacted history would be silently lost while the request still succeeds.
514
+ // A forced redacted failure record is written even when logProviderPayloads
515
+ // is disabled so the incident is diagnosable without leaking content.
516
+ writeReplayFailureArtifact(
517
+ {
518
+ reason: rewrite.reason,
519
+ parity: rewrite.parity,
520
+ compactionEntryId: latestNativeCompactionEntry.id,
521
+ provider: runtime.provider,
522
+ api: runtime.api,
523
+ model: runtime.model,
524
+ },
525
+ config,
526
+ ctx,
527
+ );
528
+ if (ctx.hasUI) {
529
+ ctx.ui.notify(
530
+ `${COMPACTION_EXTENSION_ID}: native compaction replay failed (${rewrite.reason}); provider request aborted`,
531
+ "error",
532
+ );
533
+ }
534
+ ctx.abort();
507
535
  return undefined;
508
536
  }
509
537
 
@@ -537,6 +565,8 @@ async function handleBeforeProviderRequest(event: BeforeProviderRequestEvent, ct
537
565
  }
538
566
 
539
567
  export default function (pi: ExtensionAPI) {
568
+ const inlineCompaction = registerInlineCompactionRuntime(pi, loadToolkitConfig);
569
+
540
570
  pi.on("session_start", (_event, ctx) => {
541
571
  const { config: toolkitConfig, source, warnings } = loadToolkitConfig();
542
572
  const config = toolkitConfig.compaction;
@@ -570,4 +600,6 @@ export default function (pi: ExtensionAPI) {
570
600
 
571
601
  pi.on("session_before_compact", handleSessionBeforeCompact);
572
602
  pi.on("before_provider_request", handleBeforeProviderRequest);
603
+ pi.on("turn_end", (event, ctx) => inlineCompaction.handlePublicTurnEnd(event, ctx));
604
+ pi.on("session_shutdown", (_event, ctx) => inlineCompaction.dispose(ctx));
573
605
  }