import os
from laybot_ai import Chat, Caches, via_gateway

BASE = os.getenv("LLM_BASE", "")
JUMP = os.getenv("LLM_JUMP_TOKEN", "")
KEY = os.getenv("LLM_API_KEY", "")

opt = via_gateway(BASE, token=JUMP)

def main():
    if not BASE or not KEY or not JUMP:
        raise SystemExit("please set LLM_BASE/LLM_JUMP_TOKEN/LLM_API_KEY")

    caches = Caches(
        KEY,
        vendor="gemini",
        timeout={"connect": 10, "idle": 180},
        verify=True,
        **opt
    )

    prefix = "RULES:\n" + ("你是严格JSON抽取器。\n" * 3000)

    created = caches.create_from_messages(
        model="gemini-3-flash-preview",
        messages=[
            {"role": "system", "content": "只输出JSON"},
            {"role": "user", "content": prefix},
        ],
        ttl="3600s",
        display_name="smoke_test",
    )
    name = created["name"]
    print("cache name:", name)

    chat = Chat(
        KEY,
        vendor="gemini",
        timeout={"connect": 10, "idle": 3600},
        verify=True,
        **opt
    )

    rsp = chat.completions({
        "model": "gemini-3-flash-preview",
        "cached_content": name,
        "messages": [{"role": "user", "content": '{"ping":1}'}],
        "response_mime_type": "application/json",
        "max_tokens": 256,
        "temperature": 0.1,
    })
    print("usage_norm:", rsp.get("usage_norm"))
    print("usageMetadata:", (rsp.get("raw") or {}).get("usageMetadata"))

    caches.delete(name)
    print("deleted ok")

if __name__ == "__main__":
    main()
