@ryan_nookpi/pi-extension-codex-large-context 0.1.0 → 0.2.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 (3) hide show
  1. package/README.md +15 -1
  2. package/index.ts +86 -1
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -10,10 +10,23 @@ It is intended for `gpt-5.4` and `gpt-5.5` model IDs when pi reports a smaller c
10
10
  pi install npm:@ryan_nookpi/pi-extension-codex-large-context
11
11
  ```
12
12
 
13
+ ## Usage
14
+
15
+ Large context mode is ON by default.
16
+
17
+ ```text
18
+ /codex-large-context on
19
+ /codex-large-context off
20
+ /codex-large-context status
21
+ ```
22
+
23
+ Running `/codex-large-context` with no argument shows the current status.
24
+
13
25
  ## What it does
14
26
 
15
- - Watches `session_start` and `model_select` events.
27
+ - Watches `session_start` and `model_select` events while enabled.
16
28
  - If the active model ID starts with `gpt-5.4` or `gpt-5.5`, sets its context window to `922000` tokens.
29
+ - Stores the on/off setting locally so it persists across sessions.
17
30
  - Shows a small notification when the context window is updated in the interactive UI.
18
31
 
19
32
  ## Notes
@@ -21,3 +34,4 @@ pi install npm:@ryan_nookpi/pi-extension-codex-large-context
21
34
  - This only changes pi's local model metadata for the active session/model selection.
22
35
  - It does not change API limits on the provider side.
23
36
  - If pi already reports a context window of `922000` tokens or more, it leaves the model unchanged.
37
+ - Turning the feature off prevents future context-window adjustments; it does not restore a model that was already adjusted earlier in the session.
package/index.ts CHANGED
@@ -1,7 +1,45 @@
1
- import type { ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent";
1
+ import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import { dirname, join } from "node:path";
4
+ import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
2
5
 
6
+ const STATE_FILE = join(homedir(), ".pi", "agent", "state", "codex-large-context.json");
3
7
  export const TARGET_CONTEXT_WINDOW = 922_000;
4
8
  export const TARGET_MODEL_PREFIXES = ["gpt-5.4", "gpt-5.5"] as const;
9
+ const TARGET_MODEL_LABEL = TARGET_MODEL_PREFIXES.join(" or ");
10
+
11
+ type LargeContextState = {
12
+ enabled: boolean;
13
+ };
14
+
15
+ export function loadCodexLargeContextState(): LargeContextState {
16
+ try {
17
+ const raw = readFileSync(STATE_FILE, "utf8");
18
+ const parsed = JSON.parse(raw);
19
+ if (typeof parsed?.enabled === "boolean") {
20
+ return { enabled: parsed.enabled };
21
+ }
22
+ } catch {
23
+ // Ignore missing/corrupt state and fall back to the default-enabled behavior.
24
+ }
25
+
26
+ return { enabled: true };
27
+ }
28
+
29
+ export function isCodexLargeContextEnabled(): boolean {
30
+ return loadCodexLargeContextState().enabled;
31
+ }
32
+
33
+ function saveState(state: LargeContextState): void {
34
+ mkdirSync(dirname(STATE_FILE), { recursive: true });
35
+ writeFileSync(STATE_FILE, `${JSON.stringify(state, null, 2)}\n`, "utf8");
36
+ }
37
+
38
+ function parseCommandArg(args: string): "on" | "off" | "status" | "help" {
39
+ const arg = args.trim().toLowerCase();
40
+ if (arg === "on" || arg === "off" || arg === "status") return arg;
41
+ return arg ? "help" : "status";
42
+ }
5
43
 
6
44
  export function isTargetModelId(modelId: string | undefined): boolean {
7
45
  if (!modelId) return false;
@@ -9,6 +47,8 @@ export function isTargetModelId(modelId: string | undefined): boolean {
9
47
  }
10
48
 
11
49
  export async function applyLargeContext(pi: ExtensionAPI, ctx: ExtensionContext): Promise<boolean> {
50
+ if (!isCodexLargeContextEnabled()) return false;
51
+
12
52
  const model = ctx.model;
13
53
  if (!model) return false;
14
54
  if (!isTargetModelId(model.id)) return false;
@@ -36,4 +76,49 @@ export default function codexLargeContext(pi: ExtensionAPI) {
36
76
  pi.on("session_start", async (_event, ctx) => {
37
77
  await applyLargeContext(pi, ctx);
38
78
  });
79
+
80
+ pi.registerCommand("codex-large-context", {
81
+ description: "Toggle Codex Large Context for openai-codex/gpt-5.4 or gpt-5.5",
82
+ getArgumentCompletions: (prefix) => {
83
+ const options = ["on", "off", "status"];
84
+ const filtered = options.filter((option) => option.startsWith(prefix.trim().toLowerCase()));
85
+ return filtered.length > 0 ? filtered.map((option) => ({ value: option, label: option })) : null;
86
+ },
87
+ handler: async (args, ctx) => {
88
+ const action = parseCommandArg(args);
89
+
90
+ if (action === "help") {
91
+ if (ctx.hasUI) {
92
+ ctx.ui.notify("Usage: /codex-large-context [on|off|status]", "info");
93
+ }
94
+ return;
95
+ }
96
+
97
+ if (action === "status") {
98
+ if (ctx.hasUI) {
99
+ ctx.ui.notify(
100
+ `Codex Large Context: ${isCodexLargeContextEnabled() ? "ON" : "OFF"} (${TARGET_MODEL_LABEL} → ${(TARGET_CONTEXT_WINDOW / 1000).toFixed(0)}K)`,
101
+ "info",
102
+ );
103
+ }
104
+ return;
105
+ }
106
+
107
+ const nextState = { enabled: action === "on" };
108
+ saveState(nextState);
109
+
110
+ if (ctx.hasUI) {
111
+ ctx.ui.notify(
112
+ nextState.enabled
113
+ ? `Codex Large Context enabled (${TARGET_MODEL_LABEL} → ${(TARGET_CONTEXT_WINDOW / 1000).toFixed(0)}K)`
114
+ : "Codex Large Context disabled",
115
+ "info",
116
+ );
117
+ }
118
+
119
+ if (nextState.enabled) {
120
+ await applyLargeContext(pi, ctx);
121
+ }
122
+ },
123
+ });
39
124
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ryan_nookpi/pi-extension-codex-large-context",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Codex large context window extension for pi.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -26,7 +26,7 @@
26
26
  ]
27
27
  },
28
28
  "peerDependencies": {
29
- "@mariozechner/pi-coding-agent": "*"
29
+ "@earendil-works/pi-coding-agent": "*"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public"