@hieplp/pi-account-switcher 0.2.1 → 0.2.3
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/README.md +32 -22
- package/package.json +12 -8
- package/src/commands/accounts/add.ts +1 -1
- package/src/commands/accounts/edit.ts +2 -2
- package/src/commands/accounts/index.ts +3 -1
- package/src/commands/accounts/list.ts +1 -1
- package/src/commands/accounts/oauth.ts +1 -1
- package/src/commands/accounts/remove.ts +1 -1
- package/src/commands/accounts/shared/base.ts +34 -3
- package/src/commands/accounts/shared/prompts.ts +36 -12
- package/src/commands/accounts/switch.ts +6 -3
- package/src/commands/accounts/verify.ts +298 -0
- package/src/commands/base.ts +11 -5
- package/src/commands/index.ts +1 -1
- package/src/commands/models/add.ts +1 -1
- package/src/commands/models/index.ts +1 -1
- package/src/commands/models/list.ts +16 -6
- package/src/commands/models/remove.ts +2 -2
- package/src/commands/models/shared/base.ts +7 -3
- package/src/commands/models/shared/prompts.ts +1 -1
- package/src/commands/models/shared/select.ts +1 -1
- package/src/commands/providers/add.ts +1 -1
- package/src/commands/providers/edit.ts +1 -1
- package/src/commands/providers/index.ts +1 -1
- package/src/commands/providers/list.ts +5 -2
- package/src/commands/providers/remove.ts +1 -1
- package/src/commands/providers/shared/base.ts +1 -1
- package/src/commands/providers/shared/prompts.ts +24 -25
- package/src/commands/providers/shared/select.ts +1 -1
- package/src/commands/system/export.ts +1 -2
- package/src/commands/system/import.ts +9 -3
- package/src/commands/system/index.ts +1 -1
- package/src/commands/system/reset.ts +1 -1
- package/src/constants/commands.ts +5 -0
- package/src/constants/env.ts +1 -0
- package/src/constants/index.ts +1 -0
- package/src/constants/paths.ts +1 -1
- package/src/extension.ts +2 -4
- package/src/index.ts +1 -1
- package/src/runtime/account-switcher-runtime.ts +23 -13
- package/src/runtime/account-switcher.ts +2 -1
- package/src/runtime/index.ts +2 -4
- package/src/schemas/accounts.ts +1 -2
- package/src/schemas/providers.ts +2 -6
- package/src/services/models.ts +2 -2
- package/src/services/providers.ts +3 -9
- package/src/types/context.ts +1 -1
- package/src/types/index.ts +1 -1
- package/src/utils/accounts.ts +2 -2
- package/src/utils/commands.ts +10 -0
- package/src/utils/common.ts +15 -0
- package/src/utils/errors.ts +1 -3
- package/src/utils/filterable-selector.ts +123 -3
- package/src/utils/index.ts +1 -0
- package/src/utils/models.ts +4 -2
- package/src/utils/ui.ts +36 -2
package/src/utils/accounts.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import * as piAi from "@
|
|
1
|
+
import * as piAi from "@earendil-works/pi-ai";
|
|
2
2
|
import { readFile } from "node:fs/promises";
|
|
3
|
-
import type { ModelRegistry } from "@
|
|
3
|
+
import type { ModelRegistry } from "@earendil-works/pi-coding-agent";
|
|
4
4
|
import type { AccountConfig, SecretSource } from "@/types";
|
|
5
5
|
import { commonUtil } from "./common";
|
|
6
6
|
import { fileUtil } from "./files";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { COMMAND_PREFIX_ENV } from "@/constants";
|
|
2
|
+
|
|
3
|
+
export const commandUtil = {
|
|
4
|
+
name: (name: string): string => {
|
|
5
|
+
const prefix = process.env[COMMAND_PREFIX_ENV]?.trim();
|
|
6
|
+
if (!prefix) return name;
|
|
7
|
+
|
|
8
|
+
return `${prefix.endsWith(":") ? prefix : `${prefix}:`}${name}`;
|
|
9
|
+
},
|
|
10
|
+
};
|
package/src/utils/common.ts
CHANGED
|
@@ -71,4 +71,19 @@ export const commonUtil = {
|
|
|
71
71
|
});
|
|
72
72
|
return stdout.trim();
|
|
73
73
|
},
|
|
74
|
+
|
|
75
|
+
runWithConcurrency: async <T, R>(items: T[], concurrency: number, worker: (item: T) => Promise<R>): Promise<R[]> => {
|
|
76
|
+
const results = new Array<R>(items.length);
|
|
77
|
+
let nextIndex = 0;
|
|
78
|
+
|
|
79
|
+
const runNext = async (): Promise<void> => {
|
|
80
|
+
const index = nextIndex++;
|
|
81
|
+
if (index >= items.length) return;
|
|
82
|
+
results[index] = await worker(items[index]);
|
|
83
|
+
await runNext();
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
await Promise.all(Array.from({ length: Math.min(concurrency, items.length) }, runNext));
|
|
87
|
+
return results;
|
|
88
|
+
},
|
|
74
89
|
};
|
package/src/utils/errors.ts
CHANGED
|
@@ -3,9 +3,7 @@ import z from "zod";
|
|
|
3
3
|
export const errorUtil = {
|
|
4
4
|
format: (error: unknown): string => {
|
|
5
5
|
if (error instanceof z.ZodError) {
|
|
6
|
-
return error.issues
|
|
7
|
-
.map((issue) => `${errorUtil.formatPath(issue.path)}: ${issue.message}`)
|
|
8
|
-
.join("; ");
|
|
6
|
+
return error.issues.map((issue) => `${errorUtil.formatPath(issue.path)}: ${issue.message}`).join("; ");
|
|
9
7
|
}
|
|
10
8
|
return error instanceof Error ? error.message : String(error);
|
|
11
9
|
},
|
|
@@ -1,9 +1,129 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { Container, fuzzyFilter, getKeybindings, Input, Spacer, Text, type Focusable, type Component } from "@earendil-works/pi-tui";
|
|
2
|
+
import type { ThemeColor } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Local implementation of DynamicBorder to avoid a hard runtime dependency
|
|
6
|
+
* on @earendil-works/pi-coding-agent (which is a peer dep not available in
|
|
7
|
+
* the pi agent's npm node_modules at extension load time).
|
|
8
|
+
*/
|
|
9
|
+
class DynamicBorder implements Component {
|
|
10
|
+
private color: (str: string) => string;
|
|
11
|
+
constructor(color: (str: string) => string = (str) => str) {
|
|
12
|
+
this.color = color;
|
|
13
|
+
}
|
|
14
|
+
invalidate() {}
|
|
15
|
+
render(width: number): string[] {
|
|
16
|
+
return [this.color("─".repeat(Math.max(1, width)))];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
4
19
|
|
|
5
20
|
type Theme = { fg: (color: ThemeColor, text: string) => string; bold: (text: string) => string };
|
|
6
21
|
|
|
22
|
+
export class FilterableMultiSelectComponent extends Container implements Focusable {
|
|
23
|
+
private readonly listContainer: Container;
|
|
24
|
+
private readonly theme: Theme;
|
|
25
|
+
private selectedIndex = 0;
|
|
26
|
+
private checked: boolean[];
|
|
27
|
+
|
|
28
|
+
_focused = false;
|
|
29
|
+
get focused() {
|
|
30
|
+
return this._focused;
|
|
31
|
+
}
|
|
32
|
+
set focused(value: boolean) {
|
|
33
|
+
this._focused = value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
constructor(
|
|
37
|
+
title: string,
|
|
38
|
+
private readonly options: string[],
|
|
39
|
+
initialChecked: boolean[],
|
|
40
|
+
private readonly onDone: (selected: string[] | undefined) => void,
|
|
41
|
+
theme: Theme,
|
|
42
|
+
private readonly disabled: boolean[] = [],
|
|
43
|
+
) {
|
|
44
|
+
super();
|
|
45
|
+
this.theme = theme;
|
|
46
|
+
this.checked = options.map((_, i) => !this.disabled[i] && (initialChecked[i] ?? false));
|
|
47
|
+
this.selectedIndex = Math.max(
|
|
48
|
+
0,
|
|
49
|
+
this.disabled.findIndex((value) => !value),
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
const borderColor = (str: string) => theme.fg("border", str);
|
|
53
|
+
this.addChild(new DynamicBorder(borderColor));
|
|
54
|
+
this.addChild(new Spacer(1));
|
|
55
|
+
this.addChild(new Text(theme.fg("accent", theme.bold(title)), 1, 0));
|
|
56
|
+
this.addChild(new Spacer(1));
|
|
57
|
+
|
|
58
|
+
this.listContainer = new Container();
|
|
59
|
+
this.addChild(this.listContainer);
|
|
60
|
+
this.addChild(new Spacer(1));
|
|
61
|
+
|
|
62
|
+
const hint = (key: string, desc: string) => theme.fg("dim", key) + theme.fg("muted", ` ${desc}`);
|
|
63
|
+
this.addChild(
|
|
64
|
+
new Text(
|
|
65
|
+
hint("↑↓", "navigate") +
|
|
66
|
+
" " +
|
|
67
|
+
hint("Space", "toggle") +
|
|
68
|
+
" " +
|
|
69
|
+
hint("Enter", "run") +
|
|
70
|
+
" " +
|
|
71
|
+
hint("Esc", "cancel"),
|
|
72
|
+
1,
|
|
73
|
+
0,
|
|
74
|
+
),
|
|
75
|
+
);
|
|
76
|
+
this.addChild(new Spacer(1));
|
|
77
|
+
this.addChild(new DynamicBorder(borderColor));
|
|
78
|
+
|
|
79
|
+
this.updateList();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
handleInput(keyData: string) {
|
|
83
|
+
const kb = getKeybindings();
|
|
84
|
+
if (kb.matches(keyData, "tui.select.up")) {
|
|
85
|
+
this.moveSelection(-1);
|
|
86
|
+
} else if (kb.matches(keyData, "tui.select.down")) {
|
|
87
|
+
this.moveSelection(1);
|
|
88
|
+
} else if (keyData === " ") {
|
|
89
|
+
this.toggleSelected();
|
|
90
|
+
} else if (kb.matches(keyData, "tui.select.confirm")) {
|
|
91
|
+
this.onDone(this.options.filter((_, i) => this.checked[i] && !this.disabled[i]));
|
|
92
|
+
} else if (kb.matches(keyData, "tui.select.cancel")) {
|
|
93
|
+
this.onDone(undefined);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private moveSelection(delta: number) {
|
|
98
|
+
if (this.options.length === 0) return;
|
|
99
|
+
for (let step = 0; step < this.options.length; step++) {
|
|
100
|
+
this.selectedIndex = (this.selectedIndex + delta + this.options.length) % this.options.length;
|
|
101
|
+
if (!this.disabled[this.selectedIndex]) break;
|
|
102
|
+
}
|
|
103
|
+
this.updateList();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private toggleSelected() {
|
|
107
|
+
if (this.disabled[this.selectedIndex]) return;
|
|
108
|
+
this.checked[this.selectedIndex] = !this.checked[this.selectedIndex];
|
|
109
|
+
this.updateList();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private updateList() {
|
|
113
|
+
this.listContainer.clear();
|
|
114
|
+
for (let i = 0; i < this.options.length; i++) {
|
|
115
|
+
const isSelected = i === this.selectedIndex;
|
|
116
|
+
if (this.disabled[i]) {
|
|
117
|
+
this.listContainer.addChild(new Text(this.theme.fg("muted", ` ${this.options[i]}`), 0, 0));
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
const marker = this.checked[i] ? "[x]" : "[ ]";
|
|
121
|
+
const line = `${isSelected ? "›" : " "} ${marker} ${this.options[i]}`;
|
|
122
|
+
this.listContainer.addChild(new Text(isSelected ? this.theme.fg("accent", line) : line, 0, 0));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
7
127
|
export class FilterableExtensionSelectorComponent extends Container implements Focusable {
|
|
8
128
|
private readonly searchInput: Input;
|
|
9
129
|
private readonly listContainer: Container;
|
package/src/utils/index.ts
CHANGED
package/src/utils/models.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Api, Model } from "@
|
|
1
|
+
import type { Api, Model } from "@earendil-works/pi-ai";
|
|
2
2
|
import type { AccountConfig, AccountSwitcherContext, ProviderConfig } from "@/types";
|
|
3
3
|
import { providerUtil } from "./providers";
|
|
4
4
|
import { uiUtil } from "./ui";
|
|
@@ -10,8 +10,10 @@ export const modelUtil = {
|
|
|
10
10
|
ctx: AccountSwitcherContext,
|
|
11
11
|
account: AccountConfig,
|
|
12
12
|
providers: ProviderConfig[],
|
|
13
|
+
resolvedProvider?: string,
|
|
13
14
|
): Promise<ProviderModel | undefined> => {
|
|
14
|
-
const accountProvider =
|
|
15
|
+
const accountProvider =
|
|
16
|
+
resolvedProvider ?? normalizeProvider(account.piAuth?.provider ?? account.provider, providers);
|
|
15
17
|
const candidates = getProviderModels(ctx, providers, accountProvider);
|
|
16
18
|
|
|
17
19
|
if (candidates.length === 0) {
|
package/src/utils/ui.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { ExtensionUIContext } from "@
|
|
1
|
+
import type { ExtensionUIContext } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import { commonUtil } from "./common.js";
|
|
3
|
-
import { FilterableExtensionSelectorComponent } from "./filterable-selector.js";
|
|
3
|
+
import { FilterableExtensionSelectorComponent, FilterableMultiSelectComponent } from "./filterable-selector.js";
|
|
4
4
|
|
|
5
5
|
function deduplicateLabels(labels: string[]): string[] {
|
|
6
6
|
const seen = new Map<string, number>();
|
|
@@ -46,4 +46,38 @@ export const uiUtil = {
|
|
|
46
46
|
(_tui, theme, _keybindings, done) => new FilterableExtensionSelectorComponent(title, options, done, theme),
|
|
47
47
|
);
|
|
48
48
|
},
|
|
49
|
+
|
|
50
|
+
/** Show a checkbox-style multi-select component. */
|
|
51
|
+
multiSelect: (
|
|
52
|
+
ui: ExtensionUIContext,
|
|
53
|
+
title: string,
|
|
54
|
+
options: string[],
|
|
55
|
+
initialChecked: boolean[] = [],
|
|
56
|
+
disabled: boolean[] = [],
|
|
57
|
+
): Promise<string[] | undefined> => {
|
|
58
|
+
return ui.custom<string[] | undefined>(
|
|
59
|
+
(_tui, theme, _keybindings, done) =>
|
|
60
|
+
new FilterableMultiSelectComponent(title, options, initialChecked, done, theme, disabled),
|
|
61
|
+
);
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
/** Like multiSelect but skips items where the corresponding value is null (used for group headers). */
|
|
65
|
+
multiGroupedSelect: async <T>(
|
|
66
|
+
ui: ExtensionUIContext,
|
|
67
|
+
title: string,
|
|
68
|
+
labels: string[],
|
|
69
|
+
values: Array<T | null>,
|
|
70
|
+
initialChecked: boolean[] = [],
|
|
71
|
+
): Promise<T[] | undefined> => {
|
|
72
|
+
const deduped = deduplicateLabels(labels);
|
|
73
|
+
const selected = await uiUtil.multiSelect(
|
|
74
|
+
ui,
|
|
75
|
+
title,
|
|
76
|
+
deduped,
|
|
77
|
+
initialChecked,
|
|
78
|
+
values.map((value) => value === null),
|
|
79
|
+
);
|
|
80
|
+
if (!selected) return undefined;
|
|
81
|
+
return selected.map((label) => values[deduped.indexOf(label)]).filter((value): value is T => value !== null);
|
|
82
|
+
},
|
|
49
83
|
};
|