@oryksa/react-native 1.1.1 → 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 -24
- package/README.es.md +161 -0
- package/README.fr.md +161 -0
- package/README.md +62 -24
- package/README.pt.md +161 -0
- 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/client.js
CHANGED
|
@@ -1,233 +1,267 @@
|
|
|
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
|
-
|
|
112
|
-
|
|
113
|
-
if (!this._token)
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*/
|
|
138
|
-
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
return
|
|
185
|
-
} catch (
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
}
|
|
232
|
-
|
|
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.3";
|
|
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
|
+
this._previews = new Map();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async _tok(force) {
|
|
113
|
+
if ((!this._token || force) && this._getToken) this._token = await this._getToken();
|
|
114
|
+
if (!this._token) throw new OryksaError(401, "no_token", "No session token.");
|
|
115
|
+
return this._token;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async _req(method, path, body) {
|
|
119
|
+
try {
|
|
120
|
+
return await request(this._base, await this._tok(false), method, path, body, this._fetch, this._timeout);
|
|
121
|
+
} catch (e) {
|
|
122
|
+
if (e instanceof OryksaError && (e.code === "session_expired" || e.status === 401) && this._getToken) {
|
|
123
|
+
return request(this._base, await this._tok(true), method, path, body, this._fetch, this._timeout);
|
|
124
|
+
}
|
|
125
|
+
throw e;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Name, photo, greeting and suggestions of the AI employee. */
|
|
130
|
+
agent() { return this._req("GET", "/client/agent"); }
|
|
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
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Sends a message. status is "pending" when the AI needs a few more seconds: use sendAndWait.
|
|
155
|
+
* @param {string} message
|
|
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]
|
|
158
|
+
* appContext: the screen the customer is on inside your app, so the AI answers about it.
|
|
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).
|
|
162
|
+
*/
|
|
163
|
+
send(message, opts) {
|
|
164
|
+
return this._req("POST", "/client/chat", chatBody(message, opts));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Like sendAndWait, but returns the whole reply object ({ status, reply, speech, whisper }). */
|
|
168
|
+
async sendAndWaitReply(message, opts) {
|
|
169
|
+
const o = opts || {};
|
|
170
|
+
const r = await this.send(message, o);
|
|
171
|
+
if (!r || r.status !== "pending") return r || {};
|
|
172
|
+
const end = Date.now() + (o.maxWaitMs || 40000);
|
|
173
|
+
while (Date.now() < end) {
|
|
174
|
+
await new Promise((ok) => setTimeout(ok, 1500));
|
|
175
|
+
const h = await this.messages();
|
|
176
|
+
const last = ((h && h.messages) || []).slice(-1)[0];
|
|
177
|
+
if (last && last.role === "assistant") return { status: "replied", reply: last.content, whisper: !!o.whisper };
|
|
178
|
+
}
|
|
179
|
+
return r;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async _raw(path, init) {
|
|
183
|
+
try {
|
|
184
|
+
return await rawFetch(this._base, await this._tok(false), path, init, this._fetch, this._timeout);
|
|
185
|
+
} catch (e) {
|
|
186
|
+
if (e instanceof OryksaError && (e.code === "session_expired" || e.status === 401) && this._getToken) {
|
|
187
|
+
return rawFetch(this._base, await this._tok(true), path, init, this._fetch, this._timeout);
|
|
188
|
+
}
|
|
189
|
+
throw e;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* The AI's voice (ElevenLabs, the voice chosen in ORYKSA) for one reply of this conversation, as MP3
|
|
195
|
+
* bytes (Uint8Array). Resolves null when the voice is not available: then show the text only.
|
|
196
|
+
*/
|
|
197
|
+
async tts(text, opts) {
|
|
198
|
+
try {
|
|
199
|
+
const body = { text };
|
|
200
|
+
if (opts && opts.whisper) body.whisper = true;
|
|
201
|
+
const res = await this._raw("/client/tts", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) });
|
|
202
|
+
const buf = new Uint8Array(await res.arrayBuffer());
|
|
203
|
+
return buf.length ? buf : null;
|
|
204
|
+
} catch (_) {
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** Customer voice to text. wav: Uint8Array, 16 kHz mono PCM16 WAV, up to 15 s. "" = nothing said, null = failed. */
|
|
210
|
+
async transcribe(wav) {
|
|
211
|
+
try {
|
|
212
|
+
const mp = multipart(wav, "voice.wav", "audio/wav");
|
|
213
|
+
const res = await this._raw("/client/transcribe", { method: "POST", headers: { "Content-Type": mp.contentType, Accept: "application/json" }, body: mp.body });
|
|
214
|
+
const d = await res.json();
|
|
215
|
+
return String((d && d.text) || "").trim();
|
|
216
|
+
} catch (_) {
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** Reports a voice turn that produced no message (nothing heard, a cut with nothing said). */
|
|
222
|
+
async voiceStats(stats) {
|
|
223
|
+
try { await this._req("POST", "/client/voice-stats", { platform: "rn", voice_stats: stats }); } catch (_) { /* telemetry only */ }
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Messages of this conversation: { messages: [{ role, content }] }. */
|
|
227
|
+
messages() { return this._req("GET", "/client/messages"); }
|
|
228
|
+
|
|
229
|
+
/** Sends a message and waits for the reply text. opts: { appContext } (or a number = maxWaitMs, as in 1.0). */
|
|
230
|
+
async sendAndWait(message, opts) {
|
|
231
|
+
const o = typeof opts === "number" ? { maxWaitMs: opts } : (opts || {});
|
|
232
|
+
const r = await this.sendAndWaitReply(message, o);
|
|
233
|
+
return (r && r.reply) || null;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** The JSON of POST /v1/client/chat. */
|
|
238
|
+
function chatBody(message, opts) {
|
|
239
|
+
const o = opts || {};
|
|
240
|
+
const body = { message };
|
|
241
|
+
if (o.appContext) body.app_context = appContextJson(o.appContext);
|
|
242
|
+
if (o.voice) body.voice = true;
|
|
243
|
+
if (o.whisper) body.whisper = true;
|
|
244
|
+
if (o.voiceStats) { body.platform = "rn"; body.voice_stats = o.voiceStats; }
|
|
245
|
+
if (o.image && o.image.base64) { body.image_base64 = o.image.base64; body.image_mime = o.image.mime || "image/jpeg"; }
|
|
246
|
+
if (o.file && typeof o.file.base64 === "string") {
|
|
247
|
+
body.file_base64 = o.file.base64; body.file_name = o.file.name || "file"; body.file_mime = o.file.mime || "application/octet-stream";
|
|
248
|
+
}
|
|
249
|
+
return body;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** JSON of an app context: { screen, title, items (up to 20) }. */
|
|
253
|
+
function appContextJson(c) {
|
|
254
|
+
const o = {};
|
|
255
|
+
if (c.screen) o.screen = String(c.screen);
|
|
256
|
+
if (c.title) o.title = String(c.title);
|
|
257
|
+
if (Array.isArray(c.items) && c.items.length) o.items = c.items.slice(0, 20).map(String);
|
|
258
|
+
return o;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** Picks the text for a language with fallbacks (br uses pt, then en). */
|
|
262
|
+
function pick(map, lang) {
|
|
263
|
+
if (!map || typeof map !== "object") return undefined;
|
|
264
|
+
return map[lang] || (lang === "br" ? map.pt : undefined) || map.en || Object.values(map)[0];
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
module.exports = { OryksaClient, OryksaError, pick, appContextJson, chatBody, multipart, VERSION, DEFAULT_BASE };
|
package/src/index.d.ts
CHANGED
|
@@ -35,7 +35,17 @@ export interface OryksaAppContext {
|
|
|
35
35
|
items?: string[];
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
/** A photo from the customer (JPEG, PNG or WebP, up to 2 MB). The AI looks at it. */
|
|
39
|
+
export interface OryksaImage { base64: string; mime?: string }
|
|
40
|
+
|
|
41
|
+
/** A document from the customer (PDF, Word, text or CSV, up to 8 MB). The AI reads it. */
|
|
42
|
+
export interface OryksaFile { base64: string; name: string; mime?: string }
|
|
43
|
+
|
|
38
44
|
export interface OryksaSendOptions {
|
|
45
|
+
/** A photo from the customer (sent as image_base64 / image_mime). */
|
|
46
|
+
image?: OryksaImage;
|
|
47
|
+
/** A document from the customer (sent as file_base64 / file_name / file_mime). */
|
|
48
|
+
file?: OryksaFile;
|
|
39
49
|
appContext?: OryksaAppContext;
|
|
40
50
|
/** The reply will be heard: it also carries a short `speech`. */
|
|
41
51
|
voice?: boolean;
|
|
@@ -55,9 +65,31 @@ export interface OryksaReply {
|
|
|
55
65
|
whisper?: boolean;
|
|
56
66
|
}
|
|
57
67
|
|
|
68
|
+
/** Title and picture of a link of the business's own site (OryksaClient.linkPreview). */
|
|
69
|
+
export interface OryksaLinkPreview {
|
|
70
|
+
object: "link_preview";
|
|
71
|
+
url: string;
|
|
72
|
+
/** Title of the page (empty for other sites). */
|
|
73
|
+
title: string;
|
|
74
|
+
/** Picture of the page (empty when it has none). */
|
|
75
|
+
image: string;
|
|
76
|
+
/** Name of the site. */
|
|
77
|
+
site: string;
|
|
78
|
+
/** Price, when the page says it (shops). */
|
|
79
|
+
price: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* What your app does with a link the AI gave (a product, a service, a page). Return true when your app
|
|
84
|
+
* opened it itself (for example its own product screen); return false and the system opens it.
|
|
85
|
+
*/
|
|
86
|
+
export type OryksaLinkHandler = (url: string) => boolean;
|
|
87
|
+
|
|
58
88
|
export declare class OryksaClient {
|
|
59
89
|
constructor(opts: { token?: string; getToken?: () => Promise<string>; baseUrl?: string; fetch?: typeof fetch; timeoutMs?: number });
|
|
60
90
|
agent(): Promise<OryksaAgent>;
|
|
91
|
+
/** Title and picture of a link the AI gave (only the pages of the business's own site); null when it cannot be read, never throws. */
|
|
92
|
+
linkPreview(url: string): Promise<OryksaLinkPreview | null>;
|
|
61
93
|
send(message: string, opts?: OryksaSendOptions): Promise<OryksaReply>;
|
|
62
94
|
sendAndWait(message: string, opts?: OryksaSendOptions | number): Promise<string | null>;
|
|
63
95
|
sendAndWaitReply(message: string, opts?: OryksaSendOptions): Promise<OryksaReply>;
|
|
@@ -96,6 +128,8 @@ export declare class OryksaVoiceController {
|
|
|
96
128
|
});
|
|
97
129
|
phase: OryksaVoicePhase;
|
|
98
130
|
lastReply: string;
|
|
131
|
+
/** What she said out loud last (the short spoken version of lastReply). */
|
|
132
|
+
lastSpoken: string;
|
|
99
133
|
lastHeard: string;
|
|
100
134
|
start(): Promise<void>;
|
|
101
135
|
sendNow(): void;
|
|
@@ -140,6 +174,12 @@ interface OryksaChatVoiceProps {
|
|
|
140
174
|
voice?: boolean;
|
|
141
175
|
/** Audio adapter (default: createDefaultAudio()). */
|
|
142
176
|
audio?: OryksaAudio | null;
|
|
177
|
+
/** What your app does with a link the AI gave: return true when your app opened it, false to let the system open it. */
|
|
178
|
+
onLink?: OryksaLinkHandler;
|
|
179
|
+
/** Shows the attach button (photo library, camera, document). Default true; it shows only when a picker is available. */
|
|
180
|
+
attachments?: boolean;
|
|
181
|
+
/** Picker adapter (default: createDefaultPicker()). */
|
|
182
|
+
picker?: OryksaPicker | null;
|
|
143
183
|
}
|
|
144
184
|
|
|
145
185
|
export declare function OryksaChat(props: { client: OryksaClient; lang?: OryksaLang; theme?: OryksaTheme; onClose?: () => void } & OryksaChatVoiceProps): React.ReactElement;
|
|
@@ -148,4 +188,50 @@ export declare function OryksaChatButton(props: { client: OryksaClient; lang?: O
|
|
|
148
188
|
export declare function OryksaVoiceScreen(props: {
|
|
149
189
|
client: OryksaClient; agent: OryksaAgent; visible: boolean; onClose: () => void; lang?: OryksaLang; theme?: OryksaTheme;
|
|
150
190
|
appContext?: () => OryksaAppContext | undefined; onUserText?: (text: string) => void; onReply?: (text: string) => void; audio?: OryksaAudio | null;
|
|
191
|
+
/** What your app does with a link the AI gave; tapping Open on a card closes the voice screen first. */
|
|
192
|
+
onLink?: OryksaLinkHandler;
|
|
151
193
|
}): React.ReactElement;
|
|
194
|
+
|
|
195
|
+
/** The card under a reply for one link: the picture and title of the page and an Open button. */
|
|
196
|
+
export declare function OryksaLinkCard(props: {
|
|
197
|
+
url: string; client: OryksaClient; openLabel?: string; onLink?: OryksaLinkHandler; onOpened?: () => void;
|
|
198
|
+
theme?: OryksaTheme & { line?: string }; style?: StyleProp<ViewStyle>;
|
|
199
|
+
}): React.ReactElement;
|
|
200
|
+
|
|
201
|
+
/** What a picker gives back: a photo ({ base64, mime }), a document ({ base64, name, mime } or { tooBig: true }), or null when cancelled. */
|
|
202
|
+
export interface OryksaPicked { base64?: string; name?: string; mime?: string; tooBig?: boolean }
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* The pickers of the attach button (bring your own for Expo or another stack). Photos should come made
|
|
206
|
+
* smaller (1600 px, JPEG about 80%). A missing function hides that choice.
|
|
207
|
+
*/
|
|
208
|
+
export interface OryksaPicker {
|
|
209
|
+
pickPhoto?: (() => Promise<OryksaPicked | null>) | null;
|
|
210
|
+
takePhoto?: (() => Promise<OryksaPicked | null>) | null;
|
|
211
|
+
pickDocument?: (() => Promise<OryksaPicked | null>) | null;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** The default pickers: react-native-image-picker and @react-native-documents/picker when installed (null if neither is). */
|
|
215
|
+
export declare function createDefaultPicker(): OryksaPicker | null;
|
|
216
|
+
|
|
217
|
+
export type OryksaAttachResult = { image: OryksaImage } | { file: OryksaFile } | { error: "big" | "failed" };
|
|
218
|
+
|
|
219
|
+
/** A picked photo as what the chat sends ({ error: "big" } above 2 MB). */
|
|
220
|
+
export declare function photoAttachment(p: OryksaPicked | null): OryksaAttachResult;
|
|
221
|
+
|
|
222
|
+
/** A picked document as what the chat sends: pictures up to 2 MB as a photo, the rest as a file up to 8 MB. */
|
|
223
|
+
export declare function documentAttachment(d: OryksaPicked | null): OryksaAttachResult;
|
|
224
|
+
|
|
225
|
+
/** 2 MB: the largest photo the chat sends. */
|
|
226
|
+
export declare const MAX_PHOTO_BYTES: number;
|
|
227
|
+
/** 8 MB: the largest document the chat sends. */
|
|
228
|
+
export declare const MAX_FILE_BYTES: number;
|
|
229
|
+
|
|
230
|
+
/** The distinct web links of text, in order, without the final punctuation (at most max, 3 by default). */
|
|
231
|
+
export declare function linksIn(text: string, max?: number): string[];
|
|
232
|
+
|
|
233
|
+
/** text without its web links (what the voice screen shows while she speaks). */
|
|
234
|
+
export declare function withoutLinks(text: string): string;
|
|
235
|
+
|
|
236
|
+
/** Opens a link: handler first (true = your app opened it), then the system (Linking.openURL). */
|
|
237
|
+
export declare function openLink(url: string, handler?: OryksaLinkHandler): void;
|