Skip to content

Commit 7e6f353

Browse files
authored
Merge pull request #2 from wenqinI/detect-prompt-api
Detect Prompt API availability
2 parents 8051cd5 + 66226e3 commit 7e6f353

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

script.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const statusOutputDiv = document.getElementById('statusOutput');
77
const benchmarkResultsTableDiv = document.getElementById('benchmarkResultsTable');
88

99
let webGpuAvailable = false; // Flag to track WebGPU availability
10+
let promptApiAvailable = false; // Flag to track PromptAPI availability
1011
let session = null; // Session still tracked globally to manage closing if needed
1112

1213
// Flags for controlling button state based on user's choice
@@ -31,8 +32,8 @@ function updateStatus(message) {
3132
* @returns {Promise<boolean>} True if session was successfully created, false otherwise.
3233
*/
3334
async function ensureSessionCreated(roundInfo = "") {
34-
if (!webGpuAvailable) {
35-
updateStatus("WebGPU is not available. Cannot create session.");
35+
if (!promptApiAvailable) {
36+
updateStatus("Prompt API is not available. Cannot create session.");
3637
return false;
3738
}
3839

@@ -77,11 +78,16 @@ async function ensureSessionCreated(roundInfo = "") {
7778

7879

7980
function setButtonStates() {
80-
promptInput.disabled = isOperationActive || !webGpuAvailable;
81+
promptInput.disabled = isOperationActive || !promptApiAvailable;
8182

82-
if (!webGpuAvailable) {
83+
if (!promptApiAvailable) {
8384
generateResponseButton.disabled = true;
8485
benchmarkButton.disabled = true;
86+
87+
outputDiv.innerHTML = `Prompt API is NOT available in this browser.
88+
Refer to browser setup for installation <a href="https://github.com/wenqinI/built-in-ai-benchmark?tab=readme-ov-file#browser-setup" target="_blank">browser setup instructions</a>.
89+
`;
90+
8591
return;
8692
}
8793

@@ -106,10 +112,8 @@ async function detectWebGPU() {
106112
await logGpuInfo();
107113
} else {
108114
webGpuAvailable = false;
109-
updateStatus("WebGPU is not available in this browser. This app requires a browser with WebGPU support.");
110-
console.warn("WebGPU is not available in this browser.");
115+
console.warn("WebGPU is NOT available in this browser.");
111116
}
112-
setButtonStates();
113117
}
114118

115119
async function logGpuInfo() {
@@ -125,6 +129,23 @@ async function logGpuInfo() {
125129
}
126130
}
127131

132+
async function detectPromptAPI() {
133+
if ("LanguageModel" in window) {
134+
const available = await LanguageModel.availability();
135+
if (available == "available") {
136+
promptApiAvailable = true;
137+
}
138+
}
139+
}
140+
141+
async function detectAvailability() {
142+
await detectWebGPU()
143+
await detectPromptAPI()
144+
145+
setButtonStates();
146+
}
147+
148+
128149
// --- Core Inference Function (Reusable for both single generation and benchmark) ---
129150
async function performGeneration(userPrompt, isWarmup = false) {
130151
if (!userPrompt) {
@@ -437,4 +458,4 @@ generateResponseButton.addEventListener('click', generateResponse);
437458
benchmarkButton.addEventListener('click', runBenchmark);
438459

439460
// --- Initial Setup on Page Load ---
440-
window.addEventListener('load', detectWebGPU);
461+
window.addEventListener('load', detectAvailability);

0 commit comments

Comments
 (0)