react-native-ai-core 0.1.0 → 0.2.0

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2026 Alberto Fernandez
3
+ Copyright (c) 2026 Alberto Fernandez (@albertoroda)
4
4
  Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
  of this software and associated documentation files (the "Software"), to deal
6
6
  in the Software without restriction, including without limitation the rights
package/README.md CHANGED
@@ -99,6 +99,46 @@ To start a fresh conversation without releasing the model:
99
99
  await AICore.resetConversation();
100
100
  ```
101
101
 
102
+ ### Structured output with runtime validation
103
+
104
+ For app-internal AI features such as extraction, classification, routing, or tool orchestration, use `generateStructuredResponse(...)`.
105
+
106
+ - Validates optional structured input before generation
107
+ - Forces JSON-only output
108
+ - Extracts JSON even if the model wraps it in extra text
109
+ - Validates the final payload with `zod`
110
+ - Retries automatically with a repair prompt when validation fails
111
+ - Uses a stateless native request so it does not pollute chat conversation history
112
+
113
+ ```tsx
114
+ import { z } from 'zod';
115
+ import { generateStructuredResponse } from 'react-native-ai-core';
116
+
117
+ const TicketSchema = z.object({
118
+ category: z.enum(['bug', 'billing', 'feature']),
119
+ priority: z.enum(['low', 'medium', 'high']),
120
+ summary: z.string(),
121
+ needsHuman: z.boolean(),
122
+ });
123
+
124
+ const result = await generateStructuredResponse({
125
+ prompt: 'Classify this support request and summarize it.',
126
+ input: {
127
+ message: 'The app crashes when I try to export a PDF invoice.',
128
+ },
129
+ output: TicketSchema,
130
+ });
131
+ ```
132
+
133
+ Recommended for reliability on-device:
134
+ - Keep the prompt short and task-specific
135
+ - Keep `input` compact and validated before sending it
136
+ - Prefer small output schemas over deeply nested ones
137
+ - Use this API for internal app workflows, not long-form generation
138
+ - Repair retries are bounded and prompt size is trimmed internally to avoid hitting the same context limits as chat flows
139
+
140
+ There is also a concrete demo helper in [example/src/examples/structuredOutputExample.ts](example/src/examples/structuredOutputExample.ts).
141
+
102
142
  ---
103
143
 
104
144
  ## API Reference
@@ -134,6 +174,36 @@ const response = await AICore.generateResponse('Tell me a joke');
134
174
 
135
175
  ---
136
176
 
177
+ ### `generateStructuredResponse(options): Promise<T>`
178
+
179
+ Generates stateless structured JSON and validates it against a user-defined `zod` schema.
180
+
181
+ ```tsx
182
+ import { z } from 'zod';
183
+
184
+ const OutputSchema = z.object({
185
+ intent: z.enum(['search', 'reply', 'ignore']),
186
+ confidence: z.number(),
187
+ });
188
+
189
+ const output = await generateStructuredResponse({
190
+ prompt: 'Determine the next action for this message.',
191
+ input: { message: 'Can you send me the invoice again?' },
192
+ output: OutputSchema,
193
+ });
194
+ ```
195
+
196
+ Options:
197
+ - `prompt` — natural language instruction
198
+ - `input` — optional structured input object
199
+ - `inputSchema` — optional `zod` schema to validate the input before generation
200
+ - `output` — required `zod` schema used to validate the model output
201
+ - `maxRetries` — optional number of repair attempts when validation fails
202
+
203
+ Throws `StructuredOutputError` if valid JSON matching the schema cannot be produced after retries.
204
+
205
+ ---
206
+
137
207
  ### `generateResponseStream(prompt: string, callbacks: StreamCallbacks): () => void`
138
208
 
139
209
  Generates a response token by token via streaming. Returns a cleanup function to remove event listeners.
@@ -1,2 +1,14 @@
1
1
  <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
+
3
+ <!-- Keeps the process alive while AI generation runs in background -->
4
+ <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
5
+ <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
6
+
7
+ <application>
8
+ <service
9
+ android:name=".InferenceService"
10
+ android:exported="false"
11
+ android:foregroundServiceType="dataSync" />
12
+ </application>
13
+
2
14
  </manifest>