mirror of
https://github.com/luckyyzh/pi-agent-integrated.git
synced 2026-10-03 02:59:35 +00:00
feat: integrate Pi backend and Pi Web
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user