@jameslovespancakes/pi-plus 1.0.11 → 1.0.12
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 +44 -20
- package/config/skills/workflow-code-review-actions/SKILL.md +44 -0
- package/package.json +6 -3
- package/src/core/accounts/oauth-pool.ts +102 -0
- package/src/core/accounts/registry.ts +8 -25
- package/src/core/accounts/routing.ts +122 -0
- package/src/core/anthropic/client-identity.ts +9 -64
- package/src/core/anthropic/quota.ts +8 -53
- package/src/core/anthropic/routing.ts +40 -115
- package/src/core/anthropic/store.ts +7 -9
- package/src/core/catalog/quality.ts +33 -15
- package/src/core/codex/quota.ts +8 -2
- package/src/core/codex/store.ts +12 -9
- package/src/core/config.ts +4 -17
- package/src/core/store.ts +13 -12
- package/src/domains/models/provider-picker.ts +1 -1
- package/src/domains/setup/index.ts +23 -23
- package/src/domains/subscriptions/accounts-picker.ts +18 -37
- package/src/domains/subscriptions/accounts.ts +23 -29
- package/src/domains/subscriptions/footer.ts +48 -29
- package/src/domains/subscriptions/index.ts +11 -27
- package/src/domains/subscriptions/provider.ts +38 -103
- package/src/domains/subscriptions/providers/anthropic.ts +5 -17
- package/src/domains/subscriptions/providers/codex.ts +171 -4
- package/src/domains/subscriptions/providers/hosted.ts +18 -0
- package/src/domains/subscriptions/providers/oauth-pool.ts +273 -0
- package/src/domains/subscriptions/routing.ts +19 -11
- package/src/domains/workflows/LICENSE.md +21 -0
- package/src/domains/workflows/index.ts +836 -0
- package/src/domains/workflows/runtime/advisory-challenge.ts +75 -0
- package/src/domains/workflows/runtime/advisory-evidence.ts +90 -0
- package/src/domains/workflows/runtime/advisory-schema.ts +83 -0
- package/src/domains/workflows/runtime/agent-attempt.ts +147 -0
- package/src/domains/workflows/runtime/agent-limits.ts +43 -0
- package/src/domains/workflows/runtime/agent-replay.ts +399 -0
- package/src/domains/workflows/runtime/agent-retry.ts +116 -0
- package/src/domains/workflows/runtime/agent-runner-types.ts +96 -0
- package/src/domains/workflows/runtime/agent-runner.ts +203 -0
- package/src/domains/workflows/runtime/agent-session-identity.ts +256 -0
- package/src/domains/workflows/runtime/agent-session-providers.ts +50 -0
- package/src/domains/workflows/runtime/agent-session.ts +382 -0
- package/src/domains/workflows/runtime/agent-skills.ts +270 -0
- package/src/domains/workflows/runtime/agent-workspace.ts +103 -0
- package/src/domains/workflows/runtime/background-workflow-tool.ts +75 -0
- package/src/domains/workflows/runtime/background-workflows.ts +492 -0
- package/src/domains/workflows/runtime/budget.ts +53 -0
- package/src/domains/workflows/runtime/cancellation.ts +87 -0
- package/src/domains/workflows/runtime/command-completions.ts +36 -0
- package/src/domains/workflows/runtime/concurrency.ts +403 -0
- package/src/domains/workflows/runtime/debug.ts +3 -0
- package/src/domains/workflows/runtime/diff-capture.ts +81 -0
- package/src/domains/workflows/runtime/discovery.ts +137 -0
- package/src/domains/workflows/runtime/dynamax-shortcuts.ts +122 -0
- package/src/domains/workflows/runtime/dynamax.ts +330 -0
- package/src/domains/workflows/runtime/engine.ts +543 -0
- package/src/domains/workflows/runtime/filesystem-error.ts +4 -0
- package/src/domains/workflows/runtime/finalizers.ts +66 -0
- package/src/domains/workflows/runtime/identity-canonicalization.ts +138 -0
- package/src/domains/workflows/runtime/identity-fingerprint.ts +15 -0
- package/src/domains/workflows/runtime/inline-workflow.ts +403 -0
- package/src/domains/workflows/runtime/journal.ts +313 -0
- package/src/domains/workflows/runtime/model-profiles.ts +310 -0
- package/src/domains/workflows/runtime/options.ts +157 -0
- package/src/domains/workflows/runtime/perf.ts +146 -0
- package/src/domains/workflows/runtime/pi-compat.ts +32 -0
- package/src/domains/workflows/runtime/process-runner.ts +260 -0
- package/src/domains/workflows/runtime/progress-types.ts +59 -0
- package/src/domains/workflows/runtime/progress.ts +400 -0
- package/src/domains/workflows/runtime/provider-usage-limit.ts +191 -0
- package/src/domains/workflows/runtime/replay-path-identity.ts +48 -0
- package/src/domains/workflows/runtime/research-contract.ts +89 -0
- package/src/domains/workflows/runtime/research-evidence.ts +289 -0
- package/src/domains/workflows/runtime/resume-context.ts +751 -0
- package/src/domains/workflows/runtime/review/code-review-orchestration.ts +24 -0
- package/src/domains/workflows/runtime/review/github-pr-comments.ts +249 -0
- package/src/domains/workflows/runtime/review/patch-validation.ts +120 -0
- package/src/domains/workflows/runtime/review/review-actions.ts +180 -0
- package/src/domains/workflows/runtime/review/review-budget.ts +45 -0
- package/src/domains/workflows/runtime/review/review-fix-workflow.ts +153 -0
- package/src/domains/workflows/runtime/review/review-format.ts +133 -0
- package/src/domains/workflows/runtime/review/review-handoff.ts +38 -0
- package/src/domains/workflows/runtime/review/review-issues.ts +87 -0
- package/src/domains/workflows/runtime/review/review-report.ts +42 -0
- package/src/domains/workflows/runtime/review/review-results-flow.ts +52 -0
- package/src/domains/workflows/runtime/review/review-results-viewer.ts +312 -0
- package/src/domains/workflows/runtime/review/review-session-coordinator.ts +153 -0
- package/src/domains/workflows/runtime/review/review-snapshot.ts +294 -0
- package/src/domains/workflows/runtime/review-diff-target.ts +130 -0
- package/src/domains/workflows/runtime/session-identity.ts +6 -0
- package/src/domains/workflows/runtime/structured-output.ts +32 -0
- package/src/domains/workflows/runtime/tool-capabilities.ts +44 -0
- package/src/domains/workflows/runtime/tool-source-identity.ts +150 -0
- package/src/domains/workflows/runtime/tree-fingerprint.ts +404 -0
- package/src/domains/workflows/runtime/types.ts +255 -0
- package/src/domains/workflows/runtime/ui/display-text.ts +13 -0
- package/src/domains/workflows/runtime/ui/dynamax-editor-decoration.ts +220 -0
- package/src/domains/workflows/runtime/ui/workflow-format.ts +165 -0
- package/src/domains/workflows/runtime/ui/workflow-inspector.ts +398 -0
- package/src/domains/workflows/runtime/ui/workflow-result-renderer.ts +171 -0
- package/src/domains/workflows/runtime/ui/workflow-viewer-layout.ts +51 -0
- package/src/domains/workflows/runtime/ui/workflow-widget.ts +78 -0
- package/src/domains/workflows/runtime/unknown-error.ts +8 -0
- package/src/domains/workflows/runtime/usage.ts +341 -0
- package/src/domains/workflows/runtime/workflow-advisory-utils.ts +245 -0
- package/src/domains/workflows/runtime/workflow-execution.ts +121 -0
- package/src/domains/workflows/runtime/workflow-module.ts +75 -0
- package/src/domains/workflows/runtime/workflow-run-background.ts +72 -0
- package/src/domains/workflows/runtime/workflow-run-controller.ts +342 -0
- package/src/domains/workflows/runtime/workflow-run-history.ts +162 -0
- package/src/domains/workflows/runtime/workflow-run-record.ts +635 -0
- package/src/domains/workflows/runtime/workflow-run-store.ts +176 -0
- package/src/domains/workflows/runtime/workflow-usage-limit-scheduler.ts +80 -0
- package/src/domains/workflows/runtime/workflows.ts +40 -0
- package/src/domains/workflows/runtime/worktree.ts +581 -0
- package/src/domains/workflows/workflows/code-review.ts +232 -0
- package/src/domains/workflows/workflows/diagnose.ts +154 -0
- package/src/domains/workflows/workflows/perf-review.ts +149 -0
- package/src/domains/workflows/workflows/refactor-scout.ts +143 -0
- package/src/domains/workflows/workflows/research.ts +169 -0
- package/src/services/usage-service.ts +6 -30
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type { ModelAuth, OAuthAuth, OAuthCredential, Provider } from "@earendil-works/pi-ai";
|
|
3
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
import type { AccountContext, AccountProvider, ManagedAccount, RoutingMode } from "../../../core/accounts/registry.ts";
|
|
5
|
+
import { quotaStateFromHeaders, selectRoutingCandidate, type AccountQuotaState } from "../../../core/accounts/routing.ts";
|
|
6
|
+
import {
|
|
7
|
+
loadOAuthPool,
|
|
8
|
+
oauthIdentity,
|
|
9
|
+
saveOAuthAccount,
|
|
10
|
+
saveOAuthPool,
|
|
11
|
+
setOAuthPoolMode,
|
|
12
|
+
type PooledOAuthAccount,
|
|
13
|
+
} from "../../../core/accounts/oauth-pool.ts";
|
|
14
|
+
|
|
15
|
+
export interface PooledOAuthProviderSpec {
|
|
16
|
+
id: string;
|
|
17
|
+
label: string;
|
|
18
|
+
createProvider(): Provider;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const lastUsed = new Map<string, number>();
|
|
22
|
+
const primaryQuota = new Map<string, AccountQuotaState>();
|
|
23
|
+
const refreshes = new Map<string, Promise<PooledOAuthAccount>>();
|
|
24
|
+
|
|
25
|
+
function oauthFor(spec: PooledOAuthProviderSpec): OAuthAuth {
|
|
26
|
+
const oauth = spec.createProvider().auth.oauth;
|
|
27
|
+
if (!oauth) throw new Error(`${spec.label} does not expose subscription OAuth.`);
|
|
28
|
+
return oauth;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function notifyAuthEvent(ctx: AccountContext, event: any): Promise<void> {
|
|
32
|
+
if (event?.type === "device_code") {
|
|
33
|
+
const url = String(event.verificationUri ?? "");
|
|
34
|
+
if (url) await ctx.openBrowser(url).catch(() => undefined);
|
|
35
|
+
ctx.ui.notify(`Enter code ${String(event.userCode ?? "")} at ${url}`, "info");
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (event?.type === "auth_url") {
|
|
39
|
+
const url = String(event.url ?? "");
|
|
40
|
+
if (url) await ctx.openBrowser(url).catch(() => undefined);
|
|
41
|
+
ctx.ui.notify(event.instructions ? `${event.instructions}\n${url}` : url, "info");
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (typeof event?.message === "string") ctx.ui.notify(event.message, "info");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function authenticate(spec: PooledOAuthProviderSpec, ctx: AccountContext): Promise<OAuthCredential> {
|
|
48
|
+
const signal = ctx.signal ?? new AbortController().signal;
|
|
49
|
+
return oauthFor(spec).login({
|
|
50
|
+
signal,
|
|
51
|
+
notify: (event) => { void notifyAuthEvent(ctx, event); },
|
|
52
|
+
prompt: async (prompt) => {
|
|
53
|
+
if (prompt.type === "select") {
|
|
54
|
+
const labels = prompt.options.map((option) => option.label);
|
|
55
|
+
const selected = await ctx.ui.select(prompt.message, labels);
|
|
56
|
+
const index = selected ? labels.indexOf(selected) : -1;
|
|
57
|
+
if (index < 0) throw new Error("Login cancelled.");
|
|
58
|
+
return prompt.options[index].id;
|
|
59
|
+
}
|
|
60
|
+
const value = await ctx.ui.input(prompt.message, prompt.placeholder);
|
|
61
|
+
if (!value) throw new Error("Login cancelled.");
|
|
62
|
+
return value;
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function accountLabel(account: PooledOAuthAccount): string {
|
|
68
|
+
return account.label || account.identity || account.id.slice(0, 8);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function duplicateAccount(spec: PooledOAuthProviderSpec, credential: OAuthCredential, excludeId?: string): PooledOAuthAccount | undefined {
|
|
72
|
+
const identity = oauthIdentity(credential.access);
|
|
73
|
+
return loadOAuthPool(spec.id).accounts.find((account) =>
|
|
74
|
+
account.id !== excludeId && (identity ? account.identity === identity : account.access === credential.access));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function createPooledOAuthAdapter(spec: PooledOAuthProviderSpec): AccountProvider {
|
|
78
|
+
return {
|
|
79
|
+
id: spec.id,
|
|
80
|
+
label: spec.label,
|
|
81
|
+
|
|
82
|
+
async list(): Promise<ManagedAccount[]> {
|
|
83
|
+
return loadOAuthPool(spec.id).accounts.map((account) => ({
|
|
84
|
+
id: account.id,
|
|
85
|
+
label: accountLabel(account),
|
|
86
|
+
enabled: account.enabled !== false,
|
|
87
|
+
expiresAt: account.expires,
|
|
88
|
+
}));
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
async add(ctx, label): Promise<string | undefined> {
|
|
92
|
+
if (!await ctx.ui.confirm(`Add ${spec.label} account`, `Sign in with another ${spec.label} subscription?`)) {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
const credential = await authenticate(spec, ctx);
|
|
96
|
+
const duplicate = duplicateAccount(spec, credential);
|
|
97
|
+
if (duplicate) throw new Error(`That account is already saved as “${accountLabel(duplicate)}”.`);
|
|
98
|
+
|
|
99
|
+
saveOAuthAccount(spec.id, {
|
|
100
|
+
...credential,
|
|
101
|
+
id: randomUUID(),
|
|
102
|
+
label,
|
|
103
|
+
enabled: true,
|
|
104
|
+
identity: oauthIdentity(credential.access),
|
|
105
|
+
addedAt: Date.now(),
|
|
106
|
+
});
|
|
107
|
+
return label;
|
|
108
|
+
},
|
|
109
|
+
|
|
110
|
+
async reauth(ctx, accountId): Promise<string | undefined> {
|
|
111
|
+
const account = loadOAuthPool(spec.id).accounts.find(
|
|
112
|
+
(candidate) => candidate.id === accountId || candidate.label === accountId,
|
|
113
|
+
);
|
|
114
|
+
if (!account) throw new Error(`${spec.label} account “${accountId}” not found.`);
|
|
115
|
+
|
|
116
|
+
const credential = await authenticate(spec, ctx);
|
|
117
|
+
const duplicate = duplicateAccount(spec, credential, account.id);
|
|
118
|
+
if (duplicate) throw new Error(`That account is already saved as “${accountLabel(duplicate)}”.`);
|
|
119
|
+
saveOAuthAccount(spec.id, {
|
|
120
|
+
...account,
|
|
121
|
+
...credential,
|
|
122
|
+
identity: oauthIdentity(credential.access),
|
|
123
|
+
});
|
|
124
|
+
return account.label;
|
|
125
|
+
},
|
|
126
|
+
|
|
127
|
+
async setEnabled(accountId, enabled): Promise<void> {
|
|
128
|
+
const pool = loadOAuthPool(spec.id);
|
|
129
|
+
const account = pool.accounts.find((candidate) => candidate.id === accountId);
|
|
130
|
+
if (!account) throw new Error(`${spec.label} account “${accountId}” not found.`);
|
|
131
|
+
account.enabled = enabled;
|
|
132
|
+
saveOAuthPool(spec.id, pool);
|
|
133
|
+
},
|
|
134
|
+
|
|
135
|
+
async rename(accountId, label): Promise<void> {
|
|
136
|
+
const pool = loadOAuthPool(spec.id);
|
|
137
|
+
const account = pool.accounts.find((candidate) => candidate.id === accountId);
|
|
138
|
+
if (!account) throw new Error(`${spec.label} account “${accountId}” not found.`);
|
|
139
|
+
account.label = label;
|
|
140
|
+
saveOAuthPool(spec.id, pool);
|
|
141
|
+
},
|
|
142
|
+
|
|
143
|
+
routing: {
|
|
144
|
+
async get(): Promise<RoutingMode> {
|
|
145
|
+
return loadOAuthPool(spec.id).mode;
|
|
146
|
+
},
|
|
147
|
+
async set(mode): Promise<RoutingMode> {
|
|
148
|
+
setOAuthPoolMode(spec.id, mode);
|
|
149
|
+
return mode;
|
|
150
|
+
},
|
|
151
|
+
describe(mode): string {
|
|
152
|
+
return mode === "quota-aware"
|
|
153
|
+
? `Uses ${spec.label} quota headers when available, otherwise rotates fairly.`
|
|
154
|
+
: `Uses ${spec.label} accounts in order, moving on when one is rate-limited.`;
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function chooseCredential(
|
|
161
|
+
spec: PooledOAuthProviderSpec,
|
|
162
|
+
primary: OAuthCredential,
|
|
163
|
+
): { id: string; credential: OAuthCredential; account?: PooledOAuthAccount } {
|
|
164
|
+
const pool = loadOAuthPool(spec.id);
|
|
165
|
+
const candidates = [
|
|
166
|
+
{
|
|
167
|
+
id: "main",
|
|
168
|
+
order: 0,
|
|
169
|
+
lastUsed: lastUsed.get(`${spec.id}:main`) ?? 0,
|
|
170
|
+
quota: primaryQuota.get(spec.id),
|
|
171
|
+
value: { id: "main", credential: primary },
|
|
172
|
+
},
|
|
173
|
+
...pool.accounts
|
|
174
|
+
.filter((account) => account.enabled !== false && account.access)
|
|
175
|
+
.map((account, index) => ({
|
|
176
|
+
id: account.id,
|
|
177
|
+
order: index + 1,
|
|
178
|
+
lastUsed: lastUsed.get(`${spec.id}:${account.id}`) ?? account.lastUsed ?? 0,
|
|
179
|
+
quota: account.quota,
|
|
180
|
+
value: { id: account.id, credential: account, account },
|
|
181
|
+
})),
|
|
182
|
+
];
|
|
183
|
+
return selectRoutingCandidate(candidates, pool.mode)?.value ?? { id: "main", credential: primary };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async function freshCredential(spec: PooledOAuthProviderSpec, account: PooledOAuthAccount, signal: AbortSignal): Promise<PooledOAuthAccount> {
|
|
187
|
+
if (account.expires > Date.now() + 60_000) return account;
|
|
188
|
+
|
|
189
|
+
const key = `${spec.id}:${account.id}`;
|
|
190
|
+
const active = refreshes.get(key);
|
|
191
|
+
if (active) return active;
|
|
192
|
+
|
|
193
|
+
const refresh = oauthFor(spec).refresh(account, signal).then((credential) => {
|
|
194
|
+
const updated = { ...account, ...credential };
|
|
195
|
+
saveOAuthAccount(spec.id, updated);
|
|
196
|
+
return updated;
|
|
197
|
+
}).finally(() => refreshes.delete(key));
|
|
198
|
+
refreshes.set(key, refresh);
|
|
199
|
+
return refresh;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async function routedAuth(
|
|
203
|
+
spec: PooledOAuthProviderSpec,
|
|
204
|
+
oauth: OAuthAuth,
|
|
205
|
+
primary: OAuthCredential,
|
|
206
|
+
): Promise<ModelAuth> {
|
|
207
|
+
const selected = chooseCredential(spec, primary);
|
|
208
|
+
const signal = new AbortController().signal;
|
|
209
|
+
const credential = selected.account
|
|
210
|
+
? await freshCredential(spec, selected.account, signal)
|
|
211
|
+
: primary;
|
|
212
|
+
const now = Date.now();
|
|
213
|
+
lastUsed.set(`${spec.id}:${selected.id}`, now);
|
|
214
|
+
|
|
215
|
+
if (selected.account && now - (selected.account.lastUsed ?? 0) >= 60_000) {
|
|
216
|
+
saveOAuthAccount(spec.id, { ...selected.account, ...credential, lastUsed: now });
|
|
217
|
+
}
|
|
218
|
+
return oauth.toAuth(credential);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export function registerPooledOAuthProvider(pi: ExtensionAPI, spec: PooledOAuthProviderSpec): void {
|
|
222
|
+
const provider = spec.createProvider();
|
|
223
|
+
const oauth = provider.auth.oauth;
|
|
224
|
+
if (!oauth) return;
|
|
225
|
+
|
|
226
|
+
const withQuotaObserver = (options: any) => ({
|
|
227
|
+
...options,
|
|
228
|
+
onResponse: async (response: { status: number; headers: Record<string, string> }, model: unknown) => {
|
|
229
|
+
recordQuotaResponse(spec, requestAccessToken(options), response.status, response.headers);
|
|
230
|
+
await options?.onResponse?.(response, model);
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
pi.registerProvider({
|
|
235
|
+
...provider,
|
|
236
|
+
auth: {
|
|
237
|
+
...provider.auth,
|
|
238
|
+
oauth: {
|
|
239
|
+
...oauth,
|
|
240
|
+
toAuth: (primary) => routedAuth(spec, oauth, primary),
|
|
241
|
+
},
|
|
242
|
+
},
|
|
243
|
+
stream: (model: any, context: any, options: any) => provider.stream(model, context, withQuotaObserver(options)),
|
|
244
|
+
streamSimple: (model: any, context: any, options: any) => provider.streamSimple(model, context, withQuotaObserver(options)),
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function requestAccessToken(options: any): string | undefined {
|
|
249
|
+
if (typeof options?.apiKey === "string") return options.apiKey;
|
|
250
|
+
const headers = options?.headers;
|
|
251
|
+
if (!headers || typeof headers !== "object") return undefined;
|
|
252
|
+
const authorization = Object.entries(headers).find(([key]) => key.toLowerCase() === "authorization")?.[1];
|
|
253
|
+
if (typeof authorization !== "string") return undefined;
|
|
254
|
+
const match = /^Bearer\s+(.+)$/i.exec(authorization);
|
|
255
|
+
return match?.[1];
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function recordQuotaResponse(
|
|
259
|
+
spec: PooledOAuthProviderSpec,
|
|
260
|
+
access: string | undefined,
|
|
261
|
+
status: number,
|
|
262
|
+
headers: Record<string, string>,
|
|
263
|
+
): void {
|
|
264
|
+
if (!access) return;
|
|
265
|
+
const pool = loadOAuthPool(spec.id);
|
|
266
|
+
const account = pool.accounts.find((candidate) => candidate.access === access);
|
|
267
|
+
const previous = account?.quota ?? primaryQuota.get(spec.id);
|
|
268
|
+
const quota = quotaStateFromHeaders(status, headers, previous);
|
|
269
|
+
if (!quota || quota === previous) return;
|
|
270
|
+
|
|
271
|
+
if (account) saveOAuthAccount(spec.id, { ...account, quota });
|
|
272
|
+
else primaryQuota.set(spec.id, quota);
|
|
273
|
+
}
|
|
@@ -2,16 +2,16 @@ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
|
2
2
|
import { accountProvider, routableProviders, type RoutingMode } from "../../core/accounts/registry.ts";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* `/routing [
|
|
5
|
+
* `/routing [sequential|quota-aware] [provider]`
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
7
|
+
* sequential account 1, then account 2 when the first is exhausted
|
|
8
|
+
* quota-aware use the account with the most remaining capacity
|
|
9
9
|
*
|
|
10
10
|
* With no provider named, the mode is applied to every provider that supports
|
|
11
11
|
* routing. With no mode, the current modes are reported.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
const MODES: RoutingMode[] = ["
|
|
14
|
+
const MODES: RoutingMode[] = ["sequential", "quota-aware"];
|
|
15
15
|
|
|
16
16
|
function isMode(value: string): value is RoutingMode {
|
|
17
17
|
return (MODES as string[]).includes(value);
|
|
@@ -19,12 +19,20 @@ function isMode(value: string): value is RoutingMode {
|
|
|
19
19
|
|
|
20
20
|
export function registerRoutingCommands(pi: ExtensionAPI): void {
|
|
21
21
|
pi.registerCommand("routing", {
|
|
22
|
-
description: "Account routing:
|
|
23
|
-
getArgumentCompletions: (prefix) =>
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
description: "Account routing: sequential or quota-aware",
|
|
23
|
+
getArgumentCompletions: (prefix) => {
|
|
24
|
+
const [mode, providerPrefix = ""] = prefix.trim().split(/\s+/, 2);
|
|
25
|
+
if (!/\s/.test(prefix)) {
|
|
26
|
+
return MODES.filter((candidate) => candidate.startsWith(mode ?? "")).map((candidate) => ({
|
|
27
|
+
value: candidate,
|
|
28
|
+
label: candidate === "quota-aware" ? "quota-aware: use remaining capacity" : "sequential: account 1, then 2",
|
|
29
|
+
}));
|
|
30
|
+
}
|
|
31
|
+
if (!mode || !isMode(mode)) return [];
|
|
32
|
+
return routableProviders()
|
|
33
|
+
.filter((provider) => provider.id.startsWith(providerPrefix))
|
|
34
|
+
.map((provider) => ({ value: `${mode} ${provider.id}`, label: provider.id }));
|
|
35
|
+
},
|
|
28
36
|
handler: async (args, ctx) => {
|
|
29
37
|
const [first, second] = args.trim().toLowerCase().split(/\s+/).filter(Boolean);
|
|
30
38
|
const providers = second ? [accountProvider(second)].filter((entry) => !!entry) : routableProviders();
|
|
@@ -53,7 +61,7 @@ export function registerRoutingCommands(pi: ExtensionAPI): void {
|
|
|
53
61
|
}
|
|
54
62
|
|
|
55
63
|
if (!isMode(first)) {
|
|
56
|
-
ctx.ui.notify(`Usage: /routing [
|
|
64
|
+
ctx.ui.notify(`Usage: /routing [sequential|quota-aware] [provider]`, "warning");
|
|
57
65
|
return;
|
|
58
66
|
}
|
|
59
67
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 timbrinded
|
|
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.
|