@atlaskit/rovo-triggers 1.0.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.
Files changed (34) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/LICENSE.md +11 -0
  3. package/README.md +172 -0
  4. package/dist/cjs/common/utils/params/constants.js +8 -0
  5. package/dist/cjs/common/utils/params/index.js +153 -0
  6. package/dist/cjs/common/utils/params/types.js +1 -0
  7. package/dist/cjs/index.js +67 -0
  8. package/dist/cjs/main.js +169 -0
  9. package/dist/cjs/types.js +15 -0
  10. package/dist/es2019/common/utils/params/constants.js +2 -0
  11. package/dist/es2019/common/utils/params/index.js +121 -0
  12. package/dist/es2019/common/utils/params/types.js +0 -0
  13. package/dist/es2019/index.js +2 -0
  14. package/dist/es2019/main.js +159 -0
  15. package/dist/es2019/types.js +9 -0
  16. package/dist/esm/common/utils/params/constants.js +2 -0
  17. package/dist/esm/common/utils/params/index.js +146 -0
  18. package/dist/esm/common/utils/params/types.js +0 -0
  19. package/dist/esm/index.js +2 -0
  20. package/dist/esm/main.js +162 -0
  21. package/dist/esm/types.js +9 -0
  22. package/dist/types/common/utils/params/constants.d.ts +3 -0
  23. package/dist/types/common/utils/params/index.d.ts +15 -0
  24. package/dist/types/common/utils/params/types.d.ts +20 -0
  25. package/dist/types/index.d.ts +4 -0
  26. package/dist/types/main.d.ts +15 -0
  27. package/dist/types/types.d.ts +79 -0
  28. package/dist/types-ts4.5/common/utils/params/constants.d.ts +3 -0
  29. package/dist/types-ts4.5/common/utils/params/index.d.ts +15 -0
  30. package/dist/types-ts4.5/common/utils/params/types.d.ts +20 -0
  31. package/dist/types-ts4.5/index.d.ts +4 -0
  32. package/dist/types-ts4.5/main.d.ts +15 -0
  33. package/dist/types-ts4.5/types.d.ts +79 -0
  34. package/package.json +90 -0
