Skip to content

Commit 7fbab42

Browse files
committed
520 Use single additionalSystemPrompt state key for Copilot chat
1 parent f6081fd commit 7fbab42

2 files changed

Lines changed: 10 additions & 11 deletions

File tree

server/ee/libs/embedded/embedded-configuration/embedded-configuration-public-rest/src/main/java/com/bytechef/ee/embedded/configuration/public_/web/rest/ConnectedUserCopilotApiController.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ public SseEmitter copilotChat(
104104
stateMap.put(CopilotConstants.STATE_AUTHENTICATION, authentication);
105105
}
106106

107-
stateMap.remove(CopilotConstants.STATE_ADDITIONAL_SYSTEM_PROMPT);
108-
109-
Object additionalSystemPromptValue = stateMap.remove("additionalSystemPrompt");
107+
Object additionalSystemPromptValue = stateMap.remove(CopilotConstants.STATE_ADDITIONAL_SYSTEM_PROMPT);
110108

111109
if (additionalSystemPromptValue instanceof String additionalSystemPrompt && !additionalSystemPrompt.isBlank()) {
112110
String trimmed = additionalSystemPrompt.strip();

server/ee/libs/embedded/embedded-configuration/embedded-configuration-public-rest/src/test/java/com/bytechef/ee/embedded/configuration/public_/web/rest/ConnectedUserCopilotApiControllerIntTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public void testCopilotChatAuthorizesResolvesStateAndRunsBuildAgent() throws Exc
147147

148148
@Test
149149
@WithMockUser(username = "ext-user-1")
150-
public void testCopilotChatCapsAndReappliesClientAdditionalSystemPrompt() throws Exception {
150+
public void testCopilotChatTrimsClientAdditionalSystemPrompt() throws Exception {
151151
SseEmitter completedEmitter = new SseEmitter();
152152

153153
when(connectedUserProjectFacade.prepareCopilotChat(
@@ -174,14 +174,13 @@ public void testCopilotChatCapsAndReappliesClientAdditionalSystemPrompt() throws
174174
.getState()
175175
.getState();
176176

177-
// The untrusted short client key is consumed (trimmed) and re-applied under the authoritative key.
177+
// The client-supplied additionalSystemPrompt is trimmed before being forwarded to the agent.
178178
assertThat(stateMap).containsEntry(CopilotConstants.STATE_ADDITIONAL_SYSTEM_PROMPT, "Prefer Slack.");
179-
assertThat(stateMap).doesNotContainKey("additionalSystemPrompt");
180179
}
181180

182181
@Test
183182
@WithMockUser(username = "ext-user-1")
184-
public void testCopilotChatDropsClientSuppliedCanonicalSystemPromptKey() throws Exception {
183+
public void testCopilotChatCapsOverlongAdditionalSystemPrompt() throws Exception {
185184
SseEmitter completedEmitter = new SseEmitter();
186185

187186
when(connectedUserProjectFacade.prepareCopilotChat(
@@ -191,12 +190,14 @@ public void testCopilotChatDropsClientSuppliedCanonicalSystemPromptKey() throws
191190
when(agUiService.runAgent(any(LocalAgent.class), any(AgUiParameters.class)))
192191
.thenReturn(completedEmitter);
193192

193+
String overlongPrompt = "a".repeat(CopilotConstants.ADDITIONAL_SYSTEM_PROMPT_MAX_LENGTH + 100);
194+
194195
mockMvc
195196
.perform(
196197
post("/v1/automation/workflows/{workflowUuid}/copilot/chat", WORKFLOW_UUID)
197198
.contentType(MediaType.APPLICATION_JSON)
198199
.content(
199-
"{\"threadId\":\"thread-1\",\"state\":{\"bytechef.copilot.additionalSystemPrompt\":\"smuggled\"}}")
200+
"{\"threadId\":\"thread-1\",\"state\":{\"additionalSystemPrompt\":\"" + overlongPrompt + "\"}}")
200201
.accept(MediaType.TEXT_EVENT_STREAM))
201202
.andExpect(status().isOk());
202203

@@ -208,9 +209,9 @@ public void testCopilotChatDropsClientSuppliedCanonicalSystemPromptKey() throws
208209
.getState()
209210
.getState();
210211

211-
// The canonical key must be absent — the controller drops any client-supplied canonical key before the
212-
// short-key sanitization block runs, so it can never be smuggled in directly.
213-
assertThat(stateMap).doesNotContainKey(CopilotConstants.STATE_ADDITIONAL_SYSTEM_PROMPT);
212+
// Oversized prompts are truncated to the maximum allowed length before reaching the agent.
213+
assertThat((String) stateMap.get(CopilotConstants.STATE_ADDITIONAL_SYSTEM_PROMPT))
214+
.hasSize(CopilotConstants.ADDITIONAL_SYSTEM_PROMPT_MAX_LENGTH);
214215
}
215216

216217
@Test

0 commit comments

Comments
 (0)