cozy-search 2.3.3 → 2.4.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.
package/README.md CHANGED
@@ -59,9 +59,16 @@ of verbs (cozy-home and cozy-drive use `ALL`).
59
59
 
60
60
  ### RAG indexing setup
61
61
 
62
- Call `setupRagIndexing(client)` once per session at startup (before
63
- `ensureProvisionedAssistants` if your app provisions assistants from the
64
- `rag.assistants.autoprovision` flag). It makes sure the instance's two
62
+ Opening the assistant (`AssistantView` or `AssistantDialog`) calls
63
+ `autoprovisionAssistants(client)` once per session: when the
64
+ `cozy.assistant.autoprovision` flag lists assistants, it runs
65
+ `setupRagIndexing(client)` then `ensureProvisionedAssistants(client, entries)`.
66
+ A host app that wants this done at startup, before the assistant is
67
+ opened, can call `autoprovisionAssistants(client)` itself (or the
68
+ `useAssistantsAutoprovision` hook): every call shares the first one's
69
+ promise, so the two never race.
70
+
71
+ `setupRagIndexing(client)` makes sure the instance's two
65
72
  `rag-index` triggers exist (one on `io.cozy.files`, one on
66
73
  `io.cozy.ai.chat.assistants`) and gives the root folder to any assistant
67
74
  that has no knowledge base folder yet — the stack's `rag-index` worker
@@ -76,7 +83,7 @@ never throws: on a stack whose `rag-index` worker is still reserved, the
76
83
  additionally creates one `io.cozy.accounts` document per provisioned
77
84
  assistant, and the `dirName` folder in `io.cozy.files`.
78
85
 
79
- An entry of `rag.assistants.autoprovision` can carry `"default": true`
86
+ An entry of `cozy.assistant.autoprovision` can carry `"default": true`
80
87
  (the first flagged entry wins if there are several). Every NEW
81
88
  conversation then starts on that assistant once its document exists —
82
89
  from the session after it was provisioned onward. Existing conversations
