-
-
Couldn't load subscription status.
- Fork 6
feat: Create GeoJSON Enrichment Agent #312
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
Open
ngoiyaeric
wants to merge
2
commits into
main
Choose a base branch
from
feat/geojson-enrichment-agent
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
Binary file not shown.
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,28 @@ | ||
| 'use client'; | ||
|
|
||
| import { useEffect } from 'react'; | ||
| import { useMapData } from './map-data-context'; | ||
| import { LocationResponse } from '@/lib/types/custom'; | ||
|
|
||
| interface LocationResponseHandlerProps { | ||
| locationResponse: LocationResponse; | ||
| } | ||
|
|
||
| export const LocationResponseHandler: React.FC<LocationResponseHandlerProps> = ({ locationResponse }) => { | ||
| const { setMapData } = useMapData(); | ||
|
|
||
| useEffect(() => { | ||
| if (locationResponse) { | ||
| const { geojson, map_commands } = locationResponse; | ||
| console.log('LocationResponseHandler: Received data', locationResponse); | ||
| setMapData(prevData => ({ | ||
| ...prevData, | ||
| geojson: geojson, | ||
| mapCommands: map_commands, | ||
| })); | ||
| } | ||
| }, [locationResponse, setMapData]); | ||
|
|
||
| // This component handles logic and does not render any UI. | ||
| return null; | ||
| }; |
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,75 @@ | ||
| import { CoreMessage, LanguageModel, streamText } from 'ai'; | ||
| import { getModel }from '../utils'; | ||
| import { LocationResponse } from '../types/custom'; | ||
|
|
||
| // A specialized prompt instructing the LLM to parse a textual response | ||
| // and extract structured GeoJSON data and map commands. | ||
| const GEOJSON_ENRICHMENT_PROMPT = ` | ||
| You are an AI assistant specializing in geospatial data extraction. | ||
| Your task is to process a given text and extract the following information: | ||
|
|
||
| 1. "text": The original textual response that should be displayed to the user. | ||
| 2. "geojson": A valid GeoJSON FeatureCollection representing any locations, addresses, coordinates, or routes mentioned in the text. | ||
| 3. "map_commands": A list of map camera commands to control the map view, such as flying to a location. | ||
|
|
||
| Rules for GeoJSON: | ||
| - Convert all found locations into appropriate GeoJSON features (Point, LineString). | ||
| - Use the correct coordinate format: [Longitude, Latitude] in WGS84. | ||
| - Include meaningful properties for each feature (e.g., "name", "description"). | ||
| - If no geographic data can be extracted, set "geojson" to null. | ||
|
|
||
| Rules for Map Commands: | ||
| - Identify actions in thetext that imply map movements (e.g., "fly to," "center on," "zoom to"). | ||
| - Create a list of command objects, for example: { "command": "flyTo", "params": { "center": [-71.05633, 42.356823], "zoom": 15 } }. | ||
| - If no map commands can be inferred, set "map_commands" to null. | ||
|
|
||
| The final output MUST be a single JSON object that strictly follows the LocationResponse interface. | ||
| Return ONLY the raw JSON object with no surrounding markdown, code fences, or any additional text or explanation. | ||
|
|
||
| Here is the text to process: | ||
| `; | ||
|
|
||
| /** | ||
| * An asynchronous agent that enriches a textual response with GeoJSON data and map commands. | ||
| * @param researcherResponse The text generated by the researcher agent. | ||
| * @returns A promise that resolves to a LocationResponse object. | ||
| */ | ||
| export async function geojsonEnricher( | ||
| researcherResponse: string | ||
| ): Promise<LocationResponse> { | ||
| const model = getModel() as LanguageModel; | ||
| const messages: CoreMessage[] = [ | ||
| { | ||
| role: 'user', | ||
| content: `${GEOJSON_ENRICHMENT_PROMPT}\n\n${researcherResponse}`, | ||
| }, | ||
| ]; | ||
|
|
||
| try { | ||
| const { text } = await streamText({ | ||
| model, | ||
| messages, | ||
| maxTokens: 2048, | ||
| }); | ||
|
|
||
| // Assuming the LLM returns a valid JSON string, parse it. | ||
| let responseText = await text; | ||
|
|
||
| // Strip markdown code fences if present | ||
| const jsonMatch = responseText.match(/```(json)?\n([\s\S]*?)\n```/); | ||
| if (jsonMatch && jsonMatch[2]) { | ||
| responseText = jsonMatch[2].trim(); | ||
| } | ||
|
|
||
| const enrichedData = JSON.parse(responseText) as LocationResponse; | ||
| return enrichedData; | ||
| } catch (error) { | ||
| console.error('Error enriching response with GeoJSON:', error); | ||
| // If parsing fails, return a default response that includes the original text. | ||
| return { | ||
| text: researcherResponse, | ||
| geojson: null, | ||
| map_commands: null, | ||
| }; | ||
| } | ||
| } | ||
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,33 @@ | ||
| // Defines the structure for a map command, like 'flyTo' or 'easeTo'. | ||
| export interface MapCommand { | ||
| command: 'flyTo' | 'easeTo' | 'fitBounds'; // Add other valid map commands as needed | ||
| params: any; // Parameters for the command, e.g., { center: [lon, lat], zoom: 10 } | ||
| } | ||
|
|
||
| // Defines the structure for the geometry part of a GeoJSON feature. | ||
| export interface GeoJSONGeometry { | ||
| type: 'Point' | 'LineString' | 'Polygon'; // Can be extended with other GeoJSON geometry types | ||
| coordinates: number[] | number[][] | number[][][]; | ||
| } | ||
|
|
||
| // Defines a single feature in a GeoJSON FeatureCollection. | ||
| export interface GeoJSONFeature { | ||
| type: 'Feature'; | ||
| geometry: GeoJSONGeometry; | ||
| properties: { | ||
| [key: string]: any; // Features can have any number of properties | ||
| }; | ||
| } | ||
|
|
||
| // Defines the structure for a GeoJSON FeatureCollection. | ||
| export interface GeoJSONFeatureCollection { | ||
| type: 'FeatureCollection'; | ||
| features: GeoJSONFeature[]; | ||
| } | ||
|
|
||
| // Defines the structured response that includes textual data, GeoJSON, and map commands. | ||
| export interface LocationResponse { | ||
| text: string; | ||
| geojson: GeoJSONFeatureCollection | null; | ||
| map_commands?: MapCommand[] | null; | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix import spacing to restore build.
The statement currently reads
import { getModel }from '../utils';which is invalid syntax and matches the build failure reported by CI. Insert the missing space:📝 Committable suggestion
🤖 Prompt for AI Agents