@sqaitech/visualizer 0.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.
Files changed (152) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +9 -0
  3. package/dist/es/assets/sqai-logo.mjs +2 -0
  4. package/dist/es/component/blackboard/index.css +33 -0
  5. package/dist/es/component/blackboard/index.mjs +278 -0
  6. package/dist/es/component/config-selector/index.mjs +104 -0
  7. package/dist/es/component/context-preview/index.mjs +38 -0
  8. package/dist/es/component/env-config/index.mjs +112 -0
  9. package/dist/es/component/env-config-reminder/index.css +22 -0
  10. package/dist/es/component/env-config-reminder/index.mjs +28 -0
  11. package/dist/es/component/form-field/index.mjs +163 -0
  12. package/dist/es/component/history-selector/index.css +135 -0
  13. package/dist/es/component/history-selector/index.mjs +170 -0
  14. package/dist/es/component/index.mjs +1 -0
  15. package/dist/es/component/logo/index.css +13 -0
  16. package/dist/es/component/logo/index.mjs +21 -0
  17. package/dist/es/component/misc/index.mjs +94 -0
  18. package/dist/es/component/nav-actions/index.mjs +32 -0
  19. package/dist/es/component/nav-actions/style.css +35 -0
  20. package/dist/es/component/player/index.css +185 -0
  21. package/dist/es/component/player/index.mjs +856 -0
  22. package/dist/es/component/playground/index.css +431 -0
  23. package/dist/es/component/playground/index.mjs +8 -0
  24. package/dist/es/component/playground/playground-demo-ui-context.json +290 -0
  25. package/dist/es/component/playground-result/index.css +34 -0
  26. package/dist/es/component/playground-result/index.mjs +62 -0
  27. package/dist/es/component/prompt-input/index.css +391 -0
  28. package/dist/es/component/prompt-input/index.mjs +730 -0
  29. package/dist/es/component/service-mode-control/index.mjs +105 -0
  30. package/dist/es/component/shiny-text/index.css +75 -0
  31. package/dist/es/component/shiny-text/index.mjs +15 -0
  32. package/dist/es/component/universal-playground/index.css +341 -0
  33. package/dist/es/component/universal-playground/index.mjs +302 -0
  34. package/dist/es/component/universal-playground/providers/context-provider.mjs +52 -0
  35. package/dist/es/component/universal-playground/providers/indexeddb-storage-provider.mjs +207 -0
  36. package/dist/es/component/universal-playground/providers/storage-provider.mjs +210 -0
  37. package/dist/es/hooks/usePlaygroundExecution.mjs +180 -0
  38. package/dist/es/hooks/usePlaygroundState.mjs +203 -0
  39. package/dist/es/hooks/useSafeOverrideAIConfig.mjs +24 -0
  40. package/dist/es/hooks/useServerValid.mjs +30 -0
  41. package/dist/es/icons/avatar.mjs +28 -0
  42. package/dist/es/icons/close.mjs +19 -0
  43. package/dist/es/icons/global-perspective.mjs +16 -0
  44. package/dist/es/icons/history.mjs +30 -0
  45. package/dist/es/icons/magnifying-glass.mjs +39 -0
  46. package/dist/es/icons/player-setting.mjs +26 -0
  47. package/dist/es/icons/setting.mjs +20 -0
  48. package/dist/es/icons/show-marker.mjs +16 -0
  49. package/dist/es/index.mjs +25 -0
  50. package/dist/es/static/image/sqai-logo.png +0 -0
  51. package/dist/es/store/history.mjs +89 -0
  52. package/dist/es/store/store.mjs +186 -0
  53. package/dist/es/types.mjs +70 -0
  54. package/dist/es/utils/color.mjs +35 -0
  55. package/dist/es/utils/constants.mjs +99 -0
  56. package/dist/es/utils/index.mjs +10 -0
  57. package/dist/es/utils/pixi-loader.mjs +16 -0
  58. package/dist/es/utils/playground-utils.mjs +67 -0
  59. package/dist/es/utils/replay-scripts.mjs +312 -0
  60. package/dist/lib/assets/sqai-logo.js +24 -0
  61. package/dist/lib/component/blackboard/index.css +33 -0
  62. package/dist/lib/component/blackboard/index.js +321 -0
  63. package/dist/lib/component/config-selector/index.js +148 -0
  64. package/dist/lib/component/context-preview/index.js +83 -0
  65. package/dist/lib/component/env-config/index.js +146 -0
  66. package/dist/lib/component/env-config-reminder/index.css +22 -0
  67. package/dist/lib/component/env-config-reminder/index.js +62 -0
  68. package/dist/lib/component/form-field/index.js +209 -0
  69. package/dist/lib/component/history-selector/index.css +135 -0
  70. package/dist/lib/component/history-selector/index.js +216 -0
  71. package/dist/lib/component/index.js +60 -0
  72. package/dist/lib/component/logo/index.css +13 -0
  73. package/dist/lib/component/logo/index.js +68 -0
  74. package/dist/lib/component/misc/index.js +150 -0
  75. package/dist/lib/component/nav-actions/index.js +66 -0
  76. package/dist/lib/component/nav-actions/style.css +35 -0
  77. package/dist/lib/component/player/index.css +185 -0
  78. package/dist/lib/component/player/index.js +902 -0
  79. package/dist/lib/component/playground/index.css +431 -0
  80. package/dist/lib/component/playground/index.js +113 -0
  81. package/dist/lib/component/playground/playground-demo-ui-context.json +290 -0
  82. package/dist/lib/component/playground-result/index.css +34 -0
  83. package/dist/lib/component/playground-result/index.js +106 -0
  84. package/dist/lib/component/prompt-input/index.css +391 -0
  85. package/dist/lib/component/prompt-input/index.js +774 -0
  86. package/dist/lib/component/service-mode-control/index.js +139 -0
  87. package/dist/lib/component/shiny-text/index.css +75 -0
  88. package/dist/lib/component/shiny-text/index.js +49 -0
  89. package/dist/lib/component/universal-playground/index.css +341 -0
  90. package/dist/lib/component/universal-playground/index.js +350 -0
  91. package/dist/lib/component/universal-playground/providers/context-provider.js +95 -0
  92. package/dist/lib/component/universal-playground/providers/indexeddb-storage-provider.js +247 -0
  93. package/dist/lib/component/universal-playground/providers/storage-provider.js +268 -0
  94. package/dist/lib/hooks/usePlaygroundExecution.js +214 -0
  95. package/dist/lib/hooks/usePlaygroundState.js +237 -0
  96. package/dist/lib/hooks/useSafeOverrideAIConfig.js +61 -0
  97. package/dist/lib/hooks/useServerValid.js +64 -0
  98. package/dist/lib/icons/avatar.js +62 -0
  99. package/dist/lib/icons/close.js +53 -0
  100. package/dist/lib/icons/global-perspective.js +50 -0
  101. package/dist/lib/icons/history.js +64 -0
  102. package/dist/lib/icons/magnifying-glass.js +73 -0
  103. package/dist/lib/icons/player-setting.js +60 -0
  104. package/dist/lib/icons/setting.js +54 -0
  105. package/dist/lib/icons/show-marker.js +50 -0
  106. package/dist/lib/index.js +187 -0
  107. package/dist/lib/static/image/sqai-logo.png +0 -0
  108. package/dist/lib/store/history.js +96 -0
  109. package/dist/lib/store/store.js +196 -0
  110. package/dist/lib/types.js +116 -0
  111. package/dist/lib/utils/color.js +75 -0
  112. package/dist/lib/utils/constants.js +154 -0
  113. package/dist/lib/utils/index.js +63 -0
  114. package/dist/lib/utils/pixi-loader.js +56 -0
  115. package/dist/lib/utils/playground-utils.js +110 -0
  116. package/dist/lib/utils/replay-scripts.js +355 -0
  117. package/dist/types/component/blackboard/index.d.ts +15 -0
  118. package/dist/types/component/config-selector/index.d.ts +9 -0
  119. package/dist/types/component/context-preview/index.d.ts +9 -0
  120. package/dist/types/component/env-config/index.d.ts +6 -0
  121. package/dist/types/component/env-config-reminder/index.d.ts +6 -0
  122. package/dist/types/component/form-field/index.d.ts +17 -0
  123. package/dist/types/component/history-selector/index.d.ts +10 -0
  124. package/dist/types/component/index.d.ts +1 -0
  125. package/dist/types/component/logo/index.d.ts +5 -0
  126. package/dist/types/component/misc/index.d.ts +6 -0
  127. package/dist/types/component/nav-actions/index.d.ts +10 -0
  128. package/dist/types/component/player/index.d.ts +13 -0
  129. package/dist/types/component/playground/index.d.ts +7 -0
  130. package/dist/types/component/playground-result/index.d.ts +20 -0
  131. package/dist/types/component/prompt-input/index.d.ts +22 -0
  132. package/dist/types/component/service-mode-control/index.d.ts +6 -0
  133. package/dist/types/component/shiny-text/index.d.ts +12 -0
  134. package/dist/types/component/universal-playground/index.d.ts +4 -0
  135. package/dist/types/component/universal-playground/providers/context-provider.d.ts +37 -0
  136. package/dist/types/component/universal-playground/providers/indexeddb-storage-provider.d.ts +71 -0
  137. package/dist/types/component/universal-playground/providers/storage-provider.d.ts +58 -0
  138. package/dist/types/hooks/usePlaygroundExecution.d.ts +10 -0
  139. package/dist/types/hooks/usePlaygroundState.d.ts +26 -0
  140. package/dist/types/hooks/useSafeOverrideAIConfig.d.ts +16 -0
  141. package/dist/types/hooks/useServerValid.d.ts +1 -0
  142. package/dist/types/index.d.ts +27 -0
  143. package/dist/types/store/history.d.ts +16 -0
  144. package/dist/types/store/store.d.ts +36 -0
  145. package/dist/types/types.d.ts +161 -0
  146. package/dist/types/utils/color.d.ts +4 -0
  147. package/dist/types/utils/constants.d.ts +74 -0
  148. package/dist/types/utils/index.d.ts +4 -0
  149. package/dist/types/utils/pixi-loader.d.ts +5 -0
  150. package/dist/types/utils/playground-utils.d.ts +6 -0
  151. package/dist/types/utils/replay-scripts.d.ts +34 -0
  152. package/package.json +85 -0
