@grafana/assistant 0.1.32 → 0.1.34

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 (110) hide show
  1. package/README.md +12 -2
  2. package/dist/esm/components/button/OpenAssistantButton.js +106 -0
  3. package/dist/esm/components/button/OpenAssistantButton.js.map +1 -0
  4. package/dist/esm/components/button/OpenAssistantSplitButton.js +135 -0
  5. package/dist/esm/components/button/OpenAssistantSplitButton.js.map +1 -0
  6. package/dist/esm/components/button/QueryWithAssistantButton.js +62 -0
  7. package/dist/esm/components/button/QueryWithAssistantButton.js.map +1 -0
  8. package/dist/esm/components/index.js +8 -0
  9. package/dist/esm/components/index.js.map +1 -0
  10. package/dist/esm/components/input/AITextArea.js +91 -0
  11. package/dist/esm/components/input/AITextArea.js.map +1 -0
  12. package/dist/esm/components/input/AITextInput.js +70 -0
  13. package/dist/esm/components/input/AITextInput.js.map +1 -0
  14. package/dist/esm/components/input/types.js +2 -0
  15. package/dist/esm/components/input/types.js.map +1 -0
  16. package/dist/esm/components/input/useAIGeneration.js +58 -0
  17. package/dist/esm/components/input/useAIGeneration.js.map +1 -0
  18. package/dist/esm/components/input/useGeneratingPlaceholder.js +38 -0
  19. package/dist/esm/components/input/useGeneratingPlaceholder.js.map +1 -0
  20. package/dist/esm/components/prompt-card/AssistantPromptCard.js +115 -0
  21. package/dist/esm/components/prompt-card/AssistantPromptCard.js.map +1 -0
  22. package/dist/esm/components/prompt-card/useAnimatedPlaceholder.js +68 -0
  23. package/dist/esm/components/prompt-card/useAnimatedPlaceholder.js.map +1 -0
  24. package/dist/esm/context/base.js +47 -0
  25. package/dist/esm/context/base.js.map +1 -0
  26. package/dist/esm/context/chat.js +78 -0
  27. package/dist/esm/context/chat.js.map +1 -0
  28. package/dist/esm/context/component.js +141 -0
  29. package/dist/esm/context/component.js.map +1 -0
  30. package/dist/esm/context/dashboard.js +49 -0
  31. package/dist/esm/context/dashboard.js.map +1 -0
  32. package/dist/esm/context/datasource.js +25 -0
  33. package/dist/esm/context/datasource.js.map +1 -0
  34. package/dist/esm/context/entity.js +35 -0
  35. package/dist/esm/context/entity.js.map +1 -0
  36. package/dist/esm/context/factory.js +60 -0
  37. package/dist/esm/context/factory.js.map +1 -0
  38. package/dist/esm/context/index.js +13 -0
  39. package/dist/esm/context/index.js.map +1 -0
  40. package/dist/esm/context/label.js +62 -0
  41. package/dist/esm/context/label.js.map +1 -0
  42. package/dist/esm/context/page.js +365 -0
  43. package/dist/esm/context/page.js.map +1 -0
  44. package/dist/esm/context/questions.js +111 -0
  45. package/dist/esm/context/questions.js.map +1 -0
  46. package/dist/esm/context/skill.js +28 -0
  47. package/dist/esm/context/skill.js.map +1 -0
  48. package/dist/esm/context/types.js +2 -0
  49. package/dist/esm/context/types.js.map +1 -0
  50. package/dist/esm/extension/extension.js +45 -0
  51. package/dist/esm/extension/extension.js.map +1 -0
  52. package/dist/esm/extension/index.js +2 -0
  53. package/dist/esm/extension/index.js.map +1 -0
  54. package/dist/esm/functions.js +22 -0
  55. package/dist/esm/functions.js.map +1 -0
  56. package/dist/esm/hook.js +23 -0
  57. package/dist/esm/hook.js.map +1 -0
  58. package/dist/esm/index.js +16 -0
  59. package/dist/esm/index.js.map +1 -0
  60. package/dist/esm/inline/index.js +3 -0
  61. package/dist/esm/inline/index.js.map +1 -0
  62. package/dist/esm/inline/inlineAssistant.js +75 -0
  63. package/dist/esm/inline/inlineAssistant.js.map +1 -0
  64. package/dist/esm/inline/types.js +2 -0
  65. package/dist/esm/inline/types.js.map +1 -0
  66. package/dist/esm/inline/useInlineAssistant.js +158 -0
  67. package/dist/esm/inline/useInlineAssistant.js.map +1 -0
  68. package/dist/esm/k6.js +33 -0
  69. package/dist/esm/k6.js.map +1 -0
  70. package/dist/esm/lens/index.js +3 -0
  71. package/dist/esm/lens/index.js.map +1 -0
  72. package/dist/esm/lens/investigationLens.js +103 -0
  73. package/dist/esm/lens/investigationLens.js.map +1 -0
  74. package/dist/esm/lens/types.js +2 -0
  75. package/dist/esm/lens/types.js.map +1 -0
  76. package/dist/esm/lens/useInvestigationThroughLens.js +125 -0
  77. package/dist/esm/lens/useInvestigationThroughLens.js.map +1 -0
  78. package/dist/esm/limits/index.js +140 -0
  79. package/dist/esm/limits/index.js.map +1 -0
  80. package/dist/esm/plugin.js +52 -0
  81. package/dist/esm/plugin.js.map +1 -0
  82. package/dist/esm/sidebar.js +81 -0
  83. package/dist/esm/sidebar.js.map +1 -0
  84. package/dist/esm/terms/index.js +136 -0
  85. package/dist/esm/terms/index.js.map +1 -0
  86. package/dist/esm/terms/settings.gen.js +6 -0
  87. package/dist/esm/terms/settings.gen.js.map +1 -0
  88. package/dist/esm/terms/versionCompare.js +83 -0
  89. package/dist/esm/terms/versionCompare.js.map +1 -0
  90. package/dist/esm/tools/index.js +2 -0
  91. package/dist/esm/tools/index.js.map +1 -0
  92. package/dist/esm/tools/toolFactory.js +64 -0
  93. package/dist/esm/tools/toolFactory.js.map +1 -0
  94. package/dist/esm/tools/types.js +2 -0
  95. package/dist/esm/tools/types.js.map +1 -0
  96. package/dist/esm/utils/errorHandling.js +95 -0
  97. package/dist/esm/utils/errorHandling.js.map +1 -0
  98. package/dist/esm/utils/hash.js +13 -0
  99. package/dist/esm/utils/hash.js.map +1 -0
  100. package/dist/esm/utils/theme.js +65 -0
  101. package/dist/esm/utils/theme.js.map +1 -0
  102. package/dist/extension/extension.d.ts +81 -20
  103. package/dist/extension/extension.d.ts.map +1 -1
  104. package/dist/extension/index.d.ts +1 -1
  105. package/dist/extension/index.d.ts.map +1 -1
  106. package/dist/index.js +1 -1
  107. package/dist/index.js.map +1 -1
  108. package/dist/terms/settings.gen.d.ts +134 -0
  109. package/dist/terms/settings.gen.d.ts.map +1 -1
  110. package/package.json +12 -6
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entity.js","sourceRoot":"","sources":["../../../src/context/entity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,QAAQ,EAAE,MAAM,QA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,60 @@
1
+ import { getDataSourceSrv } from '@grafana/runtime';
2
+ import { NodeData, StructuredNodeData } from './base';
3
+ import { ComponentNodeData } from './component';
4
+ import { DashboardNodeData, FolderNodeData, } from './dashboard';
5
+ import { DatasourceNodeData } from './datasource';
6
+ import { EntityNodeData } from './entity';
7
+ import { LabelNameNodeData, LabelValueNodeData, } from './label';
8
+ /**
9
+ * Helper function to get datasource type from datasource UID
10
+ */
11
+ function getDatasourceMeta(datasourceUid) {
12
+ var _a, _b, _c, _d, _e;
13
+ const datasourceInstance = getDataSourceSrv().getInstanceSettings(datasourceUid);
14
+ return {
15
+ datasourceType: (_a = datasourceInstance === null || datasourceInstance === void 0 ? void 0 : datasourceInstance.type) !== null && _a !== void 0 ? _a : 'unknown',
16
+ datasourceName: (_b = datasourceInstance === null || datasourceInstance === void 0 ? void 0 : datasourceInstance.name) !== null && _b !== void 0 ? _b : datasourceUid,
17
+ img: (_e = (_d = (_c = datasourceInstance === null || datasourceInstance === void 0 ? void 0 : datasourceInstance.meta) === null || _c === void 0 ? void 0 : _c.info) === null || _d === void 0 ? void 0 : _d.logos) === null || _e === void 0 ? void 0 : _e.small,
18
+ };
19
+ }
20
+ /**
21
+ * Creates context nodes based on type with full type safety using generics
22
+ */
23
+ export function createContextByType(type, params) {
24
+ switch (type) {
25
+ case 'datasource':
26
+ return new DatasourceNodeData({
27
+ ...params,
28
+ ...getDatasourceMeta(params.datasourceUid),
29
+ });
30
+ case 'label_name':
31
+ return new LabelNameNodeData({
32
+ ...params,
33
+ ...getDatasourceMeta(params.datasourceUid),
34
+ });
35
+ case 'label_value':
36
+ return new LabelValueNodeData({
37
+ ...params,
38
+ ...getDatasourceMeta(params.datasourceUid),
39
+ });
40
+ case 'dashboard':
41
+ return new DashboardNodeData(params);
42
+ case 'dashboard_folder':
43
+ return new FolderNodeData(params);
44
+ case 'structured':
45
+ return new StructuredNodeData(params);
46
+ case 'component':
47
+ return new ComponentNodeData({
48
+ ...params,
49
+ hidden: true,
50
+ });
51
+ case 'entity':
52
+ return new EntityNodeData(params);
53
+ case 'unknown':
54
+ return new NodeData(params);
55
+ default:
56
+ console.error(`Unknown context type: ${type}`);
57
+ return new NodeData(params);
58
+ }
59
+ }
60
+ //# sourceMappingURL=factory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"factory.js","sourceRoot":"","sources":["../../../src/context/factory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAuB,kBAAkB,EAAiC,MAAM,QAAQ,CAAC;AAC1G,OAAO,EAAE,iBAAiB,EAAqC,MAAM,aAAa,CAAC;AACnF,OAAO,EACL,iBAAiB,EACjB,cAAc,GAGf,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,kBAAkB,EAAsC,MAAM,cAAc,CAAC;AACtF,OAAO,EAAE,cAAc,EAAkC,MAAM,UAAU,CAAC;AAC1E,OAAO,EACL,iBAAiB,EACjB,kBAAkB,GAGnB,MAAM,SAAS,CAAC;AAuBjB;;GAEG;AACH,SAAS,iBAAiB,CAAC,aAAqB;;IAC9C,MAAM,kBAAkB,GAAG,gBAAgB,EAAE,CAAC,mBAAmB,CAAC,aAAa,CAAC,CAAC;IACjF,OAAO;QACL,cAAc,EAAE,MAAA,kBAAkB,aAAlB,kBAAkB,uBAAlB,kBAAkB,CAAE,IAAI,mCAAI,SAAS;QACrD,cAAc,EAAE,MAAA,kBAAkB,aAAlB,kBAAkB,uBAAlB,kBAAkB,CAAE,IAAI,mCAAI,aAAa;QACzD,GAAG,EAAE,MAAA,MAAA,MAAA,kBAAkB,aAAlB,kBAAkB,uBAAlB,kBAAkB,CAAE,IAAI,0CAAE,IAAI,0CAAE,KAAK,0CAAE,KAAK;KAClD,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,mBAAmB,CACjC,IAAO,EACP,MAAwC;IAExC,QAAQ,IAAI,EAAE,CAAC;QACb,KAAK,YAAY;YACf,OAAO,IAAI,kBAAkB,CAAC;gBAC5B,GAAI,MAAwC;gBAC5C,GAAG,iBAAiB,CAAE,MAAwC,CAAC,aAAa,CAAC;aAC9E,CAAC,CAAC;QACL,KAAK,YAAY;YACf,OAAO,IAAI,iBAAiB,CAAC;gBAC3B,GAAI,MAAuC;gBAC3C,GAAG,iBAAiB,CAAE,MAAuC,CAAC,aAAa,CAAC;aAC7E,CAAC,CAAC;QACL,KAAK,aAAa;YAChB,OAAO,IAAI,kBAAkB,CAAC;gBAC5B,GAAI,MAAwC;gBAC5C,GAAG,iBAAiB,CAAE,MAAwC,CAAC,aAAa,CAAC;aAC9E,CAAC,CAAC;QACL,KAAK,WAAW;YACd,OAAO,IAAI,iBAAiB,CAAC,MAAsC,CAAC,CAAC;QACvE,KAAK,kBAAkB;YACrB,OAAO,IAAI,cAAc,CAAC,MAAmC,CAAC,CAAC;QACjE,KAAK,YAAY;YACf,OAAO,IAAI,kBAAkB,CAAC,MAAkC,CAAC,CAAC;QACpE,KAAK,WAAW;YACd,OAAO,IAAI,iBAAiB,CAAC;gBAC3B,GAAI,MAAuC;gBAC3C,MAAM,EAAE,IAAI;aACb,CAAC,CAAC;QACL,KAAK,QAAQ;YACX,OAAO,IAAI,cAAc,CAAC,MAAmC,CAAC,CAAC;QACjE,KAAK,SAAS;YACZ,OAAO,IAAI,QAAQ,CAAC,MAAwB,CAAC,CAAC;QAChD;YACE,OAAO,CAAC,KAAK,CAAC,yBAAyB,IAAI,EAAE,CAAC,CAAC;YAC/C,OAAO,IAAI,QAAQ,CAAC,MAAwB,CAAC,CAAC;IAClD,CAAC;AACH,CAAC"}
@@ -0,0 +1,13 @@
1
+ export * from './base';
2
+ export * from './chat';
3
+ export * from './component';
4
+ export * from './dashboard';
5
+ export * from './datasource';
6
+ export * from './entity';
7
+ export * from './factory';
8
+ export * from './label';
9
+ export * from './page';
10
+ export * from './skill';
11
+ export * from './questions';
12
+ export * from './types';
13
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/context/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC"}
@@ -0,0 +1,62 @@
1
+ import { NodeData } from './base';
2
+ export class LabelNameNodeData extends NodeData {
3
+ constructor(params) {
4
+ super({ ...params, id: `${params.datasourceUid}-${params.labelName}`, type: 'label_name' });
5
+ this.datasourceUid = params.datasourceUid;
6
+ this.datasourceType = params.datasourceType;
7
+ this.datasourceName = params.datasourceName;
8
+ this.labelName = params.labelName;
9
+ this.text = params.labelName;
10
+ this.img = params.img;
11
+ }
12
+ formatForLLM(codeElementIds) {
13
+ return {
14
+ type: this.type,
15
+ codeElementIds,
16
+ bypassLimits: this.bypassLimits,
17
+ data: {
18
+ name: this.labelName,
19
+ datasourceUid: this.datasourceUid,
20
+ datasourceType: this.datasourceType,
21
+ datasourceName: this.datasourceName,
22
+ labelName: this.labelName,
23
+ text: this.text,
24
+ },
25
+ };
26
+ }
27
+ }
28
+ export class LabelValueNodeData extends NodeData {
29
+ constructor(params) {
30
+ super({
31
+ ...params,
32
+ id: `${params.datasourceUid}-${params.labelName}-${params.labelValue}-${params.operator || '='}`,
33
+ type: 'label_value',
34
+ });
35
+ this.datasourceUid = params.datasourceUid;
36
+ this.datasourceType = params.datasourceType;
37
+ this.datasourceName = params.datasourceName;
38
+ this.labelName = params.labelName;
39
+ this.labelValue = params.labelValue;
40
+ this.text = params.labelValue;
41
+ this.img = params.img;
42
+ this.operator = params.operator || '=';
43
+ }
44
+ formatForLLM(codeElementIds) {
45
+ return {
46
+ type: this.type,
47
+ codeElementIds,
48
+ bypassLimits: this.bypassLimits,
49
+ data: {
50
+ name: this.labelValue,
51
+ datasourceUid: this.datasourceUid,
52
+ datasourceType: this.datasourceType,
53
+ datasourceName: this.datasourceName,
54
+ labelName: this.labelName,
55
+ labelValue: this.labelValue,
56
+ text: this.text,
57
+ operator: this.operator,
58
+ },
59
+ };
60
+ }
61
+ }
62
+ //# sourceMappingURL=label.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"label.js","sourceRoot":"","sources":["../../../src/context/label.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAenD,MAAM,OAAO,iBAAkB,SAAQ,QAAQ;IAO7C,YAAY,MAAqD;QAC/D,KAAK,CAAC,EAAE,GAAG,MAAM,EAAE,EAAE,EAAE,GAAG,MAAM,CAAC,aAAa,IAAI,MAAM,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,YAAY,EAAE,CAAC,CAAC;QAC5F,IAAI,CAAC,aAAa,GAAG,MAAM,CAAC,aAAa,CAAC;QAC1C,IAAI,CAAC,cAAc,GAAG,MAAM,CAAC,cAAc,CAAC;QAC5C,IAAI,CAAC,cAAc,GAAG,MAAM,CAAC,cAAc,CAAC;QAC5C,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,SAAS,CAAC;QAClC,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,SAAS,CAAC;QAC7B,IAAI,CAAC,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC;IACxB,CAAC;IAED,YAAY,CAAC,cAAyB;QACpC,OAAO;YACL,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,cAAc;YACd,YAAY,EAAE,IAAI,CAAC,YAAY;YAC/B,IAAI,EAAE;gBACJ,IAAI,EAAE,IAAI,CAAC,SAAS;gBACpB,aAAa,EAAE,IAAI,CAAC,aAAa;gBACjC,cAAc,EAAE,IAAI,CAAC,cAAc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AC,UAAU;gBAC3B,IAAI,EAAE,IAAI,CAAC,IAAI;gBACf,QAAQ,EAAE,IAAI,CAAC,QAAQ;aACK;SAC/B,CAAC;IACJ,CAAC;CACF"}
@@ -0,0 +1,365 @@
1
+ import { useLocationService } from '@grafana/runtime';
2
+ import { useEffect, useState, useRef, useCallback } from 'react';
3
+ // Global registry key
4
+ const GLOBAL_REGISTRY_KEY = '__grafanaAssistantPageContextRegistry__';
5
+ // Custom event for registry changes
6
+ const REGISTRY_CHANGE_EVENT = 'grafana-assistant:page-context-registry-changed';
7
+ /**
8
+ * Legacy event names for backward compatibility with the previous implementation.
9
+ * These events are emitted alongside the new REGISTRY_CHANGE_EVENT to ensure
10
+ * any existing code listening to the old events continues to work.
11
+ *
12
+ * @deprecated These event name is maintained for backward compatibility only.
13
+ * TODO: Remove after old event-based approach is fully deprecated
14
+ */
15
+ const LEGACY_REGISTRY_UPDATE_EVENT = 'grafana-assistant:page-context-update';
16
+ /**
17
+ * @deprecated These event name is maintained for backward compatibility only.
18
+ * TODO: Remove after old event-based approach is fully deprecated
19
+ */
20
+ const LEGACY_REGISTRY_REMOVE_EVENT = 'grafana-assistant:page-context-remove';
21
+ /**
22
+ * @deprecated These event name is maintained for backward compatibility only.
23
+ * TODO: Remove after old event-based approach is fully deprecated
24
+ */
25
+ const LEGACY_REGISTRY_SYNC_EVENT = 'grafana-assistant:page-context-sync';
26
+ // Ensure global registry exists on window
27
+ if (!window[GLOBAL_REGISTRY_KEY]) {
28
+ window[GLOBAL_REGISTRY_KEY] = [];
29
+ }
30
+ // Get reference to the global registry
31
+ function getPageContextRegistry() {
32
+ return window[GLOBAL_REGISTRY_KEY];
33
+ }
34
+ // TODO: Remove skipLegacyEvents parameter after old event-based approach is fully deprecated
35
+ function setPageContextRegistry(newRegistry, skipLegacyEvents = false) {
36
+ window[GLOBAL_REGISTRY_KEY] = newRegistry;
37
+ // Notify all listeners that the registry has changed
38
+ window.dispatchEvent(new CustomEvent(REGISTRY_CHANGE_EVENT));
39
+ // TODO: Remove this legacy event emission after old event-based approach is fully deprecated
40
+ // Emit legacy sync event for backward compatibility (unless we're processing a legacy event)
41
+ if (!skipLegacyEvents) {
42
+ window.dispatchEvent(new CustomEvent(LEGACY_REGISTRY_SYNC_EVENT, {
43
+ detail: { registry: newRegistry },
44
+ }));
45
+ }
46
+ }
47
+ /**
48
+ * Bridge to handle legacy events from external apps using old versions of @grafana/assistant.
49
+ * This ensures that registrations from external apps get synced into our window registry.
50
+ *
51
+ * TODO: Remove this entire bridge after old event-based approach is fully deprecated
52
+ */
53
+ let legacyBridgeInitialized = false;
54
+ function initializeLegacyEventBridge() {
55
+ if (legacyBridgeInitialized) {
56
+ return;
57
+ }
58
+ legacyBridgeInitialized = true;
59
+ // Handle legacy update events (registration added or updated)
60
+ window.addEventListener(LEGACY_REGISTRY_UPDATE_EVENT, (event) => {
61
+ const customEvent = event;
62
+ const registration = customEvent.detail;
63
+ if (!(registration === null || registration === void 0 ? void 0 : registration.id)) {
64
+ return;
65
+ }
66
+ const currentRegistry = getPageContextRegistry();
67
+ const existingIndex = currentRegistry.findIndex((r) => r.id === registration.id);
68
+ if (existingIndex !== -1) {
69
+ // Update existing registration
70
+ const newRegistry = [...currentRegistry];
71
+ newRegistry[existingIndex] = registration;
72
+ setPageContextRegistry(newRegistry, true); // Skip legacy events to avoid loops
73
+ }
74
+ else {
75
+ // Add new registration
76
+ setPageContextRegistry([...currentRegistry, registration], true); // Skip legacy events to avoid loops
77
+ }
78
+ });
79
+ // Handle legacy remove events (registration removed)
80
+ window.addEventListener(LEGACY_REGISTRY_REMOVE_EVENT, (event) => {
81
+ var _a;
82
+ const customEvent = event;
83
+ const id = (_a = customEvent.detail) === null || _a === void 0 ? void 0 : _a.id;
84
+ if (!id) {
85
+ return;
86
+ }
87
+ const currentRegistry = getPageContextRegistry();
88
+ const index = currentRegistry.findIndex((r) => r.id === id);
89
+ if (index !== -1) {
90
+ const newRegistry = currentRegistry.filter((_, i) => i !== index);
91
+ setPageContextRegistry(newRegistry, true); // Skip legacy events to avoid loops
92
+ }
93
+ });
94
+ // Handle legacy sync events (entire registry sync from external app)
95
+ window.addEventListener(LEGACY_REGISTRY_SYNC_EVENT, (event) => {
96
+ var _a;
97
+ const customEvent = event;
98
+ const incomingRegistry = (_a = customEvent.detail) === null || _a === void 0 ? void 0 : _a.registry;
99
+ if (!Array.isArray(incomingRegistry)) {
100
+ return;
101
+ }
102
+ const currentRegistry = getPageContextRegistry();
103
+ const currentIds = new Set(currentRegistry.map((r) => r.id));
104
+ // Merge: add any registrations from external app that we don't have
105
+ const newRegistrations = incomingRegistry.filter((r) => !currentIds.has(r.id));
106
+ if (newRegistrations.length > 0) {
107
+ setPageContextRegistry([...currentRegistry, ...newRegistrations], true); // Skip legacy events to avoid loops
108
+ }
109
+ });
110
+ }
111
+ // TODO: Remove this initialization after old event-based approach is fully deprecated
112
+ // Initialize the bridge on module load
113
+ initializeLegacyEventBridge();
114
+ /**
115
+ * Registers context items for specific pages based on URL patterns.
116
+ * Returns a setter function to update the context dynamically, similar to useState.
117
+ *
118
+ * Multiple registrations with the same URL pattern are allowed and will all be merged
119
+ * when the pattern matches. Each call creates a new independent registration.
120
+ *
121
+ * @param urlPattern - URL pattern (string or RegExp) to match against page URLs
122
+ * @param initialContext - *Initial* array of ContextItems to provide when the pattern matches. IMPORTANT: will be overwritten when setContext is used to update the context.
123
+ * @returns A setter function to update the context, with an unregister method attached
124
+ */
125
+ export function providePageContext(urlPattern, initialContext) {
126
+ // Validate URL pattern at registration time (only warn once, not on every match)
127
+ validateUrlPattern(urlPattern);
128
+ // Always create a new registration to allow multiple providers with the same URL pattern
129
+ const registration = {
130
+ id: generateRegistrationId(),
131
+ urlPattern,
132
+ context: [...initialContext],
133
+ };
134
+ const registry = getPageContextRegistry();
135
+ setPageContextRegistry([...registry, registration]);
136
+ // TODO: Remove this legacy event emission after old event-based approach is fully deprecated
137
+ // Emit legacy update event for backward compatibility
138
+ window.dispatchEvent(new CustomEvent(LEGACY_REGISTRY_UPDATE_EVENT, {
139
+ detail: registration,
140
+ }));
141
+ // Create setter function that can update the context
142
+ const setContext = (newContext) => {
143
+ const currentRegistry = getPageContextRegistry();
144
+ const index = currentRegistry.findIndex((reg) => reg.id === registration.id);
145
+ if (index !== -1) {
146
+ const newRegistry = [...currentRegistry];
147
+ newRegistry[index] = { ...newRegistry[index], context: [...newContext] };
148
+ setPageContextRegistry(newRegistry);
149
+ // TODO: Remove this legacy event emission after old event-based approach is fully deprecated
150
+ // Emit legacy update event for backward compatibility
151
+ window.dispatchEvent(new CustomEvent(LEGACY_REGISTRY_UPDATE_EVENT, {
152
+ detail: newRegistry[index],
153
+ }));
154
+ }
155
+ };
156
+ // Add unregister method to the setter
157
+ setContext.unregister = () => {
158
+ const currentRegistry = getPageContextRegistry();
159
+ const index = currentRegistry.findIndex((reg) => reg.id === registration.id);
160
+ if (index !== -1) {
161
+ const newRegistry = currentRegistry.filter((_, i) => i !== index);
162
+ setPageContextRegistry(newRegistry);
163
+ // TODO: Remove this legacy event emission after old event-based approach is fully deprecated
164
+ // Emit legacy remove event for backward compatibility
165
+ window.dispatchEvent(new CustomEvent(LEGACY_REGISTRY_REMOVE_EVENT, {
166
+ detail: { id: registration.id },
167
+ }));
168
+ }
169
+ };
170
+ return setContext;
171
+ }
172
+ const empty = [];
173
+ /**
174
+ * React hook for providing page context that automatically cleans up on unmount.
175
+ * This is the recommended way to use page context in React components.
176
+ *
177
+ * @param urlPattern - URL pattern (string or RegExp) to match against page URLs
178
+ * @param initialContext - Initial array of ContextItems to provide when the pattern matches. IMPORTANT: will be overwritten when setContext is used to update the context.
179
+ * @returns A setter function to update the context
180
+ */
181
+ export function useProvidePageContext(urlPattern, initialContext = empty) {
182
+ const setContextRef = useRef(undefined);
183
+ const pendingUpdate = useRef(undefined);
184
+ const isModified = useRef(false);
185
+ // Create or update the registration when dependencies change
186
+ // biome-ignore lint/correctness/useExhaustiveDependencies: initialContext is intentionally excluded to prevent re-registering on every render; updates are handled by the second effect
187
+ useEffect(() => {
188
+ // Reset isModified flag when urlPattern changes so that initialContext updates
189
+ // are applied to the new registration until manually modified again
190
+ isModified.current = false;
191
+ // If we have pending updates, prefer those over initialContext as they represent
192
+ // newer state from child components
193
+ const contextToUse = pendingUpdate.current !== undefined ? pendingUpdate.current : initialContext;
194
+ setContextRef.current = providePageContext(urlPattern, contextToUse);
195
+ // Clear pending update as it's now applied
196
+ if (pendingUpdate.current !== undefined) {
197
+ pendingUpdate.current = undefined;
198
+ }
199
+ // Cleanup on unmount or dependency change
200
+ return () => {
201
+ var _a;
202
+ (_a = setContextRef.current) === null || _a === void 0 ? void 0 : _a.unregister();
203
+ setContextRef.current = undefined;
204
+ };
205
+ // We intentionally exclude initialContext from deps to prevent re-registering on every render
206
+ // Updates to initialContext are handled by the second effect
207
+ }, [urlPattern]);
208
+ // Update context when initialContext changes, BUT only if we haven't manually modified it.
209
+ // This prevents unstable initialContext props (e.g. passing [] inline) from overwriting
210
+ // updates made via setContext by child components.
211
+ useEffect(() => {
212
+ if (!isModified.current && setContextRef.current) {
213
+ setContextRef.current(initialContext);
214
+ }
215
+ }, [initialContext]);
216
+ // Return a stable setter function
217
+ const setContext = useCallback((newContext) => {
218
+ isModified.current = true;
219
+ if (setContextRef.current) {
220
+ setContextRef.current(newContext);
221
+ }
222
+ else {
223
+ pendingUpdate.current = newContext;
224
+ }
225
+ }, []);
226
+ return setContext;
227
+ }
228
+ /**
229
+ * Gets all context items that match the current URL.
230
+ *
231
+ * @param currentUrl - The current page URL to match against
232
+ * @param registrations - Array of registrations to match against
233
+ * @returns Array of ChatContextItem from all matching registrations
234
+ */
235
+ function getActivePageContext(currentUrl) {
236
+ if (!currentUrl) {
237
+ return [];
238
+ }
239
+ const activeContext = [];
240
+ const registry = getPageContextRegistry();
241
+ for (const registration of registry) {
242
+ if (matchesUrlPattern(currentUrl, registration.urlPattern)) {
243
+ activeContext.push(...registration.context);
244
+ }
245
+ }
246
+ return activeContext;
247
+ }
248
+ export function usePageContext(options = { allowQuestions: false, allowComponents: false }) {
249
+ const location = useLocationService();
250
+ const initialPathname = location.getLocation().pathname;
251
+ const [pageContext, setPageContext] = useState(() => getActivePageContext(initialPathname));
252
+ const currentPathnameRef = useRef(initialPathname);
253
+ useEffect(() => {
254
+ // Function to update context based on current location and registry
255
+ const updateContext = () => {
256
+ const pathname = location.getLocation().pathname;
257
+ const context = getActivePageContext(pathname);
258
+ setPageContext(context);
259
+ };
260
+ // Handle registry changes
261
+ const handleRegistryChange = () => {
262
+ updateContext();
263
+ };
264
+ // Subscribe to location changes
265
+ const locationSub = location.getLocationObservable().subscribe((loc) => {
266
+ const pathname = loc.pathname;
267
+ if (pathname !== currentPathnameRef.current) {
268
+ currentPathnameRef.current = pathname;
269
+ updateContext();
270
+ }
271
+ });
272
+ // Listen for registry changes
273
+ window.addEventListener(REGISTRY_CHANGE_EVENT, handleRegistryChange);
274
+ // TODO: Remove these legacy event listeners after old event-based approach is fully deprecated
275
+ // Listen for legacy events for backward compatibility
276
+ window.addEventListener(LEGACY_REGISTRY_UPDATE_EVENT, handleRegistryChange);
277
+ window.addEventListener(LEGACY_REGISTRY_REMOVE_EVENT, handleRegistryChange);
278
+ window.addEventListener(LEGACY_REGISTRY_SYNC_EVENT, handleRegistryChange);
279
+ // Initial update
280
+ updateContext();
281
+ return () => {
282
+ locationSub.unsubscribe();
283
+ window.removeEventListener(REGISTRY_CHANGE_EVENT, handleRegistryChange);
284
+ // TODO: Remove these legacy event listener removals after old event-based approach is fully deprecated
285
+ window.removeEventListener(LEGACY_REGISTRY_UPDATE_EVENT, handleRegistryChange);
286
+ window.removeEventListener(LEGACY_REGISTRY_REMOVE_EVENT, handleRegistryChange);
287
+ window.removeEventListener(LEGACY_REGISTRY_SYNC_EVENT, handleRegistryChange);
288
+ };
289
+ }, [location]);
290
+ let filteredContext = pageContext;
291
+ if (!options.allowQuestions) {
292
+ filteredContext = filteredContext.filter((item) => { var _a; return ((_a = item.node.data) === null || _a === void 0 ? void 0 : _a.type) !== 'question'; });
293
+ }
294
+ if (!options.allowComponents) {
295
+ filteredContext = filteredContext.filter((item) => { var _a; return ((_a = item.node.data) === null || _a === void 0 ? void 0 : _a.type) !== 'component'; });
296
+ }
297
+ return filteredContext;
298
+ }
299
+ export function generateRegistrationId(prefix = 'page-context') {
300
+ return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
301
+ }
302
+ /**
303
+ * Validates the URL pattern at registration time and logs warnings for problematic patterns.
304
+ * This is called once during registration, not on every URL match.
305
+ */
306
+ function validateUrlPattern(pattern) {
307
+ if (pattern instanceof RegExp) {
308
+ // Check if RegExp pattern matches everything (e.g., /.*/, /^.*$/)
309
+ // This is intentional for global contexts (e.g., components), so we use a scoped warning
310
+ const source = pattern.source;
311
+ if (source === '.*' || source === '^.*$' || source === '^.*' || source === '.*$') {
312
+ // Note: This is expected for global contexts like provideComponents.
313
+ // The warning is intentionally omitted here since it's a valid use case.
314
+ // If you want to warn for specific cases, filter them here.
315
+ }
316
+ return;
317
+ }
318
+ if (typeof pattern === 'string') {
319
+ // Validate urlPattern - log error if empty string is passed
320
+ if (!pattern || pattern.trim() === '') {
321
+ console.warn('useProvidePageContext: Empty string passed as urlPattern. This will cause the context to not match any pages.');
322
+ return;
323
+ }
324
+ // Check if the pattern matches everything (e.g., ** becomes ^.*$)
325
+ if (pattern === '**') {
326
+ // Note: This is expected for global contexts.
327
+ // The warning is intentionally omitted here since it's a valid use case.
328
+ }
329
+ }
330
+ }
331
+ function matchesUrlPattern(url, pattern) {
332
+ if (pattern instanceof RegExp) {
333
+ return pattern.test(url);
334
+ }
335
+ // For string patterns, support simple wildcards
336
+ if (typeof pattern === 'string') {
337
+ // Empty pattern never matches
338
+ if (!pattern || pattern.trim() === '') {
339
+ return false;
340
+ }
341
+ // Convert simple wildcard pattern to regex
342
+ // * matches any characters except /, ** matches any path segments
343
+ // Handle ** first with a placeholder to avoid conflicts
344
+ const regexPattern = pattern
345
+ .replace(/\*\*/g, '\u0000DOUBLE_STAR\u0000') // Temporary placeholder for **
346
+ .replace(/\*/g, '[^/]*') // * becomes [^/]*
347
+ // biome-ignore lint/suspicious/noControlCharactersInRegex: intentional \u0000 null byte used as placeholder to avoid glob pattern conflicts
348
+ .replace(/\u0000DOUBLE_STAR\u0000/g, '.*') // Replace placeholder with .*
349
+ .replace(/\?/g, '.'); // ? becomes .
350
+ const finalRegex = `^${regexPattern}$`;
351
+ const regex = new RegExp(finalRegex);
352
+ return regex.test(url);
353
+ }
354
+ return false;
355
+ }
356
+ /**
357
+ * Test utility function to clear all registrations from the registry.
358
+ * This should only be used in tests to ensure a clean slate between test runs.
359
+ *
360
+ * @internal This is for testing purposes only
361
+ */
362
+ export function __clearPageContextRegistryForTesting() {
363
+ setPageContextRegistry([]);
364
+ }
365
+ //# sourceMappingURL=page.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"page.js","sourceRoot":"","sources":["../../../src/context/page.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAEtD,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AAejE,sBAAsB;AACtB,MAAM,mBAAmB,GAAG,yCAAyC,CAAC;AAEtE,oCAAoC;AACpC,MAAM,qBAAqB,GAAG,iDAAiD,CAAC;AAEhF;;;;;;;GAOG;AACH,MAAM,4BAA4B,GAAG,uCAAuC,CAAC;AAC7E;;;GAGG;AACH,MAAM,4BAA4B,GAAG,uCAAuC,CAAC;AAC7E;;;GAGG;AACH,MAAM,0BAA0B,GAAG,qCAAqC,CAAC;AAEzE,0CAA0C;AAC1C,IAAI,CAAC,MAAM,CAAC,mBAAmB,CAAC,EAAE,CAAC;IACjC,MAAM,CAAC,mBAAmB,CAAC,GAAG,EAAE,CAAC;AACnC,CAAC;AAED,uCAAuC;AACvC,SAAS,sBAAsB;IAC7B,OAAO,MAAM,CAAC,mBA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;QACvC,MAAM,KAAK,GAAG,IAAI,MAAM,CAAC,UAAU,CAAC,CAAC;QACrC,OAAO,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACzB,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,oCAAoC;IAClD,sBAAsB,CAAC,EAAE,CAAC,CAAC;AAC7B,CAAC"}
@@ -0,0 +1,111 @@
1
+ import { useMemo } from 'react';
2
+ import { providePageContext, useProvidePageContext, usePageContext, generateRegistrationId } from './page';
3
+ /**
4
+ * Registers questions for specific pages based on URL patterns.
5
+ * Returns a setter function to update the questions dynamically, similar to useState.
6
+ *
7
+ * Multiple registrations with the same URL pattern are allowed and will all be merged
8
+ * when the pattern matches. Each call creates a new independent registration.
9
+ *
10
+ * @param urlPattern - URL pattern (string or RegExp) to match against page URLs
11
+ * @param initialQuestions - Initial array of questions to provide when the pattern matches
12
+ * @returns A setter function to update the questions, with an unregister method attached
13
+ */
14
+ export function provideQuestions(urlPattern, initialQuestions) {
15
+ // Generate a unique registration ID to prevent collisions when multiple registrations exist
16
+ const registrationId = generateRegistrationId('question');
17
+ // Convert questions to ChatContextItem format for the underlying system
18
+ const convertQuestionsToContext = (questions) => {
19
+ return questions.map((question, index) => ({
20
+ node: {
21
+ id: `question-${registrationId}-${index}`,
22
+ name: question.title || question.prompt,
23
+ navigable: false,
24
+ selectable: true,
25
+ icon: 'question-circle',
26
+ data: {
27
+ type: 'question',
28
+ prompt: question.prompt,
29
+ title: question.title,
30
+ context: question.context || [],
31
+ },
32
+ },
33
+ occurrences: [],
34
+ }));
35
+ };
36
+ // Use the existing providePageContext system
37
+ const setContext = providePageContext(urlPattern, convertQuestionsToContext(initialQuestions));
38
+ // Create a wrapper that converts between questions and context
39
+ const setQuestions = (newQuestions) => {
40
+ setContext(convertQuestionsToContext(newQuestions));
41
+ };
42
+ // Add unregister method
43
+ setQuestions.unregister = setContext.unregister;
44
+ return setQuestions;
45
+ }
46
+ /**
47
+ * React hook for providing questions that automatically cleans up on unmount.
48
+ * This is the recommended way to use questions in React components.
49
+ *
50
+ * @param urlPattern - URL pattern (string or RegExp) to match against page URLs
51
+ * @param initialQuestions - Initial array of questions to provide when the pattern matches
52
+ * @returns A setter function to update the questions
53
+ */
54
+ export function useProvideQuestions(urlPattern, initialQuestions = []) {
55
+ // Generate a stable unique registration ID per component instance to prevent collisions
56
+ const registrationId = useMemo(() => generateRegistrationId('question'), [] // Empty deps = generated once per component instance
57
+ );
58
+ // Convert questions to ChatContextItem format for the underlying system
59
+ const convertQuestionsToContext = (questions) => {
60
+ return questions.map((question, index) => {
61
+ var _a;
62
+ return ({
63
+ node: {
64
+ id: `question-${registrationId}-${index}`,
65
+ name: (_a = question.title) !== null && _a !== void 0 ? _a : question.prompt,
66
+ navigable: false,
67
+ selectable: true,
68
+ icon: 'question-circle',
69
+ data: {
70
+ type: 'question',
71
+ prompt: question.prompt,
72
+ title: question.title,
73
+ context: question.context || [],
74
+ },
75
+ },
76
+ occurrences: [],
77
+ });
78
+ });
79
+ };
80
+ // Use the existing useProvidePageContext system
81
+ const setContext = useProvidePageContext(urlPattern, convertQuestionsToContext(initialQuestions));
82
+ // Create a wrapper that converts between questions and context
83
+ const setQuestions = (newQuestions) => {
84
+ setContext(convertQuestionsToContext(newQuestions));
85
+ };
86
+ return setQuestions;
87
+ }
88
+ /**
89
+ * React hook to get all questions that match the current URL.
90
+ * This filters the page context to only return question-type items.
91
+ *
92
+ * @returns Array of questions from all matching registrations
93
+ */
94
+ export function useQuestions() {
95
+ const pageContext = usePageContext({ allowQuestions: true, allowComponents: false });
96
+ // Filter and convert context items to questions
97
+ return convertContextToQuestions(pageContext);
98
+ }
99
+ const convertContextToQuestions = (contextItems) => {
100
+ return contextItems
101
+ .filter((item) => { var _a; return ((_a = item.node.data) === null || _a === void 0 ? void 0 : _a.type) === 'question'; })
102
+ .map((item) => {
103
+ var _a, _b, _c;
104
+ return ({
105
+ prompt: ((_a = item.node.data) === null || _a === void 0 ? void 0 : _a.prompt) || item.node.name,
106
+ title: (_b = item.node.data) === null || _b === void 0 ? void 0 : _b.title,
107
+ context: ((_c = item.node.data) === null || _c === void 0 ? void 0 : _c.context) || [],
108
+ });
109
+ });
110
+ };
111
+ //# sourceMappingURL=questions.js.map