Skip to content

vast-ai/vast-cli / vastai

vastai

Flagged

Vast.ai CLI to manage GPU instances, volumes, serverless endpoints, and billing.

FileKindSize
SKILL.mdskill md18.6 KB
__init__.pyscript1.8 KB
_base.pyscript1.6 KB
api/__init__.pyscript31 B
api/auth.pyscript15.3 KB
api/billing.pyscript10.4 KB
api/client.pyscript9.1 KB
api/clusters.pyscript4.5 KB
api/deployments.pyscript1.4 KB
api/endpoints.pyscript12.0 KB
api/instances.pyscript17.1 KB
api/keys.pyscript4.3 KB
api/machines.pyscript7.4 KB
api/metrics.pyscript2.6 KB
api/offers.pyscript21.2 KB
api/price_increase.pyscript4.0 KB
api/query.pyscript12.2 KB
api/storage.pyscript14.5 KB
api/teams.pyscript4.4 KB
async_/__init__.pyscript79 B
async_/client.pyscript9.6 KB
async_/results.pyscript1.3 KB
cli/__init__.pyscript25 B
cli/commands/__init__.pyscript757 B
cli/commands/auth.pyscript42.8 KB
cli/commands/benchmarks.pyscript42.0 KB
cli/commands/billing.pyscript30.4 KB
cli/commands/clusters.pyscript7.8 KB
cli/commands/deployments.pyscript7.8 KB
cli/commands/endpoints.pyscript17.3 KB
cli/commands/instances.pyscript55.4 KB
cli/commands/keys.pyscript7.9 KB
cli/commands/machines.pyscript78.4 KB
cli/commands/metrics.pyscript12.7 KB
cli/commands/misc.pyscript11.0 KB
cli/commands/offers.pyscript30.0 KB
cli/commands/price_increase.pyscript14.5 KB
cli/commands/repl.pyscript719 B
cli/commands/storage.pyscript38.9 KB
cli/commands/teams.pyscript6.4 KB
cli/commands/uninstall.pyscript2.4 KB
cli/commands/update.pyscript3.1 KB
cli/display.pyscript20.1 KB
cli/main.pyscript11.5 KB
cli/parser.pyscript18.1 KB
cli/repl/__init__.pyscript318 B
cli/repl/bridge.pyscript7.3 KB
cli/repl/catalog.pyscript6.3 KB
cli/repl/completion.pyscript8.2 KB
cli/repl/session.pyscript7.3 KB
cli/self_test/__init__.pyscript46 B
cli/self_test/machine_diagnostics.pyscript21.0 KB
cli/self_test/runtime_diagnostics.pyscript18.6 KB
cli/self_test/support_bundle.pyscript11.0 KB
cli/selfupdate.pyscript14.0 KB
cli/util.pyscript26.3 KB
cli/utils.pyscript577 B
data/__init__.pyscript303 B
data/deployment.pyscript3.5 KB
data/endpoint.pyscript2.1 KB
data/instance.pyscript3.6 KB
data/offer.pyscript4.4 KB
data/query.pyscript12.8 KB
data/workergroup.pyscript1.4 KB
pdf/__init__.pyscript0 B
pdf/vast.ai-logo-50pct.pngbinary7.4 KB
pdf/vast.ai-logo.pngbinary10.1 KB
pdf/vast_pdf.pyscript19.4 KB
pdf/watcher.shexecutablescript390 B
sdk.pyscript52.2 KB
serverless/__init__.pyscript193 B
serverless/client/__init__.pyscript346 B
serverless/client/client.pyscript36.7 KB
serverless/client/connection.pyscript11.5 KB
serverless/client/endpoint.pyscript6.9 KB
serverless/client/managed.pyscript7.3 KB
serverless/client/request_status.pyscript371 B
serverless/client/session.pyscript2.8 KB
serverless/client/worker.pyscript1.3 KB
serverless/remote/__init__.pyscript245 B
serverless/remote/base.pyscript6.3 KB
serverless/remote/deploy.pyscript12.6 KB
serverless/remote/serialization.pyscript3.7 KB
serverless/remote/serve.pyscript8.6 KB
serverless/remote/serve_deployment.pyscript6.3 KB
serverless/remote/utils.pyscript9.7 KB
serverless/server/__init__.pyscript0 B
serverless/server/lib/__init__.pyscript0 B
serverless/server/lib/backend.pyscript43.3 KB
serverless/server/lib/data_types.pyscript10.6 KB
serverless/server/lib/metrics.pyscript12.2 KB
serverless/server/lib/server.pyscript3.2 KB
serverless/server/worker.pyscript17.1 KB
sync/__init__.pyscript76 B
sync/client.pyscript4.9 KB
sync/results.pyscript1.2 KB
utils.pyscript12.2 KB

serverless/client/connection.py

View on GitHub
# connection.py
import aiohttp
import asyncio
import ipaddress
import random
import re
import json
from typing import AsyncIterator, Dict, Optional, Any

