import { a as OPENCLAW_RUNTIME_CONTEXT_NOTICE, c as extractInternalRuntimeContext, i as OPENCLAW_RUNTIME_CONTEXT_CUSTOM_TYPE, o as OPENCLAW_RUNTIME_EVENT_HEADER, r as OPENCLAW_NEXT_TURN_RUNTIME_CONTEXT_HEADER } from "./internal-runtime-context-hfmw8MFR.js";
//#region src/agents/embedded-agent-runner/run/runtime-context-prompt.ts
const OPENCLAW_RUNTIME_EVENT_USER_PROMPT = "Continue the OpenClaw runtime event.";
/** Returns the visible or resumable inbound prompt prefix used before the user prompt. */
function buildCurrentInboundPromptContextPrefix(context, options) {
	return (options?.preferResumableText === true ? context?.resumableText ?? context?.text : context?.text)?.trim() ?? "";
}
/** Combines inbound context and the current prompt using the channel-provided joiner. */
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();
}
/**
* Separates user-authored prompt text from hidden runtime context. Transcript
* prompt stays user-visible; model prompt may carry runtime-only additions that
* should be delivered as hidden context instead of persisted as user text.
*/
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: OPENCLAW_RUNTIME_EVENT_USER_PROMPT,
		...modelPromptText.trim() && modelPromptText !== OPENCLAW_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" ? OPENCLAW_RUNTIME_EVENT_HEADER : OPENCLAW_NEXT_TURN_RUNTIME_CONTEXT_HEADER,
		OPENCLAW_RUNTIME_CONTEXT_NOTICE,
		"",
		params.runtimeContext
	].join("\n");
}
/** Builds the hidden next-turn system context payload for model conversion. */
function buildRuntimeContextSystemContext(runtimeContext) {
	return buildRuntimeContextMessageContent({
		runtimeContext,
		kind: "next-turn"
	});
}
/** Builds the hidden runtime-event system context payload for empty runtime-only turns. */
function buildRuntimeEventSystemContext(runtimeContext) {
	return buildRuntimeContextMessageContent({
		runtimeContext,
		kind: "runtime-event"
	});
}
/** Creates a non-displayed custom transcript message for runtime context, if any exists. */
function buildRuntimeContextCustomMessage(runtimeContext) {
	const trimmedRuntimeContext = runtimeContext?.trim();
	if (!trimmedRuntimeContext) return;
	return {
		role: "custom",
		customType: OPENCLAW_RUNTIME_CONTEXT_CUSTOM_TYPE,
		content: buildRuntimeContextSystemContext(trimmedRuntimeContext),
		display: false,
		details: { source: "openclaw-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 };
