forked from MichelNivard/gptstudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
render streaming message. close MichelNivard#85
- Loading branch information
1 parent
c518980
commit 3311a93
Showing
6 changed files
with
148 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#' Streaming message | ||
#' | ||
#' Places an invisible empty chat message that will hold a streaming message. | ||
#' It can be resetted dynamically inside a shiny app | ||
#' | ||
#' @import htmlwidgets | ||
#' | ||
#' @export | ||
streamingMessage <- function(ide_colors = get_ide_theme_info(), width = NULL, height = NULL, elementId = NULL) { | ||
|
||
message <- list(role = "assistant", content = "") | ||
|
||
# forward options using x | ||
x = list( | ||
message = style_chat_message(message, ide_colors = ide_colors) %>% as.character() | ||
) | ||
|
||
# create widget | ||
htmlwidgets::createWidget( | ||
name = 'streamingMessage', | ||
x, | ||
width = width, | ||
height = height, | ||
package = 'gptstudio', | ||
elementId = elementId | ||
) | ||
} | ||
|
||
#' Shiny bindings for streamingMessage | ||
#' | ||
#' Output and render functions for using streamingMessage within Shiny | ||
#' applications and interactive Rmd documents. | ||
#' | ||
#' @param outputId output variable to read from | ||
#' @param width,height Must be a valid CSS unit (like \code{'100\%'}, | ||
#' \code{'400px'}, \code{'auto'}) or a number, which will be coerced to a | ||
#' string and have \code{'px'} appended. | ||
#' @param expr An expression that generates a streamingMessage | ||
#' @param env The environment in which to evaluate \code{expr}. | ||
#' @param quoted Is \code{expr} a quoted expression (with \code{quote()})? This | ||
#' is useful if you want to save an expression in a variable. | ||
#' | ||
#' @name streamingMessage-shiny | ||
#' | ||
#' @export | ||
streamingMessageOutput <- function(outputId, width = '100%', height = NULL){ | ||
htmlwidgets::shinyWidgetOutput(outputId, 'streamingMessage', width, height, package = 'gptstudio') | ||
} | ||
|
||
#' @rdname streamingMessage-shiny | ||
#' @export | ||
renderStreamingMessage <- function(expr, env = parent.frame(), quoted = FALSE) { | ||
if (!quoted) { expr <- substitute(expr) } # force quoted | ||
htmlwidgets::shinyRenderWidget(expr, streamingMessageOutput, env, quoted = TRUE) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
HTMLWidgets.widget({ | ||
|
||
name: 'streamingMessage', | ||
|
||
type: 'output', | ||
|
||
factory: function(el, width, height) { | ||
|
||
// TODO: define shared variables for this instance | ||
|
||
return { | ||
|
||
renderValue: function(x) { | ||
|
||
// TODO: code to render the widget, e.g. | ||
el.innerHTML = x.message; | ||
el.classList.add("d-none"); // to start hidden | ||
el.classList.add("streaming-message"); // to be captured in a message handler | ||
|
||
}, | ||
|
||
resize: function(width, height) { | ||
|
||
// TODO: code to re-render the widget with a new size | ||
|
||
} | ||
|
||
}; | ||
} | ||
}); | ||
|
||
// This is independent from the HTMLwidget code. | ||
// It will only run inside projects with the shiny JS bindings (aka shiny apps). | ||
Shiny.addCustomMessageHandler( | ||
type = 'render-stream', function(message) { | ||
const $el = $('.streaming-message') | ||
$el.removeClass('d-none') | ||
|
||
const $messageWrapper = $el.find('.message-wrapper') | ||
$messageWrapper.html($.parseHTML(message)) | ||
|
||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# (uncomment to add a dependency) | ||
# dependencies: | ||
# - name: | ||
# version: | ||
# src: | ||
# script: | ||
# stylesheet: |