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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387"""GitHub Copilot is a model provider over the Copilot API.
GitHub's device sign-in stores an account, the daemon mints a short-livedCopilot token from the GitHub token, and each model family is served on theroute Copilot publishes for it: Chat Completions, OpenAI Responses, or AnthropicMessages. The request carries GitHub's client identity, and the picker listsonly the models the account may actually pick.
The sign-in writes the daemon's own creds, so the class is exclusive."""
import atexitimport jsonimport timeimport unittestimport urllib.parse
from github_copilot_support import ACCESS_TOKEN, COPILOT_TOKEN, Githubfrom harness import ( Albedo, Provider, Reply, exclusive_class, operation_id, text,)
CHAT_MODEL = "gemini-3.7-flash"RESPONSES_MODEL = "gpt-5.4-luna"MESSAGES_MODEL = "claude-haiku-5.5"
# The Copilot API's own model list: what the account may pick, and what each# model is served on.API_MODELS = { "data": [ { "id": CHAT_MODEL, "name": "Gemini 3.7 Flash", "version": "2026-05-01", "model_picker_enabled": True, "supported_endpoints": ["/chat/completions"], "capabilities": { "family": "gemini", "limits": {"max_context_window_tokens": 1000000}, "supports": {"tool_calls": True, "streaming": True, "vision": True}, }, }, { "id": RESPONSES_MODEL, "name": "GPT-5.4 Luna", "version": "2026-06-01", "model_picker_enabled": True, "supported_endpoints": ["/responses"], "capabilities": { "family": "gpt", "limits": {"max_context_window_tokens": 400000}, "supports": {"tool_calls": True, "streaming": True}, }, }, { "id": MESSAGES_MODEL, "name": "Claude Haiku 5.5", "version": "2026-07-01", "model_picker_enabled": True, "supported_endpoints": ["/v1/messages"], "capabilities": { "family": "claude", "limits": {"max_context_window_tokens": 200000}, "supports": {"tool_calls": True, "streaming": True}, }, }, { "id": "gpt-4o-mini", "name": "GPT-4o mini", "version": "2024-07-18", "model_picker_enabled": False, "capabilities": {"family": "gpt", "supports": {"tool_calls": False}}, }, ]}
# models.dev's `github-copilot` namespace.MODELS_DEV = { "github-copilot": { "id": "github-copilot", "name": "GitHub Copilot", "env": ["GITHUB_TOKEN"], "api": "https://api.githubcopilot.com", "models": { CHAT_MODEL: { "id": CHAT_MODEL, "limit": {"context": 1000000, "output": 64000}, }, RESPONSES_MODEL: { "id": RESPONSES_MODEL, "limit": {"context": 400000, "output": 128000}, }, MESSAGES_MODEL: { "id": MESSAGES_MODEL, "limit": {"context": 200000, "output": 64000}, }, }, }}
def anthropic_events(answer): """An Anthropic Messages stream carrying `answer` as text.""" return [ { "type": "message_start", "message": { "id": "msg_fixture", "usage": {"input_tokens": 10, "output_tokens": 1}, }, }, { "type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}, }, { "type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": answer}, }, {"type": "content_block_stop", "index": 0}, { "type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence": None}, "usage": {"output_tokens": 5}, }, {"type": "message_stop"}, ]
# One fake GitHub per test process, so every test in this file prepares the# daemon with the same hosts: the runner runs test cases one at a time, never a# class's fixtures._github = None
def github(): global _github if _github is None: _github = Github().start() atexit.register(_github.stop) return _github
@exclusive_classclass GithubCopilotTests(unittest.TestCase): @property def github(self): return github()
def start(self, model, replies, *, token="github-fixture-seed"): """A Copilot profile pinned at `model` over a scripted Copilot API.""" provider = Provider(scripting(list(replies)), catalog=API_MODELS) self.addCleanup(provider.close) app = Albedo( provider, providers={ "copilot": { "extension": "github-copilot", "baseUrl": provider.url, "model": model, "protocol": "chat_completions", } }, prepare=self.prepare, ) app.__enter__() self.addCleanup(app.__exit__, None, None, None) if token: app.store_secrets( "accounts", { "github-copilot": { "type": "key", "apiKey": token, "label": "copilot fixture token", } }, ) return app, provider
def prepare(self, app): (app.home / "models.json").write_text(json.dumps(MODELS_DEV)) app.env["COPILOT_GITHUB_WEB"] = self.github.url app.env["COPILOT_GITHUB_API"] = self.github.url
def ask(self, app, session, prompt): app.prompt(session, prompt).close() app.idle(session)
def test_device_sign_in_stores_the_account_and_streams_chat_completions(self): app, provider = self.start(CHAT_MODEL, [text("from copilot")], token=None) login = operation_id() with app.api( f"/auth/logins/{login}", {"provider": "github-copilot", "flow": "browser", "values": {}}, method="PUT", ) as response: json.load(response)
# The device code arrives once GitHub answered the authorization start. deadline = time.monotonic() + 30 while True: with app.api(f"/auth/logins/{login}") as response: state = json.load(response) if state.get("url") or state["state"] not in ("waiting", "exchanging"): break self.assertLess(time.monotonic(), deadline, state) time.sleep(0.05) if not state.get("url"): self.fail(state) self.assertTrue(state["url"].startswith(self.github.url), state) self.assertIn("ABCD-1234", json.dumps(state), state)
self.github.authorize() while state["state"] in ("waiting", "exchanging"): with app.api(f"/auth/logins/{login}") as response: state = json.load(response) self.assertLess(time.monotonic(), deadline, state) time.sleep(0.05) if state["state"] != "complete": self.fail({"state": state, "seen": self.github.seen})
# The account is stored as an OAuth credential, not a token. stored = json.loads((app.home / "creds.json").read_text()) account = stored["accounts"]["github-copilot"] self.assertEqual(account["type"], "oauth", account) self.assertEqual(account["access"], ACCESS_TOKEN, account) self.assertEqual(account["accountId"], "octocat", account)
session = app.session() self.ask(app, session, "hello")
# The request carried a Copilot token GitHub traded for the access # token under its own client identity, never the GitHub token itself. exchanges = [ entry for entry in self.github.requests("/copilot_internal/v2/token") if entry["headers"].get("authorization") == f"token {ACCESS_TOKEN}" ] self.assertTrue(exchanges, self.github.seen) self.assertEqual(exchanges[0]["headers"]["editor-version"], "vscode/1.104.1") self.assertEqual( exchanges[0]["headers"]["copilot-integration-id"], "vscode-chat" )
[request] = provider.requests self.assertTrue(request["path"].endswith("/chat/completions"), request["path"]) self.assertEqual(request["authorization"], f"Bearer {COPILOT_TOKEN}") headers = request["headers"] self.assertEqual(headers["editor-version"], "vscode/1.104.1") self.assertEqual(headers["editor-plugin-version"], "copilot-chat/0.30.0") self.assertEqual(headers["copilot-integration-id"], "vscode-chat") self.assertEqual(headers["x-github-api-version"], "2026-08-01") self.assertEqual(headers["x-initiator"], "user") self.assertEqual(request["request"]["model"], CHAT_MODEL) self.assertEqual( request["request"]["messages"][-1], {"role": "user", "content": "hello"} )
answers = [ entry for entry in app.history(session)["items"] if entry["kind"] == "assistant" ] self.assertEqual( answers[-1]["content"], [{"kind": "text", "text": "from copilot"}] )
def test_gpt5_models_stream_on_the_responses_route(self): app, provider = self.start( RESPONSES_MODEL, [text("from responses")], token="github-fixture-responses" ) session = app.session() self.ask(app, session, "hello") [request] = provider.requests self.assertTrue(request["path"].endswith("/responses"), request["path"]) self.assertEqual(request["authorization"], f"Bearer {COPILOT_TOKEN}") self.assertEqual(request["request"]["model"], RESPONSES_MODEL) self.assertIn("input", request["request"]) self.assertNotIn("messages", request["request"]) # Strict schemas are validated literally by Copilot; albedo's tools carry # optional properties, so they are offered non-strict. tools = request["request"]["tools"] self.assertTrue(tools, request["request"]) self.assertTrue(all(tool["strict"] is False for tool in tools), tools) self.assertEqual(request["headers"]["x-initiator"], "user")
answers = [ entry for entry in app.history(session)["items"] if entry["kind"] == "assistant" ] self.assertEqual( answers[-1]["content"], [{"kind": "text", "text": "from responses"}] )
def test_claude_models_stream_on_the_anthropic_messages_route(self): app, provider = self.start( MESSAGES_MODEL, [Reply("text", events=anthropic_events("from messages"))], token="github-fixture-messages", ) session = app.session() self.ask(app, session, "hello") [request] = provider.requests self.assertTrue(request["path"].endswith("/v1/messages"), request["path"]) self.assertEqual(request["authorization"], f"Bearer {COPILOT_TOKEN}") headers = request["headers"] self.assertEqual(headers["anthropic-version"], "2023-06-01") self.assertEqual(headers["anthropic-beta"], "interleaved-thinking-2025-05-14") self.assertNotIn("x-api-key", headers) self.assertEqual(request["request"]["model"], MESSAGES_MODEL) # The catalogue's ceiling, not the wire's 8192 default: a long answer # must not be cut off for a budget the turn never named. self.assertEqual(request["request"]["max_tokens"], 64000) self.assertIn("messages", request["request"])
answers = [ entry for entry in app.history(session)["items"] if entry["kind"] == "assistant" ] self.assertEqual( answers[-1]["content"], [{"kind": "text", "text": "from messages"}] )
def test_the_picker_lists_only_the_models_the_account_may_pick(self): app, provider = self.start(CHAT_MODEL, []) endpoint = urllib.parse.quote(provider.url, safe="") with app.api( f"/models?provider=github-copilot&endpoint={endpoint}" ) as response: listed = json.load(response) self.assertEqual( sorted(item["id"] for item in listed["items"]), sorted([CHAT_MODEL, RESPONSES_MODEL, MESSAGES_MODEL]), )
def test_a_pasted_github_token_is_saved_as_a_key_account_and_serves(self): """The other half of the sign-in: a pasted token enters as a key account.
`/auth/keys` stores a key only for providers `albedo_key_accounts` lists, so a Copilot profile with no saved account and no GITHUB_TOKEN depends on that entry to be usable at all. """ app, provider = self.start( CHAT_MODEL, [text("from a pasted token")], token=None ) account = operation_id() with app.api( f"/auth/keys/{account}", { "provider": "github-copilot", "endpoint": provider.url, "api_key": "github-fixture-pasted", "label": "fixture", }, method="PUT", ) as response: saved = json.load(response) self.assertEqual(saved["kind"], "key")
session = app.session() self.ask(app, session, "where do you run") answers = [ entry for entry in app.history(session)["items"] if entry["kind"] == "assistant" ] self.assertEqual( answers[-1]["content"], [{"kind": "text", "text": "from a pasted token"}] )
def scripting(replies): def reply(request): return replies.pop(0) if replies else text("done")
return reply