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

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,23 @@ 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
+ try {
56
+ writeActiveTokenToAuthJson({
57
+ access: account.accessToken,
58
+ refresh: account.refreshToken,
59
+ expires: account.expiresAt,
60
+ accountId: account.accountId,
61
+ });
62
+ } catch {
63
+ // Best-effort sync — do not block token resolution.
64
+ }
65
+ }
66
+
47
67
  onStateChange(handler: StateChangeHandler): () => void {
48
68
  this.stateChangeHandlers.add(handler);
49
69
  return () => {
@@ -363,6 +383,7 @@ export class AccountManager {
363
383
  }
364
384
 
365
385
  if (Date.now() < account.expiresAt - 5 * 60 * 1000) {
386
+ this.syncActiveTokenToAuthJson(account);
366
387
  return account.accessToken;
367
388
  }
368
389
 
@@ -390,6 +411,7 @@ export class AccountManager {
390
411
  }
391
412
  this.save();
392
413
  this.notifyStateChanged();
414
+ this.syncActiveTokenToAuthJson(account);
393
415
  return account.accessToken;
394
416
  } catch (error) {
395
417
  this.markNeedsReauth(account);
package/auth.ts CHANGED
@@ -1,4 +1,9 @@
1
- import { promises as fs } from "node:fs";
1
+ import {
2
+ existsSync,
3
+ promises as fs,
4
+ readFileSync,
5
+ writeFileSync,
6
+ } from "node:fs";
2
7
  import type { OAuthCredentials } from "@mariozechner/pi-ai/oauth";
3
8
  import { getAgentAuthPath } from "pi-provider-utils/agent-paths";
4
9
 
@@ -85,6 +90,47 @@ export function parseImportedOpenAICodexAuth(
85
90
  };
86
91
  }
87
92
 
93
+ /**
94
+ * Write the active account's tokens to auth.json so pi's background features
95
+ * (rename, compaction, inline suggestions) can resolve a valid API key through
96
+ * the normal AuthStorage path.
97
+ */
98
+ /**
99
+ * Synchronously write the active account's tokens to auth.json so pi's
100
+ * background features (rename, compaction) can resolve a valid API key.
101
+ *
102
+ * Uses synchronous I/O to avoid interleaved writes with pi's own code.
103
+ */
104
+ export function writeActiveTokenToAuthJson(creds: {
105
+ access: string;
106
+ refresh: string;
107
+ expires: number;
108
+ accountId?: string;
109
+ }): void {
110
+ let auth: Record<string, unknown> = {};
111
+ try {
112
+ if (existsSync(AUTH_FILE)) {
113
+ const raw = readFileSync(AUTH_FILE, "utf8");
114
+ const parsed = JSON.parse(raw) as unknown;
115
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
116
+ auth = parsed as Record<string, unknown>;
117
+ }
118
+ }
119
+ } catch {
120
+ // File missing or corrupt — start fresh.
121
+ }
122
+
123
+ auth["openai-codex"] = {
124
+ type: "oauth",
125
+ access: creds.access,
126
+ refresh: creds.refresh,
127
+ expires: creds.expires,
128
+ accountId: creds.accountId,
129
+ };
130
+
131
+ writeFileSync(AUTH_FILE, JSON.stringify(auth, null, 2));
132
+ }
133
+
88
134
  export async function loadImportedOpenAICodexAuth(): Promise<
89
135
  ImportedOpenAICodexAuth | undefined
90
136
  > {
@@ -95,11 +141,8 @@ export async function loadImportedOpenAICodexAuth(): Promise<
95
141
  return undefined;
96
142
  }
97
143
  return parseImportedOpenAICodexAuth(parsed as Record<string, unknown>);
98
- } catch (error) {
99
- const withCode = error as Error & { code?: string };
100
- if (withCode.code === "ENOENT") {
101
- return undefined;
102
- }
103
- throw error;
144
+ } catch {
145
+ // File missing, corrupt, or unreadable treat as no imported auth.
146
+ return undefined;
104
147
  }
105
148
  }
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.12",
4
4
  "description": "Codex account rotation extension for pi",
5
5
  "license": "MIT",
6
6
  "type": "module",