@@ -21,6 +21,8 @@ var _Conversation = _interopRequireDefault(require("../Conversations/Conversatio
21
21
 
22
22
  var _CozyAssistantRuntimeProvider = _interopRequireDefault(require("../CozyAssistantRuntimeProvider"));
23
23
 
24
+ var _useAssistantsAutoprovision = require("../KnowledgeBase/useAssistantsAutoprovision");
25
+
24
26
  var _SearchConversation = _interopRequireDefault(require("../Search/SearchConversation"));
25
27
 
26
28
  var _Sidebar = _interopRequireDefault(require("../Sidebar"));
@@ -43,6 +45,7 @@ var AssistantContainer = function AssistantContainer() {
43
45
  var _useAssistant = (0, _AssistantProvider.useAssistant)(),
44
46
  isOpenSearchConversation = _useAssistant.isOpenSearchConversation;
45
47
 
48
+ (0, _useAssistantsAutoprovision.useAssistantsAutoprovision)();
46
49
  return /*#__PURE__*/_react.default.createElement("div", {
47
50
  className: (0, _classnames.default)('u-flex u-ov-hidden u-h-100', styles['assistant-container'])
48
51
  }, /*#__PURE__*/_react.default.createElement(_Sidebar.default, {
@@ -0,0 +1,10 @@
1
+ export function resetAutoprovisionForTests(): void;
2
+ export function autoprovisionAssistants(client: import("cozy-client").CozyClient): Promise<null | {
3
+ setup: object;
4
+ created: string[];
5
+ ensured: string[];
6
+ skipped: {
7
+ id: string;
8
+ reason: string;
9
+ }[];
10
+ }>;
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.resetAutoprovisionForTests = exports.autoprovisionAssistants = void 0;
9
+
10
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
11
+
12
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
13
+
14
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
15
+
16
+ var _cozyFlags = _interopRequireDefault(require("cozy-flags"));
17
+
18
+ var _provisioning = require("./provisioning");
19
+
20
+ var _ragIndexing = require("./ragIndexing");
21
+
22
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
23
+
24
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
25
+
26
+ var warn = function warn() {
27
+ var _console;
28
+
29
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
30
+ args[_key] = arguments[_key];
31
+ }
32
+
33
+ // eslint-disable-next-line no-console
34
+ (_console = console).warn.apply(_console, ['cozy-search autoprovision:'].concat(args));
35
+ };
36
+
37
+ var pending = null;
38
+
39
+ var resetAutoprovisionForTests = function resetAutoprovisionForTests() {
40
+ pending = null;
41
+ };
42
+
43
+ exports.resetAutoprovisionForTests = resetAutoprovisionForTests;
44
+
45
+ var run = /*#__PURE__*/function () {
46
+ var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(client) {
47
+ var entries, setup, result;
48
+ return _regenerator.default.wrap(function _callee$(_context) {
49
+ while (1) {
50
+ switch (_context.prev = _context.next) {
51
+ case 0:
52
+ entries = (0, _cozyFlags.default)(_provisioning.AUTOPROVISION_FLAG);
53
+
54
+ if (!(!Array.isArray(entries) || entries.length === 0)) {
55
+ _context.next = 3;
56
+ break;
57
+ }
58
+
59
+ return _context.abrupt("return", null);
60
+
61
+ case 3:
62
+ _context.next = 5;
63
+ return (0, _ragIndexing.setupRagIndexing)(client);
64
+
65
+ case 5:
66
+ setup = _context.sent;
67
+ _context.next = 8;
68
+ return (0, _provisioning.ensureProvisionedAssistants)(client, entries);
69
+
70
+ case 8:
71
+ result = _context.sent;
72
+
73
+ if (result.skipped.length > 0) {
74
+ warn('skipped entries', result.skipped);
75
+ }
76
+
77
+ return _context.abrupt("return", _objectSpread({
78
+ setup: setup
79
+ }, result));
80
+
81
+ case 11:
82
+ case "end":
83
+ return _context.stop();
84
+ }
85
+ }
86
+ }, _callee);
87
+ }));
88
+
89
+ return function run(_x) {
90
+ return _ref.apply(this, arguments);
91
+ };
92
+ }();
93
+ /**
94
+ * Sets up the rag-index triggers, then provisions the assistants listed in
95
+ * the autoprovision flag. Runs once per session: every call shares the
96
+ * first one's promise, so the assistant UI and the host app's startup can
97
+ * both trigger it without racing each other. Never rejects.
98
+ * @param {import('cozy-client').CozyClient} client - The cozy client.
99
+ * @returns {Promise<null|{setup: object, created: string[], ensured: string[], skipped: {id: string, reason: string}[]}>} Null when the flag lists nothing.
100
+ */
101
+
102
+
103
+ var autoprovisionAssistants = function autoprovisionAssistants(client) {
104
+ if (!pending) {
105
+ pending = run(client).catch(function (error) {
106
+ warn('failed', error);
107
+ return null;
108
+ });
109
+ }
110
+
111
+ return pending;
112
+ };
113
+
114
+ exports.autoprovisionAssistants = autoprovisionAssistants;
@@ -0,0 +1,115 @@
1
+ import flag from 'cozy-flags'
2
+
3
+ import {
4
+ autoprovisionAssistants,
5
+ resetAutoprovisionForTests
6
+ } from './autoprovision'
7
+ import { ensureProvisionedAssistants } from './provisioning'
8
+ import { setupRagIndexing } from './ragIndexing'
9
+
10
+ jest.mock('cozy-flags', () => jest.fn())
11
+ jest.mock('./provisioning', () => ({
12
+ AUTOPROVISION_FLAG: 'cozy.assistant.autoprovision',
13
+ ensureProvisionedAssistants: jest.fn()
14
+ }))
15
+ jest.mock('./ragIndexing', () => ({ setupRagIndexing: jest.fn() }))
16
+
17
+ const client = { id: 'client' }
18
+ const entries = [{ name: 'Docs', dirName: 'Docs' }]
19
+ let warnSpy
20
+
21
+ describe('autoprovisionAssistants', () => {
22
+ beforeEach(() => {
23
+ resetAutoprovisionForTests()
24
+ flag.mockReset()
25
+
26
+ warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {})
27
+ setupRagIndexing
28
+ .mockReset()
29
+ .mockResolvedValue({ triggers: [], migrated: [], errors: [] })
30
+ ensureProvisionedAssistants
31
+ .mockReset()
32
+ .mockResolvedValue({ created: ['docs'], ensured: [], skipped: [] })
33
+ })
34
+
35
+ afterEach(() => {
36
+ warnSpy.mockRestore()
37
+ })
38
+
39
+ it('does nothing without flag entries', async () => {
40
+ flag.mockReturnValue(null)
41
+ await expect(autoprovisionAssistants(client)).resolves.toBeNull()
42
+ expect(flag).toHaveBeenCalledWith('cozy.assistant.autoprovision')
43
+ expect(setupRagIndexing).not.toHaveBeenCalled()
44
+ expect(ensureProvisionedAssistants).not.toHaveBeenCalled()
45
+ })
46
+
47
+ it('sets up the indexing, then provisions the flag entries', async () => {
48
+ flag.mockReturnValue(entries)
49
+ const result = await autoprovisionAssistants(client)
50
+
51
+ expect(setupRagIndexing).toHaveBeenCalledWith(client)
52
+ expect(ensureProvisionedAssistants).toHaveBeenCalledWith(client, entries)
53
+ expect(setupRagIndexing.mock.invocationCallOrder[0]).toBeLessThan(
54
+ ensureProvisionedAssistants.mock.invocationCallOrder[0]
55
+ )
56
+ expect(result).toEqual({
57
+ setup: { triggers: [], migrated: [], errors: [] },
58
+ created: ['docs'],
59
+ ensured: [],
60
+ skipped: []
61
+ })
62
+ })
63
+
64
+ it('runs once per session, sharing the promise', async () => {
65
+ flag.mockReturnValue(entries)
66
+ const first = autoprovisionAssistants(client)
67
+ const second = autoprovisionAssistants(client)
68
+ expect(second).toBe(first)
69
+ await first
70
+ await autoprovisionAssistants(client)
71
+
72
+ expect(setupRagIndexing).toHaveBeenCalledTimes(1)
73
+ expect(ensureProvisionedAssistants).toHaveBeenCalledTimes(1)
74
+ })
75
+
76
+ it('still provisions when the setup reported errors', async () => {
77
+ flag.mockReturnValue(entries)
78
+ setupRagIndexing.mockResolvedValue({
79
+ triggers: [],
80
+ migrated: [],
81
+ errors: [new Error('forbidden')]
82
+ })
83
+ const result = await autoprovisionAssistants(client)
84
+
85
+ expect(ensureProvisionedAssistants).toHaveBeenCalledTimes(1)
86
+ expect(result.setup.errors).toHaveLength(1)
87
+ })
88
+
89
+ it('logs the skipped entries', async () => {
90
+ flag.mockReturnValue(entries)
91
+ ensureProvisionedAssistants.mockResolvedValue({
92
+ created: [],
93
+ ensured: [],
94
+ skipped: [{ id: 'docs', reason: 'nope' }]
95
+ })
96
+ await autoprovisionAssistants(client)
97
+
98
+ expect(warnSpy).toHaveBeenCalledWith(
99
+ 'cozy-search autoprovision:',
100
+ 'skipped entries',
101
+ [{ id: 'docs', reason: 'nope' }]
102
+ )
103
+ })
104
+
105
+ it('never rejects', async () => {
106
+ flag.mockReturnValue(entries)
107
+ setupRagIndexing.mockRejectedValue(new Error('boom'))
108
+ await expect(autoprovisionAssistants(client)).resolves.toBeNull()
109
+ expect(warnSpy).toHaveBeenCalledWith(
110
+ 'cozy-search autoprovision:',
111
+ 'failed',
112
+ expect.any(Error)
113
+ )
114
+ })
115
+ })
@@ -1,4 +1,4 @@
1
- export const AUTOPROVISION_FLAG: "rag.assistants.autoprovision";
1
+ export const AUTOPROVISION_FLAG: "cozy.assistant.autoprovision";
2
2
  export function assistantIdFromName(name: any): string;
3
3
  export function getDefaultProvisionedAssistantId(): string | null;
4
4
  export function isMagicFolderId(dirId: any): boolean;
@@ -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';
@@ -390,7 +390,7 @@ var validate = function validate(entry) {
390
390
  };
391
391
  };
392
392
  /**
393
- * Provisions the assistants described by the rag.assistants.autoprovision
393
+ * Provisions the assistants described by the cozy.assistant.autoprovision
394
394
  * flag. Idempotent: an existing assistant is left alone, or gets the flag's
395
395
  * folder back when it lost it or fell back to the root. Two entries whose
396
396
  * names derive to the same id would fight over one assistant: the second
@@ -485,6 +485,6 @@ describe('getDefaultProvisionedAssistantId', () => {
485
485
  { name: 'Autre', dirName: 'Autre', default: true }
486
486
  ])
487
487
  expect(getDefaultProvisionedAssistantId()).toBe('mes-documents')
488
- expect(flag).toHaveBeenCalledWith('rag.assistants.autoprovision')
488
+ expect(flag).toHaveBeenCalledWith('cozy.assistant.autoprovision')
489
489
  })
490
490
  })
@@ -0,0 +1 @@
1
+ export function useAssistantsAutoprovision(): void;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useAssistantsAutoprovision = void 0;
7
+
8
+ var _react = require("react");
9
+
10
+ var _cozyClient = require("cozy-client");
11
+
12
+ var _autoprovision = require("./autoprovision");
13
+
14
+ /**
15
+ * Provisions the flagged assistants when the assistant UI opens, so an
16
+ * instance gets them even if the host app did not do it at startup.
17
+ */
18
+ var useAssistantsAutoprovision = function useAssistantsAutoprovision() {
19
+ var client = (0, _cozyClient.useClient)();
20
+ (0, _react.useEffect)(function () {
21
+ if (client) (0, _autoprovision.autoprovisionAssistants)(client);
22
+ }, [client]);
23
+ };
24
+
25
+ exports.useAssistantsAutoprovision = useAssistantsAutoprovision;
@@ -0,0 +1,33 @@
1
+ import { renderHook } from '@testing-library/react-hooks/dom'
2
+
3
+ import { useClient } from 'cozy-client'
4
+
5
+ import { autoprovisionAssistants } from './autoprovision'
6
+ import { useAssistantsAutoprovision } from './useAssistantsAutoprovision'
7
+
8
+ jest.mock('cozy-client', () => ({ useClient: jest.fn() }))
9
+ jest.mock('./autoprovision', () => ({ autoprovisionAssistants: jest.fn() }))
10
+
11
+ describe('useAssistantsAutoprovision', () => {
12
+ beforeEach(() => {
13
+ useClient.mockReset()
14
+ autoprovisionAssistants.mockReset().mockResolvedValue(null)
15
+ })
16
+
17
+ it('provisions with the client on mount', () => {
18
+ const client = { id: 'client' }
19
+ useClient.mockReturnValue(client)
20
+ const { rerender } = renderHook(() => useAssistantsAutoprovision())
21
+ rerender()
22
+
23
+ expect(autoprovisionAssistants).toHaveBeenCalledTimes(1)
24
+ expect(autoprovisionAssistants).toHaveBeenCalledWith(client)
25
+ })
26
+
27
+ it('waits for a client', () => {
28
+ useClient.mockReturnValue(null)
29
+ renderHook(() => useAssistantsAutoprovision())
30
+
31
+ expect(autoprovisionAssistants).not.toHaveBeenCalled()
32
+ })
33
+ })
@@ -23,8 +23,7 @@ exports.ACCOUNTS_DOCTYPE = ACCOUNTS_DOCTYPE;
23
23
  var EMAIL_DOCTYPE = 'com.linagora.email';
24
24
  exports.EMAIL_DOCTYPE = EMAIL_DOCTYPE;
25
25
 
26
- var defaultFetchPolicy = _cozyClient.fetchPolicies.olderThan(86400); // 24 hours
27
-
26
+ var defaultFetchPolicy = _cozyClient.fetchPolicies.olderThan(5 * 60 * 1000);
28
27
 
29
28
  var buildFilesByIds = function buildFilesByIds(ids, enabled) {
30
29
  return {
package/dist/index.d.ts CHANGED
@@ -11,3 +11,5 @@ export { StreamBridge, createCozyRealtimeChatAdapter } from './components/adapte
11
11
  export { default as AssistantView } from './components/Views/AssistantView';
12
12
  export { assistantIdFromName, AUTOPROVISION_FLAG, ensureProvisionedAssistants, getDefaultProvisionedAssistantId } from './components/KnowledgeBase/provisioning';
13
13
  export { setupRagIndexing } from './components/KnowledgeBase/ragIndexing';
14
+ export { autoprovisionAssistants } from './components/KnowledgeBase/autoprovision';
15
+ export { useAssistantsAutoprovision } from './components/KnowledgeBase/useAssistantsAutoprovision';
package/dist/index.js CHANGED
@@ -83,6 +83,12 @@ Object.defineProperty(exports, "assistantIdFromName", {
83
83
  return _provisioning.assistantIdFromName;
84
84
  }
85
85
  });
86
+ Object.defineProperty(exports, "autoprovisionAssistants", {
87
+ enumerable: true,
88
+ get: function get() {
89
+ return _autoprovision.autoprovisionAssistants;
90
+ }
91
+ });
86
92
  Object.defineProperty(exports, "createCozyRealtimeChatAdapter", {
87
93
  enumerable: true,
88
94
  get: function get() {
@@ -107,6 +113,12 @@ Object.defineProperty(exports, "setupRagIndexing", {
107
113
  return _ragIndexing.setupRagIndexing;
108
114
  }
109
115
  });
116
+ Object.defineProperty(exports, "useAssistantsAutoprovision", {
117
+ enumerable: true,
118
+ get: function get() {
119
+ return _useAssistantsAutoprovision.useAssistantsAutoprovision;
120
+ }
121
+ });
110
122
 
111
123
  var _AssistantLink = _interopRequireDefault(require("./components/Search/AssistantLink"));
112
124
 
@@ -132,4 +144,8 @@ var _AssistantView = _interopRequireDefault(require("./components/Views/Assistan
132
144
 
133
145
  var _provisioning = require("./components/KnowledgeBase/provisioning");
134
146
 
135
- var _ragIndexing = require("./components/KnowledgeBase/ragIndexing");
147
+ var _ragIndexing = require("./components/KnowledgeBase/ragIndexing");
148
+
149
+ var _autoprovision = require("./components/KnowledgeBase/autoprovision");
150
+
151
+ var _useAssistantsAutoprovision = require("./components/KnowledgeBase/useAssistantsAutoprovision");
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "cozy-search",
3
3
  "description": "UI components about search bar and IA assistant",
4
- "version": "2.3.3",
4
+ "version": "2.4.0",
5
5
  "author": {
6
6
  "name": "Cozy Cloud",
7
7
  "url": "https://github.com/cozy"
@@ -96,5 +96,5 @@
96
96
  "lint": "cd .. && yarn eslint --ext js,jsx,ts packages/cozy-search"
97
97
  },
98
98
  "types": "dist/index.d.ts",
99
- "gitHead": "5269c3bcf245d4e9e47cbefd29286aa733602bb7"
99
+ "gitHead": "c1957ceb37bd485a80d871abc3d583238ef4f2e7"
100
100
  }