Skip to content

Commit

Permalink
fix(chat-agent): allow always use new conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
weareoutman committed Jul 24, 2024
1 parent 3daa5bc commit dc1e412
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions bricks/ai/src/chat-agent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface ChatAgentProps {
agentId?: string;
robotId?: string;
conversationId?: string;
alwaysUseNewConversation?: boolean;
}

export interface Message extends BaseMessage {
Expand Down Expand Up @@ -69,6 +70,9 @@ class ChatAgent extends ReactNextElement implements ChatAgentProps {
@property()
accessor conversationId: string | undefined;

@property()
accessor alwaysUseNewConversation: boolean | undefined;

/**
* 发送消息到默认的聊天 API
*/
Expand Down Expand Up @@ -147,6 +151,7 @@ class ChatAgent extends ReactNextElement implements ChatAgentProps {
agentId={this.agentId}
robotId={this.robotId}
conversationId={this.conversationId}
alwaysUseNewConversation={this.alwaysUseNewConversation}
// onMessageChunkPush={this.#handleMessageChunkPush}
onMessagesUpdate={this.#handleMessagesUpdate}
onBusyChange={this.#handleBusyChange}
Expand Down Expand Up @@ -183,6 +188,7 @@ export function LegacyChatAgentComponent(
agentId,
robotId,
conversationId: propConversationId,
alwaysUseNewConversation,
onMessageChunkPush,
onMessagesUpdate,
onBusyChange,
Expand Down Expand Up @@ -240,6 +246,9 @@ export function LegacyChatAgentComponent(
if (busyRef.current) {
return null;
}
if (alwaysUseNewConversation || isLowLevel) {
setFullMessages((prev) => (prev.length === 0 ? prev : []));
}
const thisChatId = chatIdRef.current;
let newConversationError: Error | undefined;
const checkNewConversation = async () => {
Expand All @@ -254,7 +263,8 @@ export function LegacyChatAgentComponent(

const userKey = getMessageChunkKey();
const assistantKey = getMessageChunkKey();
let currentConversationId = isLowLevel ? null : conversationId;
let currentConversationId =
alwaysUseNewConversation || isLowLevel ? null : conversationId;

onBusyChange?.((busyRef.current = true));

Expand Down Expand Up @@ -333,6 +343,7 @@ export function LegacyChatAgentComponent(
partial: true,
});
if (
!alwaysUseNewConversation &&
(value as MessageChunk).conversationId &&
!currentConversationId
) {
Expand Down Expand Up @@ -392,7 +403,13 @@ export function LegacyChatAgentComponent(

return currentConversationId;
},
[conversationId, getMessageChunkKey, onBusyChange, pushPartialMessage]
[
conversationId,
alwaysUseNewConversation,
getMessageChunkKey,
onBusyChange,
pushPartialMessage,
]
);

useImperativeHandle(
Expand All @@ -412,7 +429,10 @@ export function LegacyChatAgentComponent(
robotId,
input: content,
stream: true,
conversationId,
conversationId:
alwaysUseNewConversation || conversationId === null
? undefined
: conversationId,
}),
headers: {
"giraffe-contract-name":
Expand All @@ -430,7 +450,14 @@ export function LegacyChatAgentComponent(
}
},
}),
[agentId, robotId, conversationId, onBusyChange, legacySendRequest]
[
legacySendRequest,
agentId,
robotId,
alwaysUseNewConversation,
conversationId,
onBusyChange,
]
);

useEffect(() => {
Expand Down

0 comments on commit dc1e412

Please sign in to comment.