import { extractTextFromToolResult, mapToolDetail, parseToolArgs, parseToolResult, } from "./tool-call-mapper.js";
function isTextContentBlock(block) {
    return (typeof block === "object" &&
        block !== null &&
        !Array.isArray(block) &&
        Reflect.get(block, "type") === "text" &&
        typeof Reflect.get(block, "text") === "string");
}
export function getUserMessageText(content) {
    if (typeof content === "string") {
        return content;
    }
    const textParts = [];
    for (const block of content) {
        if (isTextContentBlock(block)) {
            textParts.push(block.text);
        }
    }
    return textParts.join("\n\n");
}
export async function* streamPiHistory(provider, messages, userEntries = []) {
    const pendingToolCalls = new Map();
    let userIndex = 0;
    for (const message of messages) {
        if (message.role === "user") {
            const text = getUserMessageText(message.content);
            if (text) {
                const userEntry = userEntries[userIndex];
                yield {
                    type: "timeline",
                    provider,
                    item: {
                        type: "user_message",
                        text,
                        ...(userEntry ? { messageId: userEntry.id } : {}),
                    },
                };
            }
            userIndex += 1;
            continue;
        }
        if (message.role === "assistant") {
            for (const content of message.content) {
                if (content.type === "text" && content.text) {
                    yield {
                        type: "timeline",
                        provider,
                        item: { type: "assistant_message", text: content.text },
                    };
                    continue;
                }
                if (content.type === "thinking" && content.thinking) {
                    yield {
                        type: "timeline",
                        provider,
                        item: { type: "reasoning", text: content.thinking },
                    };
                    continue;
                }
                if (content.type === "toolCall") {
                    const tracked = parseToolArgs(content.name, content.arguments);
                    pendingToolCalls.set(content.id, tracked);
                    yield {
                        type: "timeline",
                        provider,
                        item: {
                            type: "tool_call",
                            callId: content.id,
                            name: tracked.toolName,
                            status: "running",
                            detail: mapToolDetail(tracked, null),
                            error: null,
                        },
                    };
                }
            }
            continue;
        }
        if (message.role === "toolResult") {
            const tracked = pendingToolCalls.get(message.toolCallId) ?? parseToolArgs(message.toolName, null);
            pendingToolCalls.delete(message.toolCallId);
            const result = parseToolResult({ content: message.content });
            const detail = mapToolDetail(tracked, result);
            yield {
                type: "timeline",
                provider,
                item: toToolResultTimelineItem({
                    callId: message.toolCallId,
                    name: tracked.toolName,
                    isError: Boolean(message.isError),
                    detail,
                    errorText: extractTextFromToolResult(result) ?? "Tool call failed",
                }),
            };
            continue;
        }
        if (message.role === "bashExecution") {
            const callId = `pi-bash-${message.timestamp}`;
            const detail = {
                type: "shell",
                command: message.command,
                output: message.output,
                exitCode: message.exitCode ?? null,
            };
            yield {
                type: "timeline",
                provider,
                item: {
                    type: "tool_call",
                    callId,
                    name: "bash",
                    status: message.cancelled ? "canceled" : "completed",
                    detail,
                    error: null,
                },
            };
        }
    }
}
function toToolResultTimelineItem(input) {
    if (input.isError) {
        return {
            type: "tool_call",
            callId: input.callId,
            name: input.name,
            status: "failed",
            detail: input.detail,
            error: input.errorText,
        };
    }
    return {
        type: "tool_call",
        callId: input.callId,
        name: input.name,
        status: "completed",
        detail: input.detail,
        error: null,
    };
}
//# sourceMappingURL=history-mapper.js.map