Skip to content

Commit 56fc88e

Browse files
Copilotbrunoborges
andcommitted
Add SessionContext, SessionListFilter, and context_changed event
Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com>
1 parent 1e205f5 commit 56fc88e

7 files changed

Lines changed: 341 additions & 3 deletions

File tree

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.github.copilot.sdk.json.ResumeSessionResponse;
3131
import com.github.copilot.sdk.json.SessionConfig;
3232
import com.github.copilot.sdk.json.SessionLifecycleHandler;
33+
import com.github.copilot.sdk.json.SessionListFilter;
3334
import com.github.copilot.sdk.json.SessionMetadata;
3435

3536
/**
@@ -474,9 +475,41 @@ public CompletableFuture<Void> deleteSession(String sessionId) {
474475
* @see #resumeSession(String)
475476
*/
476477
public CompletableFuture<List<SessionMetadata>> listSessions() {
477-
return ensureConnected()
478-
.thenCompose(connection -> connection.rpc.invoke("session.list", Map.of(), ListSessionsResponse.class)
479-
.thenApply(ListSessionsResponse::sessions));
478+
return listSessions(null);
479+
}
480+
481+
/**
482+
* Lists all available sessions with optional filtering.
483+
* <p>
484+
* Returns metadata about all sessions that can be resumed, including their IDs,
485+
* start times, summaries, and context information. Use the filter parameter to
486+
* narrow down sessions by working directory, git repository, or branch.
487+
*
488+
* <h2>Example Usage</h2>
489+
*
490+
* <pre>{@code
491+
* // List all sessions
492+
* var allSessions = client.listSessions().get();
493+
*
494+
* // Filter by repository
495+
* var filter = new SessionListFilter().setRepository("owner/repo");
496+
* var repoSessions = client.listSessions(filter).get();
497+
* }</pre>
498+
*
499+
* @param filter
500+
* optional filter to narrow down sessions by context fields, or
501+
* {@code null} to list all sessions
502+
* @return a future that resolves with a list of session metadata
503+
* @see SessionMetadata
504+
* @see SessionListFilter
505+
* @see #resumeSession(String)
506+
*/
507+
public CompletableFuture<List<SessionMetadata>> listSessions(SessionListFilter filter) {
508+
return ensureConnected().thenCompose(connection -> {
509+
Map<String, Object> params = filter != null ? Map.of("filter", filter) : Map.of();
510+
return connection.rpc.invoke("session.list", params, ListSessionsResponse.class)
511+
.thenApply(ListSessionsResponse::sessions);
512+
});
480513
}
481514

