fix: 智能体对应
This commit is contained in:
1 parent
e9e4c181b9
commit
be4b180d92
3 files changed
+513
No files matched your search
@@ -0,0 +1,168 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
|
||||
import {
|
||||
normalizeDomainInput,
|
||||
resolveIncomingIframeSelection,
|
||||
resolveConfiguredIframeAgentId,
|
||||
resolveDomainMappedAgentId,
|
||||
} from "./domain-agent.ts";
|
||||
|
||||
test("normalizes full urls and bare hosts to comparable domains", () => {
|
||||
assert.equal(
|
||||
normalizeDomainInput(" https://Foo.Example.com/path?q=1 "),
|
||||
"foo.example.com",
|
||||
);
|
||||
assert.equal(
|
||||
normalizeDomainInput("foo.example.com:8080/landing"),
|
||||
"foo.example.com:8080",
|
||||
);
|
||||
});
|
||||
|
||||
test("resolves the configured agent for a matching pageContext url", () => {
|
||||
assert.equal(
|
||||
resolveDomainMappedAgentId({
|
||||
pageUrl: "https://foo.example.com/article/123",
|
||||
mappings: [
|
||||
{
|
||||
id: "m1",
|
||||
domain: "foo.example.com",
|
||||
agentId: "agent-1",
|
||||
enabled: true,
|
||||
},
|
||||
],
|
||||
availableAgentIds: ["agent-1", "agent-2"],
|
||||
}),
|
||||
"agent-1",
|
||||
);
|
||||
});
|
||||
|
||||
test("ignores disabled mappings and unavailable agents", () => {
|
||||
assert.equal(
|
||||
resolveDomainMappedAgentId({
|
||||
pageUrl: "https://foo.example.com/article/123",
|
||||
mappings: [
|
||||
{
|
||||
id: "m1",
|
||||
domain: "foo.example.com",
|
||||
agentId: "agent-1",
|
||||
enabled: false,
|
||||
},
|
||||
{
|
||||
id: "m2",
|
||||
domain: "bar.example.com",
|
||||
agentId: "agent-2",
|
||||
enabled: true,
|
||||
},
|
||||
],
|
||||
availableAgentIds: ["agent-3"],
|
||||
}),
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
test("does not auto-select after manual selection or once a conversation has started", () => {
|
||||
assert.equal(
|
||||
resolveConfiguredIframeAgentId({
|
||||
pageUrl: "https://foo.example.com",
|
||||
mappings: [
|
||||
{
|
||||
id: "m1",
|
||||
domain: "foo.example.com",
|
||||
agentId: "agent-1",
|
||||
enabled: true,
|
||||
},
|
||||
],
|
||||
availableAgentIds: ["agent-1"],
|
||||
isNewThread: true,
|
||||
hasStartedConversation: false,
|
||||
manualSelectionLocked: true,
|
||||
}),
|
||||
undefined,
|
||||
);
|
||||
|
||||
assert.equal(
|
||||
resolveConfiguredIframeAgentId({
|
||||
pageUrl: "https://foo.example.com",
|
||||
mappings: [
|
||||
{
|
||||
id: "m1",
|
||||
domain: "foo.example.com",
|
||||
agentId: "agent-1",
|
||||
enabled: true,
|
||||
},
|
||||
],
|
||||
availableAgentIds: ["agent-1"],
|
||||
isNewThread: true,
|
||||
hasStartedConversation: true,
|
||||
manualSelectionLocked: false,
|
||||
}),
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
test("falls back to default assistant when url is unmapped and user has not manually selected an agent", () => {
|
||||
assert.deepEqual(
|
||||
resolveIncomingIframeSelection({
|
||||
pageUrl: "https://unknown.example.com",
|
||||
mappings: [
|
||||
{
|
||||
id: "m1",
|
||||
domain: "foo.example.com",
|
||||
agentId: "agent-1",
|
||||
enabled: true,
|
||||
},
|
||||
],
|
||||
availableAgentIds: ["agent-1", "agent-2"],
|
||||
manualSelectedAgentId: null,
|
||||
}),
|
||||
{
|
||||
selectedAgentId: null,
|
||||
forceDefaultAssistant: true,
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test("falls back to the user's manual selection when url is unmapped", () => {
|
||||
assert.deepEqual(
|
||||
resolveIncomingIframeSelection({
|
||||
pageUrl: "https://unknown.example.com",
|
||||
mappings: [
|
||||
{
|
||||
id: "m1",
|
||||
domain: "foo.example.com",
|
||||
agentId: "agent-1",
|
||||
enabled: true,
|
||||
},
|
||||
],
|
||||
availableAgentIds: ["agent-1", "agent-2"],
|
||||
manualSelectedAgentId: "agent-2",
|
||||
}),
|
||||
{
|
||||
selectedAgentId: "agent-2",
|
||||
forceDefaultAssistant: false,
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test("mapped domain overrides the user's manual selection for that incoming message", () => {
|
||||
assert.deepEqual(
|
||||
resolveIncomingIframeSelection({
|
||||
pageUrl: "https://foo.example.com/article",
|
||||
mappings: [
|
||||
{
|
||||
id: "m1",
|
||||
domain: "foo.example.com",
|
||||
agentId: "agent-1",
|
||||
enabled: true,
|
||||
},
|
||||
],
|
||||
availableAgentIds: ["agent-1", "agent-2"],
|
||||
manualSelectedAgentId: "agent-2",
|
||||
}),
|
||||
{
|
||||
selectedAgentId: "agent-1",
|
||||
forceDefaultAssistant: false,
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,135 @@
|
||||
export type DomainAgentMapping = {
|
||||
id: string;
|
||||
domain: string;
|
||||
agentId: string;
|
||||
enabled: boolean;
|
||||
};
|
||||
|
||||
function trimToUndefined(value: string | null | undefined): string | undefined {
|
||||
const trimmed = value?.trim();
|
||||
return trimmed ? trimmed : undefined;
|
||||
}
|
||||
|
||||
function normalizeAgentId(agentId: string | null | undefined): string | undefined {
|
||||
const trimmed = agentId?.trim();
|
||||
return trimmed ? trimmed : undefined;
|
||||
}
|
||||
|
||||
export function normalizeDomainInput(
|
||||
value: string | null | undefined,
|
||||
): string | undefined {
|
||||
const trimmed = trimToUndefined(value);
|
||||
if (!trimmed) return undefined;
|
||||
|
||||
const candidate = /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\//.test(trimmed)
|
||||
? trimmed
|
||||
: `https://${trimmed}`;
|
||||
|
||||
try {
|
||||
const parsed = new URL(candidate);
|
||||
const host = parsed.host.trim().toLowerCase();
|
||||
return host || undefined;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export function resolveDomainMappedAgentId({
|
||||
pageUrl,
|
||||
mappings,
|
||||
availableAgentIds,
|
||||
}: {
|
||||
pageUrl: string | null | undefined;
|
||||
mappings: DomainAgentMapping[];
|
||||
availableAgentIds?: readonly string[] | null;
|
||||
}): string | undefined {
|
||||
const host = normalizeDomainInput(pageUrl);
|
||||
if (!host) return undefined;
|
||||
|
||||
const allowedAgents =
|
||||
availableAgentIds && availableAgentIds.length > 0
|
||||
? new Set(availableAgentIds)
|
||||
: null;
|
||||
|
||||
for (const mapping of mappings) {
|
||||
if (!mapping.enabled) continue;
|
||||
if (normalizeDomainInput(mapping.domain) !== host) continue;
|
||||
if (allowedAgents && !allowedAgents.has(mapping.agentId)) continue;
|
||||
return mapping.agentId;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function resolveConfiguredIframeAgentId({
|
||||
pageUrl,
|
||||
mappings,
|
||||
availableAgentIds,
|
||||
isNewThread,
|
||||
hasStartedConversation,
|
||||
manualSelectionLocked,
|
||||
}: {
|
||||
pageUrl: string | null | undefined;
|
||||
mappings: DomainAgentMapping[];
|
||||
availableAgentIds?: readonly string[] | null;
|
||||
isNewThread: boolean;
|
||||
hasStartedConversation: boolean;
|
||||
manualSelectionLocked: boolean;
|
||||
}): string | undefined {
|
||||
if (!isNewThread || hasStartedConversation || manualSelectionLocked) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return resolveDomainMappedAgentId({
|
||||
pageUrl,
|
||||
mappings,
|
||||
availableAgentIds,
|
||||
});
|
||||
}
|
||||
|
||||
export function resolveIncomingIframeSelection({
|
||||
pageUrl,
|
||||
mappings,
|
||||
availableAgentIds,
|
||||
manualSelectedAgentId,
|
||||
}: {
|
||||
pageUrl: string | null | undefined;
|
||||
mappings: DomainAgentMapping[];
|
||||
availableAgentIds?: readonly string[] | null;
|
||||
manualSelectedAgentId: string | null;
|
||||
}): {
|
||||
selectedAgentId: string | null;
|
||||
forceDefaultAssistant: boolean;
|
||||
} {
|
||||
const mappedAgentId = resolveDomainMappedAgentId({
|
||||
pageUrl,
|
||||
mappings,
|
||||
availableAgentIds,
|
||||
});
|
||||
if (mappedAgentId) {
|
||||
return {
|
||||
selectedAgentId: mappedAgentId,
|
||||
forceDefaultAssistant: false,
|
||||
};
|
||||
}
|
||||
|
||||
const allowedAgents =
|
||||
availableAgentIds && availableAgentIds.length > 0
|
||||
? new Set(availableAgentIds)
|
||||
: null;
|
||||
const normalizedManualAgentId = normalizeAgentId(manualSelectedAgentId);
|
||||
if (
|
||||
normalizedManualAgentId &&
|
||||
(!allowedAgents || allowedAgents.has(normalizedManualAgentId))
|
||||
) {
|
||||
return {
|
||||
selectedAgentId: normalizedManualAgentId,
|
||||
forceDefaultAssistant: false,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
selectedAgentId: null,
|
||||
forceDefaultAssistant: true,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user