88import { OpenRouter , Memory , InMemoryStorage } from "@openrouter/sdk" ;
99
1010async function main ( ) {
11- // Create a memory instance with in-memory storage
12- const memory = new Memory ( new InMemoryStorage ( ) , {
11+ // Create storage instance
12+ const storage = new InMemoryStorage ( ) ;
13+
14+ // Create a memory instance with the storage
15+ const memory = new Memory ( storage , {
1316 maxHistoryMessages : 10 , // Keep last 10 messages in context
1417 autoInject : true , // Automatically inject history
1518 autoSave : true , // Automatically save messages
@@ -30,58 +33,23 @@ async function main() {
3033 // First message
3134 const response1 = client . getResponse ( {
3235 model : "meta-llama/llama-3.2-1b-instruct" ,
33- input : [ { role : "user" , content : " My name is Alice." } ] ,
36+ input : " My name is Alice.",
3437 threadId,
3538 resourceId : userId ,
3639 } ) ;
3740 const text1 = await response1 . text ;
38- console . log ( "Assistant :" , text1 ) ;
41+ console . log ( "AI Response :" , text1 ) ;
3942
4043 // Second message - history is automatically injected
4144 const response2 = client . getResponse ( {
4245 model : "meta-llama/llama-3.2-1b-instruct" ,
43- input : [ { role : "user" , content : " What's my name?" } ] ,
46+ input : " What's my name?",
4447 threadId,
4548 resourceId : userId ,
4649 } ) ;
4750 const text2 = await response2 . text ;
48- console . log ( "Assistant:" , text2 ) ; // Should remember the name is Alice
49-
50- // Example 2: Working with thread working memory
51- console . log ( "\n=== Example 2: Thread Working Memory ===" ) ;
52-
53- await memory . updateThreadWorkingMemory ( threadId , {
54- topic : "Introduction" ,
55- lastActivity : new Date ( ) . toISOString ( ) ,
56- messageCount : 2 ,
57- } ) ;
58-
59- const threadMemory = await memory . getThreadWorkingMemory ( threadId ) ;
60- console . log ( "Thread working memory:" , threadMemory ?. data ) ;
61-
62- // Example 3: Working with resource (user) working memory
63- console . log ( "\n=== Example 3: Resource Working Memory ===" ) ;
64-
65- await memory . updateResourceWorkingMemory ( userId , {
66- name : "Alice" ,
67- preferences : {
68- theme : "dark" ,
69- language : "en" ,
70- } ,
71- createdAt : new Date ( ) . toISOString ( ) ,
72- } ) ;
73-
74- const userMemory = await memory . getResourceWorkingMemory ( userId ) ;
75- console . log ( "User working memory:" , userMemory ?. data ) ;
76-
77- // Example 4: Managing multiple threads for a user
78- console . log ( "\n=== Example 4: Multiple Threads ===" ) ;
51+ console . log ( "AI Response:" , text2 ) ; // Should remember the name is Alice
7952
80- const thread2Id = "conversation-789" ;
81- await memory . createThread ( thread2Id , userId , "Second Conversation" ) ;
82- await memory . saveMessages ( thread2Id , userId , [
83- { role : "user" , content : "Hello from second thread!" } ,
84- ] ) ;
8553
8654 const userThreads = await memory . getThreadsByResource ( userId ) ;
8755 console . log ( `User has ${ userThreads . length } threads:` ,
@@ -90,8 +58,8 @@ async function main() {
9058 // Example 5: Serialization and persistence
9159 console . log ( "\n=== Example 5: Serialization ===" ) ;
9260
93- // Serialize entire memory state
94- const memoryState = await memory . serialize ( ) ;
61+ // Serialize entire memory state using storage
62+ const memoryState = await storage . serialize ( ) ;
9563 console . log ( "Serialized state:" , {
9664 threads : memoryState . threads . length ,
9765 messages : memoryState . messages . length ,
@@ -102,12 +70,13 @@ async function main() {
10270 // For example: fs.writeFileSync('memory-state.json', JSON.stringify(memoryState));
10371
10472 // Later, you can restore the state
105- const newMemory = new Memory ( new InMemoryStorage ( ) ) ;
106- await newMemory . hydrate ( memoryState ) ;
73+ const newStorage = new InMemoryStorage ( ) ;
74+ await newStorage . hydrate ( memoryState ) ;
75+ const newMemory = new Memory ( newStorage ) ;
10776 console . log ( "Memory restored successfully!" ) ;
10877
10978 // Example 6: Serialize a single thread
110- const threadState = await memory . serializeThread ( threadId ) ;
79+ const threadState = await storage . serializeThread ( threadId ) ;
11180 if ( threadState ) {
11281 console . log ( "Thread state:" , {
11382 threadId : threadState . thread . id ,
0 commit comments