pi-yandex-bridge 0.2.4 → 0.2.8

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 CHANGED
@@ -4,13 +4,19 @@ Pi Coding Agent provider bridge for Yandex Cloud AI (YandexGPT).
4
4
 
5
5
  ## Models
6
6
 
7
- | Model | Context | Max output |
8
- | ----------------- | ------- | ---------- |
9
- | YandexGPT Pro 5.1 | 128k | 8k |
10
- | YandexGPT Pro | 128k | 8k |
11
- | YandexGPT Lite | 32k | 4k |
7
+ All models available in your Yandex Cloud folder are fetched dynamically after authentication. Model names are displayed as `name{folder_last5/tag}` — e.g. `yandexgpt-5.1{cffev/l}`.
12
8
 
13
- Unknown models discovered via the API are displayed as `slug {last4ofFolderId}`.
9
+ ## Installation
10
+
11
+ Add to `~/.pi/agent/settings.json`:
12
+
13
+ ```json
14
+ {
15
+ "extensions": ["/path/to/pi-yandex-bridge/dist/index.js"]
16
+ }
17
+ ```
18
+
19
+ Then restart Pi and run `/yalogin`.
14
20
 
15
21
  ## Auth: OAuth (default)
16
22
 
@@ -20,9 +26,9 @@ In the [Yandex Cloud console](https://console.yandex.cloud), select your folder.
20
26
 
21
27
  ### 2. Log in
22
28
 
23
- Run `/yalogin` in Pi. A browser window will open automatically — authorize the app, and the token is captured without any pasting. Pi then prompts for your folder ID.
29
+ Run `/yalogin` in Pi. A browser window opens automatically — authorize the app, and the token is captured without any pasting. Pi then prompts for your folder ID. Available models are fetched immediately after login.
24
30
 
25
- **IAM tokens expire after 12 hours** and are refreshed automatically using the stored OAuth token.
31
+ **IAM tokens expire after 12 hours** and are refreshed automatically using the stored OAuth token. The model list is re-fetched on each refresh.
26
32
 
27
33
  To skip the browser flow, set env vars before starting Pi:
28
34
 
@@ -40,11 +46,4 @@ export YANDEX_API_KEY="your-api-key"
40
46
  export YANDEX_FOLDER_ID="your-folder-id"
41
47
  ```
42
48
 
43
- You can generate an API key in the Yandex Cloud console under **Service accounts → your account → API keys**. The key is shown only once, so copy it immediately.
44
-
45
- Env vars also work for OAuth to skip interactive prompts:
46
-
47
- ```sh
48
- export YANDEX_OAUTH_TOKEN="y0_AgAAAA..."
49
- export YANDEX_FOLDER_ID="b1g..."
50
- ```
49
+ Models are fetched at startup using the API key. You can generate an API key in the [Yandex AI Studio](https://aistudio.yandex.ru) or in the Yandex Cloud console under **Service accounts → your account → API keys**.
package/index.ts CHANGED
@@ -18,6 +18,7 @@ import { homedir } from "os";
18
18
  import { join } from "path";
19
19
  import { spawnSync } from "child_process";
20
20
  import type { AddressInfo } from "net";
21
+
21
22
  import type {
22
23
  ExtensionAPI,
23
24
  ExtensionCommandContext,
@@ -31,7 +32,7 @@ import type {
31
32
  // ─── constants ────────────────────────────────────────────────────────────────
32
33
 
33
34
  const IAM_TOKEN_URL = "https://iam.api.cloud.yandex.net/iam/v1/tokens";
34
- const AI_BASE_URL = "https://ai.api.cloud.yandex.net/v1";
35
+ const AI_BASE_URL = "https://llm.api.cloud.yandex.net/v1";
35
36
  const AUTH_PATH = join(homedir(), ".pi", "agent", "auth.json");
36
37
 
37
38
  const OAUTH_CLIENT_ID = "0414b7213b22435fa65051f64270584f";
@@ -42,32 +43,6 @@ const OAUTH_URL =
42
43
  `&client_id=${OAUTH_CLIENT_ID}` +
43
44
  `&redirect_uri=${encodeURIComponent(OAUTH_REDIRECT_URI)}`;
44
45
 
45
- // ─── model catalogue ──────────────────────────────────────────────────────────
46
-
47
- const KNOWN_MODELS = [
48
- {
49
- slug: "yandexgpt-5.1",
50
- name: "YandexGPT Pro 5.1",
51
- contextWindow: 128_000,
52
- maxTokens: 8_192,
53
- cost: { input: 8.8, output: 8.8, cacheRead: 0, cacheWrite: 0 },
54
- },
55
- {
56
- slug: "yandexgpt",
57
- name: "YandexGPT Pro",
58
- contextWindow: 128_000,
59
- maxTokens: 8_192,
60
- cost: { input: 8.8, output: 8.8, cacheRead: 0, cacheWrite: 0 },
61
- },
62
- {
63
- slug: "yandexgpt-lite",
64
- name: "YandexGPT Lite",
65
- contextWindow: 32_000,
66
- maxTokens: 4_096,
67
- cost: { input: 2.2, output: 2.2, cacheRead: 0, cacheWrite: 0 },
68
- },
69
- ] as const;
70
-
71
46
  // ─── helpers ──────────────────────────────────────────────────────────────────
72
47
 
73
48
  interface IamTokenResponse {
@@ -75,15 +50,34 @@ interface IamTokenResponse {
75
50
  expiresAt: string;
76
51
  }
77
52
 
78
- /** Turns `gpt://<folderId>/<slug>/latest` into a readable display name.
79
- * Known slugs get their canonical name; unknown ones get `slug {last4ofFolderId}`. */
53
+ // Only applies to yandex gpt:// URIs leaves all other model IDs untouched.
80
54
  function prettyModelName(id: string): string {
81
- const match = id.match(/^gpt:\/\/([^/]+)\/(.+?)(?:\/latest)?$/);
55
+ const match = id.match(/^gpt:\/\/([^/]+)\/(.+?)(?:(\/latest))?$/);
82
56
  if (!match) return id;
83
- const [, folderId, slug] = match;
84
- const known = KNOWN_MODELS.find((m) => m.slug === slug);
85
- if (known) return known.name;
86
- return `${slug} {${folderId.slice(-4)}}`;
57
+ const [, folderId, slug, hasLatest] = match;
58
+ const tag = hasLatest ? "l" : "";
59
+ return `${slug}{${folderId.slice(-5)}${tag ? "/" + tag : ""}}`;
60
+ }
61
+
62
+ function modelEntry(id: string, folderId: string) {
63
+ return {
64
+ id,
65
+ name: prettyModelName(id),
66
+ api: "openai-responses" as const,
67
+ provider: "yandex",
68
+ baseUrl: AI_BASE_URL,
69
+ reasoning: false,
70
+ input: ["text"] as ("text" | "image")[],
71
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
72
+ contextWindow: 128_000,
73
+ maxTokens: 8_192,
74
+ headers: { "OpenAI-Project": folderId },
75
+ compat: {
76
+ supportsDeveloperRole: false,
77
+ supportsReasoningEffort: false,
78
+ maxTokensField: "max_tokens" as const,
79
+ },
80
+ };
87
81
  }
88
82
 
89
83
  function openBrowser(url: string) {
@@ -104,12 +98,10 @@ async function exchangeOAuthForIam(
104
98
  headers: { "Content-Type": "application/json" },
105
99
  body: JSON.stringify({ yandexPassportOauthToken: oauthToken }),
106
100
  });
107
-
108
101
  if (!res.ok) {
109
102
  const text = await res.text().catch(() => res.statusText);
110
103
  throw new Error(`IAM token exchange failed (${res.status}): ${text}`);
111
104
  }
112
-
113
105
  const data = (await res.json()) as IamTokenResponse;
114
106
  return {
115
107
  token: data.iamToken,
@@ -117,25 +109,29 @@ async function exchangeOAuthForIam(
117
109
  };
118
110
  }
119
111
 
120
- function buildModels(folderId: string) {
121
- return KNOWN_MODELS.map((m) => ({
122
- id: `gpt://${folderId}/${m.slug}/latest`,
123
- name: m.name,
124
- api: "openai-responses" as const,
125
- provider: "yandex",
126
- baseUrl: AI_BASE_URL,
127
- reasoning: false,
128
- input: ["text"] as ("text" | "image")[],
129
- cost: m.cost,
130
- contextWindow: m.contextWindow,
131
- maxTokens: m.maxTokens,
132
- headers: { "OpenAI-Project": folderId },
133
- compat: {
134
- supportsDeveloperRole: false,
135
- supportsReasoningEffort: false,
136
- maxTokensField: "max_tokens" as const,
137
- },
138
- }));
112
+ // authHeader: 'Bearer <iam_token>' for OAuth, 'Api-Key <api_key>' for static key
113
+ async function fetchModelIds(
114
+ folderId: string,
115
+ authHeader: string,
116
+ ): Promise<string[]> {
117
+ const ac = new AbortController();
118
+ const timer = setTimeout(() => ac.abort(), 5_000);
119
+ try {
120
+ const res = await fetch(`${AI_BASE_URL}/models`, {
121
+ headers: {
122
+ Authorization: authHeader,
123
+ "OpenAI-Project": folderId,
124
+ },
125
+ signal: ac.signal,
126
+ });
127
+ clearTimeout(timer);
128
+ if (!res.ok) return [];
129
+ const payload = (await res.json()) as { data: Array<{ id: string }> };
130
+ return payload.data.map((m) => m.id);
131
+ } catch {
132
+ clearTimeout(timer);
133
+ return [];
134
+ }
139
135
  }
140
136
 
141
137
  function readAuthJson(): Record<string, unknown> {
@@ -171,9 +167,7 @@ if (t) {
171
167
  </script>
172
168
  </body></html>`;
173
169
 
174
- function captureOAuthToken(
175
- onProgress?: (msg: string) => void,
176
- ): Promise<string> {
170
+ function captureOAuthToken(): Promise<string> {
177
171
  return new Promise((resolve, reject) => {
178
172
  const server = createServer((req, res) => {
179
173
  if (req.url?.startsWith("/callback")) {
@@ -196,11 +190,7 @@ function captureOAuthToken(
196
190
 
197
191
  server.on("error", (err: NodeJS.ErrnoException) => {
198
192
  if (err.code === "EADDRINUSE") {
199
- reject(
200
- new Error(
201
- `Port ${OAUTH_CALLBACK_PORT} is already in use. Stop the process using it and try again.`,
202
- ),
203
- );
193
+ reject(new Error(`Port ${OAUTH_CALLBACK_PORT} is already in use.`));
204
194
  } else {
205
195
  reject(new Error(`Local auth server error: ${err.message}`));
206
196
  }
@@ -215,9 +205,7 @@ function captureOAuthToken(
215
205
  );
216
206
 
217
207
  server.listen(OAUTH_CALLBACK_PORT, "127.0.0.1", () => {
218
- onProgress?.(
219
- `[yandex] Listening on port ${(server.address() as AddressInfo).port}. Opening browser…`,
220
- );
208
+ void (server.address() as AddressInfo).port;
221
209
  openBrowser(OAUTH_URL);
222
210
  });
223
211
 
@@ -243,13 +231,22 @@ async function yandexLogin(
243
231
  placeholder: "b1g...",
244
232
  });
245
233
  }
234
+
246
235
  const iam = await exchangeOAuthForIam(oauthToken);
247
236
 
237
+ // Fetch available models while we have fresh credentials.
238
+ const modelIds = await fetchModelIds(
239
+ folderId as string,
240
+ `Bearer ${iam.token}`,
241
+ );
242
+
248
243
  return {
249
244
  refresh: oauthToken,
250
245
  access: iam.token,
251
246
  expires: iam.expiresAt,
252
- folderId,
247
+ folderId: folderId as string,
248
+ // Store fetched model IDs so modifyModels can stay synchronous.
249
+ modelIds: JSON.stringify(modelIds),
253
250
  };
254
251
  }
255
252
 
@@ -257,7 +254,17 @@ async function yandexRefreshToken(
257
254
  credentials: OAuthCredentials,
258
255
  ): Promise<OAuthCredentials> {
259
256
  const iam = await exchangeOAuthForIam(credentials.refresh);
260
- return { ...credentials, access: iam.token, expires: iam.expiresAt };
257
+ // Re-fetch models on token refresh to pick up any new models.
258
+ const modelIds = await fetchModelIds(
259
+ credentials.folderId as string,
260
+ `Bearer ${iam.token}`,
261
+ );
262
+ return {
263
+ ...credentials,
264
+ access: iam.token,
265
+ expires: iam.expiresAt,
266
+ modelIds: JSON.stringify(modelIds),
267
+ };
261
268
  }
262
269
 
263
270
  // ─── /yalogin command ─────────────────────────────────────────────────────────
@@ -265,7 +272,6 @@ async function yandexRefreshToken(
265
272
  async function runYaLogin(ctx: ExtensionCommandContext) {
266
273
  try {
267
274
  ctx.ui.notify("Opening browser for Yandex authorization…", "info");
268
-
269
275
  const oauthToken = await captureOAuthToken();
270
276
 
271
277
  let folderId = process.env.YANDEX_FOLDER_ID ?? "";
@@ -281,6 +287,9 @@ async function runYaLogin(ctx: ExtensionCommandContext) {
281
287
  ctx.ui.notify("Exchanging OAuth token for IAM token…", "info");
282
288
  const iam = await exchangeOAuthForIam(oauthToken);
283
289
 
290
+ ctx.ui.notify("Fetching available models…", "info");
291
+ const modelIds = await fetchModelIds(folderId, `Bearer ${iam.token}`);
292
+
284
293
  const auth = readAuthJson();
285
294
  auth.yandex = {
286
295
  type: "oauth",
@@ -288,11 +297,12 @@ async function runYaLogin(ctx: ExtensionCommandContext) {
288
297
  access: iam.token,
289
298
  expires: iam.expiresAt,
290
299
  folderId,
300
+ modelIds: JSON.stringify(modelIds),
291
301
  };
292
302
  writeAuthJson(auth);
293
303
 
294
304
  ctx.ui.notify(
295
- "✓ Yandex credentials saved. Restart Pi to activate the models.",
305
+ `✓ Yandex credentials saved (${modelIds.length} models). Restart Pi to activate.`,
296
306
  "info",
297
307
  );
298
308
  } catch (err) {
@@ -309,69 +319,32 @@ export default async function (pi: ExtensionAPI) {
309
319
  const apiKey = process.env.YANDEX_API_KEY;
310
320
  const folderId = process.env.YANDEX_FOLDER_ID;
311
321
 
312
- // Static API key path — both env vars must be present.
313
322
  if (apiKey && folderId) {
314
- const modelIds = new Set(
315
- KNOWN_MODELS.map((m) => `gpt://${folderId}/${m.slug}/latest`),
316
- );
317
-
318
- try {
319
- const ac = new AbortController();
320
- const timer = setTimeout(() => ac.abort(), 5_000);
321
- const res = await fetch(`${AI_BASE_URL}/models`, {
322
- headers: {
323
- Authorization: `Bearer ${apiKey}`,
324
- "OpenAI-Project": folderId,
325
- },
326
- signal: ac.signal,
327
- });
328
- clearTimeout(timer);
329
- if (res.ok) {
330
- const payload = (await res.json()) as { data: Array<{ id: string }> };
331
- for (const { id } of payload.data) modelIds.add(id);
332
- }
333
- } catch {
334
- /* static list is enough */
335
- }
336
-
337
- const models = [...modelIds].map((id) => {
338
- const slug = id.replace(/^gpt:\/\/[^/]+\//, "").replace(/\/latest$/, "");
339
- const known = KNOWN_MODELS.find((m) => m.slug === slug);
340
- return {
341
- id,
342
- name: prettyModelName(id),
343
- reasoning: false,
344
- input: ["text"] as ("text" | "image")[],
345
- cost: known?.cost ?? {
346
- input: 0,
347
- output: 0,
348
- cacheRead: 0,
349
- cacheWrite: 0,
350
- },
351
- contextWindow: known?.contextWindow ?? 128_000,
352
- maxTokens: known?.maxTokens ?? 8_192,
353
- headers: { "OpenAI-Project": folderId },
354
- compat: {
355
- supportsDeveloperRole: false,
356
- supportsReasoningEffort: false,
357
- maxTokensField: "max_tokens" as const,
358
- },
359
- };
360
- });
361
-
323
+ // Static API key — fetch models immediately, we have credentials.
324
+ const modelIds = await fetchModelIds(folderId, `Api-Key ${apiKey}`);
362
325
  pi.registerProvider("yandex", {
363
326
  name: "Yandex Cloud",
364
327
  baseUrl: AI_BASE_URL,
365
328
  apiKey,
366
329
  api: "openai-responses",
367
- models,
330
+ models: modelIds.map((id) => modelEntry(id, folderId)),
368
331
  } satisfies ProviderConfig);
369
332
  } else {
370
- // OAuth path — seed models from auth.json if folderId is already stored.
371
- let storedFolderId: string | undefined;
333
+ // OAuth path — seed from auth.json if credentials are already stored.
334
+ let seedModels: ReturnType<typeof modelEntry>[] = [];
372
335
  try {
373
- const auth = readAuthJson() as Record<string, { folderId?: string }>;
374
- storedFolderId = auth.yandex?.folderId;
336
+ const auth = readAuthJson() as Record<
337
+ string,
338
+ {
339
+ folderId?: string;
340
+ modelIds?: string;
341
+ }
342
+ >;
343
+ const stored = auth.yandex;
344
+ if (stored?.folderId && stored?.modelIds) {
345
+ const ids = JSON.parse(stored.modelIds) as string[];
346
+ seedModels = ids.map((id) => modelEntry(id, stored.folderId!));
347
+ }
375
348
  } catch {
376
349
  /* auth.json absent or unreadable */
377
350
  }
@@ -380,16 +353,22 @@ export default async function (pi: ExtensionAPI) {
380
353
  name: "Yandex Cloud",
381
354
  baseUrl: AI_BASE_URL,
382
355
  api: "openai-responses",
383
- models: storedFolderId ? buildModels(storedFolderId) : [],
356
+ models: seedModels,
384
357
  oauth: {
385
358
  name: "Yandex Cloud (OAuth)",
386
359
  login: yandexLogin,
387
360
  refreshToken: yandexRefreshToken,
388
361
  getApiKey: (credentials) => credentials.access,
389
- modifyModels: (models, credentials) => [
390
- ...models.filter((m) => m.provider !== "yandex"),
391
- ...buildModels(credentials.folderId as string),
392
- ],
362
+ modifyModels: (models, credentials) => {
363
+ const fId = credentials.folderId as string;
364
+ const ids: string[] = credentials.modelIds
365
+ ? (JSON.parse(credentials.modelIds as string) as string[])
366
+ : [];
367
+ return [
368
+ ...models.filter((m) => m.provider !== "yandex"),
369
+ ...ids.map((id) => modelEntry(id, fId)),
370
+ ];
371
+ },
393
372
  },
394
373
  } satisfies ProviderConfig);
395
374
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-yandex-bridge",
3
- "version": "0.2.4",
3
+ "version": "0.2.8",
4
4
  "description": "Pi Coding Agent provider bridge for Yandex Cloud AI (YandexGPT)",
5
5
  "main": "./dist/index.js",
6
6
  "type": "module",
@@ -1,7 +0,0 @@
1
- {
2
- "success": true,
3
- "clones": [],
4
- "duplicatedLines": 0,
5
- "totalLines": 400,
6
- "percentage": 0
7
- }
@@ -1,4 +0,0 @@
1
- {
2
- "timestamp": "2026-05-09T06:22:47.142Z",
3
- "scanDurationMs": 1316
4
- }
@@ -1,21 +0,0 @@
1
- {
2
- "success": true,
3
- "issues": [
4
- {
5
- "type": "file",
6
- "name": "dist/index.d.ts",
7
- "file": "dist/index.d.ts"
8
- }
9
- ],
10
- "unusedExports": [],
11
- "unusedFiles": [
12
- {
13
- "type": "file",
14
- "name": "dist/index.d.ts",
15
- "file": "dist/index.d.ts"
16
- }
17
- ],
18
- "unusedDeps": [],
19
- "unlistedDeps": [],
20
- "summary": "Found 1 issues"
21
- }
@@ -1,3 +0,0 @@
1
- {
2
- "timestamp": "2026-05-09T06:26:09.792Z"
3
- }
@@ -1 +0,0 @@
1
- null
@@ -1,3 +0,0 @@
1
- {
2
- "timestamp": "2026-05-09T06:23:50.558Z"
3
- }
@@ -1,3 +0,0 @@
1
- {
2
- "items": []
3
- }
@@ -1,3 +0,0 @@
1
- {
2
- "timestamp": "2026-05-09T06:22:45.815Z"
3
- }
@@ -1,4 +0,0 @@
1
- {
2
- "signature": "index.ts::🔴 New unresolved imports/deps in modified code (Knip):\n index.ts:29 — unlisted: @earendil-works/pi-ai\n First location: index.ts\n",
3
- "sessionId": "lens-moxfkr7l-92597cdf"
4
- }
@@ -1,3 +0,0 @@
1
- {
2
- "timestamp": "2026-05-08T21:37:51.694Z"
3
- }
@@ -1 +0,0 @@
1
- null
@@ -1,3 +0,0 @@
1
- {
2
- "timestamp": "2026-05-08T21:37:55.621Z"
3
- }