Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions packages/agent-runtime/src/reasoning-content-backfill.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { describe, expect, it } from "vitest";
import { convertMessages } from "@earendil-works/pi-ai/api/openai-completions";

// Guards the pnpm patch on @earendil-works/pi-ai (patches/@earendil-works__pi-ai@0.85.1.patch):
// DeepSeek-style endpoints accept a history where either every assistant message
// carries reasoning_content or none does, and reject a mix. A relayed model that
// is not in the catalogue has `reasoning: false`, so pi's per-message backfill
// never ran and a mixed history was sent as is (#223).

const compat = {
supportsDeveloperRole: false,
supportsOpenAIGrammarTools: false,
requiresToolResultName: false,
requiresAssistantAfterToolResult: false,
requiresThinkingAsText: false,
requiresReasoningContentOnAssistantMessages: true,
thinkingFormat: "deepseek",
deferredToolsMode: "none",
} as never;

function model(reasoning: boolean) {
return {
id: "deepseek-relay",
name: "deepseek-relay",
api: "openai-completions",
provider: "custom-relay",
baseUrl: "https://relay.example/v1",
reasoning,
input: ["text"],
contextWindow: 128_000,
maxTokens: 8_192,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
compat: {},
} as never;
}

function assistant(content: unknown[]) {
return {
role: "assistant",
api: "openai-completions",
provider: "custom-relay",
model: "deepseek-relay",
usage: {
input: 1,
output: 1,
cacheRead: 0,
cacheWrite: 0,
totalTokens: 2,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
},
stopReason: "stop",
timestamp: 1,
content,
};
}

const user = (text: string) => ({ role: "user", content: text, timestamp: 1 });

function assistantParams(messages: unknown[]) {
return convertMessages(model(false), { systemPrompt: "", messages, tools: [] } as never, compat)
.filter((message) => message.role === "assistant")
.map((message) => (message as { reasoning_content?: unknown }).reasoning_content);
}

describe("reasoning_content backfill for non-catalogue DeepSeek models", () => {
it("fills an empty reasoning_content on assistant turns without thinking once any turn has it", () => {
const reasoning = assistantParams([
user("first"),
assistant([
{ type: "thinking", thinking: "let me think", thinkingSignature: "reasoning_content" },
{ type: "text", text: "thought answer" },
]),
user("second"),
assistant([{ type: "text", text: "plain answer" }]),
]);
expect(reasoning).toEqual(["let me think", ""]);
});

it("leaves a history without any thinking untouched", () => {
const reasoning = assistantParams([
user("first"),
assistant([{ type: "text", text: "one" }]),
user("second"),
assistant([{ type: "text", text: "two" }]),
]);
expect(reasoning).toEqual([undefined, undefined]);
});
});
27 changes: 27 additions & 0 deletions patches/@earendil-works__pi-ai@0.85.1.patch
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
diff --git a/dist/api/openai-completions.js b/dist/api/openai-completions.js
index 48464f2bf56b9369d5c202e4f02fc4f13d35b412..fd36bd0f3daab13023c0220bb44c475503ec0c28 100644
--- a/dist/api/openai-completions.js
+++ b/dist/api/openai-completions.js
@@ -1141,6 +1141,22 @@ export function convertMessages(model, context, compat, options) {
}
lastRole = msg.role;
}
+ // DeepSeek-style endpoints reject a history in which only some assistant
+ // messages carry reasoning_content, while all-or-none is accepted. The
+ // per-message backfill above only runs for models flagged `reasoning`,
+ // which a relayed model outside the catalogue is not, even though its
+ // persisted turns still carry thinking. Whenever any assistant message
+ // ends up with reasoning_content, give the rest an empty one.
+ if (compat.requiresReasoningContentOnAssistantMessages &&
+ params.some((m) => m.role === "assistant" &&
+ typeof m.reasoning_content === "string" &&
+ m.reasoning_content.length > 0)) {
+ for (const m of params) {
+ if (m.role === "assistant" && m.reasoning_content === undefined) {
+ m.reasoning_content = "";
+ }
+ }
+ }
return params;
}
function convertTools(tools, compat) {
diff --git a/dist/api/openai-responses-shared.js b/dist/api/openai-responses-shared.js
index 43e463dbfd1e6cc437ebc680468036f47678de26..364c8c0426d12543c1b64daa8e6eaa22f35f6f60 100644
--- a/dist/api/openai-responses-shared.js
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading