expo-ai-kit 0.1.16 → 0.1.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -29,7 +29,8 @@ On-device AI for Expo apps. Run language models locally—no API keys, no cloud,
29
29
  - **Native performance** — Built on Apple Foundation Models (iOS) and Google ML Kit Prompt API (Android)
30
30
  - **Multi-turn conversations** — Full conversation context support
31
31
  - **Streaming support** — Progressive token streaming for responsive UIs
32
- - **Simple API** — Just 3 functions: `isAvailable()`, `sendMessage()`, and `streamMessage()`
32
+ - **Simple API** — Core functions plus prompt helpers for common tasks
33
+ - **Prompt helpers** — Built-in `summarize()`, `translate()`, `rewrite()`, and more
33
34
 
34
35
  ## Requirements
35
36
 
@@ -172,6 +173,39 @@ const { promise, stop } = streamMessage(
172
173
  await promise;
173
174
  ```
174
175
 
176
+ ### Prompt Helpers
177
+
178
+ Use built-in helpers for common AI tasks without crafting prompts:
179
+
180
+ ```tsx
181
+ import { summarize, translate, rewrite, extractKeyPoints, answerQuestion } from 'expo-ai-kit';
182
+
183
+ // Summarize text
184
+ const summary = await summarize(longArticle, { length: 'short', style: 'bullets' });
185
+
186
+ // Translate text
187
+ const translated = await translate('Hello, world!', { to: 'Spanish' });
188
+
189
+ // Rewrite in a different style
190
+ const formal = await rewrite('hey whats up', { style: 'formal' });
191
+
192
+ // Extract key points
193
+ const points = await extractKeyPoints(article, { maxPoints: 5 });
194
+
195
+ // Answer questions about content
196
+ const answer = await answerQuestion('What is the main topic?', documentText);
197
+ ```
198
+
199
+ All helpers also have streaming variants (`streamSummarize`, `streamTranslate`, etc.):
200
+
201
+ ```tsx
202
+ const { promise, stop } = streamSummarize(
203
+ longArticle,
204
+ (event) => setSummary(event.accumulatedText),
205
+ { style: 'bullets' }
206
+ );
207
+ ```
208
+
175
209
  ### Streaming with Cancel Button
176
210
 
177
211
  ```tsx
@@ -380,6 +414,97 @@ const response = await promise;
380
414
 
381
415
  ---
382
416
 
417
+ ### `summarize(text, options?)`
418
+
419
+ Summarizes text using on-device AI.
420
+
421
+ ```typescript
422
+ function summarize(text: string, options?: LLMSummarizeOptions): Promise<LLMResponse>
423
+ ```
424
+
425
+ | Parameter | Type | Description |
426
+ |-----------|------|-------------|
427
+ | `text` | `string` | Text to summarize |
428
+ | `options.length` | `'short' \| 'medium' \| 'long'` | Summary length (default: `'medium'`) |
429
+ | `options.style` | `'paragraph' \| 'bullets' \| 'tldr'` | Output format (default: `'paragraph'`) |
430
+
431
+ **Streaming:** `streamSummarize(text, onToken, options?)`
432
+
433
+ ---
434
+
435
+ ### `translate(text, options)`
436
+
437
+ Translates text to another language.
438
+
439
+ ```typescript
440
+ function translate(text: string, options: LLMTranslateOptions): Promise<LLMResponse>
441
+ ```
442
+
443
+ | Parameter | Type | Description |
444
+ |-----------|------|-------------|
445
+ | `text` | `string` | Text to translate |
446
+ | `options.to` | `string` | Target language (required) |
447
+ | `options.from` | `string` | Source language (auto-detected if omitted) |
448
+ | `options.tone` | `'formal' \| 'informal' \| 'neutral'` | Translation tone (default: `'neutral'`) |
449
+
450
+ **Streaming:** `streamTranslate(text, onToken, options)`
451
+
452
+ ---
453
+
454
+ ### `rewrite(text, options)`
455
+
456
+ Rewrites text in a different style.
457
+
458
+ ```typescript
459
+ function rewrite(text: string, options: LLMRewriteOptions): Promise<LLMResponse>
460
+ ```
461
+
462
+ | Parameter | Type | Description |
463
+ |-----------|------|-------------|
464
+ | `text` | `string` | Text to rewrite |
465
+ | `options.style` | `string` | Target style (required) |
466
+
467
+ **Available styles:** `'formal'`, `'casual'`, `'professional'`, `'friendly'`, `'concise'`, `'detailed'`, `'simple'`, `'academic'`
468
+
469
+ **Streaming:** `streamRewrite(text, onToken, options)`
470
+
471
+ ---
472
+
473
+ ### `extractKeyPoints(text, options?)`
474
+
475
+ Extracts key points from text as bullet points.
476
+
477
+ ```typescript
478
+ function extractKeyPoints(text: string, options?: LLMExtractKeyPointsOptions): Promise<LLMResponse>
479
+ ```
480
+
481
+ | Parameter | Type | Description |
482
+ |-----------|------|-------------|
483
+ | `text` | `string` | Text to analyze |
484
+ | `options.maxPoints` | `number` | Maximum points to extract (default: `5`) |
485
+
486
+ **Streaming:** `streamExtractKeyPoints(text, onToken, options?)`
487
+
488
+ ---
489
+
490
+ ### `answerQuestion(question, context, options?)`
491
+
492
+ Answers a question based on provided context.
493
+
494
+ ```typescript
495
+ function answerQuestion(question: string, context: string, options?: LLMAnswerQuestionOptions): Promise<LLMResponse>
496
+ ```
497
+
498
+ | Parameter | Type | Description |
499
+ |-----------|------|-------------|
500
+ | `question` | `string` | Question to answer |
501
+ | `context` | `string` | Context/document to base answer on |
502
+ | `options.detail` | `'brief' \| 'medium' \| 'detailed'` | Answer detail level (default: `'medium'`) |
503
+
504
+ **Streaming:** `streamAnswerQuestion(question, context, onToken, options?)`
505
+
506
+ ---
507
+
383
508
  ### Types
384
509
 
385
510
  ```typescript
@@ -417,6 +542,30 @@ type LLMStreamEvent = {
417
542
  };
418
543
 
419
544
  type LLMStreamCallback = (event: LLMStreamEvent) => void;
545
+
546
+ // Prompt Helper Types
547
+ type LLMSummarizeOptions = {
548
+ length?: 'short' | 'medium' | 'long';
549
+ style?: 'paragraph' | 'bullets' | 'tldr';
550
+ };
551
+
552
+ type LLMTranslateOptions = {
553
+ to: string;
554
+ from?: string;
555
+ tone?: 'formal' | 'informal' | 'neutral';
556
+ };
557
+
558
+ type LLMRewriteOptions = {
559
+ style: 'formal' | 'casual' | 'professional' | 'friendly' | 'concise' | 'detailed' | 'simple' | 'academic';
560
+ };
561
+
562
+ type LLMExtractKeyPointsOptions = {
563
+ maxPoints?: number;
564
+ };
565
+
566
+ type LLMAnswerQuestionOptions = {
567
+ detail?: 'brief' | 'medium' | 'detailed';
568
+ };
420
569
  ```
421
570
 
422
571
  ## Feature Comparison
@@ -426,6 +575,7 @@ type LLMStreamCallback = (event: LLMStreamEvent) => void;
426
575
  | `isAvailable()` | ✅ | ✅ |
427
576
  | `sendMessage()` | ✅ | ✅ |
428
577
  | `streamMessage()` | ✅ | ✅ |
578
+ | Prompt helpers | ✅ | ✅ |
429
579
  | System prompts | ✅ Native | ✅ Prepended |
430
580
  | Multi-turn context | ✅ | ✅ |
431
581
  | Cancel streaming | ✅ | ✅ |
@@ -476,7 +626,8 @@ const { text } = await sendMessage(messages, { systemPrompt: '...' });
476
626
  | Feature | Status | Priority |
477
627
  |---------|--------|----------|
478
628
  | ✅ Streaming responses | Done | - |
479
- | Prompt helpers (summarize, translate, etc.) | Planned | Medium |
629
+ | Prompt helpers (summarize, translate, etc.) | Done | - |
630
+ | Conversation memory (useChat hook) | Planned | High |
480
631
  | Web/generic fallback | Idea | Medium |
481
632
  | Configurable hyperparameters (temperature, etc.) | Idea | Low |
482
633
 
package/build/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { LLMMessage, LLMSendOptions, LLMResponse, LLMStreamOptions, LLMStreamCallback } from './types';
1
+ import { LLMMessage, LLMSendOptions, LLMResponse, LLMStreamOptions, LLMStreamCallback, LLMSummarizeOptions, LLMTranslateOptions, LLMRewriteOptions, LLMExtractKeyPointsOptions, LLMAnswerQuestionOptions } from './types';
2
2
  export * from './types';
3
3
  /**
4
4
  * Check if on-device AI is available on the current device.
@@ -89,4 +89,247 @@ export declare function streamMessage(messages: LLMMessage[], onToken: LLMStream
89
89
  promise: Promise<LLMResponse>;
90
90
  stop: () => void;
91
91
  };
92
+ /**
93
+ * Summarize text content using on-device AI.
94
+ *
95
+ * @param text - The text to summarize
96
+ * @param options - Optional settings for summary style and length
97
+ * @returns Promise with the generated summary
98
+ *
99
+ * @example
100
+ * ```ts
101
+ * // Basic summarization
102
+ * const result = await summarize(longArticle);
103
+ * console.log(result.text);
104
+ * ```
105
+ *
106
+ * @example
107
+ * ```ts
108
+ * // Short bullet-point summary
109
+ * const result = await summarize(longArticle, {
110
+ * length: 'short',
111
+ * style: 'bullets'
112
+ * });
113
+ * ```
114
+ *
115
+ * @example
116
+ * ```ts
117
+ * // TL;DR style
118
+ * const result = await summarize(longArticle, {
119
+ * style: 'tldr'
120
+ * });
121
+ * ```
122
+ */
123
+ export declare function summarize(text: string, options?: LLMSummarizeOptions): Promise<LLMResponse>;
124
+ /**
125
+ * Summarize text with streaming output.
126
+ *
127
+ * @param text - The text to summarize
128
+ * @param onToken - Callback for each token received
129
+ * @param options - Optional settings for summary style and length
130
+ * @returns Object with stop() function and promise
131
+ *
132
+ * @example
133
+ * ```ts
134
+ * const { promise } = streamSummarize(
135
+ * longArticle,
136
+ * (event) => setSummary(event.accumulatedText),
137
+ * { style: 'bullets' }
138
+ * );
139
+ * await promise;
140
+ * ```
141
+ */
142
+ export declare function streamSummarize(text: string, onToken: LLMStreamCallback, options?: LLMSummarizeOptions): {
143
+ promise: Promise<LLMResponse>;
144
+ stop: () => void;
145
+ };
146
+ /**
147
+ * Translate text to another language using on-device AI.
148
+ *
149
+ * @param text - The text to translate
150
+ * @param options - Translation options including target language
151
+ * @returns Promise with the translated text
152
+ *
153
+ * @example
154
+ * ```ts
155
+ * // Basic translation
156
+ * const result = await translate('Hello, world!', { to: 'Spanish' });
157
+ * console.log(result.text); // "¡Hola, mundo!"
158
+ * ```
159
+ *
160
+ * @example
161
+ * ```ts
162
+ * // Formal translation with source language
163
+ * const result = await translate('Hey, what\'s up?', {
164
+ * to: 'French',
165
+ * from: 'English',
166
+ * tone: 'formal'
167
+ * });
168
+ * ```
169
+ */
170
+ export declare function translate(text: string, options: LLMTranslateOptions): Promise<LLMResponse>;
171
+ /**
172
+ * Translate text with streaming output.
173
+ *
174
+ * @param text - The text to translate
175
+ * @param onToken - Callback for each token received
176
+ * @param options - Translation options including target language
177
+ * @returns Object with stop() function and promise
178
+ *
179
+ * @example
180
+ * ```ts
181
+ * const { promise } = streamTranslate(
182
+ * 'Hello, world!',
183
+ * (event) => setTranslation(event.accumulatedText),
184
+ * { to: 'Japanese' }
185
+ * );
186
+ * await promise;
187
+ * ```
188
+ */
189
+ export declare function streamTranslate(text: string, onToken: LLMStreamCallback, options: LLMTranslateOptions): {
190
+ promise: Promise<LLMResponse>;
191
+ stop: () => void;
192
+ };
193
+ /**
194
+ * Rewrite text in a different style using on-device AI.
195
+ *
196
+ * @param text - The text to rewrite
197
+ * @param options - Rewrite options specifying the target style
198
+ * @returns Promise with the rewritten text
199
+ *
200
+ * @example
201
+ * ```ts
202
+ * // Make text more formal
203
+ * const result = await rewrite('hey can u help me out?', {
204
+ * style: 'formal'
205
+ * });
206
+ * console.log(result.text); // "Would you be able to assist me?"
207
+ * ```
208
+ *
209
+ * @example
210
+ * ```ts
211
+ * // Simplify complex text
212
+ * const result = await rewrite(technicalText, { style: 'simple' });
213
+ * ```
214
+ */
215
+ export declare function rewrite(text: string, options: LLMRewriteOptions): Promise<LLMResponse>;
216
+ /**
217
+ * Rewrite text with streaming output.
218
+ *
219
+ * @param text - The text to rewrite
220
+ * @param onToken - Callback for each token received
221
+ * @param options - Rewrite options specifying the target style
222
+ * @returns Object with stop() function and promise
223
+ *
224
+ * @example
225
+ * ```ts
226
+ * const { promise } = streamRewrite(
227
+ * 'hey whats up',
228
+ * (event) => setRewritten(event.accumulatedText),
229
+ * { style: 'professional' }
230
+ * );
231
+ * await promise;
232
+ * ```
233
+ */
234
+ export declare function streamRewrite(text: string, onToken: LLMStreamCallback, options: LLMRewriteOptions): {
235
+ promise: Promise<LLMResponse>;
236
+ stop: () => void;
237
+ };
238
+ /**
239
+ * Extract key points from text using on-device AI.
240
+ *
241
+ * @param text - The text to extract key points from
242
+ * @param options - Optional settings for extraction
243
+ * @returns Promise with the key points as text
244
+ *
245
+ * @example
246
+ * ```ts
247
+ * // Extract key points from an article
248
+ * const result = await extractKeyPoints(article);
249
+ * console.log(result.text);
250
+ * // "• Point 1\n• Point 2\n• Point 3"
251
+ * ```
252
+ *
253
+ * @example
254
+ * ```ts
255
+ * // Limit to 3 key points
256
+ * const result = await extractKeyPoints(article, { maxPoints: 3 });
257
+ * ```
258
+ */
259
+ export declare function extractKeyPoints(text: string, options?: LLMExtractKeyPointsOptions): Promise<LLMResponse>;
260
+ /**
261
+ * Extract key points with streaming output.
262
+ *
263
+ * @param text - The text to extract key points from
264
+ * @param onToken - Callback for each token received
265
+ * @param options - Optional settings for extraction
266
+ * @returns Object with stop() function and promise
267
+ *
268
+ * @example
269
+ * ```ts
270
+ * const { promise } = streamExtractKeyPoints(
271
+ * article,
272
+ * (event) => setKeyPoints(event.accumulatedText),
273
+ * { maxPoints: 5 }
274
+ * );
275
+ * await promise;
276
+ * ```
277
+ */
278
+ export declare function streamExtractKeyPoints(text: string, onToken: LLMStreamCallback, options?: LLMExtractKeyPointsOptions): {
279
+ promise: Promise<LLMResponse>;
280
+ stop: () => void;
281
+ };
282
+ /**
283
+ * Answer a question based on provided context using on-device AI.
284
+ *
285
+ * @param question - The question to answer
286
+ * @param context - The context/document to base the answer on
287
+ * @param options - Optional settings for the answer
288
+ * @returns Promise with the answer
289
+ *
290
+ * @example
291
+ * ```ts
292
+ * // Answer a question about a document
293
+ * const result = await answerQuestion(
294
+ * 'What is the main topic?',
295
+ * documentText
296
+ * );
297
+ * console.log(result.text);
298
+ * ```
299
+ *
300
+ * @example
301
+ * ```ts
302
+ * // Get a detailed answer
303
+ * const result = await answerQuestion(
304
+ * 'Explain the methodology',
305
+ * researchPaper,
306
+ * { detail: 'detailed' }
307
+ * );
308
+ * ```
309
+ */
310
+ export declare function answerQuestion(question: string, context: string, options?: LLMAnswerQuestionOptions): Promise<LLMResponse>;
311
+ /**
312
+ * Answer a question with streaming output.
313
+ *
314
+ * @param question - The question to answer
315
+ * @param context - The context/document to base the answer on
316
+ * @param onToken - Callback for each token received
317
+ * @param options - Optional settings for the answer
318
+ * @returns Object with stop() function and promise
319
+ *
320
+ * @example
321
+ * ```ts
322
+ * const { promise } = streamAnswerQuestion(
323
+ * 'What are the key findings?',
324
+ * documentText,
325
+ * (event) => setAnswer(event.accumulatedText),
326
+ * { detail: 'detailed' }
327
+ * );
328
+ * await promise;
329
+ * ```
330
+ */
331
+ export declare function streamAnswerQuestion(question: string, context: string, onToken: LLMStreamCallback, options?: LLMAnswerQuestionOptions): {
332
+ promise: Promise<LLMResponse>;
333
+ stop: () => void;
334
+ };
92
335
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,UAAU,EACV,cAAc,EACd,WAAW,EACX,gBAAgB,EAEhB,iBAAiB,EAClB,MAAM,SAAS,CAAC;AAEjB,cAAc,SAAS,CAAC;AAUxB;;;GAGG;AACH,wBAAsB,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC,CAKpD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAsB,WAAW,CAC/B,QAAQ,EAAE,UAAU,EAAE,EACtB,OAAO,CAAC,EAAE,cAAc,GACvB,OAAO,CAAC,WAAW,CAAC,CAgBtB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,wBAAgB,aAAa,CAC3B,QAAQ,EAAE,UAAU,EAAE,EACtB,OAAO,EAAE,iBAAiB,EAC1B,OAAO,CAAC,EAAE,gBAAgB,GACzB;IAAE,OAAO,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC;IAAC,IAAI,EAAE,MAAM,IAAI,CAAA;CAAE,CAiErD"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,UAAU,EACV,cAAc,EACd,WAAW,EACX,gBAAgB,EAEhB,iBAAiB,EACjB,mBAAmB,EACnB,mBAAmB,EACnB,iBAAiB,EACjB,0BAA0B,EAC1B,wBAAwB,EACzB,MAAM,SAAS,CAAC;AAEjB,cAAc,SAAS,CAAC;AAkGxB;;;GAGG;AACH,wBAAsB,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC,CAKpD;AAED;;;;;;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