import { createLogger } from "../../utils/logger/index.js";
import { init_logger } from "../../utils/logger.js";
import { createHookContext } from "../../extensions/hooks.js";
import "../../extensions/index.js";
//#region src/agent/lifecycle/hook-handler.ts
init_logger();
const log = createLogger("HookHandler");
var HookHandler = class {
	constructor(deps) {
		this.deps = deps;
	}
	getContext(overrides) {
		return createHookContext({
			extensionId: void 0,
			sessionKey: this.deps.sessionKey,
			agentId: this.deps.agentId,
			timestamp: /* @__PURE__ */ new Date(),
			...overrides
		});
	}
	async trigger(event, eventData) {
		if (!this.deps.hookRunner) return;
		const ctx = this.getContext({
			extensionId: void 0,
			timestamp: /* @__PURE__ */ new Date()
		});
		try {
			await this.deps.hookRunner.runHooks(event, eventData, ctx);
		} catch (error) {
			const em = error instanceof Error ? error.message : String(error);
			log.warn({
				event,
				err: error,
				errorMessage: em,
				sessionKey: this.deps.sessionKey,
				agentId: this.deps.agentId
			}, `Extension hook "${event}" failed: ${em}`);
		}
	}
	/** Run a hook with an explicit session key (e.g. webchat turn lifecycle). */
	async triggerWithSessionKey(sessionKey, event, eventData) {
		if (!this.deps.hookRunner) return;
		const ctx = this.getContext({
			extensionId: void 0,
			sessionKey,
			timestamp: /* @__PURE__ */ new Date()
		});
		try {
			await this.deps.hookRunner.runHooks(event, {
				...eventData,
				sessionKey
			}, ctx);
		} catch (error) {
			const em = error instanceof Error ? error.message : String(error);
			log.warn({
				event,
				err: error,
				errorMessage: em,
				sessionKey,
				agentId: this.deps.agentId
			}, `Extension hook "${event}" failed: ${em}`);
		}
	}
	async runMessageSending(to, content, channel) {
		if (!this.deps.hookRunner) return {
			send: true,
			content
		};
		const ctx = this.getContext();
		return this.deps.hookRunner.runMessageSending(to, content, ctx, channel ? { channel } : void 0);
	}
	async runMessageSent(to, content, success, error, channel) {
		if (!this.deps.hookRunner) return;
		const ctx = this.getContext();
		await this.deps.hookRunner.runMessageSent(to, content, success, error, ctx, channel ? { channel } : void 0);
	}
	async runInputHook(text, images, source) {
		if (!this.deps.hookRunner) return {
			text,
			images,
			action: "continue",
			skipAgent: false
		};
		const ctx = this.getContext();
		return this.deps.hookRunner.runInputHook(text, images, source, ctx);
	}
	async runContextHook(messages) {
		if (!this.deps.hookRunner) return {
			messages,
			modified: false
		};
		const ctx = this.getContext();
		const result = await this.deps.hookRunner.runContextHook(messages, ctx);
		return {
			messages: result.messages,
			modified: result.modified
		};
	}
};
//#endregion
export { HookHandler };

//# sourceMappingURL=hook-handler.js.map