@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/src/index.d.ts CHANGED
@@ -1,151 +1,257 @@
1
- import * as React from "react";
2
- import { StyleProp, ViewStyle } from "react-native";
3
-
4
- export declare const VERSION: string;
5
-
6
- export declare class OryksaError extends Error {
7
- status: number;
8
- code: string;
9
- [key: string]: unknown;
10
- }
11
-
12
- export interface OryksaAgent {
13
- name: string;
14
- avatar: string;
15
- business?: string | null;
16
- gender?: "female" | "male";
17
- greeting?: Record<string, string>;
18
- subtitle?: Record<string, string>;
19
- suggestions?: Record<string, string[]>;
20
- voice_replies?: boolean;
21
- conversation_id?: string;
22
- /** The AI photo from "Your AI" in ORYKSA (never the owner's photo). Same as avatar. */
23
- photo?: string;
24
- /** ElevenLabs voice chosen for the AI in ORYKSA. */
25
- voice?: string | null;
26
- /** Main language of the AI, from ORYKSA. */
27
- language?: string | null;
28
- }
29
-
30
- /** Where the customer is inside your app (sent with each message so the AI answers about it). */
31
- export interface OryksaAppContext {
32
- screen?: string;
33
- title?: string;
34
- /** What is listed on the screen. Up to 20. */
35
- items?: string[];
36
- }
37
-
38
- export interface OryksaSendOptions {
39
- appContext?: OryksaAppContext;
40
- /** The reply will be heard: it also carries a short `speech`. */
41
- voice?: boolean;
42
- /** The customer whispered: shorter, whispered answer. */
43
- whisper?: boolean;
44
- voiceStats?: Record<string, unknown>;
45
- maxWaitMs?: number;
46
- }
47
-
48
- export interface OryksaReply {
49
- object: "chat_reply";
50
- conversation_id: string;
51
- status: "replied" | "pending";
52
- reply: string | null;
53
- /** With voice: the short spoken version of reply (1-2 sentences, no markdown). */
54
- speech?: string;
55
- whisper?: boolean;
56
- }
57
-
58
- export declare class OryksaClient {
59
- constructor(opts: { token?: string; getToken?: () => Promise<string>; baseUrl?: string; fetch?: typeof fetch; timeoutMs?: number });
60
- agent(): Promise<OryksaAgent>;
61
- send(message: string, opts?: OryksaSendOptions): Promise<OryksaReply>;
62
- sendAndWait(message: string, opts?: OryksaSendOptions | number): Promise<string | null>;
63
- sendAndWaitReply(message: string, opts?: OryksaSendOptions): Promise<OryksaReply>;
64
- messages(): Promise<{ messages: { role: "user" | "assistant"; content: string }[] }>;
65
- /** The AI's voice (MP3 bytes) for one reply of this conversation; null when not available (show the text only). */
66
- tts(text: string, opts?: { whisper?: boolean }): Promise<Uint8Array | null>;
67
- /** 16 kHz mono PCM16 WAV to text. "" = nothing said, null = failed. */
68
- transcribe(wav: Uint8Array): Promise<string | null>;
69
- voiceStats(stats: Record<string, unknown>): Promise<void>;
70
- }
71
-
72
- /** Audio adapter used by the voice conversation (bring your own for Expo or another audio stack). */
73
- export interface OryksaAudio {
74
- /** onChunk receives PCM16 little-endian mono 16 kHz. Resolves false when the mic is not available. */
75
- startMic(onChunk: (pcm: Uint8Array) => void): Promise<boolean>;
76
- stopMic(): Promise<void>;
77
- /** Plays MP3 bytes; resolves when it finished or was stopped. */
78
- play(mp3: Uint8Array): Promise<void>;
79
- stopPlayback(): Promise<void>;
80
- }
81
-
82
- /** The default adapter: react-native-live-audio-stream + react-native-sound + react-native-fs (null if not installed). */
83
- export declare function createDefaultAudio(): OryksaAudio | null;
84
-
85
- export type OryksaVoicePhase = "starting" | "listening" | "hearing" | "thinking" | "speaking" | "muted" | "micError" | "noisy" | "notUnderstood";
86
-
87
- /** The ORYKSA voice conversation: the same behaviour as the ORYKSA app (only a human voice cuts her off). */
88
- export declare class OryksaVoiceController {
89
- constructor(opts: {
90
- client: OryksaClient;
91
- audio?: OryksaAudio | null;
92
- appContext?: () => OryksaAppContext | undefined;
93
- onUserText?: (text: string) => void;
94
- onReply?: (text: string) => void;
95
- onPhase?: (phase: OryksaVoicePhase, controller: OryksaVoiceController) => void;
96
- });
97
- phase: OryksaVoicePhase;
98
- lastReply: string;
99
- lastHeard: string;
100
- start(): Promise<void>;
101
- sendNow(): void;
102
- say(text: string): Promise<void>;
103
- toggleMute(): Promise<void>;
104
- close(): Promise<void>;
105
- }
106
-
107
- /** The ORYKSA voice engine (line by line port of the ORYKSA app engine). */
108
- export declare class Vad {
109
- constructor(sampleRate?: number);
110
- static bargeFloor: number;
111
- static tetoOuvir: number;
112
- static bargeVozMin: number;
113
- static quietToCloseMs: number;
114
- static maxSpeechMs: number;
115
- static periodicidade(chunk: Uint8Array): number;
116
- static rmsOf(chunk: Uint8Array): number;
117
- readonly foiSussurro: boolean;
118
- capturing: boolean;
119
- reset(): void;
120
- newTurn(): void;
121
- feed(chunk: Uint8Array, speaking?: boolean): { isCapturing: boolean; isDone: boolean; isBargeIn: boolean; spokeMs: number; enough: boolean; isNoise: boolean; level: number };
122
- }
123
-
124
- export type OryksaLang = "en" | "pt" | "br" | "es";
125
-
126
- export interface OryksaTheme {
127
- accent?: string;
128
- ink?: string;
129
- soft?: string;
130
- background?: string;
131
- muted?: string;
132
- }
133
-
134
- export declare const DEFAULT_THEME: Required<OryksaTheme>;
135
-
136
- interface OryksaChatVoiceProps {
137
- /** Where the customer is in your app right now (sent with each message). */
138
- appContext?: () => OryksaAppContext | undefined;
139
- /** Shows the microphone when the plan has voice (default true). */
140
- voice?: boolean;
141
- /** Audio adapter (default: createDefaultAudio()). */
142
- audio?: OryksaAudio | null;
143
- }
144
-
145
- export declare function OryksaChat(props: { client: OryksaClient; lang?: OryksaLang; theme?: OryksaTheme; onClose?: () => void } & OryksaChatVoiceProps): React.ReactElement;
146
- export declare function OryksaChatModal(props: { client: OryksaClient; lang?: OryksaLang; theme?: OryksaTheme; visible: boolean; onClose: () => void } & OryksaChatVoiceProps): React.ReactElement;
147
- export declare function OryksaChatButton(props: { client: OryksaClient; lang?: OryksaLang; theme?: OryksaTheme; position?: "left" | "right"; style?: StyleProp<ViewStyle> } & OryksaChatVoiceProps): React.ReactElement;
148
- export declare function OryksaVoiceScreen(props: {
149
- client: OryksaClient; agent: OryksaAgent; visible: boolean; onClose: () => void; lang?: OryksaLang; theme?: OryksaTheme;
150
- appContext?: () => OryksaAppContext | undefined; onUserText?: (text: string) => void; onReply?: (text: string) => void; audio?: OryksaAudio | null;
151
- }): React.ReactElement;
1
+ import * as React from "react";
2
+ import { StyleProp, ViewStyle } from "react-native";
3
+
4
+ export declare const VERSION: string;
5
+
6
+ export declare class OryksaError extends Error {
7
+ status: number;
8
+ code: string;
9
+ [key: string]: unknown;
10
+ }
11
+
12
+ export interface OryksaAgent {
13
+ name: string;
14
+ avatar: string;
15
+ business?: string | null;
16
+ gender?: "female" | "male";
17
+ greeting?: Record<string, string>;
18
+ subtitle?: Record<string, string>;
19
+ suggestions?: Record<string, string[]>;
20
+ voice_replies?: boolean;
21
+ conversation_id?: string;
22
+ /** The AI photo from "Your AI" in ORYKSA (never the owner's photo). Same as avatar. */
23
+ photo?: string;
24
+ /** ElevenLabs voice chosen for the AI in ORYKSA. */
25
+ voice?: string | null;
26
+ /** Main language of the AI, from ORYKSA. */
27
+ language?: string | null;
28
+ }
29
+
30
+ /** Where the customer is inside your app (sent with each message so the AI answers about it). */
31
+ export interface OryksaAppContext {
32
+ screen?: string;
33
+ title?: string;
34
+ /** What is listed on the screen. Up to 20. */
35
+ items?: string[];
36
+ }
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
+ /** Options of OryksaClient.ai: your prompt and your JSON Schema live in your app. */
41
+ export interface OryksaAiOptions {
42
+ instructions?: string;
43
+ input?: string;
44
+ /** Up to 4 photos (about 1.5 MB each; resize to 1600 px). */
45
+ images?: OryksaImage[];
46
+ /** A JSON Schema: the answer comes back in `json`. */
47
+ schema?: object;
48
+ maxTokens?: number;
49
+ temperature?: number;
50
+ }
51
+ /** The answer of OryksaClient.ai. */
52
+ export interface OryksaAiResult {
53
+ text: string;
54
+ json: Record<string, unknown> | null;
55
+ interactionsSpent: number;
56
+ interactionsRemaining: number | null;
57
+ }
58
+
59
+ /** A document from the customer (PDF, Word, text or CSV, up to 8 MB). The AI reads it. */
60
+ export interface OryksaFile { base64: string; name: string; mime?: string }
61
+
62
+ export interface OryksaSendOptions {
63
+ /** A photo from the customer (sent as image_base64 / image_mime). */
64
+ image?: OryksaImage;
65
+ /** A document from the customer (sent as file_base64 / file_name / file_mime). */
66
+ file?: OryksaFile;
67
+ appContext?: OryksaAppContext;
68
+ /** The reply will be heard: it also carries a short `speech`. */
69
+ voice?: boolean;
70
+ /** The customer whispered: shorter, whispered answer. */
71
+ whisper?: boolean;
72
+ voiceStats?: Record<string, unknown>;
73
+ maxWaitMs?: number;
74
+ }
75
+
76
+ export interface OryksaReply {
77
+ object: "chat_reply";
78
+ conversation_id: string;
79
+ status: "replied" | "pending";
80
+ reply: string | null;
81
+ /** With voice: the short spoken version of reply (1-2 sentences, no markdown). */
82
+ speech?: string;
83
+ whisper?: boolean;
84
+ }
85
+
86
+ /** Title and picture of a link of the business's own site (OryksaClient.linkPreview). */
87
+ export interface OryksaLinkPreview {
88
+ object: "link_preview";
89
+ url: string;
90
+ /** Title of the page (empty for other sites). */
91
+ title: string;
92
+ /** Picture of the page (empty when it has none). */
93
+ image: string;
94
+ /** Name of the site. */
95
+ site: string;
96
+ /** Price, when the page says it (shops). */
97
+ price: string;
98
+ }
99
+
100
+ /**
101
+ * What your app does with a link the AI gave (a product, a service, a page). Return true when your app
102
+ * opened it itself (for example its own product screen); return false and the system opens it.
103
+ */
104
+ export type OryksaLinkHandler = (url: string) => boolean;
105
+
106
+ export declare class OryksaClient {
107
+ constructor(opts: { token?: string; getToken?: () => Promise<string>; baseUrl?: string; fetch?: typeof fetch; timeoutMs?: number });
108
+ agent(): Promise<OryksaAgent>;
109
+ /** 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. */
110
+ linkPreview(url: string): Promise<OryksaLinkPreview | null>;
111
+ /** An AI task of your own app, counted as ORYKSA interactions (1 per answer). Never creates a conversation. */
112
+ ai(opts: OryksaAiOptions): Promise<OryksaAiResult>;
113
+ send(message: string, opts?: OryksaSendOptions): Promise<OryksaReply>;
114
+ sendAndWait(message: string, opts?: OryksaSendOptions | number): Promise<string | null>;
115
+ sendAndWaitReply(message: string, opts?: OryksaSendOptions): Promise<OryksaReply>;
116
+ messages(): Promise<{ messages: { role: "user" | "assistant"; content: string }[] }>;
117
+ /** The AI's voice (MP3 bytes) for one reply of this conversation; null when not available (show the text only). */
118
+ tts(text: string, opts?: { whisper?: boolean }): Promise<Uint8Array | null>;
119
+ /** 16 kHz mono PCM16 WAV to text. "" = nothing said, null = failed. */
120
+ transcribe(wav: Uint8Array): Promise<string | null>;
121
+ voiceStats(stats: Record<string, unknown>): Promise<void>;
122
+ }
123
+
124
+ /** Audio adapter used by the voice conversation (bring your own for Expo or another audio stack). */
125
+ export interface OryksaAudio {
126
+ /** onChunk receives PCM16 little-endian mono 16 kHz. Resolves false when the mic is not available. */
127
+ startMic(onChunk: (pcm: Uint8Array) => void): Promise<boolean>;
128
+ stopMic(): Promise<void>;
129
+ /** Plays MP3 bytes; resolves when it finished or was stopped. */
130
+ play(mp3: Uint8Array): Promise<void>;
131
+ stopPlayback(): Promise<void>;
132
+ }
133
+
134
+ /** The default adapter: react-native-live-audio-stream + react-native-sound + react-native-fs (null if not installed). */
135
+ export declare function createDefaultAudio(): OryksaAudio | null;
136
+
137
+ export type OryksaVoicePhase = "starting" | "listening" | "hearing" | "thinking" | "speaking" | "muted" | "micError" | "noisy" | "notUnderstood";
138
+
139
+ /** The ORYKSA voice conversation: the same behaviour as the ORYKSA app (only a human voice cuts her off). */
140
+ export declare class OryksaVoiceController {
141
+ constructor(opts: {
142
+ client: OryksaClient;
143
+ audio?: OryksaAudio | null;
144
+ appContext?: () => OryksaAppContext | undefined;
145
+ onUserText?: (text: string) => void;
146
+ onReply?: (text: string) => void;
147
+ onPhase?: (phase: OryksaVoicePhase, controller: OryksaVoiceController) => void;
148
+ });
149
+ phase: OryksaVoicePhase;
150
+ lastReply: string;
151
+ /** What she said out loud last (the short spoken version of lastReply). */
152
+ lastSpoken: string;
153
+ lastHeard: string;
154
+ start(): Promise<void>;
155
+ sendNow(): void;
156
+ say(text: string): Promise<void>;
157
+ toggleMute(): Promise<void>;
158
+ close(): Promise<void>;
159
+ }
160
+
161
+ /** The ORYKSA voice engine (line by line port of the ORYKSA app engine). */
162
+ export declare class Vad {
163
+ constructor(sampleRate?: number);
164
+ static bargeFloor: number;
165
+ static tetoOuvir: number;
166
+ static bargeVozMin: number;
167
+ static quietToCloseMs: number;
168
+ static maxSpeechMs: number;
169
+ static periodicidade(chunk: Uint8Array): number;
170
+ static rmsOf(chunk: Uint8Array): number;
171
+ readonly foiSussurro: boolean;
172
+ capturing: boolean;
173
+ reset(): void;
174
+ newTurn(): void;
175
+ feed(chunk: Uint8Array, speaking?: boolean): { isCapturing: boolean; isDone: boolean; isBargeIn: boolean; spokeMs: number; enough: boolean; isNoise: boolean; level: number };
176
+ }
177
+
178
+ export type OryksaLang = "en" | "pt" | "br" | "es";
179
+
180
+ export interface OryksaTheme {
181
+ accent?: string;
182
+ ink?: string;
183
+ soft?: string;
184
+ background?: string;
185
+ muted?: string;
186
+ }
187
+
188
+ export declare const DEFAULT_THEME: Required<OryksaTheme>;
189
+
190
+ interface OryksaChatVoiceProps {
191
+ /** Where the customer is in your app right now (sent with each message). */
192
+ appContext?: () => OryksaAppContext | undefined;
193
+ /** Shows the microphone when the plan has voice (default true). */
194
+ voice?: boolean;
195
+ /** Audio adapter (default: createDefaultAudio()). */
196
+ audio?: OryksaAudio | null;
197
+ /** What your app does with a link the AI gave: return true when your app opened it, false to let the system open it. */
198
+ onLink?: OryksaLinkHandler;
199
+ /** Shows the attach button (photo library, camera, document). Default true; it shows only when a picker is available. */
200
+ attachments?: boolean;
201
+ /** Picker adapter (default: createDefaultPicker()). */
202
+ picker?: OryksaPicker | null;
203
+ }
204
+
205
+ export declare function OryksaChat(props: { client: OryksaClient; lang?: OryksaLang; theme?: OryksaTheme; onClose?: () => void } & OryksaChatVoiceProps): React.ReactElement;
206
+ export declare function OryksaChatModal(props: { client: OryksaClient; lang?: OryksaLang; theme?: OryksaTheme; visible: boolean; onClose: () => void } & OryksaChatVoiceProps): React.ReactElement;
207
+ export declare function OryksaChatButton(props: { client: OryksaClient; lang?: OryksaLang; theme?: OryksaTheme; position?: "left" | "right"; style?: StyleProp<ViewStyle> } & OryksaChatVoiceProps): React.ReactElement;
208
+ export declare function OryksaVoiceScreen(props: {
209
+ client: OryksaClient; agent: OryksaAgent; visible: boolean; onClose: () => void; lang?: OryksaLang; theme?: OryksaTheme;
210
+ appContext?: () => OryksaAppContext | undefined; onUserText?: (text: string) => void; onReply?: (text: string) => void; audio?: OryksaAudio | null;
211
+ /** What your app does with a link the AI gave; tapping Open on a card closes the voice screen first. */
212
+ onLink?: OryksaLinkHandler;
213
+ }): React.ReactElement;
214
+
215
+ /** The card under a reply for one link: the picture and title of the page and an Open button. */
216
+ export declare function OryksaLinkCard(props: {
217
+ url: string; client: OryksaClient; openLabel?: string; onLink?: OryksaLinkHandler; onOpened?: () => void;
218
+ theme?: OryksaTheme & { line?: string }; style?: StyleProp<ViewStyle>;
219
+ }): React.ReactElement;
220
+
221
+ /** What a picker gives back: a photo ({ base64, mime }), a document ({ base64, name, mime } or { tooBig: true }), or null when cancelled. */
222
+ export interface OryksaPicked { base64?: string; name?: string; mime?: string; tooBig?: boolean }
223
+
224
+ /**
225
+ * The pickers of the attach button (bring your own for Expo or another stack). Photos should come made
226
+ * smaller (1600 px, JPEG about 80%). A missing function hides that choice.
227
+ */
228
+ export interface OryksaPicker {
229
+ pickPhoto?: (() => Promise<OryksaPicked | null>) | null;
230
+ takePhoto?: (() => Promise<OryksaPicked | null>) | null;
231
+ pickDocument?: (() => Promise<OryksaPicked | null>) | null;
232
+ }
233
+
234
+ /** The default pickers: react-native-image-picker and @react-native-documents/picker when installed (null if neither is). */
235
+ export declare function createDefaultPicker(): OryksaPicker | null;
236
+
237
+ export type OryksaAttachResult = { image: OryksaImage } | { file: OryksaFile } | { error: "big" | "failed" };
238
+
239
+ /** A picked photo as what the chat sends ({ error: "big" } above 2 MB). */
240
+ export declare function photoAttachment(p: OryksaPicked | null): OryksaAttachResult;
241
+
242
+ /** 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. */
243
+ export declare function documentAttachment(d: OryksaPicked | null): OryksaAttachResult;
244
+
245
+ /** 2 MB: the largest photo the chat sends. */
246
+ export declare const MAX_PHOTO_BYTES: number;
247
+ /** 8 MB: the largest document the chat sends. */
248
+ export declare const MAX_FILE_BYTES: number;
249
+
250
+ /** The distinct web links of text, in order, without the final punctuation (at most max, 3 by default). */
251
+ export declare function linksIn(text: string, max?: number): string[];
252
+
253
+ /** text without its web links (what the voice screen shows while she speaks). */
254
+ export declare function withoutLinks(text: string): string;
255
+
256
+ /** Opens a link: handler first (true = your app opened it), then the system (Linking.openURL). */
257
+ export declare function openLink(url: string, handler?: OryksaLinkHandler): void;
package/src/index.js CHANGED
@@ -10,6 +10,9 @@ const voice = require("./voice");
10
10
  const voiceScreen = require("./voice-screen");
