@victor-software-house/pi-multicodex 2.0.10 → 2.0.11

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.
@@ -4,7 +4,10 @@ import {
4
4
  } from "@mariozechner/pi-ai/oauth";
5
5
  import { AuthStorage } from "@mariozechner/pi-coding-agent";
6
6
  import { normalizeUnknownError } from "pi-provider-utils/streams";
7
- import { loadImportedOpenAICodexAuth } from "./auth";
7
+ import {
8
+ loadImportedOpenAICodexAuth,
9
+ writeActiveTokenToAuthJson,
10
+ } from "./auth";
8
11
  import { isAccountAvailable, pickBestAccount } from "./selection";
9
12
  import {
10
13
  type Account,
@@ -44,6 +47,19 @@ export class AccountManager {
44
47
  }
45
48
  }
46
49
 
50
+ /**
51
+ * Write the active account's tokens to auth.json so pi's background features
52
+ * (rename, compaction) can resolve a valid API key via AuthStorage.
53
+ */
54
+ private syncActiveTokenToAuthJson(account: Account): void {
55
+ writeActiveTokenToAuthJson({
56
+ access: account.accessToken,
57
+ refresh: account.refreshToken,
58
+ expires: account.expiresAt,
59
+ accountId: account.accountId,
60
+ }).catch(() => {});
61
+ }
62
+
47
63
  onStateChange(handler: StateChangeHandler): () => void {
48
64
  this.stateChangeHandlers.add(handler);
49
65
  return () => {
@@ -363,6 +379,7 @@ export class AccountManager {
363
379
  }
364
380
 
365
381
  if (Date.now() < account.expiresAt - 5 * 60 * 1000) {
382
+ this.syncActiveTokenToAuthJson(account);
366
383
  return account.accessToken;
367
384
  }
368
385
 
@@ -390,6 +407,7 @@ export class AccountManager {
390
407
  }
391
408
  this.save();
392
409
  this.notifyStateChanged();
410
+ this.syncActiveTokenToAuthJson(account);
393
411
  return account.accessToken;
394
412
  } catch (error) {
395
413
  this.markNeedsReauth(account);
package/auth.ts CHANGED
@@ -85,6 +85,39 @@ export function parseImportedOpenAICodexAuth(
85
85
  };
86
86
  }
87
87
 
88
+ /**
89
+ * Write the active account's tokens to auth.json so pi's background features
90
+ * (rename, compaction, inline suggestions) can resolve a valid API key through
91
+ * the normal AuthStorage path.
92
+ */
93
+ export async function writeActiveTokenToAuthJson(creds: {
94
+ access: string;
95
+ refresh: string;
96
+ expires: number;
97
+ accountId?: string;
98
+ }): Promise<void> {
99
+ let auth: Record<string, unknown> = {};
100
+ try {
101
+ const raw = await fs.readFile(AUTH_FILE, "utf8");
102
+ const parsed = JSON.parse(raw) as unknown;
103
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
104
+ auth = parsed as Record<string, unknown>;
105
+ }
106
+ } catch {
107
+ // File missing or corrupt — start fresh.
108
+ }
109
+
110
+ auth["openai-codex"] = {
111
+ type: "oauth",
112
+ access: creds.access,
113
+ refresh: creds.refresh,
114
+ expires: creds.expires,
115
+ accountId: creds.accountId,
116
+ };
117
+
118
+ await fs.writeFile(AUTH_FILE, JSON.stringify(auth, null, 2));
119
+ }
120
+
88
121
  export async function loadImportedOpenAICodexAuth(): Promise<
89
122
  ImportedOpenAICodexAuth | undefined
90
123
  > {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@victor-software-house/pi-multicodex",
3
- "version": "2.0.10",
3
+ "version": "2.0.11",
4
4
  "description": "Codex account rotation extension for pi",
5
5
  "license": "MIT",
6
6
  "type": "module",