oc-auth-switcher 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 grenaad
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,109 @@
1
+ # oc-auth-switcher
2
+
3
+ Multi-account rotation companion plugin for OpenCode. Works alongside `@ex-machina/opencode-anthropic-auth` to automatically rotate between multiple Anthropic Claude Max accounts based on utilization thresholds.
4
+
5
+ ## How It Works
6
+
7
+ This plugin does **not** replace your auth plugin. Instead it:
8
+
9
+ 1. Maintains a pool of Anthropic OAuth accounts in a separate file
10
+ 2. Monitors utilization metrics via CLI `ping` commands
11
+ 3. Rotates the active account by writing credentials to `auth.json` via the OpenCode SDK
12
+ 4. `@ex-machina/opencode-anthropic-auth` reads the rotated credentials on the next API request
13
+
14
+ The plugin also hooks into OpenCode's `session.idle` and `chat.message` events to check cached metrics and rotate proactively before thresholds are hit.
15
+
16
+ ## Setup
17
+
18
+ ### 1. Install both plugins
19
+
20
+ ```json
21
+ {
22
+ "plugin": [
23
+ "@ex-machina/opencode-anthropic-auth@1.8.0",
24
+ "oc-auth-switcher@latest"
25
+ ]
26
+ }
27
+ ```
28
+
29
+ **Important:** `@ex-machina/opencode-anthropic-auth` must be listed first.
30
+
31
+ ### 2. Add accounts
32
+
33
+ ```bash
34
+ # Add your primary account
35
+ oc-auth-switcher add primary
36
+
37
+ # Add fallback accounts
38
+ oc-auth-switcher add fallback-1
39
+ oc-auth-switcher add fallback-2
40
+ ```
41
+
42
+ Each `add` command runs an OAuth flow — you'll be given a URL to open in your browser and prompted to paste the callback.
43
+
44
+ ### 3. Ping to capture initial metrics
45
+
46
+ ```bash
47
+ oc-auth-switcher ping
48
+ ```
49
+
50
+ ### 4. Use OpenCode normally
51
+
52
+ The plugin handles rotation automatically. The first account in the pool is always the "primary" (preferred). When it exceeds the utilization threshold, traffic switches to the next available fallback. When the primary recovers, it switches back.
53
+
54
+ ## CLI Commands
55
+
56
+ | Command | Description |
57
+ |---------|-------------|
58
+ | `add [name]` | Add a new account via OAuth |
59
+ | `remove <name>` | Remove an account from the pool |
60
+ | `usage [--watch]` | Show utilization dashboard with progress bars |
61
+ | `config [options]` | View/modify thresholds and check interval |
62
+ | `ping [name]` | Ping account(s) to refresh utilization metrics |
63
+ | `reauth <name>` | Re-authenticate an existing account |
64
+ | `switch <name>` | Manually switch to a specific account |
65
+ | `status` | Show current active account and rotation state |
66
+
67
+ ## Configuration
68
+
69
+ ```bash
70
+ # Set uniform threshold (default: 70%)
71
+ oc-auth-switcher config --threshold 0.80
72
+
73
+ # Set per-metric thresholds (5h, 7d, 7d-sonnet)
74
+ oc-auth-switcher config --thresholds 90,80,70
75
+
76
+ # Set primary recovery check interval (default: 60 min)
77
+ oc-auth-switcher config --interval 30
78
+
79
+ # Reset to defaults
80
+ oc-auth-switcher config --reset
81
+ ```
82
+
83
+ ## Data Files
84
+
85
+ | File | Location | Purpose |
86
+ |------|----------|---------|
87
+ | Accounts | `~/.config/opencode/auth-switcher-accounts.json` | OAuth token pool |
88
+ | State | `~/.config/opencode/auth-switcher-state.json` | Usage metrics, active account, config |
89
+
90
+ Both files use atomic writes with `.bak` fallback for crash safety.
91
+
92
+ ## Rotation Algorithm
93
+
94
+ - The first account in the pool is always "primary" (preferred)
95
+ - When the primary exceeds any utilization threshold, switch to the first fallback under threshold
96
+ - If all fallbacks are also over threshold, pick the one with the lowest utilization score
97
+ - When on a fallback, periodically check if the primary has recovered (configurable interval)
98
+ - Auth failures trigger a 1-hour cooldown per account
99
+
100
+ ## Building from Source
101
+
102
+ ```bash
103
+ bun install
104
+ bun run build
105
+ ```
106
+
107
+ ## License
108
+
109
+ MIT