@@ -0,0 +1,302 @@
1
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
+ import icons, { ArrowDownOutlined, ClearOutlined, LoadingOutlined } from "@ant-design/icons";
3
+ import { Button, Form, List, Tooltip, Typography, message } from "antd";
4
+ import { useCallback, useEffect, useMemo, useState } from "react";
5
+ import { usePlaygroundExecution } from "../../hooks/usePlaygroundExecution.mjs";
6
+ import { usePlaygroundState } from "../../hooks/usePlaygroundState.mjs";
7
+ import { useEnvConfig } from "../../store/store.mjs";
8
+ import { ContextPreview } from "../context-preview/index.mjs";
9
+ import { EnvConfigReminder } from "../env-config-reminder/index.mjs";
10
+ import { PlaygroundResultView } from "../playground-result/index.mjs";
11
+ import "./index.css";
12
+ import avatar from "../../icons/avatar.mjs";
13
+ import { PromptInput } from "../prompt-input/index.mjs";
14
+ import { createStorageProvider, detectBestStorageType } from "./providers/storage-provider.mjs";
15
+ const { Text } = Typography;
16
+ function getSDKId(sdk) {
17
+ if (sdk.id && 'string' == typeof sdk.id) return `agent-${sdk.id}`;
18
+ return 'playground-default';
19
+ }
20
+ function ErrorMessage(param) {
21
+ let { error } = param;
22
+ if (!error) return null;
23
+ return /*#__PURE__*/ jsxs(Tooltip, {
24
+ title: /*#__PURE__*/ jsx("span", {
25
+ style: {
26
+ whiteSpace: 'pre-wrap',
27
+ wordBreak: 'break-all'
28
+ },
29
+ children: error
30
+ }),
31
+ overlayStyle: {
32
+ maxWidth: '100vw'
33
+ },
34
+ children: [
35
+ "Error: ",
36
+ error.split('\n')[0]
37
+ ]
38
+ });
39
+ }
40
+ function UniversalPlayground(param) {
41
+ let { playgroundSDK, storage, contextProvider, config: componentConfig = {}, branding = {}, className = '', dryMode = false, showContextPreview = true } = param;
42
+ const [form] = Form.useForm();
43
+ const { config } = useEnvConfig();
44
+ const [sdkReady, setSdkReady] = useState(false);
45
+ useEffect(()=>{
46
+ const initializeSDK = async ()=>{
47
+ if (playgroundSDK && 'function' == typeof playgroundSDK.checkStatus) try {
48
+ await playgroundSDK.checkStatus();
49
+ setSdkReady(true);
50
+ } catch (error) {
51
+ console.warn('Failed to initialize SDK, using default namespace:', error);
52
+ setSdkReady(true);
53
+ }
54
+ else setSdkReady(true);
55
+ };
56
+ initializeSDK();
57
+ }, [
58
+ playgroundSDK
59
+ ]);
60
+ const effectiveStorage = useMemo(()=>{
61
+ if (storage) return storage;
62
+ if (!sdkReady) return null;
63
+ const namespace = componentConfig.storageNamespace || getSDKId(playgroundSDK);
64
+ const bestStorageType = detectBestStorageType();
65
+ console.log(`Using ${bestStorageType} storage for namespace: ${namespace}`);
66
+ return createStorageProvider(bestStorageType, namespace);
67
+ }, [
68
+ storage,
69
+ sdkReady,
70
+ componentConfig.storageNamespace,
71
+ playgroundSDK
72
+ ]);
73
+ const { loading, setLoading, infoList, setInfoList, actionSpace, actionSpaceLoading, uiContextPreview, setUiContextPreview, showScrollToBottomButton, verticalMode, replayCounter, setReplayCounter, infoListRef, currentRunningIdRef, interruptedFlagRef, clearInfoList, handleScrollToBottom } = usePlaygroundState(playgroundSDK, effectiveStorage, contextProvider);
74
+ const { handleRun: executeAction, handleStop, canStop } = usePlaygroundExecution(playgroundSDK, effectiveStorage, actionSpace, loading, setLoading, infoList, setInfoList, replayCounter, setReplayCounter, verticalMode, currentRunningIdRef, interruptedFlagRef);
75
+ useEffect(()=>{
76
+ if ((null == playgroundSDK ? void 0 : playgroundSDK.overrideConfig) && config) playgroundSDK.overrideConfig(config).catch((error)=>{
77
+ console.error('Failed to override SDK config:', error);
78
+ });
79
+ }, [
80
+ playgroundSDK,
81
+ config
82
+ ]);
83
+ const handleFormRun = useCallback(async ()=>{
84
+ try {
85
+ const value = form.getFieldsValue();
86
+ await executeAction(value);
87
+ } catch (error) {
88
+ message.error((null == error ? void 0 : error.message) || 'Execution failed');
89
+ }
90
+ }, [
91
+ form,
92
+ executeAction
93
+ ]);
94
+ const configAlreadySet = Object.keys(config || {}).length >= 1;
95
+ const runButtonEnabled = componentConfig.serverMode || !dryMode && !actionSpaceLoading && configAlreadySet;
96
+ const selectedType = Form.useWatch('type', form);
97
+ const finalShowContextPreview = showContextPreview && false !== componentConfig.showContextPreview;
98
+ const layout = componentConfig.layout || 'vertical';
99
+ const showVersionInfo = false !== componentConfig.showVersionInfo;
100
+ return /*#__PURE__*/ jsx("div", {
101
+ className: `playground-container ${layout}-mode ${className}`.trim(),
102
+ children: /*#__PURE__*/ jsxs(Form, {
103
+ form: form,
104
+ onFinish: handleFormRun,
105
+ className: "command-form",
106
+ children: [
107
+ finalShowContextPreview && /*#__PURE__*/ jsx("div", {
108
+ className: "context-preview-section",
109
+ children: /*#__PURE__*/ jsx(ContextPreview, {
110
+ uiContextPreview: uiContextPreview,
111
+ setUiContextPreview: setUiContextPreview,
112
+ showContextPreview: finalShowContextPreview
113
+ })
114
+ }),
115
+ /*#__PURE__*/ jsxs("div", {
116
+ className: "middle-dialog-area",
117
+ children: [
118
+ infoList.length > 1 && /*#__PURE__*/ jsx("div", {
119
+ className: "clear-button-container",
120
+ children: /*#__PURE__*/ jsx(Button, {
121
+ size: "small",
122
+ icon: /*#__PURE__*/ jsx(ClearOutlined, {}),
123
+ onClick: clearInfoList,
124
+ type: "text",
125
+ className: "clear-button"
126
+ })
127
+ }),
128
+ /*#__PURE__*/ jsx("div", {
129
+ ref: infoListRef,
130
+ className: "info-list-container",
131
+ children: /*#__PURE__*/ jsx(List, {
132
+ itemLayout: "vertical",
133
+ dataSource: infoList,
134
+ renderItem: (item)=>{
135
+ var _item_result;
136
+ return /*#__PURE__*/ jsx(List.Item, {
137
+ className: "list-item",
138
+ children: 'user' === item.type ? /*#__PURE__*/ jsx("div", {
139
+ className: "user-message-container",
140
+ children: /*#__PURE__*/ jsx("div", {
141
+ className: "user-message-bubble",
142
+ children: item.content
143
+ })
144
+ }) : 'progress' === item.type ? /*#__PURE__*/ jsx("div", {
145
+ children: (()=>{
146
+ var _parts_, _item_result, _item_result1, _item_result2;
147
+ const parts = item.content.split(' - ');
148
+ const action = null == (_parts_ = parts[0]) ? void 0 : _parts_.trim();
149
+ const description = parts.slice(1).join(' - ').trim();
150
+ const currentIndex = infoList.findIndex((listItem)=>listItem.id === item.id);
151
+ const laterProgressExists = infoList.slice(currentIndex + 1).some((listItem)=>'progress' === listItem.type);
152
+ const isLatestProgress = !laterProgressExists;
153
+ const shouldShowLoading = loading && isLatestProgress;
154
+ return /*#__PURE__*/ jsxs(Fragment, {
155
+ children: [
156
+ action && /*#__PURE__*/ jsxs("span", {
157
+ className: "progress-action-item",
158
+ children: [
159
+ action,
160
+ /*#__PURE__*/ jsx("span", {
161
+ className: `progress-status-icon ${shouldShowLoading ? 'loading' : (null == (_item_result = item.result) ? void 0 : _item_result.error) ? 'error' : 'completed'}`,
162
+ children: shouldShowLoading ? /*#__PURE__*/ jsx(LoadingOutlined, {
163
+ spin: true
164
+ }) : (null == (_item_result1 = item.result) ? void 0 : _item_result1.error) ? "\u2717" : "\u2713"
165
+ })
166
+ ]
167
+ }),
168
+ description && /*#__PURE__*/ jsx("div", {
169
+ children: /*#__PURE__*/ jsx("span", {
170
+ className: "progress-description",
171
+ children: description
172
+ })
173
+ }),
174
+ (null == (_item_result2 = item.result) ? void 0 : _item_result2.error) && /*#__PURE__*/ jsx(ErrorMessage, {
175
+ error: item.result.error
176
+ })
177
+ ]
178
+ });
179
+ })()
180
+ }) : 'separator' === item.type ? /*#__PURE__*/ jsxs("div", {
181
+ className: "new-conversation-separator",
182
+ children: [
183
+ /*#__PURE__*/ jsx("div", {
184
+ className: "separator-line"
185
+ }),
186
+ /*#__PURE__*/ jsx("div", {
187
+ className: "separator-text-container",
188
+ children: /*#__PURE__*/ jsx(Text, {
189
+ type: "secondary",
190
+ className: "separator-text",
191
+ children: item.content
192
+ })
193
+ })
194
+ ]
195
+ }) : /*#__PURE__*/ jsxs("div", {
196
+ className: "system-message-container",
197
+ children: [
198
+ /*#__PURE__*/ jsxs("div", {
199
+ className: "system-message-header",
200
+ children: [
201
+ /*#__PURE__*/ jsx(icons, {
202
+ component: branding.icon || avatar,
203
+ style: {
204
+ fontSize: 20
205
+ }
206
+ }),
207
+ /*#__PURE__*/ jsx("span", {
208
+ className: "system-message-title",
209
+ children: branding.title || 'Playground'
210
+ })
211
+ ]
212
+ }),
213
+ (item.content || item.result) && /*#__PURE__*/ jsxs("div", {
214
+ className: "system-message-content",
215
+ children: [
216
+ 'result' === item.type && (null == (_item_result = item.result) ? void 0 : _item_result.error) && /*#__PURE__*/ jsxs("div", {
217
+ className: "error-message",
218
+ children: [
219
+ /*#__PURE__*/ jsx("div", {
220
+ className: "divider"
221
+ }),
222
+ /*#__PURE__*/ jsx(ErrorMessage, {
223
+ error: item.result.error
224
+ })
225
+ ]
226
+ }),
227
+ 'result' === item.type ? /*#__PURE__*/ jsx(PlaygroundResultView, {
228
+ result: item.result || null,
229
+ loading: item.loading || false,
230
+ serverValid: true,
231
+ serviceMode: 'Server',
232
+ replayScriptsInfo: item.replayScriptsInfo || null,
233
+ replayCounter: item.replayCounter || 0,
234
+ loadingProgressText: item.loadingProgressText || '',
235
+ verticalMode: item.verticalMode || false,
236
+ fitMode: "width"
237
+ }) : /*#__PURE__*/ jsxs(Fragment, {
238
+ children: [
239
+ /*#__PURE__*/ jsx("div", {
240
+ className: "system-message-text",
241
+ children: item.content
242
+ }),
243
+ item.loading && item.loadingProgressText && /*#__PURE__*/ jsx("div", {
244
+ className: "loading-progress-text",
245
+ children: /*#__PURE__*/ jsx("span", {
246
+ children: item.loadingProgressText
247
+ })
248
+ })
249
+ ]
250
+ })
251
+ ]
252
+ })
253
+ ]
254
+ })
255
+ }, item.id);
256
+ }
257
+ })
258
+ }),
259
+ showScrollToBottomButton && false !== componentConfig.enableScrollToBottom && /*#__PURE__*/ jsx(Button, {
260
+ className: "scroll-to-bottom-button",
261
+ type: "primary",
262
+ shape: "circle",
263
+ icon: /*#__PURE__*/ jsx(ArrowDownOutlined, {}),
264
+ onClick: handleScrollToBottom,
265
+ size: "large"
266
+ })
267
+ ]
268
+ }),
269
+ /*#__PURE__*/ jsxs("div", {
270
+ className: "bottom-input-section",
271
+ children: [
272
+ componentConfig.showEnvConfigReminder ? /*#__PURE__*/ jsx(EnvConfigReminder, {}) : null,
273
+ /*#__PURE__*/ jsx(PromptInput, {
274
+ runButtonEnabled: runButtonEnabled,
275
+ form: form,
276
+ serviceMode: 'Server',
277
+ selectedType: selectedType,
278
+ dryMode: dryMode,
279
+ stoppable: canStop,
280
+ loading: loading,
281
+ onRun: handleFormRun,
282
+ onStop: handleStop,
283
+ actionSpace: actionSpace
284
+ })
285
+ ]
286
+ }),
287
+ showVersionInfo && branding.version && /*#__PURE__*/ jsx("div", {
288
+ className: "version-info-section",
289
+ children: /*#__PURE__*/ jsxs("span", {
290
+ className: "version-text",
291
+ children: [
292
+ "Midscene.js version: ",
293
+ branding.version
294
+ ]
295
+ })
296
+ })
297
+ ]
298
+ })
299
+ });
300
+ }
301
+ const universal_playground = UniversalPlayground;
302
+ export { UniversalPlayground, universal_playground as default };
@@ -0,0 +1,52 @@
1
+ function _define_property(obj, key, value) {
2
+ if (key in obj) Object.defineProperty(obj, key, {
3
+ value: value,
4
+ enumerable: true,
5
+ configurable: true,
6
+ writable: true
7
+ });
8
+ else obj[key] = value;
9
+ return obj;
10
+ }
11
+ class BaseContextProvider {
12
+ async refreshContext() {
13
+ this.cachedContext = void 0;
14
+ return await this.getUIContext();
15
+ }
16
+ constructor(){
17
+ _define_property(this, "cachedContext", void 0);
18
+ }
19
+ }
20
+ class AgentContextProvider extends BaseContextProvider {
21
+ async getUIContext() {
22
+ if (this.cachedContext) return this.cachedContext;
23
+ const agent = this.getAgent();
24
+ if (!(null == agent ? void 0 : agent.getUIContext)) throw new Error('Agent does not support getUIContext');
25
+ const context = await agent.getUIContext();
26
+ this.cachedContext = context;
27
+ return context;
28
+ }
29
+ constructor(getAgent, options){
30
+ super(), _define_property(this, "getAgent", void 0), _define_property(this, "options", void 0), this.getAgent = getAgent, this.options = options;
31
+ }
32
+ }
33
+ class StaticContextProvider extends BaseContextProvider {
34
+ async getUIContext() {
35
+ return this.context;
36
+ }
37
+ async refreshContext() {
38
+ return this.context;
39
+ }
40
+ constructor(context){
41
+ super(), _define_property(this, "context", void 0), this.context = context;
42
+ }
43
+ }
44
+ class NoOpContextProvider {
45
+ async getUIContext() {
46
+ throw new Error('Context preview is disabled');
47
+ }
48
+ async refreshContext() {
49
+ throw new Error('Context preview is disabled');
50
+ }
51
+ }
52
+ export { AgentContextProvider, BaseContextProvider, NoOpContextProvider, StaticContextProvider };
@@ -0,0 +1,207 @@
1
+ import { IndexedDBManager, createCleanupFunction, withErrorHandling } from "@sqaitech/shared/baseDB";
2
+ function _define_property(obj, key, value) {
3
+ if (key in obj) Object.defineProperty(obj, key, {
4
+ value: value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
8
+ });
9
+ else obj[key] = value;
10
+ return obj;
11
+ }
12
+ const DB_NAME = 'midscene_playground';
13
+ const DB_VERSION = 1;
14
+ const MESSAGES_STORE = 'playground_messages';
15
+ const RESULTS_STORE = 'playground_results';
16
+ const MAX_STORED_MESSAGES = 100;
17
+ const MAX_STORED_RESULTS = 50;
18
+ class IndexedDBStorageProvider {
19
+ async saveMessages(messages) {
20
+ await withErrorHandling(async ()=>{
21
+ await this.dbManager.clear(MESSAGES_STORE);
22
+ const messagesToSave = messages.slice(-MAX_STORED_MESSAGES);
23
+ await Promise.all(messagesToSave.map((msg, index)=>{
24
+ const lightMessage = {
25
+ ...msg,
26
+ result: void 0
27
+ };
28
+ const data = {
29
+ id: msg.id || `msg-${index}`,
30
+ data: lightMessage,
31
+ timestamp: msg.timestamp ? msg.timestamp.getTime() : Date.now() + index
32
+ };
33
+ return this.dbManager.put(MESSAGES_STORE, data);
34
+ }));
35
+ }, 'Failed to save messages to IndexedDB', void 0, this.messagesCleanup);
36
+ }
37
+ async loadMessages() {
38
+ const result = await withErrorHandling(async ()=>{
39
+ const messages = await this.dbManager.getAll(MESSAGES_STORE, true);
40
+ if (0 === messages.length) return [];
41
+ return Promise.all(messages.map(async (msg)=>{
42
+ const item = msg.data;
43
+ const restoredItem = {
44
+ ...item,
45
+ timestamp: new Date(item.timestamp)
46
+ };
47
+ if ('result' === item.type && item.id) {
48
+ const fullResult = await this.loadResult(item.id);
49
+ if (fullResult) {
50
+ restoredItem.result = fullResult.result;
51
+ restoredItem.replayScriptsInfo = fullResult.replayScriptsInfo;
52
+ restoredItem.replayCounter = fullResult.replayCounter;
53
+ restoredItem.verticalMode = fullResult.verticalMode;
54
+ }
55
+ }
56
+ return restoredItem;
57
+ }));
58
+ }, 'Failed to load messages from IndexedDB', [], this.messagesCleanup);
59
+ return result || [];
60
+ }
61
+ async clearMessages() {
62
+ await withErrorHandling(async ()=>{
63
+ await Promise.all([
64
+ this.dbManager.clear(MESSAGES_STORE),
65
+ this.dbManager.clear(RESULTS_STORE)
66
+ ]);
67
+ }, 'Failed to clear messages from IndexedDB');
68
+ }
69
+ async saveResult(id, result) {
70
+ await withErrorHandling(async ()=>{
71
+ const compressedResult = this.compressResultForStorage(result);
72
+ const data = {
73
+ id,
74
+ data: compressedResult,
75
+ timestamp: Date.now(),
76
+ size: JSON.stringify(compressedResult).length
77
+ };
78
+ await this.dbManager.put(RESULTS_STORE, data);
79
+ }, 'Failed to save result to IndexedDB', void 0, this.resultsCleanup);
80
+ }
81
+ async loadResult(id) {
82
+ const result = await withErrorHandling(async ()=>{
83
+ const data = await this.dbManager.get(RESULTS_STORE, id);
84
+ return (null == data ? void 0 : data.data) || null;
85
+ }, 'Failed to load result from IndexedDB', null);
86
+ return result || null;
87
+ }
88
+ compressResultForStorage(result) {
89
+ var _result_result_dump, _result_result;
90
+ if (!(null == (_result_result = result.result) ? void 0 : null == (_result_result_dump = _result_result.dump) ? void 0 : _result_result_dump.executions)) return result;
91
+ const compressedExecutions = result.result.dump.executions.map((execution)=>{
92
+ var _execution_tasks;
93
+ return {
94
+ ...execution,
95
+ tasks: (null == (_execution_tasks = execution.tasks) ? void 0 : _execution_tasks.map((task)=>{
96
+ var _task_recorder;
97
+ var _this_compressScreenshotIfNeeded;
98
+ return {
99
+ ...task,
100
+ uiContext: task.uiContext ? {
101
+ ...task.uiContext,
102
+ screenshotBase64: null != (_this_compressScreenshotIfNeeded = this.compressScreenshotIfNeeded(task.uiContext.screenshotBase64)) ? _this_compressScreenshotIfNeeded : task.uiContext.screenshotBase64
103
+ } : task.uiContext,
104
+ recorder: null == (_task_recorder = task.recorder) ? void 0 : _task_recorder.map((record)=>({
105
+ ...record,
106
+ screenshot: this.compressScreenshotIfNeeded(record.screenshot)
107
+ }))
108
+ };
109
+ })) || []
110
+ };
111
+ });
112
+ return {
113
+ ...result,
114
+ result: {
115
+ ...result.result,
116
+ dump: {
117
+ ...result.result.dump,
118
+ executions: compressedExecutions
119
+ }
120
+ }
121
+ };
122
+ }
123
+ compressScreenshotIfNeeded(screenshot) {
124
+ if (!screenshot) return screenshot;
125
+ if (screenshot.length > 1048576) {
126
+ const sizeKB = Math.round(screenshot.length / 1024);
127
+ return `[COMPRESSED: ${sizeKB}KB screenshot removed for storage]`;
128
+ }
129
+ return screenshot;
130
+ }
131
+ async getStorageStats() {
132
+ const result = await withErrorHandling(async ()=>{
133
+ const [messageCount, resultCount] = await Promise.all([
134
+ this.dbManager.count(MESSAGES_STORE),
135
+ this.dbManager.count(RESULTS_STORE)
136
+ ]);
137
+ return {
138
+ messageCount,
139
+ resultCount
140
+ };
141
+ }, 'Failed to get storage statistics', {
142
+ messageCount: 0,
143
+ resultCount: 0
144
+ });
145
+ return result || {
146
+ messageCount: 0,
147
+ resultCount: 0
148
+ };
149
+ }
150
+ async cleanup() {
151
+ await Promise.all([
152
+ this.messagesCleanup(),
153
+ this.resultsCleanup()
154
+ ]);
155
+ }
156
+ constructor(namespace = 'playground'){
157
+ _define_property(this, "dbManager", void 0);
158
+ _define_property(this, "namespace", void 0);
159
+ _define_property(this, "messagesCleanup", void 0);
160
+ _define_property(this, "resultsCleanup", void 0);
161
+ this.namespace = namespace;
162
+ this.dbManager = new IndexedDBManager(`${DB_NAME}_${namespace}`, DB_VERSION, [
163
+ {
164
+ name: MESSAGES_STORE,
165
+ keyPath: 'id'
166
+ },
167
+ {
168
+ name: RESULTS_STORE,
169
+ keyPath: 'id'
170
+ }
171
+ ]);
172
+ this.messagesCleanup = createCleanupFunction(this.dbManager, MESSAGES_STORE, MAX_STORED_MESSAGES);
173
+ this.resultsCleanup = createCleanupFunction(this.dbManager, RESULTS_STORE, MAX_STORED_RESULTS);
174
+ }
175
+ }
176
+ class MemoryStorageProvider {
177
+ async saveMessages(messages) {
178
+ this.messages = [
179
+ ...messages
180
+ ];
181
+ }
182
+ async loadMessages() {
183
+ return [
184
+ ...this.messages
185
+ ];
186
+ }
187
+ async clearMessages() {
188
+ this.messages = [];
189
+ this.results.clear();
190
+ }
191
+ async saveResult(id, result) {
192
+ this.results.set(id, result);
193
+ }
194
+ constructor(){
195
+ _define_property(this, "messages", []);
196
+ _define_property(this, "results", new Map());
197
+ }
198
+ }
199
+ class NoOpStorageProvider {
200
+ async saveMessages(_messages) {}
201
+ async loadMessages() {
202
+ return [];
203
+ }
204
+ async clearMessages() {}
205
+ async saveResult(_id, _result) {}
206
+ }
207
+ export { IndexedDBStorageProvider, MemoryStorageProvider, NoOpStorageProvider };