cozy-search 2.3.3 → 2.5.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.
@@ -27,7 +27,7 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
27
27
 
28
28
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
29
29
 
30
- var AUTOPROVISION_FLAG = 'rag.assistants.autoprovision';
30
+ var AUTOPROVISION_FLAG = 'cozy.assistant.autoprovision';
31
31
  exports.AUTOPROVISION_FLAG = AUTOPROVISION_FLAG;
32
32
  var ASSISTANTS_DOCTYPE = 'io.cozy.ai.chat.assistants';
33
33
  var APPS_DOCTYPE = 'io.cozy.apps';
@@ -248,48 +248,56 @@ var createOpenragAccount = /*#__PURE__*/function () {
248
248
  exports.createOpenragAccount = createOpenragAccount;
249
249
 
250
250
  var getAssistant = /*#__PURE__*/function () {
251
- var _ref4 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(client, id) {
251
+ var _ref4 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(client, id, known) {
252
252
  var _buildAssistantByIdQu, definition, options, _yield$client$query, data;
253
253
 
254
254
  return _regenerator.default.wrap(function _callee3$(_context3) {
255
255
  while (1) {
256
256
  switch (_context3.prev = _context3.next) {
257
257
  case 0:
258
- _context3.prev = 0;
258
+ if (!known) {
259
+ _context3.next = 2;
260
+ break;
261
+ }
262
+
263
+ return _context3.abrupt("return", known.get(id) || null);
264
+
265
+ case 2:
266
+ _context3.prev = 2;
259
267
  _buildAssistantByIdQu = (0, _queries.buildAssistantByIdQuery)(id), definition = _buildAssistantByIdQu.definition, options = _buildAssistantByIdQu.options;
260
- _context3.next = 4;
268
+ _context3.next = 6;
261
269
  return client.query(definition(), {
262
270
  as: options.as
263
271
  });
264
272
 
265
- case 4:
273
+ case 6:
266
274
  _yield$client$query = _context3.sent;
267
275
  data = _yield$client$query.data;
268
276
  return _context3.abrupt("return", data || null);
269
277
 
270
- case 9:
271
- _context3.prev = 9;
272
- _context3.t0 = _context3["catch"](0);
278
+ case 11:
279
+ _context3.prev = 11;
280
+ _context3.t0 = _context3["catch"](2);
273
281
 
274
282
  if (!isNotFound(_context3.t0)) {
275
- _context3.next = 13;
283
+ _context3.next = 15;
276
284
  break;
277
285
  }
278
286
 
279
287
  return _context3.abrupt("return", null);
280
288
 
281
- case 13:
289
+ case 15:
282
290
  throw _context3.t0;
283
291
 
284
- case 14:
292
+ case 16:
285
293
  case "end":
286
294
  return _context3.stop();
287
295
  }
288
296
  }
289
- }, _callee3, null, [[0, 9]]);
297
+ }, _callee3, null, [[2, 11]]);
290
298
  }));
291
299
 
292
- return function getAssistant(_x4, _x5) {
300
+ return function getAssistant(_x4, _x5, _x6) {
293
301
  return _ref4.apply(this, arguments);
294
302
  };
295
303
  }();
@@ -366,7 +374,7 @@ var createProvisionedAssistant = /*#__PURE__*/function () {
366
374
  }, _callee4, null, [[3, 8]]);
367
375
  }));
368
376
 
369
- return function createProvisionedAssistant(_x6, _x7, _x8, _x9) {
377
+ return function createProvisionedAssistant(_x7, _x8, _x9, _x10) {
370
378
  return _ref5.apply(this, arguments);
371
379
  };
372
380
  }();
@@ -390,25 +398,45 @@ var validate = function validate(entry) {
390
398
  };
391
399
  };
