@trainly/react 1.0.1 → 1.0.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.
@@ -1,3 +1,4 @@
1
+ "use client";
1
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -6,6 +6,7 @@ interface QueryResponse {
6
6
  export declare class TrainlyClient {
7
7
  private config;
8
8
  private scopedToken;
9
+ private currentUserId;
9
10
  constructor(config: TrainlyConfig);
10
11
  connect(): Promise<void>;
11
12
  ask(question: string, options?: {
@@ -1,3 +1,14 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
1
12
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
13
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
14
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -37,6 +48,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
37
48
  var TrainlyClient = /** @class */ (function () {
38
49
  function TrainlyClient(config) {
39
50
  this.scopedToken = null;
51
+ this.currentUserId = null;
40
52
  this.config = config;
41
53
  }
42
54
  TrainlyClient.prototype.connect = function () {
@@ -53,6 +65,11 @@ var TrainlyClient = /** @class */ (function () {
53
65
  if (!this.config.appSecret) {
54
66
  throw new Error("Either appSecret or apiKey must be provided");
55
67
  }
68
+ // App secret mode - provision user
69
+ // Ensure we use the same user ID consistently
70
+ if (!this.currentUserId) {
71
+ this.currentUserId = this.config.userId || this.generateAnonymousId();
72
+ }
56
73
  return [4 /*yield*/, fetch("".concat(this.config.baseUrl, "/v1/privacy/apps/users/provision"), {
57
74
  method: "POST",
58
75
  headers: {
@@ -60,7 +77,7 @@ var TrainlyClient = /** @class */ (function () {
60
77
  "Content-Type": "application/json",
61
78
  },
62
79
  body: JSON.stringify({
63
- end_user_id: this.config.userId || this.generateAnonymousId(),
80
+ end_user_id: this.currentUserId,
64
81
  capabilities: ["ask", "upload"],
65
82
  }),
66
83
  })];
@@ -102,7 +119,9 @@ var TrainlyClient = /** @class */ (function () {
102
119
  }
103
120
  else {
104
121
  headers["x-scoped-token"] = this.scopedToken;
105
- body.end_user_id = this.config.userId || this.generateAnonymousId();
122
+ // Use the same user ID that was used during provisioning
123
+ body.end_user_id =
124
+ this.currentUserId || this.config.userId || this.generateAnonymousId();
106
125
  body.include_citations = options.includeCitations || false;
107
126
  }
108
127
  return [4 /*yield*/, fetch(url, {
@@ -130,9 +149,9 @@ var TrainlyClient = /** @class */ (function () {
130
149
  };
131
150
  TrainlyClient.prototype.upload = function (file) {
132
151
  return __awaiter(this, void 0, void 0, function () {
133
- var formData, response, error, presignedResponse, error, upload_url, uploadResponse;
134
- return __generator(this, function (_a) {
135
- switch (_a.label) {
152
+ var formData, response, error, presignedResponse, error, _a, upload_url, upload_headers, formData, uploadResponse;
153
+ return __generator(this, function (_b) {
154
+ switch (_b.label) {
136
155
  case 0:
137
156
  if (!this.scopedToken) {
138
157
  throw new Error("Not connected. Call connect() first.");
@@ -148,11 +167,11 @@ var TrainlyClient = /** @class */ (function () {
148
167
  body: formData,
149
168
  })];
150
169
  case 1:
151
- response = _a.sent();
170
+ response = _b.sent();
152
171
  if (!!response.ok) return [3 /*break*/, 3];
153
172
  return [4 /*yield*/, response.json()];
154
173
  case 2:
155
- error = _a.sent();
174
+ error = _b.sent();
156
175
  throw new Error("Upload failed: ".concat(error.detail || response.statusText));
157
176
  case 3: return [2 /*return*/, {
158
177
  success: true,
@@ -167,30 +186,32 @@ var TrainlyClient = /** @class */ (function () {
167
186
  "x-scoped-token": this.scopedToken,
168
187
  },
169
188
  body: JSON.stringify({
170
- end_user_id: this.config.userId || this.generateAnonymousId(),
189
+ end_user_id: this.currentUserId ||
190
+ this.config.userId ||
191
+ this.generateAnonymousId(),
171
192
  filename: file.name,
172
193
  file_type: file.type,
173
194
  }),
174
195
  })];
175
196
  case 5:
176
- presignedResponse = _a.sent();
197
+ presignedResponse = _b.sent();
177
198
  if (!!presignedResponse.ok) return [3 /*break*/, 7];
178
199
  return [4 /*yield*/, presignedResponse.json()];
179
200
  case 6:
180
- error = _a.sent();
201
+ error = _b.sent();
181
202
  throw new Error("Failed to get upload URL: ".concat(error.detail || presignedResponse.statusText));
182
203
  case 7: return [4 /*yield*/, presignedResponse.json()];
183
204
  case 8:
184
- upload_url = (_a.sent()).upload_url;
205
+ _a = _b.sent(), upload_url = _a.upload_url, upload_headers = _a.upload_headers;
206
+ formData = new FormData();
207
+ formData.append("file", file);
185
208
  return [4 /*yield*/, fetch(upload_url, {
186
- method: "PUT",
187
- body: file,
188
- headers: {
189
- "Content-Type": file.type,
190
- },
209
+ method: "POST",
210
+ body: formData,
211
+ headers: __assign({}, upload_headers),
191
212
  })];
192
213
  case 9:
193
- uploadResponse = _a.sent();
214
+ uploadResponse = _b.sent();
194
215
  if (!uploadResponse.ok) {
195
216
  throw new Error("Failed to upload file");
196
217
  }
@@ -1,3 +1,4 @@
1
+ "use client";
1
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -1,3 +1,4 @@
1
+ "use client";
1
2
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
3
  import { useTrainly } from "../useTrainly";
3
4
  export function TrainlyStatus(_a) {
@@ -1,3 +1,4 @@
1
+ "use client";
1
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
4
  return new (P || (P = Promise))(function (resolve, reject) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trainly/react",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Dead simple RAG integration for React apps",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",