soulforge-framework
Version:
A TypeScript framework for building digital beings with persistent memory, emotions, and identity. Create AI companions that grow, learn, and form genuine relationships.
120 lines • 5.17 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmpathyAPI = void 0;
// Empathy API Example - Building emotional intelligence systems
const src_1 = require("../src");
class EmpathyAPI {
constructor() {
this.souls = new Map();
}
/**
* Create a new empathetic entity
*/
createEntity(id, name, empathyLevel = 80) {
const soul = new src_1.Soul()
.withIdentity({ name, role: 'Empathetic Entity' })
.withEmpathy(empathyLevel)
.withPersonality({
bigFive: {
openness: 75,
conscientiousness: 60,
extraversion: 50,
agreeableness: 90, // High agreeableness for empathy
neuroticism: 30
}
});
this.souls.set(id, soul);
return soul;
}
/**
* Process emotional input and provide empathetic response
*/
processEmotionalInput(entityId, input, userId, userName, emotionalContext) {
const soul = this.souls.get(entityId);
if (!soul)
throw new Error('Entity not found');
// Enhanced emotional analysis with context
const emotionalAnalysis = this.analyzeEmotionalState(input, emotionalContext);
// Generate empathetic response
const response = soul.respond(input, userId, userName);
// Calculate empathy score based on response appropriateness
const empathyScore = this.calculateEmpathyScore(emotionalAnalysis, response, soul.getStatus().emotionalState);
return {
empathyScore,
response: response.response,
detectedEmotion: emotionalAnalysis.emotion,
supportLevel: emotionalAnalysis.supportNeeded
};
}
/**
* Get entity's emotional intelligence metrics
*/
getEmotionalIntelligence(entityId) {
const soul = this.souls.get(entityId);
if (!soul)
throw new Error('Entity not found');
const status = soul.getStatus();
const personality = status.personality;
return {
empathyLevel: 80, // From soul configuration
emotionalAwareness: 75, // Based on recent interactions
responseQuality: 70, // Based on conversation history
adaptability: 65 // Based on personality changes
};
}
analyzeEmotionalState(input, context) {
const lowerInput = input.toLowerCase();
// Enhanced emotion detection
if (lowerInput.includes('sad') || lowerInput.includes('depressed') || lowerInput.includes('cry')) {
return { emotion: 'sadness', intensity: 80, supportNeeded: 'high' };
}
if (lowerInput.includes('angry') || lowerInput.includes('frustrated') || lowerInput.includes('mad')) {
return { emotion: 'anger', intensity: 70, supportNeeded: 'medium' };
}
if (lowerInput.includes('anxious') || lowerInput.includes('worried') || lowerInput.includes('scared')) {
return { emotion: 'anxiety', intensity: 75, supportNeeded: 'high' };
}
if (lowerInput.includes('happy') || lowerInput.includes('excited') || lowerInput.includes('joy')) {
return { emotion: 'joy', intensity: 60, supportNeeded: 'low' };
}
// Use context if provided
if (context) {
const intensity = context.intensity;
const supportLevel = intensity > 70 ? 'high' : intensity > 40 ? 'medium' : 'low';
return {
emotion: context.userMood,
intensity,
supportNeeded: supportLevel
};
}
return { emotion: 'neutral', intensity: 30, supportNeeded: 'low' };
}
calculateEmpathyScore(emotionalAnalysis, response, entityEmotionalState) {
let score = 50; // Base score
// Bonus for mood alignment
if (entityEmotionalState.mood === 'contemplative' && emotionalAnalysis.supportNeeded === 'high') {
score += 20;
}
// Bonus for appropriate response length
if (response.response.length > 50 && emotionalAnalysis.supportNeeded === 'high') {
score += 15;
}
// Bonus for emotional words in response
const emotionalWords = ['understand', 'feel', 'sorry', 'care', 'support'];
const responseWords = response.response.toLowerCase();
emotionalWords.forEach(word => {
if (responseWords.includes(word))
score += 5;
});
return Math.min(100, score);
}
}
exports.EmpathyAPI = EmpathyAPI;
// Example usage
console.log('=== Empathy API Example ===');
const empathyAPI = new EmpathyAPI();
const therapist = empathyAPI.createEntity('therapist-1', 'Dr. Emma', 95);
const result1 = empathyAPI.processEmotionalInput('therapist-1', 'I\'ve been feeling really sad lately and don\'t know what to do', 'user1', 'John', { userMood: 'melancholic', intensity: 85 });
console.log('Empathy Response:', result1);
console.log('Emotional Intelligence:', empathyAPI.getEmotionalIntelligence('therapist-1'));
//# sourceMappingURL=empathy-api.js.map
;