482515
/**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public abstract sealed class AbstractSessionEvent permits
5454
SessionStartEvent, SessionResumeEvent, SessionErrorEvent, SessionIdleEvent, SessionInfoEvent,
5555
SessionModelChangeEvent, SessionHandoffEvent, SessionTruncationEvent, SessionSnapshotRewindEvent,
5656
SessionUsageInfoEvent, SessionCompactionStartEvent, SessionCompactionCompleteEvent, SessionShutdownEvent,
57+
SessionContextChangedEvent,
5758
// Assistant events
5859
AssistantTurnStartEvent, AssistantIntentEvent, AssistantReasoningEvent, AssistantReasoningDeltaEvent,
5960
AssistantMessageEvent, AssistantMessageDeltaEvent, AssistantTurnEndEvent, AssistantUsageEvent, AbortEvent,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
*--------------------------------------------------------------------------------------------*/
4+
5+
package com.github.copilot.sdk.events;
6+
7+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
8+
import com.fasterxml.jackson.annotation.JsonProperty;
9+
10+
/**
11+
* Event: session.context_changed
12+
* <p>
13+
* Fired when the working directory context changes between turns. Contains the
14+
* updated context information including cwd, git root, repository, and branch.
15+
*
16+
* @since 1.0.0
17+
*/
18+
@JsonIgnoreProperties(ignoreUnknown = true)
19+
public final class SessionContextChangedEvent extends AbstractSessionEvent {
20+
21+
@JsonProperty("data")
22+
private SessionContextChangedData data;
23+
24+
@Override
25+
public String getType() {
26+
return "session.context_changed";
27+
}
28+
29+
public SessionContextChangedData getData() {
30+
return data;
31+
}
32+
33+
public void setData(SessionContextChangedData data) {
34+
this.data = data;
35+
}
36+
37+
@JsonIgnoreProperties(ignoreUnknown = true)
38+
public record SessionContextChangedData(@JsonProperty("cwd") String cwd, @JsonProperty("gitRoot") String gitRoot,
39+
@JsonProperty("repository") String repository, @JsonProperty("branch") String branch) {
40+
}
41+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public class SessionEventParser {
6161
TYPE_MAP.put("session.usage_info", SessionUsageInfoEvent.class);
6262
TYPE_MAP.put("session.compaction_start", SessionCompactionStartEvent.class);
6363
TYPE_MAP.put("session.compaction_complete", SessionCompactionCompleteEvent.class);
64+
TYPE_MAP.put("session.context_changed", SessionContextChangedEvent.class);
6465
TYPE_MAP.put("user.message", UserMessageEvent.class);
6566
TYPE_MAP.put("pending_messages.modified", PendingMessagesModifiedEvent.class);
6667
TYPE_MAP.put("assistant.turn_start", AssistantTurnStartEvent.class);
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
*--------------------------------------------------------------------------------------------*/
4+
5+
package com.github.copilot.sdk.json;
6+
7+
import com.fasterxml.jackson.annotation.JsonInclude;
8+
import com.fasterxml.jackson.annotation.JsonProperty;
9+
10+
/**
11+
* Working directory context for a session.
12+
* <p>
13+
* Contains information about the working directory where the session was
14+
* created, including git repository information if applicable.
15+
*
16+
* @since 1.0.0
17+
*/
18+
@JsonInclude(JsonInclude.Include.NON_NULL)
19+
public class SessionContext {
20+
21+
@JsonProperty("cwd")
22+
private String cwd;
23+
24+
@JsonProperty("gitRoot")
25+
private String gitRoot;
26+
27+
@JsonProperty("repository")
28+
private String repository;
29+
30+
@JsonProperty("branch")
31+
private String branch;
32+
33+
/**
34+
* Gets the working directory where the session was created.
35+
*
36+
* @return the current working directory path
37+
*/
38+
public String getCwd() {
39+
return cwd;
40+
}
41+
42+
/**
43+
* Sets the working directory.
44+
*
45+
* @param cwd
46+
* the current working directory path
47+
*/
48+
public void setCwd(String cwd) {
49+
this.cwd = cwd;
50+
}
51+
52+
/**
53+
* Gets the git repository root directory.
54+
*
55+
* @return the git root path, or {@code null} if not in a git repository
56+
*/
57+
public String getGitRoot() {
58+
return gitRoot;
59+
}
60+
61+
/**
62+
* Sets the git repository root directory.
63+
*
64+
* @param gitRoot
65+
* the git root path
66+
*/
67+
public void setGitRoot(String gitRoot) {
68+
this.gitRoot = gitRoot;
69+
}
70+
71+
/**
72+
* Gets the GitHub repository in "owner/repo" format.
73+
*
74+
* @return the repository identifier, or {@code null} if not available
75+
*/
76+
public String getRepository() {
77+
return repository;
78+
}
79+
80+
/**
81+
* Sets the GitHub repository.
82+
*
83+
* @param repository
84+
* the repository in "owner/repo" format
85+
*/
86+
public void setRepository(String repository) {
87+
this.repository = repository;
88+
}
89+
90+
/**
91+
* Gets the current git branch.
92+
*
93+
* @return the branch name, or {@code null} if not available
94+
*/
95+
public String getBranch() {
96+
return branch;
97+
}
98+
99+
/**
100+
* Sets the git branch.
101+
*
102+
* @param branch
103+
* the branch name
104+
*/
105+
public void setBranch(String branch) {
106+
this.branch = branch;
107+
}
108+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
*--------------------------------------------------------------------------------------------*/
4+
5+
package com.github.copilot.sdk.json;
6+
7+
import com.fasterxml.jackson.annotation.JsonInclude;
8+
import com.fasterxml.jackson.annotation.JsonProperty;
9+
10+
/**
11+
* Filter options for listing sessions.
12+
* <p>
13+
* Allows filtering sessions by working directory context fields such as cwd,
14+
* git root, repository, or branch.
15+
*
16+
* <h2>Example Usage</h2>
17+
*
18+
* <pre>{@code
19+
* // Filter sessions by repository
20+
* var filter = new SessionListFilter().setRepository("owner/repo");
21+
* var sessions = client.listSessions(filter).get();
22+
*
23+
* // Filter by working directory
24+
* var filter = new SessionListFilter().setCwd("/path/to/project");
25+
* var sessions = client.listSessions(filter).get();
26+
* }</pre>
27+
*
28+
* @see com.github.copilot.sdk.CopilotClient#listSessions(SessionListFilter)
29+
* @since 1.0.0
30+
*/
31+
@JsonInclude(JsonInclude.Include.NON_NULL)
32+
public class SessionListFilter {
33+
34+
@JsonProperty("cwd")
35+
private String cwd;
36+
37+
@JsonProperty("gitRoot")
38+
private String gitRoot;
39+
40+
@JsonProperty("repository")
41+
private String repository;
42+
43+
@JsonProperty("branch")
44+
private String branch;
45+
46+
/**
47+
* Gets the current working directory filter.
48+
*
49+
* @return the cwd filter, or {@code null} if not set
50+
*/
51+
public String getCwd() {
52+
return cwd;
53+
}
54+
55+
/**
56+
* Sets the filter for exact cwd match.
57+
*
58+
* @param cwd
59+
* the current working directory to filter by
60+
* @return this filter for method chaining
61+
*/
62+
public SessionListFilter setCwd(String cwd) {
63+
this.cwd = cwd;
64+
return this;
65+
}
66+
67+
/**
68+
* Gets the git root filter.
69+
*
70+
* @return the git root filter, or {@code null} if not set
71+
*/
72+
public String getGitRoot() {
73+
return gitRoot;
74+
}
75+
76+
/**
77+
* Sets the filter for git root directory.
78+
*
79+
* @param gitRoot
80+
* the git root path to filter by
81+
* @return this filter for method chaining
82+
*/
83+
public SessionListFilter setGitRoot(String gitRoot) {
84+
this.gitRoot = gitRoot;
85+
return this;
86+
}
87+
88+
/**
89+
* Gets the repository filter.
90+
*
91+
* @return the repository filter, or {@code null} if not set
92+
*/
93+
public String getRepository() {
94+
return repository;
95+
}
96+
97+
/**
98+
* Sets the filter for repository (in "owner/repo" format).
99+
*
100+
* @param repository
101+
* the repository identifier to filter by
102+
* @return this filter for method chaining
103+
*/
104+
public SessionListFilter setRepository(String repository) {
105+
this.repository = repository;
106+
return this;
107+
}
108+
109+
/**
110+
* Gets the branch filter.
111+
*
112+
* @return the branch filter, or {@code null} if not set
113+
*/
114+
public String getBranch() {
115+
return branch;
116+
}
117+
118+
/**
119+
* Sets the filter for git branch.
120+
*
121+
* @param branch
122+
* the branch name to filter by
123+
* @return this filter for method chaining
124+
*/
125+
public SessionListFilter setBranch(String branch) {
126+
this.branch = branch;
127+
return this;
128+
}
129+
}

src/main/java/com/github/copilot/sdk/json/SessionMetadata.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public class SessionMetadata {
4949
@JsonProperty("isRemote")
5050
private boolean isRemote;
5151

52+
@JsonProperty("context")
53+
private SessionContext context;
54+
5255
/**
5356
* Gets the unique identifier for this session.
5457
*
@@ -146,4 +149,26 @@ public boolean isRemote() {
146149
public void setRemote(boolean remote) {
147150
isRemote = remote;
148151
}
152+
153+
/**
154+
* Gets the working directory context from session creation.
155+
* <p>
156+
* Contains information about the working directory, git repository, and branch
157+
* where the session was created.
158+
*
159+
* @return the session context, or {@code null} if not available
160+
*/
161+
public SessionContext getContext() {
162+
return context;
163+
}
164+
165+
/**
166+
* Sets the working directory context.
167+
*
168+
* @param context
169+
* the session context
170+
*/
171+
public void setContext(SessionContext context) {
172+
this.context = context;
173+
}
149174
}

0 commit comments

Comments
 (0)