@mcowger/pi-plexus 0.5.4 → 0.7.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.
Files changed (3) hide show
  1. package/README.md +138 -43
  2. package/dist/extension.js +157 -148
  3. package/package.json +5 -7
package/README.md CHANGED
@@ -2,100 +2,195 @@
2
2
 
3
3
  Exposes models from a self-hosted [Plexus](https://github.com/mcowger/plexus) AI proxy as a first-class provider inside AI coding agents. Models appear in the agent's model picker with correct wire-protocol behavior, as if they were natively supported providers.
4
4
 
5
- Supported hosts:
5
+ ## Supported agents
6
6
 
7
- - **oh-my-pi** (`can1357/oh-my-pi`) — via `plexus-pi` (the legacy-pi shim handles scope remapping automatically)
8
- - **pi** (`earendil-works/pi`) — via `plexus-pi`
7
+ | Package | Agent | npm |
8
+ |---|---|---|
9
+ | `plexus-pi` | [pi](https://github.com/earendil-works/pi) | `@mcowger/pi-plexus` |
10
+ | `plexus-opencode` | [OpenCode](https://opencode.ai) | `@mcowger/opencode-plexus` |
9
11
 
10
12
  ## Prerequisites
11
13
 
12
- - [Bun](https://bun.sh) ≥ 1.1
13
14
  - A running Plexus instance
14
- - `PLEXUS_API_KEY` set in your environment
15
+
16
+ ## Installation
17
+
18
+ The built dist artifact is committed to the repo, so no build step is needed for any install method.
19
+
20
+ ---
21
+
22
+ ### pi
23
+
24
+ #### Option 1 — npm (recommended)
15
25
 
16
26
  ```sh
17
- export PLEXUS_API_KEY=your-key-here
27
+ cd ~/.pi/agent/extensions
28
+ npm install @mcowger/pi-plexus
18
29
  ```
19
30
 
20
- Add this to your shell profile (`~/.zshrc`, `~/.zprofile`, etc.) so it persists across restarts.
31
+ #### Option 2 — git clone into the extensions directory
21
32
 
22
- ## Installation
33
+ ```sh
34
+ git clone https://github.com/mcowger/plexus-agent-plugins ~/.pi/agent/extensions/plexus-agent-plugins
35
+ ```
23
36
 
24
- ### oh-my-pi
37
+ #### Option 3 — git clone anywhere + settings.json
25
38
 
26
39
  ```sh
27
- git clone https://github.com/mcowger/plexus-agent-plugins
28
- cd plexus-agent-plugins
29
- bun install
30
- bun run --cwd packages/plexus-pi build.ts
31
- omp plugin link packages/plexus-pi
40
+ git clone https://github.com/mcowger/plexus-agent-plugins ~/code/plexus-agent-plugins
41
+ ```
42
+
43
+ Then register the path in `~/.pi/agent/settings.json`:
44
+
45
+ ```json
46
+ {
47
+ "extensions": [
48
+ "~/code/plexus-agent-plugins/packages/plexus-pi"
49
+ ]
50
+ }
32
51
  ```
33
52
 
34
- ### pi (earendil-works)
53
+ ---
54
+
55
+ ### OpenCode
56
+
57
+ #### Option 1 — npm (recommended)
35
58
 
36
59
  ```sh
37
- git clone https://github.com/mcowger/plexus-agent-plugins
38
- cd plexus-agent-plugins
39
- bun install
40
- bun run --cwd packages/plexus-pi build.ts
60
+ npm install -g @mcowger/opencode-plexus
61
+ ```
62
+
63
+ Then add the plugin to your `opencode.json`:
64
+
65
+ ```json
66
+ {
67
+ "plugins": ["@mcowger/opencode-plexus"]
68
+ }
69
+ ```
70
+
71
+ #### Option 2 — path reference
72
+
73
+ ```sh
74
+ git clone https://github.com/mcowger/plexus-agent-plugins ~/code/plexus-agent-plugins
75
+ ```
76
+
77
+ Then reference the built artifact in `opencode.json`:
78
+
79
+ ```json
80
+ {
81
+ "plugins": ["~/code/plexus-agent-plugins/packages/plexus-opencode/dist/index.js"]
82
+ }
41
83
  ```
42
84
 
43
- Then add the extension path to your pi config, or symlink the package into `~/.pi/extensions/`.
85
+ ---
44
86
 
45
- ## Usage
87
+ ## First-time setup
46
88
 
47
- On the first run, configure the Plexus base URL:
89
+ ### pi
90
+
91
+ Run inside pi:
48
92
 
49
93
  ```
50
94
  /plexus login
51
95
  ```
52
96
 
53
97
  You will be prompted for:
98
+
54
99
  - **Plexus base URL** — e.g. `https://plexus.example.com`
100
+ - **Plexus API key**
55
101
  - **Default model** (optional)
56
102
 
57
- After login, models appear immediately in the model picker. They also refresh automatically on every new session.
58
-
59
- To force a refresh:
103
+ To force a model refresh:
60
104
 
61
105
  ```
62
106
  /plexus refresh
63
107
  ```
64
108
 
65
- ## Configuration
109
+ ### OpenCode
110
+
111
+ Run inside OpenCode:
112
+
113
+ ```
114
+ /connect
115
+ ```
116
+
117
+ Select **Plexus** and enter your base URL and API key. Models are loaded immediately and cached for fast startup on subsequent sessions.
118
+
119
+ You can also pre-configure via environment variables:
120
+
121
+ ```sh
122
+ export PLEXUS_BASE_URL=https://plexus.example.com
123
+ export PLEXUS_API_KEY=your-api-key
124
+ ```
125
+
126
+ ---
66
127
 
67
- | What | How |
68
- |---|---|
69
- | API key | `PLEXUS_API_KEY` environment variable (required) |
70
- | Base URL | Set via `/plexus login`, stored at `~/.{omp,pi}/agent/extensions/plexus/config.json` |
71
- | Default model | Optionally set during `/plexus login` |
128
+ ## Configuration files
72
129
 
73
- Config, cache, and logs are written under the agent's data directory:
130
+ ### pi
74
131
 
75
132
  ```
76
- ~/.omp/agent/extensions/plexus/
77
- config.json # base URL and default model
78
- plexus-models-cache.json # last-fetched model list
133
+ ~/.pi/agent/extensions/plexus/
134
+ config.json # base URL and optional default model
135
+ plexus-models-cache.json # last-fetched model list (startup cache)
79
136
  plexus-models-response.json # raw API response (diagnostics)
80
137
  plexus.log # extension activity log
81
138
  ```
82
139
 
83
- ## Development
140
+ The API key is stored in pi's own credential store (`auth.json`) — never in a separate file.
84
141
 
85
- After editing source files, rebuild before changes take effect:
142
+ ### OpenCode
86
143
 
87
- ```sh
88
- bun run --cwd packages/plexus-pi build.ts
89
144
  ```
145
+ ~/.local/share/opencode/plugins/plexus/
146
+ models-cache.json # last-fetched model list (startup cache)
147
+ models-raw.json # raw API response (diagnostics)
148
+ ```
149
+
150
+ The API key is stored in OpenCode's own credential store — never in a separate file.
90
151
 
91
- The build bundles `plexus-models` into a single `dist/extension.js`. The `dist/` file is what the agent loads; source files are the single source of truth.
152
+ ---
92
153
 
93
154
  ## Package layout
94
155
 
95
156
  ```
96
157
  packages/
97
- plexus-models/ # host-agnostic data layer (fetch, parse, cache, config)
98
- plexus-pi/ # host adapter + extension entry point
158
+ plexus-models/ # host-agnostic data layer
159
+ src/
160
+ types.ts # wire types (PlexusApiModel, PlexusModelDescriptor, etc.)
161
+ convert.ts # model fetching, conversion, compat detection
162
+ index.ts # barrel export
163
+ plexus-pi/ # pi host adapter
164
+ src/
165
+ extension.ts # entry point: commands, session refresh, auth flow
166
+ mapper.ts # PlexusModelDescriptor → pi ProviderModelConfig
167
+ config.ts # base URL / default model config I/O
168
+ cache.ts # model cache I/O
169
+ log.ts # append-only log
170
+ package.json # declares pi.extensions entry point
171
+ plexus-opencode/ # OpenCode plugin adapter
172
+ src/
173
+ plugin.ts # Plugin export: config hook, auth handler
174
+ mapper.ts # PlexusApiModel → OpenCode ConfigModel
175
+ cache.ts # model cache I/O
176
+ config-store.ts # resolveConfig, persistToGlobalConfig
177
+ log.ts # logger via OpenCode SDK
178
+ constants.ts # provider ID, env var names, timeouts
179
+ url.ts # URL helpers (trimURL, apiBase, modelsUrl)
180
+ index.ts # barrel export
181
+ package.json # npm package manifest
99
182
  ```
100
183
 
101
- `plexus-models` has zero imports from any agent framework and can be tested in isolation.
184
+ `plexus-models` has zero imports from any agent framework. Each host adapter imports it via a relative path.
185
+
186
+ ## Development
187
+
188
+ After cloning, install dependencies to set up the pre-commit hook:
189
+
190
+ ```sh
191
+ bun install
192
+ ```
193
+
194
+ The pre-commit hook (via lefthook) rebuilds both dist artifacts automatically whenever source files change. After committing, reload/restart your agent.
195
+
196
+ To add support for a new host agent, see [AGENTS.md](AGENTS.md).
package/dist/extension.js CHANGED
@@ -1,8 +1,4 @@
1
1
  // @bun
2
- // src/extension.ts
3
- import * as os from "os";
4
- import * as path4 from "path";
5
-
6
2
  // ../plexus-models/src/convert.ts
7
3
  var REASONING_PARAMS = new Set(["reasoning", "include_reasoning", "reasoning_effort"]);
8
4
  var API_DIALECT_MAP = {
@@ -175,68 +171,59 @@ async function fetchPlexusModels(apiKey, modelsUrl) {
175
171
  const raw = await res.json();
176
172
  return { models: raw.data ?? [], raw };
177
173
  }
178
- // ../plexus-models/src/config.ts
179
- import * as fs from "fs";
180
- import * as path from "path";
181
- var PLEXUS_DIR = "extensions/plexus";
182
- var CONFIG_FILE = "config.json";
183
- function getConfigPath(agentDir) {
184
- return path.join(agentDir, PLEXUS_DIR, CONFIG_FILE);
185
- }
186
- function getConfigSync(agentDir) {
174
+ // src/config.ts
175
+ import { existsSync, readFileSync } from "fs";
176
+ import { mkdir, writeFile } from "fs/promises";
177
+ import { join } from "path";
178
+ import { getAgentDir } from "@earendil-works/pi-coding-agent";
179
+ var getConfigDir = () => join(getAgentDir(), "extensions", "plexus");
180
+ var getConfigPath = () => join(getConfigDir(), "config.json");
181
+ var normalizeRoot = (raw) => raw.replace(/\/+$/, "");
182
+ function getConfigSync() {
187
183
  try {
188
- const raw = fs.readFileSync(getConfigPath(agentDir), "utf8");
189
- const parsed = JSON.parse(raw);
190
- if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
191
- return parsed;
184
+ if (existsSync(getConfigPath())) {
185
+ return JSON.parse(readFileSync(getConfigPath(), "utf8"));
192
186
  }
193
- return {};
194
- } catch {
195
- return {};
196
- }
187
+ } catch {}
188
+ return {};
197
189
  }
198
- async function saveBaseUrl(agentDir, baseUrl, defaultModel) {
199
- const configPath = getConfigPath(agentDir);
200
- const dir = path.dirname(configPath);
201
- await fs.promises.mkdir(dir, { recursive: true });
202
- const existing = getConfigSync(agentDir);
203
- const updated = {
190
+ async function saveBaseUrl(baseUrl, defaultModel) {
191
+ await mkdir(getConfigDir(), { recursive: true });
192
+ const existing = getConfigSync();
193
+ const config = {
204
194
  ...existing,
205
- baseUrl: baseUrl.replace(/\/+$/, "")
195
+ baseUrl: normalizeRoot(baseUrl),
196
+ ...defaultModel !== undefined && { defaultModel }
206
197
  };
207
- if (defaultModel !== undefined) {
208
- updated.defaultModel = defaultModel;
209
- }
210
- await fs.promises.writeFile(configPath, JSON.stringify(updated, null, 2) + `
198
+ await writeFile(getConfigPath(), `${JSON.stringify(config, null, 2)}
211
199
  `, "utf8");
212
200
  }
213
- function getRawBaseUrl(agentDir) {
214
- const cfg = getConfigSync(agentDir);
215
- const raw = cfg.baseUrl ?? process.env["PLEXUS_BASE_URL"] ?? null;
216
- if (!raw)
217
- return null;
218
- return raw.replace(/\/+$/, "");
219
- }
220
- function getBaseUrl(agentDir) {
221
- const raw = getRawBaseUrl(agentDir);
222
- return raw ? `${raw}/v1` : null;
201
+ function getRawBaseUrl() {
202
+ const config = getConfigSync();
203
+ if (config.baseUrl)
204
+ return config.baseUrl;
205
+ return process.env["PLEXUS_BASE_URL"] ?? null;
223
206
  }
224
- function getModelsUrl(agentDir) {
225
- const raw = getRawBaseUrl(agentDir);
226
- return raw ? `${raw}/v1/models` : null;
207
+ function getModelsUrl() {
208
+ const raw = getRawBaseUrl();
209
+ return raw ? `${normalizeRoot(raw)}/v1/models` : null;
227
210
  }
228
- // ../plexus-models/src/cache.ts
229
- import * as fs2 from "fs";
230
- import * as path2 from "path";
231
- var PLEXUS_DIR2 = "extensions/plexus";
232
- var CACHE_FILE = "plexus-models-cache.json";
233
- var RAW_RESPONSE_FILE = "plexus-models-response.json";
234
- function getCachePath(agentDir) {
235
- return path2.join(agentDir, PLEXUS_DIR2, CACHE_FILE);
211
+ function getBaseUrl() {
212
+ const raw = getRawBaseUrl();
213
+ return raw ? `${normalizeRoot(raw)}/v1` : null;
236
214
  }
237
- function getRawResponsePath(agentDir) {
238
- return path2.join(agentDir, PLEXUS_DIR2, RAW_RESPONSE_FILE);
215
+ function getDefaultModel() {
216
+ return getConfigSync().defaultModel ?? null;
239
217
  }
218
+
219
+ // src/cache.ts
220
+ import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
221
+ import { mkdir as mkdir2, writeFile as writeFile2 } from "fs/promises";
222
+ import { join as join2 } from "path";
223
+ import { getAgentDir as getAgentDir2 } from "@earendil-works/pi-coding-agent";
224
+ var getCacheDir = () => join2(getAgentDir2(), "extensions", "plexus");
225
+ var getModelsCachePath = () => join2(getCacheDir(), "plexus-models-cache.json");
226
+ var getRawResponsePath = () => join2(getCacheDir(), "plexus-models-response.json");
240
227
  function parseCacheData(raw) {
241
228
  try {
242
229
  const parsed = JSON.parse(raw);
@@ -253,44 +240,45 @@ function parseCacheData(raw) {
253
240
  return null;
254
241
  }
255
242
  }
256
- function readCachedModelsSync(agentDir) {
243
+ function readCachedModelsSync() {
257
244
  try {
258
- const raw = fs2.readFileSync(getCachePath(agentDir), "utf8");
259
- return parseCacheData(raw);
245
+ const p = getModelsCachePath();
246
+ if (!existsSync2(p))
247
+ return null;
248
+ return parseCacheData(readFileSync2(p, "utf8"));
260
249
  } catch {
261
250
  return null;
262
251
  }
263
252
  }
264
- async function writeCachedModels(agentDir, models) {
265
- const cachePath = getCachePath(agentDir);
266
- await fs2.promises.mkdir(path2.dirname(cachePath), { recursive: true });
253
+ async function writeCachedModels(models) {
254
+ await mkdir2(getCacheDir(), { recursive: true });
267
255
  const payload = { models, timestamp: Date.now() };
268
- await fs2.promises.writeFile(cachePath, JSON.stringify(payload, null, 2) + `
256
+ await writeFile2(getModelsCachePath(), `${JSON.stringify(payload, null, 2)}
269
257
  `, "utf8");
270
258
  }
271
- async function writeRawResponse(agentDir, data) {
272
- const rawPath = getRawResponsePath(agentDir);
273
- await fs2.promises.mkdir(path2.dirname(rawPath), { recursive: true });
274
- await fs2.promises.writeFile(rawPath, JSON.stringify(data, null, 2) + `
259
+ async function writeRawResponse(data) {
260
+ await mkdir2(getCacheDir(), { recursive: true });
261
+ await writeFile2(getRawResponsePath(), `${JSON.stringify(data, null, 2)}
275
262
  `, "utf8");
276
263
  }
277
- // ../plexus-models/src/log.ts
278
- import * as fs3 from "fs";
279
- import * as path3 from "path";
280
- var PLEXUS_DIR3 = "extensions/plexus";
281
- var LOG_FILE = "plexus.log";
282
- function log(agentDir, message, data) {
264
+
265
+ // src/log.ts
266
+ import { appendFileSync, mkdirSync } from "fs";
267
+ import { join as join3 } from "path";
268
+ import { getAgentDir as getAgentDir3 } from "@earendil-works/pi-coding-agent";
269
+ var getCacheDir2 = () => join3(getAgentDir3(), "extensions", "plexus");
270
+ var getLogPath = () => join3(getCacheDir2(), "plexus.log");
271
+ function log(message, data) {
283
272
  try {
284
- const logPath = path3.join(agentDir, PLEXUS_DIR3, LOG_FILE);
285
- const dir = path3.dirname(logPath);
286
- fs3.mkdirSync(dir, { recursive: true });
273
+ mkdirSync(getCacheDir2(), { recursive: true });
287
274
  const ts = new Date().toISOString();
288
275
  const line = data !== undefined ? `${ts} ${message} ${JSON.stringify(data)}
289
276
  ` : `${ts} ${message}
290
277
  `;
291
- fs3.appendFileSync(logPath, line, "utf8");
278
+ appendFileSync(getLogPath(), line, "utf8");
292
279
  } catch {}
293
280
  }
281
+
294
282
  // src/mapper.ts
295
283
  function descriptorToPiModel(descriptor) {
296
284
  const cost = {
@@ -323,106 +311,127 @@ function descriptorToPiModel(descriptor) {
323
311
 
324
312
  // src/extension.ts
325
313
  var PROVIDER_NAME = "plexus";
326
- function getAgentDir() {
327
- const override = process.env["PI_CODING_AGENT_DIR"];
328
- if (override)
329
- return path4.resolve(override);
330
- const configDir = process.env["PI_CONFIG_DIR"] || ".pi";
331
- return path4.join(os.homedir(), configDir, "agent");
332
- }
333
- function getApiKey() {
334
- return process.env["PLEXUS_API_KEY"];
335
- }
314
+ var currentModels = [];
336
315
  function plexusExtension(pi) {
337
- const agentDir = getAgentDir();
338
- const key = getApiKey();
339
- if (key) {
340
- const cached = readCachedModelsSync(agentDir);
341
- if (cached && cached.models.length > 0) {
342
- const baseUrl = getBaseUrl(agentDir);
343
- if (baseUrl) {
344
- pi.registerProvider(PROVIDER_NAME, {
345
- baseUrl,
346
- apiKey: key,
347
- api: "openai-completions",
348
- authHeader: true,
349
- models: cached.models.map(descriptorToPiModel)
350
- });
351
- log(agentDir, "startup: registered from cache", { count: cached.models.length });
352
- }
316
+ const cached = readCachedModelsSync();
317
+ const startupBaseUrl = getBaseUrl() ?? "http://localhost/v1";
318
+ const startupModels = cached?.models.map(descriptorToPiModel) ?? [];
319
+ log("startup", {
320
+ cachedModelCount: startupModels.length,
321
+ startupBaseUrl
322
+ });
323
+ pi.registerProvider(PROVIDER_NAME, {
324
+ api: "openai-completions",
325
+ apiKey: PROVIDER_NAME,
326
+ authHeader: true,
327
+ baseUrl: startupBaseUrl,
328
+ models: startupModels
329
+ });
330
+ currentModels = startupModels;
331
+ pi.on("session_start", async (_event, ctx) => {
332
+ const apiKey = await ctx.modelRegistry.authStorage.getApiKey(PROVIDER_NAME);
333
+ const baseUrl = getBaseUrl();
334
+ log("session_start", { hasApiKey: !!apiKey, baseUrl });
335
+ if (!apiKey || !baseUrl) {
336
+ log("session_start: no auth configured, skipping refresh");
337
+ await trySetDefaultModel(pi, startupModels);
338
+ return;
353
339
  }
354
- }
355
- pi.on("session_start", async () => {
356
- await doRefresh(pi, agentDir, null);
340
+ await doRefresh(pi, apiKey, ctx, true);
357
341
  });
358
342
  pi.registerCommand("plexus", {
359
- description: "Manage Plexus AI model proxy. Sub-commands: login, refresh",
360
- async handler(args, ctx) {
343
+ description: "Plexus provider commands: login, refresh",
344
+ getArgumentCompletions: () => [
345
+ { value: "login", label: "login", description: "Configure Plexus base URL and API key" },
346
+ { value: "refresh", label: "refresh", description: "Refresh Plexus models from the API" }
347
+ ],
348
+ handler: async (args, ctx) => {
361
349
  const sub = args.trim().toLowerCase();
362
350
  if (sub === "login" || sub === "") {
363
- await handleLogin(pi, ctx, agentDir);
364
- } else if (sub === "refresh") {
365
- await handleRefresh(pi, ctx, agentDir);
366
- } else {
367
- ctx.ui.notify(`Unknown sub-command: "${args}". Use: /plexus login | /plexus refresh`, "warning");
351
+ await handleLogin(pi, ctx);
352
+ return;
353
+ }
354
+ if (sub === "refresh") {
355
+ await handleRefresh(pi, ctx);
356
+ return;
368
357
  }
358
+ ctx.ui.notify(`Unknown sub-command: "${args}". Use: /plexus login | /plexus refresh`, "warning");
369
359
  }
370
360
  });
371
361
  }
372
- async function handleLogin(pi, ctx, agentDir) {
373
- if (!getApiKey()) {
374
- ctx.ui.notify("PLEXUS_API_KEY env var is not set. Set it and restart omp.", "error");
375
- return;
376
- }
362
+ async function handleLogin(pi, ctx) {
377
363
  const baseUrlInput = await ctx.ui.input("Plexus base URL", "https://plexus.example.com");
378
364
  if (!baseUrlInput) {
379
365
  ctx.ui.notify("Login cancelled.", "info");
380
366
  return;
381
367
  }
368
+ const apiKeyInput = await ctx.ui.input("Plexus API key");
369
+ if (!apiKeyInput) {
370
+ ctx.ui.notify("Login cancelled.", "info");
371
+ return;
372
+ }
382
373
  const defaultModelInput = await ctx.ui.input("Default model (optional \u2014 Enter to skip)", "");
383
- await saveBaseUrl(agentDir, baseUrlInput.trim(), defaultModelInput?.trim() || undefined);
384
- ctx.ui.notify("Plexus config saved. Refreshing models\u2026", "info");
385
- await doRefresh(pi, agentDir, ctx);
386
- }
387
- async function handleRefresh(pi, ctx, agentDir) {
388
- ctx.ui.notify("Refreshing Plexus models\u2026", "info");
389
- await doRefresh(pi, agentDir, ctx);
374
+ const defaultModel = defaultModelInput?.trim() || undefined;
375
+ await saveBaseUrl(baseUrlInput.trim(), defaultModel);
376
+ ctx.modelRegistry.authStorage.set(PROVIDER_NAME, { type: "api_key", key: apiKeyInput.trim() });
377
+ log("login: saved", { baseUrl: baseUrlInput.trim(), defaultModel });
378
+ ctx.ui.notify("Plexus credentials saved. Refreshing models\u2026", "info");
379
+ await doRefresh(pi, apiKeyInput.trim(), ctx, false);
390
380
  }
391
- async function doRefresh(pi, agentDir, notify) {
392
- const modelsUrl = getModelsUrl(agentDir);
393
- const baseUrl = getBaseUrl(agentDir);
394
- const key = getApiKey();
395
- if (!key) {
396
- if (notify)
397
- notify.ui.notify("PLEXUS_API_KEY env var is not set.", "error");
381
+ async function handleRefresh(pi, ctx) {
382
+ const apiKey = await ctx.modelRegistry.authStorage.getApiKey(PROVIDER_NAME);
383
+ if (!apiKey) {
384
+ ctx.ui.notify("No Plexus API key configured. Run /plexus login first.", "error");
398
385
  return;
399
386
  }
387
+ ctx.ui.notify("Refreshing Plexus models\u2026", "info");
388
+ await doRefresh(pi, apiKey, ctx, true);
389
+ }
390
+ async function doRefresh(pi, apiKey, ctx, setDefault) {
391
+ const modelsUrl = getModelsUrl();
392
+ const baseUrl = getBaseUrl();
400
393
  if (!modelsUrl || !baseUrl) {
401
- if (notify)
402
- notify.ui.notify("Plexus base URL not configured. Run /plexus login first.", "warning");
394
+ if (ctx)
395
+ ctx.ui.notify("Plexus base URL not configured. Run /plexus login first.", "warning");
396
+ log("doRefresh: no base URL configured");
403
397
  return;
404
398
  }
405
399
  try {
406
- const { models: raw, raw: rawResponse } = await fetchPlexusModels(key, modelsUrl);
407
- const descriptors = convertDescriptors(raw, baseUrl);
408
- await writeCachedModels(agentDir, descriptors);
409
- await writeRawResponse(agentDir, rawResponse);
400
+ const { models: apiModels, raw } = await fetchPlexusModels(apiKey, modelsUrl);
401
+ const descriptors = convertDescriptors(apiModels, baseUrl);
402
+ const piModels = descriptors.map(descriptorToPiModel);
403
+ await Promise.all([writeCachedModels(descriptors), writeRawResponse(raw)]);
404
+ currentModels = piModels;
410
405
  pi.registerProvider(PROVIDER_NAME, {
411
- baseUrl,
412
- apiKey: key,
413
406
  api: "openai-completions",
407
+ apiKey: PROVIDER_NAME,
414
408
  authHeader: true,
415
- models: descriptors.map(descriptorToPiModel)
409
+ baseUrl,
410
+ models: piModels
416
411
  });
417
- log(agentDir, "refresh: ok", { count: descriptors.length });
418
- if (notify)
419
- notify.ui.notify(`Plexus: loaded ${descriptors.length} models.`, "info");
420
- } catch (err) {
421
- const message = err instanceof Error ? err.message : String(err);
422
- log(agentDir, "refresh: error", { error: message });
423
- if (notify)
424
- notify.ui.notify(`Plexus refresh failed: ${message}`, "error");
412
+ log("doRefresh: registered", { count: piModels.length });
413
+ if (ctx)
414
+ ctx.ui.notify(`Refreshed ${piModels.length} Plexus models`, "info");
415
+ if (setDefault)
416
+ await trySetDefaultModel(pi, piModels);
417
+ } catch (error) {
418
+ log("doRefresh: failed", { error: String(error) });
419
+ if (ctx) {
420
+ ctx.ui.notify(`Refresh failed: ${error instanceof Error ? error.message : String(error)}`, "error");
421
+ }
422
+ }
423
+ }
424
+ async function trySetDefaultModel(pi, models) {
425
+ const defaultModelId = getDefaultModel();
426
+ if (!defaultModelId)
427
+ return;
428
+ const model = models.find((m) => m.id === defaultModelId);
429
+ if (!model) {
430
+ log("trySetDefaultModel: model not found", { defaultModelId });
431
+ return;
425
432
  }
433
+ const ok = await pi.setModel(model);
434
+ log("trySetDefaultModel", { defaultModelId, ok });
426
435
  }
427
436
  export {
428
437
  plexusExtension as default
package/package.json CHANGED
@@ -1,9 +1,8 @@
1
1
  {
2
2
  "name": "@mcowger/pi-plexus",
3
- "version": "0.5.4",
4
- "description": "Plexus AI proxy plugin for pi and oh-my-pi coding agents",
3
+ "version": "0.7.0",
4
+ "description": "Plexus AI proxy plugin for the pi coding agent",
5
5
  "type": "module",
6
- "main": "./dist/extension.js",
7
6
  "files": [
8
7
  "dist/extension.js",
9
8
  "package.json",
@@ -12,8 +11,6 @@
12
11
  "keywords": [
13
12
  "plexus",
14
13
  "pi",
15
- "omp",
16
- "oh-my-pi",
17
14
  "ai",
18
15
  "coding-agent",
19
16
  "extension",
@@ -27,8 +24,9 @@
27
24
  "scripts": {
28
25
  "build": "bun run build.ts"
29
26
  },
30
- "dependencies": {
31
- "plexus-models": "workspace:*"
27
+ "peerDependencies": {
28
+ "@earendil-works/pi-ai": "*",
29
+ "@earendil-works/pi-coding-agent": "*"
32
30
  },
33
31
  "devDependencies": {
34
32
  "@types/bun": "latest"