@@ -108,42 +108,6 @@ describe('ChromeAdapter', () => {
108
108
} )
109
109
) . to . be . false ;
110
110
} ) ;
111
- it ( 'returns false if request system instruction has function role' , async ( ) => {
112
- const adapter = new ChromeAdapter ( { } as AI , 'prefer_on_device' ) ;
113
- expect (
114
- await adapter . isAvailable ( {
115
- contents : [ ] ,
116
- systemInstruction : {
117
- role : 'function' ,
118
- parts : [ ]
119
- }
120
- } )
121
- ) . to . be . false ;
122
- } ) ;
123
- it ( 'returns false if request system instruction has multiple parts' , async ( ) => {
124
- const adapter = new ChromeAdapter ( { } as AI , 'prefer_on_device' ) ;
125
- expect (
126
- await adapter . isAvailable ( {
127
- contents : [ ] ,
128
- systemInstruction : {
129
- role : 'function' ,
130
- parts : [ { text : 'a' } , { text : 'b' } ]
131
- }
132
- } )
133
- ) . to . be . false ;
134
- } ) ;
135
- it ( 'returns false if request system instruction has non-text part' , async ( ) => {
136
- const adapter = new ChromeAdapter ( { } as AI , 'prefer_on_device' ) ;
137
- expect (
138
- await adapter . isAvailable ( {
139
- contents : [ ] ,
140
- systemInstruction : {
141
- role : 'function' ,
142
- parts : [ { inlineData : { mimeType : 'a' , data : 'b' } } ]
143
- }
144
- } )
145
- ) . to . be . false ;
146
- } ) ;
147
111
it ( 'returns true if model is readily available' , async ( ) => {
148
112
const aiProvider = {
149
113
languageModel : {
@@ -173,13 +137,18 @@ describe('ChromeAdapter', () => {
173
137
const createStub = stub ( aiProvider . languageModel , 'create' ) . resolves (
174
138
{ } as AILanguageModel
175
139
) ;
176
- const adapter = new ChromeAdapter ( aiProvider , 'prefer_on_device' ) ;
140
+ const onDeviceParams = { } as AILanguageModelCreateOptionsWithSystemPrompt ;
141
+ const adapter = new ChromeAdapter (
142
+ aiProvider ,
143
+ 'prefer_on_device' ,
144
+ onDeviceParams
145
+ ) ;
177
146
expect (
178
147
await adapter . isAvailable ( {
179
148
contents : [ { role : 'user' , parts : [ { text : 'hi' } ] } ]
180
149
} )
181
150
) . to . be . false ;
182
- expect ( createStub ) . to . have . been . calledOnce ;
151
+ expect ( createStub ) . to . have . been . calledOnceWith ( onDeviceParams ) ;
183
152
} ) ;
184
153
it ( 'avoids redundant downloads' , async ( ) => {
185
154
const aiProvider = {
@@ -250,4 +219,83 @@ describe('ChromeAdapter', () => {
250
219
) . to . be . false ;
251
220
} ) ;
252
221
} ) ;
222
+ describe ( 'generateContentOnDevice' , ( ) => {
223
+ it ( 'Extracts and concats initial prompts' , async ( ) => {
224
+ const aiProvider = {
225
+ languageModel : {
226
+ create : ( ) => Promise . resolve ( { } )
227
+ }
228
+ } as AI ;
229
+ const factoryStub = stub ( aiProvider . languageModel , 'create' ) . resolves ( {
230
+ prompt : s => Promise . resolve ( s )
231
+ } as AILanguageModel ) ;
232
+ const text = [ 'first' , 'second' , 'third' ] ;
233
+ const onDeviceParams = {
234
+ initialPrompts : [ { role : 'user' , content : text [ 0 ] } ]
235
+ } as AILanguageModelCreateOptionsWithSystemPrompt ;
236
+ const adapter = new ChromeAdapter (
237
+ aiProvider ,
238
+ 'prefer_on_device' ,
239
+ onDeviceParams
240
+ ) ;
241
+ const response = await adapter . generateContentOnDevice ( {
242
+ contents : [
243
+ { role : 'model' , parts : [ { text : text [ 1 ] } ] } ,
244
+ { role : 'user' , parts : [ { text : text [ 2 ] } ] }
245
+ ]
246
+ } ) ;
247
+ expect ( factoryStub ) . to . have . been . calledOnceWith ( {
248
+ initialPrompts : [
249
+ { role : 'user' , content : text [ 0 ] } ,
250
+ // Asserts tail is passed as initial prompts, and
251
+ // role is normalized from model to assistant.
252
+ { role : 'assistant' , content : text [ 1 ] }
253
+ ]
254
+ } ) ;
255
+ expect ( await response . json ( ) ) . to . deep . equal ( {
256
+ candidates : [
257
+ {
258
+ content : {
259
+ parts : [ { text : text [ 2 ] } ]
260
+ }
261
+ }
262
+ ]
263
+ } ) ;
264
+ } ) ;
265
+ it ( 'Extracts system prompt' , async ( ) => {
266
+ const aiProvider = {
267
+ languageModel : {
268
+ create : ( ) => Promise . resolve ( { } )
269
+ }
270
+ } as AI ;
271
+ const factoryStub = stub ( aiProvider . languageModel , 'create' ) . resolves ( {
272
+ prompt : s => Promise . resolve ( s )
273
+ } as AILanguageModel ) ;
274
+ const onDeviceParams = {
275
+ systemPrompt : 'be yourself'
276
+ } as AILanguageModelCreateOptionsWithSystemPrompt ;
277
+ const adapter = new ChromeAdapter (
278
+ aiProvider ,
279
+ 'prefer_on_device' ,
280
+ onDeviceParams
281
+ ) ;
282
+ const text = 'hi' ;
283
+ const response = await adapter . generateContentOnDevice ( {
284
+ contents : [ { role : 'user' , parts : [ { text } ] } ]
285
+ } ) ;
286
+ expect ( factoryStub ) . to . have . been . calledOnceWith ( {
287
+ initialPrompts : [ ] ,
288
+ systemPrompt : onDeviceParams . systemPrompt
289
+ } ) ;
290
+ expect ( await response . json ( ) ) . to . deep . equal ( {
291
+ candidates : [
292
+ {
293
+ content : {
294
+ parts : [ { text } ]
295
+ }
296
+ }
297
+ ]
298
+ } ) ;
299
+ } ) ;
300
+ } ) ;
253
301
} ) ;
0 commit comments