392
400
  /**
393
- * Provisions the assistants described by the rag.assistants.autoprovision
401
+ * Provisions the assistants described by the cozy.assistant.autoprovision
394
402
  * flag. Idempotent: an existing assistant is left alone, or gets the flag's
395
403
  * folder back when it lost it or fell back to the root. Two entries whose
396
404
  * names derive to the same id would fight over one assistant: the second
397
405
  * one is skipped.
398
406
  * @param {import('cozy-client').CozyClient} client - The cozy client.
399
407
  * @param {Array<{name: string, dirName?: string, dirId?: string, prompt?: string, icon?: string|null, default?: boolean}>} configs - The flag entries.
408
+ * @param {object} [options] - Options.
409
+ * @param {Array<object>} [options.assistants] - Every assistant of the instance, when already fetched: spares one query per entry.
400
410
  * @returns {Promise<{created: string[], ensured: string[], skipped: {id: string, reason: string}[]}>} What was created, ensured and skipped.
401
411
  */
402
412
 
403
413
 
404
414
  var ensureProvisionedAssistants = /*#__PURE__*/function () {
405
415
  var _ref6 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee5(client, configs) {
406
- var result, seen, _iterator2, _step2, entry, _validate, id, reason, assistant, created, dirId, wanted, current, live;
416
+ var _ref7,
417
+ assistants,
418
+ result,
419
+ known,
420
+ seen,
421
+ _iterator2,
422
+ _step2,
423
+ entry,
424
+ _validate,
425
+ id,
426
+ reason,
427
+ assistant,
428
+ created,
429
+ dirId,
430
+ current,
431
+ wanted,
432
+ live,
433
+ _args5 = arguments;
407
434
 
408
435
  return _regenerator.default.wrap(function _callee5$(_context5) {
409
436
  while (1) {
410
437
  switch (_context5.prev = _context5.next) {
411
438
  case 0:
439
+ _ref7 = _args5.length > 2 && _args5[2] !== undefined ? _args5[2] : {}, assistants = _ref7.assistants;
412
440
  result = {
413
441
  created: [],
414
442
  ensured: [],
@@ -416,22 +444,25 @@ var ensureProvisionedAssistants = /*#__PURE__*/function () {
416
444
  };
417
445
 
418
446
  if (!(!Array.isArray(configs) || configs.length === 0)) {
419
- _context5.next = 3;
447
+ _context5.next = 4;
420
448
  break;
421
449
  }
422
450
 
423
451
  return _context5.abrupt("return", result);
424
452
 
425
- case 3:
453
+ case 4:
454
+ known = assistants ? new Map(assistants.map(function (assistant) {
455
+ return [assistant._id, assistant];
456
+ })) : null;
426
457
  seen = new Set();
427
458
  _iterator2 = _createForOfIteratorHelper(configs);
428
- _context5.prev = 5;
459
+ _context5.prev = 7;
429
460
 
430
461
  _iterator2.s();
431
462
 
432
- case 7:
463
+ case 9:
433
464
  if ((_step2 = _iterator2.n()).done) {
434
- _context5.next = 70;
465
+ _context5.next = 75;
435
466
  break;
436
467
  }
437
468
 
@@ -439,7 +470,7 @@ var ensureProvisionedAssistants = /*#__PURE__*/function () {
439
470
  _validate = validate(entry), id = _validate.id, reason = _validate.reason;
440
471
 
441
472
  if (!reason) {
442
- _context5.next = 13;
473
+ _context5.next = 15;
443
474
  break;
444
475
  }
445
476
 
@@ -447,11 +478,11 @@ var ensureProvisionedAssistants = /*#__PURE__*/function () {
447
478
  id: id,
448
479
  reason: reason
449
480
  });
450
- return _context5.abrupt("continue", 68);
481
+ return _context5.abrupt("continue", 73);
451
482
 
452
- case 13:
483
+ case 15:
453
484
  if (!seen.has(id)) {
454
- _context5.next = 16;
485
+ _context5.next = 18;
455
486
  break;
456
487
  }
457
488
 
@@ -459,31 +490,31 @@ var ensureProvisionedAssistants = /*#__PURE__*/function () {
459
490
  id: id,
460
491
  reason: 'duplicate assistant id'
461
492
  });
462
- return _context5.abrupt("continue", 68);
493
+ return _context5.abrupt("continue", 73);
463
494
 
464
- case 16:
495
+ case 18:
465
496
  seen.add(id);
466
- _context5.prev = 17;
467
- _context5.next = 20;
468
- return getAssistant(client, id);
497
+ _context5.prev = 19;
498
+ _context5.next = 22;
499
+ return getAssistant(client, id, known);
469
500
 
470
- case 20:
501
+ case 22:
471
502
  assistant = _context5.sent;
472
503
  created = false;
473
504
 
474
505
  if (assistant) {
475
- _context5.next = 40;
506
+ _context5.next = 42;
476
507
  break;
477
508
  }
478
509
 
479
- _context5.next = 25;
510
+ _context5.next = 27;
480
511
  return resolveProvisionedFolder(client, entry);
481
512
 
482
- case 25:
513
+ case 27:
483
514
  dirId = _context5.sent;
484
515
 
485
516
  if (dirId) {
486
- _context5.next = 29;
517
+ _context5.next = 31;
487
518
  break;
488
519
  }
489
520
 
@@ -491,17 +522,17 @@ var ensureProvisionedAssistants = /*#__PURE__*/function () {
491
522
  id: id,
492
523
  reason: 'folder could not be resolved'
493
524
  });
494
- return _context5.abrupt("continue", 68);
525
+ return _context5.abrupt("continue", 73);
495
526
 
496
- case 29:
497
- _context5.next = 31;
527
+ case 31:
528
+ _context5.next = 33;
498
529
  return createProvisionedAssistant(client, id, entry, dirId);
499
530
 
500
- case 31:
531
+ case 33:
501
532
  created = _context5.sent;
502
533
 
503
534
  if (!created) {
504
- _context5.next = 36;
535
+ _context5.next = 38;
505
536
  break;
506
537
  }
507
538
 
@@ -512,38 +543,50 @@ var ensureProvisionedAssistants = /*#__PURE__*/function () {
512
543
  dirId: dirId
513
544
  }]
514
545
  };
515
- _context5.next = 39;
546
+ _context5.next = 41;
516
547
  break;
517
548
 
518
- case 36:
519
- _context5.next = 38;
549
+ case 38:
550
+ _context5.next = 40;
520
551
  return getAssistant(client, id);
521
552
 
522
- case 38:
553
+ case 40:
523
554
  _context5.t0 = _context5.sent;
524
555
 
525
- case 39:
556
+ case 41:
526
557
  assistant = _context5.t0;
527
558
 
528
- case 40:
559
+ case 42:
529
560
  if (!created) {
530
- _context5.next = 43;
561
+ _context5.next = 45;
531
562
  break;
532
563
  }
533
564
 
534
565
  // saveKnowledgeBase already saved the knowledge base.
535
566
  result.created.push(id);
536
- return _context5.abrupt("continue", 68);
567
+ return _context5.abrupt("continue", 73);
568
+
569
+ case 45:
570
+ current = (0, _knowledgeBase.getKnowledgeBaseDirId)(assistant);
571
+
572
+ if (!(entry.dirId && current === entry.dirId)) {
573
+ _context5.next = 49;
574
+ break;
575
+ }
576
+
577
+ // The stack's rag-index worker skips a trashed folder by itself.
578
+ result.ensured.push(id);
579
+ return _context5.abrupt("continue", 73);
537
580
 
538
- case 43:
539
- _context5.next = 45;
581
+ case 49:
582
+ _context5.next = 51;
540
583
  return resolveProvisionedFolder(client, entry);
541
584
 
542
- case 45:
585
+ case 51:
543
586
  wanted = _context5.sent;
544
587
 
545
588
  if (wanted) {
546
- _context5.next = 49;
589
+ _context5.next = 55;
547
590
  break;
548
591
  }
549
592
 
@@ -551,37 +594,35 @@ var ensureProvisionedAssistants = /*#__PURE__*/function () {
551
594
  id: id,
552
595
  reason: 'folder could not be resolved'
553
596
  });
554
- return _context5.abrupt("continue", 68);
555
-
556
- case 49:
557
- current = (0, _knowledgeBase.getKnowledgeBaseDirId)(assistant);
597
+ return _context5.abrupt("continue", 73);
558
598
 
599
+ case 55:
559
600
  if (!(current === null || (0, _knowledgeBase.isRootDirId)(current) && !(0, _knowledgeBase.isRootDirId)(wanted))) {
560
- _context5.next = 55;
601
+ _context5.next = 60;
561
602
  break;
562
603
  }
563
604
 
564
- _context5.next = 53;
605
+ _context5.next = 58;
565
606
  return (0, _knowledgeBase.saveKnowledgeBase)(client, id, [{
566
607
  doctype: _queries.FILES_DOCTYPE,
567
608
  dirId: wanted
568
609
  }]);
569
610
 
570
- case 53:
611
+ case 58:
571
612
  result.ensured.push(id);
572
- return _context5.abrupt("continue", 68);
613
+ return _context5.abrupt("continue", 73);
573
614
 
574
- case 55:
575
- _context5.next = 57;
615
+ case 60:
616
+ _context5.next = 62;
576
617
  return resolveProvisionedFolder(client, {
577
618
  dirId: current
578
619
  });
579
620
 
580
- case 57:
621
+ case 62:
581
622
  live = _context5.sent;
582
623
 
583
624
  if (live) {
584
- _context5.next = 61;
625
+ _context5.next = 66;
585
626
  break;
586
627
  }
587
628
 
@@ -589,55 +630,55 @@ var ensureProvisionedAssistants = /*#__PURE__*/function () {
589
630
  id: id,
590
631
  reason: "folder ".concat(current, " is missing or trashed")
591
632
  });
592
- return _context5.abrupt("continue", 68);
633
+ return _context5.abrupt("continue", 73);
593
634
 
594
- case 61:
635
+ case 66:
595
636
  result.ensured.push(id);
596
- _context5.next = 68;
637
+ _context5.next = 73;
597
638
  break;
598
639
 
599
- case 64:
600
- _context5.prev = 64;
601
- _context5.t1 = _context5["catch"](17);
640
+ case 69:
641
+ _context5.prev = 69;
642
+ _context5.t1 = _context5["catch"](19);
602
643
  warn("entry ".concat(id, " failed"), _context5.t1);
603
644
  result.skipped.push({
604
645
  id: id,
605
646
  reason: (_context5.t1 === null || _context5.t1 === void 0 ? void 0 : _context5.t1.message) || String(_context5.t1)
606
647
  });
607
648
 
608
- case 68:
609
- _context5.next = 7;
649
+ case 73:
650
+ _context5.next = 9;
610
651
  break;
611
652
 
612
- case 70:
613
- _context5.next = 75;
653
+ case 75:
654
+ _context5.next = 80;
614
655
  break;
615
656
 
616
- case 72:
617
- _context5.prev = 72;
618
- _context5.t2 = _context5["catch"](5);
657
+ case 77:
658
+ _context5.prev = 77;
659
+ _context5.t2 = _context5["catch"](7);
619
660
 
620
661
  _iterator2.e(_context5.t2);
621
662
 
622
- case 75:
623
- _context5.prev = 75;
663
+ case 80:
664
+ _context5.prev = 80;
624
665
 
625
666
  _iterator2.f();
626
667
 
627
- return _context5.finish(75);
668
+ return _context5.finish(80);
628
669
 
629
- case 78:
670
+ case 83:
630
671
  return _context5.abrupt("return", result);
631
672
 
632
- case 79:
673
+ case 84:
633
674
  case "end":
634
675
  return _context5.stop();
635
676
  }
