Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(pi-ai): plumb non-empty reasoning replay through getCompat

vastsa3b3b9444aa86
diff --git a/packages/agent-runtime/src/provider-binding.test.ts b/packages/agent-runtime/src/provider-binding.test.ts
index 64d95b3a4..779b67a8b 100644
--- a/packages/agent-runtime/src/provider-binding.test.ts
+++ b/packages/agent-runtime/src/provider-binding.test.ts
@@ -1,4 +1,5 @@
import { describe, expect, it, vi } from "vitest";
+import { DEEPSEEK_REASONING_REPLAY_PLACEHOLDER } from "@pi-desktop/shared";
import type { ModelAuth } from "@earendil-works/pi-ai";
import { convertMessages } from "@earendil-works/pi-ai/api/openai-completions";
import { modelConfigWithBinding } from "./model-capabilities.js";
@@ -670,3 +671,85 @@ describe("GitHub Copilot transport identity", () => {
expect(request?.headers.get("Openai-Intent")).toBe("conversation-edits");
});
});
+
+describe("DeepSeek-family relay reasoning replay (#296)", () => {
+ /**
+ * The non-empty-replay opt-in lives in `model.compat`, but pi-ai rebuilds
+ * compat from an explicit allowlist in `getCompat`. Only a request that goes
+ * through the adapter can prove the placeholder survives that rebuild and
+ * reaches the wire, so this asserts the captured body rather than the compat
+ * object. Guards patches/@[email protected].
+ */
+ it("fills a thinking-less assistant turn with the documented placeholder", async () => {
+ const provider: RuntimeProviderConfig = {
+ ...keyedProvider,
+ id: "air-outer",
+ name: "Air Outer",
+ // A relay, not deepseek.com, so the strict non-empty replay applies.
+ baseUrl: "https://ps.air-outer.com/v1",
+ modelId: "deepseek-v4-flash",
+ supportsReasoning: true,
+ supportedThinkingLevels: ["off", "high"],
+ };
+ const requests: Record<string, unknown>[] = [];
+ const fetch = vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => {
+ requests.push(JSON.parse(String(init?.body)) as Record<string, unknown>);
+ return new Response(
+ JSON.stringify({ choices: [{ delta: { content: "ok" }, finish_reason: "stop" }] }),
+ { status: 200, headers: { "content-type": "application/json" } },
+ );
+ });
+ const model = buildProviderModel(provider);
+ const usage = {
+ input: 1,
+ output: 1,
+ cacheRead: 0,
+ cacheWrite: 0,
+ totalTokens: 2,
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
+ };
+ const context = {
+ systemPrompt: "system",
+ tools: [],
+ messages: [
+ { role: "user", content: "hello", timestamp: 1 },
+ {
+ role: "assistant",
+ api: "openai-completions",
+ provider: provider.id,
+ model: provider.modelId,
+ content: [
+ { type: "thinking", thinking: "let me look", thinkingSignature: "reasoning_content" },
+ { type: "text", text: "looking" },
+ ],
+ usage,
+ stopReason: "stop",
+ timestamp: 2,
+ },
+ { role: "user", content: "again", timestamp: 3 },
+ {
+ role: "assistant",
+ api: "openai-completions",
+ provider: provider.id,
+ model: provider.modelId,
+ content: [{ type: "text", text: "done" }],
+ usage,
+ stopReason: "stop",
+ timestamp: 4,
+ },
+ ],
+ };
+
+ await createProviderModels(provider, model)
+ .streamSimple(model, context as never, { reasoning: "high", fetch })
+ .result();
+
+ const assistants = (
+ requests[0].messages as Array<Record<string, unknown>>
+ ).filter((message) => message.role === "assistant");
+ expect(assistants[0].reasoning_content).toBe("let me look");
+ expect(assistants[1].reasoning_content).toBe(
+ DEEPSEEK_REASONING_REPLAY_PLACEHOLDER,
+ );
+ });
+});
diff --git a/patches/@[email protected] b/patches/@[email protected]
index 68bb1b41e..c55a27c04 100644
--- a/patches/@[email protected]
+++ b/patches/@[email protected]
@@ -357,7 +357,7 @@ index 3106c5d470dc3610c05d8ee153026df20f86f66e..8b209bb46809c394ffebfd11d5dfd273
id: block.id,
type: "function",
diff --git a/dist/api/openai-completions.js b/dist/api/openai-completions.js
-index fd459fddd20380242bbe37ec16e260097c9e4236..7d4c90a1aee35efaa05548c4797929111de4db70 100644
+index fd459fddd20380242bbe37ec16e260097c9e4236..dff3e9b3d04a95eec40dd2483b297a53823afe14 100644
--- a/dist/api/openai-completions.js
+++ b/dist/api/openai-completions.js
@@ -1039,21 +1039,27 @@ export function convertMessages(model, context, compat, options) {
@@ -436,6 +436,25 @@ index fd459fddd20380242bbe37ec16e260097c9e4236..7d4c90a1aee35efaa05548c479792911
return params;
}
function convertTools(tools, compat) {
+@@ -1265,6 +1306,9 @@ function detectCompat(model) {
+ requiresAssistantAfterToolResult: false,
+ requiresThinkingAsText: false,
+ requiresReasoningContentOnAssistantMessages: isDeepSeek,
++ // Relays that reject an empty reasoning echo must set this explicitly:
++ // no endpoint can be auto-detected as requiring a real one.
++ requiresNonEmptyReasoningReplay: undefined,
+ thinkingFormat: isDeepSeek
+ ? "deepseek"
+ : isZai
+@@ -1317,6 +1361,8 @@ function getCompat(model) {
+ requiresThinkingAsText: model.compat.requiresThinkingAsText ?? detected.requiresThinkingAsText,
+ requiresReasoningContentOnAssistantMessages: model.compat.requiresReasoningContentOnAssistantMessages ??
+ detected.requiresReasoningContentOnAssistantMessages,
++ requiresNonEmptyReasoningReplay: model.compat.requiresNonEmptyReasoningReplay ??
++ detected.requiresNonEmptyReasoningReplay,
+ thinkingFormat: model.compat.thinkingFormat ?? detected.thinkingFormat,
+ openRouterRouting: model.compat.openRouterRouting ?? {},
+ vercelGatewayRouting: model.compat.vercelGatewayRouting ?? detected.vercelGatewayRouting,
diff --git a/dist/api/openai-responses-shared.js b/dist/api/openai-responses-shared.js
index 53597d2c3becdd2c03d4fd265c213a36b403f26b..763f99a6e7d726ff9fc1ed59a45f6c8173ad108a 100644
--- a/dist/api/openai-responses-shared.js
@@ -896,7 +915,7 @@ index cf46c043b854412d56c5d65230a509165229806e..0fe002385c3d9906709be1cce6867d0c
//# sourceMappingURL=index.js.map
\ No newline at end of file
diff --git a/dist/types.d.ts b/dist/types.d.ts
-index 49f2a3262e19aea105f95424cdec08444a7f5748..52215b53d50ed92ed0aeb7416ab71c1bdced2d49 100644
+index 49f2a3262e19aea105f95424cdec08444a7f5748..a4d5e56dce4c4ba654b6c4cee1bb516c3d41580f 100644
--- a/dist/types.d.ts
+++ b/dist/types.d.ts
@@ -267,6 +267,30 @@ export interface ToolCall {
@@ -959,7 +978,20 @@ index 49f2a3262e19aea105f95424cdec08444a7f5748..52215b53d50ed92ed0aeb7416ab71c1b
} | {
type: "done";
reason: Extract<StopReason, "stop" | "length" | "toolUse" | "deferred">;
-@@ -789,6 +819,8 @@ export interface Model<TApi extends Api> {
+@@ -546,6 +576,12 @@ export interface OpenAICompletionsCompat {
+ requiresThinkingAsText?: boolean;
+ /** Whether all replayed assistant messages must include an empty reasoning_content field when reasoning is enabled. Default: auto-detected from URL. */
+ requiresReasoningContentOnAssistantMessages?: boolean;
++ /**
++ * Whether a replayed assistant message that carries no reasoning must send a documented
++ * non-empty placeholder instead of an empty string. Some relays reject the empty echo.
++ * Default: false; only an explicit model.compat opt-in enables it.
++ */
++ requiresNonEmptyReasoningReplay?: boolean;
+ /** Format for reasoning/thinking parameter. "openai" uses reasoning_effort, "openrouter" uses reasoning: { effort }, "deepseek" uses thinking: { type } plus reasoning_effort when supported, "together" uses reasoning: { enabled } plus reasoning_effort when supported, "baseten" uses configurable chat_template_args plus reasoning_effort when supported, "zai" uses thinking: { type }, "qwen" uses top-level enable_thinking: boolean, "qwen-chat-template" uses chat_template_kwargs.enable_thinking and preserve_thinking, "chat-template" uses configurable chat_template_kwargs, "string-thinking" uses top-level thinking: string, and "ant-ling" uses reasoning: { effort } only when the mapped effort is non-null. Default: "openai". */
+ thinkingFormat?: "openai" | "openrouter" | "deepseek" | "together" | "baseten" | "zai" | "qwen" | "chat-template" | "qwen-chat-template" | "string-thinking" | "ant-ling";
+ /** Kwargs to send as `chat_template_kwargs` when `thinkingFormat` is `chat-template`. Use `{ "$var": "thinking.enabled" }`, `{ "$var": "thinking.effort" }`, or `{ "$var": "thinking.budget" }` for pi-controlled thinking values. */
+@@ -789,6 +825,8 @@ export interface Model<TApi extends Api> {
provider: ProviderId;
baseUrl: string;
reasoning: boolean;
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 67769c42e..2299ae487 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -20,7 +20,7 @@ patchedDependencies:
hash: 62947f964f4c6270c68576ce7aa748a00bd20bca9c96e0dc49a4c17b0b283be1
path: patches/@[email protected]
'@earendil-works/[email protected]':
- hash: 515688f9e0f0861aed1339adb58188da14550798850b1b57f4b4e67f906a0220
+ hash: 6dca323bf8dc43f1fe3e458a808e5ba0d46bb51fa962f1275e3cf24c63927090
path: patches/@[email protected]
'@earendil-works/[email protected]':
hash: 60323335b0bd5cdbd857e5b9cf24bb794766f85bf3682694b9a43694bb435227
@@ -45,7 +45,7 @@ importers:
devDependencies:
'@earendil-works/pi-ai':
specifier: 0.86.1
- version: 0.86.1(patch_hash=515688f9e0f0861aed1339adb58188da14550798850b1b57f4b4e67f906a0220)([email protected])([email protected])
+ version: 0.86.1(patch_hash=6dca323bf8dc43f1fe3e458a808e5ba0d46bb51fa962f1275e3cf24c63927090)([email protected])([email protected])
'@pi-desktop/agent-host':
specifier: workspace:*
version: link:../../packages/agent-host
@@ -234,7 +234,7 @@ importers:
version: 0.86.1(patch_hash=62947f964f4c6270c68576ce7aa748a00bd20bca9c96e0dc49a4c17b0b283be1)([email protected])([email protected])
'@earendil-works/pi-ai':
specifier: 0.86.1
- version: 0.86.1(patch_hash=515688f9e0f0861aed1339adb58188da14550798850b1b57f4b4e67f906a0220)([email protected])([email protected])
+ version: 0.86.1(patch_hash=6dca323bf8dc43f1fe3e458a808e5ba0d46bb51fa962f1275e3cf24c63927090)([email protected])([email protected])
'@earendil-works/pi-coding-agent':
specifier: 0.86.1
version: 0.86.1(patch_hash=60323335b0bd5cdbd857e5b9cf24bb794766f85bf3682694b9a43694bb435227)([email protected])([email protected])
@@ -4823,7 +4823,7 @@ snapshots:
'@earendil-works/[email protected](patch_hash=62947f964f4c6270c68576ce7aa748a00bd20bca9c96e0dc49a4c17b0b283be1)([email protected])([email protected])':
dependencies:
'@earendil-works/chord': 0.86.1
- '@earendil-works/pi-ai': 0.86.1(patch_hash=515688f9e0f0861aed1339adb58188da14550798850b1b57f4b4e67f906a0220)([email protected])([email protected])
+ '@earendil-works/pi-ai': 0.86.1(patch_hash=6dca323bf8dc43f1fe3e458a808e5ba0d46bb51fa962f1275e3cf24c63927090)([email protected])([email protected])
'@earendil-works/pi-telemetry': 0.86.1
diff: 8.0.4
ignore: 7.0.8
@@ -4838,7 +4838,7 @@ snapshots:
- ws
- zod
- '@earendil-works/[email protected](patch_hash=515688f9e0f0861aed1339adb58188da14550798850b1b57f4b4e67f906a0220)([email protected])([email protected])':
+ '@earendil-works/[email protected](patch_hash=6dca323bf8dc43f1fe3e458a808e5ba0d46bb51fa962f1275e3cf24c63927090)([email protected])([email protected])':
dependencies:
'@anthropic-ai/sdk': 0.124.0([email protected])
'@aws-sdk/client-bedrock-runtime': 3.1127.0
@@ -4863,7 +4863,7 @@ snapshots:
dependencies:
'@earendil-works/chord': 0.86.1
'@earendil-works/pi-agent-core': 0.86.1(patch_hash=62947f964f4c6270c68576ce7aa748a00bd20bca9c96e0dc49a4c17b0b283be1)([email protected])([email protected])
- '@earendil-works/pi-ai': 0.86.1(patch_hash=515688f9e0f0861aed1339adb58188da14550798850b1b57f4b4e67f906a0220)([email protected])([email protected])
+ '@earendil-works/pi-ai': 0.86.1(patch_hash=6dca323bf8dc43f1fe3e458a808e5ba0d46bb51fa962f1275e3cf24c63927090)([email protected])([email protected])
'@earendil-works/pi-tui': 0.86.1
'@silvia-odwyer/photon-node': 0.3.4
chalk: 6.0.0