Skip to content

Commit 1c29467

Browse files
committed
remove console logs, refine example
1 parent a4994dd commit 1c29467

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

Node/quickstarts/callable-functions-streaming/functions/index.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,34 @@ async function generateStream() {
3434
const sentence = "Hello from Cloud Functions for Firebase!";
3535

3636
for (const word of sentence.split(" ")) {
37-
await new Promise((resolve) => setTimeout(resolve, Math.random() * 500));
37+
const randomDelay = Math.floor(Math.random() * 500);
38+
await new Promise((resolve) => setTimeout(resolve, randomDelay));
3839
yield {text: () => " " + word};
3940
}
41+
42+
return {text: () => sentence};
4043
}
4144

42-
return {
43-
metadata: {
44-
acceptsStreaming: true},
45-
stream: mockAsyncIterable,
46-
};
45+
return mockAsyncIterable;
4746
}
4847

4948
exports.streamResponse = onCall(async (request, response) => {
50-
console.log("I was called!!!");
51-
console.log("data", request.data);
52-
const prompt = request.data.text || "hello";
49+
const prompt = request.data?.text || "hello world";
5350

54-
const {metadata, stream} = await generateStream(prompt);
51+
try {
52+
// Call a streaming API, like an LLM
53+
const stream = await generateStream(prompt);
5554

56-
for await (const chunk of stream()) {
57-
console.log(chunk);
58-
if (metadata.acceptsStreaming) {
59-
response.sendChunk(chunk.text());
55+
if (request.acceptsStreaming) {
56+
// Wait for each value of the returned Async Iterable
57+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncIterator
58+
for await (const chunk of stream()) {
59+
response.sendChunk(chunk.text());
60+
}
6061
}
62+
63+
return await stream.text();
64+
} catch (error) {
65+
throw new HttpsError("internal", error.message);
6166
}
62-
return true;
6367
});

Node/quickstarts/callable-functions-streaming/website/index.html

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
connectFunctionsEmulator,
1616
} from "https://www.gstatic.com/firebasejs/11.2.0/firebase-functions.js";
1717

18+
const callableButton = document.getElementById("btn-call-func");
19+
const resultElement = document.getElementById("p-result");
20+
21+
callableButton.onclick = handleClick;
22+
1823
// set your firebase config object
1924
const firebaseConfig = undefined;
2025
if (!firebaseConfig) {
@@ -25,21 +30,27 @@
2530
connectFunctionsEmulator(functions, "127.0.0.1", 5001);
2631
const streamResponse = httpsCallable(functions, "streamResponse");
2732

28-
const callableButton = document.getElementById("btn-call-func");
29-
const resultElement = document.getElementById("p-result");
30-
callableButton.onclick = async () => {
33+
async function handleClick() {
3134
// reset result
32-
resultElement.innerHTML = "";
35+
clearOutput();
3336

3437
const resp = await streamResponse.stream({
3538
text: "What is your favorite Firebase service and why?",
3639
});
3740

38-
// append text each time
39-
for await (const message of resp.stream) {
40-
resultElement.innerHTML += message;
41+
// add each new chunk to the output
42+
for await (const chunk of resp.stream) {
43+
appendToOutput(chunk);
4144
}
4245
};
46+
47+
function clearOutput() {
48+
resultElement.innerHTML = "";
49+
}
50+
51+
function appendToOutput(str) {
52+
resultElement.innerHTML += str;
53+
}
4354
</script>
4455
</body>
4556
</html>

0 commit comments

Comments
 (0)