function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
var DEFAULT_TOOL_ROLE = 'assistant';
/** @internal exported for OpenClaw 归一化复用 */ export function extractTextFromContent(content) {
    if (content === undefined || content === null) {
        return '';
    }
    if (typeof content === 'string') {
        return content;
    }
    var parts = [];
    var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
    try {
        for(var _iterator = content[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
            var part = _step.value;
            if (part.type === 'text' && typeof part.text === 'string') {
                parts.push(part.text);
                continue;
            }
            if ('refusal' in part && typeof part.refusal === 'string') {
                parts.push(part.refusal);
                continue;
            }
            if (part.type === 'image_url') {
                parts.push('[image]');
                continue;
            }
            parts.push("[".concat(part.type, "]"));
        }
    } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
    } finally{
        try {
            if (!_iteratorNormalCompletion && _iterator.return != null) {
                _iterator.return();
            }
        } finally{
            if (_didIteratorError) {
                throw _iteratorError;
            }
        }
    }
    return parts.join('\n');
}
function mapOpenAIRoleToRoleType(msg, toolRoleAs) {
    switch(msg.role){
        case 'user':
            return 'user';
        case 'system':
            return 'system';
        case 'assistant':
            return 'assistant';
        case 'tool':
        case 'function':
            return toolRoleAs;
        default:
            return 'assistant';
    }
}
function appendAssistantExtras(msg, base, appendToolCalls) {
    var _msg_tool_calls;
    if (!appendToolCalls) {
        return base;
    }
    var out = base;
    if ((_msg_tool_calls = msg.tool_calls) === null || _msg_tool_calls === void 0 ? void 0 : _msg_tool_calls.length) {
        out += "\n\n[tool_calls]\n".concat(JSON.stringify(msg.tool_calls, null, 2));
    }
    if (msg.function_call) {
        out += "\n\n[function_call]\n".concat(JSON.stringify(msg.function_call, null, 2));
    }
    return out;
}
function defaultGetMessageId(msg, index) {
    var _msg_id;
    return (_msg_id = msg.id) !== null && _msg_id !== void 0 ? _msg_id : "openai-msg-".concat(index);
}
/**
 * 将 OpenAI Chat Completions 风格的 messages 转为 MessageBubbleData[]
 */ export function mapOpenAIMessagesToMessageBubbleData(messages, options, mapMessage) {
    var _ref = options !== null && options !== void 0 ? options : {}, _ref_baseTime = _ref.baseTime, baseTime = _ref_baseTime === void 0 ? Date.now() : _ref_baseTime, _ref_timeStepMs = _ref.timeStepMs, timeStepMs = _ref_timeStepMs === void 0 ? 1 : _ref_timeStepMs, _ref_bumpUpdateAtOnLastMessage = _ref.bumpUpdateAtOnLastMessage, bumpUpdateAtOnLastMessage = _ref_bumpUpdateAtOnLastMessage === void 0 ? false : _ref_bumpUpdateAtOnLastMessage, _ref_getMessageId = _ref.getMessageId, getMessageId = _ref_getMessageId === void 0 ? defaultGetMessageId : _ref_getMessageId, _ref_toolRoleAs = _ref.toolRoleAs, toolRoleAs = _ref_toolRoleAs === void 0 ? DEFAULT_TOOL_ROLE : _ref_toolRoleAs, _ref_appendToolCallsToContent = _ref.appendToolCallsToContent, appendToolCallsToContent = _ref_appendToolCallsToContent === void 0 ? true : _ref_appendToolCallsToContent, _ref_preserveRawInExtra = _ref.preserveRawInExtra, preserveRawInExtra = _ref_preserveRawInExtra === void 0 ? true : _ref_preserveRawInExtra;
    var lastIndex = messages.length - 1;
    return messages.map(function(msg, index) {
        var id = getMessageId(msg, index);
        var role = mapOpenAIRoleToRoleType(msg, toolRoleAs);
        var createAt = baseTime + index * timeStepMs;
        var updateAt = createAt;
        if (bumpUpdateAtOnLastMessage && index === lastIndex) {
            updateAt = Date.now();
        }
        var text = '';
        if (msg.role === 'assistant') {
            var a = msg;
            text = extractTextFromContent(a.content);
            text = appendAssistantExtras(a, text, appendToolCallsToContent);
        } else if (msg.role === 'user' || msg.role === 'system') {
            text = extractTextFromContent(msg.content);
        } else if (msg.role === 'tool') {
            var _msg_content;
            var prefix = msg.tool_call_id ? "[tool_call_id: ".concat(msg.tool_call_id, "]\n") : '';
            text = prefix + ((_msg_content = msg.content) !== null && _msg_content !== void 0 ? _msg_content : '');
        } else if (msg.role === 'function') {
            var _msg_content1;
            var prefix1 = msg.name ? "[function: ".concat(msg.name, "]\n") : '';
            text = prefix1 + ((_msg_content1 = msg.content) !== null && _msg_content1 !== void 0 ? _msg_content1 : '');
        }
        var originContent = text;
        var extra = preserveRawInExtra ? {
            openai: {
                role: msg.role,
                raw: msg
            }
        } : {};
        var draft = _object_spread({
            id: id,
            role: role,
            content: text,
            originContent: originContent,
            createAt: createAt,
            updateAt: updateAt
        }, Object.keys(extra).length ? {
            extra: extra
        } : {});
        if (mapMessage) {
            draft = mapMessage(msg, index, draft);
        }
        return draft;
    });
}
