Initial commit

This commit is contained in:
2025-04-01 08:37:41 +02:00
commit 39c1b6ff0a
59 changed files with 14076 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
/**
* OpenRouter LLM Provider
* Handles communication with OpenRouter API for LLM interactions
*/
import { LlmProvider, LlmConfig, ActionRequest, ActionResponse, NarrativeRequest, NarrativeResponse } from '../interfaces/llm';
export declare class OpenRouterProvider implements LlmProvider {
private apiKey;
private model;
private client;
private temperature;
private maxTokens;
/**
* Initialize the OpenRouter provider with configuration
*/
initialize(config: LlmConfig): Promise<void>;
/**
* Translate player input into a structured action for the game engine
*/
translateAction(request: ActionRequest): Promise<ActionResponse>;
/**
* Generate narrative prose based on game events
*/
generateNarrative(request: NarrativeRequest): Promise<NarrativeResponse>;
/**
* Build the system and user prompts for action translation
*/
private buildActionPrompt;
/**
* Build the system and user prompts for narrative generation
*/
private buildNarrativePrompt;
/**
* Validate and normalize the action response
*/
private validateActionResponse;
}
+192
View File
@@ -0,0 +1,192 @@
"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
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"openrouter-provider.js","sourceRoot":"","sources":["../../src/llm/openrouter-provider.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;AAEH,kDAA6C;AAU7C,MAAa,kBAAkB;IAA/B;QACU,WAAM,GAAW,EAAE,CAAC;QACpB,UAAK,GAAW,EAAE,CAAC;QAEnB,gBAAW,GAAW,GAAG,CAAC;QAC1B,cAAS,GAAW,GAAG,CAAC;IA+LlC,CAAC;IA7LC;;OAEG;IACI,KAAK,CAAC,UAAU,CAAC,MAAiB;QACvC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;QAC5B,IAAI,CAAC,KAAK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