fix(agents): sanitize transcripts for strict tool APIs
This commit is contained in:
35
src/agents/pi-extensions/transcript-sanitize.ts
Normal file
35
src/agents/pi-extensions/transcript-sanitize.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Transcript repair/sanitization extension.
|
||||
*
|
||||
* Runs on every context build to prevent strict provider request rejections:
|
||||
* - duplicate or displaced tool results (Anthropic-compatible APIs, MiniMax, Cloud Code Assist)
|
||||
* - Cloud Code Assist tool call ID constraints + collision-safe sanitization
|
||||
*/
|
||||
|
||||
import type { AgentMessage } from "@mariozechner/pi-agent-core";
|
||||
import type {
|
||||
ContextEvent,
|
||||
ExtensionAPI,
|
||||
ExtensionContext,
|
||||
} from "@mariozechner/pi-coding-agent";
|
||||
|
||||
import { isGoogleModelApi } from "../pi-embedded-helpers.js";
|
||||
import { sanitizeToolUseResultPairing } from "../session-transcript-repair.js";
|
||||
import { sanitizeToolCallIdsForCloudCodeAssist } from "../tool-call-id.js";
|
||||
|
||||
export default function transcriptSanitizeExtension(api: ExtensionAPI): void {
|
||||
api.on("context", (event: ContextEvent, ctx: ExtensionContext) => {
|
||||
let next = event.messages as AgentMessage[];
|
||||
|
||||
const repairedTools = sanitizeToolUseResultPairing(next);
|
||||
if (repairedTools !== next) next = repairedTools;
|
||||
|
||||
if (isGoogleModelApi(ctx.model?.api)) {
|
||||
const repairedIds = sanitizeToolCallIdsForCloudCodeAssist(next);
|
||||
if (repairedIds !== next) next = repairedIds;
|
||||
}
|
||||
|
||||
if (next === event.messages) return undefined;
|
||||
return { messages: next };
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user