import { __asyncGenerator, __asyncValues, __await } from "tslib";
function readEventType(e) {
    const top = e.type;
    if (top)
        return top;
    const data = e.data;
    const fromData = data === null || data === void 0 ? void 0 : data.type;
    if (fromData)
        return fromData;
    return e.event;
}
function readEventItem(e) {
    const top = e.item;
    if (top)
        return top;
    const data = e.data;
    return data === null || data === void 0 ? void 0 : data.item;
}
function readReferencedItemId(e) {
    const top = e.item_id;
    if (typeof top === 'string')
        return top;
    const data = e.data;
    const fromData = data === null || data === void 0 ? void 0 : data.item_id;
    if (typeof fromData === 'string')
        return fromData;
    const item = readEventItem(e);
    return typeof (item === null || item === void 0 ? void 0 : item.id) === 'string' ? item.id : undefined;
}
/**
 * Drops events tied to `mcp_list_tools` output items so the chat surface does not
 * render an "MCP List Tools" card. The available-tools listing is implementation
 * detail and clutters the conversation. We record item ids announced via
 * `response.output_item.added` (and the matching `done`) and skip any other event
 * that references them (`response.mcp_list_tools.*`, etc.).
 */
export function omitMcpListToolsEvents(source) {
    return __asyncGenerator(this, arguments, function* omitMcpListToolsEvents_1() {
        var _a, e_1, _b, _c;
        const mcpListToolsItemIds = new Set();
        try {
            for (var _d = true, source_1 = __asyncValues(source), source_1_1; source_1_1 = yield __await(source_1.next()), _a = source_1_1.done, !_a; _d = true) {
                _c = source_1_1.value;
                _d = false;
                const event = _c;
                const e = event;
                const type = readEventType(e);
                const item = readEventItem(e);
                if ((type === 'response.output_item.added' || type === 'response.output_item.done') &&
                    (item === null || item === void 0 ? void 0 : item.type) === 'mcp_list_tools') {
                    if (typeof item.id === 'string') {
                        mcpListToolsItemIds.add(item.id);
                    }
                    continue;
                }
                if (mcpListToolsItemIds.size > 0) {
                    const refId = readReferencedItemId(e);
                    if (refId && mcpListToolsItemIds.has(refId)) {
                        continue;
                    }
                }
                yield yield __await(event);
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (!_d && !_a && (_b = source_1.return)) yield __await(_b.call(source_1));
            }
            finally { if (e_1) throw e_1.error; }
        }
    });
}
//# sourceMappingURL=omitMcpListToolsEvents.js.map
