Skip to content

Commit 7fceca6

Browse files
authored
merge #26 from shikokuchuo/dev: Add timeout to list_r_sessions
2 parents 4bedc0e + 1c4623f commit 7fceca6

File tree

6 files changed

+38
-33
lines changed

6 files changed

+38
-33
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Imports:
2727
cli,
2828
ellmer,
2929
jsonlite,
30-
nanonext (>= 1.5.2.9016),
30+
nanonext (>= 1.5.2.9019),
3131
promises,
3232
rlang
3333
Depends: R (>= 4.1.0)

R/acquaint-package.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,12 @@
55
#' @import rlang
66
## usethis namespace: end
77
NULL
8+
9+
.onLoad <- function(libname, pkgname) {
10+
the$socket_url <- switch(
11+
Sys.info()[["sysname"]],
12+
Linux = "abstract://acquaint-socket",
13+
Windows = "ipc://acquaint-socket",
14+
"ipc:///tmp/acquaint-socket"
15+
)
16+
}

R/server.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mcp_server <- function() {
1414

1515
the$server_socket <- nanonext::socket("poly")
1616
on.exit(nanonext::reap(the$server_socket), add = TRUE)
17-
nanonext::dial(the$server_socket, url = sprintf("%s%d", acquaint_socket, 1L))
17+
nanonext::dial(the$server_socket, url = sprintf("%s%d", the$socket_url, 1L))
1818

1919
client <- nanonext::recv_aio(reader_socket, mode = "string", cv = cv)
2020
session <- nanonext::recv_aio(the$server_socket, mode = "string", cv = cv)

R/session.R

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,16 @@ mcp_session <- function() {
5555

5656
the$session_socket <- nanonext::socket("poly")
5757
i <- 1L
58-
suppressWarnings(
59-
while (i < 1024L) {
60-
# prevent indefinite loop
61-
nanonext::listen(
62-
the$session_socket,
63-
url = sprintf("%s%d", acquaint_socket, i)
64-
) ||
65-
break
66-
i <- i + 1L
67-
}
68-
)
58+
while (i < 1024L) {
59+
# prevent indefinite loop
60+
nanonext::listen(
61+
the$session_socket,
62+
url = sprintf("%s%d", the$socket_url, i),
63+
fail = "none"
64+
) ||
65+
break
66+
i <- i + 1L
67+
}
6968
the$session <- i
7069

7170
schedule_handle_message_from_server()

R/tools.R

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,22 @@ list_r_sessions <- function() {
66
on.exit(nanonext::reap(sock))
77
cv <- nanonext::cv()
88
monitor <- nanonext::monitor(sock, cv)
9-
suppressWarnings(
10-
for (i in seq_len(1024L)) {
11-
if (
12-
nanonext::dial(
13-
sock,
14-
url = sprintf("%s%d", acquaint_socket, i),
15-
autostart = NA
16-
) &&
17-
i > 8L
18-
)
19-
break
20-
}
21-
)
9+
for (i in seq_len(1024L)) {
10+
if (
11+
nanonext::dial(
12+
sock,
13+
url = sprintf("%s%d", the$socket_url, i),
14+
autostart = NA,
15+
fail = "none"
16+
) &&
17+
i > 8L
18+
)
19+
break
20+
}
2221
pipes <- nanonext::read_monitor(monitor)
2322
res <- lapply(
2423
pipes,
25-
function(x) nanonext::recv_aio(sock, mode = "string")
24+
function(x) nanonext::recv_aio(sock, mode = "string", timeout = 5000L)
2625
)
2726
lapply(
2827
pipes,
@@ -47,14 +46,14 @@ list_r_sessions_tool <-
4746
)
4847
)
4948

50-
select_r_session <- function(i) {
49+
select_r_session <- function(session) {
5150
nanonext::reap(the$server_socket[["dialer"]][[1L]])
5251
attr(the$server_socket, "dialer") <- NULL
5352
nanonext::dial(
5453
the$server_socket,
55-
url = sprintf("%s%d", acquaint_socket, as.integer(i))
54+
url = sprintf("%s%d", the$socket_url, session)
5655
)
57-
paste0("Selected session ", i, " successfully.")
56+
sprintf("Selected session %d successfully.", session)
5857
}
5958

6059
select_r_session_tool <-
@@ -72,7 +71,7 @@ select_r_session_tool <-
7271
"Your choice of session will persist after the tool is called; only",
7372
"call this tool more than once if you need to switch between sessions."
7473
),
75-
i = ellmer::type_integer("The index of the R session to select.")
74+
session = ellmer::type_integer("The R session number to select.")
7675
)
7776

7877
get_acquaint_tools <- function() {

R/utils.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
the <- new_environment()
22

3-
acquaint_socket <- "ipc:///tmp/acquaint-socket"
4-
53
jsonrpc_response <- function(id, result = NULL, error = NULL) {
64
if (!xor(is.null(result), is.null(error))) {
75
warning("Either `result` or `error` must be provided, but not both.")

0 commit comments

Comments
 (0)