@chat-lab/ui 0.1.0-beta.52 → 0.1.0-beta.54

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 (59) hide show
  1. package/dist/components/Chatkit/index.cjs +5 -5
  2. package/dist/components/Chatkit/index.cjs.map +1 -1
  3. package/dist/components/Chatkit/index.js +5 -5
  4. package/dist/components/Chatkit/index.js.map +1 -1
  5. package/dist/components/Chatkit/types.d.ts +1 -0
  6. package/dist/components/Chatkit/types.d.ts.map +1 -1
  7. package/dist/components/assistant-ui/attachment.cjs +7 -58
  8. package/dist/components/assistant-ui/attachment.cjs.map +1 -1
  9. package/dist/components/assistant-ui/attachment.d.ts.map +1 -1
  10. package/dist/components/assistant-ui/attachment.js +8 -59
  11. package/dist/components/assistant-ui/attachment.js.map +1 -1
  12. package/dist/components/assistant-ui/message-components-with-avatar.cjs +8 -2
  13. package/dist/components/assistant-ui/message-components-with-avatar.cjs.map +1 -1
  14. package/dist/components/assistant-ui/message-components-with-avatar.d.ts +2 -4
  15. package/dist/components/assistant-ui/message-components-with-avatar.d.ts.map +1 -1
  16. package/dist/components/assistant-ui/message-components-with-avatar.js +8 -2
  17. package/dist/components/assistant-ui/message-components-with-avatar.js.map +1 -1
  18. package/dist/components/assistant-ui/thread.cjs +0 -1
  19. package/dist/components/assistant-ui/thread.cjs.map +1 -1
  20. package/dist/components/assistant-ui/thread.js +0 -1
  21. package/dist/components/assistant-ui/thread.js.map +1 -1
  22. package/dist/components/assistant-ui-tools/skill-agent-tool-set.cjs +52 -7
  23. package/dist/components/assistant-ui-tools/skill-agent-tool-set.cjs.map +1 -1
  24. package/dist/components/assistant-ui-tools/skill-agent-tool-set.d.ts.map +1 -1
  25. package/dist/components/assistant-ui-tools/skill-agent-tool-set.js +53 -8
  26. package/dist/components/assistant-ui-tools/skill-agent-tool-set.js.map +1 -1
  27. package/dist/hooks/useSkillAgent/SkillAgentPlugin.cjs +23 -14
  28. package/dist/hooks/useSkillAgent/SkillAgentPlugin.cjs.map +1 -1
  29. package/dist/hooks/useSkillAgent/SkillAgentPlugin.d.ts +2 -2
  30. package/dist/hooks/useSkillAgent/SkillAgentPlugin.d.ts.map +1 -1
  31. package/dist/hooks/useSkillAgent/SkillAgentPlugin.js +23 -14
  32. package/dist/hooks/useSkillAgent/SkillAgentPlugin.js.map +1 -1
  33. package/dist/hooks/useSkillAgent/index.cjs +15 -3
  34. package/dist/hooks/useSkillAgent/index.cjs.map +1 -1
  35. package/dist/hooks/useSkillAgent/index.d.ts +1 -1
  36. package/dist/hooks/useSkillAgent/index.d.ts.map +1 -1
  37. package/dist/hooks/useSkillAgent/index.js +16 -4
  38. package/dist/hooks/useSkillAgent/index.js.map +1 -1
  39. package/dist/index.css +1 -1
  40. package/dist/node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/uniqueId.cjs +31 -0
  41. package/dist/node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/uniqueId.cjs.map +1 -0
  42. package/dist/node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/uniqueId.js +29 -0
  43. package/dist/node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/uniqueId.js.map +1 -0
  44. package/dist/packages/core/dist/index.cjs +13 -12
  45. package/dist/packages/core/dist/index.cjs.map +1 -1
  46. package/dist/packages/core/dist/index.js +13 -12
  47. package/dist/packages/core/dist/index.js.map +1 -1
  48. package/dist/utils/convertToAssistantMessage.cjs +44 -16
  49. package/dist/utils/convertToAssistantMessage.cjs.map +1 -1
  50. package/dist/utils/convertToAssistantMessage.d.ts.map +1 -1
  51. package/dist/utils/convertToAssistantMessage.js +44 -16
  52. package/dist/utils/convertToAssistantMessage.js.map +1 -1
  53. package/dist/utils/deepAssign.cjs +31 -0
  54. package/dist/utils/deepAssign.cjs.map +1 -0
  55. package/dist/utils/deepAssign.d.ts +2 -0
  56. package/dist/utils/deepAssign.d.ts.map +1 -0
  57. package/dist/utils/deepAssign.js +29 -0
  58. package/dist/utils/deepAssign.js.map +1 -0
  59. package/package.json +1 -1
