view love/poppy/utils/redis.py @ 258:60a876c4587a

[ui] Add semantic primitive ownership Build a layered Zenbu token and sizing system, make authored controls use native-underneath primitives, migrate mrjunejune without imposing visual surfaces, and document/enforce HTML ownership in the catalog and wiki. Co-authored-by: Copilot <[email protected]>
author MrJuneJune <me@mrjunejune.com>
date Wed, 05 Aug 2026 05:25:40 -0700
parents cf9caa4abc3e
children
line wrap: on
line source

import redis.asyncio as redis
import json
import os

REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379/0")
redis_client = redis.from_url(REDIS_URL, decode_responses=True)

STREAM_KEY = "chat:stream"


async def append_message(chat_id: str, role: str, content: str):
    """Append a FULL message (user or final assistant)"""
    payload = {"chatId": chat_id, "role": role, "content": content}
    await redis_client.xadd(STREAM_KEY, {"data": json.dumps(payload)})


async def get_all_messages_from_redis() -> list[dict]:
    try:
        entries = await redis_client.xrange(STREAM_KEY, "-", "+")
        return [json.loads(entry["data"]) for entry in entries]
    except:
        return []