var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
    if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
    var m = o[Symbol.asyncIterator], i;
    return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
    function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
    function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
import { VegaLiteStreamBuffer } from "./VegaLiteStreamBuffer.js";
export class TurnStreamConsumer {
    consume(input) {
        return __awaiter(this, void 0, void 0, function* () {
            var _a, e_1, _b, _c;
            var _d, _e;
            let fullResponse = "";
            const textBuffer = new VegaLiteStreamBuffer();
            try {
                for (var _f = true, _g = __asyncValues(input.stream), _h; _h = yield _g.next(), _a = _h.done, !_a; _f = true) {
                    _c = _h.value;
                    _f = false;
                    const rawChunk = _c;
                    if ((_d = input.abortSignal) === null || _d === void 0 ? void 0 : _d.aborted) {
                        throw new DOMException("This operation was aborted", "AbortError");
                    }
                    const [token, metadata] = rawChunk;
                    const nodeName = typeof (metadata === null || metadata === void 0 ? void 0 : metadata.langgraph_node) === "string"
                        ? metadata.langgraph_node
                        : "";
                    if (nodeName && !["model", "model_request"].includes(nodeName)) {
                        continue;
                    }
                    const blocks = Array.isArray(token === null || token === void 0 ? void 0 : token.contentBlocks)
                        ? token.contentBlocks
                        : Array.isArray(token === null || token === void 0 ? void 0 : token.content)
                            ? token.content
                            : [];
                    const reasoningDelta = blocks
                        .filter((block) => (block === null || block === void 0 ? void 0 : block.type) === "reasoning")
                        .map((block) => { var _a; return String((_a = block.reasoning) !== null && _a !== void 0 ? _a : ""); })
                        .join("");
                    const textDelta = blocks
                        .filter((block) => (block === null || block === void 0 ? void 0 : block.type) === "text")
                        .map((block) => { var _a; return String((_a = block.text) !== null && _a !== void 0 ? _a : ""); })
                        .join("");
                    if (reasoningDelta) {
                        yield ((_e = input.emit) === null || _e === void 0 ? void 0 : _e.call(input, {
                            type: "reasoning-delta",
                            delta: reasoningDelta,
                        }));
                    }
                    if (textDelta) {
                        fullResponse += textDelta;
                        yield textBuffer.push(textDelta, input.emit);
                    }
                }
            }
            catch (e_1_1) { e_1 = { error: e_1_1 }; }
            finally {
                try {
                    if (!_f && !_a && (_b = _g.return)) yield _b.call(_g);
                }
                finally { if (e_1) throw e_1.error; }
            }
            yield textBuffer.flush(input.emit);
            return {
                text: fullResponse,
            };
        });
    }
}
