@mcowger/pi-plexus 0.2.0 → 0.2.2

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
@@ -57,6 +57,20 @@ On startup, the extension loads a cached model list from `~/.pi/agent/extensions
57
57
 
58
58
  Models are registered under the `plexus` provider and appear in `/model` alongside all other configured providers.
59
59
 
60
+ ### Provider-specific model fidelity via `pi_provider` / `pi_model`
61
+
62
+ Many models require provider-specific compatibility settings (e.g. DeepSeek needs `system` role instead of `developer`, Qwen uses a different thinking format, etc.). pi already knows these settings for built-in providers, but when models are proxied through Plexus, pi can't auto-detect them because the provider and base URL belong to Plexus, not the original provider.
63
+
64
+ To solve this, configure Plexus to expose `pi_provider` and `pi_model` in its `/v1/models` response. When these fields are set, the extension looks up the full model definition from pi's built-in MODELS registry and uses it for maximum fidelity — picking up the correct `api`, `compat`, `thinkingLevelMap`, `reasoning`, `contextWindow`, `maxTokens`, and `input` modalities, all sourced from pi's curated configuration rather than inferred from Plexus fields.
65
+
66
+ In your Plexus model configuration, enable the **pi Provider** and **pi Model** fields:
67
+
68
+ ![Plexus pi model configuration](docs/PlexusPiModel.png)
69
+
70
+ For example, a DeepSeek V4 Flash entry configured with `pi_provider: deepseek` and `pi_model: deepseek-v4-flash` will automatically get the correct compat settings (including `supportsDeveloperRole: false` and `thinkingFormat: deepseek`), ensuring requests are formatted correctly for DeepSeek even though they're routed through Plexus.
71
+
72
+ If `pi_provider` / `pi_model` are not set for a model, the extension falls back to inferring settings from the Plexus API fields (preferred_api, supported_parameters, architecture, etc.) — which works fine for simpler models but won't include provider-specific compat overrides.
73
+
60
74
  ## Troubleshooting
61
75
 
62
76
  The extension writes a debug log to `~/.pi/agent/extensions/plexus/plexus.log`. If models are missing or requests are failing, check that file first:
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.0",
4
+ "version": "0.2.2",
5
5
  "description": "Plexus AI provider extension package for pi",
6
6
  "license": "MIT",
7
7
  "type": "module",
@@ -1,4 +1,11 @@
1
- import type { Api, Model } from "@earendil-works/pi-ai";
1
+ import { getModel } from "@earendil-works/pi-ai";
2
+ import type {
3
+ Api,
4
+ Model,
5
+ OpenAICompletionsCompat,
6
+ OpenAIResponsesCompat,
7
+ AnthropicMessagesCompat,
8
+ } from "@earendil-works/pi-ai";
2
9
 
3
10
  export interface PlexusModelArchitecture {
4
11
  modality?: string;
@@ -34,6 +41,9 @@ export interface PlexusApiModel {
34
41
  pricing?: PlexusModelPricing;
35
42
  supported_parameters?: string[];
36
43
  top_provider?: PlexusTopProvider;
44
+ /** When set, look up the full model definition from pi's MODELS for maximum fidelity. */
45
+ pi_provider?: string;
46
+ pi_model?: string;
37
47
  }
38
48
 
39
49
  export interface PlexusApiResponse {
@@ -87,17 +97,183 @@ const inferReasoning = (supported_parameters: string[] | undefined): boolean =>
87
97
  return supported_parameters.some((p) => REASONING_PARAMS.has(p));
88
98
  };
89
99
 
100
+ /**
101
+ * Attempt to look up a model definition from pi's built-in MODELS registry
102
+ * using the pi_provider and pi_model hints from the plexus API.
103
+ * Returns null if either hint is missing or the model isn't found.
104
+ */
105
+ const lookupPiModel = (piProvider?: string, piModel?: string): Model<Api> | null => {
106
+ if (!piProvider || !piModel) return null;
107
+
108
+ // pi_provider/pi_model are dynamic strings from the plexus API,
109
+ // so we cast to bypass the branded type constraints on getModel.
110
+ // biome-ignore lint/suspicious/noExplicitAny: dynamic string args don't satisfy generic constraints
111
+ return getModel(piProvider as any, piModel as any) ?? null;
112
+ };
113
+
114
+ /**
115
+ * Detect OpenAI completions compat settings from the provider and baseUrl of a
116
+ * pi model definition. This mirrors the internal detectCompat() in pi's
117
+ * openai-completions provider so that we can produce a fully-resolved compat
118
+ * object for plexus-proxied models.
119
+ *
120
+ * Without this, providers like deepseek that are proxied through plexus won't
121
+ * get the correct implied compat (e.g. supportsDeveloperRole: false) because
122
+ * pi's runtime auto-detection checks model.provider and model.baseUrl, which
123
+ * are "plexus" and the plexus URL — not the original provider.
124
+ */
125
+ const detectOpenAICompletionsCompat = (piModel: Model<Api>): OpenAICompletionsCompat => {
126
+ const provider = piModel.provider;
127
+ const baseUrl = piModel.baseUrl;
128
+
129
+ const isZai = provider === "zai" || baseUrl.includes("api.z.ai");
130
+ const isMoonshot = provider === "moonshotai" || provider === "moonshotai-cn" || baseUrl.includes("api.moonshot.");
131
+ const isCloudflareWorkersAI = provider === "cloudflare-workers-ai" || baseUrl.includes("api.cloudflare.com");
132
+ const isCloudflareAiGateway = provider === "cloudflare-ai-gateway" || baseUrl.includes("gateway.ai.cloudflare.com");
133
+ const isNonStandard =
134
+ provider === "cerebras" ||
135
+ baseUrl.includes("cerebras.ai") ||
136
+ provider === "xai" ||
137
+ baseUrl.includes("api.x.ai") ||
138
+ baseUrl.includes("chutes.ai") ||
139
+ baseUrl.includes("deepseek.com") ||
140
+ isZai ||
141
+ isMoonshot ||
142
+ provider === "opencode" ||
143
+ baseUrl.includes("opencode.ai") ||
144
+ isCloudflareWorkersAI ||
145
+ isCloudflareAiGateway;
146
+ const useMaxTokens = baseUrl.includes("chutes.ai") || isMoonshot || isCloudflareAiGateway;
147
+ const isGrok = provider === "xai" || baseUrl.includes("api.x.ai");
148
+ const isDeepSeek = provider === "deepseek" || baseUrl.includes("deepseek.com");
149
+ const cacheControlFormat = provider === "openrouter" && piModel.id.startsWith("anthropic/") ? "anthropic" : undefined;
150
+
151
+ const detected: OpenAICompletionsCompat = {
152
+ supportsStore: !isNonStandard,
153
+ supportsDeveloperRole: !isNonStandard,
154
+ supportsReasoningEffort: !isGrok && !isZai && !isMoonshot && !isCloudflareAiGateway,
155
+ supportsUsageInStreaming: true,
156
+ maxTokensField: useMaxTokens ? "max_tokens" : "max_completion_tokens",
157
+ requiresToolResultName: false,
158
+ requiresAssistantAfterToolResult: false,
159
+ requiresThinkingAsText: false,
160
+ requiresReasoningContentOnAssistantMessages: isDeepSeek,
161
+ thinkingFormat: isDeepSeek
162
+ ? "deepseek"
163
+ : isZai
164
+ ? "zai"
165
+ : provider === "openrouter" || baseUrl.includes("openrouter.ai")
166
+ ? "openrouter"
167
+ : "openai",
168
+ openRouterRouting: {},
169
+ vercelGatewayRouting: {},
170
+ zaiToolStream: false,
171
+ supportsStrictMode: !isMoonshot && !isCloudflareAiGateway,
172
+ cacheControlFormat,
173
+ sendSessionAffinityHeaders: false,
174
+ supportsLongCacheRetention: !(isCloudflareWorkersAI || isCloudflareAiGateway),
175
+ };
176
+
177
+ return detected;
178
+ };
179
+
180
+ /**
181
+ * Resolve a fully-specified compat object for a plexus-proxied model by
182
+ * merging the pi model's stored compat overrides onto the detected defaults
183
+ * from the pi model's original provider/baseUrl.
184
+ *
185
+ * This produces the same result that pi's getCompat() would compute for the
186
+ * original model, ensuring that implied compat fields (like
187
+ * supportsDeveloperRole: false for deepseek) are correct even though the
188
+ * plexus model has provider="plexus" and a plexus baseUrl.
189
+ */
190
+ const resolveCompat = (
191
+ piModel: Model<Api>,
192
+ ): OpenAICompletionsCompat | OpenAIResponsesCompat | AnthropicMessagesCompat | undefined => {
193
+ if (piModel.api === "openai-completions") {
194
+ const detected = detectOpenAICompletionsCompat(piModel);
195
+ const overrides = piModel.compat as OpenAICompletionsCompat | undefined;
196
+ if (!overrides) return detected;
197
+ return {
198
+ supportsStore: overrides.supportsStore ?? detected.supportsStore,
199
+ supportsDeveloperRole: overrides.supportsDeveloperRole ?? detected.supportsDeveloperRole,
200
+ supportsReasoningEffort: overrides.supportsReasoningEffort ?? detected.supportsReasoningEffort,
201
+ supportsUsageInStreaming: overrides.supportsUsageInStreaming ?? detected.supportsUsageInStreaming,
202
+ maxTokensField: overrides.maxTokensField ?? detected.maxTokensField,
203
+ requiresToolResultName: overrides.requiresToolResultName ?? detected.requiresToolResultName,
204
+ requiresAssistantAfterToolResult:
205
+ overrides.requiresAssistantAfterToolResult ?? detected.requiresAssistantAfterToolResult,
206
+ requiresThinkingAsText: overrides.requiresThinkingAsText ?? detected.requiresThinkingAsText,
207
+ requiresReasoningContentOnAssistantMessages:
208
+ overrides.requiresReasoningContentOnAssistantMessages ?? detected.requiresReasoningContentOnAssistantMessages,
209
+ thinkingFormat: overrides.thinkingFormat ?? detected.thinkingFormat,
210
+ openRouterRouting: overrides.openRouterRouting ?? detected.openRouterRouting,
211
+ vercelGatewayRouting: overrides.vercelGatewayRouting ?? detected.vercelGatewayRouting,
212
+ zaiToolStream: overrides.zaiToolStream ?? detected.zaiToolStream,
213
+ supportsStrictMode: overrides.supportsStrictMode ?? detected.supportsStrictMode,
214
+ cacheControlFormat: overrides.cacheControlFormat ?? detected.cacheControlFormat,
215
+ sendSessionAffinityHeaders: overrides.sendSessionAffinityHeaders ?? detected.sendSessionAffinityHeaders,
216
+ supportsLongCacheRetention: overrides.supportsLongCacheRetention ?? detected.supportsLongCacheRetention,
217
+ };
218
+ }
219
+
220
+ // For other APIs, fall back to the stored compat as-is.
221
+ // At runtime, pi's provider will still merge with detected defaults,
222
+ // but the plexus baseUrl won't match any known provider patterns,
223
+ // so defaults are generally safe for anthropic-messages and openai-responses.
224
+ return piModel.compat;
225
+ };
226
+
227
+ const adjustBaseUrl = (baseUrl: string, api: Api): string => {
228
+ if (api === "anthropic-messages") {
229
+ return baseUrl.replace(/\/v1\/?$/, "");
230
+ }
231
+ if (api === "google-generative-ai") {
232
+ return baseUrl.replace(/\/v1\/?$/, "/v1beta");
233
+ }
234
+ return baseUrl;
235
+ };
236
+
90
237
  export const convertToPiModel = (apiModel: PlexusApiModel, baseUrl: string): PiModel => {
238
+ const piModelDef = lookupPiModel(apiModel.pi_provider, apiModel.pi_model);
239
+
240
+ // When pi's MODELS has this model, use its curated definition for maximum fidelity,
241
+ // overriding with plexus-specific fields (id, provider, baseUrl, cost from plexus pricing).
242
+ if (piModelDef) {
243
+ const resolvedCompat = resolveCompat(piModelDef);
244
+ return {
245
+ id: apiModel.id,
246
+ name: apiModel.name ?? piModelDef.name ?? apiModel.id,
247
+ api: piModelDef.api,
248
+ provider: "plexus",
249
+ baseUrl: adjustBaseUrl(baseUrl, piModelDef.api),
250
+ reasoning: piModelDef.reasoning,
251
+ ...(piModelDef.thinkingLevelMap && { thinkingLevelMap: piModelDef.thinkingLevelMap }),
252
+ input: piModelDef.input,
253
+ cost: {
254
+ input: parsePrice(apiModel.pricing?.prompt) || piModelDef.cost.input,
255
+ output: parsePrice(apiModel.pricing?.completion) || piModelDef.cost.output,
256
+ cacheRead: parsePrice(apiModel.pricing?.input_cache_read) || piModelDef.cost.cacheRead,
257
+ cacheWrite: parsePrice(apiModel.pricing?.input_cache_write) || piModelDef.cost.cacheWrite,
258
+ },
259
+ contextWindow: piModelDef.contextWindow,
260
+ maxTokens: piModelDef.maxTokens,
261
+ ...(resolvedCompat && { compat: resolvedCompat }),
262
+ };
263
+ }
264
+
265
+ // No pi MODELS match – fall back to parsing the plexus API fields.
91
266
  const contextWindow = apiModel.context_length ?? apiModel.top_provider?.context_length ?? DEFAULT_CONTEXT_WINDOW;
92
267
 
93
268
  const maxTokens = apiModel.top_provider?.max_completion_tokens ?? contextWindow;
269
+ const fallbackApi = mapPreferredApi(apiModel.preferred_api);
94
270
 
95
271
  return {
96
272
  id: apiModel.id,
97
273
  name: apiModel.name ?? apiModel.id,
98
- api: mapPreferredApi(apiModel.preferred_api),
274
+ api: fallbackApi,
99
275
  provider: "plexus",
100
- baseUrl,
276
+ baseUrl: adjustBaseUrl(baseUrl, fallbackApi),
101
277
  reasoning: inferReasoning(apiModel.supported_parameters),
102
278
  input: mapInputModalities(apiModel.architecture),
103
279
  cost: {