@hieplp/pi-account-switcher 0.2.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.
Files changed (77) hide show
  1. package/INSTALL_AS_PI_PACKAGE.md +78 -0
  2. package/LICENSE +21 -0
  3. package/README.md +214 -0
  4. package/USAGE.md +446 -0
  5. package/package.json +53 -0
  6. package/src/commands/accounts/add.ts +63 -0
  7. package/src/commands/accounts/edit.ts +31 -0
  8. package/src/commands/accounts/index.ts +19 -0
  9. package/src/commands/accounts/list.ts +31 -0
  10. package/src/commands/accounts/oauth.ts +59 -0
  11. package/src/commands/accounts/remove.ts +40 -0
  12. package/src/commands/accounts/shared/base.ts +88 -0
  13. package/src/commands/accounts/shared/index.ts +3 -0
  14. package/src/commands/accounts/shared/prompts.ts +226 -0
  15. package/src/commands/accounts/shared/select.ts +37 -0
  16. package/src/commands/accounts/switch.ts +54 -0
  17. package/src/commands/base.ts +81 -0
  18. package/src/commands/index.ts +16 -0
  19. package/src/commands/models/add.ts +30 -0
  20. package/src/commands/models/index.ts +13 -0
  21. package/src/commands/models/list.ts +45 -0
  22. package/src/commands/models/remove.ts +41 -0
  23. package/src/commands/models/shared/base.ts +44 -0
  24. package/src/commands/models/shared/index.ts +3 -0
  25. package/src/commands/models/shared/prompts.ts +36 -0
  26. package/src/commands/models/shared/select.ts +37 -0
  27. package/src/commands/providers/add.ts +28 -0
  28. package/src/commands/providers/edit.ts +29 -0
  29. package/src/commands/providers/index.ts +15 -0
  30. package/src/commands/providers/list.ts +38 -0
  31. package/src/commands/providers/remove.ts +46 -0
  32. package/src/commands/providers/shared/base.ts +30 -0
  33. package/src/commands/providers/shared/index.ts +3 -0
  34. package/src/commands/providers/shared/prompts.ts +172 -0
  35. package/src/commands/providers/shared/select.ts +24 -0
  36. package/src/commands/system/index.ts +7 -0
  37. package/src/commands/system/reset.ts +36 -0
  38. package/src/constants/commands.ts +66 -0
  39. package/src/constants/config.ts +6 -0
  40. package/src/constants/index.ts +4 -0
  41. package/src/constants/paths.ts +8 -0
  42. package/src/constants/providers.ts +36 -0
  43. package/src/extension.ts +21 -0
  44. package/src/index.ts +20 -0
  45. package/src/runtime/account-switcher-runtime.ts +194 -0
  46. package/src/runtime/account-switcher.ts +32 -0
  47. package/src/runtime/index.ts +11 -0
  48. package/src/schemas/accounts.ts +50 -0
  49. package/src/schemas/common.ts +3 -0
  50. package/src/schemas/config.ts +7 -0
  51. package/src/schemas/index.ts +4 -0
  52. package/src/schemas/providers.ts +57 -0
  53. package/src/services/accounts.ts +116 -0
  54. package/src/services/index.ts +4 -0
  55. package/src/services/models.ts +27 -0
  56. package/src/services/pi-auth.ts +23 -0
  57. package/src/services/providers.ts +123 -0
  58. package/src/storage/accounts.ts +109 -0
  59. package/src/storage/index.ts +4 -0
  60. package/src/storage/paths.ts +8 -0
  61. package/src/storage/pi-auth.ts +37 -0
  62. package/src/storage/providers.ts +85 -0
  63. package/src/storage/state.ts +43 -0
  64. package/src/types/accounts.ts +37 -0
  65. package/src/types/config.ts +6 -0
  66. package/src/types/context.ts +3 -0
  67. package/src/types/index.ts +4 -0
  68. package/src/types/providers.ts +53 -0
  69. package/src/utils/accounts.ts +99 -0
  70. package/src/utils/common.ts +74 -0
  71. package/src/utils/errors.ts +16 -0
  72. package/src/utils/files.ts +25 -0
  73. package/src/utils/filterable-selector.ts +114 -0
  74. package/src/utils/index.ts +7 -0
  75. package/src/utils/models.ts +76 -0
  76. package/src/utils/providers.ts +49 -0
  77. package/src/utils/ui.ts +49 -0
