import { h as stringifyRouteThreadId } from "./channel-route-D7X5briz.js";
import { c as normalizeAccountId } from "./delivery-context.shared-BBXufDA2.js";
import { o as resolveMessageChannel } from "./message-channel-normalize-DtCGllE7.js";
import "./message-channel-D6KA_-3u.js";
import { r as mergeSessionEntry } from "./types-D8S_uNvu.js";
import { c as patchSessionEntry } from "./session-accessor-Dbqbpyiz.js";
import { d as stripInternalRuntimeContext, l as hasInternalRuntimeContext } from "./internal-runtime-context-BH_40W4f.js";
import { _ as formatAgentInternalEventsForPrompt, g as formatAgentInternalEventsForPlainPrompt } from "./subagent-announce-delivery-Bo0emoaM.js";
//#region src/agents/command/attempt-execution.shared.ts
/**
* Shared session persistence and prompt-body helpers for agent attempt
* execution paths.
*/
/** Persists one session entry while keeping the caller's in-memory store aligned. */
function normalizeTranscriptMarkerUpdatedAt(value) {
	return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : void 0;
}
async function persistSessionEntry(params) {
	let rejectedMissingEntry = false;
	const persisted = await patchSessionEntry({
		sessionKey: params.sessionKey,
		storePath: params.storePath
	}, (_entry, context) => {
		if (params.shouldPersist && !params.shouldPersist(context.existingEntry)) {
			rejectedMissingEntry = !context.existingEntry;
			return null;
		}
		const merged = mergeSessionEntry(context.existingEntry, params.entry);
		if (params.preserveTranscriptMarkerUpdatedAt) {
			const currentUpdatedAt = normalizeTranscriptMarkerUpdatedAt(context.existingEntry?.updatedAt);
			const markerUpdatedAt = normalizeTranscriptMarkerUpdatedAt(params.entry.updatedAt);
			if (markerUpdatedAt !== void 0) merged.updatedAt = Math.max(currentUpdatedAt ?? 0, markerUpdatedAt);
		}
		for (const field of params.clearedFields ?? []) if (!Object.hasOwn(params.entry, field)) Reflect.deleteProperty(merged, field);
		return merged;
	}, {
		fallbackEntry: params.sessionStore[params.sessionKey] ?? params.entry,
		replaceEntry: true
	});
	if (rejectedMissingEntry) {
		delete params.sessionStore[params.sessionKey];
		return;
	}
	if (persisted) params.sessionStore[params.sessionKey] = persisted;
	else delete params.sessionStore[params.sessionKey];
	return persisted ?? void 0;
}
/** Prepends hidden internal event context unless the body already carries it. */
function prependInternalEventContext(body, events) {
	if (hasInternalRuntimeContext(body)) return body;
	const renderedEvents = formatAgentInternalEventsForPrompt(events);
	if (!renderedEvents) return body;
	return [renderedEvents, body].filter(Boolean).join("\n\n");
}
function resolvePlainInternalEventBody(body, events) {
	const renderedEvents = formatAgentInternalEventsForPlainPrompt(events);
	if (!renderedEvents) return body;
	return [renderedEvents, stripInternalRuntimeContext(body).trim()].filter(Boolean).join("\n\n") || body;
}
/** Resolves the prompt body submitted to ACP runtimes. */
function resolveAcpPromptBody(body, events) {
	return events?.length ? resolvePlainInternalEventBody(body, events) : body;
}
/** Resolves the body stored in transcripts after internal event rendering. */
function resolveInternalEventTranscriptBody(body, events) {
	if (!hasInternalRuntimeContext(body)) return body;
	return resolvePlainInternalEventBody(body, events);
}
//#endregion
//#region src/agents/command/run-context.ts
/**
* Resolves channel/account/thread run context for agent command execution.
*/
/** Merges explicit run context with command routing options. */
function resolveAgentRunContext(opts) {
	const merged = opts.runContext ? { ...opts.runContext } : {};
	const normalizedChannel = resolveMessageChannel(merged.messageChannel ?? opts.messageChannel, opts.replyChannel ?? opts.channel);
	if (normalizedChannel) merged.messageChannel = normalizedChannel;
	const normalizedAccountId = normalizeAccountId(merged.accountId ?? opts.accountId);
	if (normalizedAccountId) merged.accountId = normalizedAccountId;
	const groupId = (merged.groupId ?? opts.groupId)?.toString().trim();
	if (groupId) merged.groupId = groupId;
	const groupChannel = (merged.groupChannel ?? opts.groupChannel)?.toString().trim();
	if (groupChannel) merged.groupChannel = groupChannel;
	const groupSpace = (merged.groupSpace ?? opts.groupSpace)?.toString().trim();
	if (groupSpace) merged.groupSpace = groupSpace;
	if (merged.currentThreadTs == null && opts.threadId != null && opts.threadId !== "" && opts.threadId !== null) {
		const threadId = stringifyRouteThreadId(opts.threadId);
		if (threadId) merged.currentThreadTs = threadId;
	}
	if (!merged.currentChannelId && opts.to) {
		const trimmedTo = opts.to.trim();
		if (trimmedTo) merged.currentChannelId = trimmedTo;
	}
	return merged;
}
//#endregion
export { resolveInternalEventTranscriptBody as a, resolveAcpPromptBody as i, persistSessionEntry as n, prependInternalEventContext as r, resolveAgentRunContext as t };
