@figoailabs/figochat-react-native 0.0.0-stage → 1.0.0
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 +10 -0
- package/LICENSE +21 -0
- package/README.md +124 -2
- package/lib/FigoChat.d.ts +24 -0
- package/lib/FigoChat.js +76 -0
- package/lib/FigoChatView.d.ts +26 -0
- package/lib/FigoChatView.js +417 -0
- package/lib/client.d.ts +49 -0
- package/lib/client.js +211 -0
- package/lib/colors.d.ts +4 -0
- package/lib/colors.js +27 -0
- package/lib/crypto.d.ts +13 -0
- package/lib/crypto.js +58 -0
- package/lib/html.d.ts +6 -0
- package/lib/html.js +23 -0
- package/lib/icons.d.ts +17 -0
- package/lib/icons.js +48 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +17 -0
- package/lib/messages.d.ts +8 -0
- package/lib/messages.js +37 -0
- package/lib/realtime.d.ts +36 -0
- package/lib/realtime.js +81 -0
- package/lib/types.d.ts +77 -0
- package/lib/types.js +2 -0
- package/package.json +56 -4
- package/src/FigoChat.ts +92 -0
- package/src/FigoChatView.tsx +650 -0
- package/src/client.ts +246 -0
- package/src/colors.ts +24 -0
- package/src/crypto.ts +58 -0
- package/src/html.ts +20 -0
- package/src/icons.tsx +64 -0
- package/src/index.ts +17 -0
- package/src/messages.ts +40 -0
- package/src/realtime.ts +91 -0
- package/src/types.ts +85 -0
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/** Options passed to `FigoChat.initialize`. */
|
|
2
|
+
export interface FigoChatConfig {
|
|
3
|
+
/** API key created in the Figo AI dashboard (Settings → API Keys). Sent as `Authorization: Bearer <apiKey>`. */
|
|
4
|
+
apiKey: string;
|
|
5
|
+
/** Id of the assistant the user chats with. */
|
|
6
|
+
assistantId: string;
|
|
7
|
+
/** Full name of the signed-in user of the host app. */
|
|
8
|
+
fullName: string;
|
|
9
|
+
/** Email address of the signed-in user of the host app. */
|
|
10
|
+
emailAddress: string;
|
|
11
|
+
/** Mobile phone number of the signed-in user, ideally in international format (e.g. +2348012345678). */
|
|
12
|
+
mobilePhone: string;
|
|
13
|
+
/** Brand color for the header, user bubbles and buttons, as a hex string such as `#F97316`. */
|
|
14
|
+
primaryColor: string;
|
|
15
|
+
/** API base url. Defaults to `https://api.figolabs.ai`. */
|
|
16
|
+
baseUrl?: string;
|
|
17
|
+
/** Title shown in the chat header. Defaults to `Chat`. */
|
|
18
|
+
title?: string;
|
|
19
|
+
/** First assistant message shown before the user writes anything. */
|
|
20
|
+
welcomeMessage?: string;
|
|
21
|
+
/** Continue an earlier conversation by passing the thread id from `FigoChat.getThreadId()`. */
|
|
22
|
+
threadId?: string;
|
|
23
|
+
/** Most files per message. Defaults to 5. */
|
|
24
|
+
maxFiles?: number;
|
|
25
|
+
/** Largest file accepted, in megabytes. Defaults to 10. */
|
|
26
|
+
maxFileSizeMB?: number;
|
|
27
|
+
/** Send the transcribed text straight away after a voice note. When false it is placed in the input for editing. Defaults to true. */
|
|
28
|
+
autoSendVoice?: boolean;
|
|
29
|
+
/** Request timeout in milliseconds. Defaults to 120000. */
|
|
30
|
+
timeoutMs?: number;
|
|
31
|
+
}
|
|
32
|
+
/** A file picked by the user, ready to upload. */
|
|
33
|
+
export interface FigoLocalFile {
|
|
34
|
+
uri: string;
|
|
35
|
+
name: string;
|
|
36
|
+
mimeType?: string;
|
|
37
|
+
size?: number;
|
|
38
|
+
}
|
|
39
|
+
/** An attachment on a chat message. */
|
|
40
|
+
export interface FigoAttachment {
|
|
41
|
+
/** CDN url returned by the upload. */
|
|
42
|
+
url: string;
|
|
43
|
+
name: string;
|
|
44
|
+
mimeType?: string;
|
|
45
|
+
/** Local uri, used for image previews before the CDN copy loads. */
|
|
46
|
+
localUri?: string;
|
|
47
|
+
}
|
|
48
|
+
export type FigoMessageRole = 'user' | 'assistant' | 'system';
|
|
49
|
+
export interface FigoChatMessage {
|
|
50
|
+
id: string;
|
|
51
|
+
role: FigoMessageRole;
|
|
52
|
+
text: string;
|
|
53
|
+
attachments?: FigoAttachment[];
|
|
54
|
+
createdAt: Date;
|
|
55
|
+
/** `sending` while the request is in flight, `error` when it failed. */
|
|
56
|
+
status?: 'sending' | 'sent' | 'error';
|
|
57
|
+
}
|
|
58
|
+
export interface FigoChatReply {
|
|
59
|
+
answer: string;
|
|
60
|
+
threadId?: string;
|
|
61
|
+
/** `ai` normally, `liveagent` once the conversation has been handed to a human. */
|
|
62
|
+
threadMode?: string;
|
|
63
|
+
}
|
|
64
|
+
/** Public realtime settings from GET /api/v1/realtime/config. */
|
|
65
|
+
export interface FigoRealtimeConfig {
|
|
66
|
+
provider: string;
|
|
67
|
+
key: string;
|
|
68
|
+
cluster: string;
|
|
69
|
+
/** Event bound on the thread channel; its payload is { message: string }. */
|
|
70
|
+
eventName: string;
|
|
71
|
+
}
|
|
72
|
+
/** Standard envelope returned by the Figo AI REST APIs. */
|
|
73
|
+
export interface DefaultServiceResponse<T> {
|
|
74
|
+
responseCode: string;
|
|
75
|
+
responseMessage: string;
|
|
76
|
+
responseData: T;
|
|
77
|
+
}
|
package/lib/types.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,58 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@figoailabs/figochat-react-native",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "FigoChat SDK for React Native: chat with a Figo AI assistant, with voice messages and file uploads.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Figo AI Labs",
|
|
7
|
+
"homepage": "https://figolabs.ai",
|
|
8
|
+
"main": "lib/index.js",
|
|
9
|
+
"types": "lib/index.d.ts",
|
|
10
|
+
"react-native": "src/index.ts",
|
|
11
|
+
"source": "src/index.ts",
|
|
12
|
+
"files": [
|
|
13
|
+
"src",
|
|
14
|
+
"lib",
|
|
15
|
+
"README.md",
|
|
16
|
+
"CHANGELOG.md",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc -p tsconfig.json",
|
|
21
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
22
|
+
"prepare": "npm run build",
|
|
23
|
+
"prepublishOnly": "npm run typecheck && npm run build"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"figo",
|
|
27
|
+
"figochat",
|
|
28
|
+
"chat",
|
|
29
|
+
"ai",
|
|
30
|
+
"assistant",
|
|
31
|
+
"react-native",
|
|
32
|
+
"sdk"
|
|
33
|
+
],
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"expo-audio": ">=0.4.0",
|
|
36
|
+
"expo-document-picker": ">=13.0.0",
|
|
37
|
+
"react": ">=18.0.0",
|
|
38
|
+
"react-native": ">=0.74.0",
|
|
39
|
+
"@react-native-community/netinfo": ">=9.0.0"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@react-native-community/netinfo": "^11.4.1",
|
|
43
|
+
"@types/react": "^19.0.0",
|
|
44
|
+
"expo-audio": "~1.0.0",
|
|
45
|
+
"expo-document-picker": "~14.0.0",
|
|
46
|
+
"react": "19.1.0",
|
|
47
|
+
"react-native": "0.81.4",
|
|
48
|
+
"typescript": "~5.9.2",
|
|
49
|
+
"@types/crypto-js": "^4.2.2"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"crypto-js": "^4.2.0",
|
|
53
|
+
"pusher-js": "^8.4.0"
|
|
54
|
+
},
|
|
55
|
+
"publishConfig": {
|
|
56
|
+
"access": "public"
|
|
57
|
+
}
|
|
58
|
+
}
|
package/src/FigoChat.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { FigoChatClient, FigoChatError } from './client';
|
|
2
|
+
import { FigoCrypto } from './crypto';
|
|
3
|
+
import { FigoRealtimeConfigStore } from './realtime';
|
|
4
|
+
import type { FigoChatConfig } from './types';
|
|
5
|
+
|
|
6
|
+
const HEX_COLOR = /^#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
|
|
7
|
+
|
|
8
|
+
let current: {
|
|
9
|
+
config: FigoChatConfig;
|
|
10
|
+
client: FigoChatClient;
|
|
11
|
+
/** realtime settings, fetched on first use and kept encrypted with a per-session key */
|
|
12
|
+
realtime: FigoRealtimeConfigStore;
|
|
13
|
+
} | null = null;
|
|
14
|
+
const listeners = new Set<() => void>();
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Entry point of the SDK. Call `FigoChat.initialize(...)` once (for example after the user signs in),
|
|
18
|
+
* then render `<FigoChatView />`.
|
|
19
|
+
*/
|
|
20
|
+
export const FigoChat = {
|
|
21
|
+
initialize(config: FigoChatConfig): void {
|
|
22
|
+
const required: (keyof FigoChatConfig)[] = ['apiKey', 'assistantId', 'fullName', 'emailAddress', 'mobilePhone', 'primaryColor'];
|
|
23
|
+
const missing = required.filter((k) => !String(config[k] ?? '').trim());
|
|
24
|
+
if (missing.length) {
|
|
25
|
+
throw new FigoChatError(`FigoChat.initialize: missing ${missing.join(', ')}.`, 'CONFIG');
|
|
26
|
+
}
|
|
27
|
+
if (!HEX_COLOR.test(config.primaryColor.trim())) {
|
|
28
|
+
throw new FigoChatError('FigoChat.initialize: primaryColor must be a hex color such as #F97316.', 'CONFIG');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const normalized = {
|
|
32
|
+
...config,
|
|
33
|
+
apiKey: config.apiKey.trim(),
|
|
34
|
+
assistantId: config.assistantId.trim(),
|
|
35
|
+
fullName: config.fullName.trim(),
|
|
36
|
+
emailAddress: config.emailAddress.trim(),
|
|
37
|
+
mobilePhone: config.mobilePhone.trim(),
|
|
38
|
+
primaryColor: config.primaryColor.trim(),
|
|
39
|
+
};
|
|
40
|
+
const client = new FigoChatClient(normalized);
|
|
41
|
+
current = { config: normalized, client, realtime: new FigoRealtimeConfigStore(client, new FigoCrypto()) };
|
|
42
|
+
listeners.forEach((l) => l());
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
isInitialized(): boolean {
|
|
46
|
+
return current !== null;
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
/** The API client, for apps that want to build their own UI. */
|
|
50
|
+
getClient(): FigoChatClient {
|
|
51
|
+
return requireInitialized().client;
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
getConfig(): FigoChatConfig {
|
|
55
|
+
return requireInitialized().config;
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
/** @internal encrypted realtime settings used by FigoChatView to subscribe to the thread channel. */
|
|
59
|
+
getRealtimeStore(): FigoRealtimeConfigStore {
|
|
60
|
+
return requireInitialized().realtime;
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
/** Thread id of the current conversation; store it and pass it as `threadId` to resume later. */
|
|
64
|
+
getThreadId(): string | undefined {
|
|
65
|
+
return current?.client.getThreadId();
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
/** Starts a new conversation on the next message. */
|
|
69
|
+
resetConversation(): void {
|
|
70
|
+
current?.client.resetThread();
|
|
71
|
+
listeners.forEach((l) => l());
|
|
72
|
+
},
|
|
73
|
+
|
|
74
|
+
/** Clears the configuration, e.g. when the user signs out. */
|
|
75
|
+
logout(): void {
|
|
76
|
+
current = null;
|
|
77
|
+
listeners.forEach((l) => l());
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
/** @internal used by FigoChatView to re-render after initialize/reset/logout. */
|
|
81
|
+
subscribe(listener: () => void): () => void {
|
|
82
|
+
listeners.add(listener);
|
|
83
|
+
return () => listeners.delete(listener);
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
function requireInitialized() {
|
|
88
|
+
if (!current) {
|
|
89
|
+
throw new FigoChatError('FigoChat is not initialized. Call FigoChat.initialize(...) first.', 'NOT_INITIALIZED');
|
|
90
|
+
}
|
|
91
|
+
return current;
|
|
92
|
+
}
|