@oryksa/react-native 1.1.2 → 1.1.4

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/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.1.4
4
+
5
+ * **The AI doing what you want in your app: `client.ai({...})`.** An AI task of your own feature, from the app, with
6
+ the session token (never the secret key): instructions, text, up to 4 photos and an optional JSON Schema; the answer
7
+ comes back as text and, with a schema, as an object in `json`. Each answer counts as 1 ORYKSA interaction. It does
8
+ not create a conversation in the business's inbox and never acts on the account. Server route: `POST /v1/client/ai`.
9
+
10
+ ## 1.1.3
11
+
12
+ - **Links you can tap, with an Open button.** The links of her replies were plain text. They can now be tapped in the chat (web and e-mail, underlined, in the accent colour on her bubbles and white on the customer's), and each web link (3 at most) gets a card under the reply with the picture and the title of the page and an **Open** button. Titles and pictures come from `client.linkPreview(url)` (new `GET /v1/client/link-preview`, kept in memory, never throws), only for the pages of the business's own site (its website chat domains); other links show the address.
13
+ - `onLink: (url) => boolean` on `OryksaChatButton`, `OryksaChat`, `OryksaChatModal` and `OryksaVoiceScreen` lets your app open its own screens (return true) instead of the system (`Linking.openURL`).
14
+ - **The voice screen shows what she says**, the short spoken version, without the addresses (7 lines at most), with the same cards under it; tapping Open closes the voice screen, then opens the link. `OryksaVoiceController.lastSpoken`; `say()` now also sets `lastReply` and `lastSpoken`.
15
+ - New exports: `OryksaLinkCard`, `linksIn`, `withoutLinks`, `openLink` (and the types `OryksaLinkHandler`, `OryksaLinkPreview`). Tests: `test/links.test.js`; the README links code is type-checked by CI (`test/readme/readme-links.tsx`).
16
+ - **Attach a photo, take one or send a document.** The chat input has an attach button (on by default, `attachments={false}` hides it on `OryksaChat`, `OryksaChatModal` and `OryksaChatButton`): the photo library, the camera (not on the web) or a document (PDF, Word, text, CSV and pictures, up to 8 MB). Photos come made smaller to 1600 pixels, JPEG 80%, up to 2 MB, and go as `image_base64` / `image_mime`; documents go as `file_base64` / `file_name` / `file_mime` on `POST /v1/client/chat`. The customer's bubble shows the photo, or the document icon and its name; a file that is too large or cannot be read shows a short note. The pickers come from two new optional peer dependencies your app installs, `react-native-image-picker` (library and camera) and `@react-native-documents/picker` (documents); each choice shows only when its library is installed, and `picker` takes your own adapter (Expo). `send(message, { image, file })`, `createDefaultPicker`, `photoAttachment`, `documentAttachment`, `MAX_PHOTO_BYTES`, `MAX_FILE_BYTES`. Tests: `test/attach.test.js`; the README attach code is type-checked (`test/readme/readme-attach.tsx`).
17
+
3
18
  ## 1.1.2
4
19
 
5
20
  - Documentation only, no change in the SDK behaviour: README in English, Portuguese, Spanish and French, with real screenshots of the SDK components (CI, React Native for Web, ORYKSA test account), an animated overview of the ORYKSA SDKs and a *Colours and theme* section. The README theme code is type-checked by CI (`test/readme`).
package/README.es.md CHANGED
@@ -49,6 +49,68 @@ Cada respuesta de la IA tiene un botón de copiar debajo. Usa `@react-native-cli
49
49
  appContext={() => ({ screen: "product", title: "Sky Beginner Snowboard, 489.95" })} />
50
50
  ```
51
51
 
52
+ ## Enlaces con botón Abrir (1.1.3)
53
+
54
+ Los enlaces que da la IA se pueden tocar en el chat (direcciones web y e-mails), y cada enlace web tiene una tarjeta debajo de su respuesta con la foto y el título de la página y un botón **Abrir** (las páginas de tu propio sitio, los dominios de tu chat web; otros sitios muestran la dirección). La pantalla de voz muestra lo que ella dice sin las direcciones, con las mismas tarjetas; tocar Abrir cierra la pantalla de voz. Pasa `onLink` para abrir tus propias pantallas, por ejemplo la página del producto: devuelve true cuando tu app lo abrió, false para que lo abra el sistema (`Linking.openURL`).
55
+
56
+ ```jsx
57
+ <OryksaChatButton client={client} lang="es"
58
+ onLink={(url) => {
59
+ const id = myProductIdOf(url); // tus rutas
60
+ if (!id) return false;
61
+ navigation.navigate("Product", { id });
62
+ return true;
63
+ }} />
64
+ ```
65
+
66
+ `onLink` también está en `OryksaChat`, `OryksaChatModal` y `OryksaVoiceScreen`.
67
+
68
+ ## Adjuntar una foto o un documento (1.1.3)
69
+
70
+ El chat tiene un botón de adjuntar (un clip, activado por defecto): una foto de la galería, una foto de la cámara o un documento (PDF, Word, texto, CSV, hasta 8 MB). La IA mira la foto (reducida a 1600 píxeles, JPEG 80%, hasta 2 MB) y lee el documento; la burbuja del cliente muestra la foto, o el icono del documento y su nombre. `attachments={false}` oculta el botón.
71
+
72
+ React Native no tiene selector propio, así que el SDK usa dos bibliotecas opcionales que tu app instala (dependencias peer opcionales, como las de la voz). Cada opción solo aparece cuando su biblioteca está; sin las dos, el botón se oculta:
73
+
74
+ ```sh
75
+ npm install react-native-image-picker @react-native-documents/picker
76
+ ```
77
+
78
+ * `react-native-image-picker`: galería y cámara. Info.plist de iOS: `NSPhotoLibraryUsageDescription` y `NSCameraUsageDescription`. Android: ningún permiso para la galería; para la cámara, `CAMERA` solo hace falta si tu app lo declara en su propio manifest (entonces concédelo antes de abrir la cámara).
79
+ * `@react-native-documents/picker`: documentos, en el selector de documentos del sistema (sin permiso). Los documentos se leen con `react-native-fs` cuando está instalado, si no con `fetch`.
80
+
81
+ ```jsx
82
+ <OryksaChatButton client={client} lang="es" attachments={false} /> {/* sin botón de adjuntar */}
83
+
84
+ // Expo u otra stack: tus propios selectores (si falta uno, se oculta esa opción)
85
+ const picker = {
86
+ pickPhoto: () => myPhotoPicker(), // { base64, mime } reducida (1600 px, JPEG 80%), o null
87
+ pickDocument: () => myDocumentPicker(), // { base64, name, mime }, o null
88
+ };
89
+ <OryksaChat client={client} picker={picker} />
90
+ ```
91
+
92
+ Sin la interfaz: `client.send("Aquí está el presupuesto", { file: { base64, name: "presupuesto.pdf", mime: "application/pdf" } })` (fotos: `{ image: { base64, mime } }`).
93
+
94
+ ## Tus funciones de IA: `ai` (1.1.4)
95
+
96
+ Haz que la IA haga lo que tu app necesita (leer una foto, extraer datos, clasificar, traducir, escribir) y paga con interacciones ORYKSA: 1 por respuesta. Tu prompt y tu JSON Schema viven en tu app; basta el token de sesión (nunca lleves la clave secreta). No crea una conversación en tu bandeja.
97
+
98
+ ```js
99
+ const r = await client.ai({
100
+ instructions: "Read the tyre on the photo: brand, model and size exactly as written on the sidewall. " +
101
+ "If the size is not legible, readable=false and size=null. Never invent.",
102
+ images: [{ base64: photoBase64, mime: "image/jpeg" }], // resized to 1600 px
103
+ schema: {
104
+ type: "object",
105
+ properties: { readable: { type: "boolean" }, brand: { type: ["string", "null"] }, size: { type: ["string", "null"] } },
106
+ required: ["readable", "brand", "size"],
107
+ },
108
+ });
109
+ if (r.json?.readable) showProducts(r.json.size);
110
+ ```
111
+
112
+ `r.text` trae la respuesta en texto, `r.json` el objeto (con schema) y `r.interactionsSpent` las interacciones usadas. Cuando se agotan las interacciones del plan, la llamada lanza `OryksaError` con el status 402.
113
+
52
114
  ## Cómo funciona (y por qué la clave está segura)
53
115
 
54
116
  1. Tu **servidor** guarda la clave secreta de la API (`oryk_live_...`, creada en developer.oryksa.com) y crea un **token de sesión** de corta duración para cada usuario que ha iniciado sesión, con `POST /v1/sessions` (con `@oryksa/sdk` en Node: `oryksa.createSession(...)`).
package/README.fr.md CHANGED
@@ -49,6 +49,68 @@ Chaque réponse de l'IA a un bouton copier en dessous. Il utilise `@react-native
49
49
  appContext={() => ({ screen: "product", title: "Sky Beginner Snowboard, 489.95" })} />
50
50
  ```
51
51
 
52
+ ## Liens avec un bouton pour les ouvrir (1.1.3)
53
+
54
+ Les liens que donne l'IA se touchent dans le chat (adresses web et e-mails), et chaque lien web a une carte sous sa réponse avec la photo et le titre de la page et un bouton pour l'ouvrir (les pages de votre propre site, les domaines de votre chat web ; les autres sites montrent l'adresse). L'écran vocal montre ce qu'elle dit sans les adresses, avec les mêmes cartes ; toucher le bouton ferme l'écran vocal. Passez `onLink` pour ouvrir vos propres écrans, par exemple la page du produit : renvoyez true quand votre app l'a ouvert, false pour laisser le système l'ouvrir (`Linking.openURL`).
55
+
56
+ ```jsx
57
+ <OryksaChatButton client={client} lang="en"
58
+ onLink={(url) => {
59
+ const id = myProductIdOf(url); // vos routes
60
+ if (!id) return false;
61
+ navigation.navigate("Product", { id });
62
+ return true;
63
+ }} />
64
+ ```
65
+
66
+ `onLink` existe aussi sur `OryksaChat`, `OryksaChatModal` et `OryksaVoiceScreen`.
67
+
68
+ ## Joindre une photo ou un document (1.1.3)
69
+
70
+ Le chat a un bouton pour joindre (un trombone, activé par défaut) : une photo de la galerie, une photo de l'appareil photo ou un document (PDF, Word, texte, CSV, jusqu'à 8 Mo). L'IA regarde la photo (réduite à 1600 pixels, JPEG 80 %, jusqu'à 2 Mo) et lit le document ; la bulle du client montre la photo, ou l'icône du document et son nom. `attachments={false}` masque le bouton.
71
+
72
+ React Native n'a pas de sélecteur propre, donc le SDK utilise deux bibliothèques optionnelles que votre app installe (dépendances peer optionnelles, comme celles de la voix). Chaque option n'apparaît que lorsque sa bibliothèque est là ; sans les deux, le bouton est masqué :
73
+
74
+ ```sh
75
+ npm install react-native-image-picker @react-native-documents/picker
76
+ ```
77
+
78
+ * `react-native-image-picker` : galerie et appareil photo. Info.plist iOS : `NSPhotoLibraryUsageDescription` et `NSCameraUsageDescription`. Android : aucune autorisation pour la galerie ; pour l'appareil photo, `CAMERA` n'est nécessaire que si votre app la déclare dans son propre manifest (accordez-la alors avant d'ouvrir l'appareil photo).
79
+ * `@react-native-documents/picker` : documents, dans le sélecteur de documents du système (sans autorisation). Les documents sont lus avec `react-native-fs` quand il est installé, sinon avec `fetch`.
80
+
81
+ ```jsx
82
+ <OryksaChatButton client={client} lang="en" attachments={false} /> {/* sans bouton pour joindre */}
83
+
84
+ // Expo ou une autre stack : vos propres sélecteurs (s'il en manque un, ce choix est masqué)
85
+ const picker = {
86
+ pickPhoto: () => myPhotoPicker(), // { base64, mime } réduite (1600 px, JPEG 80 %), ou null
87
+ pickDocument: () => myDocumentPicker(), // { base64, name, mime }, ou null
88
+ };
89
+ <OryksaChat client={client} picker={picker} />
90
+ ```
91
+
92
+ Sans l'interface : `client.send("Voici le devis", { file: { base64, name: "devis.pdf", mime: "application/pdf" } })` (photos : `{ image: { base64, mime } }`).
93
+
94
+ ## Vos propres fonctions d'IA : `ai` (1.1.4)
95
+
96
+ Faites faire à l'IA ce dont votre app a besoin (lire une photo, extraire des données, classer, traduire, rédiger) et payez en interactions ORYKSA : 1 par réponse. Votre prompt et votre JSON Schema restent dans votre app ; le jeton de session suffit (n'embarquez jamais la clé secrète). Aucune conversation n'est créée dans votre boîte.
97
+
98
+ ```js
99
+ const r = await client.ai({
100
+ instructions: "Read the tyre on the photo: brand, model and size exactly as written on the sidewall. " +
101
+ "If the size is not legible, readable=false and size=null. Never invent.",
102
+ images: [{ base64: photoBase64, mime: "image/jpeg" }], // resized to 1600 px
103
+ schema: {
104
+ type: "object",
105
+ properties: { readable: { type: "boolean" }, brand: { type: ["string", "null"] }, size: { type: ["string", "null"] } },
106
+ required: ["readable", "brand", "size"],
107
+ },
108
+ });
109
+ if (r.json?.readable) showProducts(r.json.size);
110
+ ```
111
+
112
+ `r.text` contient la réponse en texte, `r.json` l'objet (avec un schema) et `r.interactionsSpent` les interactions utilisées. Quand les interactions du forfait sont épuisées, l'appel lève une `OryksaError` avec le status 402.
113
+
52
114
  ## Comment ça marche (et pourquoi la clé reste en sécurité)
53
115
 
54
116
  1. Votre **serveur** garde la clé API secrète (`oryk_live_...`, créée sur developer.oryksa.com) et crée un **jeton de session** de courte durée pour chaque utilisateur connecté avec `POST /v1/sessions` (avec `@oryksa/sdk` sur Node : `oryksa.createSession(...)`).
package/README.md CHANGED
@@ -49,6 +49,68 @@ Each reply of the AI has a copy button under it. It uses `@react-native-clipboar
49
49
  appContext={() => ({ screen: "product", title: "Sky Beginner Snowboard, 489.95" })} />
50
50
  ```
51
51
 
52
+ ## Links with an Open button (1.1.3)
53
+
54
+ The links the AI gives can be tapped in the chat (web addresses and e-mails), and each web link gets a card under her reply with the picture and title of the page and an **Open** button (the pages of your own site, the domains of your website chat; other sites show the address). The voice screen shows what she says without the addresses, with the same cards; tapping Open closes the voice screen. Pass `onLink` to open your own screens, for example your product page: return true when your app opened it, false to let the system open it (`Linking.openURL`).
55
+
56
+ ```jsx
57
+ <OryksaChatButton client={client} lang="en"
58
+ onLink={(url) => {
59
+ const id = myProductIdOf(url); // your routes
60
+ if (!id) return false;
61
+ navigation.navigate("Product", { id });
62
+ return true;
63
+ }} />
64
+ ```
65
+
66
+ `onLink` is also on `OryksaChat`, `OryksaChatModal` and `OryksaVoiceScreen`.
67
+
68
+ ## Attach a photo or a document (1.1.3)
69
+
70
+ The chat has an attach button (a paperclip, on by default): a photo from the library, a photo from the camera, or a document (PDF, Word, text, CSV, up to 8 MB). The AI looks at the photo (made smaller to 1600 pixels, JPEG 80%, up to 2 MB) and reads the document; the customer's bubble shows the photo, or the document icon and its name. `attachments={false}` hides the button.
71
+
72
+ React Native has no picker of its own, so the SDK uses two optional libraries your app installs (optional peer dependencies, like the voice libraries). Each choice shows only when its library is there; without both, the button is hidden:
73
+
74
+ ```sh
75
+ npm install react-native-image-picker @react-native-documents/picker
76
+ ```
77
+
78
+ * `react-native-image-picker`: photo library and camera. iOS Info.plist: `NSPhotoLibraryUsageDescription` and `NSCameraUsageDescription`. Android: no permission for the photo library; for the camera, `CAMERA` is needed only if your app declares it in its own manifest (then grant it before the camera opens).
79
+ * `@react-native-documents/picker`: documents, in the system document picker (no permission). Documents are read with `react-native-fs` when installed, else with `fetch`.
80
+
81
+ ```jsx
82
+ <OryksaChatButton client={client} lang="en" attachments={false} /> {/* no attach button */}
83
+
84
+ // Expo or another stack: your own pickers (a missing one hides that choice)
85
+ const picker = {
86
+ pickPhoto: () => myPhotoPicker(), // { base64, mime } made smaller (1600 px, JPEG 80%), or null
87
+ pickDocument: () => myDocumentPicker(), // { base64, name, mime }, or null
88
+ };
89
+ <OryksaChat client={client} picker={picker} />
90
+ ```
91
+
92
+ Without the UI: `client.send("Here is the quote", { file: { base64, name: "quote.pdf", mime: "application/pdf" } })` (photos: `{ image: { base64, mime } }`).
93
+
94
+ ## Your own AI features: `ai` (1.1.4)
95
+
96
+ Have the AI do what your app needs (read a photo, extract data, classify, translate, write) and pay with ORYKSA interactions: 1 per answer. Your prompt and your JSON Schema live in your app; the session token is enough (never ship the secret key). It does not create a conversation in your inbox.
97
+
98
+ ```js
99
+ const r = await client.ai({
100
+ instructions: "Read the tyre on the photo: brand, model and size exactly as written on the sidewall. " +
101
+ "If the size is not legible, readable=false and size=null. Never invent.",
102
+ images: [{ base64: photoBase64, mime: "image/jpeg" }], // resized to 1600 px
103
+ schema: {
104
+ type: "object",
105
+ properties: { readable: { type: "boolean" }, brand: { type: ["string", "null"] }, size: { type: ["string", "null"] } },
106
+ required: ["readable", "brand", "size"],
107
+ },
108
+ });
109
+ if (r.json?.readable) showProducts(r.json.size);
110
+ ```
111
+
112
+ `r.text` has the answer as text, `r.json` the object (with a schema), `r.interactionsSpent` the interactions used. When the plan's interactions are used up the call throws `OryksaError` with status 402.
113
+
52
114
  ## How it works (and why the key stays safe)
53
115
 
54
116
  1. Your **server** keeps the secret API key (`oryk_live_...`, created at developer.oryksa.com) and creates a short-lived **session token** for each signed-in user with `POST /v1/sessions` (with `@oryksa/sdk` on Node: `oryksa.createSession(...)`).
package/README.pt.md CHANGED
@@ -49,6 +49,68 @@ Cada resposta da IA tem um botão de copiar por baixo. Usa `@react-native-clipbo
49
49
  appContext={() => ({ screen: "product", title: "Sky Beginner Snowboard, 489.95" })} />
50
50
  ```
51
51
 
52
+ ## Links com botão Abrir (1.1.3)
53
+
54
+ Os links que a IA dá ficam clicáveis no chat (endereços web e e-mails), e cada link web ganha um cartão por baixo da resposta com a foto e o título da página e um botão **Abrir** (as páginas do teu próprio site, os domínios do teu chat do site; outros sites mostram o endereço). O ecrã de voz mostra o que ela diz sem os endereços, com os mesmos cartões; tocar em Abrir fecha o ecrã de voz. Passa `onLink` para abrir os teus próprios ecrãs, por exemplo a página do produto: devolve true quando a tua app o abriu, false para o sistema o abrir (`Linking.openURL`).
55
+
56
+ ```jsx
57
+ <OryksaChatButton client={client} lang="pt"
58
+ onLink={(url) => {
59
+ const id = myProductIdOf(url); // as tuas rotas
60
+ if (!id) return false;
61
+ navigation.navigate("Product", { id });
62
+ return true;
63
+ }} />
64
+ ```
65
+
66
+ `onLink` também existe em `OryksaChat`, `OryksaChatModal` e `OryksaVoiceScreen`.
67
+
68
+ ## Anexar uma foto ou um documento (1.1.3)
69
+
70
+ O chat tem um botão de anexar (um clipe, ligado por omissão): uma foto da galeria, uma foto da câmara ou um documento (PDF, Word, texto, CSV, até 8 MB). A IA vê a foto (reduzida a 1600 píxeis, JPEG 80%, até 2 MB) e lê o documento; o balão do cliente mostra a foto, ou o ícone do documento e o nome. `attachments={false}` esconde o botão.
71
+
72
+ O React Native não tem seletor próprio, por isso o SDK usa duas bibliotecas opcionais que a tua app instala (dependências peer opcionais, como as da voz). Cada opção só aparece quando a sua biblioteca existe; sem as duas, o botão fica escondido:
73
+
74
+ ```sh
75
+ npm install react-native-image-picker @react-native-documents/picker
76
+ ```
77
+
78
+ * `react-native-image-picker`: galeria e câmara. Info.plist do iOS: `NSPhotoLibraryUsageDescription` e `NSCameraUsageDescription`. Android: nenhuma permissão para a galeria; para a câmara, `CAMERA` só é precisa se a tua app a declarar no seu próprio manifest (então concede-a antes de a câmara abrir).
79
+ * `@react-native-documents/picker`: documentos, no seletor de documentos do sistema (sem permissão). Os documentos são lidos com `react-native-fs` quando está instalado, senão com `fetch`.
80
+
81
+ ```jsx
82
+ <OryksaChatButton client={client} lang="pt" attachments={false} /> {/* sem botão de anexar */}
83
+
84
+ // Expo ou outra stack: os teus próprios seletores (um que falte esconde essa opção)
85
+ const picker = {
86
+ pickPhoto: () => myPhotoPicker(), // { base64, mime } reduzida (1600 px, JPEG 80%), ou null
87
+ pickDocument: () => myDocumentPicker(), // { base64, name, mime }, ou null
88
+ };
89
+ <OryksaChat client={client} picker={picker} />
90
+ ```
91
+
92
+ Sem a interface: `client.send("Aqui está o orçamento", { file: { base64, name: "orcamento.pdf", mime: "application/pdf" } })` (fotos: `{ image: { base64, mime } }`).
93
+
94
+ ## As tuas funções de IA: `ai` (1.1.4)
95
+
96
+ Põe a IA a fazer o que a tua app precisa (ler uma foto, extrair dados, classificar, traduzir, escrever) e paga com interações ORYKSA: 1 por resposta. O teu prompt e o teu JSON Schema ficam na tua app; basta o token de sessão (nunca leves a chave secreta). Não cria conversa na tua caixa.
97
+
98
+ ```js
99
+ const r = await client.ai({
100
+ instructions: "Read the tyre on the photo: brand, model and size exactly as written on the sidewall. " +
101
+ "If the size is not legible, readable=false and size=null. Never invent.",
102
+ images: [{ base64: photoBase64, mime: "image/jpeg" }], // resized to 1600 px
103
+ schema: {
104
+ type: "object",
105
+ properties: { readable: { type: "boolean" }, brand: { type: ["string", "null"] }, size: { type: ["string", "null"] } },
106
+ required: ["readable", "brand", "size"],
107
+ },
108
+ });
109
+ if (r.json?.readable) showProducts(r.json.size);
110
+ ```
111
+
112
+ `r.text` tem a resposta em texto, `r.json` o objeto (com schema) e `r.interactionsSpent` as interações gastas. Quando as interações do plano acabam, a chamada lança `OryksaError` com o status 402.
113
+
52
114
  ## Como funciona (e porque é que a chave fica segura)
53
115
 
54
116
  1. O teu **servidor** guarda a chave secreta da API (`oryk_live_...`, criada em developer.oryksa.com) e cria um **token de sessão** de curta duração para cada utilizador com sessão iniciada, com `POST /v1/sessions` (com `@oryksa/sdk` em Node: `oryksa.createSession(...)`).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oryksa/react-native",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "Official React Native SDK for ORYKSA AI Employees: a ready in-app AI chat and voice (the same as the ORYKSA app), and a client for the ORYKSA API v1.",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
@@ -13,7 +13,7 @@
13
13
  ],
14
14
  "sideEffects": false,
15
15
  "scripts": {
16
- "test": "node test/client.test.js && node test/vad.test.js && node test/voice.test.js && node test/profanity.test.js"
16
+ "test": "node test/client.test.js && node test/vad.test.js && node test/voice.test.js && node test/profanity.test.js && node test/links.test.js && node test/attach.test.js && node test/ai.test.js"
17
17
  },
18
18
  "peerDependencies": {
19
19
  "react": ">=17.0.0",
@@ -21,7 +21,9 @@
21
21
  "react-native-live-audio-stream": ">=1.1.0",
22
22
  "react-native-sound": ">=0.11.0",
23
23
  "react-native-fs": ">=2.18.0",
24
- "@react-native-clipboard/clipboard": ">=1.11.0"
24
+ "@react-native-clipboard/clipboard": ">=1.11.0",
25
+ "react-native-image-picker": ">=7.0.0",
26
+ "@react-native-documents/picker": ">=10.0.0"
25
27
  },
26
28
  "keywords": [
27
29
  "oryksa",
@@ -56,6 +58,12 @@
56
58
  },
57
59
  "@react-native-clipboard/clipboard": {
58
60
  "optional": true
61
+ },
62
+ "react-native-image-picker": {
63
+ "optional": true
64
+ },
65
+ "@react-native-documents/picker": {
66
+ "optional": true
59
67
  }
60
68
  }
61
69
  }
package/src/attach.js ADDED
@@ -0,0 +1,133 @@
1
+ /*!
2
+ * @oryksa/react-native - attachments in the chat: a photo (the AI looks at it) or a document (the AI
3
+ * reads it). The pickers come from a small adapter (see createDefaultPicker), like the audio.
4
+ * License: MIT
5
+ */
6
+ "use strict";
7
+
8
+ /** The largest photo the chat sends (after it is made smaller): 2 MB. */
9
+ const MAX_PHOTO_BYTES = 2 * 1024 * 1024;
10
+ /** The largest document the chat sends: 8 MB. */
11
+ const MAX_FILE_BYTES = 8 * 1024 * 1024;
12
+ /** The longest side of a photo the chat sends, in pixels, and its JPEG quality. */
13
+ const PHOTO_MAX_SIDE = 1600;
14
+ const PHOTO_QUALITY = 0.8;
15
+
16
+ const DOC_MIME = {
17
+ pdf: "application/pdf",
18
+ doc: "application/msword",
19
+ docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
20
+ txt: "text/plain",
21
+ csv: "text/csv",
22
+ jpg: "image/jpeg",
23
+ jpeg: "image/jpeg",
24
+ png: "image/png",
25
+ webp: "image/webp",
26
+ };
27
+
28
+ const extOf = (name) => { const m = /\.([^./\\]+)$/.exec(String(name || "")); return m ? m[1].toLowerCase() : ""; };
29
+
30
+ /** The type of a file from its name ("application/octet-stream" when it is not a known one). */
31
+ function mimeOf(name) { return DOC_MIME[extOf(name)] || "application/octet-stream"; }
32
+
33
+ /** How many bytes a base64 text holds. */
34
+ function base64Bytes(b64) {
35
+ const s = String(b64 || "").replace(/\s+/g, "");
36
+ const pad = s.endsWith("==") ? 2 : s.endsWith("=") ? 1 : 0;
37
+ return Math.max(0, Math.floor((s.length * 3) / 4) - pad);
38
+ }
39
+
40
+ /**
41
+ * A photo from the picker ({ base64, mime }) as what the chat sends: { image } or { error: "big" }
42
+ * (larger than 2 MB) or { error: "failed" } (nothing could be read).
43
+ */
44
+ function photoAttachment(p) {
45
+ if (!p || typeof p.base64 !== "string" || !p.base64) return { error: "failed" };
46
+ if (base64Bytes(p.base64) > MAX_PHOTO_BYTES) return { error: "big" };
47
+ return { image: { base64: p.base64, mime: p.mime || "image/jpeg" } };
48
+ }
49
+
50
+ /**
51
+ * A document from the picker ({ base64, name, mime } or { tooBig: true }) as what the chat sends:
52
+ * pictures up to 2 MB go as { image } (the AI looks at it), the rest as { file } up to 8 MB;
53
+ * { error: "big" } or { error: "failed" } otherwise.
54
+ */
55
+ function documentAttachment(d) {
56
+ if (!d) return { error: "failed" };
57
+ if (d.tooBig) return { error: "big" };
58
+ if (typeof d.base64 !== "string") return { error: "failed" };
59
+ const size = base64Bytes(d.base64);
60
+ if (size > MAX_FILE_BYTES) return { error: "big" };
61
+ const name = d.name || "file";
62
+ const mime = DOC_MIME[extOf(name)] || d.mime || "application/octet-stream";
63
+ if (mime.startsWith("image/") && size <= MAX_PHOTO_BYTES) return { image: { base64: d.base64, mime } };
64
+ return { file: { base64: d.base64, name, mime } };
65
+ }
66
+
67
+ /** Reads a local file (file:// or content://) as base64: react-native-fs when installed, else fetch + FileReader. */
68
+ async function readBase64(uri) {
69
+ try {
70
+ const RNFS = require("react-native-fs");
71
+ const fs = RNFS.default || RNFS;
72
+ if (fs && fs.readFile) {
73
+ const path = String(uri).startsWith("file://") ? decodeURIComponent(String(uri).slice(7)) : String(uri);
74
+ return await fs.readFile(path, "base64");
75
+ }
76
+ } catch (_) { /* no react-native-fs: read it with fetch */ }
77
+ const blob = await (await fetch(uri)).blob();
78
+ return await new Promise((ok, fail) => {
79
+ const r = new FileReader();
80
+ r.onload = () => ok(String(r.result || "").replace(/^data:[^,]*,/, ""));
81
+ r.onerror = () => fail(r.error || new Error("read failed"));
82
+ r.readAsDataURL(blob);
83
+ });
84
+ }
85
+
86
+ /**
87
+ * The default picker adapter, from two optional libraries your app installs:
88
+ * react-native-image-picker photo library and camera (made smaller to 1600 px, JPEG 80%)
89
+ * @react-native-documents/picker documents (PDF, Word, text, CSV, pictures)
90
+ * Each choice shows only when its library is installed; null when neither is (no attach button).
91
+ * Expo or another stack: pass your own { pickPhoto, takePhoto, pickDocument } as `picker`.
92
+ */
93
+ function createDefaultPicker() {
94
+ let IP = null;
95
+ let DP = null;
96
+ try { IP = require("react-native-image-picker"); } catch (_) { IP = null; }
97
+ try { DP = require("@react-native-documents/picker"); } catch (_) { DP = null; }
98
+ if (!IP && !DP) return null;
99
+ const photo = async (launch) => {
100
+ const r = await launch({ mediaType: "photo", maxWidth: PHOTO_MAX_SIDE, maxHeight: PHOTO_MAX_SIDE, quality: PHOTO_QUALITY,
101
+ includeBase64: true, selectionLimit: 1, saveToPhotos: false });
102
+ if (!r || r.didCancel) return null;
103
+ if (r.errorCode) throw new Error(r.errorMessage || r.errorCode);
104
+ const a = r.assets && r.assets[0];
105
+ if (!a || !a.base64) throw new Error("no picture");
106
+ return { base64: a.base64, mime: a.type || "image/jpeg", name: a.fileName || "photo.jpg" };
107
+ };
108
+ const pickDocument = async () => {
109
+ const t = DP.types || {};
110
+ let res;
111
+ try {
112
+ [res] = await DP.pick({ type: [t.pdf, t.doc, t.docx, t.plainText, t.csv, t.images].filter(Boolean) });
113
+ } catch (e) {
114
+ const cancelled = DP.isErrorWithCode && DP.errorCodes && DP.isErrorWithCode(e) && e.code === DP.errorCodes.OPERATION_CANCELED;
115
+ if (cancelled) return null;
116
+ throw e;
117
+ }
118
+ if (!res) return null;
119
+ const name = res.name || "file";
120
+ if (res.size && res.size > MAX_FILE_BYTES) return { tooBig: true, name };
121
+ return { base64: await readBase64(res.uri), name, mime: res.type || mimeOf(name) };
122
+ };
123
+ return {
124
+ pickPhoto: IP ? () => photo(IP.launchImageLibrary) : null,
125
+ takePhoto: IP ? () => photo(IP.launchCamera) : null,
126
+ pickDocument: DP ? pickDocument : null,
127
+ };
128
+ }
129
+
130
+ module.exports = {
131
+ MAX_PHOTO_BYTES, MAX_FILE_BYTES, PHOTO_MAX_SIDE, PHOTO_QUALITY,
132
+ mimeOf, base64Bytes, photoAttachment, documentAttachment, readBase64, createDefaultPicker,
133
+ };