@oryksa/react-native 1.1.2 → 1.1.3
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 +37 -29
- package/README.es.md +161 -119
- package/README.fr.md +161 -119
- package/README.md +161 -119
- package/README.pt.md +161 -119
- package/package.json +69 -61
- package/src/attach.js +133 -0
- package/src/chat.js +102 -21
- package/src/client.js +267 -233
- package/src/index.d.ts +86 -0
- package/src/index.js +12 -0
- package/src/link-card.js +74 -0
- package/src/links.js +115 -0
- package/src/voice-screen.js +15 -8
- package/src/voice.js +9 -2
package/README.pt.md
CHANGED
|
@@ -1,119 +1,161 @@
|
|
|
1
|
-
<p align="center"><img src="https://app.oryksa.com/static/oryksa_logo.png" width="300" alt="ORYKSA"></p>
|
|
2
|
-
|
|
3
|
-
# @oryksa/react-native
|
|
4
|
-
|
|
5
|
-
[](README.md) [](README.pt.md) [](README.es.md) [](README.fr.md)
|
|
6
|
-
|
|
7
|
-
<p align="center"><a href="https://developer.oryksa.com/pt/sdks"><img src="https://developer.oryksa.com/assets/img/sdk/oryksa-sdks-pt.gif" width="760" alt="SDKs ORYKSA: o chat e a voz da IA no iOS, Android, Flutter, React Native e na web"></a></p>
|
|
8
|
-
|
|
9
|
-
SDK oficial de React Native da **ORYKSA AI Employees**. Põe dentro da tua app iOS e Android (React Native ou Expo) um colaborador de IA que já conhece o teu negócio: um chat pronto com o mesmo aspeto do chat do site da ORYKSA, o nome e a foto da tua IA vindos da tua conta ORYKSA, em inglês, português e espanhol.
|
|
10
|
-
|
|
11
|
-
* Documentação: https://developer.oryksa.com/pt/sdks
|
|
12
|
-
* API: `https://api.oryksa.com/v1`
|
|
13
|
-
* Sem código nativo: funciona no Expo Go.
|
|
14
|
-
|
|
15
|
-
## Capturas de ecrã
|
|
16
|
-
|
|
17
|
-
<p align="center"><img src="https://raw.githubusercontent.com/oryksa/oryksa-react-native/main/docs/screenshots/rn_product_pt.png" width="230" alt="Página de produto com o botão do chat"> <img src="https://raw.githubusercontent.com/oryksa/oryksa-react-native/main/docs/screenshots/rn_chat_pt.png" width="230" alt="Chat"> <img src="https://raw.githubusercontent.com/oryksa/oryksa-react-native/main/docs/screenshots/rn_theme_pt.png" width="230" alt="Chat com o tema do README"></p>
|
|
18
|
-
|
|
19
|
-
Os próprios componentes do SDK, desenhados pelo CI no Chrome em tamanho de telemóvel com React Native for Web e uma conta de teste da ORYKSA (`test/web/`). No telemóvel o chat mostra também o microfone, que precisa das bibliotecas de áudio nativas. O chat verde usa o código de *Cores e tema*, mais abaixo.
|
|
20
|
-
|
|
21
|
-
## Instalar
|
|
22
|
-
|
|
23
|
-
```bash
|
|
24
|
-
npm install @oryksa/react-native
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
## Para que serve
|
|
28
|
-
|
|
29
|
-
O SDK põe o chat e a voz da ORYKSA dentro da tua app para atender os **clientes do teu negócio**: eles perguntam, compram e marcam. A IA responde com o teu Brain, os teus produtos e serviços, com o nome, a foto e a voz que escolheste em *A tua IA*, e guarda leads, marcações e vendas nas Atividades da tua conta ORYKSA. **Não** é uma interface para o dono: ninguém altera definições, lê Atividades ou dá ordens à ORYKSA através do SDK. Mais: https://help.oryksa.com/pt/a/sdks-what-they-do
|
|
30
|
-
|
|
31
|
-
Os palavrões que o cliente escreve aparecem como asteriscos no chat (a mesma lista de todos os chats da ORYKSA, carregada de `https://app.oryksa.com/widget/profanity.json` e guardada durante 24 horas). As respostas da IA já vêm filtradas do servidor.
|
|
32
|
-
|
|
33
|
-
## Voz e contexto da app (1.1)
|
|
34
|
-
|
|
35
|
-
O chat mostra um microfone quando o teu plano tem voz. Abre o ecrã de voz com o mesmo comportamento da app ORYKSA: o microfone fica aberto enquanto ela fala e só uma voz humana a interrompe (escrever, a TV e o eco dela própria não); ela para na palavra e o texto fica no chat; um sussurro recebe uma resposta sussurrada; se a voz dela falhar, aparece o texto (nunca uma voz de robô).
|
|
36
|
-
|
|
37
|
-
A voz precisa de três bibliotecas nativas (dependências peer opcionais) e da permissão do microfone (Android `RECORD_AUDIO`, iOS `NSMicrophoneUsageDescription`):
|
|
38
|
-
|
|
39
|
-
```sh
|
|
40
|
-
npm install react-native-live-audio-stream react-native-sound react-native-fs
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
Sem elas, o chat funciona e o microfone fica escondido. Expo ou outra stack de áudio: passa o teu próprio adaptador `audio` (`startMic`, `stopMic`, `play`, `stopPlayback`).
|
|
44
|
-
|
|
45
|
-
Cada resposta da IA tem um botão de copiar por baixo. Usa `@react-native-clipboard/clipboard` quando está instalado (dependência peer opcional: `npm install @react-native-clipboard/clipboard`); as versões mais antigas do React Native usam o `Clipboard` integrado.
|
|
46
|
-
|
|
47
|
-
```jsx
|
|
48
|
-
<OryksaChatButton client={client} lang="pt"
|
|
49
|
-
appContext={() => ({ screen: "product", title: "Sky Beginner Snowboard, 489.95" })} />
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
##
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
*
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
1
|
+
<p align="center"><img src="https://app.oryksa.com/static/oryksa_logo.png" width="300" alt="ORYKSA"></p>
|
|
2
|
+
|
|
3
|
+
# @oryksa/react-native
|
|
4
|
+
|
|
5
|
+
[](README.md) [](README.pt.md) [](README.es.md) [](README.fr.md)
|
|
6
|
+
|
|
7
|
+
<p align="center"><a href="https://developer.oryksa.com/pt/sdks"><img src="https://developer.oryksa.com/assets/img/sdk/oryksa-sdks-pt.gif" width="760" alt="SDKs ORYKSA: o chat e a voz da IA no iOS, Android, Flutter, React Native e na web"></a></p>
|
|
8
|
+
|
|
9
|
+
SDK oficial de React Native da **ORYKSA AI Employees**. Põe dentro da tua app iOS e Android (React Native ou Expo) um colaborador de IA que já conhece o teu negócio: um chat pronto com o mesmo aspeto do chat do site da ORYKSA, o nome e a foto da tua IA vindos da tua conta ORYKSA, em inglês, português e espanhol.
|
|
10
|
+
|
|
11
|
+
* Documentação: https://developer.oryksa.com/pt/sdks
|
|
12
|
+
* API: `https://api.oryksa.com/v1`
|
|
13
|
+
* Sem código nativo: funciona no Expo Go.
|
|
14
|
+
|
|
15
|
+
## Capturas de ecrã
|
|
16
|
+
|
|
17
|
+
<p align="center"><img src="https://raw.githubusercontent.com/oryksa/oryksa-react-native/main/docs/screenshots/rn_product_pt.png" width="230" alt="Página de produto com o botão do chat"> <img src="https://raw.githubusercontent.com/oryksa/oryksa-react-native/main/docs/screenshots/rn_chat_pt.png" width="230" alt="Chat"> <img src="https://raw.githubusercontent.com/oryksa/oryksa-react-native/main/docs/screenshots/rn_theme_pt.png" width="230" alt="Chat com o tema do README"></p>
|
|
18
|
+
|
|
19
|
+
Os próprios componentes do SDK, desenhados pelo CI no Chrome em tamanho de telemóvel com React Native for Web e uma conta de teste da ORYKSA (`test/web/`). No telemóvel o chat mostra também o microfone, que precisa das bibliotecas de áudio nativas. O chat verde usa o código de *Cores e tema*, mais abaixo.
|
|
20
|
+
|
|
21
|
+
## Instalar
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install @oryksa/react-native
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Para que serve
|
|
28
|
+
|
|
29
|
+
O SDK põe o chat e a voz da ORYKSA dentro da tua app para atender os **clientes do teu negócio**: eles perguntam, compram e marcam. A IA responde com o teu Brain, os teus produtos e serviços, com o nome, a foto e a voz que escolheste em *A tua IA*, e guarda leads, marcações e vendas nas Atividades da tua conta ORYKSA. **Não** é uma interface para o dono: ninguém altera definições, lê Atividades ou dá ordens à ORYKSA através do SDK. Mais: https://help.oryksa.com/pt/a/sdks-what-they-do
|
|
30
|
+
|
|
31
|
+
Os palavrões que o cliente escreve aparecem como asteriscos no chat (a mesma lista de todos os chats da ORYKSA, carregada de `https://app.oryksa.com/widget/profanity.json` e guardada durante 24 horas). As respostas da IA já vêm filtradas do servidor.
|
|
32
|
+
|
|
33
|
+
## Voz e contexto da app (1.1)
|
|
34
|
+
|
|
35
|
+
O chat mostra um microfone quando o teu plano tem voz. Abre o ecrã de voz com o mesmo comportamento da app ORYKSA: o microfone fica aberto enquanto ela fala e só uma voz humana a interrompe (escrever, a TV e o eco dela própria não); ela para na palavra e o texto fica no chat; um sussurro recebe uma resposta sussurrada; se a voz dela falhar, aparece o texto (nunca uma voz de robô).
|
|
36
|
+
|
|
37
|
+
A voz precisa de três bibliotecas nativas (dependências peer opcionais) e da permissão do microfone (Android `RECORD_AUDIO`, iOS `NSMicrophoneUsageDescription`):
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
npm install react-native-live-audio-stream react-native-sound react-native-fs
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Sem elas, o chat funciona e o microfone fica escondido. Expo ou outra stack de áudio: passa o teu próprio adaptador `audio` (`startMic`, `stopMic`, `play`, `stopPlayback`).
|
|
44
|
+
|
|
45
|
+
Cada resposta da IA tem um botão de copiar por baixo. Usa `@react-native-clipboard/clipboard` quando está instalado (dependência peer opcional: `npm install @react-native-clipboard/clipboard`); as versões mais antigas do React Native usam o `Clipboard` integrado.
|
|
46
|
+
|
|
47
|
+
```jsx
|
|
48
|
+
<OryksaChatButton client={client} lang="pt"
|
|
49
|
+
appContext={() => ({ screen: "product", title: "Sky Beginner Snowboard, 489.95" })} />
|
|
50
|
+
```
|
|
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
|
+
## Como funciona (e porque é que a chave fica segura)
|
|
95
|
+
|
|
96
|
+
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(...)`).
|
|
97
|
+
2. A tua **app** recebe só esse token de sessão (`oryk_cs_...`). A chave secreta nunca entra na app.
|
|
98
|
+
3. Cada resposta da IA conta como uma interação do teu plano ORYKSA, tal como no WhatsApp.
|
|
99
|
+
|
|
100
|
+
## Na app
|
|
101
|
+
|
|
102
|
+
```jsx
|
|
103
|
+
import { OryksaClient, OryksaChatButton } from "@oryksa/react-native";
|
|
104
|
+
|
|
105
|
+
const client = new OryksaClient({
|
|
106
|
+
getToken: async () => (await fetch("https://your-server.example/oryksa-token").then((r) => r.json())).token,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
export default function App() {
|
|
110
|
+
return (
|
|
111
|
+
<View style={{ flex: 1 }}>
|
|
112
|
+
<MyScreens />
|
|
113
|
+
<OryksaChatButton client={client} lang="en" /> {/* en, pt, br, es */}
|
|
114
|
+
</View>
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Outras peças:
|
|
120
|
+
|
|
121
|
+
* `<OryksaChatModal client={client} visible={open} onClose={() => setOpen(false)} />`: abre o chat a partir do teu próprio botão.
|
|
122
|
+
* `<OryksaChat client={client} />`: o painel dentro de qualquer ecrã.
|
|
123
|
+
* `theme={{ accent: "#5B57E0" }}`: cores.
|
|
124
|
+
* Sem a interface: `await client.agent()`, `await client.sendAndWait("Are you open on Saturday?")`, `await client.messages()`.
|
|
125
|
+
|
|
126
|
+
## Mantém a IA sincronizada com a tua app
|
|
127
|
+
|
|
128
|
+
No teu servidor, com `@oryksa/sdk`:
|
|
129
|
+
|
|
130
|
+
```js
|
|
131
|
+
await oryksa.learnApp({ name: "FitTrack", description: "...", screens: [...], faq: [...] });
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Erros
|
|
135
|
+
|
|
136
|
+
As chamadas falhadas lançam `OryksaError` com `status`, `code` (por exemplo `interaction_limit_reached`, `rate_limited`, `plan_required`) e `message`.
|
|
137
|
+
|
|
138
|
+
## Cores e tema
|
|
139
|
+
|
|
140
|
+
A prop `theme` recebe um objeto com até cinco cores em texto hexadecimal: `accent` (botões, balões do visitante, ligações), `ink` (texto principal), `soft` (balões da IA), `background` (painel) e `muted` (texto secundário). As chaves em falta mantêm o valor por omissão, por isso `{ accent: "#0E8A6B" }` basta. Funciona em `OryksaChatButton`, `OryksaChatModal` e `OryksaChat`, e os valores por omissão são exportados como `DEFAULT_THEME`.
|
|
141
|
+
|
|
142
|
+
```jsx
|
|
143
|
+
const theme = { accent: "#0E8A6B" };
|
|
144
|
+
|
|
145
|
+
<OryksaChatButton client={client} lang="en" theme={theme} />
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Não há tema escuro automático: para o modo escuro, passa as tuas próprias cores escuras.
|
|
149
|
+
|
|
150
|
+
---
|
|
151
|
+
|
|
152
|
+
## Sobre o autor
|
|
153
|
+
|
|
154
|
+
**Weslley Harakawa** - Fundador da ORYKSA AI. Em Lisboa, Portugal. Especialidades: inteligência artificial, desenvolvimento web e mobile, tokenização em blockchain. Formação: University of the People.
|
|
155
|
+
|
|
156
|
+
- LinkedIn: https://www.linkedin.com/in/weslleyharakawa/
|
|
157
|
+
- ORYKSA AI Employees: https://oryksa.com (X: https://x.com/oryksa, Instagram: https://www.instagram.com/oryksaai, YouTube: https://www.youtube.com/@ORYKSA)
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
MIT License · ORYKSA AI Employees · W8 Atlantic Unipessoal Lda
|
package/package.json
CHANGED
|
@@ -1,61 +1,69 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@oryksa/react-native",
|
|
3
|
-
"version": "1.1.
|
|
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
|
-
"main": "src/index.js",
|
|
6
|
-
"types": "src/index.d.ts",
|
|
7
|
-
"react-native": "src/index.js",
|
|
8
|
-
"files": [
|
|
9
|
-
"src",
|
|
10
|
-
"README.md",
|
|
11
|
-
"LICENSE",
|
|
12
|
-
"CHANGELOG.md"
|
|
13
|
-
],
|
|
14
|
-
"sideEffects": false,
|
|
15
|
-
"scripts": {
|
|
16
|
-
"test": "node test/client.test.js && node test/vad.test.js && node test/voice.test.js && node test/profanity.test.js"
|
|
17
|
-
},
|
|
18
|
-
"peerDependencies": {
|
|
19
|
-
"react": ">=17.0.0",
|
|
20
|
-
"react-native": ">=0.68.0",
|
|
21
|
-
"react-native-live-audio-stream": ">=1.1.0",
|
|
22
|
-
"react-native-sound": ">=0.11.0",
|
|
23
|
-
"react-native-fs": ">=2.18.0",
|
|
24
|
-
"@react-native-clipboard/clipboard": ">=1.11.0"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
"
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@oryksa/react-native",
|
|
3
|
+
"version": "1.1.3",
|
|
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
|
+
"main": "src/index.js",
|
|
6
|
+
"types": "src/index.d.ts",
|
|
7
|
+
"react-native": "src/index.js",
|
|
8
|
+
"files": [
|
|
9
|
+
"src",
|
|
10
|
+
"README.md",
|
|
11
|
+
"LICENSE",
|
|
12
|
+
"CHANGELOG.md"
|
|
13
|
+
],
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"scripts": {
|
|
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"
|
|
17
|
+
},
|
|
18
|
+
"peerDependencies": {
|
|
19
|
+
"react": ">=17.0.0",
|
|
20
|
+
"react-native": ">=0.68.0",
|
|
21
|
+
"react-native-live-audio-stream": ">=1.1.0",
|
|
22
|
+
"react-native-sound": ">=0.11.0",
|
|
23
|
+
"react-native-fs": ">=2.18.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"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"oryksa",
|
|
30
|
+
"react-native",
|
|
31
|
+
"ai",
|
|
32
|
+
"chatbot",
|
|
33
|
+
"customer-support",
|
|
34
|
+
"chat",
|
|
35
|
+
"sdk",
|
|
36
|
+
"expo",
|
|
37
|
+
"voice"
|
|
38
|
+
],
|
|
39
|
+
"homepage": "https://developer.oryksa.com/en/sdks",
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "https://github.com/oryksa/oryksa-react-native"
|
|
43
|
+
},
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/oryksa/oryksa-react-native/issues"
|
|
46
|
+
},
|
|
47
|
+
"author": "ORYKSA AI Employees (https://oryksa.com)",
|
|
48
|
+
"license": "MIT",
|
|
49
|
+
"peerDependenciesMeta": {
|
|
50
|
+
"react-native-live-audio-stream": {
|
|
51
|
+
"optional": true
|
|
52
|
+
},
|
|
53
|
+
"react-native-sound": {
|
|
54
|
+
"optional": true
|
|
55
|
+
},
|
|
56
|
+
"react-native-fs": {
|
|
57
|
+
"optional": true
|
|
58
|
+
},
|
|
59
|
+
"@react-native-clipboard/clipboard": {
|
|
60
|
+
"optional": true
|
|
61
|
+
},
|
|
62
|
+
"react-native-image-picker": {
|
|
63
|
+
"optional": true
|
|
64
|
+
},
|
|
65
|
+
"@react-native-documents/picker": {
|
|
66
|
+
"optional": true
|
|
67
|
+
}
|
|
68
|
+
}
|
|
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
|
+
};
|