File size: 2,097 Bytes
ddebc3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""XAI LiveKit protocol — token endpoint and WebSocket URL.

The token fetch sends a JSON payload to /rest/livekit/tokens and receives
a short-lived LiveKit access token.  The WebSocket connection uses that
access token as a query parameter on wss://livekit.grok.com/rtc.
"""

from typing import Any, Dict
from urllib.parse import urlencode

import orjson

LIVEKIT_TOKEN_URL = "https://grok.com/rest/livekit/tokens"
LIVEKIT_WS_BASE   = "wss://livekit.grok.com"

# SDK version parameters sent with every WebSocket connection.
_WS_PARAMS: Dict[str, str] = {
    "auto_subscribe": "1",
    "sdk":            "js",
    "version":        "2.11.4",
    "protocol":       "15",
}


def build_token_request_payload(
    voice:              str   = "ara",
    personality:        str   = "assistant",
    speed:              float = 1.0,
    custom_instruction: str   = "",
) -> bytes:
    """Return the JSON body for POST /rest/livekit/tokens."""
    payload_dict = {
        "voice":           voice,
        "personality":     None,
        "playback_speed":  speed,
        "enable_vision":   False,
        "turn_detection":  {"type": "server_vad"},
    }
    if custom_instruction:
        payload_dict["instructions"] = custom_instruction
        payload_dict["is_raw_instructions"] = True
    else:
        payload_dict["personality"] = personality

    session_payload = orjson.dumps(payload_dict).decode()

    return orjson.dumps({
        "sessionPayload":       session_payload,
        "requestAgentDispatch": False,
        "livekitUrl":           LIVEKIT_WS_BASE,
        "params":               {"enable_markdown_transcript": "true"},
    })


def build_ws_url(access_token: str) -> str:
    """Build the wss:// URL for the LiveKit WebSocket connection."""
    base   = LIVEKIT_WS_BASE.rstrip("/")
    rtc    = f"{base}/rtc" if not base.endswith("/rtc") else base
    params = dict(_WS_PARAMS)
    params["access_token"] = access_token
    return f"{rtc}?{urlencode(params)}"


__all__ = [
    "LIVEKIT_TOKEN_URL", "LIVEKIT_WS_BASE",
    "build_token_request_payload", "build_ws_url",
]