@blocklet/pages-kit 0.2.364 → 0.2.366

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 (30) hide show
  1. package/lib/cjs/builtin/async/ai-runtime/components/AgentErrorBoundary.js +2 -2
  2. package/lib/cjs/builtin/async/ai-runtime/contexts/ComponentPreferences.js +7 -2
  3. package/lib/cjs/builtin/async/ai-runtime/index.js +9 -1
  4. package/lib/cjs/builtin/async/ai-runtime/runtime-components/AutoForm/index.js +15 -2
  5. package/lib/cjs/builtin/async/ai-runtime/runtime-components/SimpleChat/MessageView.js +3 -3
  6. package/lib/cjs/builtin/async/ai-runtime/runtime-components/SimpleChat/index.js +25 -2
  7. package/lib/cjs/builtin/async/ai-runtime/runtime-components/SimpleOutput/index.js +12 -8
  8. package/lib/cjs/builtin/async/ai-runtime/runtime-components/SimplePage/index.js +4 -2
  9. package/lib/cjs/tsconfig.tsbuildinfo +1 -1
  10. package/lib/cjs/types/index.js +1 -0
  11. package/lib/cjs/utils/inject-global-components.js +2 -0
  12. package/lib/esm/builtin/async/ai-runtime/components/AgentErrorBoundary.js +2 -2
  13. package/lib/esm/builtin/async/ai-runtime/contexts/ComponentPreferences.js +4 -2
  14. package/lib/esm/builtin/async/ai-runtime/index.js +5 -0
  15. package/lib/esm/builtin/async/ai-runtime/runtime-components/AutoForm/index.js +15 -2
  16. package/lib/esm/builtin/async/ai-runtime/runtime-components/SimpleChat/MessageView.js +4 -4
  17. package/lib/esm/builtin/async/ai-runtime/runtime-components/SimpleChat/index.js +1 -1
  18. package/lib/esm/builtin/async/ai-runtime/runtime-components/SimpleOutput/index.js +14 -10
  19. package/lib/esm/builtin/async/ai-runtime/runtime-components/SimplePage/index.js +4 -2
  20. package/lib/esm/tsconfig.tsbuildinfo +1 -1
  21. package/lib/esm/types/index.js +1 -0
  22. package/lib/esm/utils/inject-global-components.js +2 -0
  23. package/lib/types/builtin/async/ai-runtime/components/AgentErrorBoundary.d.ts +4 -1
  24. package/lib/types/builtin/async/ai-runtime/contexts/ComponentPreferences.d.ts +9 -3
  25. package/lib/types/builtin/async/ai-runtime/index.d.ts +5 -0
  26. package/lib/types/builtin/async/ai-runtime/runtime-components/SimpleChat/index.d.ts +2 -2
  27. package/lib/types/builtin/async/ai-runtime/runtime-components/SimplePage/index.d.ts +5 -2
  28. package/lib/types/tsconfig.tsbuildinfo +1 -1
  29. package/lib/types/types/index.d.ts +1 -0
  30. package/package.json +11 -4
@@ -18,3 +18,4 @@ __exportStar(require("./api"), exports);
18
18
  __exportStar(require("./core"), exports);
19
19
  __exportStar(require("./state"), exports);
20
20
  __exportStar(require("./preload"), exports);