636
677
  }
637
- }, _callee5, null, [[5, 72, 75, 78], [17, 64]]);
678
+ }, _callee5, null, [[7, 77, 80, 83], [19, 69]]);
638
679
  }));
639
680
 
640
- return function ensureProvisionedAssistants(_x10, _x11) {
681
+ return function ensureProvisionedAssistants(_x11, _x12) {
641
682
  return _ref6.apply(this, arguments);
642
683
  };
643
684
  }();
@@ -364,6 +364,58 @@ describe('ensureProvisionedAssistants', () => {
364
364
  expect(client.save).not.toHaveBeenCalled()
365
365
  })
366
366
 
367
+ it('skips the folder lookup when the base already matches the flag dirId', async () => {
368
+ const { client, filesCollection } = makeClient({
369
+ assistants: {
370
+ docs: {
371
+ _id: 'docs',
372
+ _type: 'io.cozy.ai.chat.assistants',
373
+ knowledgeBase: [{ doctype: 'io.cozy.files', dirId: 'dir-1' }]
374
+ }
375
+ }
376
+ })
377
+
378
+ const result = await ensureProvisionedAssistants(client, [
379
+ { name: 'Docs', dirId: 'dir-1' }
380
+ ])
381
+
382
+ expect(result.ensured).toEqual(['docs'])
383
+ expect(filesCollection.statById).not.toHaveBeenCalled()
384
+ expect(client.save).not.toHaveBeenCalled()
385
+ })
386
+
387
+ it('uses the given assistants instead of querying them', async () => {
388
+ const docs = {
389
+ _id: 'docs',
390
+ _type: 'io.cozy.ai.chat.assistants',
391
+ knowledgeBase: [{ doctype: 'io.cozy.files', dirId: 'dir-1' }]
392
+ }
393
+ const { client } = makeClient()
394
+
395
+ const result = await ensureProvisionedAssistants(
396
+ client,
397
+ [{ name: 'Docs', dirId: 'dir-1' }],
398
+ { assistants: [docs] }
399
+ )
400
+
401
+ expect(result.ensured).toEqual(['docs'])
402
+ expect(client.query).not.toHaveBeenCalled()
403
+ })
404
+
405
+ it('creates the assistants missing from the given assistants', async () => {
406
+ const { client } = makeClient({
407
+ files: { 'dir-1': { _id: 'dir-1', type: 'directory', trashed: false } }
408
+ })
409
+
410
+ const result = await ensureProvisionedAssistants(
411
+ client,
412
+ [{ name: 'Docs', dirId: 'dir-1' }],
413
+ { assistants: [] }
414
+ )
415
+
416
+ expect(result.created).toEqual(['docs'])
417
+ })
418
+
367
419
  it('skips an existing assistant whose folder is trashed', async () => {
368
420
  const { client } = makeClient({
369
421
  assistants: {
@@ -485,6 +537,6 @@ describe('getDefaultProvisionedAssistantId', () => {
485
537
  { name: 'Autre', dirName: 'Autre', default: true }
486
538
  ])
487
539
  expect(getDefaultProvisionedAssistantId()).toBe('mes-documents')
488
- expect(flag).toHaveBeenCalledWith('rag.assistants.autoprovision')
540
+ expect(flag).toHaveBeenCalledWith('cozy.assistant.autoprovision')
489
541
  })
490
542
  })
@@ -13,7 +13,9 @@ export function findRagIndexTriggers(client: any): Promise<{
13
13
  files: object | null;
14
14
  assistants: object | null;
15
15
  }>;
16
- export function migrateAssistantsWithoutFolder(client: any): Promise<string[]>;
16
+ export function fetchAssistants(client: any): any;
17
+ export function migrateAssistantsWithoutFolder(client: import("cozy-client").CozyClient, assistants?: object[] | undefined): Promise<string[]>;
18
+ export function createRagIndexTriggers(client: import("cozy-client").CozyClient): Promise<string[]>;
17
19
  export function setupRagIndexing(client: import("cozy-client").CozyClient): Promise<{
18
20
  triggers: string[];
19
21
  migrated: string[];