Skip to content

Commit c923b74

Browse files
Custom downloadable message for each API
1 parent 4a640b9 commit c923b74

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

built-in-ai/static/session.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@ const TRANSLATOR_DOCS_INSTRUCTIONS = "Please check the <a href='https://learn.mi
55

66
const ERR_LANGUAGEMODEL_API_NOT_DETECTED = `The LanguageModel API is not available. ${PROMPT_DOCS_INSTRUCTIONS}`;
77
const ERR_LANGUAGEMODEL_MODEL_NOT_AVAILABLE = `The LanguageModel API is enabled, but the model download hasn't started yet, maybe awaiting device capability check. ${PROMPT_DOCS_INSTRUCTIONS}`;
8+
const INFO_LANGUAGE_MODEL_DOWNLOADABLE = "The model will be downloaded the first time the API is used.";
89

910
const ERR_SUMMARIZER_API_NOT_DETECTED = `The Summarizer API is not available. ${WA_DOCS_INSTRUCTIONS}`;
1011
const ERR_SUMMARIZER_MODEL_NOT_AVAILABLE = `The Summarizer API is enabled, but the model download hasn't started yet, maybe awaiting device capability check. ${WA_DOCS_INSTRUCTIONS}`;
12+
const INFO_SUMMARIZER_MODEL_DOWNLOADABLE = "The model will be downloaded the first time the API is used.";
1113

1214
const ERR_WRITER_API_NOT_DETECTED = `The Writer API is not available. ${WA_DOCS_INSTRUCTIONS}`;
1315
const ERR_WRITER_MODEL_NOT_AVAILABLE = `The Writer API is enabled, but the model download hasn't started yet, maybe awaiting device capability check. ${WA_DOCS_INSTRUCTIONS}`;
16+
const INFO_WRITER_MODEL_DOWNLOADABLE = "The model will be downloaded the first time the API is used.";
1417

1518
const ERR_REWRITER_API_NOT_DETECTED = `The Rewriter API is not available. ${WA_DOCS_INSTRUCTIONS}`;
1619
const ERR_REWRITER_MODEL_NOT_AVAILABLE = `The Rewriter API is enabled, but the model download hasn't started yet, maybe awaiting device capability check. ${WA_DOCS_INSTRUCTIONS}`;
20+
const INFO_REWRITER_MODEL_DOWNLOADABLE = "The model will be downloaded the first time the API is used.";
1721

1822
const ERR_TRANSLATOR_API_NOT_DETECTED = `The Translator API is not available. ${WA_DOCS_INSTRUCTIONS}`;
1923
const ERR_TRANSLATOR_MODEL_NOT_AVAILABLE = `The Translator API is enabled, but the model download hasn't started yet, maybe awaiting device capability check. ${TRANSLATOR_DOCS_INSTRUCTIONS}`;
24+
const INFO_TRANSLATOR_MODEL_DOWNLOADABLE = "The model for a specified language pair will be downloaded the first time the API is used.";
2025

2126
const ERR_API_CAPABILITY_ERROR = "Cannot create the session now. API availability error: ";
2227
const ERR_FAILED_CREATING_MODEL = "Could not create the session. Error: ";
@@ -141,7 +146,7 @@ function getTranslatorAPI() {
141146
// You can call these functions when the page loads if you want to display the status
142147
// to the user early, so they know what to expect (e.g. if their browser supports the APIs).
143148
// These functions don't trigger the model download and do not create sessions.
144-
async function checkAPIAvailability(api, modelError, availabilityOptions) {
149+
async function checkAPIAvailability(api, modelError, availabilityOptions, modelDownloadableInfo) {
145150
const availability = await api.availability(availabilityOptions);
146151

147152
// The API is available, but the model is not.
@@ -159,7 +164,7 @@ async function checkAPIAvailability(api, modelError, availabilityOptions) {
159164
// Everything seems to be fine.
160165
let message = `On-device API and model ${availability}.`;
161166
if (availability === "downloadable") {
162-
message += " The model will be downloaded the first time the API is used.";
167+
message += ` ${modelDownloadableInfo}`;
163168
}
164169

165170
displaySessionMessage(message);
@@ -168,27 +173,52 @@ async function checkAPIAvailability(api, modelError, availabilityOptions) {
168173
}
169174

170175
async function checkLanguageModelAPIAvailability() {
171-
const availability = await checkAPIAvailability(getLanguageModelAPI(), ERR_LANGUAGEMODEL_MODEL_NOT_AVAILABLE);
176+
const availability = await checkAPIAvailability(
177+
getLanguageModelAPI(),
178+
ERR_LANGUAGEMODEL_MODEL_NOT_AVAILABLE,
179+
undefined,
180+
INFO_LANGUAGE_MODEL_DOWNLOADABLE
181+
);
172182
return availability;
173183
}
174184

175185
async function checkSummarizerAPIAvailability() {
176-
const availability = await checkAPIAvailability(getSummarizerAPI(), ERR_SUMMARIZER_MODEL_NOT_AVAILABLE);
186+
const availability = await checkAPIAvailability(
187+
getSummarizerAPI(),
188+
ERR_SUMMARIZER_MODEL_NOT_AVAILABLE,
189+
undefined,
190+
INFO_SUMMARIZER_MODEL_DOWNLOADABLE
191+
);
177192
return availability;
178193
}
179194

180195
async function checkWriterAPIAvailability() {
181-
const availability = await checkAPIAvailability(getWriterAPI(), ERR_WRITER_MODEL_NOT_AVAILABLE);
196+
const availability = await checkAPIAvailability(
197+
getWriterAPI(),
198+
ERR_WRITER_MODEL_NOT_AVAILABLE,
199+
undefined,
200+
INFO_WRITER_MODEL_DOWNLOADABLE
201+
);
182202
return availability;
183203
}
184204

185205
async function checkRewriterAPIAvailability() {
186-
const availability = await checkAPIAvailability(getRewriterAPI(), ERR_REWRITER_MODEL_NOT_AVAILABLE);
206+
const availability = await checkAPIAvailability(
207+
getRewriterAPI(),
208+
ERR_REWRITER_MODEL_NOT_AVAILABLE,
209+
undefined,
210+
INFO_REWRITER_MODEL_DOWNLOADABLE
211+
);
187212
return availability;
188213
}
189214

190215
async function checkTranslatorAPIAvailability(availabilityOptions) {
191-
const availability = await checkAPIAvailability(getTranslatorAPI(), ERR_TRANSLATOR_MODEL_NOT_AVAILABLE, availabilityOptions);
216+
const availability = await checkAPIAvailability(
217+
getTranslatorAPI(),
218+
ERR_TRANSLATOR_MODEL_NOT_AVAILABLE,
219+
availabilityOptions,
220+
INFO_TRANSLATOR_MODEL_DOWNLOADABLE
221+
);
192222
return availability;
193223
}
194224

0 commit comments

Comments
 (0)