//#region src/agent/memory/window.ts
const DEFAULT_CONFIG = {
	maxMessages: 100,
	keepRecentMessages: 20,
	preserveSystemMessages: true
};
var SlidingWindow = class {
	constructor(config = {}) {
		this.config = config;
		this.config = {
			...DEFAULT_CONFIG,
			...config
		};
	}
	/**
	* Trim messages to keep within window size
	*/
	trim(messages) {
		if (messages.length <= this.config.maxMessages) return messages;
		const { maxMessages, keepRecentMessages, preserveSystemMessages } = this.config;
		const systemMessages = messages.filter((m) => {
			const role = m.role;
			return role !== "user" && role !== "assistant" && role !== "toolResult";
		});
		const otherMessages = messages.filter((m) => {
			const role = m.role;
			return role === "user" || role === "assistant" || role === "toolResult";
		});
		if (preserveSystemMessages && systemMessages.length > 0) {
			if (systemMessages.length >= maxMessages) return systemMessages.slice(-maxMessages);
			const availableForRecent = maxMessages - systemMessages.length;
			const recentMessages = otherMessages.slice(-Math.min(keepRecentMessages, availableForRecent));
			return [...systemMessages, ...recentMessages];
		} else return messages.slice(-maxMessages);
	}
	/**
	* Check if messages need trimming
	*/
	needsTrim(messages) {
		return messages.length > this.config.maxMessages;
	}
	/**
	* Get stats about current window
	*/
	getStats(messages) {
		return {
			total: messages.length,
			system: messages.filter((m) => {
				const role = m.role;
				return role !== "user" && role !== "assistant" && role !== "toolResult";
			}).length,
			user: messages.filter((m) => m.role === "user").length,
			assistant: messages.filter((m) => m.role === "assistant").length,
			tool: messages.filter((m) => m.role === "toolResult").length,
			needsTrim: this.needsTrim(messages)
		};
	}
};
//#endregion
export { SlidingWindow };

//# sourceMappingURL=window.js.map