@juanbenjumea/pi-agy 0.6.11 → 0.7.1

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,251 @@
1
+ /**
2
+ * Antigravity provider model catalog.
3
+ *
4
+ * Projects `agy models` output into Pi provider model entries so agy-backed
5
+ * models appear in the global `/model` picker as `antigravity/<id>`.
6
+ *
7
+ * `agy models` prints one model per line: `<slug> <display label>`. Only the
8
+ * first column (the slug) is a valid `--model` value; the label is
9
+ * display-only. Gemini bases with multiple effort tiers collapse to a single
10
+ * base entry with a thinking-level toggle (the picked level is sent as agy
11
+ * `--effort`); fixed-thinking families (Claude, GPT-OSS, single-tier Gemini)
12
+ * keep agy's exact qualified slug and never receive `--effort`.
13
+ *
14
+ * Pure functions only — no spawning, no Pi imports — so the catalog is
15
+ * unit-testable without the CLI.
16
+ */
17
+
18
+ /** Reasoning-effort tiers agy accepts via `--effort`. */
19
+ export type AgyProviderEffort = "low" | "medium" | "high";
20
+
21
+ /** Pi thinking levels that map onto agy effort tiers. */
22
+ export type AgyThinkingLevel =
23
+ | "off"
24
+ | "minimal"
25
+ | "low"
26
+ | "medium"
27
+ | "high"
28
+ | "xhigh"
29
+ | "max";
30
+
31
+ const EFFORT_RANK: Record<AgyProviderEffort, number> = { low: 0, medium: 1, high: 2 };
32
+
33
+ const EFFORT_ORDER: readonly AgyProviderEffort[] = ["low", "medium", "high"];
34
+
35
+ /** Split an agy slug into (base, tier); tier is null without a tier suffix. */
36
+ const TIER_RE = /^(.+)-(high|medium|low)$/;
37
+
38
+ /**
39
+ * Validate the first column of an `agy models` line so banners, auth
40
+ * prompts, or progress chatter ("Fetching models…") can never register as a
41
+ * model, and a leading-dash token cannot reach agy's flag parser as `--model`.
42
+ */
43
+ const MODEL_LINE_RE = /^[A-Za-z0-9][A-Za-z0-9._]*-[A-Za-z0-9._-]*$/;
44
+
45
+ /**
46
+ * Families verified to accept base-slug + `--effort`. Only these collapse to
47
+ * a base entry with a thinking toggle; every other family keeps agy's exact
48
+ * qualified slug so unknown or fixed-thinking families degrade safely instead
49
+ * of forcing an unsupported `--effort`.
50
+ */
51
+ const EFFORT_CAPABLE_FAMILIES: readonly RegExp[] = [/^gemini-/];
52
+
53
+ export interface AgyProviderModelEntry {
54
+ /**
55
+ * Exact agy `--model` value: a base slug (e.g. `gemini-3.8-flash`) when
56
+ * effort-driven, otherwise agy's full qualified slug
57
+ * (e.g. `claude-sonnet-4-6`).
58
+ */
59
+ full: string;
60
+ /** Pi model id, e.g. `gemini-3-8-flash`. */
61
+ id: string;
62
+ /**
63
+ * Present iff effort-driven: the tiers agy accepts for this base. Pi's
64
+ * thinking toggle picks among them and `--effort` is always passed. Absent
65
+ * means fixed thinking — never pass `--effort`.
66
+ */
67
+ efforts?: AgyProviderEffort[];
68
+ }
69
+
70
+ /**
71
+ * Map a Pi thinking level onto one of the `efforts` tiers a base supports,
72
+ * clamping to the nearest available (agy rejects tiers a base does not list,
73
+ * e.g. medium on Pro). With no level, fall back to the first available tier.
74
+ */
75
+ export function toProviderEffort(
76
+ reasoning: AgyThinkingLevel | undefined,
77
+ efforts: readonly AgyProviderEffort[],
78
+ ): AgyProviderEffort {
79
+ let candidate: AgyProviderEffort;
80
+ switch (reasoning) {
81
+ case "minimal":
82
+ case "low":
83
+ candidate = "low";
84
+ break;
85
+ case "medium":
86
+ candidate = "medium";
87
+ break;
88
+ case "high":
89
+ case "xhigh":
90
+ case "max":
91
+ candidate = "high";
92
+ break;
93
+ default:
94
+ candidate = efforts[0] ?? "low";
95
+ }
96
+ if (efforts.includes(candidate)) return candidate;
97
+ const index = EFFORT_ORDER.indexOf(candidate);
98
+ for (let j = index; j < EFFORT_ORDER.length; j++) {
99
+ if (efforts.includes(EFFORT_ORDER[j])) return EFFORT_ORDER[j];
100
+ }
101
+ for (let j = index - 1; j >= 0; j--) {
102
+ if (efforts.includes(EFFORT_ORDER[j])) return EFFORT_ORDER[j];
103
+ }
104
+ return efforts[0] ?? "low";
105
+ }
106
+
107
+ /** `Gemini 3.8 Flash (Medium)` -> `gemini-3-8-flash-medium`. */
108
+ export function slugifyProviderId(full: string): string {
109
+ return full
110
+ .toLowerCase()
111
+ .replace(/[^a-z0-9]+/g, "-")
112
+ .replace(/^-+|-+$/g, "");
113
+ }
114
+
115
+ /**
116
+ * Build the thinking-level map for an effort-driven base: `off` and `minimal`
117
+ * are always hidden (agy has no no-thinking mode; a base requires an effort),
118
+ * as is any of low/medium/high the base does not offer. Pi treats null as
119
+ * hidden, so the toggle shows exactly agy's stops.
120
+ */
121
+ export function thinkingMapForEfforts(
122
+ efforts: readonly AgyProviderEffort[],
123
+ ): Partial<Record<AgyThinkingLevel, string | null>> {
124
+ const map: Partial<Record<AgyThinkingLevel, string | null>> = {
125
+ off: null,
126
+ minimal: null,
127
+ };
128
+ for (const level of ["low", "medium", "high"] as const) {
129
+ if (!efforts.includes(level)) map[level] = null;
130
+ }
131
+ return map;
132
+ }
133
+
134
+ /**
135
+ * Parse raw `agy models` text into provider entries. Effort-capable Gemini
136
+ * bases with >= 2 tier variants collapse to one base entry carrying the tiers
137
+ * they accept; everything else keeps agy's exact qualified slug. Insertion
138
+ * order of first-seen bases is preserved.
139
+ */
140
+ export function parseProviderCatalog(raw: string): AgyProviderModelEntry[] {
141
+ const groups = new Map<string, { lines: string[]; tiers: Set<AgyProviderEffort> }>();
142
+ for (const line of raw.split("\n")) {
143
+ const slug = line.trim().split(/\s+/)[0] ?? "";
144
+ if (!slug || !MODEL_LINE_RE.test(slug)) continue;
145
+ const match = TIER_RE.exec(slug);
146
+ const base = match ? match[1] : slug;
147
+ const tier = match ? (match[2] as AgyProviderEffort) : null;
148
+ let group = groups.get(base);
149
+ if (!group) {
150
+ group = { lines: [], tiers: new Set<AgyProviderEffort>() };
151
+ groups.set(base, group);
152
+ }
153
+ group.lines.push(slug);
154
+ if (tier) group.tiers.add(tier);
155
+ }
156
+ const entries: AgyProviderModelEntry[] = [];
157
+ const seenIds = new Set<string>();
158
+ const pushEntry = (entry: AgyProviderModelEntry): void => {
159
+ // Distinct slugs can slugify identically (e.g. `a.b` vs `a-b`); the
160
+ // picker cannot hold duplicate ids, so first-seen wins.
161
+ if (!entry.id || seenIds.has(entry.id)) return;
162
+ seenIds.add(entry.id);
163
+ entries.push(entry);
164
+ };
165
+ for (const [base, group] of groups) {
166
+ const efforts = [...group.tiers].sort((a, b) => EFFORT_RANK[a] - EFFORT_RANK[b]);
167
+ if (efforts.length >= 2 && EFFORT_CAPABLE_FAMILIES.some((re) => re.test(base))) {
168
+ pushEntry({ full: base, id: slugifyProviderId(base), efforts });
169
+ } else {
170
+ for (const line of group.lines) {
171
+ pushEntry({ full: line, id: slugifyProviderId(line) });
172
+ }
173
+ }
174
+ }
175
+ return entries;
176
+ }
177
+
178
+ /** Catalog equality for refresh: ids AND effort tiers (a base can gain/lose tiers). */
179
+ export function areProviderCatalogsEqual(
180
+ left: readonly AgyProviderModelEntry[],
181
+ right: readonly AgyProviderModelEntry[],
182
+ ): boolean {
183
+ if (left.length !== right.length) return false;
184
+ return left.every((entry, index) => {
185
+ const other = right[index];
186
+ if (!other || entry.id !== other.id || entry.full !== other.full) return false;
187
+ const leftEfforts = entry.efforts ?? [];
188
+ const rightEfforts = other.efforts ?? [];
189
+ return (
190
+ leftEfforts.length === rightEfforts.length &&
191
+ leftEfforts.every((effort, effortIndex) => effort === rightEfforts[effortIndex])
192
+ );
193
+ });
194
+ }
195
+
196
+ /** Display name for a picker entry: the exact agy `--model` value. */
197
+ export function providerEntryName(entry: AgyProviderModelEntry): string {
198
+ return entry.full;
199
+ }
200
+
201
+ /**
202
+ * Fallback catalog when `agy models` fails at load (binary missing, auth not
203
+ * yet done, network stall). Keeps the picker populated so selection yields a
204
+ * clear runtime error instead of an empty list. Update when agy ships new
205
+ * Gemini versions.
206
+ */
207
+ export const FALLBACK_PROVIDER_MODELS: AgyProviderModelEntry[] = [
208
+ { full: "gemini-3.8-flash", id: "gemini-3-8-flash", efforts: ["low", "medium", "high"] },
209
+ { full: "gemini-3.1-pro", id: "gemini-3-1-pro", efforts: ["low", "high"] },
210
+ { full: "claude-sonnet-4-6", id: "claude-sonnet-4-6" },
211
+ ];
212
+
213
+ /**
214
+ * Plain provider model definition (structurally compatible with Pi's
215
+ * `ProviderModelConfig`). Kept dependency-free so this module stays pure;
216
+ * the extension entry maps these into `registerProvider` models.
217
+ */
218
+ export interface AgyProviderModelDef {
219
+ id: string;
220
+ name: string;
221
+ api: string;
222
+ reasoning: boolean;
223
+ thinkingLevelMap?: Partial<Record<AgyThinkingLevel, string | null>>;
224
+ input: Array<"text" | "image">;
225
+ cost: { input: number; output: number; cacheRead: number; cacheWrite: number };
226
+ contextWindow: number;
227
+ maxTokens: number;
228
+ }
229
+
230
+ /** Project a catalog entry into a Pi provider model definition. */
231
+ export function toProviderModelDef(entry: AgyProviderModelEntry): AgyProviderModelDef {
232
+ const effortDriven = !!entry.efforts && entry.efforts.length > 0;
233
+ return {
234
+ id: entry.id,
235
+ name: entry.full,
236
+ // Custom API sentinel: no built-in provider claims it, so it can never
237
+ // collide. streamSimple intercepts every turn; baseUrl/apiKey below are
238
+ // present only because Pi requires non-empty values.
239
+ api: "agy-provider",
240
+ reasoning: effortDriven,
241
+ ...(effortDriven ? { thinkingLevelMap: thinkingMapForEfforts(entry.efforts!) } : {}),
242
+ // Attached images are staged as files the turn can open (see
243
+ // stageImages), so image input is genuinely served, not dropped.
244
+ input: ["text", "image"],
245
+ // Antigravity subscription billing flows through agy; nothing to track.
246
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
247
+ // agy does not expose per-model windows; 1M is the documented Gemini ceiling.
248
+ contextWindow: 1_000_000,
249
+ maxTokens: 65_536,
250
+ };
251
+ }