feat(studio): render structured AI conversations
Add reusable UI2 conversation balloons, tool cards, native Markdown, copy actions and controlled streaming scroll. Preserve typed ACP tool updates and chat lifecycle, add Alt+Enter sending, update specifications and qualify the UI through desktop tests. Include the Pong asset cache.
This commit is contained in:
@@ -194,11 +194,38 @@ public final class CodexAcpConnection implements AgentConnection {
|
||||
Event event=null;
|
||||
if(type.equals("agent_message_chunk")&&update.path("content").path("type").asText().equals("text"))
|
||||
event=new Event(id,Kind.TEXT,update.path("content").path("text").asText());
|
||||
else if(type.equals("tool_call")||type.equals("tool_call_update"))
|
||||
event=new Event(id,Kind.ACTIVITY,update.path("title").asText(update.path("status").asText("Tool activity")));
|
||||
else if(type.equals("tool_call")||type.equals("tool_call_update")){
|
||||
String toolId=update.path("toolCallId").asText();
|
||||
if(!toolId.isBlank()&&toolId.length()<=512){
|
||||
String content=null;
|
||||
if(update.has("content")&&!update.path("content").isNull()){
|
||||
var parts=new ArrayList<String>();
|
||||
for(var part:update.path("content")){
|
||||
if(part.path("type").asText().equals("content")&&part.path("content").path("type").asText().equals("text"))
|
||||
parts.add(part.path("content").path("text").asText());
|
||||
else if(part.path("type").asText().equals("diff"))
|
||||
parts.add(part.path("path").asText()+"\n"+part.path("oldText").asText()+"\n→\n"+part.path("newText").asText());
|
||||
}
|
||||
content=boundedToolText(String.join("\n",parts));
|
||||
}
|
||||
event=new Event(id,Kind.TOOL,"",new ToolUpdate(toolId,toolField(update,"title"),toolField(update,"kind"),
|
||||
toolField(update,"status"),toolField(update,"rawInput"),toolField(update,"rawOutput"),content));
|
||||
}
|
||||
}
|
||||
else if(type.equals("agent_thought_chunk"))event=new Event(id,Kind.ACTIVITY,"Working");
|
||||
if(event!=null){var acknowledgement=accepted.get(id);if(acknowledgement!=null)acknowledgement.complete(null);events.accept(event);}
|
||||
}
|
||||
private static String toolField(JsonNode update,String field){
|
||||
var value=update.get(field);
|
||||
if(value==null||value.isNull())return null;
|
||||
return boundedToolText(value.isTextual()?value.asText():value.toPrettyString());
|
||||
}
|
||||
private static String boundedToolText(String text){
|
||||
text=message(text);
|
||||
if(text.length()<=64_000)return text;
|
||||
int end=Character.isHighSurrogate(text.charAt(63_979))?63_979:63_980;
|
||||
return text.substring(0,end)+"\n[truncated]";
|
||||
}
|
||||
private void permission(JsonNode message){
|
||||
var params=message.path("params");String session=params.path("sessionId").asText();String key=message.path("id").toString();
|
||||
var choices=new ArrayList<Choice>();for(var o:params.path("options"))choices.add(new Choice(o.path("optionId").asText(),o.path("name").asText(),o.path("kind").asText()));
|
||||
|
||||
@@ -96,6 +96,10 @@ class StudioProjectsDesktopTest {
|
||||
private static void closeProject(Harness harness)throws Exception {
|
||||
fx(()->{harness.window.requestCloseProject();return null;});await(()->fx(()->harness.window.projectSession()==null&&harness.window.launcherStage().isShowing()));
|
||||
}
|
||||
private static void altEnter(TextArea prompt){
|
||||
prompt.requestFocus();
|
||||
prompt.fireEvent(new KeyEvent(KeyEvent.KEY_PRESSED,"","",KeyCode.ENTER,false,false,true,false));
|
||||
}
|
||||
@Test void acpSettingsLoginSendPermissionsDeleteAndCloseFollowTheSelectedChat()throws Exception {
|
||||
var mapper=new ObjectMapper();var setups=new ProjectLocalStudioSetupStore(mapper);
|
||||
var catalog=new ProjectCatalogService(temporary.resolve("projects"),mapper,setups);
|
||||
@@ -134,20 +138,31 @@ class StudioProjectsDesktopTest {
|
||||
fire(h.stage,"ai-prompt-toggle");fire(h.stage,"ai-compact-new-chat");
|
||||
var selector=fx(()->(ComboBox<Object>)h.stage.getScene().lookup("#ai-chat-select"));var a=fx(selector::getValue);
|
||||
var prompt=fx(()->(TextArea)h.stage.getScene().lookup("#ai-prompt"));
|
||||
fx(()->{prompt.setText("draft A · personagem");return null;});fire(h.stage,"ai-compact-new-chat");var b=fx(selector::getValue);
|
||||
fx(()->{prompt.setText("hold");return null;});fire(h.stage,"ai-send");
|
||||
fx(()->{
|
||||
prompt.setText("first line");prompt.positionCaret(prompt.getLength());
|
||||
prompt.fireEvent(new KeyEvent(KeyEvent.KEY_PRESSED,"","",KeyCode.ENTER,false,false,false,false));
|
||||
assertEquals("first line\n",prompt.getText(),"Enter remains a newline");
|
||||
assertTrue(((Button)h.stage.getScene().lookup("#ai-send")).getTooltip().getText().contains("Alt+Enter"));
|
||||
prompt.setText("draft A · personagem");return null;
|
||||
});fire(h.stage,"ai-compact-new-chat");var b=fx(selector::getValue);
|
||||
fx(()->{prompt.setText("hold");altEnter(prompt);return null;});
|
||||
var tabs=fx(()->(TabPane)h.stage.getScene().lookup("#project-tabs"));
|
||||
await(()->fx(()->"ai-tab".equals(tabs.getSelectionModel().getSelectedItem().getId())&&prompt.getText().isEmpty()));
|
||||
fx(()->{selector.setValue(a);return null;});assertEquals("draft A · personagem",fx(prompt::getText));
|
||||
assertEquals("",fx(()->((TextArea)h.stage.getScene().lookup("#ai-conversation")).getText()));
|
||||
fx(()->{altEnter(prompt);assertEquals("draft A · personagem",prompt.getText(),"Unavailable send must preserve the draft without a newline");return null;});
|
||||
assertTrue(fx(()->((p.studio.ui.javafx2.components.StudioConversation)h.stage.getScene().lookup("#ai-conversation")).conversation().items().isEmpty()));
|
||||
assertTrue(fx(()->((Label)h.stage.getScene().lookup("#ai-connection-status")).getText().contains("Chat 2")));
|
||||
fire(h.stage,"ai-cancel");await(()->Files.exists(project.rootPath().resolve("cancel.requested")));
|
||||
assertFalse(fx(()->h.stage.getScene().lookup("#ai-cancel").isDisabled()));
|
||||
Files.writeString(project.rootPath().resolve("release.cancel"),"continue");
|
||||
await(()->fx(()->h.stage.getScene().lookup("#ai-cancel").isDisabled()));
|
||||
fx(()->{selector.setValue(b);return null;});assertEquals("",fx(prompt::getText));
|
||||
assertTrue(fx(()->((TextArea)h.stage.getScene().lookup("#ai-conversation")).getText().contains("Cancelled")));
|
||||
fx(()->{prompt.setText("reject");return null;});fire(h.stage,"ai-send");
|
||||
assertTrue(fx(()->((p.studio.ui.javafx2.components.StudioConversation)h.stage.getScene().lookup("#ai-conversation")).conversation().items().stream().anyMatch(i->i.state().equals("cancelled"))));
|
||||
fx(()->{
|
||||
altEnter(prompt);assertEquals("",prompt.getText(),"Empty prompt stays empty");
|
||||
assertTrue(h.stage.getScene().lookup("#ai-cancel").isDisabled());
|
||||
prompt.setText("reject");altEnter(prompt);return null;
|
||||
});
|
||||
await(()->fx(()->((Label)h.stage.getScene().lookup("#ai-activity")).getText().contains("fixture rejection")));
|
||||
assertEquals("reject",fx(prompt::getText));
|
||||
fx(()->{prompt.setText("permission");return null;});fire(h.stage,"ai-send");
|
||||
@@ -157,6 +172,13 @@ class StudioProjectsDesktopTest {
|
||||
((Button)pane.lookupButton(ButtonType.CANCEL)).fire();return null;});
|
||||
await(()->fx(()->h.stage.getScene().lookup("#ai-cancel").isDisabled()));
|
||||
assertTrue(Files.readString(project.rootPath().resolve("permission.json")).contains("cancelled"));
|
||||
fx(()->{prompt.setText("tools");return null;});fire(h.stage,"ai-send");
|
||||
await(()->fx(()->h.stage.getScene().lookup("#ai-cancel").isDisabled()));
|
||||
await(()->fx(()->{
|
||||
var conversation=(p.studio.ui.javafx2.components.StudioConversation)h.stage.getScene().lookup("#ai-conversation");
|
||||
return conversation.conversation().items().stream().anyMatch(i->i.toolId().equals("t1")&&i.state().equals("completed")&&i.title().equals("Read config")&&i.text().equals("configuration"));
|
||||
}));
|
||||
capture(h.stage,"acp-structured-conversation-fixture");
|
||||
fx(()->{prompt.setText("hold");return null;});fire(h.stage,"ai-send");await(()->fx(()->prompt.getText().isEmpty()));
|
||||
chatAction(h.stage,"ai-compact-delete-chat");fire(chatDialog("ai-delete-dialog"),"ai-dialog-confirm");
|
||||
await(()->Files.exists(project.rootPath().resolve("cancel.requested")));
|
||||
|
||||
@@ -59,6 +59,16 @@ public final class AcpFixture {
|
||||
var params=request.putObject("params").put("sessionId",session);params.putObject("toolCall").put("title","Run fixture action");
|
||||
params.putArray("options").addObject().put("optionId","yes").put("name","Allow once").put("kind","allow_once");send(request);break;
|
||||
}
|
||||
if(text.equals("tools")){
|
||||
chunk(session,"agent_message_chunk","# Inspection\n\nReading the **configuration**.");
|
||||
tool(session,JSON.createObjectNode().put("sessionUpdate","tool_call").put("toolCallId","t1").put("title","Read config").put("kind","read").put("status","pending").put("rawInput","config.json"));
|
||||
tool(session,JSON.createObjectNode().put("sessionUpdate","tool_call_update").put("toolCallId","t1").put("status","in_progress"));
|
||||
var update=JSON.createObjectNode().put("sessionUpdate","tool_call_update").put("toolCallId","t1").put("status","completed");
|
||||
update.putArray("content").addObject().put("type","content").putObject("content").put("type","text").put("text","configuration");
|
||||
update.put("rawOutput","result");tool(session,update);
|
||||
chunk(session,"agent_message_chunk","The configuration is ready.");
|
||||
reply(id,JSON.createObjectNode().put("stopReason","end_turn"));break;
|
||||
}
|
||||
chunk(session,"agent_message_chunk","first ");
|
||||
if(text.equals("hold")){pendingPrompt=id;break;}
|
||||
chunk(session,"agent_thought_chunk","PRIVATE_THOUGHT_MUST_NOT_ESCAPE");
|
||||
@@ -91,6 +101,10 @@ public final class AcpFixture {
|
||||
return List.of(Path.of(System.getProperty("java.home"),"bin","java").toString(),"-cp",
|
||||
System.getProperty("studio.test.classpath",System.getProperty("java.class.path")),AcpFixture.class.getName(),mode);
|
||||
}
|
||||
private static void tool(String session,ObjectNode update)throws Exception {
|
||||
var frame=JSON.createObjectNode().put("jsonrpc","2.0").put("method","session/update");
|
||||
frame.putObject("params").put("sessionId",session).set("update",update);send(frame);
|
||||
}
|
||||
private static void chunk(String session,String kind,String value)throws Exception {
|
||||
var frame=JSON.createObjectNode().put("jsonrpc","2.0").put("method","session/update");
|
||||
frame.putObject("params").put("sessionId",session).putObject("update").put("sessionUpdate",kind)
|
||||
|
||||
@@ -32,6 +32,16 @@ class CodexAcpConnectionTest {
|
||||
assertThrows(ExecutionException.class,()->await(c.prompt(second,"overlap").completion()));
|
||||
await(c.cancel(first));assertEquals("cancelled",await(held.completion()));
|
||||
}
|
||||
@Test void toolIdentityAndPartialFieldsSurviveRealStdio()throws Exception {
|
||||
var c=open("normal");String id=await(c.newSession());
|
||||
var events=new CopyOnWriteArrayList<AgentConnection.Event>();c.events(events::add);
|
||||
var turn=c.prompt(id,"tools");await(turn.accepted());await(turn.completion());
|
||||
var tools=events.stream().filter(e->e.kind()==AgentConnection.Kind.TOOL).toList();assertEquals(3,tools.size());
|
||||
var first=tools.get(0).tool();assertEquals("t1",first.id());assertEquals("config.json",first.input());
|
||||
var partial=tools.get(1).tool();assertNull(partial.title());assertNull(partial.input());assertNull(partial.content());
|
||||
assertEquals("in_progress",partial.status());
|
||||
var last=tools.get(2).tool();assertEquals("configuration",last.content());assertEquals("result",last.output());
|
||||
}
|
||||
@Test void permissionIsNeverGrantedWithoutAnExplicitChoice()throws Exception {
|
||||
var c=open("normal");String id=await(c.newSession());
|
||||
var choice=new CompletableFuture<String>();c.permissions(p->{assertEquals(id,p.sessionId());return choice;});
|
||||
|
||||
Reference in New Issue
Block a user