@hieplp/pi-account-switcher 0.2.3 → 0.2.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hieplp/pi-account-switcher",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "type": "module",
5
5
  "description": "Pi extension for quickly switching between multiple accounts/API keys per provider.",
6
6
  "license": "MIT",
@@ -1,4 +1,4 @@
1
- import { completeSimple, type Api, type Model } from "@earendil-works/pi-ai";
1
+ import type { Api, Model } from "@earendil-works/pi-ai";
2
2
  import type { AuthCredential, ExtensionAPI } from "@earendil-works/pi-coding-agent";
3
3
  import type { AccountSwitcher } from "@/runtime";
4
4
  import type { AccountConfig, AccountSwitcherContext, ProviderConfig, SecretSource } from "@/types";
@@ -209,6 +209,7 @@ class VerifyAccountsCommand extends AccountCommand {
209
209
  try {
210
210
  ctx.ui.notify(`${prefix} ping: sending request via ${model.provider}/${model.id}...`, "info");
211
211
 
212
+ const { completeSimple } = await import("@earendil-works/pi-ai");
212
213
  const response = await completeSimple(
213
214
  model,
214
215
  {
package/src/extension.ts CHANGED
@@ -1,15 +1,47 @@
1
1
  import { createJiti } from "@mariozechner/jiti";
2
2
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
3
+ import { createRequire } from "node:module";
3
4
  import { dirname } from "node:path";
4
5
  import { fileURLToPath } from "node:url";
5
6
 
6
7
  export default async function accountSwitcherBootstrap(pi: ExtensionAPI) {
7
8
  const srcDir = dirname(fileURLToPath(import.meta.url));
8
- const jiti = createJiti(import.meta.url, {
9
- alias: {
10
- "@": srcDir,
11
- },
12
- });
9
+
10
+ // Resolve @earendil-works/* from the pi loader's context so the same
11
+ // module instances are used (avoids duplicate singletons / instanceof mismatches).
12
+ const loaderRequire = createRequire(import.meta.url);
13
+ const resolveOrUndefined = (id: string): string | undefined => {
14
+ try {
15
+ return loaderRequire.resolve(id);
16
+ } catch {
17
+ return undefined;
18
+ }
19
+ };
20
+
21
+ const piAiEntry = resolveOrUndefined("@earendil-works/pi-ai");
22
+ const piCodingAgentEntry = resolveOrUndefined("@earendil-works/pi-coding-agent");
23
+ const piAgentCoreEntry = resolveOrUndefined("@earendil-works/pi-agent-core");
24
+ const piTuiEntry = resolveOrUndefined("@earendil-works/pi-tui");
25
+
26
+ const alias: Record<string, string> = { "@": srcDir };
27
+ if (piAiEntry) {
28
+ alias["@earendil-works/pi-ai"] = piAiEntry;
29
+ alias["@mariozechner/pi-ai"] = piAiEntry;
30
+ }
31
+ if (piCodingAgentEntry) {
32
+ alias["@earendil-works/pi-coding-agent"] = piCodingAgentEntry;
33
+ alias["@mariozechner/pi-coding-agent"] = piCodingAgentEntry;
34
+ }
35
+ if (piAgentCoreEntry) {
36
+ alias["@earendil-works/pi-agent-core"] = piAgentCoreEntry;
37
+ alias["@mariozechner/pi-agent-core"] = piAgentCoreEntry;
38
+ }
39
+ if (piTuiEntry) {
40
+ alias["@earendil-works/pi-tui"] = piTuiEntry;
41
+ alias["@mariozechner/pi-tui"] = piTuiEntry;
42
+ }
43
+
44
+ const jiti = createJiti(import.meta.url, { alias });
13
45
 
14
46
  const extension = await jiti.import<(pi: ExtensionAPI) => void | Promise<void>>("./index", {
15
47
  default: true,
@@ -1,4 +1,3 @@
1
- import * as piAi from "@earendil-works/pi-ai";
2
1
  import { readFile } from "node:fs/promises";
3
2
  import type { ModelRegistry } from "@earendil-works/pi-coding-agent";
4
3
  import type { AccountConfig, SecretSource } from "@/types";
@@ -90,10 +89,18 @@ export const accountUtil = {
90
89
  };
91
90
 
92
91
  function closeCachedSessions(): void {
93
- const helpers = piAi as {
94
- cleanupSessionResources?: () => void;
95
- closeOpenAICodexWebSocketSessions?: () => void;
96
- };
97
- helpers.cleanupSessionResources?.();
98
- helpers.closeOpenAICodexWebSocketSessions?.();
92
+ // Dynamic import so the module is not required at load time — @earendil-works/pi-ai
93
+ // is a peerDependency provided by the pi agent host, not bundled with this package.
94
+ import("@earendil-works/pi-ai")
95
+ .then((piAi) => {
96
+ const helpers = piAi as {
97
+ cleanupSessionResources?: () => void;
98
+ closeOpenAICodexWebSocketSessions?: () => void;
99
+ };
100
+ helpers.cleanupSessionResources?.();
101
+ helpers.closeOpenAICodexWebSocketSessions?.();
102
+ })
103
+ .catch(() => {
104
+ // pi-ai not available in this environment — skip session cleanup
105
+ });
99
106
  }