//#region src/utils/streaming.ts
var StreamingAccumulator = class {
	content = "";
	inputTokens = null;
	outputTokens = null;
	totalTokens = null;
	reasoningTokens = null;
	cacheReadTokens = null;
	cacheCreationTokens = null;
	finishReason = null;
	toolCalls = [];
	model = null;
	ttfbMs = null;
	isError = false;
	errorMessage = null;
	_startTime;
	_firstChunkReceived = false;
	constructor() {
		this._startTime = performance.now();
	}
	addContent(chunk) {
		if (!this._firstChunkReceived) {
			this.ttfbMs = performance.now() - this._startTime;
			this._firstChunkReceived = true;
		}
		this.content += chunk;
	}
	/**
	* Set (overwrite) token usage fields. Only non-null values are written,
	* allowing incremental updates where different streaming events provide
	* different fields (e.g. Anthropic message_start vs message_delta).
	*/
	setUsage(usage) {
		if (usage.inputTokens != null) this.inputTokens = usage.inputTokens;
		if (usage.outputTokens != null) this.outputTokens = usage.outputTokens;
		if (usage.totalTokens != null) this.totalTokens = usage.totalTokens;
		if (usage.reasoningTokens != null) this.reasoningTokens = usage.reasoningTokens;
		if (usage.cacheReadTokens != null) this.cacheReadTokens = usage.cacheReadTokens;
		if (usage.cacheCreationTokens != null) this.cacheCreationTokens = usage.cacheCreationTokens;
	}
	addToolCall(toolCall) {
		this.toolCalls.push(toolCall);
	}
	setToolCallAt(index, toolCall) {
		while (this.toolCalls.length <= index) this.toolCalls.push({});
		this.toolCalls[index] = toolCall;
	}
	appendToolCallArgs(index, args) {
		if (index < this.toolCalls.length && this.toolCalls[index]) {
			const fn = this.toolCalls[index].function;
			if (fn) fn.arguments = (fn.arguments ?? "") + args;
		}
	}
	setError(message) {
		this.isError = true;
		this.errorMessage = message;
	}
	getState() {
		return {
			content: this.content,
			inputTokens: this.inputTokens,
			outputTokens: this.outputTokens,
			totalTokens: this.totalTokens,
			reasoningTokens: this.reasoningTokens,
			cacheReadTokens: this.cacheReadTokens,
			cacheCreationTokens: this.cacheCreationTokens,
			finishReason: this.finishReason,
			toolCalls: this.toolCalls,
			model: this.model,
			ttfbMs: this.ttfbMs,
			isError: this.isError,
			errorMessage: this.errorMessage
		};
	}
	get elapsedMs() {
		return performance.now() - this._startTime;
	}
};

//#endregion
export { StreamingAccumulator };
//# sourceMappingURL=streaming.js.map