Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Pi: move the model-catalog parser out of pi.ts

MaxWang007420606944bf9
diff --git a/server/drivers/pi-catalog.ts b/server/drivers/pi-catalog.ts
new file mode 100644
index 0000000000..2162f580fb
--- /dev/null
+++ b/server/drivers/pi-catalog.ts
@@ -0,0 +1,46 @@
+import type { ModelCatalog } from "../contracts.ts";
+
+interface PiModelEntry {
+ provider: string;
+ id: string;
+ name?: string;
+ contextWindow?: number;
+}
+
+interface PiModelsResponse {
+ type: "response";
+ command: "get_available_models";
+ success: boolean;
+ data?: { models?: PiModelEntry[] };
+}
+
+export function piCatalogFromFrame(frame: unknown, fallbackDefault: string): ModelCatalog | null {
+ const res = frame as PiModelsResponse;
+ if (res?.type !== "response" || res.command !== "get_available_models" || !res.success) return null;
+ const options: Array<{ id: string; label: string; custom: true; provider: string; contextWindow?: number }> = [];
+ for (const m of res.data?.models ?? []) {
+ if (!m?.provider || !m?.id) continue;
+ const id = `${m.provider}/${m.id}`;
+ const contextWindow = m.contextWindow;
+ options.push({
+ id, label: m.name ?? m.id, custom: true, provider: m.provider,
+ ...(typeof contextWindow === "number" && Number.isFinite(contextWindow) && contextWindow > 0 ? { contextWindow } : {}),
+ });
+ }
+ return { default: fallbackDefault || (options[0]?.id ?? ""), options };
+}
+
+export function parsePiCatalog(stdout: string, fallbackDefault = ""): ModelCatalog {
+ for (const line of stdout.split("\n")) {
+ if (!line.trim()) continue;
+ let msg: unknown;
+ try {
+ msg = JSON.parse(line);
+ } catch {
+ continue;
+ }
+ const catalog = piCatalogFromFrame(msg, fallbackDefault);
+ if (catalog) return catalog;
+ }
+ return { default: fallbackDefault, options: [] };
+}
diff --git a/server/drivers/pi.ts b/server/drivers/pi.ts
index 3c050ea147..640288a278 100644
--- a/server/drivers/pi.ts
+++ b/server/drivers/pi.ts
@@ -52,9 +52,11 @@ import { piSessionDir, piSessionPath, piTurnArgs } from "./pi-session.ts";
import { piAgentDir, readPiDefaultModel, withOwnPiAgentDir } from "./pi-agent-dir.ts";
import { noticeFailure } from "../chat-notices.ts";
+import { piCatalogFromFrame } from "./pi-catalog.ts";
import { decodePiFrame, PiRpcChunks, piLineReader, piNativeLogMessage, readPiPromptImages } from "./pi-rpc.ts";
import { piMessageUsage, PiRunUsage, type PiUsage, type TurnUsage } from "./pi-usage.ts";
export { decodePiFrame, PiRpcChunks, piLineReader } from "./pi-rpc.ts";
+export { parsePiCatalog } from "./pi-catalog.ts";
const DRIVER_KIND = "piAgent";
const PI_ARGS = ["--mode", "rpc", "--no-session"]; // catalog probes only: do not write a session file
@@ -76,51 +78,6 @@ export function piThinkingLevel(effort: EffortLevel): (typeof EFFORT_LEVELS)[num
return effort === "none" ? "off" : effort;
}
-interface PiModelEntry {
- provider: string;
- id: string;
- name?: string;
- contextWindow?: number;
-}
-
-interface PiModelsResponse {
- type: "response";
- command: "get_available_models";
- success: boolean;
- data?: { models?: PiModelEntry[] };
-}
-
-function piCatalogFromFrame(frame: unknown, fallbackDefault: string): ModelCatalog | null {
- const res = frame as PiModelsResponse;
- if (res?.type !== "response" || res.command !== "get_available_models" || !res.success) return null;
- const options: Array<{ id: string; label: string; custom: true; provider: string; contextWindow?: number }> = [];
- for (const m of res.data?.models ?? []) {
- if (!m?.provider || !m?.id) continue;
- const id = `${m.provider}/${m.id}`;
- const contextWindow = m.contextWindow;
- options.push({
- id, label: m.name ?? m.id, custom: true, provider: m.provider,
- ...(typeof contextWindow === "number" && Number.isFinite(contextWindow) && contextWindow > 0 ? { contextWindow } : {}),
- });
- }
- return { default: fallbackDefault || (options[0]?.id ?? ""), options };
-}
-
-export function parsePiCatalog(stdout: string, fallbackDefault = ""): ModelCatalog {
- for (const line of stdout.split("\n")) {
- if (!line.trim()) continue;
- let msg: unknown;
- try {
- msg = JSON.parse(line);
- } catch {
- continue;
- }
- const catalog = piCatalogFromFrame(msg, fallbackDefault);
- if (catalog) return catalog;
- }
- return { default: fallbackDefault, options: [] };
-}
-
export function splitPiModel(id: string): { provider: string; modelId: string } | null {
const inject = decodeInjectId(id);
if (inject) return { provider: inject.host, modelId: inject.model };