Parcourir la source

fix: preserve system prompts for DashScope

saladday il y a 2 mois
Parent
commit
f5dc1e53cf

+ 35 - 3
packages/local-runtime-v2/src/service/model-system/resolution/local-model-resolver.test.ts

@@ -1384,19 +1384,22 @@ describe('LocalModelResolver custom provider compat overrides', () => {
   });
   });
 
 
   // The incident was a wire-level symptom: pi chooses the system prompt role from the
   // The incident was a wire-level symptom: pi chooses the system prompt role from the
-  // resolved compat, so these two cases pin the request pi would actually send.
+  // resolved compat, so these cases pin the request pi would actually send.
   const reasoningModelConfig: LocalModelConfig = {
   const reasoningModelConfig: LocalModelConfig = {
     reasoning: true,
     reasoning: true,
     thinking: { effortOptions: ['low', 'light', 'max'] },
     thinking: { effortOptions: ['low', 'light', 'max'] },
   };
   };
 
 
-  const systemPromptRoleFor = async (compat: LocalModelConfig['compat']) => {
+  const systemPromptRoleFor = async (
+    compat: LocalModelConfig['compat'],
+    baseURL = 'https://gateway.example/v1',
+  ) => {
     const resolver = new LocalModelResolver({
     const resolver = new LocalModelResolver({
       byokConfigGetter: () => ({
       byokConfigGetter: () => ({
         custom_provider: {
         custom_provider: {
           gateway: {
           gateway: {
             api: 'openai-completions',
             api: 'openai-completions',
-            options: { apiKey: 'gateway-key', baseURL: 'https://gateway.example/v1' },
+            options: { apiKey: 'gateway-key', baseURL },
             models: { 'kimi-k2-thinking': { ...reasoningModelConfig, compat } },
             models: { 'kimi-k2-thinking': { ...reasoningModelConfig, compat } },
           },
           },
         },
         },
@@ -1443,4 +1446,33 @@ describe('LocalModelResolver custom provider compat overrides', () => {
   it('still sends `developer` when the gateway declares nothing, reproducing the incident', async () => {
   it('still sends `developer` when the gateway declares nothing, reproducing the incident', async () => {
     expect(await systemPromptRoleFor(undefined)).toBe('developer');
     expect(await systemPromptRoleFor(undefined)).toBe('developer');
   });
   });
+
+  it.each([
+    'https://dashscope.aliyuncs.com/compatible-mode/v1',
+    'https://dashscope-intl.aliyuncs.com/compatible-mode/v1',
+    'https://dashscope-us.aliyuncs.com/compatible-mode/v1',
+  ])('keeps the system role for a thinking model at %s', async (baseURL) => {
+    expect(await systemPromptRoleFor(undefined, baseURL)).toBe('system');
+  });
+
+  it.each([
+    ['https://api.openai.com/v1', 'developer'],
+    ['https://api.moonshot.cn/v1', 'system'],
+    ['https://api.moonshot.ai/v1', 'system'],
+    ['https://dashscope.aliyuncs.com.example/v1', 'developer'],
+    ['https://gateway.example/dashscope.aliyuncs.com/v1', 'developer'],
+  ])('preserves the system prompt role for %s', async (baseURL, role) => {
+    expect(await systemPromptRoleFor(undefined, baseURL)).toBe(role);
+  });
+
+  it.each([
+    'https://dashscope.aliyuncs.com/compatible-mode/v1',
+  ])('honors an explicit developer-role override on %s', async (baseURL) => {
+    expect(
+      await systemPromptRoleFor(
+        { supportsDeveloperRole: true },
+        baseURL,
+      ),
+    ).toBe('developer');
+  });
 });
 });

+ 8 - 0
third_party/pi-mono/MINIMAX_CHANGES.md

@@ -13,6 +13,14 @@ This directory vendors `pi-mono` as source so MiniMax can patch, validate, and s
 
 
 No upstream source files are changed in the baseline import.
 No upstream source files are changed in the baseline import.
 
 
+### 2026-09-19 — preserve the system role for DashScope
+
+- Reason: DashScope endpoints were treated as supporting the `developer` role, causing thinking-enabled OpenAI-compatible conversations to send an unsupported system-prompt role.
+- Affected package: `packages/ai` (`@earendil-works/pi-ai`), OpenAI Completions compatibility detection.
+- Change: default to the `system` role for the standard DashScope regional hosts (`dashscope`, `dashscope-intl`, and `dashscope-us` under `aliyuncs.com`). Preserve thinking, other request options, explicit compatibility overrides, and other providers.
+- Upstream PR: not opened.
+- Validation: offline request-payload regressions in `packages/local-runtime-v2/src/service/model-system/resolution/local-model-resolver.test.ts`. A minimal live `qwen3.5-plus` role comparison on the China DashScope endpoint returned HTTP 200 for `system` and HTTP 400 for `developer`. Other DashScope regions remain untested; this does not establish full CLI acceptance.
+
 ### 2026-08-31 — Windows PowerShell ConstrainedLanguage compatibility
 ### 2026-08-31 — Windows PowerShell ConstrainedLanguage compatibility
 
 
 - Reason: the Windows PowerShell 5.1 stdin wrapper called `Parser.ParseInput`, `ScriptBlock.Create`, and other restricted .NET APIs before user commands. Under enterprise App Control / AppLocker `ConstrainedLanguage`, the wrapper therefore failed before commands such as Python could run.
 - Reason: the Windows PowerShell 5.1 stdin wrapper called `Parser.ParseInput`, `ScriptBlock.Create`, and other restricted .NET APIs before user commands. Under enterprise App Control / AppLocker `ConstrainedLanguage`, the wrapper therefore failed before commands such as Python could run.

+ 3 - 1
third_party/pi-mono/packages/ai/src/providers/openai-completions.ts

@@ -1090,6 +1090,7 @@ function detectCompat(model: Model<"openai-completions">): ResolvedOpenAIComplet
 	const isTogether =
 	const isTogether =
 		provider === "together" || baseUrl.includes("api.together.ai") || baseUrl.includes("api.together.xyz");
 		provider === "together" || baseUrl.includes("api.together.ai") || baseUrl.includes("api.together.xyz");
 	const isMoonshot = provider === "moonshotai" || provider === "moonshotai-cn" || baseUrl.includes("api.moonshot.");
 	const isMoonshot = provider === "moonshotai" || provider === "moonshotai-cn" || baseUrl.includes("api.moonshot.");
+	const isDashScope = /^https?:\/\/dashscope(?:-intl|-us)?\.aliyuncs\.com(?::\d+)?(?:\/|$)/i.test(baseUrl);
 	const isOpenRouter = provider === "openrouter" || baseUrl.includes("openrouter.ai");
 	const isOpenRouter = provider === "openrouter" || baseUrl.includes("openrouter.ai");
 	const isCloudflareWorkersAI = provider === "cloudflare-workers-ai" || baseUrl.includes("api.cloudflare.com");
 	const isCloudflareWorkersAI = provider === "cloudflare-workers-ai" || baseUrl.includes("api.cloudflare.com");
 	const isCloudflareAiGateway = provider === "cloudflare-ai-gateway" || baseUrl.includes("gateway.ai.cloudflare.com");
 	const isCloudflareAiGateway = provider === "cloudflare-ai-gateway" || baseUrl.includes("gateway.ai.cloudflare.com");
@@ -1124,7 +1125,8 @@ function detectCompat(model: Model<"openai-completions">): ResolvedOpenAIComplet
 
 
 	return {
 	return {
 		supportsStore: !isNonStandard,
 		supportsStore: !isNonStandard,
-		supportsDeveloperRole: isOpenRouterDeveloperRoleModel || (!isNonStandard && !isOpenRouter),
+		supportsDeveloperRole:
+			!isDashScope && (isOpenRouterDeveloperRoleModel || (!isNonStandard && !isOpenRouter)),
 		supportsReasoningEffort:
 		supportsReasoningEffort:
 			!isGrok && !isZai && !isMoonshot && !isTogether && !isCloudflareAiGateway && !isNvidia && !isAntLing,
 			!isGrok && !isZai && !isMoonshot && !isTogether && !isCloudflareAiGateway && !isNvidia && !isAntLing,
 		supportsUsageInStreaming: true,
 		supportsUsageInStreaming: true,