@blocklet/pages-kit 0.2.285 → 0.2.287

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 (209) hide show
  1. package/lib/cjs/builtin/async/ai-runtime/api/assistant.js +26 -0
  2. package/lib/cjs/builtin/async/ai-runtime/api/dataset.js +127 -0
  3. package/lib/cjs/builtin/async/ai-runtime/api/message.js +33 -0
  4. package/lib/cjs/builtin/async/ai-runtime/api/request.js +47 -0
  5. package/lib/cjs/builtin/async/ai-runtime/api/session.js +121 -0
  6. package/lib/cjs/builtin/async/ai-runtime/api/subscription.js +32 -0
  7. package/lib/cjs/builtin/async/ai-runtime/components/ActionButton.js +63 -0
  8. package/lib/cjs/builtin/async/ai-runtime/components/GlobalLoading.js +60 -0
  9. package/lib/cjs/builtin/async/ai-runtime/components/LoadingButton.js +40 -0
  10. package/lib/cjs/builtin/async/ai-runtime/components/LoadingListItemButton.js +51 -0
  11. package/lib/cjs/builtin/async/ai-runtime/components/MarkdownRenderer.js +156 -0
  12. package/lib/cjs/builtin/async/ai-runtime/components/MenuButton.js +43 -0
  13. package/lib/cjs/builtin/async/ai-runtime/components/PoweredBy.js +30 -0
  14. package/lib/cjs/builtin/async/ai-runtime/components/RuntimeProvider.js +16 -0
  15. package/lib/cjs/builtin/async/ai-runtime/components/ScrollView.js +109 -0
  16. package/lib/cjs/builtin/async/ai-runtime/components/SocialShare.js +102 -0
  17. package/lib/cjs/builtin/async/ai-runtime/components/ThemeProvider.js +193 -0
  18. package/lib/cjs/builtin/async/ai-runtime/components/collection/CollectionEditDialog.js +92 -0
  19. package/lib/cjs/builtin/async/ai-runtime/components/collection/CollectionListView.js +197 -0
  20. package/lib/cjs/builtin/async/ai-runtime/components/collection/CollectionView.js +122 -0
  21. package/lib/cjs/builtin/async/ai-runtime/components/collection/DocumentListView.js +152 -0
  22. package/lib/cjs/builtin/async/ai-runtime/components/collection/DocumentView.js +135 -0
  23. package/lib/cjs/builtin/async/ai-runtime/components/common/ChatBot.js +105 -0
  24. package/lib/cjs/builtin/async/ai-runtime/components/common/MessageMetadataRenderer.js +21 -0
  25. package/lib/cjs/builtin/async/ai-runtime/components/common/MessageSuggestedQuestions.js +35 -0
  26. package/lib/cjs/builtin/async/ai-runtime/components/common/index.js +12 -0
  27. package/lib/cjs/builtin/async/ai-runtime/components/form/ParameterForm.js +48 -0
  28. package/lib/cjs/builtin/async/ai-runtime/components/form/ParameterFormDialog.js +66 -0
  29. package/lib/cjs/builtin/async/ai-runtime/components/form/ParameterFormFields.js +43 -0
  30. package/lib/cjs/builtin/async/ai-runtime/components/form/QuestionField.js +44 -0
  31. package/lib/cjs/builtin/async/ai-runtime/components/form/SettingButton.js +76 -0
  32. package/lib/cjs/builtin/async/ai-runtime/components/layout/index.js +15 -0
  33. package/lib/cjs/builtin/async/ai-runtime/components/lottie/index.js +34 -0
  34. package/lib/cjs/builtin/async/ai-runtime/components/message/MessageActions.js +38 -0
  35. package/lib/cjs/builtin/async/ai-runtime/components/message/MessageErrorView.js +30 -0
  36. package/lib/cjs/builtin/async/ai-runtime/components/message/MessageItemView.js +134 -0
  37. package/lib/cjs/builtin/async/ai-runtime/components/message/MessageListView.js +48 -0
  38. package/lib/cjs/builtin/async/ai-runtime/components/message/ReferenceLinks.js +11 -0
  39. package/lib/cjs/builtin/async/ai-runtime/components/parameters/LanguageField.js +55 -0
  40. package/lib/cjs/builtin/async/ai-runtime/components/parameters/NumberField.js +21 -0
  41. package/lib/cjs/builtin/async/ai-runtime/components/parameters/SelectField.js +24 -0
  42. package/lib/cjs/builtin/async/ai-runtime/components/parameters/StringField.js +21 -0
  43. package/lib/cjs/builtin/async/ai-runtime/components/parameters/index.js +37 -0
  44. package/lib/cjs/builtin/async/ai-runtime/components/runtime-chat/InitialForm.js +94 -0
  45. package/lib/cjs/builtin/async/ai-runtime/components/runtime-chat/RuntimeChatHeader.js +48 -0
  46. package/lib/cjs/builtin/async/ai-runtime/components/runtime-chat/RuntimeChatHome.js +21 -0
  47. package/lib/cjs/builtin/async/ai-runtime/components/runtime-chat/RuntimeChatSession.js +67 -0
  48. package/lib/cjs/builtin/async/ai-runtime/components/runtime-chat/index.js +15 -0
  49. package/lib/cjs/builtin/async/ai-runtime/components/runtime-form/RuntimeFormForms.js +103 -0
  50. package/lib/cjs/builtin/async/ai-runtime/components/runtime-form/RuntimeFormHeader.js +22 -0
  51. package/lib/cjs/builtin/async/ai-runtime/components/runtime-form/RuntimeFormResult.js +72 -0
  52. package/lib/cjs/builtin/async/ai-runtime/components/runtime-form/RuntimeFormSubmit.js +31 -0
  53. package/lib/cjs/builtin/async/ai-runtime/components/runtime-form/index.js +22 -0
  54. package/lib/cjs/builtin/async/ai-runtime/components/session/CheckSession.js +62 -0
  55. package/lib/cjs/builtin/async/ai-runtime/components/session/SessionListView.js +88 -0
  56. package/lib/cjs/builtin/async/ai-runtime/constants.js +5 -0
  57. package/lib/cjs/builtin/async/ai-runtime/hooks/header.js +32 -0
  58. package/lib/cjs/builtin/async/ai-runtime/hooks/navigate.js +37 -0
  59. package/lib/cjs/builtin/async/ai-runtime/hooks/subscribe.js +35 -0
  60. package/lib/cjs/builtin/async/ai-runtime/index.js +75 -0
  61. package/lib/cjs/builtin/async/ai-runtime/locales/index.js +147 -0
  62. package/lib/cjs/builtin/async/ai-runtime/state/assistant.js +59 -0
  63. package/lib/cjs/builtin/async/ai-runtime/state/index.js +423 -0
  64. package/lib/cjs/builtin/async/ai-runtime/state/session.js +217 -0
  65. package/lib/cjs/builtin/async/ai-runtime/state/subscription.js +52 -0
  66. package/lib/cjs/builtin/async/ai-runtime/utils.js +49 -0
  67. package/lib/cjs/builtin/async/react-scroll-to-bottom.js +1 -1
  68. package/lib/cjs/utils/builtin.js +1 -0
  69. package/lib/cjs/utils/inject-global-components.js +2 -0
  70. package/lib/esm/builtin/async/ai-runtime/api/assistant.js +22 -0
  71. package/lib/esm/builtin/async/ai-runtime/api/dataset.js +113 -0
  72. package/lib/esm/builtin/async/ai-runtime/api/message.js +28 -0
  73. package/lib/esm/builtin/async/ai-runtime/api/request.js +43 -0
  74. package/lib/esm/builtin/async/ai-runtime/api/session.js +112 -0
  75. package/lib/esm/builtin/async/ai-runtime/api/subscription.js +27 -0
  76. package/lib/esm/builtin/async/ai-runtime/components/ActionButton.js +56 -0
  77. package/lib/esm/builtin/async/ai-runtime/components/GlobalLoading.js +56 -0
  78. package/lib/esm/builtin/async/ai-runtime/components/LoadingButton.js +38 -0
  79. package/lib/esm/builtin/async/ai-runtime/components/LoadingListItemButton.js +48 -0
  80. package/lib/esm/builtin/async/ai-runtime/components/MarkdownRenderer.js +129 -0
  81. package/lib/esm/builtin/async/ai-runtime/components/MenuButton.js +40 -0
  82. package/lib/esm/builtin/async/ai-runtime/components/PoweredBy.js +27 -0
  83. package/lib/esm/builtin/async/ai-runtime/components/RuntimeProvider.js +10 -0
  84. package/lib/esm/builtin/async/ai-runtime/components/ScrollView.js +101 -0
  85. package/lib/esm/builtin/async/ai-runtime/components/SocialShare.js +93 -0
  86. package/lib/esm/builtin/async/ai-runtime/components/ThemeProvider.js +190 -0
  87. package/lib/esm/builtin/async/ai-runtime/components/collection/CollectionEditDialog.js +92 -0
  88. package/lib/esm/builtin/async/ai-runtime/components/collection/CollectionListView.js +197 -0
  89. package/lib/esm/builtin/async/ai-runtime/components/collection/CollectionView.js +122 -0
  90. package/lib/esm/builtin/async/ai-runtime/components/collection/DocumentListView.js +152 -0
  91. package/lib/esm/builtin/async/ai-runtime/components/collection/DocumentView.js +135 -0
  92. package/lib/esm/builtin/async/ai-runtime/components/common/ChatBot.js +99 -0
  93. package/lib/esm/builtin/async/ai-runtime/components/common/MessageMetadataRenderer.js +15 -0
  94. package/lib/esm/builtin/async/ai-runtime/components/common/MessageSuggestedQuestions.js +30 -0
  95. package/lib/esm/builtin/async/ai-runtime/components/common/index.js +3 -0
  96. package/lib/esm/builtin/async/ai-runtime/components/form/ParameterForm.js +42 -0
  97. package/lib/esm/builtin/async/ai-runtime/components/form/ParameterFormDialog.js +59 -0
  98. package/lib/esm/builtin/async/ai-runtime/components/form/ParameterFormFields.js +37 -0
  99. package/lib/esm/builtin/async/ai-runtime/components/form/QuestionField.js +38 -0
  100. package/lib/esm/builtin/async/ai-runtime/components/form/SettingButton.js +73 -0
  101. package/lib/esm/builtin/async/ai-runtime/components/layout/index.js +12 -0
  102. package/lib/esm/builtin/async/ai-runtime/components/lottie/index.js +8 -0
  103. package/lib/esm/builtin/async/ai-runtime/components/message/MessageActions.js +35 -0
  104. package/lib/esm/builtin/async/ai-runtime/components/message/MessageErrorView.js +24 -0
  105. package/lib/esm/builtin/async/ai-runtime/components/message/MessageItemView.js +105 -0
  106. package/lib/esm/builtin/async/ai-runtime/components/message/MessageListView.js +42 -0
  107. package/lib/esm/builtin/async/ai-runtime/components/message/ReferenceLinks.js +8 -0
  108. package/lib/esm/builtin/async/ai-runtime/components/parameters/LanguageField.js +53 -0
  109. package/lib/esm/builtin/async/ai-runtime/components/parameters/NumberField.js +19 -0
  110. package/lib/esm/builtin/async/ai-runtime/components/parameters/SelectField.js +22 -0
  111. package/lib/esm/builtin/async/ai-runtime/components/parameters/StringField.js +19 -0
  112. package/lib/esm/builtin/async/ai-runtime/components/parameters/index.js +31 -0
  113. package/lib/esm/builtin/async/ai-runtime/components/runtime-chat/InitialForm.js +88 -0
  114. package/lib/esm/builtin/async/ai-runtime/components/runtime-chat/RuntimeChatHeader.js +22 -0
  115. package/lib/esm/builtin/async/ai-runtime/components/runtime-chat/RuntimeChatHome.js +15 -0
  116. package/lib/esm/builtin/async/ai-runtime/components/runtime-chat/RuntimeChatSession.js +61 -0
  117. package/lib/esm/builtin/async/ai-runtime/components/runtime-chat/index.js +9 -0
  118. package/lib/esm/builtin/async/ai-runtime/components/runtime-form/RuntimeFormForms.js +97 -0
  119. package/lib/esm/builtin/async/ai-runtime/components/runtime-form/RuntimeFormHeader.js +19 -0
  120. package/lib/esm/builtin/async/ai-runtime/components/runtime-form/RuntimeFormResult.js +66 -0
  121. package/lib/esm/builtin/async/ai-runtime/components/runtime-form/RuntimeFormSubmit.js +25 -0
  122. package/lib/esm/builtin/async/ai-runtime/components/runtime-form/index.js +16 -0
  123. package/lib/esm/builtin/async/ai-runtime/components/session/CheckSession.js +59 -0
  124. package/lib/esm/builtin/async/ai-runtime/components/session/SessionListView.js +88 -0
  125. package/lib/esm/builtin/async/ai-runtime/constants.js +2 -0
  126. package/lib/esm/builtin/async/ai-runtime/hooks/header.js +25 -0
  127. package/lib/esm/builtin/async/ai-runtime/hooks/navigate.js +31 -0
  128. package/lib/esm/builtin/async/ai-runtime/hooks/subscribe.js +31 -0
  129. package/lib/esm/builtin/async/ai-runtime/index.js +66 -0
  130. package/lib/esm/builtin/async/ai-runtime/locales/index.js +144 -0
  131. package/lib/esm/builtin/async/ai-runtime/state/assistant.js +54 -0
  132. package/lib/esm/builtin/async/ai-runtime/state/index.js +423 -0
  133. package/lib/esm/builtin/async/ai-runtime/state/session.js +209 -0
  134. package/lib/esm/builtin/async/ai-runtime/state/subscription.js +47 -0
  135. package/lib/esm/builtin/async/ai-runtime/utils.js +42 -0
  136. package/lib/esm/builtin/async/react-scroll-to-bottom.js +1 -1
  137. package/lib/esm/utils/builtin.js +1 -0
  138. package/lib/esm/utils/inject-global-components.js +2 -0
  139. package/lib/types/builtin/async/ai-runtime/api/assistant.d.ts +18 -0
  140. package/lib/types/builtin/async/ai-runtime/api/dataset.d.ts +39 -0
  141. package/lib/types/builtin/async/ai-runtime/api/message.d.ts +39 -0
  142. package/lib/types/builtin/async/ai-runtime/api/request.d.ts +5 -0
  143. package/lib/types/builtin/async/ai-runtime/api/session.d.ts +54 -0
  144. package/lib/types/builtin/async/ai-runtime/api/subscription.d.ts +15 -0
  145. package/lib/types/builtin/async/ai-runtime/components/ActionButton.d.ts +11 -0
  146. package/lib/types/builtin/async/ai-runtime/components/GlobalLoading.d.ts +33 -0
  147. package/lib/types/builtin/async/ai-runtime/components/LoadingButton.d.ts +3 -0
  148. package/lib/types/builtin/async/ai-runtime/components/LoadingListItemButton.d.ts +2 -0
  149. package/lib/types/builtin/async/ai-runtime/components/MarkdownRenderer.d.ts +2 -0
  150. package/lib/types/builtin/async/ai-runtime/components/MenuButton.d.ts +9 -0
  151. package/lib/types/builtin/async/ai-runtime/components/PoweredBy.d.ts +4 -0
  152. package/lib/types/builtin/async/ai-runtime/components/RuntimeProvider.d.ts +4 -0
  153. package/lib/types/builtin/async/ai-runtime/components/ScrollView.d.ts +61 -0
  154. package/lib/types/builtin/async/ai-runtime/components/SocialShare.d.ts +17 -0
  155. package/lib/types/builtin/async/ai-runtime/components/ThemeProvider.d.ts +6 -0
  156. package/lib/types/builtin/async/ai-runtime/components/collection/CollectionEditDialog.d.ts +0 -0
  157. package/lib/types/builtin/async/ai-runtime/components/collection/CollectionListView.d.ts +0 -0
  158. package/lib/types/builtin/async/ai-runtime/components/collection/CollectionView.d.ts +0 -0
  159. package/lib/types/builtin/async/ai-runtime/components/collection/DocumentListView.d.ts +0 -0
  160. package/lib/types/builtin/async/ai-runtime/components/collection/DocumentView.d.ts +0 -0
  161. package/lib/types/builtin/async/ai-runtime/components/common/ChatBot.d.ts +13 -0
  162. package/lib/types/builtin/async/ai-runtime/components/common/MessageMetadataRenderer.d.ts +4 -0
  163. package/lib/types/builtin/async/ai-runtime/components/common/MessageSuggestedQuestions.d.ts +7 -0
  164. package/lib/types/builtin/async/ai-runtime/components/common/index.d.ts +3 -0
  165. package/lib/types/builtin/async/ai-runtime/components/form/ParameterForm.d.ts +12 -0
  166. package/lib/types/builtin/async/ai-runtime/components/form/ParameterFormDialog.d.ts +28 -0
  167. package/lib/types/builtin/async/ai-runtime/components/form/ParameterFormFields.d.ts +8 -0
  168. package/lib/types/builtin/async/ai-runtime/components/form/QuestionField.d.ts +13 -0
  169. package/lib/types/builtin/async/ai-runtime/components/form/SettingButton.d.ts +4 -0
  170. package/lib/types/builtin/async/ai-runtime/components/layout/index.d.ts +7 -0
  171. package/lib/types/builtin/async/ai-runtime/components/lottie/index.d.ts +4 -0
  172. package/lib/types/builtin/async/ai-runtime/components/message/MessageActions.d.ts +4 -0
  173. package/lib/types/builtin/async/ai-runtime/components/message/MessageErrorView.d.ts +6 -0
  174. package/lib/types/builtin/async/ai-runtime/components/message/MessageItemView.d.ts +10 -0
  175. package/lib/types/builtin/async/ai-runtime/components/message/MessageListView.d.ts +5 -0
  176. package/lib/types/builtin/async/ai-runtime/components/message/ReferenceLinks.d.ts +4 -0
  177. package/lib/types/builtin/async/ai-runtime/components/parameters/LanguageField.d.ts +9 -0
  178. package/lib/types/builtin/async/ai-runtime/components/parameters/NumberField.d.ts +9 -0
  179. package/lib/types/builtin/async/ai-runtime/components/parameters/SelectField.d.ts +9 -0
  180. package/lib/types/builtin/async/ai-runtime/components/parameters/StringField.d.ts +9 -0
  181. package/lib/types/builtin/async/ai-runtime/components/parameters/index.d.ts +7 -0
  182. package/lib/types/builtin/async/ai-runtime/components/runtime-chat/InitialForm.d.ts +4 -0
  183. package/lib/types/builtin/async/ai-runtime/components/runtime-chat/RuntimeChatHeader.d.ts +4 -0
  184. package/lib/types/builtin/async/ai-runtime/components/runtime-chat/RuntimeChatHome.d.ts +4 -0
  185. package/lib/types/builtin/async/ai-runtime/components/runtime-chat/RuntimeChatSession.d.ts +6 -0
  186. package/lib/types/builtin/async/ai-runtime/components/runtime-chat/index.d.ts +4 -0
  187. package/lib/types/builtin/async/ai-runtime/components/runtime-form/RuntimeFormForms.d.ts +5 -0
  188. package/lib/types/builtin/async/ai-runtime/components/runtime-form/RuntimeFormHeader.d.ts +5 -0
  189. package/lib/types/builtin/async/ai-runtime/components/runtime-form/RuntimeFormResult.d.ts +5 -0
  190. package/lib/types/builtin/async/ai-runtime/components/runtime-form/RuntimeFormSubmit.d.ts +5 -0
  191. package/lib/types/builtin/async/ai-runtime/components/runtime-form/index.d.ts +4 -0
  192. package/lib/types/builtin/async/ai-runtime/components/session/CheckSession.d.ts +9 -0
  193. package/lib/types/builtin/async/ai-runtime/components/session/SessionListView.d.ts +0 -0
  194. package/lib/types/builtin/async/ai-runtime/constants.d.ts +2 -0
  195. package/lib/types/builtin/async/ai-runtime/hooks/header.d.ts +4 -0
  196. package/lib/types/builtin/async/ai-runtime/hooks/navigate.d.ts +25 -0
  197. package/lib/types/builtin/async/ai-runtime/hooks/subscribe.d.ts +6 -0
  198. package/lib/types/builtin/async/ai-runtime/index.d.ts +17 -0
  199. package/lib/types/builtin/async/ai-runtime/locales/index.d.ts +126 -0
  200. package/lib/types/builtin/async/ai-runtime/state/assistant.d.ts +17 -0
  201. package/lib/types/builtin/async/ai-runtime/state/index.d.ts +0 -0
  202. package/lib/types/builtin/async/ai-runtime/state/session.d.ts +40 -0
  203. package/lib/types/builtin/async/ai-runtime/state/subscription.d.ts +15 -0
  204. package/lib/types/builtin/async/ai-runtime/utils.d.ts +20 -0
  205. package/lib/types/builtin/async/react-scroll-to-bottom.d.ts +3 -2
  206. package/lib/types/builtin/session.d.ts +11 -1
  207. package/lib/types/utils/builtin.d.ts +1 -0
  208. package/package.json +13 -8
  209. package/tsconfig.json +5 -1
