@oryksa/react-native 1.1.0 → 1.1.1
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 +24 -20
- package/package.json +61 -61
- package/src/client.js +233 -233
- package/src/voice-screen.js +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
## 1.1.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
*
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.1.1
|
|
4
|
+
|
|
5
|
+
- Voice screen: the words the person says are never shown on screen. While the AI thinks it shows "I'm listening" / "One moment..." in the interface language; the AI's answer still shows as text. Same behaviour as the ORYKSA apps, web and extensions.
|
|
6
|
+
|
|
7
|
+
## 1.1.0
|
|
8
|
+
|
|
9
|
+
* Copy button under each reply of the AI (copies the text, shows a check for a moment), as in the ORYKSA apps and extension. Uses `@react-native-clipboard/clipboard` when installed (optional peer dependency).
|
|
10
|
+
* Voice: a voice screen with the photo of the AI ("I'm listening" / her answer, Mute, Close) opened from the microphone in the chat. The same behaviour as the ORYKSA app: the microphone stays open while she speaks and only a human voice cuts her off (typing, TV, birds and her own echo do not); she stops on the word and the text stays in the chat; the second before the cut is kept; 700 ms of silence closes a sentence (15 s at most); a whisper gets a whispered, shorter answer; her voice is the one chosen in ORYKSA and, if it fails, the text is shown without a robot voice.
|
|
11
|
+
* The voice engine is the ORYKSA app engine (`Vad`), with the same audio tests on real recordings.
|
|
12
|
+
* `appContext`: tell the AI which screen of your app the customer is on (product, cart, booking) so it answers about it.
|
|
13
|
+
* The client gets `tts`, `transcribe` and `voiceStats`; `send` accepts `voice`, `whisper`, `appContext` and returns `speech` (the short spoken version).
|
|
14
|
+
* The agent includes `photo` (the AI photo from Your AI), `voice` and `language`.
|
|
15
|
+
* Bold text (`**`) in the chat bubbles.
|
|
16
|
+
* The SDK serves the customers of the business only: it is never an interface for the owner.
|
|
17
|
+
* Voice uses an audio adapter: by default `react-native-live-audio-stream`, `react-native-sound` and `react-native-fs` (optional peer dependencies); Expo or other stacks can pass their own. Without them the chat works and the microphone is hidden.
|
|
18
|
+
|
|
19
|
+
## 1.0.0
|
|
20
|
+
|
|
21
|
+
* First release.
|
|
22
|
+
* `OryksaClient`: in-app client with short-lived session tokens (agent, send, sendAndWait, messages), refreshes the token when it expires.
|
|
23
|
+
* `OryksaChatButton`, `OryksaChatModal` and `OryksaChat`: ready chat with the look of the ORYKSA website chat, name and photo of the AI from the ORYKSA account, in English, Portuguese (Portugal and Brazil) and Spanish.
|
|
24
|
+
* No native code, works in Expo Go. TypeScript types included.
|
package/package.json
CHANGED
|
@@ -1,61 +1,61 @@
|
|
|
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
|
-
"keywords": [
|
|
27
|
-
"oryksa",
|
|
28
|
-
"react-native",
|
|
29
|
-
"ai",
|
|
30
|
-
"chatbot",
|
|
31
|
-
"customer-support",
|
|
32
|
-
"chat",
|
|
33
|
-
"sdk",
|
|
34
|
-
"expo",
|
|
35
|
-
"voice"
|
|
36
|
-
],
|
|
37
|
-
"homepage": "https://developer.oryksa.com/en/sdks",
|
|
38
|
-
"repository": {
|
|
39
|
-
"type": "git",
|
|
40
|
-
"url": "https://github.com/oryksa/oryksa-react-native"
|
|
41
|
-
},
|
|
42
|
-
"bugs": {
|
|
43
|
-
"url": "https://github.com/oryksa/oryksa-react-native/issues"
|
|
44
|
-
},
|
|
45
|
-
"author": "ORYKSA AI Employees (https://oryksa.com)",
|
|
46
|
-
"license": "MIT",
|
|
47
|
-
"peerDependenciesMeta": {
|
|
48
|
-
"react-native-live-audio-stream": {
|
|
49
|
-
"optional": true
|
|
50
|
-
},
|
|
51
|
-
"react-native-sound": {
|
|
52
|
-
"optional": true
|
|
53
|
-
},
|
|
54
|
-
"react-native-fs": {
|
|
55
|
-
"optional": true
|
|
56
|
-
},
|
|
57
|
-
"@react-native-clipboard/clipboard": {
|
|
58
|
-
"optional": true
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@oryksa/react-native",
|
|
3
|
+
"version": "1.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
|
+
"keywords": [
|
|
27
|
+
"oryksa",
|
|
28
|
+
"react-native",
|
|
29
|
+
"ai",
|
|
30
|
+
"chatbot",
|
|
31
|
+
"customer-support",
|
|
32
|
+
"chat",
|
|
33
|
+
"sdk",
|
|
34
|
+
"expo",
|
|
35
|
+
"voice"
|
|
36
|
+
],
|
|
37
|
+
"homepage": "https://developer.oryksa.com/en/sdks",
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "https://github.com/oryksa/oryksa-react-native"
|
|
41
|
+
},
|
|
42
|
+
"bugs": {
|
|
43
|
+
"url": "https://github.com/oryksa/oryksa-react-native/issues"
|
|
44
|
+
},
|
|
45
|
+
"author": "ORYKSA AI Employees (https://oryksa.com)",
|
|
46
|
+
"license": "MIT",
|
|
47
|
+
"peerDependenciesMeta": {
|
|
48
|
+
"react-native-live-audio-stream": {
|
|
49
|
+
"optional": true
|
|
50
|
+
},
|
|
51
|
+
"react-native-sound": {
|
|
52
|
+
"optional": true
|
|
53
|
+
},
|
|
54
|
+
"react-native-fs": {
|
|
55
|
+
"optional": true
|
|
56
|
+
},
|
|
57
|
+
"@react-native-clipboard/clipboard": {
|
|
58
|
+
"optional": true
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
package/src/client.js
CHANGED
|
@@ -1,233 +1,233 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* @oryksa/react-native - in-app client. Uses a short-lived session token (oryk_cs_...)
|
|
3
|
-
* created by YOUR server with POST /v1/sessions. The secret API key never goes into the app.
|
|
4
|
-
* Docs: https://developer.oryksa.com License: MIT
|
|
5
|
-
*/
|
|
6
|
-
"use strict";
|
|
7
|
-
|
|
8
|
-
const DEFAULT_BASE = "https://api.oryksa.com/v1";
|
|
9
|
-
const VERSION = "1.1.
|
|
10
|
-
|
|
11
|
-
class OryksaError extends Error {
|
|
12
|
-
constructor(status, code, message, extra) {
|
|
13
|
-
super(message);
|
|
14
|
-
this.name = "OryksaError";
|
|
15
|
-
this.status = status;
|
|
16
|
-
this.code = code;
|
|
17
|
-
Object.assign(this, extra || {});
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
async function request(base, token, method, path, body, fetchImpl, timeoutMs) {
|
|
22
|
-
const f = fetchImpl || globalThis.fetch;
|
|
23
|
-
const ctrl = typeof AbortController !== "undefined" ? new AbortController() : null;
|
|
24
|
-
const timer = ctrl ? setTimeout(() => ctrl.abort(), timeoutMs) : null;
|
|
25
|
-
let res;
|
|
26
|
-
try {
|
|
27
|
-
res = await f(base + path, {
|
|
28
|
-
method,
|
|
29
|
-
headers: Object.assign(
|
|
30
|
-
{ Authorization: "Bearer " + token, Accept: "application/json", "X-ORYKSA-SDK": "react-native/" + VERSION },
|
|
31
|
-
body !== undefined ? { "Content-Type": "application/json" } : {}
|
|
32
|
-
),
|
|
33
|
-
body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
34
|
-
signal: ctrl ? ctrl.signal : undefined,
|
|
35
|
-
});
|
|
36
|
-
} catch (e) {
|
|
37
|
-
throw new OryksaError(0, "network_error", "Could not reach ORYKSA: " + (e && e.message));
|
|
38
|
-
} finally {
|
|
39
|
-
if (timer) clearTimeout(timer);
|
|
40
|
-
}
|
|
41
|
-
let data = null;
|
|
42
|
-
try { data = await res.json(); } catch (_) { data = null; }
|
|
43
|
-
if (!res.ok && res.status !== 202) {
|
|
44
|
-
const e = (data && data.error) || {};
|
|
45
|
-
const extra = Object.assign({}, e);
|
|
46
|
-
delete extra.code; delete extra.message;
|
|
47
|
-
throw new OryksaError(res.status, e.code || "http_" + res.status, e.message || "Request failed with HTTP " + res.status, extra);
|
|
48
|
-
}
|
|
49
|
-
return data || {};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** One raw exchange (used for the voice: MP3 back, WAV up). Returns the Response. */
|
|
53
|
-
async function rawFetch(base, token, path, init, fetchImpl, timeoutMs) {
|
|
54
|
-
const f = fetchImpl || globalThis.fetch;
|
|
55
|
-
const ctrl = typeof AbortController !== "undefined" ? new AbortController() : null;
|
|
56
|
-
const timer = ctrl ? setTimeout(() => ctrl.abort(), timeoutMs) : null;
|
|
57
|
-
let res;
|
|
58
|
-
try {
|
|
59
|
-
res = await f(base + path, Object.assign({}, init, {
|
|
60
|
-
headers: Object.assign({ Authorization: "Bearer " + token, "X-ORYKSA-SDK": "react-native/" + VERSION }, init.headers || {}),
|
|
61
|
-
signal: ctrl ? ctrl.signal : undefined,
|
|
62
|
-
}));
|
|
63
|
-
} catch (e) {
|
|
64
|
-
throw new OryksaError(0, "network_error", "Could not reach ORYKSA: " + (e && e.message));
|
|
65
|
-
} finally {
|
|
66
|
-
if (timer) clearTimeout(timer);
|
|
67
|
-
}
|
|
68
|
-
if (!res.ok) {
|
|
69
|
-
let data = null;
|
|
70
|
-
try { data = await res.json(); } catch (_) { data = null; }
|
|
71
|
-
const e = (data && data.error) || {};
|
|
72
|
-
throw new OryksaError(res.status, e.code || "http_" + res.status, e.message || "Request failed with HTTP " + res.status);
|
|
73
|
-
}
|
|
74
|
-
return res;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function utf8(str) {
|
|
78
|
-
if (typeof TextEncoder !== "undefined") return new TextEncoder().encode(str);
|
|
79
|
-
const out = [];
|
|
80
|
-
for (let i = 0; i < str.length; i++) out.push(str.charCodeAt(i) & 0xff); // ASCII only (multipart headers)
|
|
81
|
-
return new Uint8Array(out);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** multipart/form-data body with one file field `file` (built by hand: works in React Native and Node). */
|
|
85
|
-
function multipart(bytes, filename, type) {
|
|
86
|
-
const boundary = "oryksa-" + Date.now().toString(36) + Math.random().toString(36).slice(2);
|
|
87
|
-
const head = utf8("--" + boundary + '\r\nContent-Disposition: form-data; name="file"; filename="' + filename + '"\r\nContent-Type: ' + type + "\r\n\r\n");
|
|
88
|
-
const tail = utf8("\r\n--" + boundary + "--\r\n");
|
|
89
|
-
const body = new Uint8Array(head.length + bytes.length + tail.length);
|
|
90
|
-
body.set(head, 0);
|
|
91
|
-
body.set(bytes, head.length);
|
|
92
|
-
body.set(tail, head.length + bytes.length);
|
|
93
|
-
return { body, contentType: "multipart/form-data; boundary=" + boundary };
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
class OryksaClient {
|
|
97
|
-
/**
|
|
98
|
-
* @param {{token?: string, getToken?: () => Promise<string>, baseUrl?: string, fetch?: Function, timeoutMs?: number}} opts
|
|
99
|
-
*/
|
|
100
|
-
constructor(opts) {
|
|
101
|
-
const o = opts || {};
|
|
102
|
-
if (!o.token && !o.getToken) throw new Error("OryksaClient needs { token } or { getToken }.");
|
|
103
|
-
if (o.token && String(o.token).startsWith("oryk_live_")) throw new Error("Never use the secret API key in an app. Use a session token (oryk_cs_...).");
|
|
104
|
-
this._token = o.token || null;
|
|
105
|
-
this._getToken = o.getToken || null;
|
|
106
|
-
this._base = String(o.baseUrl || DEFAULT_BASE).replace(/\/$/, "");
|
|
107
|
-
this._fetch = o.fetch;
|
|
108
|
-
this._timeout = o.timeoutMs || 60000;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
async _tok(force) {
|
|
112
|
-
if ((!this._token || force) && this._getToken) this._token = await this._getToken();
|
|
113
|
-
if (!this._token) throw new OryksaError(401, "no_token", "No session token.");
|
|
114
|
-
return this._token;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
async _req(method, path, body) {
|
|
118
|
-
try {
|
|
119
|
-
return await request(this._base, await this._tok(false), method, path, body, this._fetch, this._timeout);
|
|
120
|
-
} catch (e) {
|
|
121
|
-
if (e instanceof OryksaError && (e.code === "session_expired" || e.status === 401) && this._getToken) {
|
|
122
|
-
return request(this._base, await this._tok(true), method, path, body, this._fetch, this._timeout);
|
|
123
|
-
}
|
|
124
|
-
throw e;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/** Name, photo, greeting and suggestions of the AI employee. */
|
|
129
|
-
agent() { return this._req("GET", "/client/agent"); }
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Sends a message. status is "pending" when the AI needs a few more seconds: use sendAndWait.
|
|
133
|
-
* @param {string} message
|
|
134
|
-
* @param {{appContext?: {screen?: string, title?: string, items?: string[]}, voice?: boolean, whisper?: boolean, voiceStats?: object}} [opts]
|
|
135
|
-
* appContext: the screen the customer is on inside your app, so the AI answers about it.
|
|
136
|
-
* voice: the reply will be heard (it also carries a short `speech`). whisper: the customer whispered.
|
|
137
|
-
*/
|
|
138
|
-
send(message, opts) {
|
|
139
|
-
const o = opts || {};
|
|
140
|
-
const body = { message };
|
|
141
|
-
if (o.appContext) body.app_context = appContextJson(o.appContext);
|
|
142
|
-
if (o.voice) body.voice = true;
|
|
143
|
-
if (o.whisper) body.whisper = true;
|
|
144
|
-
if (o.voiceStats) { body.platform = "rn"; body.voice_stats = o.voiceStats; }
|
|
145
|
-
return this._req("POST", "/client/chat", body);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/** Like sendAndWait, but returns the whole reply object ({ status, reply, speech, whisper }). */
|
|
149
|
-
async sendAndWaitReply(message, opts) {
|
|
150
|
-
const o = opts || {};
|
|
151
|
-
const r = await this.send(message, o);
|
|
152
|
-
if (!r || r.status !== "pending") return r || {};
|
|
153
|
-
const end = Date.now() + (o.maxWaitMs || 40000);
|
|
154
|
-
while (Date.now() < end) {
|
|
155
|
-
await new Promise((ok) => setTimeout(ok, 1500));
|
|
156
|
-
const h = await this.messages();
|
|
157
|
-
const last = ((h && h.messages) || []).slice(-1)[0];
|
|
158
|
-
if (last && last.role === "assistant") return { status: "replied", reply: last.content, whisper: !!o.whisper };
|
|
159
|
-
}
|
|
160
|
-
return r;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
async _raw(path, init) {
|
|
164
|
-
try {
|
|
165
|
-
return await rawFetch(this._base, await this._tok(false), path, init, this._fetch, this._timeout);
|
|
166
|
-
} catch (e) {
|
|
167
|
-
if (e instanceof OryksaError && (e.code === "session_expired" || e.status === 401) && this._getToken) {
|
|
168
|
-
return rawFetch(this._base, await this._tok(true), path, init, this._fetch, this._timeout);
|
|
169
|
-
}
|
|
170
|
-
throw e;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* The AI's voice (ElevenLabs, the voice chosen in ORYKSA) for one reply of this conversation, as MP3
|
|
176
|
-
* bytes (Uint8Array). Resolves null when the voice is not available: then show the text only.
|
|
177
|
-
*/
|
|
178
|
-
async tts(text, opts) {
|
|
179
|
-
try {
|
|
180
|
-
const body = { text };
|
|
181
|
-
if (opts && opts.whisper) body.whisper = true;
|
|
182
|
-
const res = await this._raw("/client/tts", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) });
|
|
183
|
-
const buf = new Uint8Array(await res.arrayBuffer());
|
|
184
|
-
return buf.length ? buf : null;
|
|
185
|
-
} catch (_) {
|
|
186
|
-
return null;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/** Customer voice to text. wav: Uint8Array, 16 kHz mono PCM16 WAV, up to 15 s. "" = nothing said, null = failed. */
|
|
191
|
-
async transcribe(wav) {
|
|
192
|
-
try {
|
|
193
|
-
const mp = multipart(wav, "voice.wav", "audio/wav");
|
|
194
|
-
const res = await this._raw("/client/transcribe", { method: "POST", headers: { "Content-Type": mp.contentType, Accept: "application/json" }, body: mp.body });
|
|
195
|
-
const d = await res.json();
|
|
196
|
-
return String((d && d.text) || "").trim();
|
|
197
|
-
} catch (_) {
|
|
198
|
-
return null;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/** Reports a voice turn that produced no message (nothing heard, a cut with nothing said). */
|
|
203
|
-
async voiceStats(stats) {
|
|
204
|
-
try { await this._req("POST", "/client/voice-stats", { platform: "rn", voice_stats: stats }); } catch (_) { /* telemetry only */ }
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/** Messages of this conversation: { messages: [{ role, content }] }. */
|
|
208
|
-
messages() { return this._req("GET", "/client/messages"); }
|
|
209
|
-
|
|
210
|
-
/** Sends a message and waits for the reply text. opts: { appContext } (or a number = maxWaitMs, as in 1.0). */
|
|
211
|
-
async sendAndWait(message, opts) {
|
|
212
|
-
const o = typeof opts === "number" ? { maxWaitMs: opts } : (opts || {});
|
|
213
|
-
const r = await this.sendAndWaitReply(message, o);
|
|
214
|
-
return (r && r.reply) || null;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/** JSON of an app context: { screen, title, items (up to 20) }. */
|
|
219
|
-
function appContextJson(c) {
|
|
220
|
-
const o = {};
|
|
221
|
-
if (c.screen) o.screen = String(c.screen);
|
|
222
|
-
if (c.title) o.title = String(c.title);
|
|
223
|
-
if (Array.isArray(c.items) && c.items.length) o.items = c.items.slice(0, 20).map(String);
|
|
224
|
-
return o;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/** Picks the text for a language with fallbacks (br uses pt, then en). */
|
|
228
|
-
function pick(map, lang) {
|
|
229
|
-
if (!map || typeof map !== "object") return undefined;
|
|
230
|
-
return map[lang] || (lang === "br" ? map.pt : undefined) || map.en || Object.values(map)[0];
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
module.exports = { OryksaClient, OryksaError, pick, appContextJson, multipart, VERSION, DEFAULT_BASE };
|
|
1
|
+
/*!
|
|
2
|
+
* @oryksa/react-native - in-app client. Uses a short-lived session token (oryk_cs_...)
|
|
3
|
+
* created by YOUR server with POST /v1/sessions. The secret API key never goes into the app.
|
|
4
|
+
* Docs: https://developer.oryksa.com License: MIT
|
|
5
|
+
*/
|
|
6
|
+
"use strict";
|
|
7
|
+
|
|
8
|
+
const DEFAULT_BASE = "https://api.oryksa.com/v1";
|
|
9
|
+
const VERSION = "1.1.1";
|
|
10
|
+
|
|
11
|
+
class OryksaError extends Error {
|
|
12
|
+
constructor(status, code, message, extra) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = "OryksaError";
|
|
15
|
+
this.status = status;
|
|
16
|
+
this.code = code;
|
|
17
|
+
Object.assign(this, extra || {});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function request(base, token, method, path, body, fetchImpl, timeoutMs) {
|
|
22
|
+
const f = fetchImpl || globalThis.fetch;
|
|
23
|
+
const ctrl = typeof AbortController !== "undefined" ? new AbortController() : null;
|
|
24
|
+
const timer = ctrl ? setTimeout(() => ctrl.abort(), timeoutMs) : null;
|
|
25
|
+
let res;
|
|
26
|
+
try {
|
|
27
|
+
res = await f(base + path, {
|
|
28
|
+
method,
|
|
29
|
+
headers: Object.assign(
|
|
30
|
+
{ Authorization: "Bearer " + token, Accept: "application/json", "X-ORYKSA-SDK": "react-native/" + VERSION },
|
|
31
|
+
body !== undefined ? { "Content-Type": "application/json" } : {}
|
|
32
|
+
),
|
|
33
|
+
body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
34
|
+
signal: ctrl ? ctrl.signal : undefined,
|
|
35
|
+
});
|
|
36
|
+
} catch (e) {
|
|
37
|
+
throw new OryksaError(0, "network_error", "Could not reach ORYKSA: " + (e && e.message));
|
|
38
|
+
} finally {
|
|
39
|
+
if (timer) clearTimeout(timer);
|
|
40
|
+
}
|
|
41
|
+
let data = null;
|
|
42
|
+
try { data = await res.json(); } catch (_) { data = null; }
|
|
43
|
+
if (!res.ok && res.status !== 202) {
|
|
44
|
+
const e = (data && data.error) || {};
|
|
45
|
+
const extra = Object.assign({}, e);
|
|
46
|
+
delete extra.code; delete extra.message;
|
|
47
|
+
throw new OryksaError(res.status, e.code || "http_" + res.status, e.message || "Request failed with HTTP " + res.status, extra);
|
|
48
|
+
}
|
|
49
|
+
return data || {};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** One raw exchange (used for the voice: MP3 back, WAV up). Returns the Response. */
|
|
53
|
+
async function rawFetch(base, token, path, init, fetchImpl, timeoutMs) {
|
|
54
|
+
const f = fetchImpl || globalThis.fetch;
|
|
55
|
+
const ctrl = typeof AbortController !== "undefined" ? new AbortController() : null;
|
|
56
|
+
const timer = ctrl ? setTimeout(() => ctrl.abort(), timeoutMs) : null;
|
|
57
|
+
let res;
|
|
58
|
+
try {
|
|
59
|
+
res = await f(base + path, Object.assign({}, init, {
|
|
60
|
+
headers: Object.assign({ Authorization: "Bearer " + token, "X-ORYKSA-SDK": "react-native/" + VERSION }, init.headers || {}),
|
|
61
|
+
signal: ctrl ? ctrl.signal : undefined,
|
|
62
|
+
}));
|
|
63
|
+
} catch (e) {
|
|
64
|
+
throw new OryksaError(0, "network_error", "Could not reach ORYKSA: " + (e && e.message));
|
|
65
|
+
} finally {
|
|
66
|
+
if (timer) clearTimeout(timer);
|
|
67
|
+
}
|
|
68
|
+
if (!res.ok) {
|
|
69
|
+
let data = null;
|
|
70
|
+
try { data = await res.json(); } catch (_) { data = null; }
|
|
71
|
+
const e = (data && data.error) || {};
|
|
72
|
+
throw new OryksaError(res.status, e.code || "http_" + res.status, e.message || "Request failed with HTTP " + res.status);
|
|
73
|
+
}
|
|
74
|
+
return res;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function utf8(str) {
|
|
78
|
+
if (typeof TextEncoder !== "undefined") return new TextEncoder().encode(str);
|
|
79
|
+
const out = [];
|
|
80
|
+
for (let i = 0; i < str.length; i++) out.push(str.charCodeAt(i) & 0xff); // ASCII only (multipart headers)
|
|
81
|
+
return new Uint8Array(out);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** multipart/form-data body with one file field `file` (built by hand: works in React Native and Node). */
|
|
85
|
+
function multipart(bytes, filename, type) {
|
|
86
|
+
const boundary = "oryksa-" + Date.now().toString(36) + Math.random().toString(36).slice(2);
|
|
87
|
+
const head = utf8("--" + boundary + '\r\nContent-Disposition: form-data; name="file"; filename="' + filename + '"\r\nContent-Type: ' + type + "\r\n\r\n");
|
|
88
|
+
const tail = utf8("\r\n--" + boundary + "--\r\n");
|
|
89
|
+
const body = new Uint8Array(head.length + bytes.length + tail.length);
|
|
90
|
+
body.set(head, 0);
|
|
91
|
+
body.set(bytes, head.length);
|
|
92
|
+
body.set(tail, head.length + bytes.length);
|
|
93
|
+
return { body, contentType: "multipart/form-data; boundary=" + boundary };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
class OryksaClient {
|
|
97
|
+
/**
|
|
98
|
+
* @param {{token?: string, getToken?: () => Promise<string>, baseUrl?: string, fetch?: Function, timeoutMs?: number}} opts
|
|
99
|
+
*/
|
|
100
|
+
constructor(opts) {
|
|
101
|
+
const o = opts || {};
|
|
102
|
+
if (!o.token && !o.getToken) throw new Error("OryksaClient needs { token } or { getToken }.");
|
|
103
|
+
if (o.token && String(o.token).startsWith("oryk_live_")) throw new Error("Never use the secret API key in an app. Use a session token (oryk_cs_...).");
|
|
104
|
+
this._token = o.token || null;
|
|
105
|
+
this._getToken = o.getToken || null;
|
|
106
|
+
this._base = String(o.baseUrl || DEFAULT_BASE).replace(/\/$/, "");
|
|
107
|
+
this._fetch = o.fetch;
|
|
108
|
+
this._timeout = o.timeoutMs || 60000;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async _tok(force) {
|
|
112
|
+
if ((!this._token || force) && this._getToken) this._token = await this._getToken();
|
|
113
|
+
if (!this._token) throw new OryksaError(401, "no_token", "No session token.");
|
|
114
|
+
return this._token;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async _req(method, path, body) {
|
|
118
|
+
try {
|
|
119
|
+
return await request(this._base, await this._tok(false), method, path, body, this._fetch, this._timeout);
|
|
120
|
+
} catch (e) {
|
|
121
|
+
if (e instanceof OryksaError && (e.code === "session_expired" || e.status === 401) && this._getToken) {
|
|
122
|
+
return request(this._base, await this._tok(true), method, path, body, this._fetch, this._timeout);
|
|
123
|
+
}
|
|
124
|
+
throw e;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Name, photo, greeting and suggestions of the AI employee. */
|
|
129
|
+
agent() { return this._req("GET", "/client/agent"); }
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Sends a message. status is "pending" when the AI needs a few more seconds: use sendAndWait.
|
|
133
|
+
* @param {string} message
|
|
134
|
+
* @param {{appContext?: {screen?: string, title?: string, items?: string[]}, voice?: boolean, whisper?: boolean, voiceStats?: object}} [opts]
|
|
135
|
+
* appContext: the screen the customer is on inside your app, so the AI answers about it.
|
|
136
|
+
* voice: the reply will be heard (it also carries a short `speech`). whisper: the customer whispered.
|
|
137
|
+
*/
|
|
138
|
+
send(message, opts) {
|
|
139
|
+
const o = opts || {};
|
|
140
|
+
const body = { message };
|
|
141
|
+
if (o.appContext) body.app_context = appContextJson(o.appContext);
|
|
142
|
+
if (o.voice) body.voice = true;
|
|
143
|
+
if (o.whisper) body.whisper = true;
|
|
144
|
+
if (o.voiceStats) { body.platform = "rn"; body.voice_stats = o.voiceStats; }
|
|
145
|
+
return this._req("POST", "/client/chat", body);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Like sendAndWait, but returns the whole reply object ({ status, reply, speech, whisper }). */
|
|
149
|
+
async sendAndWaitReply(message, opts) {
|
|
150
|
+
const o = opts || {};
|
|
151
|
+
const r = await this.send(message, o);
|
|
152
|
+
if (!r || r.status !== "pending") return r || {};
|
|
153
|
+
const end = Date.now() + (o.maxWaitMs || 40000);
|
|
154
|
+
while (Date.now() < end) {
|
|
155
|
+
await new Promise((ok) => setTimeout(ok, 1500));
|
|
156
|
+
const h = await this.messages();
|
|
157
|
+
const last = ((h && h.messages) || []).slice(-1)[0];
|
|
158
|
+
if (last && last.role === "assistant") return { status: "replied", reply: last.content, whisper: !!o.whisper };
|
|
159
|
+
}
|
|
160
|
+
return r;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async _raw(path, init) {
|
|
164
|
+
try {
|
|
165
|
+
return await rawFetch(this._base, await this._tok(false), path, init, this._fetch, this._timeout);
|
|
166
|
+
} catch (e) {
|
|
167
|
+
if (e instanceof OryksaError && (e.code === "session_expired" || e.status === 401) && this._getToken) {
|
|
168
|
+
return rawFetch(this._base, await this._tok(true), path, init, this._fetch, this._timeout);
|
|
169
|
+
}
|
|
170
|
+
throw e;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* The AI's voice (ElevenLabs, the voice chosen in ORYKSA) for one reply of this conversation, as MP3
|
|
176
|
+
* bytes (Uint8Array). Resolves null when the voice is not available: then show the text only.
|
|
177
|
+
*/
|
|
178
|
+
async tts(text, opts) {
|
|
179
|
+
try {
|
|
180
|
+
const body = { text };
|
|
181
|
+
if (opts && opts.whisper) body.whisper = true;
|
|
182
|
+
const res = await this._raw("/client/tts", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) });
|
|
183
|
+
const buf = new Uint8Array(await res.arrayBuffer());
|
|
184
|
+
return buf.length ? buf : null;
|
|
185
|
+
} catch (_) {
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Customer voice to text. wav: Uint8Array, 16 kHz mono PCM16 WAV, up to 15 s. "" = nothing said, null = failed. */
|
|
191
|
+
async transcribe(wav) {
|
|
192
|
+
try {
|
|
193
|
+
const mp = multipart(wav, "voice.wav", "audio/wav");
|
|
194
|
+
const res = await this._raw("/client/transcribe", { method: "POST", headers: { "Content-Type": mp.contentType, Accept: "application/json" }, body: mp.body });
|
|
195
|
+
const d = await res.json();
|
|
196
|
+
return String((d && d.text) || "").trim();
|
|
197
|
+
} catch (_) {
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Reports a voice turn that produced no message (nothing heard, a cut with nothing said). */
|
|
203
|
+
async voiceStats(stats) {
|
|
204
|
+
try { await this._req("POST", "/client/voice-stats", { platform: "rn", voice_stats: stats }); } catch (_) { /* telemetry only */ }
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Messages of this conversation: { messages: [{ role, content }] }. */
|
|
208
|
+
messages() { return this._req("GET", "/client/messages"); }
|
|
209
|
+
|
|
210
|
+
/** Sends a message and waits for the reply text. opts: { appContext } (or a number = maxWaitMs, as in 1.0). */
|
|
211
|
+
async sendAndWait(message, opts) {
|
|
212
|
+
const o = typeof opts === "number" ? { maxWaitMs: opts } : (opts || {});
|
|
213
|
+
const r = await this.sendAndWaitReply(message, o);
|
|
214
|
+
return (r && r.reply) || null;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** JSON of an app context: { screen, title, items (up to 20) }. */
|
|
219
|
+
function appContextJson(c) {
|
|
220
|
+
const o = {};
|
|
221
|
+
if (c.screen) o.screen = String(c.screen);
|
|
222
|
+
if (c.title) o.title = String(c.title);
|
|
223
|
+
if (Array.isArray(c.items) && c.items.length) o.items = c.items.slice(0, 20).map(String);
|
|
224
|
+
return o;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Picks the text for a language with fallbacks (br uses pt, then en). */
|
|
228
|
+
function pick(map, lang) {
|
|
229
|
+
if (!map || typeof map !== "object") return undefined;
|
|
230
|
+
return map[lang] || (lang === "br" ? map.pt : undefined) || map.en || Object.values(map)[0];
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
module.exports = { OryksaClient, OryksaError, pick, appContextJson, multipart, VERSION, DEFAULT_BASE };
|
package/src/voice-screen.js
CHANGED
|
@@ -68,7 +68,8 @@ function OryksaVoiceScreen(props) {
|
|
|
68
68
|
const ctl = ctlRef.current;
|
|
69
69
|
const pick = {
|
|
70
70
|
starting: [t.listening, t.listeningSub], listening: [t.listening, t.listeningSub], hearing: [t.listening, t.hearing],
|
|
71
|
-
|
|
71
|
+
// never the words the person said on screen: only "listening" in the interface language; her answer shows as text
|
|
72
|
+
thinking: [t.listening, t.thinking], speaking: [(agent && agent.name) || "ORYKSA", (ctl && ctl.lastReply) || ""],
|
|
72
73
|
muted: [t.muted, t.mutedSub], micError: [t.micError, t.micErrorSub], noisy: [t.noisy, t.noisySub],
|
|
73
74
|
notUnderstood: [t.notUnderstood, t.notUnderstoodSub],
|
|
74
75
|
}[phase] || [t.listening, t.listeningSub];
|