import { toolSessionManager } from "../services/ToolSessionManager.js";
import { getModelContextWindow } from "../../utils/model-context-utils.js";
import { IncrementalStreamParser } from "../../stream/IncrementalStreamParser.js";
import { getPrimaryModel } from "../generation/model-config.js";

//#region src/domains/run/agents/streaming/stream-parser.ts
function setupStreamParser(ctx, sessionId, contextId) {
	const streamHelper = ctx.streamHelper;
	if (!streamHelper) throw new Error("Stream helper is unexpectedly undefined in streaming context");
	const session = toolSessionManager.getSession(sessionId);
	const modelContextInfo = getModelContextWindow(getPrimaryModel(ctx.config));
	const artifactParserOptions = {
		sessionId,
		taskId: session?.taskId,
		projectId: session?.projectId,
		artifactComponents: ctx.artifactComponents,
		streamRequestId: ctx.streamRequestId ?? "",
		subAgentId: ctx.config.id,
		contextWindowSize: modelContextInfo.contextWindow ?? void 0
	};
	return new IncrementalStreamParser(streamHelper, ctx.executionContext, contextId, artifactParserOptions);
}

//#endregion
export { setupStreamParser };