Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
distribution = "2201.12.0"
org = "ballerinax"
name = "ai.memory.mssql"
version = "1.0.0"
version = "1.0.1"
license = ["Apache-2.0"]
authors = ["Ballerina"]
keywords = ["ai", "agent", "memory"]
Expand Down
16 changes: 9 additions & 7 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ distribution-version = "2201.12.0"
[[package]]
org = "ballerina"
name = "ai"
version = "1.6.0"
version = "1.7.0"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "data.jsondata"},
Expand All @@ -26,14 +26,16 @@ dependencies = [
{org = "ballerina", name = "math.vector"},
{org = "ballerina", name = "mcp"},
{org = "ballerina", name = "mime"},
{org = "ballerina", name = "observe"},
{org = "ballerina", name = "time"},
{org = "ballerina", name = "url"},
{org = "ballerina", name = "uuid"},
{org = "ballerina", name = "yaml"}
]
modules = [
{org = "ballerina", packageName = "ai", moduleName = "ai"},
{org = "ballerina", packageName = "ai", moduleName = "ai.intelligence"}
{org = "ballerina", packageName = "ai", moduleName = "ai.intelligence"},
{org = "ballerina", packageName = "ai", moduleName = "ai.observe"}
]

[[package]]
Expand Down Expand Up @@ -268,7 +270,7 @@ version = "1.2.0"
[[package]]
org = "ballerina"
name = "mcp"
version = "1.0.1"
version = "1.0.2"
dependencies = [
{org = "ballerina", name = "http"},
{org = "ballerina", name = "jballerina.java"},
Expand Down Expand Up @@ -302,7 +304,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "observe"
version = "1.5.1"
version = "1.6.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand Down Expand Up @@ -357,15 +359,15 @@ modules = [
[[package]]
org = "ballerina"
name = "time"
version = "2.7.0"
version = "2.8.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "url"
version = "2.6.0"
version = "2.6.1"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand Down Expand Up @@ -405,7 +407,7 @@ dependencies = [
[[package]]
org = "ballerinax"
name = "ai.memory.mssql"
version = "1.0.0"
version = "1.0.1"
dependencies = [
{org = "ballerina", name = "ai"},
{org = "ballerina", name = "cache"},
Expand Down
21 changes: 12 additions & 9 deletions ballerina/store.bal
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public isolated class ShortTermMemoryStore {
*ai:ShortTermMemoryStore;

private final mssql:Client dbClient;
private final cache:Cache cache;
private final cache:Cache? cache;
private final int maxMessagesPerKey;

# Initializes the MS SQL-backed short-term memory store.
Expand All @@ -66,7 +66,7 @@ public isolated class ShortTermMemoryStore {
# + returns - An error if the initialization fails
public isolated function init(mssql:Client|DatabaseConfiguration mssqlClient,
int maxMessagesPerKey = 20,
cache:CacheConfig cacheConfig = {capacity: 20}) returns Error? {
cache:CacheConfig? cacheConfig = ()) returns Error? {
if mssqlClient is mssql:Client {
self.dbClient = mssqlClient;
} else {
Expand All @@ -77,7 +77,7 @@ public isolated class ShortTermMemoryStore {
self.dbClient = initializedClient;
}
self.maxMessagesPerKey = maxMessagesPerKey;
self.cache = new (cacheConfig);
self.cache = cacheConfig is () ? () : new (cacheConfig);
return self.initializeDatabase();
}

Expand Down Expand Up @@ -396,8 +396,9 @@ public isolated class ShortTermMemoryStore {

final ai:ChatInteractiveMessage[] & readonly immutableInteractiveMessages = interactiveMessages.cloneReadOnly();
lock {
if !self.cache.hasKey(key) {
check self.cache.put(
cache:Cache? cache = self.cache;
if cache !is () && !cache.hasKey(key) {
check cache.put(
key, <CachedMessages> {systemMessage, interactiveMessages: [...immutableInteractiveMessages]});
}
}
Expand All @@ -413,8 +414,9 @@ public isolated class ShortTermMemoryStore {

private isolated function removeCacheEntry(string key) {
lock {
if self.cache.hasKey(key) {
cache:Error? err = self.cache.invalidate(key);
cache:Cache? cache = self.cache;
if cache !is () && cache.hasKey(key) {
cache:Error? err = cache.invalidate(key);
if err is cache:Error {
// Ignore, as this is for non-existent key
}
Expand All @@ -424,11 +426,12 @@ public isolated class ShortTermMemoryStore {

private isolated function getCacheEntry(string key) returns CachedMessages? {
lock {
if !self.cache.hasKey(key) {
cache:Cache? cache = self.cache;
if cache is () || !cache.hasKey(key) {
return ();
}

any|cache:Error cacheEntry = self.cache.get(key);
any|cache:Error cacheEntry = cache.get(key);
if cacheEntry is cache:Error {
return ();
}
Expand Down
Loading
Loading