/**
 * Mock agent client for testing
 *
 * Returns immediate fixed responses without any API calls.
 * Useful for testing workflows without incurring costs or latency.
 */
import { randomUUID } from 'node:crypto';
import { getScenarioQueue } from './scenario.js';
/**
 * Generate a mock session ID
 */
function generateMockSessionId() {
    return `mock-session-${randomUUID()}`;
}
async function delayWithAbort(ms, signal) {
    return new Promise((resolve, reject) => {
        if (signal?.aborted) {
            reject(new DOMException('Aborted', 'AbortError'));
            return;
        }
        const timer = setTimeout(resolve, ms);
        signal?.addEventListener('abort', () => {
            clearTimeout(timer);
            reject(new DOMException('Aborted', 'AbortError'));
        }, { once: true });
    });
}
/**
 * Call mock agent - returns immediate fixed response
 */
export async function callMock(personaName, prompt, options) {
    const sessionId = options.sessionId ?? generateMockSessionId();
    // Scenario queue takes priority over explicit options
    const scenarioEntry = getScenarioQueue()?.consume(personaName);
    // Apply artificial delay if specified (respects abortSignal)
    if (scenarioEntry?.delayMs) {
        try {
            await delayWithAbort(scenarioEntry.delayMs, options.abortSignal);
        }
        catch (e) {
            if (e instanceof DOMException && e.name === 'AbortError') {
                return {
                    persona: personaName,
                    status: 'blocked',
                    content: '[MOCK:ABORTED]\n\nMock response interrupted by abort signal.',
                    timestamp: new Date(),
                    sessionId,
                };
            }
            throw e;
        }
    }
    const status = scenarioEntry?.status ?? options.mockStatus ?? 'done';
    const statusMarker = `[MOCK:${status.toUpperCase()}]`;
    const allowedToolsSuffix = options.allowedTools && options.allowedTools.length > 0
        ? `\nAllowed tools: ${options.allowedTools.join(', ')}`
        : '';
    const content = scenarioEntry?.content ?? options.mockResponse ??
        `${statusMarker}\n\nMock response for persona "${personaName}".\nPrompt: ${prompt.slice(0, 100)}${prompt.length > 100 ? '...' : ''}${allowedToolsSuffix}`;
    // Emit stream events if callback is provided
    if (options.onStream) {
        const initEvent = {
            type: 'init',
            data: { model: 'mock-model', sessionId },
        };
        options.onStream(initEvent);
        const textEvent = {
            type: 'text',
            data: { text: content },
        };
        options.onStream(textEvent);
        const resultEvent = {
            type: 'result',
            data: { success: true, result: content, sessionId },
        };
        options.onStream(resultEvent);
    }
    return {
        persona: personaName,
        status,
        content,
        timestamp: new Date(),
        sessionId,
        structuredOutput: scenarioEntry?.structuredOutput ?? options.structuredOutput,
        error: scenarioEntry?.error ?? options.error,
        failureCategory: scenarioEntry?.failureCategory ?? options.failureCategory,
    };
}
/**
 * Call mock agent with custom system prompt (same as callMock for mock provider)
 */
export async function callMockCustom(personaName, prompt, _systemPrompt, options) {
    // For mock, system prompt is ignored - just return fixed response
    return callMock(personaName, prompt, options);
}
//# sourceMappingURL=client.js.map