/**
 * RunAgentPromptPlaygroundUseCase
 *
 * Streams a chat completion against the configured executor for an
 * agent type. The user can supply an inline `promptBody` to test an
 * unsaved override; otherwise the resolver falls back to the bundled
 * prompt for the requested slot (or the slot's stored override when
 * one already exists).
 *
 * Operates on an isolated transcript — does NOT touch agent_runs,
 * agent_messages, or supervisor_decisions tables (FR-40, NFR-18).
 */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { inject, injectable } from 'tsyringe';
import { getBuiltinPrompt, isKnownPromptSlot } from '../../services/builtin-prompt-registry.js';
let RunAgentPromptPlaygroundUseCase = class RunAgentPromptPlaygroundUseCase {
    executorProvider;
    promptResolver;
    constructor(executorProvider, promptResolver) {
        this.executorProvider = executorProvider;
        this.promptResolver = promptResolver;
    }
    async *execute(input) {
        if (!input.agentType.trim())
            throw new Error('agentType is required');
        if (!input.promptId.trim())
            throw new Error('promptId is required');
        if (!Array.isArray(input.messages) || input.messages.length === 0) {
            throw new Error('messages must be a non-empty array');
        }
        if (!isKnownPromptSlot(input.agentType, input.promptId)) {
            throw new Error(`Unknown prompt slot: ${input.agentType}/${input.promptId}. ` +
                'Only slots in the built-in registry can be played with.');
        }
        const lastUser = [...input.messages].reverse().find((m) => m.role === 'user');
        if (!lastUser?.content?.trim()) {
            throw new Error('messages must end with a non-empty user turn');
        }
        const builtin = getBuiltinPrompt(input.agentType, input.promptId);
        const systemPrompt = input.promptBody?.length
            ? input.promptBody
            : await this.promptResolver.resolve(input.agentType, input.promptId, builtin?.body ?? '');
        yield { type: 'system', content: systemPrompt };
        const executor = await this.executorProvider.getExecutor();
        const transcript = renderTranscript(input.messages);
        try {
            for await (const event of executor.executeStream(transcript, {
                ...(input.modelId && { model: input.modelId }),
                systemPrompt,
                silent: true,
                // Playground runs are isolated — no MCP tools, no FS sandbox needed.
                disableMcp: true,
            })) {
                if (event.type === 'error') {
                    yield { type: 'error', content: event.content };
                    return;
                }
                if (event.content.length > 0) {
                    yield { type: 'delta', content: event.content };
                }
                if (event.type === 'result') {
                    yield { type: 'done', content: '' };
                    return;
                }
            }
            yield { type: 'done', content: '' };
        }
        catch (error) {
            const message = error instanceof Error ? error.message : String(error);
            yield { type: 'error', content: message };
        }
    }
};
RunAgentPromptPlaygroundUseCase = __decorate([
    injectable(),
    __param(0, inject('IAgentExecutorProvider')),
    __param(1, inject('IAgentPromptResolver')),
    __metadata("design:paramtypes", [Object, Object])
], RunAgentPromptPlaygroundUseCase);
export { RunAgentPromptPlaygroundUseCase };
function renderTranscript(messages) {
    return messages
        .map((m) => `${m.role === 'assistant' ? 'Assistant' : m.role === 'user' ? 'User' : 'System'}: ${m.content}`)
        .join('\n\n');
}
