echoChat.ts 764 B

1234567891011121314151617181920212223242526
  1. import { Message } from "./messages";
  2. export async function getEchoChatResponseStream(messages: Message[]) {
  3. const stream = new ReadableStream({
  4. async start(controller: ReadableStreamDefaultController) {
  5. try {
  6. let lastMessage = messages[messages.length - 1].content;
  7. const lastChar = lastMessage.length > 0 ? lastMessage[lastMessage.length - 1] : ' ';
  8. if (lastChar !== '.' && lastChar !== '?' && lastChar !== '!') {
  9. lastMessage += ".";
  10. }
  11. lastMessage.split(' ').map((word) => word + ' ').forEach((word) => {
  12. controller.enqueue(word);
  13. });
  14. } catch (error) {
  15. controller.error(error);
  16. } finally {
  17. controller.close();
  18. }
  19. },
  20. });
  21. return stream;
  22. }