feat(map): add coordinate zoom action
This commit is contained in:
@@ -148,6 +148,46 @@ const compactNames = (names: string[]) => {
|
||||
: names.join(", ");
|
||||
};
|
||||
|
||||
const readFiniteNumber = (value: unknown): number | null => {
|
||||
if (typeof value === "number" && Number.isFinite(value)) {
|
||||
return value;
|
||||
}
|
||||
if (typeof value === "string" && value.trim()) {
|
||||
const parsed = Number(value);
|
||||
return Number.isFinite(parsed) ? parsed : null;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const parseZoomTo3857Action = (
|
||||
params: Record<string, unknown>,
|
||||
): Extract<ChatToolAction, { type: "zoom_to_map" }> | null => {
|
||||
const rawCoordinate = params.coordinate ?? params.coordinates ?? params.center;
|
||||
const tuple = Array.isArray(rawCoordinate)
|
||||
? rawCoordinate
|
||||
: [params.x ?? params.lon ?? params.longitude, params.y ?? params.lat ?? params.latitude];
|
||||
const x = readFiniteNumber(tuple[0]);
|
||||
const y = readFiniteNumber(tuple[1]);
|
||||
if (x === null || y === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const zoom = readFiniteNumber(params.zoom);
|
||||
const durationMs = readFiniteNumber(params.duration_ms ?? params.durationMs);
|
||||
const rawSourceCrs = params.source_crs ?? params.sourceCrs ?? params.crs;
|
||||
const normalizedSourceCrs =
|
||||
typeof rawSourceCrs === "string" ? rawSourceCrs.trim().toUpperCase() : "";
|
||||
const sourceCrs =
|
||||
normalizedSourceCrs === "EPSG:4326" ? "EPSG:4326" : "EPSG:3857";
|
||||
return {
|
||||
type: "zoom_to_map",
|
||||
coordinate: [x, y],
|
||||
sourceCrs,
|
||||
zoom: zoom ?? undefined,
|
||||
durationMs: durationMs ?? undefined,
|
||||
};
|
||||
};
|
||||
|
||||
const buildLocateArtifact = (
|
||||
tool: string,
|
||||
params: Record<string, unknown>,
|
||||
@@ -190,6 +230,18 @@ const buildToolAction = (
|
||||
};
|
||||
}
|
||||
|
||||
if (tool === "zoom_to_map") {
|
||||
const action = parseZoomTo3857Action(params);
|
||||
return {
|
||||
action,
|
||||
kind: "map",
|
||||
title: "缩放到地图坐标",
|
||||
description: action
|
||||
? `${action.coordinate[0]}, ${action.coordinate[1]} (${action.sourceCrs})`
|
||||
: "地图坐标",
|
||||
};
|
||||
}
|
||||
|
||||
if (tool === "locate_features" || LOCATE_TOOL_CONFIG[tool]) {
|
||||
const locate = buildLocateArtifact(tool, params);
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user