Skip to content

Commit eab7f09

Browse files
SerjborySergey Borisov
andauthored
Forward provider config to SessionConfig in GitHubCopilotAgent (fixes #5190) (#5195)
Co-authored-by: Sergey Borisov <[email protected]>
1 parent 68b9364 commit eab7f09

2 files changed

Lines changed: 205 additions & 1 deletion

File tree

‎python/packages/github_copilot/agent_framework_github_copilot/_agent.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
try:
3232
from copilot import CopilotClient, CopilotSession, SubprocessConfig
3333
from copilot.generated.session_events import PermissionRequest, SessionEvent, SessionEventType
34-
from copilot.session import MCPServerConfig, PermissionRequestResult, SystemMessageConfig
34+
from copilot.session import MCPServerConfig, PermissionRequestResult, ProviderConfig, SystemMessageConfig
3535
from copilot.tools import Tool as CopilotTool
3636
from copilot.tools import ToolInvocation, ToolResult
3737
except ImportError as _copilot_import_error:
@@ -120,6 +120,12 @@ class GitHubCopilotOptions(TypedDict, total=False):
120120
Supports both local (stdio) and remote (HTTP/SSE) servers.
121121
"""
122122

123+
provider: ProviderConfig
124+
"""Custom API provider configuration for BYOK (Bring Your Own Key) scenarios.
125+
Allows routing requests through your own OpenAI, Azure, or Anthropic endpoint
126+
instead of the default GitHub Copilot backend.
127+
"""
128+
123129

124130
OptionsT = TypeVar(
125131
"OptionsT",
@@ -232,6 +238,7 @@ def __init__(
232238
log_level = opts.pop("log_level", None)
233239
on_permission_request: PermissionHandlerType | None = opts.pop("on_permission_request", None)
234240
mcp_servers: dict[str, MCPServerConfig] | None = opts.pop("mcp_servers", None)
241+
provider: ProviderConfig | None = opts.pop("provider", None)
235242

236243
self._settings = load_settings(
237244
GitHubCopilotSettings,
@@ -247,6 +254,7 @@ def __init__(
247254
self._tools = normalize_tools(tools)
248255
self._permission_handler = on_permission_request
249256
self._mcp_servers = mcp_servers
257+
self._provider = provider
250258
self._default_options = opts
251259
self._started = False
252260

@@ -730,6 +738,7 @@ async def _create_session(
730738
opts.get("on_permission_request") or self._permission_handler or _deny_all_permissions
731739
)
732740
mcp_servers = opts.get("mcp_servers") or self._mcp_servers or None
741+
provider = opts.get("provider") or self._provider or None
733742
tools = self._prepare_tools(self._tools) if self._tools else None
734743

735744
return await self._client.create_session(
@@ -739,6 +748,7 @@ async def _create_session(
739748
system_message=system_message or None,
740749
tools=tools or None,
741750
mcp_servers=mcp_servers or None,
751+
provider=provider or None,
742752
)
743753

744754
async def _resume_session(self, session_id: str, streaming: bool) -> CopilotSession:
@@ -755,4 +765,5 @@ async def _resume_session(self, session_id: str, streaming: bool) -> CopilotSess
755765
streaming=streaming,
756766
tools=tools or None,
757767
mcp_servers=self._mcp_servers or None,
768+
provider=self._provider or None,
758769
)

‎python/packages/github_copilot/tests/test_github_copilot_agent.py‎

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,7 @@ async def test_session_resumed_for_same_session(
861861
streaming=unittest.mock.ANY,
862862
tools=unittest.mock.ANY,
863863
mcp_servers=unittest.mock.ANY,
864+
provider=unittest.mock.ANY,
864865
)
865866

866867
async def test_session_config_includes_model(
@@ -1084,6 +1085,198 @@ async def test_session_config_excludes_mcp_servers_when_not_set(
10841085
assert config["mcp_servers"] is None
10851086

10861087

1088+
class TestGitHubCopilotAgentProvider:
1089+
"""Test cases for provider configuration (BYOK / Managed Identity)."""
1090+
1091+
async def test_provider_passed_to_create_session(
1092+
self,
1093+
mock_client: MagicMock,
1094+
) -> None:
1095+
"""Test that provider config is passed through to create_session."""
1096+
from copilot.session import ProviderConfig
1097+
1098+
provider: ProviderConfig = {
1099+
"type": "azure",
1100+
"base_url": "https://my-resource.openai.azure.com",
1101+
"bearer_token": "test-token",
1102+
}
1103+
1104+
agent: GitHubCopilotAgent[GitHubCopilotOptions] = GitHubCopilotAgent(
1105+
client=mock_client,
1106+
default_options={"provider": provider},
1107+
)
1108+
await agent.start()
1109+
1110+
await agent._get_or_create_session(AgentSession()) # type: ignore
1111+
1112+
call_args = mock_client.create_session.call_args
1113+
config = call_args.kwargs
1114+
assert config["provider"]["type"] == "azure"
1115+
assert config["provider"]["base_url"] == "https://my-resource.openai.azure.com"
1116+
assert config["provider"]["bearer_token"] == "test-token"
1117+
1118+
async def test_provider_passed_to_resume_session(
1119+
self,
1120+
mock_client: MagicMock,
1121+
) -> None:
1122+
"""Test that provider config is passed through to resume_session."""
1123+
from copilot.session import ProviderConfig
1124+
1125+
provider: ProviderConfig = {
1126+
"type": "azure",
1127+
"base_url": "https://my-resource.openai.azure.com",
1128+
"bearer_token": "test-token",
1129+
}
1130+
1131+
agent: GitHubCopilotAgent[GitHubCopilotOptions] = GitHubCopilotAgent(
1132+
client=mock_client,
1133+
default_options={"provider": provider},
1134+
)
1135+
await agent.start()
1136+
1137+
session = AgentSession()
1138+
session.service_session_id = "existing-session-id"
1139+
1140+
await agent._get_or_create_session(session) # type: ignore
1141+
1142+
mock_client.resume_session.assert_called_once()
1143+
call_args = mock_client.resume_session.call_args
1144+
config = call_args.kwargs
1145+
assert config["provider"]["type"] == "azure"
1146+
1147+
async def test_session_config_excludes_provider_when_not_set(
1148+
self,
1149+
mock_client: MagicMock,
1150+
) -> None:
1151+
"""Test that provider is None in session config when not set."""
1152+
agent = GitHubCopilotAgent(client=mock_client)
1153+
await agent.start()
1154+
1155+
await agent._get_or_create_session(AgentSession()) # type: ignore
1156+
1157+
call_args = mock_client.create_session.call_args
1158+
config = call_args.kwargs
1159+
assert config["provider"] is None
1160+
1161+
async def test_resume_session_excludes_provider_when_not_set(
1162+
self,
1163+
mock_client: MagicMock,
1164+
) -> None:
1165+
"""Test that provider is None in resume session config when not set."""
1166+
agent = GitHubCopilotAgent(client=mock_client)
1167+
await agent.start()
1168+
1169+
session = AgentSession()
1170+
session.service_session_id = "existing-session-id"
1171+
1172+
await agent._get_or_create_session(session) # type: ignore
1173+
1174+
call_args = mock_client.resume_session.call_args
1175+
config = call_args.kwargs
1176+
assert config["provider"] is None
1177+
1178+
async def test_runtime_provider_takes_precedence(
1179+
self,
1180+
mock_client: MagicMock,
1181+
) -> None:
1182+
"""Test that runtime provider options override default_options provider."""
1183+
from copilot.session import ProviderConfig
1184+
1185+
default_provider: ProviderConfig = {
1186+
"type": "azure",
1187+
"base_url": "https://default.openai.azure.com",
1188+
"bearer_token": "default-token",
1189+
}
1190+
runtime_provider: ProviderConfig = {
1191+
"type": "openai",
1192+
"base_url": "https://runtime.openai.com",
1193+
"api_key": "runtime-key",
1194+
}
1195+
1196+
agent: GitHubCopilotAgent[GitHubCopilotOptions] = GitHubCopilotAgent(
1197+
client=mock_client,
1198+
default_options={"provider": default_provider},
1199+
)
1200+
await agent.start()
1201+
1202+
await agent._get_or_create_session( # type: ignore
1203+
AgentSession(),
1204+
runtime_options={"provider": runtime_provider},
1205+
)
1206+
1207+
call_args = mock_client.create_session.call_args
1208+
config = call_args.kwargs
1209+
assert config["provider"]["type"] == "openai"
1210+
assert config["provider"]["base_url"] == "https://runtime.openai.com"
1211+
1212+
async def test_provider_not_leaked_into_default_options(
1213+
self,
1214+
mock_client: MagicMock,
1215+
) -> None:
1216+
"""Test that provider is popped from opts and not left in _default_options."""
1217+
from copilot.session import ProviderConfig
1218+
1219+
provider: ProviderConfig = {
1220+
"type": "azure",
1221+
"base_url": "https://my-resource.openai.azure.com",
1222+
"bearer_token": "test-token",
1223+
}
1224+
1225+
agent: GitHubCopilotAgent[GitHubCopilotOptions] = GitHubCopilotAgent(
1226+
client=mock_client,
1227+
default_options={"provider": provider, "model": "gpt-5"},
1228+
)
1229+
1230+
assert "provider" not in agent._default_options
1231+
assert agent._provider is not None
1232+
assert agent._provider["type"] == "azure"
1233+
1234+
async def test_provider_coexists_with_other_options(
1235+
self,
1236+
mock_client: MagicMock,
1237+
) -> None:
1238+
"""Test that provider works alongside model, tools, and mcp_servers."""
1239+
from copilot.session import MCPServerConfig, ProviderConfig
1240+
1241+
provider: ProviderConfig = {
1242+
"type": "azure",
1243+
"base_url": "https://my-resource.openai.azure.com",
1244+
"bearer_token": "test-token",
1245+
}
1246+
mcp_servers: dict[str, MCPServerConfig] = {
1247+
"test-server": {
1248+
"type": "stdio",
1249+
"command": "echo",
1250+
"args": ["hello"],
1251+
"tools": ["*"],
1252+
},
1253+
}
1254+
1255+
def my_tool(arg: str) -> str:
1256+
"""A test tool."""
1257+
return arg
1258+
1259+
agent: GitHubCopilotAgent[GitHubCopilotOptions] = GitHubCopilotAgent(
1260+
client=mock_client,
1261+
tools=[my_tool],
1262+
default_options={
1263+
"model": "gpt-5",
1264+
"provider": provider,
1265+
"mcp_servers": mcp_servers,
1266+
},
1267+
)
1268+
await agent.start()
1269+
1270+
await agent._get_or_create_session(AgentSession()) # type: ignore
1271+
1272+
call_args = mock_client.create_session.call_args
1273+
config = call_args.kwargs
1274+
assert config["provider"]["type"] == "azure"
1275+
assert config["model"] == "gpt-5"
1276+
assert config["mcp_servers"] is not None
1277+
assert config["tools"] is not None
1278+
1279+
10871280
class TestGitHubCopilotAgentToolConversion:
10881281
"""Test cases for tool conversion."""
10891282

0 commit comments

Comments
 (0)