@@ -5,6 +5,7 @@ var without = require('../node_modules/.pnpm/lodash-es@4.17.21/node_modules/loda
5
5
  var uniq = require('../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/uniq.cjs');
6
6
  var compact = require('../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/compact.cjs');
7
7
  var last = require('../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/last.cjs');
8
+ var uniqueId = require('../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/uniqueId.cjs');
8
9
  var keys = require('../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/keys.cjs');
9
10
 
10
11
  const convertToAssistantMessage = (messages, options = {
@@ -29,6 +30,7 @@ const convertToAssistantMessage = (messages, options = {
29
30
  };
30
31
  const reasoningParts = [];
31
32
  const authorFirstMessage = new Set();
33
+ const attaches = [];
32
34
  const content = [...cur.content]
33
35
  .sort((a, b) => {
34
36
  const aAuthor = a.author;
@@ -48,6 +50,28 @@ const convertToAssistantMessage = (messages, options = {
48
50
  authorFirstMessage.add(author);
49
51
  }
50
52
  if (part.type === 'text') {
53
+ try {
54
+ const text = part.text;
55
+ if (text?.[0] === '[' &&
56
+ text?.[text.length - 1] === ']' &&
57
+ cur.role === 'user') {
58
+ const files = text.slice(1, -1).split(',');
59
+ files.forEach(file => {
60
+ attaches.push({
61
+ id: uniqueId(),
62
+ type: 'file',
63
+ status: {
64
+ type: 'complete',
65
+ },
66
+ contentType: 'plain/text',
67
+ name: file,
68
+ content: [],
69
+ });
70
+ });
71
+ return acc;
72
+ }
73
+ }
74
+ catch (e) { }
51
75
  if (lastPart &&
52
76
  lastPart.type === 'text' &&
53
77
  lastPart.author === author) {
@@ -80,7 +104,7 @@ const convertToAssistantMessage = (messages, options = {
80
104
  }
81
105
  }
82
106
  else if (part.type === 'image') {
83
- acc.push({
107
+ attaches.push({
84
108
  type: part.type,
85
109
  image: part.imageUrl.url,
86
110
  ...(authors.length > 2 && { showAvatar, marginLeft }),
@@ -90,12 +114,12 @@ const convertToAssistantMessage = (messages, options = {
90
114
  const data = part.fileUrl ||
91
115
  `data:${part.mimeType};base64,${part.fileData}` ||
92
116
  '';
93
- acc.push({
117
+ attaches.push({
94
118
  type: part.type,
95
119
  data,
96
120
  filename: part.name,
97
121
  mimeType: part.mimeType,
98
- ...(authors.length > 2 && { showAvatar, marginLeft }),
122
+ // ...(authors.length > 2 && { showAvatar, marginLeft }),
99
123
  });
100
124
  }
101
125
  else if (part.type === 'tool_call') {
@@ -153,19 +177,22 @@ const convertToAssistantMessage = (messages, options = {
153
177
  id: cur.id,
154
178
  role: cur.role,
155
179
  status,
156
- attachments: cur.attaches?.map(item => ({
157
- status: { type: 'complete' },
158
- id: item.id,
159
- name: item.id,
160
- type: 'image',
161
- contentType: item.mimeType,
162
- content: [
163
- {
164
- type: 'image',
165
- image: item.url,
166
- },
167
- ],
168
- })),
180
+ attachments: [
181
+ ...(cur.attaches?.map(item => ({
182
+ status: { type: 'complete' },
183
+ id: item.id,
184
+ name: item.id,
185
+ type: 'file',
186
+ contentType: item.mimeType,
187
+ content: [
188
+ {
189
+ type: item.type,
190
+ image: item.url,
191
+ },
192
+ ],
193
+ })) || []),
194
+ ...attaches,
195
+ ],
169
196
  content: compact([
170
197
  ...reasoningParts,
171
198
  cur.role === 'assistant' &&
@@ -182,6 +209,7 @@ const convertToAssistantMessage = (messages, options = {
182
209
  };
183
210
  return uiMessage;
184
211
  });
212
+ console.log(uiMessages, messages);
185
213
  return { uiMessages, authors };
186
214
  };
187
215
 
@@ -1 +1 @@
1
- {"version":3,"file":"convertToAssistantMessage.cjs","sources":["../../src/utils/convertToAssistantMessage.ts"],"sourcesContent":["import {\n MessageStatus,\n ReasoningMessagePart,\n ThreadAssistantMessagePart,\n ThreadMessageLike,\n ToolCallMessagePart,\n} from '@assistant-ui/react';\nimport {\n ChatMessage,\n MessageStatus as MessageStatusEnum,\n} from '@chat-lab/core';\nimport { compact, keys, last, uniq, without } from 'lodash-es';\ntype Mutable<T> = {\n -readonly [K in keyof T]: T[K]; // -readonly 表示移除 readonly 修饰符\n};\n\nconst convertToAssistantMessage = (\n messages: Readonly<ChatMessage>[],\n options: { toolDisplay?: 'group' | 'none' | 'step' } = {\n toolDisplay: 'group',\n },\n) => {\n const authors = without(\n uniq(\n messages\n .map(message =>\n compact(\n message.content.map(\n part => part.type === 'text' && (part as any)?.author,\n ),\n ),\n )\n .flat(),\n ),\n 'user',\n );\n\n const uiMessages = messages.map<ThreadMessageLike>(cur => {\n let status: MessageStatus | undefined = undefined;\n if (cur.role === 'assistant') {\n status = ![MessageStatusEnum.PENDING, MessageStatusEnum.SENDING].includes(\n cur.status,\n )\n ? { type: 'complete', reason: 'stop' }\n : { type: 'running' };\n }\n\n const toolGroup: ToolCallMessagePart<Record<string, any>, null> = {\n type: 'tool-call',\n toolCallId: `tool-group-${cur.id}`,\n toolName: 'tool-group',\n args: {},\n argsText: JSON.stringify({}),\n };\n\n const reasoningParts: (Mutable<ReasoningMessagePart> & {\n author: string;\n authorIdx: number;\n })[] = [];\n\n const authorFirstMessage = new Set();\n const content = [...cur.content]\n .sort((a, b) => {\n const aAuthor = (a as any).author;\n const bAuthor = (b as any).author;\n if (aAuthor && bAuthor) {\n return authors.indexOf(aAuthor) - authors.indexOf(bAuthor);\n }\n return 0;\n })\n .reduce((acc, part, index) => {\n const lastPart = last(acc);\n const author = (part as any).author;\n const authorIdx = authors.indexOf(author);\n const showAvatar =\n authors.length >= 2 && !authorFirstMessage.has(author);\n const marginLeft =\n authors.length >= 2 && authorFirstMessage.has(author);\n\n if (author && showAvatar) {\n authorFirstMessage.add(author);\n }\n\n if (part.type === 'text') {\n if (\n lastPart &&\n lastPart.type === 'text' &&\n (lastPart as any).author === author\n ) {\n lastPart.text += '\\n' + part.text;\n } else {\n acc.push({\n type: part.type,\n text: part.text,\n author,\n authorIdx,\n ...(authors.length >= 2 && { showAvatar, marginLeft }),\n });\n }\n } else if (part.type === 'reasoning') {\n const lastReasoningPart = last(reasoningParts);\n if (\n lastReasoningPart &&\n (lastReasoningPart as any).author === author\n ) {\n lastReasoningPart.text += part.text;\n } else {\n reasoningParts.push({\n type: 'reasoning',\n text: part.text,\n author,\n authorIdx,\n ...(authors.length > 2 && { showAvatar, marginLeft }),\n });\n }\n } else if (part.type === 'image') {\n acc.push({\n type: part.type,\n image: part.imageUrl.url,\n ...(authors.length > 2 && { showAvatar, marginLeft }),\n });\n } else if (part.type === 'file') {\n const data =\n part.fileUrl ||\n `data:${part.mimeType};base64,${part.fileData}` ||\n '';\n acc.push({\n type: part.type,\n data,\n filename: part.name,\n mimeType: part.mimeType,\n ...(authors.length > 2 && { showAvatar, marginLeft }),\n });\n } else if (part.type === 'tool_call') {\n if (options.toolDisplay === 'group') {\n toolGroup.args[part.toolCallId] = {\n type: 'tool-call',\n index,\n toolCallId: part.toolCallId,\n toolName: part.toolCallName,\n args: part.toolCallArgs as Record<string, string>,\n argsText: JSON.stringify(part.toolCallArgs),\n result: part.content,\n author,\n authorIdx,\n ...(authors.length >= 2 && { showAvatar, marginLeft }),\n };\n } else if (options.toolDisplay === 'step') {\n acc.push({\n type: 'tool-call',\n toolCallId: part.toolCallId,\n toolName: part.toolCallName,\n args: part.toolCallArgs as Record<string, string>,\n argsText: JSON.stringify(part.toolCallArgs),\n result: part.content,\n author,\n authorIdx,\n ...(authors.length >= 2 && { showAvatar, marginLeft }),\n });\n }\n }\n return acc;\n }, [] as any[]);\n\n if (cur.status === MessageStatusEnum.ERROR || cur.error) {\n return {\n id: cur.id,\n role: cur.role,\n status: {\n type: 'incomplete',\n reason: 'error',\n error:\n (cur.error as { message: string }).message || (cur.error as any),\n },\n content: compact([\n cur.role === 'assistant' &&\n options.toolDisplay === 'group' &&\n keys(toolGroup.args).length > 0\n ? toolGroup\n : undefined,\n ...(content as ThreadAssistantMessagePart[]),\n ]),\n metadata: cur.metadata,\n };\n }\n\n const uiMessage: Mutable<\n Omit<ThreadMessageLike, 'content'> & {\n content: Exclude<ThreadMessageLike['content'], string>;\n }\n > = {\n id: cur.id,\n role: cur.role,\n status,\n attachments: cur.attaches?.map(item => ({\n status: { type: 'complete' },\n id: item.id,\n name: item.id,\n type: 'image',\n contentType: item.mimeType,\n content: [\n {\n type: 'image',\n image: item.url,\n },\n ],\n })),\n content: compact([\n ...reasoningParts,\n cur.role === 'assistant' &&\n options.toolDisplay === 'group' &&\n keys(toolGroup.args).length > 0\n ? toolGroup\n : undefined,\n ...(content as ThreadAssistantMessagePart[]),\n ]),\n metadata: {\n ...cur.metadata,\n invocationIds:\n keys(toolGroup.args).length > 0 ? keys(toolGroup.args) : undefined,\n } as any,\n };\n return uiMessage;\n });\n\n return { uiMessages, authors };\n};\n\nexport default convertToAssistantMessage;\n"],"names":["MessageStatusEnum"],"mappings":";;;;;;;;;AAgBA,MAAM,yBAAyB,GAAG,CAChC,QAAiC,EACjC,OAAuD,GAAA;AACrD,IAAA,WAAW,EAAE,OAAO;AACrB,CAAA,KACC;AACF,IAAA,MAAM,OAAO,GAAG,OAAO,CACrB,IAAI,CACF,QAAQ;AACL,SAAA,GAAG,CAAC,OAAO,IACV,OAAO,CACL,OAAO,CAAC,OAAO,CAAC,GAAG,CACjB,IAAI,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,IAAK,IAAY,EAAE,MAAM,CACtD,CACF,CACF;AACA,SAAA,IAAI,EAAE,CACV,EACD,MAAM,CACP,CAAC;IAEF,MAAM,UAAU,GAAG,QAAQ,CAAC,GAAG,CAAoB,GAAG,IAAG;QACvD,IAAI,MAAM,GAA8B,SAAS,CAAC;AAClD,QAAA,IAAI,GAAG,CAAC,IAAI,KAAK,WAAW,EAAE;AAC5B,YAAA,MAAM,GAAG,CAAC,CAACA,mBAAiB,CAAC,OAAO,EAAEA,mBAAiB,CAAC,OAAO,CAAC,CAAC,QAAQ,CACvE,GAAG,CAAC,MAAM,CACX;kBACG,EAAE,IAAI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E,UAAU,EAAE,OAAO,EAAE,CAAC;AACjC;;;;"}
1
+ {"version":3,"file":"convertToAssistantMessage.cjs","sources":["../../src/utils/convertToAssistantMessage.ts"],"sourcesContent":["import {\n CompleteAttachment,\n MessageStatus,\n ReasoningMessagePart,\n ThreadAssistantMessagePart,\n ThreadMessageLike,\n ToolCallMessagePart,\n} from '@assistant-ui/react';\nimport {\n ChatMessage,\n MessageStatus as MessageStatusEnum,\n} from '@chat-lab/core';\nimport { compact, keys, last, uniq, uniqueId, without } from 'lodash-es';\ntype Mutable<T> = {\n -readonly [K in keyof T]: T[K]; // -readonly 表示移除 readonly 修饰符\n};\n\nconst convertToAssistantMessage = (\n messages: Readonly<ChatMessage>[],\n options: { toolDisplay?: 'group' | 'none' | 'step' } = {\n toolDisplay: 'group',\n },\n) => {\n const authors = without(\n uniq(\n messages\n .map(message =>\n compact(\n message.content.map(\n part => part.type === 'text' && (part as any)?.author,\n ),\n ),\n )\n .flat(),\n ),\n 'user',\n );\n\n const uiMessages = messages.map<ThreadMessageLike>(cur => {\n let status: MessageStatus | undefined = undefined;\n if (cur.role === 'assistant') {\n status = ![MessageStatusEnum.PENDING, MessageStatusEnum.SENDING].includes(\n cur.status,\n )\n ? { type: 'complete', reason: 'stop' }\n : { type: 'running' };\n }\n\n const toolGroup: ToolCallMessagePart<Record<string, any>, null> = {\n type: 'tool-call',\n toolCallId: `tool-group-${cur.id}`,\n toolName: 'tool-group',\n args: {},\n argsText: JSON.stringify({}),\n };\n\n const reasoningParts: (Mutable<ReasoningMessagePart> & {\n author: string;\n authorIdx: number;\n })[] = [];\n\n const authorFirstMessage = new Set();\n\n const attaches: CompleteAttachment[] = [];\n const content = [...cur.content]\n .sort((a, b) => {\n const aAuthor = (a as any).author;\n const bAuthor = (b as any).author;\n if (aAuthor && bAuthor) {\n return authors.indexOf(aAuthor) - authors.indexOf(bAuthor);\n }\n return 0;\n })\n .reduce((acc, part, index) => {\n const lastPart = last(acc);\n const author = (part as any).author;\n const authorIdx = authors.indexOf(author);\n const showAvatar =\n authors.length >= 2 && !authorFirstMessage.has(author);\n const marginLeft =\n authors.length >= 2 && authorFirstMessage.has(author);\n\n if (author && showAvatar) {\n authorFirstMessage.add(author);\n }\n\n if (part.type === 'text') {\n try {\n const text = part.text;\n if (\n text?.[0] === '[' &&\n text?.[text.length - 1] === ']' &&\n cur.role === 'user'\n ) {\n const files = text.slice(1, -1).split(',');\n files.forEach(file => {\n attaches.push({\n id: uniqueId(),\n type: 'file',\n status: {\n type: 'complete',\n },\n contentType: 'plain/text',\n name: file,\n content: [],\n });\n });\n\n return acc;\n }\n } catch (e) {}\n if (\n lastPart &&\n lastPart.type === 'text' &&\n (lastPart as any).author === author\n ) {\n lastPart.text += '\\n' + part.text;\n } else {\n acc.push({\n type: part.type,\n text: part.text,\n author,\n authorIdx,\n ...(authors.length >= 2 && { showAvatar, marginLeft }),\n });\n }\n } else if (part.type === 'reasoning') {\n const lastReasoningPart = last(reasoningParts);\n if (\n lastReasoningPart &&\n (lastReasoningPart as any).author === author\n ) {\n lastReasoningPart.text += part.text;\n } else {\n reasoningParts.push({\n type: 'reasoning',\n text: part.text,\n author,\n authorIdx,\n ...(authors.length > 2 && { showAvatar, marginLeft }),\n });\n }\n } else if (part.type === 'image') {\n attaches.push({\n type: part.type,\n image: part.imageUrl.url,\n ...(authors.length > 2 && { showAvatar, marginLeft }),\n });\n } else if (part.type === 'file') {\n const data =\n part.fileUrl ||\n `data:${part.mimeType};base64,${part.fileData}` ||\n '';\n attaches.push({\n type: part.type,\n data,\n filename: part.name,\n mimeType: part.mimeType,\n // ...(authors.length > 2 && { showAvatar, marginLeft }),\n });\n } else if (part.type === 'tool_call') {\n if (options.toolDisplay === 'group') {\n toolGroup.args[part.toolCallId] = {\n type: 'tool-call',\n index,\n toolCallId: part.toolCallId,\n toolName: part.toolCallName,\n args: part.toolCallArgs as Record<string, string>,\n argsText: JSON.stringify(part.toolCallArgs),\n result: part.content,\n author,\n authorIdx,\n ...(authors.length >= 2 && { showAvatar, marginLeft }),\n };\n } else if (options.toolDisplay === 'step') {\n acc.push({\n type: 'tool-call',\n toolCallId: part.toolCallId,\n toolName: part.toolCallName,\n args: part.toolCallArgs as Record<string, string>,\n argsText: JSON.stringify(part.toolCallArgs),\n result: part.content,\n author,\n authorIdx,\n ...(authors.length >= 2 && { showAvatar, marginLeft }),\n });\n }\n }\n return acc;\n }, [] as any[]);\n\n if (cur.status === MessageStatusEnum.ERROR || cur.error) {\n return {\n id: cur.id,\n role: cur.role,\n status: {\n type: 'incomplete',\n reason: 'error',\n error:\n (cur.error as { message: string }).message || (cur.error as any),\n },\n content: compact([\n cur.role === 'assistant' &&\n options.toolDisplay === 'group' &&\n keys(toolGroup.args).length > 0\n ? toolGroup\n : undefined,\n ...(content as ThreadAssistantMessagePart[]),\n ]),\n metadata: cur.metadata,\n };\n }\n\n const uiMessage: Mutable<\n Omit<ThreadMessageLike, 'content'> & {\n content: Exclude<ThreadMessageLike['content'], string>;\n }\n > = {\n id: cur.id,\n role: cur.role,\n status,\n attachments: [\n ...((cur.attaches?.map(item => ({\n status: { type: 'complete' },\n id: item.id,\n name: item.id,\n type: 'file',\n contentType: item.mimeType,\n content: [\n {\n type: item.type,\n image: item.url,\n },\n ],\n })) || []) as CompleteAttachment[]),\n ...attaches,\n ],\n content: compact([\n ...reasoningParts,\n cur.role === 'assistant' &&\n options.toolDisplay === 'group' &&\n keys(toolGroup.args).length > 0\n ? toolGroup\n : undefined,\n ...(content as ThreadAssistantMessagePart[]),\n ]),\n metadata: {\n ...cur.metadata,\n invocationIds:\n keys(toolGroup.args).length > 0 ? keys(toolGroup.args) : undefined,\n } as any,\n };\n return uiMessage;\n });\n\n console.log(uiMessages, messages);\n return { uiMessages, authors };\n};\n\nexport default convertToAssistantMessage;\n"],"names":["MessageStatusEnum"],"mappings":";;;;;;;;;;AAiBA,MAAM,yBAAyB,GAAG,CAChC,QAAiC,EACjC,OAAuD,GAAA;AACrD,IAAA,WAAW,EAAE,OA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AS,CAAC,IAAI,CAAC,CAAC,MAAM,GAAG,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,SAAS;AAC9D,aAAA;SACT,CAAC;AACF,QAAA,OAAO,SAAS,CAAC;AACnB,KAAC,CAAC,CAAC;AAEH,IAAA,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC;AAClC,IAAA,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,CAAC;AACjC;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"convertToAssistantMessage.d.ts","sourceRoot":"","sources":["../../src/utils/convertToAssistantMessage.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,iBAAiB,EAElB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,WAAW,EAEZ,MAAM,gBAAgB,CAAC;AAMxB,QAAA,MAAM,yBAAyB,GAC7B,UAAU,QAAQ,CAAC,WAAW,CAAC,EAAE,EACjC,UAAS;IAAE,WAAW,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,CAAA;CAEjD;;;CA8MF,CAAC;AAEF,eAAe,yBAAyB,CAAC"}
1
+ {"version":3,"file":"convertToAssistantMessage.d.ts","sourceRoot":"","sources":["../../src/utils/convertToAssistantMessage.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,iBAAiB,EAElB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,WAAW,EAEZ,MAAM,gBAAgB,CAAC;AAMxB,QAAA,MAAM,yBAAyB,GAC7B,UAAU,QAAQ,CAAC,WAAW,CAAC,EAAE,EACjC,UAAS;IAAE,WAAW,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,CAAA;CAEjD;;;CA4OF,CAAC;AAEF,eAAe,yBAAyB,CAAC"}
@@ -3,6 +3,7 @@ import without from '../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash
3
3
  import uniq from '../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/uniq.js';
4
4
  import compact from '../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/compact.js';
5
5
  import last from '../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/last.js';
6
+ import uniqueId from '../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/uniqueId.js';
6
7
  import keys from '../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/keys.js';
7
8
 
8
9
  const convertToAssistantMessage = (messages, options = {
@@ -27,6 +28,7 @@ const convertToAssistantMessage = (messages, options = {
27
28
  };
28
29
  const reasoningParts = [];
29
30
  const authorFirstMessage = new Set();
31
+ const attaches = [];
30
32
  const content = [...cur.content]
31
33
  .sort((a, b) => {
32
34
  const aAuthor = a.author;
@@ -46,6 +48,28 @@ const convertToAssistantMessage = (messages, options = {
46
48
  authorFirstMessage.add(author);
47
49
  }
48
50
  if (part.type === 'text') {
51
+ try {
52
+ const text = part.text;
53
+ if (text?.[0] === '[' &&
54
+ text?.[text.length - 1] === ']' &&
55
+ cur.role === 'user') {
56
+ const files = text.slice(1, -1).split(',');
57
+ files.forEach(file => {
58
+ attaches.push({
59
+ id: uniqueId(),
60
+ type: 'file',
61
+ status: {
62
+ type: 'complete',
63
+ },
64
+ contentType: 'plain/text',
65
+ name: file,
66
+ content: [],
67
+ });
68
+ });
69
+ return acc;
70
+ }
71
+ }
72
+ catch (e) { }
49
73
  if (lastPart &&
50
74
  lastPart.type === 'text' &&
51
75
  lastPart.author === author) {
@@ -78,7 +102,7 @@ const convertToAssistantMessage = (messages, options = {
78
102
  }
79
103
  }
80
104
  else if (part.type === 'image') {
81
- acc.push({
105
+ attaches.push({
82
106
  type: part.type,
83
107
  image: part.imageUrl.url,
84
108
  ...(authors.length > 2 && { showAvatar, marginLeft }),
@@ -88,12 +112,12 @@ const convertToAssistantMessage = (messages, options = {
88
112
  const data = part.fileUrl ||
89
113
  `data:${part.mimeType};base64,${part.fileData}` ||
90
114
  '';
91
- acc.push({
115
+ attaches.push({
92
116
  type: part.type,
93
117
  data,
94
118
  filename: part.name,
95
119
  mimeType: part.mimeType,
96
- ...(authors.length > 2 && { showAvatar, marginLeft }),
120
+ // ...(authors.length > 2 && { showAvatar, marginLeft }),
97
121
  });
98
122
  }
99
123
  else if (part.type === 'tool_call') {
@@ -151,19 +175,22 @@ const convertToAssistantMessage = (messages, options = {
151
175
  id: cur.id,
152
176
  role: cur.role,
153
177
  status,
154
- attachments: cur.attaches?.map(item => ({
155
- status: { type: 'complete' },
156
- id: item.id,
157
- name: item.id,
158
- type: 'image',
159
- contentType: item.mimeType,
160
- content: [
161
- {
162
- type: 'image',
163
- image: item.url,
164
- },
165
- ],
166
- })),
178
+ attachments: [
179
+ ...(cur.attaches?.map(item => ({
180
+ status: { type: 'complete' },
181
+ id: item.id,
182
+ name: item.id,
183
+ type: 'file',
184
+ contentType: item.mimeType,
185
+ content: [
186
+ {
187
+ type: item.type,
188
+ image: item.url,
189
+ },
190
+ ],
191
+ })) || []),
192
+ ...attaches,
193
+ ],
167
194
  content: compact([
168
195
  ...reasoningParts,
169
196
  cur.role === 'assistant' &&
@@ -180,6 +207,7 @@ const convertToAssistantMessage = (messages, options = {
180
207
  };
181
208
  return uiMessage;
182
209
  });
210
+ console.log(uiMessages, messages);
183
211
  return { uiMessages, authors };
184
212
  };
185
213
 
@@ -1 +1 @@
1
- {"version":3,"file":"convertToAssistantMessage.js","sources":["../../src/utils/convertToAssistantMessage.ts"],"sourcesContent":["import {\n MessageStatus,\n ReasoningMessagePart,\n ThreadAssistantMessagePart,\n ThreadMessageLike,\n ToolCallMessagePart,\n} from '@assistant-ui/react';\nimport {\n ChatMessage,\n MessageStatus as MessageStatusEnum,\n} from '@chat-lab/core';\nimport { compact, keys, last, uniq, without } from 'lodash-es';\ntype Mutable<T> = {\n -readonly [K in keyof T]: T[K]; // -readonly 表示移除 readonly 修饰符\n};\n\nconst convertToAssistantMessage = (\n messages: Readonly<ChatMessage>[],\n options: { toolDisplay?: 'group' | 'none' | 'step' } = {\n toolDisplay: 'group',\n },\n) => {\n const authors = without(\n uniq(\n messages\n .map(message =>\n compact(\n message.content.map(\n part => part.type === 'text' && (part as any)?.author,\n ),\n ),\n )\n .flat(),\n ),\n 'user',\n );\n\n const uiMessages = messages.map<ThreadMessageLike>(cur => {\n let status: MessageStatus | undefined = undefined;\n if (cur.role === 'assistant') {\n status = ![MessageStatusEnum.PENDING, MessageStatusEnum.SENDING].includes(\n cur.status,\n )\n ? { type: 'complete', reason: 'stop' }\n : { type: 'running' };\n }\n\n const toolGroup: ToolCallMessagePart<Record<string, any>, null> = {\n type: 'tool-call',\n toolCallId: `tool-group-${cur.id}`,\n toolName: 'tool-group',\n args: {},\n argsText: JSON.stringify({}),\n };\n\n const reasoningParts: (Mutable<ReasoningMessagePart> & {\n author: string;\n authorIdx: number;\n })[] = [];\n\n const authorFirstMessage = new Set();\n const content = [...cur.content]\n .sort((a, b) => {\n const aAuthor = (a as any).author;\n const bAuthor = (b as any).author;\n if (aAuthor && bAuthor) {\n return authors.indexOf(aAuthor) - authors.indexOf(bAuthor);\n }\n return 0;\n })\n .reduce((acc, part, index) => {\n const lastPart = last(acc);\n const author = (part as any).author;\n const authorIdx = authors.indexOf(author);\n const showAvatar =\n authors.length >= 2 && !authorFirstMessage.has(author);\n const marginLeft =\n authors.length >= 2 && authorFirstMessage.has(author);\n\n if (author && showAvatar) {\n authorFirstMessage.add(author);\n }\n\n if (part.type === 'text') {\n if (\n lastPart &&\n lastPart.type === 'text' &&\n (lastPart as any).author === author\n ) {\n lastPart.text += '\\n' + part.text;\n } else {\n acc.push({\n type: part.type,\n text: part.text,\n author,\n authorIdx,\n ...(authors.length >= 2 && { showAvatar, marginLeft }),\n });\n }\n } else if (part.type === 'reasoning') {\n const lastReasoningPart = last(reasoningParts);\n if (\n lastReasoningPart &&\n (lastReasoningPart as any).author === author\n ) {\n lastReasoningPart.text += part.text;\n } else {\n reasoningParts.push({\n type: 'reasoning',\n text: part.text,\n author,\n authorIdx,\n ...(authors.length > 2 && { showAvatar, marginLeft }),\n });\n }\n } else if (part.type === 'image') {\n acc.push({\n type: part.type,\n image: part.imageUrl.url,\n ...(authors.length > 2 && { showAvatar, marginLeft }),\n });\n } else if (part.type === 'file') {\n const data =\n part.fileUrl ||\n `data:${part.mimeType};base64,${part.fileData}` ||\n '';\n acc.push({\n type: part.type,\n data,\n filename: part.name,\n mimeType: part.mimeType,\n ...(authors.length > 2 && { showAvatar, marginLeft }),\n });\n } else if (part.type === 'tool_call') {\n if (options.toolDisplay === 'group') {\n toolGroup.args[part.toolCallId] = {\n type: 'tool-call',\n index,\n toolCallId: part.toolCallId,\n toolName: part.toolCallName,\n args: part.toolCallArgs as Record<string, string>,\n argsText: JSON.stringify(part.toolCallArgs),\n result: part.content,\n author,\n authorIdx,\n ...(authors.length >= 2 && { showAvatar, marginLeft }),\n };\n } else if (options.toolDisplay === 'step') {\n acc.push({\n type: 'tool-call',\n toolCallId: part.toolCallId,\n toolName: part.toolCallName,\n args: part.toolCallArgs as Record<string, string>,\n argsText: JSON.stringify(part.toolCallArgs),\n result: part.content,\n author,\n authorIdx,\n ...(authors.length >= 2 && { showAvatar, marginLeft }),\n });\n }\n }\n return acc;\n }, [] as any[]);\n\n if (cur.status === MessageStatusEnum.ERROR || cur.error) {\n return {\n id: cur.id,\n role: cur.role,\n status: {\n type: 'incomplete',\n reason: 'error',\n error:\n (cur.error as { message: string }).message || (cur.error as any),\n },\n content: compact([\n cur.role === 'assistant' &&\n options.toolDisplay === 'group' &&\n keys(toolGroup.args).length > 0\n ? toolGroup\n : undefined,\n ...(content as ThreadAssistantMessagePart[]),\n ]),\n metadata: cur.metadata,\n };\n }\n\n const uiMessage: Mutable<\n Omit<ThreadMessageLike, 'content'> & {\n content: Exclude<ThreadMessageLike['content'], string>;\n }\n > = {\n id: cur.id,\n role: cur.role,\n status,\n attachments: cur.attaches?.map(item => ({\n status: { type: 'complete' },\n id: item.id,\n name: item.id,\n type: 'image',\n contentType: item.mimeType,\n content: [\n {\n type: 'image',\n image: item.url,\n },\n ],\n })),\n content: compact([\n ...reasoningParts,\n cur.role === 'assistant' &&\n options.toolDisplay === 'group' &&\n keys(toolGroup.args).length > 0\n ? toolGroup\n : undefined,\n ...(content as ThreadAssistantMessagePart[]),\n ]),\n metadata: {\n ...cur.metadata,\n invocationIds:\n keys(toolGroup.args).length > 0 ? keys(toolGroup.args) : undefined,\n } as any,\n };\n return uiMessage;\n });\n\n return { uiMessages, authors };\n};\n\nexport default convertToAssistantMessage;\n"],"names":["MessageStatusEnum"],"mappings":";;;;;;;AAgBA,MAAM,yBAAyB,GAAG,CAChC,QAAiC,EACjC,OAAuD,GAAA;AACrD,IAAA,WAAW,EAAE,OAAO;AACrB,CAAA,KACC;AACF,IAAA,MAAM,OAAO,GAAG,OAAO,CACrB,IAAI,CACF,QAAQ;AACL,SAAA,GAAG,CAAC,OAAO,IACV,OAAO,CACL,OAAO,CAAC,OAAO,CAAC,GAAG,CACjB,IAAI,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,IAAK,IAAY,EAAE,MAAM,CACtD,CACF,CACF;AACA,SAAA,IAAI,EAAE,CACV,EACD,MAAM,CACP,CAAC;IAEF,MAAM,UAAU,GAAG,QAAQ,CAAC,GAAG,CAAoB,GAAG,IAAG;QACvD,IAAI,MAAM,GAA8B,SAAS,CAAC;AAClD,QAAA,IAAI,GAAG,CAAC,IAAI,KAAK,WAAW,EAAE;AAC5B,YAAA,MAAM,GAAG,CAAC,CAACA,aAAiB,CAAC,OAAO,EAAEA,aAAiB,CAAC,OAAO,CAAC,CAAC,QAAQ,CACvE,GAAG,CAAC,MAAM,CACX;kBACG,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE;AACtC,kBAAE,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;SACzB;AAED,QAAA,MAAM,SAAS,GAAmD;AAChE,YAAA,IAAI,EAAE,WAAW;AACjB,YAAA,UAAU,EAAE,CAAA,WAAA,EAAc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AAC,EAAE;YACV,IAAI,EAAE,GAAG,CAAC,IAAI;YACd,MAAM;YACN,WAAW,EAAE,GAAG,CAAC,QAAQ,EAAE,GAAG,CAAC,IAAI,KAAK;AACtC,gBAAA,MAAM,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE;gBAC5B,EAAE,EAAE,IAAI,CAAC,EAAE;gBACX,IAAI,EAAE,IAAI,CAAC,EAAE;AACb,gBAAA,IAAI,EAAE,OAAO;gBACb,WAAW,EAAE,IAAI,CAAC,QAAQ;AAC1B,gBAAA,OAAO,EAAE;AACP,oBAAA;AACE,wBAAA,IAAI,EAAE,OAAO;wBACb,KAAK,EAAE,IAAI,CAAC,GAAG;AAChB,qBAAA;AACF,iBAAA;AACF,aAAA,CAAC,CAAC;YACH,OAAO,EAAE,OAAO,CAAC;AACf,gBAAA,GAAG,cAAc;gBACjB,GAAG,CAAC,IAAI,KAAK,WAAW;oBACxB,OAAO,CAAC,WAAW,KAAK,OAAO;oBAC/B,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,MAAM,GAAG,CAAC;AAC7B,sBAAE,SAAS;AACX,sBAAE,SAAS;AACb,gBAAA,GAAI,OAAwC;aAC7C,CAAC;AACF,YAAA,QAAQ,EAAE;gBACR,GAAG,GAAG,CAAC,QAAQ;gBACf,aAAa,EACX,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,MAAM,GAAG,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,SAAS;AAC9D,aAAA;SACT,CAAC;AACF,QAAA,OAAO,SAAS,CAAC;AACnB,KAAC,CAAC,CAAC;AAEH,IAAA,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,CAAC;AACjC;;;;"}
1
+ {"version":3,"file":"convertToAssistantMessage.js","sources":["../../src/utils/convertToAssistantMessage.ts"],"sourcesContent":["import {\n CompleteAttachment,\n MessageStatus,\n ReasoningMessagePart,\n ThreadAssistantMessagePart,\n ThreadMessageLike,\n ToolCallMessagePart,\n} from '@assistant-ui/react';\nimport {\n ChatMessage,\n MessageStatus as MessageStatusEnum,\n} from '@chat-lab/core';\nimport { compact, keys, last, uniq, uniqueId, without } from 'lodash-es';\ntype Mutable<T> = {\n -readonly [K in keyof T]: T[K]; // -readonly 表示移除 readonly 修饰符\n};\n\nconst convertToAssistantMessage = (\n messages: Readonly<ChatMessage>[],\n options: { toolDisplay?: 'group' | 'none' | 'step' } = {\n toolDisplay: 'group',\n },\n) => {\n const authors = without(\n uniq(\n messages\n .map(message =>\n compact(\n message.content.map(\n part => part.type === 'text' && (part as any)?.author,\n ),\n ),\n )\n .flat(),\n ),\n 'user',\n );\n\n const uiMessages = messages.map<ThreadMessageLike>(cur => {\n let status: MessageStatus | undefined = undefined;\n if (cur.role === 'assistant') {\n status = ![MessageStatusEnum.PENDING, MessageStatusEnum.SENDING].includes(\n cur.status,\n )\n ? { type: 'complete', reason: 'stop' }\n : { type: 'running' };\n }\n\n const toolGroup: ToolCallMessagePart<Record<string, any>, null> = {\n type: 'tool-call',\n toolCallId: `tool-group-${cur.id}`,\n toolName: 'tool-group',\n args: {},\n argsText: JSON.stringify({}),\n };\n\n const reasoningParts: (Mutable<ReasoningMessagePart> & {\n author: string;\n authorIdx: number;\n })[] = [];\n\n const authorFirstMessage = new Set();\n\n const attaches: CompleteAttachment[] = [];\n const content = [...cur.content]\n .sort((a, b) => {\n const aAuthor = (a as any).author;\n const bAuthor = (b as any).author;\n if (aAuthor && bAuthor) {\n return authors.indexOf(aAuthor) - authors.indexOf(bAuthor);\n }\n return 0;\n })\n .reduce((acc, part, index) => {\n const lastPart = last(acc);\n const author = (part as any).author;\n const authorIdx = authors.indexOf(author);\n const showAvatar =\n authors.length >= 2 && !authorFirstMessage.has(author);\n const marginLeft =\n authors.length >= 2 && authorFirstMessage.has(author);\n\n if (author && showAvatar) {\n authorFirstMessage.add(author);\n }\n\n if (part.type === 'text') {\n try {\n const text = part.text;\n if (\n text?.[0] === '[' &&\n text?.[text.length - 1] === ']' &&\n cur.role === 'user'\n ) {\n const files = text.slice(1, -1).split(',');\n files.forEach(file => {\n attaches.push({\n id: uniqueId(),\n type: 'file',\n status: {\n type: 'complete',\n },\n contentType: 'plain/text',\n name: file,\n content: [],\n });\n });\n\n return acc;\n }\n } catch (e) {}\n if (\n lastPart &&\n lastPart.type === 'text' &&\n (lastPart as any).author === author\n ) {\n lastPart.text += '\\n' + part.text;\n } else {\n acc.push({\n type: part.type,\n text: part.text,\n author,\n authorIdx,\n ...(authors.length >= 2 && { showAvatar, marginLeft }),\n });\n }\n } else if (part.type === 'reasoning') {\n const lastReasoningPart = last(reasoningParts);\n if (\n lastReasoningPart &&\n (lastReasoningPart as any).author === author\n ) {\n lastReasoningPart.text += part.text;\n } else {\n reasoningParts.push({\n type: 'reasoning',\n text: part.text,\n author,\n authorIdx,\n ...(authors.length > 2 && { showAvatar, marginLeft }),\n });\n }\n } else if (part.type === 'image') {\n attaches.push({\n type: part.type,\n image: part.imageUrl.url,\n ...(authors.length > 2 && { showAvatar, marginLeft }),\n });\n } else if (part.type === 'file') {\n const data =\n part.fileUrl ||\n `data:${part.mimeType};base64,${part.fileData}` ||\n '';\n attaches.push({\n type: part.type,\n data,\n filename: part.name,\n mimeType: part.mimeType,\n // ...(authors.length > 2 && { showAvatar, marginLeft }),\n });\n } else if (part.type === 'tool_call') {\n if (options.toolDisplay === 'group') {\n toolGroup.args[part.toolCallId] = {\n type: 'tool-call',\n index,\n toolCallId: part.toolCallId,\n toolName: part.toolCallName,\n args: part.toolCallArgs as Record<string, string>,\n argsText: JSON.stringify(part.toolCallArgs),\n result: part.content,\n author,\n authorIdx,\n ...(authors.length >= 2 && { showAvatar, marginLeft }),\n };\n } else if (options.toolDisplay === 'step') {\n acc.push({\n type: 'tool-call',\n toolCallId: part.toolCallId,\n toolName: part.toolCallName,\n args: part.toolCallArgs as Record<string, string>,\n argsText: JSON.stringify(part.toolCallArgs),\n result: part.content,\n author,\n authorIdx,\n ...(authors.length >= 2 && { showAvatar, marginLeft }),\n });\n }\n }\n return acc;\n }, [] as any[]);\n\n if (cur.status === MessageStatusEnum.ERROR || cur.error) {\n return {\n id: cur.id,\n role: cur.role,\n status: {\n type: 'incomplete',\n reason: 'error',\n error:\n (cur.error as { message: string }).message || (cur.error as any),\n },\n content: compact([\n cur.role === 'assistant' &&\n options.toolDisplay === 'group' &&\n keys(toolGroup.args).length > 0\n ? toolGroup\n : undefined,\n ...(content as ThreadAssistantMessagePart[]),\n ]),\n metadata: cur.metadata,\n };\n }\n\n const uiMessage: Mutable<\n Omit<ThreadMessageLike, 'content'> & {\n content: Exclude<ThreadMessageLike['content'], string>;\n }\n > = {\n id: cur.id,\n role: cur.role,\n status,\n attachments: [\n ...((cur.attaches?.map(item => ({\n status: { type: 'complete' },\n id: item.id,\n name: item.id,\n type: 'file',\n contentType: item.mimeType,\n content: [\n {\n type: item.type,\n image: item.url,\n },\n ],\n })) || []) as CompleteAttachment[]),\n ...attaches,\n ],\n content: compact([\n ...reasoningParts,\n cur.role === 'assistant' &&\n options.toolDisplay === 'group' &&\n keys(toolGroup.args).length > 0\n ? toolGroup\n : undefined,\n ...(content as ThreadAssistantMessagePart[]),\n ]),\n metadata: {\n ...cur.metadata,\n invocationIds:\n keys(toolGroup.args).length > 0 ? keys(toolGroup.args) : undefined,\n } as any,\n };\n return uiMessage;\n });\n\n console.log(uiMessages, messages);\n return { uiMessages, authors };\n};\n\nexport default convertToAssistantMessage;\n"],"names":["MessageStatusEnum"],"mappings":";;;;;;;;AAiBA,MAAM,yBAAyB,GAAG,CAChC,QAAiC,EACjC,OAAuD,GAAA;AACrD,IAAA,WAAW,EAAE,OAAO;AACrB,CAAA,KACC;AACF,IAAA,MAAM,OAAO,GAAG,OAAO,CACrB,IAAI,CACF,QAAQ;AACL,SAAA,GAAG,CAAC,OAAO,IACV,OAAO,CACL,OAAO,CAAC,OAAO,CAAC,GAAG,CACjB,IAAI,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,IAA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AC,UAAU,EAAE,QAAQ,CAAC,CAAC;AAClC,IAAA,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,CAAC;AACjC;;;;"}
@@ -0,0 +1,31 @@
1
+ 'use strict';
2
+
3
+ function deepAssign(a, b) {
4
+ // 创建一个新对象,避免直接修改原对象
5
+ const result = { ...a };
6
+ // 遍历 b 中的所有键
7
+ for (const key in b) {
8
+ if (Object.prototype.hasOwnProperty.call(b, key)) {
9
+ const value = b[key];
10
+ const aValue = result[key];
11
+ // 如果 b 的值是对象且不是 null,并且 a 中对应的值也是对象
12
+ if (typeof value === 'object' &&
13
+ value !== null &&
14
+ !Array.isArray(value) &&
15
+ typeof aValue === 'object' &&
16
+ aValue !== null &&
17
+ !Array.isArray(aValue)) {
18
+ // 递归合并嵌套对象
19
+ result[key] = deepAssign(aValue, value);
20
+ }
21
+ else {
22
+ // 直接覆盖值(包括数组、基本类型和 null)
23
+ result[key] = value;
24
+ }
25
+ }
26
+ }
27
+ return result;
28
+ }
29
+
30
+ exports.deepAssign = deepAssign;
31
+ //# sourceMappingURL=deepAssign.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deepAssign.cjs","sources":["../../src/utils/deepAssign.ts"],"sourcesContent":["export function deepAssign<T extends object>(a: T, b: Partial<T>): T {\n // 创建一个新对象,避免直接修改原对象\n const result: T = { ...a };\n\n // 遍历 b 中的所有键\n for (const key in b) {\n if (Object.prototype.hasOwnProperty.call(b, key)) {\n const value = b[key];\n const aValue = result[key];\n\n // 如果 b 的值是对象且不是 null,并且 a 中对应的值也是对象\n if (\n typeof value === 'object' &&\n value !== null &&\n !Array.isArray(value) &&\n typeof aValue === 'object' &&\n aValue !== null &&\n !Array.isArray(aValue)\n ) {\n // 递归合并嵌套对象\n result[key] = deepAssign(aValue, value);\n } else {\n // 直接覆盖值(包括数组、基本类型和 null)\n (result as any)[key] = value;\n }\n }\n }\n\n return result as T;\n}\n"],"names":[],"mappings":";;AAAgB,SAAA,UAAU,CAAmB,CAAI,EAAE,CAAa,EAAA;;AAE9D,IAAA,MAAM,MAAM,GAAM,EAAE,GAAG,CAAC,EAAE,CAAC;;AAG3B,IAAA,KAAK,MAAM,GAAG,IAAI,CAAC,EAAE;AACnB,QAAA,IAAI,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE;AAChD,YAAA,MAAM,KAAK,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;AACrB,YAAA,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;;YAG3B,IACE,OAAO,KAAK,KAAK,QAAQ;AACzB,gBAAA,KAAK,KAAK,IAAI;AACd,gBAAA,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;gBACrB,OAAO,MAAM,KAAK,QAAQ;AAC1B,gBAAA,MAAM,KAAK,IAAI;AACf,gBAAA,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EACtB;;gBAEA,MAAM,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;aACzC;iBAAM;;AAEJ,gBAAA,MAAc,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;aAC9B;SACF;KACF;AAED,IAAA,OAAO,MAAW,CAAC;AACrB;;;;"}
@@ -0,0 +1,2 @@
1
+ export declare function deepAssign<T extends object>(a: T, b: Partial<T>): T;
2
+ //# sourceMappingURL=deepAssign.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deepAssign.d.ts","sourceRoot":"","sources":["../../src/utils/deepAssign.ts"],"names":[],"mappings":"AAAA,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CA6BnE"}
@@ -0,0 +1,29 @@
1
+ function deepAssign(a, b) {
2
+ // 创建一个新对象,避免直接修改原对象
3
+ const result = { ...a };
4
+ // 遍历 b 中的所有键
5
+ for (const key in b) {
6
+ if (Object.prototype.hasOwnProperty.call(b, key)) {
7
+ const value = b[key];
8
+ const aValue = result[key];
9
+ // 如果 b 的值是对象且不是 null,并且 a 中对应的值也是对象
10
+ if (typeof value === 'object' &&
11
+ value !== null &&
12
+ !Array.isArray(value) &&
13
+ typeof aValue === 'object' &&
14
+ aValue !== null &&
15
+ !Array.isArray(aValue)) {
16
+ // 递归合并嵌套对象
17
+ result[key] = deepAssign(aValue, value);
18
+ }
19
+ else {
20
+ // 直接覆盖值(包括数组、基本类型和 null)
21
+ result[key] = value;
22
+ }
23
+ }
24
+ }
25
+ return result;
26
+ }
27
+
28
+ export { deepAssign };
29
+ //# sourceMappingURL=deepAssign.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deepAssign.js","sources":["../../src/utils/deepAssign.ts"],"sourcesContent":["export function deepAssign<T extends object>(a: T, b: Partial<T>): T {\n // 创建一个新对象,避免直接修改原对象\n const result: T = { ...a };\n\n // 遍历 b 中的所有键\n for (const key in b) {\n if (Object.prototype.hasOwnProperty.call(b, key)) {\n const value = b[key];\n const aValue = result[key];\n\n // 如果 b 的值是对象且不是 null,并且 a 中对应的值也是对象\n if (\n typeof value === 'object' &&\n value !== null &&\n !Array.isArray(value) &&\n typeof aValue === 'object' &&\n aValue !== null &&\n !Array.isArray(aValue)\n ) {\n // 递归合并嵌套对象\n result[key] = deepAssign(aValue, value);\n } else {\n // 直接覆盖值(包括数组、基本类型和 null)\n (result as any)[key] = value;\n }\n }\n }\n\n return result as T;\n}\n"],"names":[],"mappings":"AAAgB,SAAA,UAAU,CAAmB,CAAI,EAAE,CAAa,EAAA;;AAE9D,IAAA,MAAM,MAAM,GAAM,EAAE,GAAG,CAAC,EAAE,CAAC;;AAG3B,IAAA,KAAK,MAAM,GAAG,IAAI,CAAC,EAAE;AACnB,QAAA,IAAI,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE;AAChD,YAAA,MAAM,KAAK,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;AACrB,YAAA,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;;YAG3B,IACE,OAAO,KAAK,KAAK,QAAQ;AACzB,gBAAA,KAAK,KAAK,IAAI;AACd,gBAAA,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;gBACrB,OAAO,MAAM,KAAK,QAAQ;AAC1B,gBAAA,MAAM,KAAK,IAAI;AACf,gBAAA,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EACtB;;gBAEA,MAAM,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;aACzC;iBAAM;;AAEJ,gBAAA,MAAc,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;aAC9B;SACF;KACF;AAED,IAAA,OAAO,MAAW,CAAC;AACrB;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chat-lab/ui",
3
- "version": "0.1.0-beta.52",
3
+ "version": "0.1.0-beta.54",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",