Replies: 4 comments 8 replies
-
Even better if you could also receive optional export async function action({ request }) {
return eventStream(request.signal, async (send, controller) => {
await LongRunningTask.start(send)
controller.abort()
return () => {}
})
} which does this boilerplate for you: const controller = new AbortController()
request.signal.addEventListener('abort', () => controller.abort()) This will be useful for a lot of LLM-based applications. |
Beta Was this translation helpful? Give feedback.
-
I need the cleanup function returned by the callback you pass to If it was a promise, I would have to wait for the promise to resolve to get the cleanup function, at that point the connection may have already been closed by the browser. Also in your example, this: export async function action({ request }) {
return eventStream(request.signal, (send) => {
async function run() {
await LongRunningTask.start(send)
}
run()
return () => {}
})
} Is the same as doing: export async function action({ request }) {
return eventStream(request.signal, (send) => {
LongRunningTask.start(send)
return () => {}
})
} Without needing to add async/await, but remember your LongRunningTask should be abortable/cancellable in case the connection is closed, or should handle that using |
Beta Was this translation helpful? Give feedback.
-
I see, I'm imagining it's called in the In my case, only need return eventStream(request.signal, (send, close) => {
async function run() {
await Task.run(send)
close()
}
run()
return close
}) but then I'm getting
ChatGPT SDK here: https://github.com/openai/openai-node#streaming-responses Here's my code that's been successfully working so far: export async function action({ request }) {
// const params = ...
return eventStream(request.signal, (send, close) => {
async function run() {
await ChatGPT.run(send, params)
close()
}
run()
return () => {}
})
} Several points to note:
for await (const chunk of stream) {
const delta = chunk.choices[0]?.delta?.content || ''
send({ event: 'message', data: delta })
} Am I missing anything here? Otherwise I'm pretty satisfied with the current implementation, but just wanted to let you know that there's an area of improvements for this kind of use cases. |
Beta Was this translation helpful? Give feedback.
-
I sent a PR to add an alternative API to eventStream to accept sync functions #336 |
Beta Was this translation helpful? Give feedback.
-
Right now, eventStream takes a function like so:
But it would be helpful if it could optionally take async functions, and automatically invoke it. Now the code looks much cleaner.
Beta Was this translation helpful? Give feedback.
All reactions