@@ -0,0 +1,78 @@
1
+ # Install Pi Account Switcher as a Pi Package
2
+
3
+ This repo can be installed by Pi from GitHub now, and can be installed with `pi install npm:@hieplp/pi-account-switcher` after it is published to npm.
4
+
5
+ ## Install from GitHub
6
+
7
+ Install globally:
8
+
9
+ ```bash
10
+ pi install git:github.com/hieplp/pi-account-switcher
11
+ ```
12
+
13
+ Or test for one Pi run without permanently installing:
14
+
15
+ ```bash
16
+ pi -e git:github.com/hieplp/pi-account-switcher
17
+ ```
18
+
19
+ Install project-locally, writing to `.pi/settings.json`:
20
+
21
+ ```bash
22
+ pi install -l git:github.com/hieplp/pi-account-switcher
23
+ ```
24
+
25
+ Then inside Pi, add your first account:
26
+
27
+ ```txt
28
+ /reload
29
+ /accounts:add
30
+ ```
31
+
32
+ ## Install like `pi install npm:@hieplp/pi-account-switcher`
33
+
34
+ The package is configured for npm publishing with:
35
+
36
+ - `keywords: ["pi-package", "pi-extension", ...]`
37
+ - `pi.extensions: ["./src/extension.ts"]`
38
+ - Pi core packages in `peerDependencies`
39
+ - runtime dependency `zod` in `dependencies`
40
+ - package files limited to `src`, `README.md`, `USAGE.md`, and this install guide
41
+
42
+ ### Publish to npm
43
+
44
+ Login to npm:
45
+
46
+ ```bash
47
+ npm login
48
+ ```
49
+
50
+ Optional: verify what will be published:
51
+
52
+ ```bash
53
+ npm pack --dry-run
54
+ ```
55
+
56
+ Publish (scoped packages require `--access public`):
57
+
58
+ ```bash
59
+ npm publish --access public
60
+ ```
61
+
62
+ After publishing, users can install globally with:
63
+
64
+ ```bash
65
+ pi install npm:@hieplp/pi-account-switcher
66
+ ```
67
+
68
+ Or install project-locally with:
69
+
70
+ ```bash
71
+ pi install -l npm:@hieplp/pi-account-switcher
72
+ ```
73
+
74
+ To test without permanently installing:
75
+
76
+ ```bash
77
+ pi -e npm:@hieplp/pi-account-switcher
78
+ ```
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 hieplp
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,214 @@
1
+ # Pi Account Switcher
2
+
3
+ > Switch between multiple API keys and accounts — per provider — inside Pi. No more manual env-var juggling.
4
+
5
+ ```
6
+ claude/work · claude/personal · openai/team · gemini/testing
7
+ ```
8
+
9
+ ---
10
+
11
+ ## Install
12
+
13
+ **From GitHub (recommended)**
14
+
15
+ ```bash
16
+ pi install git:github.com/hieplp/pi-account-switcher
17
+ ```
18
+
19
+ **From npm**
20
+
21
+ ```bash
22
+ pi install npm:@hieplp/pi-account-switcher
23
+ ```
24
+
25
+ **Test without installing**
26
+
27
+ ```bash
28
+ pi -e git:github.com/hieplp/pi-account-switcher
29
+ ```
30
+
31
+ **Project-local install** (writes to `.pi/settings.json`)
32
+
33
+ ```bash
34
+ pi install -l git:github.com/hieplp/pi-account-switcher
35
+ ```
36
+
37
+ **Run from a local checkout**
38
+
39
+ ```bash
40
+ npm install
41
+ pi -e ./src/extension.ts
42
+ ```
43
+
44
+ After installing, reload Pi and add your first account:
45
+
46
+ ```
47
+ /reload
48
+ /accounts:add
49
+ ```
50
+
51
+ ---
52
+
53
+ ## Commands
54
+
55
+ ### Accounts
56
+
57
+ | Command | Description |
58
+ |---|---|
59
+ | `/accounts:add` | Add a new account interactively |
60
+ | `/accounts:list` | List all accounts and activate the selected one |
61
+ | `/accounts:switch` | Switch to another account within the current provider |
62
+ | `/accounts:edit` | Edit label, provider, id, or credential source |
63
+ | `/accounts:remove` | Delete an account |
64
+ | `/accounts:oauth` | Import the current Pi `/login` OAuth session as a named account |
65
+
66
+ ### Providers
67
+
68
+ | Command | Description |
69
+ |---|---|
70
+ | `/providers:add` | Add a reusable custom provider |
71
+ | `/providers:list` | List custom providers |
72
+ | `/providers:edit` | Edit a custom provider |
73
+ | `/providers:remove` | Remove an unused custom provider |
74
+
75
+ ### Models
76
+
77
+ | Command | Description |
78
+ |---|---|
79
+ | `/models:list` | List all available models and switch to the selected one |
80
+ | `/models:add` | Add a custom model config to the current provider |
81
+ | `/models:remove` | Remove a custom model config |
82
+
83
+ ### System
84
+
85
+ | Command | Description |
86
+ |---|---|
87
+ | `/system:reset` | Delete all accounts, providers, and state |
88
+
89
+ ---
90
+
91
+ ## Adding Accounts
92
+
93
+ Run `/accounts:add` and the wizard will ask for:
94
+
95
+ 1. **Provider** — pick a built-in or custom provider
96
+ 2. **Label** — a friendly display name (e.g. `Claude — Work`)
97
+ 3. **Account ID** — a unique slug (e.g. `claude-work`)
98
+ 4. **Credential env var** — e.g. `ANTHROPIC_API_KEY`
99
+ 5. **Secret source** — one of:
100
+ - Pasted API key (stored in plaintext — prefer the options below)
101
+ - Existing environment variable
102
+ - File path
103
+ - Shell command
104
+ - 1Password `op://` reference
105
+
106
+ If the account ID already exists, Pi will ask whether to replace it, enter a new ID, or cancel.
107
+
108
+ ---
109
+
110
+ ## OAuth Accounts (Claude, Codex, etc.)
111
+
112
+ For subscription providers, use Pi's built-in login first, then import it as a named account:
113
+
114
+ ```
115
+ /login
116
+ ```
117
+
118
+ Complete browser/device login, then:
119
+
120
+ ```
121
+ /accounts:oauth
122
+ ```
123
+
124
+ Give it a label like `Claude — Work`. Repeat for as many accounts as you need — each gets its own saved credentials. Switch between them any time with `/accounts:list`.
125
+
126
+ OAuth credentials are read from `~/.pi/agent/auth.json` and written back to Pi's live auth storage on switch.
127
+
128
+ ---
129
+
130
+ ## Custom Providers
131
+
132
+ Define a provider once, reuse it across accounts:
133
+
134
+ ```
135
+ /providers:add
136
+ ```
137
+
138
+ Custom providers are stored at `~/.pi/account-switcher/providers.json` and support all Pi model-provider fields:
139
+
140
+ ```json
141
+ {
142
+ "providers": {
143
+ "acme": {
144
+ "name": "Acme AI",
145
+ "baseUrl": "https://api.acme.test/v1",
146
+ "api": "openai-completions",
147
+ "apiKey": "ACME_API_KEY",
148
+ "envKeys": ["ACME_API_KEY"],
149
+ "aliases": ["acme-ai"],
150
+ "models": [{ "id": "acme-coder", "name": "Acme Coder" }]
151
+ }
152
+ }
153
+ }
154
+ ```
155
+
156
+ > Removing a provider is blocked while any account uses it — edit or remove those accounts first.
157
+
158
+ ---
159
+
160
+ ## Config Reference
161
+
162
+ ### Accounts — `~/.pi/account-switcher/accounts.json`
163
+
164
+ ```json
165
+ {
166
+ "switchMode": "env",
167
+ "accounts": [
168
+ {
169
+ "id": "claude-work",
170
+ "label": "Claude — Work",
171
+ "provider": "anthropic",
172
+ "env": {
173
+ "ANTHROPIC_API_KEY": { "type": "env", "name": "ANTHROPIC_WORK_API_KEY" }
174
+ }
175
+ },
176
+ {
177
+ "id": "openai-personal",
178
+ "label": "OpenAI — Personal",
179
+ "provider": "openai",
180
+ "env": {
181
+ "OPENAI_API_KEY": { "type": "file", "path": "~/.keys/openai-personal.txt" }
182
+ }
183
+ }
184
+ ]
185
+ }
186
+ ```
187
+
188
+ ### Secret Sources
189
+
190
+ ```json
191
+ { "type": "literal", "value": "sk-..." }
192
+ { "type": "env", "name": "MY_API_KEY" }
193
+ { "type": "file", "path": "~/.keys/key.txt" }
194
+ { "type": "command", "command": "op read op://AI/Claude/api-key" }
195
+ { "type": "op", "reference": "op://AI/Claude/api-key" }
196
+ ```
197
+
198
+ A plain string is treated as a literal; strings starting with `op://` are resolved via `op read`.
199
+
200
+ ### State — `~/.pi/account-switcher/state.json`
201
+
202
+ Tracks the selected account and model across sessions. Restored automatically on `session_start`.
203
+
204
+ ---
205
+
206
+ ## Credential Caching
207
+
208
+ On switch, the extension updates `process.env`, Pi's live API-key overrides, and Pi's OAuth auth storage. If a provider still uses old credentials, run `/reload` or restart Pi.
209
+
210
+ ---
211
+
212
+ ## License
213
+
214
+ MIT — see [LICENSE](./LICENSE).