@@ -0,0 +1,121 @@
1
+ import { ROVO_PARAM_PREFIX, ROVO_VALID_PARAMS } from './constants';
2
+ export const firstCharUpper = str => str.charAt(0).toUpperCase() + str.slice(1);
3
+ export const firstCharLower = str => str.charAt(0).toLowerCase() + str.slice(1);
4
+ const isValidURL = url => {
5
+ try {
6
+ new URL(url);
7
+ return true;
8
+ } catch (error) {
9
+ return false;
10
+ }
11
+ };
12
+
13
+ // For new style camelCase params
14
+ export const addPrefix = param => `${ROVO_PARAM_PREFIX}${firstCharUpper(param)}`;
15
+ export const removePrefix = param => {
16
+ return firstCharLower(param.replace(ROVO_PARAM_PREFIX, ''));
17
+ };
18
+
19
+ // Creates RovoChatParams from URLSearchParams
20
+ // Optionally filter to specific params
21
+ const processParams = (input, options = {
22
+ filter: []
23
+ }) => {
24
+ const output = {};
25
+ const safeSearchParamsInput = typeof input === 'string' ? isValidURL(input) ? new URLSearchParams(new URL(input).search) : new URLSearchParams(input) : input;
26
+ const processedInput = new URLSearchParams(safeSearchParamsInput);
27
+ const extracted = new URLSearchParams();
28
+ safeSearchParamsInput.forEach((value, key) => {
29
+ var _options$filter;
30
+ // Only look at rovoChat params
31
+ if (!key.startsWith(ROVO_PARAM_PREFIX)) {
32
+ return;
33
+ }
34
+ const paramKey = removePrefix(key);
35
+ if (options !== null && options !== void 0 && (_options$filter = options.filter) !== null && _options$filter !== void 0 && _options$filter.length) {
36
+ if (options.filter.includes(paramKey)) {
37
+ output[paramKey] = decodeURIComponent(value);
38
+ extracted.append(key, value);
39
+ }
40
+ } else {
41
+ output[paramKey] = decodeURIComponent(value);
42
+ }
43
+
44
+ // Remove rovoParam from processed input
45
+ processedInput.delete(key);
46
+ });
47
+ const combinedQueryString = [processedInput, extracted].map(params => params.toString()).filter(part => part.length > 0).join('&');
48
+ return {
49
+ processed: processedInput,
50
+ rovoParams: output,
51
+ combinedQueryString
52
+ };
53
+ };
54
+
55
+ // Get all rovoChat params from a URL or the current window location if undefined
56
+ export const getRovoParams = url => {
57
+ try {
58
+ const search = url ? new URL(url).search : window.location.search;
59
+ const q = new URLSearchParams(search);
60
+ return processParams(q).rovoParams;
61
+ } catch (error) {
62
+ return {};
63
+ }
64
+ };
65
+
66
+ // Update the address bar without reloading the page
67
+ export const updatePageRovoParams = params => {
68
+ window.history.pushState({}, '', addRovoParamsToUrl(window.location.pathname, params));
69
+ };
70
+
71
+ // Add any valid rovoChat params to a URL
72
+ export const addRovoParamsToUrl = (url, params) => {
73
+ const {
74
+ processed,
75
+ rovoParams
76
+ } = processParams(url);
77
+ const rovoParamsWithExisting = {
78
+ ...rovoParams,
79
+ ...params
80
+ };
81
+ const baseUrl = url.includes('?') ? url.split('?')[0] : url;
82
+ const baseQuery = Array.from(processed).length ? `?${processed.toString()}&` : '?';
83
+ const updatedRovoParamsString = encodeRovoParams(rovoParamsWithExisting);
84
+ return `${baseUrl}${baseQuery}${updatedRovoParamsString}`;
85
+ };
86
+ export const encodeRovoParams = (params, encodeAsObject) => {
87
+ if (encodeAsObject) {
88
+ return Object.entries(params).reduce((acc, [key, value]) => {
89
+ acc[addPrefix(key)] = encodeURIComponent(value);
90
+ return acc;
91
+ }, {});
92
+ }
93
+ return Object.entries(params).map(([key, value]) => {
94
+ return `${addPrefix(key)}=${encodeURIComponent(value)}`;
95
+ }).join('&');
96
+ };
97
+ export const assertOnlySpecificFieldsDefined = (params, fields) => {
98
+ return Object.entries(params).every(([key, value]) => {
99
+ const field = fields.find(field => field === key);
100
+ if (field) {
101
+ return value !== undefined;
102
+ } else {
103
+ return value === undefined;
104
+ }
105
+ });
106
+ };
107
+ export const getListOfRovoParams = ({
108
+ resourceRouterQuery = false
109
+ } = {}) => {
110
+ /*
111
+ For products using react-resource-router (e.g Atlas) to
112
+ pass as the value of `query` on the route definition. It ensures that our parameters
113
+ are not stripped from the URL when loading the page, or through redirect hops
114
+ Any new parameters we want to maintain should be added here as well, which means
115
+ only a version bump is needed in those products to support the latest param list
116
+ The !=false simply means that the parameter is supported but is optional, and should
117
+ not contain a value of 'false'
118
+ */
119
+ const suffix = resourceRouterQuery ? '!=false' : '';
120
+ return ROVO_VALID_PARAMS.map(param => `${addPrefix(param)}${suffix}`);
121
+ };
File without changes
@@ -0,0 +1,2 @@
1
+ export { usePublish, useSubscribe, useSubscribeAll, Subscriber } from './main';
2
+ export { getRovoParams, updatePageRovoParams, addRovoParamsToUrl, assertOnlySpecificFieldsDefined, encodeRovoParams, getListOfRovoParams } from './common/utils/params';
@@ -0,0 +1,159 @@
1
+ import { useCallback, useEffect, useLayoutEffect, useRef } from 'react';
2
+ const ignoredTriggerLatestEvents = new Set(['editor-context-payload']);
3
+ const createPubSub = () => {
4
+ let subscribedEvents = {};
5
+ let publishQueue = {};
6
+ let wildcardEvents = [];
7
+ const subscribe = ({
8
+ topic,
9
+ triggerLatest
10
+ }, callback) => {
11
+ var _subscribedEvents$top;
12
+ const events = (_subscribedEvents$top = subscribedEvents[topic]) !== null && _subscribedEvents$top !== void 0 ? _subscribedEvents$top : [];
13
+ const subId = events.length.toString();
14
+ const subExists = events.some(({
15
+ id
16
+ }) => id === subId);
17
+
18
+ // Push to Topic stack if not already there
19
+ if (!subExists) {
20
+ subscribedEvents = {
21
+ ...subscribedEvents,
22
+ [topic]: [...events, {
23
+ callback,
24
+ id: subId
25
+ }]
26
+ };
27
+ // If this Topic already has a published event and `triggerLatest` is true, trigger the callback then clear the publishQueue for that Topic
28
+ if (triggerLatest && !!publishQueue[topic]) {
29
+ const payload = publishQueue[topic];
30
+ callback(payload);
31
+ delete publishQueue[topic];
32
+ }
33
+ }
34
+ return () => {
35
+ // Remove from Topic stack
36
+ subscribedEvents = {
37
+ ...subscribedEvents,
38
+ [topic]: (subscribedEvents[topic] || []).filter(({
39
+ id
40
+ }) => id !== subId)
41
+ };
42
+ };
43
+ };
44
+ const subscribeAll = callback => {
45
+ const subId = wildcardEvents.length.toString();
46
+ wildcardEvents = [...wildcardEvents, {
47
+ callback,
48
+ id: subId
49
+ }];
50
+ return () => {
51
+ wildcardEvents = wildcardEvents.filter(({
52
+ id
53
+ }) => id !== subId);
54
+ };
55
+ };
56
+ const publish = (topic, payload) => {
57
+ /**
58
+ * Log that this Topic received a published event, regardless of whether it has subscribers or not.
59
+ * This ensures new subscribers can trigger their callback if `triggerLatest` is true, and the event hasn't already been triggered.
60
+ */
61
+ // This `ignoredTriggerLatestEvents` is a quick fix to prevent triggering the latest event for certain events
62
+ if (!ignoredTriggerLatestEvents.has(payload.type)) {
63
+ publishQueue[topic] = payload;
64
+ }
65
+
66
+ // Notify `subscribeAll` subscribers as they are Topic agnostic
67
+ wildcardEvents.forEach(({
68
+ callback
69
+ }) => callback(payload));
70
+ const topicSubs = subscribedEvents[topic] || [];
71
+
72
+ // If there are no subscribers for this Topic, nothing to do.
73
+ if (!topicSubs.length) {
74
+ return;
75
+ }
76
+
77
+ // Notify all Topic subscribers of this event
78
+ topicSubs.forEach(({
79
+ callback
80
+ }) => callback(payload));
81
+ };
82
+ const flushQueue = () => {
83
+ publishQueue = {};
84
+ };
85
+ return {
86
+ subscribe,
87
+ subscribeAll,
88
+ publish,
89
+ flushQueue
90
+ };
91
+ };
92
+ const pubSub = createPubSub();
93
+ const usePubSub = () => {
94
+ return pubSub;
95
+ };
96
+ export const useSubscribe = ({
97
+ topic,
98
+ triggerLatest
99
+ }, callback) => {
100
+ const {
101
+ subscribe
102
+ } = usePubSub();
103
+ const callbackRef = useRef(callback);
104
+ callbackRef.current = callback;
105
+ useEffect(() => {
106
+ const unsubscribe = subscribe({
107
+ topic,
108
+ triggerLatest
109
+ }, (...args) => callbackRef.current(...args));
110
+ return unsubscribe;
111
+ },
112
+ // eslint-disable-next-line react-hooks/exhaustive-deps
113
+ [topic]);
114
+ };
115
+ export const useSubscribeAll = callback => {
116
+ const {
117
+ subscribeAll
118
+ } = usePubSub();
119
+ const callbackRef = useRef(callback);
120
+ callbackRef.current = callback;
121
+ useEffect(() => {
122
+ const unsubscribe = subscribeAll((...args) => callbackRef.current(...args));
123
+ return unsubscribe;
124
+ },
125
+ // eslint-disable-next-line react-hooks/exhaustive-deps
126
+ []);
127
+ };
128
+ const useFlushOnUnmount = (active = false) => {
129
+ const {
130
+ flushQueue
131
+ } = usePubSub();
132
+ useLayoutEffect(() => {
133
+ return () => {
134
+ if (active) {
135
+ flushQueue();
136
+ }
137
+ };
138
+ }, [active, flushQueue]);
139
+ };
140
+ export const usePublish = topic => {
141
+ const {
142
+ publish
143
+ } = usePubSub();
144
+ const publishFn = useCallback(payload => publish(topic, payload), [publish, topic]);
145
+ return publishFn;
146
+ };
147
+ export const Subscriber = ({
148
+ topic,
149
+ triggerLatest,
150
+ onEvent,
151
+ flushQueueOnUnmount
152
+ }) => {
153
+ useSubscribe({
154
+ topic,
155
+ triggerLatest
156
+ }, onEvent);
157
+ useFlushOnUnmount(flushQueueOnUnmount);
158
+ return null;
159
+ };
@@ -0,0 +1,9 @@
1
+ export const Topics = {
2
+ AI_MATE: 'ai-mate'
3
+ };
4
+
5
+ // Not using the PayloadCore because the `data: type | undefined` is necessary
6
+ // but `| undefined` will cause `data` to be removed by PayloadCore
7
+
8
+ // Not using the PayloadCore because the `data: type | undefined` is necessary
9
+ // but `| undefined` will cause `data` to be removed by PayloadCore
@@ -0,0 +1,2 @@
1
+ export var ROVO_PARAM_PREFIX = 'rovoChat';
2
+ export var ROVO_VALID_PARAMS = ['pathway', 'agentId', 'conversationId', 'prompt', 'cloudId', 'triggerOpen'];
@@ -0,0 +1,146 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
3
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
4
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
5
+ import { ROVO_PARAM_PREFIX, ROVO_VALID_PARAMS } from './constants';
6
+ export var firstCharUpper = function firstCharUpper(str) {
7
+ return str.charAt(0).toUpperCase() + str.slice(1);
8
+ };
9
+ export var firstCharLower = function firstCharLower(str) {
10
+ return str.charAt(0).toLowerCase() + str.slice(1);
11
+ };
12
+ var isValidURL = function isValidURL(url) {
13
+ try {
14
+ new URL(url);
15
+ return true;
16
+ } catch (error) {
17
+ return false;
18
+ }
19
+ };
20
+
21
+ // For new style camelCase params
22
+ export var addPrefix = function addPrefix(param) {
23
+ return "".concat(ROVO_PARAM_PREFIX).concat(firstCharUpper(param));
24
+ };
25
+ export var removePrefix = function removePrefix(param) {
26
+ return firstCharLower(param.replace(ROVO_PARAM_PREFIX, ''));
27
+ };
28
+
29
+ // Creates RovoChatParams from URLSearchParams
30
+ // Optionally filter to specific params
31
+ var processParams = function processParams(input) {
32
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
33
+ filter: []
34
+ };
35
+ var output = {};
36
+ var safeSearchParamsInput = typeof input === 'string' ? isValidURL(input) ? new URLSearchParams(new URL(input).search) : new URLSearchParams(input) : input;
37
+ var processedInput = new URLSearchParams(safeSearchParamsInput);
38
+ var extracted = new URLSearchParams();
39
+ safeSearchParamsInput.forEach(function (value, key) {
40
+ var _options$filter;
41
+ // Only look at rovoChat params
42
+ if (!key.startsWith(ROVO_PARAM_PREFIX)) {
43
+ return;
44
+ }
45
+ var paramKey = removePrefix(key);
46
+ if (options !== null && options !== void 0 && (_options$filter = options.filter) !== null && _options$filter !== void 0 && _options$filter.length) {
47
+ if (options.filter.includes(paramKey)) {
48
+ output[paramKey] = decodeURIComponent(value);
49
+ extracted.append(key, value);
50
+ }
51
+ } else {
52
+ output[paramKey] = decodeURIComponent(value);
53
+ }
54
+
55
+ // Remove rovoParam from processed input
56
+ processedInput.delete(key);
57
+ });
58
+ var combinedQueryString = [processedInput, extracted].map(function (params) {
59
+ return params.toString();
60
+ }).filter(function (part) {
61
+ return part.length > 0;
62
+ }).join('&');
63
+ return {
64
+ processed: processedInput,
65
+ rovoParams: output,
66
+ combinedQueryString: combinedQueryString
67
+ };
68
+ };
69
+
70
+ // Get all rovoChat params from a URL or the current window location if undefined
71
+ export var getRovoParams = function getRovoParams(url) {
72
+ try {
73
+ var search = url ? new URL(url).search : window.location.search;
74
+ var q = new URLSearchParams(search);
75
+ return processParams(q).rovoParams;
76
+ } catch (error) {
77
+ return {};
78
+ }
79
+ };
80
+
81
+ // Update the address bar without reloading the page
82
+ export var updatePageRovoParams = function updatePageRovoParams(params) {
83
+ window.history.pushState({}, '', addRovoParamsToUrl(window.location.pathname, params));
84
+ };
85
+
86
+ // Add any valid rovoChat params to a URL
87
+ export var addRovoParamsToUrl = function addRovoParamsToUrl(url, params) {
88
+ var _processParams = processParams(url),
89
+ processed = _processParams.processed,
90
+ rovoParams = _processParams.rovoParams;
91
+ var rovoParamsWithExisting = _objectSpread(_objectSpread({}, rovoParams), params);
92
+ var baseUrl = url.includes('?') ? url.split('?')[0] : url;
93
+ var baseQuery = Array.from(processed).length ? "?".concat(processed.toString(), "&") : '?';
94
+ var updatedRovoParamsString = encodeRovoParams(rovoParamsWithExisting);
95
+ return "".concat(baseUrl).concat(baseQuery).concat(updatedRovoParamsString);
96
+ };
97
+ export var encodeRovoParams = function encodeRovoParams(params, encodeAsObject) {
98
+ if (encodeAsObject) {
99
+ return Object.entries(params).reduce(function (acc, _ref) {
100
+ var _ref2 = _slicedToArray(_ref, 2),
101
+ key = _ref2[0],
102
+ value = _ref2[1];
103
+ acc[addPrefix(key)] = encodeURIComponent(value);
104
+ return acc;
105
+ }, {});
106
+ }
107
+ return Object.entries(params).map(function (_ref3) {
108
+ var _ref4 = _slicedToArray(_ref3, 2),
109
+ key = _ref4[0],
110
+ value = _ref4[1];
111
+ return "".concat(addPrefix(key), "=").concat(encodeURIComponent(value));
112
+ }).join('&');
113
+ };
114
+ export var assertOnlySpecificFieldsDefined = function assertOnlySpecificFieldsDefined(params, fields) {
115
+ return Object.entries(params).every(function (_ref5) {
116
+ var _ref6 = _slicedToArray(_ref5, 2),
117
+ key = _ref6[0],
118
+ value = _ref6[1];
119
+ var field = fields.find(function (field) {
120
+ return field === key;
121
+ });
122
+ if (field) {
123
+ return value !== undefined;
124
+ } else {
125
+ return value === undefined;
126
+ }
127
+ });
128
+ };
129
+ export var getListOfRovoParams = function getListOfRovoParams() {
130
+ var _ref7 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
131
+ _ref7$resourceRouterQ = _ref7.resourceRouterQuery,
132
+ resourceRouterQuery = _ref7$resourceRouterQ === void 0 ? false : _ref7$resourceRouterQ;
133
+ /*
134
+ For products using react-resource-router (e.g Atlas) to
135
+ pass as the value of `query` on the route definition. It ensures that our parameters
136
+ are not stripped from the URL when loading the page, or through redirect hops
137
+ Any new parameters we want to maintain should be added here as well, which means
138
+ only a version bump is needed in those products to support the latest param list
139
+ The !=false simply means that the parameter is supported but is optional, and should
140
+ not contain a value of 'false'
141
+ */
142
+ var suffix = resourceRouterQuery ? '!=false' : '';
143
+ return ROVO_VALID_PARAMS.map(function (param) {
144
+ return "".concat(addPrefix(param)).concat(suffix);
145
+ });
146
+ };
File without changes
@@ -0,0 +1,2 @@
1
+ export { usePublish, useSubscribe, useSubscribeAll, Subscriber } from './main';
2
+ export { getRovoParams, updatePageRovoParams, addRovoParamsToUrl, assertOnlySpecificFieldsDefined, encodeRovoParams, getListOfRovoParams } from './common/utils/params';
@@ -0,0 +1,162 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
3
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
4
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
5
+ import { useCallback, useEffect, useLayoutEffect, useRef } from 'react';
6
+ var ignoredTriggerLatestEvents = new Set(['editor-context-payload']);
7
+ var createPubSub = function createPubSub() {
8
+ var subscribedEvents = {};
9
+ var publishQueue = {};
10
+ var wildcardEvents = [];
11
+ var subscribe = function subscribe(_ref, callback) {
12
+ var _subscribedEvents$top;
13
+ var topic = _ref.topic,
14
+ triggerLatest = _ref.triggerLatest;
15
+ var events = (_subscribedEvents$top = subscribedEvents[topic]) !== null && _subscribedEvents$top !== void 0 ? _subscribedEvents$top : [];
16
+ var subId = events.length.toString();
17
+ var subExists = events.some(function (_ref2) {
18
+ var id = _ref2.id;
19
+ return id === subId;
20
+ });
21
+
22
+ // Push to Topic stack if not already there
23
+ if (!subExists) {
24
+ subscribedEvents = _objectSpread(_objectSpread({}, subscribedEvents), {}, _defineProperty({}, topic, [].concat(_toConsumableArray(events), [{
25
+ callback: callback,
26
+ id: subId
27
+ }])));
28
+ // If this Topic already has a published event and `triggerLatest` is true, trigger the callback then clear the publishQueue for that Topic
29
+ if (triggerLatest && !!publishQueue[topic]) {
30
+ var _payload = publishQueue[topic];
31
+ callback(_payload);
32
+ delete publishQueue[topic];
33
+ }
34
+ }
35
+ return function () {
36
+ // Remove from Topic stack
37
+ subscribedEvents = _objectSpread(_objectSpread({}, subscribedEvents), {}, _defineProperty({}, topic, (subscribedEvents[topic] || []).filter(function (_ref3) {
38
+ var id = _ref3.id;
39
+ return id !== subId;
40
+ })));
41
+ };
42
+ };
43
+ var subscribeAll = function subscribeAll(callback) {
44
+ var subId = wildcardEvents.length.toString();
45
+ wildcardEvents = [].concat(_toConsumableArray(wildcardEvents), [{
46
+ callback: callback,
47
+ id: subId
48
+ }]);
49
+ return function () {
50
+ wildcardEvents = wildcardEvents.filter(function (_ref4) {
51
+ var id = _ref4.id;
52
+ return id !== subId;
53
+ });
54
+ };
55
+ };
56
+ var publish = function publish(topic, payload) {
57
+ /**
58
+ * Log that this Topic received a published event, regardless of whether it has subscribers or not.
59
+ * This ensures new subscribers can trigger their callback if `triggerLatest` is true, and the event hasn't already been triggered.
60
+ */
61
+ // This `ignoredTriggerLatestEvents` is a quick fix to prevent triggering the latest event for certain events
62
+ if (!ignoredTriggerLatestEvents.has(payload.type)) {
63
+ publishQueue[topic] = payload;
64
+ }
65
+
66
+ // Notify `subscribeAll` subscribers as they are Topic agnostic
67
+ wildcardEvents.forEach(function (_ref5) {
68
+ var callback = _ref5.callback;
69
+ return callback(payload);
70
+ });
71
+ var topicSubs = subscribedEvents[topic] || [];
72
+
73
+ // If there are no subscribers for this Topic, nothing to do.
74
+ if (!topicSubs.length) {
75
+ return;
76
+ }
77
+
78
+ // Notify all Topic subscribers of this event
79
+ topicSubs.forEach(function (_ref6) {
80
+ var callback = _ref6.callback;
81
+ return callback(payload);
82
+ });
83
+ };
84
+ var flushQueue = function flushQueue() {
85
+ publishQueue = {};
86
+ };
87
+ return {
88
+ subscribe: subscribe,
89
+ subscribeAll: subscribeAll,
90
+ publish: publish,
91
+ flushQueue: flushQueue
92
+ };
93
+ };
94
+ var pubSub = createPubSub();
95
+ var usePubSub = function usePubSub() {
96
+ return pubSub;
97
+ };
98
+ export var useSubscribe = function useSubscribe(_ref7, callback) {
99
+ var topic = _ref7.topic,
100
+ triggerLatest = _ref7.triggerLatest;
101
+ var _usePubSub = usePubSub(),
102
+ subscribe = _usePubSub.subscribe;
103
+ var callbackRef = useRef(callback);
104
+ callbackRef.current = callback;
105
+ useEffect(function () {
106
+ var unsubscribe = subscribe({
107
+ topic: topic,
108
+ triggerLatest: triggerLatest
109
+ }, function () {
110
+ return callbackRef.current.apply(callbackRef, arguments);
111
+ });
112
+ return unsubscribe;
113
+ },
114
+ // eslint-disable-next-line react-hooks/exhaustive-deps
115
+ [topic]);
116
+ };
117
+ export var useSubscribeAll = function useSubscribeAll(callback) {
118
+ var _usePubSub2 = usePubSub(),
119
+ subscribeAll = _usePubSub2.subscribeAll;
120
+ var callbackRef = useRef(callback);
121
+ callbackRef.current = callback;
122
+ useEffect(function () {
123
+ var unsubscribe = subscribeAll(function () {
124
+ return callbackRef.current.apply(callbackRef, arguments);
125
+ });
126
+ return unsubscribe;
127
+ },
128
+ // eslint-disable-next-line react-hooks/exhaustive-deps
129
+ []);
130
+ };
131
+ var useFlushOnUnmount = function useFlushOnUnmount() {
132
+ var active = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
133
+ var _usePubSub3 = usePubSub(),
134
+ flushQueue = _usePubSub3.flushQueue;
135
+ useLayoutEffect(function () {
136
+ return function () {
137
+ if (active) {
138
+ flushQueue();
139
+ }
140
+ };
141
+ }, [active, flushQueue]);
142
+ };
143
+ export var usePublish = function usePublish(topic) {
144
+ var _usePubSub4 = usePubSub(),
145
+ publish = _usePubSub4.publish;
146
+ var publishFn = useCallback(function (payload) {
147
+ return publish(topic, payload);
148
+ }, [publish, topic]);
149
+ return publishFn;
150
+ };
151
+ export var Subscriber = function Subscriber(_ref8) {
152
+ var topic = _ref8.topic,
153
+ triggerLatest = _ref8.triggerLatest,
154
+ onEvent = _ref8.onEvent,
155
+ flushQueueOnUnmount = _ref8.flushQueueOnUnmount;
156
+ useSubscribe({
157
+ topic: topic,
158
+ triggerLatest: triggerLatest
159
+ }, onEvent);
160
+ useFlushOnUnmount(flushQueueOnUnmount);
161
+ return null;
162
+ };
@@ -0,0 +1,9 @@
1
+ export var Topics = {
2
+ AI_MATE: 'ai-mate'
3
+ };
4
+
5
+ // Not using the PayloadCore because the `data: type | undefined` is necessary
6
+ // but `| undefined` will cause `data` to be removed by PayloadCore
7
+
8
+ // Not using the PayloadCore because the `data: type | undefined` is necessary
9
+ // but `| undefined` will cause `data` to be removed by PayloadCore
@@ -0,0 +1,3 @@
1
+ import { type ValidParam, type ValidPrefix } from './types';
2
+ export declare const ROVO_PARAM_PREFIX: ValidPrefix;
3
+ export declare const ROVO_VALID_PARAMS: ValidParam[];
@@ -0,0 +1,15 @@
1
+ import { type RovoChatParams, type ValidParam } from './types';
2
+ export declare const firstCharUpper: (str: string) => string;
3
+ export declare const firstCharLower: (str: string) => string;
4
+ export declare const addPrefix: (param: ValidParam) => string;
5
+ export declare const removePrefix: (param: string) => string;
6
+ export declare const getRovoParams: (url?: string) => RovoChatParams;
7
+ export declare const updatePageRovoParams: (params: RovoChatParams) => void;
8
+ export declare const addRovoParamsToUrl: (url: string, params: RovoChatParams) => string;
9
+ export declare const encodeRovoParams: (params: RovoChatParams, encodeAsObject?: boolean) => string | {
10
+ [key: string]: string;
11
+ };
12
+ export declare const assertOnlySpecificFieldsDefined: (params: RovoChatParams, fields: (keyof RovoChatParams)[]) => boolean;
13
+ export declare const getListOfRovoParams: ({ resourceRouterQuery }?: {
14
+ resourceRouterQuery?: boolean | undefined;
15
+ }) => string[];
@@ -0,0 +1,20 @@
1
+ export type RovoChatPathway = 'chat' | 'agents-browse' | 'agents-create';
2
+ export interface BaseRovoChatParams {
3
+ pathway: RovoChatPathway;
4
+ agentId: string;
5
+ conversationId: string;
6
+ prompt: string;
7
+ cloudId: string;
8
+ triggerOpen: boolean;
9
+ }
10
+ export type ValidPrefix = 'rovoChat';
11
+ export type ValidParam = keyof BaseRovoChatParams;
12
+ export type ValidPrefixedParam = `${ValidPrefix}${ValidParam}`;
13
+ type RovoParams<T extends RovoChatPathway, P = object> = BaseRovoChatParams & {
14
+ pathway: T;
15
+ } & P;
16
+ type ChatParams = RovoParams<'chat'>;
17
+ type AgentBrowseParams = RovoParams<'agents-browse'>;
18
+ type AgentCreateParams = RovoParams<'agents-create'>;
19
+ export type RovoChatParams = Partial<ChatParams | AgentCreateParams | AgentBrowseParams>;
20
+ export {};
@@ -0,0 +1,4 @@
1
+ export { usePublish, useSubscribe, useSubscribeAll, Subscriber } from './main';
2
+ export type { Payload, Callback, Topic, EditorContextPayloadData, BrowserContextPayloadData, } from './types';
3
+ export { getRovoParams, updatePageRovoParams, addRovoParamsToUrl, assertOnlySpecificFieldsDefined, encodeRovoParams, getListOfRovoParams, } from './common/utils/params';
4
+ export type { RovoChatParams, RovoChatPathway } from './common/utils/params/types';