@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/src/chat.js
CHANGED
|
@@ -10,37 +10,55 @@ const RN = require("react-native");
|
|
|
10
10
|
const { pick } = require("./client");
|
|
11
11
|
const { OryksaVoiceScreen, MicIcon } = require("./voice-screen");
|
|
12
12
|
const { createDefaultAudio } = require("./voice");
|
|
13
|
+
const { richParts, openLink } = require("./links");
|
|
14
|
+
const { OryksaLinkCards } = require("./link-card");
|
|
15
|
+
const { createDefaultPicker, photoAttachment, documentAttachment } = require("./attach");
|
|
13
16
|
const profanity = require("./profanity");
|
|
14
17
|
|
|
15
18
|
const h = React.createElement;
|
|
16
19
|
const { View, Text, TextInput, Image, Pressable, FlatList, Modal, KeyboardAvoidingView, Platform, StyleSheet, SafeAreaView, useWindowDimensions } = RN;
|
|
17
20
|
|
|
18
21
|
const TX = {
|
|
19
|
-
en: { talk: "Talk to", ph: "Type your question", send: "Send", err: "Sorry, something went wrong. Try again.", voice: "Talk by voice", copy: "Copy" },
|
|
20
|
-
pt: { talk: "Falar com", ph: "Escreve a tua pergunta", send: "Enviar", err: "Desculpa, algo correu mal. Tenta de novo.", voice: "Falar por voz", copy: "Copiar" },
|
|
21
|
-
br: { talk: "Falar com", ph: "Digite sua pergunta", send: "Enviar", err: "Desculpe, algo deu errado. Tente de novo.", voice: "Falar por voz", copy: "Copiar" },
|
|
22
|
-
es: { talk: "Hablar con", ph: "Escribe tu pregunta", send: "Enviar", err: "Lo siento, algo salió mal. Inténtalo de nuevo.", voice: "Hablar por voz", copy: "Copiar" },
|
|
22
|
+
en: { talk: "Talk to", ph: "Type your question", send: "Send", err: "Sorry, something went wrong. Try again.", voice: "Talk by voice", copy: "Copy", open: "Open", attach: "Attach", gallery: "Photo library", camera: "Camera", document: "Document", big: "The file is too large (max 8 MB).", attachErr: "Could not attach the file." },
|
|
23
|
+
pt: { talk: "Falar com", ph: "Escreve a tua pergunta", send: "Enviar", err: "Desculpa, algo correu mal. Tenta de novo.", voice: "Falar por voz", copy: "Copiar", open: "Abrir", attach: "Anexar", gallery: "Foto da galeria", camera: "Câmara", document: "Documento", big: "O ficheiro é demasiado grande (máx. 8 MB).", attachErr: "Não foi possível anexar o ficheiro." },
|
|
24
|
+
br: { talk: "Falar com", ph: "Digite sua pergunta", send: "Enviar", err: "Desculpe, algo deu errado. Tente de novo.", voice: "Falar por voz", copy: "Copiar", open: "Abrir", attach: "Anexar", gallery: "Foto da galeria", camera: "Câmera", document: "Documento", big: "O arquivo é grande demais (máx. 8 MB).", attachErr: "Não foi possível anexar o arquivo." },
|
|
25
|
+
es: { talk: "Hablar con", ph: "Escribe tu pregunta", send: "Enviar", err: "Lo siento, algo salió mal. Inténtalo de nuevo.", voice: "Hablar por voz", copy: "Copiar", open: "Abrir", attach: "Adjuntar", gallery: "Foto de la galería", camera: "Cámara", document: "Documento", big: "El archivo es demasiado grande (máx. 8 MB).", attachErr: "No se pudo adjuntar el archivo." },
|
|
23
26
|
};
|
|
24
27
|
const DEFAULT_THEME = { accent: "#5B57E0", ink: "#161B3D", soft: "#EEEBFB", background: "#FFFFFF", muted: "#6B7280" };
|
|
25
28
|
const FALLBACK_AVATAR = "https://oryksa.com/assets/img/avatar_official_oryksa.png";
|
|
26
29
|
const L = (lang) => (TX[lang] ? lang : "en");
|
|
27
30
|
|
|
28
|
-
/**
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Text with **bold** parts (the server marks them, the app only draws them) and links (web and
|
|
33
|
+
* e-mail) the customer can tap: underlined, in linkColor; onLink first, then the system opens them.
|
|
34
|
+
*/
|
|
35
|
+
function rich(text, style, linkColor, onLink) {
|
|
36
|
+
return richParts(text).map((p, i) => {
|
|
37
|
+
const b = p.bold ? { fontWeight: "700" } : null;
|
|
38
|
+
if (p.url) {
|
|
39
|
+
const url = p.url;
|
|
40
|
+
return h(Text, { key: i, onPress: () => openLink(url, onLink), accessibilityRole: "link",
|
|
41
|
+
style: [style, b, { color: linkColor, textDecorationLine: "underline" }] }, p.text);
|
|
42
|
+
}
|
|
43
|
+
return b ? h(Text, { key: i, style: [style, b] }, p.text) : p.text;
|
|
44
|
+
});
|
|
33
45
|
}
|
|
34
46
|
|
|
35
47
|
/**
|
|
36
48
|
* The chat panel: header with the photo and name of the AI, messages, suggestions, input and (when
|
|
37
49
|
* the plan has voice and an audio adapter is available) the microphone that opens the voice screen.
|
|
38
50
|
* Props: client, lang, theme, onClose, appContext (() => ({ screen, title, items })), voice (default
|
|
39
|
-
* true), audio (adapter; default: react-native-live-audio-stream + react-native-sound + react-native-fs)
|
|
51
|
+
* true), audio (adapter; default: react-native-live-audio-stream + react-native-sound + react-native-fs),
|
|
52
|
+
* onLink ((url) => boolean: true when your app opened a link the AI gave; otherwise the system opens it),
|
|
53
|
+
* attachments (default true: the attach button, photo library, camera, document), picker (adapter;
|
|
54
|
+
* default: react-native-image-picker + @react-native-documents/picker, see createDefaultPicker).
|
|
40
55
|
*/
|
|
41
56
|
function OryksaChat(props) {
|
|
42
|
-
const { client, lang = "en", theme, onClose, appContext, voice = true } = props;
|
|
57
|
+
const { client, lang = "en", theme, onClose, appContext, voice = true, onLink, attachments = true } = props;
|
|
43
58
|
const audio = React.useMemo(() => props.audio || createDefaultAudio(), [props.audio]);
|
|
59
|
+
const picker = React.useMemo(() => (attachments ? props.picker || createDefaultPicker() : null), [props.picker, attachments]);
|
|
60
|
+
const [menu, setMenu] = React.useState(false);
|
|
61
|
+
const [note, setNote] = React.useState("");
|
|
44
62
|
const [voiceOpen, setVoiceOpen] = React.useState(false);
|
|
45
63
|
const th = Object.assign({}, DEFAULT_THEME, theme || {});
|
|
46
64
|
const tx = TX[L(lang)];
|
|
@@ -80,18 +98,42 @@ function OryksaChat(props) {
|
|
|
80
98
|
return () => { alive = false; };
|
|
81
99
|
}, [client, lang]);
|
|
82
100
|
|
|
83
|
-
const
|
|
101
|
+
React.useEffect(() => { if (!note) return undefined; const t = setTimeout(() => setNote(""), 3500); return () => clearTimeout(t); }, [note]);
|
|
102
|
+
|
|
103
|
+
// att: { image: { base64, mime } } or { file: { base64, name, mime } } (the attach button).
|
|
104
|
+
const send = React.useCallback(async (value, att) => {
|
|
84
105
|
const q = String(value || "").trim();
|
|
85
|
-
|
|
106
|
+
const a = att || {};
|
|
107
|
+
if ((!q && !a.image && !a.file) || busy) return;
|
|
86
108
|
setBusy(true); setSug([]); setText("");
|
|
87
109
|
const id = String(Date.now());
|
|
88
|
-
setMsgs((m) => m.concat([{ id: id + "u", role: "user", text: q
|
|
110
|
+
setMsgs((m) => m.concat([{ id: id + "u", role: "user", text: q, image: a.image || null, fileName: a.file ? a.file.name : null },
|
|
111
|
+
{ id: id + "t", role: "typing", text: "..." }]));
|
|
89
112
|
let reply = null;
|
|
90
|
-
try { reply = await client.sendAndWait(q, { appContext: appContext ? appContext() : undefined }); } catch (_) { reply = null; }
|
|
113
|
+
try { reply = await client.sendAndWait(q, { appContext: appContext ? appContext() : undefined, image: a.image, file: a.file }); } catch (_) { reply = null; }
|
|
91
114
|
setMsgs((m) => m.filter((x) => x.role !== "typing").concat([{ id: id + "a", role: "assistant", text: reply || tx.err }]));
|
|
92
115
|
setBusy(false);
|
|
93
116
|
}, [busy, client, tx]);
|
|
94
117
|
|
|
118
|
+
// The attach menu: photo library, camera (not on the web), document; a note when too large or unreadable.
|
|
119
|
+
const attach = async (kind) => {
|
|
120
|
+
setMenu(false);
|
|
121
|
+
try {
|
|
122
|
+
const got = await picker[kind]();
|
|
123
|
+
if (!got) return;
|
|
124
|
+
const r = kind === "pickDocument" ? documentAttachment(got) : photoAttachment(got);
|
|
125
|
+
if (r.error) { setNote(r.error === "big" ? tx.big : tx.attachErr); return; }
|
|
126
|
+
await send(text, r);
|
|
127
|
+
} catch (_) {
|
|
128
|
+
setNote(tx.attachErr);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
const choices = picker ? [
|
|
132
|
+
picker.pickPhoto ? ["pickPhoto", tx.gallery] : null,
|
|
133
|
+
picker.takePhoto && Platform.OS !== "web" ? ["takePhoto", tx.camera] : null,
|
|
134
|
+
picker.pickDocument ? ["pickDocument", tx.document] : null,
|
|
135
|
+
].filter(Boolean) : [];
|
|
136
|
+
|
|
95
137
|
const name = (agent && agent.name) || "";
|
|
96
138
|
const sub = agent ? (pick(agent.subtitle, L(lang)) || agent.business || "") : "";
|
|
97
139
|
|
|
@@ -111,20 +153,38 @@ function OryksaChat(props) {
|
|
|
111
153
|
onContentSizeChange: () => listRef.current && listRef.current.scrollToEnd({ animated: true }),
|
|
112
154
|
renderItem: ({ item }) => {
|
|
113
155
|
const mine = item.role === "user";
|
|
156
|
+
const hasText = !!item.text || (!item.image && !item.fileName);
|
|
114
157
|
const bubble = h(View, { style: [s.bubble, mine ? [s.me, { backgroundColor: th.accent }] : [s.ai, { backgroundColor: th.soft }], item.role === "typing" ? { opacity: 0.6 } : null] },
|
|
115
|
-
h(
|
|
158
|
+
item.image ? h(Image, { source: { uri: "data:" + (item.image.mime || "image/jpeg") + ";base64," + item.image.base64 },
|
|
159
|
+
style: [s.photo, hasText || item.fileName ? { marginBottom: 8 } : null], accessibilityLabel: tx.gallery }) : null,
|
|
160
|
+
item.fileName ? h(View, { style: [s.fileRow, hasText ? { marginBottom: 8 } : null] },
|
|
161
|
+
h(DocGlyph, { color: mine ? "#fff" : th.accent }),
|
|
162
|
+
h(Text, { style: [s.fileName, { color: mine ? "#fff" : th.ink }], numberOfLines: 2 }, item.fileName)) : null,
|
|
163
|
+
hasText ? h(Text, { selectable: true, style: { color: mine ? "#fff" : th.ink, fontSize: 14, lineHeight: 21 } },
|
|
164
|
+
rich(mine ? profanity.mask(item.text, L(lang)) : item.text, { color: mine ? "#fff" : th.ink }, mine ? "#fff" : th.accent, onLink)) : null);
|
|
116
165
|
if (mine || item.role === "typing" || !String(item.text || "").trim()) return bubble;
|
|
117
|
-
//
|
|
118
|
-
|
|
166
|
+
// A card with an Open button for each link of the reply (owner 2026-11-21: the links were plain text),
|
|
167
|
+
// then the copy button (same as the ORYKSA apps and extension).
|
|
168
|
+
return h(View, null, bubble,
|
|
169
|
+
h(OryksaLinkCards, { text: item.text, client, openLabel: tx.open, onLink, theme: th, style: s.card }),
|
|
170
|
+
h(CopyButton, { text: String(item.text).replace(/\*\*/g, ""), label: tx.copy, color: th.muted }));
|
|
119
171
|
},
|
|
120
172
|
}),
|
|
121
173
|
sug.length ? h(View, { style: s.sug }, sug.map((q) =>
|
|
122
174
|
h(Pressable, { key: q, onPress: () => send(q), style: s.chip }, h(Text, { style: { color: th.accent, fontSize: 12.5 } }, q)))) : null,
|
|
175
|
+
note ? h(Text, { style: [s.note, { backgroundColor: th.ink }], accessibilityLiveRegion: "polite" }, note) : null,
|
|
176
|
+
menu && choices.length ? h(View, { style: [s.menu, { backgroundColor: th.background }] }, choices.map(([kind, label]) =>
|
|
177
|
+
h(Pressable, { key: kind, onPress: () => attach(kind), accessibilityRole: "button", style: s.menuItem },
|
|
178
|
+
h(Text, { style: { color: th.ink, fontSize: 14 } }, label)))) : null,
|
|
123
179
|
h(View, { style: s.form },
|
|
124
180
|
h(TextInput, {
|
|
125
181
|
value: text, onChangeText: setText, placeholder: tx.ph, placeholderTextColor: "#9CA3AF", maxLength: 2000,
|
|
126
182
|
style: [s.input, { color: th.ink }], returnKeyType: "send", onSubmitEditing: () => send(text),
|
|
127
183
|
}),
|
|
184
|
+
choices.length
|
|
185
|
+
? h(Pressable, { onPress: () => setMenu((v) => !v), disabled: busy, accessibilityRole: "button", accessibilityLabel: tx.attach, style: s.micBtn },
|
|
186
|
+
h(ClipGlyph, { color: th.accent }))
|
|
187
|
+
: null,
|
|
128
188
|
voice && audio && agent && agent.voice_replies && !text.trim()
|
|
129
189
|
? h(Pressable, { onPress: () => setVoiceOpen(true), disabled: busy, accessibilityRole: "button", accessibilityLabel: tx.voice, style: s.micBtn },
|
|
130
190
|
h(MicIcon, { color: th.accent }))
|
|
@@ -133,7 +193,7 @@ function OryksaChat(props) {
|
|
|
133
193
|
h(Text, { style: s.sendTxt }, tx.send.toUpperCase()))),
|
|
134
194
|
h(Text, { style: s.pw }, "POWERED BY ", h(Text, { style: { color: th.accent, fontWeight: "800" } }, "ORYKSA")),
|
|
135
195
|
voiceOpen ? h(OryksaVoiceScreen, {
|
|
136
|
-
client, agent, visible: true, lang: L(lang), theme: th, appContext, audio,
|
|
196
|
+
client, agent, visible: true, lang: L(lang), theme: th, appContext, audio, onLink,
|
|
137
197
|
onClose: () => setVoiceOpen(false),
|
|
138
198
|
onUserText: (t) => { setSug([]); setMsgs((m) => m.concat([{ id: "v" + Date.now() + "u", role: "user", text: t }])); },
|
|
139
199
|
onReply: (t) => setMsgs((m) => m.concat([{ id: "v" + Date.now() + "a", role: "assistant", text: t }])),
|
|
@@ -152,7 +212,7 @@ function OryksaChatModal(props) {
|
|
|
152
212
|
|
|
153
213
|
/** Floating "Talk to name" button with the photo of the AI. Opens the chat. */
|
|
154
214
|
function OryksaChatButton(props) {
|
|
155
|
-
const { client, lang = "en", theme, position = "right", style, appContext, voice, audio } = props;
|
|
215
|
+
const { client, lang = "en", theme, position = "right", style, appContext, voice, audio, onLink, attachments, picker } = props;
|
|
156
216
|
const th = Object.assign({}, DEFAULT_THEME, theme || {});
|
|
157
217
|
const [agent, setAgent] = React.useState(null);
|
|
158
218
|
const [open, setOpen] = React.useState(false);
|
|
@@ -169,7 +229,21 @@ function OryksaChatButton(props) {
|
|
|
169
229
|
},
|
|
170
230
|
h(Image, { source: { uri: (agent && agent.avatar) || FALLBACK_AVATAR }, style: s.fabImg }),
|
|
171
231
|
h(Text, { style: s.fabTxt }, (tx.talk + " " + ((agent && agent.name) || "ORYKSA")).toUpperCase())),
|
|
172
|
-
h(OryksaChatModal, { client, lang, theme, appContext, voice, audio, visible: open, onClose: () => setOpen(false) }));
|
|
232
|
+
h(OryksaChatModal, { client, lang, theme, appContext, voice, audio, onLink, attachments, picker, visible: open, onClose: () => setOpen(false) }));
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** A paperclip drawn with views (no icon font): two rounded loops, turned. */
|
|
236
|
+
function ClipGlyph({ color }) {
|
|
237
|
+
return h(View, { style: { width: 22, height: 22, alignItems: "center", justifyContent: "center", transform: [{ rotate: "40deg" }] } },
|
|
238
|
+
h(View, { style: { width: 9, height: 19, borderRadius: 4.5, borderWidth: 1.8, borderColor: color } }),
|
|
239
|
+
h(View, { style: { position: "absolute", top: 5, width: 4, height: 10, borderRadius: 2, borderWidth: 1.6, borderColor: color, borderTopWidth: 0 } }));
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** A document drawn with views: a page with two lines. */
|
|
243
|
+
function DocGlyph({ color }) {
|
|
244
|
+
return h(View, { style: { width: 14, height: 18, borderRadius: 2, borderWidth: 1.6, borderColor: color, alignItems: "center", justifyContent: "center" } },
|
|
245
|
+
h(View, { style: { width: 7, height: 1.6, backgroundColor: color, marginBottom: 2.5 } }),
|
|
246
|
+
h(View, { style: { width: 7, height: 1.6, backgroundColor: color } }));
|
|
173
247
|
}
|
|
174
248
|
|
|
175
249
|
/** Copies text: @react-native-clipboard/clipboard when installed, else the Clipboard of older React Native. */
|
|
@@ -205,6 +279,13 @@ const s = StyleSheet.create({
|
|
|
205
279
|
close: { fontSize: 26, paddingHorizontal: 6 },
|
|
206
280
|
bubble: { maxWidth: "82%", paddingHorizontal: 14, paddingVertical: 10, borderRadius: 16, marginBottom: 10 },
|
|
207
281
|
copy: { alignSelf: "flex-start", padding: 6, marginTop: -6, marginBottom: 6, borderRadius: 7 },
|
|
282
|
+
card: { width: "86%", marginTop: -4, marginBottom: 10 },
|
|
283
|
+
photo: { width: 200, height: 160, borderRadius: 12 },
|
|
284
|
+
fileRow: { flexDirection: "row", alignItems: "center", gap: 6 },
|
|
285
|
+
fileName: { flexShrink: 1, fontSize: 13, fontWeight: "600" },
|
|
286
|
+
note: { alignSelf: "center", color: "#fff", fontSize: 12.5, paddingHorizontal: 12, paddingVertical: 8, borderRadius: 999, marginBottom: 8, overflow: "hidden" },
|
|
287
|
+
menu: { borderTopWidth: 1, borderTopColor: "#ECEEF6", paddingVertical: 4 },
|
|
288
|
+
menuItem: { paddingHorizontal: 16, paddingVertical: 12 },
|
|
208
289
|
ai: { alignSelf: "flex-start", borderBottomLeftRadius: 6 },
|
|
209
290
|
me: { alignSelf: "flex-end", borderBottomRightRadius: 6 },
|
|
210
291
|
sug: { flexDirection: "row", flexWrap: "wrap", gap: 8, paddingHorizontal: 16, paddingBottom: 10 },
|