Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
/** Shared identifiers and connection defaults for first-class provider presets. */
import type { CatalogApiStyle, DiscoveryStyle } from "./model-catalog.js";
export const OPENCODE_GO_API_STYLE = "opencode_go" as const;
export const OPENCODE_GO_NAME = "OpenCode Go";
export const OPENCODE_GO_BASE_URL = "https://opencode.ai/zen/go/v1";
export type NamedEndpointPreset = {
id: string;
/** models.dev provider key persisted as `vendorKey`. */
vendorKey: string;
name: string;
baseUrl: string;
apiStyle: CatalogApiStyle;
/**
* Model-list style this preset's deployment documents, when it differs from
* the one `apiStyle` implies. Only a documented statement belongs here; a
* derivation from `apiStyle` must not override the probe style.
*/
discoveryStyle?: DiscoveryStyle;
/** i18n key under `settings`. */
labelKey: string;
aliases?: readonly string[];
/** Completions thinking/tool-stream flags for Zhipu / Z.AI hosts. */
zhipuCompat?: boolean;
};
export const NAMED_ENDPOINT_PRESETS: readonly NamedEndpointPreset[] = [
{
id: "openai",
vendorKey: "openai",
name: "OpenAI",
baseUrl: "https://api.openai.com/v1",
apiStyle: "responses",
labelKey: "settings.presetOpenai",
},
{
id: "anthropic",
vendorKey: "anthropic",
name: "Anthropic",
baseUrl: "https://api.anthropic.com",
apiStyle: "anthropic_messages",
labelKey: "settings.presetAnthropic",
},
{
id: "google",
vendorKey: "google",
name: "Google Gemini",
baseUrl: "https://generativelanguage.googleapis.com/v1beta",
apiStyle: "google_generative_ai",
labelKey: "settings.presetGoogle",
aliases: ["gemini"],
},
{
id: "openrouter",
vendorKey: "openrouter",
name: "OpenRouter",
baseUrl: "https://openrouter.ai/api/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetOpenrouter",
},
{
id: "groq",
vendorKey: "groq",
name: "Groq",
baseUrl: "https://api.groq.com/openai/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetGroq",
},
{
id: "xai",
vendorKey: "xai",
name: "xAI",
baseUrl: "https://api.x.ai/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetXai",
},
{
id: "mistral",
vendorKey: "mistral",
name: "Mistral",
baseUrl: "https://api.mistral.ai/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetMistral",
},
{
id: "togetherai",
vendorKey: "togetherai",
name: "Together AI",
baseUrl: "https://api.together.xyz/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetTogether",
aliases: ["together"],
},
{
id: "fireworks-ai",
vendorKey: "fireworks-ai",
name: "Fireworks",
baseUrl: "https://api.fireworks.ai/inference/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetFireworks",
aliases: ["fireworks"],
},
{
id: OPENCODE_GO_API_STYLE,
vendorKey: "opencode-go",
name: OPENCODE_GO_NAME,
baseUrl: OPENCODE_GO_BASE_URL,
apiStyle: OPENCODE_GO_API_STYLE,
labelKey: "settings.apiStyleOpenCodeGo",
},
{
id: "zai",
vendorKey: "zai",
name: "Z.AI",
baseUrl: "https://api.z.ai/api/paas/v4",
apiStyle: "chat_completions",
labelKey: "settings.presetZaiApi",
zhipuCompat: true,
},
{
id: "zai-coding-plan",
vendorKey: "zai-coding-plan",
name: "Z.AI Coding Plan",
baseUrl: "https://api.z.ai/api/coding/paas/v4",
apiStyle: "chat_completions",
labelKey: "settings.presetZaiCodingPlan",
zhipuCompat: true,
},
{
id: "deepseek",
vendorKey: "deepseek",
name: "DeepSeek",
baseUrl: "https://api.deepseek.com",
apiStyle: "chat_completions",
labelKey: "settings.presetDeepseek",
},
{
id: "alibaba-cn",
vendorKey: "alibaba-cn",
name: "Alibaba (China)",
baseUrl: "https://dashscope.aliyuncs.com/compatible-mode/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetAlibabaCn",
aliases: ["dashscope", "qwen"],
},
{
id: "moonshotai-cn",
vendorKey: "moonshotai-cn",
name: "Moonshot AI (China)",
baseUrl: "https://api.moonshot.cn/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetMoonshotCn",
aliases: ["moonshot"],
},
{
id: "zhipuai",
vendorKey: "zhipuai",
name: "Zhipu AI",
baseUrl: "https://open.bigmodel.cn/api/paas/v4",
apiStyle: "chat_completions",
labelKey: "settings.presetZhipuApi",
aliases: ["zhipu", "bigmodel"],
zhipuCompat: true,
},
{
id: "zhipuai-coding-plan",
vendorKey: "zhipuai-coding-plan",
name: "Zhipu AI Coding Plan",
baseUrl: "https://open.bigmodel.cn/api/coding/paas/v4",
apiStyle: "chat_completions",
labelKey: "settings.presetZhipuCodingPlan",
aliases: ["zai-coding-cn"],
zhipuCompat: true,
},
{
id: "siliconflow-cn",
vendorKey: "siliconflow-cn",
name: "SiliconFlow (China)",
baseUrl: "https://api.siliconflow.cn/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetSiliconflowCn",
},
{
id: "volcengine",
vendorKey: "volcengine",
name: "Volcengine Ark",
baseUrl: "https://ark.cn-beijing.volces.com/api/v3",
apiStyle: "chat_completions",
labelKey: "settings.presetVolcengine",
aliases: ["doubao", "ark"],
},
{
id: "minimax-cn",
vendorKey: "minimax-cn",
name: "MiniMax",
baseUrl: "https://api.minimaxi.com/anthropic/v1",
apiStyle: "anthropic_messages",
labelKey: "settings.presetMinimaxCn",
aliases: ["minimax"],
},
{
id: "minimax-cn-openai",
vendorKey: "minimax-cn",
name: "MiniMax (OpenAI)",
baseUrl: "https://api.minimaxi.com/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetMinimaxCnOpenai",
aliases: ["minimax-openai", "minimax-compatible"],
},
{
id: "stepfun-plan",
vendorKey: "stepfun-step-plan",
name: "StepFun Plan",
baseUrl: "https://api.stepfun.com/step_plan/v1",
apiStyle: "anthropic_messages",
// Subscription plan: generation on /messages, model list with a Bearer token.
discoveryStyle: "openai_models",
labelKey: "settings.presetStepfunPlan",
},
{
id: "xiaomi",
vendorKey: "xiaomi",
name: "Xiaomi",
baseUrl: "https://api.xiaomimimo.com/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetXiaomi",
aliases: ["mimo", "xiaomimimo"],
},
{
id: "kimi-for-coding",
vendorKey: "kimi-for-coding",
name: "Kimi For Coding",
baseUrl: "https://api.kimi.com/coding/v1",
apiStyle: "anthropic_messages",
labelKey: "settings.presetKimiCoding",
aliases: ["kimi-coding", "kimi"],
},
{
id: "ant-ling",
vendorKey: "ant-ling",
name: "Ant Ling",
baseUrl: "https://api.ant-ling.com/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetAntLing",
},
{
id: "baseten",
vendorKey: "baseten",
name: "Baseten",
baseUrl: "https://inference.baseten.co/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetBaseten",
},
{
id: "cerebras",
vendorKey: "cerebras",
name: "Cerebras",
baseUrl: "https://api.cerebras.ai/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetCerebras",
},
{
id: "huggingface",
vendorKey: "huggingface",
name: "Hugging Face",
baseUrl: "https://router.huggingface.co/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetHuggingface",
aliases: ["hugging-face", "hf"],
},
{
id: "meta",
vendorKey: "meta",
name: "Meta",
baseUrl: "https://api.meta.ai/v1",
apiStyle: "responses",
labelKey: "settings.presetMeta",
},
{
id: "minimax",
vendorKey: "minimax",
name: "MiniMax (International)",
baseUrl: "https://api.minimax.io/anthropic/v1",
apiStyle: "anthropic_messages",
labelKey: "settings.presetMinimaxIntl",
},
{
id: "moonshotai",
vendorKey: "moonshotai",
name: "Moonshot AI (International)",
baseUrl: "https://api.moonshot.ai/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetMoonshotIntl",
},
{
id: "nvidia",
vendorKey: "nvidia",
name: "NVIDIA",
baseUrl: "https://integrate.api.nvidia.com/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetNvidia",
aliases: ["nim"],
},
{
id: "opencode",
vendorKey: "opencode",
name: "OpenCode Zen",
baseUrl: "https://opencode.ai/zen/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetOpenCodeZen",
aliases: ["opencode-zen"],
},
{
id: "vercel",
vendorKey: "vercel",
name: "Vercel AI Gateway",
baseUrl: "https://ai-gateway.vercel.sh/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetVercelGateway",
aliases: ["vercel-ai-gateway"],
},
{
id: "alibaba-token-plan",
vendorKey: "alibaba-token-plan",
name: "Qwen Token Plan",
baseUrl: "https://token-plan.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetAlibabaTokenPlan",
aliases: ["qwen-token-plan", "qwen-token-plan-individual"],
},
{
id: "alibaba-token-plan-cn",
vendorKey: "alibaba-token-plan-cn",
name: "Qwen Token Plan (China)",
baseUrl: "https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetAlibabaTokenPlanCn",
aliases: ["qwen-token-plan-cn", "qwen-token-plan-individual-cn"],
},
{
id: "xiaomi-token-plan-cn",
vendorKey: "xiaomi-token-plan-cn",
name: "Xiaomi Token Plan (China)",
baseUrl: "https://token-plan-cn.xiaomimimo.com/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetXiaomiTokenPlanCn",
},
{
id: "xiaomi-token-plan-ams",
vendorKey: "xiaomi-token-plan-ams",
name: "Xiaomi Token Plan (Europe)",
baseUrl: "https://token-plan-ams.xiaomimimo.com/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetXiaomiTokenPlanAms",
},
{
id: "xiaomi-token-plan-sgp",
vendorKey: "xiaomi-token-plan-sgp",
name: "Xiaomi Token Plan (Singapore)",
baseUrl: "https://token-plan-sgp.xiaomimimo.com/v1",
apiStyle: "chat_completions",
labelKey: "settings.presetXiaomiTokenPlanSgp",
},
];
/** Canonical form of a configured endpoint for preset matching. */
export function normalizeEndpointUrl(value: string | undefined): string | undefined {
const raw = value?.trim();
if (!raw) return undefined;
try {
const parsed = new URL(raw);
const pathname = parsed.pathname.replace(/\/+$/, "");
return `${parsed.protocol.toLowerCase()}//${parsed.host.toLowerCase()}${pathname}`;
} catch {
return raw.replace(/\/+$/, "").toLowerCase();
}
}
function normalizedVendorKey(value: string | undefined): string {
return (value ?? "").trim().toLowerCase().replace(/_/g, "-");
}
function endpointHost(value: string | undefined): string | undefined {
const normalized = normalizeEndpointUrl(value);
if (!normalized) return undefined;
try {
return new URL(normalized).host.toLowerCase();
} catch {
return undefined;
}
}
/**
* Whether a configured base URL addresses a vendor's own published endpoint.
*
* Matching is by host, not by the full canonical URL: an OpenAI-compatible
* vendor is routinely configured with a path the preset record does not spell
* out (`/v1`, a Coding-Plan suffix, a regional host) and that is still the
* vendor's host. A row pointed anywhere else belongs to the user — a
* self-hosted gateway or a relay — and the runtime decides its request shape.
*/
export function addressesPublishedVendorHost(baseUrl: string | undefined): boolean {
const host = endpointHost(baseUrl);
if (!host) return false;
return NAMED_ENDPOINT_PRESETS.some((preset) => endpointHost(preset.baseUrl) === host);
}
function presetByUrl(url: string): NamedEndpointPreset | undefined {
const exact = NAMED_ENDPOINT_PRESETS.find(
(preset) => normalizeEndpointUrl(preset.baseUrl) === url,
);
if (exact) return exact;
if (url.includes("open.bigmodel.cn")) {
return NAMED_ENDPOINT_PRESETS.find((preset) =>
preset.id === (url.includes("/coding/") ? "zhipuai-coding-plan" : "zhipuai"),
);
}
if (url.includes("api.z.ai")) {
return NAMED_ENDPOINT_PRESETS.find((preset) =>
preset.id === (url.includes("/coding/") ? "zai-coding-plan" : "zai"),
);
}
return undefined;
}
/**
* Resolve a named endpoint from a stored row.
*
* URL wins when it addresses a published host, so API vs Coding Plan cannot
* be catalog-matched as the wrong record.
*/
export function matchNamedPreset(input: {
vendorKey?: string;
baseUrl?: string;
apiStyle?: string;
}): NamedEndpointPreset | undefined {
const url = normalizeEndpointUrl(input.baseUrl);
if (url) {
const byUrl = presetByUrl(url);
if (byUrl) return byUrl;
}
if (input.apiStyle === OPENCODE_GO_API_STYLE) {
return NAMED_ENDPOINT_PRESETS.find((preset) => preset.id === OPENCODE_GO_API_STYLE);
}
const key = normalizedVendorKey(input.vendorKey);
if (!key || key === "custom") return undefined;
return NAMED_ENDPOINT_PRESETS.find(
(preset) => preset.vendorKey === key || preset.aliases?.includes(key) || preset.id === key,
);
}
export const ZHIPU_ENDPOINT_PRESETS = NAMED_ENDPOINT_PRESETS.filter(
(preset) => preset.zhipuCompat,
);
export function matchZhipuPreset(input: {
vendorKey?: string;
baseUrl?: string;
}): NamedEndpointPreset | undefined {
const matched = matchNamedPreset(input);
return matched?.zhipuCompat ? matched : undefined;
}
export function isZhipuEndpoint(input: {
vendorKey?: string;
baseUrl?: string;
}): boolean {
return matchZhipuPreset(input) !== undefined;
}
/** pi-ai Completions flags for Zhipu / Z.AI thinking and tool streaming. */
export function zhipuRequestCompat(input: {
vendorKey?: string;
baseUrl?: string;
}): { thinkingFormat: "zai"; zaiToolStream: true } | undefined {
return isZhipuEndpoint(input)
? { thinkingFormat: "zai", zaiToolStream: true }
: undefined;
}
function isMistralHostname(baseUrl: string | undefined): boolean {
const raw = baseUrl?.trim();
if (!raw) return false;
try {
const hostname = new URL(raw).hostname.toLowerCase();
return hostname === "mistral.ai" || hostname.endsWith(".mistral.ai");
} catch {
return false;
}
}
/** Whether a configured row targets Mistral's Chat Completions endpoint. */
export function isMistralEndpoint(input: {
vendorKey?: string;
baseUrl?: string;
}): boolean {
if (isMistralHostname(input.baseUrl)) return true;
if (addressesPublishedVendorHost(input.baseUrl)) return false;
return matchNamedPreset(input)?.id === "mistral";
}
/**
* Mistral's Chat Completions endpoint strictly rejects the OpenAI-only `store`
* field with HTTP 422 (`extra_forbidden`, #2539). pi-ai's `openai-completions`
* auto-detection does not classify Mistral as non-standard, so suppress `store`
* explicitly here.
*/
export function mistralRequestCompat(input: {
vendorKey?: string;
baseUrl?: string;
}): { supportsStore: false } | undefined {
return isMistralEndpoint(input) ? { supportsStore: false } : undefined;
}
function mentionsDeepSeek(value: string | undefined): boolean {
return (value ?? "").toLowerCase().includes("deepseek");
}
/**
* DeepSeek thinking mode requires every replayed assistant message to carry a
* reasoning field. Official `deepseek.com` endpoints accept `""` for turns that
* produced no thinking (#223 / D389). OpenCode and third-party relays for the
* same model family reject empty echoes and require a non-empty value (#296).
* pi-ai auto-detects only `provider === "deepseek"` or a `deepseek.com` URL;
* PI-Desktop stores a UUID as `model.provider`, so aggregators and custom
* gateways never match. Detect the family from vendorKey, URL, model id, or
* catalog family without changing `thinkingFormat`.
*/
export function isDeepSeekReasoningReplay(input: {
vendorKey?: string;
baseUrl?: string;
modelId?: string;
family?: string;
}): boolean {
const key = normalizedVendorKey(input.vendorKey);
if (key.includes("deepseek")) return true;
if ((input.baseUrl ?? "").toLowerCase().includes("deepseek.com")) return true;
return mentionsDeepSeek(input.modelId) || mentionsDeepSeek(input.family);
}
/** Official DeepSeek Completions hosts that still accept empty-string replay. */
export function isOfficialDeepSeekEndpoint(input: { baseUrl?: string }): boolean {
const baseUrl = input.baseUrl?.trim();
if (!baseUrl) return false;
try {
const hostname = new URL(baseUrl).hostname.toLowerCase();
return hostname === "deepseek.com" || hostname.endsWith(".deepseek.com");
} catch {
return false;
}
}
/**
* Documented non-empty stand-in when a strict DeepSeek-compatible relay requires
* reasoning replay but the turn's real thinking was never retained (compaction
* summary, synthetic bridge assistants, or thinking-less turns). Must match the
* literal embedded in patches/@[email protected].
*/
export const DEEPSEEK_REASONING_REPLAY_PLACEHOLDER =
"[reasoning not retained for this turn]";
/**
* The placeholder a model echoed back as the start of its own reasoning (and
* its observed `[reasoning retained for this turn]` rewrite), repeated or not.
*/
const ECHOED_REASONING_REPLAY_PLACEHOLDER =
/^(?:\s*\[reasoning (?:not )?retained for this turn\])+\s*/;
/**
* Thinking without a leading echo of {@link DEEPSEEK_REASONING_REPLAY_PLACEHOLDER}.
* Models read the replayed placeholder as their previous reasoning and
* continued it, so persisted thinking can start with it (#1945). It is not
* reasoning: it is neither shown nor replayed. Stored text is not rewritten.
*/
export function withoutReasoningReplayPlaceholder(thinking: string): string {
return thinking.replace(ECHOED_REASONING_REPLAY_PLACEHOLDER, "");
}
export type DeepSeekRequestCompat = {
requiresReasoningContentOnAssistantMessages: true;
/**
* Relays reported rejecting the OpenAI-style `reasoning_effort` field
* (#1664), so it is suppressed off `deepseek.com`. Official endpoints
* document it next to `thinking` and keep pi-ai's detection (#1821).
*/
supportsReasoningEffort?: false;
/** When set, missing reasoning is filled with {@link DEEPSEEK_REASONING_REPLAY_PLACEHOLDER}. */
requiresNonEmptyReasoningReplay?: true;
};
/** pi-ai Completions flags for DeepSeek-family reasoning replay. */
export function deepseekRequestCompat(input: {
vendorKey?: string;
baseUrl?: string;
modelId?: string;
family?: string;
}): DeepSeekRequestCompat | undefined {
if (!isDeepSeekReasoningReplay(input)) return undefined;
if (isOfficialDeepSeekEndpoint(input)) {
return { requiresReasoningContentOnAssistantMessages: true };
}
return {
requiresReasoningContentOnAssistantMessages: true,
supportsReasoningEffort: false,
requiresNonEmptyReasoningReplay: true,
};
}