Skip to content

Instantly share code, notes, and snippets.

@cowboyd
Created May 29, 2025 14:47
Show Gist options
  • Save cowboyd/2f230a3ee41dec9e04fe1893b29ee52e to your computer and use it in GitHub Desktop.
Save cowboyd/2f230a3ee41dec9e04fe1893b29ee52e to your computer and use it in GitHub Desktop.
streaming effection chatbot resource
import type { Result, Stream } from "effection"
import { Ok, Err } from "effection";
function chat(history: Array<Message>): Stream<string, Result<void>> {
return resource<{
channel: Signal<string, void>;
await: Operation<E.Either<Error, void>>;
}>(function* (provide) {
const signal = yield* useAbortSignal();
const channel = createSignal<string, Result<void>>();
const result = streamText({
system: prompt,
messages: history,
abortSignal: signal,
model: openai("gpt-4o-mini"),
onError({ error }) {
let e: Error;
if (error instanceof Error) {
e = error;
} else {
e = new Error("Uknown error");
e.cause = error;
}
channel.close(Err(e));
},
onFinish(_) {
channel.close(Ok();
},
});
result.consumeStream();
yield* spawn(function* () {
yield* call(async () => {
for await (const chunk of result.textStream) {
channel.send(chunk);
}
});
});
try {
yield* provide(yield* channel);
} finally {
channel.close(Ok());
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment