@howaboua/pi-gpt-switcher 0.1.0 → 0.1.2
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.
- package/CHANGELOG.md +18 -0
- package/README.md +27 -4
- package/biome.json +1 -1
- package/changelog.ts +337 -0
- package/codex-model-shortcuts.ts +34 -28
- package/config.ts +159 -0
- package/index.ts +11 -1
- package/package.json +11 -7
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# @howaboua/pi-gpt-switcher
|
|
2
|
+
|
|
3
|
+
## 0.1.2
|
|
4
|
+
|
|
5
|
+
- Add /astra for GPT-6 Astra with low reasoning by default and an optional reasoning override.
|
|
6
|
+
|
|
7
|
+
## 0.1.1
|
|
8
|
+
|
|
9
|
+
### Changes
|
|
10
|
+
|
|
11
|
+
- [#333](https://github.com/IgorWarzocha/howaboua-pi-stuff/pull/333) [`a9143cf`](https://github.com/IgorWarzocha/howaboua-pi-stuff/commit/a9143cfeb802406c2e62b4424e27b69da141b7ae) Thanks [@IgorWarzocha](https://github.com/IgorWarzocha)! - Add configurable session context windows and reasoning defaults for GPT shortcuts.
|
|
12
|
+
|
|
13
|
+
## 0.1.0
|
|
14
|
+
|
|
15
|
+
### Changes
|
|
16
|
+
|
|
17
|
+
- [#164](https://github.com/IgorWarzocha/howaboua-pi-stuff/pull/164) [`b8731f6`](https://github.com/IgorWarzocha/howaboua-pi-stuff/commit/b8731f68c8b6cbfb167e29728aad07fb59e560bb) Thanks [@IgorWarzocha](https://github.com/IgorWarzocha)!:
|
|
18
|
+
- Add `/sol`, `/terra`, and `/luna` commands for switching GPT-5.6 Codex models and reasoning levels.
|
package/README.md
CHANGED
|
@@ -7,9 +7,32 @@ Pi extension adding quick model commands:
|
|
|
7
7
|
| `/sol [reasoning]` | `openai-codex/gpt-5.6-sol` |
|
|
8
8
|
| `/terra [reasoning]` | `openai-codex/gpt-5.6-terra` |
|
|
9
9
|
| `/luna [reasoning]` | `openai-codex/gpt-5.6-luna` |
|
|
10
|
+
| `/astra [reasoning]` | `openai-codex/gpt-6-astra` |
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
`medium`, `high`, `xhigh`, and `max`; for example,
|
|
12
|
+
An explicit reasoning argument overrides the configured default. Valid arguments
|
|
13
|
+
are `off`, `minimal`, `low`, `medium`, `high`, `xhigh`, and `max`; for example,
|
|
14
|
+
`/luna low`.
|
|
15
|
+
|
|
16
|
+
## Defaults
|
|
17
|
+
|
|
18
|
+
On first load, the extension creates `pi-gpt-switcher.json` in Pi's global
|
|
19
|
+
agent directory:
|
|
20
|
+
|
|
21
|
+
```json
|
|
22
|
+
{
|
|
23
|
+
"sol": { "contextWindow": 272000, "reasoning": "high" },
|
|
24
|
+
"terra": { "contextWindow": 872000, "reasoning": "high" },
|
|
25
|
+
"luna": { "contextWindow": 472000, "reasoning": "xhigh" },
|
|
26
|
+
"astra": { "contextWindow": 272000, "reasoning": "low" }
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Edit the file to change a shortcut's session context window or default
|
|
31
|
+
reasoning. Changes apply on the next shortcut invocation and do not alter the
|
|
32
|
+
provider catalogue. Context windows may be lowered to 128k but not raised
|
|
33
|
+
above the shortcut's shipped limit.
|
|
34
|
+
|
|
35
|
+
Existing config files need no migration: omitted shortcuts use their shipped defaults.
|
|
13
36
|
|
|
14
37
|
## Install
|
|
15
38
|
|
|
@@ -29,8 +52,8 @@ The commands use Pi's model registry and normal provider authentication. If a
|
|
|
29
52
|
model is unavailable or the OpenAI Codex credentials are missing, the command
|
|
30
53
|
reports that instead of changing the current model.
|
|
31
54
|
|
|
32
|
-
|
|
33
|
-
|
|
55
|
+
Pi clamps the requested reasoning level automatically if the selected model
|
|
56
|
+
supports fewer levels.
|
|
34
57
|
|
|
35
58
|
## Local development
|
|
36
59
|
|
package/biome.json
CHANGED
package/changelog.ts
ADDED
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
// Generated extension packages copy this file during changelog:extensions.
|
|
2
|
+
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { readFileSync } from "node:fs";
|
|
5
|
+
import { mkdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
6
|
+
import { dirname, join } from "node:path";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import {
|
|
9
|
+
DynamicBorder,
|
|
10
|
+
type ExtensionAPI,
|
|
11
|
+
getAgentDir,
|
|
12
|
+
getMarkdownTheme,
|
|
13
|
+
} from "@earendil-works/pi-coding-agent";
|
|
14
|
+
import { Container, Markdown, Spacer, Text } from "@earendil-works/pi-tui";
|
|
15
|
+
|
|
16
|
+
const ENTRY_TYPE = "@howaboua/pi-stuff/changelog";
|
|
17
|
+
const REGISTRATION_CHANNEL = "@howaboua/pi-stuff/changelog/register/v1";
|
|
18
|
+
const STATE_FILENAME = "howaboua-pi-stuff-changelog.json";
|
|
19
|
+
const SUPPRESS_KEY = "suppress";
|
|
20
|
+
const LOCK_STALE_MS = 30_000;
|
|
21
|
+
const LOCK_TIMEOUT_MS = 2_000;
|
|
22
|
+
|
|
23
|
+
interface PackageMetadata {
|
|
24
|
+
name: string;
|
|
25
|
+
version: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface PackageRegistration extends PackageMetadata {
|
|
29
|
+
changelogPath: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface RegistrationEvent {
|
|
33
|
+
accept: () => void;
|
|
34
|
+
registration: PackageRegistration;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface ChangelogEntry {
|
|
38
|
+
content: string;
|
|
39
|
+
version: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface ChangelogEntryData {
|
|
43
|
+
markdown: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
type ChangelogState = Record<string, boolean | string>;
|
|
47
|
+
|
|
48
|
+
function packageRegistration(): PackageRegistration {
|
|
49
|
+
const packageDirectory = fileURLToPath(new URL(".", import.meta.url));
|
|
50
|
+
const metadata = JSON.parse(
|
|
51
|
+
readFileSync(join(packageDirectory, "package.json"), "utf8"),
|
|
52
|
+
) as Partial<PackageMetadata>;
|
|
53
|
+
if (typeof metadata.name !== "string" || typeof metadata.version !== "string")
|
|
54
|
+
throw new Error(`Invalid package metadata in ${packageDirectory}`);
|
|
55
|
+
return {
|
|
56
|
+
name: metadata.name,
|
|
57
|
+
version: metadata.version,
|
|
58
|
+
changelogPath: join(packageDirectory, "CHANGELOG.md"),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function isRegistrationEvent(value: unknown): value is RegistrationEvent {
|
|
63
|
+
if (!value || typeof value !== "object") return false;
|
|
64
|
+
const event = value as Partial<RegistrationEvent>;
|
|
65
|
+
return (
|
|
66
|
+
typeof event.accept === "function" &&
|
|
67
|
+
typeof event.registration?.name === "string" &&
|
|
68
|
+
typeof event.registration.version === "string" &&
|
|
69
|
+
typeof event.registration.changelogPath === "string"
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function parseChangelog(markdown: string): ChangelogEntry[] {
|
|
74
|
+
const entries: ChangelogEntry[] = [];
|
|
75
|
+
let currentLines: string[] = [];
|
|
76
|
+
let currentVersion: string | undefined;
|
|
77
|
+
|
|
78
|
+
for (const line of markdown.split("\n")) {
|
|
79
|
+
const version = line.match(/^##\s+\[?(\d+\.\d+\.\d+)\]?(?:\s.*)?$/)?.[1];
|
|
80
|
+
if (version) {
|
|
81
|
+
if (currentVersion)
|
|
82
|
+
entries.push({
|
|
83
|
+
content: currentLines.join("\n").trim(),
|
|
84
|
+
version: currentVersion,
|
|
85
|
+
});
|
|
86
|
+
currentVersion = version;
|
|
87
|
+
currentLines = [line];
|
|
88
|
+
} else if (currentVersion) {
|
|
89
|
+
currentLines.push(line);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (currentVersion)
|
|
94
|
+
entries.push({
|
|
95
|
+
content: currentLines.join("\n").trim(),
|
|
96
|
+
version: currentVersion,
|
|
97
|
+
});
|
|
98
|
+
return entries;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function compareVersions(left: string, right: string): number {
|
|
102
|
+
const leftParts = left.split(".").map(Number);
|
|
103
|
+
const rightParts = right.split(".").map(Number);
|
|
104
|
+
for (let index = 0; index < 3; index++) {
|
|
105
|
+
const difference = (leftParts[index] ?? 0) - (rightParts[index] ?? 0);
|
|
106
|
+
if (difference !== 0) return difference;
|
|
107
|
+
}
|
|
108
|
+
return 0;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function isStableVersion(version: string): boolean {
|
|
112
|
+
return /^\d+\.\d+\.\d+$/.test(version);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function statePath(): string {
|
|
116
|
+
return join(getAgentDir(), STATE_FILENAME);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function readState(path: string): Promise<ChangelogState> {
|
|
120
|
+
let text: string;
|
|
121
|
+
try {
|
|
122
|
+
text = await readFile(path, "utf8");
|
|
123
|
+
} catch (error) {
|
|
124
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return {};
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
const parsed = JSON.parse(text) as unknown;
|
|
128
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
129
|
+
throw new Error(`${path} must contain a JSON object`);
|
|
130
|
+
for (const [name, value] of Object.entries(parsed)) {
|
|
131
|
+
if (name === SUPPRESS_KEY) {
|
|
132
|
+
if (typeof value !== "boolean")
|
|
133
|
+
throw new Error(`${path} ${SUPPRESS_KEY} must be a boolean`);
|
|
134
|
+
} else if (typeof value !== "string") {
|
|
135
|
+
throw new Error(`${path} version for ${name} must be a string`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return parsed as ChangelogState;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function acquireLock(path: string): Promise<() => Promise<void>> {
|
|
142
|
+
const deadline = Date.now() + LOCK_TIMEOUT_MS;
|
|
143
|
+
while (true) {
|
|
144
|
+
try {
|
|
145
|
+
await mkdir(path);
|
|
146
|
+
return () => rm(path, { force: true, recursive: true });
|
|
147
|
+
} catch (error) {
|
|
148
|
+
if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error;
|
|
149
|
+
const info = await stat(path).catch(() => undefined);
|
|
150
|
+
if (info && Date.now() - info.mtimeMs > LOCK_STALE_MS) {
|
|
151
|
+
await rm(path, { force: true, recursive: true });
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (Date.now() >= deadline)
|
|
155
|
+
throw new Error(`Timed out waiting for ${path}`);
|
|
156
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async function writeState(path: string, state: ChangelogState): Promise<void> {
|
|
162
|
+
await mkdir(dirname(path), { mode: 0o700, recursive: true });
|
|
163
|
+
const temporaryPath = `${path}.${process.pid}.${randomUUID()}.tmp`;
|
|
164
|
+
try {
|
|
165
|
+
const ordered = Object.fromEntries(
|
|
166
|
+
Object.entries(state).sort(([left], [right]) =>
|
|
167
|
+
left.localeCompare(right),
|
|
168
|
+
),
|
|
169
|
+
);
|
|
170
|
+
await writeFile(temporaryPath, `${JSON.stringify(ordered, null, 2)}\n`, {
|
|
171
|
+
encoding: "utf8",
|
|
172
|
+
mode: 0o600,
|
|
173
|
+
});
|
|
174
|
+
await rename(temporaryPath, path);
|
|
175
|
+
} catch (error) {
|
|
176
|
+
await rm(temporaryPath, { force: true });
|
|
177
|
+
throw error;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function packageMarkdown(
|
|
182
|
+
registration: PackageRegistration,
|
|
183
|
+
entries: ChangelogEntry[],
|
|
184
|
+
): string {
|
|
185
|
+
const body = (entry: ChangelogEntry) =>
|
|
186
|
+
entry.content
|
|
187
|
+
.replace(/^##[^\n]*\n*/, "")
|
|
188
|
+
.replace(/^###\s+(?:Changes|Minor Changes|Patch Changes)\s*\n*/gim, "")
|
|
189
|
+
.replace(/^###\s+(.+)$/gm, "**$1**");
|
|
190
|
+
if (entries.length === 1) {
|
|
191
|
+
const [entry] = entries;
|
|
192
|
+
if (!entry) return "";
|
|
193
|
+
return `## ${registration.name} ${entry.version}\n\n${body(entry)}`;
|
|
194
|
+
}
|
|
195
|
+
return [
|
|
196
|
+
`## ${registration.name}`,
|
|
197
|
+
...entries.map((entry) => `**${entry.version}**\n\n${body(entry)}`),
|
|
198
|
+
].join("\n\n");
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async function claimUpdates(
|
|
202
|
+
registrations: Iterable<PackageRegistration>,
|
|
203
|
+
): Promise<{ errors: string[]; markdown?: string }> {
|
|
204
|
+
const stableRegistrations = [...registrations]
|
|
205
|
+
.filter((registration) => isStableVersion(registration.version))
|
|
206
|
+
.sort((left, right) => left.name.localeCompare(right.name));
|
|
207
|
+
if (stableRegistrations.length === 0) return { errors: [] };
|
|
208
|
+
const path = statePath();
|
|
209
|
+
await mkdir(dirname(path), { mode: 0o700, recursive: true });
|
|
210
|
+
const release = await acquireLock(`${path}.lock`);
|
|
211
|
+
try {
|
|
212
|
+
const state = await readState(path);
|
|
213
|
+
const errors: string[] = [];
|
|
214
|
+
const sections: string[] = [];
|
|
215
|
+
let changed = false;
|
|
216
|
+
if (state[SUPPRESS_KEY] === true) {
|
|
217
|
+
for (const registration of stableRegistrations) {
|
|
218
|
+
const seenVersion = state[registration.name];
|
|
219
|
+
if (
|
|
220
|
+
typeof seenVersion !== "string" ||
|
|
221
|
+
compareVersions(registration.version, seenVersion) > 0
|
|
222
|
+
) {
|
|
223
|
+
state[registration.name] = registration.version;
|
|
224
|
+
changed = true;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (changed) await writeState(path, state);
|
|
228
|
+
return { errors };
|
|
229
|
+
}
|
|
230
|
+
for (const registration of stableRegistrations) {
|
|
231
|
+
const seenVersion = state[registration.name];
|
|
232
|
+
if (
|
|
233
|
+
typeof seenVersion === "string" &&
|
|
234
|
+
compareVersions(registration.version, seenVersion) <= 0
|
|
235
|
+
)
|
|
236
|
+
continue;
|
|
237
|
+
try {
|
|
238
|
+
const entries = parseChangelog(
|
|
239
|
+
await readFile(registration.changelogPath, "utf8"),
|
|
240
|
+
).filter(
|
|
241
|
+
(entry) =>
|
|
242
|
+
compareVersions(entry.version, registration.version) <= 0 &&
|
|
243
|
+
(typeof seenVersion === "string"
|
|
244
|
+
? compareVersions(entry.version, seenVersion) > 0
|
|
245
|
+
: entry.version === registration.version),
|
|
246
|
+
);
|
|
247
|
+
if (entries.length === 0) {
|
|
248
|
+
errors.push(
|
|
249
|
+
`No ${registration.version} changelog entry found for ${registration.name}`,
|
|
250
|
+
);
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
sections.push(packageMarkdown(registration, entries));
|
|
254
|
+
state[registration.name] = registration.version;
|
|
255
|
+
changed = true;
|
|
256
|
+
} catch (error) {
|
|
257
|
+
errors.push(
|
|
258
|
+
`Could not read the ${registration.name} changelog: ${error instanceof Error ? error.message : String(error)}`,
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
if (changed) await writeState(path, state);
|
|
263
|
+
return {
|
|
264
|
+
errors,
|
|
265
|
+
...(sections.length > 0 ? { markdown: sections.join("\n\n") } : {}),
|
|
266
|
+
};
|
|
267
|
+
} finally {
|
|
268
|
+
await release();
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function registerCoordinator(
|
|
273
|
+
pi: ExtensionAPI,
|
|
274
|
+
registration: PackageRegistration,
|
|
275
|
+
): void {
|
|
276
|
+
let accepted = false;
|
|
277
|
+
pi.events.emit(REGISTRATION_CHANNEL, {
|
|
278
|
+
registration,
|
|
279
|
+
accept: () => {
|
|
280
|
+
accepted = true;
|
|
281
|
+
},
|
|
282
|
+
} satisfies RegistrationEvent);
|
|
283
|
+
if (accepted) return;
|
|
284
|
+
|
|
285
|
+
const registrations = new Map([[registration.name, registration]]);
|
|
286
|
+
pi.events.on(REGISTRATION_CHANNEL, (value) => {
|
|
287
|
+
if (!isRegistrationEvent(value)) return;
|
|
288
|
+
registrations.set(value.registration.name, value.registration);
|
|
289
|
+
value.accept();
|
|
290
|
+
});
|
|
291
|
+
pi.registerEntryRenderer<ChangelogEntryData>(
|
|
292
|
+
ENTRY_TYPE,
|
|
293
|
+
(entry, _options, theme) => {
|
|
294
|
+
if (!entry.data) return undefined;
|
|
295
|
+
const container = new Container();
|
|
296
|
+
container.addChild(new DynamicBorder());
|
|
297
|
+
container.addChild(
|
|
298
|
+
new Text(theme.bold(theme.fg("accent", "What's New")), 1, 0),
|
|
299
|
+
);
|
|
300
|
+
container.addChild(new Spacer(1));
|
|
301
|
+
container.addChild(
|
|
302
|
+
new Markdown(entry.data.markdown.trim(), 1, 0, getMarkdownTheme()),
|
|
303
|
+
);
|
|
304
|
+
container.addChild(new Spacer(1));
|
|
305
|
+
container.addChild(new DynamicBorder());
|
|
306
|
+
return container;
|
|
307
|
+
},
|
|
308
|
+
);
|
|
309
|
+
pi.on("session_start", async (event, ctx) => {
|
|
310
|
+
if (event.reason === "reload" || ctx.mode !== "tui") return;
|
|
311
|
+
if (
|
|
312
|
+
ctx.sessionManager
|
|
313
|
+
.getEntries()
|
|
314
|
+
.some((entry) =>
|
|
315
|
+
["branch_summary", "compaction", "message"].includes(entry.type),
|
|
316
|
+
)
|
|
317
|
+
)
|
|
318
|
+
return;
|
|
319
|
+
try {
|
|
320
|
+
const update = await claimUpdates(registrations.values());
|
|
321
|
+
if (update.markdown)
|
|
322
|
+
pi.appendEntry<ChangelogEntryData>(ENTRY_TYPE, {
|
|
323
|
+
markdown: update.markdown,
|
|
324
|
+
});
|
|
325
|
+
for (const error of update.errors) ctx.ui.notify(error, "warning");
|
|
326
|
+
} catch (error) {
|
|
327
|
+
ctx.ui.notify(
|
|
328
|
+
`Could not update Howaboua changelog state: ${error instanceof Error ? error.message : String(error)}`,
|
|
329
|
+
"warning",
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export default function howabouaPackageChangelog(pi: ExtensionAPI): void {
|
|
336
|
+
registerCoordinator(pi, packageRegistration());
|
|
337
|
+
}
|
package/codex-model-shortcuts.ts
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Adds short slash commands for
|
|
2
|
+
* Adds short slash commands for OpenAI Codex model variants.
|
|
3
3
|
* The commands use Pi's model registry, so normal provider authentication and
|
|
4
4
|
* model availability checks remain in charge.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
8
|
+
import {
|
|
9
|
+
ensureGptSwitcherConfig,
|
|
10
|
+
type GptSwitcherConfig,
|
|
11
|
+
readGptSwitcherConfig,
|
|
12
|
+
type ShortcutAlias,
|
|
13
|
+
THINKING_LEVELS,
|
|
14
|
+
type ThinkingLevel,
|
|
15
|
+
} from "./config.js";
|
|
8
16
|
|
|
9
17
|
const MODELS = {
|
|
10
18
|
sol: "gpt-5.6-sol",
|
|
11
19
|
terra: "gpt-5.6-terra",
|
|
12
20
|
luna: "gpt-5.6-luna",
|
|
21
|
+
astra: "gpt-6-astra",
|
|
13
22
|
} as const;
|
|
14
23
|
|
|
15
|
-
type Alias = keyof typeof MODELS;
|
|
16
|
-
type ThinkingLevel =
|
|
17
|
-
| "off"
|
|
18
|
-
| "minimal"
|
|
19
|
-
| "low"
|
|
20
|
-
| "medium"
|
|
21
|
-
| "high"
|
|
22
|
-
| "xhigh"
|
|
23
|
-
| "max";
|
|
24
|
+
type Alias = keyof typeof MODELS & ShortcutAlias;
|
|
24
25
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"low",
|
|
30
|
-
"medium",
|
|
31
|
-
"high",
|
|
32
|
-
"xhigh",
|
|
33
|
-
"max",
|
|
34
|
-
]);
|
|
35
|
-
|
|
36
|
-
function parseThinkingLevel(args: string): ThinkingLevel | undefined {
|
|
26
|
+
function parseThinkingLevel(
|
|
27
|
+
args: string,
|
|
28
|
+
defaultThinkingLevel: ThinkingLevel,
|
|
29
|
+
): ThinkingLevel | undefined {
|
|
37
30
|
const requested = args.trim().toLowerCase();
|
|
38
|
-
if (requested === "") return
|
|
31
|
+
if (requested === "") return defaultThinkingLevel;
|
|
39
32
|
return THINKING_LEVELS.has(requested as ThinkingLevel)
|
|
40
33
|
? (requested as ThinkingLevel)
|
|
41
34
|
: undefined;
|
|
42
35
|
}
|
|
43
36
|
|
|
44
|
-
export default function (
|
|
37
|
+
export default function (
|
|
38
|
+
pi: ExtensionAPI,
|
|
39
|
+
options: { getConfig?: () => GptSwitcherConfig } = {},
|
|
40
|
+
) {
|
|
41
|
+
const getConfig = options.getConfig ?? readGptSwitcherConfig;
|
|
42
|
+
if (!options.getConfig) ensureGptSwitcherConfig();
|
|
43
|
+
|
|
45
44
|
for (const alias of Object.keys(MODELS) as Alias[]) {
|
|
46
45
|
pi.registerCommand(alias, {
|
|
47
|
-
description: `Switch to GPT
|
|
46
|
+
description: `Switch to GPT-${alias === "astra" ? "6" : "5.6"} ${alias.charAt(0).toUpperCase()}${alias.slice(1)}`,
|
|
48
47
|
handler: async (args, ctx) => {
|
|
49
|
-
const
|
|
48
|
+
const defaults = getConfig()[alias];
|
|
49
|
+
const thinkingLevel = parseThinkingLevel(args, defaults.reasoning);
|
|
50
50
|
if (!thinkingLevel) {
|
|
51
51
|
ctx.ui.notify(
|
|
52
52
|
`Usage: /${alias} [off|minimal|low|medium|high|xhigh|max]`,
|
|
@@ -63,12 +63,18 @@ export default function (pi: ExtensionAPI) {
|
|
|
63
63
|
return;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
+
const selectedModel = {
|
|
67
|
+
...model,
|
|
68
|
+
contextWindow: defaults.contextWindow,
|
|
69
|
+
};
|
|
66
70
|
const alreadySelected =
|
|
67
|
-
ctx.model?.provider === "openai-codex" &&
|
|
71
|
+
ctx.model?.provider === "openai-codex" &&
|
|
72
|
+
ctx.model.id === modelId &&
|
|
73
|
+
ctx.model.contextWindow === defaults.contextWindow;
|
|
68
74
|
if (!alreadySelected) {
|
|
69
75
|
let switched: boolean;
|
|
70
76
|
try {
|
|
71
|
-
switched = await pi.setModel(
|
|
77
|
+
switched = await pi.setModel(selectedModel);
|
|
72
78
|
} catch (error) {
|
|
73
79
|
ctx.ui.notify(
|
|
74
80
|
`Could not switch to ${model.name}: ${error instanceof Error ? error.message : String(error)}`,
|
package/config.ts
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
|
|
5
|
+
export const SHORTCUT_ALIASES = ["sol", "terra", "luna", "astra"] as const;
|
|
6
|
+
export type ShortcutAlias = (typeof SHORTCUT_ALIASES)[number];
|
|
7
|
+
export type ThinkingLevel =
|
|
8
|
+
| "off"
|
|
9
|
+
| "minimal"
|
|
10
|
+
| "low"
|
|
11
|
+
| "medium"
|
|
12
|
+
| "high"
|
|
13
|
+
| "xhigh"
|
|
14
|
+
| "max";
|
|
15
|
+
|
|
16
|
+
export const THINKING_LEVELS = new Set<ThinkingLevel>([
|
|
17
|
+
"off",
|
|
18
|
+
"minimal",
|
|
19
|
+
"low",
|
|
20
|
+
"medium",
|
|
21
|
+
"high",
|
|
22
|
+
"xhigh",
|
|
23
|
+
"max",
|
|
24
|
+
]);
|
|
25
|
+
|
|
26
|
+
export type ShortcutDefaults = {
|
|
27
|
+
contextWindow: number;
|
|
28
|
+
reasoning: ThinkingLevel;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export type GptSwitcherConfig = Record<ShortcutAlias, ShortcutDefaults>;
|
|
32
|
+
|
|
33
|
+
export const DEFAULT_GPT_SWITCHER_CONFIG: GptSwitcherConfig = {
|
|
34
|
+
sol: { contextWindow: 272_000, reasoning: "high" },
|
|
35
|
+
terra: { contextWindow: 872_000, reasoning: "high" },
|
|
36
|
+
luna: { contextWindow: 472_000, reasoning: "xhigh" },
|
|
37
|
+
astra: { contextWindow: 272_000, reasoning: "low" },
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const CONFIG_BASENAME = "pi-gpt-switcher.json";
|
|
41
|
+
const MIN_CONTEXT_WINDOW = 128_000;
|
|
42
|
+
|
|
43
|
+
function isObject(value: unknown): value is Record<string, unknown> {
|
|
44
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function configurationError(message: string): void {
|
|
48
|
+
console.warn(`[pi-gpt-switcher] ${message}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function normalizeContextWindow(
|
|
52
|
+
value: unknown,
|
|
53
|
+
fallback: number,
|
|
54
|
+
alias: ShortcutAlias,
|
|
55
|
+
): number {
|
|
56
|
+
if (value === undefined) return fallback;
|
|
57
|
+
if (
|
|
58
|
+
typeof value === "number" &&
|
|
59
|
+
Number.isSafeInteger(value) &&
|
|
60
|
+
value >= MIN_CONTEXT_WINDOW &&
|
|
61
|
+
value <= fallback
|
|
62
|
+
)
|
|
63
|
+
return value;
|
|
64
|
+
configurationError(
|
|
65
|
+
`${alias}.contextWindow must be an integer from ${MIN_CONTEXT_WINDOW} to ${fallback}; using the default`,
|
|
66
|
+
);
|
|
67
|
+
return fallback;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function normalizeReasoning(
|
|
71
|
+
value: unknown,
|
|
72
|
+
fallback: ThinkingLevel,
|
|
73
|
+
alias: ShortcutAlias,
|
|
74
|
+
): ThinkingLevel {
|
|
75
|
+
if (value === undefined) return fallback;
|
|
76
|
+
if (typeof value === "string") {
|
|
77
|
+
const normalized = value.trim().toLowerCase();
|
|
78
|
+
if (THINKING_LEVELS.has(normalized as ThinkingLevel))
|
|
79
|
+
return normalized as ThinkingLevel;
|
|
80
|
+
}
|
|
81
|
+
configurationError(
|
|
82
|
+
`${alias}.reasoning must be a supported Pi thinking level; using the default`,
|
|
83
|
+
);
|
|
84
|
+
return fallback;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function normalizeGptSwitcherConfig(value: unknown): GptSwitcherConfig {
|
|
88
|
+
if (!isObject(value)) {
|
|
89
|
+
configurationError("configuration must be a JSON object; using defaults");
|
|
90
|
+
return structuredClone(DEFAULT_GPT_SWITCHER_CONFIG);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return Object.fromEntries(
|
|
94
|
+
SHORTCUT_ALIASES.map((alias) => {
|
|
95
|
+
const defaults = DEFAULT_GPT_SWITCHER_CONFIG[alias];
|
|
96
|
+
const configured = value[alias];
|
|
97
|
+
if (configured === undefined) return [alias, { ...defaults }];
|
|
98
|
+
if (!isObject(configured)) {
|
|
99
|
+
configurationError(`${alias} must be an object; using defaults`);
|
|
100
|
+
return [alias, { ...defaults }];
|
|
101
|
+
}
|
|
102
|
+
return [
|
|
103
|
+
alias,
|
|
104
|
+
{
|
|
105
|
+
contextWindow: normalizeContextWindow(
|
|
106
|
+
configured["contextWindow"],
|
|
107
|
+
defaults.contextWindow,
|
|
108
|
+
alias,
|
|
109
|
+
),
|
|
110
|
+
reasoning: normalizeReasoning(
|
|
111
|
+
configured["reasoning"],
|
|
112
|
+
defaults.reasoning,
|
|
113
|
+
alias,
|
|
114
|
+
),
|
|
115
|
+
},
|
|
116
|
+
];
|
|
117
|
+
}),
|
|
118
|
+
) as GptSwitcherConfig;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function getGptSwitcherConfigPath(
|
|
122
|
+
agentDir: string = getAgentDir(),
|
|
123
|
+
): string {
|
|
124
|
+
return join(agentDir, CONFIG_BASENAME);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function ensureGptSwitcherConfig(
|
|
128
|
+
configPath: string = getGptSwitcherConfigPath(),
|
|
129
|
+
): void {
|
|
130
|
+
if (existsSync(configPath)) return;
|
|
131
|
+
try {
|
|
132
|
+
mkdirSync(dirname(configPath), { recursive: true, mode: 0o700 });
|
|
133
|
+
writeFileSync(
|
|
134
|
+
configPath,
|
|
135
|
+
`${JSON.stringify(DEFAULT_GPT_SWITCHER_CONFIG, null, 2)}\n`,
|
|
136
|
+
{ encoding: "utf8", mode: 0o600, flag: "wx" },
|
|
137
|
+
);
|
|
138
|
+
} catch (error) {
|
|
139
|
+
if ((error as NodeJS.ErrnoException).code === "EEXIST") return;
|
|
140
|
+
configurationError(
|
|
141
|
+
`could not create ${configPath}: ${error instanceof Error ? error.message : String(error)}`,
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function readGptSwitcherConfig(
|
|
147
|
+
configPath: string = getGptSwitcherConfigPath(),
|
|
148
|
+
): GptSwitcherConfig {
|
|
149
|
+
try {
|
|
150
|
+
return normalizeGptSwitcherConfig(
|
|
151
|
+
JSON.parse(readFileSync(configPath, "utf8")) as unknown,
|
|
152
|
+
);
|
|
153
|
+
} catch (error) {
|
|
154
|
+
configurationError(
|
|
155
|
+
`could not read ${configPath}: ${error instanceof Error ? error.message : String(error)}; using defaults`,
|
|
156
|
+
);
|
|
157
|
+
return structuredClone(DEFAULT_GPT_SWITCHER_CONFIG);
|
|
158
|
+
}
|
|
159
|
+
}
|
package/index.ts
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
1
|
/** Pi package entry point for the Codex model shortcut extension. */
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
import registerPackageChangelog from "./changelog.js";
|
|
5
|
+
import registerCodexModelShortcuts from "./codex-model-shortcuts.js";
|
|
6
|
+
|
|
7
|
+
export default function gptSwitcher(
|
|
8
|
+
pi: ExtensionAPI,
|
|
9
|
+
options: Parameters<typeof registerCodexModelShortcuts>[1] = {},
|
|
10
|
+
): void {
|
|
11
|
+
registerPackageChangelog(pi);
|
|
12
|
+
registerCodexModelShortcuts(pi, options);
|
|
13
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@howaboua/pi-gpt-switcher",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Quick Pi commands for switching between GPT
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Quick Pi commands for switching between GPT Codex models and reasoning levels.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "Igor Warzocha",
|
|
@@ -31,10 +31,13 @@
|
|
|
31
31
|
"files": [
|
|
32
32
|
"index.ts",
|
|
33
33
|
"codex-model-shortcuts.ts",
|
|
34
|
+
"config.ts",
|
|
34
35
|
"README.md",
|
|
35
36
|
"LICENSE",
|
|
36
37
|
"tsconfig.json",
|
|
37
|
-
"biome.json"
|
|
38
|
+
"biome.json",
|
|
39
|
+
"changelog.ts",
|
|
40
|
+
"CHANGELOG.md"
|
|
38
41
|
],
|
|
39
42
|
"pi": {
|
|
40
43
|
"extensions": [
|
|
@@ -42,12 +45,13 @@
|
|
|
42
45
|
]
|
|
43
46
|
},
|
|
44
47
|
"peerDependencies": {
|
|
45
|
-
"@earendil-works/pi-coding-agent": ">=0.
|
|
48
|
+
"@earendil-works/pi-coding-agent": ">=0.84.3",
|
|
49
|
+
"@earendil-works/pi-tui": ">=0.84.3"
|
|
46
50
|
},
|
|
47
51
|
"devDependencies": {
|
|
48
|
-
"@biomejs/biome": "^2.
|
|
49
|
-
"@earendil-works/pi-coding-agent": "0.
|
|
50
|
-
"typescript": "^
|
|
52
|
+
"@biomejs/biome": "^2.5.10",
|
|
53
|
+
"@earendil-works/pi-coding-agent": "0.85.0",
|
|
54
|
+
"typescript": "^7.0.2"
|
|
51
55
|
},
|
|
52
56
|
"scripts": {
|
|
53
57
|
"check": "bun run lint && bun run typecheck && bun run test",
|