paseo-plugin-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.
@@ -0,0 +1,63 @@
1
+ import type { UsageWindow } from "../../shared/contracts";
2
+ import type { CollectedUsage } from "./index";
3
+
4
+ export function object(value: unknown): Record<string, unknown> | null {
5
+ return value !== null && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : null;
6
+ }
7
+ export function string(value: unknown): string | null { return typeof value === "string" && value.length > 0 ? value : null; }
8
+ function numeric(value: unknown): number | null { return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : null; }
9
+ function date(value: unknown, seconds = false): string | null {
10
+ if (value === null || value === undefined || (seconds ? typeof value !== "number" : typeof value !== "string")) return null;
11
+ const time = seconds ? (value as number) * 1000 : Date.parse(value as string);
12
+ return Number.isFinite(time) && Math.abs(time) <= 8.64e15 ? new Date(time).toISOString() : null;
13
+ }
14
+ function durationLabel(minutes: number | null, fallback: string): string {
15
+ if (minutes === null) return fallback;
16
+ if (minutes % 1440 === 0) return `${minutes / 1440} days`;
17
+ if (minutes % 60 === 0) return `${minutes / 60} hours`;
18
+ return `${minutes} min`;
19
+ }
20
+ export function parseCodexUsage(payload: unknown): CollectedUsage {
21
+ const root = object(payload) ?? {};
22
+ const map = object(root.rateLimitsByLimitId);
23
+ const fallback = object(root.rateLimits);
24
+ const buckets = map && Object.keys(map).length ? Object.entries(map) : fallback ? [[string(fallback.limitId) ?? "codex", fallback] as const] : [];
25
+ const windows: UsageWindow[] = [];
26
+ let plan: string | null = null;
27
+ for (const [id, candidate] of buckets) {
28
+ const bucket = object(candidate);
29
+ if (!bucket) continue;
30
+ plan ??= string(bucket.planType);
31
+ for (const key of ["primary", "secondary"]) {
32
+ const window = object(bucket[key]);
33
+ if (!window) continue;
34
+ const duration = numeric(window.windowDurationMins);
35
+ windows.push({ id: `${id}:${key}`, label: `${string(bucket.limitName) ?? id} · ${durationLabel(duration, key)}`, usedPercent: numeric(window.usedPercent), windowDurationMins: duration, resetsAt: date(window.resetsAt, true) });
36
+ }
37
+ }
38
+ return { identityKey: string(root.accountId), plan, windows };
39
+ }
40
+
41
+ export function parseClaudeUsage(payload: unknown): CollectedUsage {
42
+ const root = object(payload) ?? {};
43
+ const windows: UsageWindow[] = [];
44
+ for (const [key, value] of Object.entries(root)) {
45
+ if (key !== "five_hour" && !/^seven_day(?:_[a-z0-9_]+)?$/.test(key)) continue;
46
+ const row = object(value);
47
+ if (value !== null && !row) continue;
48
+ const weekly = key !== "five_hour";
49
+ const suffix = key.slice("seven_day_".length).replaceAll("_", " ");
50
+ windows.push({ id: key, label: !weekly ? "5 hours" : key === "seven_day" ? "Weekly" : `Weekly · ${suffix}`, usedPercent: numeric(row?.utilization), windowDurationMins: weekly ? 10080 : 300, resetsAt: date(row?.resets_at) });
51
+ }
52
+ if (Array.isArray(root.limits)) for (const [index, value] of root.limits.entries()) {
53
+ const row = object(value);
54
+ if (!row || row.kind !== "weekly_scoped") continue;
55
+ const scope = object(row.scope);
56
+ const model = object(scope?.model);
57
+ const surface = object(scope?.surface);
58
+ const labels = [string(model?.display_name) ?? string(scope?.model), string(surface?.display_name) ?? string(scope?.surface)].filter((s): s is string => !!s);
59
+ if (!labels.length) continue;
60
+ windows.push({ id: `weekly_scoped:${index}`, label: `Weekly · ${labels.join(" · ")}`, usedPercent: numeric(row.percent), windowDurationMins: 10080, resetsAt: date(row.resets_at) });
61
+ }
62
+ return { identityKey: null, plan: null, windows };
63
+ }
@@ -0,0 +1,63 @@
1
+ import { defineRpc } from "@getpaseo/plugin";
2
+ import { z } from "zod";
3
+
4
+ export const ProviderSchema = z.enum(["claude", "codex"]);
5
+ export type Provider = z.infer<typeof ProviderSchema>;
6
+ export const LoginModeSchema = z.enum(["device", "browser"]);
7
+ export const LoginSessionSchema = z.object({
8
+ id: z.string(), accountId: z.string(), provider: ProviderSchema, mode: LoginModeSchema,
9
+ status: z.enum(["starting", "waiting", "verifying", "complete", "error"]),
10
+ authorizationUrl: z.string().nullable(), userCode: z.string().nullable(),
11
+ canSubmitCode: z.boolean(), error: z.string().nullable(), expiresAt: z.string().nullable(),
12
+ });
13
+ export type LoginSession = z.infer<typeof LoginSessionSchema>;
14
+ export const AccountSchema = z.object({
15
+ id: z.string(), provider: ProviderSchema, label: z.string(),
16
+ source: z.enum(["managed", "system"]), email: z.string().nullable(),
17
+ plan: z.string().nullable(), authStatus: z.enum(["ready", "needs_auth", "unknown", "error"]),
18
+ createdAt: z.string(),
19
+ login: z.union([z.object({sessionId:z.string(),mode:LoginModeSchema}),z.object({workspaceId:z.string(),terminalId:z.string()})]).nullable().optional(),
20
+ });
21
+ export type Account = z.infer<typeof AccountSchema>;
22
+ export const BindingSchema = z.object({
23
+ agentId: z.string(), provider: ProviderSchema,
24
+ currentAccountId: z.string().nullable(), pendingAccountId: z.string().nullable(),
25
+ status: z.enum(["ready", "switching", "error"]), error: z.string().nullable(),
26
+ });
27
+ export type Binding = z.infer<typeof BindingSchema>;
28
+ export const StateSchema = z.object({
29
+ accounts: z.array(AccountSchema), bindings: z.array(BindingSchema),
30
+ defaults: z.object({ claude: z.string().nullable(), codex: z.string().nullable() }),
31
+ integration: z.object({ enabled: z.boolean(), error: z.string().nullable() }),
32
+ });
33
+ export type AccountState = z.infer<typeof StateSchema>;
34
+ export const UsageWindowSchema = z.object({
35
+ id: z.string(), label: z.string(), usedPercent: z.number().nullable(),
36
+ windowDurationMins: z.number().nullable(), resetsAt: z.string().nullable(),
37
+ });
38
+ export type UsageWindow = z.infer<typeof UsageWindowSchema>;
39
+ export const UsageSnapshotSchema = z.object({
40
+ accountId: z.string(), provider: ProviderSchema,
41
+ status: z.enum(["ok", "stale", "needs_auth", "unavailable"]),
42
+ plan: z.string().nullable(), windows: z.array(UsageWindowSchema),
43
+ fetchedAt: z.string().nullable(), checkedAt: z.string(), nextRetryAt: z.string().nullable(),
44
+ error: z.string().nullable(),
45
+ });
46
+ export type UsageSnapshot = z.infer<typeof UsageSnapshotSchema>;
47
+ const Empty = z.object({});
48
+ const AccountId = z.object({ accountId: z.string().min(1) });
49
+ export const listAccounts = defineRpc({ name: "accounts.list", input: Empty, output: StateSchema });
50
+ export const addAccount = defineRpc({ name: "accounts.add", input: z.object({ provider: ProviderSchema, label: z.string().trim().min(1).max(80) }), output: AccountSchema });
51
+ export const renameAccount = defineRpc({ name: "accounts.rename", input: AccountId.extend({ label: z.string().trim().min(1).max(80) }), output: AccountSchema });
52
+ export const removeAccount = defineRpc({ name: "accounts.remove", input: AccountId, output: Empty });
53
+ export const setDefaultAccount = defineRpc({ name: "accounts.default", input: z.object({ provider: ProviderSchema, accountId: z.string().nullable() }), output: Empty });
54
+ export const startLogin = defineRpc({ name: "auth.start", input: AccountId.extend({ mode: LoginModeSchema.optional() }), output: LoginSessionSchema });
55
+ export const getLoginStatus = defineRpc({ name: "auth.status", input: AccountId.extend({ sessionId: z.string().min(1) }), output: LoginSessionSchema });
56
+ export const submitLoginCode = defineRpc({ name: "auth.submit-code", input: AccountId.extend({ sessionId: z.string().min(1), code: z.string().trim().min(1).max(2048) }), output: LoginSessionSchema });
57
+ export const checkLogin = defineRpc({ name: "auth.check", input: AccountId, output: AccountSchema });
58
+ export const cancelLogin = defineRpc({ name: "auth.cancel", input: AccountId.extend({ sessionId: z.string().min(1).optional() }), output: AccountSchema });
59
+ export const prepareSwitch = defineRpc({ name: "switch.prepare", input: z.object({ agentId: z.string().min(1), accountId: z.string().nullable() }), output: BindingSchema });
60
+ export const applySwitch = defineRpc({ name: "switch.apply", input: z.object({ agentId: z.string().min(1) }), output: BindingSchema });
61
+ export const setIntegration = defineRpc({ name: "integration.set", input: z.object({ enabled: z.boolean() }), output: StateSchema });
62
+ export const listUsage = defineRpc({ name: "usage.list", input: z.object({ visible: z.boolean().optional() }), output: z.object({ accounts: z.array(UsageSnapshotSchema) }) });
63
+ export const refreshUsage = defineRpc({ name: "usage.refresh", input: z.object({ accountId: z.string().optional() }), output: z.object({ accounts: z.array(UsageSnapshotSchema) }) });