Skip to content

Commit da9ec9c

Browse files
Copilotedburns
andauthored
Migrate events package to auto-generated SessionEvent types
Replace hand-written com.github.copilot.sdk.events package with auto-generated com.github.copilot.sdk.generated types. - Replace AbstractSessionEvent with SessionEvent from generated package - Replace SessionEventParser with Jackson native polymorphic deserialization - Update all main source and test files to use generated types - Adapt ForwardCompatibilityTest to remove getOriginalType() tests - Update type assertions for renamed data records (FooData -> FooEventData) - Fix enum type comparisons in tests (elicitation mode, shutdown type, etc.) - Update site documentation to reference generated package - Delete old events package Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: edburns <75821+edburns@users.noreply.github.com>
1 parent e9c0bf7 commit da9ec9c

File tree

93 files changed

+324
-3244
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+324
-3244
lines changed

src/main/java/com/github/copilot/sdk/CopilotSession.java

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,15 @@
2929
import com.fasterxml.jackson.annotation.JsonProperty;
3030
import com.fasterxml.jackson.databind.JsonNode;
3131
import com.fasterxml.jackson.databind.ObjectMapper;
32-
import com.github.copilot.sdk.events.AbstractSessionEvent;
33-
import com.github.copilot.sdk.events.AssistantMessageEvent;
34-
import com.github.copilot.sdk.events.CapabilitiesChangedEvent;
35-
import com.github.copilot.sdk.events.CommandExecuteEvent;
36-
import com.github.copilot.sdk.events.ElicitationRequestedEvent;
37-
import com.github.copilot.sdk.events.ExternalToolRequestedEvent;
38-
import com.github.copilot.sdk.events.PermissionRequestedEvent;
39-
import com.github.copilot.sdk.events.SessionErrorEvent;
40-
import com.github.copilot.sdk.events.SessionEventParser;
41-
import com.github.copilot.sdk.events.SessionIdleEvent;
32+
import com.github.copilot.sdk.generated.AssistantMessageEvent;
33+
import com.github.copilot.sdk.generated.CapabilitiesChangedEvent;
34+
import com.github.copilot.sdk.generated.CommandExecuteEvent;
35+
import com.github.copilot.sdk.generated.ElicitationRequestedEvent;
36+
import com.github.copilot.sdk.generated.ExternalToolRequestedEvent;
37+
import com.github.copilot.sdk.generated.PermissionRequestedEvent;
38+
import com.github.copilot.sdk.generated.SessionErrorEvent;
39+
import com.github.copilot.sdk.generated.SessionEvent;
40+
import com.github.copilot.sdk.generated.SessionIdleEvent;
4241
import com.github.copilot.sdk.json.AgentInfo;
4342
import com.github.copilot.sdk.json.CommandContext;
4443
import com.github.copilot.sdk.json.CommandDefinition;
@@ -116,7 +115,7 @@
116115
* @see CopilotClient#createSession(com.github.copilot.sdk.json.SessionConfig)
117116
* @see CopilotClient#resumeSession(String,
118117
* com.github.copilot.sdk.json.ResumeSessionConfig)
119-
* @see AbstractSessionEvent
118+
* @see SessionEvent
120119
* @since 1.0.0
121120
*/
122121
public final class CopilotSession implements AutoCloseable {
@@ -135,7 +134,7 @@ public final class CopilotSession implements AutoCloseable {
135134
private volatile SessionCapabilities capabilities = new SessionCapabilities();
136135
private final SessionUiApi ui;
137136
private final JsonRpcClient rpc;
138-
private final Set<Consumer<AbstractSessionEvent>> eventHandlers = ConcurrentHashMap.newKeySet();
137+
private final Set<Consumer<SessionEvent>> eventHandlers = ConcurrentHashMap.newKeySet();
139138
private final Map<String, ToolDefinition> toolHandlers = new ConcurrentHashMap<>();
140139
private final Map<String, CommandHandler> commandHandlers = new ConcurrentHashMap<>();
141140
private final AtomicReference<PermissionHandler> permissionHandler = new AtomicReference<>();
@@ -450,7 +449,7 @@ public CompletableFuture<AssistantMessageEvent> sendAndWait(MessageOptions optio
450449
var future = new CompletableFuture<AssistantMessageEvent>();
451450
var lastAssistantMessage = new AtomicReference<AssistantMessageEvent>();
452451

453-
Consumer<AbstractSessionEvent> handler = evt -> {
452+
Consumer<SessionEvent> handler = evt -> {
454453
if (evt instanceof AssistantMessageEvent msg) {
455454
lastAssistantMessage.set(msg);
456455
} else if (evt instanceof SessionIdleEvent) {
@@ -563,7 +562,7 @@ public CompletableFuture<AssistantMessageEvent> sendAndWait(MessageOptions optio
563562
*
564563
* <pre>{@code
565564
* // Collect all events
566-
* var events = new ArrayList<AbstractSessionEvent>();
565+
* var events = new ArrayList<SessionEvent>();
567566
* session.on(events::add);
568567
* }</pre>
569568
*
@@ -573,10 +572,10 @@ public CompletableFuture<AssistantMessageEvent> sendAndWait(MessageOptions optio
573572
* @throws IllegalStateException
574573
* if this session has been terminated
575574
* @see #on(Class, Consumer)
576-
* @see AbstractSessionEvent
575+
* @see SessionEvent
577576
* @see #setEventErrorPolicy(EventErrorPolicy)
578577
*/
579-
public Closeable on(Consumer<AbstractSessionEvent> handler) {
578+
public Closeable on(Consumer<SessionEvent> handler) {
580579
ensureNotTerminated();
581580
eventHandlers.add(handler);
582581
return () -> eventHandlers.remove(handler);
@@ -626,11 +625,11 @@ public Closeable on(Consumer<AbstractSessionEvent> handler) {
626625
* @throws IllegalStateException
627626
* if this session has been terminated
628627
* @see #on(Consumer)
629-
* @see AbstractSessionEvent
628+
* @see SessionEvent
630629
*/
631-
public <T extends AbstractSessionEvent> Closeable on(Class<T> eventType, Consumer<T> handler) {
630+
public <T extends SessionEvent> Closeable on(Class<T> eventType, Consumer<T> handler) {
632631
ensureNotTerminated();
633-
Consumer<AbstractSessionEvent> wrapper = event -> {
632+
Consumer<SessionEvent> wrapper = event -> {
634633
if (eventType.isInstance(event)) {
635634
handler.accept(eventType.cast(event));
636635
}
@@ -662,12 +661,12 @@ public <T extends AbstractSessionEvent> Closeable on(Class<T> eventType, Consume
662661
* @see #setEventErrorHandler(EventErrorHandler)
663662
* @see #setEventErrorPolicy(EventErrorPolicy)
664663
*/
665-
void dispatchEvent(AbstractSessionEvent event) {
664+
void dispatchEvent(SessionEvent event) {
666665
// Handle broadcast request events (protocol v3) before dispatching to user
667666
// handlers. These are fire-and-forget: the response is sent asynchronously.
668667
handleBroadcastEventAsync(event);
669668

670-
for (Consumer<AbstractSessionEvent> handler : eventHandlers) {
669+
for (Consumer<SessionEvent> handler : eventHandlers) {
671670
try {
672671
handler.accept(event);
673672
} catch (Exception e) {
@@ -697,7 +696,7 @@ void dispatchEvent(AbstractSessionEvent event) {
697696
* @param event
698697
* the event to handle
699698
*/
700-
private void handleBroadcastEventAsync(AbstractSessionEvent event) {
699+
private void handleBroadcastEventAsync(SessionEvent event) {
701700
if (event instanceof ExternalToolRequestedEvent toolEvent) {
702701
var data = toolEvent.getData();
703702
if (data == null || data.requestId() == null || data.toolName() == null) {
@@ -721,7 +720,8 @@ private void handleBroadcastEventAsync(AbstractSessionEvent event) {
721720
if (handler == null) {
722721
return; // This client doesn't handle permissions; another client will
723722
}
724-
executePermissionAndRespondAsync(data.requestId(), data.permissionRequest(), handler);
723+
executePermissionAndRespondAsync(data.requestId(),
724+
MAPPER.convertValue(data.permissionRequest(), PermissionRequest.class), handler);
725725
} else if (event instanceof CommandExecuteEvent cmdEvent) {
726726
var data = cmdEvent.getData();
727727
if (data == null || data.requestId() == null || data.commandName() == null) {
@@ -742,8 +742,8 @@ private void handleBroadcastEventAsync(AbstractSessionEvent event) {
742742
.setRequired(data.requestedSchema().required());
743743
}
744744
var context = new ElicitationContext().setSessionId(sessionId).setMessage(data.message())
745-
.setRequestedSchema(schema).setMode(data.mode()).setElicitationSource(data.elicitationSource())
746-
.setUrl(data.url());
745+
.setRequestedSchema(schema).setMode(data.mode() != null ? data.mode().getValue() : null)
746+
.setElicitationSource(data.elicitationSource()).setUrl(data.url());
747747
handleElicitationRequestAsync(context, data.requestId());
748748
}
749749
} else if (event instanceof CapabilitiesChangedEvent capEvent) {
@@ -1436,17 +1436,17 @@ CompletableFuture<Object> handleHooksInvoke(String hookType, JsonNode input) {
14361436
* @return a future that resolves with a list of all session events
14371437
* @throws IllegalStateException
14381438
* if this session has been terminated
1439-
* @see AbstractSessionEvent
1439+
* @see SessionEvent
14401440
*/
1441-
public CompletableFuture<List<AbstractSessionEvent>> getMessages() {
1441+
public CompletableFuture<List<SessionEvent>> getMessages() {
14421442
ensureNotTerminated();
14431443
return rpc.invoke("session.getMessages", Map.of("sessionId", sessionId), GetMessagesResponse.class)
14441444
.thenApply(response -> {
1445-
var events = new ArrayList<AbstractSessionEvent>();
1445+
var events = new ArrayList<SessionEvent>();
14461446
if (response.events() != null) {
14471447
for (JsonNode eventNode : response.events()) {
14481448
try {
1449-
AbstractSessionEvent event = SessionEventParser.parse(eventNode);
1449+
SessionEvent event = MAPPER.treeToValue(eventNode, SessionEvent.class);
14501450
if (event != null) {
14511451
events.add(event);
14521452
}

src/main/java/com/github/copilot/sdk/EventErrorHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
package com.github.copilot.sdk;
66

7-
import com.github.copilot.sdk.events.AbstractSessionEvent;
7+
import com.github.copilot.sdk.generated.SessionEvent;
88

99
/**
1010
* A handler for errors thrown by event handlers during event dispatch.
@@ -54,5 +54,5 @@ public interface EventErrorHandler {
5454
* @param exception
5555
* the exception thrown by the event handler
5656
*/
57-
void handleError(AbstractSessionEvent event, Exception exception);
57+
void handleError(SessionEvent event, Exception exception);
5858
}

src/main/java/com/github/copilot/sdk/RpcHandlerDispatcher.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
import com.fasterxml.jackson.databind.JsonNode;
1818
import com.fasterxml.jackson.databind.ObjectMapper;
19-
import com.github.copilot.sdk.events.AbstractSessionEvent;
20-
import com.github.copilot.sdk.events.SessionEventParser;
19+
import com.github.copilot.sdk.generated.SessionEvent;
2120
import com.github.copilot.sdk.json.PermissionRequestResult;
2221
import com.github.copilot.sdk.json.PermissionRequestResultKind;
2322
import com.github.copilot.sdk.json.SessionLifecycleEvent;
@@ -93,7 +92,7 @@ private void handleSessionEvent(JsonNode params) {
9392

9493
CopilotSession session = sessions.get(sessionId);
9594
if (session != null && eventNode != null) {
96-
AbstractSessionEvent event = SessionEventParser.parse(eventNode);
95+
SessionEvent event = MAPPER.treeToValue(eventNode, SessionEvent.class);
9796
if (event != null) {
9897
session.dispatchEvent(event);
9998
}

src/main/java/com/github/copilot/sdk/events/AbortEvent.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)