@blocklet/pages-kit 0.2.291 → 0.2.293

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 (137) hide show
  1. package/lib/cjs/builtin/async/ai-runtime/api/assistant.js +2 -1
  2. package/lib/cjs/builtin/async/ai-runtime/api/message.js +14 -1
  3. package/lib/cjs/builtin/async/ai-runtime/api/request.js +5 -3
  4. package/lib/cjs/builtin/async/ai-runtime/api/session.js +2 -2
  5. package/lib/cjs/builtin/async/ai-runtime/components/PoweredBy.js +1 -9
  6. package/lib/cjs/builtin/async/ai-runtime/components/RuntimeProvider.js +1 -1
  7. package/lib/cjs/builtin/async/ai-runtime/components/ThemeProvider.js +1 -1
  8. package/lib/cjs/builtin/async/ai-runtime/components/form/ParameterForm.js +4 -4
  9. package/lib/cjs/builtin/async/ai-runtime/components/form/ParameterFormFields.js +5 -1
  10. package/lib/cjs/builtin/async/ai-runtime/components/form/QuestionField.js +2 -2
  11. package/lib/cjs/builtin/async/ai-runtime/components/form/SettingButton.js +16 -9
  12. package/lib/cjs/builtin/async/ai-runtime/components/message/MessageErrorView.js +14 -3
  13. package/lib/cjs/builtin/async/ai-runtime/components/message/MessageItemView.js +65 -29
  14. package/lib/cjs/builtin/async/ai-runtime/components/message/MessageListView.js +6 -5
  15. package/lib/cjs/builtin/async/ai-runtime/components/parameters/RadioField.js +39 -0
  16. package/lib/cjs/builtin/async/ai-runtime/components/parameters/SelectField.js +1 -1
  17. package/lib/cjs/builtin/async/ai-runtime/components/parameters/index.js +5 -1
  18. package/lib/cjs/builtin/async/ai-runtime/components/runtime/components/AutoForm/index.js +153 -0
  19. package/lib/cjs/builtin/async/ai-runtime/components/runtime/components/ChatOutput/MessageErrorView.js +19 -0
  20. package/lib/cjs/builtin/async/ai-runtime/components/runtime/components/ChatOutput/MessageItemView.js +179 -0
  21. package/lib/cjs/builtin/async/ai-runtime/components/runtime/components/ChatOutput/index.js +13 -0
  22. package/lib/cjs/builtin/async/ai-runtime/components/runtime/components/PhotoGallery/index.js +129 -0
  23. package/lib/cjs/builtin/async/ai-runtime/components/runtime/components/PhotoGalleryItem/index.js +117 -0
  24. package/lib/cjs/builtin/async/ai-runtime/components/runtime/components/SimpleChat/index.js +109 -0
  25. package/lib/cjs/builtin/async/ai-runtime/components/runtime/components/SimpleOutput/index.js +17 -0
  26. package/lib/cjs/builtin/async/ai-runtime/components/runtime/components/SimplePage/index.js +87 -0
  27. package/lib/cjs/builtin/async/ai-runtime/components/runtime/contexts/CurrentAgent.js +27 -0
  28. package/lib/cjs/builtin/async/ai-runtime/components/runtime/contexts/CurrentMessage.js +19 -0
  29. package/lib/cjs/builtin/async/ai-runtime/components/runtime/index.js +42 -0
  30. package/lib/cjs/builtin/async/ai-runtime/components/runtime/state/assistant.js +61 -0
  31. package/lib/cjs/builtin/async/ai-runtime/components/runtime/state/runtime.js +56 -0
  32. package/lib/cjs/builtin/async/ai-runtime/components/runtime/state/session.js +213 -0
  33. package/lib/cjs/builtin/async/ai-runtime/components/runtime/utils/map-right.js +10 -0
  34. package/lib/cjs/builtin/async/ai-runtime/components/runtime-chat/InitialForm.js +42 -20
  35. package/lib/cjs/builtin/async/ai-runtime/components/runtime-chat/RuntimeChatSession.js +12 -5
  36. package/lib/cjs/builtin/async/ai-runtime/components/runtime-form/RuntimeFormForms.js +5 -6
  37. package/lib/cjs/builtin/async/ai-runtime/components/runtime-form/RuntimeFormResult.js +3 -6
  38. package/lib/cjs/builtin/async/ai-runtime/components/runtime-form/index.js +4 -0
  39. package/lib/cjs/builtin/async/ai-runtime/components/session/CheckSession.js +16 -7
  40. package/lib/cjs/builtin/async/ai-runtime/hooks/auth.js +31 -0
  41. package/lib/cjs/builtin/async/ai-runtime/hooks/header.js +29 -19
  42. package/lib/cjs/builtin/async/ai-runtime/hooks/subscribe.js +6 -4
  43. package/lib/cjs/builtin/async/ai-runtime/index.js +38 -30
  44. package/lib/cjs/builtin/async/ai-runtime/locales/index.js +28 -0
  45. package/lib/cjs/builtin/async/ai-runtime/state/session.js +15 -18
  46. package/lib/cjs/builtin/async/ai-runtime/state/subscription.js +4 -4
  47. package/lib/cjs/components/CustomComponentRenderer/index.js +0 -1
  48. package/lib/cjs/components/CustomComponentRenderer/state.js +64 -57
  49. package/lib/cjs/components/index.js +1 -0
  50. package/lib/esm/builtin/async/ai-runtime/api/assistant.js +2 -1
  51. package/lib/esm/builtin/async/ai-runtime/api/message.js +14 -1
  52. package/lib/esm/builtin/async/ai-runtime/api/request.js +5 -3
  53. package/lib/esm/builtin/async/ai-runtime/api/session.js +2 -2
  54. package/lib/esm/builtin/async/ai-runtime/components/PoweredBy.js +2 -10
  55. package/lib/esm/builtin/async/ai-runtime/components/RuntimeProvider.js +1 -1
  56. package/lib/esm/builtin/async/ai-runtime/components/ThemeProvider.js +2 -2
  57. package/lib/esm/builtin/async/ai-runtime/components/form/ParameterForm.js +4 -4
  58. package/lib/esm/builtin/async/ai-runtime/components/form/ParameterFormFields.js +5 -1
  59. package/lib/esm/builtin/async/ai-runtime/components/form/QuestionField.js +2 -2
  60. package/lib/esm/builtin/async/ai-runtime/components/form/SettingButton.js +16 -9
  61. package/lib/esm/builtin/async/ai-runtime/components/message/MessageErrorView.js +14 -3
  62. package/lib/esm/builtin/async/ai-runtime/components/message/MessageItemView.js +65 -31
  63. package/lib/esm/builtin/async/ai-runtime/components/message/MessageListView.js +6 -5
  64. package/lib/esm/builtin/async/ai-runtime/components/parameters/RadioField.js +37 -0
  65. package/lib/esm/builtin/async/ai-runtime/components/parameters/SelectField.js +1 -1
  66. package/lib/esm/builtin/async/ai-runtime/components/parameters/index.js +5 -1
  67. package/lib/esm/builtin/async/ai-runtime/components/runtime/components/AutoForm/index.js +146 -0
  68. package/lib/esm/builtin/async/ai-runtime/components/runtime/components/ChatOutput/MessageErrorView.js +13 -0
  69. package/lib/esm/builtin/async/ai-runtime/components/runtime/components/ChatOutput/MessageItemView.js +149 -0
  70. package/lib/esm/builtin/async/ai-runtime/components/runtime/components/ChatOutput/index.js +7 -0
  71. package/lib/esm/builtin/async/ai-runtime/components/runtime/components/PhotoGallery/index.js +100 -0
  72. package/lib/esm/builtin/async/ai-runtime/components/runtime/components/PhotoGalleryItem/index.js +114 -0
  73. package/lib/esm/builtin/async/ai-runtime/components/runtime/components/SimpleChat/index.js +80 -0
  74. package/lib/esm/builtin/async/ai-runtime/components/runtime/components/SimpleOutput/index.js +11 -0
  75. package/lib/esm/builtin/async/ai-runtime/components/runtime/components/SimplePage/index.js +58 -0
  76. package/lib/esm/builtin/async/ai-runtime/components/runtime/contexts/CurrentAgent.js +22 -0
  77. package/lib/esm/builtin/async/ai-runtime/components/runtime/contexts/CurrentMessage.js +14 -0
  78. package/lib/esm/builtin/async/ai-runtime/components/runtime/index.js +36 -0
  79. package/lib/esm/builtin/async/ai-runtime/components/runtime/state/assistant.js +56 -0
  80. package/lib/esm/builtin/async/ai-runtime/components/runtime/state/runtime.js +52 -0
  81. package/lib/esm/builtin/async/ai-runtime/components/runtime/state/session.js +209 -0
  82. package/lib/esm/builtin/async/ai-runtime/components/runtime/utils/map-right.js +7 -0
  83. package/lib/esm/builtin/async/ai-runtime/components/runtime-chat/InitialForm.js +21 -22
  84. package/lib/esm/builtin/async/ai-runtime/components/runtime-chat/RuntimeChatSession.js +13 -6
  85. package/lib/esm/builtin/async/ai-runtime/components/runtime-form/RuntimeFormForms.js +5 -6
  86. package/lib/esm/builtin/async/ai-runtime/components/runtime-form/RuntimeFormResult.js +3 -6
  87. package/lib/esm/builtin/async/ai-runtime/components/runtime-form/index.js +4 -0
  88. package/lib/esm/builtin/async/ai-runtime/components/session/CheckSession.js +9 -0
  89. package/lib/esm/builtin/async/ai-runtime/hooks/auth.js +27 -0
  90. package/lib/esm/builtin/async/ai-runtime/hooks/header.js +30 -20
  91. package/lib/esm/builtin/async/ai-runtime/hooks/subscribe.js +6 -4
  92. package/lib/esm/builtin/async/ai-runtime/index.js +15 -29
  93. package/lib/esm/builtin/async/ai-runtime/locales/index.js +28 -0
  94. package/lib/esm/builtin/async/ai-runtime/state/session.js +17 -20
  95. package/lib/esm/builtin/async/ai-runtime/state/subscription.js +2 -2
  96. package/lib/esm/components/CustomComponentRenderer/index.js +0 -1
  97. package/lib/esm/components/CustomComponentRenderer/state.js +63 -57
  98. package/lib/esm/components/index.js +1 -0
  99. package/lib/types/builtin/async/ai-runtime/api/assistant.d.ts +4 -2
  100. package/lib/types/builtin/async/ai-runtime/api/message.d.ts +9 -2
  101. package/lib/types/builtin/async/ai-runtime/api/request.d.ts +4 -1
  102. package/lib/types/builtin/async/ai-runtime/api/session.d.ts +2 -1
  103. package/lib/types/builtin/async/ai-runtime/components/form/ParameterForm.d.ts +3 -2
  104. package/lib/types/builtin/async/ai-runtime/components/form/QuestionField.d.ts +3 -2
  105. package/lib/types/builtin/async/ai-runtime/components/form/SettingButton.d.ts +5 -2
  106. package/lib/types/builtin/async/ai-runtime/components/message/MessageErrorView.d.ts +4 -5
  107. package/lib/types/builtin/async/ai-runtime/components/message/MessageItemView.d.ts +11 -4
  108. package/lib/types/builtin/async/ai-runtime/components/message/MessageListView.d.ts +5 -3
  109. package/lib/types/builtin/async/ai-runtime/components/parameters/RadioField.d.ts +9 -0
  110. package/lib/types/builtin/async/ai-runtime/components/runtime/components/AutoForm/index.d.ts +9 -0
  111. package/lib/types/builtin/async/ai-runtime/components/runtime/components/ChatOutput/MessageErrorView.d.ts +3 -0
  112. package/lib/types/builtin/async/ai-runtime/components/runtime/components/ChatOutput/MessageItemView.d.ts +12 -0
  113. package/lib/types/builtin/async/ai-runtime/components/runtime/components/ChatOutput/index.d.ts +1 -0
  114. package/lib/types/builtin/async/ai-runtime/components/runtime/components/PhotoGallery/index.d.ts +5 -0
  115. package/lib/types/builtin/async/ai-runtime/components/runtime/components/PhotoGalleryItem/index.d.ts +1 -0
  116. package/lib/types/builtin/async/ai-runtime/components/runtime/components/SimpleChat/index.d.ts +3 -0
  117. package/lib/types/builtin/async/ai-runtime/components/runtime/components/SimpleOutput/index.d.ts +1 -0
  118. package/lib/types/builtin/async/ai-runtime/components/runtime/components/SimplePage/index.d.ts +4 -0
  119. package/lib/types/builtin/async/ai-runtime/components/runtime/contexts/CurrentAgent.d.ts +12 -0
  120. package/lib/types/builtin/async/ai-runtime/components/runtime/contexts/CurrentMessage.d.ts +10 -0
  121. package/lib/types/builtin/async/ai-runtime/components/runtime/index.d.ts +1 -0
  122. package/lib/types/builtin/async/ai-runtime/components/runtime/state/assistant.d.ts +17 -0
  123. package/lib/types/builtin/async/ai-runtime/components/runtime/state/runtime.d.ts +14 -0
  124. package/lib/types/builtin/async/ai-runtime/components/runtime/state/session.d.ts +29 -0
  125. package/lib/types/builtin/async/ai-runtime/components/runtime/utils/map-right.d.ts +1 -0
  126. package/lib/types/builtin/async/ai-runtime/hooks/auth.d.ts +4 -0
  127. package/lib/types/builtin/async/ai-runtime/hooks/subscribe.d.ts +1 -0
  128. package/lib/types/builtin/async/ai-runtime/index.d.ts +15 -8
  129. package/lib/types/builtin/async/ai-runtime/locales/index.d.ts +22 -0
  130. package/lib/types/builtin/async/ai-runtime/state/assistant.d.ts +0 -2
  131. package/lib/types/builtin/async/ai-runtime/state/session.d.ts +0 -3
  132. package/lib/types/builtin/async/ai-runtime/state/subscription.d.ts +1 -1
  133. package/lib/types/components/CustomComponentRenderer/index.d.ts +0 -1
  134. package/lib/types/components/CustomComponentRenderer/state.d.ts +2 -3
  135. package/lib/types/components/index.d.ts +1 -0
  136. package/lib/types/types/core.d.ts +1 -0
  137. package/package.json +9 -7
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ const jsx_runtime_1 = require("react/jsx-runtime");
30
+ const types_1 = require("@blocklet/ai-runtime/types");
31
+ const material_1 = require("@mui/material");
32
+ const react_1 = require("react");
33
+ const react_wrap_balancer_1 = __importStar(require("react-wrap-balancer"));
34
+ const CustomComponentRenderer_1 = __importDefault(require("../../../../../../../components/CustomComponentRenderer"));
35
+ const common_1 = require("../../../common");
36
+ const ScrollView_1 = __importStar(require("../../../ScrollView"));
37
+ const CurrentAgent_1 = __importStar(require("../../contexts/CurrentAgent"));
38
+ const CurrentMessage_1 = __importStar(require("../../contexts/CurrentMessage"));
39
+ const runtime_1 = require("../../state/runtime");
40
+ function SimpleChat({ primaryColor }) {
41
+ const inheritedTheme = (0, material_1.useTheme)();
42
+ const theme = (0, react_1.useMemo)(() => {
43
+ let { primary } = inheritedTheme.palette;
44
+ try {
45
+ if (primaryColor) {
46
+ primary = inheritedTheme.palette.augmentColor({ color: { main: primaryColor } });
47
+ }
48
+ }
49
+ catch (error) {
50
+ console.error('augment primary color error', { error });
51
+ }
52
+ return (0, material_1.createTheme)(inheritedTheme, {
53
+ palette: { primary },
54
+ shape: {
55
+ borderRadius: 8,
56
+ },
57
+ });
58
+ }, [inheritedTheme, primaryColor]);
59
+ return ((0, jsx_runtime_1.jsx)(material_1.ThemeProvider, { theme: theme, children: (0, jsx_runtime_1.jsx)(ScrollView_1.default, { scroller: "window", initialScrollBehavior: "auto", children: (0, jsx_runtime_1.jsx)(SimpleChatView, {}) }) }));
60
+ }
61
+ exports.default = SimpleChat;
62
+ function SimpleChatView() {
63
+ var _a;
64
+ const { agent, appearancePage, sessionState } = (0, runtime_1.useRuntimeState)();
65
+ const scrollToBottom = (0, ScrollView_1.useScrollToBottomModule)().useScrollToBottom();
66
+ (0, react_1.useEffect)(() => {
67
+ if (sessionState.running)
68
+ scrollToBottom();
69
+ }, [scrollToBottom, sessionState.running]);
70
+ const children = (0, react_1.useMemo)(() => {
71
+ var _a, _b;
72
+ return (_b = (_a = agent.outputVariables) === null || _a === void 0 ? void 0 : _a.find((i) => i.name === types_1.RuntimeOutputVariable.children)) === null || _b === void 0 ? void 0 : _b.initialValue;
73
+ }, [agent]);
74
+ return ((0, jsx_runtime_1.jsxs)(material_1.Stack, { flexGrow: 1, maxWidth: "md", width: "100%", mx: "auto", px: { xs: 2, sm: 3 }, children: [(0, jsx_runtime_1.jsx)(material_1.Stack, { gap: 2, mt: 8, mb: 4, children: (0, jsx_runtime_1.jsxs)(react_wrap_balancer_1.Provider, { children: [(appearancePage === null || appearancePage === void 0 ? void 0 : appearancePage.name) && ((0, jsx_runtime_1.jsx)(material_1.Typography, { component: "h5", fontSize: 30, fontWeight: 700, textAlign: "center", children: (0, jsx_runtime_1.jsx)(react_wrap_balancer_1.default, { children: appearancePage.name }) })), (appearancePage === null || appearancePage === void 0 ? void 0 : appearancePage.description) && ((0, jsx_runtime_1.jsx)(material_1.Typography, { textAlign: "center", children: (0, jsx_runtime_1.jsx)(react_wrap_balancer_1.default, { children: appearancePage.description }) }))] }) }), (0, jsx_runtime_1.jsx)(material_1.Stack, { children: (0, jsx_runtime_1.jsx)(OutputView, {}) }), (0, jsx_runtime_1.jsx)(material_1.Box, { flexGrow: 1, minHeight: 200 }), (0, jsx_runtime_1.jsx)(material_1.Stack, { sx: { position: 'sticky', bottom: 0, py: 1, bgcolor: 'background.paper', zIndex: 10 }, children: (_a = children === null || children === void 0 ? void 0 : children.agents) === null || _a === void 0 ? void 0 : _a.map((i) => ((0, jsx_runtime_1.jsx)(CurrentAgent_1.default, { agentId: i.id, children: (0, jsx_runtime_1.jsx)(AgentInputRender, {}) }, i.id))) })] }));
75
+ }
76
+ function AgentInputRender() {
77
+ var _a, _b;
78
+ const { agent } = (0, CurrentAgent_1.useCurrentAgent)();
79
+ const input = (_b = (_a = agent.outputVariables) === null || _a === void 0 ? void 0 : _a.find((i) => i.name === types_1.RuntimeOutputVariable.appearanceInput)) === null || _b === void 0 ? void 0 : _b.initialValue;
80
+ if (!(input === null || input === void 0 ? void 0 : input.componentId))
81
+ return null;
82
+ return ((0, jsx_runtime_1.jsx)(material_1.Stack, { children: (0, jsx_runtime_1.jsx)(CustomComponentRenderer_1.default, { componentId: input.componentId, properties: input.componentProps }) }));
83
+ }
84
+ function OutputView() {
85
+ var _a, _b;
86
+ const { sessionState } = (0, runtime_1.useRuntimeState)();
87
+ const lastMessage = (_a = sessionState.messages) === null || _a === void 0 ? void 0 : _a.slice(-1)[0];
88
+ return ((0, jsx_runtime_1.jsxs)(material_1.Stack, { gap: 2, children: [(_b = sessionState.messages) === null || _b === void 0 ? void 0 : _b.map((message) => ((0, jsx_runtime_1.jsx)(material_1.Stack, { children: (0, jsx_runtime_1.jsx)(CurrentAgent_1.default, { agentId: message.assistantId, children: (0, jsx_runtime_1.jsx)(CurrentMessage_1.default, { message: message, children: (0, jsx_runtime_1.jsx)(react_1.Suspense, { children: (0, jsx_runtime_1.jsx)(OutputItemView, {}) }) }) }) }))), lastMessage && ((0, jsx_runtime_1.jsx)(CurrentAgent_1.default, { agentId: lastMessage.assistantId, children: (0, jsx_runtime_1.jsx)(CurrentMessage_1.default, { message: lastMessage, children: (0, jsx_runtime_1.jsx)(SuggestedQuestionsView, {}) }) }))] }));
89
+ }
90
+ function OutputItemView() {
91
+ var _a, _b;
92
+ const { agent } = (0, CurrentAgent_1.useCurrentAgent)();
93
+ const input = (_b = (_a = agent.outputVariables) === null || _a === void 0 ? void 0 : _a.find((i) => i.name === types_1.RuntimeOutputVariable.appearanceOutput)) === null || _b === void 0 ? void 0 : _b.initialValue;
94
+ if (!(input === null || input === void 0 ? void 0 : input.componentId))
95
+ return null;
96
+ return (0, jsx_runtime_1.jsx)(CustomComponentRenderer_1.default, { componentId: input.componentId, properties: input.componentProps });
97
+ }
98
+ function SuggestedQuestionsView() {
99
+ const { execute } = (0, runtime_1.useRuntimeState)();
100
+ const { aid } = (0, CurrentAgent_1.useCurrentAgent)();
101
+ const { message } = (0, CurrentMessage_1.useCurrentMessage)();
102
+ const suggestedQuestions = (0, react_1.useMemo)(() => {
103
+ var _a, _b, _c;
104
+ return (_c = (_b = (_a = message === null || message === void 0 ? void 0 : message.result) === null || _a === void 0 ? void 0 : _a.objects) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.data[types_1.RuntimeOutputVariable.suggestedQuestions];
105
+ }, [message]);
106
+ return ((0, jsx_runtime_1.jsx)(material_1.Box, { ml: 6.5, mr: 2.5, children: (suggestedQuestions === null || suggestedQuestions === void 0 ? void 0 : suggestedQuestions.length) ? ((0, jsx_runtime_1.jsx)(common_1.MessageSuggestedQuestions, { dataSource: suggestedQuestions, onClick: (item) => {
107
+ execute({ aid, parameters: Object.assign(Object.assign({}, message.parameters), { question: item.question }) });
108
+ } })) : null }));
109
+ }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const jsx_runtime_1 = require("react/jsx-runtime");
7
+ const material_1 = require("@mui/material");
8
+ const common_1 = require("../../../common");
9
+ const MarkdownRenderer_1 = __importDefault(require("../../../MarkdownRenderer"));
10
+ const CurrentMessage_1 = require("../../contexts/CurrentMessage");
11
+ function BaseOutputs() {
12
+ var _a, _b;
13
+ const { message } = (0, CurrentMessage_1.useCurrentMessage)();
14
+ const objects = (_a = message.result) === null || _a === void 0 ? void 0 : _a.objects;
15
+ return ((0, jsx_runtime_1.jsxs)(material_1.Stack, { gap: 2, boxShadow: 1, borderRadius: 1, p: 2, children: [(0, jsx_runtime_1.jsx)(MarkdownRenderer_1.default, { children: (_b = message.result) === null || _b === void 0 ? void 0 : _b.content }), objects === null || objects === void 0 ? void 0 : objects.map((item) => (0, jsx_runtime_1.jsx)(common_1.MessageMetadataRenderer, { object: item.data }))] }));
16
+ }
17
+ exports.default = BaseOutputs;
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ const jsx_runtime_1 = require("react/jsx-runtime");
30
+ const types_1 = require("@blocklet/ai-runtime/types");
31
+ const material_1 = require("@mui/material");
32
+ const react_1 = require("react");
33
+ const react_wrap_balancer_1 = __importStar(require("react-wrap-balancer"));
34
+ const CustomComponentRenderer_1 = __importDefault(require("../../../../../../../components/CustomComponentRenderer"));
35
+ const CurrentAgent_1 = __importStar(require("../../contexts/CurrentAgent"));
36
+ const CurrentMessage_1 = __importDefault(require("../../contexts/CurrentMessage"));
37
+ const runtime_1 = require("../../state/runtime");
38
+ function SimplePage({ resultTitle, primaryColor }) {
39
+ var _a;
40
+ const { agent, appearancePage } = (0, runtime_1.useRuntimeState)();
41
+ const children = (0, react_1.useMemo)(() => {
42
+ var _a, _b;
43
+ return (_b = (_a = agent.outputVariables) === null || _a === void 0 ? void 0 : _a.find((i) => i.name === types_1.RuntimeOutputVariable.children)) === null || _b === void 0 ? void 0 : _b.initialValue;
44
+ }, [agent]);
45
+ const inheritedTheme = (0, material_1.useTheme)();
46
+ const theme = (0, react_1.useMemo)(() => {
47
+ let { primary } = inheritedTheme.palette;
48
+ try {
49
+ if (primaryColor) {
50
+ primary = inheritedTheme.palette.augmentColor({ color: { main: primaryColor } });
51
+ }
52
+ }
53
+ catch (error) {
54
+ console.error('augment primary color error', { error });
55
+ }
56
+ return (0, material_1.createTheme)(inheritedTheme, {
57
+ palette: { primary },
58
+ shape: {
59
+ borderRadius: 8,
60
+ },
61
+ });
62
+ }, [inheritedTheme, primaryColor]);
63
+ return ((0, jsx_runtime_1.jsx)(material_1.ThemeProvider, { theme: theme, children: (0, jsx_runtime_1.jsxs)(material_1.Stack, { gap: 2, maxWidth: "md", width: "100%", mx: "auto", px: { xs: 2, sm: 3 }, children: [(0, jsx_runtime_1.jsx)(material_1.Stack, { gap: 2, mt: 8, mb: 4, children: (0, jsx_runtime_1.jsxs)(react_wrap_balancer_1.Provider, { children: [(appearancePage === null || appearancePage === void 0 ? void 0 : appearancePage.name) && ((0, jsx_runtime_1.jsx)(material_1.Typography, { component: "h5", fontSize: 30, fontWeight: 700, textAlign: "center", children: (0, jsx_runtime_1.jsx)(react_wrap_balancer_1.default, { children: appearancePage.name }) })), (appearancePage === null || appearancePage === void 0 ? void 0 : appearancePage.description) && ((0, jsx_runtime_1.jsx)(material_1.Typography, { textAlign: "center", children: (0, jsx_runtime_1.jsx)(react_wrap_balancer_1.default, { children: appearancePage.description }) }))] }) }), (0, jsx_runtime_1.jsx)(material_1.Stack, { children: (_a = children === null || children === void 0 ? void 0 : children.agents) === null || _a === void 0 ? void 0 : _a.map((i) => ((0, jsx_runtime_1.jsx)(CurrentAgent_1.default, { agentId: i.id, children: (0, jsx_runtime_1.jsx)(AgentInputRender, {}) }, i.id))) }), (0, jsx_runtime_1.jsx)(material_1.Stack, { children: (0, jsx_runtime_1.jsx)(OutputView, { resultTitle: resultTitle }) })] }) }));
64
+ }
65
+ exports.default = SimplePage;
66
+ function AgentInputRender() {
67
+ var _a, _b;
68
+ const { agent } = (0, CurrentAgent_1.useCurrentAgent)();
69
+ const input = (_b = (_a = agent.outputVariables) === null || _a === void 0 ? void 0 : _a.find((i) => i.name === types_1.RuntimeOutputVariable.appearanceInput)) === null || _b === void 0 ? void 0 : _b.initialValue;
70
+ if (!(input === null || input === void 0 ? void 0 : input.componentId))
71
+ return null;
72
+ return ((0, jsx_runtime_1.jsx)(material_1.Stack, { children: (0, jsx_runtime_1.jsx)(CustomComponentRenderer_1.default, { componentId: input.componentId, properties: input.componentProps }) }));
73
+ }
74
+ function OutputView({ resultTitle }) {
75
+ var _a;
76
+ const { sessionState } = (0, runtime_1.useRuntimeState)();
77
+ const lastMessage = (_a = sessionState.messages) === null || _a === void 0 ? void 0 : _a.slice(-1)[0];
78
+ return ((0, jsx_runtime_1.jsx)(material_1.Stack, { gap: 2, children: lastMessage && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(material_1.Stack, { children: resultTitle && ((0, jsx_runtime_1.jsx)(material_1.Typography, { component: "h5", fontSize: 36, fontWeight: 700, textAlign: "center", children: (0, jsx_runtime_1.jsx)(react_wrap_balancer_1.default, { children: resultTitle }) })) }), (0, jsx_runtime_1.jsx)(material_1.Stack, { children: (0, jsx_runtime_1.jsx)(CurrentAgent_1.default, { agentId: lastMessage.assistantId, children: (0, jsx_runtime_1.jsx)(CurrentMessage_1.default, { message: lastMessage, children: (0, jsx_runtime_1.jsx)(react_1.Suspense, { children: (0, jsx_runtime_1.jsx)(OutputItemView, {}) }) }) }) })] })) }));
79
+ }
80
+ function OutputItemView() {
81
+ var _a, _b;
82
+ const { agent } = (0, CurrentAgent_1.useCurrentAgent)();
83
+ const input = (_b = (_a = agent.outputVariables) === null || _a === void 0 ? void 0 : _a.find((i) => i.name === types_1.RuntimeOutputVariable.appearanceOutput)) === null || _b === void 0 ? void 0 : _b.initialValue;
84
+ if (!(input === null || input === void 0 ? void 0 : input.componentId))
85
+ return null;
86
+ return (0, jsx_runtime_1.jsx)(CustomComponentRenderer_1.default, { componentId: input.componentId, properties: input.componentProps });
87
+ }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useCurrentAgent = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const react_1 = require("react");
6
+ const utils_1 = require("../../../utils");
7
+ const assistant_1 = require("../state/assistant");
8
+ const runtime_1 = require("../state/runtime");
9
+ const context = (0, react_1.createContext)(undefined);
10
+ function CurrentAgentProvider({ agentId, children }) {
11
+ const { aid } = (0, runtime_1.useRuntimeState)();
12
+ const state = (0, react_1.useMemo)(() => ({
13
+ aid: (0, utils_1.stringifyIdentity)(Object.assign(Object.assign({}, (0, utils_1.parseIdentity)(aid, { rejectWhenError: true })), { assistantId: agentId })),
14
+ }), [aid, agentId]);
15
+ return (0, jsx_runtime_1.jsx)(context.Provider, { value: state, children: children });
16
+ }
17
+ exports.default = CurrentAgentProvider;
18
+ function useCurrentAgent() {
19
+ const { working } = (0, runtime_1.useRuntimeState)();
20
+ const current = (0, react_1.useContext)(context);
21
+ if (!current) {
22
+ throw new Error('No such current agent state. You should use `useCurrentAgent` within the `CurrentAgentProvider`');
23
+ }
24
+ const [agent] = (0, assistant_1.useAssistantState)({ aid: current.aid, working });
25
+ return Object.assign(Object.assign({}, current), { agent });
26
+ }
27
+ exports.useCurrentAgent = useCurrentAgent;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useCurrentMessage = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const react_1 = require("react");
6
+ const context = (0, react_1.createContext)(undefined);
7
+ function CurrentMessageProvider({ message, children }) {
8
+ const state = (0, react_1.useMemo)(() => ({ message }), [message]);
9
+ return (0, jsx_runtime_1.jsx)(context.Provider, { value: state, children: children });
10
+ }
11
+ exports.default = CurrentMessageProvider;
12
+ function useCurrentMessage() {
13
+ const current = (0, react_1.useContext)(context);
14
+ if (!current) {
15
+ throw new Error('No such message state. You should use `useCurrentMessage` within the `CurrentMessageProvider`');
16
+ }
17
+ return current;
18
+ }
19
+ exports.useCurrentMessage = useCurrentMessage;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const jsx_runtime_1 = require("react/jsx-runtime");
7
+ const types_1 = require("@blocklet/ai-runtime/types");
8
+ const material_1 = require("@mui/material");
9
+ const react_1 = require("react");
10
+ const react_helmet_1 = require("react-helmet");
11
+ const CustomComponentRenderer_1 = __importDefault(require("../../../../../components/CustomComponentRenderer"));
12
+ const ux_1 = require("../../../../arcblock/ux");
13
+ const header_1 = require("../../../../page/header");
14
+ const RuntimeProvider_1 = __importDefault(require("../RuntimeProvider"));
15
+ const runtime_1 = require("./state/runtime");
16
+ function Runtime() {
17
+ var _a, _b;
18
+ const { agent, appearancePage, sessionState } = (0, runtime_1.useRuntimeState)();
19
+ (0, react_1.useEffect)(() => {
20
+ header_1.useHeaderState.setState((state) => {
21
+ var _a;
22
+ const logo = (_a = appearancePage === null || appearancePage === void 0 ? void 0 : appearancePage.logo) === null || _a === void 0 ? void 0 : _a.url;
23
+ state.logo = logo && (0, jsx_runtime_1.jsx)(material_1.Box, { component: ux_1.Avatar, src: logo, did: blocklet === null || blocklet === void 0 ? void 0 : blocklet.appId, borderRadius: 1, size: 44 });
24
+ state.brand = appearancePage === null || appearancePage === void 0 ? void 0 : appearancePage.name;
25
+ });
26
+ return () => {
27
+ header_1.useHeaderState.setState((state) => {
28
+ state.logo = undefined;
29
+ state.brand = undefined;
30
+ });
31
+ };
32
+ }, [appearancePage]);
33
+ (0, react_1.useEffect)(() => {
34
+ sessionState.load();
35
+ }, []);
36
+ const appearance = (_b = (_a = agent.outputVariables) === null || _a === void 0 ? void 0 : _a.find((i) => i.name === types_1.RuntimeOutputVariable.appearancePage)) === null || _b === void 0 ? void 0 : _b.initialValue;
37
+ const componentId = appearance === null || appearance === void 0 ? void 0 : appearance.componentId;
38
+ if (!componentId)
39
+ throw new Error('Missing output `appearance`');
40
+ return ((0, jsx_runtime_1.jsxs)(RuntimeProvider_1.default, { children: [appearancePage && ((0, jsx_runtime_1.jsxs)(react_helmet_1.Helmet, { children: [appearancePage.name && (0, jsx_runtime_1.jsx)("title", { children: appearancePage.name }), appearancePage.description && (0, jsx_runtime_1.jsx)("meta", { name: "description", content: appearancePage.description })] })), (0, jsx_runtime_1.jsx)(CustomComponentRenderer_1.default, { componentId: componentId, properties: appearance.componentProps })] }));
41
+ }
42
+ exports.default = Runtime;
@@ -0,0 +1,61 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.useAssistantState = exports.createAssistantState = void 0;
13
+ const zustand_1 = require("zustand");
14
+ const immer_1 = require("zustand/middleware/immer");
15
+ const assistant_1 = require("../../../api/assistant");
16
+ const STATES = {};
17
+ const createAssistantState = ({ aid, working, assistant, }) => {
18
+ var _a;
19
+ const key = working ? `${aid}-working` : aid;
20
+ (_a = STATES[key]) !== null && _a !== void 0 ? _a : (STATES[key] = (0, zustand_1.create)()((0, immer_1.immer)((set) => ({
21
+ assistant,
22
+ load: () => __awaiter(void 0, void 0, void 0, function* () {
23
+ set((state) => {
24
+ state.loading = true;
25
+ });
26
+ try {
27
+ const assistant = yield (0, assistant_1.getAssistant)({ assistantId: aid, working });
28
+ set((state) => {
29
+ state.assistant = assistant;
30
+ });
31
+ }
32
+ catch (error) {
33
+ console.error(error);
34
+ set((state) => {
35
+ state.error = error;
36
+ });
37
+ }
38
+ finally {
39
+ set((state) => {
40
+ state.loading = false;
41
+ });
42
+ }
43
+ }),
44
+ }))));
45
+ return STATES[key];
46
+ };
47
+ exports.createAssistantState = createAssistantState;
48
+ const LOADING_TASKS = {};
49
+ function useAssistantState({ aid, working }) {
50
+ var _a;
51
+ const state = (0, exports.createAssistantState)({ aid, working })();
52
+ if (!state.assistant) {
53
+ if (state.error)
54
+ throw state.error;
55
+ const key = working ? `${aid}-working` : aid;
56
+ (_a = LOADING_TASKS[key]) !== null && _a !== void 0 ? _a : (LOADING_TASKS[key] = state.load());
57
+ throw LOADING_TASKS[key];
58
+ }
59
+ return [state.assistant, state];
60
+ }
61
+ exports.useAssistantState = useAssistantState;
@@ -0,0 +1,56 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.useRuntimeState = void 0;
13
+ const types_1 = require("@blocklet/ai-runtime/types");
14
+ const react_1 = require("react");
15
+ const react_router_dom_1 = require("react-router-dom");
16
+ const session_1 = require("../../../../../session");
17
+ const error_1 = require("../../../error");
18
+ const assistant_1 = require("./assistant");
19
+ const session_2 = require("./session");
20
+ function useRuntimeState() {
21
+ var _a;
22
+ const [query] = (0, react_router_dom_1.useSearchParams)();
23
+ const aid = query.get('aid');
24
+ if (!aid)
25
+ throw new error_1.CustomError(404, 'Missing required query parameters `aid`');
26
+ const working = query.get('working') === 'true';
27
+ const [agent] = (0, assistant_1.useAssistantState)({ aid, working });
28
+ const sessionState = (0, session_2.createSessionState)({ aid })();
29
+ const { session: authSession } = (0, session_1.useSessionContext)();
30
+ // reset state after logged out
31
+ (0, react_1.useEffect)(() => {
32
+ var _a;
33
+ if (!((_a = authSession.user) === null || _a === void 0 ? void 0 : _a.did)) {
34
+ sessionState.reset();
35
+ }
36
+ }, [(_a = authSession.user) === null || _a === void 0 ? void 0 : _a.did]);
37
+ const login = (0, react_1.useCallback)(() => __awaiter(this, void 0, void 0, function* () {
38
+ yield new Promise((resolve) => {
39
+ authSession.login(() => resolve());
40
+ });
41
+ yield Promise.all([sessionState.load()]);
42
+ }), [aid, authSession, sessionState]);
43
+ const execute = (0, react_1.useCallback)((args) => __awaiter(this, void 0, void 0, function* () {
44
+ if (!authSession.user) {
45
+ yield login();
46
+ }
47
+ return sessionState.execute(Object.assign(Object.assign({}, args), { working }));
48
+ }), [authSession.user, login, sessionState]);
49
+ const appearancePage = (0, react_1.useMemo)(() => {
50
+ var _a, _b;
51
+ const initialValue = (_b = (_a = agent.outputVariables) === null || _a === void 0 ? void 0 : _a.find((i) => i.name === types_1.RuntimeOutputVariable.appearancePage)) === null || _b === void 0 ? void 0 : _b.initialValue;
52
+ return initialValue;
53
+ }, [agent]);
54
+ return { aid, working, agent, appearancePage, sessionState, execute };
55
+ }
56
+ exports.useRuntimeState = useRuntimeState;
@@ -0,0 +1,213 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.createSessionState = void 0;
20
+ const zustand_1 = require("zustand");
21
+ const immer_1 = require("zustand/middleware/immer");
22
+ const message_1 = require("../../../api/message");
23
+ const session_1 = require("../../../api/session");
24
+ const utils_1 = require("../../../utils");
25
+ const STATES = {};
26
+ const GET_MESSAGES_LIMIT = 100;
27
+ const GET_MESSAGES_ORDER_DIRECTION = 'desc';
28
+ const createSessionState = ({ aid }) => {
29
+ var _a;
30
+ (_a = STATES[aid]) !== null && _a !== void 0 ? _a : (STATES[aid] = (0, zustand_1.create)()((0, immer_1.immer)((set, get) => ({
31
+ reset: () => {
32
+ set((state) => {
33
+ state.loading = undefined;
34
+ state.loaded = undefined;
35
+ state.running = undefined;
36
+ state.session = undefined;
37
+ state.messages = undefined;
38
+ state.noMoreMessage = undefined;
39
+ state.messageLoading = undefined;
40
+ state.error = undefined;
41
+ });
42
+ },
43
+ load: () => __awaiter(void 0, void 0, void 0, function* () {
44
+ var _b;
45
+ set((state) => {
46
+ state.loading = true;
47
+ });
48
+ try {
49
+ const { sessions } = yield (0, session_1.getSessions)((0, utils_1.parseIdentity)(aid, { rejectWhenError: true }));
50
+ const session = (_b = sessions[0]) !== null && _b !== void 0 ? _b : (yield (0, session_1.createSession)({ assistantId: aid })).created;
51
+ const { messages } = yield (0, message_1.getMessages)({
52
+ sessionId: session.id,
53
+ orderDirection: GET_MESSAGES_ORDER_DIRECTION,
54
+ limit: GET_MESSAGES_LIMIT,
55
+ });
56
+ set((state) => {
57
+ state.session = session;
58
+ state.messages = messages.toReversed();
59
+ state.noMoreMessage = messages.length < GET_MESSAGES_LIMIT;
60
+ });
61
+ }
62
+ catch (error) {
63
+ set((state) => {
64
+ state.error = error;
65
+ });
66
+ throw error;
67
+ }
68
+ finally {
69
+ set((state) => {
70
+ state.loaded = true;
71
+ state.loading = false;
72
+ });
73
+ }
74
+ }),
75
+ loadMoreMessages: () => __awaiter(void 0, void 0, void 0, function* () {
76
+ var _c;
77
+ set((state) => {
78
+ state.messageLoading = true;
79
+ });
80
+ const { session, messages } = get();
81
+ if (!session)
82
+ return;
83
+ try {
84
+ const result = yield (0, message_1.getMessages)({
85
+ sessionId: session.id,
86
+ before: (_c = messages === null || messages === void 0 ? void 0 : messages.slice(-1)[0]) === null || _c === void 0 ? void 0 : _c.id,
87
+ orderDirection: GET_MESSAGES_ORDER_DIRECTION,
88
+ limit: GET_MESSAGES_LIMIT,
89
+ });
90
+ set((state) => {
91
+ var _a;
92
+ state.messages = ((_a = state.messages) !== null && _a !== void 0 ? _a : []).concat(result.messages.toReversed());
93
+ state.noMoreMessage = result.messages.length < GET_MESSAGES_LIMIT;
94
+ });
95
+ }
96
+ finally {
97
+ set((state) => {
98
+ state.messageLoading = false;
99
+ });
100
+ }
101
+ }),
102
+ execute: (_d) => __awaiter(void 0, [_d], void 0, function* ({ aid, working, parameters, onResponseStart, }) {
103
+ var _e, e_1, _f, _g;
104
+ let { session } = get();
105
+ if (!session) {
106
+ yield get().load();
107
+ session = get().session;
108
+ }
109
+ if (!session)
110
+ throw new Error('This session is not initialized');
111
+ const { id: sessionId } = session;
112
+ const identity = (0, utils_1.parseIdentity)(aid, { rejectWhenError: true });
113
+ let message;
114
+ set((state) => {
115
+ state.running = true;
116
+ state.error = undefined;
117
+ });
118
+ try {
119
+ const stream = (0, session_1.runAssistant)({
120
+ sessionId,
121
+ identity,
122
+ working,
123
+ parameters: Object.assign(Object.assign({}, parameters), { $clientTime: new Date().toISOString() }),
124
+ });
125
+ let responseStarted = false;
126
+ try {
127
+ for (var _h = true, stream_1 = __asyncValues(stream), stream_1_1; stream_1_1 = yield stream_1.next(), _e = stream_1_1.done, !_e; _h = true) {
128
+ _g = stream_1_1.value;
129
+ _h = false;
130
+ const value = _g;
131
+ if (!responseStarted) {
132
+ responseStarted = true;
133
+ onResponseStart === null || onResponseStart === void 0 ? void 0 : onResponseStart();
134
+ }
135
+ if ((value === null || value === void 0 ? void 0 : value.type) === 'CHUNK') {
136
+ if (!message) {
137
+ message = {
138
+ taskId: value.taskId,
139
+ assistantId: identity.assistantId,
140
+ sessionId,
141
+ parameters,
142
+ createdAt: new Date().toISOString(),
143
+ updatedAt: new Date().toISOString(),
144
+ loading: true,
145
+ };
146
+ set((state) => {
147
+ var _a;
148
+ (_a = state.messages) !== null && _a !== void 0 ? _a : (state.messages = []);
149
+ state.messages.push(message);
150
+ });
151
+ }
152
+ if (message.taskId === value.taskId) {
153
+ requestAnimationFrame(() => {
154
+ set((state) => {
155
+ var _a, _b, _c, _d, _e, _f;
156
+ var _g;
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.content = (msg.result.content || '') + (value.delta.content || '');
162
+ if ((_d = (_c = value.delta) === null || _c === void 0 ? void 0 : _c.images) === null || _d === void 0 ? void 0 : _d.length) {
163
+ msg.result.images = ((_e = msg.result.images) !== null && _e !== void 0 ? _e : []).concat(value.delta.images);
164
+ }
165
+ if (value.delta.object) {
166
+ (_f = (_g = msg.result).objects) !== null && _f !== void 0 ? _f : (_g.objects = []);
167
+ msg.result.objects.push({ taskId: value.taskId, data: value.delta.object });
168
+ }
169
+ });
170
+ });
171
+ }
172
+ }
173
+ if ((value === null || value === void 0 ? void 0 : value.type) === 'ERROR') {
174
+ set((state) => {
175
+ var _a;
176
+ const msg = (_a = state.messages) === null || _a === void 0 ? void 0 : _a.findLast((i) => i.taskId === message.taskId);
177
+ if (msg)
178
+ msg.error = value.error;
179
+ else
180
+ throw new Error(value.error.message);
181
+ });
182
+ }
183
+ }
184
+ }
185
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
186
+ finally {
187
+ try {
188
+ if (!_h && !_e && (_f = stream_1.return)) yield _f.call(stream_1);
189
+ }
190
+ finally { if (e_1) throw e_1.error; }
191
+ }
192
+ }
193
+ catch (error) {
194
+ set((state) => {
195
+ state.error = error;
196
+ });
197
+ }
198
+ finally {
199
+ set((state) => {
200
+ var _a;
201
+ state.running = false;
202
+ if (message) {
203
+ const msg = (_a = state.messages) === null || _a === void 0 ? void 0 : _a.findLast((i) => i.taskId === message.taskId);
204
+ if (msg)
205
+ msg.loading = false;
206
+ }
207
+ });
208
+ }
209
+ }),
210
+ }))));
211
+ return STATES[aid];
212
+ };
213
+ exports.createSessionState = createSessionState;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function mapRight(array, fn) {
4
+ const result = new Array(array.length);
5
+ for (let i = array.length - 1; i >= 0; i--) {
6
+ result.push(fn(array[i], i, array));
7
+ }
8
+ return result;
9
+ }
10
+ exports.default = mapRight;