@resolveio/server-lib 20.14.48 → 20.15.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.
@@ -61,6 +61,13 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
61
61
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
62
62
  }
63
63
  };
64
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
65
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
66
+ var m = o[Symbol.asyncIterator], i;
67
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
68
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
69
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
70
+ };
64
71
  var __read = (this && this.__read) || function (o, n) {
65
72
  var m = typeof Symbol === "function" && o[Symbol.iterator];
66
73
  if (!m) return o;
@@ -788,7 +795,7 @@ function executeAiFormPatch(payload, context) {
788
795
  }
789
796
  function executeAiAssistantCodexRun(payload, context) {
790
797
  return __awaiter(this, void 0, void 0, function () {
791
- var input, message, requestId, guardrail, conversation_2, now_2, userMsg, assistantMsg, user, isSuperAdmin, hasInvoiceAccess, customerId, conversation, now, attachments, attachmentData, historyLimit, history, _a, historyLines, assistantContext, prompt, workspaceRoot, codexConfig, runOptions, userDoc, initialProgress, assistantDoc, insertResult, assistantMessageId, progressTracker, assistantContent, toolResult, responseText, directive, cleanedResponseText, toolRequest, toolResponse, _b, toolPayload, followupPrompt, followupText, _c, error_1, error_2, finalNow, finalMetadata, finalAssistantDoc;
798
+ var input, message, requestId, guardrail, conversation_2, now_2, userMsg, assistantMsg, user, isSuperAdmin, hasInvoiceAccess, customerId, conversation, now, attachments, attachmentData, historyLimit, history, _a, historyLines, assistantContext, prompt, workspaceRoot, codexConfig, runOptions, userDoc, initialProgress, assistantDoc, insertResult, assistantMessageId, progressTracker, streamProgress, assistantContent, toolResult, responseText, directive, cleanedResponseText, toolRequest, toolResponse, _b, toolPayload, followupPrompt, followupText, _c, error_1, error_2, finalNow, finalMetadata, finalAssistantDoc;
792
799
  var _d, _e;
793
800
  return __generator(this, function (_f) {
794
801
  switch (_f.label) {
@@ -916,12 +923,13 @@ function executeAiAssistantCodexRun(payload, context) {
916
923
  insertResult = _f.sent();
917
924
  assistantMessageId = (insertResult === null || insertResult === void 0 ? void 0 : insertResult._id) || (insertResult === null || insertResult === void 0 ? void 0 : insertResult.insertedId);
918
925
  progressTracker = createAssistantProgressTracker(assistantMessageId, initialProgress);
926
+ streamProgress = createAssistantStreamProgressHandler(progressTracker);
919
927
  assistantContent = '';
920
928
  toolResult = null;
921
929
  _f.label = 15;
922
930
  case 15:
923
931
  _f.trys.push([15, 30, 31, 32]);
924
- return [4 /*yield*/, runCodexInWorkerThread(prompt, runOptions, codexConfig)];
932
+ return [4 /*yield*/, runCodexInWorkerThread(prompt, runOptions, codexConfig, streamProgress)];
925
933
  case 16:
926
934
  responseText = _f.sent();
927
935
  directive = extractAssistantMongoDirective(responseText);
@@ -951,7 +959,7 @@ function executeAiAssistantCodexRun(payload, context) {
951
959
  _f.label = 22;
952
960
  case 22:
953
961
  _f.trys.push([22, 24, , 25]);
954
- return [4 /*yield*/, runCodexInWorkerThread(followupPrompt, runOptions, codexConfig)];
962
+ return [4 /*yield*/, runCodexInWorkerThread(followupPrompt, runOptions, codexConfig, streamProgress)];
955
963
  case 23:
956
964
  followupText = _f.sent();
957
965
  assistantContent = sanitizeAssistantResponse(followupText);
@@ -1381,6 +1389,60 @@ function buildAssistantToolErrorMessage(error, directive, request) {
1381
1389
  }
1382
1390
  return "I couldn't access the requested data. ".concat(routeLine);
1383
1391
  }
1392
+ function deriveAssistantStreamStatus(event) {
1393
+ var _a;
1394
+ if (!event || !event.type) {
1395
+ return null;
1396
+ }
1397
+ if (event.type === 'thread.started' || event.type === 'turn.started') {
1398
+ return 'Reviewing request';
1399
+ }
1400
+ if (event.type === 'turn.completed') {
1401
+ return 'Drafting response';
1402
+ }
1403
+ if (event.type === 'item.started' || event.type === 'item.updated' || event.type === 'item.completed') {
1404
+ var itemType = String(((_a = event === null || event === void 0 ? void 0 : event.item) === null || _a === void 0 ? void 0 : _a.type) || '').trim().toLowerCase();
1405
+ if (!itemType) {
1406
+ return null;
1407
+ }
1408
+ if (itemType === 'mcp_tool_call') {
1409
+ return 'Calling tool';
1410
+ }
1411
+ if (itemType === 'command_execution') {
1412
+ return 'Running command';
1413
+ }
1414
+ if (itemType === 'web_search') {
1415
+ return 'Searching references';
1416
+ }
1417
+ if (itemType === 'file_change') {
1418
+ return 'Drafting response';
1419
+ }
1420
+ if (itemType === 'agent_message') {
1421
+ return 'Drafting response';
1422
+ }
1423
+ if (itemType === 'reasoning') {
1424
+ return 'Analyzing';
1425
+ }
1426
+ }
1427
+ return null;
1428
+ }
1429
+ function createAssistantStreamProgressHandler(progressTracker) {
1430
+ var lastStatus = '';
1431
+ var lastSentAt = 0;
1432
+ return function (status) {
1433
+ var trimmed = normalizeOptionalString(status);
1434
+ if (!trimmed) {
1435
+ return;
1436
+ }
1437
+ var now = Date.now();
1438
+ if (trimmed === lastStatus && now - lastSentAt < 3000) {
1439
+ return;
1440
+ }
1441
+ lastStatus = trimmed;
1442
+ lastSentAt = now;
1443
+ progressTracker.push(trimmed);
1444
+ };
1445
+ }
1384
1446
  function normalizeAssistantProgress(items) {
1385
1447
  var seen = new Set();
1386
1448
  var cleaned = [];
@@ -2400,26 +2462,103 @@ var CodexWorkerBootstrapError = /** @class */ (function (_super) {
2400
2462
  }
2401
2463
  return CodexWorkerBootstrapError;
2402
2464
  }(Error));
2403
- function runCodexInWorkerThread(prompt, runOptions, config) {
2465
+ function applyCodexStreamStatusHandler(runOptions, streamStatusHandler) {
2466
+ var _this = this;
2467
+ if (!streamStatusHandler) {
2468
+ return runOptions;
2469
+ }
2470
+ var existing = runOptions === null || runOptions === void 0 ? void 0 : runOptions.onStreamEvent;
2471
+ return __assign(__assign({}, runOptions), { onStreamEvent: function (event) { return __awaiter(_this, void 0, void 0, function () {
2472
+ var status;
2473
+ return __generator(this, function (_a) {
2474
+ switch (_a.label) {
2475
+ case 0:
2476
+ if (!existing) return [3 /*break*/, 2];
2477
+ return [4 /*yield*/, existing(event)];
2478
+ case 1:
2479
+ _a.sent();
2480
+ _a.label = 2;
2481
+ case 2:
2482
+ status = deriveAssistantStreamStatus(event);
2483
+ if (status) {
2484
+ streamStatusHandler(status);
2485
+ }
2486
+ return [2 /*return*/];
2487
+ }
2488
+ });
2489
+ }); } });
2490
+ }
2491
+ function waitForCodexWorkerMessage(worker, streamStatusHandler) {
2404
2492
  return __awaiter(this, void 0, void 0, function () {
2405
- var codexClient, workerPath, codexClient, error_3, codexClient;
2493
+ var _a, _b, _c, _d, message, payload, status_1, e_2_1;
2494
+ var _e, e_2, _f, _g;
2495
+ return __generator(this, function (_h) {
2496
+ switch (_h.label) {
2497
+ case 0:
2498
+ _h.trys.push([0, 5, 6, 11]);
2499
+ _a = true, _b = __asyncValues((0, events_1.on)(worker, 'message'));
2500
+ _h.label = 1;
2501
+ case 1: return [4 /*yield*/, _b.next()];
2502
+ case 2:
2503
+ if (!(_c = _h.sent(), _e = _c.done, !_e)) return [3 /*break*/, 4];
2504
+ _g = _c.value;
2505
+ _a = false;
2506
+ _d = __read(_g, 1), message = _d[0];
2507
+ payload = message;
2508
+ if ((payload === null || payload === void 0 ? void 0 : payload.type) === 'stream') {
2509
+ status_1 = normalizeOptionalString(payload.status);
2510
+ if (status_1 && streamStatusHandler) {
2511
+ streamStatusHandler(status_1);
2512
+ }
2513
+ return [3 /*break*/, 3];
2514
+ }
2515
+ return [2 /*return*/, payload];
2516
+ case 3:
2517
+ _a = true;
2518
+ return [3 /*break*/, 1];
2519
+ case 4: return [3 /*break*/, 11];
2520
+ case 5:
2521
+ e_2_1 = _h.sent();
2522
+ e_2 = { error: e_2_1 };
2523
+ return [3 /*break*/, 11];
2524
+ case 6:
2525
+ _h.trys.push([6, , 9, 10]);
2526
+ if (!(!_a && !_e && (_f = _b.return))) return [3 /*break*/, 8];
2527
+ return [4 /*yield*/, _f.call(_b)];
2528
+ case 7:
2529
+ _h.sent();
2530
+ _h.label = 8;
2531
+ case 8: return [3 /*break*/, 10];
2532
+ case 9:
2533
+ if (e_2) throw e_2.error;
2534
+ return [7 /*endfinally*/];
2535
+ case 10: return [7 /*endfinally*/];
2536
+ case 11: throw new CodexWorkerBootstrapError('Codex worker exited before completing.');
2537
+ }
2538
+ });
2539
+ });
2540
+ }
2541
+ function runCodexInWorkerThread(prompt, runOptions, config, streamStatusHandler) {
2542
+ return __awaiter(this, void 0, void 0, function () {
2543
+ var streamedOptions, codexClient, workerPath, codexClient, error_3, codexClient;
2406
2544
  return __generator(this, function (_a) {
2407
2545
  switch (_a.label) {
2408
2546
  case 0:
2547
+ streamedOptions = applyCodexStreamStatusHandler(runOptions, streamStatusHandler);
2409
2548
  if (!!resolveCodexWorkerThreadEnabled()) return [3 /*break*/, 2];
2410
2549
  codexClient = getAssistantCodexClient();
2411
- return [4 /*yield*/, codexClient.run(prompt, runOptions)];
2550
+ return [4 /*yield*/, codexClient.run(prompt, streamedOptions)];
2412
2551
  case 1: return [2 /*return*/, _a.sent()];
2413
2552
  case 2: return [4 /*yield*/, resolveCodexWorkerPath()];
2414
2553
  case 3:
2415
2554
  workerPath = _a.sent();
2416
2555
  if (!!workerPath) return [3 /*break*/, 5];
2417
2556
  codexClient = getAssistantCodexClient();
2418
- return [4 /*yield*/, codexClient.run(prompt, runOptions)];
2557
+ return [4 /*yield*/, codexClient.run(prompt, streamedOptions)];
2419
2558
  case 4: return [2 /*return*/, _a.sent()];
2420
2559
  case 5:
2421
2560
  _a.trys.push([5, 7, , 9]);
2422
- return [4 /*yield*/, runCodexInWorkerThreadInternal(workerPath, prompt, runOptions, config)];
2561
+ return [4 /*yield*/, runCodexInWorkerThreadInternal(workerPath, prompt, runOptions, config, streamStatusHandler)];
2423
2562
  case 6: return [2 /*return*/, _a.sent()];
2424
2563
  case 7:
2425
2564
  error_3 = _a.sent();
@@ -2428,14 +2567,14 @@ function runCodexInWorkerThread(prompt, runOptions, config) {
2428
2567
  }
2429
2568
  console.error('Codex worker bootstrap failed, falling back to in-process run.', error_3);
2430
2569
  codexClient = getAssistantCodexClient();
2431
- return [4 /*yield*/, codexClient.run(prompt, runOptions)];
2570
+ return [4 /*yield*/, codexClient.run(prompt, streamedOptions)];
2432
2571
  case 8: return [2 /*return*/, _a.sent()];
2433
2572
  case 9: return [2 /*return*/];
2434
2573
  }
2435
2574
  });
2436
2575
  });
2437
2576
  }
2438
- function runCodexInWorkerThreadInternal(workerPath, prompt, runOptions, config) {
2577
+ function runCodexInWorkerThreadInternal(workerPath, prompt, runOptions, config, streamStatusHandler) {
2439
2578
  return __awaiter(this, void 0, void 0, function () {
2440
2579
  var sanitizedOptions, worker, wrapped, timeoutMs, timeoutController, timeoutPromise, messagePromise, errorPromise, exitPromise, outcome, wrapped;
2441
2580
  var _this = this;
@@ -2449,7 +2588,8 @@ function runCodexInWorkerThreadInternal(workerPath, prompt, runOptions, config)
2449
2588
  workerData: {
2450
2589
  prompt: prompt,
2451
2590
  runOptions: sanitizedOptions,
2452
- config: config
2591
+ config: config,
2592
+ streamStatus: !!streamStatusHandler
2453
2593
  }
2454
2594
  });
2455
2595
  }
@@ -2483,12 +2623,12 @@ function runCodexInWorkerThreadInternal(workerPath, prompt, runOptions, config)
2483
2623
  });
2484
2624
  }); })();
2485
2625
  messagePromise = (function () { return __awaiter(_this, void 0, void 0, function () {
2486
- var _a, message;
2487
- return __generator(this, function (_b) {
2488
- switch (_b.label) {
2489
- case 0: return [4 /*yield*/, (0, events_1.once)(worker, 'message')];
2626
+ var message;
2627
+ return __generator(this, function (_a) {
2628
+ switch (_a.label) {
2629
+ case 0: return [4 /*yield*/, waitForCodexWorkerMessage(worker, streamStatusHandler)];
2490
2630
  case 1:
2491
- _a = __read.apply(void 0, [_b.sent(), 1]), message = _a[0];
2631
+ message = _a.sent();
2492
2632
  return [2 /*return*/, { type: 'message', message: message }];
2493
2633
  }
2494
2634
  });
@@ -2524,7 +2664,6 @@ function runCodexInWorkerThreadInternal(workerPath, prompt, runOptions, config)
2524
2664
  return [3 /*break*/, 4];
2525
2665
  case 3:
2526
2666
  timeoutController.abort();
2527
- worker.removeAllListeners('message');
2528
2667
  worker.removeAllListeners('error');
2529
2668
  worker.removeAllListeners('exit');
2530
2669
  return [7 /*endfinally*/];
@@ -2730,7 +2869,7 @@ function normalizeRouteMatchKey(value) {
2730
2869
  return normalizeRouteKey(value).toLowerCase();
2731
2870
  }
2732
2871
  function buildClientRouteIndex() {
2733
- var e_2, _a;
2872
+ var e_3, _a;
2734
2873
  var _b;
2735
2874
  var routes = ((_b = resolveio_server_app_1.ResolveIOServer.getClientRoutes) === null || _b === void 0 ? void 0 : _b.call(resolveio_server_app_1.ResolveIOServer)) || [];
2736
2875
  var set = new Set();
@@ -2749,12 +2888,12 @@ function buildClientRouteIndex() {
2749
2888
  }
2750
2889
  }
2751
2890
  }
2752
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
2891
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
2753
2892
  finally {
2754
2893
  try {
2755
2894
  if (routes_1_1 && !routes_1_1.done && (_a = routes_1.return)) _a.call(routes_1);
2756
2895
  }
2757
- finally { if (e_2) throw e_2.error; }
2896
+ finally { if (e_3) throw e_3.error; }
2758
2897
  }
2759
2898
  return { set: set, map: map, size: routes.length };
2760
2899
  }
@@ -2877,7 +3016,7 @@ function sanitizeAssistantResponse(value) {
2877
3016
  return normalizeAssistantRoutes(cleaned);
2878
3017
  }
2879
3018
  function evaluateAssistantGuardrails(message) {
2880
- var e_3, _a;
3019
+ var e_4, _a;
2881
3020
  var normalized = String(message || '').toLowerCase();
2882
3021
  var patterns = [
2883
3022
  {
@@ -2923,12 +3062,12 @@ function evaluateAssistantGuardrails(message) {
2923
3062
  }
2924
3063
  }
2925
3064
  }
2926
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
3065
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
2927
3066
  finally {
2928
3067
  try {
2929
3068
  if (patterns_1_1 && !patterns_1_1.done && (_a = patterns_1.return)) _a.call(patterns_1);
2930
3069
  }
2931
- finally { if (e_3) throw e_3.error; }
3070
+ finally { if (e_4) throw e_4.error; }
2932
3071
  }
2933
3072
  return null;
2934
3073
  }
@@ -3092,8 +3231,8 @@ function handleCodexUpload(id_conversation, file_name, content_base64, size, con
3092
3231
  }
3093
3232
  function readAttachmentContents(attachments) {
3094
3233
  return __awaiter(this, void 0, void 0, function () {
3095
- var limits, totalBytes, totalChars, chunks, cleaned, attachments_1, attachments_1_1, attachment, localPath, safe, stat, ext, name_1, type, readable, content, _a, e_4_1;
3096
- var e_4, _b;
3234
+ var limits, totalBytes, totalChars, chunks, cleaned, attachments_1, attachments_1_1, attachment, localPath, safe, stat, ext, name_1, type, readable, content, _a, e_5_1;
3235
+ var e_5, _b;
3097
3236
  return __generator(this, function (_c) {
3098
3237
  switch (_c.label) {
3099
3238
  case 0:
@@ -3172,14 +3311,14 @@ function readAttachmentContents(attachments) {
3172
3311
  return [3 /*break*/, 2];
3173
3312
  case 10: return [3 /*break*/, 13];
3174
3313
  case 11:
3175
- e_4_1 = _c.sent();
3176
- e_4 = { error: e_4_1 };
3314
+ e_5_1 = _c.sent();
3315
+ e_5 = { error: e_5_1 };
3177
3316
  return [3 /*break*/, 13];
3178
3317
  case 12:
3179
3318
  try {
3180
3319
  if (attachments_1_1 && !attachments_1_1.done && (_b = attachments_1.return)) _b.call(attachments_1);
3181
3320
  }
3182
- finally { if (e_4) throw e_4.error; }
3321
+ finally { if (e_5) throw e_5.error; }
3183
3322
  return [7 /*endfinally*/];
3184
3323
  case 13: return [2 /*return*/, {
3185
3324
  promptText: chunks.length ? "\n\nAttachments:\n".concat(chunks.join('\n\n')) : '',
@@ -3356,7 +3495,7 @@ function estimateUsage(messages, responseText, model) {
3356
3495
  };
3357
3496
  }
3358
3497
  function evaluateGuardrails(message) {
3359
- var e_5, _a;
3498
+ var e_6, _a;
3360
3499
  var normalized = String(message || '').toLowerCase();
3361
3500
  var patterns = [
3362
3501
  { pattern: /\b(source\s*code|full\s*code|entire\s*code|repo\s*dump|repository|git\s*clone)\b/i, reason: 'Code access is restricted.' },
@@ -3378,12 +3517,12 @@ function evaluateGuardrails(message) {
3378
3517
  }
3379
3518
  }
3380
3519
  }
3381
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
3520
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
3382
3521
  finally {
3383
3522
  try {
3384
3523
  if (patterns_2_1 && !patterns_2_1.done && (_a = patterns_2.return)) _a.call(patterns_2);
3385
3524
  }
3386
- finally { if (e_5) throw e_5.error; }
3525
+ finally { if (e_6) throw e_6.error; }
3387
3526
  }
3388
3527
  return null;
3389
3528
  }