@trainly/react 1.1.0 → 1.1.2

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.
@@ -8,7 +8,8 @@ export interface TrainlyProviderProps {
8
8
  baseUrl?: string;
9
9
  userId?: string;
10
10
  userEmail?: string;
11
+ getToken?: () => Promise<string | null>;
11
12
  }
12
13
  export declare function TrainlyProvider({ children, appSecret, apiKey, appId, // NEW: For V1 authentication
13
- baseUrl, userId, userEmail, }: TrainlyProviderProps): import("react/jsx-runtime").JSX.Element;
14
+ baseUrl, userId, userEmail, getToken, }: TrainlyProviderProps): import("react/jsx-runtime").JSX.Element;
14
15
  export declare function useTrainlyContext(): TrainlyContextValue;
@@ -52,7 +52,7 @@ export function TrainlyProvider(_a) {
52
52
  var _this = this;
53
53
  var children = _a.children, appSecret = _a.appSecret, apiKey = _a.apiKey, appId = _a.appId, // NEW: For V1 authentication
54
54
  _b = _a.baseUrl, // NEW: For V1 authentication
55
- baseUrl = _b === void 0 ? "http://localhost:8000" : _b, userId = _a.userId, userEmail = _a.userEmail;
55
+ baseUrl = _b === void 0 ? "http://localhost:8000" : _b, userId = _a.userId, userEmail = _a.userEmail, getToken = _a.getToken;
56
56
  var client = React.useState(function () {
57
57
  return new TrainlyClient({
58
58
  appSecret: appSecret,
@@ -131,11 +131,11 @@ export function TrainlyProvider(_a) {
131
131
  });
132
132
  }); };
133
133
  var ask = function (question) { return __awaiter(_this, void 0, void 0, function () {
134
- var response, err_3, error_1;
134
+ var response, err_3, errorMessage, newToken, response, refreshError_1, error_1;
135
135
  return __generator(this, function (_a) {
136
136
  switch (_a.label) {
137
137
  case 0:
138
- _a.trys.push([0, 2, 3, 4]);
138
+ _a.trys.push([0, 2, 10, 11]);
139
139
  setIsLoading(true);
140
140
  setError(null);
141
141
  return [4 /*yield*/, client.ask(question)];
@@ -144,6 +144,34 @@ export function TrainlyProvider(_a) {
144
144
  return [2 /*return*/, response.answer];
145
145
  case 2:
146
146
  err_3 = _a.sent();
147
+ errorMessage = err_3 instanceof Error ? err_3.message : String(err_3);
148
+ if (!(getToken &&
149
+ appId &&
150
+ (errorMessage.includes("401") ||
151
+ errorMessage.includes("authentication") ||
152
+ errorMessage.includes("Unauthorized")))) return [3 /*break*/, 9];
153
+ _a.label = 3;
154
+ case 3:
155
+ _a.trys.push([3, 8, , 9]);
156
+ console.log("🔄 Token expired, refreshing...");
157
+ return [4 /*yield*/, getToken()];
158
+ case 4:
159
+ newToken = _a.sent();
160
+ if (!newToken) return [3 /*break*/, 7];
161
+ return [4 /*yield*/, client.connectWithOAuthToken(newToken)];
162
+ case 5:
163
+ _a.sent();
164
+ return [4 /*yield*/, client.ask(question)];
165
+ case 6:
166
+ response = _a.sent();
167
+ console.log("✅ Query succeeded after token refresh");
168
+ return [2 /*return*/, response.answer];
169
+ case 7: return [3 /*break*/, 9];
170
+ case 8:
171
+ refreshError_1 = _a.sent();
172
+ console.error("❌ Token refresh failed:", refreshError_1);
173
+ return [3 /*break*/, 9];
174
+ case 9:
147
175
  error_1 = {
148
176
  code: "QUERY_FAILED",
149
177
  message: "Failed to get answer",
@@ -151,10 +179,10 @@ export function TrainlyProvider(_a) {
151
179
  };
152
180
  setError(error_1);
153
181
  throw error_1;
154
- case 3:
182
+ case 10:
155
183
  setIsLoading(false);
156
184
  return [7 /*endfinally*/];
157
- case 4: return [2 /*return*/];
185
+ case 11: return [2 /*return*/];
158
186
  }
159
187
  });
160
188
  }); };
@@ -190,11 +218,11 @@ export function TrainlyProvider(_a) {
190
218
  });
191
219
  }); };
192
220
  var upload = function (file) { return __awaiter(_this, void 0, void 0, function () {
193
- var result, err_5, error_3;
221
+ var result, err_5, errorMessage, newToken, result, refreshError_2, error_3;
194
222
  return __generator(this, function (_a) {
195
223
  switch (_a.label) {
196
224
  case 0:
197
- _a.trys.push([0, 2, 3, 4]);
225
+ _a.trys.push([0, 2, 10, 11]);
198
226
  setIsLoading(true);
199
227
  setError(null);
200
228
  return [4 /*yield*/, client.upload(file)];
@@ -203,6 +231,34 @@ export function TrainlyProvider(_a) {
203
231
  return [2 /*return*/, result];
204
232
  case 2:
205
233
  err_5 = _a.sent();
234
+ errorMessage = err_5 instanceof Error ? err_5.message : String(err_5);
235
+ if (!(getToken &&
236
+ appId &&
237
+ (errorMessage.includes("401") ||
238
+ errorMessage.includes("authentication") ||
239
+ errorMessage.includes("Unauthorized")))) return [3 /*break*/, 9];
240
+ _a.label = 3;
241
+ case 3:
242
+ _a.trys.push([3, 8, , 9]);
243
+ console.log("🔄 Token expired during upload, refreshing...");
244
+ return [4 /*yield*/, getToken()];
245
+ case 4:
246
+ newToken = _a.sent();
247
+ if (!newToken) return [3 /*break*/, 7];
248
+ return [4 /*yield*/, client.connectWithOAuthToken(newToken)];
249
+ case 5:
250
+ _a.sent();
251
+ return [4 /*yield*/, client.upload(file)];
252
+ case 6:
253
+ result = _a.sent();
254
+ console.log("✅ Upload succeeded after token refresh");
255
+ return [2 /*return*/, result];
256
+ case 7: return [3 /*break*/, 9];
257
+ case 8:
258
+ refreshError_2 = _a.sent();
259
+ console.error("❌ Token refresh failed:", refreshError_2);
260
+ return [3 /*break*/, 9];
261
+ case 9:
206
262
  error_3 = {
207
263
  code: "UPLOAD_FAILED",
208
264
  message: "Failed to upload file",
@@ -210,10 +266,10 @@ export function TrainlyProvider(_a) {
210
266
  };
211
267
  setError(error_3);
212
268
  throw error_3;
213
- case 3:
269
+ case 10:
214
270
  setIsLoading(false);
215
271
  return [7 /*endfinally*/];
216
- case 4: return [2 /*return*/];
272
+ case 11: return [2 /*return*/];
217
273
  }
218
274
  });
219
275
  }); };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trainly/react",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "Dead simple RAG integration for React apps with OAuth authentication",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",