2
2
* Shims @types/dom-chromium-ai
3
3
* TODO: replace with @types/dom-chromium-ai once we can use es2020.intl.
4
4
*/
5
- interface AI {
6
- readonly languageModel : AILanguageModelFactory ;
7
- readonly summarizer : AISummarizerFactory ;
8
- readonly writer : AIWriterFactory ;
9
- readonly rewriter : AIRewriterFactory ;
10
- readonly translator : AITranslatorFactory ;
11
- readonly languageDetector : AILanguageDetectorFactory ;
12
- }
13
-
14
5
interface AICreateMonitor extends EventTarget {
6
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
7
ondownloadprogress : ( ( this : AICreateMonitor , ev : DownloadProgressEvent ) => any ) | null ;
16
8
17
9
addEventListener < K extends keyof AICreateMonitorEventMap > (
18
10
type : K ,
11
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
19
12
listener : ( this : AICreateMonitor , ev : AICreateMonitorEventMap [ K ] ) => any ,
20
13
options ?: boolean | AddEventListenerOptions ,
21
14
) : void ;
@@ -26,6 +19,7 @@ interface AICreateMonitor extends EventTarget {
26
19
) : void ;
27
20
removeEventListener < K extends keyof AICreateMonitorEventMap > (
28
21
type : K ,
22
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
29
23
listener : ( this : AICreateMonitor , ev : AICreateMonitorEventMap [ K ] ) => any ,
30
24
options ?: boolean | EventListenerOptions ,
31
25
) : void ;
@@ -47,18 +41,9 @@ interface AICreateMonitorEventMap {
47
41
48
42
type AICreateMonitorCallback = ( monitor : AICreateMonitor ) => void ;
49
43
50
- type AICapabilityAvailability = "readily" | "after-download" | "no" ;
51
-
52
44
// Language Model
53
45
// https://github.com/explainers-by-googlers/prompt-api/#full-api-surface-in-web-idl
54
46
55
- interface AILanguageModelFactory {
56
- create (
57
- options ?: AILanguageModelCreateOptionsWithSystemPrompt | AILanguageModelCreateOptionsWithoutSystemPrompt ,
58
- ) : Promise < AILanguageModel > ;
59
- capabilities ( ) : Promise < AILanguageModelCapabilities > ;
60
- }
61
-
62
47
interface AILanguageModelCreateOptions {
63
48
signal ?: AbortSignal ;
64
49
monitor ?: AICreateMonitorCallback ;
@@ -72,310 +57,9 @@ export interface AILanguageModelCreateOptionsWithSystemPrompt extends AILanguage
72
57
initialPrompts ?: AILanguageModelPrompt [ ] ;
73
58
}
74
59
75
- interface AILanguageModelCreateOptionsWithoutSystemPrompt extends AILanguageModelCreateOptions {
76
- systemPrompt ?: never ;
77
- initialPrompts ?:
78
- | [ AILanguageModelSystemPrompt , ...AILanguageModelPrompt [ ] ]
79
- | AILanguageModelPrompt [ ] ;
80
- }
81
-
82
60
type AILanguageModelPromptRole = "user" | "assistant" ;
83
- type AILanguageModelInitialPromptRole = "system" | AILanguageModelPromptRole ;
84
61
85
62
interface AILanguageModelPrompt {
86
63
role : AILanguageModelPromptRole ;
87
64
content : string ;
88
65
}
89
-
90
- interface AILanguageModelInitialPrompt {
91
- role : AILanguageModelInitialPromptRole ;
92
- content : string ;
93
- }
94
-
95
- interface AILanguageModelSystemPrompt extends AILanguageModelInitialPrompt {
96
- role : "system" ;
97
- }
98
-
99
- type AILanguageModelPromptInput = string | AILanguageModelPrompt | AILanguageModelPrompt [ ] ;
100
-
101
- interface AILanguageModel extends EventTarget {
102
- prompt ( input : AILanguageModelPromptInput , options ?: AILanguageModelPromptOptions ) : Promise < string > ;
103
- promptStreaming ( input : AILanguageModelPromptInput , options ?: AILanguageModelPromptOptions ) : ReadableStream < string > ;
104
-
105
- countPromptTokens ( input : AILanguageModelPromptInput , options ?: AILanguageModelPromptOptions ) : Promise < number > ;
106
- readonly maxTokens : number ;
107
- readonly tokensSoFar : number ;
108
- readonly tokensLeft : number ;
109
-
110
- readonly topK : number ;
111
- readonly temperature : number ;
112
-
113
- oncontextoverflow : ( ( this : AILanguageModel , ev : Event ) => any ) | null ;
114
-
115
- addEventListener < K extends keyof AILanguageModelEventMap > (
116
- type : K ,
117
- listener : ( this : AILanguageModel , ev : AILanguageModelEventMap [ K ] ) => any ,
118
- options ?: boolean | AddEventListenerOptions ,
119
- ) : void ;
120
- addEventListener (
121
- type : string ,
122
- listener : EventListenerOrEventListenerObject ,
123
- options ?: boolean | AddEventListenerOptions ,
124
- ) : void ;
125
- removeEventListener < K extends keyof AILanguageModelEventMap > (
126
- type : K ,
127
- listener : ( this : AILanguageModel , ev : AILanguageModelEventMap [ K ] ) => any ,
128
- options ?: boolean | EventListenerOptions ,
129
- ) : void ;
130
- removeEventListener (
131
- type : string ,
132
- listener : EventListenerOrEventListenerObject ,
133
- options ?: boolean | EventListenerOptions ,
134
- ) : void ;
135
-
136
- clone ( options ?: AILanguageModelCloneOptions ) : Promise < AILanguageModel > ;
137
- destroy ( ) : void ;
138
- }
139
-
140
- interface AILanguageModelEventMap {
141
- contextoverflow : Event ;
142
- }
143
-
144
- interface AILanguageModelPromptOptions {
145
- signal ?: AbortSignal ;
146
- }
147
-
148
- interface AILanguageModelCloneOptions {
149
- signal ?: AbortSignal ;
150
- }
151
-
152
- interface AILanguageModelCapabilities {
153
- readonly available : AICapabilityAvailability ;
154
- languageAvailable ( languageTag : Intl . UnicodeBCP47LocaleIdentifier ) : AICapabilityAvailability ;
155
-
156
- readonly defaultTopK : number | null ;
157
- readonly maxTopK : number | null ;
158
- readonly defaultTemperature : number | null ;
159
- readonly maxTemperature : number | null ;
160
- }
161
-
162
- // Summarizer
163
- // https://github.com/explainers-by-googlers/writing-assistance-apis/#full-api-surface-in-web-idl
164
-
165
- interface AISummarizerFactory {
166
- create ( options ?: AISummarizerCreateOptions ) : Promise < AISummarizer > ;
167
- capabilities ( ) : Promise < AISummarizerCapabilities > ;
168
- }
169
-
170
- interface AISummarizerCreateOptions {
171
- signal ?: AbortSignal ;
172
- monitor ?: AICreateMonitorCallback ;
173
-
174
- sharedContext ?: string ;
175
- type ?: AISummarizerType ;
176
- format ?: AISummarizerFormat ;
177
- length ?: AISummarizerLength ;
178
- }
179
-
180
- type AISummarizerType = "tl;dr" | "key-points" | "teaser" | "headline" ;
181
- type AISummarizerFormat = "plain-text" | "markdown" ;
182
- type AISummarizerLength = "short" | "medium" | "long" ;
183
-
184
- interface AISummarizer {
185
- summarize ( input : string , options ?: AISummarizerSummarizeOptions ) : Promise < string > ;
186
- summarizeStreaming ( input : string , options ?: AISummarizerSummarizeOptions ) : ReadableStream < string > ;
187
-
188
- readonly sharedContext : string ;
189
- readonly type : AISummarizerType ;
190
- readonly format : AISummarizerFormat ;
191
- readonly length : AISummarizerLength ;
192
-
193
- destroy ( ) : void ;
194
- }
195
-
196
- interface AISummarizerSummarizeOptions {
197
- signal ?: AbortSignal ;
198
- context ?: string ;
199
- }
200
-
201
- interface AISummarizerCapabilities {
202
- readonly available : AICapabilityAvailability ;
203
-
204
- supportsType ( type : AISummarizerType ) : AICapabilityAvailability ;
205
- supportsFormat ( format : AISummarizerFormat ) : AICapabilityAvailability ;
206
- supportsLength ( length : AISummarizerLength ) : AICapabilityAvailability ;
207
-
208
- languageAvailable ( languageTag : Intl . UnicodeBCP47LocaleIdentifier ) : AICapabilityAvailability ;
209
- }
210
-
211
- // Writer
212
- // https://github.com/explainers-by-googlers/writing-assistance-apis/#full-api-surface-in-web-idl
213
-
214
- interface AIWriterFactory {
215
- create ( options ?: AIWriterCreateOptions ) : Promise < AIWriter > ;
216
- capabilities ( ) : Promise < AIWriterCapabilities > ;
217
- }
218
-
219
- interface AIWriterCreateOptions {
220
- signal ?: AbortSignal ;
221
- monitor ?: AICreateMonitorCallback ;
222
-
223
- sharedContext ?: string ;
224
- tone ?: AIWriterTone ;
225
- format ?: AIWriterFormat ;
226
- length ?: AIWriterLength ;
227
- }
228
-
229
- type AIWriterTone = "formal" | "neutral" | "casual" ;
230
- type AIWriterFormat = "plain-text" | "markdown" ;
231
- type AIWriterLength = "short" | "medium" | "long" ;
232
-
233
- interface AIWriter {
234
- write ( writingTask : string , options ?: AIWriterWriteOptions ) : Promise < string > ;
235
- writeStreaming ( writingTask : string , options ?: AIWriterWriteOptions ) : ReadableStream < string > ;
236
-
237
- readonly sharedContext : string ;
238
- readonly tone : AIWriterTone ;
239
- readonly format : AIWriterFormat ;
240
- readonly length : AIWriterLength ;
241
-
242
- destroy ( ) : void ;
243
- }
244
-
245
- interface AIWriterWriteOptions {
246
- signal ?: AbortSignal ;
247
- context ?: string ;
248
- }
249
-
250
- interface AIWriterCapabilities {
251
- readonly available : AICapabilityAvailability ;
252
-
253
- supportsTone ( tone : AIWriterTone ) : AICapabilityAvailability ;
254
- supportsFormat ( format : AIWriterFormat ) : AICapabilityAvailability ;
255
- supportsLength ( length : AIWriterLength ) : AICapabilityAvailability ;
256
-
257
- languageAvailable ( languageTag : Intl . UnicodeBCP47LocaleIdentifier ) : AICapabilityAvailability ;
258
- }
259
-
260
- // Rewriter
261
- // https://github.com/explainers-by-googlers/writing-assistance-apis/#full-api-surface-in-web-idl
262
-
263
- interface AIRewriterFactory {
264
- create ( options ?: AIRewriterCreateOptions ) : Promise < AIRewriter > ;
265
- capabilities ( ) : Promise < AIRewriterCapabilities > ;
266
- }
267
-
268
- interface AIRewriterCreateOptions {
269
- signal ?: AbortSignal ;
270
- monitor ?: AICreateMonitorCallback ;
271
-
272
- sharedContext ?: string ;
273
- tone ?: AIRewriterTone ;
274
- format ?: AIRewriterFormat ;
275
- length ?: AIRewriterLength ;
276
- }
277
-
278
- type AIRewriterTone = "as-is" | "more-formal" | "more-casual" ;
279
- type AIRewriterFormat = "as-is" | "plain-text" | "markdown" ;
280
- type AIRewriterLength = "as-is" | "shorter" | "longer" ;
281
-
282
- interface AIRewriter {
283
- rewrite ( input : string , options ?: AIRewriterRewriteOptions ) : Promise < string > ;
284
- rewriteStreaming ( input : string , options ?: AIRewriterRewriteOptions ) : ReadableStream < string > ;
285
-
286
- readonly sharedContext : string ;
287
- readonly tone : AIRewriterTone ;
288
- readonly format : AIRewriterFormat ;
289
- readonly length : AIRewriterLength ;
290
-
291
- destroy ( ) : void ;
292
- }
293
-
294
- interface AIRewriterRewriteOptions {
295
- signal ?: AbortSignal ;
296
- context ?: string ;
297
- }
298
-
299
- interface AIRewriterCapabilities {
300
- readonly available : AICapabilityAvailability ;
301
-
302
- supportsTone ( tone : AIRewriterTone ) : AICapabilityAvailability ;
303
- supportsFormat ( format : AIRewriterFormat ) : AICapabilityAvailability ;
304
- supportsLength ( length : AIRewriterLength ) : AICapabilityAvailability ;
305
-
306
- languageAvailable ( languageTag : Intl . UnicodeBCP47LocaleIdentifier ) : AICapabilityAvailability ;
307
- }
308
-
309
- // Translator
310
- // https://github.com/WICG/translation-api?tab=readme-ov-file#full-api-surface-in-web-idl
311
-
312
- interface AITranslatorFactory {
313
- create ( options : AITranslatorCreateOptions ) : Promise < AITranslator > ;
314
- capabilities ( ) : Promise < AITranslatorCapabilities > ;
315
- }
316
-
317
- interface AITranslator {
318
- translate ( input : string , options ?: AITranslatorTranslateOptions ) : Promise < string > ;
319
- translateStreaming ( input : string , options ?: AITranslatorTranslateOptions ) : ReadableStream ;
320
-
321
- readonly sourceLanguage : Intl . UnicodeBCP47LocaleIdentifier ;
322
- readonly targetLanguage : Intl . UnicodeBCP47LocaleIdentifier ;
323
-
324
- destroy ( ) : void ;
325
- }
326
-
327
- interface AITranslatorCapabilities {
328
- readonly available : AICapabilityAvailability ;
329
-
330
- languagePairAvailable (
331
- sourceLanguage : Intl . UnicodeBCP47LocaleIdentifier ,
332
- targetLanguage : Intl . UnicodeBCP47LocaleIdentifier ,
333
- ) : AICapabilityAvailability ;
334
- }
335
-
336
- interface AITranslatorCreateOptions {
337
- signal ?: AbortSignal ;
338
- monitor ?: AICreateMonitorCallback ;
339
-
340
- sourceLanguage : Intl . UnicodeBCP47LocaleIdentifier ;
341
- targetLanguage : Intl . UnicodeBCP47LocaleIdentifier ;
342
- }
343
-
344
- interface AITranslatorTranslateOptions {
345
- signal ?: AbortSignal ;
346
- }
347
-
348
- // Language detector
349
- // https://github.com/WICG/translation-api?tab=readme-ov-file#full-api-surface-in-web-idl
350
-
351
- interface AILanguageDetectorFactory {
352
- create ( options ?: AILanguageDetectorCreateOptions ) : Promise < AILanguageDetector > ;
353
- capabilities ( ) : Promise < AILanguageDetectorCapabilities > ;
354
- }
355
-
356
- interface AILanguageDetector {
357
- detect ( input : string , options ?: AILanguageDetectorDetectOptions ) : Promise < LanguageDetectionResult [ ] > ;
358
-
359
- destroy ( ) : void ;
360
- }
361
-
362
- interface AILanguageDetectorCapabilities {
363
- readonly available : AICapabilityAvailability ;
364
-
365
- languageAvailable ( languageTag : Intl . UnicodeBCP47LocaleIdentifier ) : AICapabilityAvailability ;
366
- }
367
-
368
- interface AILanguageDetectorCreateOptions {
369
- signal ?: AbortSignal ;
370
- monitor ?: AICreateMonitorCallback ;
371
- }
372
-
373
- interface AILanguageDetectorDetectOptions {
374
- signal ?: AbortSignal ;
375
- }
376
-
377
- interface LanguageDetectionResult {
378
- /** null represents unknown language */
379
- detectedLanguage : Intl . UnicodeBCP47LocaleIdentifier | null ;
380
- confidence : number ;
381
- }
0 commit comments