//#region src/session/session-context-for-llm.ts
function isTranscriptContextEntry(x) {
	if (!x || typeof x !== "object") return false;
	return x.kind === "context";
}
const LLM_ROLES = new Set([
	"user",
	"assistant",
	"system",
	"tool",
	"toolResult"
]);
function isLikelyAgentMessage(x) {
	if (!x || typeof x !== "object") return false;
	const role = x.role;
	return typeof role === "string" && LLM_ROLES.has(role);
}
/**
* Normalize a JSON array from on-disk transcript into stored rows (drops unrecognized objects).
*/
function transcriptRowsFromJsonArray(arr) {
	const out = [];
	for (const x of arr) {
		if (isTranscriptContextEntry(x)) {
			out.push(x);
			continue;
		}
		if (isLikelyAgentMessage(x)) out.push(x);
	}
	return out;
}
/** Messages only — what providers and pi-agent should see. */
function buildSessionContextForLlm(rows) {
	const out = [];
	for (const r of rows) if (!isTranscriptContextEntry(r)) out.push(r);
	return out;
}
/**
* When persisting LLM messages, keep prior `kind: 'context'` rows in their relative positions:
* each non-context slot in the previous file is replaced by the next incoming LLM message;
* trailing new LLM rows are appended. Extra old LLM rows are dropped if the new list is shorter.
*/
function mergeLlmMessagesPreservingContextRows(prevRows, llmMessages) {
	let i = 0;
	const out = [];
	for (const r of prevRows) if (isTranscriptContextEntry(r)) out.push(r);
	else if (i < llmMessages.length) {
		out.push(llmMessages[i]);
		i += 1;
	}
	while (i < llmMessages.length) {
		out.push(llmMessages[i]);
		i += 1;
	}
	return out;
}
//#endregion
export { buildSessionContextForLlm, isTranscriptContextEntry, mergeLlmMessagesPreservingContextRows, transcriptRowsFromJsonArray };

//# sourceMappingURL=session-context-for-llm.js.map