@mcowger/pi-plexus 0.2.2 → 0.2.4

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
@@ -32,8 +32,9 @@ Once installed, run the login command inside pi:
32
32
  You will be prompted for:
33
33
  1. **Base URL** — the root URL of your Plexus instance, e.g. `https://plexus.example.com` (no `/v1` suffix needed)
34
34
  2. **API key** — your Plexus API key
35
+ 3. **Default model** (optional) — the model ID to use by default, e.g. `glm-5.1` (use the raw model name, not the `plexus/` prefix)
35
36
 
36
- Credentials are stored in `~/.pi/agent/auth.json` (API key, managed by pi) and `~/.pi/agent/extensions/plexus/config.json` (base URL).
37
+ Credentials are stored in `~/.pi/agent/auth.json` (API key, managed by pi) and `~/.pi/agent/extensions/plexus/config.json` (base URL and default model).
37
38
 
38
39
  ### Environment variables
39
40
 
package/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { ExtensionAPI, ProviderConfig } from "@earendil-works/pi-coding-agent";
2
2
  import type { Api } from "@earendil-works/pi-ai";
3
3
  import { readCachedModelsSync, writeCachedModels, writeRawResponse } from "./src/plexus-cache.js";
4
- import { getBaseUrl, getBaseUrlSync, getModelsUrl, saveBaseUrl } from "./src/plexus-config.js";
4
+ import { getBaseUrl, getBaseUrlSync, getDefaultModel, getModelsUrl, saveBaseUrl } from "./src/plexus-config.js";
5
5
  import { log } from "./src/plexus-log.js";
6
6
  import { convertModels, fetchPlexusModels } from "./src/plexus-models.js";
7
7
  import type { PiModel } from "./src/plexus-models.js";
@@ -16,7 +16,10 @@ const createProviderConfig = (models: PiModel[], baseUrl: string): ProviderConfi
16
16
  models,
17
17
  });
18
18
 
19
- const attemptLiveRefresh = async (pi: ExtensionAPI, apiKey: string): Promise<void> => {
19
+ // Store current models globally for use by setDefaultModel
20
+ let currentModels: PiModel[] = [];
21
+
22
+ const attemptLiveRefresh = async (pi: ExtensionAPI, apiKey: string, setDefault = true): Promise<void> => {
20
23
  const baseUrl = getBaseUrl();
21
24
  const modelsUrl = getModelsUrl();
22
25
 
@@ -34,13 +37,47 @@ const attemptLiveRefresh = async (pi: ExtensionAPI, apiKey: string): Promise<voi
34
37
  log("attemptLiveRefresh: fetched models", { count: models.length, sampleModelBaseUrl: models[0]?.baseUrl });
35
38
 
36
39
  await Promise.all([writeCachedModels(models), writeRawResponse(raw)]);
40
+ currentModels = models;
37
41
  pi.registerProvider(PLEXUS_PROVIDER, createProviderConfig(models, baseUrl));
38
42
  log("attemptLiveRefresh: provider registered");
43
+
44
+ // Set default model after registering new provider
45
+ if (setDefault) {
46
+ await setDefaultModel(pi, models);
47
+ }
39
48
  } catch (error) {
40
49
  log("attemptLiveRefresh: fetch failed", { error: String(error) });
41
50
  }
42
51
  };
43
52
 
53
+ const setDefaultModel = async (pi: ExtensionAPI, models?: PiModel[]): Promise<void> => {
54
+ const defaultModelId = getDefaultModel();
55
+ if (!defaultModelId) return;
56
+
57
+ // Use provided models or fall back to currentModels (set during refresh)
58
+ const modelList = models ?? currentModels;
59
+ if (!modelList || modelList.length === 0) {
60
+ log("setDefaultModel: no models available", { defaultModelId });
61
+ return;
62
+ }
63
+
64
+ log("setDefaultModel: searching for model", {
65
+ defaultModelId,
66
+ availableModels: modelList.slice(0, 5).map(m => m.id),
67
+ });
68
+
69
+ // Find model by ID in our list
70
+ const model = modelList.find(m => m.id === defaultModelId);
71
+
72
+ if (!model) {
73
+ log("setDefaultModel: model not found in list", { defaultModelId });
74
+ return;
75
+ }
76
+
77
+ const success = await pi.setModel(model);
78
+ log("setDefaultModel", { defaultModelId, success });
79
+ };
80
+
44
81
  export default function plexusExtension(pi: ExtensionAPI) {
45
82
  const cached = readCachedModelsSync();
46
83
  const startupBaseUrl = getBaseUrlSync() ?? "http://localhost/v1";
@@ -53,6 +90,7 @@ export default function plexusExtension(pi: ExtensionAPI) {
53
90
  });
