@mcowger/pi-plexus 0.8.4 → 1.0.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.
- package/README.md +43 -8
- package/dist/extension.js +179 -49
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -88,10 +88,10 @@ Then reference the built artifact in `opencode.json`:
|
|
|
88
88
|
|
|
89
89
|
### pi
|
|
90
90
|
|
|
91
|
-
Run inside pi:
|
|
91
|
+
Run inside pi using the native login flow:
|
|
92
92
|
|
|
93
93
|
```
|
|
94
|
-
/plexus
|
|
94
|
+
/login plexus
|
|
95
95
|
```
|
|
96
96
|
|
|
97
97
|
You will be prompted for:
|
|
@@ -106,6 +106,8 @@ To force a model refresh:
|
|
|
106
106
|
/plexus refresh
|
|
107
107
|
```
|
|
108
108
|
|
|
109
|
+
Use `/login plexus` for setup and `/logout plexus` to remove stored credentials.
|
|
110
|
+
|
|
109
111
|
### OpenCode
|
|
110
112
|
|
|
111
113
|
Run inside OpenCode:
|
|
@@ -114,14 +116,19 @@ Run inside OpenCode:
|
|
|
114
116
|
/connect
|
|
115
117
|
```
|
|
116
118
|
|
|
117
|
-
Select **Plexus** and enter your base URL and API key. Models are
|
|
119
|
+
Select **Plexus** and enter your base URL and API key. Models are discovered through OpenCode's provider hook and cached for fast startup on subsequent sessions.
|
|
118
120
|
|
|
119
|
-
|
|
121
|
+
You may enter either the Plexus root URL or the `/v1` API base URL:
|
|
120
122
|
|
|
121
123
|
```text
|
|
124
|
+
https://plexus.example.com
|
|
122
125
|
https://plexus.example.com/v1
|
|
123
126
|
```
|
|
124
127
|
|
|
128
|
+
The plugins normalize either form to the Plexus root URL for storage and derive `/v1` paths when calling the API.
|
|
129
|
+
|
|
130
|
+
OpenCode stores the API key in its native auth store and stores the Plexus base URL as auth metadata on that connection. Existing `provider.plexus.options.plexusBaseURL` config is still honored as a fallback.
|
|
131
|
+
|
|
125
132
|
The OpenCode plugin respects each model's `preferred_api` value and routes models through the matching SDK/API shape:
|
|
126
133
|
|
|
127
134
|
- `chat_completions` / `openai-completions` → OpenAI-compatible chat completions
|
|
@@ -132,10 +139,12 @@ The OpenCode plugin respects each model's `preferred_api` value and routes model
|
|
|
132
139
|
You can also pre-configure via environment variables:
|
|
133
140
|
|
|
134
141
|
```sh
|
|
135
|
-
export
|
|
142
|
+
export PLEXUS_API_URL=https://plexus.example.com
|
|
136
143
|
export PLEXUS_API_KEY=your-api-key
|
|
137
144
|
```
|
|
138
145
|
|
|
146
|
+
`PLEXUS_BASE_URL` is also accepted for compatibility; `PLEXUS_API_URL` wins when both are set.
|
|
147
|
+
|
|
139
148
|
---
|
|
140
149
|
|
|
141
150
|
## Configuration files
|
|
@@ -150,7 +159,7 @@ export PLEXUS_API_KEY=your-api-key
|
|
|
150
159
|
plexus.log # extension activity log
|
|
151
160
|
```
|
|
152
161
|
|
|
153
|
-
The API key is stored
|
|
162
|
+
The API key is stored through pi's own auth storage. `PLEXUS_API_URL` or `PLEXUS_BASE_URL` can be used as an environment override.
|
|
154
163
|
|
|
155
164
|
### OpenCode
|
|
156
165
|
|
|
@@ -160,7 +169,9 @@ The API key is stored in pi's own credential store (`auth.json`) — never in a
|
|
|
160
169
|
models-raw.json # raw API response (diagnostics)
|
|
161
170
|
```
|
|
162
171
|
|
|
163
|
-
The API key is stored
|
|
172
|
+
The API key is stored through OpenCode's auth flow, and the Plexus base URL is stored as auth metadata. `PLEXUS_API_URL`, `PLEXUS_BASE_URL`, and `PLEXUS_API_KEY` can be used as environment overrides.
|
|
173
|
+
|
|
174
|
+
Both adapters also accept pi-style environment interpolation in configured strings, such as `${PLEXUS_API_URL}` or `$PLEXUS_API_KEY`. This is useful when checking non-secret config into an agent config file while keeping the actual values in the environment.
|
|
164
175
|
|
|
165
176
|
---
|
|
166
177
|
|
|
@@ -183,7 +194,7 @@ packages/
|
|
|
183
194
|
package.json # declares pi.extensions entry point
|
|
184
195
|
plexus-opencode/ # OpenCode plugin adapter
|
|
185
196
|
src/
|
|
186
|
-
plugin.ts # Plugin export: config hook, auth handler
|
|
197
|
+
plugin.ts # Plugin export: config hook, provider hook, auth handler
|
|
187
198
|
mapper.ts # PlexusApiModel → OpenCode ConfigModel
|
|
188
199
|
cache.ts # model cache I/O
|
|
189
200
|
config-store.ts # resolveConfig, persistToGlobalConfig
|
|
@@ -196,6 +207,30 @@ packages/
|
|
|
196
207
|
|
|
197
208
|
`plexus-models` has zero imports from any agent framework. Each host adapter imports it via a relative path.
|
|
198
209
|
|
|
210
|
+
## Plexus model metadata
|
|
211
|
+
|
|
212
|
+
The `/v1/models` endpoint returns an OpenRouter-style list. These fields drive host behavior:
|
|
213
|
+
|
|
214
|
+
| Field | Effect |
|
|
215
|
+
|---|---|
|
|
216
|
+
| `preferred_api` | String or array. First recognized value selects the host API dialect. |
|
|
217
|
+
| `supported_parameters` | `reasoning`, `include_reasoning`, or `reasoning_effort` enables reasoning support. |
|
|
218
|
+
| `architecture.input_modalities` | Enables text/image/audio/video/pdf support where the host supports it. |
|
|
219
|
+
| `architecture.output_modalities` | Non-text-only output models are filtered from OpenCode chat providers. |
|
|
220
|
+
| `pricing` | Converted into host per-million-token cost metadata. Plexus returns per-token prices. |
|
|
221
|
+
| `pricing.tiers` | Alternate rates above `input_tokens_above`; mapped to native pi and OpenCode context pricing tiers. |
|
|
222
|
+
| `top_provider` | Supplies context and output token limits when present. |
|
|
223
|
+
| `pi_provider` / `pi_model` | Lets the pi adapter reuse built-in pi compat, headers, and thinking-level metadata. |
|
|
224
|
+
| `pi_options` | pi compat overrides. These win over heuristic and built-in metadata. |
|
|
225
|
+
|
|
226
|
+
Models with a falsy `id` are skipped. Missing metadata falls back to safe defaults.
|
|
227
|
+
|
|
228
|
+
## Adapter behavior
|
|
229
|
+
|
|
230
|
+
- **pi** refreshes on session start and through `/plexus refresh`. It accepts either root URLs or URLs ending in `/v1` and normalizes them before calling Plexus.
|
|
231
|
+
- **OpenCode** seeds the provider from cache or a placeholder model during config loading, then performs live discovery through the `provider.models` hook. If Plexus is slow or unavailable, OpenCode uses the cache and lets the refresh continue in the background.
|
|
232
|
+
- Both adapters convert Plexus's per-token base and tier rates to the per-million-token units expected by their host.
|
|
233
|
+
|
|
199
234
|
## Development
|
|
200
235
|
|
|
201
236
|
After cloning, install dependencies to set up the pre-commit hook:
|
package/dist/extension.js
CHANGED
|
@@ -64,11 +64,29 @@ function resolveMaxTokens(model, contextWindow) {
|
|
|
64
64
|
const v = model.top_provider?.max_completion_tokens ?? null;
|
|
65
65
|
return v != null && v > 0 ? v : contextWindow;
|
|
66
66
|
}
|
|
67
|
+
function resolvePricingTiers(model) {
|
|
68
|
+
const pricing = model.pricing;
|
|
69
|
+
if (!pricing?.tiers)
|
|
70
|
+
return;
|
|
71
|
+
const tiers = pricing.tiers.flatMap((tier) => {
|
|
72
|
+
if (!Number.isFinite(tier.input_tokens_above) || tier.input_tokens_above < 0)
|
|
73
|
+
return [];
|
|
74
|
+
return [{
|
|
75
|
+
inputTokensAbove: tier.input_tokens_above,
|
|
76
|
+
input: parsePrice(tier.prompt ?? pricing.prompt),
|
|
77
|
+
output: parsePrice(tier.completion ?? pricing.completion),
|
|
78
|
+
cacheRead: parsePrice(tier.input_cache_read ?? pricing.input_cache_read),
|
|
79
|
+
cacheWrite: parsePrice(tier.input_cache_write ?? pricing.input_cache_write)
|
|
80
|
+
}];
|
|
81
|
+
});
|
|
82
|
+
return tiers.length > 0 ? tiers : undefined;
|
|
83
|
+
}
|
|
67
84
|
function convertToDescriptor(raw, baseUrl) {
|
|
68
85
|
const preferredApi = mapPreferredApi(raw.preferred_api);
|
|
69
86
|
const adjustedBaseUrl = adjustBaseUrl(baseUrl, preferredApi);
|
|
70
87
|
const contextWindow = resolveContextWindow(raw);
|
|
71
88
|
const maxTokens = resolveMaxTokens(raw, contextWindow);
|
|
89
|
+
const tiers = resolvePricingTiers(raw);
|
|
72
90
|
const descriptor = {
|
|
73
91
|
id: raw.id,
|
|
74
92
|
name: raw.name ?? raw.id,
|
|
@@ -81,7 +99,8 @@ function convertToDescriptor(raw, baseUrl) {
|
|
|
81
99
|
input: parsePrice(raw.pricing?.prompt),
|
|
82
100
|
output: parsePrice(raw.pricing?.completion),
|
|
83
101
|
cacheRead: parsePrice(raw.pricing?.input_cache_read),
|
|
84
|
-
cacheWrite: parsePrice(raw.pricing?.input_cache_write)
|
|
102
|
+
cacheWrite: parsePrice(raw.pricing?.input_cache_write),
|
|
103
|
+
...tiers !== undefined ? { tiers } : {}
|
|
85
104
|
},
|
|
86
105
|
contextWindow,
|
|
87
106
|
maxTokens
|
|
@@ -167,11 +186,11 @@ async function fetchPlexusModels(apiKey, modelsUrl, timeoutMs = DEFAULT_MODELS_F
|
|
|
167
186
|
const controller = new AbortController;
|
|
168
187
|
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
169
188
|
try {
|
|
189
|
+
const headers = { Accept: "application/json" };
|
|
190
|
+
if (apiKey)
|
|
191
|
+
headers.Authorization = `Bearer ${apiKey}`;
|
|
170
192
|
const res = await fetch(modelsUrl, {
|
|
171
|
-
headers
|
|
172
|
-
Authorization: `Bearer ${apiKey}`,
|
|
173
|
-
Accept: "application/json"
|
|
174
|
-
},
|
|
193
|
+
headers,
|
|
175
194
|
signal: controller.signal
|
|
176
195
|
});
|
|
177
196
|
if (!res.ok) {
|
|
@@ -195,7 +214,76 @@ import { join } from "path";
|
|
|
195
214
|
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
196
215
|
var getConfigDir = () => join(getAgentDir(), "extensions", "plexus");
|
|
197
216
|
var getConfigPath = () => join(getConfigDir(), "config.json");
|
|
198
|
-
var
|
|
217
|
+
var ENV_BASE_URL = "PLEXUS_BASE_URL";
|
|
218
|
+
var ENV_API_URL = "PLEXUS_API_URL";
|
|
219
|
+
var ENV_API_KEY = "PLEXUS_API_KEY";
|
|
220
|
+
var ENV_VAR_NAME_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
221
|
+
var ENV_VAR_NAME_PREFIX_RE = /^[A-Za-z_][A-Za-z0-9_]*/;
|
|
222
|
+
var normalizeRoot = (raw) => raw.trim().replace(/\/+$/, "");
|
|
223
|
+
function resolveConfigTemplate(value) {
|
|
224
|
+
let result = "";
|
|
225
|
+
let index = 0;
|
|
226
|
+
while (index < value.length) {
|
|
227
|
+
const dollarIndex = value.indexOf("$", index);
|
|
228
|
+
if (dollarIndex < 0) {
|
|
229
|
+
result += value.slice(index);
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
result += value.slice(index, dollarIndex);
|
|
233
|
+
const nextChar = value[dollarIndex + 1];
|
|
234
|
+
if (nextChar === "$" || nextChar === "!") {
|
|
235
|
+
result += nextChar;
|
|
236
|
+
index = dollarIndex + 2;
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
if (nextChar === "{") {
|
|
240
|
+
const endIndex = value.indexOf("}", dollarIndex + 2);
|
|
241
|
+
if (endIndex < 0) {
|
|
242
|
+
result += "$";
|
|
243
|
+
index = dollarIndex + 1;
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
const name = value.slice(dollarIndex + 2, endIndex);
|
|
247
|
+
if (!ENV_VAR_NAME_RE.test(name)) {
|
|
248
|
+
result += value.slice(dollarIndex, endIndex + 1);
|
|
249
|
+
index = endIndex + 1;
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
const envValue = process.env[name];
|
|
253
|
+
if (envValue === undefined)
|
|
254
|
+
return;
|
|
255
|
+
result += envValue;
|
|
256
|
+
index = endIndex + 1;
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
const match = value.slice(dollarIndex + 1).match(ENV_VAR_NAME_PREFIX_RE);
|
|
260
|
+
if (match) {
|
|
261
|
+
const envValue = process.env[match[0]];
|
|
262
|
+
if (envValue === undefined)
|
|
263
|
+
return;
|
|
264
|
+
result += envValue;
|
|
265
|
+
index = dollarIndex + 1 + match[0].length;
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
result += "$";
|
|
269
|
+
index = dollarIndex + 1;
|
|
270
|
+
}
|
|
271
|
+
return result;
|
|
272
|
+
}
|
|
273
|
+
function resolveStringOption(value) {
|
|
274
|
+
if (!value)
|
|
275
|
+
return;
|
|
276
|
+
const resolved = resolveConfigTemplate(value)?.trim();
|
|
277
|
+
return resolved || undefined;
|
|
278
|
+
}
|
|
279
|
+
var normalizeConfigBaseUrl = (raw) => {
|
|
280
|
+
const root = normalizeRoot(raw);
|
|
281
|
+
return root.endsWith("/v1") ? root.slice(0, -3) : root;
|
|
282
|
+
};
|
|
283
|
+
var normalizeApiBase = (raw) => {
|
|
284
|
+
const root = normalizeConfigBaseUrl(raw);
|
|
285
|
+
return root ? `${root}/v1` : "";
|
|
286
|
+
};
|
|
199
287
|
var cachedConfig = null;
|
|
200
288
|
function getConfigSync() {
|
|
201
289
|
if (cachedConfig)
|
|
@@ -214,7 +302,7 @@ async function saveBaseUrl(baseUrl, defaultModel) {
|
|
|
214
302
|
const existing = getConfigSync();
|
|
215
303
|
const config = {
|
|
216
304
|
...existing,
|
|
217
|
-
baseUrl:
|
|
305
|
+
baseUrl: normalizeConfigBaseUrl(baseUrl),
|
|
218
306
|
...defaultModel !== undefined && { defaultModel }
|
|
219
307
|
};
|
|
220
308
|
await writeFile(getConfigPath(), `${JSON.stringify(config, null, 2)}
|
|
@@ -223,17 +311,18 @@ async function saveBaseUrl(baseUrl, defaultModel) {
|
|
|
223
311
|
}
|
|
224
312
|
function getRawBaseUrl() {
|
|
225
313
|
const config = getConfigSync();
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
314
|
+
return resolveStringOption(process.env[ENV_API_URL]) ?? resolveStringOption(process.env[ENV_BASE_URL]) ?? resolveStringOption(config.baseUrl) ?? null;
|
|
315
|
+
}
|
|
316
|
+
function getEnvApiKey() {
|
|
317
|
+
return resolveStringOption(process.env[ENV_API_KEY]) ?? null;
|
|
229
318
|
}
|
|
230
319
|
function getModelsUrl() {
|
|
231
320
|
const raw = getRawBaseUrl();
|
|
232
|
-
return raw ? `${
|
|
321
|
+
return raw ? `${normalizeApiBase(raw)}/models` : null;
|
|
233
322
|
}
|
|
234
323
|
function getBaseUrl() {
|
|
235
324
|
const raw = getRawBaseUrl();
|
|
236
|
-
return raw ?
|
|
325
|
+
return raw ? normalizeApiBase(raw) : null;
|
|
237
326
|
}
|
|
238
327
|
function getDefaultModel() {
|
|
239
328
|
return getConfigSync().defaultModel ?? null;
|
|
@@ -310,20 +399,41 @@ async function writeLogLine(message, data) {
|
|
|
310
399
|
}
|
|
311
400
|
|
|
312
401
|
// src/mapper.ts
|
|
402
|
+
import { getBuiltinModel } from "@earendil-works/pi-ai/providers/all";
|
|
313
403
|
function descriptorToPiModel(descriptor) {
|
|
404
|
+
let builtinModel;
|
|
405
|
+
if (descriptor.piProvider && descriptor.piModel) {
|
|
406
|
+
try {
|
|
407
|
+
builtinModel = getBuiltinModel(descriptor.piProvider, descriptor.piModel);
|
|
408
|
+
} catch {
|
|
409
|
+
builtinModel = undefined;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
314
412
|
const cost = {
|
|
315
413
|
input: descriptor.cost.input * 1e6,
|
|
316
414
|
output: descriptor.cost.output * 1e6,
|
|
317
415
|
cacheRead: descriptor.cost.cacheRead * 1e6,
|
|
318
|
-
cacheWrite: descriptor.cost.cacheWrite * 1e6
|
|
416
|
+
cacheWrite: descriptor.cost.cacheWrite * 1e6,
|
|
417
|
+
...descriptor.cost.tiers ? {
|
|
418
|
+
tiers: descriptor.cost.tiers.map((tier) => ({
|
|
419
|
+
inputTokensAbove: tier.inputTokensAbove,
|
|
420
|
+
input: tier.input * 1e6,
|
|
421
|
+
output: tier.output * 1e6,
|
|
422
|
+
cacheRead: tier.cacheRead * 1e6,
|
|
423
|
+
cacheWrite: tier.cacheWrite * 1e6
|
|
424
|
+
}))
|
|
425
|
+
} : {}
|
|
319
426
|
};
|
|
320
427
|
let compat;
|
|
321
428
|
if (descriptor.preferredApi === "openai-completions") {
|
|
322
|
-
const heuristic = detectOpenAICompletionsCompat(descriptor.provider, descriptor.baseUrl);
|
|
323
|
-
const
|
|
429
|
+
const heuristic = detectOpenAICompletionsCompat(descriptor.piProvider ?? descriptor.provider, descriptor.baseUrl);
|
|
430
|
+
const builtinCompat = builtinModel?.compat;
|
|
431
|
+
const merged = { ...heuristic, ...builtinCompat ?? {}, ...descriptor.piOptions ?? {} };
|
|
324
432
|
compat = merged;
|
|
325
433
|
} else if (descriptor.piOptions) {
|
|
326
434
|
compat = descriptor.piOptions;
|
|
435
|
+
} else if (builtinModel?.compat) {
|
|
436
|
+
compat = builtinModel.compat;
|
|
327
437
|
}
|
|
328
438
|
return {
|
|
329
439
|
id: descriptor.id,
|
|
@@ -335,12 +445,16 @@ function descriptorToPiModel(descriptor) {
|
|
|
335
445
|
cost,
|
|
336
446
|
contextWindow: descriptor.contextWindow,
|
|
337
447
|
maxTokens: descriptor.maxTokens,
|
|
448
|
+
...builtinModel?.thinkingLevelMap !== undefined ? { thinkingLevelMap: builtinModel.thinkingLevelMap } : {},
|
|
449
|
+
...builtinModel?.headers !== undefined ? { headers: builtinModel.headers } : {},
|
|
338
450
|
...compat !== undefined ? { compat } : {}
|
|
339
451
|
};
|
|
340
452
|
}
|
|
341
453
|
|
|
342
454
|
// src/extension.ts
|
|
343
455
|
var PROVIDER_NAME = "plexus";
|
|
456
|
+
var PROVIDER_API_KEY_TEMPLATE = "${PLEXUS_API_KEY}";
|
|
457
|
+
var PLEXUS_CREDENTIAL_EXPIRES_AT = 253402300799000;
|
|
344
458
|
var currentModels = [];
|
|
345
459
|
function plexusExtension(pi) {
|
|
346
460
|
const cached = readCachedModelsSync();
|
|
@@ -352,14 +466,15 @@ function plexusExtension(pi) {
|
|
|
352
466
|
});
|
|
353
467
|
pi.registerProvider(PROVIDER_NAME, {
|
|
354
468
|
api: "openai-completions",
|
|
355
|
-
apiKey:
|
|
469
|
+
apiKey: PROVIDER_API_KEY_TEMPLATE,
|
|
356
470
|
authHeader: true,
|
|
357
471
|
baseUrl: startupBaseUrl,
|
|
358
|
-
models: startupModels
|
|
472
|
+
models: startupModels,
|
|
473
|
+
oauth: createPlexusLoginProvider(pi)
|
|
359
474
|
});
|
|
360
475
|
currentModels = startupModels;
|
|
361
476
|
pi.on("session_start", async (_event, ctx) => {
|
|
362
|
-
const apiKey = await ctx.modelRegistry.authStorage.getApiKey(PROVIDER_NAME);
|
|
477
|
+
const apiKey = await ctx.modelRegistry.authStorage.getApiKey(PROVIDER_NAME) ?? getEnvApiKey();
|
|
363
478
|
const baseUrl = getBaseUrl();
|
|
364
479
|
log("session_start", { hasApiKey: !!apiKey, baseUrl });
|
|
365
480
|
if (!apiKey || !baseUrl) {
|
|
@@ -370,48 +485,62 @@ function plexusExtension(pi) {
|
|
|
370
485
|
await doRefresh(pi, apiKey, ctx, true);
|
|
371
486
|
});
|
|
372
487
|
pi.registerCommand("plexus", {
|
|
373
|
-
description: "Plexus provider commands: login
|
|
488
|
+
description: "Plexus provider commands: refresh (setup: /login plexus)",
|
|
374
489
|
getArgumentCompletions: () => [
|
|
375
|
-
{ value: "login", label: "login", description: "Configure Plexus base URL and API key" },
|
|
376
490
|
{ value: "refresh", label: "refresh", description: "Refresh Plexus models from the API" }
|
|
377
491
|
],
|
|
378
492
|
handler: async (args, ctx) => {
|
|
379
493
|
const sub = args.trim().toLowerCase();
|
|
380
|
-
if (sub === "
|
|
381
|
-
await handleLogin(pi, ctx);
|
|
382
|
-
return;
|
|
383
|
-
}
|
|
384
|
-
if (sub === "refresh") {
|
|
494
|
+
if (sub === "refresh" || sub === "") {
|
|
385
495
|
await handleRefresh(pi, ctx);
|
|
386
496
|
return;
|
|
387
497
|
}
|
|
388
|
-
ctx.ui.notify(`Unknown sub-command: "${args}". Use
|
|
498
|
+
ctx.ui.notify(`Unknown sub-command: "${args}". Use /login plexus for setup or /plexus refresh to refresh models.`, "warning");
|
|
389
499
|
}
|
|
390
500
|
});
|
|
391
501
|
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
502
|
+
function createPlexusLoginProvider(pi) {
|
|
503
|
+
return {
|
|
504
|
+
name: "Plexus",
|
|
505
|
+
async login(callbacks) {
|
|
506
|
+
const baseUrl = (await callbacks.onPrompt({
|
|
507
|
+
message: "Plexus base URL",
|
|
508
|
+
placeholder: "https://plexus.example.com"
|
|
509
|
+
})).trim();
|
|
510
|
+
if (!baseUrl)
|
|
511
|
+
throw new Error("Plexus base URL is required.");
|
|
512
|
+
const apiKey = (await callbacks.onPrompt({ message: "Plexus API key" })).trim();
|
|
513
|
+
if (!apiKey)
|
|
514
|
+
throw new Error("Plexus API key is required.");
|
|
515
|
+
await saveBaseUrl(baseUrl);
|
|
516
|
+
callbacks.onProgress?.("Refreshing Plexus models...");
|
|
517
|
+
await doRefresh(pi, apiKey, null, false);
|
|
518
|
+
return {
|
|
519
|
+
access: apiKey,
|
|
520
|
+
refresh: apiKey,
|
|
521
|
+
expires: PLEXUS_CREDENTIAL_EXPIRES_AT,
|
|
522
|
+
plexusBaseUrl: baseUrl
|
|
523
|
+
};
|
|
524
|
+
},
|
|
525
|
+
async refreshToken(credentials) {
|
|
526
|
+
return { ...credentials, expires: PLEXUS_CREDENTIAL_EXPIRES_AT };
|
|
527
|
+
},
|
|
528
|
+
getApiKey(credentials) {
|
|
529
|
+
return String(credentials.access || credentials.refresh || "");
|
|
530
|
+
},
|
|
531
|
+
modifyModels(models, credentials) {
|
|
532
|
+
const baseUrl = credentials.plexusBaseUrl;
|
|
533
|
+
if (!baseUrl)
|
|
534
|
+
return models;
|
|
535
|
+
const apiBase = baseUrl.trim().replace(/\/+$/, "").endsWith("/v1") ? baseUrl.trim().replace(/\/+$/, "") : `${baseUrl.trim().replace(/\/+$/, "")}/v1`;
|
|
536
|
+
return models.map((model) => model.provider === PROVIDER_NAME ? { ...model, baseUrl: apiBase } : model);
|
|
537
|
+
}
|
|
538
|
+
};
|
|
410
539
|
}
|
|
411
540
|
async function handleRefresh(pi, ctx) {
|
|
412
|
-
const apiKey = await ctx.modelRegistry.authStorage.getApiKey(PROVIDER_NAME);
|
|
541
|
+
const apiKey = await ctx.modelRegistry.authStorage.getApiKey(PROVIDER_NAME) ?? getEnvApiKey();
|
|
413
542
|
if (!apiKey) {
|
|
414
|
-
ctx.ui.notify("No Plexus API key configured. Run /plexus
|
|
543
|
+
ctx.ui.notify("No Plexus API key configured. Run /login plexus first.", "error");
|
|
415
544
|
return;
|
|
416
545
|
}
|
|
417
546
|
ctx.ui.notify("Refreshing Plexus models\u2026", "info");
|
|
@@ -422,7 +551,7 @@ async function doRefresh(pi, apiKey, ctx, setDefault) {
|
|
|
422
551
|
const baseUrl = getBaseUrl();
|
|
423
552
|
if (!modelsUrl || !baseUrl) {
|
|
424
553
|
if (ctx)
|
|
425
|
-
ctx.ui.notify("Plexus base URL not configured. Run /plexus
|
|
554
|
+
ctx.ui.notify("Plexus base URL not configured. Run /login plexus first.", "warning");
|
|
426
555
|
log("doRefresh: no base URL configured");
|
|
427
556
|
return;
|
|
428
557
|
}
|
|
@@ -434,10 +563,11 @@ async function doRefresh(pi, apiKey, ctx, setDefault) {
|
|
|
434
563
|
currentModels = piModels;
|
|
435
564
|
pi.registerProvider(PROVIDER_NAME, {
|
|
436
565
|
api: "openai-completions",
|
|
437
|
-
apiKey:
|
|
566
|
+
apiKey: PROVIDER_API_KEY_TEMPLATE,
|
|
438
567
|
authHeader: true,
|
|
439
568
|
baseUrl,
|
|
440
|
-
models: piModels
|
|
569
|
+
models: piModels,
|
|
570
|
+
oauth: createPlexusLoginProvider(pi)
|
|
441
571
|
});
|
|
442
572
|
log("doRefresh: registered", { count: piModels.length });
|
|
443
573
|
if (ctx)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mcowger/pi-plexus",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Plexus AI proxy plugin for the pi coding agent",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -25,10 +25,12 @@
|
|
|
25
25
|
"build": "bun run build.ts"
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
|
-
"@earendil-works/pi-ai": "
|
|
29
|
-
"@earendil-works/pi-coding-agent": "
|
|
28
|
+
"@earendil-works/pi-ai": ">=0.80.6",
|
|
29
|
+
"@earendil-works/pi-coding-agent": ">=0.80.6"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
+
"@earendil-works/pi-ai": "^0.80.6",
|
|
33
|
+
"@earendil-works/pi-coding-agent": "^0.80.6",
|
|
32
34
|
"@types/bun": "latest"
|
|
33
35
|
},
|
|
34
36
|
"repository": {
|