forked from github/copilot-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkillsTest.java
More file actions
236 lines (185 loc) · 9 KB
/
SkillsTest.java
File metadata and controls
236 lines (185 loc) · 9 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
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.BeforeEach;
import org.junit.jupiter.api.Test;
import com.github.copilot.sdk.generated.AssistantMessageEvent;
import com.github.copilot.sdk.json.CustomAgentConfig;
import com.github.copilot.sdk.json.MessageOptions;
import com.github.copilot.sdk.json.PermissionHandler;
import com.github.copilot.sdk.json.SessionConfig;
/**
* Tests for skills configuration functionality.
*
* <p>
* These tests verify that skills can be loaded from skill directories and
* disabled via configuration. Snapshots are stored in test/snapshots/skills/.
* </p>
*/
public class SkillsTest {
private static E2ETestContext ctx;
private static final String SKILL_MARKER = "PINEAPPLE_COCONUT_42";
private Path skillsDir;
@BeforeAll
static void setup() throws Exception {
ctx = E2ETestContext.create();
}
@AfterAll
static void teardown() throws Exception {
if (ctx != null) {
ctx.close();
}
}
@BeforeEach
void setupEach() throws IOException {
// Ensure we start fresh each time
skillsDir = ctx.getWorkDir().resolve(".test_skills");
if (Files.exists(skillsDir)) {
deleteDirectory(skillsDir);
}
}
private void deleteDirectory(Path dir) throws IOException {
if (Files.exists(dir)) {
Files.walk(dir).sorted((a, b) -> b.compareTo(a)) // reverse order for deletion
.forEach(path -> {
try {
Files.delete(path);
} catch (IOException e) {
// Ignore
}
});
}
}
private Path createSkillDir() throws IOException {
Files.createDirectories(skillsDir);
// Create a skill subdirectory with SKILL.md
Path skillSubdir = skillsDir.resolve("test-skill");
Files.createDirectories(skillSubdir);
// Create a skill that instructs the model to include a specific marker in
// responses
String skillContent = """
---
name: test-skill
description: A test skill that adds a marker to responses
---
# Test Skill Instructions
IMPORTANT: You MUST include the exact text "%s" somewhere in EVERY response you give. This is a mandatory requirement. Include it naturally in your response.
"""
.formatted(SKILL_MARKER);
Files.writeString(skillSubdir.resolve("SKILL.md"), skillContent);
return skillsDir;
}
/**
* Verifies that skills are loaded and applied from skill directories.
*
* @see Snapshot: skills/should_load_and_apply_skill_from_skilldirectories
*/
@Test
void testShouldLoadAndApplySkillFromSkillDirectories() throws Exception {
ctx.configureForTest("skills", "should_load_and_apply_skill_from_skilldirectories");
Path skillsDir = createSkillDir();
SessionConfig config = new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
.setSkillDirectories(List.of(skillsDir.toString()));
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(config).get();
assertNotNull(session.getSessionId());
// The skill instructs the model to include a marker - verify it appears
AssistantMessageEvent response = session
.sendAndWait(new MessageOptions().setPrompt("Say hello briefly using the test skill."))
.get(60, TimeUnit.SECONDS);
assertNotNull(response);
assertTrue(response.getData().content().contains(SKILL_MARKER),
"Response should contain skill marker '" + SKILL_MARKER + "': " + response.getData().content());
session.close();
}
}
/**
* Verifies that skills are not applied when disabled via disabledSkills.
*
* @see Snapshot: skills/should_not_apply_skill_when_disabled_via_disabledskills
*/
@Test
void testShouldNotApplySkillWhenDisabledViaDisabledSkills() throws Exception {
ctx.configureForTest("skills", "should_not_apply_skill_when_disabled_via_disabledskills");
Path skillsDir = createSkillDir();
SessionConfig config = new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
.setSkillDirectories(List.of(skillsDir.toString())).setDisabledSkills(List.of("test-skill"));
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(config).get();
assertNotNull(session.getSessionId());
// The skill is disabled, so the marker should NOT appear
AssistantMessageEvent response = session
.sendAndWait(new MessageOptions().setPrompt("Say hello briefly using the test skill."))
.get(60, TimeUnit.SECONDS);
assertNotNull(response);
assertFalse(response.getData().content().contains(SKILL_MARKER),
"Response should NOT contain skill marker when skill is disabled: " + response.getData().content());
session.close();
}
}
/**
* Verifies that an agent with a Skills field can preload and invoke the skill.
*
* @see Snapshot: skills/should_allow_agent_with_skills_to_invoke_skill
*/
@Test
void testShouldAllowAgentWithSkillsToInvokeSkill() throws Exception {
ctx.configureForTest("skills", "should_allow_agent_with_skills_to_invoke_skill");
Path skillsDirPath = createSkillDir();
var agent = new CustomAgentConfig().setName("skill-agent").setDescription("An agent with access to test-skill")
.setPrompt("You are a helpful test agent.").setSkills(List.of("test-skill"));
SessionConfig config = new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
.setSkillDirectories(List.of(skillsDirPath.toString())).setCustomAgents(List.of(agent))
.setAgent("skill-agent");
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(config).get();
assertNotNull(session.getSessionId());
// The agent has Skills = ["test-skill"], so the skill content is preloaded
AssistantMessageEvent response = session
.sendAndWait(new MessageOptions().setPrompt("Say hello briefly using the test skill."))
.get(60, TimeUnit.SECONDS);
assertNotNull(response);
assertTrue(response.getData().content().contains(SKILL_MARKER),
"Response should contain skill marker '" + SKILL_MARKER + "': " + response.getData().content());
session.close();
}
}
/**
* Verifies that an agent without a Skills field does not get skill content
* injected.
*
* @see Snapshot: skills/should_not_provide_skills_to_agent_without_skills_field
*/
@Test
void testShouldNotProvideSkillsToAgentWithoutSkillsField() throws Exception {
ctx.configureForTest("skills", "should_not_provide_skills_to_agent_without_skills_field");
Path skillsDirPath = createSkillDir();
var agent = new CustomAgentConfig().setName("no-skill-agent").setDescription("An agent without skills access")
.setPrompt("You are a helpful test agent.");
SessionConfig config = new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
.setSkillDirectories(List.of(skillsDirPath.toString())).setCustomAgents(List.of(agent))
.setAgent("no-skill-agent");
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(config).get();
assertNotNull(session.getSessionId());
// The agent has no Skills field, so no skill content is injected
AssistantMessageEvent response = session
.sendAndWait(new MessageOptions().setPrompt("Say hello briefly using the test skill."))
.get(60, TimeUnit.SECONDS);
assertNotNull(response);
assertFalse(response.getData().content().contains(SKILL_MARKER),
"Response should NOT contain skill marker when agent has no Skills field: "
+ response.getData().content());
session.close();
}
}
}