import { ShortcutXLAgentAssistantContentKind, ShortcutXLModelStreamEventKind } from '../contracts/model-stream.js';
function toAssistantContent(content) {
    switch (content.type) {
        case ShortcutXLAgentAssistantContentKind.Text:
            return {
                type: 'text',
                text: content.text
            };
        case ShortcutXLAgentAssistantContentKind.Thinking:
            return {
                type: 'thinking',
                thinking: content.thinking,
                redacted: content.redacted,
                thinkingSignature: content.thinkingSignature
            };
        case ShortcutXLAgentAssistantContentKind.ToolCall:
            return {
                type: 'toolCall',
                id: content.id,
                name: content.name,
                arguments: content.arguments
            };
    }
    throw new Error(`Unhandled ShortcutXL runtime assistant content: ${String(content)}`);
}
function toAssistantMessage(message) {
    const assistant = {
        api: message.api,
        content: message.content.map((content) => toAssistantContent(content)),
        errorMessage: message.errorMessage,
        model: message.model,
        provider: message.provider,
        role: 'assistant',
        stopReason: message.stopReason,
        timestamp: message.timestamp,
        usage: message.usage
    };
    if (message.errorDetails) {
        assistant.errorDetails = message.errorDetails;
    }
    return assistant;
}
/** Forward one runtime stream event onto a owned AI assistant-message stream. */
export function pushShortcutXLModelStreamEvent(stream, event) {
    const partial = toAssistantMessage(event.partial);
    switch (event.kind) {
        case ShortcutXLModelStreamEventKind.Start:
            stream.push({
                type: 'start',
                partial
            });
            return;
        case ShortcutXLModelStreamEventKind.TextStart:
            stream.push({
                type: 'text_start',
                contentIndex: event.contentIndex,
                partial
            });
            return;
        case ShortcutXLModelStreamEventKind.TextDelta:
            stream.push({
                type: 'text_delta',
                contentIndex: event.contentIndex,
                delta: event.delta,
                partial
            });
            return;
        case ShortcutXLModelStreamEventKind.TextEnd: {
            const content = partial.content[event.contentIndex];
            const text = content?.type === 'text' ? content.text : '';
            stream.push({
                type: 'text_end',
                contentIndex: event.contentIndex,
                content: text,
                partial
            });
            return;
        }
        case ShortcutXLModelStreamEventKind.ThinkingStart:
            stream.push({
                type: 'thinking_start',
                contentIndex: event.contentIndex,
                partial
            });
            return;
        case ShortcutXLModelStreamEventKind.ThinkingDelta:
            stream.push({
                type: 'thinking_delta',
                contentIndex: event.contentIndex,
                delta: event.delta,
                partial
            });
            return;
        case ShortcutXLModelStreamEventKind.ThinkingEnd:
            stream.push({
                type: 'thinking_end',
                contentIndex: event.contentIndex,
                content: event.content,
                partial
            });
            return;
        case ShortcutXLModelStreamEventKind.ThinkingRedacted:
            return;
        case ShortcutXLModelStreamEventKind.ToolCallStart:
            stream.push({
                type: 'toolcall_start',
                contentIndex: event.contentIndex,
                partial
            });
            return;
        case ShortcutXLModelStreamEventKind.ToolCallDelta:
            stream.push({
                type: 'toolcall_delta',
                contentIndex: event.contentIndex,
                delta: event.delta,
                partial
            });
            return;
        case ShortcutXLModelStreamEventKind.ToolCallEnd:
            stream.push({
                type: 'toolcall_end',
                contentIndex: event.contentIndex,
                partial,
                toolCall: toAssistantContent(event.toolCall)
            });
            return;
        case ShortcutXLModelStreamEventKind.Done:
            stream.push({
                type: 'done',
                reason: event.reason,
                message: toAssistantMessage(event.message)
            });
            return;
        case ShortcutXLModelStreamEventKind.Error:
            stream.push({
                type: 'error',
                reason: event.reason,
                error: toAssistantMessage(event.error)
            });
            return;
    }
}
//# sourceMappingURL=stream-events.js.map