Title: Inline Asynchronous Generator Results into Documents
Version: 0.2.0
Description: Given a 'coro' asynchronous generator instance that produces text, write that text into a document selection in 'RStudio' and 'Positron'. This is particularly helpful for streaming large language model responses into the user's editor.
License: MIT + file LICENSE
Config/testthat/edition: 3
Encoding: UTF-8
RoxygenNote: 7.3.2
URL: https://github.com/simonpcouch/streamy, https://simonpcouch.github.io/streamy/
BugReports: https://github.com/simonpcouch/streamy/issues
Imports: cli (≥ 3.6.3), coro (≥ 1.1.0), rlang, rstudioapi (≥ 0.17.1), withr
Suggests: ellmer, knitr, rmarkdown, testthat (≥ 3.0.0)
NeedsCompilation: no
Packaged: 2025-05-21 19:33:07 UTC; simoncouch
Author: Simon Couch [aut, cre], Posit Software, PBC [cph, fnd]
Maintainer: Simon Couch <simon.couch@posit.co>
Repository: CRAN
Date/Publication: 2025-05-21 19:50:02 UTC

streamy: Inline Asynchronous Generator Results into Documents

Description

Given a 'coro' asynchronous generator instance that produces text, write that text into a document selection in 'RStudio' and 'Positron'. This is particularly helpful for streaming large language model responses into the user's editor.

Author(s)

Maintainer: Simon Couch simon.couch@posit.co

Other contributors:

See Also

Useful links:


Stream generator results into a document

Description

Given an asychronous generator that produces text, this function iteratively polls that generator and inlines its results into the currently open RStudio or Positron document. This is particularly useful for streaming results from large language models.

Usage

stream(
  generator,
  context = active_document_context(),
  interface = c("prefix", "replace", "suffix")
)

Arguments

generator

A coro::generator() function.

context

Optional. An RStudio document context.

interface

One of "prefix", "replace", or "suffix", describing how to the active selection will be interfaced with. Defaults to "replace".

Details

This function is generally not intended for interactive usage. See the gander, chores, and ensure package, which this package powers.

Value

The streamed result, invisibly; called for its side effect, modifying the context of the current selection with results from the generator.

Examples

## Not run: 
if (rlang::is_installed("ellmer") &&
    !identical("ANTHROPIC_API_KEY", "") &&
    rstudioapi::isAvailable()
 ) {
  library(ellmer)

  gen <- chat_claude()$stream("hey there!")

  stream(gen, interface = "suffix")
}

## End(Not run)