Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110"""Native Gemini HTTP assertions on the harness's scripted provider server."""
from __future__ import annotations
import jsonimport urllib.parse
from harness import Provider
class GoogleAIStudioProvider(Provider): """Keep native endpoint, credential and catalog state on one provider route."""
def __init__(self, script, *, key, model="gemini-fixture", count=None): super().__init__(script, chunk_size=4, count=count) self.key = key self.model = model self.base_url = self.url + "/v1beta" # One page; the paging rules are a property in the extension's tests. self.pages = { "": { "models": [ { "name": f"models/{model}", "supportedGenerationMethods": ["generateContent"], } ] } } self.http_requests = [] self.catalog_requests = [] self.violations = []
def _checked(self, handler, check): try: check() except (AssertionError, KeyError, TypeError, ValueError) as failure: with self._lock: self.violations.append(str(failure) or type(failure).__name__) self._json(handler, {"error": {"message": "invalid native request"}}, 400) return False return True
def _transport(self, handler): record = { "path": handler.path, "headers": {key.lower(): value for key, value in handler.headers.items()}, } with self._lock: self.http_requests.append(record) headers = record["headers"] assert headers.get("x-goog-api-key") == self.key, "missing native API key" assert "authorization" not in headers, "API key sent as Authorization" url = urllib.parse.urlsplit(handler.path) assert self.key not in handler.path, "API key leaked into URL" query = urllib.parse.parse_qs(url.query) assert "key" not in query, "API key query parameter" assert url.path.startswith(f"/t/{self.route}/v1beta/"), url.path return url, query
@staticmethod def _json(handler, value, status=200): payload = json.dumps(value).encode() handler.send_response(status) handler.send_header("Content-Type", "application/json") handler.end_headers() handler.wfile.write(payload)
def _get(owner, self): def serve(): url, query = owner._transport(self) assert url.path == f"/t/{owner.route}/v1beta/models", url.path assert set(query) <= {"pageToken", "pageSize"}, query token = query.get("pageToken", [""])[0] assert token in owner.pages, f"unexpected page token {token!r}" with owner._lock: owner.catalog_requests.append(token) owner._json(self, owner.pages[token])
owner._checked(self, serve)
def _post(owner, self): def check(): url, query = owner._transport(self) model_path = f"/t/{owner.route}/v1beta/models/{owner.model}" if url.path.endswith(":countTokens"): assert url.path == model_path + ":countTokens", url.path assert query == {}, query else: assert url.path == model_path + ":streamGenerateContent", url.path assert query == {"alt": ["sse"]}, query
if owner._checked(self, check): super()._post(self)
def _count(owner, self, request): def check(): assert set(request) == {"generateContentRequest"}, request content = request["generateContentRequest"] assert content["model"] == f"models/{owner.model}", content assert content["contents"], "count lost conversation contents" assert content["systemInstruction"]["parts"], "count lost instructions" assert content["tools"][0]["functionDeclarations"], "count lost tools" assert "generationConfig" not in content, ( "count includes generation options" )
if owner._checked(self, check): super()._count(self, request)