//#region src/domains/run/services/slackBlockKit.ts
/** Slack Incoming Webhook URLs (chat.postMessage-compatible). */
const SLACK_INCOMING_WEBHOOK_URL_PREFIX = "https://hooks.slack.com/";
function isSlackIncomingWebhookUrl(url) {
	return url.startsWith(SLACK_INCOMING_WEBHOOK_URL_PREFIX);
}
function buildProjectUrl(ctx) {
	return `${ctx.manageUiBaseUrl}/${ctx.tenantId}/projects/${ctx.projectId}`;
}
function escapeSlackMrkdwn(value) {
	return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}
function buildConversationSlack(data, eventType, ctx) {
	const conversation = data.conversation;
	const convId = conversation?.id;
	const title = conversation?.title || convId || "Unknown";
	const header = eventType === "conversation.created" ? "New Conversation" : "Conversation Updated";
	const text = `${header}: ${escapeSlackMrkdwn(title)}`;
	const baseProjectUrl = buildProjectUrl(ctx);
	const links = [];
	if (convId) links.push(`<${baseProjectUrl}/traces/conversations/${convId}|View Conversation>`);
	const blocks = [{
		type: "header",
		text: {
			type: "plain_text",
			text: header
		}
	}, {
		type: "section",
		fields: [{
			type: "mrkdwn",
			text: `*Conversation:*\n${escapeSlackMrkdwn(title)}`
		}, {
			type: "mrkdwn",
			text: `*Agent:*\n${escapeSlackMrkdwn(ctx.agentId || "N/A")}`
		}]
	}];
	if (links.length > 0) blocks.push({
		type: "section",
		text: {
			type: "mrkdwn",
			text: links.join("  |  ")
		}
	});
	blocks.push({
		type: "context",
		elements: [{
			type: "mrkdwn",
			text: "Inkeep"
		}]
	});
	return {
		text,
		blocks
	};
}
function buildFeedbackSlack(data, ctx) {
	const feedback = data.feedback;
	const conversation = data.conversation;
	const feedbackType = feedback?.type || "unknown";
	const details = feedback?.details;
	const convId = conversation?.id;
	const convTitle = conversation?.title || convId || "Unknown";
	const emoji = feedbackType === "positive" ? "+1" : "-1";
	const text = `Feedback received: ${escapeSlackMrkdwn(feedbackType)} on conversation ${escapeSlackMrkdwn(convTitle)}`;
	const baseProjectUrl = buildProjectUrl(ctx);
	const links = [];
	if (convId) {
		links.push(`<${baseProjectUrl}/traces/conversations/${convId}|View Conversation>`);
		links.push(`<${baseProjectUrl}/feedback?conversationId=${convId}|View Feedback>`);
	}
	const fields = [{
		type: "mrkdwn",
		text: `*Type:*\n:${emoji}: ${escapeSlackMrkdwn(feedbackType)}`
	}, {
		type: "mrkdwn",
		text: `*Conversation:*\n${escapeSlackMrkdwn(convTitle)}`
	}];
	if (details) {
		const truncated = details.length > 200 ? `${details.slice(0, 197)}...` : details;
		fields.push({
			type: "mrkdwn",
			text: `*Details:*\n${escapeSlackMrkdwn(truncated)}`
		});
	}
	const blocks = [{
		type: "header",
		text: {
			type: "plain_text",
			text: "Feedback Received"
		}
	}, {
		type: "section",
		fields
	}];
	if (links.length > 0) blocks.push({
		type: "section",
		text: {
			type: "mrkdwn",
			text: links.join("  |  ")
		}
	});
	blocks.push({
		type: "context",
		elements: [{
			type: "mrkdwn",
			text: "Inkeep"
		}]
	});
	return {
		text,
		blocks
	};
}
function buildEventSlack(data, ctx) {
	const event = data.event;
	const eventType = event?.type || "unknown";
	const eventId = event?.id || "unknown";
	const convId = event?.conversationId;
	const text = `Event created: ${escapeSlackMrkdwn(eventType)} (${escapeSlackMrkdwn(eventId)})`;
	const baseProjectUrl = buildProjectUrl(ctx);
	const links = [];
	if (convId) links.push(`<${baseProjectUrl}/traces/conversations/${convId}|View Conversation>`);
	const blocks = [{
		type: "header",
		text: {
			type: "plain_text",
			text: "Event Created"
		}
	}, {
		type: "section",
		fields: [{
			type: "mrkdwn",
			text: `*Event Type:*\n${escapeSlackMrkdwn(eventType)}`
		}, {
			type: "mrkdwn",
			text: `*Event ID:*\n${escapeSlackMrkdwn(eventId)}`
		}]
	}];
	if (links.length > 0) blocks.push({
		type: "section",
		text: {
			type: "mrkdwn",
			text: links.join("  |  ")
		}
	});
	blocks.push({
		type: "context",
		elements: [{
			type: "mrkdwn",
			text: "Inkeep"
		}]
	});
	return {
		text,
		blocks
	};
}
function buildEvaluationFailedSlack(data, ctx, meta) {
	const evaluatorObj = data.evaluator;
	const conversationObj = data.conversation;
	const evaluatorName = evaluatorObj?.name || "Unknown";
	const conversationId = conversationObj?.id || "";
	const failedConditions = data.failedConditions ?? [];
	const evaluationRunConfigId = meta?.evaluationRunConfigId ?? null;
	const evaluationJobConfigId = meta?.evaluationJobConfigId ?? null;
	const scoreText = failedConditions.length > 0 ? failedConditions.map((c) => `expected ${escapeSlackMrkdwn(c.field)} ${escapeSlackMrkdwn(c.operator)} ${c.value}, got ${c.actual}`).join("; ") : "failed pass criteria";
	const text = `Evaluation failed: ${escapeSlackMrkdwn(evaluatorName)} ${scoreText} on conversation ${escapeSlackMrkdwn(conversationId)}`;
	const conditionSections = failedConditions.flatMap((c) => [{ type: "divider" }, {
		type: "section",
		fields: [
			{
				type: "mrkdwn",
				text: `*Field:*\n${escapeSlackMrkdwn(c.field)}`
			},
			{
				type: "mrkdwn",
				text: `*Operator:*\n\`${c.operator}\``
			},
			{
				type: "mrkdwn",
				text: `*Expected:*\n${c.value}`
			},
			{
				type: "mrkdwn",
				text: `*Actual:*\n${c.actual}`
			}
		]
	}]);
	const baseProjectUrl = buildProjectUrl(ctx);
	const links = [];
	if (conversationId) links.push(`<${baseProjectUrl}/traces/conversations/${conversationId}|View Conversation>`);
	if (evaluationRunConfigId) links.push(`<${baseProjectUrl}/evaluations/run-configs/${evaluationRunConfigId}|View Evaluation>`);
	else if (evaluationJobConfigId) links.push(`<${baseProjectUrl}/evaluations/jobs/${evaluationJobConfigId}|View Evaluation>`);
	const blocks = [
		{
			type: "header",
			text: {
				type: "plain_text",
				text: "Evaluation Failed"
			}
		},
		{
			type: "section",
			fields: [{
				type: "mrkdwn",
				text: `*Evaluator:*\n${escapeSlackMrkdwn(evaluatorName)}`
			}]
		},
		...conditionSections
	];
	if (links.length > 0) blocks.push({
		type: "section",
		text: {
			type: "mrkdwn",
			text: links.join("  |  ")
		}
	});
	blocks.push({
		type: "context",
		elements: [{
			type: "mrkdwn",
			text: "Inkeep"
		}]
	});
	return {
		text,
		blocks
	};
}
/** Payload for Manage UI "Test" deliveries to Slack incoming webhooks. */
function buildTestSlackPayload(envelope, ctx) {
	const title = (envelope.data ?? {}).conversation?.title || "Test webhook delivery";
	const text = `Test Webhook: ${escapeSlackMrkdwn(title)}`;
	const blocks = [
		{
			type: "header",
			text: {
				type: "plain_text",
				text: "Test Webhook Delivery"
			}
		},
		{
			type: "section",
			fields: [{
				type: "mrkdwn",
				text: `*Conversation:*\n${escapeSlackMrkdwn(title)}`
			}, {
				type: "mrkdwn",
				text: `*Agent:*\n${escapeSlackMrkdwn(ctx.agentId)}`
			}]
		},
		{
			type: "section",
			text: {
				type: "mrkdwn",
				text: "This message confirms your Slack incoming webhook is configured correctly."
			}
		},
		{
			type: "context",
			elements: [{
				type: "mrkdwn",
				text: "Inkeep"
			}]
		}
	];
	return {
		...envelope,
		text,
		blocks
	};
}
function buildConversationErrorSlack(data, eventType, ctx) {
	const conversationId = data.conversation?.id;
	const toolName = data.tool?.name;
	const contextDef = data.contextDefinition;
	const reason = data.reason || "Unknown error";
	const text = `Conversation ${eventType.replace("conversation.", "").replace(".error", "")} error: ${reason}`;
	const baseProjectUrl = buildProjectUrl(ctx);
	const convUrl = conversationId ? `${baseProjectUrl}/traces/conversations/${conversationId}` : void 0;
	const fields = [{
		type: "mrkdwn",
		text: `*Error Type:*\n${escapeSlackMrkdwn(eventType)}`
	}, {
		type: "mrkdwn",
		text: `*Reason:*\n${escapeSlackMrkdwn(reason)}`
	}];
	if (toolName) fields.push({
		type: "mrkdwn",
		text: `*Tool:*\n${escapeSlackMrkdwn(toolName)}`
	});
	if (contextDef?.id) fields.push({
		type: "mrkdwn",
		text: `*Context Definition:*\n${escapeSlackMrkdwn(contextDef.id)}`
	});
	const blocks = [{
		type: "header",
		text: {
			type: "plain_text",
			text: "Conversation Error"
		}
	}, {
		type: "section",
		fields
	}];
	if (convUrl) blocks.push({
		type: "section",
		text: {
			type: "mrkdwn",
			text: `<${convUrl}|View Conversation>`
		}
	});
	blocks.push({
		type: "context",
		elements: [{
			type: "mrkdwn",
			text: "Inkeep"
		}]
	});
	return {
		text,
		blocks
	};
}
function buildSlackPayload(eventType, envelope, ctx, meta) {
	const data = envelope.data ?? {};
	let slackFields;
	switch (eventType) {
		case "conversation.created":
		case "conversation.updated":
			slackFields = buildConversationSlack(data, eventType, ctx);
			break;
		case "feedback.created":
			slackFields = buildFeedbackSlack(data, ctx);
			break;
		case "event.created":
			slackFields = buildEventSlack(data, ctx);
			break;
		case "evaluation.failed":
			slackFields = buildEvaluationFailedSlack(data, ctx, meta);
			break;
		case "conversation.execution.error":
		case "conversation.generation.error":
		case "conversation.tool.error":
		case "conversation.context.error":
			slackFields = buildConversationErrorSlack(data, eventType, ctx);
			break;
		default: slackFields = {
			text: `[${eventType}] event fired`,
			blocks: []
		};
	}
	return {
		...envelope,
		...slackFields
	};
}

//#endregion
export { SLACK_INCOMING_WEBHOOK_URL_PREFIX, buildSlackPayload, buildTestSlackPayload, isSlackIncomingWebhookUrl };