Skip to content

Commit 1e17f91

Browse files
committed
Fix handling of past departure and arrival times in GoogleRoutesAPI
1 parent 591c95d commit 1e17f91

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

libs/langchain-community/src/tools/google_routes.ts

+23-18
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ function createTravelInstructions(stepsOverview: any): travelInstructions[] {
125125
return stepsOverview.multiModalSegments.map((segment: any) => ({
126126
...(segment.navigationInstruction
127127
? {
128-
navigationInstruction: segment.navigationInstruction.instructions,
129-
}
128+
navigationInstruction: segment.navigationInstruction.instructions,
129+
}
130130
: {}),
131131
travelMode: segment.travelMode,
132132
}));
@@ -290,9 +290,6 @@ export class GoogleRoutesAPI extends StructuredTool {
290290
Expected departure time should be provided as a timestamp in RFC3339 format: YYYY-MM-DDThh:mm:ss+00:00. The date should be in UTC time and the +00:00 represents the UTC offset.
291291
For instance, if the the user's timezone is -5, the offset would be -05:00 meaning YYYY-MM-DDThh:mm:ss-05:00 with YYYY-MM-DDThh:mm:ss being in UTC.
292292
For reference, here is the current time in UTC: ${new Date().toISOString()} and the user's timezone offset is ${getTimezoneOffsetInHours()}.
293-
Reminder that the departure time must be in the future.
294-
If the user asks for a departure time in the past, warn them that it is not possible.
295-
If the user asks for a departure time in a passed hour today, calculate it for the next day.
296293
If the departure time is not specified it should not be included.
297294
`
298295
),
@@ -305,34 +302,26 @@ export class GoogleRoutesAPI extends StructuredTool {
305302
Expected arrival time should be provided as a timestamp in RFC3339 format: YYYY-MM-DDThh:mm:ss+00:00. The date should be in UTC time and the +00:00 represents the UTC offset.
306303
For instance, if the the user's timezone is -5, the offset would be -05:00 meaning YYYY-MM-DDThh:mm:ss-05:00 with YYYY-MM-DDThh:mm:ss being in UTC.
307304
For reference, here is the current time in UTC: ${new Date().toISOString()} and the user's timezone offset is ${getTimezoneOffsetInHours()}.
308-
Reminder that the arrival time must be in the future, if the user asks for a departure time in the past, warn them that it is not possible.
309-
If the user asks for a departure time in a passed hour today, calculate it for the next day.
310-
Arrival time only works for transit mode. It should not be included if transit mode is not selected.
311-
If the user wants to calculate the arrival time for other modes you must warn them that it is not possible.
305+
Reminder that the arrival time must be in the future, if the user asks for a arrival time in the past instead of processing the request, warn them that it is not possible to calculate a route for a past time.
306+
If the user asks for a arrival time in a passed hour today, calculate it for the next day.
312307
If the arrival time is not specified it should not be included. `
313308
),
314309
transitPreferences: z
315310
.object({
316311
routingPreference: z.enum(["LESS_WALKING", "FEWER_TRANSFERS"])
317312
.describe(`Transit routing preference.
318-
Only works for transit mode.
319-
It should not be included if transit mode is not selected.
320313
By default, it should not be included.`),
321314
})
322315
.optional()
323316
.describe(
324317
`Transit routing preference.
325-
Only works for transit mode.
326-
It should not be included if transit mode is not selected.
327318
By default, it should not be included.`
328319
),
329320
extraComputations: z
330321
.array(z.enum(["TOLLS"]))
331322
.optional()
332323
.describe(
333-
`Calculate tolls for the route.
334-
Does not work for transit mode.
335-
It should not be included if transit mode is selected, if the user wants to calculate tolls for transit mode you should warn them that it is not possible.`
324+
`Calculate tolls for the route.`
336325
),
337326
});
338327
}
@@ -349,7 +338,23 @@ export class GoogleRoutesAPI extends StructuredTool {
349338
extraComputations,
350339
} = input;
351340

352-
console.log("input:", input);
341+
const now = new Date();
342+
343+
if (departureTime && new Date(departureTime) < now) {
344+
return "It is not possible to calculate a route with a past departure time. Warn the user that it is not possible to calculate a route with a past departure time.";
345+
}
346+
347+
if (arrivalTime && new Date(arrivalTime) < now) {
348+
return "It is not possible to calculate a route with a past arrival time. Warn the user that it is not possible to calculate a route with a past arrival time.";
349+
}
350+
351+
if (travel_mode !== "TRANSIT" && arrivalTime) {
352+
return "It is not possible to calculate an arrival time for modes other than transit. Warn the user that it is not possible to calculate an arrival time for the selected mode of transport.";
353+
}
354+
355+
if (travel_mode === "TRANSIT" && extraComputations) {
356+
return "It is not possible to calculate tolls for transit mode. Warn the user that it is not possible to calculate tolls for transit mode.";
357+
}
353358

354359
const body: Body = {
355360
origin: {
@@ -413,7 +418,7 @@ export class GoogleRoutesAPI extends StructuredTool {
413418
// eslint-disable-next-line @typescript-eslint/no-explicit-any
414419
(route: any) => filterRoutes(route, travel_mode)
415420
);
416-
console.log("output:", routes);
421+
417422
return JSON.stringify(routes);
418423
}
419424
}

0 commit comments

Comments
 (0)