Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add get_session endpoint in code interpreter API #689

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ Future<?> closeSession() {
return Future.succeededFuture();
}

Future<?> getSession() {
context.getRequest()
.body()
.compose(body -> {
CodeInterpreterSessionId data = convertJson(body, CodeInterpreterSessionId.class);
return vertx.executeBlocking(() -> service.getSession(context, data.getSessionId()), false);
})
.onSuccess(this::respondJson)
.onFailure(this::respondError);

return Future.succeededFuture();
}

Future<?> executeCode() {
context.getRequest()
.body()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ public class ControllerSelector {

private static final Pattern APP_SCHEMAS = Pattern.compile("^/v1/application_type_schemas/(schemas|schema|meta_schema)?");
private static final Pattern CODE_INTERPRETER = Pattern.compile("^/v1/ops/code_interpreter/"
+ "(open_session|close_session|execute_code|"
+ "upload_file|download_file|list_files|"
+ "transfer_input_file|transfer_output_file)$");
+ "(open_session|close_session|get_session|"
+ "execute_code|"
+ "upload_file|download_file|list_files|"
+ "transfer_input_file|transfer_output_file)$");

static {
// GET routes
Expand Down Expand Up @@ -294,6 +295,7 @@ public class ControllerSelector {
return switch (operation) {
case "open_session" -> controller::openSession;
case "close_session" -> controller::closeSession;
case "get_session" -> controller::getSession;
case "execute_code" -> controller::executeCode;
case "upload_file" -> controller::uploadFile;
case "download_file" -> controller::downloadFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,21 @@ private void cleanupSession(ResourceDescriptor resource, Predicate<CodeInterpret
}
}

public CodeInterpreterSession getSession(ProxyContext context, String sessionId) {
verifyActive();
verifySessionId(sessionId);

ResourceDescriptor resource = sessionResource(context, sessionId);
String json = resourceService.getResource(resource);
CodeInterpreterSession session = convertToObject(json, CodeInterpreterSession.class);

if (session == null) {
throw new ResourceNotFoundException("Session is not found: " + sessionId);
}

return session;
}

public CodeInterpreterSession touchSession(ProxyContext context, String sessionId) {
verifyActive();
verifySessionId(sessionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ void testStatefulWorkflow() {
{}""");
verifyJson(response, 200, """
{"sessionId":"0123"}""");
response = send(HttpMethod.POST, "/v1/ops/code_interpreter/get_session", null, """
{"sessionId":"0123"}""");
verifyJson(response, 200, """
{"sessionId":"0123"}""");

webServer.map(HttpMethod.POST, "/execute_code", 200, """
{"status":"SUCCESS","stdout":"","stderr":"","result":{"text/plain":"3"},"display":[]}""");
Expand Down Expand Up @@ -91,6 +95,9 @@ void testStatefulWorkflow() {
{"sessionId":"0123"}""");
verifyJson(response, 200, """
{"sessionId":"0123"}""");
response = send(HttpMethod.POST, "/v1/ops/code_interpreter/get_session", null, """
{"sessionId":"0123"}""");
verify(response, 404, "Session is not found: 0123");
}

@Test
Expand Down
Loading