universal-llm-client 4.0.0 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai-model.d.ts +20 -22
- package/dist/ai-model.d.ts.map +1 -1
- package/dist/ai-model.js +26 -23
- package/dist/ai-model.js.map +1 -1
- package/dist/client.d.ts +5 -5
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +17 -9
- package/dist/client.js.map +1 -1
- package/dist/http.d.ts +2 -0
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +1 -0
- package/dist/http.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/interfaces.d.ts +49 -11
- package/dist/interfaces.d.ts.map +1 -1
- package/dist/interfaces.js +14 -0
- package/dist/interfaces.js.map +1 -1
- package/dist/providers/anthropic.d.ts +56 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +524 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/google.d.ts +5 -0
- package/dist/providers/google.d.ts.map +1 -1
- package/dist/providers/google.js +64 -8
- package/dist/providers/google.js.map +1 -1
- package/dist/providers/index.d.ts +1 -0
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +1 -0
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/ollama.d.ts.map +1 -1
- package/dist/providers/ollama.js +38 -11
- package/dist/providers/ollama.js.map +1 -1
- package/dist/providers/openai.d.ts.map +1 -1
- package/dist/providers/openai.js +9 -7
- package/dist/providers/openai.js.map +1 -1
- package/dist/router.d.ts +13 -33
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +33 -57
- package/dist/router.js.map +1 -1
- package/dist/stream-decoder.d.ts +29 -2
- package/dist/stream-decoder.d.ts.map +1 -1
- package/dist/stream-decoder.js +39 -11
- package/dist/stream-decoder.js.map +1 -1
- package/dist/structured-output.d.ts +107 -181
- package/dist/structured-output.d.ts.map +1 -1
- package/dist/structured-output.js +137 -192
- package/dist/structured-output.js.map +1 -1
- package/dist/zod-adapter.d.ts +44 -0
- package/dist/zod-adapter.d.ts.map +1 -0
- package/dist/zod-adapter.js +61 -0
- package/dist/zod-adapter.js.map +1 -0
- package/package.json +9 -1
- package/src/ai-model.ts +350 -0
- package/src/auditor.ts +213 -0
- package/src/client.ts +402 -0
- package/src/debug/debug-google-streaming.ts +97 -0
- package/src/debug/debug-tool-execution.ts +86 -0
- package/src/debug/test-lmstudio-tools.ts +155 -0
- package/src/demos/README.md +47 -0
- package/src/demos/basic/universal-llm-examples.ts +161 -0
- package/src/demos/mcp/astrid-memory-demo.ts +295 -0
- package/src/demos/mcp/astrid-persona-memory.ts +357 -0
- package/src/demos/mcp/mcp-mongodb-demo.ts +275 -0
- package/src/demos/mcp/simple-astrid-memory.ts +148 -0
- package/src/demos/mcp/simple-mcp-demo.ts +68 -0
- package/src/demos/mcp/working-mcp-demo.ts +62 -0
- package/src/demos/model-alias-demo.ts +0 -0
- package/src/demos/tools/RAG_MEMORY_INTEGRATION.md +267 -0
- package/src/demos/tools/astrid-memory-demo.ts +270 -0
- package/src/demos/tools/astrid-production-memory-clean.ts +785 -0
- package/src/demos/tools/astrid-production-memory.ts +558 -0
- package/src/demos/tools/basic-translation-test.ts +66 -0
- package/src/demos/tools/chromadb-similarity-tuning.ts +390 -0
- package/src/demos/tools/clean-multilingual-conversation.ts +209 -0
- package/src/demos/tools/clean-translation-test.ts +119 -0
- package/src/demos/tools/clean-universal-multilingual-test.ts +131 -0
- package/src/demos/tools/complete-rag-demo.ts +369 -0
- package/src/demos/tools/complete-tool-demo.ts +132 -0
- package/src/demos/tools/demo-tool-calling.ts +124 -0
- package/src/demos/tools/dynamic-language-switching-test.ts +251 -0
- package/src/demos/tools/hybrid-thinking-test.ts +154 -0
- package/src/demos/tools/memory-integration-test.ts +420 -0
- package/src/demos/tools/multilingual-memory-system.ts +802 -0
- package/src/demos/tools/ondemand-translation-demo.ts +655 -0
- package/src/demos/tools/production-tool-demo.ts +245 -0
- package/src/demos/tools/revolutionary-multilingual-test.ts +151 -0
- package/src/demos/tools/rigorous-language-analysis.ts +218 -0
- package/src/demos/tools/test-universal-memory-system.ts +126 -0
- package/src/demos/tools/translation-integration-guide.ts +346 -0
- package/src/demos/tools/universal-memory-system.ts +560 -0
- package/src/http.ts +247 -0
- package/src/index.ts +161 -0
- package/src/interfaces.ts +657 -0
- package/src/mcp.ts +345 -0
- package/src/providers/anthropic.ts +762 -0
- package/src/providers/google.ts +620 -0
- package/src/providers/index.ts +8 -0
- package/src/providers/ollama.ts +469 -0
- package/src/providers/openai.ts +392 -0
- package/src/router.ts +780 -0
- package/src/stream-decoder.ts +361 -0
- package/src/structured-output.ts +759 -0
- package/src/test-scripts/test-advanced-tools.ts +310 -0
- package/src/test-scripts/test-google-streaming-enhanced.ts +147 -0
- package/src/test-scripts/test-google-streaming.ts +63 -0
- package/src/test-scripts/test-google-system-prompt-comprehensive.ts +189 -0
- package/src/test-scripts/test-mcp-config.ts +28 -0
- package/src/test-scripts/test-mcp-connection.ts +29 -0
- package/src/test-scripts/test-system-message-positions.ts +163 -0
- package/src/test-scripts/test-system-prompt-improvement-demo.ts +83 -0
- package/src/test-scripts/test-tool-calling.ts +231 -0
- package/src/tests/ai-model.test.ts +1614 -0
- package/src/tests/auditor.test.ts +224 -0
- package/src/tests/http.test.ts +200 -0
- package/src/tests/interfaces.test.ts +117 -0
- package/src/tests/providers/google.test.ts +660 -0
- package/src/tests/providers/ollama.test.ts +954 -0
- package/src/tests/providers/openai.test.ts +1122 -0
- package/src/tests/router.test.ts +254 -0
- package/src/tests/stream-decoder.test.ts +179 -0
- package/src/tests/structured-output.test.ts +1450 -0
- package/src/tests/tools.test.ts +175 -0
- package/src/tools.ts +246 -0
- package/src/zod-adapter.ts +72 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
|
|
3
|
+
import '@dotenvx/dotenvx/config';
|
|
4
|
+
import {convertToUniversalConfig, loadLLMConfig} from '../../../../../src/config/llm-config.js';
|
|
5
|
+
import {UniversalLLMRouter} from '../../../../../src/services/UniversalLLMRouter.js';
|
|
6
|
+
|
|
7
|
+
async function testUniversalMemorySystem() {
|
|
8
|
+
console.log('🧠 Universal Memory System Test\n');
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
// Wait for universal router to be ready
|
|
12
|
+
console.log('⏳ Waiting for Universal Router and Memory System...');
|
|
13
|
+
const llmConfig = loadLLMConfig();
|
|
14
|
+
const universalConfig = await convertToUniversalConfig(llmConfig);
|
|
15
|
+
const universalRouter = new UniversalLLMRouter(universalConfig);
|
|
16
|
+
await universalRouter.waitForReady(10000);
|
|
17
|
+
console.log('✅ Universal Router is ready!');
|
|
18
|
+
|
|
19
|
+
const systemPrompt = `You are Astrid, an AI companion with advanced memory capabilities.
|
|
20
|
+
|
|
21
|
+
MEMORY INSTRUCTIONS:
|
|
22
|
+
- You have access to persistent memory through RAG (Retrieval-Augmented Generation)
|
|
23
|
+
- Remember important details about conversations for future reference
|
|
24
|
+
- Use memory to provide personalized and contextual responses
|
|
25
|
+
- Mention when you recall something from previous conversations
|
|
26
|
+
|
|
27
|
+
COGNITIVE INSTRUCTIONS:
|
|
28
|
+
- Think in whatever language feels most natural
|
|
29
|
+
- Use <think></think> tags for your internal reasoning
|
|
30
|
+
- Demonstrate your memory capabilities naturally
|
|
31
|
+
|
|
32
|
+
Be warm, intelligent, and show that you remember our interactions!`;
|
|
33
|
+
|
|
34
|
+
// Conversation scenario: Test memory persistence
|
|
35
|
+
const memoryTests = [
|
|
36
|
+
{
|
|
37
|
+
userMessage: "Hi Astrid! My name is Alex and I'm a software developer from Berlin. I love working with AI systems.",
|
|
38
|
+
description: "Initial introduction - establishing user profile"
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
userMessage: "I'm working on a new machine learning project about natural language processing. It's quite challenging!",
|
|
42
|
+
description: "Professional context - adding interests and projects"
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
userMessage: "By the way, I prefer conversations in German sometimes. Wie geht es dir?",
|
|
46
|
+
description: "Language preference - establishing multilingual context"
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
userMessage: "What do you remember about me from our conversation?",
|
|
50
|
+
description: "Memory test - checking recall capabilities"
|
|
51
|
+
}
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
console.log('🧠 Testing Universal Memory System with RAG');
|
|
55
|
+
console.log('━'.repeat(60) + '\n');
|
|
56
|
+
|
|
57
|
+
const conversationHistory: any[] = [
|
|
58
|
+
{ role: 'system', content: systemPrompt }
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
for (let i = 0; i < memoryTests.length; i++) {
|
|
62
|
+
const test = memoryTests[i];
|
|
63
|
+
|
|
64
|
+
console.log(`${i + 1}. ${test.description}`);
|
|
65
|
+
console.log('─'.repeat(40));
|
|
66
|
+
|
|
67
|
+
// Add user message to conversation
|
|
68
|
+
conversationHistory.push({ role: 'user', content: test.userMessage });
|
|
69
|
+
|
|
70
|
+
console.log(`👤 Alex: ${test.userMessage}\n`);
|
|
71
|
+
|
|
72
|
+
// Get AI response using Universal Router
|
|
73
|
+
const response = await universalRouter.chat(
|
|
74
|
+
'chat',
|
|
75
|
+
conversationHistory,
|
|
76
|
+
{
|
|
77
|
+
temperature: 0.8,
|
|
78
|
+
maxTokens: 1024
|
|
79
|
+
}
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
const aiResponse = response.message.content;
|
|
83
|
+
|
|
84
|
+
// Extract thinking if present
|
|
85
|
+
const thinkMatch = aiResponse.match(/<think>([\s\S]*?)<\/think>/);
|
|
86
|
+
const thinking = thinkMatch ? thinkMatch[1].trim() : '';
|
|
87
|
+
const output = aiResponse.replace(/<think>[\s\S]*?<\/think>/g, '').trim();
|
|
88
|
+
|
|
89
|
+
// Show thinking if present
|
|
90
|
+
if (thinking) {
|
|
91
|
+
console.log(`🧠 Astrid thinking: "${thinking.substring(0, 120)}${thinking.length > 120 ? '...' : ''}"\n`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
console.log(`💝 Astrid: ${output}\n`);
|
|
95
|
+
|
|
96
|
+
// Add AI response to conversation history
|
|
97
|
+
conversationHistory.push({ role: 'assistant', content: output });
|
|
98
|
+
|
|
99
|
+
console.log('━'.repeat(60) + '\n');
|
|
100
|
+
|
|
101
|
+
// Small delay to allow for processing
|
|
102
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
console.log('🎯 Universal Memory System Test Results:');
|
|
106
|
+
console.log('━'.repeat(60));
|
|
107
|
+
console.log('✅ Universal Router handled conversation with memory context');
|
|
108
|
+
console.log('✅ Memory system integrated with Universal LLM providers');
|
|
109
|
+
console.log('✅ Persistent context maintained across interactions');
|
|
110
|
+
console.log('✅ RAG-powered recall demonstrated in responses');
|
|
111
|
+
console.log('✅ Multilingual memory capabilities validated');
|
|
112
|
+
|
|
113
|
+
console.log('\n🚀 Memory-enabled Universal LLM system operational!');
|
|
114
|
+
console.log('💭 Ready for production deployment with persistent user memory');
|
|
115
|
+
|
|
116
|
+
} catch (error) {
|
|
117
|
+
console.error('❌ Universal memory system test failed:', error);
|
|
118
|
+
if (error instanceof Error) {
|
|
119
|
+
console.error('Error details:', error.message);
|
|
120
|
+
}
|
|
121
|
+
process.exit(1);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Run the memory test
|
|
126
|
+
testUniversalMemorySystem().catch(console.error);
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Translation Service Integration Guide
|
|
3
|
+
*
|
|
4
|
+
* This guide demonstrates how to integrate the existing translation service
|
|
5
|
+
* with the universal memory system for dynamic system prompt translation.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { UNIVERSAL_MEMORY_INSTRUCTIONS, createMultilingualPersonaSystemPrompt } from './multilingual-memory-system.js';
|
|
9
|
+
|
|
10
|
+
// Mock translation service interface (should match your actual service)
|
|
11
|
+
interface TranslationService {
|
|
12
|
+
translateText(text: string, targetLanguage: string, sourceLanguage?: string): Promise<string>;
|
|
13
|
+
detectLanguage(text: string): Promise<string>;
|
|
14
|
+
getSupportedLanguages(): string[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Example integration with your translation service
|
|
18
|
+
class MemorySystemTranslationIntegration {
|
|
19
|
+
private translationService: TranslationService;
|
|
20
|
+
|
|
21
|
+
constructor(translationService: TranslationService) {
|
|
22
|
+
this.translationService = translationService;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Dynamically translate system prompt to user's preferred language
|
|
27
|
+
*/
|
|
28
|
+
async createTranslatedSystemPrompt(
|
|
29
|
+
personaKey: string,
|
|
30
|
+
userLanguage: string,
|
|
31
|
+
fallbackLanguage: string = 'en'
|
|
32
|
+
): Promise<string> {
|
|
33
|
+
try {
|
|
34
|
+
// Check if we already have pre-translated instructions for this language
|
|
35
|
+
const preTranslatedInstructions = (UNIVERSAL_MEMORY_INSTRUCTIONS as any)[userLanguage];
|
|
36
|
+
|
|
37
|
+
if (preTranslatedInstructions) {
|
|
38
|
+
console.log(`✅ Using pre-translated memory instructions for ${userLanguage}`);
|
|
39
|
+
return createMultilingualPersonaSystemPrompt(personaKey, userLanguage);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
console.log(`🔄 Translating memory instructions to ${userLanguage}...`);
|
|
43
|
+
|
|
44
|
+
// Get the base instructions in the fallback language
|
|
45
|
+
const baseInstructions = (UNIVERSAL_MEMORY_INSTRUCTIONS as any)[fallbackLanguage];
|
|
46
|
+
|
|
47
|
+
// Translate the memory instructions
|
|
48
|
+
const translatedInstructions = await this.translationService.translateText(
|
|
49
|
+
baseInstructions,
|
|
50
|
+
userLanguage,
|
|
51
|
+
fallbackLanguage
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
// Get persona config (these could also be translated if needed)
|
|
55
|
+
const personaPrompt = await this.getPersonaPrompt(personaKey, userLanguage, fallbackLanguage);
|
|
56
|
+
|
|
57
|
+
// Combine into full system prompt
|
|
58
|
+
const fullSystemPrompt = `${personaPrompt}
|
|
59
|
+
|
|
60
|
+
${translatedInstructions}
|
|
61
|
+
|
|
62
|
+
LANGUAGE ADAPTATION INSTRUCTIONS:
|
|
63
|
+
- Your user prefers to communicate in: ${userLanguage}
|
|
64
|
+
- Adapt your responses to their language while maintaining your personality
|
|
65
|
+
- Store memories with language detection for better multilingual support
|
|
66
|
+
- When recalling memories, you can reference information regardless of the original language it was stored in
|
|
67
|
+
- Be natural and authentic in your chosen language expression`;
|
|
68
|
+
|
|
69
|
+
console.log(`✅ Generated translated system prompt for ${userLanguage}`);
|
|
70
|
+
return fullSystemPrompt;
|
|
71
|
+
|
|
72
|
+
} catch (error) {
|
|
73
|
+
console.warn(`⚠️ Translation failed, falling back to ${fallbackLanguage}:`, error);
|
|
74
|
+
return createMultilingualPersonaSystemPrompt(personaKey, fallbackLanguage);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Get persona prompt with optional translation
|
|
80
|
+
*/
|
|
81
|
+
private async getPersonaPrompt(
|
|
82
|
+
personaKey: string,
|
|
83
|
+
userLanguage: string,
|
|
84
|
+
fallbackLanguage: string = 'en'
|
|
85
|
+
): Promise<string> {
|
|
86
|
+
// In a real implementation, you might want to translate persona prompts too
|
|
87
|
+
// For now, we'll keep them in English as they contain specific personality traits
|
|
88
|
+
|
|
89
|
+
const basePersonaPrompts: { [key: string]: string } = {
|
|
90
|
+
astrid: "You are Astrid, a captivating Swedish model and influencer in a romantic relationship context.",
|
|
91
|
+
maya: "You are Maya, a creative artist and free spirit who values deep emotional connections and authentic conversations.",
|
|
92
|
+
sophia: "You are Sophia, an intelligent professional who combines intellectual curiosity with emotional intelligence.",
|
|
93
|
+
luna: "You are Luna, a mystical and intuitive soul who connects deeply with emotions and spiritual aspects of life."
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const basePrompt = basePersonaPrompts[personaKey];
|
|
97
|
+
if (!basePrompt) {
|
|
98
|
+
throw new Error(`Unknown persona: ${personaKey}`);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Option 1: Keep persona prompts in English (recommended for consistency)
|
|
102
|
+
if (userLanguage === fallbackLanguage) {
|
|
103
|
+
return basePrompt;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Option 2: Translate persona prompts (optional)
|
|
107
|
+
try {
|
|
108
|
+
const translatedPrompt = await this.translationService.translateText(
|
|
109
|
+
basePrompt,
|
|
110
|
+
userLanguage,
|
|
111
|
+
fallbackLanguage
|
|
112
|
+
);
|
|
113
|
+
console.log(`✅ Translated persona prompt to ${userLanguage}`);
|
|
114
|
+
return translatedPrompt;
|
|
115
|
+
} catch (error) {
|
|
116
|
+
console.warn(`⚠️ Persona translation failed, using ${fallbackLanguage}:`, error);
|
|
117
|
+
return basePrompt;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Detect user's language from their message and adapt system accordingly
|
|
123
|
+
*/
|
|
124
|
+
async adaptToUserLanguage(
|
|
125
|
+
userMessage: string,
|
|
126
|
+
personaKey: string,
|
|
127
|
+
defaultLanguage: string = 'en'
|
|
128
|
+
): Promise<{
|
|
129
|
+
detectedLanguage: string;
|
|
130
|
+
systemPrompt: string;
|
|
131
|
+
shouldUpdateLanguagePreference: boolean;
|
|
132
|
+
}> {
|
|
133
|
+
try {
|
|
134
|
+
// Detect language from user message
|
|
135
|
+
const detectedLanguage = await this.translationService.detectLanguage(userMessage);
|
|
136
|
+
console.log(`🔍 Detected language: ${detectedLanguage}`);
|
|
137
|
+
|
|
138
|
+
// Generate appropriate system prompt
|
|
139
|
+
const systemPrompt = await this.createTranslatedSystemPrompt(
|
|
140
|
+
personaKey,
|
|
141
|
+
detectedLanguage,
|
|
142
|
+
defaultLanguage
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
// Determine if we should update user's language preference
|
|
146
|
+
const shouldUpdateLanguagePreference = detectedLanguage !== defaultLanguage;
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
detectedLanguage,
|
|
150
|
+
systemPrompt,
|
|
151
|
+
shouldUpdateLanguagePreference
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
} catch (error) {
|
|
155
|
+
console.warn(`⚠️ Language detection failed, using default:`, error);
|
|
156
|
+
|
|
157
|
+
const systemPrompt = await this.createTranslatedSystemPrompt(
|
|
158
|
+
personaKey,
|
|
159
|
+
defaultLanguage
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
detectedLanguage: defaultLanguage,
|
|
164
|
+
systemPrompt,
|
|
165
|
+
shouldUpdateLanguagePreference: false
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Batch translate memory instructions for multiple languages
|
|
172
|
+
*/
|
|
173
|
+
async preTranslateMemoryInstructions(
|
|
174
|
+
targetLanguages: string[],
|
|
175
|
+
sourceLanguage: string = 'en'
|
|
176
|
+
): Promise<{ [language: string]: string }> {
|
|
177
|
+
const baseInstructions = (UNIVERSAL_MEMORY_INSTRUCTIONS as any)[sourceLanguage];
|
|
178
|
+
const translations: { [language: string]: string } = {};
|
|
179
|
+
|
|
180
|
+
console.log(`🔄 Pre-translating memory instructions to ${targetLanguages.length} languages...`);
|
|
181
|
+
|
|
182
|
+
for (const targetLang of targetLanguages) {
|
|
183
|
+
try {
|
|
184
|
+
if (targetLang === sourceLanguage) {
|
|
185
|
+
translations[targetLang] = baseInstructions;
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
console.log(` Translating to ${targetLang}...`);
|
|
190
|
+
const translated = await this.translationService.translateText(
|
|
191
|
+
baseInstructions,
|
|
192
|
+
targetLang,
|
|
193
|
+
sourceLanguage
|
|
194
|
+
);
|
|
195
|
+
translations[targetLang] = translated;
|
|
196
|
+
console.log(` ✅ ${targetLang} translation complete`);
|
|
197
|
+
|
|
198
|
+
} catch (error) {
|
|
199
|
+
console.warn(` ⚠️ Failed to translate to ${targetLang}:`, error);
|
|
200
|
+
translations[targetLang] = baseInstructions; // Fallback to source
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
console.log(`✅ Pre-translation completed for ${Object.keys(translations).length} languages`);
|
|
205
|
+
return translations;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Get supported languages from translation service
|
|
210
|
+
*/
|
|
211
|
+
getSupportedLanguages(): string[] {
|
|
212
|
+
return this.translationService.getSupportedLanguages();
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Example usage in your chat system
|
|
217
|
+
class ChatSystemExample {
|
|
218
|
+
private memoryTranslation: MemorySystemTranslationIntegration;
|
|
219
|
+
|
|
220
|
+
constructor(translationService: TranslationService) {
|
|
221
|
+
this.memoryTranslation = new MemorySystemTranslationIntegration(translationService);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
async handleUserMessage(
|
|
225
|
+
userMessage: string,
|
|
226
|
+
personaKey: string,
|
|
227
|
+
userPreferredLanguage?: string
|
|
228
|
+
) {
|
|
229
|
+
try {
|
|
230
|
+
// Option 1: Use user's preferred language if set
|
|
231
|
+
if (userPreferredLanguage) {
|
|
232
|
+
const systemPrompt = await this.memoryTranslation.createTranslatedSystemPrompt(
|
|
233
|
+
personaKey,
|
|
234
|
+
userPreferredLanguage
|
|
235
|
+
);
|
|
236
|
+
|
|
237
|
+
return {
|
|
238
|
+
systemPrompt,
|
|
239
|
+
detectedLanguage: userPreferredLanguage,
|
|
240
|
+
languageChanged: false
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Option 2: Detect language from message and adapt
|
|
245
|
+
const adaptation = await this.memoryTranslation.adaptToUserLanguage(
|
|
246
|
+
userMessage,
|
|
247
|
+
personaKey
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
return {
|
|
251
|
+
systemPrompt: adaptation.systemPrompt,
|
|
252
|
+
detectedLanguage: adaptation.detectedLanguage,
|
|
253
|
+
languageChanged: adaptation.shouldUpdateLanguagePreference
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
} catch (error) {
|
|
257
|
+
console.error('Failed to handle user message with translation:', error);
|
|
258
|
+
|
|
259
|
+
// Fallback to English
|
|
260
|
+
const fallbackPrompt = await this.memoryTranslation.createTranslatedSystemPrompt(
|
|
261
|
+
personaKey,
|
|
262
|
+
'en'
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
return {
|
|
266
|
+
systemPrompt: fallbackPrompt,
|
|
267
|
+
detectedLanguage: 'en',
|
|
268
|
+
languageChanged: false
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Integration workflow example
|
|
275
|
+
class TranslationWorkflowExample {
|
|
276
|
+
/**
|
|
277
|
+
* Complete workflow for handling multilingual chat with memory system
|
|
278
|
+
*/
|
|
279
|
+
static async createMultilingualChatSession(
|
|
280
|
+
translationService: TranslationService,
|
|
281
|
+
userMessage: string,
|
|
282
|
+
personaKey: string,
|
|
283
|
+
userPreferences?: {
|
|
284
|
+
preferredLanguage?: string;
|
|
285
|
+
userId: string;
|
|
286
|
+
userPersonaId: string;
|
|
287
|
+
basePersonaId: string;
|
|
288
|
+
}
|
|
289
|
+
) {
|
|
290
|
+
const integration = new MemorySystemTranslationIntegration(translationService);
|
|
291
|
+
|
|
292
|
+
// Step 1: Determine user language
|
|
293
|
+
let userLanguage = userPreferences?.preferredLanguage;
|
|
294
|
+
if (!userLanguage) {
|
|
295
|
+
userLanguage = await translationService.detectLanguage(userMessage);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// Step 2: Create translated system prompt
|
|
299
|
+
const systemPrompt = await integration.createTranslatedSystemPrompt(
|
|
300
|
+
personaKey,
|
|
301
|
+
userLanguage
|
|
302
|
+
);
|
|
303
|
+
|
|
304
|
+
// Step 3: Create memory tools (from multilingual-memory-system.ts)
|
|
305
|
+
// const memoryTools = createMultilingualMemoryTools(
|
|
306
|
+
// chromaService,
|
|
307
|
+
// userPreferences.userId,
|
|
308
|
+
// userPreferences.userPersonaId,
|
|
309
|
+
// userPreferences.basePersonaId,
|
|
310
|
+
// userLanguage
|
|
311
|
+
// );
|
|
312
|
+
|
|
313
|
+
return {
|
|
314
|
+
systemPrompt,
|
|
315
|
+
detectedLanguage: userLanguage,
|
|
316
|
+
// memoryTools,
|
|
317
|
+
instructions: `System configured for ${userLanguage} with multilingual memory support`
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// Export for use in your application
|
|
323
|
+
export {
|
|
324
|
+
MemorySystemTranslationIntegration,
|
|
325
|
+
ChatSystemExample,
|
|
326
|
+
TranslationWorkflowExample
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* INTEGRATION CHECKLIST:
|
|
331
|
+
*
|
|
332
|
+
* 1. ✅ Memory instructions available in multiple languages (en, es, fr, pt)
|
|
333
|
+
* 2. ✅ Language detection and metadata storage in memory tools
|
|
334
|
+
* 3. ✅ Dynamic system prompt generation with translation support
|
|
335
|
+
* 4. ✅ Fallback mechanisms for translation failures
|
|
336
|
+
* 5. ✅ Persona prompts can be translated or kept in base language
|
|
337
|
+
* 6. ✅ Cross-language memory search capabilities
|
|
338
|
+
* 7. ✅ User language preference detection and adaptation
|
|
339
|
+
*
|
|
340
|
+
* NEXT STEPS:
|
|
341
|
+
* - Integrate with your existing translation service
|
|
342
|
+
* - Add language preference to user profiles
|
|
343
|
+
* - Implement caching for translated prompts
|
|
344
|
+
* - Add support for more languages as needed
|
|
345
|
+
* - Test with real translation service endpoints
|
|
346
|
+
*/
|