import { r as formatErrorMessage } from "./errors-Z0lU1pH3.js";
import { a as readSessionTranscriptRunId, s as resolveTerminalAssistantTranscriptRunId } from "./transcript-events-Dh6dX7iG.js";
import { D as persistSessionTranscriptTurn } from "./session-accessor-Dwfzn6Ej.js";
import { t as extractAssistantPhaseText } from "./chat-message-content-Dws_XUEQ.js";
import { t as applyAssistantDeliveryDirectives } from "./transcript-assistant-delivery-XLYl1NiZ.js";
//#region src/gateway/server-methods/chat-transcript-inject.ts
function resolveInjectedAssistantContent(params) {
	const labelPrefix = params.label ? `[${params.label}]\n\n` : "";
	if (params.content && params.content.length > 0) {
		if (!labelPrefix) return params.content;
		const first = params.content[0];
		if (first && typeof first === "object" && first.type === "text" && typeof first.text === "string") return [{
			...first,
			text: `${labelPrefix}${first.text}`
		}, ...params.content.slice(1)];
		return [{
			type: "text",
			text: labelPrefix.trim()
		}, ...params.content];
	}
	return [{
		type: "text",
		text: `${labelPrefix}${params.message}`
	}];
}
/** Clone Gateway display blocks into the transcript's assistant-content boundary. */
function prepareGatewayInjectedAssistantContent(content) {
	return content.map((block) => Object.assign({}, block));
}
/** Append a gateway-authored assistant message while preserving transcript parent links. */
async function appendInjectedAssistantMessageToTranscript(params) {
	const now = params.now ?? Date.now();
	const usage = {
		input: 0,
		output: 0,
		cacheRead: 0,
		cacheWrite: 0,
		totalTokens: 0,
		cost: {
			input: 0,
			output: 0,
			cacheRead: 0,
			cacheWrite: 0,
			total: 0
		}
	};
	const resolvedContent = resolveInjectedAssistantContent({
		message: params.message,
		label: params.label,
		content: params.content
	});
	const rawDeliveryFacts = applyAssistantDeliveryDirectives({
		role: "assistant",
		content: [{
			type: "text",
			text: params.message
		}]
	}).openclawDelivery;
	const abortRunId = params.abortMeta?.runId;
	const messageBody = applyAssistantDeliveryDirectives({
		role: "assistant",
		content: prepareGatewayInjectedAssistantContent(resolvedContent),
		timestamp: now,
		stopReason: "stop",
		usage,
		api: "openai-responses",
		provider: "openclaw",
		model: "gateway-injected",
		...params.idempotencyKey ? { idempotencyKey: params.idempotencyKey } : {},
		...params.ttsSupplement ? { openclawTtsSupplement: params.ttsSupplement } : {},
		...params.abortMeta ? { openclawAbort: {
			aborted: true,
			origin: params.abortMeta.origin,
			runId: params.abortMeta.runId
		} } : {}
	});
	if (rawDeliveryFacts && messageBody.openclawDelivery === void 0) messageBody.openclawDelivery = rawDeliveryFacts;
	try {
		if (!params.transcriptPath && (!params.storePath || !params.sessionId || !params.sessionKey)) return {
			ok: false,
			error: "transcript identity not resolved"
		};
		let predicateDeclined = false;
		const appended = (await persistSessionTranscriptTurn({
			sessionKey: params.sessionKey ?? "",
			...params.transcriptPath ? { sessionFile: params.transcriptPath } : {},
			...params.storePath ? { storePath: params.storePath } : {},
			...params.sessionId ? { sessionId: params.sessionId } : {},
			...params.agentId ? { agentId: params.agentId } : {}
		}, {
			updateMode: "inline",
			...params.abortMeta ? { runId: params.abortMeta.runId } : {},
			touchSessionEntry: Boolean(params.storePath && params.sessionId && params.sessionKey),
			...params.config ? { config: params.config } : {},
			messages: [{
				message: messageBody,
				idempotencyLookup: "scan-assistant",
				...params.abortMeta ? { shouldAppendInTransaction: (latestAssistantMessage) => {
					const committedRunId = resolveTerminalAssistantTranscriptRunId(latestAssistantMessage, readSessionTranscriptRunId(latestAssistantMessage));
					const committedText = extractAssistantPhaseText(latestAssistantMessage)?.trim();
					predicateDeclined = committedRunId === abortRunId && committedText === params.message.trim();
					return !predicateDeclined;
				} } : {},
				now,
				useRawWhenLinear: true
			}]
		})).messages[0];
		if (!appended) {
			if (predicateDeclined) return {
				ok: true,
				skipped: true
			};
			return {
				ok: false,
				error: "gateway-injected assistant message was not appended"
			};
		}
		return {
			ok: true,
			messageId: appended.messageId,
			message: appended.message
		};
	} catch (err) {
		return {
			ok: false,
			error: formatErrorMessage(err)
		};
	}
}
//#endregion
export { prepareGatewayInjectedAssistantContent as n, appendInjectedAssistantMessageToTranscript as t };
