"use strict";
/**
 * OpenRouter LLM Provider
 * Handles communication with OpenRouter API for LLM interactions
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenRouterProvider = void 0;
const axios_1 = __importDefault(require("axios"));
class OpenRouterProvider {
    constructor() {
        this.apiKey = '';
        this.model = '';
        this.temperature = 0.7;
        this.maxTokens = 800;
    }
    /**
     * Initialize the OpenRouter provider with configuration
     */
    async initialize(config) {
        this.apiKey = config.apiKey;
        this.model = config.model;
        this.temperature = config.temperature ?? 0.7;
        this.maxTokens = config.maxTokens ?? 800;
        this.client = axios_1.default.create({
            baseURL: 'https://openrouter.ai/api/v1',
            headers: {
                'Authorization': `Bearer ${this.apiKey}`,
                'Content-Type': 'application/json'
            }
        });
    }
    /**
     * Translate player input into a structured action for the game engine
     */
    async translateAction(request) {
        try {
            const prompt = this.buildActionPrompt(request);
            const response = await this.client.post('/chat/completions', {
                model: this.model,
                messages: [
                    {
                        role: 'system',
                        content: prompt.system
                    },
                    {
                        role: 'user',
                        content: prompt.user
                    }
                ],
                temperature: 0.2, // Lower temperature for more deterministic outputs
                max_tokens: 150,
                response_format: { type: 'json_object' }
            });
            const content = response.data.choices[0].message.content;
            const parsedResponse = JSON.parse(content);
            return this.validateActionResponse(parsedResponse);
        }
        catch (error) {
            console.error('Error translating action:', error);
            // Fallback to a simple "look" action when errors occur
            return {
                action: 'look',
                confidence: 0.5
            };
        }
    }
    /**
     * Generate narrative prose based on game events
     */
    async generateNarrative(request) {
        try {
            const prompt = this.buildNarrativePrompt(request);
            const response = await this.client.post('/chat/completions', {
                model: this.model,
                messages: [
                    {
                        role: 'system',
                        content: prompt.system
                    },
                    {
                        role: 'user',
                        content: prompt.user
                    }
                ],
                temperature: this.temperature,
                max_tokens: this.maxTokens
            });
            const content = response.data.choices[0].message.content;
            // Check if response is JSON format or plain text
            try {
                const parsedResponse = JSON.parse(content);
                return {
                    text: parsedResponse.text,
                    suggestions: parsedResponse.suggestions || []
                };
            }
            catch {
                // Plain text response, just use the content directly
                return {
                    text: content
                };
            }
        }
        catch (error) {
            console.error('Error generating narrative:', error);
            return {
                text: `Something happened, but the narrator is at a loss for words. (Error: ${error instanceof Error ? error.message : String(error)})`
            };
        }
    }
    /**
     * Build the system and user prompts for action translation
     */
    buildActionPrompt(request) {
        const systemPrompt = `You are an AI assistant that translates natural language input into structured action commands for an interactive fiction game. 
Your task is to convert player input into a JSON object representing an action that can be understood by the game engine.

The player is currently in the "${request.currentRoom}" room.
Visible objects: ${request.visibleObjects.join(', ')}
Visible characters: ${request.visibleCharacters.join(', ')}
Inventory: ${request.inventory.join(', ')}
Available actions: ${request.possibleActions.join(', ')}

Game context: ${request.gameContext}

Respond ONLY with a JSON object that follows this structure:
{
  "action": "string", // Name of the action (e.g., "take", "examine", "go", "talk", etc.)
  "object": "string", // Optional: Primary object of the action
  "target": "string", // Optional: Secondary object/target of the action
  "parameters": {}, // Optional: Additional parameters as key-value pairs
  "confidence": number // How confident you are in this interpretation (0.0-1.0)
}

Choose the action from the list of available actions. If the player's input is ambiguous or doesn't map well to an available action, choose the closest match and set a lower confidence score.`;
        const userPrompt = request.playerInput;
        return {
            system: systemPrompt,
            user: userPrompt
        };
    }
    /**
     * Build the system and user prompts for narrative generation
     */
    buildNarrativePrompt(request) {
        const tone = request.tone || 'descriptive';
        const systemPrompt = `You are an AI assistant that generates engaging narrative prose for an interactive fiction game. 
Your task is to describe what happens when a player performs an action in the game world.

Craft a vivid, ${tone} description that tells the player what happened as a result of their action. Make your prose engaging and atmospheric.

Current room description: "${request.roomDescription}"
Visible objects: ${request.visibleObjects.join(', ')}
Visible characters: ${request.visibleCharacters.join(', ')}

${request.previousContext ? `Previous context: ${request.previousContext}` : ''}

Respond with engaging prose that describes the outcome of the player's action. 
You can optionally include 1-3 subtle hints about interesting things to try next.`;
        const userPrompt = `The player has performed this action: "${request.action}".
The result of the action is: "${request.result}".
Please describe what happens in an engaging, narrative way.`;
        return {
            system: systemPrompt,
            user: userPrompt
        };
    }
    /**
     * Validate and normalize the action response
     */
    validateActionResponse(response) {
        const validatedResponse = {
            action: typeof response.action === 'string' ? response.action : 'look',
            confidence: typeof response.confidence === 'number' ? response.confidence : 0.5
        };
        if (typeof response.object === 'string') {
            validatedResponse.object = response.object;
        }
        if (typeof response.target === 'string') {
            validatedResponse.target = response.target;
        }
        if (response.parameters && typeof response.parameters === 'object') {
            validatedResponse.parameters = response.parameters;
        }
        return validatedResponse;
    }
}
exports.OpenRouterProvider = OpenRouterProvider;
//# sourceMappingURL=openrouter-provider.js.map