Skip to content

Commit

Permalink
Slight cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jcheng5 committed Dec 6, 2024
1 parent 2504ae3 commit c286dec
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions R/provider-gemini.R
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,10 @@ merge_optional <- function(merge_func) {
}
}

merge_by_spec <- function(...) {
merge_objects <- function(...) {
spec <- list(...)
function(left, right) {
# TODO: left and right should be named lists
stopifnot(all(nzchar(names(spec))))
stopifnot(is.list(left), is.list(right), all(nzchar(names(spec))))
mapply(names(spec), spec, FUN = function(key, value) {
value(left[[key]], right[[key]])
}, USE.NAMES = TRUE, SIMPLIFY = FALSE)
Expand All @@ -332,7 +331,7 @@ merge_indexed_list <- function(...) {
}
lst
}
# TODO: We shouldn't need to do this--why don't we see .index??cry
# TODO: We shouldn't need to do this--why don't we see .index??
left <- ensure_indices(left)
right <- ensure_indices(right)
# left and right are lists of objects with [["index"]]
Expand All @@ -347,7 +346,7 @@ merge_indexed_list <- function(...) {
} else if (is.null(right_item)) {
left_item
} else {
merge_by_spec(...)(left_item, right_item)
merge_objects(...)(left_item, right_item)
}
})
}
Expand All @@ -368,7 +367,7 @@ merge_parts <- function(...) {
c(left, right)
} else {
# Merge the last left and first right
result <- merge_by_spec(...)(last_left, first_right)
result <- merge_objects(...)(last_left, first_right)
# Drop NULL properties
result <- result[!vapply(result, is.null, logical(1))]
# Put everything back together
Expand All @@ -378,18 +377,19 @@ merge_parts <- function(...) {
}
}

merge_gemini_chunks <- merge_by_spec(
# Put it all together...
merge_gemini_chunks <- merge_objects(
candidates = merge_indexed_list(
index = merge_identical(),
content = merge_by_spec(
content = merge_objects(
role = merge_any_or_empty(),
parts = merge_parts(
text = merge_optional(merge_concatenate()),
executable_code = merge_optional(merge_by_spec(
executable_code = merge_optional(merge_objects(
language = merge_first(),
code = merge_concatenate()
)),
code_execution_result = merge_optional(merge_by_spec(
code_execution_result = merge_optional(merge_objects(
outcome = merge_last(),
output = merge_concatenate()
))
Expand Down

0 comments on commit c286dec

Please sign in to comment.