-
Notifications
You must be signed in to change notification settings - Fork 213
feat: add display_map tool for point maps on OpenStreetMap #1206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b4e02cd
feat: add display_map tool for point maps on OpenStreetMap
tckaros d410b38
add theme handler + include display_map tool in whatsapp
socallmebertille 7956732
fix from cubic review
socallmebertille 4742e36
chore: sync package-lock with maplibre-gl dependency
socallmebertille File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { buildMapPoints, MAX_MAP_POINTS, resolveColumnName } from '@nao/shared'; | ||
| import { displayMap } from '@nao/shared/tools'; | ||
|
|
||
| import { DisplayMapOutput, renderToModelOutput } from '../../components/tool-outputs'; | ||
| import { getQueryResult } from '../../services/query-result.service'; | ||
| import { createTool } from '../../utils/tools'; | ||
|
|
||
| export default createTool<displayMap.Input, displayMap.Output>({ | ||
| description: | ||
| 'Display an interactive map of geographic data from a previous `execute_sql` tool call. The query result must contain latitude and longitude columns in decimal degrees.', | ||
| inputSchema: displayMap.InputSchema, | ||
| outputSchema: displayMap.OutputSchema, | ||
|
|
||
| execute: async (input, context) => { | ||
| const queryResult = await getQueryResult(context, input.query_id); | ||
| if (!queryResult) { | ||
| return { | ||
| _version: '1', | ||
| success: false, | ||
| error: `Query result "${input.query_id}" not found. Run \`execute_sql\` first and use the id from its output.`, | ||
| }; | ||
| } | ||
|
|
||
| const latitudeColumn = resolveColumnName(queryResult.columns, input.latitude_key); | ||
| const longitudeColumn = resolveColumnName(queryResult.columns, input.longitude_key); | ||
| const missingColumn = [latitudeColumn, longitudeColumn].find((column) => !queryResult.columns.includes(column)); | ||
| if (missingColumn) { | ||
| return { | ||
| _version: '1', | ||
| success: false, | ||
| error: `Column "${missingColumn}" not found in the query result. Available columns: ${queryResult.columns.join(', ')}.`, | ||
| }; | ||
| } | ||
| if (latitudeColumn === longitudeColumn) { | ||
| return { | ||
| _version: '1', | ||
| success: false, | ||
| error: 'latitude_key and longitude_key must reference different columns.', | ||
| }; | ||
| } | ||
|
|
||
| const points = buildMapPoints(queryResult.data, { | ||
| ...input, | ||
| latitude_key: latitudeColumn, | ||
| longitude_key: longitudeColumn, | ||
| }); | ||
| if (points.length === 0) { | ||
| return { | ||
| _version: '1', | ||
| success: false, | ||
| error: 'The query result contains no rows with valid decimal-degree coordinates, so there is nothing to plot.', | ||
| }; | ||
| } | ||
|
|
||
| const warnings: string[] = []; | ||
| const missingPopupKeys = [input.label_key, ...(input.tooltip_keys ?? [])] | ||
| .filter((key): key is string => !!key) | ||
| .filter((key) => !queryResult.columns.includes(resolveColumnName(queryResult.columns, key))); | ||
| if (missingPopupKeys.length > 0) { | ||
| warnings.push( | ||
| `Popup column(s) ${missingPopupKeys.map((key) => `"${key}"`).join(', ')} not found in the query result, so popups will not show them. Available columns: ${queryResult.columns.join(', ')}.`, | ||
| ); | ||
| } | ||
| if (points.length > MAX_MAP_POINTS) { | ||
| warnings.push( | ||
| `The map only renders the first ${MAX_MAP_POINTS} points — mention this to the user, or aggregate in SQL to stay under the limit.`, | ||
| ); | ||
| } | ||
|
|
||
| return { | ||
| _version: '1', | ||
| success: true, | ||
| point_count: points.length, | ||
| dropped_row_count: queryResult.data.length - points.length, | ||
| ...(warnings.length > 0 && { warning: warnings.join(' ') }), | ||
| }; | ||
| }, | ||
|
|
||
| toModelOutput: ({ output }) => renderToModelOutput(DisplayMapOutput({ output }), output), | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import type { displayMap } from '@nao/shared/tools'; | ||
|
|
||
| import { Block } from '../../lib/markdown'; | ||
|
|
||
| export function DisplayMapOutput({ output }: { output: displayMap.Output }) { | ||
| if (output.error) { | ||
| return <Block>Could not display the map: {output.error}</Block>; | ||
| } | ||
| const points = output.point_count != null ? ` with ${output.point_count} point(s)` : ''; | ||
| const dropped = output.dropped_row_count | ||
| ? ` ${output.dropped_row_count} row(s) were dropped for missing or invalid coordinates — adjust the SQL if they should appear.` | ||
| : ''; | ||
| const warning = output.warning ? ` Warning: ${output.warning}` : ''; | ||
| return ( | ||
| <Block> | ||
| Map displayed successfully{points}.{dropped} | ||
| {warning} | ||
| </Block> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.