modified: slop_with_models.py

This commit is contained in:
Russell Ballestrini 2025-04-05 18:07:18 -04:00
parent 33da167c93
commit 0fcd05a7a1

View file

@ -68,6 +68,8 @@ def save_memory_to_file(memory_data):
try: try:
with open(MEMORY_FILE, "w") as f: with open(MEMORY_FILE, "w") as f:
json.dump(memory_data, f) json.dump(memory_data, f)
f.flush() # Ensure write is completed
os.fsync(f.fileno()) # Sync to disk
logger.debug(f"Saved memory to {MEMORY_FILE}: {memory_data}") logger.debug(f"Saved memory to {MEMORY_FILE}: {memory_data}")
except Exception as e: except Exception as e:
logger.error(f"Error saving memory to {MEMORY_FILE}: {str(e)}", exc_info=True) logger.error(f"Error saving memory to {MEMORY_FILE}: {str(e)}", exc_info=True)
@ -111,6 +113,8 @@ def save_resources_to_file(resources_data):
try: try:
with open(RESOURCES_FILE, "w") as f: with open(RESOURCES_FILE, "w") as f:
json.dump(resources_data, f) json.dump(resources_data, f)
f.flush() # Ensure write is completed
os.fsync(f.fileno()) # Sync to disk
logger.debug(f"Saved resources to {RESOURCES_FILE}: {resources_data}") logger.debug(f"Saved resources to {RESOURCES_FILE}: {resources_data}")
except Exception as e: except Exception as e:
logger.error( logger.error(
@ -247,7 +251,7 @@ tools = {
}, },
} }
# Load resources from file at startup # Load resources from file at startup (initial load only)
resources = load_resources_from_file() resources = load_resources_from_file()
@ -255,6 +259,7 @@ resources = load_resources_from_file()
def get_or_create_wallet(agent_id): def get_or_create_wallet(agent_id):
wallet_key = f"casino/wallet/agent_{agent_id}" wallet_key = f"casino/wallet/agent_{agent_id}"
with resource_lock: with resource_lock:
resources = load_resources_from_file() # Always load fresh
if wallet_key not in resources: if wallet_key not in resources:
resources[wallet_key] = { resources[wallet_key] = {
"id": wallet_key, "id": wallet_key,
@ -268,6 +273,7 @@ def get_or_create_wallet(agent_id):
def get_or_create_stats(game, agent_id): def get_or_create_stats(game, agent_id):
stats_key = f"casino/{game}/stats/agent_{agent_id}" stats_key = f"casino/{game}/stats/agent_{agent_id}"
with resource_lock: with resource_lock:
resources = load_resources_from_file() # Always load fresh
if stats_key not in resources: if stats_key not in resources:
if game == "slots": if game == "slots":
resources[stats_key] = { resources[stats_key] = {
@ -299,10 +305,8 @@ def get_or_create_stats(game, agent_id):
def update_resource(resource_id, new_content): def update_resource(resource_id, new_content):
with resource_lock: with resource_lock:
# If the resource doesn't exist, initialize it with the correct structure resources = load_resources_from_file() # Always load fresh
if resource_id not in resources: resources[resource_id] = {"id": resource_id, "content": new_content}
resources[resource_id] = {"id": resource_id, "content": None}
resources[resource_id]["content"] = new_content
save_resources_to_file(resources) save_resources_to_file(resources)
logger.debug(f"Updated resource {resource_id}: {new_content}") logger.debug(f"Updated resource {resource_id}: {new_content}")
@ -438,7 +442,12 @@ def play_blackjack(bet, agent_id, game_id=None, action=None):
# Continue an existing game # Continue an existing game
game_key = f"casino/blackjack/games/{game_id}" game_key = f"casino/blackjack/games/{game_id}"
with resource_lock: with resource_lock:
resources = load_resources_from_file()
logger.debug(
f"Checking for game {game_key} in resources: {list(resources.keys())}"
)
if game_key not in resources: if game_key not in resources:
logger.warning(f"Game {game_id} not found in resources")
return {"error": f"Game {game_id} not found"} return {"error": f"Game {game_id} not found"}
game_state = resources[game_key]["content"] game_state = resources[game_key]["content"]
@ -447,6 +456,7 @@ def play_blackjack(bet, agent_id, game_id=None, action=None):
if game_state["status"] != "active": if game_state["status"] != "active":
return {"error": "This game is already complete"} return {"error": "This game is already complete"}
if action not in ["hit", "stand"]: if action not in ["hit", "stand"]:
logger.warning(f"Invalid action '{action}' received for game {game_id}")
return {"error": "Action must be 'hit' or 'stand'"} return {"error": "Action must be 'hit' or 'stand'"}
wallet = get_or_create_wallet(agent_id) wallet = get_or_create_wallet(agent_id)
@ -761,6 +771,7 @@ def use_tool(tool_id):
def list_resources(): def list_resources():
logger.info("Received /resources request") logger.info("Received /resources request")
with resource_lock: with resource_lock:
resources = load_resources_from_file()
resource_list = list(resources.values()) resource_list = list(resources.values())
logger.debug(f"Returning resources: {resource_list}") logger.debug(f"Returning resources: {resource_list}")
return jsonify({"resources": resource_list}), 200 return jsonify({"resources": resource_list}), 200
@ -770,8 +781,6 @@ def list_resources():
def get_resource(resource_id): def get_resource(resource_id):
logger.info(f"Received /resources/{resource_id} GET request") logger.info(f"Received /resources/{resource_id} GET request")
with resource_lock: with resource_lock:
# Reload resources to ensure we have the latest state
global resources
resources = load_resources_from_file() resources = load_resources_from_file()
logger.debug(f"Resources after reload: {list(resources.keys())}") logger.debug(f"Resources after reload: {list(resources.keys())}")
if resource_id in resources: if resource_id in resources:
@ -802,6 +811,7 @@ def update_resource_endpoint(resource_id):
logger.warning(f"Invalid resource update request: {data}") logger.warning(f"Invalid resource update request: {data}")
return jsonify({"error": "Missing 'content'"}), 400 return jsonify({"error": "Missing 'content'"}), 400
with resource_lock: with resource_lock:
resources = load_resources_from_file()
resources[resource_id] = {"id": resource_id, "content": data["content"]} resources[resource_id] = {"id": resource_id, "content": data["content"]}
save_resources_to_file(resources) save_resources_to_file(resources)
logger.info(f"Updated/created resource {resource_id}: {resources[resource_id]}") logger.info(f"Updated/created resource {resource_id}: {resources[resource_id]}")