/**
 * one-shot.ts — `olam ask "<question>"` answers once and exits.
 *
 * Script-friendly: prints the answer to stdout, exits 0 on success and
 * non-zero on failure (SDK unavailable → exit 1 with an actionable message).
 */
import { KNOWLEDGE_PACK } from './knowledge-pack.generated.js';
import { assembleSystemPrompt } from './system-prompt.js';
import { findOlamCheckoutRoot } from './checkout.js';
import { askOnce, AskSdkUnavailableError } from './sdk-client.js';
import { printError } from '../output.js';
import { EXIT_GENERIC_ERROR } from '../exit-codes.js';
/**
 * Run a single question. Returns the process exit code (0 success, 1 failure)
 * rather than calling process.exit directly, so it is unit-testable.
 */
export async function runOneShot(question, deps = {}) {
    const write = deps.write ?? ((s) => void process.stdout.write(s));
    const trimmed = question.trim();
    if (trimmed.length === 0) {
        printError('olam ask: empty question. Usage: olam ask "<your question>"');
        return EXIT_GENERIC_ERROR;
    }
    const repoRoot = deps.checkoutRoot !== undefined ? deps.checkoutRoot : findOlamCheckoutRoot();
    const systemPromptAppend = assembleSystemPrompt({
        knowledgePack: KNOWLEDGE_PACK,
        insideCheckout: repoRoot !== null,
        repoRoot: repoRoot ?? undefined,
    });
    try {
        const answer = await askOnce(trimmed, {
            systemPromptAppend,
            repoRoot: repoRoot ?? undefined,
            queryFn: deps.queryFn,
        }, (chunk) => write(chunk));
        // Ensure a trailing newline even if the stream didn't end with one.
        if (!answer.endsWith('\n'))
            write('\n');
        return 0;
    }
    catch (err) {
        if (err instanceof AskSdkUnavailableError) {
            printError(err.message);
            return EXIT_GENERIC_ERROR;
        }
        printError(err instanceof Error ? err.message : String(err));
        return EXIT_GENERIC_ERROR;
    }
}
//# sourceMappingURL=one-shot.js.map