@zero-library/chat-copilot 3.2.4 → 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);
@@ -2188,6 +2235,21 @@ function createChatStore() {
2188
2235
  const focusTarget = proxy({
2189
2236
  target: null
2190
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
+ };
2191
2253
  const openSubAgent = async (item) => {
2192
2254
  let subConvId = item.subConversationId ?? item.metadata?.conversationId;
2193
2255
  let agentName = item.agentName;
@@ -2856,6 +2918,7 @@ function createChatStore() {
2856
2918
  const canProceed = await config.hooks?.onBeforeSend?.(msgContent, files || []);
2857
2919
  if (canProceed === false) return;
2858
2920
  if (conversation.active.userInput?.length) {
2921
+ console.log(conversation.active.userInput);
2859
2922
  const invalid = conversation.active.userInput.some((item) => {
2860
2923
  const isRequired = item.rules?.some((r) => r.type === "required");
2861
2924
  if (isRequired) {
@@ -3022,7 +3085,13 @@ function createChatStore() {
3022
3085
  /** 关闭子智能体执行详情面板 */
3023
3086
  closeSubAgent,
3024
3087
  /** 右侧面板聚焦意图(通用) */
3025
- focusTarget
3088
+ focusTarget,
3089
+ /** 待插入的资源节点请求 */
3090
+ resourceInsertion,
3091
+ /** 发起资源节点插入 */
3092
+ insertResource,
3093
+ /** 消费已处理的资源节点请求 */
3094
+ consumeInsertedResource
3026
3095
  };
3027
3096
  }
3028
3097
  var AuthImage = ({ path, size, shape = "square" }) => {
@@ -4099,6 +4168,7 @@ var MdEdit_default = ({ data, loading }) => {
4099
4168
  };
4100
4169
  var PreviewLink_default = ({ data, loading, message: message3, ...rest }) => {
4101
4170
  const chatStore = useChatStore();
4171
+ const conversationId = chatStore.conversation.active?.id;
4102
4172
  const conversationWorkspacePrefix = "/workspace";
4103
4173
  const getLinkFileName = (path) => {
4104
4174
  const lastSegment = path.split("/").pop();
@@ -4128,7 +4198,7 @@ var PreviewLink_default = ({ data, loading, message: message3, ...rest }) => {
4128
4198
  chatStore.setPreview({
4129
4199
  fileUrl: chatStore.config.services.request.getPreviewUrl({
4130
4200
  path: normalizedPath,
4131
- workspaceId: message3.conversationId
4201
+ workspaceId: conversationId
4132
4202
  }),
4133
4203
  fileName,
4134
4204
  suffix
@@ -5892,7 +5962,6 @@ var RunStartedNode = React10__default.memo(() => {
5892
5962
  ] });
5893
5963
  });
5894
5964
  var RESOURCE_MARKDOWN_TAG = "resourcetag";
5895
- var RESOURCE_TOKEN_REGEX = /(\w+):\/\/([^?\s]+)\?name=([^&\s]+)&label=([^\s]+)/g;
5896
5965
  function QuestionSummary({ content }) {
5897
5966
  const { t: t11 } = useTranslation(NS_CHAT_COPILOT);
5898
5967
  return /* @__PURE__ */ jsxs("span", { style: { display: "inline-flex", flexDirection: "column", gap: 4 }, children: [
@@ -5906,7 +5975,8 @@ function MessageResourceTag({ data }) {
5906
5975
  return /* @__PURE__ */ jsx(ReadonlyResourceTag, { resource });
5907
5976
  }
5908
5977
  function transformResourceText(content) {
5909
- return content.replace(RESOURCE_TOKEN_REGEX, (_, resourceType, resourceId, resourceName, resourceLabel) => {
5978
+ const resourceTokenRegex = createResourceTokenRegex();
5979
+ return content.replace(resourceTokenRegex, (_, resourceType, resourceId, resourceName, resourceLabel) => {
5910
5980
  const resource = {
5911
5981
  resourceType: decodeURIComponent(resourceType),
5912
5982
  resourceId: decodeURIComponent(resourceId),
@@ -6879,29 +6949,8 @@ var emptyAgentResources2 = {
6879
6949
  subAgents: [],
6880
6950
  tools: []
6881
6951
  };
6882
- var focusSenderInputToEnd = () => {
6883
- requestAnimationFrame(() => {
6884
- requestAnimationFrame(() => {
6885
- const input = document.querySelector('.zero-chat-sender .ant-sender-input[contenteditable="true"]');
6886
- if (!input) return;
6887
- input.focus();
6888
- const selection = window.getSelection();
6889
- if (!selection) return;
6890
- const range = document.createRange();
6891
- range.selectNodeContents(input);
6892
- range.collapse(false);
6893
- selection.removeAllRanges();
6894
- selection.addRange(range);
6895
- });
6896
- });
6897
- };
6898
- var appendResourceToken = (content, token) => {
6899
- if (!content) return token;
6900
- return /\s$/.test(content) ? `${content}${token}` : `${content} ${token}`;
6901
- };
6902
6952
  var ResourceBtn_default = ({ types = defaultResourceTypes }) => {
6903
6953
  const chatStore = useChatStore();
6904
- const conversationState = useSnapshot(chatStore.conversation);
6905
6954
  const { t: t11 } = useTranslation(NS_CHAT_COPILOT);
6906
6955
  const [open, setOpen] = useState(false);
6907
6956
  const [loading, setLoading] = useState(false);
@@ -6911,15 +6960,13 @@ var ResourceBtn_default = ({ types = defaultResourceTypes }) => {
6911
6960
  key: getResourceKey(item),
6912
6961
  label: getLabel(item),
6913
6962
  onClick: () => {
6914
- const resourceName = getLabel(item);
6915
- const currentContent = conversationState.messages[conversationState.active.id]?.content || "";
6916
- const nextContent = appendResourceToken(
6917
- currentContent,
6918
- serializeResourceToken(resourceLabel, resourceName, resourceTypeMap[categoryKey].nodeType, getResourceKey(item))
6919
- );
6920
- chatStore.setContent(nextContent);
6963
+ chatStore.insertResource({
6964
+ resourceLabel,
6965
+ resourceName: getLabel(item),
6966
+ resourceType: resourceTypeMap[categoryKey].nodeType,
6967
+ resourceId: getResourceKey(item)
6968
+ });
6921
6969
  setOpen(false);
6922
- focusSenderInputToEnd();
6923
6970
  }
6924
6971
  }));
6925
6972
  const buildSubMenu = (key, label, items2, getLabel) => ({
@@ -6963,7 +7010,7 @@ var ResourceBtn_default = ({ types = defaultResourceTypes }) => {
6963
7010
  label: t11("empty")
6964
7011
  }
6965
7012
  ];
6966
- }, [chatStore, conversationState.active.id, conversationState.messages, loading, resources, t11, types]);
7013
+ }, [chatStore, loading, resources, t11, types]);
6967
7014
  const handleOpenChange = async (nextOpen) => {
6968
7015
  setOpen(nextOpen);
6969
7016
  if (!nextOpen) return;
@@ -7239,10 +7286,19 @@ var UserInputPanel = () => {
7239
7286
  const [activeKey, setActiveKey] = useState(["1"]);
7240
7287
  const { t: t11 } = useTranslation(NS_CHAT_COPILOT);
7241
7288
  const inputs = useMemo(() => {
7242
- if (agentState.agentInfo.userInput?.length > 0) {
7243
- return agentState.agentInfo.userInput;
7244
- }
7289
+ return agentState.agentInfo.userInput || [];
7245
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
+ );
7246
7302
  useEffect(() => {
7247
7303
  if (inputs.length > 0) {
7248
7304
  const initialValues = inputs.reduce((acc, cur) => {
@@ -7251,23 +7307,15 @@ var UserInputPanel = () => {
7251
7307
  return acc;
7252
7308
  }, {});
7253
7309
  form.setFieldsValue(initialValues);
7310
+ syncConversationUserInput(initialValues);
7254
7311
  }
7255
- }, [inputs, form]);
7256
- const handleValuesChange = (changedValues) => {
7257
- if (agentState.agentInfo.userInput?.length > 0) {
7258
- const newInput = agentState.agentInfo.userInput.map((v) => {
7259
- if (v.name in changedValues) {
7260
- const newValue = changedValues[v.name];
7261
- return { ...v, value: newValue };
7262
- }
7263
- return v;
7264
- });
7265
- chatStore.setConversationUserInput(newInput);
7266
- return;
7267
- }
7312
+ }, [form, inputs, syncConversationUserInput]);
7313
+ const handleValuesChange = (_changedValues, allValues) => {
7314
+ syncConversationUserInput(allValues);
7268
7315
  };
7269
7316
  const handleStartChat = () => {
7270
7317
  form.validateFields().then(() => {
7318
+ syncConversationUserInput(form.getFieldsValue(true));
7271
7319
  setActiveKey([]);
7272
7320
  }).catch(() => {
7273
7321
  message.error(t11("checkParams"));