import { appendPiTranscriptMessage } from "../../session/parity/jsonl-transcript-io.js";
import { initSessionTurn } from "../../session/init-session-turn.js";
import "../../session/index.js";
import { buildDirectUserMessageContent } from "./build-direct-message-content.js";
import { hydratePerTurnState, runDirectAgentTurn, tryRunSlashCommand } from "./direct-turn-helpers.js";
//#region src/agent/service/process-direct-one-shot.ts
async function runProcessDirect(deps, input) {
	const { channel, chatId } = deps.parseSessionKey(input.sessionKey);
	deps.initSessionContext(input.sessionKey, channel, chatId);
	try {
		let turnBody = input.content;
		let resetTriggeredAtInit = false;
		const turn = await initSessionTurn({
			cfg: deps.config,
			sessionKey: input.sessionKey,
			body: input.content,
			resetSession: deps.resetSession
		});
		resetTriggeredAtInit = turn.resetTriggered;
		if (turn.bareReset && turn.ackMessage) return turn.ackMessage;
		turnBody = turn.bodyStripped;
		await hydratePerTurnState(deps, input.sessionKey, input.thinking);
		const prepared = await deps.prepareInboundAttachments(input.sessionKey, input.attachments);
		const slash = await tryRunSlashCommand(deps, {
			sessionKey: input.sessionKey,
			channel,
			chatId
		}, turnBody, { skipResetCommands: resetTriggeredAtInit });
		if (slash.matched) {
			const trimmed = slash.aggregatedText.trim();
			if (trimmed) {
				const { absPath } = await deps.sessionStore.resolveTranscriptPath(input.sessionKey);
				await appendPiTranscriptMessage({
					absPath,
					cwd: deps.agentManager.getResolvedWorkspaceForSession(input.sessionKey),
					message: {
						role: "assistant",
						content: [{
							type: "text",
							text: trimmed
						}],
						timestamp: Date.now()
					},
					sessionKey: input.sessionKey
				});
			}
			return slash.aggregatedText ?? "";
		}
		const userMessage = {
			role: "user",
			content: await buildDirectUserMessageContent({
				content: turnBody.trimStart().startsWith("/skill:") ? deps.agentManager.expandSkillUserText(turnBody) : turnBody,
				attachments: prepared,
				sessionKey: input.sessionKey,
				config: deps.config,
				agentManager: deps.agentManager,
				modelManager: deps.modelManager
			}),
			timestamp: Date.now()
		};
		const result = await runDirectAgentTurn({
			...deps,
			config: deps.config
		}, {
			sessionKey: input.sessionKey,
			userMessage
		});
		if (result.lastAssistantText) deps.onTurnComplete?.(input.sessionKey, result.lastAssistantText);
		return result.lastAssistantText ?? "";
	} finally {
		deps.endDirectRequestContext();
	}
}
//#endregion
export { runProcessDirect };

//# sourceMappingURL=process-direct-one-shot.js.map