feat: integrate Pi backend and Pi Web

This commit is contained in:
luckyyzh
2026-07-30 19:37:53 +08:00
commit 7392ab9dd7
1390 changed files with 337197 additions and 0 deletions
+32
View File
@@ -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;
}