@autobe/ui 0.29.2 → 0.30.0-dev.20260315
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -661
- package/README.md +261 -0
- package/lib/components/AutoBeChatMain.js +5 -5
- package/lib/components/AutoBeChatMain.js.map +1 -1
- package/lib/components/AutoBeConfigModal.js +9 -9
- package/lib/components/AutoBeStatusModal.js +4 -4
- package/lib/components/AutoBeStatusModal.js.map +1 -1
- package/lib/components/AutoBeUserMessageMovie.d.ts +2 -2
- package/lib/components/common/ChatBubble.d.ts +2 -2
- package/lib/components/common/openai/OpenAIContent.d.ts +2 -2
- package/lib/components/common/openai/OpenAIContent.js.map +1 -1
- package/lib/components/common/openai/OpenAIUserAudioContent.js +1 -1
- package/lib/components/common/openai/OpenAIUserAudioContent.js.map +1 -1
- package/lib/components/common/openai/OpenAIUserFileContent.js +1 -1
- package/lib/components/common/openai/OpenAIUserFileContent.js.map +1 -1
- package/lib/components/common/openai/OpenAIUserImageContent.d.ts +2 -2
- package/lib/components/events/AutoBeCompleteEventMovie.d.ts +2 -2
- package/lib/components/events/AutoBeCompleteEventMovie.js +5 -5
- package/lib/components/events/AutoBeCompleteEventMovie.js.map +1 -1
- package/lib/components/events/AutoBeCorrectEventMovie.d.ts +2 -2
- package/lib/components/events/AutoBeCorrectEventMovie.js +4 -4
- package/lib/components/events/AutoBeCorrectEventMovie.js.map +1 -1
- package/lib/components/events/AutoBeEventMovie.js +38 -17
- package/lib/components/events/AutoBeEventMovie.js.map +1 -1
- package/lib/components/events/AutoBeProgressEventMovie.js +73 -13
- package/lib/components/events/AutoBeProgressEventMovie.js.map +1 -1
- package/lib/components/events/AutoBeScenarioEventMovie.d.ts +2 -2
- package/lib/components/events/AutoBeScenarioEventMovie.js +18 -5
- package/lib/components/events/AutoBeScenarioEventMovie.js.map +1 -1
- package/lib/components/events/AutoBeStartEventMovie.d.ts +2 -2
- package/lib/components/events/AutoBeStartEventMovie.js +2 -2
- package/lib/components/events/AutoBeStartEventMovie.js.map +1 -1
- package/lib/components/events/AutoBeValidateEventMovie.d.ts +2 -2
- package/lib/components/events/AutoBeValidateEventMovie.js +3 -11
- package/lib/components/events/AutoBeValidateEventMovie.js.map +1 -1
- package/lib/components/events/groups/CorrectEventGroup.d.ts +2 -2
- package/lib/components/events/groups/CorrectEventGroup.js +1 -1
- package/lib/components/events/groups/CorrectEventGroup.js.map +1 -1
- package/lib/components/events/groups/ValidateEventGroup.d.ts +2 -2
- package/lib/components/events/groups/ValidateEventGroup.js +1 -2
- package/lib/components/events/groups/ValidateEventGroup.js.map +1 -1
- package/lib/components/events/utils/eventGrouper.js +1 -2
- package/lib/components/events/utils/eventGrouper.js.map +1 -1
- package/lib/components/upload/AutoBeChatUploadBox.d.ts +3 -4
- package/lib/components/upload/AutoBeChatUploadBox.js +2 -1
- package/lib/components/upload/AutoBeChatUploadBox.js.map +1 -1
- package/lib/components/upload/AutoBeChatUploadSendButton.js +1 -1
- package/lib/components/upload/AutoBeChatUploadSendButton.js.map +1 -1
- package/lib/context/AutoBeAgentContext.d.ts +1 -3
- package/lib/context/AutoBeAgentContext.js +0 -4
- package/lib/context/AutoBeAgentContext.js.map +1 -1
- package/lib/hooks/useSessionStorage.d.ts +4 -0
- package/lib/hooks/useSessionStorage.js +16 -0
- package/lib/hooks/useSessionStorage.js.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/strategy/AutoBeAgentSessionStorageStrategy.d.ts +10 -0
- package/lib/strategy/AutoBeAgentSessionStorageStrategy.js +117 -0
- package/lib/strategy/AutoBeAgentSessionStorageStrategy.js.map +1 -0
- package/lib/structure/AutoBeListener.js +91 -23
- package/lib/structure/AutoBeListener.js.map +1 -1
- package/lib/structure/AutoBeListenerState.d.ts +3 -3
- package/lib/structure/AutoBeListenerState.js +4 -4
- package/lib/structure/AutoBeListenerState.js.map +1 -1
- package/lib/structure/IAutoBeAgentSessionStorageStrategy.js +1 -1
- package/lib/structure/IAutoBeAgentSessionStorageStrategy.js.map +1 -1
- package/lib/utils/AutoBeFileUploader.d.ts +2 -2
- package/lib/utils/AutoBeFileUploader.js.map +1 -1
- package/package.json +3 -4
- package/src/components/AutoBeAssistantMessageMovie.tsx +22 -22
- package/src/components/AutoBeChatMain.tsx +376 -376
- package/src/components/AutoBeChatSidebar.tsx +414 -414
- package/src/components/AutoBeConfigButton.tsx +83 -83
- package/src/components/AutoBeConfigModal.tsx +443 -443
- package/src/components/AutoBeStatusButton.tsx +75 -75
- package/src/components/AutoBeStatusModal.tsx +486 -484
- package/src/components/AutoBeUserMessageMovie.tsx +27 -27
- package/src/components/common/ActionButton.tsx +205 -205
- package/src/components/common/ActionButtonGroup.tsx +80 -80
- package/src/components/common/AutoBeConfigInput.tsx +185 -185
- package/src/components/common/ChatBubble.tsx +119 -119
- package/src/components/common/Collapsible.tsx +95 -95
- package/src/components/common/CompactSessionIndicator.tsx +73 -73
- package/src/components/common/CompactSessionList.tsx +82 -82
- package/src/components/common/index.ts +8 -8
- package/src/components/common/openai/OpenAIContent.tsx +53 -53
- package/src/components/common/openai/OpenAIUserAudioContent.tsx +70 -70
- package/src/components/common/openai/OpenAIUserFileContent.tsx +76 -76
- package/src/components/common/openai/OpenAIUserImageContent.tsx +34 -34
- package/src/components/common/openai/OpenAIUserTextContent.tsx +15 -15
- package/src/components/common/openai/index.ts +5 -5
- package/src/components/events/AutoBeCompleteEventMovie.tsx +402 -402
- package/src/components/events/AutoBeCorrectEventMovie.tsx +354 -368
- package/src/components/events/AutoBeEventGroupMovie.tsx +18 -18
- package/src/components/events/AutoBeEventMovie.tsx +158 -139
- package/src/components/events/AutoBeProgressEventMovie.tsx +217 -157
- package/src/components/events/AutoBeScenarioEventMovie.tsx +135 -95
- package/src/components/events/AutoBeStartEventMovie.tsx +82 -82
- package/src/components/events/AutoBeValidateEventMovie.tsx +249 -286
- package/src/components/events/README.md +300 -300
- package/src/components/events/common/CollapsibleEventGroup.tsx +211 -211
- package/src/components/events/common/EventCard.tsx +61 -61
- package/src/components/events/common/EventContent.tsx +31 -31
- package/src/components/events/common/EventHeader.tsx +85 -85
- package/src/components/events/common/EventIcon.tsx +82 -82
- package/src/components/events/common/ProgressBar.tsx +64 -64
- package/src/components/events/common/index.ts +13 -13
- package/src/components/events/groups/CorrectEventGroup.tsx +183 -183
- package/src/components/events/groups/ValidateEventGroup.tsx +143 -146
- package/src/components/events/groups/index.ts +8 -8
- package/src/components/events/index.ts +16 -16
- package/src/components/events/utils/eventGrouper.tsx +116 -117
- package/src/components/events/utils/index.ts +1 -1
- package/src/components/index.ts +13 -13
- package/src/components/upload/AutoBeChatUploadBox.tsx +425 -424
- package/src/components/upload/AutoBeChatUploadSendButton.tsx +66 -66
- package/src/components/upload/AutoBeFileUploadBox.tsx +123 -123
- package/src/components/upload/AutoBeUploadConfig.ts +5 -5
- package/src/components/upload/AutoBeVoiceRecoderButton.tsx +100 -100
- package/src/components/upload/index.ts +5 -5
- package/src/constant/color.ts +28 -28
- package/src/context/AutoBeAgentContext.tsx +245 -258
- package/src/context/AutoBeAgentSessionList.tsx +58 -58
- package/src/context/SearchParamsContext.tsx +49 -49
- package/src/hooks/index.ts +3 -3
- package/src/hooks/useEscapeKey.ts +24 -24
- package/src/hooks/useIsomorphicLayoutEffect.ts +8 -8
- package/src/hooks/useMediaQuery.ts +73 -73
- package/src/hooks/useSessionStorage.ts +10 -0
- package/src/icons/Receipt.tsx +74 -74
- package/src/index.ts +9 -8
- package/src/strategy/AutoBeAgentSessionStorageStrategy.ts +127 -0
- package/src/structure/AutoBeListener.ts +373 -304
- package/src/structure/AutoBeListenerState.ts +53 -53
- package/src/structure/IAutoBeAgentSessionStorageStrategy.ts +87 -87
- package/src/structure/IAutoBeEventGroup.ts +6 -6
- package/src/structure/index.ts +4 -4
- package/src/types/config.ts +44 -44
- package/src/types/index.ts +1 -1
- package/src/utils/AutoBeFileUploader.ts +279 -279
- package/src/utils/AutoBeVoiceRecorder.ts +95 -95
- package/src/utils/__tests__/crypto.test.ts +286 -286
- package/src/utils/__tests__/storage.test.ts +229 -229
- package/src/utils/crypto.ts +95 -95
- package/src/utils/index.ts +6 -6
- package/src/utils/number.ts +17 -17
- package/src/utils/storage.ts +96 -96
- package/src/utils/time.ts +14 -14
- package/tsconfig.json +9 -9
- package/vitest.config.ts +15 -15
|
@@ -1,258 +1,245 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
})
|
|
70
|
-
//
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const [
|
|
80
|
-
null
|
|
81
|
-
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
sessionId
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
[
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
setTokenUsage
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
.
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
>
|
|
247
|
-
{children}
|
|
248
|
-
</AutoBeAgentContext.Provider>
|
|
249
|
-
);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
export function useAutoBeAgent() {
|
|
253
|
-
const context = useContext(AutoBeAgentContext);
|
|
254
|
-
if (!context) {
|
|
255
|
-
throw new Error("useAutoBeAgent must be used within a AutoBeAgentProvider");
|
|
256
|
-
}
|
|
257
|
-
return context;
|
|
258
|
-
}
|
|
1
|
+
import { IAutoBeRpcService, IAutoBeTokenUsageJson } from "@autobe/interface";
|
|
2
|
+
import {
|
|
3
|
+
ReactNode,
|
|
4
|
+
createContext,
|
|
5
|
+
useCallback,
|
|
6
|
+
useContext,
|
|
7
|
+
useEffect,
|
|
8
|
+
useState,
|
|
9
|
+
} from "react";
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
AutoBeListener,
|
|
13
|
+
AutoBeListenerState,
|
|
14
|
+
IAutoBeAgentSessionStorageStrategy,
|
|
15
|
+
IAutoBeEventGroup,
|
|
16
|
+
} from "../structure";
|
|
17
|
+
import { IAutoBeConfig } from "../types/config";
|
|
18
|
+
import { useAutoBeAgentSessionList } from "./AutoBeAgentSessionList";
|
|
19
|
+
import { useSearchParams } from "./SearchParamsContext";
|
|
20
|
+
|
|
21
|
+
export interface IAutoBeServiceData {
|
|
22
|
+
service: IAutoBeRpcService;
|
|
23
|
+
listener: AutoBeListener;
|
|
24
|
+
close: () => void | Promise<void>;
|
|
25
|
+
sessionId: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type AutoBeServiceFactory = (
|
|
29
|
+
config: IAutoBeConfig,
|
|
30
|
+
) => Promise<IAutoBeServiceData>;
|
|
31
|
+
|
|
32
|
+
export type AutoBeConnectionStatus =
|
|
33
|
+
| "disconnected"
|
|
34
|
+
| "connecting"
|
|
35
|
+
| "connected";
|
|
36
|
+
|
|
37
|
+
interface AutoBeAgentContextType {
|
|
38
|
+
// Service state
|
|
39
|
+
connectionStatus: AutoBeConnectionStatus;
|
|
40
|
+
|
|
41
|
+
// Service data (available when ready)
|
|
42
|
+
eventGroups: IAutoBeEventGroup[];
|
|
43
|
+
tokenUsage: IAutoBeTokenUsageJson | null;
|
|
44
|
+
state: AutoBeListenerState | null;
|
|
45
|
+
service: IAutoBeRpcService | null;
|
|
46
|
+
listener: AutoBeListener | null;
|
|
47
|
+
|
|
48
|
+
// Service management
|
|
49
|
+
getAutoBeService: (config?: IAutoBeConfig) => Promise<IAutoBeServiceData>;
|
|
50
|
+
resetService: () => void;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const AutoBeAgentContext = createContext<AutoBeAgentContextType | null>(null);
|
|
54
|
+
|
|
55
|
+
export function AutoBeAgentProvider({
|
|
56
|
+
children,
|
|
57
|
+
serviceFactory,
|
|
58
|
+
storageStrategy,
|
|
59
|
+
}: {
|
|
60
|
+
serviceFactory: AutoBeServiceFactory;
|
|
61
|
+
children: ReactNode;
|
|
62
|
+
storageStrategy: IAutoBeAgentSessionStorageStrategy;
|
|
63
|
+
}) {
|
|
64
|
+
// Service state
|
|
65
|
+
const [connectionStatus, setConnectionStatus] =
|
|
66
|
+
useState<AutoBeConnectionStatus>("disconnected");
|
|
67
|
+
|
|
68
|
+
// Service data
|
|
69
|
+
const { searchParams, setSearchParams } = useSearchParams();
|
|
70
|
+
// Use URL parameter for conversation ID - enables bookmark/share support
|
|
71
|
+
const activeConversationId = searchParams.get("session-id") ?? null;
|
|
72
|
+
|
|
73
|
+
const [tokenUsage, setTokenUsage] = useState<IAutoBeTokenUsageJson | null>(
|
|
74
|
+
null,
|
|
75
|
+
);
|
|
76
|
+
const [eventGroups, setEventGroups] = useState<IAutoBeEventGroup[]>([]);
|
|
77
|
+
|
|
78
|
+
// Context-scoped service instance (contains service, listener, header)
|
|
79
|
+
const [serviceInstance, setServiceInstance] =
|
|
80
|
+
useState<IAutoBeServiceData | null>(null);
|
|
81
|
+
|
|
82
|
+
const { refreshSessionList } = useAutoBeAgentSessionList();
|
|
83
|
+
// Context-scoped service getter
|
|
84
|
+
const getAutoBeService = useCallback(
|
|
85
|
+
async (
|
|
86
|
+
config: IAutoBeConfig = {} as IAutoBeConfig,
|
|
87
|
+
): Promise<IAutoBeServiceData> => {
|
|
88
|
+
// Return existing instance if available
|
|
89
|
+
if (serviceInstance && connectionStatus === "connected") {
|
|
90
|
+
return serviceInstance;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Prevent multiple concurrent creations
|
|
94
|
+
if (connectionStatus === "connecting") {
|
|
95
|
+
throw new Error("Service is already connecting. Please wait.");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (!serviceFactory) {
|
|
99
|
+
throw new Error("No service factory provided. Cannot create service.");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
setConnectionStatus("connecting");
|
|
104
|
+
|
|
105
|
+
// Create new service instance
|
|
106
|
+
const newServiceData = await serviceFactory({
|
|
107
|
+
...config,
|
|
108
|
+
sessionId: activeConversationId,
|
|
109
|
+
});
|
|
110
|
+
setServiceInstance(newServiceData);
|
|
111
|
+
|
|
112
|
+
setSearchParams((sp) => {
|
|
113
|
+
const newSp = new URLSearchParams(sp);
|
|
114
|
+
newSp.set("session-id", newServiceData.sessionId);
|
|
115
|
+
return newSp;
|
|
116
|
+
});
|
|
117
|
+
setConnectionStatus("connected");
|
|
118
|
+
|
|
119
|
+
return newServiceData;
|
|
120
|
+
} catch (error) {
|
|
121
|
+
setConnectionStatus("disconnected");
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
[
|
|
126
|
+
serviceFactory,
|
|
127
|
+
serviceInstance,
|
|
128
|
+
connectionStatus,
|
|
129
|
+
activeConversationId,
|
|
130
|
+
searchParams,
|
|
131
|
+
],
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
// Reset service (for reconnection, etc.)
|
|
135
|
+
const resetService = useCallback(() => {
|
|
136
|
+
setServiceInstance(null);
|
|
137
|
+
setConnectionStatus("disconnected");
|
|
138
|
+
setEventGroups([]);
|
|
139
|
+
setTokenUsage(null);
|
|
140
|
+
}, []);
|
|
141
|
+
|
|
142
|
+
useEffect(() => {
|
|
143
|
+
if (activeConversationId === null) {
|
|
144
|
+
setEventGroups([]);
|
|
145
|
+
setTokenUsage(null);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
storageStrategy
|
|
150
|
+
.getSession({
|
|
151
|
+
id: activeConversationId,
|
|
152
|
+
})
|
|
153
|
+
.then((v) => {
|
|
154
|
+
if (v === null) {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
refreshSessionList();
|
|
158
|
+
setEventGroups(v.events);
|
|
159
|
+
setTokenUsage(v.tokenUsage);
|
|
160
|
+
})
|
|
161
|
+
.catch(console.error);
|
|
162
|
+
}, [activeConversationId]);
|
|
163
|
+
|
|
164
|
+
useEffect(() => {
|
|
165
|
+
if (serviceInstance === null) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
serviceInstance.listener.on(async (e) => {
|
|
170
|
+
serviceInstance.service
|
|
171
|
+
.getTokenUsage()
|
|
172
|
+
.then(setTokenUsage)
|
|
173
|
+
.catch(() => {});
|
|
174
|
+
setEventGroups(e);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
serviceInstance.service
|
|
178
|
+
.getTokenUsage()
|
|
179
|
+
.then(setTokenUsage)
|
|
180
|
+
.catch(() => {});
|
|
181
|
+
}, [serviceInstance]);
|
|
182
|
+
|
|
183
|
+
useEffect(() => {
|
|
184
|
+
if (activeConversationId === null || serviceInstance === null) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const originConversate = serviceInstance.service.conversate;
|
|
189
|
+
serviceInstance.service.conversate = async (content) => {
|
|
190
|
+
const result = await originConversate(content);
|
|
191
|
+
await storageStrategy.appendHistory({
|
|
192
|
+
id: activeConversationId,
|
|
193
|
+
history: result,
|
|
194
|
+
});
|
|
195
|
+
return result;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const registerEvent = async (e: IAutoBeEventGroup[]) => {
|
|
199
|
+
await storageStrategy.appendEvent({
|
|
200
|
+
id: activeConversationId,
|
|
201
|
+
events: e,
|
|
202
|
+
});
|
|
203
|
+
await storageStrategy.setTokenUsage({
|
|
204
|
+
id: activeConversationId,
|
|
205
|
+
tokenUsage: await serviceInstance.service.getTokenUsage(),
|
|
206
|
+
});
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
serviceInstance.listener.on(registerEvent);
|
|
210
|
+
return () => {
|
|
211
|
+
serviceInstance.service.conversate = originConversate;
|
|
212
|
+
serviceInstance.listener.off(registerEvent);
|
|
213
|
+
};
|
|
214
|
+
}, [activeConversationId, serviceInstance]);
|
|
215
|
+
|
|
216
|
+
return (
|
|
217
|
+
<AutoBeAgentContext.Provider
|
|
218
|
+
value={{
|
|
219
|
+
// Service state
|
|
220
|
+
connectionStatus,
|
|
221
|
+
|
|
222
|
+
// Service data
|
|
223
|
+
eventGroups,
|
|
224
|
+
tokenUsage,
|
|
225
|
+
state: serviceInstance?.listener?.getState() ?? null,
|
|
226
|
+
service: serviceInstance?.service ?? null,
|
|
227
|
+
listener: serviceInstance?.listener ?? null,
|
|
228
|
+
|
|
229
|
+
// Service management
|
|
230
|
+
getAutoBeService,
|
|
231
|
+
resetService,
|
|
232
|
+
}}
|
|
233
|
+
>
|
|
234
|
+
{children}
|
|
235
|
+
</AutoBeAgentContext.Provider>
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export function useAutoBeAgent() {
|
|
240
|
+
const context = useContext(AutoBeAgentContext);
|
|
241
|
+
if (!context) {
|
|
242
|
+
throw new Error("useAutoBeAgent must be used within a AutoBeAgentProvider");
|
|
243
|
+
}
|
|
244
|
+
return context;
|
|
245
|
+
}
|
|
@@ -1,58 +1,58 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ReactNode,
|
|
3
|
-
createContext,
|
|
4
|
-
useCallback,
|
|
5
|
-
useContext,
|
|
6
|
-
useEffect,
|
|
7
|
-
useState,
|
|
8
|
-
} from "react";
|
|
9
|
-
|
|
10
|
-
import {
|
|
11
|
-
IAutoBeAgentSession,
|
|
12
|
-
IAutoBeAgentSessionStorageStrategy,
|
|
13
|
-
} from "../structure";
|
|
14
|
-
|
|
15
|
-
interface AutoBeAgentSessionListContextType {
|
|
16
|
-
sessionList: IAutoBeAgentSession[];
|
|
17
|
-
refreshSessionList: () => Promise<void>;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const AutoBeAgentSessionListContext =
|
|
21
|
-
createContext<AutoBeAgentSessionListContextType | null>(null);
|
|
22
|
-
|
|
23
|
-
export function AutoBeAgentSessionListProvider({
|
|
24
|
-
children,
|
|
25
|
-
storageStrategy,
|
|
26
|
-
}: {
|
|
27
|
-
storageStrategy: IAutoBeAgentSessionStorageStrategy;
|
|
28
|
-
children: ReactNode;
|
|
29
|
-
}) {
|
|
30
|
-
const [sessionList, setSessionList] = useState<IAutoBeAgentSession[]>([]);
|
|
31
|
-
|
|
32
|
-
const refreshSessionList = useCallback(async () => {
|
|
33
|
-
await storageStrategy.getSessionList().then(setSessionList);
|
|
34
|
-
}, [storageStrategy]);
|
|
35
|
-
|
|
36
|
-
useEffect(() => {
|
|
37
|
-
refreshSessionList();
|
|
38
|
-
}, [storageStrategy]);
|
|
39
|
-
|
|
40
|
-
return (
|
|
41
|
-
<AutoBeAgentSessionListContext.Provider
|
|
42
|
-
value={{
|
|
43
|
-
sessionList,
|
|
44
|
-
refreshSessionList,
|
|
45
|
-
}}
|
|
46
|
-
>
|
|
47
|
-
{children}
|
|
48
|
-
</AutoBeAgentSessionListContext.Provider>
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export function useAutoBeAgentSessionList() {
|
|
53
|
-
const context = useContext(AutoBeAgentSessionListContext);
|
|
54
|
-
if (!context) {
|
|
55
|
-
throw new Error("useAutoBeAgent must be used within a AutoBeAgentProvider");
|
|
56
|
-
}
|
|
57
|
-
return context;
|
|
58
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
ReactNode,
|
|
3
|
+
createContext,
|
|
4
|
+
useCallback,
|
|
5
|
+
useContext,
|
|
6
|
+
useEffect,
|
|
7
|
+
useState,
|
|
8
|
+
} from "react";
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
IAutoBeAgentSession,
|
|
12
|
+
IAutoBeAgentSessionStorageStrategy,
|
|
13
|
+
} from "../structure";
|
|
14
|
+
|
|
15
|
+
interface AutoBeAgentSessionListContextType {
|
|
16
|
+
sessionList: IAutoBeAgentSession[];
|
|
17
|
+
refreshSessionList: () => Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const AutoBeAgentSessionListContext =
|
|
21
|
+
createContext<AutoBeAgentSessionListContextType | null>(null);
|
|
22
|
+
|
|
23
|
+
export function AutoBeAgentSessionListProvider({
|
|
24
|
+
children,
|
|
25
|
+
storageStrategy,
|
|
26
|
+
}: {
|
|
27
|
+
storageStrategy: IAutoBeAgentSessionStorageStrategy;
|
|
28
|
+
children: ReactNode;
|
|
29
|
+
}) {
|
|
30
|
+
const [sessionList, setSessionList] = useState<IAutoBeAgentSession[]>([]);
|
|
31
|
+
|
|
32
|
+
const refreshSessionList = useCallback(async () => {
|
|
33
|
+
await storageStrategy.getSessionList().then(setSessionList);
|
|
34
|
+
}, [storageStrategy]);
|
|
35
|
+
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
refreshSessionList();
|
|
38
|
+
}, [storageStrategy]);
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
<AutoBeAgentSessionListContext.Provider
|
|
42
|
+
value={{
|
|
43
|
+
sessionList,
|
|
44
|
+
refreshSessionList,
|
|
45
|
+
}}
|
|
46
|
+
>
|
|
47
|
+
{children}
|
|
48
|
+
</AutoBeAgentSessionListContext.Provider>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function useAutoBeAgentSessionList() {
|
|
53
|
+
const context = useContext(AutoBeAgentSessionListContext);
|
|
54
|
+
if (!context) {
|
|
55
|
+
throw new Error("useAutoBeAgent must be used within a AutoBeAgentProvider");
|
|
56
|
+
}
|
|
57
|
+
return context;
|
|
58
|
+
}
|