@lobehub/chat 1.2.12 → 1.2.14

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 (33) hide show
  1. package/CHANGELOG.md +50 -0
  2. package/docs/usage/tools-calling/anthropic.mdx +185 -1
  3. package/docs/usage/tools-calling/anthropic.zh-CN.mdx +14 -19
  4. package/docs/usage/tools-calling/google.mdx +116 -1
  5. package/docs/usage/tools-calling/google.zh-CN.mdx +104 -3
  6. package/docs/usage/tools-calling/moonshot.mdx +1 -0
  7. package/docs/usage/tools-calling/moonshot.zh-CN.mdx +24 -0
  8. package/docs/usage/tools-calling/openai.mdx +139 -1
  9. package/docs/usage/tools-calling/openai.zh-CN.mdx +0 -694
  10. package/docs/usage/tools-calling.zh-CN.mdx +15 -14
  11. package/locales/ar/setting.json +1 -1
  12. package/locales/bg-BG/setting.json +1 -1
  13. package/locales/de-DE/setting.json +1 -1
  14. package/locales/en-US/setting.json +1 -1
  15. package/locales/es-ES/setting.json +1 -1
  16. package/locales/fr-FR/setting.json +1 -1
  17. package/locales/it-IT/setting.json +1 -1
  18. package/locales/ja-JP/setting.json +1 -1
  19. package/locales/ko-KR/setting.json +1 -1
  20. package/locales/nl-NL/setting.json +1 -1
  21. package/locales/pl-PL/setting.json +1 -1
  22. package/locales/pt-BR/setting.json +1 -1
  23. package/locales/ru-RU/setting.json +1 -1
  24. package/locales/tr-TR/setting.json +1 -1
  25. package/locales/vi-VN/setting.json +1 -1
  26. package/locales/zh-CN/setting.json +1 -1
  27. package/locales/zh-TW/setting.json +1 -1
  28. package/package.json +30 -30
  29. package/src/features/AgentSetting/AgentModal/index.tsx +4 -1
  30. package/src/libs/agent-runtime/google/index.test.ts +2 -2
  31. package/src/locales/default/setting.ts +1 -1
  32. package/src/services/__tests__/chat.test.ts +136 -1
  33. package/src/services/chat.ts +44 -2
@@ -420,7 +420,7 @@ class ChatService {
420
420
  ] as UserMessageContentPart[];
421
421
  };
422
422
 
423
- const postMessages = messages.map((m): OpenAIChatMessage => {
423
+ let postMessages = messages.map((m): OpenAIChatMessage => {
424
424
  switch (m.role) {
425
425
  case 'user': {
426
426
  return { content: getContent(m), role: m.role };
@@ -458,7 +458,7 @@ class ChatService {
458
458
  }
459
459
  });
460
460
 
461
- return produce(postMessages, (draft) => {
461
+ postMessages = produce(postMessages, (draft) => {
462
462
  // if it's a welcome question, inject InboxGuide SystemRole
463
463
  const inboxGuideSystemRole =
464
464
  options?.isWelcomeQuestion &&
@@ -492,6 +492,8 @@ class ChatService {
492
492
  });
493
493
  }
494
494
  });
495
+
496
+ return this.reorderToolMessages(postMessages);
495
497
  };
496
498
 
497
499
  private mapTrace(trace?: TracePayload, tag?: TraceTagMap): TracePayload {
@@ -523,6 +525,46 @@ class ChatService {
523
525
 
524
526
  return agentRuntime.chat(data, { signal: params.signal });
525
527
  };
528
+
529
+ /**
530
+ * Reorder tool messages to ensure that tool messages are displayed in the correct order.
531
+ * see https://github.com/lobehub/lobe-chat/pull/3155
532
+ */
533
+ private reorderToolMessages = (messages: OpenAIChatMessage[]): OpenAIChatMessage[] => {
534
+ const reorderedMessages: OpenAIChatMessage[] = [];
535
+ const toolMessages: Record<string, OpenAIChatMessage> = {};
536
+
537
+ // 1. collect all tool messages
538
+ messages.forEach((message) => {
539
+ if (message.role === 'tool' && message.tool_call_id) {
540
+ toolMessages[message.tool_call_id] = message;
541
+ }
542
+ });
543
+
544
+ // 2. reorder messages
545
+ messages.forEach((message) => {
546
+ const hasPushed = reorderedMessages.some(
547
+ (m) => !!message.tool_call_id && m.tool_call_id === message.tool_call_id,
548
+ );
549
+
550
+ if (hasPushed) return;
551
+
552
+ reorderedMessages.push(message);
553
+
554
+ if (message.role === 'assistant' && message.tool_calls) {
555
+ message.tool_calls.forEach((toolCall) => {
556
+ const correspondingToolMessage = toolMessages[toolCall.id];
557
+ if (correspondingToolMessage) {
558
+ reorderedMessages.push(correspondingToolMessage);
559
+ // 从 toolMessages 中删除已处理的消息,避免重复
560
+ delete toolMessages[toolCall.id];
561
+ }
562
+ });
563
+ }
564
+ });
565
+
566
+ return reorderedMessages;
567
+ };
526
568
  }
527
569
 
528
570
  export const chatService = new ChatService();