1. Introduction
For now, see the explainer.
2. Shared AI APIs and infrastructure
partial interface WindowOrWorkerGlobalScope { [Replaceable ,SecureContext ]readonly attribute AI ai ; }; [Exposed =(Window ,Worker ),SecureContext ]interface {}; [
AI Exposed =(Window ,Worker ),SecureContext ]interface :
AICreateMonitor EventTarget {attribute EventHandler ondownloadprogress ; };callback =
AICreateMonitorCallback undefined (AICreateMonitor );
monitor enum {
AICapabilityAvailability ,
"readily" ,
"after-download" };
"no"
Each WindowOrWorkerGlobalScope
has an AI namespace, an AI
object. Upon creation of the WindowOrWorkerGlobalScope
object, its AI namespace must be set to a new AI
object created in the WindowOrWorkerGlobalScope
object’s relevant realm.
The ai
getter steps are to return this's AI namespace.
Tasks queued by this specification use the AI task source.
The following are the event handlers (and their corresponding event handler event types) that must be supported, as event handler IDL attributes, by all AICreateMonitor
objects:
Event handler | Event handler event type |
---|---|
ondownloadprogress
| downloadprogress
|
3. The summarizer API
partial interface AI {readonly attribute AISummarizerFactory summarizer ; }; [Exposed =(Window ,Worker ),SecureContext ]interface {
AISummarizerFactory Promise <AISummarizer >create (optional AISummarizerCreateOptions = {});
options Promise <AISummarizerCapabilities >capabilities (); }; [Exposed =(Window ,Worker ),SecureContext ]interface {
AISummarizer Promise <DOMString >summarize (DOMString ,
input optional AISummarizerSummarizeOptions = {} );
options ReadableStream summarizeStreaming (DOMString ,
input optional AISummarizerSummarizeOptions = {} );
options readonly attribute DOMString sharedContext ;readonly attribute AISummarizerType type ;readonly attribute AISummarizerFormat format ;readonly attribute AISummarizerLength length ;undefined destroy (); }; [Exposed =(Window ,Worker ),SecureContext ]interface {
AISummarizerCapabilities readonly attribute AICapabilityAvailability available ;AICapabilityAvailability createOptionsAvailable (optional AISummarizerCreateCoreOptions = {} );
options AICapabilityAvailability languageAvailable (DOMString ); };
languageTag dictionary {
AISummarizerCreateCoreOptions AISummarizerType = "key-points";
type AISummarizerFormat = "markdown";
format AISummarizerLength = "short"; };
length dictionary :
AISummarizerCreateOptions AISummarizerCreateCoreOptions {AbortSignal ;
signal AICreateMonitorCallback ;
monitor DOMString ; };
sharedContext dictionary {
AISummarizerSummarizeOptions AbortSignal ;
signal DOMString ; };
context enum {
AISummarizerType "tl;dr" ,"teaser" ,"key-points" ,"headline" };enum {
AISummarizerFormat "plain-text" ,"markdown" };enum {
AISummarizerLength "short" ,"medium" ,"long" };
Each AI
has an summarizer factory, an AISummarizerFactory
object. Upon creation of the AI
object, its summarizer factory must be set to a new AISummarizerFactory
object created in the AI
object’s relevant realm.
The summarizer
getter steps are to return this's summarizer factory.
3.1. Creation
create(options)
method steps are:
-
If this's relevant global object is a
Window
whose associated Document is not fully active, then return a promise rejected with an "InvalidStateError
"DOMException
. -
If options["
signal
"] exists and is aborted, then return a promise rejected with options["signal
"]'s abort reason. -
Let fireProgressEvent be an algorithm taking two arguments that does nothing.
-
If options["
monitor
"] exists, then:-
Let monitor be a new
AICreateMonitor
created in this's relevant realm. -
Invoke options["
monitor
"] with « monitor » and "rethrow
".If an exception e is thrown, return a promise rejected with e.
-
Set fireProgressEvent to an algorithm taking arguments loaded and total, which performs the following steps:
-
Assert: this algorithm is running in parallel.
-
Queue a global task on the AI task source given this's relevant global object to perform the following steps:
-
Fire an event named
downloadprogress
at monitor, usingProgressEvent
, with theloaded
attribute initialized to loaded, thetotal
attribute initialized to total, and thelengthComputable
attribute initialized to true.
-
-
-
-
Let abortedDuringDownload be false.
This variable will be written to from the event loop, but read from in parallel.
-
If options["
signal
"] exists, then add the following abort steps to options["signal
"]:-
Set abortedDuringDownload to true.
-
-
Let promise be a new promise created in this's relevant realm.
-
-
Let availability be the current summarizer create options availability given options["
type
"], options["format
"], and options["length
"]. -
Switch on availability:
- null
-
-
Reject promise with an "
UnknownError
"DOMException
. -
Abort these steps.
-
- "
no
" -
-
Reject promise with a "
NotSupportedError
"DOMException
. -
Abort these steps.
-
- "
readily
" -
-
If initializing the summarization model given promise and options returns false, then abort these steps.
-
Let totalBytes be the total size of the previously-downloaded summarization capabilities, in bytes.
-
Assert: totalBytes is greater than 0.
-
Perform fireProgressEvent given 0 and totalBytes.
-
Perform fireProgressEvent given totalBytes and totalBytes.
-
Finalize summarizer creation given promise and options.
-
- "
after-download
" -
-
Initiate the download process for everything the user agent needs to summarize text according to options["
type
"], options["format
"], or options["length
"]. -
Run the following steps, but abort when abortedDuringDownload becomes true:
-
Wait for the total number of bytes to be downloaded to become determined, and let that number be totalBytes.
-
Let lastProgressTime be the monotonic clock's unsafe current time.
-
Perform fireProgressEvent given 0 and totalBytes.
-
While true:
-
If one or more bytes have been downloaded, then:
-
If the monotonic clock's unsafe current time minus lastProgressTime is greater than 50 ms, then:
-
Let bytesSoFar be the number of bytes downloaded so far.
-
Assert: bytesSoFar is greater than 0 and less than or equal to totalBytes.
-
Perform fireProgressEvent given bytesSoFar and totalBytes.
-
If bytesSoFar equals totalBytes, then break.
-
Set lastProgressTime to the monotonic clock's unsafe current time.
-
-
-
Otherwise, if downloading has failed and cannot continue, then:
-
Queue a global task on the AI task source given this's relevant global object to reject promise with a "
NetworkError
"DOMException
. -
Abort these steps.
-
-
-
-
If aborted, then:
-
Queue a global task on the AI task source given this's relevant global object to perform the following steps:
-
Abort these steps.
-
-
If initializing the summarization model given promise and options returns false, then abort these steps.
-
Finalize summarizer creation given promise and options.
-
-
-
Return promise.
Promise
promise and an AISummarizerCreateOptions
options:
-
Assert: these steps are running in parallel.
-
Perform any necessary initialization operations for the AI model backing the user agent's summarization capabilities.
This could include loading the model into memory, loading options["
sharedContext
"] into the model’s context window, or loading any fine-tunings necessary to support options["type
"], options["format
"], or options["length
"]. -
If initialization failed for any reason, then:
-
Queue a global task on the AI task source given promise’s relevant global object to reject promise with an "
OperationError
"DOMException
. -
Return false.
-
-
Return true.
Promise
promise and an AISummarizerCreateOptions
options:
-
Assert: these steps are running in parallel.
-
Assert: the current summarizer create options availability for options["
type
"], options["format
"], and options["length
"] is "readily
". -
Queue a global task on the AI task source given promise’s relevant global object to perform the following steps:
-
If options["
signal
"] exists and is aborted, then:-
Reject promise with options["
signal
"]'s abort reason. -
Abort these steps.
This check is necessary in case any code running on the event loop caused the
AbortSignal
to become aborted before this task ran. -
-
Let summarizer be a new
AISummarizer
object, created in promise’s relevant realm, with- shared context
-
options["
sharedContext
"] if it exists; otherwise null - summary type
-
options["
type
"] - summary format
-
options["
format
"] - summary length
-
options["
length
"]
-
If options["
signal
"] exists, then add the following abort steps to options["signal
"]:-
Destroy summarizer with options["
signal
"]'s abort reason.
-
-
Resolve promise with summarizer.
-
3.2. Capabilities
capabilities()
method steps are:
-
If this's relevant global object is a
Window
whose associated Document is not fully active, then return a promise rejected with an "InvalidStateError
"DOMException
. -
Let promise be a new promise created in this's relevant realm.
-
-
Let availableCreateOptions be a new map from tuples of (
AISummarizerType
,AISummarizerFormat
,AISummarizerLength
) values toAICapabilityAvailability
values, initially empty. -
For each type of
AISummarizerType
's enumeration values:-
For each format of
AISummarizerFormat
's enumeration values:-
For each length of
AISummarizerLength
's enumeration values:-
Set availableCreateOptions[(type, format, length)] to the current summarizer create options availability given type, format, and length.
-
-
-
-
Let « readilyAvailableLanguages, afterDownloadAvailableLanguages » be the current summarizer language availabilities.
-
If readilyAvailableLanguages is null, afterDownloadAvailableLanguages is null, or availableCreateOptions’s values contains null, then queue a global task on the AI task source given this to perform the following steps:
-
Reject promise with an "
UnknownError
"DOMException
.
-
-
Otherwise, queue a global task on the AI task source given this to perform the following steps:
-
Let capabilitiesObject be a new
AISummarizerCapabilities
object, created in this's relevant realm, with- available create options
-
availableCreateOptions
- readily available languages
-
readilyAvailableLanguages
- after-download available languages
-
afterDownloadAvailableLanguages
-
Resolve promise with capabilitiesObject.
-
-
Every AISummarizerCapabilities
has an available create options, a map from tuples of (AISummarizerType
, AISummarizerFormat
, AISummarizerLength
) values to AICapabilityAvailability
values, set during creation.
Every AISummarizerCapabilities
has an readily available languages, a set of strings representing BCP 47 language tags, set during creation.
Every AISummarizerCapabilities
has an after-download available languages, a set of strings representing BCP 47 language tags, set during creation.
available
getter steps are:
-
If this's readily available languages and after-download available languages are empty, then return "
no
". -
If this's all of this's available create options values are "
no
", then return "no
". -
If this's readily available languages are empty, then return "
after-download
". -
Return "
readily
".
createOptionsAvailable(options)
method steps are:
-
Return this's available create options[(options["
type
"], options["format
"], options["length
"])].
languageAvailable(languageTag)
method steps are:
-
If IsStructurallyValidLanguageTag(languageTag) is false, then throw a
TypeError
. -
Set languageTag to CanonicalizeUnicodeLocaleId(languageTag).
-
Let bestReadilyAvailableMatch be LookupMatchingLocaleByBestFit(this's readily available languages, languageTag).
-
If bestReadilyAvailableMatch is not undefined, then return "
readily
".bestReadilyAvailableMatch.[[locale]] contains the actual language tag from this's readily available languages, which might be different from languageTag.
-
Let bestAfterDownloadAvailableMatch be LookupMatchingLocaleByBestFit(this's after-download available languages, languageTag).
-
If bestAfterDownloadAvailableMatch is not undefined, then return "
after-download
".bestAfterDownloadAvailableMatch.[[locale]] contains the actual language tag from this's after-download available languages, which might be different from languageTag.
-
Return "
no
".
AISummarizerType
type, AISummarizerFormat
format, and an AISummarizerLength
length, is given by the following steps. They return an AICapabilityAvailability
value or null.
-
Assert: this algorithm is running in parallel.
-
If the user agent supports summarizing text into the type of summary described by type, in the format described by format, and with the length guidance given by length without performing any downloading operations, then return "
readily
". -
If the user agent believes it can summarize text according to type, format, and length, but only after performing a download (e.g., of an AI model or fine-tuning), then return "
after-download
". -
If there is some error attempting to determine whether the user agent supports summarizing text, which the user agent believes to be transient (such that re-querying the current summarizer create options availability could stop producing such an error), then return null.
-
Otherwise, return "
no
".
-
Assert: this algorithm is running in parallel.
-
If there is some error attempting to determine whether the user agent supports summarizing text, which the user agent believes to be transient (such that re-querying the current summarizer language availabilities could stop producing such an error), then return « null, null ».
-
Let readilyAvailableLanguages and afterDownloadAvailableLanguages be empty sets.
-
For each human language languageTag, represented as a Unicode canonicalized locale identifier, for which the user agent supports summarizing text written in that language, without performing any downloading operations:
-
Append languageTag to readilyAvailableLanguages.
-
-
For each human language languageTag, represented as a Unicode canonicalized locale identifier, for which the user agent believes it can summarize text written in that language, but only after performing a download (e.g., of an AI model or fine-tuning):
-
If the union of readilyAvailableLanguages and afterDownloadAvailableLanguages does not meet the language tag set completeness rules, then:
-
Let missingLanguageTags be the set of missing language tags necessary to meet the language tag set completeness rules.
-
For each languageTag of missingLanguageTags:
-
Append languageTag to either readilyAvailableLanguages or afterDownloadAvailableLanguages. Which of the two sets to append to is implementation-defined, and should be guided by considerations similar to that of LookupMatchingLocaleByBestFit in terms of keeping "best fallback languages" together.
-
-
-
Return « readilyAvailableLanguages, afterDownloadAvailableLanguages ».
This definition is intended to align with that of [[AvailableLocales]] in ECMAScript Internationalization API Specification. [ECMA-402]
de-DE
" text, it will also count as supporting "de
" text.
The converse direction is supported not by the language tag set completeness rules, but instead by the use of LookupMatchingLocaleByBestFit, which ensures that if an implementation supports summarizing "de
" text, it also counts as supporting summarization of "de-CH
", "de-Latn-CH
", etc.
One way this could be implemented would be for current summarizer language availabilities to return that « "zh-Hant
" » is readily available, and « "zh
", "zh-Hans
" » is available after download. This return value conforms to the requirements of the language tag set completeness rules, in ensuring that "zh
" is present. Per the "should"-level guidance, the implementation has determined that "zh
" belongs in the list of after-download available languages, with "zh-Hans
", instead of in the list of readily available languages, with "zh-Hant
".
Combined with the use of LookupMatchingLocaleByBestFit, this means languageAvailable()
will give the following answers:
c. languageAvailable( "zh" ) === "after-download" ; c. languageAvailable( "zh-Hant" ) === "readily" ; c. languageAvailable( "zh-Hans" ) === "after-download" ; c. languageAvailable( "zh-TW" ) === "readily" ; // zh-TW will best-fit to zh-Hant c. languageAvailable( "zh-HK" ) === "readily" ; // zh-HK will best-fit to zh-Hant c. languageAvailable( "zh-CN" ) === "after-download" ; // zh-CN will best-fit to zh-Hans c. languageAvailable( "zh-BR" ) === "after-download" ; // zh-BR will best-fit to zh c. languageAvailable( "zh-Kana" ) === "after-download" ; // zh-Kana will best-fit to zh
3.3. The AISummarizer
class
Every AISummarizer
has a shared context, a string-or-null, set during creation.
Every AISummarizer
has a summary type, an AISummarizerType
, set during creation.
Every AISummarizer
has a summary format, an AISummarizerFormat
, set during creation.
Every AISummarizer
has a summary length, an AISummarizerLength
, set during creation.
Every AISummarizer
has a destruction reason, a JavaScript value, initially undefined.
Every AISummarizer
has a destroyed boolean, initially false.
This value is separate from the destruction reason so that it can be read from in parallel during the summarization process.
The sharedContext
getter steps are to return this's shared context.
The type
getter steps are to return this's summary type.
The format
getter steps are to return this's summary format.
The length
getter steps are to return this's summary length.
summarize(input, options)
method steps are:
-
If this's relevant global object is a
Window
whose associated Document is not fully active, then return a promise rejected with an "InvalidStateError
"DOMException
. -
If this's destroyed is true, then return a promise rejected with this's destruction reason.
-
If options["
signal
"] exists and is aborted, then return a promise rejected with options["signal
"]'s abort reason. -
Let abortedDuringSummarization be false.
This variable will be written to from the event loop, but read from in parallel.
-
If options["
signal
"] exists, then add the following abort steps to options["signal
"]:-
Set abortedDuringSummarization to true.
-
-
Let promise be a new promise created in this's relevant realm.
-
Let context be options["
context
"] if it exists; otherwise null. -
-
Let summary be the empty string.
-
Let chunkProduced be the following steps given a string chunk:
-
Queue a global task on the AI task source given this's relevant global object to perform the following steps:
-
If abortedDuringSummarization is true, then:
-
Reject promise with options["
signal
"]'s abort reason. -
Abort these steps.
-
-
If this's destroyed is true, then:
-
Reject promise with this's destruction reason.
-
Abort these steps.
-
-
Append chunk to summary.
-
-
-
Let done be the following steps:
-
Queue a global task on the AI task source given this's relevant global object to perform the following steps:
-
If abortedDuringSummarization is true, then:
-
Reject promise with options["
signal
"]'s abort reason. -
Abort these steps.
-
-
If this's destroyed is true, then:
-
Reject promise with this's destruction reason.
-
Abort these steps.
-
-
Resolve promise with summary.
-
-
-
Let error be the following steps given summarization error information errorInfo:
-
Queue a global task on the AI task source given this's relevant global object to perform the following steps:
-
If abortedDuringSummarization is true, then:
-
Reject promise with options["
signal
"]'s abort reason. -
Abort these steps.
-
-
Let exception be the result of creating a
DOMException
with name given by errorInfo’s error name, using errorInfo’s error information to populate the message appropriately. -
Reject promise with exception.
-
-
-
Let stopProducing be the following steps:
-
Return abortedDuringSummarization.
-
-
Summarize input given this's shared context, context, this's summary type, this's summary format, this's summary length, chunkProduced, done, error, and stopProducing.
-
-
Return promise.
summarizeStreaming(input, options)
method steps are:
-
If this's relevant global object is a
Window
whose associated Document is not fully active, then return a promise rejected with an "InvalidStateError
"DOMException
. -
If this's destroyed is true, then return a promise rejected with this's destruction reason.
-
If options["
signal
"] exists and is aborted, then return a promise rejected with options["signal
"]'s abort reason. -
Let abortedDuringSummarization be false.
This variable tracks web developer aborts via the options["
signal
"]AbortSignal
, which are surfaced as errors. It will be written to from the event loop, but sometimes read from in parallel. -
If options["
signal
"] exists, then add the following abort steps to options["signal
"]:-
Set abortedDuringSummarization to true.
-
-
Let stream be a new
ReadableStream
created in this's relevant realm. -
Let canceledDuringSummarization be false.
This variable tracks web developer stream cancelations via
stream.cancel()
, which are not surfaced as errors. It will be written to from the event loop, but sometimes read from in parallel. -
Set up stream with cancelAlgorithm set to the following steps (ignoring the reason argument):
-
Set canceledDuringSummarization to true.
-
-
Let context be options["
context
"] if it exists; otherwise null. -
-
Let chunkProduced be the following steps given a string chunk:
-
Queue a global task on the AI task source given this's relevant global object to perform the following steps:
-
If abortedDuringSummarization is true, then:
-
Error stream with options["
signal
"]'s abort reason. -
Abort these steps.
-
-
If this's destroyed is true, then:
-
Error stream with this's destruction reason.
-
Abort these steps.
-
-
Enqueue chunk into stream.
-
-
-
Let done be the following steps:
-
Queue a global task on the AI task source given this's relevant global object to perform the following steps:
-
If abortedDuringSummarization is true, then:
-
Error stream with options["
signal
"]'s abort reason. -
Abort these steps.
-
-
If this's destroyed is true, then:
-
Error stream with this's destruction reason.
-
Abort these steps.
-
-
Close stream.
-
-
-
Let error be the following steps given summarization error information errorInfo:
-
Queue a global task on the AI task source given this's relevant global object to perform the following steps:
-
If abortedDuringSummarization is true, then:
-
Error stream with options["
signal
"]'s abort reason. -
Abort these steps.
-
-
If this's destroyed is true, then:
-
Error stream with this's destruction reason.
-
Abort these steps.
-
-
Let exception be the result of creating a
DOMException
with name given by errorInfo’s error name, using errorInfo’s error information to populate the message appropriately. -
Error stream with exception.
-
-
-
Let stopProducing be the following steps:
-
Summarize input given this's shared context, context, this's summary type, this's summary format, this's summary length, chunkProduced, done, error, and stopProducing.
-
-
Return stream.
AISummarizerType
type, an AISummarizerFormat
format, an AISummarizerLength
length, an algorithm chunkProduced that takes a string and returns nothing, an algorithm done that takes no arguments and returns nothing, an algorithm error that takes summarization error information and returns nothing, and an algorithm stopProducing that takes no arguments and returns a boolean:
-
Assert: this algorithm is running in parallel.
-
Assert: the current summarizer create options availability given type, format, and length is "
readily
".Otherwise, the
AISummarizer
object would not have been created. -
In an implementation-defined manner, subject to the following guidelines, begin the processs of summarizing input into a string.
If they are non-null, sharedContext and context should be used to aid in the summarization by providing context on how the web developer wishes the input to be summarized.
The summarization should conform to the guidance given by type, format, and length, in the definitions of each of their enumeration values.
-
While true:
-
Wait for the next chunk of summarization data to be produced, for the summarization process to finish, or for the result of calling stopProducing to become true.
-
If such a chunk is successfully produced:
-
Let it be represented as a string chunk.
-
Perform chunkProduced given chunk.
-
-
Otherwise, if the summarization process has finished:
-
Perform done.
-
-
Otherwise, if stopProducing returns true, then break.
The caller will handle signaling cancelation or aborting as necessary.
-
Otherwise, if an error occurred during summarization:
-
Let the error be represented as summarization error information errorInfo according to the guidance in § 3.5 Errors.
-
Perform error given errorInfo.
-
-
The destroy()
method steps are to destroy this given a new "AbortError
" DOMException
.
AISummarizer
summarizer, given a JavaScript value reason:
-
Set summarizer’s destroyed to true.
-
Set summarizer’s destruction reason to reason.
-
The user agent should release any resources associated with summarizer, such as AI models loaded during initialize the summarization model, as long as those resources are not needed for other ongoing operations.
3.4. Options
The summarize algorithm’s details are implementation-defined, as they are expected to be powered by an AI model. However, it is intended to be controllable by the web developer through the AISummarizerType
, AISummarizerFormat
, and AISummarizerLength
enumerations.
This section gives normative guidance on how the implementation of summarize should use each enumeration value to guide the summarization process.
Value | Meaning |
---|---|
"tl;dr "
|
The summary should be short and to the point, providing a quick overview of the input, suitable for a busy reader. |
"teaser "
|
The summary should focus on the most interesting or intriguing parts of the input, designed to draw the reader in to read more. |
"key-points "
|
The summary should extract the most important points from the input, presented as a bulleted list. |
"headline "
|
The summary should effectively contain the main point of the input in a single sentence, in the format of an article headline. |
Value | Meaning |
---|---|
"short "
|
The guidance is dependent on the value of
|
"medium "
|
The guidance is dependent on the value of
|
"long "
|
The guidance is dependent on the value of
|
As with all "should"-level guidance, user agents might not conform perfectly to these. Especially in the case of counting words, it’s expected that language models might not conform perfectly.
Value | Meaning |
---|---|
"plain-text "
|
The summary should not contain any formatting or markup language. |
"markdown "
|
The summary should be formatted using the Markdown markup language, ideally as valid CommonMark. [COMMONMARK] |
3.5. Errors
A summarization error information is a struct with the following items:
- error name
-
a string that will be used for the
DOMException
's name. - error information
-
other information necessary to create a useful
DOMException
for the web developer. (Typically, just an exception message.)
When summarization fails, the following possible reasons may be surfaced to the web developer. This table lists the possible DOMException
names and the cases in which an implementation should use them:
DOMException name
| Scenarios |
---|---|
"NotAllowedError "
|
Summarization is disabled by user choice or user agent policy. |
"NotReadableError "
|
The summarization output was filtered by the user agent, e.g., because it was detected to be harmful, inaccurate, or nonsensical. |
"NotSupportedError "
|
The input to be summarized was in a language that the user agent does not support summarizing. The summarization output was detected to be in a language that the user agent does not have sufficient quality control for and is not willing to support. |
"QuotaExceededError "
|
The input to be summarized was too large for the user agent to handle. |
"UnknownError "
|
All other scenarios, or if the user agent would prefer not to disclose the failure reason. |
This table does not give the complete list of exceptions that can be surfaced by summarizer.summarize()
and summarizer.summarizeStreaming()
. It only contains those which can come from the implementation-defined summarize algorithm.