Skip to content

Commit d9690ad

Browse files
committed
fix(appserver): serialize turns per session
1 parent ec08e02 commit d9690ad

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

src/main/java/io/github/easy4j/codex/appserver/CodexAppServerClient.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public class CodexAppServerClient implements AutoCloseable {
7878
private final ObjectMapper objectMapper =
7979
JsonMapper.builder().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).build();
8080
private final ThreadMappingCache threadBySession;
81+
private final SessionExecutionCoordinator sessionCoordinator = new SessionExecutionCoordinator();
8182
private final Object httpClientLock = new Object();
8283
private final ExecutorService clientExecutor = Executors.newCachedThreadPool(r -> {
8384
Thread thread = new Thread(r, "codex-app-server-client");
@@ -142,6 +143,19 @@ public CompletableFuture<AppServerTurnResult> runTurnAsync(AppServerTurnRequest
142143
if (Objects.isNull(request.getPrompt()) || request.getPrompt().trim().isEmpty()) {
143144
throw new IllegalArgumentException("Codex prompt must not be blank");
144145
}
146+
String sessionKey = request.normalizedSessionKey();
147+
if (Objects.isNull(sessionKey)) {
148+
return startTurn(request);
149+
}
150+
return sessionCoordinator.submit(sessionKey, () -> startTurn(request));
151+
}
152+
153+
private CompletableFuture<AppServerTurnResult> startTurn(AppServerTurnRequest request) {
154+
if (closed) {
155+
CompletableFuture<AppServerTurnResult> failed = new CompletableFuture<>();
156+
failed.completeExceptionally(new IllegalStateException("Codex app-server client is closed"));
157+
return failed;
158+
}
145159
CodexAppServerTurn turn =
146160
new CodexAppServerTurn(request, config, objectMapper, threadBySession, httpClient());
147161
return turn.start();

0 commit comments

Comments
 (0)