@mcowger/pi-plexus 0.2.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 +66 -0
- package/index.ts +133 -0
- package/package.json +46 -0
- package/src/plexus-cache.ts +52 -0
- package/src/plexus-config.ts +41 -0
- package/src/plexus-log.ts +17 -0
- package/src/plexus-models.ts +142 -0
package/README.md
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# pi-plexus
|
|
2
|
+
|
|
3
|
+
A [pi](https://github.com/earendil-works/pi-mono) extension that adds [Plexus](https://github.com/mcowger/plexus) as a provider, dynamically discovering models from your Plexus instance.
|
|
4
|
+
|
|
5
|
+
## Requirements
|
|
6
|
+
|
|
7
|
+
- pi `0.74.0` or later
|
|
8
|
+
- A running Plexus instance with a valid API key
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
### Via npm (recommended)
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pi install npm:@mcowger/pi-plexus
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
### Via git
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pi install git:github.com/mcowger/pi-plexus
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Configuration
|
|
25
|
+
|
|
26
|
+
Once installed, run the login command inside pi:
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
/plexus login
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
You will be prompted for:
|
|
33
|
+
1. **Base URL** — the root URL of your Plexus instance, e.g. `https://plexus.example.com` (no `/v1` suffix needed)
|
|
34
|
+
2. **API key** — your Plexus API key
|
|
35
|
+
|
|
36
|
+
Credentials are stored in `~/.pi/agent/auth.json` (API key, managed by pi) and `~/.pi/agent/extensions/plexus/config.json` (base URL).
|
|
37
|
+
|
|
38
|
+
### Environment variables
|
|
39
|
+
|
|
40
|
+
As an alternative to `/plexus login`, you can set environment variables before starting pi:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
export PLEXUS_BASE_URL=https://plexus.example.com
|
|
44
|
+
export PLEXUS_API_KEY=sk-...
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Commands
|
|
48
|
+
|
|
49
|
+
| Command | Description |
|
|
50
|
+
|---|---|
|
|
51
|
+
| `/plexus login` | Configure base URL and API key |
|
|
52
|
+
| `/plexus refresh` | Re-fetch the model list from your Plexus instance and update the local cache |
|
|
53
|
+
|
|
54
|
+
## How it works
|
|
55
|
+
|
|
56
|
+
On startup, the extension loads a cached model list from `~/.pi/agent/extensions/plexus/plexus-models-cache.json` so models are available immediately. On each session start it attempts a live refresh from `/v1/models` to pick up any new or removed models.
|
|
57
|
+
|
|
58
|
+
Models are registered under the `plexus` provider and appear in `/model` alongside all other configured providers.
|
|
59
|
+
|
|
60
|
+
## Troubleshooting
|
|
61
|
+
|
|
62
|
+
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:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
cat ~/.pi/agent/extensions/plexus/plexus.log
|
|
66
|
+
```
|
package/index.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import type { ExtensionAPI, ProviderConfig } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { Api } from "@earendil-works/pi-ai";
|
|
3
|
+
import { readCachedModelsSync, writeCachedModels, writeRawResponse } from "./src/plexus-cache.js";
|
|
4
|
+
import { getBaseUrl, getBaseUrlSync, getModelsUrl, saveBaseUrl } from "./src/plexus-config.js";
|
|
5
|
+
import { log } from "./src/plexus-log.js";
|
|
6
|
+
import { convertModels, fetchPlexusModels } from "./src/plexus-models.js";
|
|
7
|
+
import type { PiModel } from "./src/plexus-models.js";
|
|
8
|
+
|
|
9
|
+
const PLEXUS_PROVIDER = "plexus";
|
|
10
|
+
|
|
11
|
+
const createProviderConfig = (models: PiModel[], baseUrl: string): ProviderConfig => ({
|
|
12
|
+
api: "openai-completions" as Api,
|
|
13
|
+
apiKey: "PLEXUS_API_KEY",
|
|
14
|
+
authHeader: true,
|
|
15
|
+
baseUrl,
|
|
16
|
+
models,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const attemptLiveRefresh = async (pi: ExtensionAPI, apiKey: string): Promise<void> => {
|
|
20
|
+
const baseUrl = getBaseUrl();
|
|
21
|
+
const modelsUrl = getModelsUrl();
|
|
22
|
+
|
|
23
|
+
log("attemptLiveRefresh", { hasApiKey: !!apiKey, baseUrl, modelsUrl });
|
|
24
|
+
|
|
25
|
+
if (!baseUrl || !modelsUrl) {
|
|
26
|
+
log("attemptLiveRefresh: missing baseUrl, skipping");
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
const { models: apiModels, raw } = await fetchPlexusModels(apiKey, modelsUrl);
|
|
32
|
+
const models = convertModels(apiModels, baseUrl);
|
|
33
|
+
|
|
34
|
+
log("attemptLiveRefresh: fetched models", { count: models.length, sampleModelBaseUrl: models[0]?.baseUrl });
|
|
35
|
+
|
|
36
|
+
await Promise.all([writeCachedModels(models), writeRawResponse(raw)]);
|
|
37
|
+
pi.registerProvider(PLEXUS_PROVIDER, createProviderConfig(models, baseUrl));
|
|
38
|
+
log("attemptLiveRefresh: provider registered");
|
|
39
|
+
} catch (error) {
|
|
40
|
+
log("attemptLiveRefresh: fetch failed", { error: String(error) });
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export default function plexusExtension(pi: ExtensionAPI) {
|
|
45
|
+
const cached = readCachedModelsSync();
|
|
46
|
+
const startupBaseUrl = getBaseUrlSync() ?? "http://localhost/v1";
|
|
47
|
+
const startupModels = cached?.models ?? [];
|
|
48
|
+
|
|
49
|
+
log("startup", {
|
|
50
|
+
cachedModelCount: startupModels.length,
|
|
51
|
+
startupBaseUrl,
|
|
52
|
+
sampleCachedModelBaseUrl: startupModels[0]?.baseUrl,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
pi.registerProvider(PLEXUS_PROVIDER, createProviderConfig(startupModels, startupBaseUrl));
|
|
56
|
+
|
|
57
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
58
|
+
const apiKey = await ctx.modelRegistry.authStorage.getApiKey(PLEXUS_PROVIDER);
|
|
59
|
+
const baseUrl = getBaseUrl();
|
|
60
|
+
|
|
61
|
+
log("session_start", { hasApiKey: !!apiKey, baseUrl });
|
|
62
|
+
|
|
63
|
+
if (!apiKey || !baseUrl) {
|
|
64
|
+
log("session_start: no auth configured, skipping refresh");
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
await attemptLiveRefresh(pi, apiKey);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
pi.registerCommand("plexus", {
|
|
72
|
+
description: "Plexus provider commands: login, refresh",
|
|
73
|
+
getArgumentCompletions: () => [
|
|
74
|
+
{ value: "login", label: "login", description: "Configure Plexus base URL and API key" },
|
|
75
|
+
{ value: "refresh", label: "refresh", description: "Refresh Plexus models from the API" },
|
|
76
|
+
],
|
|
77
|
+
handler: async (args, ctx) => {
|
|
78
|
+
const sub = args.trim();
|
|
79
|
+
|
|
80
|
+
if (sub === "login") {
|
|
81
|
+
const baseUrl = await ctx.ui.input("Plexus base URL", "https://plexus.example.com");
|
|
82
|
+
if (!baseUrl) return;
|
|
83
|
+
|
|
84
|
+
const apiKey = await ctx.ui.input("Plexus API key");
|
|
85
|
+
if (!apiKey) return;
|
|
86
|
+
|
|
87
|
+
await saveBaseUrl(baseUrl.trim());
|
|
88
|
+
ctx.modelRegistry.authStorage.set(PLEXUS_PROVIDER, { type: "api_key", key: apiKey.trim() });
|
|
89
|
+
|
|
90
|
+
log("login: saved", { baseUrl: baseUrl.trim() });
|
|
91
|
+
ctx.ui.notify("Plexus credentials saved", "info");
|
|
92
|
+
|
|
93
|
+
await attemptLiveRefresh(pi, apiKey.trim());
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (sub === "refresh") {
|
|
98
|
+
const apiKey = await ctx.modelRegistry.authStorage.getApiKey(PLEXUS_PROVIDER);
|
|
99
|
+
const baseUrl = getBaseUrl();
|
|
100
|
+
const modelsUrl = getModelsUrl();
|
|
101
|
+
|
|
102
|
+
log("refresh command", { hasApiKey: !!apiKey, baseUrl });
|
|
103
|
+
|
|
104
|
+
if (!apiKey) {
|
|
105
|
+
ctx.ui.notify("No Plexus API key configured. Run /plexus login first.", "error");
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (!baseUrl || !modelsUrl) {
|
|
109
|
+
ctx.ui.notify("No Plexus base URL configured. Run /plexus login first.", "error");
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
ctx.ui.notify("Fetching Plexus models...", "info");
|
|
115
|
+
const { models: apiModels, raw } = await fetchPlexusModels(apiKey, modelsUrl);
|
|
116
|
+
const models = convertModels(apiModels, baseUrl);
|
|
117
|
+
|
|
118
|
+
await Promise.all([writeCachedModels(models), writeRawResponse(raw)]);
|
|
119
|
+
pi.registerProvider(PLEXUS_PROVIDER, createProviderConfig(models, baseUrl));
|
|
120
|
+
|
|
121
|
+
log("refresh command: done", { count: models.length });
|
|
122
|
+
ctx.ui.notify(`Refreshed ${models.length} Plexus models`, "info");
|
|
123
|
+
} catch (error) {
|
|
124
|
+
log("refresh command: failed", { error: String(error) });
|
|
125
|
+
ctx.ui.notify(`Refresh failed: ${error instanceof Error ? error.message : String(error)}`, "error");
|
|
126
|
+
}
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
ctx.ui.notify("Usage: /plexus login | /plexus refresh", "info");
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mcowger/pi-plexus",
|
|
3
|
+
"author": "Matt Cowger",
|
|
4
|
+
"version": "0.2.0",
|
|
5
|
+
"description": "Plexus AI provider extension package for pi",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"pi-package",
|
|
10
|
+
"pi",
|
|
11
|
+
"pi-extension",
|
|
12
|
+
"plexus",
|
|
13
|
+
"provider"
|
|
14
|
+
],
|
|
15
|
+
"engines": {
|
|
16
|
+
"bun": ">=1.0"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"index.ts",
|
|
20
|
+
"src/",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"postinstall": "lefthook install",
|
|
26
|
+
"check": "bunx tsc --noEmit",
|
|
27
|
+
"test": "bun test",
|
|
28
|
+
"format": "biome format --write .",
|
|
29
|
+
"format:check": "biome format .",
|
|
30
|
+
"lint": "biome lint .",
|
|
31
|
+
"pack:dry-run": "bun pack --dry-run"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@biomejs/biome": "2.4.14",
|
|
35
|
+
"lefthook": "2.1.6"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@earendil-works/pi-ai": "0.74.0",
|
|
39
|
+
"@earendil-works/pi-coding-agent": "0.74.0"
|
|
40
|
+
},
|
|
41
|
+
"pi": {
|
|
42
|
+
"extensions": [
|
|
43
|
+
"./index.ts"
|
|
44
|
+
]
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import type { PlexusApiResponse } from "./plexus-models.js";
|
|
6
|
+
import type { PiModel } from "./plexus-models.js";
|
|
7
|
+
|
|
8
|
+
const getCacheDir = (): string => join(getAgentDir(), "extensions", "plexus");
|
|
9
|
+
|
|
10
|
+
const getModelsCachePath = (): string => join(getCacheDir(), "plexus-models-cache.json");
|
|
11
|
+
|
|
12
|
+
const getResponseCachePath = (): string => join(getCacheDir(), "plexus-models-response.json");
|
|
13
|
+
|
|
14
|
+
interface ModelCache {
|
|
15
|
+
models: PiModel[];
|
|
16
|
+
timestamp: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const readCachedModels = async (): Promise<ModelCache | null> => {
|
|
20
|
+
try {
|
|
21
|
+
const content = await readFile(getModelsCachePath(), "utf8");
|
|
22
|
+
const cache = JSON.parse(content) as ModelCache;
|
|
23
|
+
if (!Array.isArray(cache.models)) return null;
|
|
24
|
+
return cache;
|
|
25
|
+
} catch {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const readCachedModelsSync = (): ModelCache | null => {
|
|
31
|
+
try {
|
|
32
|
+
const path = getModelsCachePath();
|
|
33
|
+
if (!existsSync(path)) return null;
|
|
34
|
+
const content = readFileSync(path, "utf8");
|
|
35
|
+
const cache = JSON.parse(content) as ModelCache;
|
|
36
|
+
if (!Array.isArray(cache.models)) return null;
|
|
37
|
+
return cache;
|
|
38
|
+
} catch {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const writeCachedModels = async (models: PiModel[]): Promise<void> => {
|
|
44
|
+
const cache: ModelCache = { models, timestamp: Date.now() };
|
|
45
|
+
await mkdir(getCacheDir(), { recursive: true });
|
|
46
|
+
await writeFile(getModelsCachePath(), `${JSON.stringify(cache, null, 2)}\n`, "utf8");
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const writeRawResponse = async (data: PlexusApiResponse): Promise<void> => {
|
|
50
|
+
await mkdir(getCacheDir(), { recursive: true });
|
|
51
|
+
await writeFile(getResponseCachePath(), `${JSON.stringify(data, null, 2)}\n`, "utf8");
|
|
52
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
|
|
6
|
+
const getConfigDir = (): string => join(getAgentDir(), "extensions", "plexus");
|
|
7
|
+
const getConfigPath = (): string => join(getConfigDir(), "config.json");
|
|
8
|
+
|
|
9
|
+
interface PlexusConfig {
|
|
10
|
+
baseUrl?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const normalizeRoot = (raw: string): string => raw.replace(/\/+$/, "");
|
|
14
|
+
|
|
15
|
+
export const saveBaseUrl = async (baseUrl: string): Promise<void> => {
|
|
16
|
+
await mkdir(getConfigDir(), { recursive: true });
|
|
17
|
+
const config: PlexusConfig = { baseUrl: normalizeRoot(baseUrl) };
|
|
18
|
+
await writeFile(getConfigPath(), `${JSON.stringify(config, null, 2)}\n`, "utf8");
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
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 {}
|
|
28
|
+
return process.env.PLEXUS_BASE_URL ?? null;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const getBaseUrl = (): string | null => {
|
|
32
|
+
const raw = getRawBaseUrl();
|
|
33
|
+
return raw ? `${normalizeRoot(raw)}/v1` : null;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const getModelsUrl = (): string | null => {
|
|
37
|
+
const raw = getRawBaseUrl();
|
|
38
|
+
return raw ? `${normalizeRoot(raw)}/v1/models` : null;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const getBaseUrlSync = (): string | null => getBaseUrl();
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { appendFileSync, mkdirSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
|
|
5
|
+
const getLogPath = (): string => join(getAgentDir(), "extensions", "plexus", "plexus.log");
|
|
6
|
+
|
|
7
|
+
const getCacheDir = (): string => join(getAgentDir(), "extensions", "plexus");
|
|
8
|
+
|
|
9
|
+
export const log = (message: string, data?: Record<string, unknown>): void => {
|
|
10
|
+
try {
|
|
11
|
+
mkdirSync(getCacheDir(), { recursive: true });
|
|
12
|
+
const line = `${new Date().toISOString()} ${message}${data ? ` ${JSON.stringify(data)}` : ""}\n`;
|
|
13
|
+
appendFileSync(getLogPath(), line, "utf8");
|
|
14
|
+
} catch {
|
|
15
|
+
// never throw from logging
|
|
16
|
+
}
|
|
17
|
+
};
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import type { Api, Model } from "@earendil-works/pi-ai";
|
|
2
|
+
|
|
3
|
+
export interface PlexusModelArchitecture {
|
|
4
|
+
modality?: string;
|
|
5
|
+
input_modalities?: string[];
|
|
6
|
+
output_modalities?: string[];
|
|
7
|
+
tokenizer?: string;
|
|
8
|
+
instruct_type?: string | null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface PlexusModelPricing {
|
|
12
|
+
prompt?: string;
|
|
13
|
+
completion?: string;
|
|
14
|
+
input_cache_read?: string;
|
|
15
|
+
input_cache_write?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface PlexusTopProvider {
|
|
19
|
+
context_length?: number | null;
|
|
20
|
+
max_completion_tokens?: number | null;
|
|
21
|
+
is_moderated?: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface PlexusApiModel {
|
|
25
|
+
id: string;
|
|
26
|
+
object?: string;
|
|
27
|
+
created?: number;
|
|
28
|
+
owned_by?: string;
|
|
29
|
+
preferred_api?: string | string[];
|
|
30
|
+
name?: string;
|
|
31
|
+
description?: string;
|
|
32
|
+
context_length?: number | null;
|
|
33
|
+
architecture?: PlexusModelArchitecture;
|
|
34
|
+
pricing?: PlexusModelPricing;
|
|
35
|
+
supported_parameters?: string[];
|
|
36
|
+
top_provider?: PlexusTopProvider;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface PlexusApiResponse {
|
|
40
|
+
object: string;
|
|
41
|
+
data: PlexusApiModel[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type PiModel = Model<Api>;
|
|
45
|
+
|
|
46
|
+
const DEFAULT_CONTEXT_WINDOW = 8192;
|
|
47
|
+
const REASONING_PARAMS = new Set(["reasoning", "include_reasoning", "reasoning_effort"]);
|
|
48
|
+
|
|
49
|
+
const parsePrice = (value: string | undefined): number => {
|
|
50
|
+
if (!value) return 0;
|
|
51
|
+
const parsed = parseFloat(value);
|
|
52
|
+
return Number.isNaN(parsed) ? 0 : parsed;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const mapPreferredApi = (preferred_api: string | string[] | undefined): Api => {
|
|
56
|
+
const SUPPORTED_MAP: Record<string, Api> = {
|
|
57
|
+
chat_completions: "openai-completions",
|
|
58
|
+
messages: "anthropic-messages",
|
|
59
|
+
gemini: "google-generative-ai",
|
|
60
|
+
responses: "openai-responses",
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
if (!preferred_api) return "openai-completions";
|
|
64
|
+
|
|
65
|
+
const candidates = Array.isArray(preferred_api) ? preferred_api : [preferred_api];
|
|
66
|
+
|
|
67
|
+
for (const candidate of candidates) {
|
|
68
|
+
const mapped = SUPPORTED_MAP[candidate];
|
|
69
|
+
if (mapped) return mapped;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return "openai-completions";
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const mapInputModalities = (architecture: PlexusModelArchitecture | undefined): ("text" | "image")[] => {
|
|
76
|
+
const modalities = architecture?.input_modalities ?? [];
|
|
77
|
+
const result: ("text" | "image")[] = [];
|
|
78
|
+
|
|
79
|
+
if (modalities.includes("text")) result.push("text");
|
|
80
|
+
if (modalities.includes("image")) result.push("image");
|
|
81
|
+
|
|
82
|
+
return result.length > 0 ? result : ["text"];
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const inferReasoning = (supported_parameters: string[] | undefined): boolean => {
|
|
86
|
+
if (!supported_parameters) return false;
|
|
87
|
+
return supported_parameters.some((p) => REASONING_PARAMS.has(p));
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export const convertToPiModel = (apiModel: PlexusApiModel, baseUrl: string): PiModel => {
|
|
91
|
+
const contextWindow = apiModel.context_length ?? apiModel.top_provider?.context_length ?? DEFAULT_CONTEXT_WINDOW;
|
|
92
|
+
|
|
93
|
+
const maxTokens = apiModel.top_provider?.max_completion_tokens ?? contextWindow;
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
id: apiModel.id,
|
|
97
|
+
name: apiModel.name ?? apiModel.id,
|
|
98
|
+
api: mapPreferredApi(apiModel.preferred_api),
|
|
99
|
+
provider: "plexus",
|
|
100
|
+
baseUrl,
|
|
101
|
+
reasoning: inferReasoning(apiModel.supported_parameters),
|
|
102
|
+
input: mapInputModalities(apiModel.architecture),
|
|
103
|
+
cost: {
|
|
104
|
+
input: parsePrice(apiModel.pricing?.prompt),
|
|
105
|
+
output: parsePrice(apiModel.pricing?.completion),
|
|
106
|
+
cacheRead: parsePrice(apiModel.pricing?.input_cache_read),
|
|
107
|
+
cacheWrite: parsePrice(apiModel.pricing?.input_cache_write),
|
|
108
|
+
},
|
|
109
|
+
contextWindow,
|
|
110
|
+
maxTokens,
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export const fetchPlexusModels = async (
|
|
115
|
+
apiKey: string,
|
|
116
|
+
modelsUrl: string,
|
|
117
|
+
): Promise<{ models: PlexusApiModel[]; raw: PlexusApiResponse }> => {
|
|
118
|
+
const response = await fetch(modelsUrl, {
|
|
119
|
+
headers: {
|
|
120
|
+
Accept: "application/json",
|
|
121
|
+
Authorization: `Bearer ${apiKey}`,
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
if (!response.ok) {
|
|
126
|
+
throw new Error(`Plexus API error: ${response.status} ${response.statusText}`);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const raw = (await response.json()) as PlexusApiResponse;
|
|
130
|
+
return { models: raw.data ?? [], raw };
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
export const convertModels = (apiModels: PlexusApiModel[], baseUrl: string): PiModel[] => {
|
|
134
|
+
const result: PiModel[] = [];
|
|
135
|
+
|
|
136
|
+
for (const apiModel of apiModels) {
|
|
137
|
+
if (!apiModel.id) continue;
|
|
138
|
+
result.push(convertToPiModel(apiModel, baseUrl));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return result;
|
|
142
|
+
};
|