@resolveio/server-lib 20.14.11 → 20.14.13

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,4 +1,19 @@
1
1
  "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
2
17
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
18
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
19
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -46,12 +61,31 @@ var __values = (this && this.__values) || function(o) {
46
61
  };
47
62
  throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
48
63
  };
64
+ var __read = (this && this.__read) || function (o, n) {
65
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
66
+ if (!m) return o;
67
+ var i = m.call(o), r, ar = [], e;
68
+ try {
69
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
70
+ }
71
+ catch (error) { e = { error: error }; }
72
+ finally {
73
+ try {
74
+ if (r && !r.done && (m = i["return"])) m.call(i);
75
+ }
76
+ finally { if (e) throw e.error; }
77
+ }
78
+ return ar;
79
+ };
49
80
  Object.defineProperty(exports, "__esModule", { value: true });
50
81
  exports.loadAiTerminalMethods = loadAiTerminalMethods;
51
82
  exports.executeAiAssistantMongoRead = executeAiAssistantMongoRead;
52
83
  var fs_1 = require("fs");
84
+ var events_1 = require("events");
53
85
  var os = require("os");
54
86
  var path = require("path");
87
+ var promises_1 = require("timers/promises");
88
+ var worker_threads_1 = require("worker_threads");
55
89
  var simpl_schema_1 = require("simpl-schema");
56
90
  var resolveio_server_app_1 = require("../resolveio-server-app");
57
91
  var user_collection_1 = require("../collections/user.collection");
@@ -383,6 +417,7 @@ function loadAiTerminalMethods(methodManager) {
383
417
  }
384
418
  }),
385
419
  forceWorker: true,
