@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.
- package/CHANGELOG.md +50 -0
- package/docs/usage/tools-calling/anthropic.mdx +185 -1
- package/docs/usage/tools-calling/anthropic.zh-CN.mdx +14 -19
- package/docs/usage/tools-calling/google.mdx +116 -1
- package/docs/usage/tools-calling/google.zh-CN.mdx +104 -3
- package/docs/usage/tools-calling/moonshot.mdx +1 -0
- package/docs/usage/tools-calling/moonshot.zh-CN.mdx +24 -0
- package/docs/usage/tools-calling/openai.mdx +139 -1
- package/docs/usage/tools-calling/openai.zh-CN.mdx +0 -694
- package/docs/usage/tools-calling.zh-CN.mdx +15 -14
- package/locales/ar/setting.json +1 -1
- package/locales/bg-BG/setting.json +1 -1
- package/locales/de-DE/setting.json +1 -1
- package/locales/en-US/setting.json +1 -1
- package/locales/es-ES/setting.json +1 -1
- package/locales/fr-FR/setting.json +1 -1
- package/locales/it-IT/setting.json +1 -1
- package/locales/ja-JP/setting.json +1 -1
- package/locales/ko-KR/setting.json +1 -1
- package/locales/nl-NL/setting.json +1 -1
- package/locales/pl-PL/setting.json +1 -1
- package/locales/pt-BR/setting.json +1 -1
- package/locales/ru-RU/setting.json +1 -1
- package/locales/tr-TR/setting.json +1 -1
- package/locales/vi-VN/setting.json +1 -1
- package/locales/zh-CN/setting.json +1 -1
- package/locales/zh-TW/setting.json +1 -1
- package/package.json +30 -30
- package/src/features/AgentSetting/AgentModal/index.tsx +4 -1
- package/src/libs/agent-runtime/google/index.test.ts +2 -2
- package/src/locales/default/setting.ts +1 -1
- package/src/services/__tests__/chat.test.ts +136 -1
- package/src/services/chat.ts +44 -2
package/src/services/chat.ts
CHANGED
|
@@ -420,7 +420,7 @@ class ChatService {
|
|
|
420
420
|
] as UserMessageContentPart[];
|
|
421
421
|
};
|
|
422
422
|
|
|
423
|
-
|
|
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
|
-
|
|
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();
|