from vastai.utils import VERSION

_JITTER_CAP_SECONDS = 5.0
_WORKER_URL = re.compile(r"https?://([0-9]{1,3}(?:\.[0-9]{1,3}){3}):([0-9]{1,5})")

class WorkerUrlRejected(ValueError):
    pass

def _require_public_worker_url(url) -> None:
    match = _WORKER_URL.fullmatch(url or "")
    if not (match and 0 < int(match[2]) < 65536 and _is_public_ipv4(match[1])):
        raise WorkerUrlRejected(f"Refusing worker url {url!r}: expected http(s)://<public ipv4>:<port>")

def _is_public_ipv4(host: str) -> bool:
    try:
        ip = ipaddress.IPv4Address(host)
    except ValueError:
        return False
    return ip.is_global and not ip.is_multicast

def _retryable(status: int) -> bool:
    return status in (408, 429) or (500 <= status < 600)

def _backoff_delay(attempt: int) -> float:
    # capped exponential backoff with jitter
    return min((2 ** attempt) + random.uniform(0, 1), _JITTER_CAP_SECONDS)

def _build_kwargs(
    *,
    headers: Dict[str, str],
    params: Dict[str, str],
    ssl_context,
    timeout: Optional[float],
    body: Optional[dict],
    method: str,
    stream: bool,
    allow_redirects: bool,
) -> Dict:
    return {
        "headers": headers,
        "allow_redirects": allow_redirects,
        "params": params,
        "ssl": ssl_context,
        "timeout": aiohttp.ClientTimeout(total=None if stream else timeout),
        **({"json": body} if method != "GET" and body else {}),
    }

async def _iter_sse_json(resp: aiohttp.ClientResponse) -> AsyncIterator[dict]:
    """
    Yield JSON objects from an SSE/text stream. Accepts lines starting with 'data:' or raw JSONL.
    """
    buffer = b""
    async for chunk in resp.content.iter_any():
        if not chunk:
            continue
        buffer += chunk
        while b"\n" in buffer:
            line, buffer = buffer.split(b"\n", 1)
            line = line.strip()
            if not line:
                continue
            if line.startswith(b"data:"):
                line = line[5:].strip()
            try:
                yield json.loads(line.decode("utf-8"))
            except Exception:
                # Ignore keepalives/bad fragments
                continue

    # flush tail if present
    tail = buffer.strip()
    if tail:
        try:
            yield json.loads(tail.decode("utf-8"))
        except Exception:
            pass

async def _open_once(
    *,
    session: aiohttp.ClientSession,
    method: str,
    url: str,
    route: str,
    kwargs: Dict,
):
    """
    Execute one HTTP attempt and return the aiohttp response object.
    Caller is responsible for reading/closing via 'async with' or resp.release()/resp.close().
    """
    request_fn = {"GET": session.get, "POST": session.post, "PUT": session.put, "DELETE": session.delete}.get(method, session.post)
    return await request_fn(url + route, **kwargs)


