import { mapOutputToContent } from "./mapOutputToContent.js";
function toMessageMetadata(value) {
    if (value && typeof value === 'object' && !Array.isArray(value)) {
        return value;
    }
    return {};
}
/** Single response → TChatMessage[] (one assistant message; output items merged into content). */
export function openAIResponseToMessages(response) {
    var _a, _b;
    if (!((_a = response === null || response === void 0 ? void 0 : response.output) === null || _a === void 0 ? void 0 : _a.length)) {
        return [];
    }
    const baseMeta = toMessageMetadata(response.metadata);
    const content = mapOutputToContent(response.output);
    const firstMessage = response.output.find((item) => item.type === 'message' && 'role' in item && item.role === 'assistant');
    const id = (_b = firstMessage === null || firstMessage === void 0 ? void 0 : firstMessage.id) !== null && _b !== void 0 ? _b : response.id;
    const assistantMessage = Object.assign(Object.assign(Object.assign({}, (id && { id })), { role: 'assistant', content: content || '', metadata: baseMeta }), (response.error && { error: response.error }));
    return [assistantMessage];
}
//# sourceMappingURL=openAIResponseToMessages.js.map
