/**
 * Chat协议事件生成器
 * 提供纯函数，将Chat响应数据转换为SSE事件对象，不处理流写入
 */
import { IdUtils, TimeUtils } from '../../shared/utils.js';
// 默认配置
export const DEFAULT_CHAT_EVENT_GENERATOR_CONFIG = {
    chunkSize: 12,
    chunkDelayMs: 8,
    enableIdGeneration: true,
    enableTimestampGeneration: true
};
/**
 * 创建默认上下文
 */
export function createDefaultContext(model, requestId) {
    return {
        model,
        requestId,
        choiceIndex: 0,
        toolCallIndexCounter: 0,
        contentIndexCounter: new Map()
    };
}
/**
 * 生成基础chunk数据
 */
function createBaseChunk(context, config) {
    return {
        id: config.enableIdGeneration ? IdUtils.generateRequestId() : context.requestId,
        object: 'chat.completion.chunk',
        created: config.enableTimestampGeneration ? Math.floor(TimeUtils.now() / 1000) : 0,
        model: context.model
    };
}
/**
 * 构建role delta事件
 */
export function buildRoleDelta(role, context, config = DEFAULT_CHAT_EVENT_GENERATOR_CONFIG) {
    const baseChunk = createBaseChunk(context, config);
    const chunk = {
        ...baseChunk,
        choices: [{
                index: context.choiceIndex,
                delta: { role: role },
                logprobs: null,
                finish_reason: null
            }]
    };
    return [{
            event: 'chat_chunk',
            type: 'chat_chunk',
            timestamp: TimeUtils.now(),
            data: JSON.stringify(chunk),
            sequenceNumber: 0,
            protocol: 'chat',
            direction: 'json_to_sse'
        }];
}
/**
 * 构建reasoning delta事件
 */
export function* buildReasoningDeltas(reasoning, context, config = DEFAULT_CHAT_EVENT_GENERATOR_CONFIG) {
    if (!reasoning)
        return;
    const baseChunk = createBaseChunk(context, config);
    const chunk = {
        ...baseChunk,
        choices: [{
                index: context.choiceIndex,
                delta: { reasoning },
                logprobs: null,
                finish_reason: null
            }]
    };
    yield {
        event: 'chat_chunk',
        type: 'chat_chunk',
        timestamp: TimeUtils.now(),
        data: JSON.stringify(chunk),
        sequenceNumber: 0,
        protocol: 'chat',
        direction: 'json_to_sse'
    };
}
/**
 * 构建content delta事件
 */
export function* buildContentDeltas(content, context, config = DEFAULT_CHAT_EVENT_GENERATOR_CONFIG) {
    if (!content)
        return;
    const baseChunk = createBaseChunk(context, config);
    const chunk = {
        ...baseChunk,
        choices: [{
                index: context.choiceIndex,
                delta: { content },
                logprobs: null,
                finish_reason: null
            }]
    };
    yield {
        event: 'chat_chunk',
        type: 'chat_chunk',
        timestamp: TimeUtils.now(),
        data: JSON.stringify(chunk),
        sequenceNumber: 0,
        protocol: 'chat',
        direction: 'json_to_sse'
    };
}
/**
 * 构建tool_call开始事件
 */
export function buildToolCallStart(toolCall, toolCallIndex, context, config = DEFAULT_CHAT_EVENT_GENERATOR_CONFIG) {
    const baseChunk = createBaseChunk(context, config);
    const chunk = {
        ...baseChunk,
        choices: [{
                index: context.choiceIndex,
                delta: {
                    tool_calls: [{
                            index: toolCallIndex,
                            id: toolCall.id,
                            type: toolCall.type || 'function',
                            function: {
                                name: toolCall.function.name
                            }
                        }]
                },
                logprobs: null,
                finish_reason: null
            }]
    };
    return {
        event: 'chat_chunk',
        type: 'chat_chunk',
        timestamp: TimeUtils.now(),
        data: JSON.stringify(chunk),
        sequenceNumber: 0,
        protocol: 'chat',
        direction: 'json_to_sse'
    };
}
/**
 * 构建tool_call arguments delta事件
 */
export function* buildToolCallArgsDeltas(args, toolCallIndex, context, config = DEFAULT_CHAT_EVENT_GENERATOR_CONFIG) {
    if (!args)
        return;
    const baseChunk = createBaseChunk(context, config);
    const chunk = {
        ...baseChunk,
        choices: [{
                index: context.choiceIndex,
                delta: {
                    tool_calls: [{
                            index: toolCallIndex,
                            function: { arguments: args }
                        }]
                },
                logprobs: null,
                finish_reason: null
            }]
    };
    yield {
        event: 'chat_chunk',
        type: 'chat_chunk',
        timestamp: TimeUtils.now(),
        data: JSON.stringify(chunk),
        sequenceNumber: 0,
        protocol: 'chat',
        direction: 'json_to_sse'
    };
}
/**
 * 构建finish_reason事件
 */
export function buildFinishEvent(finishReason, context, config = DEFAULT_CHAT_EVENT_GENERATOR_CONFIG) {
    const baseChunk = createBaseChunk(context, config);
    const chunk = {
        ...baseChunk,
        choices: [{
                index: context.choiceIndex,
                delta: {},
                logprobs: null,
                finish_reason: finishReason
            }]
    };
    return {
        event: 'chat_chunk',
        type: 'chat_chunk',
        timestamp: TimeUtils.now(),
        data: JSON.stringify(chunk),
        sequenceNumber: 0,
        protocol: 'chat',
        direction: 'json_to_sse'
    };
}
/**
 * 构建done事件
 */
export function buildDoneEvent(context, config = DEFAULT_CHAT_EVENT_GENERATOR_CONFIG) {
    return {
        event: 'chat.done',
        type: 'chat.done',
        timestamp: TimeUtils.now(),
        data: '[DONE]',
        sequenceNumber: 0,
        protocol: 'chat',
        direction: 'json_to_sse'
    };
}
/**
 * 构建error事件
 */
export function buildErrorEvent(error, context, config = DEFAULT_CHAT_EVENT_GENERATOR_CONFIG) {
    return {
        event: 'error',
        type: 'error',
        timestamp: TimeUtils.now(),
        data: JSON.stringify({
            error: {
                message: error.message,
                type: 'internal_error',
                code: 'generation_error'
            }
        }),
        sequenceNumber: 0,
        protocol: 'chat',
        direction: 'json_to_sse'
    };
}
