@zero-library/chat-copilot 3.2.3 → 3.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -13,7 +13,7 @@ import { LexicalErrorBoundary } from '@lexical/react/LexicalErrorBoundary';
13
13
  import { HistoryPlugin } from '@lexical/react/LexicalHistoryPlugin';
14
14
  import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin';
15
15
  import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin';
16
- import { DecoratorNode, FOCUS_COMMAND, COMMAND_PRIORITY_LOW, BLUR_COMMAND, $getRoot, $createParagraphNode, $createTextNode, $getNodeByKey } from 'lexical';
16
+ import { DecoratorNode, FOCUS_COMMAND, COMMAND_PRIORITY_LOW, BLUR_COMMAND, $getRoot, $createParagraphNode, $createTextNode, $getSelection, $isRangeSelection, $getNodeByKey } from 'lexical';
17
17
  import { LexicalTypeaheadMenuPlugin, MenuOption } from '@lexical/react/LexicalTypeaheadMenuPlugin';
18
18
  import { mergeRegister } from '@lexical/utils';
19
19
  import * as ReactDOM from 'react-dom';
@@ -1055,7 +1055,7 @@ function formatResourceName(resourceName) {
1055
1055
  return `${name.slice(0, 3)}***${name.slice(-3)}${extension}`;
1056
1056
  }
