@trainly/react 1.0.0 → 1.0.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/dist/TrainlyProvider.d.ts +2 -2
- package/dist/TrainlyProvider.js +16 -20
- package/dist/api/TrainlyClient.js +1 -4
- package/dist/components/TrainlyChat.js +13 -16
- package/dist/components/TrainlyStatus.js +5 -8
- package/dist/components/TrainlyUpload.js +11 -14
- package/dist/index.js +5 -13
- package/dist/types.js +1 -2
- package/dist/useTrainly.js +3 -6
- package/package.json +1 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as React from "react";
|
|
2
2
|
import { TrainlyContextValue } from "./types";
|
|
3
3
|
export interface TrainlyProviderProps {
|
|
4
|
-
children: ReactNode;
|
|
4
|
+
children: React.ReactNode;
|
|
5
5
|
appSecret?: string;
|
|
6
6
|
apiKey?: string;
|
|
7
7
|
baseUrl?: string;
|
package/dist/TrainlyProvider.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
2
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
3
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -44,18 +43,15 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
|
44
43
|
}
|
|
45
44
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
46
45
|
};
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
var
|
|
51
|
-
|
|
52
|
-
var TrainlyClient_1 = require("./api/TrainlyClient");
|
|
53
|
-
var TrainlyContext = (0, react_1.createContext)(undefined);
|
|
54
|
-
function TrainlyProvider(_a) {
|
|
46
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
47
|
+
import * as React from "react";
|
|
48
|
+
import { TrainlyClient } from "./api/TrainlyClient";
|
|
49
|
+
var TrainlyContext = React.createContext(undefined);
|
|
50
|
+
export function TrainlyProvider(_a) {
|
|
55
51
|
var _this = this;
|
|
56
|
-
var children = _a.children, appSecret = _a.appSecret, apiKey = _a.apiKey, _b = _a.baseUrl, baseUrl = _b === void 0 ? "
|
|
57
|
-
var client =
|
|
58
|
-
return new
|
|
52
|
+
var children = _a.children, appSecret = _a.appSecret, apiKey = _a.apiKey, _b = _a.baseUrl, baseUrl = _b === void 0 ? "http://localhost:8000" : _b, userId = _a.userId, userEmail = _a.userEmail;
|
|
53
|
+
var client = React.useState(function () {
|
|
54
|
+
return new TrainlyClient({
|
|
59
55
|
appSecret: appSecret,
|
|
60
56
|
apiKey: apiKey,
|
|
61
57
|
baseUrl: baseUrl,
|
|
@@ -63,12 +59,12 @@ function TrainlyProvider(_a) {
|
|
|
63
59
|
userEmail: userEmail,
|
|
64
60
|
});
|
|
65
61
|
})[0];
|
|
66
|
-
var _c =
|
|
67
|
-
var _d =
|
|
68
|
-
var _e =
|
|
69
|
-
var _f =
|
|
62
|
+
var _c = React.useState(false), isLoading = _c[0], setIsLoading = _c[1];
|
|
63
|
+
var _d = React.useState(false), isConnected = _d[0], setIsConnected = _d[1];
|
|
64
|
+
var _e = React.useState(null), error = _e[0], setError = _e[1];
|
|
65
|
+
var _f = React.useState([]), messages = _f[0], setMessages = _f[1];
|
|
70
66
|
// Auto-connect on mount
|
|
71
|
-
|
|
67
|
+
React.useEffect(function () {
|
|
72
68
|
connect();
|
|
73
69
|
}, []);
|
|
74
70
|
var connect = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
@@ -239,10 +235,10 @@ function TrainlyProvider(_a) {
|
|
|
239
235
|
sendMessage: sendMessage,
|
|
240
236
|
clearMessages: clearMessages,
|
|
241
237
|
};
|
|
242
|
-
return ((
|
|
238
|
+
return (_jsx(TrainlyContext.Provider, { value: value, children: children }));
|
|
243
239
|
}
|
|
244
|
-
function useTrainlyContext() {
|
|
245
|
-
var context =
|
|
240
|
+
export function useTrainlyContext() {
|
|
241
|
+
var context = React.useContext(TrainlyContext);
|
|
246
242
|
if (context === undefined) {
|
|
247
243
|
throw new Error("useTrainlyContext must be used within a TrainlyProvider");
|
|
248
244
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
2
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
3
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -35,8 +34,6 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
35
34
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
35
|
}
|
|
37
36
|
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.TrainlyClient = void 0;
|
|
40
37
|
var TrainlyClient = /** @class */ (function () {
|
|
41
38
|
function TrainlyClient(config) {
|
|
42
39
|
this.scopedToken = null;
|
|
@@ -223,4 +220,4 @@ var TrainlyClient = /** @class */ (function () {
|
|
|
223
220
|
};
|
|
224
221
|
return TrainlyClient;
|
|
225
222
|
}());
|
|
226
|
-
|
|
223
|
+
export { TrainlyClient };
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
2
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
3
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -35,25 +34,23 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
35
34
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
35
|
}
|
|
37
36
|
};
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
var useTrainly_1 = require("../useTrainly");
|
|
43
|
-
function TrainlyChat(_a) {
|
|
37
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
38
|
+
import * as React from "react";
|
|
39
|
+
import { useTrainly } from "../useTrainly";
|
|
40
|
+
export function TrainlyChat(_a) {
|
|
44
41
|
var _this = this;
|
|
45
42
|
var _b = _a.height, height = _b === void 0 ? "400px" : _b, _c = _a.className, className = _c === void 0 ? "" : _c, _d = _a.placeholder, placeholder = _d === void 0 ? "Ask me anything..." : _d, _e = _a.showCitations, showCitations = _e === void 0 ? true : _e, _f = _a.enableFileUpload, enableFileUpload = _f === void 0 ? true : _f, _g = _a.theme, theme = _g === void 0 ? "auto" : _g, onMessage = _a.onMessage, onError = _a.onError;
|
|
46
|
-
var _h =
|
|
47
|
-
var _j =
|
|
48
|
-
var messagesEndRef =
|
|
49
|
-
var fileInputRef =
|
|
43
|
+
var _h = useTrainly(), messages = _h.messages, sendMessage = _h.sendMessage, isLoading = _h.isLoading, error = _h.error, clearError = _h.clearError;
|
|
44
|
+
var _j = React.useState(""), input = _j[0], setInput = _j[1];
|
|
45
|
+
var messagesEndRef = React.useRef(null);
|
|
46
|
+
var fileInputRef = React.useRef(null);
|
|
50
47
|
// Auto-scroll to bottom
|
|
51
|
-
|
|
48
|
+
React.useEffect(function () {
|
|
52
49
|
var _a;
|
|
53
50
|
(_a = messagesEndRef.current) === null || _a === void 0 ? void 0 : _a.scrollIntoView({ behavior: "smooth" });
|
|
54
51
|
}, [messages]);
|
|
55
52
|
// Handle errors
|
|
56
|
-
|
|
53
|
+
React.useEffect(function () {
|
|
57
54
|
if (error && onError) {
|
|
58
55
|
onError(error.message);
|
|
59
56
|
}
|
|
@@ -93,9 +90,9 @@ function TrainlyChat(_a) {
|
|
|
93
90
|
}
|
|
94
91
|
};
|
|
95
92
|
var baseClasses = "\n flex flex-col border border-gray-200 rounded-lg overflow-hidden\n ".concat(theme === "dark" ? "bg-gray-900 text-white border-gray-700" : "bg-white text-gray-900", "\n ").concat(className, "\n ");
|
|
96
|
-
return ((
|
|
93
|
+
return (_jsxs("div", { className: baseClasses, style: { height: height }, children: [_jsxs("div", { className: "flex-1 overflow-y-auto p-4 space-y-4", children: [messages.length === 0 && (_jsx("div", { className: "text-center text-gray-500 py-8", children: _jsx("p", { children: "Start a conversation by asking a question!" }) })), messages.map(function (message) { return (_jsx("div", { className: "flex ".concat(message.role === "user" ? "justify-end" : "justify-start"), children: _jsxs("div", { className: "\n max-w-[80%] p-3 rounded-lg\n ".concat(message.role === "user"
|
|
97
94
|
? "bg-blue-500 text-white rounded-br-none"
|
|
98
|
-
: "bg-gray-100 text-gray-900 rounded-bl-none", "\n "), children: [(
|
|
95
|
+
: "bg-gray-100 text-gray-900 rounded-bl-none", "\n "), children: [_jsx("p", { className: "whitespace-pre-wrap", children: message.content }), showCitations &&
|
|
99
96
|
message.citations &&
|
|
100
|
-
message.citations.length > 0 && ((
|
|
97
|
+
message.citations.length > 0 && (_jsxs("div", { className: "mt-3 pt-3 border-t border-gray-300", children: [_jsx("p", { className: "text-xs font-semibold mb-2", children: "Sources:" }), message.citations.map(function (citation, idx) { return (_jsxs("div", { className: "text-xs bg-white bg-opacity-20 p-2 rounded mb-1", children: [_jsx("p", { className: "font-medium", children: citation.source }), _jsxs("p", { className: "opacity-75", children: ["\"", citation.snippet, "\""] })] }, idx)); })] }))] }) }, message.id)); }), isLoading && (_jsx("div", { className: "flex justify-start", children: _jsx("div", { className: "bg-gray-100 text-gray-900 p-3 rounded-lg rounded-bl-none", children: _jsxs("div", { className: "flex items-center space-x-2", children: [_jsx("div", { className: "animate-spin rounded-full h-4 w-4 border-b-2 border-blue-500" }), _jsx("span", { children: "Thinking..." })] }) }) })), _jsx("div", { ref: messagesEndRef })] }), error && (_jsx("div", { className: "px-4 py-2 bg-red-50 border-t border-red-200 text-red-700 text-sm", children: _jsxs("div", { className: "flex justify-between items-center", children: [_jsx("span", { children: error.message }), _jsx("button", { onClick: clearError, className: "text-red-500 hover:text-red-700", children: "\u00D7" })] }) })), _jsx("div", { className: "border-t border-gray-200 p-4", children: _jsxs("form", { onSubmit: handleSubmit, className: "flex gap-2", children: [enableFileUpload && (_jsxs(_Fragment, { children: [_jsx("input", { ref: fileInputRef, type: "file", accept: ".pdf,.doc,.docx,.txt,.md", onChange: handleFileUpload, className: "hidden" }), _jsx("button", { type: "button", onClick: function () { var _a; return (_a = fileInputRef.current) === null || _a === void 0 ? void 0 : _a.click(); }, className: "px-3 py-2 text-gray-500 hover:text-gray-700 border border-gray-300 rounded-lg", title: "Upload file", children: "\uD83D\uDCCE" })] })), _jsx("input", { type: "text", value: input, onChange: function (e) { return setInput(e.target.value); }, placeholder: placeholder, disabled: isLoading, className: "flex-1 px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" }), _jsx("button", { type: "submit", disabled: isLoading || !input.trim(), className: "px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 disabled:opacity-50 disabled:cursor-not-allowed", children: "Send" })] }) })] }));
|
|
101
98
|
}
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
-
var useTrainly_1 = require("../useTrainly");
|
|
6
|
-
function TrainlyStatus(_a) {
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useTrainly } from "../useTrainly";
|
|
3
|
+
export function TrainlyStatus(_a) {
|
|
7
4
|
var _b = _a.showDetails, showDetails = _b === void 0 ? true : _b, _c = _a.position, position = _c === void 0 ? "inline" : _c, _d = _a.className, className = _d === void 0 ? "" : _d;
|
|
8
|
-
var _e =
|
|
5
|
+
var _e = useTrainly(), isConnected = _e.isConnected, isLoading = _e.isLoading, error = _e.error;
|
|
9
6
|
var getStatusInfo = function () {
|
|
10
7
|
if (error) {
|
|
11
8
|
return {
|
|
@@ -47,5 +44,5 @@ function TrainlyStatus(_a) {
|
|
|
47
44
|
inline: "",
|
|
48
45
|
};
|
|
49
46
|
var baseClasses = "\n inline-flex items-center gap-2 px-3 py-2 rounded-lg border text-sm font-medium\n ".concat(status.color, "\n ").concat(positionClasses[position], "\n ").concat(className, "\n ");
|
|
50
|
-
return ((
|
|
47
|
+
return (_jsxs("div", { className: baseClasses, children: [_jsx("span", { className: status.icon === "🔄" ? "animate-spin" : "", children: status.icon }), _jsx("span", { children: status.text }), showDetails && status.details && (_jsxs("span", { className: "text-xs opacity-75 ml-1", children: ["- ", status.details] }))] }));
|
|
51
48
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
2
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
3
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -35,17 +34,15 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
35
34
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
35
|
}
|
|
37
36
|
};
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
var useTrainly_1 = require("../useTrainly");
|
|
43
|
-
function TrainlyUpload(_a) {
|
|
37
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
38
|
+
import * as React from "react";
|
|
39
|
+
import { useTrainly } from "../useTrainly";
|
|
40
|
+
export function TrainlyUpload(_a) {
|
|
44
41
|
var _this = this;
|
|
45
42
|
var _b = _a.variant, variant = _b === void 0 ? "drag-drop" : _b, _c = _a.accept, accept = _c === void 0 ? ".pdf,.doc,.docx,.txt,.md" : _c, _d = _a.maxSize, maxSize = _d === void 0 ? "10MB" : _d, _e = _a.multiple, multiple = _e === void 0 ? false : _e, _f = _a.className, className = _f === void 0 ? "" : _f, onUpload = _a.onUpload, onError = _a.onError;
|
|
46
|
-
var _g =
|
|
47
|
-
var _h =
|
|
48
|
-
var fileInputRef =
|
|
43
|
+
var _g = useTrainly(), upload = _g.upload, isLoading = _g.isLoading;
|
|
44
|
+
var _h = React.useState(false), isDragOver = _h[0], setIsDragOver = _h[1];
|
|
45
|
+
var fileInputRef = React.useRef(null);
|
|
49
46
|
var maxSizeBytes = parseMaxSize(maxSize);
|
|
50
47
|
var handleFiles = function (files) { return __awaiter(_this, void 0, void 0, function () {
|
|
51
48
|
var fileArray, _i, fileArray_1, file, error, _a, fileArray_2, file, err_1;
|
|
@@ -107,15 +104,15 @@ function TrainlyUpload(_a) {
|
|
|
107
104
|
handleFiles(e.target.files);
|
|
108
105
|
};
|
|
109
106
|
if (variant === "button") {
|
|
110
|
-
return ((
|
|
107
|
+
return (_jsxs(_Fragment, { children: [_jsx("input", { ref: fileInputRef, type: "file", accept: accept, multiple: multiple, onChange: handleFileSelect, className: "hidden" }), _jsx("button", { onClick: function () { var _a; return (_a = fileInputRef.current) === null || _a === void 0 ? void 0 : _a.click(); }, disabled: isLoading, className: "\n px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600\n disabled:opacity-50 disabled:cursor-not-allowed\n ".concat(className, "\n "), children: isLoading ? "Uploading..." : "Upload Files" })] }));
|
|
111
108
|
}
|
|
112
109
|
if (variant === "minimal") {
|
|
113
|
-
return ((
|
|
110
|
+
return (_jsxs(_Fragment, { children: [_jsx("input", { ref: fileInputRef, type: "file", accept: accept, multiple: multiple, onChange: handleFileSelect, className: "hidden" }), _jsx("button", { onClick: function () { var _a; return (_a = fileInputRef.current) === null || _a === void 0 ? void 0 : _a.click(); }, disabled: isLoading, className: "text-blue-500 hover:text-blue-600 underline ".concat(className), children: isLoading ? "Uploading..." : "Upload" })] }));
|
|
114
111
|
}
|
|
115
112
|
// Default: drag-drop variant
|
|
116
|
-
return ((
|
|
113
|
+
return (_jsxs(_Fragment, { children: [_jsx("input", { ref: fileInputRef, type: "file", accept: accept, multiple: multiple, onChange: handleFileSelect, className: "hidden" }), _jsx("div", { onDrop: handleDrop, onDragOver: handleDragOver, onDragLeave: handleDragLeave, onClick: function () { var _a; return (_a = fileInputRef.current) === null || _a === void 0 ? void 0 : _a.click(); }, className: "\n border-2 border-dashed border-gray-300 rounded-lg p-8 text-center cursor-pointer\n transition-colors duration-200\n ".concat(isDragOver ? "border-blue-500 bg-blue-50" : "hover:border-gray-400", "\n ").concat(isLoading ? "opacity-50 cursor-not-allowed" : "", "\n ").concat(className, "\n "), children: _jsxs("div", { className: "space-y-4", children: [_jsx("div", { className: "text-4xl", children: "\uD83D\uDCC4" }), _jsxs("div", { children: [_jsx("p", { className: "text-lg font-medium text-gray-700", children: isLoading
|
|
117
114
|
? "Uploading..."
|
|
118
|
-
: "Drop files here or click to upload" }), (
|
|
115
|
+
: "Drop files here or click to upload" }), _jsxs("p", { className: "text-sm text-gray-500 mt-2", children: ["Supports: ", accept.replace(/\./g, "").toUpperCase(), " files up to", " ", maxSize] })] })] }) })] }));
|
|
119
116
|
}
|
|
120
117
|
function parseMaxSize(maxSize) {
|
|
121
118
|
var match = maxSize.match(/^(\d+)\s*(GB|MB|KB)?$/i);
|
package/dist/index.js
CHANGED
|
@@ -1,15 +1,7 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TrainlyStatus = exports.TrainlyUpload = exports.TrainlyChat = exports.useTrainly = exports.TrainlyProvider = void 0;
|
|
4
1
|
// Main exports for @trainly/react SDK
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var useTrainly_1 = require("./useTrainly");
|
|
8
|
-
Object.defineProperty(exports, "useTrainly", { enumerable: true, get: function () { return useTrainly_1.useTrainly; } });
|
|
2
|
+
export { TrainlyProvider } from "./TrainlyProvider";
|
|
3
|
+
export { useTrainly } from "./useTrainly";
|
|
9
4
|
// Pre-built components
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
Object.defineProperty(exports, "TrainlyUpload", { enumerable: true, get: function () { return TrainlyUpload_1.TrainlyUpload; } });
|
|
14
|
-
var TrainlyStatus_1 = require("./components/TrainlyStatus");
|
|
15
|
-
Object.defineProperty(exports, "TrainlyStatus", { enumerable: true, get: function () { return TrainlyStatus_1.TrainlyStatus; } });
|
|
5
|
+
export { TrainlyChat } from "./components/TrainlyChat";
|
|
6
|
+
export { TrainlyUpload } from "./components/TrainlyUpload";
|
|
7
|
+
export { TrainlyStatus } from "./components/TrainlyStatus";
|
package/dist/types.js
CHANGED
package/dist/useTrainly.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useTrainly = useTrainly;
|
|
4
|
-
var TrainlyProvider_1 = require("./TrainlyProvider");
|
|
1
|
+
import { useTrainlyContext } from "./TrainlyProvider";
|
|
5
2
|
/**
|
|
6
3
|
* Main hook for using Trainly in your React components
|
|
7
4
|
*
|
|
@@ -19,6 +16,6 @@ var TrainlyProvider_1 = require("./TrainlyProvider");
|
|
|
19
16
|
* }
|
|
20
17
|
* ```
|
|
21
18
|
*/
|
|
22
|
-
function useTrainly() {
|
|
23
|
-
return
|
|
19
|
+
export function useTrainly() {
|
|
20
|
+
return useTrainlyContext();
|
|
24
21
|
}
|