@@ -0,0 +1,423 @@
1
+ "use strict";
2
+ // import { StoreApi, UseBoundStore, create } from 'zustand';
3
+ // import { immer } from 'zustand/middleware/immer';
4
+ // import { EventSourceParserStream } from '../../../stream';
5
+ // import { getComponentMountPoint, joinURL } from '../../../utils';
6
+ // import { Assistant, getAssistant } from '../api/assistant';
7
+ // import {
8
+ // Dataset,
9
+ // createDataset,
10
+ // createDocument,
11
+ // deleteDataset,
12
+ // deleteDocument,
13
+ // getDataset,
14
+ // getDatasets,
15
+ // getDocuments,
16
+ // updateDataset,
17
+ // updateDocument,
18
+ // } from '../api/dataset';
19
+ // import { deleteMessages, getMessages } from '../api/message';
20
+ // import { Session, createSession, getSessions, updateSession } from '../api/session';
21
+ // import { Subscription, getSubscription } from '../api/subscription';
22
+ // import { AI_STUDIO_DID } from '../constants';
23
+ // import { parseIdentity } from '../utils';
24
+ // const STATES: {
25
+ // assistant: { [key: string]: UseBoundStore<StoreApi<State>> };
26
+ // dataset: { [key: string]: ReturnType<typeof createDatasetState> };
27
+ // datasetDetail: { [key: string]: ReturnType<typeof createDatasetDetailState> };
28
+ // } = { assistant: {}, dataset: {}, datasetDetail: {} };
29
+ // export interface State {
30
+ // loading: boolean;
31
+ // loaded: boolean;
32
+ // assistant?: Assistant;
33
+ // sessions?: Session[];
34
+ // subscription?: Subscription;
35
+ // sessionMap: { [sessionId: string]: { session: Session } };
36
+ // computed: {
37
+ // get isChat(): boolean | undefined;
38
+ // get requireNewSubscription(): boolean | undefined;
39
+ // };
40
+ // }
41
+ // export const createState = ({
42
+ // releaseId,
43
+ // preload: { assistant, sessions, subscription } = {},
44
+ // }: {
45
+ // releaseId: string;
46
+ // preload?: {
47
+ // assistant?: Assistant;
48
+ // sessions?: Session[];
49
+ // subscription?: Subscription;
50
+ // };
51
+ // }) => {
52
+ // STATES.assistant[releaseId] ??= create<State>()(
53
+ // immer<State>((set, get) => ({
54
+ // loading: false,
55
+ // loaded: false,
56
+ // assistant,
57
+ // sessions,
58
+ // subscription,
59
+ // sessionMap: Object.fromEntries((sessions ?? []).map((session) => [session.id, { session }])),
60
+ // computed: {
61
+ // get isChat() {
62
+ // const parameters = get().assistant?.parameters;
63
+ // return parameters?.some((i) => i.key === 'question');
64
+ // },
65
+ // get requireNewSubscription() {
66
+ // // TODO: implement payment
67
+ // // const { subscription } = get();
68
+ // // const { paymentEnabled, paymentLinkId } = release;
69
+ // // return !!paymentEnabled && !!paymentLinkId && !subscription;
70
+ // return false;
71
+ // },
72
+ // get withCollection() {
73
+ // const parameters = get().assistant?.parameters;
74
+ // return parameters?.some((i) => i.key === 'datasetId');
75
+ // },
76
+ // },
77
+ // getPaymentLink({ userId }: { userId: string }) {
78
+ // // TODO: implement
79
+ // // return withQuery(
80
+ // // joinURL('/', getComponentMountPoint(PAYMENT_KIT_DID), '/checkout/pay', release.paymentLinkId),
81
+ // // {
82
+ // // 'metadata.releaseId': release.id,
83
+ // // 'metadata.userId': userId,
84
+ // // redirect: window.location.href,
85
+ // // }
86
+ // // );
87
+ // },
88
+ // getAssistant: async () => {
89
+ // set((state) => {
90
+ // state.loading = true;
91
+ // });
92
+ // try {
93
+ // const { subscription } = await getSubscription({ releaseId });
94
+ // const identity = parseIdentity(releaseId, { rejectWhenError: true });
95
+ // const [assistant, { sessions }] = await Promise.all([getAssistant(identity), getSessions(identity)]);
96
+ // set((state) => {
97
+ // state.assistant = assistant;
98
+ // state.sessions = sessions;
99
+ // state.subscription = subscription;
100
+ // state.sessionMap = Object.fromEntries(
101
+ // (sessions ?? []).map((session) => [session.id, { ...state.sessionMap[session.id], session }])
102
+ // );
103
+ // });
104
+ // } finally {
105
+ // set((state) => {
106
+ // state.loading = false;
107
+ // state.loaded = true;
108
+ // });
109
+ // }
110
+ // },
111
+ // getSessions: async () => {
112
+ // const identity = parseIdentity(releaseId, { rejectWhenError: true });
113
+ // const { sessions } = await getSessions(identity);
114
+ // set((state) => {
115
+ // state.sessions = sessions;
116
+ // state.sessionMap = Object.fromEntries(
117
+ // (sessions ?? []).map((session) => [session.id, { ...state.sessionMap[session.id], session }])
118
+ // );
119
+ // });
120
+ // },
121
+ // reloadMessages: async ({ sessionId }) => {
122
+ // set((state) => {
123
+ // const s = state.sessionMap[sessionId];
124
+ // if (!s) return;
125
+ // s.loading = true;
126
+ // });
127
+ // try {
128
+ // const { messages, count } = await getMessages({ sessionId });
129
+ // set((state) => {
130
+ // const s = state.sessionMap[sessionId];
131
+ // if (!s) return;
132
+ // s.messages = messages;
133
+ // s.messageCount = count;
134
+ // });
135
+ // } finally {
136
+ // set((state) => {
137
+ // const s = state.sessionMap[sessionId];
138
+ // if (!s) return;
139
+ // s.loading = false;
140
+ // });
141
+ // }
142
+ // },
143
+ // cleanSession: async ({ sessionId }) => {
144
+ // set((state) => {
145
+ // const s = state.sessionMap[sessionId];
146
+ // if (s) s.sessionReseting = true;
147
+ // });
148
+ // await deleteMessages({ sessionId });
149
+ // set((state) => {
150
+ // const s = state.sessionMap[sessionId];
151
+ // if (s) {
152
+ // s.sessionReseting = false;
153
+ // s.messages = [];
154
+ // }
155
+ // });
156
+ // },
157
+ // createSession: async ({ name, parameters, entry }) => {
158
+ // const identity = parseIdentity(releaseId);
159
+ // const { sessions, created } = await createSession({ ...identity, name, parameters, entry });
160
+ // set((state) => {
161
+ // state.sessions = sessions;
162
+ // state.sessionMap = Object.fromEntries(
163
+ // (sessions ?? []).map((session) => [session.id, { ...state.sessionMap[session.id], session }])
164
+ // );
165
+ // });
166
+ // return created;
167
+ // },
168
+ // updateSession: async ({ sessionId, parameters }) => {
169
+ // set((state) => {
170
+ // const s = state.sessionMap[sessionId].session;
171
+ // if (s.parameters) Object.assign(s.parameters, parameters);
172
+ // });
173
+ // const { sessions } = await updateSession({ sessionId, parameters });
174
+ // set((state) => {
175
+ // state.sessions = sessions;
176
+ // state.sessionMap = Object.fromEntries(
177
+ // (sessions ?? []).map((session) => [session.id, { ...state.sessionMap[session.id], session }])
178
+ // );
179
+ // });
180
+ // },
181
+ // execute: async ({ sessionId, parameters, onResponseStart, assistantIdentity }) => {
182
+ // const identity = parseIdentity(releaseId);
183
+ // let message;
184
+ // const childMessages = [];
185
+ // const childMessagesMap = {};
186
+ // set((state) => {
187
+ // const session = state.sessionMap[sessionId];
188
+ // if (session) session.executing = true;
189
+ // });
190
+ // try {
191
+ // const res = await fetch(joinURL(getComponentMountPoint(AI_STUDIO_DID), '/api/ai/call'), {
192
+ // method: 'POST',
193
+ // headers: {
194
+ // 'Content-Type': 'application/json',
195
+ // Accept: 'text/event-stream',
196
+ // },
197
+ // body: JSON.stringify({
198
+ // sessionId,
199
+ // parameters: { ...parameters, $clientTime: new Date().toISOString() },
200
+ // ...(assistantIdentity || identity),
201
+ // ref: (assistantIdentity || identity).projectRef,
202
+ // working: true,
203
+ // }),
204
+ // });
205
+ // if (!(res.status >= 200 && res.status < 300)) {
206
+ // let json: any;
207
+ // try {
208
+ // json = await res.json();
209
+ // } catch (error) {}
210
+ // const { message } = json.error;
211
+ // if (typeof message === 'string') {
212
+ // throw new Error(message);
213
+ // }
214
+ // throw new Error(`Unknown Error: ${res.status}`);
215
+ // }
216
+ // const stream = res
217
+ // .body!.pipeThrough(new TextDecoderStream())
218
+ // .pipeThrough(new EventSourceParserStream())
219
+ // .getReader();
220
+ // let responseStarted = false;
221
+ // for (;;) {
222
+ // const { value, done } = await stream.read();
223
+ // if (!responseStarted) {
224
+ // responseStarted = true;
225
+ // onResponseStart?.();
226
+ // }
227
+ // if (value?.type === 'CHUNK') {
228
+ // if (!message) {
229
+ // message = {
230
+ // taskId: value.taskId,
231
+ // parameters,
232
+ // loading: true,
233
+ // createdAt: new Date().toISOString(),
234
+ // };
235
+ // set((state) => {
236
+ // state.sessionMap[sessionId] ??= {};
237
+ // state.sessionMap[sessionId].messages ??= [];
238
+ // state.sessionMap[sessionId].messages.push(message);
239
+ // });
240
+ // }
241
+ // if (message.taskId === value.taskId) {
242
+ // set((state) => {
243
+ // const msg = state.sessionMap[sessionId]?.messages?.findLast((i) => i.taskId === message.taskId);
244
+ // if (!msg) return;
245
+ // msg.result ??= {};
246
+ // msg.result.content = (msg.result.content || '') + (value.delta.content || '');
247
+ // if (value.delta.object) {
248
+ // msg.result.objects ??= [];
249
+ // msg.result.objects.push(value.delta.object);
250
+ // if (value.delta.object.metadata) {
251
+ // const session = state.sessionMap[sessionId];
252
+ // session.metadata ??= {};
253
+ // deepMerge(session.metadata, value.delta.object.metadata);
254
+ // }
255
+ // }
256
+ // if (value.delta?.images?.length) {
257
+ // msg.result.images = (msg.result.images ?? []).concat(value.delta.images);
258
+ // }
259
+ // });
260
+ // } else if (value.respondAs && value.respondAs !== 'none') {
261
+ // let childMessage = childMessagesMap[value.taskId];
262
+ // if (!childMessage) {
263
+ // childMessage = {
264
+ // taskId: value.taskId,
265
+ // createdAt: new Date().toISOString(),
266
+ // respondAs: value.respondAs,
267
+ // };
268
+ // childMessagesMap[value.taskId] = childMessage;
269
+ // childMessages.push(childMessage);
270
+ // }
271
+ // childMessage.result ??= {};
272
+ // childMessage.result.content = (childMessage.result.content || '') + (value.delta.content || '');
273
+ // if (value.delta?.images?.length) {
274
+ // childMessage.result.images = (childMessage.result.images ?? []).concat(value.delta.images);
275
+ // }
276
+ // set((state) => {
277
+ // const msg = state.sessionMap[sessionId]?.messages?.findLast((i) => i.taskId === message.taskId);
278
+ // if (!msg) return;
279
+ // msg.result ??= {};
280
+ // msg.result.messages = JSON.parse(JSON.stringify(childMessages));
281
+ // });
282
+ // }
283
+ // }
284
+ // if (value?.type === 'ERROR') {
285
+ // set((state) => {
286
+ // const msg = state.sessionMap[sessionId]?.messages?.findLast((i) => i.taskId === message.taskId);
287
+ // if (msg) msg.error = value.error;
288
+ // else throw new Error(value.error.message);
289
+ // });
290
+ // }
291
+ // if (done) break;
292
+ // }
293
+ // } finally {
294
+ // set((state) => {
295
+ // const session = state.sessionMap[sessionId];
296
+ // session.executing = false;
297
+ // if (session && message) {
298
+ // const msg = session.messages?.findLast((i) => i.taskId === message.taskId);
299
+ // if (msg) msg.loading = false;
300
+ // }
301
+ // });
302
+ // }
303
+ // },
304
+ // }))
305
+ // );
306
+ // return STATES.assistant[releaseId]!;
307
+ // };
308
+ // export function createDatasetState({ releaseId, datasets }: { releaseId: string; datasets?: Dataset[] }) {
309
+ // STATES.dataset[releaseId] ??= create()(
310
+ // immer((set) => ({
311
+ // loading: false,
312
+ // list: datasets,
313
+ // error: undefined,
314
+ // creating: false,
315
+ // async reload() {
316
+ // try {
317
+ // set((state) => {
318
+ // state.loading = true;
319
+ // });
320
+ // const datasets = await getDatasets({ appId: releaseId });
321
+ // set((state) => {
322
+ // state.loading = false;
323
+ // state.list = datasets;
324
+ // state.error = undefined;
325
+ // });
326
+ // } catch (error) {
327
+ // set((state) => {
328
+ // state.loading = false;
329
+ // state.error = error;
330
+ // });
331
+ // }
332
+ // },
333
+ // async createDataset({ name, description }: { name: string; description?: string }) {
334
+ // try {
335
+ // set((state) => {
336
+ // state.creating = true;
337
+ // });
338
+ // const created = await createDataset({ appId: releaseId, name, description });
339
+ // await this.reload();
340
+ // return created;
341
+ // } finally {
342
+ // set((state) => {
343
+ // state.creating = false;
344
+ // });
345
+ // }
346
+ // },
347
+ // async updateDataset(datasetId: string, { name, description }: { name: string; description?: string }) {
348
+ // const updated = await updateDataset(datasetId, { name, description });
349
+ // await this.reload();
350
+ // return updated;
351
+ // },
352
+ // async deleteDataset(datasetId: string) {
353
+ // await deleteDataset(datasetId);
354
+ // await this.reload();
355
+ // },
356
+ // }))
357
+ // );
358
+ // return STATES.dataset[releaseId];
359
+ // }
360
+ // export function createDatasetDetailState({ datasetId, list }: { datasetId: string; list?: Document[] }) {
361
+ // STATES.datasetDetail[datasetId] ??= create()(
362
+ // immer((set, get) => ({
363
+ // loading: false,
364
+ // dataset: undefined,
365
+ // list,
366
+ // error: undefined,
367
+ // creating: false,
368
+ // async reload() {
369
+ // if (get().loading) return;
370
+ // set((state) => {
371
+ // state.loading = true;
372
+ // });
373
+ // try {
374
+ // const [dataset, { items }] = await Promise.all([getDataset({ datasetId }), getDocuments(datasetId)]);
375
+ // set((state) => {
376
+ // state.dataset = dataset;
377
+ // state.loading = false;
378
+ // state.list = items;
379
+ // state.error = undefined;
380
+ // });
381
+ // } catch (error) {
382
+ // set((state) => {
383
+ // state.loading = false;
384
+ // state.error = error;
385
+ // });
386
+ // }
387
+ // },
388
+ // async createDocument(type: 'text' | 'file', input: Parameters<typeof createDocument>[1]) {
389
+ // try {
390
+ // set((state) => {
391
+ // state.creating = true;
392
+ // });
393
+ // const created = await createDocument(datasetId, type, input);
394
+ // await this.reload();
395
+ // return created;
396
+ // } finally {
397
+ // set((state) => {
398
+ // state.creating = false;
399
+ // });
400
+ // }
401
+ // },
402
+ // async updateDocument(documentId: string, type: 'text', input: Parameters<typeof updateDocument>[1]) {
403
+ // try {
404
+ // set((state) => {
405
+ // state.updating = true;
406
+ // });
407
+ // const updated = await updateDocument(datasetId, documentId, type, input);
408
+ // await this.reload();
409
+ // return updated;
410
+ // } finally {
411
+ // set((state) => {
412
+ // state.updating = false;
413
+ // });
414
+ // }
415
+ // },
416
+ // async deleteDocument(documentId) {
417
+ // await deleteDocument(datasetId, documentId);
418
+ // await this.reload();
419
+ // },
420
+ // }))
421
+ // );
422
+ // return STATES.datasetDetail[datasetId];
423
+ // }
@@ -0,0 +1,217 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
12
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
13
+ var m = o[Symbol.asyncIterator], i;
14
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
15
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
16
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
17
+ };
18
+ var __importDefault = (this && this.__importDefault) || function (mod) {
19
+ return (mod && mod.__esModule) ? mod : { "default": mod };
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.useSessionState = exports.createSessionState = void 0;
23
+ const types_1 = require("@blocklet/ai-runtime/types");
24
+ const pick_1 = __importDefault(require("lodash/pick"));
25
+ const zustand_1 = require("zustand");
26
+ const immer_1 = require("zustand/middleware/immer");
27
+ const message_1 = require("../api/message");
28
+ const session_1 = require("../api/session");
29
+ const utils_1 = require("../utils");
30
+ const STATES = {};
31
+ const createSessionState = ({ sessionId, session }) => {
32
+ var _a;
33
+ (_a = STATES[sessionId]) !== null && _a !== void 0 ? _a : (STATES[sessionId] = (0, zustand_1.create)()((0, immer_1.immer)((set, get) => ({
34
+ session,
35
+ clearSession: (_b) => __awaiter(void 0, [_b], void 0, function* ({ sessionId }) {
36
+ yield (0, message_1.deleteMessages)({ sessionId });
37
+ get().load();
38
+ }),
39
+ updateSession: (_c) => __awaiter(void 0, [_c], void 0, function* ({ parameters }) {
40
+ const { updated } = yield (0, session_1.updateSession)({ sessionId, parameters });
41
+ set((state) => {
42
+ state.session = updated;
43
+ });
44
+ }),
45
+ load: () => __awaiter(void 0, void 0, void 0, function* () {
46
+ set((state) => {
47
+ state.loading = true;
48
+ });
49
+ try {
50
+ const [{ session }, { messages }] = yield Promise.all([
51
+ (0, session_1.getSession)({ sessionId }),
52
+ (0, message_1.getMessages)({ sessionId }),
53
+ ]);
54
+ set((state) => {
55
+ state.session = session;
56
+ state.messages = messages;
57
+ });
58
+ }
59
+ catch (error) {
60
+ set((state) => {
61
+ state.error = error;
62
+ });
63
+ throw error;
64
+ }
65
+ finally {
66
+ set((state) => {
67
+ state.loading = false;
68
+ });
69
+ }
70
+ }),
71
+ execute: (_d) => __awaiter(void 0, [_d], void 0, function* ({ assistantId, parameters, onResponseStart, includeSessionParameters, }) {
72
+ var _e, e_1, _f, _g;
73
+ var _h, _j, _k, _l, _m;
74
+ const identity = (0, utils_1.parseIdentity)(assistantId, { rejectWhenError: true });
75
+ let message;
76
+ const childMessages = [];
77
+ const childMessagesMap = {};
78
+ set((state) => {
79
+ state.running = true;
80
+ state.error = undefined;
81
+ });
82
+ let realParameters = Object.assign(Object.assign({}, parameters), { $clientTime: new Date().toISOString() });
83
+ if (includeSessionParameters) {
84
+ realParameters = Object.assign(Object.assign({}, (_h = get().session) === null || _h === void 0 ? void 0 : _h.parameters), parameters);
85
+ }
86
+ try {
87
+ const stream = (0, session_1.runAssistant)({
88
+ sessionId,
89
+ identity,
90
+ parameters: realParameters,
91
+ });
92
+ let responseStarted = false;
93
+ try {
94
+ for (var _o = true, stream_1 = __asyncValues(stream), stream_1_1; stream_1_1 = yield stream_1.next(), _e = stream_1_1.done, !_e; _o = true) {
95
+ _g = stream_1_1.value;
96
+ _o = false;
97
+ const value = _g;
98
+ if (!responseStarted) {
99
+ responseStarted = true;
100
+ onResponseStart === null || onResponseStart === void 0 ? void 0 : onResponseStart();
101
+ }
102
+ if ((value === null || value === void 0 ? void 0 : value.type) === 'CHUNK') {
103
+ if (!message) {
104
+ message = {
105
+ taskId: value.taskId,
106
+ sessionId,
107
+ parameters,
108
+ createdAt: new Date().toISOString(),
109
+ updatedAt: new Date().toISOString(),
110
+ loading: true,
111
+ };
112
+ set((state) => {
113
+ var _a;
114
+ (_a = state.messages) !== null && _a !== void 0 ? _a : (state.messages = []);
115
+ state.messages.push(message);
116
+ });
117
+ }
118
+ if (message.taskId === value.taskId) {
119
+ set((state) => {
120
+ var _a, _b, _c, _d, _e, _f, _g;
121
+ var _h;
122
+ const msg = (_a = state.messages) === null || _a === void 0 ? void 0 : _a.findLast((i) => i.taskId === message.taskId);
123
+ if (!msg)
124
+ return;
125
+ (_b = msg.result) !== null && _b !== void 0 ? _b : (msg.result = {});
126
+ msg.result.content = (msg.result.content || '') + (value.delta.content || '');
127
+ if ((_d = (_c = value.delta) === null || _c === void 0 ? void 0 : _c.images) === null || _d === void 0 ? void 0 : _d.length) {
128
+ msg.result.images = ((_e = msg.result.images) !== null && _e !== void 0 ? _e : []).concat(value.delta.images);
129
+ }
130
+ if (value.delta.object) {
131
+ (_f = (_h = msg.result).objects) !== null && _f !== void 0 ? _f : (_h.objects = []);
132
+ msg.result.objects.push({ taskId: value.taskId, data: value.delta.object });
133
+ if (value.delta.object) {
134
+ (_g = state.metadata) !== null && _g !== void 0 ? _g : (state.metadata = {});
135
+ (0, utils_1.deepMerge)(state.metadata, (0, pick_1.default)(value.delta.object, types_1.RuntimeOutputVariableNames));
136
+ }
137
+ }
138
+ });
139
+ }
140
+ else if (value.respondAs && value.respondAs !== 'none') {
141
+ let childMessage = childMessagesMap[value.taskId];
142
+ if (!childMessage) {
143
+ childMessage = {
144
+ taskId: value.taskId,
145
+ respondAs: value.respondAs,
146
+ };
147
+ childMessagesMap[value.taskId] = childMessage;
148
+ childMessages.push(childMessage);
149
+ }
150
+ (_j = childMessage.result) !== null && _j !== void 0 ? _j : (childMessage.result = {});
151
+ childMessage.result.content = (childMessage.result.content || '') + (value.delta.content || '');
152
+ if ((_l = (_k = value.delta) === null || _k === void 0 ? void 0 : _k.images) === null || _l === void 0 ? void 0 : _l.length) {
153
+ childMessage.result.images = ((_m = childMessage.result.images) !== null && _m !== void 0 ? _m : []).concat(value.delta.images);
154
+ }
155
+ set((state) => {
156
+ var _a, _b;
157
+ const msg = (_a = state.messages) === null || _a === void 0 ? void 0 : _a.findLast((i) => i.taskId === message.taskId);
158
+ if (!msg)
159
+ return;
160
+ (_b = msg.result) !== null && _b !== void 0 ? _b : (msg.result = {});
161
+ msg.result.messages = JSON.parse(JSON.stringify(childMessages));
162
+ });
163
+ }
164
+ }
165
+ if ((value === null || value === void 0 ? void 0 : value.type) === 'ERROR') {
166
+ set((state) => {
167
+ var _a;
168
+ const msg = (_a = state.messages) === null || _a === void 0 ? void 0 : _a.findLast((i) => i.taskId === message.taskId);
169
+ if (msg)
170
+ msg.error = value.error;
171
+ else
172
+ throw new Error(value.error.message);
173
+ });
174
+ }
175
+ }
176
+ }
177
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
178
+ finally {
179
+ try {
180
+ if (!_o && !_e && (_f = stream_1.return)) yield _f.call(stream_1);
181
+ }
182
+ finally { if (e_1) throw e_1.error; }
183
+ }
184
+ }
185
+ catch (error) {
186
+ set((state) => {
187
+ state.error = error;
188
+ });
189
+ }
190
+ finally {
191
+ set((state) => {
192
+ var _a;
193
+ state.running = false;
194
+ if (message) {
195
+ const msg = (_a = state.messages) === null || _a === void 0 ? void 0 : _a.findLast((i) => i.taskId === message.taskId);
196
+ if (msg)
197
+ msg.loading = false;
198
+ }
199
+ });
200
+ }
201
+ }),
202
+ }))));
203
+ return STATES[sessionId];
204
+ };
205
+ exports.createSessionState = createSessionState;
206
+ const LOADING_TASKS = {};
207
+ function useSessionState({ sessionId, required }) {
208
+ var _a;
209
+ const state = (0, exports.createSessionState)({ sessionId })();
210
+ if (!state.session) {
211
+ (_a = LOADING_TASKS[sessionId]) !== null && _a !== void 0 ? _a : (LOADING_TASKS[sessionId] = state.load());
212
+ if (required)
213
+ throw LOADING_TASKS[sessionId];
214
+ }
215
+ return [state.session, state];
216
+ }
217
+ exports.useSessionState = useSessionState;