// Streaming textual tool intent extractor (对齐)
// Detects <function=execute> blocks and structured apply_patch payloads
// and converts them into OpenAI tool_calls incrementally.
import { extractStreamingToolCallsWithNative } from '../../router/virtual-router/engine-selection/native-shared-conversion-semantics.js';
function assertStreamingToolExtractorNativeAvailable() {
    if (typeof extractStreamingToolCallsWithNative !== 'function') {
        throw new Error('[streaming-text-extractor] native bindings unavailable');
    }
}
export class StreamingTextToolExtractor {
    opts;
    buffer = '';
    idCounter = 0;
    constructor(opts = {}) {
        this.opts = opts;
    }
    reset() {
        this.buffer = '';
        this.idCounter = 0;
    }
    feedText(text) {
        if (typeof text !== 'string' || !text)
            return [];
        assertStreamingToolExtractorNativeAvailable();
        const idPrefix = this.opts.idPrefix || 'call';
        const output = extractStreamingToolCallsWithNative({
            buffer: this.buffer,
            text,
            idPrefix,
            idCounter: this.idCounter,
            nowMs: Date.now()
        });
        this.buffer = typeof output.buffer === 'string' ? output.buffer : this.buffer;
        this.idCounter = typeof output.idCounter === 'number' ? output.idCounter : this.idCounter;
        return Array.isArray(output.toolCalls) ? output.toolCalls : [];
    }
}
export function createStreamingToolExtractor(opts) {
    return new StreamingTextToolExtractor(opts);
}
