fix(agents): prevent Anthropic 400 'Incorrect role information' error
Add validateAnthropicTurns() to merge consecutive user messages that can occur when steering messages are injected during streaming. This prevents the API from rejecting requests due to improper role alternation. Changes: - Add validateAnthropicTurns() function in pi-embedded-helpers.ts - Integrate validation into sanitization pipeline in pi-embedded-runner.ts - Add user-friendly error message for role ordering errors - Add comprehensive tests for the new validation function
This commit is contained in:
committed by
Peter Steinberger
parent
bb9a9633a8
commit
c5fa757ef6
@@ -100,6 +100,7 @@ import {
|
||||
pickFallbackThinkingLevel,
|
||||
sanitizeGoogleTurnOrdering,
|
||||
sanitizeSessionMessagesImages,
|
||||
validateAnthropicTurns,
|
||||
validateGeminiTurns,
|
||||
} from "./pi-embedded-helpers.js";
|
||||
import {
|
||||
@@ -1291,7 +1292,9 @@ export async function compactEmbeddedPiSession(params: {
|
||||
sessionManager,
|
||||
sessionId: params.sessionId,
|
||||
});
|
||||
const validated = validateGeminiTurns(prior);
|
||||
// Validate turn ordering for both Gemini (consecutive assistant) and Anthropic (consecutive user)
|
||||
const validatedGemini = validateGeminiTurns(prior);
|
||||
const validated = validateAnthropicTurns(validatedGemini);
|
||||
const limited = limitHistoryTurns(
|
||||
validated,
|
||||
getDmHistoryLimitFromSessionKey(params.sessionKey, params.config),
|
||||
@@ -1714,7 +1717,9 @@ export async function runEmbeddedPiAgent(params: {
|
||||
sessionManager,
|
||||
sessionId: params.sessionId,
|
||||
});
|
||||
const validated = validateGeminiTurns(prior);
|
||||
// Validate turn ordering for both Gemini (consecutive assistant) and Anthropic (consecutive user)
|
||||
const validatedGemini = validateGeminiTurns(prior);
|
||||
const validated = validateAnthropicTurns(validatedGemini);
|
||||
const limited = limitHistoryTurns(
|
||||
validated,
|
||||
getDmHistoryLimitFromSessionKey(params.sessionKey, params.config),
|
||||
|
||||
Reference in New Issue
Block a user