forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopilotRequestSessionIdE2ETest.java
More file actions
110 lines (93 loc) · 5.28 KB
/
Copy pathCopilotRequestSessionIdE2ETest.java
File metadata and controls
110 lines (93 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot;
import static com.github.copilot.CopilotRequestTestSupport.SYNTHETIC_TEXT;
import static com.github.copilot.CopilotRequestTestSupport.assistantText;
import static com.github.copilot.CopilotRequestTestSupport.newLlmClient;
import static com.github.copilot.CopilotRequestTestSupport.setupCapiAuth;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import com.github.copilot.CopilotRequestTestSupport.InterceptedRequest;
import com.github.copilot.CopilotRequestTestSupport.RecordingRequestHandler;
import com.github.copilot.generated.AssistantMessageEvent;
import com.github.copilot.rpc.MessageOptions;
import com.github.copilot.rpc.PermissionHandler;
import com.github.copilot.rpc.ProviderConfig;
import com.github.copilot.rpc.SessionConfig;
/**
* Verifies that the triggering session id is threaded into every inference
* request context, for both CAPI and BYOK sessions, and that per-session ids
* differ.
*/
public class CopilotRequestSessionIdE2ETest {
private static E2ETestContext ctx;
@BeforeAll
static void setup() throws Exception {
ctx = E2ETestContext.create();
}
@AfterAll
static void teardown() throws Exception {
if (ctx != null) {
ctx.close();
}
}
@Test
void threadsSessionIdForCapiAndByok() throws Exception {
setupCapiAuth(ctx);
RecordingRequestHandler handler = new RecordingRequestHandler(SYNTHETIC_TEXT);
try (CopilotClient client = newLlmClient(ctx, handler)) {
// CAPI session.
CopilotSession capiSession = client
.createSession(new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get();
String capiSessionId = capiSession.getSessionId();
AssistantMessageEvent capiResult = capiSession.sendAndWait(new MessageOptions().setPrompt("Say OK."))
.get(60, TimeUnit.SECONDS);
capiSession.close();
List<InterceptedRequest> capiInference = handler.inferenceRequests();
assertFalse(capiInference.isEmpty(), "Expected at least one intercepted inference request");
for (InterceptedRequest r : capiInference) {
assertEquals(capiSessionId, r.sessionId(), "CAPI inference request must carry the session id");
assertAgentMetadata(r);
}
assertTrue(assistantText(capiResult).contains("OK from the synthetic"),
"Expected synthetic content in CAPI assistant reply, got " + assistantText(capiResult));
// BYOK session.
int before = handler.inferenceRequests().size();
ProviderConfig provider = new ProviderConfig().setType("openai").setWireApi("responses")
.setBaseUrl("https://byok.invalid/v1").setApiKey("byok-secret").setModelId("claude-sonnet-4.5")
.setWireModel("claude-sonnet-4.5");
CopilotSession byokSession = client
.createSession(new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
.setModel("claude-sonnet-4.5").setProvider(provider))
.get();
String byokSessionId = byokSession.getSessionId();
AssistantMessageEvent byokResult = byokSession.sendAndWait(new MessageOptions().setPrompt("Say OK."))
.get(60, TimeUnit.SECONDS);
byokSession.close();
List<InterceptedRequest> byokInference = handler.inferenceRequests();
assertTrue(byokInference.size() > before, "Expected at least one intercepted BYOK inference request");
for (InterceptedRequest r : byokInference.subList(before, byokInference.size())) {
assertEquals(byokSessionId, r.sessionId(), "BYOK inference request must carry the session id");
assertAgentMetadata(r);
}
assertNotEquals(capiSessionId, byokSessionId, "Expected per-session ids to differ between turns");
assertTrue(assistantText(byokResult).contains("OK from the synthetic"),
"Expected synthetic content in BYOK assistant reply, got " + assistantText(byokResult));
}
}
private static void assertAgentMetadata(InterceptedRequest request) {
assertNotNull(request.agentId(), "Inference request must carry an agent id");
assertFalse(request.agentId().isEmpty(), "Inference request must carry an agent id");
assertNotNull(request.interactionType(), "Inference request must carry an interaction type");
assertFalse(request.interactionType().isEmpty(), "Inference request must carry an interaction type");
}
}