Skip to content

Commit

Permalink
some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ghgoodreau committed Dec 15, 2023
1 parent a001388 commit cb3252c
Showing 1 changed file with 4 additions and 15 deletions.
19 changes: 4 additions & 15 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ function App() {
if (!googleApiKey) {
throw new Error("Google API key is not set.");
}
// decodes weird escape characters. gotta fix this later.
// TODO: decodes weird escape characters. gotta fix this later.
const decodedApiKey = decodeURIComponent(googleApiKey);
const fixedApiKey = decodedApiKey.replace(/^"|"$/g, "");
const genAI = new GoogleGenerativeAI(fixedApiKey);
Expand All @@ -520,15 +520,13 @@ function App() {

const abortController = new AbortController();

// Handle the streams incrementally as they arrive.
for (let i = 0; i < responses; i++) {
const msg = parentNode.data.text;
const correspondingNodeId =
overrideExistingIfPossible && i < currentNodeChildren.length
? currentNodeChildren[i].id
: newNodes[newNodes.length - responses + i].id;

// Start the stream
const streamPromise = chat.sendMessageStream(msg);

streamPromise.then((stream) => {
Expand All @@ -537,19 +535,17 @@ function App() {
for await (const chunk of stream.stream) {
if (abortController.signal.aborted) break;
const chunkText = chunk.text();
text += chunkText; // Append the latest text chunk
text += chunkText;

// Update the node with the accumulated text
setNodes((newerNodes) => {
return modifyFluxNodeText(newerNodes, {
id: correspondingNodeId,
text: text, // Set the text to the accumulated content
streamId, // This will cause a throw if the streamId has changed.
text: text,
streamId,
});
});
}

// After the stream is done, update the edge to no longer be animated
setEdges((edges) =>
modifyFluxEdge(edges, {
source: parentNode.id,
Expand All @@ -558,7 +554,6 @@ function App() {
})
);

// Reset the stream id.
setNodes((nodes) =>
setFluxNodeStreamId(nodes, { id: correspondingNodeId, streamId: undefined })
);
Expand All @@ -582,8 +577,6 @@ function App() {
let newEdges = [...edges];

for (let i = 0; i < responses; i++) {
// Update the links between
// re-used nodes if necessary.
if (overrideExistingIfPossible && i < currentNodeChildren.length) {
const childId = currentNodeChildren[i].id;

Expand All @@ -596,11 +589,8 @@ function App() {
animated: true,
};
} else {
// The new nodes are added to the end of the array, so we need to
// subtract responses from and add i to length of the array to access.
const childId = newNodes[newNodes.length - responses + i].id;

// Otherwise, add a new edge.
newEdges.push(
newFluxEdge({
source: parentNode.id,
Expand All @@ -621,7 +611,6 @@ function App() {

// The completeNextWords function remains unchanged
const completeNextWords = () => {
// ... existing logic
takeSnapshot();

const temp = settings.temp;
Expand Down

0 comments on commit cb3252c

Please sign in to comment.