import { Container, Markdown, Spacer, Text } from "@earendil-works/pi-tui";
export class AssistantMessageComponent extends Container {
    theme;
    contentContainer;
    thoughtsVisible;
    currentMessage = null;
    _hasVisibleText = false;
    get hasVisibleText() {
        return this._hasVisibleText;
    }
    constructor(theme, model, thoughtsVisible = false) {
        super();
        this.theme = theme;
        this.contentContainer = new Container();
        this.thoughtsVisible = thoughtsVisible;
        this.addChild(this.contentContainer);
        if (model) {
            this.update(model);
        }
    }
    setThinkingVisibility(visible) {
        this.thoughtsVisible = visible;
        if (this.currentMessage !== null) {
            this.updateFromMessage(this.currentMessage);
        }
    }
    update(model) {
        if (model.type === "assistant") {
            this.updateFromMessage(model.message);
        }
        else {
            this.updatePartial(model.text, model.thinking);
        }
    }
    updatePartial(text, thinking) {
        const partial = {
            role: "assistant",
            api: "openai-responses",
            provider: "openai",
            model: "",
            timestamp: Date.now(),
            usage: {
                input: 0,
                output: 0,
                cacheRead: 0,
                cacheWrite: 0,
                totalTokens: 0,
                cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
            },
            stopReason: "stop",
            content: [
                ...(thinking?.trim() ? [{ type: "thinking", thinking }] : []),
                { type: "text", text },
            ],
        };
        this.updateFromMessage(partial);
    }
    updateFromMessage(message) {
        const { palette, markdownTheme } = this.theme;
        this.currentMessage = message;
        this.contentContainer.clear();
        this._hasVisibleText = false;
        for (let i = 0; i < message.content.length; i++) {
            const content = message.content[i];
            if (this.thoughtsVisible && content.type === "thinking" && content.thinking.trim()) {
                this.contentContainer.addChild(new Markdown(content.thinking.trim(), 1, 0, markdownTheme, {
                    color: (t) => palette.thinkingText(t),
                    italic: true,
                }));
                this._hasVisibleText = true;
                const hasTextAfter = message.content
                    .slice(i + 1)
                    .some((c) => c.type === "text" && c.text.trim());
                if (hasTextAfter) {
                    this.contentContainer.addChild(new Spacer(1));
                }
            }
            if (content.type === "text" && content.text.trim()) {
                this.contentContainer.addChild(new Markdown(content.text.trim(), 1, 0, markdownTheme));
                this._hasVisibleText = true;
            }
        }
        const appendStopReason = (line, style) => {
            if (this._hasVisibleText) {
                this.contentContainer.addChild(new Spacer(1));
            }
            this.contentContainer.addChild(new Text(style(line), 1, 0));
            this._hasVisibleText = true;
        };
        if (message.stopReason === "aborted") {
            appendStopReason("aborted", palette.statusWarn);
        }
        else if (message.stopReason === "error") {
            const errorMsg = message.errorMessage || "unknown error";
            appendStopReason(errorMsg, palette.statusError);
        }
    }
}
//# sourceMappingURL=assistant_message.js.map