ctfy.sdk.resources.llm_sessions

client.llm_sessions -- a local agent's session on the platform's model gateway (#3307): open one paid by your own model key, list yours, end one; read a competition's session board and a session's recorded calls (PR 3).

  1"""``client.llm_sessions`` -- a local agent's session on the platform's model
  2gateway (#3307): open one paid by your own model key, list yours, end one;
  3read a competition's session board and a session's recorded calls (PR 3)."""
  4
  5from __future__ import annotations
  6
  7from typing import Any
  8
  9from ctfy.sdk._helpers import PagedList, _extract_items, _raise_for_status
 10from ctfy.sdk.base import BaseHttpClient
 11from ctfy.server.models import (
 12    LlmCallBody,
 13    LlmCallPage,
 14    LlmLocalSessionCreate,
 15    LlmLocalSessionCreated,
 16    LlmLocalSessionInfo,
 17    LlmSessionBoardPage,
 18    LlmSessionRow,
 19)
 20
 21
 22class LlmSessionsResource:
 23    """Your local agent sessions. The handle is returned by :meth:`create`
 24    once and never again; the platform keeps only its hash."""
 25
 26    def __init__(self, http: BaseHttpClient) -> None:
 27        self._http = http
 28
 29    def create(
 30        self,
 31        competition_id: str,
 32        llm_key_id: str,
 33        *,
 34        challenge_id: str = "",
 35        model_ref: str = "",
 36        label: str = "",
 37        ttl_s: int | None = None,
 38    ) -> LlmLocalSessionCreated:
 39        """Open a session in ``competition_id`` paid by your key
 40        ``llm_key_id`` (``client.eval.my_llm_keys()``'s ``id``).
 41
 42        ``model_ref=""`` passes your agent's own model choice through;
 43        ``ttl_s=None`` takes the server's default lifetime.
 44        """
 45        fields: dict[str, Any] = {
 46            "competition_id": competition_id,
 47            "llm_key_id": llm_key_id,
 48            "challenge_id": challenge_id,
 49            "model_ref": model_ref,
 50            "label": label,
 51        }
 52        if ttl_s is not None:
 53            fields["ttl_s"] = ttl_s
 54        # ⚠️ ``exclude_unset``: an unspecified lifetime is *not sent*, so the
 55        # server's default applies rather than this client's copy of it.
 56        body = LlmLocalSessionCreate(**fields).model_dump(exclude_unset=True)
 57        resp = self._http.request("POST", "/llm-sessions", json=body)
 58        _raise_for_status(resp)
 59        return LlmLocalSessionCreated.model_validate(resp.json())
 60
 61    def list(
 62        self, *, competition_id: str = "", offset: int = 0, limit: int = 50
 63    ) -> PagedList[LlmLocalSessionInfo]:
 64        """One page of your sessions, newest first, with the ``total``
 65        (``limit`` at most 200).
 66
 67        ⚠️ This returned the rows alone, so "these are all" and "these are
 68        the first fifty" read the same; a caller that wants every session
 69        walks it (the CLI's ``every_page``)."""
 70        params: dict[str, Any] = {"offset": offset, "limit": limit}
 71        if competition_id:
 72            params["competition_id"] = competition_id
 73        resp = self._http.request("GET", "/llm-sessions", params=params)
 74        _raise_for_status(resp)
 75        return _extract_items(resp.json(), LlmLocalSessionInfo)
 76
 77    def get(self, session_id: str) -> LlmLocalSessionInfo:
 78        """One of your sessions; anybody else's is a 404."""
 79        resp = self._http.request("GET", f"/llm-sessions/{session_id}")
 80        _raise_for_status(resp)
 81        return LlmLocalSessionInfo.model_validate(resp.json())
 82
 83    def revoke(self, session_id: str) -> LlmLocalSessionInfo:
 84        """End a session; a second revoke keeps the first instant."""
 85        resp = self._http.request("POST", f"/llm-sessions/{session_id}/revoke")
 86        _raise_for_status(resp)
 87        return LlmLocalSessionInfo.model_validate(resp.json())
 88
 89    def board(
 90        self, competition_id: str, *, offset: int = 0, limit: int = 50
 91    ) -> LlmSessionBoardPage[LlmSessionRow]:
 92        """One page of ``competition_id``'s session board: your sessions and
 93        your team-mates' there, or every team's if you organise it. ``scope``
 94        says which; ``available`` whether this deployment serves sessions.
 95        Each row says whether you may open its calls' bodies."""
 96        resp = self._http.request(
 97            "GET",
 98            f"/competitions/{competition_id}/llm-sessions",
 99            params={"offset": offset, "limit": limit},
100        )
101        _raise_for_status(resp)
102        return LlmSessionBoardPage[LlmSessionRow].model_validate(resp.json())
103
104    def calls(self, session_id: str, *, offset: int = 0, limit: int = 50) -> LlmCallPage:
105        """A session's recorded calls, newest first -- metadata, readable by
106        its owner, their team-mates in its competition and its organisers."""
107        params: dict[str, Any] = {"offset": offset, "limit": limit}
108        resp = self._http.request("GET", f"/llm-sessions/{session_id}/llm-calls", params=params)
109        _raise_for_status(resp)
110        return LlmCallPage.model_validate(resp.json())
111
112    def call(self, session_id: str, call_id: str) -> LlmCallBody:
113        """One recorded conversation -- **the session owner's** (a team-mate
114        or an organiser is ``403 transcript_body_owner_only``)."""
115        resp = self._http.request("GET", f"/llm-sessions/{session_id}/llm-calls/{call_id}")
116        _raise_for_status(resp)
117        return LlmCallBody.model_validate(resp.json())
class LlmSessionsResource:
 23class LlmSessionsResource:
 24    """Your local agent sessions. The handle is returned by :meth:`create`
 25    once and never again; the platform keeps only its hash."""
 26
 27    def __init__(self, http: BaseHttpClient) -> None:
 28        self._http = http
 29
 30    def create(
 31        self,
 32        competition_id: str,
 33        llm_key_id: str,
 34        *,
 35        challenge_id: str = "",
 36        model_ref: str = "",
 37        label: str = "",
 38        ttl_s: int | None = None,
 39    ) -> LlmLocalSessionCreated:
 40        """Open a session in ``competition_id`` paid by your key
 41        ``llm_key_id`` (``client.eval.my_llm_keys()``'s ``id``).
 42
 43        ``model_ref=""`` passes your agent's own model choice through;
 44        ``ttl_s=None`` takes the server's default lifetime.
 45        """
 46        fields: dict[str, Any] = {
 47            "competition_id": competition_id,
 48            "llm_key_id": llm_key_id,
 49            "challenge_id": challenge_id,
 50            "model_ref": model_ref,
 51            "label": label,
 52        }
 53        if ttl_s is not None:
 54            fields["ttl_s"] = ttl_s
 55        # ⚠️ ``exclude_unset``: an unspecified lifetime is *not sent*, so the
 56        # server's default applies rather than this client's copy of it.
 57        body = LlmLocalSessionCreate(**fields).model_dump(exclude_unset=True)
 58        resp = self._http.request("POST", "/llm-sessions", json=body)
 59        _raise_for_status(resp)
 60        return LlmLocalSessionCreated.model_validate(resp.json())
 61
 62    def list(
 63        self, *, competition_id: str = "", offset: int = 0, limit: int = 50
 64    ) -> PagedList[LlmLocalSessionInfo]:
 65        """One page of your sessions, newest first, with the ``total``
 66        (``limit`` at most 200).
 67
 68        ⚠️ This returned the rows alone, so "these are all" and "these are
 69        the first fifty" read the same; a caller that wants every session
 70        walks it (the CLI's ``every_page``)."""
 71        params: dict[str, Any] = {"offset": offset, "limit": limit}
 72        if competition_id:
 73            params["competition_id"] = competition_id
 74        resp = self._http.request("GET", "/llm-sessions", params=params)
 75        _raise_for_status(resp)
 76        return _extract_items(resp.json(), LlmLocalSessionInfo)
 77
 78    def get(self, session_id: str) -> LlmLocalSessionInfo:
 79        """One of your sessions; anybody else's is a 404."""
 80        resp = self._http.request("GET", f"/llm-sessions/{session_id}")
 81        _raise_for_status(resp)
 82        return LlmLocalSessionInfo.model_validate(resp.json())
 83
 84    def revoke(self, session_id: str) -> LlmLocalSessionInfo:
 85        """End a session; a second revoke keeps the first instant."""
 86        resp = self._http.request("POST", f"/llm-sessions/{session_id}/revoke")
 87        _raise_for_status(resp)
 88        return LlmLocalSessionInfo.model_validate(resp.json())
 89
 90    def board(
 91        self, competition_id: str, *, offset: int = 0, limit: int = 50
 92    ) -> LlmSessionBoardPage[LlmSessionRow]:
 93        """One page of ``competition_id``'s session board: your sessions and
 94        your team-mates' there, or every team's if you organise it. ``scope``
 95        says which; ``available`` whether this deployment serves sessions.
 96        Each row says whether you may open its calls' bodies."""
 97        resp = self._http.request(
 98            "GET",
 99            f"/competitions/{competition_id}/llm-sessions",
100            params={"offset": offset, "limit": limit},
101        )
102        _raise_for_status(resp)
103        return LlmSessionBoardPage[LlmSessionRow].model_validate(resp.json())
104
105    def calls(self, session_id: str, *, offset: int = 0, limit: int = 50) -> LlmCallPage:
106        """A session's recorded calls, newest first -- metadata, readable by
107        its owner, their team-mates in its competition and its organisers."""
108        params: dict[str, Any] = {"offset": offset, "limit": limit}
109        resp = self._http.request("GET", f"/llm-sessions/{session_id}/llm-calls", params=params)
110        _raise_for_status(resp)
111        return LlmCallPage.model_validate(resp.json())
112
113    def call(self, session_id: str, call_id: str) -> LlmCallBody:
114        """One recorded conversation -- **the session owner's** (a team-mate
115        or an organiser is ``403 transcript_body_owner_only``)."""
116        resp = self._http.request("GET", f"/llm-sessions/{session_id}/llm-calls/{call_id}")
117        _raise_for_status(resp)
118        return LlmCallBody.model_validate(resp.json())

Your local agent sessions. The handle is returned by create() once and never again; the platform keeps only its hash.

LlmSessionsResource(http: ctfy.sdk.base.BaseHttpClient)
27    def __init__(self, http: BaseHttpClient) -> None:
28        self._http = http
def create( self, competition_id: str, llm_key_id: str, *, challenge_id: str = '', model_ref: str = '', label: str = '', ttl_s: int | None = None) -> ctfy.server.models.LlmLocalSessionCreated:
30    def create(
31        self,
32        competition_id: str,
33        llm_key_id: str,
34        *,
35        challenge_id: str = "",
36        model_ref: str = "",
37        label: str = "",
38        ttl_s: int | None = None,
39    ) -> LlmLocalSessionCreated:
40        """Open a session in ``competition_id`` paid by your key
41        ``llm_key_id`` (``client.eval.my_llm_keys()``'s ``id``).
42
43        ``model_ref=""`` passes your agent's own model choice through;
44        ``ttl_s=None`` takes the server's default lifetime.
45        """
46        fields: dict[str, Any] = {
47            "competition_id": competition_id,
48            "llm_key_id": llm_key_id,
49            "challenge_id": challenge_id,
50            "model_ref": model_ref,
51            "label": label,
52        }
53        if ttl_s is not None:
54            fields["ttl_s"] = ttl_s
55        # ⚠️ ``exclude_unset``: an unspecified lifetime is *not sent*, so the
56        # server's default applies rather than this client's copy of it.
57        body = LlmLocalSessionCreate(**fields).model_dump(exclude_unset=True)
58        resp = self._http.request("POST", "/llm-sessions", json=body)
59        _raise_for_status(resp)
60        return LlmLocalSessionCreated.model_validate(resp.json())

Open a session in competition_id paid by your key llm_key_id (client.eval.my_llm_keys()'s id).

model_ref="" passes your agent's own model choice through; ttl_s=None takes the server's default lifetime.

def list( self, *, competition_id: str = '', offset: int = 0, limit: int = 50) -> ctfy.sdk._helpers.PagedList[ctfy.server.models.LlmLocalSessionInfo]:
62    def list(
63        self, *, competition_id: str = "", offset: int = 0, limit: int = 50
64    ) -> PagedList[LlmLocalSessionInfo]:
65        """One page of your sessions, newest first, with the ``total``
66        (``limit`` at most 200).
67
68        ⚠️ This returned the rows alone, so "these are all" and "these are
69        the first fifty" read the same; a caller that wants every session
70        walks it (the CLI's ``every_page``)."""
71        params: dict[str, Any] = {"offset": offset, "limit": limit}
72        if competition_id:
73            params["competition_id"] = competition_id
74        resp = self._http.request("GET", "/llm-sessions", params=params)
75        _raise_for_status(resp)
76        return _extract_items(resp.json(), LlmLocalSessionInfo)

One page of your sessions, newest first, with the total (limit at most 200).

⚠️ This returned the rows alone, so "these are all" and "these are the first fifty" read the same; a caller that wants every session walks it (the CLI's every_page).

def get( self, session_id: str) -> ctfy.server.models.LlmLocalSessionInfo:
78    def get(self, session_id: str) -> LlmLocalSessionInfo:
79        """One of your sessions; anybody else's is a 404."""
80        resp = self._http.request("GET", f"/llm-sessions/{session_id}")
81        _raise_for_status(resp)
82        return LlmLocalSessionInfo.model_validate(resp.json())

One of your sessions; anybody else's is a 404.

def revoke( self, session_id: str) -> ctfy.server.models.LlmLocalSessionInfo:
84    def revoke(self, session_id: str) -> LlmLocalSessionInfo:
85        """End a session; a second revoke keeps the first instant."""
86        resp = self._http.request("POST", f"/llm-sessions/{session_id}/revoke")
87        _raise_for_status(resp)
88        return LlmLocalSessionInfo.model_validate(resp.json())

End a session; a second revoke keeps the first instant.

def board( self, competition_id: str, *, offset: int = 0, limit: int = 50) -> ctfy.server.models.LlmSessionBoardPage[LlmSessionRow]:
 90    def board(
 91        self, competition_id: str, *, offset: int = 0, limit: int = 50
 92    ) -> LlmSessionBoardPage[LlmSessionRow]:
 93        """One page of ``competition_id``'s session board: your sessions and
 94        your team-mates' there, or every team's if you organise it. ``scope``
 95        says which; ``available`` whether this deployment serves sessions.
 96        Each row says whether you may open its calls' bodies."""
 97        resp = self._http.request(
 98            "GET",
 99            f"/competitions/{competition_id}/llm-sessions",
100            params={"offset": offset, "limit": limit},
101        )
102        _raise_for_status(resp)
103        return LlmSessionBoardPage[LlmSessionRow].model_validate(resp.json())

One page of competition_id's session board: your sessions and your team-mates' there, or every team's if you organise it. scope says which; available whether this deployment serves sessions. Each row says whether you may open its calls' bodies.

def calls( self, session_id: str, *, offset: int = 0, limit: int = 50) -> ctfy.server.models.LlmCallPage:
105    def calls(self, session_id: str, *, offset: int = 0, limit: int = 50) -> LlmCallPage:
106        """A session's recorded calls, newest first -- metadata, readable by
107        its owner, their team-mates in its competition and its organisers."""
108        params: dict[str, Any] = {"offset": offset, "limit": limit}
109        resp = self._http.request("GET", f"/llm-sessions/{session_id}/llm-calls", params=params)
110        _raise_for_status(resp)
111        return LlmCallPage.model_validate(resp.json())

A session's recorded calls, newest first -- metadata, readable by its owner, their team-mates in its competition and its organisers.

def call( self, session_id: str, call_id: str) -> ctfy.server.models.LlmCallBody:
113    def call(self, session_id: str, call_id: str) -> LlmCallBody:
114        """One recorded conversation -- **the session owner's** (a team-mate
115        or an organiser is ``403 transcript_body_owner_only``)."""
116        resp = self._http.request("GET", f"/llm-sessions/{session_id}/llm-calls/{call_id}")
117        _raise_for_status(resp)
118        return LlmCallBody.model_validate(resp.json())

One recorded conversation -- the session owner's (a team-mate or an organiser is 403 transcript_body_owner_only).