feat(setup): add native Pi client support (#1376)
* feat(setup): add native Pi client support * fix(setup): isolate invalid Pi agent directory overrides Keep malformed Pi path evidence on its client entry so unrelated selected clients can complete setup and doctor. Preserve explicit Pi rejection and block filesystem operations rather than falling back to another directory. * fix(setup): parse Pi configuration as strict JSON Pi's installed MCP loader reads mcp.json with a bare JSON.parse and no BOM tolerance, so comments, trailing commas, a BOM, or an empty existing file yield loader errors and no servers. Parsing the Pi dialect with objectSchema.parse(JSON.parse(text)) before the shared empty/JSONC shortcut rejects those files at every mutation boundary instead of reporting configured registrations Pi cannot load. A missing file still initializes a fresh strict-JSON document, other dialects keep their JSONC/BOM/empty tolerance, and malformed files are never rewritten. --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
co-authored by
morluto
parent
f970b886aa
commit
9e22da845b
@@ -99,6 +99,10 @@ const parseDocument = (
|
||||
): Record<string, unknown> => {
|
||||
if (format === "toml" || format === "grok")
|
||||
return objectSchema.parse(parseToml(text));
|
||||
// Pi loads `mcp.json` with a strict `JSON.parse` and no BOM tolerance, so
|
||||
// comments, trailing commas, a BOM, or an empty existing file must be
|
||||
// rejected instead of registering into a document Pi cannot load.
|
||||
if (format === "pi") return objectSchema.parse(JSON.parse(text));
|
||||
// Hermes stores MCP servers in `mcp_servers` inside a YAML document.
|
||||
if (format === "hermes") return objectSchema.parse(parseYaml(text));
|
||||
// An empty file, which some clients create before any server is added,
|
||||
@@ -1369,6 +1373,7 @@ export const clientRegistrationEntry = (
|
||||
};
|
||||
case "vscode":
|
||||
case "omp":
|
||||
case "pi":
|
||||
return {
|
||||
type: "stdio",
|
||||
command: executable,
|
||||
|
||||
@@ -80,6 +80,16 @@ export const readClientRegistrationStatuses = async (
|
||||
options.platform,
|
||||
options.environment,
|
||||
)) {
|
||||
if (client.configPathError !== undefined) {
|
||||
statuses.push({
|
||||
client: client.name,
|
||||
config_path: client.configPath,
|
||||
command: [],
|
||||
state: "invalid",
|
||||
remediation: client.configPathError,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
!(await exists(client.markerPath)) &&
|
||||
!(await exists(client.configPath))
|
||||
@@ -166,9 +176,9 @@ const registrationAligned = (
|
||||
)
|
||||
return false;
|
||||
if (client.format === "vscode" && registration.type !== "stdio") return false;
|
||||
// OMP infers stdio for a command entry without an explicit type.
|
||||
// OMP and Pi infer stdio for a command entry without an explicit type.
|
||||
if (
|
||||
client.format === "omp" &&
|
||||
(client.format === "omp" || client.format === "pi") &&
|
||||
registration.type !== undefined &&
|
||||
registration.type !== "stdio"
|
||||
)
|
||||
@@ -230,7 +240,7 @@ const parseRegistration = (
|
||||
if (client.format === "commandcode" && registration.transport !== "stdio")
|
||||
throw new TypeError("Expected an stdio registration");
|
||||
if (
|
||||
client.format === "omp" &&
|
||||
(client.format === "omp" || client.format === "pi") &&
|
||||
registration.type !== undefined &&
|
||||
registration.type !== "stdio"
|
||||
)
|
||||
|
||||
@@ -33,6 +33,8 @@ export const configureClientConfiguration = (
|
||||
environment: SetupProviderEnvironment = {},
|
||||
command: readonly string[] = npxRegistrationCommand(),
|
||||
): Promise<ClientConfigurationResult> => {
|
||||
if (client.configPathError !== undefined)
|
||||
return Promise.resolve({ status: "failed", reason: "path" });
|
||||
if (client.format === undefined || client.format === "unsupported")
|
||||
return Promise.resolve({ status: "failed", reason: "readback" });
|
||||
return configureClientDocument(client, environment, command, client.format);
|
||||
@@ -154,6 +156,7 @@ export const clientConfigurationAligned = async (
|
||||
providerEnvironment: SetupProviderEnvironment,
|
||||
command: readonly string[],
|
||||
): Promise<boolean> => {
|
||||
if (client.configPathError !== undefined) return false;
|
||||
try {
|
||||
const original = await readFile(client.configPath, "utf8");
|
||||
const parsed = parseClientConfiguration(original, client.format);
|
||||
@@ -179,6 +182,8 @@ export const inspectClientConfiguration = async (
|
||||
providerEnvironment: SetupProviderEnvironment,
|
||||
command: readonly string[],
|
||||
): Promise<ClientConfigurationInspection> => {
|
||||
if (client.configPathError !== undefined)
|
||||
return { status: "invalid", remediation: client.configPathError };
|
||||
if (client.format === "unsupported") return { status: "already_current" };
|
||||
const transactionPath = await resolveClientConfigTransactionPath(
|
||||
client.configPath,
|
||||
|
||||
@@ -167,6 +167,7 @@ export const detectClients = async (
|
||||
): Promise<readonly SetupClient[]> => {
|
||||
const detected: SetupClient[] = [];
|
||||
for (const candidate of supportedClients(home, platform, environment)) {
|
||||
if (candidate.configPathError !== undefined) continue;
|
||||
const [hasConfig, hasMarker] = await Promise.all([
|
||||
exists(candidate.configPath),
|
||||
candidate.markerPath === undefined ? false : exists(candidate.markerPath),
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { isAbsolute, join, resolve } from "node:path";
|
||||
import { isAbsolute, join, resolve, win32 } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { lstatSync } from "node:fs";
|
||||
|
||||
/** One supported client configuration location. */
|
||||
@@ -6,6 +7,8 @@ export interface SetupClient {
|
||||
readonly name: string;
|
||||
readonly displayName?: string;
|
||||
readonly configPath: string;
|
||||
/** Unresolved path evidence only; consumers must not perform I/O on it. */
|
||||
readonly configPathError?: string;
|
||||
readonly markerPath?: string;
|
||||
readonly format?:
|
||||
| "json"
|
||||
@@ -16,6 +19,7 @@ export interface SetupClient {
|
||||
| "commandcode"
|
||||
| "grok"
|
||||
| "omp"
|
||||
| "pi"
|
||||
| "hermes"
|
||||
| "unsupported";
|
||||
}
|
||||
@@ -197,6 +201,41 @@ const ompAgentDirectory = (context: ClientPathContext): string => {
|
||||
: join(root, "agent");
|
||||
};
|
||||
|
||||
/**
|
||||
* Pi's public getAgentDir()/normalizePath semantics, not OMP's profiles.
|
||||
* Keep relative overrides relative: filesystem consumers resolve them in cwd.
|
||||
*/
|
||||
const piAgentDirectory = ({
|
||||
home,
|
||||
platform,
|
||||
env,
|
||||
}: ClientPathContext): string => {
|
||||
const paths = platform === "win32" ? win32 : { join };
|
||||
let directory = env.PI_CODING_AGENT_DIR;
|
||||
if (!directory) return paths.join(home, ".pi", "agent");
|
||||
if (
|
||||
platform === "win32" &&
|
||||
directory.startsWith("/") &&
|
||||
!directory.startsWith("//") &&
|
||||
!directory.includes("\\")
|
||||
) {
|
||||
const drive = /^\/(?:mnt\/|cygdrive\/)?([a-z])(?:\/(.*))?$/iu.exec(
|
||||
directory,
|
||||
);
|
||||
if (drive !== null)
|
||||
directory = `${drive[1]?.toUpperCase()}:\\${drive[2]?.replaceAll("/", "\\") ?? ""}`;
|
||||
}
|
||||
if (directory === "~") return home;
|
||||
if (
|
||||
directory.startsWith("~/") ||
|
||||
(platform === "win32" && directory.startsWith("~\\"))
|
||||
)
|
||||
return paths.join(home, directory.slice(2));
|
||||
if (directory.startsWith("file://"))
|
||||
return fileURLToPath(directory, { windows: platform === "win32" });
|
||||
return directory;
|
||||
};
|
||||
|
||||
const copilotDirectory = ({ home, env }: ClientPathContext): string =>
|
||||
env.COPILOT_HOME ?? join(home, ".copilot");
|
||||
|
||||
@@ -358,6 +397,17 @@ export const SUPPORTED_CLIENT_DEFINITIONS = [
|
||||
markerPath: ompAgentDirectory,
|
||||
format: "omp",
|
||||
},
|
||||
{
|
||||
name: "pi",
|
||||
displayName: "Pi",
|
||||
configPath: (context: ClientPathContext) =>
|
||||
(context.platform === "win32" ? win32 : { join }).join(
|
||||
piAgentDirectory(context),
|
||||
"mcp.json",
|
||||
),
|
||||
markerPath: piAgentDirectory,
|
||||
format: "pi",
|
||||
},
|
||||
{
|
||||
name: "hermes",
|
||||
displayName: "Hermes",
|
||||
@@ -387,13 +437,26 @@ export const supportedClients = (
|
||||
env: ClientPathContext["env"] = process.env,
|
||||
): readonly SetupClient[] => {
|
||||
const context = { home, platform, env };
|
||||
return SUPPORTED_CLIENT_DEFINITIONS.map((definition) => ({
|
||||
name: definition.name,
|
||||
displayName: definition.displayName,
|
||||
configPath: resolvePath(definition.configPath, context),
|
||||
markerPath: resolvePath(definition.markerPath, context),
|
||||
format: definition.format,
|
||||
}));
|
||||
return SUPPORTED_CLIENT_DEFINITIONS.map((definition) => {
|
||||
try {
|
||||
return {
|
||||
name: definition.name,
|
||||
displayName: definition.displayName,
|
||||
configPath: resolvePath(definition.configPath, context),
|
||||
markerPath: resolvePath(definition.markerPath, context),
|
||||
format: definition.format,
|
||||
};
|
||||
} catch (cause: unknown) {
|
||||
if (definition.name !== "pi") throw cause;
|
||||
return {
|
||||
name: definition.name,
|
||||
displayName: definition.displayName,
|
||||
format: definition.format,
|
||||
configPath: env.PI_CODING_AGENT_DIR ?? "",
|
||||
configPathError: `Invalid PI_CODING_AGENT_DIR: ${cause instanceof Error ? cause.message : String(cause)}. Repair the override before configuring Pi.`,
|
||||
};
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/** Resolve personal skill roots using the same client environment as setup. */
|
||||
|
||||
@@ -201,6 +201,8 @@ const readClientConfiguration = async (
|
||||
client: SetupClient,
|
||||
fileSystem: UninstallFileSystem,
|
||||
): Promise<ClientConfigurationRead> => {
|
||||
if (client.configPathError !== undefined)
|
||||
return itemRead(item(client.name, "failed", client.configPathError));
|
||||
if (client.format === "unsupported")
|
||||
return itemRead(
|
||||
item(
|
||||
|
||||
Reference in New Issue
Block a user