feat: iframe增加智能体显示
This commit is contained in:
1 parent
be4b180d92
commit
1ab4216ccb
3 files changed
+107
-56
No files matched your search
@@ -83,6 +83,13 @@ type BrainyChatInterruptMessage = {
|
||||
};
|
||||
requestId?: string;
|
||||
};
|
||||
type BrainyUrlContextMessage = {
|
||||
source?: string;
|
||||
type?: string;
|
||||
payload?: {
|
||||
url?: string;
|
||||
};
|
||||
};
|
||||
type PendingIframeSubmit = {
|
||||
message: PromptInputMessage;
|
||||
externalContext?: Record<string, unknown>;
|
||||
@@ -429,25 +436,10 @@ export default function IframeChatPage() {
|
||||
[thread],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
const handleMessage = (
|
||||
event: MessageEvent<BrainyChatSubmitMessage | BrainyChatInterruptMessage>,
|
||||
) => {
|
||||
const data = event.data;
|
||||
if (data?.source !== "brainy-extension") return;
|
||||
if (data?.type === "brainy:chat-interrupt") {
|
||||
void handleInterrupt(data as BrainyChatInterruptMessage);
|
||||
return;
|
||||
}
|
||||
if (data?.type !== "brainy:chat-submit") return;
|
||||
const submitMessage = data as BrainyChatSubmitMessage;
|
||||
const { submitText, files, iframeContext, additionalKwargs } =
|
||||
parseBrainyChatSubmitMessage(submitMessage);
|
||||
|
||||
if (!submitText && files.length === 0) return;
|
||||
|
||||
const applyIncomingUrlSelection = useCallback(
|
||||
(pageUrl: string | null | undefined) => {
|
||||
const nextSelection = resolveIncomingIframeSelection({
|
||||
pageUrl: submitMessage.payload?.pageContext?.url,
|
||||
pageUrl,
|
||||
mappings: pluginManagementSettings?.domainAgentMappings ?? [],
|
||||
availableAgentIds: selectorLoading
|
||||
? undefined
|
||||
@@ -456,6 +448,40 @@ export default function IframeChatPage() {
|
||||
});
|
||||
setSelectedAgentId(nextSelection.selectedAgentId);
|
||||
setForceDefaultAssistant(nextSelection.forceDefaultAssistant);
|
||||
return nextSelection;
|
||||
},
|
||||
[
|
||||
manualSelectedAgentId,
|
||||
pluginManagementSettings?.domainAgentMappings,
|
||||
selectorAgents,
|
||||
selectorLoading,
|
||||
],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
const handleMessage = (
|
||||
event: MessageEvent<
|
||||
BrainyChatSubmitMessage | BrainyChatInterruptMessage | BrainyUrlContextMessage
|
||||
>,
|
||||
) => {
|
||||
const data = event.data;
|
||||
if (data?.source !== "brainy-extension") return;
|
||||
if (data?.type === "brainy:chat-interrupt") {
|
||||
void handleInterrupt(data as BrainyChatInterruptMessage);
|
||||
return;
|
||||
}
|
||||
if (data?.type === "brainy:context-url") {
|
||||
applyIncomingUrlSelection(
|
||||
(data as BrainyUrlContextMessage).payload?.url ?? null,
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (data?.type !== "brainy:chat-submit") return;
|
||||
const submitMessage = data as BrainyChatSubmitMessage;
|
||||
const { submitText, files, iframeContext, additionalKwargs } =
|
||||
parseBrainyChatSubmitMessage(submitMessage);
|
||||
|
||||
if (!submitText && files.length === 0) return;
|
||||
|
||||
console.log("收到插件消息", {
|
||||
text: submitMessage.payload?.text,
|
||||
@@ -473,8 +499,8 @@ export default function IframeChatPage() {
|
||||
message: { text: submitText, files },
|
||||
externalContext: iframeContext,
|
||||
options: additionalKwargs ? { additionalKwargs } : undefined,
|
||||
targetSelectedAgentId: nextSelection.selectedAgentId,
|
||||
targetForceDefaultAssistant: nextSelection.forceDefaultAssistant,
|
||||
targetSelectedAgentId: selectedAgentId,
|
||||
targetForceDefaultAssistant: forceDefaultAssistant,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -483,11 +509,10 @@ export default function IframeChatPage() {
|
||||
window.removeEventListener("message", handleMessage);
|
||||
};
|
||||
}, [
|
||||
applyIncomingUrlSelection,
|
||||
forceDefaultAssistant,
|
||||
handleInterrupt,
|
||||
manualSelectedAgentId,
|
||||
pluginManagementSettings?.domainAgentMappings,
|
||||
selectorAgents,
|
||||
selectorLoading,
|
||||
selectedAgentId,
|
||||
]);
|
||||
|
||||
const isLandingState = isNewThread && !hasStartedConversation;
|
||||
|
||||
Reference in new issue
Block a user