11
11
  const vad = require("./vad");
12
12
  const profanity = require("./profanity");
13
+ const links = require("./links");
14
+ const linkCard = require("./link-card");
15
+ const attach = require("./attach");
13
16
 
14
17
  module.exports = {
15
18
  OryksaClient: client.OryksaClient,
@@ -25,4 +28,13 @@ module.exports = {
25
28
  Vad: vad.Vad,
26
29
  maskProfanity: profanity.mask,
27
30
  loadProfanity: profanity.load,
31
+ OryksaLinkCard: linkCard.OryksaLinkCard,
32
+ linksIn: links.linksIn,
33
+ withoutLinks: links.withoutLinks,
34
+ openLink: links.openLink,
35
+ createDefaultPicker: attach.createDefaultPicker,
36
+ photoAttachment: attach.photoAttachment,
37
+ documentAttachment: attach.documentAttachment,
38
+ MAX_PHOTO_BYTES: attach.MAX_PHOTO_BYTES,
39
+ MAX_FILE_BYTES: attach.MAX_FILE_BYTES,
28
40
  };
@@ -0,0 +1,74 @@
1
+ /*!
2
+ * @oryksa/react-native - the card under a reply for each link: the picture and title of the page
3
+ * and an Open button. License: MIT
4
+ */
5
+ "use strict";
6
+
7
+ const React = require("react");
8
+ const RN = require("react-native");
9
+ const { cardTexts, openLink, linksIn } = require("./links");
10
+
11
+ const h = React.createElement;
12
+ const { View, Text, Image, Pressable, StyleSheet } = RN;
13
+
14
+ const COLORS = { accent: "#5B47E0", ink: "#1F2937", muted: "#6B7280", background: "#FFFFFF", line: "#E5E7EB" };
15
+
16
+ /** A link drawn with views (no icon font): two rounded rings, crossed. */
17
+ function LinkGlyph({ color }) {
18
+ const ring = (left) => h(View, { style: { position: "absolute", top: 7, left, width: 12, height: 8, borderRadius: 4, borderWidth: 2, borderColor: color } });
19
+ return h(View, { style: { width: 22, height: 22, transform: [{ rotate: "-45deg" }] } }, ring(1), ring(9));
20
+ }
21
+
22
+ /**
23
+ * The card for one link of the AI's reply. Props: url, client, openLabel ("Open", "Abrir"), onLink
24
+ * ((url) => boolean: true when your app opened it), onOpened (called when the card opens the link),
25
+ * theme ({ accent, ink, muted, background, line }), style.
26
+ * Only the pages of the business's own site get a title and a picture; other links show the address.
27
+ */
28
+ function OryksaLinkCard(props) {
29
+ const { url, client, openLabel = "Open", onLink, onOpened, theme, style } = props;
30
+ const th = Object.assign({}, COLORS, theme || {});
31
+ const [preview, setPreview] = React.useState(null);
32
+ const [broken, setBroken] = React.useState(false);
33
+ React.useEffect(() => {
34
+ let alive = true;
35
+ setPreview(null); setBroken(false);
36
+ if (client && client.linkPreview) client.linkPreview(url).then((p) => { if (alive && p) setPreview(p); }).catch(() => {});
37
+ return () => { alive = false; };
38
+ }, [url, client]);
39
+ const { title, sub } = cardTexts(url, preview);
40
+ const img = (preview && preview.image) || "";
41
+ const open = () => { if (onOpened) onOpened(); openLink(url, onLink); };
42
+ return h(Pressable, {
43
+ onPress: open, accessibilityRole: "button", accessibilityLabel: openLabel + ": " + title,
44
+ style: [s.card, { backgroundColor: th.background, borderColor: th.line }, style],
45
+ },
46
+ h(View, { style: [s.pic, { backgroundColor: th.line }] },
47
+ img && !broken
48
+ ? h(Image, { source: { uri: img }, style: s.img, resizeMode: "cover", onError: () => setBroken(true) })
49
+ : h(LinkGlyph, { color: th.muted })),
50
+ h(View, { style: s.texts },
51
+ h(Text, { style: [s.title, { color: th.ink }], numberOfLines: 2 }, title),
52
+ sub ? h(Text, { style: [s.sub, { color: th.muted }], numberOfLines: 1 }, sub) : null),
53
+ h(Pressable, { onPress: open, accessibilityRole: "button", accessibilityLabel: openLabel, style: [s.open, { backgroundColor: th.accent }] },
54
+ h(Text, { style: s.openTxt }, openLabel)));
55
+ }
56
+
57
+ /** The cards for the links of text (at most 3), one under the other. */
58
+ function OryksaLinkCards(props) {
59
+ const { text, style } = props;
60
+ return linksIn(text).map((u) => h(OryksaLinkCard, Object.assign({}, props, { key: u, url: u, style })));
61
+ }
62
+
63
+ const s = StyleSheet.create({
64
+ card: { flexDirection: "row", alignItems: "center", padding: 8, borderRadius: 14, borderWidth: 1 },
65
+ pic: { width: 52, height: 52, borderRadius: 10, overflow: "hidden", alignItems: "center", justifyContent: "center" },
66
+ img: { width: 52, height: 52 },
67
+ texts: { flex: 1, marginLeft: 10, marginRight: 8 },
68
+ title: { fontSize: 13.5, lineHeight: 17.5, fontWeight: "700" },
69
+ sub: { fontSize: 11.5, marginTop: 1 },
70
+ open: { minHeight: 36, paddingHorizontal: 14, borderRadius: 999, alignItems: "center", justifyContent: "center" },
71
+ openTxt: { color: "#fff", fontSize: 13, fontWeight: "700" },
72
+ });
73
+
74
+ module.exports = { OryksaLinkCard, OryksaLinkCards };
package/src/links.js ADDED
@@ -0,0 +1,115 @@
1
+ /*!
2
+ * @oryksa/react-native - the links of the AI's replies: found in the text, taken out of what she
3
+ * says on the voice screen, and opened by your app (onLink) or by the system. License: MIT
4
+ */
5
+ "use strict";
6
+
7
+ const URL_RE = /https?:\/\/[^\s<>"'\)\]]+/gi;
8
+ const MAIL_RE = /[\w.+-]+@[\w-]+(\.[\w-]+)+/g;
9
+
10
+ /** Every match of re in s (a fresh copy of the regular expression, so no state is shared). */
11
+ function allMatches(re, s) {
12
+ const r = new RegExp(re.source, re.flags);
13
+ const out = [];
14
+ let m;
15
+ while ((m = r.exec(s)) !== null) {
16
+ out.push(m);
17
+ if (m[0] === "") r.lastIndex++;
18
+ }
19
+ return out;
20
+ }
21
+
22
+ /** The address without the punctuation that ends the sentence (.,;:!?…). */
23
+ function trimEnd(u) { return u.replace(/[.,;:!?…]+$/, ""); }
24
+
25
+ /** The distinct web links of text, in order (at most max). */
26
+ function linksIn(text, max) {
27
+ const limit = max === undefined ? 3 : max;
28
+ const out = [];
29
+ for (const m of allMatches(URL_RE, String(text || ""))) {
30
+ const u = trimEnd(m[0]);
31
+ if (!out.includes(u)) out.push(u);
32
+ if (out.length >= limit) break;
33
+ }
34
+ return out;
35
+ }
36
+
37
+ /** text without its web links (for the voice screen: the link is on the button). */
38
+ function withoutLinks(text) {
39
+ return String(text || "")
40
+ .replace(URL_RE, "")
41
+ .replace(/[ \t]+\n/g, "\n")
42
+ .replace(/\n{3,}/g, "\n\n")
43
+ .replace(/[ \t]{2,}/g, " ")
44
+ .replace(/\s+([.,;!?])/g, "$1")
45
+ .replace(/:\s*$/gm, "")
46
+ .trim();
47
+ }
48
+
49
+ /** The parts of s that are links: web addresses and e-mails (as mailto:), in order. */
50
+ function linkHits(s) {
51
+ const hits = [];
52
+ for (const m of allMatches(URL_RE, s)) {
53
+ const u = trimEnd(m[0]);
54
+ hits.push({ start: m.index, end: m.index + u.length, url: u });
55
+ }
56
+ for (const m of allMatches(MAIL_RE, s)) {
57
+ const start = m.index;
58
+ const end = m.index + m[0].length;
59
+ if (hits.some((x) => start < x.end && end > x.start)) continue;
60
+ hits.push({ start, end, url: "mailto:" + m[0] });
61
+ }
62
+ return hits.sort((a, b) => a.start - b.start);
63
+ }
64
+
65
+ /**
66
+ * A message as parts to draw: { text, bold, url } (url only on the links, web and e-mail).
67
+ * **bold** parts stay bold.
68
+ */
69
+ function richParts(text) {
70
+ const out = [];
71
+ const links = (s, bold) => {
72
+ let at = 0;
73
+ for (const x of linkHits(s)) {
74
+ if (x.start < at) continue;
75
+ if (x.start > at) out.push({ text: s.slice(at, x.start), bold, url: null });
76
+ out.push({ text: s.slice(x.start, x.end), bold, url: x.url });
77
+ at = x.end;
78
+ }
79
+ if (at < s.length) out.push({ text: s.slice(at), bold, url: null });
80
+ };
81
+ const t = String(text || "");
82
+ const parts = t.split("**");
83
+ if (parts.length < 3) { links(t, false); return out; }
84
+ parts.forEach((p, i) => { if (p) links(p, i % 2 === 1); });
85
+ return out;
86
+ }
87
+
88
+ /** The host of url without www. (what the card shows when it has no title). */
89
+ function hostOf(url) {
90
+ const m = /^[a-z][a-z0-9+.-]*:\/\/([^/?#:]+)/i.exec(String(url || ""));
91
+ return m ? m[1].replace(/^www\./i, "") : "";
92
+ }
93
+
94
+ /** Title and sub line (price · site, or site, or host) of the card for url. */
95
+ function cardTexts(url, p) {
96
+ const host = hostOf(url);
97
+ const title = p && p.title ? p.title : host;
98
+ const sub = p && p.price ? p.price + " · " + (p.site || "") : (p && p.site ? p.site : host);
99
+ return { title, sub };
100
+ }
101
+
102
+ /**
103
+ * Opens a link: your handler first (return true when your app opened it), then the system
104
+ * (React Native Linking: the browser or the app that owns the address).
105
+ */
106
+ function openLink(url, handler) {
107
+ if (handler && handler(url) === true) return;
108
+ try {
109
+ const { Linking } = require("react-native");
110
+ const p = Linking.openURL(url);
111
+ if (p && p.catch) p.catch(() => {});
112
+ } catch (_) { /* no way to open it */ }
113
+ }
114
+
115
+ module.exports = { linksIn, withoutLinks, linkHits, richParts, hostOf, cardTexts, openLink };