420
+ maxConcurrencyPerInstance: 1,
386
421
  function: function (payload) {
387
422
  return __awaiter(this, void 0, void 0, function () {
388
423
  return __generator(this, function (_a) {
@@ -649,7 +684,7 @@ function executeAiFormPatch(payload, context) {
649
684
  }
650
685
  function executeAiAssistantCodexRun(payload, context) {
651
686
  return __awaiter(this, void 0, void 0, function () {
652
- var input, message, guardrail, conversation_2, now_2, userMsg, assistantMsg, user, isSuperAdmin, hasInvoiceAccess, conversation, now, attachments, attachmentData, historyLimit, history, _a, historyLines, prompt, workspaceRoot, codexClient, runOptions, responseText, assistantContent, userDoc, assistantDoc, insertResult;
687
+ var input, message, guardrail, conversation_2, now_2, userMsg, assistantMsg, user, isSuperAdmin, hasInvoiceAccess, conversation, now, attachments, attachmentData, historyLimit, history, _a, historyLines, prompt, workspaceRoot, codexConfig, runOptions, responseText, assistantContent, userDoc, assistantDoc, insertResult;
653
688
  var _b;
654
689
  return __generator(this, function (_c) {
655
690
  switch (_c.label) {
@@ -736,7 +771,7 @@ function executeAiAssistantCodexRun(payload, context) {
736
771
  return [4 /*yield*/, resolveAssistantWorkspaceRoot()];
737
772
  case 12:
738
773
  workspaceRoot = _c.sent();
739
- codexClient = getAssistantCodexClient();
774
+ codexConfig = resolveCodexSettings();
740
775
  runOptions = {
741
776
  timeoutMs: resolveCodexTimeoutMs(),
742
777
  threadOptions: {
@@ -750,7 +785,7 @@ function executeAiAssistantCodexRun(payload, context) {
750
785
  approvalPolicy: 'never'
751
786
  }
752
787
  };
753
- return [4 /*yield*/, codexClient.run(prompt, runOptions)];
788
+ return [4 /*yield*/, runCodexInWorkerThread(prompt, runOptions, codexConfig)];
754
789
  case 13:
755
790
  responseText = _c.sent();
756
791
  assistantContent = sanitizeAssistantResponse(responseText);
@@ -1120,6 +1155,244 @@ function getAssistantCodexClient() {
1120
1155
  }
1121
1156
  return assistantCodexClient;
1122
1157
  }
1158
+ var CodexWorkerBootstrapError = /** @class */ (function (_super) {
1159
+ __extends(CodexWorkerBootstrapError, _super);
1160
+ function CodexWorkerBootstrapError(message) {
1161
+ var _this = _super.call(this, message) || this;
1162
+ _this.name = 'CodexWorkerBootstrapError';
1163
+ return _this;
1164
+ }
1165
+ return CodexWorkerBootstrapError;
1166
+ }(Error));
1167
+ function runCodexInWorkerThread(prompt, runOptions, config) {
1168
+ return __awaiter(this, void 0, void 0, function () {
1169
+ var workerPath, codexClient, error_1, codexClient;
1170
+ return __generator(this, function (_a) {
1171
+ switch (_a.label) {
1172
+ case 0: return [4 /*yield*/, resolveCodexWorkerPath()];
1173
+ case 1:
1174
+ workerPath = _a.sent();
1175
+ if (!!workerPath) return [3 /*break*/, 3];
1176
+ codexClient = getAssistantCodexClient();
1177
+ return [4 /*yield*/, codexClient.run(prompt, runOptions)];
1178
+ case 2: return [2 /*return*/, _a.sent()];
1179
+ case 3:
1180
+ _a.trys.push([3, 5, , 7]);
1181
+ return [4 /*yield*/, runCodexInWorkerThreadInternal(workerPath, prompt, runOptions, config)];
1182
+ case 4: return [2 /*return*/, _a.sent()];
1183
+ case 5:
1184
+ error_1 = _a.sent();
1185
+ if (!(error_1 instanceof CodexWorkerBootstrapError)) {
1186
+ throw error_1;
1187
+ }
1188
+ console.error('Codex worker bootstrap failed, falling back to in-process run.', error_1);
1189
+ codexClient = getAssistantCodexClient();
1190
+ return [4 /*yield*/, codexClient.run(prompt, runOptions)];
1191
+ case 6: return [2 /*return*/, _a.sent()];
1192
+ case 7: return [2 /*return*/];
1193
+ }
1194
+ });
1195
+ });
1196
+ }
1197
+ function runCodexInWorkerThreadInternal(workerPath, prompt, runOptions, config) {
1198
+ return __awaiter(this, void 0, void 0, function () {
1199
+ var sanitizedOptions, worker, wrapped, timeoutMs, timeoutController, timeoutPromise, messagePromise, errorPromise, exitPromise, outcome, wrapped;
1200
+ var _this = this;
1201
+ var _a, _b;
1202
+ return __generator(this, function (_c) {
1203
+ switch (_c.label) {
1204
+ case 0:
1205
+ sanitizedOptions = sanitizeCodexRunOptions(runOptions);
1206
+ try {
1207
+ worker = new worker_threads_1.Worker(workerPath, {
1208
+ workerData: {
1209
+ prompt: prompt,
1210
+ runOptions: sanitizedOptions,
1211
+ config: config
1212
+ }
1213
+ });
1214
+ }
1215
+ catch (error) {
1216
+ wrapped = new CodexWorkerBootstrapError('Failed to start codex worker.');
1217
+ if (error instanceof Error && error.stack) {
1218
+ wrapped.stack = error.stack;
1219
+ }
1220
+ throw wrapped;
1221
+ }
1222
+ timeoutMs = ((sanitizedOptions === null || sanitizedOptions === void 0 ? void 0 : sanitizedOptions.timeoutMs) || resolveCodexTimeoutMs()) + 15000;
1223
+ timeoutController = new AbortController();
1224
+ timeoutPromise = (function () { return __awaiter(_this, void 0, void 0, function () {
1225
+ var error_2;
1226
+ return __generator(this, function (_a) {
1227
+ switch (_a.label) {
1228
+ case 0:
1229
+ _a.trys.push([0, 2, , 3]);
1230
+ return [4 /*yield*/, (0, promises_1.setTimeout)(timeoutMs, null, { signal: timeoutController.signal })];
1231
+ case 1:
1232
+ _a.sent();
1233
+ return [2 /*return*/, { type: 'timeout' }];
1234
+ case 2:
1235
+ error_2 = _a.sent();
1236
+ if ((error_2 === null || error_2 === void 0 ? void 0 : error_2.name) === 'AbortError') {
1237
+ return [2 /*return*/, { type: 'aborted' }];
1238
+ }
1239
+ throw error_2;
1240
+ case 3: return [2 /*return*/];
1241
+ }
1242
+ });
1243
+ }); })();
1244
+ messagePromise = (function () { return __awaiter(_this, void 0, void 0, function () {
1245
+ var _a, message;
1246
+ return __generator(this, function (_b) {
1247
+ switch (_b.label) {
1248
+ case 0: return [4 /*yield*/, (0, events_1.once)(worker, 'message')];
1249
+ case 1:
1250
+ _a = __read.apply(void 0, [_b.sent(), 1]), message = _a[0];
1251
+ return [2 /*return*/, { type: 'message', message: message }];
1252
+ }
1253
+ });
1254
+ }); })();
1255
+ errorPromise = (function () { return __awaiter(_this, void 0, void 0, function () {
1256
+ var _a, error;
1257
+ return __generator(this, function (_b) {
1258
+ switch (_b.label) {
1259
+ case 0: return [4 /*yield*/, (0, events_1.once)(worker, 'error')];
1260
+ case 1:
1261
+ _a = __read.apply(void 0, [_b.sent(), 1]), error = _a[0];
1262
+ return [2 /*return*/, { type: 'error', error: error }];
1263
+ }
1264
+ });
1265
+ }); })();
1266
+ exitPromise = (function () { return __awaiter(_this, void 0, void 0, function () {
1267
+ var _a, code;
1268
+ return __generator(this, function (_b) {
1269
+ switch (_b.label) {
1270
+ case 0: return [4 /*yield*/, (0, events_1.once)(worker, 'exit')];
1271
+ case 1:
1272
+ _a = __read.apply(void 0, [_b.sent(), 1]), code = _a[0];
1273
+ return [2 /*return*/, { type: 'exit', code: code }];
1274
+ }
1275
+ });
1276
+ }); })();
1277
+ _c.label = 1;
1278
+ case 1:
1279
+ _c.trys.push([1, , 3, 4]);
1280
+ return [4 /*yield*/, Promise.race([messagePromise, errorPromise, exitPromise, timeoutPromise])];
1281
+ case 2:
1282
+ outcome = _c.sent();
1283
+ return [3 /*break*/, 4];
1284
+ case 3:
1285
+ timeoutController.abort();
1286
+ worker.removeAllListeners('message');
1287
+ worker.removeAllListeners('error');
1288
+ worker.removeAllListeners('exit');
1289
+ return [7 /*endfinally*/];
1290
+ case 4:
1291
+ if (!(outcome.type === 'message')) return [3 /*break*/, 6];
1292
+ return [4 /*yield*/, terminateWorker(worker)];
1293
+ case 5:
1294
+ _c.sent();
1295
+ if (outcome.message && outcome.message.ok) {
1296
+ return [2 /*return*/, outcome.message.result || ''];
1297
+ }
1298
+ throw new Error(((_a = outcome.message) === null || _a === void 0 ? void 0 : _a.error) || 'Codex worker failed.');
1299
+ case 6:
1300
+ if (!(outcome.type === 'timeout')) return [3 /*break*/, 8];
1301
+ return [4 /*yield*/, terminateWorker(worker)];
1302
+ case 7:
1303
+ _c.sent();
1304
+ throw new CodexWorkerBootstrapError('Codex worker timed out.');
1305
+ case 8:
1306
+ if (!(outcome.type === 'error')) return [3 /*break*/, 10];
1307
+ return [4 /*yield*/, terminateWorker(worker)];
1308
+ case 9:
1309
+ _c.sent();
1310
+ wrapped = new CodexWorkerBootstrapError('Codex worker errored before completing.');
1311
+ if ((_b = outcome.error) === null || _b === void 0 ? void 0 : _b.stack) {
1312
+ wrapped.stack = outcome.error.stack;
1313
+ }
1314
+ throw wrapped;
1315
+ case 10:
1316
+ if (outcome.type === 'exit') {
1317
+ throw new CodexWorkerBootstrapError("Codex worker exited before completing (code ".concat(outcome.code, ")."));
1318
+ }
1319
+ throw new CodexWorkerBootstrapError('Codex worker aborted before completing.');
1320
+ }
1321
+ });
1322
+ });
1323
+ }
1324
+ function terminateWorker(worker) {
1325
+ return __awaiter(this, void 0, void 0, function () {
1326
+ var _a;
1327
+ return __generator(this, function (_b) {
1328
+ switch (_b.label) {
1329
+ case 0:
1330
+ _b.trys.push([0, 2, , 3]);
1331
+ return [4 /*yield*/, worker.terminate()];
1332
+ case 1:
1333
+ _b.sent();
1334
+ return [3 /*break*/, 3];
1335
+ case 2:
1336
+ _a = _b.sent();
1337
+ return [3 /*break*/, 3];
1338
+ case 3: return [2 /*return*/];
1339
+ }
1340
+ });
1341
+ });
1342
+ }
1343
+ function sanitizeCodexRunOptions(options) {
1344
+ if (!options) {
1345
+ return {};
1346
+ }
1347
+ return {
1348
+ timeoutMs: options.timeoutMs,
1349
+ threadOptions: options.threadOptions,
1350
+ threadKey: options.threadKey,
1351
+ reuseThread: options.reuseThread
1352
+ };
1353
+ }
1354
+ function resolveCodexWorkerPath() {
1355
+ return __awaiter(this, void 0, void 0, function () {
1356
+ var jsPath, tsPath;
1357
+ return __generator(this, function (_a) {
1358
+ switch (_a.label) {
1359
+ case 0:
1360
+ jsPath = path.join(__dirname, '../workers/codex-runner.worker.js');
1361
+ return [4 /*yield*/, pathExists(jsPath)];
1362
+ case 1:
1363
+ if (_a.sent()) {
1364
+ return [2 /*return*/, jsPath];
1365
+ }
1366
+ tsPath = path.join(__dirname, '../workers/codex-runner.worker.ts');
1367
+ return [4 /*yield*/, pathExists(tsPath)];
1368
+ case 2:
1369
+ if (_a.sent()) {
1370
+ return [2 /*return*/, tsPath];
1371
+ }
1372
+ return [2 /*return*/, null];
1373
+ }
1374
+ });
1375
+ });
1376
+ }
1377
+ function pathExists(target) {
1378
+ return __awaiter(this, void 0, void 0, function () {
1379
+ var stat, _a;
1380
+ return __generator(this, function (_b) {
1381
+ switch (_b.label) {
1382
+ case 0:
1383
+ _b.trys.push([0, 2, , 3]);
1384
+ return [4 /*yield*/, fs_1.promises.stat(target)];
1385
+ case 1:
1386
+ stat = _b.sent();
1387
+ return [2 /*return*/, stat.isFile()];
1388
+ case 2:
1389
+ _a = _b.sent();
1390
+ return [2 /*return*/, false];
1391
+ case 3: return [2 /*return*/];
1392
+ }
1393
+ });
1394
+ });
1395
+ }
1123
1396
  function resolveAssistantWorkspaceRoot() {
1124
1397
  return __awaiter(this, void 0, void 0, function () {
1125
1398
  var config, configured, candidate, stat, _a;