12#include <agents-cpp/types.h>
17#if __has_include(<folly/experimental/coro/Task.h>)
18 #include <folly/experimental/coro/Task.h>
19 #include <folly/experimental/coro/AsyncGenerator.h>
20 #include <folly/experimental/coro/BlockingWait.h>
21 #include <folly/experimental/coro/AsyncScope.h>
22 #include <folly/io/async/ScopedEventBaseThread.h>
23 #include <folly/executors/CPUThreadPoolExecutor.h>
24 #include <folly/executors/GlobalExecutor.h>
25 #define HAS_FOLLY_CORO 1
26#elif __has_include(<folly/coro/Task.h>)
27 #include <folly/coro/Task.h>
28 #include <folly/coro/AsyncGenerator.h>
29 #include <folly/coro/AsyncScope.h>
30 #include <folly/coro/BlockingWait.h>
31 #include <folly/coro/Collect.h>
32 #include <folly/io/async/ScopedEventBaseThread.h>
33 #include <folly/executors/CPUThreadPoolExecutor.h>
34 #include <folly/executors/GlobalExecutor.h>
39 #define HAS_FOLLY_CORO 1
47 #define HAS_FOLLY_CORO 0
58using Task = folly::coro::Task<T>;
71 return folly::coro::blockingWait(std::move(task));
82 std::vector<T> results;
83 while (
auto item =
co_await generator.next()) {
84 results.push_back(std::move(*item));
97 return blockingWait(collectAllFromGenerator(std::move(generator)));
104 static folly::CPUThreadPoolExecutor executor(
105 std::thread::hardware_concurrency(),
106 std::make_shared<folly::NamedThreadFactory>(
"AgentExecutor"));
120 std::function<std::future<T>()> _func;
129 Task(std::function<std::future<T>()> func) : _func(std::move(func)) {}
147 std::function<std::vector<T>()> _func;
170 auto future = task.get_future();
182 return generator.collect();
197 template <
typename F>
199 std::thread(std::forward<F>(f)).detach();
A minimal AsyncGenerator implementation that doesn't rely on coroutines.
Definition coroutine_utils.h:142
AsyncGenerator(std::function< std::vector< T >()> func)
Constructor.
Definition coroutine_utils.h:153
std::vector< T > collect()
Collect the results.
Definition coroutine_utils.h:159
A minimal executor implementation.
Definition coroutine_utils.h:190
void add(F &&f)
Add a function to the executor.
Definition coroutine_utils.h:198
Provide a future-based fallback for Task.
Definition coroutine_utils.h:115
std::future< T > get_future()
Get the future.
Definition coroutine_utils.h:134
Task(std::function< std::future< T >()> func)
Constructor.
Definition coroutine_utils.h:129
Framework Namespace.
Definition agent.h:18
std::vector< T > collectAll(AsyncGenerator< T > &&generator)
Helper to collect all results from an AsyncGenerator.
Definition coroutine_utils.h:181
T blockingWait(Task< T > &&task)
Helper to run a task and get the result synchronously.
Definition coroutine_utils.h:169
Executor * getExecutor()
Get a global executor.
Definition coroutine_utils.h:207