Created
May 29, 2025 14:47
-
-
Save cowboyd/2f230a3ee41dec9e04fe1893b29ee52e to your computer and use it in GitHub Desktop.
streaming effection chatbot resource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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