21
+ __exportStar(require("./builtin"), exports);
@@ -55,6 +55,8 @@ const CustomComponentRenderer_1 = __importDefault(require("../components/CustomC
55
55
  const builtin_1 = require("../types/builtin");
56
56
  function injectGlobalComponents() {
57
57
  const win = window;
58
+ if (win[builtin_1.BuiltinModulesGlobalVariableName])
59
+ return;
58
60
  win[builtin_1.BuiltinModulesGlobalVariableName] = {
59
61
  modules: {
60
62
  '@blocklet/pages-kit/builtin/pages-kit': { CustomComponentRenderer: CustomComponentRenderer_1.default },
@@ -7,8 +7,8 @@ import { useLocaleContext } from '../../../locale';
7
7
  import { useIsAgentAdmin } from '../hooks/use-agent-admin';
8
8
  import { useRuntimeState } from '../state/runtime';
9
9
  import { settingsDialogState } from './AgentSettings/AgentSettingsDialog';
10
- export default function AgentErrorBoundary() {
11
- return _jsx(ErrorBoundary, { FallbackComponent: AgentErrorView });
10
+ export default function AgentErrorBoundary({ children }) {
11
+ return _jsx(ErrorBoundary, { FallbackComponent: AgentErrorView, children: children });
12
12
  }
13
13
  export function AgentErrorView({ error }) {
14
14
  if (error.type === 'MissingSecretError') {
@@ -10,11 +10,13 @@ var __rest = (this && this.__rest) || function (s, e) {
10
10
  return t;
11
11
  };
12
12
  import { jsx as _jsx } from "react/jsx-runtime";
13
+ import omitBy from 'lodash/omitBy';
13
14
  import { createContext, useContext, useMemo } from 'react';
14
15
  const componentPreferencesContext = createContext(undefined);
15
- export function ComponentPreferencesProvider(_a) {
16
+ export default function ComponentPreferencesProvider(_a) {
16
17
  var { children } = _a, preferences = __rest(_a, ["children"]);
17
- const value = useMemo(() => (Object.assign({}, preferences)), Object.values(preferences));
18
+ const inherited = useComponentPreferences();
19
+ const value = useMemo(() => (Object.assign(Object.assign({}, inherited), omitBy(preferences, (i) => i === undefined || i === null))), [inherited, Object.values(preferences)]);
18
20
  return _jsx(componentPreferencesContext.Provider, { value: value, children: children });
19
21
  }
20
22
  export function useComponentPreferences() {
@@ -1,7 +1,12 @@
1
+ export * from './contexts/ComponentPreferences';
2
+ export { default as ComponentPreferencesProvider } from './contexts/ComponentPreferences';
1
3
  export * from './contexts/CurrentAgent';
2
4
  export { default as CurrentAgentProvider } from './contexts/CurrentAgent';
3
5
  export * from './contexts/CurrentMessage';
4
6
  export { default as CurrentMessageProvider } from './contexts/CurrentMessage';
7
+ export * from './contexts/ActiveAgent';
8
+ export { default as ActiveAgentProvider } from './contexts/ActiveAgent';
9
+ export { default as useAppearances } from './hooks/use-appearances';
5
10
  export * from './state/runtime';
6
11
  export * from './state/session';
7
12
  export { default as Runtime } from './runtime/Runtime';
@@ -17,11 +17,13 @@ import { Controller, useForm } from 'react-hook-form';
17
17
  import { useLocaleContext } from '../../../../locale';
18
18
  import AgentInputField from '../../components/AgentInputField';
19
19
  import LoadingButton from '../../components/LoadingButton';
20
+ import { useComponentPreferences } from '../../contexts/ComponentPreferences';
20
21
  import { useCurrentAgent } from '../../contexts/CurrentAgent';
21
22
  import { useRuntimeState } from '../../state/runtime';
22
23
  import { useCurrentSessionState } from '../../state/session';
23
24
  import { isValidInput } from '../../utils/agent-inputs';
24
25
  export default function AutoForm({ submitText, inlineLabel, autoFillLastForm = true, submitInQuestionField, chatMode, }) {
26
+ const preferences = useComponentPreferences();
25
27
  const submitRef = useRef(null);
26
28
  const { t } = useLocaleContext();
27
29
  const { aid, agent } = useCurrentAgent();
@@ -29,7 +31,7 @@ export default function AutoForm({ submitText, inlineLabel, autoFillLastForm = t
29
31
  const running = useCurrentSessionState((s) => s === null || s === void 0 ? void 0 : s.running);
30
32
  const parameters = useMemo(() => {
31
33
  var _a;
32
- return (_a = agent.parameters) === null || _a === void 0 ? void 0 : _a.filter(isValidInput).map((i) => {
34
+ return (_a = agent.parameters) === null || _a === void 0 ? void 0 : _a.filter((i) => { var _a; return isValidInput(i) && !((_a = preferences === null || preferences === void 0 ? void 0 : preferences.hideInputFields) === null || _a === void 0 ? void 0 : _a.includes(i.key)); }).map((i) => {
33
35
  var _a;
34
36
  return (Object.assign(Object.assign({}, i), { label: ((_a = i.label) === null || _a === void 0 ? void 0 : _a.trim()) || undefined }));
35
37
  });
@@ -37,6 +39,12 @@ export default function AutoForm({ submitText, inlineLabel, autoFillLastForm = t
37
39
  const defaultForm = useInitialFormValues();
38
40
  const form = useForm({ defaultValues: defaultForm });
39
41
  const submitDisabled = !form.formState.isValid;
42
+ useEffect(() => {
43
+ var _a, _b;
44
+ if ((preferences === null || preferences === void 0 ? void 0 : preferences.autoGenerate) && !form.formState.submitCount) {
45
+ (_b = (_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.form) === null || _b === void 0 ? void 0 : _b.requestSubmit();
46
+ }
47
+ }, [defaultForm, preferences === null || preferences === void 0 ? void 0 : preferences.autoGenerate]);
40
48
  useEffect(() => {
41
49
  if (autoFillLastForm && !form.formState.isSubmitted && !form.formState.isSubmitting) {
42
50
  form.reset(chatMode ? omit(defaultForm, 'question') : defaultForm);
@@ -56,6 +64,8 @@ export default function AutoForm({ submitText, inlineLabel, autoFillLastForm = t
56
64
  });
57
65
  return (_jsxs(Form, { component: "form", className: cx('form', `label-position-${inlineLabel ? 'start' : 'top'}`), onSubmit: form.handleSubmit(onSubmit), children: [parameters === null || parameters === void 0 ? void 0 : parameters.map((parameter, index) => {
58
66
  const { key, required } = parameter !== null && parameter !== void 0 ? parameter : {};
67
+ if (!key)
68
+ return null;
59
69
  return (_jsx(Box, { children: _jsx(Controller, { control: form.control, name: key, rules: {
60
70
  required: required || key === 'question',
61
71
  min: parameter.type === 'number' && typeof parameter.min === 'number'
@@ -81,6 +91,7 @@ export default function AutoForm({ submitText, inlineLabel, autoFillLastForm = t
81
91
  }), !(submitInQuestionField && (parameters === null || parameters === void 0 ? void 0 : parameters.some((i) => i.key === 'question'))) && (_jsx(LoadingButton, { ref: submitRef, type: "submit", variant: "contained", loading: running, disabled: submitDisabled, sx: { height: 40 }, children: submitText || t('generate') }))] }));
82
92
  }
83
93
  function useInitialFormValues() {
94
+ const preferences = useComponentPreferences();
84
95
  const { agent } = useCurrentAgent();
85
96
  const lastMessage = useCurrentSessionState((s) => { var _a; return (_a = s === null || s === void 0 ? void 0 : s.messages) === null || _a === void 0 ? void 0 : _a.at(-1); });
86
97
  const [lastInputs, setLastInputs] = useState();
@@ -93,11 +104,13 @@ function useInitialFormValues() {
93
104
  }, [lastMessage]);
94
105
  return useMemo(() => {
95
106
  var _a, _b;
107
+ if (preferences === null || preferences === void 0 ? void 0 : preferences.initialInputValues)
108
+ return preferences.initialInputValues;
96
109
  if (lastInputs) {
97
110
  return lastInputs;
98
111
  }
99
112
  return Object.fromEntries((_b = (_a = agent.parameters) === null || _a === void 0 ? void 0 : _a.map((parameter) => [parameter.key, parameter.defaultValue])) !== null && _b !== void 0 ? _b : []);
100
- }, [lastInputs, agent]);
113
+ }, [lastInputs, agent, preferences === null || preferences === void 0 ? void 0 : preferences.initialInputValues]);
101
114
  }
102
115
  const Form = styled(Stack)(({ theme }) => theme.unstable_sx({
103
116
  gap: 2,
@@ -11,11 +11,11 @@ var __rest = (this && this.__rest) || function (s, e) {
11
11
  };
12
12
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
13
13
  import { Box, Stack } from '@mui/material';
14
- import { memo } from 'react';
14
+ import { Suspense, memo } from 'react';
15
15
  import CustomComponentRenderer from '../../../../../components/CustomComponentRenderer';
16
16
  import { useSessionContext } from '../../../../session';
17
17
  import { getAssetUrl } from '../../api/asset';
18
- import { AgentErrorView } from '../../components/AgentErrorBoundary';
18
+ import AgentErrorBoundary, { AgentErrorView } from '../../components/AgentErrorBoundary';
19
19
  import UserInfo from '../../components/UserInfo';
20
20
  import { useComponentPreferences } from '../../contexts/ComponentPreferences';
21
21
  import CurrentAgentProvider from '../../contexts/CurrentAgent';
@@ -34,8 +34,8 @@ const MessageView = memo(({ message }) => {
34
34
  const { hideAgentAvatar, hideUserInputs } = (_b = useComponentPreferences()) !== null && _b !== void 0 ? _b : {};
35
35
  if (!(appearanceOutput === null || appearanceOutput === void 0 ? void 0 : appearanceOutput.componentId))
36
36
  return null;
37
- const agentMessage = (_jsx(MessageBodyContainer, { messageRole: "assistant", children: _jsx(CustomComponentRenderer, { componentId: appearanceOutput.componentId, properties: appearanceOutput.componentProperties, fallbackRender: AgentErrorView }, message.id) }));
38
- return (_jsx(CurrentAgentProvider, { agentId: message.agentId, children: _jsx(CurrentMessageProvider, { message: message, children: _jsxs(Stack, { gap: 2, children: [!hideUserInputs && (_jsx(Box, { children: _jsx(UserInfo, { name: (_c = authSession.user) === null || _c === void 0 ? void 0 : _c.fullName, did: (_d = authSession.user) === null || _d === void 0 ? void 0 : _d.did, avatar: getAssetUrl({ filename: (_e = authSession.user) === null || _e === void 0 ? void 0 : _e.avatar, preset: 'avatar' }), time: message.createdAt, reverse: true, alignItems: "flex-start", UserNameProps: { sx: { color: hasBg ? 'white' : undefined } }, children: _jsx(Stack, { sx: { alignItems: 'flex-end' }, children: _jsx(MessageBodyContainer, { messageRole: "user", children: _jsx(UserMessageView, {}) }) }) }) })), _jsx(Box, { children: !hideAgentAvatar ? (_jsx(UserInfo, { name: profile.name, did: (_f = globalThis.blocklet) === null || _f === void 0 ? void 0 : _f.appId, avatar: getAssetUrl({ blockletDid, aid, filename: profile.avatar, preset: 'avatar' }), time: message.createdAt, alignItems: "flex-start", UserNameProps: { sx: { color: hasBg ? 'white' : undefined } }, children: agentMessage })) : (agentMessage) })] }) }) }));
37
+ const agentMessage = (_jsx(MessageBodyContainer, { messageRole: "assistant", children: _jsx(Suspense, { children: _jsx(CustomComponentRenderer, { componentId: appearanceOutput.componentId, properties: appearanceOutput.componentProperties, fallbackRender: AgentErrorView }, message.id) }) }));
38
+ return (_jsx(CurrentAgentProvider, { agentId: message.agentId, children: _jsx(CurrentMessageProvider, { message: message, children: _jsxs(Stack, { gap: 2, children: [!hideUserInputs && (_jsx(Box, { children: _jsx(UserInfo, { name: (_c = authSession.user) === null || _c === void 0 ? void 0 : _c.fullName, did: (_d = authSession.user) === null || _d === void 0 ? void 0 : _d.did, avatar: getAssetUrl({ filename: (_e = authSession.user) === null || _e === void 0 ? void 0 : _e.avatar, preset: 'avatar' }), time: message.createdAt, reverse: true, alignItems: "flex-start", UserNameProps: { sx: { color: hasBg ? 'white' : undefined } }, children: _jsx(Stack, { sx: { alignItems: 'flex-end' }, children: _jsx(MessageBodyContainer, { messageRole: "user", children: _jsx(AgentErrorBoundary, { children: _jsx(Suspense, { children: _jsx(UserMessageView, {}) }) }) }) }) }) })), _jsx(Box, { children: !hideAgentAvatar ? (_jsx(UserInfo, { name: profile.name, did: (_f = globalThis.blocklet) === null || _f === void 0 ? void 0 : _f.appId, avatar: getAssetUrl({ blockletDid, aid, filename: profile.avatar, preset: 'avatar' }), time: message.createdAt, alignItems: "flex-start", UserNameProps: { sx: { color: hasBg ? 'white' : undefined } }, children: agentMessage })) : (agentMessage) })] }) }) }));
39
39
  });
40
40
  export default MessageView;
41
41
  export function MessageBodyContainer(_a) {
@@ -17,7 +17,7 @@ import SimpleHeader from '../../components/Header/SimpleHeader';
17
17
  import SimpleLayout from '../../components/Layout/SimpleLayout';
18
18
  import ScrollView from '../../components/ScrollView';
19
19
  import { useActiveAgent } from '../../contexts/ActiveAgent';
20
- import { ComponentPreferencesProvider, useComponentPreferences } from '../../contexts/ComponentPreferences';
20
+ import ComponentPreferencesProvider, { useComponentPreferences, } from '../../contexts/ComponentPreferences';
21
21
  import CurrentAgentProvider from '../../contexts/CurrentAgent';
22
22
  import { useAutoLoadSessionState, useCurrentSessionState, useSessionState } from '../../state/session';
23
23
  import BackgroundImage from './BackgroundImage';
@@ -1,9 +1,11 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { RuntimeOutputVariable } from '@blocklet/ai-runtime/types';
3
3
  import { Stack } from '@mui/material';
4
- import { useMemo } from 'react';
4
+ import { Suspense, useMemo } from 'react';
5
5
  import CustomComponentRenderer from '../../../../../components/CustomComponentRenderer';
6
+ import AgentErrorBoundary from '../../components/AgentErrorBoundary';
6
7
  import { getDefaultOutputComponent } from '../../constants';
8
+ import { useComponentPreferences } from '../../contexts/ComponentPreferences';
7
9
  import { useCurrentAgent } from '../../contexts/CurrentAgent';
8
10
  import { useCurrentMessage } from '../../contexts/CurrentMessage';
9
11
  const ignoredOutputs = new Set([
@@ -14,6 +16,8 @@ const ignoredOutputs = new Set([
14
16
  RuntimeOutputVariable.openingMessage,
15
17
  ]);
16
18
  export default function SimpleOutput() {
19
+ const preferences = useComponentPreferences();
20
+ const CustomComponentActionsComponent = preferences === null || preferences === void 0 ? void 0 : preferences.customOutputActionsComponent;
17
21
  const { message } = useCurrentMessage();
18
22
  if (message.error)
19
23
  throw message.error;
@@ -27,12 +31,12 @@ export default function SimpleOutput() {
27
31
  }, [agent.outputVariables]);
28
32
  if (!(outputs === null || outputs === void 0 ? void 0 : outputs.length))
29
33
  return null;
30
- return (_jsx(Stack, { gap: 2, children: outputs.map((output) => {
31
- var _a, _b, _c, _d, _e, _f, _g, _h;
32
- const value = output.name === RuntimeOutputVariable.text
33
- ? ((_a = message.outputs) === null || _a === void 0 ? void 0 : _a.content) ||
34
- ((_d = (_c = (_b = message.outputs) === null || _b === void 0 ? void 0 : _b.objects) === null || _c === void 0 ? void 0 : _c.find((i) => i === null || i === void 0 ? void 0 : i[RuntimeOutputVariable.text])) === null || _d === void 0 ? void 0 : _d[RuntimeOutputVariable.text])
35
- : (_g = (_f = (_e = message.outputs) === null || _e === void 0 ? void 0 : _e.objects) === null || _f === void 0 ? void 0 : _f[0]) === null || _g === void 0 ? void 0 : _g[output.name];
36
- return (_jsx(CustomComponentRenderer, { instanceId: output.id, componentId: output.appearance.componentId, properties: output.appearance.componentProperties, props: Object.assign({ output, outputValue: value, writing: output.name === RuntimeOutputVariable.text && message.loading }, (((_h = output === null || output === void 0 ? void 0 : output.appearance) === null || _h === void 0 ? void 0 : _h.componentProps) || {})) }, output.id));
37
- }) }));
34
+ return (_jsxs(Stack, { gap: 2, children: [outputs.map((output) => {
35
+ var _a, _b, _c, _d, _e, _f, _g, _h;
36
+ const value = output.name === RuntimeOutputVariable.text
37
+ ? ((_a = message.outputs) === null || _a === void 0 ? void 0 : _a.content) ||
38
+ ((_d = (_c = (_b = message.outputs) === null || _b === void 0 ? void 0 : _b.objects) === null || _c === void 0 ? void 0 : _c.find((i) => i === null || i === void 0 ? void 0 : i[RuntimeOutputVariable.text])) === null || _d === void 0 ? void 0 : _d[RuntimeOutputVariable.text])
39
+ : (_g = (_f = (_e = message.outputs) === null || _e === void 0 ? void 0 : _e.objects) === null || _f === void 0 ? void 0 : _f[0]) === null || _g === void 0 ? void 0 : _g[output.name];
40
+ return (_jsx(CustomComponentRenderer, { instanceId: output.id, componentId: output.appearance.componentId, properties: output.appearance.componentProperties, props: Object.assign({ output, outputValue: value, writing: output.name === RuntimeOutputVariable.text && message.loading }, (((_h = output === null || output === void 0 ? void 0 : output.appearance) === null || _h === void 0 ? void 0 : _h.componentProps) || {})) }, output.id));
41
+ }), CustomComponentActionsComponent && (_jsx(AgentErrorBoundary, { children: _jsx(Suspense, { children: _jsx(CustomComponentActionsComponent, {}) }) }))] }));
38
42
  }
@@ -18,14 +18,16 @@ import { AgentErrorView } from '../../components/AgentErrorBoundary';
18
18
  import SimpleHeader from '../../components/Header/SimpleHeader';
19
19
  import SimpleLayout from '../../components/Layout/SimpleLayout';
20
20
  import { useActiveAgent } from '../../contexts/ActiveAgent';
21
+ import ComponentPreferencesProvider from '../../contexts/ComponentPreferences';
21
22
  import CurrentAgentProvider from '../../contexts/CurrentAgent';
22
23
  import CurrentMessageProvider from '../../contexts/CurrentMessage';
23
24
  import useAppearances from '../../hooks/use-appearances';
24
25
  import { useAutoLoadSessionState, useCurrentSessionState } from '../../state/session';
25
- export default function SimplePage({ resultTitle }) {
26
+ export default function SimplePage(_a) {
27
+ var { resultTitle } = _a, preferences = __rest(_a, ["resultTitle"]);
26
28
  const { activeAgentId } = useActiveAgent();
27
29
  useAutoLoadSessionState();
28
- return (_jsxs(SimpleLayout, { pb: 4, children: [_jsx(SimpleHeader, {}), _jsx(CurrentAgentProvider, { agentId: activeAgentId, children: _jsx(AgentInputRender, { className: "aigne-inputs aigne-simple-page-inputs" }) }), _jsx(OutputView, { className: "aigne-outputs aigne-simple-page-outputs", resultTitle: resultTitle })] }));
30
+ return (_jsx(ComponentPreferencesProvider, Object.assign({}, preferences, { children: _jsxs(SimpleLayout, { pb: 4, children: [_jsx(SimpleHeader, {}), _jsx(CurrentAgentProvider, { agentId: activeAgentId, children: _jsx(AgentInputRender, { className: "aigne-inputs aigne-simple-page-inputs" }) }), _jsx(OutputView, { className: "aigne-outputs aigne-simple-page-outputs", resultTitle: resultTitle })] }) })));
29
31
  }
30
32
  function AgentInputRender(_a) {
31
33
  var props = __rest(_a, []);