@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 +15 -0
- package/README.es.md +62 -0
- package/README.fr.md +62 -0
- package/README.md +62 -0
- package/README.pt.md +62 -0
- package/package.json +11 -3
- package/src/attach.js +133 -0
- package/src/chat.js +102 -21
- package/src/client.js +84 -10
- package/src/index.d.ts +257 -151
- 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 },
|
package/src/client.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
"use strict";
|
|
7
7
|
|
|
8
8
|
const DEFAULT_BASE = "https://api.oryksa.com/v1";
|
|
9
|
-
const VERSION = "1.1.
|
|
9
|
+
const VERSION = "1.1.4";
|
|
10
10
|
|
|
11
11
|
class OryksaError extends Error {
|
|
12
12
|
constructor(status, code, message, extra) {
|
|
@@ -106,6 +106,7 @@ class OryksaClient {
|
|
|
106
106
|
this._base = String(o.baseUrl || DEFAULT_BASE).replace(/\/$/, "");
|
|
107
107
|
this._fetch = o.fetch;
|
|
108
108
|
this._timeout = o.timeoutMs || 60000;
|
|
109
|
+
this._previews = new Map();
|
|
109
110
|
}
|
|
110
111
|
|
|
111
112
|
async _tok(force) {
|
|
@@ -128,21 +129,52 @@ class OryksaClient {
|
|
|
128
129
|
/** Name, photo, greeting and suggestions of the AI employee. */
|
|
129
130
|
agent() { return this._req("GET", "/client/agent"); }
|
|
130
131
|
|
|
132
|
+
/**
|
|
133
|
+
* Title and picture of a link the AI gave, for the card under her reply:
|
|
134
|
+
* { object: "link_preview", url, title, image, site, price }. Only the pages of the business's own
|
|
135
|
+
* site (its website chat domains) get a title and a picture. Resolves null when it cannot be read;
|
|
136
|
+
* never throws. Kept in memory for this client.
|
|
137
|
+
*/
|
|
138
|
+
async linkPreview(url) {
|
|
139
|
+
const key = String(url || "");
|
|
140
|
+
if (this._previews.has(key)) return this._previews.get(key);
|
|
141
|
+
try {
|
|
142
|
+
const d = await this._req("GET", "/client/link-preview?url=" + encodeURIComponent(key));
|
|
143
|
+
const str = (v) => (v === null || v === undefined ? "" : String(v));
|
|
144
|
+
const p = { object: "link_preview", url: str(d.url), title: str(d.title), image: str(d.image), site: str(d.site), price: str(d.price) };
|
|
145
|
+
if (this._previews.size > 200) this._previews.clear();
|
|
146
|
+
this._previews.set(key, p);
|
|
147
|
+
return p;
|
|
148
|
+
} catch (_) {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
131
153
|
/**
|
|
132
154
|
* Sends a message. status is "pending" when the AI needs a few more seconds: use sendAndWait.
|
|
133
155
|
* @param {string} message
|
|
134
|
-
* @param {{appContext?: {screen?: string, title?: string, items?: string[]}, voice?: boolean, whisper?: boolean, voiceStats?: object
|
|
156
|
+
* @param {{appContext?: {screen?: string, title?: string, items?: string[]}, voice?: boolean, whisper?: boolean, voiceStats?: object,
|
|
157
|
+
* image?: {base64: string, mime?: string}, file?: {base64: string, name: string, mime?: string}}} [opts]
|
|
135
158
|
* appContext: the screen the customer is on inside your app, so the AI answers about it.
|
|
136
159
|
* voice: the reply will be heard (it also carries a short `speech`). whisper: the customer whispered.
|
|
160
|
+
* image: a photo from the customer, the AI looks at it (up to 2 MB).
|
|
161
|
+
* file: a document from the customer (PDF, Word, text, CSV), the AI reads it (up to 8 MB).
|
|
137
162
|
*/
|
|
138
163
|
send(message, opts) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
164
|
+
return this._req("POST", "/client/chat", chatBody(message, opts));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* An AI task of your own app, counted as ORYKSA interactions (1 per answer): read a photo, extract data,
|
|
169
|
+
* classify, translate, write. It never creates a conversation in the business's inbox and never acts on
|
|
170
|
+
* the account. Your prompt (instructions) and your JSON Schema (schema) live in your app.
|
|
171
|
+
* @param {{instructions?: string, input?: string, images?: {base64: string, mime?: string}[], schema?: object,
|
|
172
|
+
* maxTokens?: number, temperature?: number}} opts images: up to 4 photos (about 1.5 MB each; resize to 1600 px).
|
|
173
|
+
* @returns {Promise<{text: string, json: object|null, interactionsSpent: number, interactionsRemaining: number|null}>}
|
|
174
|
+
*/
|
|
175
|
+
async ai(opts) {
|
|
176
|
+
const d = await this._req("POST", "/client/ai", aiBody(opts));
|
|
177
|
+
return aiResult(d);
|
|
146
178
|
}
|
|
147
179
|
|
|
148
180
|
/** Like sendAndWait, but returns the whole reply object ({ status, reply, speech, whisper }). */
|
|
@@ -215,6 +247,48 @@ class OryksaClient {
|
|
|
215
247
|
}
|
|
216
248
|
}
|
|
217
249
|
|
|
250
|
+
/** The JSON of POST /v1/client/chat. */
|
|
251
|
+
function chatBody(message, opts) {
|
|
252
|
+
const o = opts || {};
|
|
253
|
+
const body = { message };
|
|
254
|
+
if (o.appContext) body.app_context = appContextJson(o.appContext);
|
|
255
|
+
if (o.voice) body.voice = true;
|
|
256
|
+
if (o.whisper) body.whisper = true;
|
|
257
|
+
if (o.voiceStats) { body.platform = "rn"; body.voice_stats = o.voiceStats; }
|
|
258
|
+
if (o.image && o.image.base64) { body.image_base64 = o.image.base64; body.image_mime = o.image.mime || "image/jpeg"; }
|
|
259
|
+
if (o.file && typeof o.file.base64 === "string") {
|
|
260
|
+
body.file_base64 = o.file.base64; body.file_name = o.file.name || "file"; body.file_mime = o.file.mime || "application/octet-stream";
|
|
261
|
+
}
|
|
262
|
+
return body;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** The JSON of POST /v1/client/ai. */
|
|
266
|
+
function aiBody(opts) {
|
|
267
|
+
const o = opts || {};
|
|
268
|
+
const images = Array.isArray(o.images) ? o.images : [];
|
|
269
|
+
if (!o.instructions && !o.input && !images.length) throw new Error("ai() needs instructions, input or an image.");
|
|
270
|
+
if (images.length > 4) throw new Error("ai() takes at most 4 images.");
|
|
271
|
+
const body = { platform: "rn" };
|
|
272
|
+
if (o.instructions) body.instructions = String(o.instructions);
|
|
273
|
+
if (o.input) body.input = String(o.input);
|
|
274
|
+
if (images.length) body.images = images.map((i) => ({ base64: i.base64, mime: i.mime || "image/jpeg" }));
|
|
275
|
+
if (o.schema) body.schema = o.schema;
|
|
276
|
+
if (o.maxTokens != null) body.max_tokens = o.maxTokens;
|
|
277
|
+
if (o.temperature != null) body.temperature = o.temperature;
|
|
278
|
+
return body;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** Reads an ai_result answer. */
|
|
282
|
+
function aiResult(d) {
|
|
283
|
+
const use = (d && d.oryksa) || {};
|
|
284
|
+
return {
|
|
285
|
+
text: (d && d.text) || "",
|
|
286
|
+
json: d && d.json && typeof d.json === "object" ? d.json : null,
|
|
287
|
+
interactionsSpent: use.interactions_spent || 0,
|
|
288
|
+
interactionsRemaining: use.interactions_remaining == null ? null : use.interactions_remaining,
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
|
|
218
292
|
/** JSON of an app context: { screen, title, items (up to 20) }. */
|
|
219
293
|
function appContextJson(c) {
|
|
220
294
|
const o = {};
|
|
@@ -230,4 +304,4 @@ function pick(map, lang) {
|
|
|
230
304
|
return map[lang] || (lang === "br" ? map.pt : undefined) || map.en || Object.values(map)[0];
|
|
231
305
|
}
|
|
232
306
|
|
|
233
|
-
module.exports = { OryksaClient, OryksaError, pick, appContextJson, multipart, VERSION, DEFAULT_BASE };
|
|
307
|
+
module.exports = { OryksaClient, OryksaError, pick, appContextJson, chatBody, aiBody, aiResult, multipart, VERSION, DEFAULT_BASE };
|