@hieplp/pi-account-switcher 0.2.3 → 0.3.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.
@@ -1,4 +1,5 @@
1
1
  import { exec, execFile } from "node:child_process";
2
+ import { homedir } from "node:os";
2
3
  import { promisify } from "node:util";
3
4
 
4
5
  const execAsync = promisify(exec);
@@ -87,3 +88,29 @@ export const commonUtil = {
87
88
  return results;
88
89
  },
89
90
  };
91
+
92
+ /**
93
+ * Find the account whose dirs contain the longest prefix of cwd.
94
+ * Returns the account id, or undefined if no match.
95
+ * On tie (same dir length on two accounts), first-in-array wins.
96
+ */
97
+ export function findLongestMatchingDir<T extends { id: string; dirs?: string[] }>(
98
+ accounts: T[],
99
+ cwd: string,
100
+ ): string | undefined {
101
+ let bestId: string | undefined;
102
+ let bestLen = -1;
103
+ for (const account of accounts) {
104
+ const dirs = account.dirs;
105
+ if (!dirs || dirs.length === 0) continue;
106
+ for (const dir of dirs) {
107
+ const resolved = dir.startsWith("~") ? dir.replace("~", homedir()) : dir;
108
+ const normalized = resolved.replace(/\/+$/, "");
109
+ if ((cwd === normalized || cwd.startsWith(normalized + "/")) && normalized.length > bestLen) {
110
+ bestLen = normalized.length;
111
+ bestId = account.id;
112
+ }
113
+ }
114
+ }
115
+ return bestId;
116
+ }
@@ -1,4 +1,13 @@
1
- import { Container, fuzzyFilter, getKeybindings, Input, Spacer, Text, type Focusable, type Component } from "@earendil-works/pi-tui";
1
+ import {
2
+ Container,
3
+ fuzzyFilter,
4
+ getKeybindings,
5
+ Input,
6
+ Spacer,
7
+ Text,
8
+ type Focusable,
9
+ type Component,
10
+ } from "@earendil-works/pi-tui";
2
11
  import type { ThemeColor } from "@earendil-works/pi-coding-agent";
3
12
 
4
13
  /**
@@ -22,9 +22,10 @@ export const providerUtil = {
22
22
  );
23
23
  },
24
24
 
25
- providerChoices: (customProviders: ProviderConfig[] = []): string[] => {
25
+ providerChoices: (customProviders: ProviderConfig[] = [], piProviderIds: string[] = []): string[] => {
26
26
  const customIds = customProviders.map((p) => providerUtil.normalizeProvider(p.id)).sort();
27
- return [...BUILT_IN_PROVIDER_IDS, ...customIds, "custom"];
27
+ const dynamicIds = [...new Set(piProviderIds.map(providerUtil.normalizeProvider))].sort();
28
+ return [...new Set([...BUILT_IN_PROVIDER_IDS, ...dynamicIds, ...customIds, "custom"])];
28
29
  },
29
30
 
30
31
  hasProvider: (provider: string, providers: ProviderConfig[]): boolean => {