import { a as NODMIX_RUNTIME_CONTEXT_NOTICE, c as extractInternalRuntimeContext, i as NODMIX_RUNTIME_CONTEXT_CUSTOM_TYPE, o as NODMIX_RUNTIME_EVENT_HEADER, r as NODMIX_NEXT_TURN_RUNTIME_CONTEXT_HEADER } from "./internal-runtime-context-8Wgvne04.js";
//#region src/agents/embedded-agent-runner/run/runtime-context-prompt.ts
const NODMIX_RUNTIME_EVENT_USER_PROMPT = "Continue the Nodmix runtime event.";
function buildCurrentInboundPromptContextPrefix(context, options) {
	return (options?.preferResumableText === true ? context?.resumableText ?? context?.text : context?.text)?.trim() ?? "";
}
function buildCurrentInboundPrompt(params) {
	const prefix = buildCurrentInboundPromptContextPrefix(params.context, { preferResumableText: params.preferResumableText });
	if (!prefix) return params.prompt;
	if (!params.prompt) return prefix;
	return [prefix, params.prompt].join(params.context?.promptJoiner ?? "\n\n");
}
function removeLastPromptOccurrence(text, prompt) {
	const index = text.lastIndexOf(prompt);
	if (index === -1) return null;
	return [text.slice(0, index).trimEnd(), text.slice(index + prompt.length).trimStart()].filter((part) => part.length > 0).join("\n\n").trim();
}
function resolveRuntimeContextPromptParts(params) {
	const transcriptPrompt = params.transcriptPrompt;
	const shouldExtractInternalRuntimeContext = transcriptPrompt !== void 0;
	const extracted = shouldExtractInternalRuntimeContext ? extractInternalRuntimeContext(params.effectivePrompt) : { text: params.effectivePrompt };
	const modelPrompt = params.modelPrompt === void 0 ? void 0 : shouldExtractInternalRuntimeContext ? extractInternalRuntimeContext(params.modelPrompt) : { text: params.modelPrompt };
	const modelPromptText = modelPrompt?.text ?? transcriptPrompt ?? extracted.text;
	const prompt = transcriptPrompt ?? extracted.text;
	if (!prompt.trim() && params.emptyTranscriptMode === "model-prompt") return {
		prompt: extracted.text,
		...modelPromptText.trim() && modelPromptText !== extracted.text ? { modelPrompt: modelPromptText } : {},
		...extracted.runtimeContext ? { runtimeContext: extracted.runtimeContext } : {}
	};
	const runtimeContext = [modelPrompt ? removeLastPromptOccurrence(extracted.text, modelPrompt.text)?.trim() ?? (transcriptPrompt ? removeLastPromptOccurrence(extracted.text, transcriptPrompt)?.trim() : void 0) : transcriptPrompt ? removeLastPromptOccurrence(extracted.text, transcriptPrompt)?.trim() : void 0, extracted.runtimeContext].filter((value) => Boolean(value?.trim())).join("\n\n") || (!prompt.trim() ? extracted.text.trim() : void 0);
	if (!prompt.trim()) return runtimeContext ? {
		prompt: NODMIX_RUNTIME_EVENT_USER_PROMPT,
		...modelPromptText.trim() && modelPromptText !== NODMIX_RUNTIME_EVENT_USER_PROMPT ? { modelPrompt: modelPromptText } : {},
		runtimeContext,
		runtimeOnly: true,
		runtimeSystemContext: buildRuntimeEventSystemContext(runtimeContext)
	} : {
		prompt: "",
		...modelPromptText ? { modelPrompt: modelPromptText } : {}
	};
	return {
		prompt,
		...modelPromptText.trim() && modelPromptText !== prompt ? { modelPrompt: modelPromptText } : {},
		...runtimeContext ? { runtimeContext } : {}
	};
}
function buildRuntimeContextMessageContent(params) {
	return [
		params.kind === "runtime-event" ? NODMIX_RUNTIME_EVENT_HEADER : NODMIX_NEXT_TURN_RUNTIME_CONTEXT_HEADER,
		NODMIX_RUNTIME_CONTEXT_NOTICE,
		"",
		params.runtimeContext
	].join("\n");
}
function buildRuntimeContextSystemContext(runtimeContext) {
	return buildRuntimeContextMessageContent({
		runtimeContext,
		kind: "next-turn"
	});
}
function buildRuntimeEventSystemContext(runtimeContext) {
	return buildRuntimeContextMessageContent({
		runtimeContext,
		kind: "runtime-event"
	});
}
function buildRuntimeContextCustomMessage(runtimeContext) {
	const trimmedRuntimeContext = runtimeContext?.trim();
	if (!trimmedRuntimeContext) return;
	return {
		role: "custom",
		customType: NODMIX_RUNTIME_CONTEXT_CUSTOM_TYPE,
		content: buildRuntimeContextSystemContext(trimmedRuntimeContext),
		display: false,
		details: { source: "nodmix-runtime-context" },
		timestamp: Date.now()
	};
}
//#endregion
export { buildRuntimeEventSystemContext as a, buildRuntimeContextSystemContext as i, buildCurrentInboundPromptContextPrefix as n, resolveRuntimeContextPromptParts as o, buildRuntimeContextCustomMessage as r, buildCurrentInboundPrompt as t };
