Refactor project: remove unused code, clean up logs, streamline error handling, update TypeScript configs, and enhance message streaming.

- Deployed
This commit is contained in:
geoffsee
2025-06-24 16:28:25 -04:00
parent 004ec580d3
commit 9698fc6f3b
19 changed files with 227 additions and 228 deletions

View File

@@ -1,21 +1,83 @@
import { types } from "mobx-state-tree";
import { types, flow } from "mobx-state-tree";
// Utility to pause execution inside a flow
const sleep = (ms: number) => new Promise<void>((res) => setTimeout(res, ms));
// Simple function to generate a unique ID
export const generateId = () => {
return Date.now().toString(36) + Math.random().toString(36).substring(2);
return Date.now().toString(36) + Math.random().toString(36).substring(2);
};
export default types
.model("Message", {
id: types.optional(types.identifier, generateId),
content: types.string,
role: types.enumeration(["user", "assistant"]),
})
.actions((self) => ({
setContent(newContent: string) {
self.content = newContent;
},
append(newContent: string) {
self.content += newContent;
},
}));
// Utility for efficient batched content updates
let batchedContent = "";
let batchUpdateTimeout: NodeJS.Timeout | null = null;
const BATCH_UPDATE_DELAY = 50; // ms
export const batchContentUpdate = (message: any, content: string) => {
if (!content) return;
// Add the content to the batch
batchedContent += content;
// If we already have a timeout scheduled, do nothing
if (batchUpdateTimeout) return;
// Schedule a timeout to apply the batched content
batchUpdateTimeout = setTimeout(() => {
if (message && batchedContent) {
message.append(batchedContent);
batchedContent = "";
}
batchUpdateTimeout = null;
}, BATCH_UPDATE_DELAY);
};
const Message = types
.model("Message", {
id: types.optional(types.identifier, generateId),
content: types.string,
role: types.enumeration(["user", "assistant"]),
})
// Runtimeonly flags that never persist or get serialized
.volatile(() => ({
/** Indicates that characters are still being streamed in */
isStreaming: false,
}))
.actions((self) => {
// Basic mutators ---------------------------------------------------------
const setContent = (newContent: string) => {
self.content = newContent;
};
const append = (newContent: string) => {
self.content += newContent;
};
/**
* Stream content into the message for a smooth “typing” effect.
* @param newContent The full text to stream in.
* @param chunkSize How many characters to reveal per tick (default 3).
* @param delay Delay (ms) between ticks (default 20ms).
*/
const streamContent = flow(function* (
newContent: string,
chunkSize = 3,
delay = 20
) {
self.isStreaming = true;
let pointer = 0;
// Reveal the content chunkbychunk
while (pointer < newContent.length) {
append(newContent.slice(pointer, pointer + chunkSize));
pointer += chunkSize;
yield sleep(delay);
}
self.isStreaming = false; // finished
});
return { setContent, append, streamContent };
});
export default Message;