async def _make_request(
    client,
    route: str,
    api_key: str,
    url: str = "",
    body: Optional[dict] = None,
    params: Optional[dict] = None,
    method: str = "GET",
    retries: int = 5,
    timeout: float = 30,
    stream: bool = False,
    allow_non_json: bool = False,
    allow_redirects: bool = True,
) -> Dict[str, Any]:
    """
    Make an HTTP request with capped exponential backoff + jitter, returning a structured result.

    - Never raises for HTTP non-2xx responses. Instead returns result with ok=False and status/text/json.
    - Raises only for "mechanical" failures (aiohttp/transport) and invalid JSON on successful (2xx) responses.
    - allow_non_json: a 2xx text/* or media body that is not a JSON object or array is returned
      (str for text/*, bytes otherwise) instead of raising.

    Return shape (non-stream):
      {
        "ok": bool,
        "status": int|None,
        "url": str,
        "headers": dict,
        "text": str,
        "json": Any|None,
        "content": bytes|str|None,    # allow_non_json only: str for text/*, else bytes
        "content_type": str,          # the declared type, without parameters ("" if none)
        "retryable": bool,
        "attempt": int
      }

    Return shape (stream=True, ok=True adds "stream"):
      { ... , "stream": AsyncIterator[dict] }
    """
    method = method.upper()
    body = body or {}
    params = {**(params or {})}
    params["api_key"] = api_key

    headers = {"Authorization": f"Bearer {api_key}", "User-Agent": f"vastai-sdk/{VERSION}"}

    session = await client._get_session()
    ssl_context = await client.get_ssl_context() if client else None

    full_url = url + route

    last_result: Dict[str, Any] = {
        "ok": False,
        "status": None,
        "url": full_url,
        "headers": {},
        "text": "",
        "json": None,
        "retryable": False,
        "attempt": 0,
    }

    # STREAMING PATH (open upfront so we can return status + a stream iterator)
    if stream:
        for attempt in range(1, retries + 1):
            last_result["attempt"] = attempt
            try:
                kwargs = _build_kwargs(
                    headers=headers,
                    params=params,
                    ssl_context=ssl_context,
                    timeout=timeout,
                    body=body,
                    method=method,
                    stream=True,
                    allow_redirects=allow_redirects,
                )

                resp = await _open_once(
                    session=session,
                    method=method,
                    url=url,
                    route=route,
                    kwargs=kwargs,
                )

                status = resp.status
                last_result["status"] = status
                last_result["headers"] = dict(resp.headers)
                last_result["retryable"] = _retryable(status)

                if status < 200 or status >= 300:
                    text = await resp.text()
                    last_result["text"] = text

                    # best-effort JSON parse (do not raise)
                    try:
                        if text and text.lstrip().startswith(("{", "[")):
                            last_result["json"] = json.loads(text)
                    except Exception:
                        pass

                    resp.release()

                    if last_result["retryable"] and attempt < retries:
                        await asyncio.sleep(_backoff_delay(attempt))
                        continue

                    # final non-2xx: return what we saw
                    return last_result

                # 2xx: return stream iterator that owns the response lifetime
                last_result["ok"] = True
                last_result["text"] = ""  # streaming: we don't pre-read body

                async def _stream_iter() -> AsyncIterator[dict]:
                    try:
                        async for obj in _iter_sse_json(resp):
                            yield obj
                    finally:
                        try:
                            resp.release()
                        except Exception:
                            pass

                last_result["stream"] = _stream_iter()
                return last_result

            except asyncio.TimeoutError as ex:
                if attempt == retries:
                    raise TimeoutError(f"Request to {full_url} timed out after {timeout}s") from ex
                await asyncio.sleep(_backoff_delay(attempt))
            except Exception as ex:
                if attempt == retries:
                    raise ex
                await asyncio.sleep(_backoff_delay(attempt))

        return last_result

    # NON-STREAMING PATH
    for attempt in range(1, retries + 1):
        last_result["attempt"] = attempt
        try:
            kwargs = _build_kwargs(
                headers=headers,
                params=params,
                ssl_context=ssl_context,
                timeout=timeout,
                body=body,
                method=method,
                stream=False,
                allow_redirects=allow_redirects,
            )

            request_fn = {"GET": session.get, "POST": session.post, "PUT": session.put, "DELETE": session.delete}.get(method, session.post)
            async with await request_fn(full_url, **kwargs) as resp:
                status = resp.status
                content_type = (resp.headers.get("Content-Type") or "").split(";", 1)[0].lower()
                is_text = content_type.startswith("text/")
                is_media = (allow_non_json and bool(content_type) and not is_text
                            and "json" not in content_type)
                raw = await resp.read() if is_media else None
                text = "" if is_media and 200 <= status < 300 else await resp.text(errors="replace")

                result: Dict[str, Any] = {
                    "ok": 200 <= status < 300,
                    "status": status,
                    "url": full_url,
                    "headers": dict(resp.headers),
                    "text": text,
                    "json": None,
                    "content": None,
                    "content_type": content_type,
                    "retryable": _retryable(status),
                    "attempt": attempt,
                }

                if result["ok"]:
                    if is_media or (allow_non_json and is_text):
                        # The body, unless it is an object or array (mislabelled JSON).
                        body = raw if is_media else text
                        try:
                            if body.lstrip()[:1] in ("{", "[", b"{", b"["):
                                result["json"] = json.loads(body)
                                return result
                        except ValueError:
                            pass
                        result["content"] = body
                        return result
                    # Successful responses are expected to be JSON; invalid JSON is a hard failure
                    try:
                        result["json"] = await resp.json(content_type=None)
                    except Exception:
                        raise Exception(f"Invalid JSON from {full_url}:\n{text}")

                    # Debug: log the exact response
                    if hasattr(client, 'logger'):
                        client.logger.debug(f"_make_request response from {route}: {result}")

                    return result

                # Non-2xx: best-effort JSON parse (do not raise)
                try:
                    if text and text.lstrip().startswith(("{", "[")):
                        result["json"] = json.loads(text)
                except Exception:
                    pass

                # Retry only if retryable and attempts remain
                if result["retryable"] and attempt < retries:
                    last_result = result
                    await asyncio.sleep(_backoff_delay(attempt))
                    continue

                # Debug: log the exact response for non-2xx
                if hasattr(client, 'logger'):
                    client.logger.debug(f"_make_request response from {route}: {result}")

                return result

        except asyncio.TimeoutError as ex:
            if attempt == retries:
                raise TimeoutError(f"Request to {full_url} timed out after {timeout}s") from ex
            await asyncio.sleep(_backoff_delay(attempt))
        except Exception as ex:
            if attempt == retries:
                raise ex
            await asyncio.sleep(_backoff_delay(attempt))

    return last_result
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall