Agents 0.0.2
Edge AI Agents SDK
Loading...
Searching...
No Matches
coroutine_utils.h
1
10#pragma once
11
12#include <agents-cpp/types.h>
13#include <memory>
14#include <vector>
15
16// Try to include from both possible locations
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
40#else
41 #include <functional>
42 #include <future>
47 #define HAS_FOLLY_CORO 0
48#endif
49
50namespace agents {
51
52#if HAS_FOLLY_CORO
57template <typename T>
58using Task = folly::coro::Task<T>;
59
60template <typename T>
61using AsyncGenerator = folly::coro::AsyncGenerator<T>;
62
69template <typename T>
70T blockingWait(Task<T>&& task) {
71 return folly::coro::blockingWait(std::move(task));
72}
73
80template <typename T>
81Task<std::vector<T>> collectAllFromGenerator(AsyncGenerator<T>&& generator) {
82 std::vector<T> results;
83 while (auto item = co_await generator.next()) {
84 results.push_back(std::move(*item));
85 }
86 co_return results;
87}
88
95template <typename T>
96std::vector<T> collectAll(AsyncGenerator<T>&& generator) {
97 return blockingWait(collectAllFromGenerator(std::move(generator)));
98}
99
103inline folly::Executor* getExecutor() {
104 static folly::CPUThreadPoolExecutor executor(
105 std::thread::hardware_concurrency(),
106 std::make_shared<folly::NamedThreadFactory>("AgentExecutor"));
107 return &executor;
108}
109#else
114template <typename T>
115class Task {
116private:
120 std::function<std::future<T>()> _func;
125public:
129 Task(std::function<std::future<T>()> func) : _func(std::move(func)) {}
134 std::future<T> get_future() { return _func(); }
135};
136
141template <typename T>
143private:
147 std::function<std::vector<T>()> _func;
148public:
153 AsyncGenerator(std::function<std::vector<T>()> func) : _func(std::move(func)) {}
154
159 std::vector<T> collect() { return _func(); }
160};
161
168template <typename T>
170 auto future = task.get_future();
171 return future.get();
172}
173
180template <typename T>
181std::vector<T> collectAll(AsyncGenerator<T>&& generator) {
182 return generator.collect();
183}
184
190class Executor {
191public:
197 template <typename F>
198 void add(F&& f) {
199 std::thread(std::forward<F>(f)).detach();
200 }
201};
202
208 static Executor executor;
209 return &executor;
210}
211#endif /* HAS_FOLLY_CORO */
212
213} // namespace agents
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