54
91
 
55
92
  pi.registerProvider(PLEXUS_PROVIDER, createProviderConfig(startupModels, startupBaseUrl));
93
+ currentModels = startupModels;
56
94
 
57
95
  pi.on("session_start", async (_event, ctx) => {
58
96
  const apiKey = await ctx.modelRegistry.authStorage.getApiKey(PLEXUS_PROVIDER);
@@ -62,10 +100,12 @@ export default function plexusExtension(pi: ExtensionAPI) {
62
100
 
63
101
  if (!apiKey || !baseUrl) {
64
102
  log("session_start: no auth configured, skipping refresh");
103
+ // Still try to set default model even without auth
104
+ await setDefaultModel(pi, startupModels);
65
105
  return;
66
106
  }
67
107
 
68
- await attemptLiveRefresh(pi, apiKey);
108
+ await attemptLiveRefresh(pi, apiKey, true);
69
109
  });
70
110
 
71
111
  pi.registerCommand("plexus", {
@@ -84,13 +124,19 @@ export default function plexusExtension(pi: ExtensionAPI) {
84
124
  const apiKey = await ctx.ui.input("Plexus API key");
85
125
  if (!apiKey) return;
86
126
 
87
- await saveBaseUrl(baseUrl.trim());
127
+ // Prompt for default model (optional)
128
+ const defaultModel = await ctx.ui.input("Default model (optional)", "");
129
+ const defaultModelTrimmed = defaultModel?.trim() || undefined;
130
+
131
+ await saveBaseUrl(baseUrl.trim(), defaultModelTrimmed);
88
132
  ctx.modelRegistry.authStorage.set(PLEXUS_PROVIDER, { type: "api_key", key: apiKey.trim() });
89
133
 
90
- log("login: saved", { baseUrl: baseUrl.trim() });
134
+ log("login: saved", { baseUrl: baseUrl.trim(), defaultModel: defaultModelTrimmed });
91
135
  ctx.ui.notify("Plexus credentials saved", "info");
92
136
 
93
- await attemptLiveRefresh(pi, apiKey.trim());
137
+ await attemptLiveRefresh(pi, apiKey.trim(), false);
138
+
139
+ // Set default model after refresh (attemptLiveRefresh handles it when setDefault=true)
94
140
  return;
95
141
  }
96
142
 
@@ -120,6 +166,9 @@ export default function plexusExtension(pi: ExtensionAPI) {
120
166
 
121
167
  log("refresh command: done", { count: models.length });
122
168
  ctx.ui.notify(`Refreshed ${models.length} Plexus models`, "info");
169
+
170
+ // Re-apply default model after refresh
171
+ await setDefaultModel(pi, currentModels);
123
172
  } catch (error) {
124
173
  log("refresh command: failed", { error: String(error) });
125
174
  ctx.ui.notify(`Refresh failed: ${error instanceof Error ? error.message : String(error)}`, "error");
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mcowger/pi-plexus",
3
3
  "author": "Matt Cowger",
4
- "version": "0.2.2",
4
+ "version": "0.2.4",
5
5
  "description": "Plexus AI provider extension package for pi",
6
6
  "license": "MIT",
7
7
  "type": "module",
@@ -22,7 +22,7 @@
22
22
  "LICENSE"
23
23
  ],
24
24
  "scripts": {
25
- "postinstall": "lefthook install",
25
+ "postinstall": "command -v lefthook >/dev/null 2>&1 && lefthook install || true",
26
26
  "check": "bunx tsc --noEmit",
27
27
  "test": "bun test",
28
28
  "format": "biome format --write .",
@@ -31,12 +31,12 @@
31
31
  "pack:dry-run": "bun pack --dry-run"
32
32
  },
33
33
  "devDependencies": {
34
- "@biomejs/biome": "2.4.14",
35
- "lefthook": "2.1.6"
34
+ "@biomejs/biome": "2.4.15",
35
+ "lefthook": "2.1.8"
36
36
  },
37
37
  "peerDependencies": {
38
- "@earendil-works/pi-ai": "0.74.0",
39
- "@earendil-works/pi-coding-agent": "0.74.0"
38
+ "@earendil-works/pi-ai": "0.75.4",
39
+ "@earendil-works/pi-coding-agent": "0.75.4"
40
40
  },
41
41
  "pi": {
42
42
  "extensions": [
@@ -8,34 +8,51 @@ const getConfigPath = (): string => join(getConfigDir(), "config.json");
8
8
 
9
9
  interface PlexusConfig {
10
10
  baseUrl?: string;
11
+ defaultModel?: string;
11
12
  }
12
13
 
13
14
  const normalizeRoot = (raw: string): string => raw.replace(/\/+$/, "");
14
15
 
15
- export const saveBaseUrl = async (baseUrl: string): Promise<void> => {
16
+ export const getConfigSync = (): PlexusConfig => {
17
+ try {
18
+ if (existsSync(getConfigPath())) {
19
+ return JSON.parse(readFileSync(getConfigPath(), "utf8")) as PlexusConfig;
20
+ }
21
+ } catch {}
22
+ return {};
23
+ };
24
+
25
+ export const saveBaseUrl = async (baseUrl: string, defaultModel?: string): Promise<void> => {
16
26
  await mkdir(getConfigDir(), { recursive: true });
17
- const config: PlexusConfig = { baseUrl: normalizeRoot(baseUrl) };
27
+ const existing = getConfigSync();
28
+ const config: PlexusConfig = {
29
+ ...existing,
30
+ baseUrl: normalizeRoot(baseUrl),
31
+ ...(defaultModel !== undefined && { defaultModel }),
32
+ };
18
33
  await writeFile(getConfigPath(), `${JSON.stringify(config, null, 2)}\n`, "utf8");
19
34
  };
20
35
 
21
36
  export const getRawBaseUrl = (): string | null => {
22
- try {
23
- if (existsSync(getConfigPath())) {
24
- const config = JSON.parse(readFileSync(getConfigPath(), "utf8")) as PlexusConfig;
25
- if (config.baseUrl) return config.baseUrl;
26
- }
27
- } catch {}
37
+ const config = getConfigSync();
38
+ if (config.baseUrl) return config.baseUrl;
28
39
  return process.env.PLEXUS_BASE_URL ?? null;
29
40
  };
30
41
 
31
- export const getBaseUrl = (): string | null => {
42
+ export const getModelsUrl = (): string | null => {
32
43
  const raw = getRawBaseUrl();
33
- return raw ? `${normalizeRoot(raw)}/v1` : null;
44
+ return raw ? `${normalizeRoot(raw)}/v1/models` : null;
34
45
  };
35
46
 
36
- export const getModelsUrl = (): string | null => {
47
+ export const getBaseUrlSync = (): string | null => {
37
48
  const raw = getRawBaseUrl();
38
- return raw ? `${normalizeRoot(raw)}/v1/models` : null;
49
+ return raw ? `${normalizeRoot(raw)}/v1` : null;
39
50
  };
40
51
 
41
- export const getBaseUrlSync = (): string | null => getBaseUrl();
52
+ // Alias for backward compatibility
53
+ export const getBaseUrl = getBaseUrlSync;
54
+
55
+ export const getDefaultModel = (): string | null => {
56
+ const config = getConfigSync();
57
+ return config.defaultModel ?? null;
58
+ };