抽象统一定位方法,支持多种地理要素
This commit is contained in:
@@ -34,8 +34,26 @@ type ToolMeta = {
|
||||
color: string;
|
||||
};
|
||||
|
||||
const LOCATE_TOOL_TO_LAYER: Record<string, string> = {
|
||||
locate_features: "",
|
||||
locate_junctions: "geo_junctions_mat",
|
||||
locate_pipes: "geo_pipes_mat",
|
||||
locate_valves: "geo_valves",
|
||||
locate_reservoirs: "geo_reservoirs",
|
||||
locate_pumps: "geo_pumps",
|
||||
locate_tanks: "geo_tanks",
|
||||
};
|
||||
|
||||
const LOCATE_LINE_TOOLS = new Set<string>(["locate_pipes"]);
|
||||
|
||||
const TOOL_META: Record<string, ToolMeta> = {
|
||||
locate_nodes: {
|
||||
locate_features: {
|
||||
label: "定位要素",
|
||||
icon: <LocationOnRounded sx={{ fontSize: 18 }} />,
|
||||
actionLabel: "定位到地图",
|
||||
color: "#5470c6",
|
||||
},
|
||||
locate_junctions: {
|
||||
label: "定位节点",
|
||||
icon: <LocationOnRounded sx={{ fontSize: 18 }} />,
|
||||
actionLabel: "定位到地图",
|
||||
@@ -47,6 +65,30 @@ const TOOL_META: Record<string, ToolMeta> = {
|
||||
actionLabel: "定位到地图",
|
||||
color: "#91cc75",
|
||||
},
|
||||
locate_valves: {
|
||||
label: "定位阀门",
|
||||
icon: <LocationOnRounded sx={{ fontSize: 18 }} />,
|
||||
actionLabel: "定位到地图",
|
||||
color: "#9a60b4",
|
||||
},
|
||||
locate_reservoirs: {
|
||||
label: "定位水源",
|
||||
icon: <LocationOnRounded sx={{ fontSize: 18 }} />,
|
||||
actionLabel: "定位到地图",
|
||||
color: "#ea7ccc",
|
||||
},
|
||||
locate_pumps: {
|
||||
label: "定位泵站",
|
||||
icon: <LocationOnRounded sx={{ fontSize: 18 }} />,
|
||||
actionLabel: "定位到地图",
|
||||
color: "#fc8452",
|
||||
},
|
||||
locate_tanks: {
|
||||
label: "定位水池",
|
||||
icon: <LocationOnRounded sx={{ fontSize: 18 }} />,
|
||||
actionLabel: "定位到地图",
|
||||
color: "#3ba272",
|
||||
},
|
||||
view_history: {
|
||||
label: "查看计算结果",
|
||||
icon: <TimelineRounded sx={{ fontSize: 18 }} />,
|
||||
@@ -71,6 +113,19 @@ const TOOL_META: Record<string, ToolMeta> = {
|
||||
|
||||
function getToolDescription(toolCall: ToolCall): string {
|
||||
const { params } = toolCall;
|
||||
const normalizeIds = (): string[] => {
|
||||
const rawIds = params.ids;
|
||||
if (Array.isArray(rawIds)) {
|
||||
return rawIds.map((id) => String(id)).filter((id) => id.trim().length > 0);
|
||||
}
|
||||
if (typeof rawIds === "string") {
|
||||
return rawIds
|
||||
.split(",")
|
||||
.map((id) => id.trim())
|
||||
.filter(Boolean);
|
||||
}
|
||||
return [];
|
||||
};
|
||||
const resolveScadaFeatureInfos = (): [string, string][] => {
|
||||
const rawFeatureInfos = params.feature_infos;
|
||||
if (Array.isArray(rawFeatureInfos)) {
|
||||
@@ -119,13 +174,36 @@ function getToolDescription(toolCall: ToolCall): string {
|
||||
(params.to as string | undefined) ??
|
||||
(params.end as string | undefined),
|
||||
});
|
||||
const resolveLocateFeatureType = (): string => {
|
||||
const rawType = params.feature_type;
|
||||
if (typeof rawType === "string" && rawType.trim()) {
|
||||
return rawType.trim().toLowerCase();
|
||||
}
|
||||
return "";
|
||||
};
|
||||
switch (toolCall.tool) {
|
||||
case "locate_nodes":
|
||||
case "locate_pipes": {
|
||||
const ids = (params.ids as string[] | undefined) ?? [];
|
||||
return ids.length > 3
|
||||
case "locate_features":
|
||||
case "locate_junctions":
|
||||
case "locate_pipes":
|
||||
case "locate_valves":
|
||||
case "locate_reservoirs":
|
||||
case "locate_pumps":
|
||||
case "locate_tanks": {
|
||||
const ids = normalizeIds();
|
||||
const idsText =
|
||||
ids.length > 3
|
||||
? `${ids.slice(0, 3).join(", ")} 等 ${ids.length} 个`
|
||||
: ids.join(", ");
|
||||
if (toolCall.tool !== "locate_features") {
|
||||
return idsText;
|
||||
}
|
||||
const featureType = resolveLocateFeatureType();
|
||||
if (!featureType) {
|
||||
return idsText;
|
||||
}
|
||||
return idsText
|
||||
? `${featureType} · ${idsText}`
|
||||
: featureType;
|
||||
}
|
||||
case "view_history":
|
||||
case "view_scada": {
|
||||
@@ -155,6 +233,19 @@ function getToolDescription(toolCall: ToolCall): string {
|
||||
|
||||
function buildAction(toolCall: ToolCall): ChatToolAction | null {
|
||||
const { params } = toolCall;
|
||||
const normalizeIds = (): string[] => {
|
||||
const rawIds = params.ids;
|
||||
if (Array.isArray(rawIds)) {
|
||||
return rawIds.map((id) => String(id)).filter((id) => id.trim().length > 0);
|
||||
}
|
||||
if (typeof rawIds === "string") {
|
||||
return rawIds
|
||||
.split(",")
|
||||
.map((id) => id.trim())
|
||||
.filter(Boolean);
|
||||
}
|
||||
return [];
|
||||
};
|
||||
const resolveScadaFeatureInfos = (): [string, string][] => {
|
||||
const rawFeatureInfos = params.feature_infos;
|
||||
if (Array.isArray(rawFeatureInfos)) {
|
||||
@@ -204,16 +295,36 @@ function buildAction(toolCall: ToolCall): ChatToolAction | null {
|
||||
(params.end as string | undefined),
|
||||
});
|
||||
switch (toolCall.tool) {
|
||||
case "locate_nodes":
|
||||
case "locate_features": {
|
||||
const featureTypeRaw = params.feature_type;
|
||||
const featureType =
|
||||
typeof featureTypeRaw === "string"
|
||||
? featureTypeRaw.trim().toLowerCase()
|
||||
: "";
|
||||
const config = locateFeatureTypeToConfig(featureType);
|
||||
if (!config) return null;
|
||||
return {
|
||||
type: "locate_nodes",
|
||||
ids: (params.ids as string[] | undefined) ?? [],
|
||||
type: "locate_features",
|
||||
ids: normalizeIds(),
|
||||
layer: config.layer,
|
||||
geometryKind: config.geometryKind,
|
||||
};
|
||||
}
|
||||
case "locate_junctions":
|
||||
case "locate_pipes":
|
||||
case "locate_valves":
|
||||
case "locate_reservoirs":
|
||||
case "locate_pumps":
|
||||
case "locate_tanks": {
|
||||
const layer = LOCATE_TOOL_TO_LAYER[toolCall.tool];
|
||||
if (!layer) return null;
|
||||
return {
|
||||
type: "locate_pipes",
|
||||
ids: (params.ids as string[] | undefined) ?? [],
|
||||
type: "locate_features",
|
||||
ids: normalizeIds(),
|
||||
layer,
|
||||
geometryKind: LOCATE_LINE_TOOLS.has(toolCall.tool) ? "line" : "point",
|
||||
};
|
||||
}
|
||||
case "view_history": {
|
||||
const historyRange = resolveTimeRange();
|
||||
return {
|
||||
@@ -383,3 +494,29 @@ export const ChatToolCallBlock: React.FC<ChatToolCallBlockProps> = ({
|
||||
</Paper>
|
||||
);
|
||||
};
|
||||
const locateFeatureTypeToConfig = (
|
||||
featureType: string,
|
||||
): { layer: string; geometryKind: "point" | "line" } | null => {
|
||||
switch (featureType) {
|
||||
case "junction":
|
||||
case "junctions":
|
||||
return { layer: "geo_junctions_mat", geometryKind: "point" };
|
||||
case "pipe":
|
||||
case "pipes":
|
||||
return { layer: "geo_pipes_mat", geometryKind: "line" };
|
||||
case "valve":
|
||||
case "valves":
|
||||
return { layer: "geo_valves", geometryKind: "point" };
|
||||
case "reservoir":
|
||||
case "reservoirs":
|
||||
return { layer: "geo_reservoirs", geometryKind: "point" };
|
||||
case "pump":
|
||||
case "pumps":
|
||||
return { layer: "geo_pumps", geometryKind: "point" };
|
||||
case "tank":
|
||||
case "tanks":
|
||||
return { layer: "geo_tanks", geometryKind: "point" };
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user