mirror of
https://github.com/luckyyzh/pi-agent-integrated.git
synced 2026-10-03 02:59:35 +00:00
32 lines
1.5 KiB
TypeScript
32 lines
1.5 KiB
TypeScript
import type { AgentMessage, AssistantMessage, ToolCallContent } from "./types";
|
|
|
|
function isObject(val: unknown): val is Record<string, unknown> {
|
|
return typeof val === "object" && val !== null && !Array.isArray(val);
|
|
}
|
|
|
|
function normalizeToolCallBlock(block: unknown): ToolCallContent | null {
|
|
if (!isObject(block) || block.type !== "toolCall") return null;
|
|
return {
|
|
type: "toolCall",
|
|
toolCallId: typeof block.toolCallId === "string" ? block.toolCallId : (typeof block.id === "string" ? block.id : ""),
|
|
toolName: typeof block.toolName === "string" ? block.toolName : (typeof block.name === "string" ? block.name : ""),
|
|
input: typeof block.input === "object" && block.input !== null && !Array.isArray(block.input)
|
|
? block.input as Record<string, unknown>
|
|
: (typeof block.arguments === "object" && block.arguments !== null && !Array.isArray(block.arguments)
|
|
? block.arguments as Record<string, unknown>
|
|
: {}),
|
|
};
|
|
}
|
|
|
|
export function normalizeToolCalls(msg: AgentMessage): AgentMessage {
|
|
// Non-assistant roles (user, toolResult, bashExecution, custom) are returned
|
|
// unchanged — only assistant messages go through tool-call field normalization.
|
|
if (msg.role !== "assistant") return msg;
|
|
const content = (msg as AssistantMessage).content;
|
|
if (!Array.isArray(content)) return msg;
|
|
const normalized = content.map((block) => {
|
|
const result = normalizeToolCallBlock(block);
|
|
return result ?? block;
|
|
});
|
|
return { ...msg, content: normalized } as AgentMessage;
|
|
} |