-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
add snapshots to memory
- ai-agents
- (#143)
- development
- (#126, #143)
Olasunkanmi Oyinlola
authored and
Olasunkanmi Oyinlola
committed
Jan 27, 2025
1 parent
b87c1be
commit 2ae80e4
Showing
14 changed files
with
189 additions
and
360 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { MEMORY_CACHE_OPTIONS } from "../application/constant"; | ||
|
||
interface ICacheEntry { | ||
value: any; | ||
expiry: number; | ||
} | ||
|
||
export class Memory { | ||
private static bank: Map<string, ICacheEntry>; | ||
private static instance: Memory; | ||
|
||
constructor() { | ||
Memory.bank = new Map(); | ||
} | ||
|
||
public static getInstance(): Memory { | ||
if (!Memory.instance) { | ||
return (Memory.instance = new Memory()); | ||
} | ||
return Memory.instance; | ||
} | ||
|
||
static set(key: string, value: any): Map<string, ICacheEntry> { | ||
const expiry = Date.now() + MEMORY_CACHE_OPTIONS.sessionTTL; | ||
return Memory.bank.set(key, { value, expiry }); | ||
} | ||
|
||
static get(key: string): any { | ||
const entry = Memory.bank.get(key); | ||
if (entry && Date.now() < entry.expiry) { | ||
return entry.value; | ||
} | ||
return undefined; | ||
} | ||
|
||
static delete(key: string): boolean | undefined { | ||
const cached = Memory.get(key); | ||
if (cached) { | ||
return Memory.bank.delete(key); | ||
} | ||
return undefined; | ||
} | ||
|
||
static keys(): string[] { | ||
return Array.from(Memory.bank.keys()); | ||
} | ||
|
||
static values(): ICacheEntry[] { | ||
return Array.from(Memory.bank.values()); | ||
} | ||
|
||
static has(key: string): boolean { | ||
return Memory.bank.has(key); | ||
} | ||
|
||
static clear(): void { | ||
return Memory.bank.clear(); | ||
} | ||
|
||
static createSnapShot(): Memory { | ||
return this.instance; | ||
} | ||
|
||
static loadSnapShot(snapShot: Memory): void { | ||
Object.assign(this, snapShot); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters