feat: add Pi management interface

This commit is contained in:
2026-08-05 05:53:28 +02:00
parent 4422568f61
commit 7a8bcacbfe
6 changed files with 606 additions and 10 deletions
+82
View File
@@ -0,0 +1,82 @@
import { ApiError, apiFetch } from "./client";
export type PiReasoning = "low" | "medium" | "high";
export interface PiInstallationConfig {
provider: string;
model: string;
reasoning: PiReasoning;
}
export interface PiManagementStatus {
version?: string;
ready: boolean;
config: Partial<PiInstallationConfig>;
checkedAt: string;
message?: string;
}
export interface PiManagementOptions {
providers: string[];
models: Array<{ provider: string; id: string }>;
reasoning: PiReasoning[];
checkedAt: string;
}
export interface PiManagementTestResult {
ready: boolean;
checkedAt: string;
message?: string;
}
export interface PiManagementLogs {
lines: string[];
checkedAt: string;
}
export type PiManagementApiErrorCode =
| "pi_management_forbidden"
| "pi_management_unavailable"
| "pi_management_invalid_config"
| "pi_management_write_failed";
export interface PiManagementApiError {
status: number;
code: PiManagementApiErrorCode;
message: string;
}
const errorCodes = new Set<PiManagementApiErrorCode>([
"pi_management_forbidden",
"pi_management_unavailable",
"pi_management_invalid_config",
"pi_management_write_failed",
]);
function object(value: unknown): Record<string, unknown> | undefined {
return value && typeof value === "object" && !Array.isArray(value)
? value as Record<string, unknown>
: undefined;
}
/** Narrows the sanctioned error payload without ever surfacing its raw response body. */
export function asPiManagementApiError(error: unknown): PiManagementApiError | undefined {
if (!(error instanceof ApiError)) return undefined;
const payload = object(error.payload);
const code = payload?.code;
const message = payload?.error;
if (typeof code !== "string" || !errorCodes.has(code as PiManagementApiErrorCode) || typeof message !== "string") {
return undefined;
}
return { status: error.status, code: code as PiManagementApiErrorCode, message };
}
export const getPiManagementStatus = () => apiFetch<PiManagementStatus>("/pi-management/status");
export const getPiManagementOptions = () => apiFetch<PiManagementOptions>("/pi-management/options");
export const savePiManagementConfig = (config: PiInstallationConfig) =>
apiFetch<PiInstallationConfig & { updatedAt: string }>("/pi-management/config", {
method: "PUT", body: JSON.stringify(config),
});
export const runPiManagementTest = () =>
apiFetch<PiManagementTestResult>("/pi-management/test", { method: "POST" });
export const getPiManagementLogs = () => apiFetch<PiManagementLogs>("/pi-management/logs");