1057
1057
  function ReadonlyResourceTag({ resource }) {
1058
- return /* @__PURE__ */ jsx("div", { className: index_module_default.resourceTag, children: /* @__PURE__ */ jsxs(Tag, { color: "blue", children: [
1058
+ return /* @__PURE__ */ jsx("span", { className: index_module_default.resourceTag, children: /* @__PURE__ */ jsxs(Tag, { color: "blue", children: [
1059
1059
  resource.resourceLabel,
1060
1060
  "\uFF1A",
1061
1061
  formatResourceName(resource.resourceName)
@@ -1071,7 +1071,7 @@ function ResourceTag({ resource }) {
1071
1071
  node?.remove();
1072
1072
  });
1073
1073
  };
1074
- return /* @__PURE__ */ jsx("div", { className: index_module_default.resourceTag, children: /* @__PURE__ */ jsxs(Tag, { closeIcon: true, color: "blue", onClose: handleClose, children: [
1074
+ return /* @__PURE__ */ jsx("span", { className: index_module_default.resourceTag, children: /* @__PURE__ */ jsxs(Tag, { closeIcon: true, color: "blue", onClose: handleClose, children: [
1075
1075
  resource.resourceLabel,
1076
1076
  "\uFF1A",
1077
1077
  formatResourceName(resource.resourceName)
@@ -1167,10 +1167,14 @@ var VariableNode = class _VariableNode extends DecoratorNode {
1167
1167
  function $createVariableNode(resourceLabel, resourceName, resourceType, resourceId) {
1168
1168
  return new VariableNode(resourceLabel, resourceName, resourceType, resourceId);
1169
1169
  }
1170
+ var RESOURCE_TOKEN_REGEX_SOURCE = String.raw`\/?(\w+):\/\/([^?\s#]+)\?name=([^&\s#]+)&label=([^#\s]+)(?:#)?`;
1171
+ function createResourceTokenRegex() {
1172
+ return new RegExp(RESOURCE_TOKEN_REGEX_SOURCE, "g");
1173
+ }
1170
1174
  function serializeResourceToken(resourceLabel, resourceName, resourceType, resourceId) {
1171
1175
  return `${encodeURIComponent(resourceType)}://${encodeURIComponent(resourceId)}?name=${encodeURIComponent(resourceName)}&label=${encodeURIComponent(
1172
1176
  resourceLabel
1173
- )}`;
1177
+ )}#`;
1174
1178
  }
1175
1179
  var emptyAgentResources = {
1176
1180
  skills: [],
@@ -1222,6 +1226,45 @@ var categoryValueTypeMap = Object.fromEntries(
1222
1226
  Object.entries(resourceTypeConfigMap).map(([type, config]) => [config.categoryValue, type])
1223
1227
  );
1224
1228
  var isHashCommandConfig = (config) => !!config && !Array.isArray(config);
1229
+ function insertVariableNode(editor, { resourceLabel, resourceName, resourceType, resourceId }, nodeToReplace) {
1230
+ editor.update(() => {
1231
+ const variableNode = $createVariableNode(resourceLabel, resourceName, resourceType, resourceId);
1232
+ if (nodeToReplace) {
1233
+ nodeToReplace.replace(variableNode);
1234
+ variableNode.selectNext();
1235
+ return;
1236
+ }
1237
+ const trailingTextNode = $createTextNode("");
1238
+ const selection = $getSelection();
1239
+ if ($isRangeSelection(selection)) {
1240
+ selection.insertNodes([variableNode, trailingTextNode]);
1241
+ trailingTextNode.select();
1242
+ return;
1243
+ }
1244
+ const root = $getRoot();
1245
+ const lastChild = root.getLastChild();
1246
+ const paragraph = lastChild?.getType() === "paragraph" ? lastChild : $createParagraphNode();
1247
+ if (!paragraph.getParent()) {
1248
+ root.append(paragraph);
1249
+ }
1250
+ paragraph.append(variableNode, trailingTextNode);
1251
+ trailingTextNode.select();
1252
+ });
1253
+ }
1254
+ function ResourceInsertPlugin() {
1255
+ const [editor] = useLexicalComposerContext();
1256
+ const chatStore = useChatStore();
1257
+ const resourceInsertionState = useSnapshot(chatStore.resourceInsertion);
1258
+ useEffect(() => {
1259
+ const pendingResource = resourceInsertionState.pending;
1260
+ if (!pendingResource) return;
1261
+ editor.focus(() => {
1262
+ insertVariableNode(editor, pendingResource);
1263
+ });
1264
+ chatStore.consumeInsertedResource(pendingResource.requestId);
1265
+ }, [chatStore, editor, resourceInsertionState.pending]);
1266
+ return null;
1267
+ }
1225
1268
  var ResourceOption = class extends MenuOption {
1226
1269
  group;
1227
1270
  item;
@@ -1381,13 +1424,16 @@ function VariablePickerPlugin({ commandConfig = {} }) {
1381
1424
  const resource = selectedOption.item;
1382
1425
  const resourceLabel = selectedOption.group.label;
1383
1426
  const resourceType = selectedOption.group.value;
1384
- editor.update(() => {
1385
- const variableNode = $createVariableNode(resourceLabel, resource.label, resourceType, resource.value);
1386
- if (nodeToReplace) {
1387
- nodeToReplace.replace(variableNode);
1388
- variableNode.selectNext();
1389
- }
1390
- });
1427
+ insertVariableNode(
1428
+ editor,
1429
+ {
1430
+ resourceLabel,
1431
+ resourceName: resource.label,
1432
+ resourceType,
1433
+ resourceId: resource.value
1434
+ },
1435
+ nodeToReplace
1436
+ );
1391
1437
  closeMenu();
1392
1438
  },
1393
1439
  [editor]
@@ -1427,7 +1473,7 @@ function VariablePickerPlugin({ commandConfig = {} }) {
1427
1473
  function InitialStatePlugin({ value }) {
1428
1474
  const [editor] = useLexicalComposerContext();
1429
1475
  function parseAndAppendNodes(paragraph, value2) {
1430
- const variableRegex = /\/?(\w+):\/\/([^?\s]+)\?name=([^&\s]+)&label=([^\s]+)/g;
1476
+ const variableRegex = createResourceTokenRegex();
1431
1477
  let lastIndex = 0;
1432
1478
  let match;
1433
1479
  while ((match = variableRegex.exec(value2)) !== null) {
@@ -1582,6 +1628,7 @@ var ChatInput = React10.forwardRef(
1582
1628
  ),
1583
1629
  /* @__PURE__ */ jsx(HistoryPlugin, {}),
1584
1630
  /* @__PURE__ */ jsx(VariablePickerPlugin, { commandConfig }),
1631
+ /* @__PURE__ */ jsx(ResourceInsertPlugin, {}),
1585
1632
  /* @__PURE__ */ jsx(OnChangePlugin, { onChange: handleOnChange }),
1586
1633
  /* @__PURE__ */ jsx(InitialStatePlugin, { value: stringValue })
1587
1634
  ] }) });
@@ -1970,7 +2017,7 @@ function createChatStore() {
1970
2017
  await getAgentInfo(agentInfo.id);
1971
2018
  }
1972
2019
  agent.agentInfo = { ...agent.agentInfo, ...agentInfo };
1973
- if (config.layout.userInput) {
2020
+ if (config.layout.userInput && agent.agentInfo.agentType === 1 /* FLOW */) {
1974
2021
  await getUserInput(agent.agentInfo.id);
1975
2022
  }
1976
2023
  config.hooks?.onAfterSwitchAgent?.(agent.agentInfo);
@@ -2058,6 +2105,7 @@ function createChatStore() {
2058
2105
  }
2059
2106
  await getMessages(conversationId);
2060
2107
  await setPreview();
2108
+ closeSubAgent();
2061
2109
  config.hooks?.onAfterSwitchConversation?.(conversationId);
2062
2110
  };
2063
2111
  const updateConversations = (data) => {
@@ -2187,6 +2235,21 @@ function createChatStore() {
2187
2235
  const focusTarget = proxy({
2188
2236
  target: null
2189
2237
  });
2238
+ let resourceInsertionRequestId = 0;
2239
+ const resourceInsertion = proxy({
2240
+ pending: null
2241
+ });
2242
+ const insertResource = (resource) => {
2243
+ resourceInsertion.pending = {
2244
+ requestId: ++resourceInsertionRequestId,
2245
+ ...resource
2246
+ };
2247
+ };
2248
+ const consumeInsertedResource = (requestId) => {
2249
+ if (resourceInsertion.pending?.requestId === requestId) {
2250
+ resourceInsertion.pending = null;
2251
+ }
2252
+ };
2190
2253
  const openSubAgent = async (item) => {
2191
2254
  let subConvId = item.subConversationId ?? item.metadata?.conversationId;
2192
2255
  let agentName = item.agentName;
@@ -2855,6 +2918,7 @@ function createChatStore() {
2855
2918
  const canProceed = await config.hooks?.onBeforeSend?.(msgContent, files || []);
2856
2919
  if (canProceed === false) return;
2857
2920
  if (conversation.active.userInput?.length) {
2921
+ console.log(conversation.active.userInput);
2858
2922
  const invalid = conversation.active.userInput.some((item) => {
2859
2923
  const isRequired = item.rules?.some((r) => r.type === "required");
2860
2924
  if (isRequired) {
@@ -3021,7 +3085,13 @@ function createChatStore() {
3021
3085
  /** 关闭子智能体执行详情面板 */
3022
3086
  closeSubAgent,
3023
3087
  /** 右侧面板聚焦意图(通用) */
3024
- focusTarget
3088
+ focusTarget,
3089
+ /** 待插入的资源节点请求 */
3090
+ resourceInsertion,
3091
+ /** 发起资源节点插入 */
3092
+ insertResource,
3093
+ /** 消费已处理的资源节点请求 */
3094
+ consumeInsertedResource
3025
3095
  };
3026
3096
  }
3027
3097
  var AuthImage = ({ path, size, shape = "square" }) => {
@@ -4098,6 +4168,7 @@ var MdEdit_default = ({ data, loading }) => {
4098
4168
  };
4099
4169
  var PreviewLink_default = ({ data, loading, message: message3, ...rest }) => {
4100
4170
  const chatStore = useChatStore();
4171
+ const conversationId = chatStore.conversation.active?.id;
4101
4172
  const conversationWorkspacePrefix = "/workspace";
4102
4173
  const getLinkFileName = (path) => {
4103
4174
  const lastSegment = path.split("/").pop();
@@ -4127,7 +4198,7 @@ var PreviewLink_default = ({ data, loading, message: message3, ...rest }) => {
4127
4198
  chatStore.setPreview({
4128
4199
  fileUrl: chatStore.config.services.request.getPreviewUrl({
4129
4200
  path: normalizedPath,
4130
- workspaceId: message3.conversationId
4201
+ workspaceId: conversationId
4131
4202
  }),
4132
4203
  fileName,
4133
4204
  suffix
@@ -5891,7 +5962,6 @@ var RunStartedNode = React10__default.memo(() => {
5891
5962
  ] });
5892
5963
  });
5893
5964
  var RESOURCE_MARKDOWN_TAG = "resourcetag";
5894
- var RESOURCE_TOKEN_REGEX = /(\w+):\/\/([^?\s]+)\?name=([^&\s]+)&label=([^\s]+)/g;
5895
5965
  function QuestionSummary({ content }) {
5896
5966
  const { t: t11 } = useTranslation(NS_CHAT_COPILOT);
5897
5967
  return /* @__PURE__ */ jsxs("span", { style: { display: "inline-flex", flexDirection: "column", gap: 4 }, children: [
@@ -5905,7 +5975,8 @@ function MessageResourceTag({ data }) {
5905
5975
  return /* @__PURE__ */ jsx(ReadonlyResourceTag, { resource });
5906
5976
  }
5907
5977
  function transformResourceText(content) {
5908
- return content.replace(RESOURCE_TOKEN_REGEX, (_, resourceType, resourceId, resourceName, resourceLabel) => {
5978
+ const resourceTokenRegex = createResourceTokenRegex();
5979
+ return content.replace(resourceTokenRegex, (_, resourceType, resourceId, resourceName, resourceLabel) => {
5909
5980
  const resource = {
5910
5981
  resourceType: decodeURIComponent(resourceType),
5911
5982
  resourceId: decodeURIComponent(resourceId),
@@ -6878,29 +6949,8 @@ var emptyAgentResources2 = {
6878
6949
  subAgents: [],
6879
6950
  tools: []
6880
6951
  };
6881
- var focusSenderInputToEnd = () => {
6882
- requestAnimationFrame(() => {
6883
- requestAnimationFrame(() => {
6884
- const input = document.querySelector('.zero-chat-sender .ant-sender-input[contenteditable="true"]');
6885
- if (!input) return;
6886
- input.focus();
6887
- const selection = window.getSelection();
6888
- if (!selection) return;
6889
- const range = document.createRange();
6890
- range.selectNodeContents(input);
6891
- range.collapse(false);
6892
- selection.removeAllRanges();
6893
- selection.addRange(range);
6894
- });
6895
- });
6896
- };
6897
- var appendResourceToken = (content, token) => {
6898
- if (!content) return token;
6899
- return /\s$/.test(content) ? `${content}${token}` : `${content} ${token}`;
6900
- };
6901
6952
  var ResourceBtn_default = ({ types = defaultResourceTypes }) => {
6902
6953
  const chatStore = useChatStore();
6903
- const conversationState = useSnapshot(chatStore.conversation);
6904
6954
  const { t: t11 } = useTranslation(NS_CHAT_COPILOT);
6905
6955
  const [open, setOpen] = useState(false);
6906
6956
  const [loading, setLoading] = useState(false);
@@ -6910,15 +6960,13 @@ var ResourceBtn_default = ({ types = defaultResourceTypes }) => {
6910
6960
  key: getResourceKey(item),
6911
6961
  label: getLabel(item),
6912
6962
  onClick: () => {
6913
- const resourceName = getLabel(item);
6914
- const currentContent = conversationState.messages[conversationState.active.id]?.content || "";
6915
- const nextContent = appendResourceToken(
6916
- currentContent,
6917
- serializeResourceToken(resourceLabel, resourceName, resourceTypeMap[categoryKey].nodeType, getResourceKey(item))
6918
- );
6919
- chatStore.setContent(nextContent);
6963
+ chatStore.insertResource({
6964
+ resourceLabel,
6965
+ resourceName: getLabel(item),
6966
+ resourceType: resourceTypeMap[categoryKey].nodeType,
6967
+ resourceId: getResourceKey(item)
6968
+ });
6920
6969
  setOpen(false);
6921
- focusSenderInputToEnd();
6922
6970
  }
6923
6971
  }));
6924
6972
  const buildSubMenu = (key, label, items2, getLabel) => ({
@@ -6962,7 +7010,7 @@ var ResourceBtn_default = ({ types = defaultResourceTypes }) => {
6962
7010
  label: t11("empty")
6963
7011
  }
6964
7012
  ];
6965
- }, [chatStore, conversationState.active.id, conversationState.messages, loading, resources, t11, types]);
7013
+ }, [chatStore, loading, resources, t11, types]);
6966
7014
  const handleOpenChange = async (nextOpen) => {
6967
7015
  setOpen(nextOpen);
6968
7016
  if (!nextOpen) return;
@@ -7238,10 +7286,19 @@ var UserInputPanel = () => {
7238
7286
  const [activeKey, setActiveKey] = useState(["1"]);
7239
7287
  const { t: t11 } = useTranslation(NS_CHAT_COPILOT);
7240
7288
  const inputs = useMemo(() => {
7241
- if (agentState.agentInfo.userInput?.length > 0) {
7242
- return agentState.agentInfo.userInput;
7243
- }
7289
+ return agentState.agentInfo.userInput || [];
7244
7290
  }, [agentState.agentInfo.userInput]);
7291
+ const syncConversationUserInput = useCallback(
7292
+ (values) => {
7293
+ if (inputs.length === 0) return;
7294
+ const newInput = inputs.map((item) => ({
7295
+ ...item,
7296
+ value: values[item.name]
7297
+ }));
7298
+ chatStore.setConversationUserInput(newInput);
7299
+ },
7300
+ [chatStore, inputs]
7301
+ );
7245
7302
  useEffect(() => {
7246
7303
  if (inputs.length > 0) {
7247
7304
  const initialValues = inputs.reduce((acc, cur) => {
@@ -7250,23 +7307,15 @@ var UserInputPanel = () => {
7250
7307
  return acc;
7251
7308
  }, {});
7252
7309
  form.setFieldsValue(initialValues);
7310
+ syncConversationUserInput(initialValues);
7253
7311
  }
7254
- }, [inputs, form]);
7255
- const handleValuesChange = (changedValues) => {
7256
- if (agentState.agentInfo.userInput?.length > 0) {
7257
- const newInput = agentState.agentInfo.userInput.map((v) => {
7258
- if (v.name in changedValues) {
7259
- const newValue = changedValues[v.name];
7260
- return { ...v, value: newValue };
7261
- }
7262
- return v;
7263
- });
7264
- chatStore.setConversationUserInput(newInput);
7265
- return;
7266
- }
7312
+ }, [form, inputs, syncConversationUserInput]);
7313
+ const handleValuesChange = (_changedValues, allValues) => {
7314
+ syncConversationUserInput(allValues);
7267
7315
  };
7268
7316
  const handleStartChat = () => {
7269
7317
  form.validateFields().then(() => {
7318
+ syncConversationUserInput(form.getFieldsValue(true));
7270
7319
  setActiveKey([]);
7271
7320
  }).catch(() => {
7272
7321
  message.error(t11("checkParams"));
@@ -8138,9 +8187,7 @@ var ChatSubAgentPanel_default = ChatSubAgentPanel;
8138
8187
  var index_module_default3 = {
8139
8188
  chatLayout: "index_module_chatLayout",
8140
8189
  animatedSplitter: "index_module_animatedSplitter",
8141
- hideSplitterBar: "index_module_hideSplitterBar",
8142
- rightTabs: "index_module_rightTabs"
8143
- };
8190
+ hideSplitterBar: "index_module_hideSplitterBar"};
8144
8191
  var layouts_default = forwardRef(({ theme: theme3, params, hooks, layout, config, services }, _ref) => {
8145
8192
  const { t: t11 } = useTranslation(NS_CHAT_COPILOT);
8146
8193
  const chatStore = useMemo(() => createChatStore(), []);
@@ -8280,7 +8327,7 @@ var layouts_default = forwardRef(({ theme: theme3, params, hooks, layout, config
8280
8327
  hasRightPanel && /* @__PURE__ */ jsx(Splitter.Panel, { collapsible: false, min: 360, size: sizes[1], children: /* @__PURE__ */ jsx(
8281
8328
  Tabs,
8282
8329
  {
8283
- className: classNames9("height-full", index_module_default3.rightTabs),
8330
+ className: "full-tabs",
8284
8331
  activeKey: effectiveRightTab,
8285
8332
  onChange: (k) => setActiveRightTab(k),
8286
8333
  tabBarStyle: { marginBottom: 0 },