fix: reset code
This commit is contained in:
1 parent
c012146c18
commit
be06cac223
3 files changed
+50
-101
No files matched your search
@@ -20,7 +20,6 @@ import {
|
|||||||
} from "@/components/ui/dialog";
|
} from "@/components/ui/dialog";
|
||||||
import { PromptInputButton } from "@/components/ai-elements/prompt-input";
|
import { PromptInputButton } from "@/components/ai-elements/prompt-input";
|
||||||
import { AgentManageDialog } from "@/components/workspace/agents/agent-manage-dialog";
|
import { AgentManageDialog } from "@/components/workspace/agents/agent-manage-dialog";
|
||||||
import { Tooltip } from "@/components/workspace/tooltip";
|
|
||||||
import { useAgentSelector, useAgentsPaginated } from "@/core/agents";
|
import { useAgentSelector, useAgentsPaginated } from "@/core/agents";
|
||||||
import type { Agent } from "@/core/agents";
|
import type { Agent } from "@/core/agents";
|
||||||
import { getStoredAuth } from "@/core/auth";
|
import { getStoredAuth } from "@/core/auth";
|
||||||
@@ -87,7 +86,6 @@ export function AgentSelector({
|
|||||||
};
|
};
|
||||||
|
|
||||||
const triggerLabel = selectedAgent ? desensitize(selectedAgent.name) : "选择 Agent";
|
const triggerLabel = selectedAgent ? desensitize(selectedAgent.name) : "选择 Agent";
|
||||||
const iconTooltipLabel = selectedAgent ? triggerLabel : "未选择";
|
|
||||||
const iconMode = triggerVariant === "icon";
|
const iconMode = triggerVariant === "icon";
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -95,40 +93,38 @@ export function AgentSelector({
|
|||||||
<Dialog open={open} onOpenChange={setOpen}>
|
<Dialog open={open} onOpenChange={setOpen}>
|
||||||
<DialogTrigger asChild>
|
<DialogTrigger asChild>
|
||||||
{iconMode ? (
|
{iconMode ? (
|
||||||
<Tooltip content={iconTooltipLabel}>
|
<Button
|
||||||
<Button
|
type="button"
|
||||||
type="button"
|
variant="ghost"
|
||||||
variant="ghost"
|
size="icon"
|
||||||
size="icon"
|
disabled={disabled}
|
||||||
disabled={disabled}
|
aria-label={selectedAgent ? `已选择智能体:${triggerLabel}` : "选择智能体"}
|
||||||
aria-label={selectedAgent ? `已选择智能体:${triggerLabel}` : "选择智能体"}
|
className={cn(
|
||||||
className={cn(
|
"relative size-9 rounded-full text-muted-foreground shadow-none hover:bg-muted/60 hover:text-foreground",
|
||||||
"relative size-9 rounded-full text-muted-foreground shadow-none hover:bg-muted/60 hover:text-foreground",
|
selectedAgent && "text-amber-600 hover:text-amber-700",
|
||||||
selectedAgent && "text-amber-600 hover:text-amber-700",
|
selectedAgent && iconActiveClassName,
|
||||||
selectedAgent && iconActiveClassName,
|
iconButtonClassName,
|
||||||
iconButtonClassName,
|
)}
|
||||||
)}
|
>
|
||||||
>
|
<BotIcon className="size-4" />
|
||||||
<BotIcon className="size-4" />
|
{selectedAgent && (
|
||||||
{selectedAgent && (
|
<span
|
||||||
<span
|
role="button"
|
||||||
role="button"
|
tabIndex={0}
|
||||||
tabIndex={0}
|
aria-label="清除已选 agent"
|
||||||
aria-label="清除已选 agent"
|
className="absolute -right-1 -top-1 inline-flex size-4 items-center justify-center rounded-full border bg-background text-foreground shadow-sm hover:bg-muted"
|
||||||
className="absolute -right-1 -top-1 inline-flex size-4 items-center justify-center rounded-full border bg-background text-foreground shadow-sm hover:bg-muted"
|
onClick={handleClear}
|
||||||
onClick={handleClear}
|
onKeyDown={(e) => {
|
||||||
onKeyDown={(e) => {
|
if (e.key === "Enter" || e.key === " ") {
|
||||||
if (e.key === "Enter" || e.key === " ") {
|
e.preventDefault();
|
||||||
e.preventDefault();
|
onAgentChange(null);
|
||||||
onAgentChange(null);
|
}
|
||||||
}
|
}}
|
||||||
}}
|
>
|
||||||
>
|
<XIcon className="size-2.5" />
|
||||||
<XIcon className="size-2.5" />
|
</span>
|
||||||
</span>
|
)}
|
||||||
)}
|
</Button>
|
||||||
</Button>
|
|
||||||
</Tooltip>
|
|
||||||
) : (
|
) : (
|
||||||
<PromptInputButton
|
<PromptInputButton
|
||||||
type="button"
|
type="button"
|
||||||
|
|||||||
@@ -165,26 +165,4 @@ test("mapped domain overrides the user's manual selection for that incoming mess
|
|||||||
forceDefaultAssistant: false,
|
forceDefaultAssistant: false,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
|
||||||
|
|
||||||
test("unmapped incoming url resets auto-selected agent back to default when there is no manual selection", () => {
|
|
||||||
assert.deepEqual(
|
|
||||||
resolveIncomingIframeSelection({
|
|
||||||
pageUrl: "https://another-unknown.example.com/article",
|
|
||||||
mappings: [
|
|
||||||
{
|
|
||||||
id: "m1",
|
|
||||||
domain: "foo.example.com",
|
|
||||||
agentId: "agent-1",
|
|
||||||
enabled: true,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
availableAgentIds: ["agent-1", "agent-2"],
|
|
||||||
manualSelectedAgentId: null,
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
selectedAgentId: null,
|
|
||||||
forceDefaultAssistant: true,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
@@ -83,13 +83,6 @@ type BrainyChatInterruptMessage = {
|
|||||||
};
|
};
|
||||||
requestId?: string;
|
requestId?: string;
|
||||||
};
|
};
|
||||||
type BrainyUrlContextMessage = {
|
|
||||||
source?: string;
|
|
||||||
type?: string;
|
|
||||||
payload?: {
|
|
||||||
url?: string;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
type PendingIframeSubmit = {
|
type PendingIframeSubmit = {
|
||||||
message: PromptInputMessage;
|
message: PromptInputMessage;
|
||||||
externalContext?: Record<string, unknown>;
|
externalContext?: Record<string, unknown>;
|
||||||
@@ -436,33 +429,9 @@ export default function IframeChatPage() {
|
|||||||
[thread],
|
[thread],
|
||||||
);
|
);
|
||||||
|
|
||||||
const applyIncomingUrlSelection = useCallback(
|
|
||||||
(pageUrl: string | null | undefined) => {
|
|
||||||
const nextSelection = resolveIncomingIframeSelection({
|
|
||||||
pageUrl,
|
|
||||||
mappings: pluginManagementSettings?.domainAgentMappings ?? [],
|
|
||||||
availableAgentIds: selectorLoading
|
|
||||||
? undefined
|
|
||||||
: selectorAgents.map((agent) => agent.id),
|
|
||||||
manualSelectedAgentId,
|
|
||||||
});
|
|
||||||
setSelectedAgentId(nextSelection.selectedAgentId);
|
|
||||||
setForceDefaultAssistant(nextSelection.forceDefaultAssistant);
|
|
||||||
return nextSelection;
|
|
||||||
},
|
|
||||||
[
|
|
||||||
manualSelectedAgentId,
|
|
||||||
pluginManagementSettings?.domainAgentMappings,
|
|
||||||
selectorAgents,
|
|
||||||
selectorLoading,
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleMessage = (
|
const handleMessage = (
|
||||||
event: MessageEvent<
|
event: MessageEvent<BrainyChatSubmitMessage | BrainyChatInterruptMessage>,
|
||||||
BrainyChatSubmitMessage | BrainyChatInterruptMessage | BrainyUrlContextMessage
|
|
||||||
>,
|
|
||||||
) => {
|
) => {
|
||||||
const data = event.data;
|
const data = event.data;
|
||||||
if (data?.source !== "brainy-extension") return;
|
if (data?.source !== "brainy-extension") return;
|
||||||
@@ -470,12 +439,6 @@ export default function IframeChatPage() {
|
|||||||
void handleInterrupt(data as BrainyChatInterruptMessage);
|
void handleInterrupt(data as BrainyChatInterruptMessage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (data?.type === "brainy:context-url") {
|
|
||||||
applyIncomingUrlSelection(
|
|
||||||
(data as BrainyUrlContextMessage).payload?.url ?? null,
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (data?.type !== "brainy:chat-submit") return;
|
if (data?.type !== "brainy:chat-submit") return;
|
||||||
const submitMessage = data as BrainyChatSubmitMessage;
|
const submitMessage = data as BrainyChatSubmitMessage;
|
||||||
const { submitText, files, iframeContext, additionalKwargs } =
|
const { submitText, files, iframeContext, additionalKwargs } =
|
||||||
@@ -483,6 +446,17 @@ export default function IframeChatPage() {
|
|||||||
|
|
||||||
if (!submitText && files.length === 0) return;
|
if (!submitText && files.length === 0) return;
|
||||||
|
|
||||||
|
const nextSelection = resolveIncomingIframeSelection({
|
||||||
|
pageUrl: submitMessage.payload?.pageContext?.url,
|
||||||
|
mappings: pluginManagementSettings?.domainAgentMappings ?? [],
|
||||||
|
availableAgentIds: selectorLoading
|
||||||
|
? undefined
|
||||||
|
: selectorAgents.map((agent) => agent.id),
|
||||||
|
manualSelectedAgentId,
|
||||||
|
});
|
||||||
|
setSelectedAgentId(nextSelection.selectedAgentId);
|
||||||
|
setForceDefaultAssistant(nextSelection.forceDefaultAssistant);
|
||||||
|
|
||||||
console.log("收到插件消息", {
|
console.log("收到插件消息", {
|
||||||
text: submitMessage.payload?.text,
|
text: submitMessage.payload?.text,
|
||||||
prompt: submitMessage.payload?.prompt,
|
prompt: submitMessage.payload?.prompt,
|
||||||
@@ -499,8 +473,8 @@ export default function IframeChatPage() {
|
|||||||
message: { text: submitText, files },
|
message: { text: submitText, files },
|
||||||
externalContext: iframeContext,
|
externalContext: iframeContext,
|
||||||
options: additionalKwargs ? { additionalKwargs } : undefined,
|
options: additionalKwargs ? { additionalKwargs } : undefined,
|
||||||
targetSelectedAgentId: selectedAgentId,
|
targetSelectedAgentId: nextSelection.selectedAgentId,
|
||||||
targetForceDefaultAssistant: forceDefaultAssistant,
|
targetForceDefaultAssistant: nextSelection.forceDefaultAssistant,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -509,10 +483,11 @@ export default function IframeChatPage() {
|
|||||||
window.removeEventListener("message", handleMessage);
|
window.removeEventListener("message", handleMessage);
|
||||||
};
|
};
|
||||||
}, [
|
}, [
|
||||||
applyIncomingUrlSelection,
|
|
||||||
forceDefaultAssistant,
|
|
||||||
handleInterrupt,
|
handleInterrupt,
|
||||||
selectedAgentId,
|
manualSelectedAgentId,
|
||||||
|
pluginManagementSettings?.domainAgentMappings,
|
||||||
|
selectorAgents,
|
||||||
|
selectorLoading,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const isLandingState = isNewThread && !hasStartedConversation;
|
const isLandingState = isNewThread && !hasStartedConversation;
|
||||||
|
|||||||
Reference in new issue
Block a user