oira666_pi-limits-wait 0.2.4 → 0.3.0

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +54 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oira666_pi-limits-wait",
3
- "version": "0.2.4",
3
+ "version": "0.3.0",
4
4
  "description": "Extension for PI that allows to automatically wait for limits to reset and retry the request",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/index.ts CHANGED
@@ -13,6 +13,18 @@ import {
13
13
 
14
14
  // ─── Constants ────────────────────────────────────────────────────────────────
15
15
 
16
+ const CLAUDE_CODE_IDENTITY =
17
+ "You are Claude Code, Anthropic's official CLI for Claude.";
18
+
19
+ const PI_REMOVAL_ANCHORS = [
20
+ "pi-coding-agent",
21
+ "@mariozechner/pi-coding-agent",
22
+ "badlogic/pi-mono",
23
+ ] as const;
24
+
25
+ const PI_IDENTITY_SENTENCE_PATTERN =
26
+ /(?:^|\n)\s*You are pi\b[^.!?\n]*(?:[.!?](?=\s|$)|(?=\n|$))/gi;
27
+
16
28
  const DEFAULT_RATE_LIMIT_WAIT_MS = 30 * 60 * 1_000; // 30 minutes
17
29
  const DEFAULT_OVERLOADED_WAIT_MS = 5 * 60 * 1_000; // 5 minutes
18
30
 
@@ -34,6 +46,34 @@ let ambientStatusCleanup: (() => void) | undefined;
34
46
  let activeProviderRequests = 0;
35
47
  const wrappedApis = new Set<Api>();
36
48
 
49
+ // ─── Anthropic subscription prompt sanitisation ──────────────────────────────
50
+
51
+ export function sanitiseSystemPrompt(raw: string): string {
52
+ const paragraphs = raw.split(/\n\n+/);
53
+ const filtered = paragraphs.filter((p) =>
54
+ !PI_REMOVAL_ANCHORS.some((anchor) => p.includes(anchor)),
55
+ );
56
+
57
+ return filtered
58
+ .join("\n\n")
59
+ .replace(PI_IDENTITY_SENTENCE_PATTERN, "")
60
+ .replace(/\n{3,}/g, "\n\n")
61
+ .trim();
62
+ }
63
+
64
+ /**
65
+ * Returns true if Anthropic OAuth is configured for this session, regardless
66
+ * of which model is currently active. This handles cases where a synthetic or
67
+ * temporary model (e.g. a subagent/resume provider) is active at
68
+ * before_agent_start time, even though the session will ultimately use an
69
+ * Anthropic subscription model.
70
+ */
71
+ function isAnthropicOAuthSession(ctx: ExtensionContext): boolean {
72
+ return ctx.modelRegistry.isUsingOAuth(
73
+ { provider: "anthropic" } as Parameters<typeof ctx.modelRegistry.isUsingOAuth>[0],
74
+ );
75
+ }
76
+
37
77
  // ─── Retryable error detection ────────────────────────────────────────────────
38
78
 
39
79
  export function isRateLimitError(msg: string): boolean {
@@ -470,7 +510,7 @@ function registerWrappedApi(pi: ExtensionAPI, api: Api): void {
470
510
  // ─── Extension entry point ────────────────────────────────────────────────────
471
511
 
472
512
  export default function (pi: ExtensionAPI) {
473
- pi.on("before_agent_start", (_event, ctx) => {
513
+ pi.on("before_agent_start", (event, ctx) => {
474
514
  sharedCtx = ctx;
475
515
 
476
516
  // Some extensions may register providers after this extension loads. Wrap
@@ -479,5 +519,18 @@ export default function (pi: ExtensionAPI) {
479
519
  registerWrappedApi(pi, model.api);
480
520
  }
481
521
  if (ctx.model) registerWrappedApi(pi, ctx.model.api);
522
+
523
+ // Anthropic subscription/OAuth requests identify as Claude Code, not Pi.
524
+ // Check the session-wide Anthropic OAuth configuration rather than only the
525
+ // current model: resumed/subagent sessions can temporarily expose a
526
+ // synthetic active model during before_agent_start.
527
+ if (!isAnthropicOAuthSession(ctx)) return;
528
+
529
+ const sanitised = sanitiseSystemPrompt(event.systemPrompt);
530
+ return {
531
+ systemPrompt: sanitised
532
+ ? `${CLAUDE_CODE_IDENTITY}\n\n${sanitised}`
533
+ : CLAUDE_CODE_IDENTITY,
534
+ };
482
535
  });
483
536
  }